Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 869
Beloo Avatar asked Nov 28 '25 06:11

Beloo


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:

  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.

like image 189
Beloo Avatar answered Nov 30 '25 21:11

Beloo


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.

like image 35
azizbekian Avatar answered Nov 30 '25 19:11

azizbekian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!