Skip to content

Instantly share code, notes, and snippets.

@chemouna
Created September 2, 2015 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemouna/fe8230d6a1721b02307f to your computer and use it in GitHub Desktop.
Save chemouna/fe8230d6a1721b02307f to your computer and use it in GitHub Desktop.
A ViewAction (espresso) that changes the orientation of the screen
/**
* An Espresso ViewAction that changes the orientation of the screen
*/
public static class OrientationChangeAction implements ViewAction {
private final int orientation;
private OrientationChangeAction(int orientation) {
this.orientation = orientation;
}
@Override public Matcher<View> getConstraints() {
return isRoot();
}
@Override public String getDescription() {
return "change orientation to " + orientation;
}
@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");
}
}
public static ViewAction orientationLandscape() {
return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
public static ViewAction orientationPortrait() {
return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment