How to rotate activity, I mean: screen orientation change using Espresso?

喜欢而已 提交于 2019-11-30 06:41:31

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!

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();

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!