using the new version way of instanstiating the activity class

This commit is contained in:
Antonio Zugaldia
2015-09-16 11:03:10 -04:00
parent c3dffb6e46
commit 602a075333
2 changed files with 36 additions and 67 deletions

View File

@@ -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<MainActivity> {
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);
}
}

View File

@@ -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<MainActivity> 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");
}
}