diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-05-07 14:10:17 +0300 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-05-21 06:49:17 -0700 |
commit | 00790dd022c80fafb2cc1a33df265b3438d31641 (patch) | |
tree | accfcf9f3eb18ff4bb789addf4d41e89c3dcf784 /platform | |
parent | 80dd90bd66649c1d7af28d3ce1f2d534b7b83e0b (diff) | |
download | qtlocation-mapboxgl-00790dd022c80fafb2cc1a33df265b3438d31641.tar.gz |
[android] Add unit test for onRemoveUnusedStyleImages API
Diffstat (limited to 'platform')
-rw-r--r-- | platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/RemoveUnusedImagesTest.kt | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/RemoveUnusedImagesTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/RemoveUnusedImagesTest.kt new file mode 100644 index 0000000000..d42494b3e9 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/maps/RemoveUnusedImagesTest.kt @@ -0,0 +1,162 @@ +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.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 { + + @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"] + } + }] + } + """ + } +} |