Search code examples
androidtestingandroid-espressoandroid-instrumentation

Force instrumented activity run in multi-window mode


As you know, android provided Multi-Window support mode in android N. Our application has multi-window support.
But how to test it? How to force test run the app in that mode? I haven't founded any such method in Instrumentation class or anywhere else in documentation. Maybe it is somehow possible with Espresso?


Solution

  • Unfortunately the way provided by azizbekian requires an app which has been loaded in multi-window mode previously, so I want to provide own solution. At the answer I have found how to enter multi-window mode programmatically. Using it I built the complete solution:

        UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
        //enter multi-window mode
        uiAutomation.performGlobalAction(AccessibilityService.GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN);
        //wait for completion, unfortunately waitForIdle doesn't applicable here
        Thread.sleep(1000);
        //simulate selection of our activity
        MotionEvent motionDown = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN,
                150, 200, 0);
        motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
        uiAutomation.injectInputEvent(motionDown, true);
        motionDown.recycle();
        MotionEvent motionUp = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_UP,
                150, 200, 0);
        motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
        uiAutomation.injectInputEvent(motionUp, true);
        motionUp.recycle();
        //perform test actions below
    

    As you can see, there are two workarounds:

    1. We can't use uiAutomation.waitForIdle to wait entering multi-mode completion
    2. I haven't found a way to select an application in task manager to request focus to our activity. So I just perform some touch event on the possible location of our activity.

    After implementing it you'll be able to test the activity as usual with Espresso etc.