blob: bd0e0dfb49007edfb8c32e002b5b9cb592228f34 [file] [log] [blame]
Wale Ogunwale822e5122017-07-26 06:02:24 -07001/*
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
17package com.android.server.wm;
18
19import org.junit.Test;
20
21import android.app.WindowConfiguration;
22import android.content.res.Configuration;
23import android.graphics.Rect;
24import android.platform.test.annotations.Presubmit;
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27import android.view.DisplayInfo;
28
29import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
Wale Ogunwale687b4272017-07-27 02:56:23 -070030import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
31import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwale822e5122017-07-26 06:02:24 -070032import static android.app.WindowConfiguration.WINDOW_CONFIG_APP_BOUNDS;
Wale Ogunwale687b4272017-07-27 02:56:23 -070033import static android.app.WindowConfiguration.WINDOW_CONFIG_WINDOWING_MODE;
Wale Ogunwale822e5122017-07-26 06:02:24 -070034import static android.content.pm.ActivityInfo.CONFIG_WINDOW_CONFIGURATION;
35import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertNotEquals;
37import static org.junit.Assert.assertTrue;
38
39/**
40 * Test class to for {@link android.app.WindowConfiguration}.
41 *
42 * Build/Install/Run:
43 * bit FrameworksServicesTests:com.android.server.wm.WindowConfigurationTests
44 */
45@SmallTest
46@Presubmit
47@org.junit.runner.RunWith(AndroidJUnit4.class)
48public class WindowConfigurationTests extends WindowTestsBase {
49 private Rect mParentBounds;
50
51 @Override
52 public void setUp() throws Exception {
53 super.setUp();
54 mParentBounds = new Rect(10 /*left*/, 30 /*top*/, 80 /*right*/, 60 /*bottom*/);
55 }
56
57 /** Tests {@link android.app.WindowConfiguration#diff(WindowConfiguration, boolean)}. */
58 @Test
59 public void testDiff() {
60 final Configuration config1 = new Configuration();
61 final WindowConfiguration winConfig1 = config1.windowConfiguration;
62 final Configuration config2 = new Configuration();
63 final WindowConfiguration winConfig2 = config2.windowConfiguration;
64 final Configuration config3 = new Configuration();
65 final WindowConfiguration winConfig3 = config3.windowConfiguration;
66
67 winConfig1.setAppBounds(0, 1, 1, 0);
68 winConfig2.setAppBounds(1, 2, 2, 1);
69 winConfig3.setAppBounds(winConfig1.getAppBounds());
70
71
72 assertEquals(CONFIG_WINDOW_CONFIGURATION, config1.diff(config2));
73 assertEquals(0, config1.diffPublicOnly(config2));
74 assertEquals(WINDOW_CONFIG_APP_BOUNDS,
75 winConfig1.diff(winConfig2, false /* compareUndefined */));
76
Wale Ogunwale687b4272017-07-27 02:56:23 -070077 winConfig2.setWindowingMode(WINDOWING_MODE_FREEFORM);
78 assertEquals(WINDOW_CONFIG_APP_BOUNDS | WINDOW_CONFIG_WINDOWING_MODE,
79 winConfig1.diff(winConfig2, false /* compareUndefined */));
80
Wale Ogunwale822e5122017-07-26 06:02:24 -070081 assertEquals(0, config1.diff(config3));
82 assertEquals(0, config1.diffPublicOnly(config3));
83 assertEquals(0, winConfig1.diff(winConfig3, false /* compareUndefined */));
84 }
85
86 /** Tests {@link android.app.WindowConfiguration#compareTo(WindowConfiguration)}. */
87 @Test
88 public void testConfigurationCompareTo() throws Exception {
89 final Configuration blankConfig = new Configuration();
90 final WindowConfiguration blankWinConfig = new WindowConfiguration();
91
92 final Configuration config1 = new Configuration();
93 final WindowConfiguration winConfig1 = config1.windowConfiguration;
94 winConfig1.setAppBounds(1, 2, 3, 4);
95
96 final Configuration config2 = new Configuration(config1);
97 final WindowConfiguration winConfig2 = config2.windowConfiguration;
98
99 assertEquals(config1.compareTo(config2), 0);
100 assertEquals(winConfig1.compareTo(winConfig2), 0);
101
Wale Ogunwale687b4272017-07-27 02:56:23 -0700102 // Different windowing mode
103 winConfig2.setWindowingMode(WINDOWING_MODE_FREEFORM);
104 assertNotEquals(config1.compareTo(config2), 0);
105 assertNotEquals(winConfig1.compareTo(winConfig2), 0);
106 winConfig2.setWindowingMode(winConfig1.getWindowingMode());
107
Wale Ogunwale822e5122017-07-26 06:02:24 -0700108 // Different bounds
109 winConfig2.setAppBounds(0, 2, 3, 4);
Wale Ogunwale822e5122017-07-26 06:02:24 -0700110 assertNotEquals(config1.compareTo(config2), 0);
111 assertNotEquals(winConfig1.compareTo(winConfig2), 0);
112
113 // No bounds
114 assertEquals(config1.compareTo(blankConfig), -1);
115 assertEquals(winConfig1.compareTo(blankWinConfig), -1);
116
117 assertEquals(blankConfig.compareTo(config1), 1);
118 assertEquals(blankWinConfig.compareTo(winConfig1), 1);
119 }
120
121 /** Ensures the configuration app bounds at the root level match the app dimensions. */
122 @Test
123 public void testAppBounds_RootConfigurationBounds() throws Exception {
124 final DisplayInfo info = mDisplayContent.getDisplayInfo();
125 info.appWidth = 1024;
126 info.appHeight = 768;
127
128 final Rect appBounds = sWm.computeNewConfiguration(
129 mDisplayContent.getDisplayId()).windowConfiguration.getAppBounds();
130 // The bounds should always be positioned in the top left.
131 assertEquals(appBounds.left, 0);
132 assertEquals(appBounds.top, 0);
133
134 // The bounds should equal the defined app width and height
135 assertEquals(appBounds.width(), info.appWidth);
136 assertEquals(appBounds.height(), info.appHeight);
137 }
138
139 /** Ensures that bounds are clipped to their parent. */
140 @Test
141 public void testAppBounds_BoundsClipping() throws Exception {
142 final Rect shiftedBounds = new Rect(mParentBounds);
143 shiftedBounds.offset(10, 10);
144 final Rect expectedBounds = new Rect(mParentBounds);
145 expectedBounds.intersect(shiftedBounds);
146 testStackBoundsConfiguration(null /*stackId*/, mParentBounds, shiftedBounds,
147 expectedBounds);
148 }
149
150 /** Ensures that empty bounds are not propagated to the configuration. */
151 @Test
152 public void testAppBounds_EmptyBounds() throws Exception {
153 final Rect emptyBounds = new Rect();
154 testStackBoundsConfiguration(null /*stackId*/, mParentBounds, emptyBounds,
155 null /*ExpectedBounds*/);
156 }
157
158 /** Ensures that bounds on freeform stacks are not clipped. */
159 @Test
160 public void testAppBounds_FreeFormBounds() throws Exception {
161 final Rect freeFormBounds = new Rect(mParentBounds);
162 freeFormBounds.offset(10, 10);
163 testStackBoundsConfiguration(FREEFORM_WORKSPACE_STACK_ID, mParentBounds, freeFormBounds,
164 freeFormBounds);
165 }
166
167 /** Ensures that fully contained bounds are not clipped. */
168 @Test
169 public void testAppBounds_ContainedBounds() throws Exception {
170 final Rect insetBounds = new Rect(mParentBounds);
171 insetBounds.inset(5, 5, 5, 5);
172 testStackBoundsConfiguration(null /*stackId*/, mParentBounds, insetBounds, insetBounds);
173 }
174
175 /** Ensures that full screen free form bounds are clipped */
176 @Test
177 public void testAppBounds_FullScreenFreeFormBounds() throws Exception {
178 final Rect fullScreenBounds = new Rect(0, 0, mDisplayInfo.logicalWidth,
179 mDisplayInfo.logicalHeight);
180 testStackBoundsConfiguration(null /*stackId*/, mParentBounds, fullScreenBounds,
181 mParentBounds);
182 }
183
184 private void testStackBoundsConfiguration(Integer stackId, Rect parentBounds, Rect bounds,
185 Rect expectedConfigBounds) {
186 final StackWindowController stackController = stackId != null ?
187 createStackControllerOnStackOnDisplay(stackId, mDisplayContent)
188 : createStackControllerOnDisplay(mDisplayContent);
189
190 final Configuration parentConfig = mDisplayContent.getConfiguration();
191 parentConfig.windowConfiguration.setAppBounds(parentBounds);
192
193 final Configuration config = new Configuration();
194 final WindowConfiguration winConfig = config.windowConfiguration;
195 stackController.adjustConfigurationForBounds(bounds, null /*insetBounds*/,
196 new Rect() /*nonDecorBounds*/, new Rect() /*stableBounds*/, false /*overrideWidth*/,
197 false /*overrideHeight*/, mDisplayInfo.logicalDensityDpi, config, parentConfig);
198 // Assert that both expected and actual are null or are equal to each other
199
200 assertTrue((expectedConfigBounds == null && winConfig.getAppBounds() == null)
201 || expectedConfigBounds.equals(winConfig.getAppBounds()));
202 }
203
204}