From 602a075333861160ead82067fda8f568deec3112 Mon Sep 17 00:00:00 2001 From: Antonio Zugaldia Date: Wed, 16 Sep 2015 11:03:10 -0400 Subject: [PATCH] using the new version way of instanstiating the activity class --- .../com/mapbox/mapboxgl/testapp/BaseTest.java | 66 +++++-------------- .../mapboxgl/testapp/MainActivityTest.java | 37 ++++++----- 2 files changed, 36 insertions(+), 67 deletions(-) diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/BaseTest.java b/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/BaseTest.java index 60ac10f14..9c6646105 100644 --- a/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/BaseTest.java +++ b/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/BaseTest.java @@ -1,5 +1,6 @@ package com.mapbox.mapboxgl.testapp; +import android.app.Activity; import android.support.test.InstrumentationRegistry; import android.test.ActivityInstrumentationTestCase2; @@ -7,66 +8,31 @@ import com.mapbox.mapboxgl.testapp.utils.ScreenshotUtil; import org.junit.Before; +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 Espresso class for all tests, helps working with ActivityInstrumentationTestCase2 */ -public class BaseTest - extends ActivityInstrumentationTestCase2 { +public class BaseTest { - private MainActivity mActivity; - private final static boolean screenshotOnTearDown = false; + /* + * Shortcuts for common UI tests + */ - public BaseTest() { - super(MainActivity.class); + protected void checkViewIsDisplayed(int id) { + onView(withId(id)) + .check(matches(isDisplayed())); } /* - * Get the activity before running every test + * Screenshots logic */ - @Before - public void setUp() throws Exception { - super.setUp(); - injectInstrumentation(InstrumentationRegistry.getInstrumentation()); - mActivity = getActivity(); - } - - /* - * Take a screenshot after every test - */ - - @Override - protected void tearDown() throws Exception { - if (screenshotOnTearDown) { - try { - // Screenshots should be taken on the UI thread - runTestOnUiThread(new Runnable() { - @Override - public void run() { - ScreenshotUtil.take(getActivity(), null); - } - }); - } catch (Throwable throwable) { - throwable.printStackTrace(); - } - } - - super.tearDown(); - mActivity = null; - } - - protected void takeNamedScreenshot(final String name) { - try { - // Screenshots should be taken on the UI thread - runTestOnUiThread(new Runnable() { - @Override - public void run() { - ScreenshotUtil.take(getActivity(), name); - } - }); - } catch (Throwable throwable) { - throwable.printStackTrace(); - } + protected void takeNamedScreenshot(Activity activity, String name) { + ScreenshotUtil.take(activity, name); } } diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/MainActivityTest.java b/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/MainActivityTest.java index 86f501b13..b880176fe 100644 --- a/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/MainActivityTest.java +++ b/android/java/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxgl/testapp/MainActivityTest.java @@ -1,8 +1,13 @@ package com.mapbox.mapboxgl.testapp; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.LargeTest; +import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; @@ -22,11 +27,23 @@ import static org.hamcrest.Matchers.not; /** * Tests on MainActivity */ +@RunWith(AndroidJUnit4.class) @LargeTest public class MainActivityTest extends BaseTest { private final static String HOME_BUTTON_STRING = "Navigate up"; + @Rule + public ActivityTestRule mActivityRule = new ActivityTestRule<>( + MainActivity.class); + + private MainActivity mActivity = null; + + @Before + public void setActivity() { + mActivity = mActivityRule.getActivity(); + } + /* * Note that we need to keep the `test` prefix if we want to be able to run these * tests on AWS Device Farm, annotations are not enough: @@ -250,7 +267,7 @@ public class MainActivityTest extends BaseTest { @Test public void testMapIconContentDescription() { // Mapbox logo - onView(withContentDescription(getActivity().getResources() + onView(withContentDescription(mActivity.getResources() .getString(R.string.mapboxIconContentDescription))) .check(matches(isDisplayed())); } @@ -258,7 +275,7 @@ public class MainActivityTest extends BaseTest { @Test public void testMapCompassContentDescription() { // Map compass - onView(withContentDescription(getActivity().getResources() + onView(withContentDescription(mActivity.getResources() .getString(R.string.compassContentDescription))) .check(matches(isDisplayed())); } @@ -266,23 +283,9 @@ public class MainActivityTest extends BaseTest { @Test public void testMapAttributionsIconContentDescription() { // Attribution icon - onView(withContentDescription(getActivity().getResources() + onView(withContentDescription(mActivity.getResources() .getString(R.string.attributionsIconContentDescription))) .check(matches(isDisplayed())); } - /* - * Take a screenshot of Mapbox Streets to monitor #1649 - */ - - @Test - public void testMapboxStreetsBlackAndWhite() { - // Click home and switch to Mapbox streets - onView(withContentDescription(HOME_BUTTON_STRING)) - .perform(click()); - onView(withText(R.string.styleMapboxStreets)) - .perform(click()); - takeNamedScreenshot("testMapboxStreetsBlackAndWhite"); - } - }