1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
package com.mapbox.mapboxsdk.testapp.maps
import android.graphics.Bitmap
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import com.mapbox.mapboxsdk.AppCenter
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.maps.MapView
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.testapp.R
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
@RunWith(AndroidJUnit4::class)
class RemoveUnusedImagesTest : AppCenter() {
@Rule
@JvmField
var rule = ActivityTestRule(EspressoTestActivity::class.java)
private lateinit var mapView: MapView
private lateinit var mapboxMap: MapboxMap
private val latch = CountDownLatch(1)
@Before
fun setup() {
rule.runOnUiThread {
mapView = rule.activity.findViewById(R.id.mapView)
mapView.getMapAsync {
mapboxMap = it
mapboxMap.setStyle(Style.Builder().fromJson(styleJson))
}
}
}
@Test
fun testRemoveUnusedImagesUserProvidedListener() {
var callbackLatch = CountDownLatch(2)
rule.runOnUiThread {
mapView.addOnStyleImageMissingListener {
mapboxMap.style!!.addImage(it, Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888))
}
// Remove layer and source, so that rendered tiles are no longer used, therefore, map must
// notify client about unused images.
mapView.addOnDidBecomeIdleListener {
mapboxMap.style!!.removeLayer("icon")
mapboxMap.style!!.removeSource("geojson")
}
mapView.addOnCanRemoveUnusedStyleImageListener {
callbackLatch.countDown()
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(0.0, 120.0), 8.0))
mapView.addOnDidFinishRenderingFrameListener {
assertNotNull(mapboxMap.style!!.getImage("small"))
assertNotNull(mapboxMap.style!!.getImage("large"))
latch.countDown()
}
return@addOnCanRemoveUnusedStyleImageListener false
}
}
if (!latch.await(5, TimeUnit.SECONDS) && !callbackLatch.await(5, TimeUnit.SECONDS)) {
throw TimeoutException()
}
}
@Test
fun testRemoveUnusedImagesDefaultListener() {
rule.runOnUiThread {
mapView.addOnStyleImageMissingListener {
mapboxMap.style!!.addImage(it, Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888))
}
// Remove layer and source, so that rendered tiles are no longer used, thus
// map must request removal of unused images.
mapView.addOnDidBecomeIdleListener {
mapboxMap.style!!.removeLayer("icon")
mapboxMap.style!!.removeSource("geojson")
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(0.0, 120.0), 8.0))
// Wait for the next frame and check that images were removed from the style.
mapView.addOnDidFinishRenderingFrameListener {
if (mapboxMap.style!!.getImage("small") == null && mapboxMap.style!!.getImage("large") == null) {
latch.countDown()
}
}
}
}
if (!latch.await(5, TimeUnit.SECONDS)) {
throw TimeoutException()
}
}
companion object {
private const val styleJson = """
{
"version": 8,
"name": "Mapbox Streets",
"sources": {
"geojson": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"image": "small"
},
"geometry": {
"type": "Point",
"coordinates": [
0,
0
]
}
},
{
"type": "Feature",
"properties": {
"image": "large"
},
"geometry": {
"type": "Point",
"coordinates": [
1,
1
]
}
}
]
}
}
},
"layers": [{
"id": "bg",
"type": "background",
"paint": {
"background-color": "#f00"
}
},{
"id": "icon",
"type": "symbol",
"source": "geojson",
"layout": {
"icon-image": ["get", "image"]
}
}]
}
"""
}
}
|