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 |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 14 | * limitations under the License. |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server.wm; |
| 18 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 19 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND; |
| 20 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; |
| 21 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; |
| 22 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET; |
| 23 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 24 | |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 25 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.any; |
| 26 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyFloat; |
| 27 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.eq; |
| 28 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; |
| 29 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.never; |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 30 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy; |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 31 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.times; |
| 32 | import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 33 | import static com.android.server.wm.WindowContainer.POSITION_BOTTOM; |
| 34 | import static com.android.server.wm.WindowContainer.POSITION_TOP; |
| 35 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 36 | import static org.junit.Assert.assertEquals; |
| 37 | import static org.junit.Assert.assertFalse; |
| 38 | import static org.junit.Assert.assertNotNull; |
| 39 | import static org.junit.Assert.assertNull; |
| 40 | import static org.junit.Assert.assertTrue; |
Bryce Lee | d92ae48 | 2018-01-22 13:56:23 -0800 | [diff] [blame] | 41 | |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 42 | import android.content.pm.ActivityInfo; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 43 | import android.content.res.Configuration; |
| 44 | import android.graphics.Rect; |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 45 | import android.os.IBinder; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 46 | import android.platform.test.annotations.Presubmit; |
| 47 | import android.view.SurfaceControl; |
| 48 | import android.view.SurfaceSession; |
| 49 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 50 | import androidx.test.filters.SmallTest; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 51 | |
| 52 | import org.junit.Test; |
Garfield Tan | 49dae10 | 2019-02-04 09:51:59 -0800 | [diff] [blame] | 53 | import org.mockito.Mockito; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 54 | |
| 55 | import java.util.Comparator; |
| 56 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Test class for {@link WindowContainer}. |
| 59 | * |
Wale Ogunwale | f7cab10 | 2016-10-25 15:25:14 -0700 | [diff] [blame] | 60 | * Build/Install/Run: |
Yunfan Chen | d552c16 | 2019-02-08 16:51:28 +0900 | [diff] [blame] | 61 | * atest WmTests:WindowContainerTests |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 62 | */ |
| 63 | @SmallTest |
Wale Ogunwale | 5fc7096 | 2016-09-09 22:36:19 -0700 | [diff] [blame] | 64 | @Presubmit |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 65 | public class WindowContainerTests extends WindowTestsBase { |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 66 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 67 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 68 | public void testCreation() { |
| 69 | final TestWindowContainer w = new TestWindowContainerBuilder(mWm).setLayer(0).build(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 70 | assertNull("window must have no parent", w.getParentWindow()); |
| 71 | assertEquals("window must have no children", 0, w.getChildrenCount()); |
| 72 | } |
| 73 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 74 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 75 | public void testAdd() { |
| 76 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 77 | final TestWindowContainer root = builder.setLayer(0).build(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 78 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 79 | final TestWindowContainer layer1 = root.addChildWindow(builder.setLayer(1)); |
| 80 | final TestWindowContainer secondLayer1 = root.addChildWindow(builder.setLayer(1)); |
| 81 | final TestWindowContainer layer2 = root.addChildWindow(builder.setLayer(2)); |
| 82 | final TestWindowContainer layerNeg1 = root.addChildWindow(builder.setLayer(-1)); |
| 83 | final TestWindowContainer layerNeg2 = root.addChildWindow(builder.setLayer(-2)); |
| 84 | final TestWindowContainer secondLayerNeg1 = root.addChildWindow(builder.setLayer(-1)); |
| 85 | final TestWindowContainer layer0 = root.addChildWindow(builder.setLayer(0)); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 86 | |
| 87 | assertEquals(7, root.getChildrenCount()); |
| 88 | |
| 89 | assertEquals(root, layer1.getParentWindow()); |
| 90 | assertEquals(root, secondLayer1.getParentWindow()); |
| 91 | assertEquals(root, layer2.getParentWindow()); |
| 92 | assertEquals(root, layerNeg1.getParentWindow()); |
| 93 | assertEquals(root, layerNeg2.getParentWindow()); |
| 94 | assertEquals(root, secondLayerNeg1.getParentWindow()); |
| 95 | assertEquals(root, layer0.getParentWindow()); |
| 96 | |
| 97 | assertEquals(layerNeg2, root.getChildAt(0)); |
| 98 | assertEquals(secondLayerNeg1, root.getChildAt(1)); |
| 99 | assertEquals(layerNeg1, root.getChildAt(2)); |
| 100 | assertEquals(layer0, root.getChildAt(3)); |
| 101 | assertEquals(layer1, root.getChildAt(4)); |
| 102 | assertEquals(secondLayer1, root.getChildAt(5)); |
| 103 | assertEquals(layer2, root.getChildAt(6)); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 104 | |
Riddle Hsu | 3a4bb61 | 2019-01-31 00:02:22 +0800 | [diff] [blame] | 105 | assertTrue(layer1.mOnParentChangedCalled); |
| 106 | assertTrue(secondLayer1.mOnParentChangedCalled); |
| 107 | assertTrue(layer2.mOnParentChangedCalled); |
| 108 | assertTrue(layerNeg1.mOnParentChangedCalled); |
| 109 | assertTrue(layerNeg2.mOnParentChangedCalled); |
| 110 | assertTrue(secondLayerNeg1.mOnParentChangedCalled); |
| 111 | assertTrue(layer0.mOnParentChangedCalled); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 114 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 115 | public void testAddChildSetsSurfacePosition() { |
| 116 | try (MockSurfaceBuildingContainer top = new MockSurfaceBuildingContainer(mWm)) { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 117 | WindowContainer child = new WindowContainer(mWm); |
| 118 | child.setBounds(1, 1, 10, 10); |
Evan Rosky | b1ea7ca | 2018-04-05 17:17:35 -0700 | [diff] [blame] | 119 | |
chaviw | dd5bde0 | 2019-06-07 16:33:50 -0700 | [diff] [blame] | 120 | verify(mTransaction, never()).setPosition(any(), anyFloat(), anyFloat()); |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 121 | top.addChild(child, 0); |
chaviw | dd5bde0 | 2019-06-07 16:33:50 -0700 | [diff] [blame] | 122 | verify(mTransaction, times(1)).setPosition(any(), eq(1.f), eq(1.f)); |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 123 | } |
Evan Rosky | b1ea7ca | 2018-04-05 17:17:35 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 127 | public void testAdd_AlreadyHasParent() { |
| 128 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 129 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 130 | |
| 131 | final TestWindowContainer child1 = root.addChildWindow(); |
| 132 | final TestWindowContainer child2 = root.addChildWindow(); |
| 133 | |
| 134 | boolean gotException = false; |
| 135 | try { |
| 136 | child1.addChildWindow(child2); |
| 137 | } catch (IllegalArgumentException e) { |
| 138 | gotException = true; |
| 139 | } |
| 140 | assertTrue(gotException); |
| 141 | |
| 142 | gotException = false; |
| 143 | try { |
| 144 | root.addChildWindow(child2); |
| 145 | } catch (IllegalArgumentException e) { |
| 146 | gotException = true; |
| 147 | } |
| 148 | assertTrue(gotException); |
| 149 | } |
| 150 | |
| 151 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 152 | public void testHasChild() { |
| 153 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 154 | final TestWindowContainer root = builder.setLayer(0).build(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 155 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 156 | final TestWindowContainer child1 = root.addChildWindow(); |
| 157 | final TestWindowContainer child2 = root.addChildWindow(); |
| 158 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 159 | final TestWindowContainer child12 = child1.addChildWindow(); |
| 160 | final TestWindowContainer child21 = child2.addChildWindow(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 161 | |
| 162 | assertEquals(2, root.getChildrenCount()); |
| 163 | assertEquals(2, child1.getChildrenCount()); |
| 164 | assertEquals(1, child2.getChildrenCount()); |
| 165 | |
| 166 | assertTrue(root.hasChild(child1)); |
| 167 | assertTrue(root.hasChild(child2)); |
| 168 | assertTrue(root.hasChild(child11)); |
| 169 | assertTrue(root.hasChild(child12)); |
| 170 | assertTrue(root.hasChild(child21)); |
| 171 | |
| 172 | assertTrue(child1.hasChild(child11)); |
| 173 | assertTrue(child1.hasChild(child12)); |
| 174 | assertFalse(child1.hasChild(child21)); |
| 175 | |
| 176 | assertTrue(child2.hasChild(child21)); |
| 177 | assertFalse(child2.hasChild(child11)); |
| 178 | assertFalse(child2.hasChild(child12)); |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 179 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 180 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 181 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 182 | public void testRemoveImmediately() { |
| 183 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 184 | final TestWindowContainer root = builder.setLayer(0).build(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 185 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 186 | final TestWindowContainer child1 = root.addChildWindow(); |
| 187 | final TestWindowContainer child2 = root.addChildWindow(); |
| 188 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 189 | final TestWindowContainer child12 = child1.addChildWindow(); |
| 190 | final TestWindowContainer child21 = child2.addChildWindow(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 191 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 192 | assertNotNull(child12.getParentWindow()); |
| 193 | child12.removeImmediately(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 194 | assertNull(child12.getParentWindow()); |
| 195 | assertEquals(1, child1.getChildrenCount()); |
| 196 | assertFalse(child1.hasChild(child12)); |
| 197 | assertFalse(root.hasChild(child12)); |
| 198 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 199 | assertTrue(root.hasChild(child2)); |
| 200 | assertNotNull(child2.getParentWindow()); |
| 201 | child2.removeImmediately(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 202 | assertNull(child2.getParentWindow()); |
| 203 | assertNull(child21.getParentWindow()); |
| 204 | assertEquals(0, child2.getChildrenCount()); |
| 205 | assertEquals(1, root.getChildrenCount()); |
| 206 | assertFalse(root.hasChild(child2)); |
| 207 | assertFalse(root.hasChild(child21)); |
| 208 | |
| 209 | assertTrue(root.hasChild(child1)); |
| 210 | assertTrue(root.hasChild(child11)); |
| 211 | |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 212 | root.removeImmediately(); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 213 | assertEquals(0, root.getChildrenCount()); |
| 214 | } |
| 215 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 216 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 217 | public void testRemoveImmediately_WithController() { |
| 218 | final WindowContainer container = new WindowContainer(mWm); |
| 219 | final WindowContainerController controller = new WindowContainerController<>(null, mWm); |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 220 | |
| 221 | container.setController(controller); |
| 222 | assertEquals(controller, container.getController()); |
| 223 | assertEquals(container, controller.mContainer); |
| 224 | |
| 225 | container.removeImmediately(); |
| 226 | assertNull(container.getController()); |
| 227 | assertNull(controller.mContainer); |
| 228 | } |
| 229 | |
| 230 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 231 | public void testSetController() { |
| 232 | final WindowContainerController controller = new WindowContainerController<>(null, mWm); |
| 233 | final WindowContainer container = new WindowContainer(mWm); |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 234 | |
| 235 | container.setController(controller); |
| 236 | assertEquals(controller, container.getController()); |
| 237 | assertEquals(container, controller.mContainer); |
| 238 | |
| 239 | // Assert we can't change the controller to another one once set |
| 240 | boolean gotException = false; |
| 241 | try { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 242 | container.setController(new WindowContainerController<>(null, mWm)); |
Wale Ogunwale | 26c0dfe | 2016-12-14 14:42:30 -0800 | [diff] [blame] | 243 | } catch (IllegalArgumentException e) { |
| 244 | gotException = true; |
| 245 | } |
| 246 | assertTrue(gotException); |
| 247 | |
| 248 | // Assert that we can set the controller to null. |
| 249 | container.setController(null); |
| 250 | assertNull(container.getController()); |
| 251 | assertNull(controller.mContainer); |
| 252 | } |
| 253 | |
| 254 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 255 | public void testAddChildByIndex() { |
| 256 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Kazuki Takise | f85197b | 2018-06-18 18:18:36 +0900 | [diff] [blame] | 257 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 258 | |
| 259 | final TestWindowContainer child = root.addChildWindow(); |
| 260 | |
| 261 | final TestWindowContainer child2 = builder.setLayer(1).build(); |
| 262 | final TestWindowContainer child3 = builder.setLayer(2).build(); |
| 263 | final TestWindowContainer child4 = builder.setLayer(3).build(); |
| 264 | |
| 265 | // Test adding at top. |
| 266 | root.addChild(child2, POSITION_TOP); |
| 267 | assertEquals(child2, root.getChildAt(root.getChildrenCount() - 1)); |
| 268 | |
| 269 | // Test adding at bottom. |
| 270 | root.addChild(child3, POSITION_BOTTOM); |
| 271 | assertEquals(child3, root.getChildAt(0)); |
| 272 | |
| 273 | // Test adding in the middle. |
| 274 | root.addChild(child4, 1); |
| 275 | assertEquals(child3, root.getChildAt(0)); |
| 276 | assertEquals(child4, root.getChildAt(1)); |
| 277 | assertEquals(child, root.getChildAt(2)); |
| 278 | assertEquals(child2, root.getChildAt(3)); |
| 279 | } |
| 280 | |
| 281 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 282 | public void testPositionChildAt() { |
| 283 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 284 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 285 | |
| 286 | final TestWindowContainer child1 = root.addChildWindow(); |
| 287 | final TestWindowContainer child2 = root.addChildWindow(); |
| 288 | final TestWindowContainer child3 = root.addChildWindow(); |
| 289 | |
| 290 | // Test position at top. |
| 291 | root.positionChildAt(POSITION_TOP, child1, false /* includingParents */); |
| 292 | assertEquals(child1, root.getChildAt(root.getChildrenCount() - 1)); |
| 293 | |
| 294 | // Test position at bottom. |
| 295 | root.positionChildAt(POSITION_BOTTOM, child1, false /* includingParents */); |
| 296 | assertEquals(child1, root.getChildAt(0)); |
| 297 | |
| 298 | // Test position in the middle. |
| 299 | root.positionChildAt(1, child3, false /* includingParents */); |
| 300 | assertEquals(child1, root.getChildAt(0)); |
| 301 | assertEquals(child3, root.getChildAt(1)); |
| 302 | assertEquals(child2, root.getChildAt(2)); |
| 303 | } |
| 304 | |
| 305 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 306 | public void testPositionChildAtIncludeParents() { |
| 307 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 308 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 309 | |
| 310 | final TestWindowContainer child1 = root.addChildWindow(); |
| 311 | final TestWindowContainer child2 = root.addChildWindow(); |
| 312 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 313 | final TestWindowContainer child12 = child1.addChildWindow(); |
| 314 | final TestWindowContainer child13 = child1.addChildWindow(); |
| 315 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 316 | final TestWindowContainer child22 = child2.addChildWindow(); |
| 317 | final TestWindowContainer child23 = child2.addChildWindow(); |
| 318 | |
| 319 | // Test moving to top. |
| 320 | child1.positionChildAt(POSITION_TOP, child11, true /* includingParents */); |
| 321 | assertEquals(child12, child1.getChildAt(0)); |
| 322 | assertEquals(child13, child1.getChildAt(1)); |
| 323 | assertEquals(child11, child1.getChildAt(2)); |
| 324 | assertEquals(child2, root.getChildAt(0)); |
| 325 | assertEquals(child1, root.getChildAt(1)); |
| 326 | |
| 327 | // Test moving to bottom. |
| 328 | child1.positionChildAt(POSITION_BOTTOM, child11, true /* includingParents */); |
| 329 | assertEquals(child11, child1.getChildAt(0)); |
| 330 | assertEquals(child12, child1.getChildAt(1)); |
| 331 | assertEquals(child13, child1.getChildAt(2)); |
| 332 | assertEquals(child1, root.getChildAt(0)); |
| 333 | assertEquals(child2, root.getChildAt(1)); |
| 334 | |
| 335 | // Test moving to middle, includeParents shouldn't do anything. |
| 336 | child2.positionChildAt(1, child21, true /* includingParents */); |
| 337 | assertEquals(child11, child1.getChildAt(0)); |
| 338 | assertEquals(child12, child1.getChildAt(1)); |
| 339 | assertEquals(child13, child1.getChildAt(2)); |
| 340 | assertEquals(child22, child2.getChildAt(0)); |
| 341 | assertEquals(child21, child2.getChildAt(1)); |
| 342 | assertEquals(child23, child2.getChildAt(2)); |
| 343 | assertEquals(child1, root.getChildAt(0)); |
| 344 | assertEquals(child2, root.getChildAt(1)); |
| 345 | } |
| 346 | |
| 347 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 348 | public void testPositionChildAtInvalid() { |
| 349 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 350 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 351 | |
| 352 | final TestWindowContainer child1 = root.addChildWindow(); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 353 | |
| 354 | boolean gotException = false; |
| 355 | try { |
| 356 | // Check response to negative position. |
| 357 | root.positionChildAt(-1, child1, false /* includingParents */); |
| 358 | } catch (IllegalArgumentException e) { |
| 359 | gotException = true; |
| 360 | } |
| 361 | assertTrue(gotException); |
| 362 | |
| 363 | gotException = false; |
| 364 | try { |
| 365 | // Check response to position that's bigger than child number. |
Wale Ogunwale | c5cc301 | 2017-01-13 13:26:16 -0800 | [diff] [blame] | 366 | root.positionChildAt(3, child1, false /* includingParents */); |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 367 | } catch (IllegalArgumentException e) { |
| 368 | gotException = true; |
| 369 | } |
| 370 | assertTrue(gotException); |
| 371 | } |
| 372 | |
| 373 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 374 | public void testIsAnimating() { |
| 375 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 376 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 377 | |
| 378 | final TestWindowContainer child1 = root.addChildWindow(builder.setIsAnimating(true)); |
| 379 | final TestWindowContainer child2 = root.addChildWindow(); |
| 380 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 381 | final TestWindowContainer child12 = child1.addChildWindow(builder.setIsAnimating(true)); |
| 382 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 383 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 384 | assertFalse(root.isAnimating()); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 385 | assertTrue(child1.isAnimating()); |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 386 | assertTrue(child11.isAnimating()); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 387 | assertTrue(child12.isAnimating()); |
| 388 | assertFalse(child2.isAnimating()); |
| 389 | assertFalse(child21.isAnimating()); |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 390 | |
| 391 | assertTrue(root.isSelfOrChildAnimating()); |
| 392 | assertTrue(child1.isSelfOrChildAnimating()); |
| 393 | assertFalse(child11.isSelfOrChildAnimating()); |
| 394 | assertTrue(child12.isSelfOrChildAnimating()); |
| 395 | assertFalse(child2.isSelfOrChildAnimating()); |
| 396 | assertFalse(child21.isSelfOrChildAnimating()); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 399 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 400 | public void testIsVisible() { |
| 401 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 402 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 403 | |
| 404 | final TestWindowContainer child1 = root.addChildWindow(builder.setIsVisible(true)); |
| 405 | final TestWindowContainer child2 = root.addChildWindow(); |
| 406 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 407 | final TestWindowContainer child12 = child1.addChildWindow(builder.setIsVisible(true)); |
| 408 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 409 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 410 | assertFalse(root.isVisible()); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 411 | assertTrue(child1.isVisible()); |
| 412 | assertFalse(child11.isVisible()); |
| 413 | assertTrue(child12.isVisible()); |
| 414 | assertFalse(child2.isVisible()); |
| 415 | assertFalse(child21.isVisible()); |
| 416 | } |
| 417 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 418 | @Test |
Wale Ogunwale | 55ddf8f | 2017-03-20 08:56:38 -0700 | [diff] [blame] | 419 | public void testOverrideConfigurationAncestorNotification() { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 420 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | 55ddf8f | 2017-03-20 08:56:38 -0700 | [diff] [blame] | 421 | final TestWindowContainer grandparent = builder.setLayer(0).build(); |
| 422 | |
| 423 | final TestWindowContainer parent = grandparent.addChildWindow(); |
| 424 | final TestWindowContainer child = parent.addChildWindow(); |
Evan Rosky | dfe3da7 | 2018-10-26 17:21:06 -0700 | [diff] [blame] | 425 | child.onRequestedOverrideConfigurationChanged(new Configuration()); |
Wale Ogunwale | 55ddf8f | 2017-03-20 08:56:38 -0700 | [diff] [blame] | 426 | |
| 427 | assertTrue(grandparent.mOnDescendantOverrideCalled); |
| 428 | } |
| 429 | |
| 430 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 431 | public void testRemoveChild() { |
| 432 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 433 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 434 | final TestWindowContainer child1 = root.addChildWindow(); |
| 435 | final TestWindowContainer child2 = root.addChildWindow(); |
| 436 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 437 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 438 | |
| 439 | assertTrue(root.hasChild(child2)); |
| 440 | assertTrue(root.hasChild(child21)); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 441 | root.removeChild(child2); |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 442 | assertFalse(root.hasChild(child2)); |
| 443 | assertFalse(root.hasChild(child21)); |
| 444 | assertNull(child2.getParentWindow()); |
| 445 | |
| 446 | boolean gotException = false; |
| 447 | assertTrue(root.hasChild(child11)); |
| 448 | try { |
| 449 | // Can only detach our direct children. |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 450 | root.removeChild(child11); |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 451 | } catch (IllegalArgumentException e) { |
| 452 | gotException = true; |
| 453 | } |
| 454 | assertTrue(gotException); |
| 455 | } |
| 456 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 457 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 458 | public void testGetOrientation_childSpecified() { |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 459 | testGetOrientation_childSpecifiedConfig(false, SCREEN_ORIENTATION_LANDSCAPE, |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 460 | SCREEN_ORIENTATION_LANDSCAPE); |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 461 | testGetOrientation_childSpecifiedConfig(false, SCREEN_ORIENTATION_UNSET, |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 462 | SCREEN_ORIENTATION_UNSPECIFIED); |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | private void testGetOrientation_childSpecifiedConfig(boolean childVisible, int childOrientation, |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 466 | int expectedOrientation) { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 467 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 468 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 469 | root.setFillsParent(true); |
| 470 | |
| 471 | builder.setIsVisible(childVisible); |
| 472 | |
| 473 | if (childOrientation != SCREEN_ORIENTATION_UNSET) { |
| 474 | builder.setOrientation(childOrientation); |
| 475 | } |
| 476 | |
| 477 | final TestWindowContainer child1 = root.addChildWindow(builder); |
| 478 | child1.setFillsParent(true); |
| 479 | |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 480 | assertEquals(expectedOrientation, root.getOrientation()); |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 484 | public void testGetOrientation_Unset() { |
| 485 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 486 | final TestWindowContainer root = builder.setLayer(0).setIsVisible(true).build(); |
| 487 | // Unspecified well because we didn't specify anything... |
| 488 | assertEquals(SCREEN_ORIENTATION_UNSPECIFIED, root.getOrientation()); |
| 489 | } |
| 490 | |
| 491 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 492 | public void testGetOrientation_InvisibleParentUnsetVisibleChildren() { |
| 493 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 494 | final TestWindowContainer root = builder.setLayer(0).setIsVisible(true).build(); |
| 495 | |
| 496 | builder.setIsVisible(false).setLayer(-1); |
| 497 | final TestWindowContainer invisible = root.addChildWindow(builder); |
| 498 | builder.setIsVisible(true).setLayer(-2); |
| 499 | final TestWindowContainer invisibleChild1VisibleAndSet = invisible.addChildWindow(builder); |
| 500 | invisibleChild1VisibleAndSet.setOrientation(SCREEN_ORIENTATION_LANDSCAPE); |
| 501 | // Landscape well because the container is visible and that is what we set on it above. |
| 502 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, invisibleChild1VisibleAndSet.getOrientation()); |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 503 | // Landscape because even though the container isn't visible it has a child that is |
| 504 | // specifying it can influence the orientation by being visible. |
| 505 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, invisible.getOrientation()); |
| 506 | // Landscape because the grandchild is visible and therefore can participate. |
| 507 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, root.getOrientation()); |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 508 | |
| 509 | builder.setIsVisible(true).setLayer(-3); |
| 510 | final TestWindowContainer visibleUnset = root.addChildWindow(builder); |
| 511 | visibleUnset.setOrientation(SCREEN_ORIENTATION_UNSET); |
| 512 | assertEquals(SCREEN_ORIENTATION_UNSET, visibleUnset.getOrientation()); |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 513 | assertEquals(SCREEN_ORIENTATION_LANDSCAPE, root.getOrientation()); |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 517 | public void testGetOrientation_setBehind() { |
| 518 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 519 | final TestWindowContainer root = builder.setLayer(0).setIsVisible(true).build(); |
| 520 | |
| 521 | builder.setIsVisible(true).setLayer(-1); |
| 522 | final TestWindowContainer visibleUnset = root.addChildWindow(builder); |
| 523 | visibleUnset.setOrientation(SCREEN_ORIENTATION_UNSET); |
| 524 | |
| 525 | builder.setIsVisible(true).setLayer(-2); |
| 526 | final TestWindowContainer visibleUnsetChild1VisibleSetBehind = |
| 527 | visibleUnset.addChildWindow(builder); |
| 528 | visibleUnsetChild1VisibleSetBehind.setOrientation(SCREEN_ORIENTATION_BEHIND); |
| 529 | // Setting to visible behind will be used by the parents if there isn't another other |
| 530 | // container behind this one that has an orientation set. |
| 531 | assertEquals(SCREEN_ORIENTATION_BEHIND, |
| 532 | visibleUnsetChild1VisibleSetBehind.getOrientation()); |
| 533 | assertEquals(SCREEN_ORIENTATION_BEHIND, visibleUnset.getOrientation()); |
| 534 | assertEquals(SCREEN_ORIENTATION_BEHIND, root.getOrientation()); |
| 535 | } |
| 536 | |
| 537 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 538 | public void testGetOrientation_fillsParent() { |
| 539 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 540 | final TestWindowContainer root = builder.setLayer(0).setIsVisible(true).build(); |
| 541 | |
| 542 | builder.setIsVisible(true).setLayer(-1); |
| 543 | final TestWindowContainer visibleUnset = root.addChildWindow(builder); |
| 544 | visibleUnset.setOrientation(SCREEN_ORIENTATION_BEHIND); |
| 545 | |
| 546 | builder.setLayer(1).setIsVisible(true); |
| 547 | final TestWindowContainer visibleUnspecifiedRootChild = root.addChildWindow(builder); |
| 548 | visibleUnspecifiedRootChild.setFillsParent(false); |
| 549 | visibleUnspecifiedRootChild.setOrientation(SCREEN_ORIENTATION_UNSPECIFIED); |
| 550 | // Unset because the child doesn't fill the parent. May as well be invisible... |
| 551 | assertEquals(SCREEN_ORIENTATION_UNSET, visibleUnspecifiedRootChild.getOrientation()); |
| 552 | // The parent uses whatever orientation is set behind this container since it doesn't fill |
| 553 | // the parent. |
| 554 | assertEquals(SCREEN_ORIENTATION_BEHIND, root.getOrientation()); |
| 555 | |
| 556 | // Test case of child filling its parent, but its parent isn't filling its own parent. |
| 557 | builder.setLayer(2).setIsVisible(true); |
| 558 | final TestWindowContainer visibleUnspecifiedRootChildChildFillsParent = |
| 559 | visibleUnspecifiedRootChild.addChildWindow(builder); |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 560 | visibleUnspecifiedRootChildChildFillsParent.setOrientation(SCREEN_ORIENTATION_PORTRAIT); |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 561 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, |
| 562 | visibleUnspecifiedRootChildChildFillsParent.getOrientation()); |
| 563 | assertEquals(SCREEN_ORIENTATION_UNSET, visibleUnspecifiedRootChild.getOrientation()); |
| 564 | assertEquals(SCREEN_ORIENTATION_BEHIND, root.getOrientation()); |
| 565 | |
| 566 | |
| 567 | visibleUnspecifiedRootChild.setFillsParent(true); |
| 568 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, visibleUnspecifiedRootChild.getOrientation()); |
| 569 | assertEquals(SCREEN_ORIENTATION_PORTRAIT, root.getOrientation()); |
| 570 | } |
| 571 | |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 572 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 573 | public void testCompareTo() { |
| 574 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 575 | final TestWindowContainer root = builder.setLayer(0).build(); |
| 576 | |
| 577 | final TestWindowContainer child1 = root.addChildWindow(); |
| 578 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 579 | final TestWindowContainer child12 = child1.addChildWindow(); |
| 580 | |
| 581 | final TestWindowContainer child2 = root.addChildWindow(); |
| 582 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 583 | final TestWindowContainer child22 = child2.addChildWindow(); |
Wale Ogunwale | 63d4ecc | 2016-09-08 18:48:26 -0700 | [diff] [blame] | 584 | final TestWindowContainer child222 = child22.addChildWindow(); |
| 585 | final TestWindowContainer child223 = child22.addChildWindow(); |
| 586 | final TestWindowContainer child2221 = child222.addChildWindow(); |
| 587 | final TestWindowContainer child2222 = child222.addChildWindow(); |
| 588 | final TestWindowContainer child2223 = child222.addChildWindow(); |
| 589 | |
| 590 | final TestWindowContainer root2 = builder.setLayer(0).build(); |
| 591 | |
| 592 | assertEquals(0, root.compareTo(root)); |
| 593 | assertEquals(-1, child1.compareTo(child2)); |
| 594 | assertEquals(1, child2.compareTo(child1)); |
| 595 | |
| 596 | boolean inTheSameTree = true; |
| 597 | try { |
| 598 | root.compareTo(root2); |
| 599 | } catch (IllegalArgumentException e) { |
| 600 | inTheSameTree = false; |
| 601 | } |
| 602 | assertFalse(inTheSameTree); |
| 603 | |
| 604 | assertEquals(-1, child1.compareTo(child11)); |
| 605 | assertEquals(1, child21.compareTo(root)); |
| 606 | assertEquals(1, child21.compareTo(child12)); |
| 607 | assertEquals(-1, child11.compareTo(child2)); |
| 608 | assertEquals(1, child2221.compareTo(child11)); |
| 609 | assertEquals(-1, child2222.compareTo(child223)); |
| 610 | assertEquals(1, child2223.compareTo(child21)); |
| 611 | } |
| 612 | |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 613 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 614 | public void testPrefixOrderIndex() { |
| 615 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 616 | final TestWindowContainer root = builder.build(); |
| 617 | |
| 618 | final TestWindowContainer child1 = root.addChildWindow(); |
| 619 | |
| 620 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 621 | final TestWindowContainer child12 = child1.addChildWindow(); |
| 622 | |
| 623 | final TestWindowContainer child2 = root.addChildWindow(); |
| 624 | |
| 625 | final TestWindowContainer child21 = child2.addChildWindow(); |
| 626 | final TestWindowContainer child22 = child2.addChildWindow(); |
| 627 | |
| 628 | final TestWindowContainer child221 = child22.addChildWindow(); |
| 629 | final TestWindowContainer child222 = child22.addChildWindow(); |
| 630 | final TestWindowContainer child223 = child22.addChildWindow(); |
| 631 | |
| 632 | final TestWindowContainer child23 = child2.addChildWindow(); |
| 633 | |
| 634 | assertEquals(0, root.getPrefixOrderIndex()); |
| 635 | assertEquals(1, child1.getPrefixOrderIndex()); |
| 636 | assertEquals(2, child11.getPrefixOrderIndex()); |
| 637 | assertEquals(3, child12.getPrefixOrderIndex()); |
| 638 | assertEquals(4, child2.getPrefixOrderIndex()); |
| 639 | assertEquals(5, child21.getPrefixOrderIndex()); |
| 640 | assertEquals(6, child22.getPrefixOrderIndex()); |
| 641 | assertEquals(7, child221.getPrefixOrderIndex()); |
| 642 | assertEquals(8, child222.getPrefixOrderIndex()); |
| 643 | assertEquals(9, child223.getPrefixOrderIndex()); |
| 644 | assertEquals(10, child23.getPrefixOrderIndex()); |
| 645 | } |
| 646 | |
| 647 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 648 | public void testPrefixOrder_addEntireSubtree() { |
| 649 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 650 | final TestWindowContainer root = builder.build(); |
| 651 | final TestWindowContainer subtree = builder.build(); |
| 652 | final TestWindowContainer subtree2 = builder.build(); |
| 653 | |
| 654 | final TestWindowContainer child1 = subtree.addChildWindow(); |
| 655 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 656 | final TestWindowContainer child2 = subtree2.addChildWindow(); |
| 657 | final TestWindowContainer child3 = subtree2.addChildWindow(); |
| 658 | subtree.addChild(subtree2, 1); |
| 659 | root.addChild(subtree, 0); |
| 660 | |
| 661 | assertEquals(0, root.getPrefixOrderIndex()); |
| 662 | assertEquals(1, subtree.getPrefixOrderIndex()); |
| 663 | assertEquals(2, child1.getPrefixOrderIndex()); |
| 664 | assertEquals(3, child11.getPrefixOrderIndex()); |
| 665 | assertEquals(4, subtree2.getPrefixOrderIndex()); |
| 666 | assertEquals(5, child2.getPrefixOrderIndex()); |
| 667 | assertEquals(6, child3.getPrefixOrderIndex()); |
| 668 | } |
| 669 | |
| 670 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 671 | public void testPrefixOrder_remove() { |
| 672 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Jorim Jaggi | 619c9f7 | 2017-12-19 18:04:29 +0100 | [diff] [blame] | 673 | final TestWindowContainer root = builder.build(); |
| 674 | |
| 675 | final TestWindowContainer child1 = root.addChildWindow(); |
| 676 | |
| 677 | final TestWindowContainer child11 = child1.addChildWindow(); |
| 678 | final TestWindowContainer child12 = child1.addChildWindow(); |
| 679 | |
| 680 | final TestWindowContainer child2 = root.addChildWindow(); |
| 681 | |
| 682 | assertEquals(0, root.getPrefixOrderIndex()); |
| 683 | assertEquals(1, child1.getPrefixOrderIndex()); |
| 684 | assertEquals(2, child11.getPrefixOrderIndex()); |
| 685 | assertEquals(3, child12.getPrefixOrderIndex()); |
| 686 | assertEquals(4, child2.getPrefixOrderIndex()); |
| 687 | |
| 688 | root.removeChild(child1); |
| 689 | |
| 690 | assertEquals(1, child2.getPrefixOrderIndex()); |
| 691 | } |
| 692 | |
Bryce Lee | d92ae48 | 2018-01-22 13:56:23 -0800 | [diff] [blame] | 693 | /** |
| 694 | * Ensure children of a {@link WindowContainer} do not have |
| 695 | * {@link WindowContainer#onParentResize()} called when {@link WindowContainer#onParentResize()} |
| 696 | * is invoked with overridden bounds. |
| 697 | */ |
| 698 | @Test |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 699 | public void testOnParentResizePropagation() { |
| 700 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
Bryce Lee | d92ae48 | 2018-01-22 13:56:23 -0800 | [diff] [blame] | 701 | final TestWindowContainer root = builder.build(); |
| 702 | |
| 703 | final TestWindowContainer child = root.addChildWindow(); |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 704 | child.setBounds(new Rect(1, 1, 2, 2)); |
Bryce Lee | d92ae48 | 2018-01-22 13:56:23 -0800 | [diff] [blame] | 705 | |
| 706 | final TestWindowContainer grandChild = mock(TestWindowContainer.class); |
| 707 | |
| 708 | child.addChildWindow(grandChild); |
| 709 | root.onResize(); |
| 710 | |
| 711 | // Make sure the child does not propagate resize through onParentResize when bounds are set. |
| 712 | verify(grandChild, never()).onParentResize(); |
| 713 | |
| 714 | child.removeChild(grandChild); |
| 715 | |
| 716 | child.setBounds(null); |
| 717 | child.addChildWindow(grandChild); |
| 718 | root.onResize(); |
| 719 | |
| 720 | // Make sure the child propagates resize through onParentResize when no bounds set. |
| 721 | verify(grandChild, times(1)).onParentResize(); |
| 722 | } |
| 723 | |
Garfield Tan | 90b0428 | 2018-12-11 14:04:42 -0800 | [diff] [blame] | 724 | @Test |
| 725 | public void testOnDescendantOrientationRequestChangedPropagation() { |
| 726 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
| 727 | final TestWindowContainer root = spy(builder.build()); |
| 728 | |
| 729 | final IBinder binder = mock(IBinder.class); |
| 730 | final ActivityRecord activityRecord = mock(ActivityRecord.class); |
| 731 | final TestWindowContainer child = root.addChildWindow(); |
| 732 | |
| 733 | child.setOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED, binder, activityRecord); |
| 734 | verify(root).onDescendantOrientationChanged(binder, activityRecord); |
| 735 | } |
| 736 | |
Garfield Tan | 49dae10 | 2019-02-04 09:51:59 -0800 | [diff] [blame] | 737 | @Test |
| 738 | public void testHandlesOrientationChangeFromDescendantProgation() { |
| 739 | final TestWindowContainerBuilder builder = new TestWindowContainerBuilder(mWm); |
| 740 | final TestWindowContainer root = spy(builder.build()); |
| 741 | |
| 742 | final TestWindowContainer child = root.addChildWindow(); |
| 743 | assertFalse(child.handlesOrientationChangeFromDescendant()); |
| 744 | |
| 745 | Mockito.doReturn(true).when(root).handlesOrientationChangeFromDescendant(); |
| 746 | assertTrue(child.handlesOrientationChangeFromDescendant()); |
| 747 | } |
| 748 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 749 | /* Used so we can gain access to some protected members of the {@link WindowContainer} class */ |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 750 | private static class TestWindowContainer extends WindowContainer<TestWindowContainer> { |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 751 | private final int mLayer; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 752 | private boolean mIsAnimating; |
| 753 | private boolean mIsVisible; |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 754 | private boolean mFillsParent; |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 755 | private Integer mOrientation; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 756 | |
Riddle Hsu | 3a4bb61 | 2019-01-31 00:02:22 +0800 | [diff] [blame] | 757 | private boolean mOnParentChangedCalled; |
Wale Ogunwale | 55ddf8f | 2017-03-20 08:56:38 -0700 | [diff] [blame] | 758 | private boolean mOnDescendantOverrideCalled; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 759 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 760 | /** |
| 761 | * Compares 2 window layers and returns -1 if the first is lesser than the second in terms |
| 762 | * of z-order and 1 otherwise. |
| 763 | */ |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 764 | private static final Comparator<TestWindowContainer> SUBLAYER_COMPARATOR = (w1, w2) -> { |
Wale Ogunwale | d90546a | 2016-09-09 23:28:03 -0700 | [diff] [blame] | 765 | final int layer1 = w1.mLayer; |
| 766 | final int layer2 = w2.mLayer; |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 767 | if (layer1 < layer2 || (layer1 == layer2 && layer2 < 0)) { |
Wale Ogunwale | d90546a | 2016-09-09 23:28:03 -0700 | [diff] [blame] | 768 | // We insert the child window into the list ordered by the mLayer. For same layers, |
| 769 | // the negative one should go below others; the positive one should go above others. |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 770 | return -1; |
| 771 | } |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 772 | if (layer1 == layer2) return 0; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 773 | return 1; |
| 774 | }; |
| 775 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 776 | TestWindowContainer(WindowManagerService wm, int layer, boolean isAnimating, |
| 777 | boolean isVisible, Integer orientation) { |
| 778 | super(wm); |
| 779 | |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 780 | mLayer = layer; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 781 | mIsAnimating = isAnimating; |
| 782 | mIsVisible = isVisible; |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 783 | mFillsParent = true; |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 784 | mOrientation = orientation; |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | TestWindowContainer getParentWindow() { |
| 788 | return (TestWindowContainer) getParent(); |
| 789 | } |
| 790 | |
| 791 | int getChildrenCount() { |
| 792 | return mChildren.size(); |
| 793 | } |
| 794 | |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 795 | TestWindowContainer addChildWindow(TestWindowContainer child) { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 796 | addChild(child, SUBLAYER_COMPARATOR); |
Wale Ogunwale | f619286 | 2016-09-10 13:42:30 -0700 | [diff] [blame] | 797 | return child; |
| 798 | } |
| 799 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 800 | TestWindowContainer addChildWindow(TestWindowContainerBuilder childBuilder) { |
| 801 | TestWindowContainer child = childBuilder.build(); |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 802 | addChild(child, SUBLAYER_COMPARATOR); |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 803 | return child; |
| 804 | } |
| 805 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 806 | TestWindowContainer addChildWindow() { |
Wale Ogunwale | 8b19de9 | 2018-11-29 19:58:26 -0800 | [diff] [blame] | 807 | return addChildWindow(new TestWindowContainerBuilder(mWmService).setLayer(1)); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 810 | @Override |
Riddle Hsu | 3a4bb61 | 2019-01-31 00:02:22 +0800 | [diff] [blame] | 811 | void onParentChanged() { |
| 812 | mOnParentChangedCalled = true; |
Andrii Kulian | d276563 | 2016-12-12 22:26:34 -0800 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | @Override |
Wale Ogunwale | 55ddf8f | 2017-03-20 08:56:38 -0700 | [diff] [blame] | 816 | void onDescendantOverrideConfigurationChanged() { |
| 817 | mOnDescendantOverrideCalled = true; |
| 818 | super.onDescendantOverrideConfigurationChanged(); |
| 819 | } |
| 820 | |
| 821 | @Override |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 822 | boolean isSelfAnimating() { |
| 823 | return mIsAnimating; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | @Override |
| 827 | boolean isVisible() { |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 828 | return mIsVisible; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 829 | } |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 830 | |
| 831 | @Override |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 832 | int getOrientation(int candidate) { |
| 833 | return mOrientation != null ? mOrientation : super.getOrientation(candidate); |
| 834 | } |
| 835 | |
| 836 | @Override |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 837 | int getOrientation() { |
Wale Ogunwale | 5e5a68d | 2017-03-24 17:36:38 -0700 | [diff] [blame] | 838 | return getOrientation(super.mOrientation); |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | @Override |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 842 | boolean fillsParent() { |
| 843 | return mFillsParent; |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 846 | void setFillsParent(boolean fillsParent) { |
| 847 | mFillsParent = fillsParent; |
Wale Ogunwale | 571771c | 2016-08-26 13:18:50 -0700 | [diff] [blame] | 848 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 851 | private static class TestWindowContainerBuilder { |
| 852 | private final WindowManagerService mWm; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 853 | private int mLayer; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 854 | private boolean mIsAnimating; |
| 855 | private boolean mIsVisible; |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 856 | private Integer mOrientation; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 857 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 858 | TestWindowContainerBuilder(WindowManagerService wm) { |
| 859 | mWm = wm; |
| 860 | mLayer = 0; |
| 861 | mIsAnimating = false; |
| 862 | mIsVisible = false; |
| 863 | mOrientation = null; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Wale Ogunwale | 5136249 | 2016-09-08 17:49:17 -0700 | [diff] [blame] | 866 | TestWindowContainerBuilder setLayer(int layer) { |
| 867 | mLayer = layer; |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 868 | return this; |
| 869 | } |
| 870 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 871 | TestWindowContainerBuilder setIsAnimating(boolean isAnimating) { |
| 872 | mIsAnimating = isAnimating; |
| 873 | return this; |
| 874 | } |
| 875 | |
| 876 | TestWindowContainerBuilder setIsVisible(boolean isVisible) { |
| 877 | mIsVisible = isVisible; |
| 878 | return this; |
| 879 | } |
| 880 | |
Bryce Lee | a163b76 | 2017-01-24 11:05:01 -0800 | [diff] [blame] | 881 | TestWindowContainerBuilder setOrientation(int orientation) { |
| 882 | mOrientation = orientation; |
| 883 | return this; |
| 884 | } |
| 885 | |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 886 | TestWindowContainer build() { |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 887 | return new TestWindowContainer(mWm, mLayer, mIsAnimating, mIsVisible, mOrientation); |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 888 | } |
| 889 | } |
Evan Rosky | b1ea7ca | 2018-04-05 17:17:35 -0700 | [diff] [blame] | 890 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 891 | private static class MockSurfaceBuildingContainer extends WindowContainer<WindowContainer> |
| 892 | implements AutoCloseable { |
| 893 | private final SurfaceSession mSession = new SurfaceSession(); |
Evan Rosky | b1ea7ca | 2018-04-05 17:17:35 -0700 | [diff] [blame] | 894 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 895 | MockSurfaceBuildingContainer(WindowManagerService wm) { |
| 896 | super(wm); |
Evan Rosky | b1ea7ca | 2018-04-05 17:17:35 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 899 | static class MockSurfaceBuilder extends SurfaceControl.Builder { |
Evan Rosky | b1ea7ca | 2018-04-05 17:17:35 -0700 | [diff] [blame] | 900 | MockSurfaceBuilder(SurfaceSession ss) { |
| 901 | super(ss); |
| 902 | } |
| 903 | |
| 904 | @Override |
| 905 | public SurfaceControl build() { |
| 906 | return mock(SurfaceControl.class); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | @Override |
| 911 | SurfaceControl.Builder makeChildSurface(WindowContainer child) { |
| 912 | return new MockSurfaceBuilder(mSession); |
| 913 | } |
Tadashi G. Takaoka | b6e148c | 2018-11-03 02:59:06 -0700 | [diff] [blame] | 914 | |
| 915 | @Override |
| 916 | public void close() { |
| 917 | mSession.kill(); |
| 918 | } |
Evan Rosky | b1ea7ca | 2018-04-05 17:17:35 -0700 | [diff] [blame] | 919 | } |
Wale Ogunwale | d63594a | 2016-07-18 07:48:30 -0700 | [diff] [blame] | 920 | } |