Created
September 2, 2015 19:28
-
-
Save chemouna/fe8230d6a1721b02307f to your computer and use it in GitHub Desktop.
A ViewAction (espresso) that changes the orientation of the screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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