summaryrefslogtreecommitdiff
path: root/tests/framework/base/windowmanager/src/android/server/wm/MultiDisplayPolicyTests.java
blob: 35f565bd4e12032cac2a164f663816101b24f20d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package android.server.wm;

import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
import static android.server.wm.ComponentNameUtils.getWindowName;
import static android.server.wm.ShellCommandHelper.executeShellCommand;
import static android.server.wm.StateLogger.logE;
import static android.server.wm.WindowManagerState.STATE_RESUMED;
import static android.server.wm.WindowManagerState.STATE_STOPPED;
import static android.server.wm.WindowManagerState.TRANSIT_TASK_CLOSE;
import static android.server.wm.WindowManagerState.TRANSIT_TASK_OPEN;
import static android.server.wm.app.Components.BOTTOM_ACTIVITY;
import static android.server.wm.app.Components.BROADCAST_RECEIVER_ACTIVITY;
import static android.server.wm.app.Components.LAUNCHING_ACTIVITY;
import static android.server.wm.app.Components.LAUNCH_TEST_ON_DESTROY_ACTIVITY;
import static android.server.wm.app.Components.RESIZEABLE_ACTIVITY;
import static android.server.wm.app.Components.SHOW_WHEN_LOCKED_ATTR_ACTIVITY;
import static android.server.wm.app.Components.TEST_ACTIVITY;
import static android.server.wm.app.Components.TOAST_ACTIVITY;
import static android.server.wm.app.Components.VIRTUAL_DISPLAY_ACTIVITY;
import static android.server.wm.app27.Components.SDK_27_LAUNCHING_ACTIVITY;
import static android.server.wm.app27.Components.SDK_27_SEPARATE_PROCESS_ACTIVITY;
import static android.server.wm.app27.Components.SDK_27_TEST_ACTIVITY;
import static android.server.wm.lifecycle.ActivityStarterTests.StandardActivity;
import static android.view.Display.DEFAULT_DISPLAY;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

import android.platform.test.annotations.Presubmit;
import android.server.wm.CommandSession.ActivityCallback;
import android.server.wm.CommandSession.ActivitySession;
import android.server.wm.CommandSession.SizeInfo;
import android.server.wm.WindowManagerState.DisplayContent;
import android.server.wm.WindowManagerState.Task;

import org.junit.Before;
import org.junit.Test;

/**
 * Build/Install/Run:
 *     atest CtsWindowManagerDeviceTestCases:MultiDisplayPolicyTests
 *
 * Tests each expected policy on multi-display environment.
 */
@Presubmit
@android.server.wm.annotation.Group3
public class MultiDisplayPolicyTests extends MultiDisplayTestBase {

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
        assumeTrue(supportsMultiDisplay());
    }
    /**
     * Tests that all activities that were on the private display are destroyed on display removal.
     */
    @Test
    public void testContentDestroyOnDisplayRemoved() {
        try (final VirtualDisplaySession virtualDisplaySession = new VirtualDisplaySession()) {
            // Create new private virtual display.
            final DisplayContent newDisplay = virtualDisplaySession.createDisplay();
            mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true /* visible */);

            // Launch activities on new secondary display.
            launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);
            waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                    "Launched activity must be resumed");

            launchActivityOnDisplay(RESIZEABLE_ACTIVITY, newDisplay.mId);
            waitAndAssertActivityStateOnDisplay(RESIZEABLE_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                    "Launched activity must be resumed");

            separateTestJournal();
            // Destroy the display and check if activities are removed from system.
        }

        mWmState.waitForActivityRemoved(TEST_ACTIVITY);
        mWmState.waitForActivityRemoved(RESIZEABLE_ACTIVITY);

        // Check AM state.
        assertFalse("Activity from removed display must be destroyed",
                mWmState.containsActivity(TEST_ACTIVITY));
        assertFalse("Activity from removed display must be destroyed",
                mWmState.containsActivity(RESIZEABLE_ACTIVITY));
        // Check WM state.
        assertFalse("Activity windows from removed display must be destroyed",
                mWmState.containsWindow(getWindowName(TEST_ACTIVITY)));
        assertFalse("Activity windows from removed display must be destroyed",
                mWmState.containsWindow(getWindowName(RESIZEABLE_ACTIVITY)));
        // Check activity logs.
        assertActivityDestroyed(TEST_ACTIVITY);
        assertActivityDestroyed(RESIZEABLE_ACTIVITY);
    }

    /**
     * Tests that newly launched activity will be landing on default display on display removal.
     */
    @Test
    public void testActivityLaunchOnContentDestroyDisplayRemoved() {
        try (final VirtualDisplaySession virtualDisplaySession = new VirtualDisplaySession()) {
            // Create new private virtual display.
            final DisplayContent newDisplay = virtualDisplaySession.createDisplay();
            mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true /* visible */);

            // Launch activities on new secondary display.
            launchActivityOnDisplay(LAUNCH_TEST_ON_DESTROY_ACTIVITY, newDisplay.mId);

            waitAndAssertActivityStateOnDisplay(LAUNCH_TEST_ON_DESTROY_ACTIVITY, STATE_RESUMED,
                    newDisplay.mId,"Launched activity must be resumed on secondary display");

            // Destroy the display
        }

        waitAndAssertTopResumedActivity(TEST_ACTIVITY, DEFAULT_DISPLAY,
                "Newly launches activity should be landing on default display");
    }

    /**
     * Tests that the update of display metrics updates all its content.
     */
    @Test
    public void testDisplayResize() {
        final VirtualDisplaySession virtualDisplaySession = createManagedVirtualDisplaySession();
        // Create new virtual display.
        final DisplayContent newDisplay = virtualDisplaySession.createDisplay();
        mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true /* visible */);

        // Launch a resizeable activity on new secondary display.
        separateTestJournal();
        launchActivityOnDisplay(RESIZEABLE_ACTIVITY, WINDOWING_MODE_FULLSCREEN, newDisplay.mId);
        waitAndAssertActivityStateOnDisplay(RESIZEABLE_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Launched activity must be resumed");

        // Grab reported sizes and compute new with slight size change.
        final SizeInfo initialSize = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);

        // Resize the display
        separateTestJournal();
        virtualDisplaySession.resizeDisplay();

        mWmState.waitForWithAmState(amState -> {
            try {
                return amState.hasActivityState(RESIZEABLE_ACTIVITY, STATE_RESUMED)
                        && new ActivityLifecycleCounts(RESIZEABLE_ACTIVITY)
                                .getCount(ActivityCallback.ON_CONFIGURATION_CHANGED) == 1;
            } catch (Exception e) {
                logE("Error waiting for valid state: " + e.getMessage());
                return false;
            }
        }, "the configuration change to happen and activity to be resumed");

        mWmState.computeState(
                new WaitForValidActivityState(RESIZEABLE_ACTIVITY),
                new WaitForValidActivityState(VIRTUAL_DISPLAY_ACTIVITY));
        mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true);
        mWmState.assertVisibility(RESIZEABLE_ACTIVITY, true);

        // Check if activity in virtual display was resized properly.
        assertRelaunchOrConfigChanged(RESIZEABLE_ACTIVITY, 0 /* numRelaunch */,
                1 /* numConfigChange */);

        final SizeInfo updatedSize = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);
        assertTrue(updatedSize.widthDp <= initialSize.widthDp);
        assertTrue(updatedSize.heightDp <= initialSize.heightDp);
        assertTrue(updatedSize.displayWidth == initialSize.displayWidth / 2);
        assertTrue(updatedSize.displayHeight == initialSize.displayHeight / 2);
    }

    /**
     * Tests that when primary display is rotated secondary displays are not affected.
     */
    @Test
    public void testRotationNotAffectingSecondaryScreen() {
        final VirtualDisplayLauncher virtualLauncher =
                mObjectTracker.manage(new VirtualDisplayLauncher());
        // Create new virtual display.
        final DisplayContent newDisplay = virtualLauncher.setResizeDisplay(false).createDisplay();
        mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true /* visible */);

        // Launch activity on new secondary display.
        final ActivitySession resizeableActivitySession =
                virtualLauncher.launchActivityOnDisplay(RESIZEABLE_ACTIVITY, newDisplay);
        waitAndAssertActivityStateOnDisplay(RESIZEABLE_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Top activity must be on secondary display");
        final SizeInfo initialSize = resizeableActivitySession.getConfigInfo().sizeInfo;

        assertNotNull("Test activity must have reported initial size on launch", initialSize);

        final RotationSession rotationSession = createManagedRotationSession();
        // Rotate primary display and check that activity on secondary display is not affected.
        rotateAndCheckSameSizes(rotationSession, resizeableActivitySession, initialSize);

        // Launch activity to secondary display when primary one is rotated.
        final int initialRotation = mWmState.getRotation();
        rotationSession.set((initialRotation + 1) % 4);

        final ActivitySession testActivitySession =
                virtualLauncher.launchActivityOnDisplay(TEST_ACTIVITY, newDisplay);
        waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Top activity must be on secondary display");
        final SizeInfo testActivitySize = testActivitySession.getConfigInfo().sizeInfo;

        assertEquals("Sizes of secondary display must not change after rotation of primary"
                + " display", initialSize, testActivitySize);
    }

    private void rotateAndCheckSameSizes(RotationSession rotationSession,
            ActivitySession activitySession, SizeInfo initialSize) {
        for (int rotation = 3; rotation >= 0; --rotation) {
            rotationSession.set(rotation);
            final SizeInfo rotatedSize = activitySession.getConfigInfo().sizeInfo;

            assertEquals("Sizes must not change after rotation", initialSize, rotatedSize);
        }
    }

    /**
     * Tests that turning the primary display off does not affect the activity running
     * on an external secondary display.
     */
    @Test
    public void testExternalDisplayActivityTurnPrimaryOff() {
        // Launch something on the primary display so we know there is a resumed activity there
        launchActivity(RESIZEABLE_ACTIVITY);
        waitAndAssertTopResumedActivity(RESIZEABLE_ACTIVITY, DEFAULT_DISPLAY,
                "Activity launched on primary display must be resumed");

        final DisplayContent newDisplay = createManagedExternalDisplaySession()
                .setCanShowWithInsecureKeyguard(true).createVirtualDisplay();

        launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);

        // Check that the activity is launched onto the external display
        waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Activity launched on external display must be resumed");
        mWmState.assertFocusedAppOnDisplay("App on default display must still be focused",
                RESIZEABLE_ACTIVITY, DEFAULT_DISPLAY);

        separateTestJournal();
        mObjectTracker.manage(new PrimaryDisplayStateSession()).turnScreenOff();

        // Wait for the fullscreen stack to start sleeping, and then make sure the
        // test activity is still resumed.
        final ActivityLifecycleCounts counts = new ActivityLifecycleCounts(RESIZEABLE_ACTIVITY);
        if (!Condition.waitFor(counts.countWithRetry(RESIZEABLE_ACTIVITY + " to be stopped",
                countSpec(ActivityCallback.ON_STOP, CountSpec.EQUALS, 1)))) {
            fail(RESIZEABLE_ACTIVITY + " has received "
                    + counts.getCount(ActivityCallback.ON_STOP)
                    + " onStop() calls, expecting 1");
        }
        // For this test we create this virtual display with flag showContentWhenLocked, so it
        // cannot be effected when default display screen off.
        waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Activity launched on external display must be resumed");
    }

    /**
     * Tests that turning the secondary display off stops activities running and makes invisible
     * on that display.
     */
    @Test
    public void testExternalDisplayToggleState() {
        final ExternalDisplaySession externalDisplaySession = createManagedExternalDisplaySession();
        final DisplayContent newDisplay = externalDisplaySession.createVirtualDisplay();

        launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);

        // Check that the test activity is resumed on the external display
        waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Activity launched on external display must be resumed");

        externalDisplaySession.turnDisplayOff();

        // Check that turning off the external display stops the activity, and makes it
        // invisible.
        waitAndAssertActivityState(TEST_ACTIVITY, STATE_STOPPED,
                "Activity launched on external display must be stopped after turning off");
        mWmState.assertVisibility(TEST_ACTIVITY, false /* visible */);

        externalDisplaySession.turnDisplayOn();

        // Check that turning on the external display resumes the activity
        waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Activity launched on external display must be resumed");
    }

    /**
     * Tests no leaking after external display removed.
     */
    @Test
    public void testNoLeakOnExternalDisplay() throws Exception {
        // How this test works:
        // When receiving the request to remove a display and some activities still exist on that
        // display, it will finish those activities first, so the display won't be removed
        // immediately. Then, when all activities were destroyed, the display removes itself.

        // Get display count before testing, as some devices may have more than one built-in
        // display.
        mWmState.computeState();
        final int displayCount = mWmState.getDisplayCount();
        try (final VirtualDisplaySession externalDisplaySession = new VirtualDisplaySession()) {
            final DisplayContent newDisplay = externalDisplaySession
                    .setSimulateDisplay(true).createDisplay();
            launchActivityOnDisplay(VIRTUAL_DISPLAY_ACTIVITY, newDisplay.mId);
            waitAndAssertTopResumedActivity(VIRTUAL_DISPLAY_ACTIVITY, newDisplay.mId,
                    "Virtual activity should be Top Resumed Activity.");
            mWmState.assertFocusedAppOnDisplay("Activity on second display must be focused.",
                    VIRTUAL_DISPLAY_ACTIVITY, newDisplay.mId);
        }
        mWmState.waitFor((amState) -> amState.getDisplayCount() == displayCount,
                "external displays to be removed");
        assertEquals(displayCount, mWmState.getDisplayCount());
        assertEquals(displayCount, mWmState.getKeyguardControllerState().
                mKeyguardOccludedStates.size());
    }

    /**
     * Tests launching activities on secondary and then on primary display to see if the stack
     * visibility is not affected.
     */
    @Test
    public void testLaunchActivitiesAffectsVisibility() {
        // Start launching activity.
        launchActivity(LAUNCHING_ACTIVITY);

        // Create new virtual display.
        final DisplayContent newDisplay = createManagedVirtualDisplaySession().createDisplay();
        mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true /* visible */);

        // Launch activity on new secondary display.
        launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);
        mWmState.assertVisibility(TEST_ACTIVITY, true /* visible */);
        mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true /* visible */);

        // Launch activity on primary display and check if it doesn't affect activity on
        // secondary display.
        getLaunchActivityBuilder().setTargetActivity(RESIZEABLE_ACTIVITY).execute();
        mWmState.waitForValidState(RESIZEABLE_ACTIVITY);
        mWmState.assertVisibility(TEST_ACTIVITY, true /* visible */);
        mWmState.assertVisibility(RESIZEABLE_ACTIVITY, true /* visible */);
        assertBothDisplaysHaveResumedActivities(pair(DEFAULT_DISPLAY, RESIZEABLE_ACTIVITY),
                pair(newDisplay.mId, TEST_ACTIVITY));
    }

    /**
     * Test that move-task works when moving between displays.
     */
    @Test
    public void testMoveTaskBetweenDisplays() {
        // Create new virtual display.
        final DisplayContent newDisplay = createManagedVirtualDisplaySession().createDisplay();
        mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true /* visible */);
        mWmState.assertFocusedActivity("Virtual display activity must be on top",
                VIRTUAL_DISPLAY_ACTIVITY);
        final int defaultDisplayStackId = mWmState.getFocusedTaskId();
        Task frontStack = mWmState.getRootTask(
                defaultDisplayStackId);
        assertEquals("Top stack must remain on primary display",
                DEFAULT_DISPLAY, frontStack.mDisplayId);

        // Launch activity on new secondary display.
        launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);

        waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Top activity must be on secondary display");
        assertBothDisplaysHaveResumedActivities(pair(DEFAULT_DISPLAY, VIRTUAL_DISPLAY_ACTIVITY),
                pair(newDisplay.mId, TEST_ACTIVITY));

        // Move activity from secondary display to primary.
        moveActivityToRootTaskOrOnTop(TEST_ACTIVITY, defaultDisplayStackId);
        waitAndAssertTopResumedActivity(TEST_ACTIVITY, DEFAULT_DISPLAY,
                "Moved activity must be on top");
    }

    /**
     * Tests launching activities on secondary display and then removing it to see if stack focus
     * is moved correctly.
     * This version launches virtual display creator to fullscreen stack in split-screen.
     */
    @Test
    public void testStackFocusSwitchOnDisplayRemoved() {
        assumeTrue(supportsSplitScreenMultiWindow());

        // Start launching activity into docked stack.
        launchActivitiesInSplitScreen(
                getLaunchActivityBuilder().setTargetActivity(LAUNCHING_ACTIVITY),
                getLaunchActivityBuilder().setTargetActivity(TEST_ACTIVITY));
        mWmState.assertVisibility(LAUNCHING_ACTIVITY, true /* visible */);

        tryCreatingAndRemovingDisplayWithActivity(true /* splitScreen */,
                WINDOWING_MODE_MULTI_WINDOW);
    }

    /**
     * Tests launching activities on secondary display and then removing it to see if stack focus
     * is moved correctly.
     * This version launches virtual display creator to docked stack in split-screen.
     */
    @Test
    public void testStackFocusSwitchOnDisplayRemoved2() {
        assumeTrue(supportsSplitScreenMultiWindow());

        // Setup split-screen.
        launchActivitiesInSplitScreen(
                getLaunchActivityBuilder().setTargetActivity(TEST_ACTIVITY),
                getLaunchActivityBuilder().setTargetActivity(LAUNCHING_ACTIVITY));
        mWmState.assertVisibility(LAUNCHING_ACTIVITY, true /* visible */);

        tryCreatingAndRemovingDisplayWithActivity(true /* splitScreen */,
                WINDOWING_MODE_MULTI_WINDOW);
    }

    /**
     * Tests launching activities on secondary display and then removing it to see if stack focus
     * is moved correctly.
     * This version works without split-screen.
     */
    @Test
    public void testStackFocusSwitchOnDisplayRemoved3() {
        // Start an activity on default display to determine default stack.
        launchActivity(BROADCAST_RECEIVER_ACTIVITY);
        final int focusedStackWindowingMode = mWmState.getFrontRootTaskWindowingMode(
                DEFAULT_DISPLAY);
        // Finish probing activity.
        mBroadcastActionTrigger.finishBroadcastReceiverActivity();

        tryCreatingAndRemovingDisplayWithActivity(false /* splitScreen */,
                focusedStackWindowingMode);
    }

    /**
     * Create a virtual display, launch a test activity there, destroy the display and check if test
     * activity is moved to a stack on the default display.
     */
    private void tryCreatingAndRemovingDisplayWithActivity(boolean splitScreen, int windowingMode) {
        try (final VirtualDisplaySession virtualDisplaySession = new VirtualDisplaySession()) {
            // Create new virtual display.
            final DisplayContent newDisplay = virtualDisplaySession
                    .setPublicDisplay(true)
                    .setLaunchInSplitScreen(splitScreen)
                    .createDisplay();
            if (splitScreen) {
                // Set the secondary split root task as launch root to verify remaining tasks will
                // be reparented to matching launch root after removed the virtual display.
                mTaskOrganizer.setLaunchRoot(mTaskOrganizer.getSecondarySplitTaskId());
                // Launch activity on new secondary display in fullscreen to put it in splitscreen
                // after the display's disconenction.
                launchActivityOnDisplay(
                        RESIZEABLE_ACTIVITY, WINDOWING_MODE_FULLSCREEN, newDisplay.mId);
            } else {
                // Launch activity on new secondary display
                launchActivityOnDisplay(RESIZEABLE_ACTIVITY, newDisplay.mId);
            }

            waitAndAssertActivityStateOnDisplay(RESIZEABLE_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                    "Test activity must be on secondary display");

            separateTestJournal();
            // Destroy virtual display.
        }

        mWmState.computeState();
        assertActivityLifecycle(RESIZEABLE_ACTIVITY, false /* relaunched */);
        mWmState.waitForValidState(new WaitForValidActivityState.Builder(RESIZEABLE_ACTIVITY)
                .setWindowingMode(windowingMode)
                .setActivityType(ACTIVITY_TYPE_STANDARD)
                .build());
        mWmState.assertValidity();

        // Check if the top activity is now back on primary display.
        mWmState.assertVisibility(RESIZEABLE_ACTIVITY, true /* visible */);
        mWmState.assertFocusedRootTask(
                "Default stack on primary display must be focused after display removed",
                windowingMode, ACTIVITY_TYPE_STANDARD);
        mWmState.assertFocusedActivity(
                "Focus must be switched back to activity on primary display",
                RESIZEABLE_ACTIVITY);
    }

    /**
     * Tests launching activities on secondary display and then removing it to see if stack focus
     * is moved correctly.
     */
    @Test
    public void testStackFocusSwitchOnStackEmptiedInSleeping() {
        assumeTrue(supportsLockScreen());

        validateStackFocusSwitchOnStackEmptied(createManagedVirtualDisplaySession(),
                createManagedLockScreenSession());
    }

    /**
     * Tests launching activities on secondary display and then finishing it to see if stack focus
     * is moved correctly.
     */
    @Test
    public void testStackFocusSwitchOnStackEmptied() {
        validateStackFocusSwitchOnStackEmptied(createManagedVirtualDisplaySession(),
                null /* lockScreenSession */);
    }

    private void validateStackFocusSwitchOnStackEmptied(VirtualDisplaySession virtualDisplaySession,
            LockScreenSession lockScreenSession) {
        if (lockScreenSession != null) {
            lockScreenSession.setLockCredential();
        }

        // Create new virtual display.
        final DisplayContent newDisplay = virtualDisplaySession.createDisplay();
        mWmState.assertVisibility(VIRTUAL_DISPLAY_ACTIVITY, true /* visible */);

        // Launch activity on new secondary display.
        launchActivityOnDisplay(BROADCAST_RECEIVER_ACTIVITY, newDisplay.mId);
        waitAndAssertActivityStateOnDisplay(BROADCAST_RECEIVER_ACTIVITY, STATE_RESUMED,
                newDisplay.mId,"Top activity must be on secondary display");

        if (lockScreenSession != null) {
            // Lock the device, so that activity containers will be detached.
            lockScreenSession.sleepDevice();
        }

        // Finish activity on secondary display.
        mBroadcastActionTrigger.finishBroadcastReceiverActivity();

        if (lockScreenSession != null) {
            // Unlock and check if the focus is switched back to primary display.
            lockScreenSession.wakeUpDevice().enterAndConfirmLockCredential();
        }

        waitAndAssertTopResumedActivity(VIRTUAL_DISPLAY_ACTIVITY, DEFAULT_DISPLAY,
                "Top activity must be switched back to primary display");
    }

    /**
     * Tests that input events on the primary display take focus from the virtual display.
     */
    @Test
    public void testStackFocusSwitchOnTouchEvent() {
        // If config_perDisplayFocusEnabled, the focus will not move even if touching on
        // the Activity in the different display.
        assumeFalse(perDisplayFocusEnabled());

        // Create new virtual display.
        final DisplayContent newDisplay = createManagedVirtualDisplaySession().createDisplay();

        mWmState.computeState(VIRTUAL_DISPLAY_ACTIVITY);
        mWmState.assertFocusedActivity("Top activity must be the latest launched one",
                VIRTUAL_DISPLAY_ACTIVITY);

        launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);

        waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Activity launched on secondary display must be resumed");

        // Tap on task center to switch focus between displays. Using task center instead of
        // display center to cover the multi window scenario.
        tapOnTaskCenter(mWmState.getTaskByActivity(VIRTUAL_DISPLAY_ACTIVITY));

        waitAndAssertTopResumedActivity(VIRTUAL_DISPLAY_ACTIVITY, DEFAULT_DISPLAY,
                "Top activity must be on the primary display");
        assertBothDisplaysHaveResumedActivities(pair(DEFAULT_DISPLAY, VIRTUAL_DISPLAY_ACTIVITY),
                pair(newDisplay.mId, TEST_ACTIVITY));

        tapOnDisplayCenter(newDisplay.mId);
        mWmState.waitForValidState(TEST_ACTIVITY);
        mWmState.assertFocusedAppOnDisplay("App on secondary display must be focused",
                TEST_ACTIVITY, newDisplay.mId);
    }


    /**
     * Tests that tapping on the primary display after showing the keyguard resumes the
     * activity on the primary display.
     */
    @Test
    public void testStackFocusSwitchOnTouchEventAfterKeyguard() {
        assumeFalse(perDisplayFocusEnabled());
        assumeTrue(supportsLockScreen());

        // Launch something on the primary display so we know there is a resumed activity there
        launchActivity(RESIZEABLE_ACTIVITY);
        waitAndAssertTopResumedActivity(RESIZEABLE_ACTIVITY, DEFAULT_DISPLAY,
                "Activity launched on primary display must be resumed");

        final LockScreenSession lockScreenSession = createManagedLockScreenSession();
        lockScreenSession.sleepDevice();

        // Make sure there is no resumed activity when the primary display is off
        waitAndAssertActivityState(RESIZEABLE_ACTIVITY, STATE_STOPPED,
                "Activity launched on primary display must be stopped after turning off");
        assertEquals("Unexpected resumed activity",
                0, mWmState.getResumedActivitiesCount());

        final DisplayContent newDisplay = createManagedExternalDisplaySession()
                .setCanShowWithInsecureKeyguard(true).createVirtualDisplay();

        launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);

        // Unlock the device and tap on the middle of the primary display
        lockScreenSession.wakeUpDevice();
        executeShellCommand("wm dismiss-keyguard");
        mWmState.waitForKeyguardGone();
        mWmState.waitForValidState(RESIZEABLE_ACTIVITY, TEST_ACTIVITY);

        // Check that the test activity is resumed on the external display and is on top
        waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Activity on external display must be resumed");
        assertBothDisplaysHaveResumedActivities(pair(DEFAULT_DISPLAY, RESIZEABLE_ACTIVITY),
                pair(newDisplay.mId, TEST_ACTIVITY));

        // Tap on task center to switch focus between displays. Using task center instead of
        // display center to cover the multi window scenario.
        tapOnTaskCenter(mWmState.getTaskByActivity(RESIZEABLE_ACTIVITY));

        // Check that the activity on the primary display is the topmost resumed
        waitAndAssertTopResumedActivity(RESIZEABLE_ACTIVITY, DEFAULT_DISPLAY,
                "Activity on primary display must be resumed and on top");
        assertBothDisplaysHaveResumedActivities(pair(DEFAULT_DISPLAY, RESIZEABLE_ACTIVITY),
                pair(newDisplay.mId, TEST_ACTIVITY));
    }

    /**
     * Tests that showWhenLocked works on a secondary display.
     */
    @Test
    public void testSecondaryDisplayShowWhenLocked() {
        assumeTrue(supportsSecureLock());

        final LockScreenSession lockScreenSession = createManagedLockScreenSession();
        lockScreenSession.setLockCredential();

        launchActivity(TEST_ACTIVITY);

        final DisplayContent newDisplay = createManagedExternalDisplaySession()
                .createVirtualDisplay();
        launchActivityOnDisplay(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, newDisplay.mId);

        lockScreenSession.gotoKeyguard();

        waitAndAssertActivityState(TEST_ACTIVITY, STATE_STOPPED,
                "Expected stopped activity on default display");
        waitAndAssertActivityStateOnDisplay(SHOW_WHEN_LOCKED_ATTR_ACTIVITY, STATE_RESUMED,
                newDisplay.mId, "Expected resumed activity on secondary display");
    }

    /**
     * Tests tap and set focus between displays.
     */
    @Test
    public void testSecondaryDisplayFocus() {
        assumeFalse(perDisplayFocusEnabled());

        launchActivity(TEST_ACTIVITY);
        mWmState.waitForActivityState(TEST_ACTIVITY, STATE_RESUMED);

        final DisplayContent newDisplay = createManagedVirtualDisplaySession()
                .setSimulateDisplay(true).createDisplay();
        launchActivityOnDisplay(VIRTUAL_DISPLAY_ACTIVITY, newDisplay.mId);
        waitAndAssertTopResumedActivity(VIRTUAL_DISPLAY_ACTIVITY, newDisplay.mId,
                "Virtual activity should be Top Resumed Activity.");
        mWmState.assertFocusedAppOnDisplay("Activity on second display must be focused.",
                VIRTUAL_DISPLAY_ACTIVITY, newDisplay.mId);

        // Tap on task center to switch focus between displays. Using task center instead of
        // display center to cover the multi window scenario.
        tapOnTaskCenter(mWmState.getTaskByActivity(TEST_ACTIVITY));

        waitAndAssertTopResumedActivity(TEST_ACTIVITY, DEFAULT_DISPLAY,
                "Activity should be top resumed when tapped.");
        mWmState.assertFocusedActivity("Activity on default display must be top focused.",
                TEST_ACTIVITY);

        tapOnDisplayCenter(newDisplay.mId);

        waitAndAssertTopResumedActivity(VIRTUAL_DISPLAY_ACTIVITY, newDisplay.mId,
                "Virtual display activity should be top resumed when tapped.");
        mWmState.assertFocusedActivity("Activity on second display must be top focused.",
                VIRTUAL_DISPLAY_ACTIVITY);
        mWmState.assertFocusedAppOnDisplay(
                "Activity on default display must be still focused.",
                TEST_ACTIVITY, DEFAULT_DISPLAY);
    }

    /**
     * Tests that toast works on a secondary display.
     */
    @Test
    public void testSecondaryDisplayShowToast() {
        final DisplayContent newDisplay = createManagedVirtualDisplaySession()
                .setPublicDisplay(true)
                .createDisplay();
        final String TOAST_NAME = "Toast";
        launchActivityOnDisplay(TOAST_ACTIVITY, newDisplay.mId);
        waitAndAssertActivityStateOnDisplay(TOAST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                "Activity launched on external display must be resumed");

        assertTrue("Toast window must be shown", mWmState.waitForWithAmState(
                state -> state.containsWindow(TOAST_NAME), "toast window to show"));
        assertTrue("Toast window must be visible",
                mWmState.isWindowSurfaceShown(TOAST_NAME));
    }

    /**
     * Tests that the surface size of a fullscreen task is same as its display's surface size.
     * Also check that the surface size has updated after reparenting to other display.
     */
    @Test
    public void testTaskSurfaceSizeAfterReparentDisplay() {
        try (final VirtualDisplaySession virtualDisplaySession = new VirtualDisplaySession()) {
            // Create new simulated display and launch an activity on it.
            final DisplayContent newDisplay = virtualDisplaySession.setSimulateDisplay(true)
                    .createDisplay();
            launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);

            waitAndAssertActivityStateOnDisplay(TEST_ACTIVITY, STATE_RESUMED, newDisplay.mId,
                    "Top activity must be the newly launched one");
            assertTopTaskSameSurfaceSizeWithDisplay(newDisplay.mId);

            separateTestJournal();
            // Destroy the display.
        }

        // Activity must be reparented to default display and relaunched.
        assertActivityLifecycle(TEST_ACTIVITY, true /* relaunched */);
        waitAndAssertTopResumedActivity(TEST_ACTIVITY, DEFAULT_DISPLAY,
                "Top activity must be reparented to default display");

        // Check the surface size after task was reparented to default display.
        assertTopTaskSameSurfaceSizeWithDisplay(DEFAULT_DISPLAY);
    }

    private void assertTopTaskSameSurfaceSizeWithDisplay(int displayId) {
        final DisplayContent display = mWmState.getDisplay(displayId);
        final int stackId = mWmState.getFrontRootTaskId(displayId);
        final Task task = mWmState.getRootTask(stackId).getTopTask();

        assertEquals("Task must have same surface width with its display",
                display.getSurfaceSize(), task.getSurfaceWidth());
        assertEquals("Task must have same surface height with its display",
                display.getSurfaceSize(), task.getSurfaceHeight());
    }

    @Test
    public void testAppTransitionForActivityOnDifferentDisplay() {
        assumeFalse(ENABLE_SHELL_TRANSITIONS);
        final TestActivitySession<StandardActivity> transitionActivitySession =
                createManagedTestActivitySession();
        // Create new simulated display.
        final DisplayContent newDisplay = createManagedVirtualDisplaySession()
                .setSimulateDisplay(true).createDisplay();

        // Launch BottomActivity on top of launcher activity to prevent transition state
        // affected by wallpaper theme.
        launchActivityOnDisplay(BOTTOM_ACTIVITY, DEFAULT_DISPLAY);
        waitAndAssertTopResumedActivity(BOTTOM_ACTIVITY, DEFAULT_DISPLAY,
                "Activity must be resumed");

        // Launch StandardActivity on default display, verify last transition if is correct.
        transitionActivitySession.launchTestActivityOnDisplaySync(StandardActivity.class,
                DEFAULT_DISPLAY);
        mWmState.waitForAppTransitionIdleOnDisplay(DEFAULT_DISPLAY);
        mWmState.assertValidity();
        assertEquals(TRANSIT_TASK_OPEN,
                mWmState.getDisplay(DEFAULT_DISPLAY).getLastTransition());

        // Finish current activity & launch another TestActivity in virtual display in parallel.
        transitionActivitySession.finishCurrentActivityNoWait();
        launchActivityOnDisplayNoWait(TEST_ACTIVITY, newDisplay.mId);
        mWmState.waitForAppTransitionIdleOnDisplay(DEFAULT_DISPLAY);
        mWmState.waitForAppTransitionIdleOnDisplay(newDisplay.mId);
        mWmState.assertValidity();

        // Verify each display's last transition if is correct as expected.
        assertEquals(TRANSIT_TASK_CLOSE,
                mWmState.getDisplay(DEFAULT_DISPLAY).getLastTransition());
        assertEquals(TRANSIT_TASK_OPEN,
                mWmState.getDisplay(newDisplay.mId).getLastTransition());
    }

    @Test
    public void testNoTransitionWhenMovingActivityToDisplay() throws Exception {
        // Create new simulated display & capture new display's transition state.
        final DisplayContent newDisplay = createManagedVirtualDisplaySession()
                .setSimulateDisplay(true).createDisplay();

        // Launch TestActivity in virtual display & capture its transition state.
        launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);
        mWmState.waitForAppTransitionIdleOnDisplay(newDisplay.mId);
        mWmState.assertValidity();
        final String lastTranstionOnVirtualDisplay = mWmState
                .getDisplay(newDisplay.mId).getLastTransition();

        // Move TestActivity from virtual display to default display.
        getLaunchActivityBuilder().setTargetActivity(TEST_ACTIVITY)
                .allowMultipleInstances(false).setNewTask(true)
                .setDisplayId(DEFAULT_DISPLAY).execute();

        // Verify TestActivity moved to virtual display.
        waitAndAssertTopResumedActivity(TEST_ACTIVITY, DEFAULT_DISPLAY,
                "Existing task must be brought to front");

        // Make sure last transition will not change when task move to another display.
        assertEquals(lastTranstionOnVirtualDisplay,
                mWmState.getDisplay(newDisplay.mId).getLastTransition());
    }

    @Test
    public void testPreQTopProcessResumedActivity() {
        final DisplayContent newDisplay = createManagedVirtualDisplaySession()
                .setSimulateDisplay(true).createDisplay();

        getLaunchActivityBuilder().setUseInstrumentation()
                .setTargetActivity(SDK_27_TEST_ACTIVITY).setNewTask(true)
                .setDisplayId(newDisplay.mId).execute();
        waitAndAssertTopResumedActivity(SDK_27_TEST_ACTIVITY, newDisplay.mId,
                "Activity launched on secondary display must be resumed and focused");

        getLaunchActivityBuilder().setUseInstrumentation()
                .setTargetActivity(SDK_27_LAUNCHING_ACTIVITY).setNewTask(true)
                .setDisplayId(DEFAULT_DISPLAY).setWindowingMode(WINDOWING_MODE_FULLSCREEN)
                .execute();
        waitAndAssertTopResumedActivity(SDK_27_LAUNCHING_ACTIVITY, DEFAULT_DISPLAY,
                "Activity launched on default display must be resumed and focused");

        assertEquals("There must be only one resumed activity in the package.", 1,
                mWmState.getResumedActivitiesCountInPackage(
                        SDK_27_LAUNCHING_ACTIVITY.getPackageName()));

        // Start SeparateProcessActivity in the same task as LaunchingActivity by setting
        // allowMultipleInstances to false, and the TestActivity should be resumed.
        getLaunchActivityBuilder().setUseInstrumentation()
                .setTargetActivity(SDK_27_SEPARATE_PROCESS_ACTIVITY).setNewTask(true)
                .setDisplayId(DEFAULT_DISPLAY).setWindowingMode(WINDOWING_MODE_FULLSCREEN)
                .allowMultipleInstances(false).execute();
        waitAndAssertTopResumedActivity(SDK_27_SEPARATE_PROCESS_ACTIVITY, DEFAULT_DISPLAY,
                "Activity launched on default display must be resumed and focused");
        assertTrue("Activity that was on secondary display must be resumed",
                mWmState.hasActivityState(SDK_27_TEST_ACTIVITY, STATE_RESUMED));
        assertEquals("There must be only two resumed activities in the package.", 2,
                mWmState.getResumedActivitiesCountInPackage(
                        SDK_27_TEST_ACTIVITY.getPackageName()));
    }

    @Test
    public void testPreQTopProcessResumedDisplayMoved() throws Exception {
        final DisplayContent newDisplay = createManagedVirtualDisplaySession()
                .setSimulateDisplay(true).createDisplay();
        getLaunchActivityBuilder().setUseInstrumentation()
                .setTargetActivity(SDK_27_LAUNCHING_ACTIVITY).setNewTask(true)
                .setDisplayId(DEFAULT_DISPLAY).execute();
        waitAndAssertTopResumedActivity(SDK_27_LAUNCHING_ACTIVITY, DEFAULT_DISPLAY,
                "Activity launched on default display must be resumed and focused");

        getLaunchActivityBuilder().setUseInstrumentation()
                .setTargetActivity(SDK_27_TEST_ACTIVITY).setNewTask(true)
                .setDisplayId(newDisplay.mId).execute();
        // Dismiss DeprecatedTargetSdkVersionDialog to avoid it disturbing tapOnTaskCenter.
        DeprecatedTargetSdkTest.waitAndDismissDeprecatedTargetSdkDialog(mWmState);
        waitAndAssertTopResumedActivity(SDK_27_TEST_ACTIVITY, newDisplay.mId,
                "Activity launched on secondary display must be resumed and focused");

        // Tap on task center to switch focus between displays. Using task center instead of
        // display center to cover the multi window scenario.
        tapOnTaskCenter(mWmState.getTaskByActivity(SDK_27_LAUNCHING_ACTIVITY));
        waitAndAssertTopResumedActivity(SDK_27_LAUNCHING_ACTIVITY, DEFAULT_DISPLAY,
                "Activity launched on default display must be resumed and focused");
        assertEquals("There must be only one resumed activity in the package.", 1,
                mWmState.getResumedActivitiesCountInPackage(
                        SDK_27_LAUNCHING_ACTIVITY.getPackageName()));
    }
}