Forked from kotor-le-chat/OrientationChangeAction.java
Last active
August 29, 2015 14:21
-
-
Save ivngrz/439fffbe3d4ee13e034d to your computer and use it in GitHub Desktop.
Revisions
-
ivngrz revised this gist
May 13, 2015 . 1 changed file with 9 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -37,10 +37,17 @@ public String getDescription() { @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); final Activity activity = findActivity(view.getContext()); if (activity == null){ throw new IllegalStateException("Could not find the current activity"); } activity.setRequestedOrientation(orientation); Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry .getInstance().getActivitiesInStage(Stage.RESUMED); if (resumedActivities.isEmpty()) { throw new RuntimeException("Could not change orientation"); } -
ivngrz revised this gist
May 13, 2015 . 1 changed file with 12 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ public String getDescription() { @Override public void perform(UiController uiController, View view) { uiController.loopMainThreadUntilIdle(); final Activity activity = findActivity(view.getContext()); activity.setRequestedOrientation(orientation); Collection<Activity> resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED); @@ -46,6 +46,17 @@ public void perform(UiController uiController, View view) { } } private static Activity findActivity(Context context) { if (context == null) return null; else if (context instanceof Activity) return (Activity) context; else if (context instanceof ContextWrapper) return findActivity(((ContextWrapper) context).getBaseContext()); return null; } public static ViewAction orientationLandscape() { return new OrientationChangeAction(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } -
kotor-le-chat created this gist
Mar 9, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ package net.slideshare.mobile.test.util; import android.app.Activity; import android.content.pm.ActivityInfo; import android.support.test.espresso.UiController; import android.support.test.espresso.ViewAction; import android.support.test.internal.runner.lifecycle.ActivityLifecycleMonitorRegistry; import android.support.test.runner.lifecycle.Stage; import android.view.View; import org.hamcrest.Matcher; import java.util.Collection; import static android.support.test.espresso.matcher.ViewMatchers.isRoot; /** * An Espresso ViewAction that changes the orientation of the screen */ public 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); } }