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