1 /*
2  * Copyright (C) 2016 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.server.wm;
18 
19 import static android.server.wm.ComponentNameUtils.getActivityName;
20 import static android.server.wm.app.Components.TEST_ACTIVITY;
21 import static android.view.Display.DEFAULT_DISPLAY;
22 
23 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
24 
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assume.assumeFalse;
28 import static org.junit.Assume.assumeTrue;
29 
30 import android.content.Context;
31 import android.content.res.Configuration;
32 import android.hardware.display.DisplayManager;
33 import android.platform.test.annotations.Presubmit;
34 import android.server.wm.WindowManagerState.DisplayContent;
35 import android.util.Size;
36 import android.view.Display;
37 
38 import org.junit.Test;
39 
40 import java.util.List;
41 
42 /**
43  * Build/Install/Run:
44  *     atest CtsWindowManagerDeviceTestCases:DisplayTests
45  */
46 @Presubmit
47 @android.server.wm.annotation.Group3
48 public class DisplayTests extends MultiDisplayTestBase {
49 
50     /**
51      * Tests that the global configuration is equal to the default display's override configuration.
52      */
53     @Test
testDefaultDisplayOverrideConfiguration()54     public void testDefaultDisplayOverrideConfiguration() throws Exception {
55         final List<DisplayContent> reportedDisplays = getDisplaysStates();
56         final DisplayContent primaryDisplay = getDisplayState(reportedDisplays, DEFAULT_DISPLAY);
57         assertEquals("Primary display's configuration should be equal to global configuration.",
58                 primaryDisplay.mOverrideConfiguration, primaryDisplay.mFullConfiguration);
59         assertEquals("Primary display's configuration should be equal to global configuration.",
60                 primaryDisplay.mOverrideConfiguration, primaryDisplay.mMergedOverrideConfiguration);
61     }
62 
63     /**
64      * Tests that config_remoteInsetsControllerControlsSystemBars is not set to true for
65      * non-automotive devices.
66      */
67     @Test
testRemoteInsetsControllerNotControlSystemBarsForNonAutoDevies()68     public void testRemoteInsetsControllerNotControlSystemBarsForNonAutoDevies() {
69         assumeFalse(isCar());
70 
71         assertFalse("Non auto devices should not set config_remoteInsetsControllerControlsSystemBars",
72                 remoteInsetsControllerControlsSystemBars());
73     }
74 
75     /**
76      * Tests that secondary display has override configuration set.
77      */
78     @Test
testCreateVirtualDisplayWithCustomConfig()79     public void testCreateVirtualDisplayWithCustomConfig() throws Exception {
80         final DisplayContent newDisplay = createManagedVirtualDisplaySession().createDisplay();
81 
82         // Find the density of created display.
83         final int newDensityDpi = newDisplay.mFullConfiguration.densityDpi;
84         assertEquals(CUSTOM_DENSITY_DPI, newDensityDpi);
85     }
86 
87     @Test
testNonDefaultDisplayResourcesConfiguration()88     public void testNonDefaultDisplayResourcesConfiguration() throws Exception {
89         final int smallDisplaySize = 1000;
90         final int longDisplaySize = 1920;
91 
92         // Get land-sized display's resources configuration.
93         final int smallestScreenWidthForLandDisplay = getDisplayResourcesConfiguration(
94                 longDisplaySize, smallDisplaySize).smallestScreenWidthDp;
95 
96         // Get port-sized display's resources configuration.
97         final int smallestScreenWidthForPortDisplay = getDisplayResourcesConfiguration(
98                 smallDisplaySize, longDisplaySize).smallestScreenWidthDp;
99 
100         // Check whether smallestScreenWidthDp configuration is proper.
101         assertEquals("smallestScreenWidthDp configuration should be set to smallest possible size.",
102                 smallestScreenWidthForLandDisplay, smallestScreenWidthForPortDisplay);
103     }
104 
105     /**
106      * Tests that launch on secondary display is not permitted if device has the feature disabled.
107      * Activities requested to be launched on a secondary display in this case should land on the
108      * default display.
109      */
110     @Test
testMultiDisplayDisabled()111     public void testMultiDisplayDisabled() throws Exception {
112         // Only check devices with the feature disabled.
113         assumeFalse(supportsMultiDisplay());
114 
115         // Create new virtual display.
116         final DisplayContent newDisplay = createManagedVirtualDisplaySession().createDisplay();
117 
118         // Launch activity on new secondary display.
119         launchActivityOnDisplay(TEST_ACTIVITY, newDisplay.mId);
120         mWmState.computeState(TEST_ACTIVITY);
121 
122         mWmState.assertFocusedActivity("Launched activity must be focused",
123                 TEST_ACTIVITY);
124 
125         // Check that activity is on the right display.
126         final int frontStackId = mWmState.getFrontRootTaskId(DEFAULT_DISPLAY);
127         final WindowManagerState.ActivityTask frontStack =
128                 mWmState.getRootTask(frontStackId);
129         assertEquals("Launched activity must be resumed",
130                 getActivityName(TEST_ACTIVITY), frontStack.mResumedActivity);
131         assertEquals("Front stack must be on the default display",
132                 DEFAULT_DISPLAY, frontStack.mDisplayId);
133         mWmState.assertFocusedStack("Focus must be on the default display", frontStackId);
134     }
135 
136     @Test
testCreateMultipleVirtualDisplays()137     public void testCreateMultipleVirtualDisplays() throws Exception {
138         final List<DisplayContent> originalDs = getDisplaysStates();
139         try (final VirtualDisplaySession virtualDisplaySession = new VirtualDisplaySession()) {
140             // Create new virtual displays
141             virtualDisplaySession.createDisplays(3);
142             getDisplayStateAfterChange(originalDs.size() + 3);
143         }
144         getDisplayStateAfterChange(originalDs.size());
145     }
146 
147     /**
148      * Test that display overrides apply correctly and won't be affected by display changes.
149      * This sets overrides to display size and density, initiates a display changed event by locking
150      * and unlocking the phone and verifies that overrides are kept.
151      */
152     @Test
testForceDisplayMetrics()153     public void testForceDisplayMetrics() {
154         assumeTrue(supportsLockScreen());
155 
156         launchHomeActivity();
157 
158         final DisplayMetricsSession displayMetricsSession =
159                 createManagedDisplayMetricsSession(DEFAULT_DISPLAY);
160         final LockScreenSession lockScreenSession = createManagedLockScreenSession();
161 
162         // Read initial sizes.
163         final ReportedDisplayMetrics originalDisplayMetrics =
164                 displayMetricsSession.getInitialDisplayMetrics();
165 
166         // Apply new override values that don't match the physical metrics.
167         final Size overrideSize = new Size(
168                 (int) (originalDisplayMetrics.physicalSize.getWidth() * 1.5),
169                 (int) (originalDisplayMetrics.physicalSize.getHeight() * 1.5));
170         final Integer overrideDensity = (int) (originalDisplayMetrics.physicalDensity * 1.1);
171         displayMetricsSession.overrideDisplayMetrics(overrideSize, overrideDensity);
172 
173         // Check if overrides applied correctly.
174         ReportedDisplayMetrics displayMetrics = displayMetricsSession.getDisplayMetrics();
175         assertEquals(overrideSize, displayMetrics.overrideSize);
176         assertEquals(overrideDensity, displayMetrics.overrideDensity);
177 
178         // Lock and unlock device. This will cause a DISPLAY_CHANGED event to be triggered and
179         // might update the metrics.
180         lockScreenSession.sleepDevice()
181                 .wakeUpDevice()
182                 .unlockDevice();
183         mWmState.waitForHomeActivityVisible();
184 
185         // Check if overrides are still applied.
186         displayMetrics = displayMetricsSession.getDisplayMetrics();
187         assertEquals(overrideSize, displayMetrics.overrideSize);
188         assertEquals(overrideDensity, displayMetrics.overrideDensity);
189     }
190 
getDisplayResourcesConfiguration(int displayWidth, int displayHeight)191     private Configuration getDisplayResourcesConfiguration(int displayWidth, int displayHeight)
192             throws Exception  {
193         final Context context = getInstrumentation().getContext();
194 
195         try (final VirtualDisplaySession virtualDisplaySession = new VirtualDisplaySession()) {
196             final DisplayContent displayContent = virtualDisplaySession
197                     .setSimulateDisplay(true)
198                     .setSimulationDisplaySize(displayWidth, displayHeight)
199                     .createDisplay();
200             final Display display = mDm.getDisplay(displayContent.mId);
201             Configuration config = context.createDisplayContext(display)
202                     .getResources().getConfiguration();
203             return config;
204         }
205     }
206 }
207