diff options
author | tobrun <tobrun.van.nuland@gmail.com> | 2018-05-25 10:15:30 +0200 |
---|---|---|
committer | tobrun <tobrun.van.nuland@gmail.com> | 2018-05-25 10:34:08 +0200 |
commit | d9de6d00fd64d8b5a0869c30d95c41115abfde86 (patch) | |
tree | 8cf5ce17fd96d5479f02b188f5edfc592274d93f | |
parent | 27876a8d41a9ea01dcdf2a713ba08df3dca07bde (diff) | |
download | qtlocation-mapboxgl-upstream/tvn-add-thread-checking.tar.gz |
[android] - fix icon test to use main thread, simplify executing on main thread for testsupstream/tvn-add-thread-checking
5 files changed, 106 insertions, 89 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/annotations/IconTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/annotations/IconTest.java index 1c259af2d0..e56ca559d4 100644 --- a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/annotations/IconTest.java +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/annotations/IconTest.java @@ -1,7 +1,6 @@ package com.mapbox.mapboxsdk.annotations; import android.graphics.Bitmap; - import org.junit.Before; import org.junit.Test; import org.mockito.Mock; diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapboxMapTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapboxMapTest.java index 38fd8491a8..1dca34b95d 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapboxMapTest.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapboxMapTest.java @@ -634,7 +634,7 @@ public class MapboxMapTest extends BaseActivityTest { })); } - private class MapboxMapAction implements ViewAction { + public class MapboxMapAction implements ViewAction { private InvokeViewAction invokeViewAction; 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 47af80cab9..5e8f3ed365 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 @@ -17,7 +17,7 @@ public class MapboxMapAction implements ViewAction { private OnInvokeActionListener invokeViewAction; private MapboxMap mapboxMap; - private MapboxMapAction(OnInvokeActionListener invokeViewAction, MapboxMap mapboxMap) { + public MapboxMapAction(OnInvokeActionListener invokeViewAction, MapboxMap mapboxMap) { this.invokeViewAction = invokeViewAction; this.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 6d90c20a46..5480aa7a1c 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 @@ -6,19 +6,17 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.support.test.espresso.Espresso; 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 timber.log.Timber; import static android.support.test.espresso.Espresso.onView; @@ -83,6 +81,14 @@ public abstract class BaseActivityTest { return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } + protected ViewInteraction onMapView() { + return onView(withId(R.id.mapView)); + } + + protected MapboxMapAction getMapboxMapAction(MapboxMapAction.OnInvokeActionListener onInvokeActionListener) { + return new MapboxMapAction(onInvokeActionListener, mapboxMap); + } + @After public void afterTest() { Timber.e("@After test: unregister idle resource"); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/annotations/IconTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/annotations/IconTest.java index 33a946d0a1..7099d81e57 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/annotations/IconTest.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/annotations/IconTest.java @@ -1,6 +1,7 @@ package com.mapbox.mapboxsdk.testapp.annotations; import android.app.Activity; +import android.support.test.espresso.UiController; import android.support.v4.content.res.ResourcesCompat; import com.mapbox.mapboxsdk.annotations.Icon; @@ -11,6 +12,7 @@ import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.IconManagerResolver; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.testapp.R; +import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction; import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest; import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity; import com.mapbox.mapboxsdk.testapp.utils.IconUtils; @@ -39,104 +41,114 @@ public class IconTest extends BaseActivityTest { } @Test - public void testEmpty() throws Exception { + public void testEmpty() { assertTrue(iconMap.isEmpty()); } @Test - public void testAddSameIconMarker() throws Exception { - Icon defaultMarker = IconFactory.getInstance(rule.getActivity()).defaultMarker(); - getMapboxMap().addMarker(new MarkerOptions().position(new LatLng())); - getMapboxMap().addMarker(new MarkerOptions().position(new LatLng(1, 1))); - assertEquals(1, iconMap.size()); - assertEquals(2, iconMap.get(defaultMarker), 0); + public void testAddSameIconMarker() { + validateTestSetup(); + onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> { + Icon defaultMarker = IconFactory.getInstance(rule.getActivity()).defaultMarker(); + mapboxMap.addMarker(new MarkerOptions().position(new LatLng())); + mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1))); + assertEquals(1, iconMap.size()); + assertEquals(2, iconMap.get(defaultMarker), 0); + })); } @Test - public void testAddDifferentIconMarker() throws Exception { - Icon icon = IconFactory.getInstance(rule.getActivity()).fromResource(R.drawable.mapbox_logo_icon); - getMapboxMap().addMarker(new MarkerOptions().icon(icon).position(new LatLng())); - getMapboxMap().addMarker(new MarkerOptions().position(new LatLng(1, 1))); - assertEquals(iconMap.size(), 2); - assertTrue(iconMap.containsKey(icon)); - assertTrue(iconMap.get(icon) == 1); + public void testAddDifferentIconMarker() { + validateTestSetup(); + onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> { + Icon icon = IconFactory.getInstance(rule.getActivity()).fromResource(R.drawable.mapbox_logo_icon); + getMapboxMap().addMarker(new MarkerOptions().icon(icon).position(new LatLng())); + getMapboxMap().addMarker(new MarkerOptions().position(new LatLng(1, 1))); + assertEquals(iconMap.size(), 2); + assertTrue(iconMap.containsKey(icon)); + assertTrue(iconMap.get(icon) == 1); + })); } @Test - public void testAddRemoveIconMarker() throws Exception { - MapboxMap mapboxMap = getMapboxMap(); - - Icon icon = IconFactory.getInstance(rule.getActivity()).fromResource(R.drawable.mapbox_logo_icon); - Marker marker = mapboxMap.addMarker(new MarkerOptions().icon(icon).position(new LatLng())); - mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1))); - assertEquals(iconMap.size(), 2); - assertTrue(iconMap.containsKey(icon)); - assertTrue(iconMap.get(icon) == 1); - - mapboxMap.removeMarker(marker); - assertEquals(iconMap.size(), 1); - assertFalse(iconMap.containsKey(icon)); + public void testAddRemoveIconMarker() { + validateTestSetup(); + onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> { + Icon icon = IconFactory.getInstance(rule.getActivity()).fromResource(R.drawable.mapbox_logo_icon); + Marker marker = mapboxMap.addMarker(new MarkerOptions().icon(icon).position(new LatLng())); + mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1))); + assertEquals(iconMap.size(), 2); + assertTrue(iconMap.containsKey(icon)); + assertTrue(iconMap.get(icon) == 1); + + mapboxMap.removeMarker(marker); + assertEquals(iconMap.size(), 1); + assertFalse(iconMap.containsKey(icon)); + })); } @Test - public void testAddRemoveDefaultMarker() throws Exception { - MapboxMap mapboxMap = getMapboxMap(); - - Marker marker = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1))); - assertEquals(iconMap.size(), 1); - - mapboxMap.removeMarker(marker); - assertEquals(iconMap.size(), 0); - - mapboxMap.addMarker(new MarkerOptions().position(new LatLng())); - assertEquals(iconMap.size(), 1); + public void testAddRemoveDefaultMarker() { + validateTestSetup(); + onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> { + Marker marker = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1))); + assertEquals(iconMap.size(), 1); + + mapboxMap.removeMarker(marker); + assertEquals(iconMap.size(), 0); + + mapboxMap.addMarker(new MarkerOptions().position(new LatLng())); + assertEquals(iconMap.size(), 1); + })); } @Test - public void testAddRemoveMany() throws Exception { - Activity activity = rule.getActivity(); - MapboxMap mapboxMap = getMapboxMap(); - IconFactory iconFactory = IconFactory.getInstance(activity); - - // add 2 default icon markers - Marker defaultMarkerOne = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1))); - Marker defaultMarkerTwo = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(2, 1))); - - // add 4 unique icon markers - mapboxMap.addMarker(new MarkerOptions() - .icon(iconFactory.fromResource(R.drawable.mapbox_logo_icon)) - .position(new LatLng(3, 1)) - ); - mapboxMap.addMarker(new MarkerOptions() - .icon(iconFactory.fromResource(R.drawable.mapbox_compass_icon)) - .position(new LatLng(4, 1)) - ); - mapboxMap.addMarker(new MarkerOptions() - .icon(IconUtils.drawableToIcon(activity, R.drawable.ic_stars, - ResourcesCompat.getColor(activity.getResources(), - R.color.blueAccent, activity.getTheme()))) - .position(new LatLng(5, 1)) - ); - mapboxMap.addMarker(new MarkerOptions() - .icon(iconFactory.fromResource(R.drawable.ic_android)) - .position(new LatLng(6, 1)) - ); - - assertEquals("Amount of icons should match 5", 5, iconMap.size()); - assertEquals("Refcounter of default marker should match 2", 2, iconMap.get(iconFactory.defaultMarker()), 0); - - mapboxMap.removeMarker(defaultMarkerOne); - - assertEquals("Amount of icons should match 5", 5, iconMap.size()); - assertEquals("Refcounter of default marker should match 1", 1, iconMap.get(iconFactory.defaultMarker()), 0); - - mapboxMap.removeMarker(defaultMarkerTwo); - - assertEquals("Amount of icons should match 4", 4, iconMap.size()); - assertNull("DefaultMarker shouldn't exist anymore", iconMap.get(iconFactory.defaultMarker())); - - mapboxMap.clear(); - assertEquals("Amount of icons should match 0", 0, iconMap.size()); + public void testAddRemoveMany() { + validateTestSetup(); + onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> { + Activity activity = rule.getActivity(); + IconFactory iconFactory = IconFactory.getInstance(activity); + + // add 2 default icon markers + Marker defaultMarkerOne = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(1, 1))); + Marker defaultMarkerTwo = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(2, 1))); + + // add 4 unique icon markers + mapboxMap.addMarker(new MarkerOptions() + .icon(iconFactory.fromResource(R.drawable.mapbox_logo_icon)) + .position(new LatLng(3, 1)) + ); + mapboxMap.addMarker(new MarkerOptions() + .icon(iconFactory.fromResource(R.drawable.mapbox_compass_icon)) + .position(new LatLng(4, 1)) + ); + mapboxMap.addMarker(new MarkerOptions() + .icon(IconUtils.drawableToIcon(activity, R.drawable.ic_stars, + ResourcesCompat.getColor(activity.getResources(), + R.color.blueAccent, activity.getTheme()))) + .position(new LatLng(5, 1)) + ); + mapboxMap.addMarker(new MarkerOptions() + .icon(iconFactory.fromResource(R.drawable.ic_android)) + .position(new LatLng(6, 1)) + ); + + assertEquals("Amount of icons should match 5", 5, iconMap.size()); + assertEquals("Refcounter of default marker should match 2", 2, iconMap.get(iconFactory.defaultMarker()), 0); + + mapboxMap.removeMarker(defaultMarkerOne); + + assertEquals("Amount of icons should match 5", 5, iconMap.size()); + assertEquals("Refcounter of default marker should match 1", 1, iconMap.get(iconFactory.defaultMarker()), 0); + + mapboxMap.removeMarker(defaultMarkerTwo); + + assertEquals("Amount of icons should match 4", 4, iconMap.size()); + assertNull("DefaultMarker shouldn't exist anymore", iconMap.get(iconFactory.defaultMarker())); + + mapboxMap.clear(); + assertEquals("Amount of icons should match 0", 0, iconMap.size()); + })); } @Override |