Instrumentation test for Android - How to receive new Activity after orientation change?

后端 未结 5 850
臣服心动
臣服心动 2021-01-06 04:11


I\'m trying to test, if newly created Activity (after orientation change) is properly reinitialized. The code below shows that activity returned from getActivity() is t

5条回答
  •  佛祖请我去吃肉
    2021-01-06 04:45

    Took @Smok's answer and updated it to always rotate:

    @SuppressWarnings("unchecked") // it's fine
    public static  T rotate(ActivityInstrumentationTestCase2 testCase) {
        T activity = testCase.getActivity();
        int orientation = activity.getWindowManager().getDefaultDisplay().getRotation();
        int nextOrientation = orientation == Configuration.ORIENTATION_LANDSCAPE ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(activity.getClass().getName(), null, false);
        testCase.getInstrumentation().addMonitor(monitor);
        activity.setRequestedOrientation(nextOrientation);
        testCase.getInstrumentation().waitForIdleSync();
        return (T) testCase.getInstrumentation().waitForMonitor(monitor);
    }
    

    This fixes the problem of the orientation returning UNSPECIFIED

提交回复
热议问题