summaryrefslogtreecommitdiff
path: root/tests/framework/base/windowmanager/src/android/server/wm/AppConfigurationTests.java
blob: 2396f4bc59dea0133e2c518409e789f0dd20d135 (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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
/*
 * Copyright (C) 2016 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_FREEFORM;
import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static android.server.wm.StateLogger.logE;
import static android.server.wm.WindowManagerState.STATE_RESUMED;
import static android.server.wm.WindowManagerState.dpToPx;
import static android.server.wm.app.Components.BROADCAST_RECEIVER_ACTIVITY;
import static android.server.wm.app.Components.DIALOG_WHEN_LARGE_ACTIVITY;
import static android.server.wm.app.Components.LANDSCAPE_ORIENTATION_ACTIVITY;
import static android.server.wm.app.Components.LAUNCHING_ACTIVITY;
import static android.server.wm.app.Components.LandscapeOrientationActivity.EXTRA_APP_CONFIG_INFO;
import static android.server.wm.app.Components.LandscapeOrientationActivity.EXTRA_CONFIG_INFO_IN_ON_CREATE;
import static android.server.wm.app.Components.LandscapeOrientationActivity.EXTRA_DISPLAY_REAL_SIZE;
import static android.server.wm.app.Components.LandscapeOrientationActivity.EXTRA_SYSTEM_RESOURCES_CONFIG_INFO;
import static android.server.wm.app.Components.NIGHT_MODE_ACTIVITY;
import static android.server.wm.app.Components.PORTRAIT_ORIENTATION_ACTIVITY;
import static android.server.wm.app.Components.RESIZEABLE_ACTIVITY;
import static android.server.wm.app.Components.TEST_ACTIVITY;
import static android.server.wm.translucentapp26.Components.SDK26_TRANSLUCENT_LANDSCAPE_ACTIVITY;
import static android.view.Surface.ROTATION_0;
import static android.view.Surface.ROTATION_180;
import static android.view.Surface.ROTATION_270;
import static android.view.Surface.ROTATION_90;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
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.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.platform.test.annotations.Presubmit;
import android.server.wm.CommandSession.ActivitySession;
import android.server.wm.CommandSession.ActivitySessionClient;
import android.server.wm.CommandSession.ConfigInfo;
import android.server.wm.CommandSession.SizeInfo;
import android.server.wm.TestJournalProvider.TestJournalContainer;
import android.view.Display;
import android.window.WindowContainerTransaction;

import org.junit.Test;

import java.util.function.Function;

/**
 * Build/Install/Run:
 *     atest CtsWindowManagerDeviceTestCases:AppConfigurationTests
 */
@Presubmit
public class AppConfigurationTests extends MultiDisplayTestBase {

    private static final int SMALL_WIDTH_DP = 426;
    private static final int SMALL_HEIGHT_DP = 320;

    /**
     * Tests that the WindowManager#getDefaultDisplay() and the Configuration of the Activity
     * has an updated size when the Activity is resized from fullscreen to docked state.
     *
     * The Activity handles configuration changes, so it will not be restarted between resizes.
     * On Configuration changes, the Activity logs the Display size and Configuration width
     * and heights. The values reported in fullscreen should be larger than those reported in
     * docked state.
     */
    @Test
    public void testConfigurationUpdatesWhenResizedFromFullscreen() {
        assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());

        separateTestJournal();
        launchActivity(RESIZEABLE_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        final SizeInfo fullscreenSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);

        separateTestJournal();
        putActivityInPrimarySplit(RESIZEABLE_ACTIVITY);
        final SizeInfo dockedSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);

        assertSizesAreSane(fullscreenSizes, dockedSizes);
    }

    /**
     * Same as {@link #testConfigurationUpdatesWhenResizedFromFullscreen()} but resizing
     * from docked state to fullscreen (reverse).
     */
    @Test
    public void testConfigurationUpdatesWhenResizedFromDockedStack() {
        assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());

        separateTestJournal();
        launchActivitiesInSplitScreen(
                getLaunchActivityBuilder().setTargetActivity(RESIZEABLE_ACTIVITY)
                                        .setWindowingMode(WINDOWING_MODE_FULLSCREEN),
                getLaunchActivityBuilder().setTargetActivity(TEST_ACTIVITY)
                                        .setWindowingMode(WINDOWING_MODE_FULLSCREEN));

        final SizeInfo dockedSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);

        separateTestJournal();
        dismissSplitScreen(true /* primaryOnTop */);
        final SizeInfo fullscreenSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);

        assertSizesAreSane(fullscreenSizes, dockedSizes);
    }

    /**
     * Tests whether the Display sizes change when rotating the device.
     */
    @Test
    public void testConfigurationUpdatesWhenRotatingWhileFullscreen() {
        assumeTrue("Skipping test: no rotation support", supportsRotation());

        final RotationSession rotationSession = createManagedRotationSession();
        rotationSession.set(ROTATION_0);

        separateTestJournal();
        final ActivitySessionClient resizeableActivityClient = createManagedActivityClientSession();
        resizeableActivityClient.startActivity(getLaunchActivityBuilder()
                        .setUseInstrumentation()
                        .setTargetActivity(RESIZEABLE_ACTIVITY)
                        .setWindowingMode(WINDOWING_MODE_FULLSCREEN));
        final SizeInfo initialSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);

        rotateAndCheckSizes(rotationSession, resizeableActivityClient, initialSizes);
    }

    /**
     * Same as {@link #testConfigurationUpdatesWhenRotatingWhileFullscreen()} but when the Activity
     * is in the docked stack.
     */
    @Test
    public void testConfigurationUpdatesWhenRotatingWhileDocked() {
        assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());

        final ActivitySessionClient resizeableActivityClient = createManagedActivityClientSession();
        final RotationSession rotationSession = createManagedRotationSession();
        rotationSession.set(ROTATION_0);

        separateTestJournal();
        // Launch our own activity to side in case Recents (or other activity to side) doesn't
        // support rotation.
        launchActivitiesInSplitScreen(
                getLaunchActivityBuilder().setTargetActivity(LAUNCHING_ACTIVITY),
                getLaunchActivityBuilder().setTargetActivity(TEST_ACTIVITY));
        // Launch target activity in docked stack.
        getLaunchActivityBuilder().setTargetActivity(RESIZEABLE_ACTIVITY)
                .setActivitySessionClient(resizeableActivityClient).execute();
        final SizeInfo initialSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);

        rotateAndCheckSizes(rotationSession, resizeableActivityClient, initialSizes);
    }

    /**
     * Same as {@link #testConfigurationUpdatesWhenRotatingWhileDocked()} but when the Activity
     * is launched to side from docked stack.
     */
    @Test
    public void testConfigurationUpdatesWhenRotatingToSideFromDocked() {
        assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());

        final ActivitySessionClient resizeableActivityClient = createManagedActivityClientSession();
        final RotationSession rotationSession = createManagedRotationSession();
        rotationSession.set(ROTATION_0);

        separateTestJournal();
        launchActivitiesInSplitScreen(
                getLaunchActivityBuilder().setTargetActivity(LAUNCHING_ACTIVITY),
                getLaunchActivityBuilder().setTargetActivity(RESIZEABLE_ACTIVITY)
                        .setActivitySessionClient(resizeableActivityClient));
        final SizeInfo initialSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);

        rotateAndCheckSizes(rotationSession, resizeableActivityClient, initialSizes);
    }

    private void rotateAndCheckSizes(RotationSession rotationSession,
            ActivitySessionClient noRelaunchActivityClient, SizeInfo prevSizes) {
        final ActivitySession activitySession = noRelaunchActivityClient.getLastStartedSession();
        final ComponentName activityName = activitySession.getName();
        final WindowManagerState.Task task = mWmState.getTaskByActivity(activityName);
        final int displayId = mWmState.getRootTask(task.mRootTaskId).mDisplayId;

        assumeTrue(supportsLockedUserRotation(rotationSession, displayId));

        final boolean isCloseToSquareDisplay = isCloseToSquareDisplay();
        final int[] rotations = { ROTATION_270, ROTATION_180, ROTATION_90, ROTATION_0 };
        for (final int rotation : rotations) {
            separateTestJournal();
            rotationSession.set(rotation);
            final int newDeviceRotation = getDeviceRotation(displayId);
            if (newDeviceRotation == INVALID_DEVICE_ROTATION) {
                logE("Got an invalid device rotation value. "
                        + "Continuing the test despite of that, but it is likely to fail.");
            }
            final boolean expectConfigChange = task.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
                    && !isCloseToSquareDisplay;
            if (expectConfigChange) {
                assertActivityLifecycle(activityName, false /* relaunch */);
            }
            final SizeInfo rotatedSizes = activitySession.getConfigInfo().sizeInfo;
            assertSizesRotate(prevSizes, rotatedSizes,
                    // Skip orientation checks if we are not in fullscreen mode, or when the display
                    // is close to square because the app config orientation may always be landscape
                    // excluding the system insets.
                    !expectConfigChange /* skipOrientationCheck */);
            prevSizes = rotatedSizes;
        }
    }

    /**
     * Tests when activity moved from fullscreen stack to docked and back. Activity will be
     * relaunched twice and it should have same config as initial one.
     */
    @Test
    public void testSameConfigurationFullSplitFullRelaunch() {
        moveActivityFullSplitFull(true /* relaunch */);
    }

    /**
     * Same as {@link #testSameConfigurationFullSplitFullRelaunch} but without relaunch.
     */
    @Test
    public void testSameConfigurationFullSplitFullNoRelaunch() {
        moveActivityFullSplitFull(false /* relaunch */);
    }

    /**
     * Launches activity in fullscreen task, moves to docked task and back to fullscreen task.
     * Asserts that initial and final reported sizes in fullscreen task are the same.
     */
    private void moveActivityFullSplitFull(boolean relaunch) {
        assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());

        final ComponentName activityName = relaunch ? TEST_ACTIVITY : RESIZEABLE_ACTIVITY;
        // Launch to fullscreen task and record size.
        separateTestJournal();
        launchActivity(activityName, WINDOWING_MODE_FULLSCREEN);
        final SizeInfo initialFullscreenSizes = getLastReportedSizesForActivity(activityName);

        // Ensure the orientation configuration is different while moving the activity into split
        // primary task later if we expected activity to be launched.
        if (relaunch) {
            mTaskOrganizer.registerOrganizerIfNeeded();
            Rect primaryTaskBounds = mTaskOrganizer.getPrimaryTaskBounds();
            if (initialFullscreenSizes.displayHeight > initialFullscreenSizes.displayWidth) {
                primaryTaskBounds.bottom = primaryTaskBounds.width() / 2;
            } else {
                primaryTaskBounds.right = primaryTaskBounds.height() / 2;
            }
            mTaskOrganizer.setRootPrimaryTaskBounds(primaryTaskBounds);
        }

        // Move the task to the primary split task.
        separateTestJournal();
        putActivityInPrimarySplit(activityName);
        // Currently launchActivityInPrimarySplit launches the target activity and then move it
        // to split task, so it requires waiting of lifecycle to get the stable initial size.
        if (relaunch) {
            assertActivityLifecycle(activityName, true /* relaunch */);
        } else {
            // The lifecycle callbacks contain the initial launch event so only wait for
            // multi-window mode changed.
            waitForOnMultiWindowModeChanged(activityName);
        }
        final SizeInfo dockedSizes = getLastReportedSizesForActivity(activityName);
        assertSizesAreSane(initialFullscreenSizes, dockedSizes);
        final boolean orientationChanged =
                initialFullscreenSizes.orientation != dockedSizes.orientation;

        separateTestJournal();
        // Restore to fullscreen.
        final int activityTaskId = mWmState.getTaskByActivity(activityName).mTaskId;
        final WindowContainerTransaction wct = new WindowContainerTransaction()
            .setWindowingMode(mTaskOrganizer.getTaskInfo(activityTaskId).getToken(),
                WINDOWING_MODE_FULLSCREEN);
        mTaskOrganizer.dismissSplitScreen(wct, false /* primaryOnTop */);
        // Home task could be on top since it was the top-most task while in split-screen mode
        // (dock task was minimized), start the activity again to ensure the activity is at
        // foreground.
        launchActivity(activityName, WINDOWING_MODE_FULLSCREEN);
        if (relaunch && !orientationChanged) {
            // If there is no orientation changes while moving the non-resizeable activity out of
            // the split, the Activity won't be relaunched because size changes won't cross the
            // size config buckets. So, there won't be any lifecycle changes.
            waitForOnMultiWindowModeChanged(activityName);
        } else {
            assertActivityLifecycle(activityName, relaunch);
        }

        // It must report same size as original one after split-screen dismissed.
        final SizeInfo finalFullscreenSizes = getLastReportedSizesForActivity(activityName);
        assertSizesAreSame(initialFullscreenSizes, finalFullscreenSizes);
    }

    /**
     * Tests that an activity with the DialogWhenLarge theme can transform properly when in split
     * screen.
     */
    @Test
    public void testDialogWhenLargeSplitSmall() {
        assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());

        launchActivitiesInSplitScreen(
                getLaunchActivityBuilder().setTargetActivity(DIALOG_WHEN_LARGE_ACTIVITY),
                getLaunchActivityBuilder().setTargetActivity(TEST_ACTIVITY));
        int displayId = mWmState.getDisplayByActivity(DIALOG_WHEN_LARGE_ACTIVITY);
        final WindowManagerState.DisplayContent display = mWmState.getDisplay(displayId);
        final int density = display.getDpi();
        final int smallWidthPx = dpToPx(SMALL_WIDTH_DP, density);
        final int smallHeightPx = dpToPx(SMALL_HEIGHT_DP, density);

        mTaskOrganizer.setRootPrimaryTaskBounds(new Rect(0, 0, smallWidthPx, smallHeightPx));
        mWmState.waitForValidState(
                new WaitForValidActivityState.Builder(DIALOG_WHEN_LARGE_ACTIVITY)
                        .setWindowingMode(WINDOWING_MODE_MULTI_WINDOW)
                        .setActivityType(ACTIVITY_TYPE_STANDARD)
                        .build());
    }

    /**
     * Test that device handles consequent requested orientations and displays the activities.
     */
    @Test
    public void testFullscreenAppOrientationRequests() {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());

        separateTestJournal();
        launchActivity(PORTRAIT_ORIENTATION_ACTIVITY);
        mWmState.assertVisibility(PORTRAIT_ORIENTATION_ACTIVITY, true /* visible */);
        SizeInfo reportedSizes = getLastReportedSizesForActivity(PORTRAIT_ORIENTATION_ACTIVITY);
        assertEquals("portrait activity should be in portrait",
                ORIENTATION_PORTRAIT, reportedSizes.orientation);
        assertTrue("portrait activity should have height >= width",
                reportedSizes.heightDp >= reportedSizes.widthDp);
        separateTestJournal();

        launchActivity(LANDSCAPE_ORIENTATION_ACTIVITY);
        mWmState.assertVisibility(LANDSCAPE_ORIENTATION_ACTIVITY, true /* visible */);
        reportedSizes = getLastReportedSizesForActivity(LANDSCAPE_ORIENTATION_ACTIVITY);
        assertEquals("landscape activity should be in landscape",
                ORIENTATION_LANDSCAPE, reportedSizes.orientation);
        assertTrue("landscape activity should have height < width",
                reportedSizes.heightDp < reportedSizes.widthDp);
        separateTestJournal();

        launchActivity(PORTRAIT_ORIENTATION_ACTIVITY);
        mWmState.assertVisibility(PORTRAIT_ORIENTATION_ACTIVITY, true /* visible */);
        reportedSizes = getLastReportedSizesForActivity(PORTRAIT_ORIENTATION_ACTIVITY);
        assertEquals("portrait activity should be in portrait",
                ORIENTATION_PORTRAIT, reportedSizes.orientation);
    }

    @Test
    public void testTranslucentAppOrientationRequests() {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());
        disableIgnoreOrientationRequest();

        separateTestJournal();
        launchActivity(PORTRAIT_ORIENTATION_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        final SizeInfo initialReportedSizes =
                getLastReportedSizesForActivity(PORTRAIT_ORIENTATION_ACTIVITY);
        assertEquals("portrait activity should be in portrait",
                ORIENTATION_PORTRAIT, initialReportedSizes.orientation);
        assertTrue("portrait activity should have height >= width",
                initialReportedSizes.heightDp >= initialReportedSizes.widthDp);
        assumeFalse("Skipping test: device is fixed to user rotation",
                mWmState.isFixedToUserRotation());

        separateTestJournal();

        launchActivity(SDK26_TRANSLUCENT_LANDSCAPE_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        assumeNotIgnoringOrientation(SDK26_TRANSLUCENT_LANDSCAPE_ACTIVITY);
        assertEquals("Legacy translucent activity requested landscape orientation",
                SCREEN_ORIENTATION_LANDSCAPE, mWmState.getLastOrientation());

        // TODO(b/36897968): uncomment once we can suppress unsupported configurations
        // final ReportedSizes updatedReportedSizes =
        //      getLastReportedSizesForActivity(PORTRAIT_ACTIVITY_NAME, logSeparator);
        // assertEquals("portrait activity should not have moved from portrait",
        //         1 /* portrait */, updatedReportedSizes.orientation);
    }

    /**
     * Test that device handles consequent requested orientations and will not report a config
     * change to an invisible activity.
     */
    @Test
    public void testAppOrientationRequestConfigChanges() {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());

        separateTestJournal();
        launchActivity(PORTRAIT_ORIENTATION_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        mWmState.assertVisibility(PORTRAIT_ORIENTATION_ACTIVITY, true /* visible */);

        assertLifecycleCounts(PORTRAIT_ORIENTATION_ACTIVITY,
                1 /* create */, 1 /* start */, 1 /* resume */,
                0 /* pause */, 0 /* stop */, 0 /* destroy */, 0 /* config */);

        launchActivity(LANDSCAPE_ORIENTATION_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        mWmState.assertVisibility(LANDSCAPE_ORIENTATION_ACTIVITY, true /* visible */);

        assertLifecycleCounts(PORTRAIT_ORIENTATION_ACTIVITY,
                1 /* create */, 1 /* start */, 1 /* resume */,
                1 /* pause */, 1 /* stop */, 0 /* destroy */, 0 /* config */);
        assertLifecycleCounts(LANDSCAPE_ORIENTATION_ACTIVITY,
                1 /* create */, 1 /* start */, 1 /* resume */,
                0 /* pause */, 0 /* stop */, 0 /* destroy */, 0 /* config */);

        launchActivity(PORTRAIT_ORIENTATION_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        mWmState.assertVisibility(PORTRAIT_ORIENTATION_ACTIVITY, true /* visible */);

        assertLifecycleCounts(PORTRAIT_ORIENTATION_ACTIVITY,
                2 /* create */, 2 /* start */, 2 /* resume */,
                1 /* pause */, 1 /* stop */, 0 /* destroy */, 0 /* config */);
        assertLifecycleCounts(LANDSCAPE_ORIENTATION_ACTIVITY,
                1 /* create */, 1 /* start */, 1 /* resume */,
                1 /* pause */, 1 /* stop */, 0 /* destroy */, 0 /* config */);
    }

    /**
     * Test that device orientation is restored when an activity that requests it is no longer
     * visible.
     *
     * TODO(b/139936670, b/112688380): This test case fails on some vendor devices which has
     * rotation sensing optimization. So this is listed in cts-known-failures.xml.
     */
    @Test
    public void testAppOrientationRequestConfigClears() {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());

        separateTestJournal();
        launchActivity(TEST_ACTIVITY);
        mWmState.assertVisibility(TEST_ACTIVITY, true /* visible */);
        final SizeInfo initialReportedSizes = getLastReportedSizesForActivity(TEST_ACTIVITY);
        final int initialOrientation = initialReportedSizes.orientation;

        // Launch an activity that requests different orientation and check that it will be applied
        final boolean launchingPortrait;
        if (initialOrientation == ORIENTATION_LANDSCAPE) {
            launchingPortrait = true;
        } else if (initialOrientation == ORIENTATION_PORTRAIT) {
            launchingPortrait = false;
        } else {
            fail("Unexpected orientation value: " + initialOrientation);
            return;
        }
        final ComponentName differentOrientationActivity = launchingPortrait
                ? PORTRAIT_ORIENTATION_ACTIVITY : LANDSCAPE_ORIENTATION_ACTIVITY;
        separateTestJournal();
        launchActivity(differentOrientationActivity);
        mWmState.assertVisibility(differentOrientationActivity, true /* visible */);
        final SizeInfo rotatedReportedSizes =
                getLastReportedSizesForActivity(differentOrientationActivity);
        assertEquals("Applied orientation must correspond to activity request",
                launchingPortrait ? 1 : 2, rotatedReportedSizes.orientation);

        // Launch another activity on top and check that its orientation is not affected by previous
        // activity.
        separateTestJournal();
        launchActivity(RESIZEABLE_ACTIVITY);
        mWmState.assertVisibility(RESIZEABLE_ACTIVITY, true /* visible */);
        final SizeInfo finalReportedSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);
        assertEquals("Applied orientation must not be influenced by previously visible activity",
                initialOrientation, finalReportedSizes.orientation);
    }

    @Test
    public void testRotatedInfoWithFixedRotationTransform() {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());

        // Start a portrait activity first to ensure that the orientation will change.
        launchActivity(PORTRAIT_ORIENTATION_ACTIVITY);
        mWmState.waitForDisplayOrientation(ORIENTATION_PORTRAIT);
        final int prevRotation = mWmState.getRotation();

        getLaunchActivityBuilder()
                .setUseInstrumentation()
                .setTargetActivity(LANDSCAPE_ORIENTATION_ACTIVITY)
                // Request the info from onCreate because at that moment the real display hasn't
                // rotated but the activity is rotated.
                .setIntentExtra(bundle -> bundle.putBoolean(EXTRA_CONFIG_INFO_IN_ON_CREATE, true))
                .execute();
        mWmState.waitForDisplayOrientation(ORIENTATION_LANDSCAPE);

        final SizeInfo reportedSizes =
                getLastReportedSizesForActivity(LANDSCAPE_ORIENTATION_ACTIVITY);
        final Bundle extras = TestJournalContainer.get(LANDSCAPE_ORIENTATION_ACTIVITY).extras;
        final ConfigInfo appConfigInfo = extras.getParcelable(EXTRA_APP_CONFIG_INFO);
        final Point onCreateRealDisplaySize = extras.getParcelable(EXTRA_DISPLAY_REAL_SIZE);
        final ConfigInfo onCreateConfigInfo = extras.getParcelable(EXTRA_CONFIG_INFO_IN_ON_CREATE);
        final SizeInfo onCreateSize = onCreateConfigInfo.sizeInfo;
        final ConfigInfo globalConfigInfo =
                extras.getParcelable(EXTRA_SYSTEM_RESOURCES_CONFIG_INFO);
        final SizeInfo globalSizeInfo = globalConfigInfo.sizeInfo;

        assertEquals("The last reported size should be the same as the one from onCreate",
                reportedSizes, onCreateConfigInfo.sizeInfo);

        final WindowManagerState.DisplayContent dc = mWmState.getDisplay(Display.DEFAULT_DISPLAY);
        final Point realDisplaySize =
                new Point(dc.getDisplayRect().width(), dc.getDisplayRect().height());
        final int currentRotation = mWmState.getRotation();
        // Some devices may launch the activity in a letterboxed area so the display won't rotate.
        final boolean displayRotationChanged = prevRotation != currentRotation;

        assertEquals("The activity should get the final display rotation in onCreate",
                currentRotation, onCreateConfigInfo.rotation);
        assertEquals("The application should get the final display rotation in onCreate",
                currentRotation, appConfigInfo.rotation);
        assertEquals("The orientation of application must be landscape",
                ORIENTATION_LANDSCAPE, appConfigInfo.sizeInfo.orientation);
        assertEquals("The orientation of system resources must be landscape",
                ORIENTATION_LANDSCAPE, globalSizeInfo.orientation);

        final boolean isLandscape = onCreateSize.displayWidth > onCreateSize.displayHeight;
        if (displayRotationChanged) {
            assertEquals("The activity should get the final display size in onCreate",
                    realDisplaySize, onCreateRealDisplaySize);
            assertEquals("The app size of activity should have the same orientation", isLandscape,
                    realDisplaySize.x > realDisplaySize.y);
            assertEquals("The display metrics of system resources must be landscape", isLandscape,
                    globalSizeInfo.metricsWidth > globalSizeInfo.metricsHeight);
        }
        assertEquals("The application should get the same orientation", isLandscape,
                appConfigInfo.sizeInfo.displayWidth > appConfigInfo.sizeInfo.displayHeight);
        assertEquals("The app display metrics must be landscape", isLandscape,
                appConfigInfo.sizeInfo.metricsWidth > appConfigInfo.sizeInfo.metricsHeight);
    }

    @Test
    public void testTranslucentActivityPermitted() throws Exception {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());

        disableIgnoreOrientationRequest();

        final RotationSession rotationSession = createManagedRotationSession();
        rotationSession.set(ROTATION_0);

        launchActivity(SDK26_TRANSLUCENT_LANDSCAPE_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        assumeNotIgnoringOrientation(SDK26_TRANSLUCENT_LANDSCAPE_ACTIVITY);
        mWmState.assertResumedActivity(
                "target SDK <= 26 translucent activity should be allowed to launch",
                SDK26_TRANSLUCENT_LANDSCAPE_ACTIVITY);
        assertEquals("translucent activity requested landscape orientation",
                SCREEN_ORIENTATION_LANDSCAPE, mWmState.getLastOrientation());
    }

    /**
     * Test that device handles moving between two tasks with different orientations.
     */
    @Test
    public void testTaskCloseRestoreFixedOrientation() {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());
        disableIgnoreOrientationRequest();

        // Start landscape activity.
        launchActivity(LANDSCAPE_ORIENTATION_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        assumeFalse("Skipping test: device is fixed to user rotation",
                mWmState.isFixedToUserRotation());
        assumeNotIgnoringOrientation(LANDSCAPE_ORIENTATION_ACTIVITY);
        mWmState.assertVisibility(LANDSCAPE_ORIENTATION_ACTIVITY, true /* visible */);
        mWmState.waitAndAssertLastOrientation("Fullscreen app requested landscape orientation",
                SCREEN_ORIENTATION_LANDSCAPE);

        // Start another activity in a different task.
        launchActivityInNewTask(BROADCAST_RECEIVER_ACTIVITY);

        // Request portrait
        mBroadcastActionTrigger.requestOrientation(SCREEN_ORIENTATION_PORTRAIT);
        mWmState.waitForLastOrientation(SCREEN_ORIENTATION_PORTRAIT);
        waitForBroadcastActivityReady(ORIENTATION_PORTRAIT);

        // Finish activity
        mBroadcastActionTrigger.finishBroadcastReceiverActivity();

        // Verify that activity brought to front is in originally requested orientation.
        mWmState.computeState(LANDSCAPE_ORIENTATION_ACTIVITY);
        mWmState.waitAndAssertLastOrientation("Should return to app in landscape orientation",
                SCREEN_ORIENTATION_LANDSCAPE);
    }

    /**
     * Test that device handles moving between two tasks with different orientations.
     *
     * TODO(b/139936670, b/112688380): This test case fails on some vendor devices which has
     * rotation sensing optimization. So this is listed in cts-known-failures.xml.
     */
    @Test
    public void testTaskCloseRestoreFreeOrientation() {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());

        // Start landscape activity.
        launchActivity(RESIZEABLE_ACTIVITY);
        mWmState.assertVisibility(RESIZEABLE_ACTIVITY, true /* visible */);
        final int initialServerOrientation = mWmState.getLastOrientation();

        // Verify fixed-landscape
        separateTestJournal();
        launchActivityInNewTask(BROADCAST_RECEIVER_ACTIVITY);
        mBroadcastActionTrigger.requestOrientation(SCREEN_ORIENTATION_LANDSCAPE);
        mWmState.waitForLastOrientation(SCREEN_ORIENTATION_LANDSCAPE);
        waitForBroadcastActivityReady(ORIENTATION_LANDSCAPE);
        mBroadcastActionTrigger.finishBroadcastReceiverActivity();

        // Verify that activity brought to front is in originally requested orientation.
        mWmState.waitForActivityState(RESIZEABLE_ACTIVITY, STATE_RESUMED);
        mWmState.waitAndAssertLastOrientation("Should come back in original server orientation",
                initialServerOrientation);
        assertRelaunchOrConfigChanged(RESIZEABLE_ACTIVITY, 0 /* numRelaunch */,
                0 /* numConfigChange */);

        // Verify fixed-portrait
        separateTestJournal();
        launchActivityInNewTask(BROADCAST_RECEIVER_ACTIVITY);
        mBroadcastActionTrigger.requestOrientation(SCREEN_ORIENTATION_PORTRAIT);
        mWmState.waitForLastOrientation(SCREEN_ORIENTATION_PORTRAIT);
        waitForBroadcastActivityReady(ORIENTATION_PORTRAIT);
        mBroadcastActionTrigger.finishBroadcastReceiverActivity();

        // Verify that activity brought to front is in originally requested orientation.
        mWmState.waitForActivityState(RESIZEABLE_ACTIVITY, STATE_RESUMED);
        mWmState.waitAndAssertLastOrientation("Should come back in original server orientation",
                initialServerOrientation);
        assertRelaunchOrConfigChanged(RESIZEABLE_ACTIVITY, 0 /* numRelaunch */,
                0 /* numConfigChange */);
    }

    /**
     * Test that activity orientation will change when device is rotated.
     * Also verify that occluded activity will not get config changes.
     */
    @Test
    public void testAppOrientationWhenRotating() throws Exception {
        assumeFalse("Skipping test: square size may not have configuration changes",
                isCloseToSquareDisplay());
        assumeTrue("Skipping test: no rotation support", supportsRotation());

        // Start resizeable activity that handles configuration changes.
        separateTestJournal();
        launchActivity(TEST_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        launchActivity(RESIZEABLE_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        mWmState.assertVisibility(RESIZEABLE_ACTIVITY, true /* visible */);

        final int displayId = mWmState.getDisplayByActivity(RESIZEABLE_ACTIVITY);

        // Rotate the activity and check that it receives configuration changes with a different
        // orientation each time.
        final RotationSession rotationSession = createManagedRotationSession();
        assumeTrue("Skipping test: no locked user rotation mode support.",
                supportsLockedUserRotation(rotationSession, displayId));

        rotationSession.set(ROTATION_0);
        SizeInfo reportedSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);
        int prevOrientation = reportedSizes.orientation;

        final int[] rotations = { ROTATION_270, ROTATION_180, ROTATION_90, ROTATION_0 };
        for (final int rotation : rotations) {
            separateTestJournal();
            rotationSession.set(rotation);

            // Verify lifecycle count and orientation changes.
            assertRelaunchOrConfigChanged(RESIZEABLE_ACTIVITY, 0 /* numRelaunch */,
                    1 /* numConfigChange */);
            reportedSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);
            assertNotEquals(prevOrientation, reportedSizes.orientation);
            assertRelaunchOrConfigChanged(TEST_ACTIVITY, 0 /* numRelaunch */,
                    0 /* numConfigChange */);

            prevOrientation = reportedSizes.orientation;
        }
    }

    /**
     * Test that the orientation for a simulated display context derived from an application context
     * will not change when the device rotates.
     */
    @Test
    public void testAppContextDerivedDisplayContextOrientationWhenRotating() {
        assumeTrue("Skipping test: no rotation support", supportsRotation());
        assumeTrue("Skipping test: no multi-display support", supportsMultiDisplay());

        assertDisplayContextDoesntChangeOrientationWhenRotating(Activity::getApplicationContext);
    }

    /**
     * Test that the orientation for a simulated display context derived from an activity context
     * will not change when the device rotates.
     */
    @Test
    public void testActivityContextDerivedDisplayContextOrientationWhenRotating() {
        assumeTrue("Skipping test: no rotation support", supportsRotation());
        assumeTrue("Skipping test: no multi-display support", supportsMultiDisplay());

        assertDisplayContextDoesntChangeOrientationWhenRotating(activity -> activity);
    }

    /**
     * Asserts that the orientation for a simulated display context derived from a base context will
     * not change when the device rotates.
     *
     * @param baseContextSupplier function that returns a base context used to created the display
     *                            context.
     *
     * @see #testAppContextDerivedDisplayContextOrientationWhenRotating
     * @see #testActivityContextDerivedDisplayContextOrientationWhenRotating
     */
    private void assertDisplayContextDoesntChangeOrientationWhenRotating(
            Function<Activity, Context> baseContextSupplier) {
        RotationSession rotationSession = createManagedRotationSession();
        rotationSession.set(ROTATION_0);

        TestActivitySession<ConfigChangeHandlingActivity> activitySession
                = createManagedTestActivitySession();
        activitySession.launchTestActivityOnDisplaySync(
                ConfigChangeHandlingActivity.class,
                Display.DEFAULT_DISPLAY,
                WINDOWING_MODE_FULLSCREEN);
        final ConfigChangeHandlingActivity activity = activitySession.getActivity();

        VirtualDisplaySession virtualDisplaySession = createManagedVirtualDisplaySession();
        WindowManagerState.DisplayContent displayContent = virtualDisplaySession
                .setSimulateDisplay(true)
                .setSimulationDisplaySize(100 /* width */, 200 /* height */)
                .createDisplay();

        DisplayManager dm = activity.getSystemService(DisplayManager.class);
        Display simulatedDisplay = dm.getDisplay(displayContent.mId);
        Context simulatedDisplayContext = baseContextSupplier.apply(activity)
                .createDisplayContext(simulatedDisplay);
        assertEquals(ORIENTATION_PORTRAIT,
                simulatedDisplayContext.getResources().getConfiguration().orientation);

        separateTestJournal();

        final int[] rotations = {ROTATION_270, ROTATION_180, ROTATION_90, ROTATION_0};
        for (final int rotation : rotations) {
            rotationSession.set(rotation);

            assertRelaunchOrConfigChanged(activity.getComponentName(), 0 /* numRelaunch */,
                    1 /* numConfigChange */);
            separateTestJournal();

            assertEquals("Display context orientation must not be changed", ORIENTATION_PORTRAIT,
                    simulatedDisplayContext.getResources().getConfiguration().orientation);
        }
    }

    /**
     * Test that activity orientation will not change when trying to rotate fixed-orientation
     * activity.
     * Also verify that occluded activity will not get config changes.
     */
    @Test
    public void testFixedOrientationWhenRotating() {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());
        // TODO(b/110533226): Fix test on devices with display cutout
        assumeFalse("Skipping test: display cutout present, can't predict exact lifecycle",
                hasDisplayCutout());
        disableIgnoreOrientationRequest();

        // Start portrait-fixed activity
        separateTestJournal();
        final ActivitySession activitySession = createManagedActivityClientSession()
                .startActivity(getLaunchActivityBuilder()
                        .setUseInstrumentation()
                        .setWindowingMode(WINDOWING_MODE_FULLSCREEN)
                        .setTargetActivity(RESIZEABLE_ACTIVITY));
        mWmState.assertVisibility(RESIZEABLE_ACTIVITY, true /* visible */);

        final int displayId = mWmState.getDisplayByActivity(RESIZEABLE_ACTIVITY);

        final RotationSession rotationSession = createManagedRotationSession();
        assumeTrue("Skipping test: no user locked rotation support.",
                supportsLockedUserRotation(rotationSession, displayId));

        launchActivity(PORTRAIT_ORIENTATION_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        assumeNotIgnoringOrientation(PORTRAIT_ORIENTATION_ACTIVITY);
        mWmState.assertVisibility(PORTRAIT_ORIENTATION_ACTIVITY, true /* visible */);
        final SizeInfo initialSize = activitySession.getConfigInfo().sizeInfo;

        // Rotate the display and check that the orientation doesn't change
        rotationSession.set(ROTATION_0);
        final int[] rotations = { ROTATION_270, ROTATION_180, ROTATION_90, ROTATION_0 };
        for (final int rotation : rotations) {
            separateTestJournal();
            rotationSession.set(rotation, false /* waitDeviceRotation */);

            // Verify lifecycle count and orientation changes.
            assertRelaunchOrConfigChanged(PORTRAIT_ORIENTATION_ACTIVITY, 0 /* numRelaunch */,
                    0 /* numConfigChange */);
            final SizeInfo currentSize = activitySession.getConfigInfo().sizeInfo;
            assertEquals("Sizes must not be changed", initialSize, currentSize);
            assertRelaunchOrConfigChanged(RESIZEABLE_ACTIVITY, 0 /* numRelaunch */,
                    0 /* numConfigChange */);
        }
    }

    /**
     * Test that device handles moving between two tasks with different orientations.
     */
    @Test
    public void testTaskMoveToBackOrientation() {
        assumeTrue("Skipping test: no orientation request support", supportsOrientationRequest());
        disableIgnoreOrientationRequest();

        // Start landscape activity.
        launchActivity(LANDSCAPE_ORIENTATION_ACTIVITY, WINDOWING_MODE_FULLSCREEN);
        assumeNotIgnoringOrientation(LANDSCAPE_ORIENTATION_ACTIVITY);
        mWmState.assertVisibility(LANDSCAPE_ORIENTATION_ACTIVITY, true /* visible */);
        final boolean isFixedToUserRotation = mWmState.isFixedToUserRotation();
        if (!isFixedToUserRotation) {
            mWmState.waitAndAssertLastOrientation(
                        "Fullscreen app requested landscape orientation",
                        SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            final SizeInfo reportedSizes =
                    getLastReportedSizesForActivity(LANDSCAPE_ORIENTATION_ACTIVITY);
            assertEquals("landscape activity should be in landscape",
                    ORIENTATION_LANDSCAPE, reportedSizes.orientation);
            assertTrue("landscape activity should have height < width",
                    reportedSizes.heightDp < reportedSizes.widthDp);
        }

        // Start another activity in a different task.
        launchActivityInNewTask(BROADCAST_RECEIVER_ACTIVITY);

        // Request portrait
        mBroadcastActionTrigger.requestOrientation(SCREEN_ORIENTATION_PORTRAIT);
        mWmState.waitForLastOrientation(SCREEN_ORIENTATION_PORTRAIT);
        waitForBroadcastActivityReady(ORIENTATION_PORTRAIT);

        // Finish activity
        mBroadcastActionTrigger.moveTopTaskToBack();

        // Verify that activity brought to front is in originally requested orientation.
        mWmState.waitForValidState(LANDSCAPE_ORIENTATION_ACTIVITY);
        if (!isFixedToUserRotation) {
            mWmState.waitAndAssertLastOrientation(
                        "Fullscreen app requested landscape orientation",
                        SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            final SizeInfo reportedSizes =
                    getLastReportedSizesForActivity(LANDSCAPE_ORIENTATION_ACTIVITY);
            assertEquals("landscape activity should be in landscape",
                    ORIENTATION_LANDSCAPE, reportedSizes.orientation);
            assertTrue("landscape activity should have height < width",
                    reportedSizes.heightDp < reportedSizes.widthDp);
        }
    }

    /**
     * Test that device doesn't change device orientation by app request while in multi-window.
     */
    @Test
    public void testSplitscreenPortraitAppOrientationRequests() throws Exception {
        requestOrientationInSplitScreen(createManagedRotationSession(),
                isDisplayPortrait() ? ROTATION_0 : ROTATION_90, LANDSCAPE_ORIENTATION_ACTIVITY);
    }

    /**
     * Test that device doesn't change device orientation by app request while in multi-window.
     */
    @Test
    public void testSplitscreenLandscapeAppOrientationRequests() throws Exception {
        requestOrientationInSplitScreen(createManagedRotationSession(),
                isDisplayPortrait() ? ROTATION_90 : ROTATION_0, PORTRAIT_ORIENTATION_ACTIVITY);
    }

    /**
     * Launch specified activity in split-screen then rotate the divice, checking orientation
     * should always change by the device rotation.
     */
    private void requestOrientationInSplitScreen(RotationSession rotationSession, int rotation,
            ComponentName activity) throws Exception {
        assumeTrue("Skipping test: no rotation support", supportsRotation());
        assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());

        // Launch activities in split screen.
        launchActivitiesInSplitScreen(
                getLaunchActivityBuilder().setTargetActivity(LAUNCHING_ACTIVITY),
                getLaunchActivityBuilder().setTargetActivity(activity));
        mWmState.assertVisibility(activity, true /* visible */);

        // Rotate the device and it should always rotate regardless orientation app requested.
        rotationSession.set(rotation);
        assertEquals("Split-screen apps shouldn't influence device orientation",
                rotation, mWmState.getRotation());

        // Launch target activity during split-screen again and check orientation still not change.
        mTaskOrganizer.setLaunchRoot(mTaskOrganizer.getSecondarySplitTaskId());
        getLaunchActivityBuilder().setMultipleTask(true).setTargetActivity(activity).execute();
        mWmState.computeState(activity);
        mWmState.assertVisibility(activity, true /* visible */);
        assertEquals("Split-screen apps shouldn't influence device orientation",
                rotation, mWmState.getRotation());
    }

    /**
     * Asserts that after rotation, the aspect ratios of display size, metrics, and configuration
     * have flipped.
     */
    private static void assertSizesRotate(SizeInfo rotationA, SizeInfo rotationB,
            boolean skipOrientationCheck) {
        assertEquals(rotationA.displayWidth, rotationA.metricsWidth);
        assertEquals(rotationA.displayHeight, rotationA.metricsHeight);
        assertEquals(rotationB.displayWidth, rotationB.metricsWidth);
        assertEquals(rotationB.displayHeight, rotationB.metricsHeight);

        if (skipOrientationCheck) {
            // All done if we are not doing orientation check.
            return;
        }
        final boolean beforePortrait = rotationA.displayWidth < rotationA.displayHeight;
        final boolean afterPortrait = rotationB.displayWidth < rotationB.displayHeight;
        assertFalse(beforePortrait == afterPortrait);

        final boolean beforeConfigPortrait = rotationA.widthDp < rotationA.heightDp;
        final boolean afterConfigPortrait = rotationB.widthDp < rotationB.heightDp;
        assertEquals(beforePortrait, beforeConfigPortrait);
        assertEquals(afterPortrait, afterConfigPortrait);

        assertEquals(rotationA.smallestWidthDp, rotationB.smallestWidthDp);
    }

    /**
     * Throws an AssertionError if fullscreenSizes has widths/heights (depending on aspect ratio)
     * that are smaller than the dockedSizes.
     */
    private static void assertSizesAreSane(SizeInfo fullscreenSizes, SizeInfo dockedSizes) {
        final boolean isHorizontalDivision =
                fullscreenSizes.displayHeight - dockedSizes.displayHeight >
                fullscreenSizes.displayWidth - dockedSizes.displayWidth;
        if (isHorizontalDivision) {
            assertThat(dockedSizes.displayHeight, lessThan(fullscreenSizes.displayHeight));
            assertThat(dockedSizes.heightDp, lessThan(fullscreenSizes.heightDp));
            assertThat(dockedSizes.metricsHeight, lessThan(fullscreenSizes.metricsHeight));
        } else {
            assertThat(dockedSizes.displayWidth, lessThan(fullscreenSizes.displayWidth));
            assertThat(dockedSizes.widthDp, lessThan(fullscreenSizes.widthDp));
            assertThat(dockedSizes.metricsWidth, lessThan(fullscreenSizes.metricsWidth));
        }
    }

    /**
     * Throws an AssertionError if sizes are different.
     */
    private static void assertSizesAreSame(SizeInfo firstSize, SizeInfo secondSize) {
        assertEquals(firstSize.widthDp, secondSize.widthDp);
        assertEquals(firstSize.heightDp, secondSize.heightDp);
        assertEquals(firstSize.displayWidth, secondSize.displayWidth);
        assertEquals(firstSize.displayHeight, secondSize.displayHeight);
        assertEquals(firstSize.metricsWidth, secondSize.metricsWidth);
        assertEquals(firstSize.metricsHeight, secondSize.metricsHeight);
        assertEquals(firstSize.smallestWidthDp, secondSize.smallestWidthDp);
    }

    private void waitForBroadcastActivityReady(int orientation) {
        mWmState.waitForActivityOrientation(BROADCAST_RECEIVER_ACTIVITY, orientation);
        mWmState.waitForActivityState(BROADCAST_RECEIVER_ACTIVITY, STATE_RESUMED);
    }

    /**
     * Test launching an activity which requests specific UI mode during creation.
     */
    @Test
    public void testLaunchWithUiModeChange() {
        // Launch activity that changes UI mode and handles this configuration change.
        launchActivity(NIGHT_MODE_ACTIVITY);
        mWmState.waitForActivityState(NIGHT_MODE_ACTIVITY, STATE_RESUMED);

        // Check if activity is launched successfully.
        mWmState.assertVisibility(NIGHT_MODE_ACTIVITY, true /* visible */);
        mWmState.assertFocusedActivity("Launched activity should be focused",
                NIGHT_MODE_ACTIVITY);
        mWmState.assertResumedActivity("Launched activity must be resumed", NIGHT_MODE_ACTIVITY);
    }

    @Test
    public void testAppConfigurationMatchesActivityInMultiWindow() throws Exception {
        assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());

        final ActivitySession activitySession = createManagedActivityClientSession()
                .startActivity(getLaunchActivityBuilder()
                        .setUseInstrumentation()
                        .setTargetActivity(RESIZEABLE_ACTIVITY)
                        .setWindowingMode(WINDOWING_MODE_FULLSCREEN));
        putActivityInPrimarySplit(RESIZEABLE_ACTIVITY);
        SizeInfo dockedActivitySizes = getActivitySizeInfo(activitySession);
        SizeInfo applicationSizes = getAppSizeInfo(activitySession);
        assertSizesAreSame(dockedActivitySizes, applicationSizes);

        // Move the activity to fullscreen and check that the size was updated
        separateTestJournal();
        mTaskOrganizer.dismissSplitScreen(true /* primaryOnTop */);
        waitForOrFail("Activity and application configuration must match",
                () -> activityAndAppSizesMatch(activitySession));
        final SizeInfo fullscreenSizes = getLastReportedSizesForActivity(RESIZEABLE_ACTIVITY);
        applicationSizes = getAppSizeInfo(activitySession);
        assertSizesAreSane(fullscreenSizes, dockedActivitySizes);
        assertSizesAreSame(fullscreenSizes, applicationSizes);

        // Move the activity to docked size again, check if the sizes were updated
        separateTestJournal();
        putActivityInPrimarySplit(RESIZEABLE_ACTIVITY);
        waitForOrFail("Activity and application configuration must match",
                () -> activityAndAppSizesMatch(activitySession));
        dockedActivitySizes = getActivitySizeInfo(activitySession);
        applicationSizes = getAppSizeInfo(activitySession);
        assertSizesAreSane(fullscreenSizes, dockedActivitySizes);
        assertSizesAreSame(dockedActivitySizes, applicationSizes);
    }

    @Test
    public void testAppConfigurationMatchesTopActivityInMultiWindow() throws Exception {
        assumeTrue("Skipping test: no multi-window support", supportsSplitScreenMultiWindow());

        // Launch initial activity in fullscreen and assert sizes
        final ActivitySession fullscreenActivitySession = createManagedActivityClientSession()
                .startActivity(getLaunchActivityBuilder()
                        .setUseInstrumentation()
                        .setTargetActivity(TEST_ACTIVITY)
                        .setWindowingMode(WINDOWING_MODE_FULLSCREEN));
        SizeInfo fullscreenActivitySizes = getActivitySizeInfo(fullscreenActivitySession);
        SizeInfo applicationSizes = getAppSizeInfo(fullscreenActivitySession);
        assertSizesAreSame(fullscreenActivitySizes, applicationSizes);

        // Launch second activity in split-screen and assert that sizes were updated
        separateTestJournal();
        final ActivitySession secondActivitySession = createManagedActivityClientSession()
                .startActivity(getLaunchActivityBuilder()
                        .setUseInstrumentation()
                        .setTargetActivity(RESIZEABLE_ACTIVITY)
                        .setNewTask(true)
                        .setMultipleTask(true));
        putActivityInPrimarySplit(RESIZEABLE_ACTIVITY);
        waitForOrFail("Activity and application configuration must match",
                () -> activityAndAppSizesMatch(secondActivitySession));
        SizeInfo dockedActivitySizes = getActivitySizeInfo(secondActivitySession);
        applicationSizes = getAppSizeInfo(secondActivitySession);
        assertSizesAreSame(dockedActivitySizes, applicationSizes);
        assertSizesAreSane(fullscreenActivitySizes, dockedActivitySizes);

        // Launch third activity in secondary split-screen and assert that sizes were updated
        separateTestJournal();
        final ActivitySession thirdActivitySession = createManagedActivityClientSession()
                .startActivity(getLaunchActivityBuilder()
                        .setUseInstrumentation()
                        .setTargetActivity(RESIZEABLE_ACTIVITY)
                        .setNewTask(true)
                        .setMultipleTask(true));
        putActivityInPrimarySplit(RESIZEABLE_ACTIVITY);
        waitForOrFail("Activity and application configuration must match",
                () -> activityAndAppSizesMatch(thirdActivitySession));
        SizeInfo secondarySplitActivitySizes = getActivitySizeInfo(thirdActivitySession);
        applicationSizes = getAppSizeInfo(thirdActivitySession);
        assertSizesAreSame(secondarySplitActivitySizes, applicationSizes);
        assertSizesAreSane(fullscreenActivitySizes, secondarySplitActivitySizes);
    }

    @Test
    public void testAppConfigurationMatchesActivityInFreeform() throws Exception {
        assumeTrue("Skipping test: no freeform support", supportsFreeform());

        // Launch activity in freeform and assert sizes
        final ActivitySession freeformActivitySession = createManagedActivityClientSession()
                .startActivity(getLaunchActivityBuilder()
                        .setUseInstrumentation()
                        .setTargetActivity(TEST_ACTIVITY)
                        .setWindowingMode(WINDOWING_MODE_FREEFORM));
        SizeInfo freeformActivitySizes = getActivitySizeInfo(freeformActivitySession);
        SizeInfo applicationSizes = getAppSizeInfo(freeformActivitySession);
        assertSizesAreSame(freeformActivitySizes, applicationSizes);
    }

    private boolean activityAndAppSizesMatch(ActivitySession activitySession) {
        final SizeInfo activitySize = activitySession.getConfigInfo().sizeInfo;
        final SizeInfo appSize = activitySession.getAppConfigInfo().sizeInfo;
        return activitySize.equals(appSize);
    }

    private SizeInfo getActivitySizeInfo(ActivitySession activitySession) {
        return activitySession.getConfigInfo().sizeInfo;
    }

    private SizeInfo getAppSizeInfo(ActivitySession activitySession) {
        return activitySession.getAppConfigInfo().sizeInfo;
    }

    private void assumeNotIgnoringOrientation(ComponentName activityName) {
        assumeFalse("Skipping test: display area is ignoring orientation request",
                getWmState().isTaskDisplayAreaIgnoringOrientationRequest(activityName));
    }
}