diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2018-12-12 16:57:40 +0100 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2019-01-10 08:59:45 +0100 |
commit | 3a77ea6ff77558e3182510ef0f90000039b29bfe (patch) | |
tree | af6e1c180f3721dc6c0c63ff84e5d525bcab98bc /platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseTest.java | |
parent | 4be73454e64c498723abddf462026ecdb9d4ef94 (diff) | |
download | qtlocation-mapboxgl-3a77ea6ff77558e3182510ef0f90000039b29bfe.tar.gz |
[android] - espresso tests rework
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseTest.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseTest.java | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseTest.java new file mode 100644 index 0000000000..ea69f8adae --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/activity/BaseTest.java @@ -0,0 +1,116 @@ +package com.mapbox.mapboxsdk.testapp.activity; + +import android.app.Activity; +import android.support.test.espresso.Espresso; +import android.support.test.espresso.IdlingRegistry; +import android.support.test.espresso.IdlingResource; +import android.support.test.espresso.IdlingResourceTimeoutException; +import android.support.test.espresso.ViewInteraction; +import android.support.test.rule.ActivityTestRule; + +import com.mapbox.mapboxsdk.Mapbox; +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.FinishLoadingStyleIdlingResource; + +import com.mapbox.mapboxsdk.testapp.utils.MapboxIdlingResource; +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; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; +import static android.support.test.espresso.matcher.ViewMatchers.withId; + +/** + * Base class for all Activity test hooking into an existing Activity that will load style. + */ +public abstract class BaseTest { + + @Rule + public ActivityTestRule<Activity> rule = new ActivityTestRule<>(getActivityClass()); + + @Rule + public TestName testNameRule = new TestName(); + + protected MapboxMap mapboxMap; + protected MapboxIdlingResource idlingResource; + + @Before + public void beforeTest() { + try { + Timber.e(String.format( + "%s - %s - %s", + getClass().getSimpleName(), + testNameRule.getMethodName(), + "@Before test: register idle resource" + )); + idlingResource = (MapboxIdlingResource) generateIdlingResource(); + IdlingRegistry.getInstance().register(idlingResource); + Espresso.onIdle(); + mapboxMap = idlingResource.getMapboxMap(); + } catch (IdlingResourceTimeoutException idlingResourceTimeoutException) { + throw new RuntimeException( + String.format( + "Could not start %s test for %s.", + testNameRule.getMethodName(), + getActivityClass().getSimpleName() + ) + ); + } + } + + protected IdlingResource generateIdlingResource() { + return new FinishLoadingStyleIdlingResource(rule.getActivity()); + } + + protected void validateTestSetup() { + if (!Mapbox.isConnected()) { + Timber.e("Not connected to the internet while running test"); + } + + checkViewIsDisplayed(R.id.mapView); + Assert.assertNotNull(mapboxMap); + } + + protected MapboxMap getMapboxMap() { + return mapboxMap; + } + + protected abstract Class getActivityClass(); + + protected void checkViewIsDisplayed(int id) { + onView(withId(id)).check(matches(isDisplayed())); + } + + protected void waitAction() { + waitAction(500); + } + + protected void waitAction(long waitTime) { + onView(withId(R.id.mapView)).perform(new WaitAction(waitTime)); + } + + 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(String.format("%s - %s", testNameRule.getMethodName(), "@After test: unregister idle resource")); + IdlingRegistry.getInstance().unregister(idlingResource); + } +} + |