From 465316b4cef4b0d4630d855a789f5b6acef2084e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Mon, 3 Sep 2018 21:51:43 +0200 Subject: [android] adapt LocationLayerPlugin's test suite --- .../plugins/locationlayer/LocationLayer.java | 4 - .../plugins/locationlayer/LocationLayerPlugin.java | 26 +- .../plugins/locationlayer/StaleStateManager.java | 34 +- .../locationlayer/LocationLayerPluginTest.kt | 860 +++++++++++---------- .../plugins/locationlayer/LocationLayerTest.kt | 177 ++--- .../mapboxsdk/plugins/utils/GenericPluginAction.kt | 48 -- .../plugins/utils/LocationLayerPluginAction.kt | 40 + .../mapboxsdk/plugins/utils/MapboxTestingUtils.kt | 30 +- .../plugins/utils/PluginGenerationUtil.kt | 51 -- .../mapboxsdk/plugins/utils/TestLifecycleOwner.kt | 19 - .../mapboxsdk/testapp/action/MapboxMapAction.java | 3 +- .../testapp/activity/BaseActivityTest.java | 19 +- .../mapboxsdk/testapp/activity/SingleActivity.java | 4 + .../src/debug/res/layout/activity_single.xml | 2 +- 14 files changed, 665 insertions(+), 652 deletions(-) delete mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/GenericPluginAction.kt create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/LocationLayerPluginAction.kt delete mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/PluginGenerationUtil.kt delete mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/TestLifecycleOwner.kt diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java index 0e7ce37a07..67cd26d9cc 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayer.java @@ -346,10 +346,6 @@ final class LocationLayer implements PluginAnimator.OnLayerAnimationsValuesChang } void setLocationsStale(boolean isStale) { - // If options has stale state disabled, just return here. - if (!options.enableStaleState()) { - return; - } locationFeature.addBooleanProperty(PROPERTY_LOCATION_STALE, isStale); refreshSource(); if (renderMode != RenderMode.GPS) { diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java index 988f01dce8..45a13ed58b 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPlugin.java @@ -114,7 +114,9 @@ public final class LocationLayerPlugin { = new CopyOnWriteArrayList<>(); /** - * Construct a LocationLayerPlugin + * Construct a LocationLayerPlugin. In order to display location, + * the location layer has to be activated with {@link LocationLayerPlugin#activateLocationLayerPlugin(Context)}, + * or one of the overloads. * * @param mapboxMap the MapboxMap to apply the LocationLayerPlugin with */ @@ -151,6 +153,22 @@ public final class LocationLayerPlugin { .mapbox_LocationLayer)); } + /** + * This method will show the location icon and enable the camera tracking the location. + *

+ * Note: This constructor will initialize and use an internal {@link LocationEngine}. + * + * @param context the context + */ + @RequiresPermission(anyOf = {ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION}) + public void activateLocationLayerPlugin(@NonNull Context context, boolean useDefaultLocationEngine) { + if (useDefaultLocationEngine) { + activateLocationLayerPlugin(context, R.style.mapbox_LocationLayer); + } else { + activateLocationLayerPlugin(context, null, R.style.mapbox_LocationLayer); + } + } + /** * This method will show the location icon and enable the camera tracking the location. *

@@ -312,9 +330,7 @@ public final class LocationLayerPlugin { public void applyStyle(LocationLayerOptions options) { this.options = options; locationLayer.applyStyle(options); - if (!options.enableStaleState()) { - staleStateManager.onStop(); - } + staleStateManager.setEnabled(options.enableStaleState()); staleStateManager.setDelayTime(options.staleStateTimeout()); updateMapWithOptions(options); } @@ -740,7 +756,7 @@ public final class LocationLayerPlugin { SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); compassEngine = new LocationLayerCompassEngine(windowManager, sensorManager); compassEngine.addCompassListener(compassListener); - staleStateManager = new StaleStateManager(onLocationStaleListener, options.staleStateTimeout()); + staleStateManager = new StaleStateManager(onLocationStaleListener, options); updateMapWithOptions(options); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateManager.java index 5c180b5c47..9b6afd2d93 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateManager.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/StaleStateManager.java @@ -10,35 +10,42 @@ import android.os.Handler; */ class StaleStateManager { + private boolean isEnabled; private final OnLocationStaleListener innerOnLocationStaleListeners; private final Handler handler; private boolean isStale = true; private long delayTime; - StaleStateManager(OnLocationStaleListener innerListener, long delayTime) { + StaleStateManager(OnLocationStaleListener innerListener, LocationLayerOptions options) { innerOnLocationStaleListeners = innerListener; - this.delayTime = delayTime; handler = new Handler(); - innerOnLocationStaleListeners.onStaleStateChange(true); + isEnabled = options.enableStaleState(); + delayTime = options.staleStateTimeout(); } private Runnable staleStateRunnable = new Runnable() { @Override public void run() { - isStale = true; - innerOnLocationStaleListeners.onStaleStateChange(true); + setState(true); } }; + void setEnabled(boolean enabled) { + if (enabled) { + setState(isStale); + } else if (isEnabled) { + onStop(); + innerOnLocationStaleListeners.onStaleStateChange(false); + } + isEnabled = enabled; + } + boolean isStale() { return isStale; } void updateLatestLocationTime() { - if (isStale) { - isStale = false; - innerOnLocationStaleListeners.onStaleStateChange(false); - } + setState(false); postTheCallback(); } @@ -61,4 +68,13 @@ class StaleStateManager { handler.removeCallbacksAndMessages(null); handler.postDelayed(staleStateRunnable, delayTime); } + + private void setState(boolean stale) { + if (stale != isStale) { + isStale = stale; + if (isEnabled) { + innerOnLocationStaleListeners.onStaleStateChange(stale); + } + } + } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPluginTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPluginTest.kt index 12a64850b0..1820d56d95 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPluginTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerPluginTest.kt @@ -2,7 +2,6 @@ package com.mapbox.mapboxsdk.plugins.locationlayer import android.Manifest import android.R -import android.arch.lifecycle.Lifecycle import android.content.Context import android.graphics.Color import android.graphics.RectF @@ -12,93 +11,69 @@ import android.support.test.espresso.IdlingRegistry import android.support.test.espresso.UiController import android.support.test.espresso.assertion.ViewAssertions.matches import android.support.test.espresso.matcher.ViewMatchers.* -import android.support.test.filters.LargeTest -import android.support.test.rule.ActivityTestRule import android.support.test.rule.GrantPermissionRule -import android.support.test.runner.AndroidJUnit4 import android.support.v4.content.ContextCompat import com.mapbox.geojson.Point -import com.mapbox.mapboxsdk.camera.CameraPosition import com.mapbox.mapboxsdk.camera.CameraUpdateFactory import com.mapbox.mapboxsdk.constants.Style import com.mapbox.mapboxsdk.geometry.LatLng -import com.mapbox.mapboxsdk.maps.MapView import com.mapbox.mapboxsdk.maps.MapboxMap -import com.mapbox.mapboxsdk.maps.MapboxMapOptions -import com.mapbox.mapboxsdk.maps.SupportMapFragment import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.* import com.mapbox.mapboxsdk.plugins.locationlayer.modes.CameraMode import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode import com.mapbox.mapboxsdk.plugins.utils.* import com.mapbox.mapboxsdk.plugins.utils.MapboxTestingUtils.Companion.MAPBOX_HEAVY_STYLE +import com.mapbox.mapboxsdk.plugins.utils.MapboxTestingUtils.Companion.MAP_CONNECTION_DELAY +import com.mapbox.mapboxsdk.plugins.utils.MapboxTestingUtils.Companion.MAP_RENDER_DELAY import com.mapbox.mapboxsdk.plugins.utils.MapboxTestingUtils.Companion.pushSourceUpdates -import com.mapbox.mapboxsdk.plugins.utils.PluginGenerationUtil.Companion.MAP_CONNECTION_DELAY -import com.mapbox.mapboxsdk.plugins.utils.PluginGenerationUtil.Companion.MAP_RENDER_DELAY -import com.mapbox.mapboxsdk.style.layers.PropertyFactory -import com.mapbox.mapboxsdk.style.sources.GeoJsonSource -import com.mapbox.mapboxsdk.testapp.activity.SingleFragmentActivity +import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest +import com.mapbox.mapboxsdk.testapp.activity.SingleActivity +import com.mapbox.mapboxsdk.utils.ColorUtils import org.hamcrest.CoreMatchers.* import org.junit.* import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue -import org.junit.rules.TestName -import org.junit.runner.RunWith -import timber.log.Timber -/** - * Test class that uses a map fragment to keep onMapReady actions isolated to within the test - */ -@RunWith(AndroidJUnit4::class) -@LargeTest -class LocationLayerPluginTest { - - @Rule - @JvmField - val activityRule = ActivityTestRule(SingleFragmentActivity::class.java, true, true) - - @Rule - @JvmField - val nameRule = TestName() +class LocationLayerPluginTest : BaseActivityTest() { @Rule @JvmField val permissionRule: GrantPermissionRule = GrantPermissionRule.grant(Manifest.permission.ACCESS_FINE_LOCATION) - private lateinit var idlingResource: OnMapFragmentReadyIdlingResource + override fun getActivityClass(): Class<*> { + return SingleActivity::class.java + } + private lateinit var styleChangeIdlingResource: StyleChangeIdlingResource - private lateinit var fragment: SupportMapFragment - private lateinit var mapboxMap: MapboxMap private val location: Location by lazy { val initLocation = Location("test") initLocation.latitude = 15.0 initLocation.longitude = 17.0 + initLocation.bearing = 10f + initLocation.accuracy = 150f initLocation } @Before - fun beforeTest() { + override fun beforeTest() { + super.beforeTest() - // Create a default support map fragment and pass it into the empty activity - val options = MapboxMapOptions() - .camera(CameraPosition.Builder().zoom(2.0).build()) // to match plugins min zoom - fragment = SupportMapFragment.newInstance(options) - activityRule.activity.setFragment(fragment) + location.latitude = 15.0 + location.longitude = 17.0 + location.bearing = 10f + location.accuracy = 150f - Timber.e("@Before: ${nameRule.methodName} - register idle resource") - // If idlingResource is null, throw Kotlin exception - idlingResource = OnMapFragmentReadyIdlingResource(fragment) styleChangeIdlingResource = StyleChangeIdlingResource() - IdlingRegistry.getInstance().register(idlingResource) IdlingRegistry.getInstance().register(styleChangeIdlingResource) - onView(withId(R.id.content)).check(matches(isDisplayed())) - mapboxMap = idlingResource.mapboxMap } @Test fun locationLayerPlugin_initializesLocationEngineCorrectlyWhenOnesNotProvided() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + validateTestSetup() + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context) val locationEngine = plugin.locationEngine assertThat(locationEngine, notNullValue()) @@ -108,14 +83,23 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, true)) + executePluginTest(pluginAction) } @Test fun locationLayerPlugin_initializesLocationEngineCorrectlyWhenOnesNotProvidedButHasOptions() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + validateTestSetup() + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin( + context, + LocationLayerOptions.builder(context) + .staleStateTimeout(200) + .enableStaleState(false) + .accuracyAlpha(.5f) + .accuracyColor(Color.BLUE) + .build()) val locationEngine = plugin.locationEngine val pluginOptions = plugin.locationLayerOptions @@ -130,24 +114,49 @@ class LocationLayerPluginTest { } } - val options = LocationLayerOptions.builder(fragment.activity) - .staleStateTimeout(200) - .enableStaleState(false) - .accuracyAlpha(.5f) - .accuracyColor(Color.BLUE) - .build() - - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider( - activityRule.activity, true, null, options)) + executePluginTest(pluginAction) } @Test - fun settingMapStyleImmediatelyBeforeLoadingPlugin_doesStillLoadLayersProperly() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + fun locationLayerPlugin_doesntInitializeEngineWhenNullProvided() { + validateTestSetup() + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin( + null, + LocationLayerOptions.builder(context) + .staleStateTimeout(200) + .enableStaleState(false) + .accuracyAlpha(.5f) + .accuracyColor(Color.BLUE) + .build()) + + val locationEngine = plugin.locationEngine + val pluginOptions = plugin.locationLayerOptions + + assertThat(locationEngine, nullValue()) + assertThat(pluginOptions, notNullValue()) uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) + assertThat(pluginOptions?.accuracyAlpha(), `is`(.5f)) + assertThat(pluginOptions?.accuracyColor(), `is`(Color.BLUE)) + } + } + + executePluginTest(pluginAction) + } + + @Test + fun settingMapStyleImmediatelyBeforeLoadingPlugin_doesStillLoadLayersProperly() { + validateTestSetup() + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + mapboxMap.setStyle(Style.LIGHT) + plugin.activateLocationLayerPlugin(context, false) + plugin.forceLocationUpdate(location) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(plugin.renderMode, `is`(equalTo(RenderMode.NORMAL))) assertThat(mapboxMap.isLayerVisible(FOREGROUND_LAYER), `is`(true)) @@ -158,39 +167,26 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, object : GenericPluginAction.PluginProvider { - override fun providePlugin(mapView: MapView, mapboxMap: MapboxMap, context: Context): LocationLayerPlugin { - // changing the style just before instantiating the plugin - mapboxMap.setStyleUrl(Style.LIGHT) - val plugin = - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false) - .providePlugin(mapView, mapboxMap, context) - plugin.forceLocationUpdate(location) - return plugin - } - - override fun isPluginDataReady(plugin: LocationLayerPlugin, mapboxMap: MapboxMap): Boolean { - val source = mapboxMap.getSource(LOCATION_SOURCE) - return source != null && (source as GeoJsonSource).querySourceFeatures(null).isNotEmpty() - } - }) + executePluginTest(pluginAction) } @Test fun locationLayer_doesntShowUntilFirstLocationFix() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + validateTestSetup() + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) // Source should be present but empty - val mapView = fragment.view as MapView + val mapView = (rule.activity as SingleActivity).mapView assertThat(mapboxMap.queryRenderedFeatures( RectF(0f, 0f, mapView.width.toFloat(), mapView.height.toFloat()), FOREGROUND_LAYER) .isEmpty(), `is`(true)) // Force the first location update plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) // Check if the puck is visible assertThat(mapboxMap.queryRenderedFeatures(location, FOREGROUND_LAYER).isEmpty(), `is`(false)) @@ -205,11 +201,18 @@ class LocationLayerPluginTest { @Test fun locationLayerOptions_disablingStaleStateDoesWorkCorrectly() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + validateTestSetup() + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, + LocationLayerOptions.builder(context) + .staleStateTimeout(200) + .enableStaleState(false) + .build()) plugin.forceLocationUpdate(location) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) uiController.loopMainThreadForAtLeast(200) uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) @@ -221,28 +224,34 @@ class LocationLayerPluginTest { } } - val options = LocationLayerOptions.builder(fragment.activity) - .staleStateTimeout(200) - .enableStaleState(false) - .build() - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, options)) + executePluginTest(pluginAction) } @Test fun locationLayerOptions_loadsForegroundBitmapFromNameOption() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, + LocationLayerOptions.builder(context) + .foregroundName("custom-foreground-bitmap") + .backgroundName("custom-background-bitmap") + .foregroundStaleName("custom-foreground-stale-bitmap") + .backgroundStaleName("custom-background-stale-bitmap") + .bearingName("custom-bearing-bitmap") + .build()) + val foregroundDrawable = ContextCompat.getDrawable(context, R.drawable.ic_media_play) - mapboxMap.addImageFromDrawable("custom-foreground-bitmap", foregroundDrawable!!) - mapboxMap.addImageFromDrawable("custom-background-bitmap", foregroundDrawable) - mapboxMap.addImageFromDrawable("custom-foreground-stale-bitmap", foregroundDrawable) - mapboxMap.addImageFromDrawable("custom-background-stale-bitmap", foregroundDrawable) - mapboxMap.addImageFromDrawable("custom-bearing-bitmap", foregroundDrawable) + foregroundDrawable?.let { + mapboxMap.addImageFromDrawable("custom-foreground-bitmap", it) + mapboxMap.addImageFromDrawable("custom-background-bitmap", it) + mapboxMap.addImageFromDrawable("custom-foreground-stale-bitmap", it) + mapboxMap.addImageFromDrawable("custom-background-stale-bitmap", it) + mapboxMap.addImageFromDrawable("custom-bearing-bitmap", it) + } plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.queryRenderedFeatures(location, FOREGROUND_LAYER).isEmpty(), `is`(false)) val feature = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0] @@ -254,55 +263,56 @@ class LocationLayerPluginTest { } } - val options = LocationLayerOptions.builder(fragment.activity) - .foregroundName("custom-foreground-bitmap") - .backgroundName("custom-background-bitmap") - .foregroundStaleName("custom-foreground-stale-bitmap") - .backgroundStaleName("custom-background-stale-bitmap") - .bearingName("custom-bearing-bitmap") - .build() - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, options)) + executePluginTest(pluginAction) } @Test fun locationLayerOptions_loadsGpsNameWithGpsRenderMode() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, + LocationLayerOptions.builder(context) + .foregroundName("custom-foreground-bitmap") + .gpsName("custom-gps-bitmap") + .build()) + plugin.renderMode = RenderMode.GPS plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) val foregroundDrawable = ContextCompat.getDrawable(context, R.drawable.ic_media_play) - mapboxMap.addImageFromDrawable("custom-foreground-bitmap", foregroundDrawable!!) - mapboxMap.addImageFromDrawable("custom-gps-bitmap", foregroundDrawable) + foregroundDrawable?.let { + mapboxMap.addImageFromDrawable("custom-foreground-bitmap", it) + mapboxMap.addImageFromDrawable("custom-gps-bitmap", it) + } val foregroundId = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getStringProperty(PROPERTY_FOREGROUND_ICON) assertThat(foregroundId, `is`(equalTo("custom-gps-bitmap"))) } } - val options = LocationLayerOptions.builder(fragment.activity) - .foregroundName("custom-foreground-bitmap") - .gpsName("custom-gps-bitmap") - .build() - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, options)) + executePluginTest(pluginAction) } @Test fun locationLayerOptions_customIconNameRevertsToDefault() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, + LocationLayerOptions.builder(context) + .foregroundName("custom-foreground-bitmap") + .gpsName("custom-gps-bitmap") + .build()) + plugin.renderMode = RenderMode.GPS plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) val foregroundId = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getStringProperty(PROPERTY_FOREGROUND_ICON) assertThat(foregroundId, `is`(equalTo("custom-gps-bitmap"))) - plugin.applyStyle(LocationLayerOptions.builder(fragment.activity).build()) + plugin.applyStyle(LocationLayerOptions.builder(context).build()) uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) val revertedForegroundId = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getStringProperty(PROPERTY_FOREGROUND_ICON) @@ -310,22 +320,23 @@ class LocationLayerPluginTest { } } - val options = LocationLayerOptions.builder(fragment.activity) - .foregroundName("custom-foreground-bitmap") - .gpsName("custom-gps-bitmap") - .build() - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, options)) + executePluginTest(pluginAction) } @Test fun locationLayerOptions_customGpsIconNameChangeBackWithMode() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + + plugin.activateLocationLayerPlugin(context, + LocationLayerOptions.builder(context) + .gpsName("custom-gps-bitmap") + .build()) + plugin.renderMode = RenderMode.GPS plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) val foregroundId = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getStringProperty(PROPERTY_FOREGROUND_ICON) assertThat(foregroundId, `is`(equalTo("custom-gps-bitmap"))) @@ -338,75 +349,73 @@ class LocationLayerPluginTest { } } - val options = LocationLayerOptions.builder(fragment.activity) - .gpsName("custom-gps-bitmap") - .build() - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, options)) + executePluginTest(pluginAction) } @Test fun stillStaleAfterResuming() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, + LocationLayerOptions.builder(context) + .staleStateTimeout(200) + .build()) plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(300) // engaging stale state + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) + uiController.loopMainThreadForAtLeast(250) // engaging stale state uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE), `is`(true)) - testLifecycleOwner.markState(Lifecycle.State.CREATED) - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + plugin.onStop() + plugin.onStart() + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE), `is`(true)) assertThat(mapboxMap.isLayerVisible(ACCURACY_LAYER), `is`(false)) } } - val options = LocationLayerOptions.builder(fragment.activity) - .staleStateTimeout(200) - .build() - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, options, false)) + + executePluginTest(pluginAction) } @Test fun stillNotStaleAfterResuming() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) - + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE), `is`(false)) - testLifecycleOwner.markState(Lifecycle.State.CREATED) - testLifecycleOwner.markState(Lifecycle.State.RESUMED) + plugin.onStop() + plugin.onStart() uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE), `is`(false)) assertThat(mapboxMap.isLayerVisible(ACCURACY_LAYER), `is`(true)) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun locationLayerOptions_accuracyRingWithColor() { val color = Color.parseColor("#4A90E2") - val rgbaColor = PropertyFactory.colorToRgbaString(color) + val rgbaColor = ColorUtils.colorToRgbaString(color) - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, + LocationLayerOptions.builder(context) + .accuracyColor(color) + .build()) + + plugin.forceLocationUpdate(location) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) // Check that the source property changes correctly mapboxMap.querySourceFeatures(LOCATION_SOURCE).also { @@ -417,22 +426,17 @@ class LocationLayerPluginTest { } } - val options = LocationLayerOptions.builder(fragment.activity) - .accuracyColor(color) - .build() - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, options)) + executePluginTest(pluginAction) } @Test fun forceLocationUpdate_doesMoveLocationLayerIconToCorrectPosition() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.forceLocationUpdate(location) - - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) val point: Point = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].geometry() as Point @@ -446,19 +450,20 @@ class LocationLayerPluginTest { @Test fun disablingPluginHidesPuck() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) - val point: Point = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].geometry() as Point + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) + + val point: Point = mapboxMap.queryRenderedFeatures(location, FOREGROUND_LAYER)[0].geometry() as Point assertEquals(point.latitude(), location.latitude, 0.1) assertEquals(point.longitude(), location.longitude, 0.1) - plugin.isLocationLayerEnabled = false + plugin.deactivateLocationLayerPlugin() uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) - assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE).isEmpty(), `is`(true)) + assertThat(mapboxMap.queryRenderedFeatures(location, FOREGROUND_LAYER).isEmpty(), `is`(true)) } } executePluginTest(pluginAction) @@ -466,19 +471,18 @@ class LocationLayerPluginTest { @Test fun disablingPluginAndChangingStyleAllowsToEnableAgain() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) - plugin.isLocationLayerEnabled = false - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + plugin.deactivateLocationLayerPlugin() mapboxMap.setStyle(Style.LIGHT) - uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) - plugin.isLocationLayerEnabled = true - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + + plugin.activateLocationLayerPlugin(context, false) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.isLayerVisible(FOREGROUND_LAYER), `is`(true)) } } @@ -486,148 +490,131 @@ class LocationLayerPluginTest { } @Test - fun lifecycle_keepsEnabledWhenStoppedAndStarted() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) + fun lifecycle_isDisabledOnStart() { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + assertThat(plugin.isLocationLayerEnabled, `is`(false)) + plugin.onStop() + plugin.onStart() + assertThat(plugin.isLocationLayerEnabled, `is`(false)) + plugin.activateLocationLayerPlugin(context, false) + assertThat(plugin.isLocationLayerEnabled, `is`(true)) + } + } + executePluginTest(pluginAction) + } + @Test + fun lifecycle_keepsEnabledWhenStoppedAndStarted() { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) assertThat(plugin.isLocationLayerEnabled, `is`(true)) - testLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE) - testLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_STOP) - testLifecycleOwner.markState(Lifecycle.State.RESUMED) + plugin.onStop() + plugin.onStart() assertThat(plugin.isLocationLayerEnabled, `is`(true)) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false)) + executePluginTest(pluginAction) } @Test fun lifecycle_keepsDisabledWhenStoppedAndStarted() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - plugin.isLocationLayerEnabled = false - - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) - + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) + plugin.deactivateLocationLayerPlugin() assertThat(plugin.isLocationLayerEnabled, `is`(false)) - testLifecycleOwner.markState(Lifecycle.State.CREATED) - testLifecycleOwner.markState(Lifecycle.State.RESUMED) + plugin.onStop() + plugin.onStart() assertThat(plugin.isLocationLayerEnabled, `is`(false)) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false)) + executePluginTest(pluginAction) } @Test fun lifecycle_ableToChangeStyleAfterResuming() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) - testLifecycleOwner.markState(Lifecycle.State.CREATED) - testLifecycleOwner.markState(Lifecycle.State.RESUMED) + plugin.onStop() + plugin.onStart() mapboxMap.setStyle(Style.DARK) uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false)) + executePluginTest(pluginAction) } @Test fun lifecycle_interruptedDuringStyleChange() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) mapboxMap.setStyle(Style.DARK) - - testLifecycleOwner.markState(Lifecycle.State.CREATED) - testLifecycleOwner.markState(Lifecycle.State.RESUMED) + plugin.onStop() + plugin.onStart() uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false)) + executePluginTest(pluginAction) } @Test fun lifecycle_forceLocationUpdateAfterStopped() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) - - testLifecycleOwner.markState(Lifecycle.State.CREATED) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) + plugin.onStop() plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE).isEmpty(), `is`(true)) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false)) + executePluginTest(pluginAction) } @Test fun lifecycle_acceptAndReuseLocationUpdatesBeforeLayerStarted() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) - - testLifecycleOwner.markState(Lifecycle.State.CREATED) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) + plugin.onStop() plugin.forceLocationUpdate(location) - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + plugin.onStart() + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) val point: Point = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].geometry() as Point assertEquals(point.latitude(), location.latitude, 0.1) assertEquals(point.longitude(), location.longitude, 0.1) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false)) + executePluginTest(pluginAction) } @Test fun lifecycle_lifecycleChangeRightAfterStyleReload() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) - + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.forceLocationUpdate(location) mapboxMap.setStyle(Style.LIGHT) - testLifecycleOwner.markState(Lifecycle.State.CREATED) + plugin.onStop() uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + plugin.onStart() + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) val point: Point = mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].geometry() as Point assertEquals(point.latitude(), location.latitude, 0.1) @@ -640,17 +627,17 @@ class LocationLayerPluginTest { assertThat(mapboxMap.isLayerVisible(BEARING_LAYER), `is`(false)) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false)) + executePluginTest(pluginAction) } @Test fun mapChange_settingPluginStyle() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - styleChangeIdlingResource.waitForStyle(fragment.view as MapView, mapboxMap, MAPBOX_HEAVY_STYLE) - val options = LocationLayerOptions.builder(fragment.activity) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) + styleChangeIdlingResource.waitForStyle((rule.activity as SingleActivity).mapView, mapboxMap, MAPBOX_HEAVY_STYLE) + val options = LocationLayerOptions.builder(context) .accuracyColor(Color.RED) .build() @@ -661,8 +648,7 @@ class LocationLayerPluginTest { uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) // Waiting for style to finish loading while pushing updates onView(withId(R.id.content)).check(matches(isDisplayed())) @@ -670,10 +656,11 @@ class LocationLayerPluginTest { @Test fun mapChange_forcingLocation() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - styleChangeIdlingResource.waitForStyle(fragment.view as MapView, mapboxMap, MAPBOX_HEAVY_STYLE) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) + styleChangeIdlingResource.waitForStyle((rule.activity as SingleActivity).mapView, mapboxMap, MAPBOX_HEAVY_STYLE) pushSourceUpdates(styleChangeIdlingResource) { plugin.forceLocationUpdate(location) @@ -682,8 +669,7 @@ class LocationLayerPluginTest { uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) // Waiting for style to finish loading while pushing updates onView(withId(R.id.content)).check(matches(isDisplayed())) @@ -691,10 +677,13 @@ class LocationLayerPluginTest { @Test fun mapChange_settingMapStyleBeforePluginCreation() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - val options = LocationLayerOptions.builder(fragment.activity) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + styleChangeIdlingResource.waitForStyle((rule.activity as SingleActivity).mapView, mapboxMap, MAPBOX_HEAVY_STYLE) + plugin.activateLocationLayerPlugin(context, false) + + val options = LocationLayerOptions.builder(context) .accuracyColor(Color.RED) .build() @@ -704,19 +693,7 @@ class LocationLayerPluginTest { } } } - - executePluginTest(pluginAction, object : GenericPluginAction.PluginProvider { - override fun providePlugin(mapView: MapView, mapboxMap: MapboxMap, context: Context): LocationLayerPlugin { - // changing the style just before instantiating the plugin - styleChangeIdlingResource.waitForStyle(mapView, mapboxMap, MAPBOX_HEAVY_STYLE) - return PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false) - .providePlugin(mapView, mapboxMap, context) - } - - override fun isPluginDataReady(plugin: LocationLayerPlugin, mapboxMap: MapboxMap): Boolean { - return true - } - }) + executePluginTest(pluginAction) // Waiting for style to finish loading while pushing updates onView(withId(R.id.content)).check(matches(isDisplayed())) @@ -724,34 +701,36 @@ class LocationLayerPluginTest { @Test fun animators_layerBearingCorrect() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.renderMode = RenderMode.GPS location.bearing = 77f plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(1000) + uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS + MAP_RENDER_DELAY) assertEquals(77.0, mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getNumberProperty(PROPERTY_GPS_BEARING) as Double, 0.1) location.bearing = 92f plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(2000) // Waiting for the animation to finish + uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS + MAP_RENDER_DELAY) // Waiting for the animation to finish assertEquals(92.0, mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getNumberProperty(PROPERTY_GPS_BEARING) as Double, 0.1) } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_cameraLatLngBearingCorrect() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING_GPS location.bearing = 77f plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(1000) + uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS + MAP_RENDER_DELAY) assertEquals(77.0, mapboxMap.cameraPosition.bearing, 0.1) assertEquals(location.latitude, mapboxMap.cameraPosition.target.latitude, 0.1) assertEquals(location.longitude, mapboxMap.cameraPosition.target.longitude, 0.1) @@ -760,28 +739,29 @@ class LocationLayerPluginTest { location.latitude = 30.0 location.longitude = 35.0 plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(2000) // Waiting for the animation to finish + uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS + MAP_RENDER_DELAY) // Waiting for the animation to finish assertEquals(92.0, mapboxMap.cameraPosition.bearing, 0.1) assertEquals(location.latitude, mapboxMap.cameraPosition.target.latitude, 0.1) assertEquals(location.longitude, mapboxMap.cameraPosition.target.longitude, 0.1) } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_cameraBearingCorrect() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.NONE_GPS val latitude = mapboxMap.cameraPosition.target.latitude val longitude = mapboxMap.cameraPosition.target.longitude location.bearing = 77f plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(1000) + uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS + MAP_RENDER_DELAY) assertEquals(77.0, mapboxMap.cameraPosition.bearing, 0.1) assertEquals(latitude, mapboxMap.cameraPosition.target.latitude, 0.1) assertEquals(longitude, mapboxMap.cameraPosition.target.longitude, 0.1) @@ -790,21 +770,22 @@ class LocationLayerPluginTest { location.latitude = 30.0 location.longitude = 35.0 plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(2000) // Waiting for the animation to finish + uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS + MAP_RENDER_DELAY) assertEquals(92.0, mapboxMap.cameraPosition.bearing, 0.1) assertEquals(latitude, mapboxMap.cameraPosition.target.latitude, 0.1) assertEquals(longitude, mapboxMap.cameraPosition.target.longitude, 0.1) } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_cameraNoneCorrect() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.NONE val latitude = mapboxMap.cameraPosition.target.latitude val longitude = mapboxMap.cameraPosition.target.longitude @@ -812,7 +793,7 @@ class LocationLayerPluginTest { location.bearing = 77f plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(1000) + uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS + MAP_RENDER_DELAY) assertEquals(bearing, mapboxMap.cameraPosition.bearing, 0.1) assertEquals(latitude, mapboxMap.cameraPosition.target.latitude, 0.1) assertEquals(longitude, mapboxMap.cameraPosition.target.longitude, 0.1) @@ -821,38 +802,40 @@ class LocationLayerPluginTest { location.latitude = 30.0 location.longitude = 35.0 plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(2000) // Waiting for the animation to finish + uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS + MAP_RENDER_DELAY) // Waiting for the animation to finish assertEquals(bearing, mapboxMap.cameraPosition.bearing, 0.1) assertEquals(latitude, mapboxMap.cameraPosition.target.latitude, 0.1) assertEquals(longitude, mapboxMap.cameraPosition.target.longitude, 0.1) } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_focalPointAdjustment() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING plugin.cameraMode = CameraMode.NONE plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.uiSettings.focalPoint, nullValue()) } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_dontZoomWhileNotTracking() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.NONE val zoom = mapboxMap.cameraPosition.zoom plugin.zoomWhileTracking(10.0) @@ -862,14 +845,15 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_zoomWhileTracking() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING plugin.zoomWhileTracking(10.0) uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION) @@ -878,15 +862,16 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test @Ignore fun animators_zoomWhileTrackingCanceledOnModeChange() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING plugin.zoomWhileTracking(15.0) uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION / 2) @@ -897,24 +882,21 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_dontZoomWhileStopped() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) val zoom = mapboxMap.cameraPosition.zoom - testLifecycleOwner.markState(Lifecycle.State.CREATED) + plugin.onStop() plugin.zoomWhileTracking(10.0) uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION) @@ -922,16 +904,16 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false)) + executePluginTest(pluginAction) } @Test @Ignore fun animators_cancelZoomWhileTracking() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING plugin.zoomWhileTracking(15.0) uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_ZOOM_ANIMATION_DURATION / 2) @@ -942,14 +924,15 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_dontTiltWhileNotTracking() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.NONE val tilt = mapboxMap.cameraPosition.tilt plugin.tiltWhileTracking(30.0) @@ -959,14 +942,15 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_tiltWhileTracking() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING plugin.tiltWhileTracking(30.0) uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_TILT_ANIMATION_DURATION) @@ -975,15 +959,16 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test @Ignore fun animators_tiltWhileTrackingCanceledOnModeChange() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING plugin.tiltWhileTracking(30.0) uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_TILT_ANIMATION_DURATION / 2) @@ -994,23 +979,19 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun animators_dontTiltWhileStopped() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - - val testLifecycleOwner = TestLifecycleOwner() - testLifecycleOwner.markState(Lifecycle.State.RESUMED) - testLifecycleOwner.lifecycle.addObserver(plugin) - + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING val tilt = mapboxMap.cameraPosition.tilt - testLifecycleOwner.markState(Lifecycle.State.CREATED) + plugin.onStop() plugin.tiltWhileTracking(30.0) uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_TILT_ANIMATION_DURATION) @@ -1018,16 +999,16 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, - PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity, false, null, null, false)) + executePluginTest(pluginAction) } @Test @Ignore fun animators_cancelTiltWhileTracking() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING plugin.tiltWhileTracking(30.0) uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_TILT_ANIMATION_DURATION / 2) @@ -1038,21 +1019,22 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test fun cameraPositionAdjustedToTrackingModeWhenPluginEnabled() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.cameraMode = CameraMode.TRACKING_GPS plugin.forceLocationUpdate(location) - plugin.isLocationLayerEnabled = false + plugin.deactivateLocationLayerPlugin() mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(LatLng(51.0, 17.0))) mapboxMap.moveCamera(CameraUpdateFactory.bearingTo(90.0)) - plugin.isLocationLayerEnabled = true - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + plugin.activateLocationLayerPlugin(context, false) + uiController.loopMainThreadForAtLeast(MAX_ANIMATION_DURATION_MS + MAP_RENDER_DELAY) assertEquals(location.bearing.toDouble(), mapboxMap.cameraPosition.bearing, 0.1) assertEquals(location.latitude, mapboxMap.cameraPosition.target.latitude, 0.1) @@ -1060,30 +1042,66 @@ class LocationLayerPluginTest { } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) } @Test - fun onPluginInitialized_defaultCompassEngineIsProvided() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + fun compassEngine_onPluginInitializedDefaultIsProvided() { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) assertTrue(plugin.compassEngine is LocationLayerCompassEngine) } } - executePluginTest(pluginAction, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) + executePluginTest(pluginAction) + } + + @Test + fun compassEngine_changesWhenNewProvided() { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) + val engine: CompassEngine = object : CompassEngine { + override fun addCompassListener(compassListener: CompassListener) { + } + + override fun removeCompassListener(compassListener: CompassListener) { + } + + override fun getLastHeading(): Float { + return 0f + } + + override fun getLastAccuracySensorStatus(): Int { + return 0 + } + + override fun onStart() { + } + + override fun onStop() { + } + } + + plugin.compassEngine = engine + assertThat(plugin.compassEngine, notNullValue()) + assertThat(plugin.compassEngine, `is`(equalTo(engine))) + } + } + + executePluginTest(pluginAction) } @After - fun afterTest() { - Timber.e("@After: ${nameRule.methodName} - unregister idle resource") - IdlingRegistry.getInstance().unregister(idlingResource) + override fun afterTest() { + super.afterTest() IdlingRegistry.getInstance().unregister(styleChangeIdlingResource) } - private fun executePluginTest(listener: GenericPluginAction.OnPerformGenericPluginAction, - pluginProvider: GenericPluginAction.PluginProvider = PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity)) { - onView(withId(R.id.content)).perform(GenericPluginAction(fragment.view as MapView, mapboxMap, pluginProvider, listener)) + private fun executePluginTest(listener: LocationLayerPluginAction.OnPerformLocationLayerPluginAction) { + onView(withId(R.id.content)).perform(LocationLayerPluginAction(mapboxMap, listener)) } } \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTest.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTest.kt index 121c8f2d22..d7823031fc 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTest.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerTest.kt @@ -10,11 +10,8 @@ import android.support.test.espresso.UiController import android.support.test.espresso.assertion.ViewAssertions.matches import android.support.test.espresso.matcher.ViewMatchers.isDisplayed import android.support.test.espresso.matcher.ViewMatchers.withId -import android.support.test.filters.LargeTest -import android.support.test.rule.ActivityTestRule import android.support.test.rule.GrantPermissionRule import android.support.test.rule.GrantPermissionRule.grant -import android.support.test.runner.AndroidJUnit4 import com.mapbox.mapboxsdk.camera.CameraUpdateFactory import com.mapbox.mapboxsdk.constants.Style import com.mapbox.mapboxsdk.geometry.LatLng @@ -23,10 +20,11 @@ import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.* import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode import com.mapbox.mapboxsdk.plugins.utils.* import com.mapbox.mapboxsdk.plugins.utils.MapboxTestingUtils.Companion.MAPBOX_HEAVY_STYLE +import com.mapbox.mapboxsdk.plugins.utils.MapboxTestingUtils.Companion.MAP_CONNECTION_DELAY +import com.mapbox.mapboxsdk.plugins.utils.MapboxTestingUtils.Companion.MAP_RENDER_DELAY import com.mapbox.mapboxsdk.plugins.utils.MapboxTestingUtils.Companion.pushSourceUpdates -import com.mapbox.mapboxsdk.plugins.utils.PluginGenerationUtil.Companion.MAP_CONNECTION_DELAY -import com.mapbox.mapboxsdk.plugins.utils.PluginGenerationUtil.Companion.MAP_RENDER_DELAY import com.mapbox.mapboxsdk.style.sources.GeoJsonSource +import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest import com.mapbox.mapboxsdk.testapp.activity.SingleActivity import org.hamcrest.CoreMatchers.`is` import org.hamcrest.CoreMatchers.notNullValue @@ -37,47 +35,32 @@ import org.junit.Assert.assertThat import org.junit.Before import org.junit.Rule import org.junit.Test -import org.junit.rules.TestName -import org.junit.runner.RunWith -import timber.log.Timber -@RunWith(AndroidJUnit4::class) -@LargeTest -class LocationLayerTest { - - @Rule - @JvmField - val activityRule = ActivityTestRule(SingleActivity::class.java) - - @Rule - @JvmField - val nameRule = TestName() +class LocationLayerTest : BaseActivityTest() { @Rule @JvmField val permissionRule: GrantPermissionRule = grant(Manifest.permission.ACCESS_FINE_LOCATION) - private lateinit var idlingResource: OnMapReadyIdlingResource + override fun getActivityClass(): Class<*> { + return SingleActivity::class.java + } + private lateinit var styleChangeIdlingResource: StyleChangeIdlingResource - private lateinit var mapboxMap: MapboxMap private val location: Location by lazy { val initLocation = Location("test") initLocation.latitude = 15.0 initLocation.longitude = 17.0 + initLocation.bearing = 10f initLocation.accuracy = 2000f initLocation } @Before - fun beforeTest() { - Timber.e("@Before: ${nameRule.methodName} - register idle resource") - // If idlingResource is null, throw Kotlin exception - idlingResource = OnMapReadyIdlingResource(activityRule.activity) + override fun beforeTest() { + super.beforeTest() styleChangeIdlingResource = StyleChangeIdlingResource() - IdlingRegistry.getInstance().register(idlingResource) IdlingRegistry.getInstance().register(styleChangeIdlingResource) - onView(withId(android.R.id.content)).check(matches(isDisplayed())) - mapboxMap = idlingResource.mapboxMap } // @@ -86,9 +69,10 @@ class LocationLayerTest { @Test fun renderModeNormal_sourceDoesGetAdded() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.renderMode = RenderMode.NORMAL uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) assertThat(mapboxMap.getSource(LOCATION_SOURCE), notNullValue()) @@ -103,12 +87,13 @@ class LocationLayerTest { @Test fun renderModeNormal_trackingNormalLayersDoGetAdded() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.renderMode = RenderMode.NORMAL plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.isLayerVisible(FOREGROUND_LAYER), `is`(true)) assertThat(mapboxMap.isLayerVisible(BACKGROUND_LAYER), `is`(true)) assertThat(mapboxMap.isLayerVisible(SHADOW_LAYER), `is`(true)) @@ -121,12 +106,13 @@ class LocationLayerTest { @Test fun renderModeCompass_bearingLayersDoGetAdded() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.renderMode = RenderMode.COMPASS plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.isLayerVisible(FOREGROUND_LAYER), `is`(true)) assertThat(mapboxMap.isLayerVisible(BACKGROUND_LAYER), `is`(true)) assertThat(mapboxMap.isLayerVisible(SHADOW_LAYER), `is`(true)) @@ -139,12 +125,13 @@ class LocationLayerTest { @Test fun renderModeGps_navigationLayersDoGetAdded() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.renderMode = RenderMode.GPS plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.isLayerVisible(FOREGROUND_LAYER), `is`(true)) assertThat(mapboxMap.isLayerVisible(BACKGROUND_LAYER), `is`(true)) assertThat(mapboxMap.isLayerVisible(SHADOW_LAYER), `is`(false)) @@ -157,11 +144,14 @@ class LocationLayerTest { @Test fun dontShowPuckWhenRenderModeSetAndPluginDisabled() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.forceLocationUpdate(location) - plugin.isLocationLayerEnabled = false + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) + plugin.deactivateLocationLayerPlugin() + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER, shouldDisappear = true) plugin.renderMode = RenderMode.GPS uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) assertThat(mapboxMap.isLayerVisible(FOREGROUND_LAYER), `is`(false)) @@ -176,12 +166,15 @@ class LocationLayerTest { @Test fun whenLocationLayerPluginDisabled_doesSetAllLayersToVisibilityNone() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.renderMode = RenderMode.NORMAL plugin.forceLocationUpdate(location) - plugin.isLocationLayerEnabled = false + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) + plugin.deactivateLocationLayerPlugin() + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER, shouldDisappear = true) uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) // Check that all layers visibilities are set to none @@ -197,14 +190,17 @@ class LocationLayerTest { @Test fun onMapChange_locationLayerLayersDoGetRedrawn() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.renderMode = RenderMode.NORMAL plugin.forceLocationUpdate(location) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) mapboxMap.setStyleUrl(Style.LIGHT) - plugin.forceLocationUpdate(location) uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) + plugin.forceLocationUpdate(location) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(plugin.renderMode, `is`(equalTo(RenderMode.NORMAL))) @@ -229,18 +225,21 @@ class LocationLayerTest { @Test fun whenStyleChanged_continuesUsingStaleIcons() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.applyStyle(LocationLayerOptions.builder(context).staleStateTimeout(100).build()) plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(200) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) + uiController.loopMainThreadForAtLeast(150) uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE), `is`(true)) mapboxMap.setStyleUrl(Style.LIGHT) uiController.loopMainThreadForAtLeast(MAP_CONNECTION_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) assertThat(mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0].getBooleanProperty(PROPERTY_LOCATION_STALE), `is`(true)) } @@ -250,11 +249,12 @@ class LocationLayerTest { @Test fun whenStyleChanged_staleStateChanges() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.applyStyle(LocationLayerOptions.builder(context).staleStateTimeout(1).build()) - styleChangeIdlingResource.waitForStyle(idlingResource.mapView, mapboxMap, MAPBOX_HEAVY_STYLE) + styleChangeIdlingResource.waitForStyle((rule.activity as SingleActivity).mapView, mapboxMap, MAPBOX_HEAVY_STYLE) pushSourceUpdates(styleChangeIdlingResource) { plugin.forceLocationUpdate(location) } @@ -268,13 +268,17 @@ class LocationLayerTest { @Test fun whenStyleChanged_layerVisibilityUpdates() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - styleChangeIdlingResource.waitForStyle(idlingResource.mapView, mapboxMap, MAPBOX_HEAVY_STYLE) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + styleChangeIdlingResource.waitForStyle((rule.activity as SingleActivity).mapView, mapboxMap, MAPBOX_HEAVY_STYLE) var show = true pushSourceUpdates(styleChangeIdlingResource) { - plugin.isLocationLayerEnabled = show + if (show) { + plugin.activateLocationLayerPlugin(context, false) + } else { + plugin.deactivateLocationLayerPlugin() + } show = !show } @@ -289,13 +293,14 @@ class LocationLayerTest { @Test fun accuracy_visibleWithNewLocation() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(location), 16.0)) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY + ACCURACY_RADIUS_ANIMATION_DURATION) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) + uiController.loopMainThreadForAtLeast(ACCURACY_RADIUS_ANIMATION_DURATION) assertEquals(Utils.calculateZoomLevelRadius(mapboxMap, location) /*meters projected to radius on zoom 16*/, mapboxMap.querySourceFeatures(LOCATION_SOURCE)[0] @@ -307,12 +312,12 @@ class LocationLayerTest { @Test fun accuracy_visibleWhenCameraEased() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) plugin.forceLocationUpdate(location) - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) + mapboxMap.waitForLayer(uiController, location, FOREGROUND_LAYER) mapboxMap.easeCamera(CameraUpdateFactory.newLatLngZoom(LatLng(location), 16.0), 300) uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY + 300) @@ -326,9 +331,10 @@ class LocationLayerTest { @Test fun accuracy_visibleWhenCameraMoved() { - val pluginAction = object : GenericPluginAction.OnPerformGenericPluginAction { - override fun onGenericPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, - uiController: UiController, context: Context) { + val pluginAction = object : LocationLayerPluginAction.OnPerformLocationLayerPluginAction { + override fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, + uiController: UiController, context: Context) { + plugin.activateLocationLayerPlugin(context, false) uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) plugin.forceLocationUpdate(location) uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) @@ -344,13 +350,12 @@ class LocationLayerTest { } @After - fun afterTest() { - Timber.e("@After: ${nameRule.methodName} - unregister idle resource") - IdlingRegistry.getInstance().unregister(idlingResource) + override fun afterTest() { + super.afterTest() IdlingRegistry.getInstance().unregister(styleChangeIdlingResource) } - private fun executePluginTest(listener: GenericPluginAction.OnPerformGenericPluginAction) { - onView(withId(android.R.id.content)).perform(GenericPluginAction(idlingResource.mapView, mapboxMap, PluginGenerationUtil.getLocationLayerPluginProvider(activityRule.activity), listener)) + private fun executePluginTest(listener: LocationLayerPluginAction.OnPerformLocationLayerPluginAction) { + onView(withId(R.id.content)).perform(LocationLayerPluginAction(mapboxMap, listener)) } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/GenericPluginAction.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/GenericPluginAction.kt deleted file mode 100644 index 210f7b4758..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/GenericPluginAction.kt +++ /dev/null @@ -1,48 +0,0 @@ -package com.mapbox.mapboxsdk.plugins.utils - -import android.content.Context -import android.support.test.espresso.UiController -import android.support.test.espresso.ViewAction -import android.support.test.espresso.matcher.ViewMatchers.isDisplayed -import android.view.View -import com.mapbox.mapboxsdk.maps.MapView -import com.mapbox.mapboxsdk.maps.MapboxMap -import com.mapbox.mapboxsdk.plugins.utils.PluginGenerationUtil.Companion.MAP_RENDER_DELAY -import org.hamcrest.Matcher - -class GenericPluginAction(private val mapView: MapView, private val mapboxMap: MapboxMap, private val pluginProvider: PluginProvider, - private val onPerformGenericPluginAction: OnPerformGenericPluginAction) : ViewAction { - - override fun getConstraints(): Matcher { - return isDisplayed() - } - - override fun getDescription(): String { - return javaClass.simpleName - } - - override fun perform(uiController: UiController, view: View) { - val plugin = pluginProvider.providePlugin(mapView, mapboxMap, view.context) - - // ensuring that the asynchronous renderer has time to render data we want to test - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) - while (!pluginProvider.isPluginDataReady(plugin, mapboxMap)) { - uiController.loopMainThreadForAtLeast(MAP_RENDER_DELAY) - } - - onPerformGenericPluginAction.onGenericPluginAction( - plugin, - mapboxMap, - uiController, - view.context) - } - - interface OnPerformGenericPluginAction { - fun onGenericPluginAction(plugin: T, mapboxMap: MapboxMap, uiController: UiController, context: Context) - } - - interface PluginProvider { - fun providePlugin(mapView: MapView, mapboxMap: MapboxMap, context: Context): T - fun isPluginDataReady(plugin: T, mapboxMap: MapboxMap): Boolean - } -} \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/LocationLayerPluginAction.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/LocationLayerPluginAction.kt new file mode 100644 index 0000000000..af2c9adf35 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/LocationLayerPluginAction.kt @@ -0,0 +1,40 @@ +package com.mapbox.mapboxsdk.plugins.utils + +import android.content.Context +import android.support.test.espresso.UiController +import android.support.test.espresso.ViewAction +import android.support.test.espresso.matcher.ViewMatchers.isDisplayed +import android.view.View +import com.mapbox.mapboxsdk.maps.MapboxMap +import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin +import org.hamcrest.Matcher + +class LocationLayerPluginAction(private val mapboxMap: MapboxMap, + private val onPerformLocationLayerPluginAction: OnPerformLocationLayerPluginAction) : ViewAction { + + override fun getConstraints(): Matcher { + return isDisplayed() + } + + override fun getDescription(): String { + return javaClass.simpleName + } + + override fun perform(uiController: UiController, view: View) { + val plugin = mapboxMap.locationLayerPlugin + + while (mapboxMap.getSource("mapbox-location-source") == null) { + uiController.loopMainThreadForAtLeast(MapboxTestingUtils.MAP_RENDER_DELAY) + } + + onPerformLocationLayerPluginAction.onLocationLayerPluginAction( + plugin, + mapboxMap, + uiController, + view.context) + } + + interface OnPerformLocationLayerPluginAction { + fun onLocationLayerPluginAction(plugin: LocationLayerPlugin, mapboxMap: MapboxMap, uiController: UiController, context: Context) + } +} \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/MapboxTestingUtils.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/MapboxTestingUtils.kt index b65cb3278b..b77ab127d9 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/MapboxTestingUtils.kt +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/MapboxTestingUtils.kt @@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable import android.location.Location import android.os.Handler import android.os.Looper +import android.support.test.espresso.UiController import com.mapbox.geojson.Feature import com.mapbox.mapboxsdk.geometry.LatLng import com.mapbox.mapboxsdk.maps.MapboxMap @@ -14,7 +15,7 @@ import com.mapbox.mapboxsdk.style.layers.Property import com.mapbox.mapboxsdk.style.sources.GeoJsonSource fun MapboxMap.querySourceFeatures(sourceId: String): List { - return this.getSourceAs(sourceId)?.querySourceFeatures(null) as List + return this.getSourceAs(sourceId)?.querySourceFeatures(null) ?: emptyList() } fun MapboxMap.queryRenderedFeatures(location: Location, layerId: String): List { @@ -27,9 +28,34 @@ fun MapboxMap.isLayerVisible(layerId: String): Boolean { return this.getLayer(layerId)?.visibility?.value?.equals(Property.VISIBLE)!! } +fun MapboxMap.waitForSource(uiController: UiController, sourceId: String) { + var counter = 0 + val delay = MapboxTestingUtils.MAP_RENDER_DELAY + while (this.querySourceFeatures(sourceId).isEmpty() && delay * counter < MapboxTestingUtils.RENDER_TIMEOUT) { + uiController.loopMainThreadForAtLeast(delay) + counter++ + } +} + +fun MapboxMap.waitForLayer(uiController: UiController, location: Location, layerId: String, shouldDisappear: Boolean = false) { + var counter = 0 + val delay = MapboxTestingUtils.MAP_RENDER_DELAY + while ( + if (shouldDisappear) this.queryRenderedFeatures(location, layerId).isNotEmpty() else this.queryRenderedFeatures(location, layerId).isEmpty() + && delay * counter < MapboxTestingUtils.RENDER_TIMEOUT) { + uiController.loopMainThreadForAtLeast(delay) + counter++ + } +} + + class MapboxTestingUtils { companion object { + const val MAP_RENDER_DELAY = 250L + const val MAP_CONNECTION_DELAY = 1000L + const val RENDER_TIMEOUT = 3_000L + /** * Used to increase style load time for stress testing. */ @@ -71,7 +97,7 @@ fun MapboxMap.addImageFromDrawable(string: String, drawable: Drawable) { private fun getBitmapFromDrawable(drawable: Drawable): Bitmap { if (drawable is BitmapDrawable) return drawable.bitmap val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, - drawable.intrinsicHeight, Bitmap.Config.ARGB_8888) + drawable.intrinsicHeight, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) drawable.setBounds(0, 0, canvas.width, canvas.height) drawable.draw(canvas) diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/PluginGenerationUtil.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/PluginGenerationUtil.kt deleted file mode 100644 index 9049a28298..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/PluginGenerationUtil.kt +++ /dev/null @@ -1,51 +0,0 @@ -package com.mapbox.mapboxsdk.plugins.utils - -import android.content.Context -import android.support.v7.app.AppCompatActivity -import com.mapbox.android.core.location.LocationEngine -import com.mapbox.mapboxsdk.maps.MapView -import com.mapbox.mapboxsdk.maps.MapboxMap -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerOptions -import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin - -class PluginGenerationUtil { - companion object { - fun getLocationLayerPluginProvider(activity: AppCompatActivity, - useDefaultEngine: Boolean = false, - engine: LocationEngine? = null, - options: LocationLayerOptions? = null, - registerLifecycleObserver: Boolean = true) - : GenericPluginAction.PluginProvider { - return object : GenericPluginAction.PluginProvider { - override fun providePlugin(mapView: MapView, mapboxMap: MapboxMap, context: Context): LocationLayerPlugin { - val plugin = if (useDefaultEngine) { - if (options != null) { - LocationLayerPlugin(mapView, mapboxMap, options) - } else { - LocationLayerPlugin(mapView, mapboxMap) - } - } else { - if (options != null) { - LocationLayerPlugin(mapView, mapboxMap, engine, options) - } else { - LocationLayerPlugin(mapView, mapboxMap, engine) - } - } - - if (registerLifecycleObserver) { - activity.lifecycle.addObserver(plugin) - } - - return plugin - } - - override fun isPluginDataReady(plugin: LocationLayerPlugin, mapboxMap: MapboxMap): Boolean { - return mapboxMap.getSource("mapbox-location-source") != null - } - } - } - - const val MAP_RENDER_DELAY = 250L - const val MAP_CONNECTION_DELAY = 1000L - } -} \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/TestLifecycleOwner.kt b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/TestLifecycleOwner.kt deleted file mode 100644 index ccb8da17a7..0000000000 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/plugins/utils/TestLifecycleOwner.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.mapbox.mapboxsdk.plugins.utils - -import android.arch.lifecycle.Lifecycle -import android.arch.lifecycle.LifecycleOwner -import android.arch.lifecycle.LifecycleRegistry - -class TestLifecycleOwner : LifecycleOwner { - private val lifecycleRegistry = LifecycleRegistry(this) - - override fun getLifecycle() = lifecycleRegistry - - fun markState(state: Lifecycle.State) { - lifecycleRegistry.markState(state) - } - - fun handleLifecycleEvent(event: Lifecycle.Event) { - lifecycleRegistry.handleLifecycleEvent(event) - } -} \ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/action/MapboxMapAction.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/action/MapboxMapAction.java index 5e8f3ed365..926212afc8 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/action/MapboxMapAction.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/action/MapboxMapAction.java @@ -1,5 +1,6 @@ package com.mapbox.mapboxsdk.testapp.action; +import android.support.annotation.NonNull; import android.support.test.espresso.UiController; import android.support.test.espresso.ViewAction; import android.view.View; @@ -42,7 +43,7 @@ public class MapboxMapAction implements ViewAction { } public interface OnInvokeActionListener { - void onInvokeAction(UiController uiController, MapboxMap mapboxMap); + void onInvokeAction(@NonNull UiController uiController, @NonNull MapboxMap mapboxMap); } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseActivityTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseActivityTest.java index 5480aa7a1c..3682440aeb 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseActivityTest.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseActivityTest.java @@ -4,19 +4,24 @@ import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; -import android.support.test.espresso.Espresso; +import android.support.test.espresso.IdlingRegistry; import android.support.test.espresso.IdlingResourceTimeoutException; import android.support.test.espresso.ViewInteraction; import android.support.test.rule.ActivityTestRule; + import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.testapp.R; import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction; import com.mapbox.mapboxsdk.testapp.action.WaitAction; import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource; + import junit.framework.Assert; + import org.junit.After; import org.junit.Before; import org.junit.Rule; +import org.junit.rules.TestName; + import timber.log.Timber; import static android.support.test.espresso.Espresso.onView; @@ -28,15 +33,19 @@ public abstract class BaseActivityTest { @Rule public ActivityTestRule rule = new ActivityTestRule<>(getActivityClass()); + + @Rule + public TestName testNameRule = new TestName(); + protected MapboxMap mapboxMap; protected OnMapReadyIdlingResource idlingResource; @Before public void beforeTest() { try { - Timber.e("@Before test: register idle resource"); + Timber.e(String.format("%s - %s", testNameRule.getMethodName(), "@Before test: register idle resource")); idlingResource = new OnMapReadyIdlingResource(rule.getActivity()); - Espresso.registerIdlingResources(idlingResource); + IdlingRegistry.getInstance().register(idlingResource); checkViewIsDisplayed(R.id.mapView); mapboxMap = idlingResource.getMapboxMap(); } catch (IdlingResourceTimeoutException idlingResourceTimeoutException) { @@ -91,8 +100,8 @@ public abstract class BaseActivityTest { @After public void afterTest() { - Timber.e("@After test: unregister idle resource"); - Espresso.unregisterIdlingResources(idlingResource); + Timber.e(String.format("%s - %s", testNameRule.getMethodName(), "@After test: unregister idle resource")); + IdlingRegistry.getInstance().unregister(idlingResource); } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/debug/java/com/mapbox/mapboxsdk/testapp/activity/SingleActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/debug/java/com/mapbox/mapboxsdk/testapp/activity/SingleActivity.java index 3c7812f9b1..5daea88ba0 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/debug/java/com/mapbox/mapboxsdk/testapp/activity/SingleActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/debug/java/com/mapbox/mapboxsdk/testapp/activity/SingleActivity.java @@ -18,6 +18,10 @@ public class SingleActivity extends AppCompatActivity { mapView.onCreate(savedInstanceState); } + public MapView getMapView() { + return mapView; + } + @Override protected void onStart() { super.onStart(); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/debug/res/layout/activity_single.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/debug/res/layout/activity_single.xml index d7dfcf2dd0..f7e3d1f8ec 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/debug/res/layout/activity_single.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/debug/res/layout/activity_single.xml @@ -8,6 +8,6 @@ android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" - app:mapbox_cameraZoom="2.0" /> + app:mapbox_cameraZoomMin="2.0" /> \ No newline at end of file -- cgit v1.2.1