[android] - add instrumentation tests for FileSource activation/deactivation
This commit is contained in:
@@ -16,17 +16,17 @@ public class OrientationTest extends BaseActivityTest {
|
||||
@Test
|
||||
public void testChangeDeviceOrientation() {
|
||||
onView(isRoot()).perform(orientationLandscape());
|
||||
waitLoop(2200);
|
||||
waitAction(2200);
|
||||
onView(isRoot()).perform(orientationPortrait());
|
||||
waitLoop(2500);
|
||||
waitAction(2500);
|
||||
onView(isRoot()).perform(orientationLandscapeReverse());
|
||||
waitLoop(500);
|
||||
waitAction(500);
|
||||
onView(isRoot()).perform(orientationPortraitReverse());
|
||||
waitLoop(1250);
|
||||
waitAction(1250);
|
||||
onView(isRoot()).perform(orientationLandscape());
|
||||
waitLoop(750);
|
||||
waitAction(750);
|
||||
onView(isRoot()).perform(orientationPortrait());
|
||||
waitLoop(950);
|
||||
waitAction(950);
|
||||
onView(isRoot()).perform(orientationLandscapeReverse());
|
||||
onView(isRoot()).perform(orientationPortraitReverse());
|
||||
onView(isRoot()).perform(orientationLandscape());
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.mapbox.mapboxsdk.testapp.action;
|
||||
|
||||
import android.support.test.espresso.UiController;
|
||||
import android.support.test.espresso.ViewAction;
|
||||
import android.view.View;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
|
||||
|
||||
public final class WaitAction implements ViewAction {
|
||||
|
||||
private static final long DEFAULT_LOOP_TIME = 375;
|
||||
private final long loopTime;
|
||||
|
||||
public WaitAction() {
|
||||
this(DEFAULT_LOOP_TIME);
|
||||
}
|
||||
|
||||
public WaitAction(long loopTime) {
|
||||
this.loopTime = loopTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Matcher<View> getConstraints() {
|
||||
return isDisplayed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(UiController uiController, View view) {
|
||||
uiController.loopMainThreadForAtLeast(loopTime);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,18 +6,19 @@ 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.UiController;
|
||||
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.action.WaitAction;
|
||||
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;
|
||||
@@ -67,12 +68,12 @@ public abstract class BaseActivityTest {
|
||||
onView(withId(id)).check(matches(isDisplayed()));
|
||||
}
|
||||
|
||||
protected void waitLoop() {
|
||||
waitLoop(500);
|
||||
protected void waitAction() {
|
||||
waitAction(500);
|
||||
}
|
||||
|
||||
protected void waitLoop(long waitTime) {
|
||||
onView(withId(R.id.mapView)).perform(new LoopAction(waitTime));
|
||||
protected void waitAction(long waitTime) {
|
||||
onView(withId(R.id.mapView)).perform(new WaitAction(waitTime));
|
||||
}
|
||||
|
||||
static boolean isConnected(Context context) {
|
||||
@@ -87,29 +88,5 @@ public abstract class BaseActivityTest {
|
||||
Timber.e("@After test: unregister idle resource");
|
||||
Espresso.unregisterIdlingResources(idlingResource);
|
||||
}
|
||||
|
||||
private class LoopAction implements ViewAction {
|
||||
|
||||
private long loopTime;
|
||||
|
||||
public LoopAction(long loopTime) {
|
||||
this.loopTime = loopTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Matcher<View> getConstraints() {
|
||||
return isDisplayed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(UiController uiController, View view) {
|
||||
uiController.loopMainThreadForAtLeast(loopTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ public class CompassViewTest extends BaseActivityTest {
|
||||
.build()
|
||||
)));
|
||||
onView(withId(R.id.compassView)).perform(click());
|
||||
waitLoop();
|
||||
waitAction();
|
||||
onView(withId(R.id.compassView)).check(matches(not(isDisplayed())));
|
||||
invoke(mapboxMap, (uiController, mapboxMap) -> {
|
||||
CameraPosition cameraPosition = mapboxMap.getCameraPosition();
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package com.mapbox.mapboxsdk.testapp.storage;
|
||||
|
||||
import android.os.Looper;
|
||||
import android.support.test.espresso.UiController;
|
||||
import android.support.test.espresso.ViewAction;
|
||||
import android.support.test.rule.ActivityTestRule;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.view.View;
|
||||
|
||||
import com.mapbox.mapboxsdk.storage.FileSource;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.testapp.action.WaitAction;
|
||||
import com.mapbox.mapboxsdk.testapp.activity.FeatureOverviewActivity;
|
||||
|
||||
import org.hamcrest.Matcher;
|
||||
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.Espresso.pressBack;
|
||||
import static android.support.test.espresso.action.ViewActions.click;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withId;
|
||||
import static android.support.test.espresso.matcher.ViewMatchers.withText;
|
||||
import static com.mapbox.mapboxsdk.testapp.action.OrientationChangeAction.orientationLandscape;
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class FileSourceTest {
|
||||
|
||||
@Rule
|
||||
public ActivityTestRule<FeatureOverviewActivity> rule = new ActivityTestRule<>(FeatureOverviewActivity.class);
|
||||
|
||||
private FileSource fileSource;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
onView(withId(R.id.recyclerView)).perform(new FileSourceCreator());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefault() throws Exception {
|
||||
assertFalse("FileSource should not be active", fileSource.isActivated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testActivateDeactivate() throws Exception {
|
||||
assertFalse("1) FileSource should not be active", fileSource.isActivated());
|
||||
onView(withId(R.id.recyclerView)).perform(new FileSourceActivator(true));
|
||||
assertTrue("2) FileSource should be active", fileSource.isActivated());
|
||||
onView(withId(R.id.recyclerView)).perform(new FileSourceActivator(false));
|
||||
assertFalse("3) FileSource should not be active", fileSource.isActivated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenCloseMapView() throws Exception {
|
||||
assertFalse("1) FileSource should not be active", fileSource.isActivated());
|
||||
onView(withText("Simple Map")).perform(click());
|
||||
onView(withId(R.id.mapView)).perform(new WaitAction());
|
||||
assertTrue("2) FileSource should be active", fileSource.isActivated());
|
||||
onView(withId(R.id.mapView)).perform(new WaitAction());
|
||||
pressBack();
|
||||
assertFalse("3) FileSource should not be active", fileSource.isActivated());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRotateMapView() throws Exception {
|
||||
assertFalse("1) FileSource should not be active", fileSource.isActivated());
|
||||
onView(withText("Simple Map")).perform(click());
|
||||
onView(withId(R.id.mapView)).perform(new WaitAction());
|
||||
onView(isRoot()).perform(orientationLandscape());
|
||||
onView(withId(R.id.mapView)).perform(new WaitAction());
|
||||
assertTrue("2) FileSource should be active", fileSource.isActivated());
|
||||
onView(withId(R.id.mapView)).perform(new WaitAction());
|
||||
pressBack();
|
||||
assertFalse("3) FileSource should not be active", fileSource.isActivated());
|
||||
}
|
||||
|
||||
private class FileSourceCreator implements ViewAction {
|
||||
@Override
|
||||
public Matcher<View> getConstraints() {
|
||||
return isDisplayed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Creates the filesource instance on the UI thread";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(UiController uiController, View view) {
|
||||
assertTrue(Looper.myLooper() == Looper.getMainLooper());
|
||||
fileSource = FileSource.getInstance(rule.getActivity());
|
||||
}
|
||||
}
|
||||
|
||||
private class FileSourceActivator implements ViewAction {
|
||||
|
||||
private boolean activate;
|
||||
|
||||
FileSourceActivator(boolean activate) {
|
||||
this.activate = activate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Matcher<View> getConstraints() {
|
||||
return isDisplayed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Creates the filesource instance on the UI thread";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(UiController uiController, View view) {
|
||||
assertTrue(Looper.myLooper() == Looper.getMainLooper());
|
||||
if (activate) {
|
||||
fileSource.activate();
|
||||
} else {
|
||||
fileSource.deactivate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user