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?
- 60,783
 - 13
 - 169
 - 249
 
- 9,723
 - 7
 - 40
 - 71
 
2 Answers
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:
- We can't use 
uiAutomation.waitForIdleto wait entering multi-mode completion - 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.
- 
                    1I think you should unselect my answer and mark yours as the correct and full answer. – azizbekian Apr 18 '17 at 11:06
 
From Launch New Activities in Multi-Window Mode:
When you launch a new activity, you can hint to the system that the new activity should be displayed adjacent to the current one, if possible. To do this, use the intent flag FLAG_ACTIVITY_LAUNCH_ADJACENT.
From docs of Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT:
This flag is only used in split-screen multi-window mode. The new activity will be displayed adjacent to the one launching it. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK. Also, setting FLAG_ACTIVITY_MULTIPLE_TASK is required if you want a new instance of an existing activity to be created.
As shown here how to start activity under test:
@Test
public void customIntentToStartActivity() {
    Intent intent = new Intent(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT) 
                | Intent.FLAG_ACTIVITY_NEW_TASK);
    mActivity = mActivityRule.launchActivity(intent);
}
Note, this is my guess based on documentation, haven't tried it. Although, it seems to me you have to start a "fake" Activity first, and from there launch tested activity in multi-window mode, because "The new activity will be displayed adjacent to the one launching it", so there should be another activity who launches it with specified Intent flags.
- 60,783
 - 13
 - 169
 - 249