Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 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 com.android.server.wm; |
| 18 | |
| 19 | import android.test.AndroidTestCase; |
| 20 | import android.test.suitebuilder.annotation.SmallTest; |
| 21 | |
| 22 | import java.util.Comparator; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 23 | import java.util.LinkedList; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Test class for {@link WindowContainer}. |
| 27 | * |
| 28 | * Build: mmma -j32 frameworks/base/services/tests/servicestests |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 29 | * Install: adb install -r out/target/product/$TARGET_PRODUCT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 30 | * Run: adb shell am instrument -w -e class com.android.server.wm.WindowContainerTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner |
| 31 | */ |
| 32 | @SmallTest |
| 33 | public class WindowContainerTests extends AndroidTestCase { |
| 34 | |
| 35 | public void testCreation() throws Exception { |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 36 | final TestWindowContainer w = new TestWindowContainerBuilder().setLayer(0).build(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 37 | assertNull("window must have no parent", w.getParentWindow()); |
| 38 | assertEquals("window must have no children", 0, w.getChildrenCount()); |
| 39 | } |
| 40 | |
| 41 | public void testAdd() throws Exception { |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 42 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(); |
| 43 | final TestWindowContainer root = builder.setLayer(0).build(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 44 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 45 | final TestWindowContainer layer1 = root.addChildWindow(builder.setLayer(1)); |
| 46 | final TestWindowContainer secondLayer1 = root.addChildWindow(builder.setLayer(1)); |
| 47 | final TestWindowContainer layer2 = root.addChildWindow(builder.setLayer(2)); |
| 48 | final TestWindowContainer layerNeg1 = root.addChildWindow(builder.setLayer(-1)); |
| 49 | final TestWindowContainer layerNeg2 = root.addChildWindow(builder.setLayer(-2)); |
| 50 | final TestWindowContainer secondLayerNeg1 = root.addChildWindow(builder.setLayer(-1)); |
| 51 | final TestWindowContainer layer0 = root.addChildWindow(builder.setLayer(0)); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 52 | |
| 53 | assertEquals(7, root.getChildrenCount()); |
| 54 | |
| 55 | assertEquals(root, layer1.getParentWindow()); |
| 56 | assertEquals(root, secondLayer1.getParentWindow()); |
| 57 | assertEquals(root, layer2.getParentWindow()); |
| 58 | assertEquals(root, layerNeg1.getParentWindow()); |
| 59 | assertEquals(root, layerNeg2.getParentWindow()); |
| 60 | assertEquals(root, secondLayerNeg1.getParentWindow()); |
| 61 | assertEquals(root, layer0.getParentWindow()); |
| 62 | |
| 63 | assertEquals(layerNeg2, root.getChildAt(0)); |
| 64 | assertEquals(secondLayerNeg1, root.getChildAt(1)); |
| 65 | assertEquals(layerNeg1, root.getChildAt(2)); |
| 66 | assertEquals(layer0, root.getChildAt(3)); |
| 67 | assertEquals(layer1, root.getChildAt(4)); |
| 68 | assertEquals(secondLayer1, root.getChildAt(5)); |
| 69 | assertEquals(layer2, root.getChildAt(6)); |
| 70 | } |
| 71 | |
| 72 | public void testHasChild() throws Exception { |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 73 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(); |
| 74 | final TestWindowContainer root = builder.setLayer(0).build(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 75 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 76 | final TestWindowContainer child1 = root.addChildWindow(); |
| 77 | final TestWindowContainer child2 = root.addChildWindow(); |
| 78 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 79 | final TestWindowContainer child12 = child1.addChildWindow(); |
| 80 | final TestWindowContainer child21 = child2.addChildWindow(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 81 | |
| 82 | assertEquals(2, root.getChildrenCount()); |
| 83 | assertEquals(2, child1.getChildrenCount()); |
| 84 | assertEquals(1, child2.getChildrenCount()); |
| 85 | |
| 86 | assertTrue(root.hasChild(child1)); |
| 87 | assertTrue(root.hasChild(child2)); |
| 88 | assertTrue(root.hasChild(child11)); |
| 89 | assertTrue(root.hasChild(child12)); |
| 90 | assertTrue(root.hasChild(child21)); |
| 91 | |
| 92 | assertTrue(child1.hasChild(child11)); |
| 93 | assertTrue(child1.hasChild(child12)); |
| 94 | assertFalse(child1.hasChild(child21)); |
| 95 | |
| 96 | assertTrue(child2.hasChild(child21)); |
| 97 | assertFalse(child2.hasChild(child11)); |
| 98 | assertFalse(child2.hasChild(child12)); |
| 99 | } |
| 100 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame^] | 101 | public void testRemoveImmediately() throws Exception { |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 102 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(); |
| 103 | final TestWindowContainer root = builder.setLayer(0).build(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 104 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 105 | final TestWindowContainer child1 = root.addChildWindow(); |
| 106 | final TestWindowContainer child2 = root.addChildWindow(); |
| 107 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 108 | final TestWindowContainer child12 = child1.addChildWindow(); |
| 109 | final TestWindowContainer child21 = child2.addChildWindow(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 110 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame^] | 111 | assertNotNull(child12.getParentWindow()); |
| 112 | child12.removeImmediately(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 113 | assertNull(child12.getParentWindow()); |
| 114 | assertEquals(1, child1.getChildrenCount()); |
| 115 | assertFalse(child1.hasChild(child12)); |
| 116 | assertFalse(root.hasChild(child12)); |
| 117 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame^] | 118 | assertTrue(root.hasChild(child2)); |
| 119 | assertNotNull(child2.getParentWindow()); |
| 120 | child2.removeImmediately(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 121 | assertNull(child2.getParentWindow()); |
| 122 | assertNull(child21.getParentWindow()); |
| 123 | assertEquals(0, child2.getChildrenCount()); |
| 124 | assertEquals(1, root.getChildrenCount()); |
| 125 | assertFalse(root.hasChild(child2)); |
| 126 | assertFalse(root.hasChild(child21)); |
| 127 | |
| 128 | assertTrue(root.hasChild(child1)); |
| 129 | assertTrue(root.hasChild(child11)); |
| 130 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame^] | 131 | root.removeImmediately(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 132 | assertEquals(0, root.getChildrenCount()); |
| 133 | } |
| 134 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 135 | public void testDetachFromDisplay() throws Exception { |
| 136 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(); |
| 137 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 138 | |
| 139 | final TestWindowContainer child1 = root.addChildWindow(builder.setCanDetach(true)); |
| 140 | final TestWindowContainer child2 = root.addChildWindow(); |
| 141 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 142 | final TestWindowContainer child12 = child1.addChildWindow(builder.setCanDetach(true)); |
| 143 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 144 | |
| 145 | assertTrue(root.detachFromDisplay()); |
| 146 | assertTrue(child1.detachFromDisplay()); |
| 147 | assertFalse(child11.detachFromDisplay()); |
| 148 | assertTrue(child12.detachFromDisplay()); |
| 149 | assertFalse(child2.detachFromDisplay()); |
| 150 | assertFalse(child21.detachFromDisplay()); |
| 151 | } |
| 152 | |
| 153 | public void testIsAnimating() throws Exception { |
| 154 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(); |
| 155 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 156 | |
| 157 | final TestWindowContainer child1 = root.addChildWindow(builder.setIsAnimating(true)); |
| 158 | final TestWindowContainer child2 = root.addChildWindow(); |
| 159 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 160 | final TestWindowContainer child12 = child1.addChildWindow(builder.setIsAnimating(true)); |
| 161 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 162 | |
| 163 | assertTrue(root.isAnimating()); |
| 164 | assertTrue(child1.isAnimating()); |
| 165 | assertFalse(child11.isAnimating()); |
| 166 | assertTrue(child12.isAnimating()); |
| 167 | assertFalse(child2.isAnimating()); |
| 168 | assertFalse(child21.isAnimating()); |
| 169 | } |
| 170 | |
| 171 | public void testIsVisible() throws Exception { |
| 172 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(); |
| 173 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 174 | |
| 175 | final TestWindowContainer child1 = root.addChildWindow(builder.setIsVisible(true)); |
| 176 | final TestWindowContainer child2 = root.addChildWindow(); |
| 177 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 178 | final TestWindowContainer child12 = child1.addChildWindow(builder.setIsVisible(true)); |
| 179 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 180 | |
| 181 | assertTrue(root.isVisible()); |
| 182 | assertTrue(child1.isVisible()); |
| 183 | assertFalse(child11.isVisible()); |
| 184 | assertTrue(child12.isVisible()); |
| 185 | assertFalse(child2.isVisible()); |
| 186 | assertFalse(child21.isVisible()); |
| 187 | } |
| 188 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame^] | 189 | public void testDetachChild() throws Exception { |
| 190 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(); |
| 191 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 192 | final TestWindowContainer child1 = root.addChildWindow(); |
| 193 | final TestWindowContainer child2 = root.addChildWindow(); |
| 194 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 195 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 196 | |
| 197 | assertTrue(root.hasChild(child2)); |
| 198 | assertTrue(root.hasChild(child21)); |
| 199 | root.detachChild(child2); |
| 200 | assertFalse(root.hasChild(child2)); |
| 201 | assertFalse(root.hasChild(child21)); |
| 202 | assertNull(child2.getParentWindow()); |
| 203 | |
| 204 | boolean gotException = false; |
| 205 | assertTrue(root.hasChild(child11)); |
| 206 | try { |
| 207 | // Can only detach our direct children. |
| 208 | root.detachChild(child11); |
| 209 | } catch (IllegalArgumentException e) { |
| 210 | gotException = true; |
| 211 | } |
| 212 | assertTrue(gotException); |
| 213 | } |
| 214 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 215 | /* Used so we can gain access to some protected members of the {@link WindowContainer} class */ |
| 216 | private class TestWindowContainer extends WindowContainer { |
| 217 | private final int mLayer; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 218 | private final LinkedList<String> mUsers = new LinkedList(); |
| 219 | private final boolean mCanDetach; |
| 220 | private boolean mIsAnimating; |
| 221 | private boolean mIsVisible; |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame^] | 222 | private int mRemoveIfPossibleCount; |
| 223 | private int mRemoveImmediatelyCount; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * Compares 2 window layers and returns -1 if the first is lesser than the second in terms |
| 227 | * of z-order and 1 otherwise. |
| 228 | */ |
| 229 | private final Comparator<WindowContainer> mWindowSubLayerComparator = (w1, w2) -> { |
| 230 | final int layer1 = ((TestWindowContainer)w1).mLayer; |
| 231 | final int layer2 = ((TestWindowContainer)w2).mLayer; |
| 232 | if (layer1 < layer2 || (layer1 == layer2 && layer2 < 0 )) { |
| 233 | // We insert the child window into the list ordered by the mLayer. |
| 234 | // For same layers, the negative one should go below others; the positive one should |
| 235 | // go above others. |
| 236 | return -1; |
| 237 | } |
| 238 | return 1; |
| 239 | }; |
| 240 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 241 | TestWindowContainer(int layer, LinkedList<String> users, boolean canDetach, |
| 242 | boolean isAnimating, boolean isVisible) { |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 243 | mLayer = layer; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 244 | mUsers.addAll(users); |
| 245 | mCanDetach = canDetach; |
| 246 | mIsAnimating = isAnimating; |
| 247 | mIsVisible = isVisible; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | TestWindowContainer getParentWindow() { |
| 251 | return (TestWindowContainer) getParent(); |
| 252 | } |
| 253 | |
| 254 | int getChildrenCount() { |
| 255 | return mChildren.size(); |
| 256 | } |
| 257 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 258 | TestWindowContainer addChildWindow(TestWindowContainerBuilder childBuilder) { |
| 259 | TestWindowContainer child = childBuilder.build(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 260 | addChild(child, mWindowSubLayerComparator); |
| 261 | return child; |
| 262 | } |
| 263 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 264 | TestWindowContainer addChildWindow() { |
| 265 | return addChildWindow(new TestWindowContainerBuilder().setLayer(1)); |
| 266 | } |
| 267 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 268 | TestWindowContainer getChildAt(int index) { |
| 269 | return (TestWindowContainer) mChildren.get(index); |
| 270 | } |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 271 | |
| 272 | @Override |
| 273 | boolean detachFromDisplay() { |
| 274 | return super.detachFromDisplay() || mCanDetach; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | boolean isAnimating() { |
| 279 | return mIsAnimating || super.isAnimating(); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | boolean isVisible() { |
| 284 | return mIsVisible || super.isVisible(); |
| 285 | } |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame^] | 286 | |
| 287 | @Override |
| 288 | void removeImmediately() { |
| 289 | super.removeImmediately(); |
| 290 | mRemoveImmediatelyCount++; |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | void removeIfPossible() { |
| 295 | super.removeIfPossible(); |
| 296 | mRemoveIfPossibleCount++; |
| 297 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 300 | private class TestWindowContainerBuilder { |
| 301 | private int mLayer; |
| 302 | private LinkedList<String> mUsers = new LinkedList(); |
| 303 | private boolean mCanDetach; |
| 304 | private boolean mIsAnimating; |
| 305 | private boolean mIsVisible; |
| 306 | |
| 307 | TestWindowContainerBuilder setLayer(int layer) { |
| 308 | mLayer = layer; |
| 309 | return this; |
| 310 | } |
| 311 | |
| 312 | TestWindowContainerBuilder addUser(String user) { |
| 313 | mUsers.add(user); |
| 314 | return this; |
| 315 | } |
| 316 | |
| 317 | TestWindowContainerBuilder setCanDetach(boolean canDetach) { |
| 318 | mCanDetach = canDetach; |
| 319 | return this; |
| 320 | } |
| 321 | |
| 322 | TestWindowContainerBuilder setIsAnimating(boolean isAnimating) { |
| 323 | mIsAnimating = isAnimating; |
| 324 | return this; |
| 325 | } |
| 326 | |
| 327 | TestWindowContainerBuilder setIsVisible(boolean isVisible) { |
| 328 | mIsVisible = isVisible; |
| 329 | return this; |
| 330 | } |
| 331 | |
| 332 | TestWindowContainerBuilder reset() { |
| 333 | mLayer = 0; |
| 334 | mUsers.clear(); |
| 335 | mCanDetach = false; |
| 336 | mIsAnimating = false; |
| 337 | mIsVisible = false; |
| 338 | return this; |
| 339 | } |
| 340 | |
| 341 | TestWindowContainer build() { |
| 342 | return new TestWindowContainer(mLayer, mUsers, mCanDetach, mIsAnimating, mIsVisible); |
| 343 | } |
| 344 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 345 | } |