blob: a15b74b3c52f9692727660313148634ea017805d [file] [log] [blame]
Wale Ogunwaled63594a2016-07-18 07:48:30 -07001/*
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
17package com.android.server.wm;
18
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070019import org.junit.Test;
20import org.junit.runner.RunWith;
21
22import android.support.test.filters.SmallTest;
23import android.support.test.runner.AndroidJUnit4;
Wale Ogunwaled63594a2016-07-18 07:48:30 -070024
25import java.util.Comparator;
Wale Ogunwaled1c37912016-08-16 03:19:39 -070026import java.util.LinkedList;
Wale Ogunwaled63594a2016-07-18 07:48:30 -070027
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070028import static org.junit.Assert.assertEquals;
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertNotNull;
31import static org.junit.Assert.assertNull;
32import static org.junit.Assert.assertTrue;
33
Wale Ogunwaled63594a2016-07-18 07:48:30 -070034/**
35 * Test class for {@link WindowContainer}.
36 *
37 * Build: mmma -j32 frameworks/base/services/tests/servicestests
Wale Ogunwaled1c37912016-08-16 03:19:39 -070038 * Install: adb install -r out/target/product/$TARGET_PRODUCT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk
Wale Ogunwaled63594a2016-07-18 07:48:30 -070039 * Run: adb shell am instrument -w -e class com.android.server.wm.WindowContainerTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
40 */
41@SmallTest
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070042@RunWith(AndroidJUnit4.class)
43public class WindowContainerTests {
Wale Ogunwaled63594a2016-07-18 07:48:30 -070044
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070045 @Test
Wale Ogunwaled63594a2016-07-18 07:48:30 -070046 public void testCreation() throws Exception {
Wale Ogunwaled1c37912016-08-16 03:19:39 -070047 final TestWindowContainer w = new TestWindowContainerBuilder().setLayer(0).build();
Wale Ogunwaled63594a2016-07-18 07:48:30 -070048 assertNull("window must have no parent", w.getParentWindow());
49 assertEquals("window must have no children", 0, w.getChildrenCount());
50 }
51
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070052 @Test
Wale Ogunwaled63594a2016-07-18 07:48:30 -070053 public void testAdd() throws Exception {
Wale Ogunwaled1c37912016-08-16 03:19:39 -070054 final TestWindowContainerBuilder builder = new TestWindowContainerBuilder();
55 final TestWindowContainer root = builder.setLayer(0).build();
Wale Ogunwaled63594a2016-07-18 07:48:30 -070056
Wale Ogunwaled1c37912016-08-16 03:19:39 -070057 final TestWindowContainer layer1 = root.addChildWindow(builder.setLayer(1));
58 final TestWindowContainer secondLayer1 = root.addChildWindow(builder.setLayer(1));
59 final TestWindowContainer layer2 = root.addChildWindow(builder.setLayer(2));
60 final TestWindowContainer layerNeg1 = root.addChildWindow(builder.setLayer(-1));
61 final TestWindowContainer layerNeg2 = root.addChildWindow(builder.setLayer(-2));
62 final TestWindowContainer secondLayerNeg1 = root.addChildWindow(builder.setLayer(-1));
63 final TestWindowContainer layer0 = root.addChildWindow(builder.setLayer(0));
Wale Ogunwaled63594a2016-07-18 07:48:30 -070064
65 assertEquals(7, root.getChildrenCount());
66
67 assertEquals(root, layer1.getParentWindow());
68 assertEquals(root, secondLayer1.getParentWindow());
69 assertEquals(root, layer2.getParentWindow());
70 assertEquals(root, layerNeg1.getParentWindow());
71 assertEquals(root, layerNeg2.getParentWindow());
72 assertEquals(root, secondLayerNeg1.getParentWindow());
73 assertEquals(root, layer0.getParentWindow());
74
75 assertEquals(layerNeg2, root.getChildAt(0));
76 assertEquals(secondLayerNeg1, root.getChildAt(1));
77 assertEquals(layerNeg1, root.getChildAt(2));
78 assertEquals(layer0, root.getChildAt(3));
79 assertEquals(layer1, root.getChildAt(4));
80 assertEquals(secondLayer1, root.getChildAt(5));
81 assertEquals(layer2, root.getChildAt(6));
82 }
83
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070084 @Test
Wale Ogunwaled63594a2016-07-18 07:48:30 -070085 public void testHasChild() throws Exception {
Wale Ogunwaled1c37912016-08-16 03:19:39 -070086 final TestWindowContainerBuilder builder = new TestWindowContainerBuilder();
87 final TestWindowContainer root = builder.setLayer(0).build();
Wale Ogunwaled63594a2016-07-18 07:48:30 -070088
Wale Ogunwaled1c37912016-08-16 03:19:39 -070089 final TestWindowContainer child1 = root.addChildWindow();
90 final TestWindowContainer child2 = root.addChildWindow();
91 final TestWindowContainer child11 = child1.addChildWindow();
92 final TestWindowContainer child12 = child1.addChildWindow();
93 final TestWindowContainer child21 = child2.addChildWindow();
Wale Ogunwaled63594a2016-07-18 07:48:30 -070094
95 assertEquals(2, root.getChildrenCount());
96 assertEquals(2, child1.getChildrenCount());
97 assertEquals(1, child2.getChildrenCount());
98
99 assertTrue(root.hasChild(child1));
100 assertTrue(root.hasChild(child2));
101 assertTrue(root.hasChild(child11));
102 assertTrue(root.hasChild(child12));
103 assertTrue(root.hasChild(child21));
104
105 assertTrue(child1.hasChild(child11));
106 assertTrue(child1.hasChild(child12));
107 assertFalse(child1.hasChild(child21));
108
109 assertTrue(child2.hasChild(child21));
110 assertFalse(child2.hasChild(child11));
111 assertFalse(child2.hasChild(child12));
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700112 }
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700113
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700114 @Test
Wale Ogunwale571771c2016-08-26 13:18:50 -0700115 public void testRemoveImmediately() throws Exception {
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700116 final TestWindowContainerBuilder builder = new TestWindowContainerBuilder();
117 final TestWindowContainer root = builder.setLayer(0).build();
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700118
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700119 final TestWindowContainer child1 = root.addChildWindow();
120 final TestWindowContainer child2 = root.addChildWindow();
121 final TestWindowContainer child11 = child1.addChildWindow();
122 final TestWindowContainer child12 = child1.addChildWindow();
123 final TestWindowContainer child21 = child2.addChildWindow();
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700124
Wale Ogunwale571771c2016-08-26 13:18:50 -0700125 assertNotNull(child12.getParentWindow());
126 child12.removeImmediately();
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700127 assertNull(child12.getParentWindow());
128 assertEquals(1, child1.getChildrenCount());
129 assertFalse(child1.hasChild(child12));
130 assertFalse(root.hasChild(child12));
131
Wale Ogunwale571771c2016-08-26 13:18:50 -0700132 assertTrue(root.hasChild(child2));
133 assertNotNull(child2.getParentWindow());
134 child2.removeImmediately();
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700135 assertNull(child2.getParentWindow());
136 assertNull(child21.getParentWindow());
137 assertEquals(0, child2.getChildrenCount());
138 assertEquals(1, root.getChildrenCount());
139 assertFalse(root.hasChild(child2));
140 assertFalse(root.hasChild(child21));
141
142 assertTrue(root.hasChild(child1));
143 assertTrue(root.hasChild(child11));
144
Wale Ogunwale571771c2016-08-26 13:18:50 -0700145 root.removeImmediately();
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700146 assertEquals(0, root.getChildrenCount());
147 }
148
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700149 @Test
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700150 public void testDetachFromDisplay() throws Exception {
151 final TestWindowContainerBuilder builder = new TestWindowContainerBuilder();
152 final TestWindowContainer root = builder.setLayer(0).build();
153
154 final TestWindowContainer child1 = root.addChildWindow(builder.setCanDetach(true));
155 final TestWindowContainer child2 = root.addChildWindow();
156 final TestWindowContainer child11 = child1.addChildWindow();
157 final TestWindowContainer child12 = child1.addChildWindow(builder.setCanDetach(true));
158 final TestWindowContainer child21 = child2.addChildWindow();
159
160 assertTrue(root.detachFromDisplay());
161 assertTrue(child1.detachFromDisplay());
162 assertFalse(child11.detachFromDisplay());
163 assertTrue(child12.detachFromDisplay());
164 assertFalse(child2.detachFromDisplay());
165 assertFalse(child21.detachFromDisplay());
166 }
167
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700168 @Test
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700169 public void testIsAnimating() throws Exception {
170 final TestWindowContainerBuilder builder = new TestWindowContainerBuilder();
171 final TestWindowContainer root = builder.setLayer(0).build();
172
173 final TestWindowContainer child1 = root.addChildWindow(builder.setIsAnimating(true));
174 final TestWindowContainer child2 = root.addChildWindow();
175 final TestWindowContainer child11 = child1.addChildWindow();
176 final TestWindowContainer child12 = child1.addChildWindow(builder.setIsAnimating(true));
177 final TestWindowContainer child21 = child2.addChildWindow();
178
179 assertTrue(root.isAnimating());
180 assertTrue(child1.isAnimating());
181 assertFalse(child11.isAnimating());
182 assertTrue(child12.isAnimating());
183 assertFalse(child2.isAnimating());
184 assertFalse(child21.isAnimating());
185 }
186
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700187 @Test
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700188 public void testIsVisible() throws Exception {
189 final TestWindowContainerBuilder builder = new TestWindowContainerBuilder();
190 final TestWindowContainer root = builder.setLayer(0).build();
191
192 final TestWindowContainer child1 = root.addChildWindow(builder.setIsVisible(true));
193 final TestWindowContainer child2 = root.addChildWindow();
194 final TestWindowContainer child11 = child1.addChildWindow();
195 final TestWindowContainer child12 = child1.addChildWindow(builder.setIsVisible(true));
196 final TestWindowContainer child21 = child2.addChildWindow();
197
198 assertTrue(root.isVisible());
199 assertTrue(child1.isVisible());
200 assertFalse(child11.isVisible());
201 assertTrue(child12.isVisible());
202 assertFalse(child2.isVisible());
203 assertFalse(child21.isVisible());
204 }
205
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700206 @Test
Wale Ogunwale571771c2016-08-26 13:18:50 -0700207 public void testDetachChild() throws Exception {
208 final TestWindowContainerBuilder builder = new TestWindowContainerBuilder();
209 final TestWindowContainer root = builder.setLayer(0).build();
210 final TestWindowContainer child1 = root.addChildWindow();
211 final TestWindowContainer child2 = root.addChildWindow();
212 final TestWindowContainer child11 = child1.addChildWindow();
213 final TestWindowContainer child21 = child2.addChildWindow();
214
215 assertTrue(root.hasChild(child2));
216 assertTrue(root.hasChild(child21));
217 root.detachChild(child2);
218 assertFalse(root.hasChild(child2));
219 assertFalse(root.hasChild(child21));
220 assertNull(child2.getParentWindow());
221
222 boolean gotException = false;
223 assertTrue(root.hasChild(child11));
224 try {
225 // Can only detach our direct children.
226 root.detachChild(child11);
227 } catch (IllegalArgumentException e) {
228 gotException = true;
229 }
230 assertTrue(gotException);
231 }
232
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700233 /* Used so we can gain access to some protected members of the {@link WindowContainer} class */
234 private class TestWindowContainer extends WindowContainer {
235 private final int mLayer;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700236 private final LinkedList<String> mUsers = new LinkedList();
237 private final boolean mCanDetach;
238 private boolean mIsAnimating;
239 private boolean mIsVisible;
Wale Ogunwale571771c2016-08-26 13:18:50 -0700240 private int mRemoveIfPossibleCount;
241 private int mRemoveImmediatelyCount;
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700242
243 /**
244 * Compares 2 window layers and returns -1 if the first is lesser than the second in terms
245 * of z-order and 1 otherwise.
246 */
247 private final Comparator<WindowContainer> mWindowSubLayerComparator = (w1, w2) -> {
248 final int layer1 = ((TestWindowContainer)w1).mLayer;
249 final int layer2 = ((TestWindowContainer)w2).mLayer;
250 if (layer1 < layer2 || (layer1 == layer2 && layer2 < 0 )) {
251 // We insert the child window into the list ordered by the mLayer.
252 // For same layers, the negative one should go below others; the positive one should
253 // go above others.
254 return -1;
255 }
256 return 1;
257 };
258
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700259 TestWindowContainer(int layer, LinkedList<String> users, boolean canDetach,
260 boolean isAnimating, boolean isVisible) {
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700261 mLayer = layer;
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700262 mUsers.addAll(users);
263 mCanDetach = canDetach;
264 mIsAnimating = isAnimating;
265 mIsVisible = isVisible;
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700266 }
267
268 TestWindowContainer getParentWindow() {
269 return (TestWindowContainer) getParent();
270 }
271
272 int getChildrenCount() {
273 return mChildren.size();
274 }
275
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700276 TestWindowContainer addChildWindow(TestWindowContainerBuilder childBuilder) {
277 TestWindowContainer child = childBuilder.build();
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700278 addChild(child, mWindowSubLayerComparator);
279 return child;
280 }
281
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700282 TestWindowContainer addChildWindow() {
283 return addChildWindow(new TestWindowContainerBuilder().setLayer(1));
284 }
285
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700286 TestWindowContainer getChildAt(int index) {
287 return (TestWindowContainer) mChildren.get(index);
288 }
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700289
290 @Override
291 boolean detachFromDisplay() {
292 return super.detachFromDisplay() || mCanDetach;
293 }
294
295 @Override
296 boolean isAnimating() {
297 return mIsAnimating || super.isAnimating();
298 }
299
300 @Override
301 boolean isVisible() {
302 return mIsVisible || super.isVisible();
303 }
Wale Ogunwale571771c2016-08-26 13:18:50 -0700304
305 @Override
306 void removeImmediately() {
307 super.removeImmediately();
308 mRemoveImmediatelyCount++;
309 }
310
311 @Override
312 void removeIfPossible() {
313 super.removeIfPossible();
314 mRemoveIfPossibleCount++;
315 }
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700316 }
317
Wale Ogunwaled1c37912016-08-16 03:19:39 -0700318 private class TestWindowContainerBuilder {
319 private int mLayer;
320 private LinkedList<String> mUsers = new LinkedList();
321 private boolean mCanDetach;
322 private boolean mIsAnimating;
323 private boolean mIsVisible;
324
325 TestWindowContainerBuilder setLayer(int layer) {
326 mLayer = layer;
327 return this;
328 }
329
330 TestWindowContainerBuilder addUser(String user) {
331 mUsers.add(user);
332 return this;
333 }
334
335 TestWindowContainerBuilder setCanDetach(boolean canDetach) {
336 mCanDetach = canDetach;
337 return this;
338 }
339
340 TestWindowContainerBuilder setIsAnimating(boolean isAnimating) {
341 mIsAnimating = isAnimating;
342 return this;
343 }
344
345 TestWindowContainerBuilder setIsVisible(boolean isVisible) {
346 mIsVisible = isVisible;
347 return this;
348 }
349
350 TestWindowContainerBuilder reset() {
351 mLayer = 0;
352 mUsers.clear();
353 mCanDetach = false;
354 mIsAnimating = false;
355 mIsVisible = false;
356 return this;
357 }
358
359 TestWindowContainer build() {
360 return new TestWindowContainer(mLayer, mUsers, mCanDetach, mIsAnimating, mIsVisible);
361 }
362 }
Wale Ogunwaled63594a2016-07-18 07:48:30 -0700363}