static ViewAction clickNoConstraints() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isEnabled(); // No constraints, isEnabled and isClickable are checked } @Override public String getDescription() { return "Click a view with no constraints."; } @Override public void perform(UiController uiController, View view) { view.performClick(); } }; }
private ViewAction fillEntropy() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return allOf(isDisplayed(), isAssignableFrom(EntropyView.class)); } @Override public String getDescription() { return "Dismisses the 'Gathering entropy...' dialog"; } @Override public void perform(final UiController uiController, final View view) { ((EntropyView) view).notifyListeners(); } }; }
private static ViewAction showControls() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(MovieView.class); } @Override public String getDescription() { return "Show controls of MovieView"; } @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); ((MovieView) view).showControls(); uiController.loopMainThreadUntilIdle(); } }; }
/** * From: http://stackoverflow.com/questions/33505953/espresso-how-to-test-swiperefreshlayout/33516360#33516360 * <p> * ViewPager will throw an error if not 90% of it is displayed. This reduces the restriction. * * @param action Action to be done. * @param constraints Restrictions on the view. * @return Performs an action on the view. */ public static ViewAction withCustomConstraints(final ViewAction action, final Matcher<View> constraints) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return constraints; } @Override public String getDescription() { return action.getDescription(); } @Override public void perform(UiController uiController, View view) { action.perform(uiController, view); } }; }
static ViewAction setOrientation(final int orientation) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "Change orientation"; } @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); changeOrientation(view, orientation); } }; }
@Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); int orientation = getActivityOrientation(view); boolean checkOrientation = false; switch (orientationType) { case PORTRAIT: checkOrientation = orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT || orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT; break; case LANDSCAPE: checkOrientation = orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE || orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE; break; } if (checkOrientation) { isOrientation[0] = true; } }
@Test public void onMapChangeLocationLayerRedrawn() throws Exception { executeLocationLayerTest(new LocationLayerPluginAction.onPerformLocationLayerAction() { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController) { locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); mapboxMap.setStyleUrl(Style.SATELLITE); uiController.loopMainThreadForAtLeast(500); assertEquals(locationLayerPlugin.getLocationLayerMode(), LocationLayerMode.TRACKING); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER) != null); assertTrue(mapboxMap.getLayer(FOREGROUND_LAYER).getVisibility().getValue() .equals(Property.VISIBLE)); } }); }
public static ViewAction swipeOnCoord(final float fromX, final float fromY, final float toX, final float toY) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return "Swipe from" + fromX + ", " + fromY + " to " + toX + ", " + toY; } @Override public void perform(UiController uiController, final View view) { drag(fromX, fromY, toX, toY); } }; }
public void perform(UiController uiController, View view) { RecyclerView recyclerView = (RecyclerView) view; (new ScrollToPositionViewAction(this.position)).perform(uiController, view); uiController.loopMainThreadUntilIdle(); View targetView = recyclerView.getChildAt(this.position).findViewById(this.viewId); if (targetView == null) { throw (new PerformException.Builder()).withActionDescription(this.toString()) .withViewDescription(HumanReadables.describe(view)) .withCause(new IllegalStateException( "No view with id " + this.viewId + " found at position: " + this.position) ) .build(); } else { this.viewAction.perform(uiController, targetView); } }
/** * Test if the manual input activity opens. */ @Test public void testGoToManual() { onView(withId(R.id.manual_input_button)).check(matches(allOf( isEnabled(), isClickable()))).perform( new ViewAction() { @Override public Matcher<View> getConstraints() { return isEnabled(); // no constraints, they are checked above } @Override public String getDescription() { return "click manual input button"; } @Override public void perform(UiController uiController, View view) { view.performClick(); } } ); intended(hasComponent(ManualInputActivity.class.getName())); }
/** * Set value for {@link NumberPicker} */ public static ViewAction setNumber(final int n) { return new ViewAction() { @Override public void perform(UiController uiController, View view) { ((NumberPicker) view).setValue(n); } @Override public String getDescription() { return "Set NumberPicker value"; } @Override public Matcher<View> getConstraints() { return ViewMatchers.isAssignableFrom(NumberPicker.class); } }; }
/** * Try to getText from TextView */ private String getText(final Matcher<View> matcher) { final String[] stringHolder = { null }; onView(matcher).perform(new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(TextView.class); } @Override public String getDescription() { return "getting text from a TextView"; } @Override public void perform(UiController uiController, View view) { TextView tv = (TextView) view; stringHolder[0] = tv.getText().toString(); } }); return stringHolder[0]; }
public static ViewAction scrollToEnd() { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isAssignableFrom(RecyclerBodyView.class); } @Override public String getDescription() { return "scroll to end"; } @Override public void perform(final UiController uiController, final View view) { final RecyclerBodyView castView = (RecyclerBodyView) view; castView.showItem(castView.getItems().size() - 1); } }; }
public static ViewAction withCustomConstraints(final ViewAction action, final Matcher<View> constraints) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return constraints; } @Override public String getDescription() { return action.getDescription(); } @Override public void perform(UiController uiController, View view) { action.perform(uiController, view); } }; }
@Test public void lineOffsetFunctionMotorwayBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { LineLayer layer = mapboxMap.getLayerAs(MotorWay.BASE_LAYER_ID); assertNotNull(layer.getLineOffset()); assertNotNull(layer.getLineOffset().getFunction()); assertEquals(CameraFunction.class, layer.getLineOffset().getFunction().getClass()); assertEquals(ExponentialStops.class, layer.getLineOffset().getFunction().getStops().getClass()); assertEquals(1.5f, ((ExponentialStops) layer.getLineOffset().getFunction().getStops()).getBase(), 0.001); assertEquals(5, ((ExponentialStops) layer.getLineOffset().getFunction().getStops()).size()); } }); }
@Test public void lineJoinTrunkCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { assertEquals(ROUND, ((LineLayer) mapboxMap.getLayerAs(Trunk.CASE_LAYER_ID)).getLineJoin().getValue()); } }); }
@Test public void lineOffsetFunctionMotorwayCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { LineLayer layer = mapboxMap.getLayerAs(MotorWay.CASE_LAYER_ID); assertNotNull(layer.getLineOffset()); assertNotNull(layer.getLineOffset().getFunction()); assertEquals(CameraFunction.class, layer.getLineOffset().getFunction().getClass()); assertEquals(ExponentialStops.class, layer.getLineOffset().getFunction().getStops().getClass()); assertEquals(1.5f, ((ExponentialStops) layer.getLineOffset().getFunction().getStops()).getBase(), 0.001); assertEquals(5, ((ExponentialStops) layer.getLineOffset().getFunction().getStops()).size()); } }); }
@Test public void lineJoinTrunkBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { assertEquals(ROUND, ((LineLayer) mapboxMap.getLayerAs(Trunk.BASE_LAYER_ID)).getLineJoin().getValue()); } }); }
@Test public void routeLayerShieldVisible() throws Exception { executeNavigationMapRoute(new NavigationMapRouteAction.OnPerformNavigationMapRouteAction() { @Override public void onNavigationMapRouteAction(NavigationMapRoute navigationMapRoute, MapboxMap mapboxMap, UiController controller) { assertTrue(mapboxMap.getLayer(GENERIC_ROUTE_SHIELD_LAYER_ID) .getVisibility().getValue().equals(Property.VISIBLE)); } }); }
@Test public void routeLayerVisible() throws Exception { executeNavigationMapRoute(new NavigationMapRouteAction.OnPerformNavigationMapRouteAction() { @Override public void onNavigationMapRouteAction(NavigationMapRoute navigationMapRoute, MapboxMap mapboxMap, UiController controller) { assertTrue(mapboxMap.getLayer(GENERIC_ROUTE_LAYER_ID) .getVisibility().getValue().equals(Property.VISIBLE)); } }); }
@Test public void removeRouteHidesSheildLayer() throws Exception { executeNavigationMapRoute(new NavigationMapRouteAction.OnPerformNavigationMapRouteAction() { @Override public void onNavigationMapRouteAction(NavigationMapRoute navigationMapRoute, MapboxMap mapboxMap, UiController controller) { navigationMapRoute.removeRoute(); assertTrue(mapboxMap.getLayer(GENERIC_ROUTE_SHIELD_LAYER_ID) .getVisibility().getValue().equals(Property.NONE)); } }); }
@Test public void removeRouteHidesLayer() throws Exception { executeNavigationMapRoute(new NavigationMapRouteAction.OnPerformNavigationMapRouteAction() { @Override public void onNavigationMapRouteAction(NavigationMapRoute navigationMapRoute, MapboxMap mapboxMap, UiController controller) { navigationMapRoute.removeRoute(); assertTrue(mapboxMap.getLayer(GENERIC_ROUTE_LAYER_ID) .getVisibility().getValue().equals(Property.NONE)); } }); }
@Test public void lineJoinPrimaryBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { assertEquals(ROUND, ((LineLayer) mapboxMap.getLayerAs(Primary.BASE_LAYER_ID)).getLineJoin().getValue()); } }); }
@Test public void reattachRouteLayerWithLoadNewStyle() throws Exception { executeNavigationMapRoute(new NavigationMapRouteAction.OnPerformNavigationMapRouteAction() { @Override public void onNavigationMapRouteAction(NavigationMapRoute navigationMapRoute, MapboxMap mapboxMap, UiController controller) { mapboxMap.setStyleUrl(Style.DARK); controller.loopMainThreadForAtLeast(500); assertTrue(mapboxMap.getLayer(GENERIC_ROUTE_LAYER_ID) != null); } }); }
@Test public void locationSourceAdded() throws Exception { executeLocationLayerTest(new LocationLayerPluginAction.onPerformLocationLayerAction() { @Override public void onLocationLayerAction(LocationLayerPlugin locationLayerPlugin, MapboxMap mapboxMap, UiController uiController) { locationLayerPlugin.setLocationLayerEnabled(LocationLayerMode.TRACKING); assertTrue(mapboxMap.getSource(LOCATION_SOURCE) != null); } }); }
@Override public void perform(UiController uiController, View view) { view.performClick(); }
@Test public void lineJoinSecondaryBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { assertEquals(ROUND, ((LineLayer) mapboxMap.getLayerAs(Secondary.BASE_LAYER_ID)).getLineJoin().getValue()); } }); }
public static void performWaitLoop(UiController controller, long waitTime) { controller.loopMainThreadForAtLeast(waitTime); }
private static ViewAction waitFor(final long ms) { return new AnythingAction("wait") { @Override public void perform(UiController uiController, View view) { SystemClock.sleep(ms); } }; }
@Test public void lineJoinPrimaryCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { assertEquals(ROUND, ((LineLayer) mapboxMap.getLayerAs(Primary.CASE_LAYER_ID)).getLineJoin().getValue()); } }); }
@Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); final Activity activity = (Activity) view.getContext(); activity.setRequestedOrientation(orientation); Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED); if (resumedActivities.isEmpty()) { throw new RuntimeException("Could not change orientation"); } }
@Test public void lineOffsetFunctionPrimaryBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { LineLayer layer = mapboxMap.getLayerAs(Primary.BASE_LAYER_ID); assertNotNull(layer.getLineOffset()); assertNotNull(layer.getLineOffset().getFunction()); assertEquals(CameraFunction.class, layer.getLineOffset().getFunction().getClass()); assertEquals(ExponentialStops.class, layer.getLineOffset().getFunction().getStops().getClass()); assertEquals(1.5f, ((ExponentialStops) layer.getLineOffset().getFunction().getStops()).getBase(), 0.001); assertEquals(4, ((ExponentialStops) layer.getLineOffset().getFunction().getStops()).size()); } }); }
public static ViewAction clickClickableSpan(final CharSequence textToClick) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return instanceOf(TextView.class); } @Override public String getDescription() { return "clicking on a ClickableSpan"; } @Override public void perform(UiController uiController, View view) { TextView textView = (TextView) view; SpannableString spannableString = (SpannableString) textView.getText(); if (spannableString.length() == 0) { // TextView is empty, nothing to do throw new NoMatchingViewException.Builder() .includeViewHierarchy(true) .withRootView(textView) .build(); } // Get the links inside the TextView and check if we find textToClick ClickableSpan[] spans = spannableString.getSpans(0, spannableString.length(), ClickableSpan.class); if (spans.length > 0) { ClickableSpan spanCandidate; for (ClickableSpan span : spans) { spanCandidate = span; int start = spannableString.getSpanStart(spanCandidate); int end = spannableString.getSpanEnd(spanCandidate); CharSequence sequence = spannableString.subSequence(start, end); if (textToClick.toString().equals(sequence.toString())) { span.onClick(textView); return; } } } // textToClick not found in TextView throw new NoMatchingViewException.Builder() .includeViewHierarchy(true) .withRootView(textView) .build(); } }; }
@Test public void lineOffsetFunctionTrunkBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { LineLayer layer = mapboxMap.getLayerAs(Trunk.BASE_LAYER_ID); assertNotNull(layer.getLineOffset()); assertNotNull(layer.getLineOffset().getFunction()); assertEquals(CameraFunction.class, layer.getLineOffset().getFunction().getClass()); assertEquals(ExponentialStops.class, layer.getLineOffset().getFunction().getStops().getClass()); assertEquals(1.5f, ((ExponentialStops) layer.getLineOffset().getFunction().getStops()).getBase(), 0.001); assertEquals(4, ((ExponentialStops) layer.getLineOffset().getFunction().getStops()).size()); } }); }
@Test public void lineWidthFunctionTrunkCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { LineLayer layer = mapboxMap.getLayerAs(Trunk.CASE_LAYER_ID); assertNotNull(layer.getLineWidth()); assertNotNull(layer.getLineWidth().getFunction()); assertEquals(CameraFunction.class, layer.getLineWidth().getFunction().getClass()); assertEquals(ExponentialStops.class, layer.getLineWidth().getFunction().getStops().getClass()); assertEquals(1.5f, ((ExponentialStops) layer.getLineWidth().getFunction().getStops()).getBase(), 0.001); assertEquals(4, ((ExponentialStops) layer.getLineWidth().getFunction().getStops()).size()); } }); }
@Test public void lineWidthFunctionMotorwayCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { LineLayer layer = mapboxMap.getLayerAs(MotorWay.CASE_LAYER_ID); assertNotNull(layer.getLineWidth()); assertNotNull(layer.getLineWidth().getFunction()); assertEquals(CameraFunction.class, layer.getLineWidth().getFunction().getClass()); assertEquals(ExponentialStops.class, layer.getLineWidth().getFunction().getStops().getClass()); assertEquals(1.5f, ((ExponentialStops) layer.getLineWidth().getFunction().getStops()).getBase(), 0.001); assertEquals(4, ((ExponentialStops) layer.getLineWidth().getFunction().getStops()).size()); } }); }
@Test public void lineJoinLocalBaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { assertEquals(ROUND, ((LineLayer) mapboxMap.getLayerAs(Local.BASE_LAYER_ID)).getLineJoin().getValue()); } }); }
@Test public void lineWidthFunctionPrimaryCaseLayer() throws Exception { executeTrafficTest(new TrafficPluginAction.OnPerformTrafficAction() { @Override public void onTrafficAction(TrafficPlugin trafficPlugin, MapboxMap mapboxMap, UiController controller) { LineLayer layer = mapboxMap.getLayerAs(Primary.CASE_LAYER_ID); assertNotNull(layer.getLineWidth()); assertNotNull(layer.getLineWidth().getFunction()); assertEquals(CameraFunction.class, layer.getLineWidth().getFunction().getClass()); assertEquals(ExponentialStops.class, layer.getLineWidth().getFunction().getStops().getClass()); assertEquals(1.5f, ((ExponentialStops) layer.getLineWidth().getFunction().getStops()).getBase(), 0.001); assertEquals(3, ((ExponentialStops) layer.getLineWidth().getFunction().getStops()).size()); } }); }