xref: /aosp_15_r20/cts/tests/app/src/android/app/cts/DisplayTest.java (revision b7c941bb3fa97aba169d73cee0bed2de8ac964bf)
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package android.app.cts;
18 
19 import static com.android.compatibility.common.util.PackageUtil.supportsRotation;
20 
21 import static com.google.common.truth.Truth.assertWithMessage;
22 
23 import android.app.ActivityOptions;
24 import android.app.stubs.DisplayTestActivity;
25 import android.app.stubs.OrientationTestUtils;
26 import android.content.Intent;
27 import android.graphics.Point;
28 import android.server.wm.SetRequestedOrientationRule;
29 import android.util.Pair;
30 import android.view.Display;
31 
32 import androidx.test.platform.app.InstrumentationRegistry;
33 
34 import com.android.compatibility.common.util.ApiTest;
35 
36 import org.junit.Rule;
37 import org.junit.Test;
38 
39 /**
40  * Tests to verify functionality of {@link Display}.
41  */
42 public class DisplayTest {
43     @Rule public SetRequestedOrientationRule mSetRequestedOrientationRule =
44             new SetRequestedOrientationRule();
45 
46     /**
47      * Tests that the underlying {@link android.view.DisplayAdjustments} in {@link Display} updates.
48      * The method {@link DisplayTestActivity#getDisplay()} fetches the Display directly from the
49      * {@link android.view.WindowManager}. A display fetched before the rotation should have the
50      * updated adjustments after a rotation.
51      */
52     @Test
53     @ApiTest(apis = {"android.content.Context#getDisplay"})
testRotation()54     public void testRotation() throws Throwable {
55         if (!supportsRotation()) {
56             // Skip rotation test if device doesn't support it.
57             return;
58         }
59 
60         final Pair<Intent, ActivityOptions> launchArgs =
61                 SetRequestedOrientationRule.buildFullScreenLaunchArgs(DisplayTestActivity.class);
62 
63         final DisplayTestActivity activity = (DisplayTestActivity) InstrumentationRegistry
64                 .getInstrumentation()
65                 .startActivitySync(launchArgs.first, launchArgs.second.toBundle());
66         try {
67             // Get a {@link Display} instance before rotation.
68             final Display origDisplay = activity.getDisplay();
69 
70             // Capture the originally reported width and heights
71             final Point origSize = new Point();
72             origDisplay.getRealSize(origSize);
73 
74             // Change orientation
75             activity.configurationChangeObserver.startObserving();
76             OrientationTestUtils.switchOrientation(activity);
77 
78             final boolean closeToSquareBounds =
79                     OrientationTestUtils.isCloseToSquareBounds(activity);
80 
81             // Don't wait for the configuration to change if
82             // the display is square. In many cases it won't.
83             if (!closeToSquareBounds) {
84                 activity.configurationChangeObserver.await();
85             }
86 
87             final Point newOrigSize = new Point();
88             origDisplay.getRealSize(newOrigSize);
89 
90             // Get a {@link Display} instance after rotation.
91             final Display updatedDisplay = activity.getDisplay();
92             final Point updatedSize = new Point();
93             updatedDisplay.getRealSize(updatedSize);
94 
95             // For square screens the following assertions do not make sense and will always
96             // fail.
97             if (!closeToSquareBounds) {
98                 // Ensure that the width and height of the original instance no longer are the
99                 // same.
100                 // Note that this will be false if the device width and height are identical.
101                 // Note there are cases where width and height may not all be updated, such as
102                 // on docked devices where the app is letterboxed. However, at least one
103                 // dimension needs to be updated.
104                 assertWithMessage(
105                         "size from original display instance should have changed")
106                         .that(origSize).isNotEqualTo(newOrigSize);
107             }
108 
109             // Ensure that the width and height of the original instance have been updated to
110             // match the values that would be found in a new instance.
111             assertWithMessage(
112                     "size from original display instance should match current")
113                     .that(newOrigSize).isEqualTo(updatedSize);
114         } finally {
115             InstrumentationRegistry.getInstrumentation().runOnMainSync(activity::finish);
116         }
117     }
118 }
119