summaryrefslogtreecommitdiff
path: root/tests/framework/base/windowmanager/jetpack/src/android/server/wm/jetpack/layout/ExtensionWindowLayoutComponentTest.java
blob: 2d943da579536cb90ab08e6435bd39d89ebbf276 (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
/*
 * Copyright (C) 2023 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.jetpack.layout;

import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static android.server.wm.jetpack.extensions.util.ExtensionsUtil.EXTENSION_VERSION_2;
import static android.server.wm.jetpack.extensions.util.ExtensionsUtil.assertEqualWindowLayoutInfo;
import static android.server.wm.jetpack.extensions.util.ExtensionsUtil.assumeHasDisplayFeatures;
import static android.server.wm.jetpack.extensions.util.ExtensionsUtil.getExtensionWindowLayoutInfo;
import static android.server.wm.jetpack.extensions.util.ExtensionsUtil.getWindowExtensions;
import static android.server.wm.jetpack.extensions.util.ExtensionsUtil.isExtensionVersionAtLeast;
import static android.server.wm.jetpack.extensions.util.SidecarUtil.assumeSidecarSupportedDevice;
import static android.server.wm.jetpack.extensions.util.SidecarUtil.getSidecarInterface;
import static android.view.Display.DEFAULT_DISPLAY;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static androidx.window.extensions.layout.FoldingFeature.STATE_FLAT;
import static androidx.window.extensions.layout.FoldingFeature.STATE_HALF_OPENED;
import static androidx.window.extensions.layout.FoldingFeature.TYPE_FOLD;
import static androidx.window.extensions.layout.FoldingFeature.TYPE_HINGE;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeTrue;

import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Rect;
import android.hardware.devicestate.DeviceStateManager;
import android.hardware.devicestate.DeviceStateRequest;
import android.hardware.display.DisplayManager;
import android.platform.test.annotations.Presubmit;
import android.server.wm.DeviceStateUtils;
import android.server.wm.DisplayMetricsSession;
import android.server.wm.SetRequestedOrientationRule;
import android.server.wm.jetpack.extensions.util.TestValueCountConsumer;
import android.server.wm.jetpack.utils.TestActivity;
import android.server.wm.jetpack.utils.TestConfigChangeHandlingActivity;
import android.server.wm.jetpack.utils.WindowExtensionTestRule;
import android.server.wm.jetpack.utils.WindowManagerJetpackTestBase;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowMetrics;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.FlakyTest;
import androidx.test.filters.LargeTest;
import androidx.window.extensions.layout.DisplayFeature;
import androidx.window.extensions.layout.FoldingFeature;
import androidx.window.extensions.layout.WindowLayoutComponent;
import androidx.window.extensions.layout.WindowLayoutInfo;
import androidx.window.sidecar.SidecarDisplayFeature;
import androidx.window.sidecar.SidecarInterface;

import com.android.compatibility.common.util.ApiTest;
import com.android.compatibility.common.util.CddTest;

import com.google.common.collect.BoundType;
import com.google.common.collect.Range;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Tests for the {@link androidx.window.extensions.layout.WindowLayoutComponent} implementation
 * provided on the device (and only if one is available). This class applies
 * {@link SetRequestedOrientationRule} so that screen rotation is not blocked.
 *
 * Build/Install/Run:
 *     atest CtsWindowManagerJetpackTestCases:ExtensionWindowLayoutComponentTest
 */
@Presubmit
@LargeTest
@RunWith(AndroidJUnit4.class)
public class ExtensionWindowLayoutComponentTest extends WindowManagerJetpackTestBase {

    private WindowLayoutComponent mWindowLayoutComponent;
    private WindowLayoutInfo mWindowLayoutInfo;

    @Rule
    public final WindowExtensionTestRule mWindowExtensionTestRule =
            new WindowExtensionTestRule(WindowLayoutComponent.class);

    // To disable special handling which prevents setRequestedOrientation from changing the screen
    // rotation for large screen devices.
    @ClassRule
    public static final SetRequestedOrientationRule sSetRequestedOrientationRule =
            new SetRequestedOrientationRule();

    @Before
    @Override
    public void setUp() {
        super.setUp();
        mWindowLayoutComponent =
                (WindowLayoutComponent) mWindowExtensionTestRule.getExtensionComponent();
        assumeNotNull(mWindowLayoutComponent);
    }

    private Context createContextWithNonActivityWindow() {
        Display defaultDisplay = mContext.getSystemService(DisplayManager.class).getDisplay(
                DEFAULT_DISPLAY);
        Context windowContext = mContext.createWindowContext(defaultDisplay,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, null /* options */);

        mInstrumentation.runOnMainSync(() -> {
            final View view = new View(windowContext);
            WindowManager wm = windowContext.getSystemService(WindowManager.class);
            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
            wm.addView(view, params);
        });
        return windowContext;
    }

    private void assumeExtensionVersionSupportsWindowContextLayout() {
        assumeTrue("This test should only be run on devices with version: ",
                isExtensionVersionAtLeast(EXTENSION_VERSION_2));
    }

    /**
     * Test adding and removing a window layout change listener.
     */
    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutComponent#addWindowLayoutInfoListener"})
    public void testWindowLayoutComponent_onWindowLayoutChangeListener() throws Exception {
        TestActivity testActivity = startFullScreenActivityNewTask(
                TestActivity.class, null /* activityId */);
        changeActivityOrientationThenVerifyWindowLayout(testActivity, testActivity);
    }

    /**
     * Test adding and removing a window layout change listener with a wrapped activity context.
     */
    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutComponent#addWindowLayoutInfoListener"})
    public void testWindowLayoutComponent_onWindowLayoutChangeListener_wrappedContext()
            throws Exception {
        TestActivity testActivity = startFullScreenActivityNewTask(
                TestActivity.class, null /* activityId */);
        Context wrappedContext = new ContextWrapper(testActivity);
        WindowLayoutInfo windowLayoutInfoFromContext = getExtensionWindowLayoutInfo(testActivity);
        WindowLayoutInfo windowLayoutInfoFromWrappedContext =
                getExtensionWindowLayoutInfo(wrappedContext);
        assertEquals(windowLayoutInfoFromContext, windowLayoutInfoFromWrappedContext);

        changeActivityOrientationThenVerifyWindowLayout(testActivity, wrappedContext);
    }

    /**
     * Test adding and removing a window layout change listener with a window context.
     */
    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutComponent#addWindowLayoutInfoListener"})
    public void testWindowLayoutComponent_onWindowLayoutChangeListener_windowContext()
            throws Exception {
        try (DisplayMetricsSession displaySession = new DisplayMetricsSession(DEFAULT_DISPLAY)) {
            Context context = createContextWithNonActivityWindow();
            changeDisplayMetricThenVerifyWindowLayout(context, displaySession);
        }
    }

    /**
     * Test adding and removing a window layout change listener with a wrapped window context.
     */
    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutComponent#addWindowLayoutInfoListener"})
    public void testWindowLayoutComponent_onWindowLayoutChangeListener_wrappedWindowContext()
            throws Exception {
        try (DisplayMetricsSession displaySession = new DisplayMetricsSession(DEFAULT_DISPLAY)) {
            Context context = createContextWithNonActivityWindow();
            Context wrappedContext = new ContextWrapper(context);
            WindowLayoutInfo windowLayoutInfoFromContext = getExtensionWindowLayoutInfo(context);
            WindowLayoutInfo windowLayoutInfoFromWrappedContext =
                    getExtensionWindowLayoutInfo(wrappedContext);
            assertEquals(windowLayoutInfoFromContext, windowLayoutInfoFromWrappedContext);

            changeDisplayMetricThenVerifyWindowLayout(wrappedContext, displaySession);
        }
    }

    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutComponent#addWindowLayoutInfoListener"})
    public void testWindowLayoutComponent_windowLayoutInfoListener() {
        TestActivity activity = startFullScreenActivityNewTask(TestActivity.class,
                null /* activityId */);
        final TestValueCountConsumer<WindowLayoutInfo> windowLayoutInfoConsumer =
                new TestValueCountConsumer<>();
        // Test that adding and removing callback succeeds
        mWindowLayoutComponent.addWindowLayoutInfoListener(activity, windowLayoutInfoConsumer);
        mWindowLayoutComponent.removeWindowLayoutInfoListener(windowLayoutInfoConsumer);
    }

    /** Test changing device states and verify no crash. */
    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutComponent#addWindowLayoutInfoListener"})
    public void testWindowLayoutComponent_windowLayoutInfoListener_deviceStateChanged()
            throws Throwable {
        final Context instrumentationContext = getInstrumentation().getTargetContext();
        final DeviceStateManager deviceStateManager = instrumentationContext
                .getSystemService(DeviceStateManager.class);
        final int[] supportedDeviceStates = deviceStateManager.getSupportedStates();
        assumeTrue(supportedDeviceStates.length > 1);

        TestActivity testActivity = startFullScreenActivityNewTask(
                TestActivity.class, null /* activityId */);
        TestValueCountConsumer<WindowLayoutInfo> windowLayoutInfoConsumer =
                new TestValueCountConsumer<>();
        windowLayoutInfoConsumer.setCount(1);
        mWindowLayoutComponent.addWindowLayoutInfoListener(testActivity, windowLayoutInfoConsumer);

        // Switch to all device states to verify no crash.
        TestValueCountConsumer<DeviceStateRequest> deviceStateCallbackConsumer =
                new TestValueCountConsumer<>();
        deviceStateCallbackConsumer.setCount(1);
        for (int deviceState : supportedDeviceStates) {
            DeviceStateRequest request = DeviceStateRequest.newBuilder(deviceState).build();
            DeviceStateUtils.runWithControlDeviceStatePermission(() ->
                    deviceStateManager.requestBaseStateOverride(
                            request,
                            getInstrumentation().getTargetContext().getMainExecutor(),
                            new DeviceStateRequest.Callback() {
                                @Override
                                public void onRequestActivated(DeviceStateRequest request) {
                                    deviceStateCallbackConsumer.accept(request);
                                }
                            }));
            deviceStateCallbackConsumer.waitAndGet();
            deviceStateCallbackConsumer.clearQueue();
        }
        DeviceStateUtils.runWithControlDeviceStatePermission(
                deviceStateManager::cancelBaseStateOverride);

        mWindowLayoutComponent.removeWindowLayoutInfoListener(windowLayoutInfoConsumer);
    }

    @ApiTest(apis = {"androidx.window.extensions.layout.WindowLayoutInfo#getDisplayFeatures"})
    @Test
    public void testWindowLayoutComponent_providesWindowLayoutFromActivity()
            throws InterruptedException {
        TestActivity activity = startActivityNewTask(TestActivity.class);
        mWindowLayoutInfo = getExtensionWindowLayoutInfo(activity);
        assumeHasDisplayFeatures(mWindowLayoutInfo);
        for (DisplayFeature displayFeature : mWindowLayoutInfo.getDisplayFeatures()) {
            // Check that the feature bounds are valid
            final Rect featureRect = displayFeature.getBounds();
            // Feature cannot have negative width or height
            assertHasNonNegativeDimensions(featureRect);
            // The feature cannot have zero area
            assertNotBothDimensionsZero(featureRect);
            // The feature cannot be outside the activity bounds
            assertTrue(getActivityBounds(activity).contains(featureRect));

            if (displayFeature instanceof FoldingFeature) {
                // Check that the folding feature has a valid type and state
                final FoldingFeature foldingFeature = (FoldingFeature) displayFeature;
                final int featureType = foldingFeature.getType();
                assertThat(featureType).isIn(Range.range(
                        TYPE_FOLD, BoundType.CLOSED,
                        TYPE_HINGE, BoundType.CLOSED));
                final int featureState = foldingFeature.getState();
                assertThat(featureState).isIn(Range.range(
                        STATE_FLAT, BoundType.CLOSED,
                        STATE_HALF_OPENED, BoundType.CLOSED));
            }
        }
    }

    @Test
    @ApiTest(apis = {"androidx.window.extensions.layout.WindowLayoutInfo#getDisplayFeatures"})
    public void testWindowLayoutComponent_providesWindowLayoutFromWindowContext()
            throws InterruptedException {
        assumeExtensionVersionSupportsWindowContextLayout();
        Context windowContext = createContextWithNonActivityWindow();

        mWindowLayoutInfo = getExtensionWindowLayoutInfo(windowContext);
        assumeHasDisplayFeatures(mWindowLayoutInfo);

        // Verify that window layouts and metrics are reasonable.
        WindowManager mWm = windowContext.getSystemService(WindowManager.class);
        final WindowMetrics currentMetrics = mWm.getCurrentWindowMetrics();

        for (DisplayFeature displayFeature : mWindowLayoutInfo.getDisplayFeatures()) {
            final Rect featureRect = displayFeature.getBounds();
            assertHasNonNegativeDimensions(featureRect);
            assertNotBothDimensionsZero(featureRect);
            assertTrue(currentMetrics.getBounds().contains(featureRect));
        }
    }

    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutComponent#addWindowLayoutInfoListener"})
    public void testWindowLayoutComponent_windowLayoutMatchesBetweenActivityAndWindowContext()
            throws InterruptedException {
        assumeExtensionVersionSupportsWindowContextLayout();
        TestConfigChangeHandlingActivity activity =
                (TestConfigChangeHandlingActivity) startFullScreenActivityNewTask(
                        TestConfigChangeHandlingActivity.class, null /* activityId */);
        Context windowContext = createContextWithNonActivityWindow();

        WindowLayoutInfo windowLayoutInfoFromContext = getExtensionWindowLayoutInfo(windowContext);
        WindowLayoutInfo windowLayoutInfoFromActivity = getExtensionWindowLayoutInfo(activity);

        assertEquals(windowLayoutInfoFromContext, windowLayoutInfoFromActivity);
    }

    @CddTest(requirements = {"7.1.1.1"})
    @ApiTest(apis = {"androidx.window.extensions.layout.WindowLayoutInfo#getDisplayFeatures"})
    @Test
    public void testGetWindowLayoutInfo_configChanged_windowLayoutUpdates()
            throws InterruptedException {
        assumeSupportsRotation();

        final TestConfigChangeHandlingActivity activity = startFullScreenActivityNewTask(
                TestConfigChangeHandlingActivity.class, null /* activityId */);

        mWindowLayoutInfo = getExtensionWindowLayoutInfo(activity);
        assumeHasDisplayFeatures(mWindowLayoutInfo);

        setActivityOrientationActivityHandlesOrientationChanges(activity,
                ORIENTATION_PORTRAIT);
        final WindowLayoutInfo portraitWindowLayoutInfo = getExtensionWindowLayoutInfo(
                activity);
        final Rect portraitBounds = getActivityBounds(activity);
        final Rect portraitMaximumBounds = getMaximumActivityBounds(activity);

        setActivityOrientationActivityHandlesOrientationChanges(activity,
                ORIENTATION_LANDSCAPE);
        final WindowLayoutInfo landscapeWindowLayoutInfo = getExtensionWindowLayoutInfo(
                activity);
        final Rect landscapeBounds = getActivityBounds(activity);
        final Rect landscapeMaximumBounds = getMaximumActivityBounds(activity);

        final boolean doesDisplayRotateForOrientation = doesDisplayRotateForOrientation(
                portraitMaximumBounds, landscapeMaximumBounds);
        assertTrue(doesDisplayRotateForOrientation);
        assertEqualWindowLayoutInfo(portraitWindowLayoutInfo, landscapeWindowLayoutInfo,
                portraitBounds, landscapeBounds, doesDisplayRotateForOrientation);
    }

    /**
     * Test updating the display metrics and verify the updated WindowLayoutInfo.
     */
    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutInfo#getDisplayFeatures"})
    public void testGetWindowLayoutInfo_displayMetricsChanged_windowLayoutUpdates()
            throws Exception {
        try (DisplayMetricsSession displaySession = new DisplayMetricsSession(DEFAULT_DISPLAY)) {
            TestActivity testActivity = startFullScreenActivityNewTask(
                    TestActivity.class, null /* activityId */);
            TestValueCountConsumer<WindowLayoutInfo> windowLayoutInfoConsumer =
                    new TestValueCountConsumer<>();
            windowLayoutInfoConsumer.setCount(1);

            // Get the initial WindowLayoutInfo
            mWindowLayoutComponent.addWindowLayoutInfoListener(
                    testActivity, windowLayoutInfoConsumer);
            WindowLayoutInfo windowLayoutInit = windowLayoutInfoConsumer.waitAndGet();
            assertNotNull(windowLayoutInit);
            windowLayoutInfoConsumer.clearQueue();

            // Update the display metrics and get the updated WindowLayoutInfo
            final double displayResizeRatio = 0.8;
            displaySession.changeDisplayMetrics(displayResizeRatio, 1.0 /* densityRatio */);
            WindowLayoutInfo windowLayoutUpdated = windowLayoutInfoConsumer.waitAndGet();
            assertNotNull(windowLayoutUpdated);
            windowLayoutInfoConsumer.clearQueue();

            assertEquals(
                    windowLayoutInit.getDisplayFeatures().size(),
                    windowLayoutUpdated.getDisplayFeatures().size());

            for (int i = 0; i < windowLayoutInit.getDisplayFeatures().size(); i++) {
                Rect windowLayoutSizeInitBounds =
                        windowLayoutInit.getDisplayFeatures().get(i).getBounds();
                Rect windowLayoutSizeUpdatedBounds =
                        windowLayoutUpdated.getDisplayFeatures().get(i).getBounds();

                // Expect the hinge dimension to shrink in one direction. The actual
                // dimension depends on device implementation.
                assertTrue(
                        windowLayoutSizeInitBounds.width() * displayResizeRatio
                                == windowLayoutSizeUpdatedBounds.width()
                                || windowLayoutSizeInitBounds.height() * displayResizeRatio
                                == windowLayoutSizeUpdatedBounds.height()
                );
            }

            // Clean up
            mWindowLayoutComponent.removeWindowLayoutInfoListener(windowLayoutInfoConsumer);
            displaySession.restoreDisplayMetrics();
        }
    }

    @ApiTest(apis = {"androidx.window.extensions.layout.WindowLayoutInfo#getDisplayFeatures"})
    @Test
    public void testGetWindowLayoutInfo_enterExitPip_windowLayoutInfoMatches()
            throws InterruptedException {
        TestConfigChangeHandlingActivity configHandlingActivity = startActivityNewTask(
                        TestConfigChangeHandlingActivity.class, null);
        mWindowLayoutInfo = getExtensionWindowLayoutInfo(configHandlingActivity);
        assumeHasDisplayFeatures(mWindowLayoutInfo);

        TestValueCountConsumer<WindowLayoutInfo> consumer = new TestValueCountConsumer<>();
        // We expect 3 values, 1 before entering PiP, one while in PiP, one after exiting PiP.
        consumer.setCount(3);
        getWindowExtensions().getWindowLayoutComponent().addWindowLayoutInfoListener(
                configHandlingActivity, consumer);

        enterPipActivityHandlesConfigChanges(configHandlingActivity);
        exitPipActivityHandlesConfigChanges(configHandlingActivity);

        List<WindowLayoutInfo> values = consumer.waitAndGetAllValues();

        assertEquals(3, values.size());
        assertEquals(mWindowLayoutInfo, values.get(0));
        assertTrue(values.get(1).getDisplayFeatures().isEmpty());
        assertEquals(mWindowLayoutInfo, values.get(2));
    }

    /**
     * Similar to {@link #testGetWindowLayoutInfo_configChanged_windowLayoutUpdates}, here we
     * trigger rotations with a full screen activity on one Display Area, verify that
     * WindowLayoutInfo from both Activity and WindowContext are updated with callbacks.
     */
    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutComponent#addWindowLayoutInfoListener",
            "androidx.window.extensions.layout.WindowLayoutComponent#removeWindowLayoutInfoListener"
    })
    public void testWindowLayoutComponent_updatesWindowLayoutFromContextAfterRotation()
            throws InterruptedException {
        assumeExtensionVersionSupportsWindowContextLayout();
        assumeSupportsRotation();

        final TestConfigChangeHandlingActivity activity = startFullScreenActivityNewTask(
                TestConfigChangeHandlingActivity.class, null /* activityId */);

        // Fix the device orientation before the test begins.
        setActivityOrientationActivityHandlesOrientationChanges(activity,
                ORIENTATION_PORTRAIT);

        // Here we make an assumption that the full-screen activity and the APPLICATION_OVERLAY
        // Window are located in the same area on Display.
        Context windowContext = createContextWithNonActivityWindow();
        WindowLayoutInfo firstWindowLayoutContext = getExtensionWindowLayoutInfo(windowContext);
        Rect windowContextBounds = windowContext.getSystemService(
                        WindowManager.class).getCurrentWindowMetrics().getBounds();

        final Rect firstBounds = getActivityBounds(activity);
        final Rect firstMaximumBounds = getMaximumActivityBounds(activity);
        WindowLayoutInfo firstWindowLayoutActivity = getExtensionWindowLayoutInfo(
                activity);
        boolean doesDisplayRotateForOrientation = doesDisplayRotateForOrientation(
                firstMaximumBounds, windowContextBounds);
        assertEqualWindowLayoutInfo(firstWindowLayoutActivity, firstWindowLayoutContext,
                firstBounds, windowContextBounds, doesDisplayRotateForOrientation);

        // Trigger a rotation to the Display via Activity orientation request.
        setActivityOrientationActivityHandlesOrientationChanges(activity,
                ORIENTATION_LANDSCAPE);

        WindowLayoutInfo secondWindowLayoutActivity = getExtensionWindowLayoutInfo(
                activity);
        final Rect secondBounds = getActivityBounds(activity);
        final Rect secondMaximumBounds = getMaximumActivityBounds(activity);

        // We assume after rotation both the Activity and the OVERLAY window are still located
        // in the same area, so their Display Features are still the same.
        WindowLayoutInfo secondWindowLayoutContext =
                getExtensionWindowLayoutInfo(windowContext);
        Rect secondWindowContextBounds = windowContext.getSystemService(
                        WindowManager.class).getCurrentWindowMetrics()
                .getBounds();
        assertEqualWindowLayoutInfo(secondWindowLayoutActivity, secondWindowLayoutContext,
                secondBounds, secondWindowContextBounds,
                doesDisplayRotateForOrientation(secondMaximumBounds,
                        secondWindowContextBounds));

        // Verify Activity Display Feature is consistent regardless of rotation.
        doesDisplayRotateForOrientation = doesDisplayRotateForOrientation(
                firstMaximumBounds, secondMaximumBounds);
        assertEqualWindowLayoutInfo(firstWindowLayoutActivity, secondWindowLayoutActivity,
                firstBounds, secondBounds, doesDisplayRotateForOrientation);
    }

    @Test
    @ApiTest(apis = {
            "androidx.window.extensions.layout.WindowLayoutComponent#addWindowLayoutInfoListener"})
    public void testGetWindowLayoutInfo_windowRecreated_windowLayoutUpdates()
            throws InterruptedException {
        assumeSupportsRotation();
        final TestActivity activity = startFullScreenActivityNewTask(TestActivity.class,
                null /* activityId */);

        mWindowLayoutInfo = getExtensionWindowLayoutInfo(activity);
        assumeHasDisplayFeatures(mWindowLayoutInfo);

        setActivityOrientationActivityDoesNotHandleOrientationChanges(activity,
                ORIENTATION_PORTRAIT);
        final WindowLayoutInfo portraitWindowLayoutInfo =
                getExtensionWindowLayoutInfo(activity);
        final Rect portraitBounds = getActivityBounds(activity);
        final Rect portraitMaximumBounds = getMaximumActivityBounds(activity);

        setActivityOrientationActivityDoesNotHandleOrientationChanges(activity,
                ORIENTATION_LANDSCAPE);
        final WindowLayoutInfo landscapeWindowLayoutInfo =
                getExtensionWindowLayoutInfo(activity);
        final Rect landscapeBounds = getActivityBounds(activity);
        final Rect landscapeMaximumBounds = getMaximumActivityBounds(activity);

        final boolean doesDisplayRotateForOrientation = doesDisplayRotateForOrientation(
                portraitMaximumBounds, landscapeMaximumBounds);
        assertTrue(doesDisplayRotateForOrientation);
        assertEqualWindowLayoutInfo(portraitWindowLayoutInfo, landscapeWindowLayoutInfo,
                portraitBounds, landscapeBounds, doesDisplayRotateForOrientation);
    }

    /**
     * Tests that if sidecar is also present, then it returns the same display features as
     * extensions.
     */
    @CddTest(requirements = {"7.1.1.1"})
    @Test
    @FlakyTest(bugId = 295892511)
    public void testSidecarHasSameDisplayFeatures() throws InterruptedException {
        TestActivity activity = startFullScreenActivityNewTask(TestActivity.class,
                null /* activityId */);
        assumeSidecarSupportedDevice(activity);
        mWindowLayoutInfo = getExtensionWindowLayoutInfo(activity);
        assumeHasDisplayFeatures(mWindowLayoutInfo);

        // Retrieve and sort the extension folding features
        final List<FoldingFeature> extensionFoldingFeatures = new ArrayList<>(
                mWindowLayoutInfo.getDisplayFeatures())
                .stream()
                .filter(d -> d instanceof FoldingFeature)
                .map(d -> (FoldingFeature) d)
                .collect(Collectors.toList());

        // Retrieve and sort the sidecar display features in the same order as the extension
        // display features
        final SidecarInterface sidecarInterface = getSidecarInterface(activity);
        final List<SidecarDisplayFeature> sidecarDisplayFeatures = sidecarInterface
                .getWindowLayoutInfo(getActivityWindowToken(activity)).displayFeatures;

        // Check that the display features are the same
        assertEquals(extensionFoldingFeatures.size(), sidecarDisplayFeatures.size());
        final int nFeatures = extensionFoldingFeatures.size();
        if (nFeatures == 0) {
            return;
        }
        final boolean[] extensionDisplayFeatureMatched = new boolean[nFeatures];
        final boolean[] sidecarDisplayFeatureMatched = new boolean[nFeatures];
        for (int extensionIndex = 0; extensionIndex < nFeatures; extensionIndex++) {
            if (extensionDisplayFeatureMatched[extensionIndex]) {
                // A match has already been found for this extension folding feature
                continue;
            }
            final FoldingFeature extensionFoldingFeature = extensionFoldingFeatures
                    .get(extensionIndex);
            for (int sidecarIndex = 0; sidecarIndex < nFeatures; sidecarIndex++) {
                if (sidecarDisplayFeatureMatched[sidecarIndex]) {
                    // A match has already been found for this sidecar display feature
                    continue;
                }
                final SidecarDisplayFeature sidecarDisplayFeature = sidecarDisplayFeatures
                        .get(sidecarIndex);
                // Check that the bounds, type, and state match
                if (extensionFoldingFeature.getBounds().equals(sidecarDisplayFeature.getRect())
                        && extensionFoldingFeature.getType() == sidecarDisplayFeature.getType()
                        && areExtensionAndSidecarDeviceStateEqual(
                                extensionFoldingFeature.getState(),
                                sidecarInterface.getDeviceState().posture)) {
                    // Match found
                    extensionDisplayFeatureMatched[extensionIndex] = true;
                    sidecarDisplayFeatureMatched[sidecarIndex] = true;
                }
            }
        }

        // Check that a match was found for each display feature
        for (int i = 0; i < nFeatures; i++) {
            assertTrue(extensionDisplayFeatureMatched[i] && sidecarDisplayFeatureMatched[i]);
        }
    }

    /**
     * Tests that the public API for {@link DisplayFeature} matches the API
     * provided on device. The window-extensions artifact is provided by OEMs
     * and may not match the API defined in the androidx repository.
     */
    @Test
    public void testDisplayFeature_publicApi() throws NoSuchMethodException {
        Class<DisplayFeature> displayFeatureClass = DisplayFeature.class;

        /* DisplayFeature Method Validation */
        validateClassInfo(displayFeatureClass,
                "getBounds", new Class<?>[]{}, Rect.class);
    }

    /**
     * Tests that the public API of {@link FoldingFeature} matches the implementation provided.
     */
    @Test
    public void testFoldingFeature_publicApi() throws NoSuchMethodException {
        Class<DisplayFeature> displayFeatureClass = DisplayFeature.class;
        Class<FoldingFeature> foldingFeatureClass = FoldingFeature.class;
        // Assert Instance Hierarchy. Reason: OEMs should not change the class hierarchy.
        assertTrue(displayFeatureClass.isAssignableFrom(foldingFeatureClass));

        // Validate the signature of public methods in FoldingFeature
        validateClassInfo(foldingFeatureClass,
                "toString", new Class<?>[]{}, String.class);
        validateClassInfo(foldingFeatureClass,
                "equals", new Class<?>[]{Object.class}, boolean.class);
        validateClassInfo(foldingFeatureClass,
                "hashCode", new Class<?>[]{}, int.class);

        // Create a FoldingFeature instance with any constructor (just checking it works).
        final int foldType = TYPE_FOLD;
        final int foldState = STATE_FLAT;
        final Rect foldBoundaries = new Rect(0, 1, 1, 0);
        FoldingFeature foldingFeatureInstance =
                new FoldingFeature(foldBoundaries, foldType, foldState);

        /* FoldingFeature Instance Validation */
        assertEquals(foldBoundaries, foldingFeatureInstance.getBounds());
        assertEquals(foldType, foldingFeatureInstance.getType());
        assertEquals(foldState, foldingFeatureInstance.getState());
    }

    /**
     * Tests that the public API of {@link WindowLayoutInfo} matches the implementation provided.
     */
    @Test
    public void testWindowLayoutInfo_publicApi() throws NoSuchMethodException {
        // Create a FoldingFeature that will be added to WindowLayoutInfo as a DisplayFeature
        final Rect foldBoundaries = new Rect(0, 1, 1, 0);
        FoldingFeature foldingFeature = new FoldingFeature(foldBoundaries, TYPE_FOLD, STATE_FLAT);
        // Add the FoldingFeature to a list of DisplayFeatures
        List<DisplayFeature> displayFeatures = new ArrayList<>();
        displayFeatures.add(foldingFeature);
        // Create a WindowLayoutInfo that holds the DisplayFeatures
        WindowLayoutInfo windowLayoutInfo = new WindowLayoutInfo(displayFeatures);

        /* WindowLayoutInfo Instance Validation */
        assertEquals(displayFeatures, windowLayoutInfo.getDisplayFeatures());

        Class<WindowLayoutInfo> windowLayoutInfoClass = WindowLayoutInfo.class;
        // Validate the signature of public methods in WindowLayoutInfo
        validateClassInfo(windowLayoutInfoClass,
                "toString", new Class<?>[]{}, String.class);
        validateClassInfo(windowLayoutInfoClass,
                "equals", new Class<?>[]{Object.class}, boolean.class);
        validateClassInfo(windowLayoutInfoClass,
                "hashCode", new Class<?>[]{}, int.class);
    }

    /**
     * Verifies that the inputClass has the expected access modifiers, function name,
     * input parameters, and return type. Fails if any are not what is expected.
     * @param inputClass: The Class in Question; Type: Class.
     * @param methodName: The Name of the Method to Check; Type: String.
     * @param parameterList: A list of classes representing the input parameter's Data Types.
     * @param returnType: The return type of the method; Type: Class.
     * @throws NoSuchMethodException: If No Class found, throws a NoSuchMethodException error.
     */
    public void validateClassInfo(Class<?> inputClass, String methodName, Class<?>[] parameterList,
            Class<?> returnType) throws NoSuchMethodException {
        // Get the specified method from the class.
        // Fails if it cannot find the methodName with the Correct input parameters.
        Method classMethod = inputClass.getMethod(methodName, parameterList);

        assertEquals(returnType, classMethod.getReturnType());
        // This should not fail if getMethod did not fail.
        assertTrue(Modifier.isPublic(classMethod.getModifiers()));
    }

    private void changeActivityOrientationThenVerifyWindowLayout(
            TestActivity testActivity, Context listenerContext)
            throws Exception {
        // Set activity to portrait
        setActivityOrientationActivityDoesNotHandleOrientationChanges(testActivity,
                ORIENTATION_PORTRAIT);

        // Create the callback, onWindowLayoutChanged should only be called twice in this
        // test, not the third time when the orientation will change because the listener will be
        // removed.
        TestValueCountConsumer<WindowLayoutInfo> windowLayoutInfoConsumer =
                new TestValueCountConsumer<>();
        windowLayoutInfoConsumer.setCount(1);

        // Add window layout listener for mWindowToken - onWindowLayoutChanged should be called
        mWindowLayoutComponent.addWindowLayoutInfoListener(
                listenerContext, windowLayoutInfoConsumer);
        // Initial registration invokes a consumer callback synchronously, clear the queue to
        // make sure there's no residual value or from the first orientation change.
        windowLayoutInfoConsumer.clearQueue();

        // Change the activity orientation - onWindowLayoutChanged should be called
        setActivityOrientationActivityDoesNotHandleOrientationChanges(testActivity,
                ORIENTATION_LANDSCAPE);

        // Check we have received exactly one layout update.
        assertNotNull(windowLayoutInfoConsumer.waitAndGet());


        // Remove the listener
        mWindowLayoutComponent.removeWindowLayoutInfoListener(windowLayoutInfoConsumer);
        windowLayoutInfoConsumer.clearQueue();

        // Change the activity orientation - onWindowLayoutChanged should NOT be called
        setActivityOrientationActivityDoesNotHandleOrientationChanges(testActivity,
                ORIENTATION_PORTRAIT);
        WindowLayoutInfo lastValue = windowLayoutInfoConsumer.waitAndGet();
        assertNull(lastValue);
    }

    private void changeDisplayMetricThenVerifyWindowLayout(
            Context context, DisplayMetricsSession displaySession) throws Exception {
        // Create the callback, onWindowLayoutChanged should only be called twice in this
        // test, not the third time when the orientation will change because the listener will be
        // removed.
        TestValueCountConsumer<WindowLayoutInfo> windowLayoutInfoConsumer =
                new TestValueCountConsumer<>();
        windowLayoutInfoConsumer.setCount(1);

        // Add window layout listener for mWindowToken - onWindowLayoutChanged should be called
        mWindowLayoutComponent.addWindowLayoutInfoListener(context, windowLayoutInfoConsumer);
        // Initial registration invokes a consumer callback synchronously, clear the queue to
        // make sure there's no residual value or from the first orientation change.
        WindowLayoutInfo windowLayoutInit = windowLayoutInfoConsumer.waitAndGet();
        assertNotNull(windowLayoutInit);
        windowLayoutInfoConsumer.clearQueue();

        // Change the display size - onWindowLayoutChanged should be called
        final double displayResizeRatio = 0.8;
        displaySession.changeDisplayMetrics(
                displayResizeRatio,
                1.0 /* densityRatio */);
        WindowLayoutInfo windowLayoutUpdated = windowLayoutInfoConsumer.waitAndGet();

        // Check we have received exactly one layout update.
        assertNotNull(windowLayoutUpdated);
        assertEquals(
                windowLayoutInit.getDisplayFeatures().size(),
                windowLayoutUpdated.getDisplayFeatures().size());

        // Remove the listener
        mWindowLayoutComponent.removeWindowLayoutInfoListener(windowLayoutInfoConsumer);
        windowLayoutInfoConsumer.clearQueue();

        // Restore Display to original size - onWindowLayoutChanged should NOT be called
        displaySession.restoreDisplayMetrics();
        WindowLayoutInfo lastValue = windowLayoutInfoConsumer.waitAndGet();
        assertNull(lastValue);
    }
}