From 6ce38ccc6f49d6411e59cdc082ea941fe3e7b9c9 Mon Sep 17 00:00:00 2001 From: tobrun Date: Tue, 2 Jan 2018 12:15:18 +0100 Subject: [PATCH] [android] - add orientation change regression test --- .../mapboxsdk/maps/OrientationTest.java | 41 ++++++++++ .../action/OrientationChangeAction.java | 74 +++++++++++++++++++ .../testapp/activity/BaseActivityTest.java | 10 +-- 3 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/OrientationTest.java create mode 100644 platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/action/OrientationChangeAction.java diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/OrientationTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/OrientationTest.java new file mode 100644 index 00000000000..7a1fcbf5f3a --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/OrientationTest.java @@ -0,0 +1,41 @@ +package com.mapbox.mapboxsdk.maps; + +import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest; +import com.mapbox.mapboxsdk.testapp.activity.camera.CameraAnimationTypeActivity; +import org.junit.Test; + +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.matcher.ViewMatchers.isRoot; +import static com.mapbox.mapboxsdk.testapp.action.OrientationChangeAction.orientationLandscape; +import static com.mapbox.mapboxsdk.testapp.action.OrientationChangeAction.orientationLandscapeReverse; +import static com.mapbox.mapboxsdk.testapp.action.OrientationChangeAction.orientationPortrait; +import static com.mapbox.mapboxsdk.testapp.action.OrientationChangeAction.orientationPortraitReverse; + +public class OrientationTest extends BaseActivityTest { + + @Test + public void testChangeDeviceOrientation() { + onView(isRoot()).perform(orientationLandscape()); + waitLoop(2200); + onView(isRoot()).perform(orientationPortrait()); + waitLoop(2500); + onView(isRoot()).perform(orientationLandscapeReverse()); + waitLoop(500); + onView(isRoot()).perform(orientationPortraitReverse()); + waitLoop(1250); + onView(isRoot()).perform(orientationLandscape()); + waitLoop(750); + onView(isRoot()).perform(orientationPortrait()); + waitLoop(950); + onView(isRoot()).perform(orientationLandscapeReverse()); + onView(isRoot()).perform(orientationPortraitReverse()); + onView(isRoot()).perform(orientationLandscape()); + onView(isRoot()).perform(orientationPortrait()); + } + + @Override + protected Class getActivityClass() { + return CameraAnimationTypeActivity.class; + } + +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/action/OrientationChangeAction.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/action/OrientationChangeAction.java new file mode 100644 index 00000000000..7f73d6a7f3d --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/action/OrientationChangeAction.java @@ -0,0 +1,74 @@ +package com.mapbox.mapboxsdk.testapp.action; + + +import android.app.Activity; +import android.content.Context; +import android.content.ContextWrapper; +import android.content.pm.ActivityInfo; +import android.support.test.espresso.UiController; +import android.support.test.espresso.ViewAction; +import android.view.View; +import android.view.ViewGroup; +import org.hamcrest.Matcher; + +import static android.support.test.espresso.matcher.ViewMatchers.isRoot; + +public class OrientationChangeAction implements ViewAction { + + private final int orientation; + + private OrientationChangeAction(int orientation) { + this.orientation = orientation; + } + + public static ViewAction orientationLandscape() { + return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); + } + + public static ViewAction orientationPortrait() { + return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } + + public static ViewAction orientationLandscapeReverse() { + return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); + } + + public static ViewAction orientationPortraitReverse() { + return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); + } + + @Override + public Matcher getConstraints() { + return isRoot(); + } + + @Override + public String getDescription() { + return "change orientation to " + orientation; + } + + @Override + public void perform(UiController uiController, View view) { + uiController.loopMainThreadUntilIdle(); + Activity activity = getActivity(view.getContext()); + if (activity == null && view instanceof ViewGroup) { + ViewGroup v = (ViewGroup) view; + int c = v.getChildCount(); + for (int i = 0; i < c && activity == null; ++i) { + activity = getActivity(v.getChildAt(i).getContext()); + } + } + activity.setRequestedOrientation(orientation); + } + + private Activity getActivity(Context context) { + while (context instanceof ContextWrapper) { + if (context instanceof Activity) { + return (Activity) context; + } + context = ((ContextWrapper) context).getBaseContext(); + } + return null; + } + +} 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 61bff1f1130..3f324430218 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 @@ -10,18 +10,14 @@ import android.support.test.espresso.ViewAction; import android.support.test.rule.ActivityTestRule; import android.view.View; - import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.testapp.R; import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource; - import junit.framework.Assert; - import org.hamcrest.Matcher; import org.junit.After; import org.junit.Before; import org.junit.Rule; - import timber.log.Timber; import static android.support.test.espresso.Espresso.onView; @@ -72,7 +68,11 @@ protected void checkViewIsDisplayed(int id) { } protected void waitLoop() { - onView(withId(R.id.mapView)).perform(new LoopAction(500)); + waitLoop(500); + } + + protected void waitLoop(long waitTime) { + onView(withId(R.id.mapView)).perform(new LoopAction(waitTime)); } static boolean isConnected(Context context) {