How to rotate activity, I mean: screen orientation

2019-03-17 23:14发布

问题:

I have decided that one of the testing criteria for my application tests with Google's Espresso is:

Test should maintain Activity state after screen orientation rotation

How do I rotate the screen when using Espresso?


I have tried the following Robotium code (Yes I placed Robotium code in my Espresso test so sue me)

solo.setActivityOrientation(solo.LANDSCAPE);
solo.setActivityOrientation(solo.PORTRAIT);

but It crashes the application when I run it within my Espresso test.
Is there any way to do this?

Thanks in advance for any help

回答1:

If you have the only Activity in your test case, you can do:

1. Declare you test Rule.

@Rule
public ActivityTestRule<TestActivity> mActivityTestRule = new ActivityTestRule<>(TestActivity.class);

2. Get you Activity and apply a screen rotation.

mActivityTestRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mActivityTestRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

That's a piece of pie!



回答2:

You can do it with uiautomator library

dependencies {
  androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
}

ui automator require min sdk version 18 so if your app has a lower min sdk you need to create a new AndroidManifest.xml in androidTest folder

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:tools="http://schemas.android.com/tools"
    package="your.package.name">
    <uses-sdk tools:overrideLibrary="android.support.test.uiautomator.v18"/>
</manifest>

and then in your test

UiDevice device = UiDevice.getInstance(getInstrumentation());

device.setOrientationLeft();
device.setOrientationNatural();
device.setOrientationRight();


回答3:

How to rotate the screen:

public static void rotateScreen(Activity activity) {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    final int orientation = InstrumentationRegistry.getTargetContext()
            .getResources()
            .getConfiguration()
            .orientation;
    final int newOrientation = (orientation == Configuration.ORIENTATION_PORTRAIT) ?
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

    activity.setRequestedOrientation(newOrientation);

    getInstrumentation().waitForIdle(new Runnable() {
        @Override
        public void run() {
            countDownLatch.countDown();
        }
    });

    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        throw new RuntimeException("Screen rotation failed", e);
    }
}

The Activity can be obtained from the ActivityRule.



回答4:

This more complete solution creates a custom Espresso ViewActionand works well. It shows how to get the Activity (even when it is an AppCompatActivity) before calling its setRequestedOrientation() method. It also has a clean caller interface:

onView(isRoot()).perform(orientationLandscape()); 
onView(isRoot()).perform(orientationPortrait());

I follow each orientation change with a 100 ms delay, though you may not need it.



回答5:

You can't mix Robotium and Espresso tests. The best way sometimes to solve any issue is to check source code of desired but not comaptible method.

I'm pretty sure that you have already setUp() method, which has code like:

myActivity = this.getActivity();

Use this to change your screen orientation change:

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

or

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

You may also need to use myActivity.getInstrumentation().waitForIdleSync(); or Thread.sleep(milliseconds); in order to wait for the rotation end because it is performed in Async manner. The second methods depends on emulator/device so choose it wisely.

Hope it help.