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; |
| 23 | |
| 24 | /** |
| 25 | * Test class for {@link WindowContainer}. |
| 26 | * |
| 27 | * Build: mmma -j32 frameworks/base/services/tests/servicestests |
| 28 | * Install: adb install -r out/target/product/marlin/data/app/FrameworksServicesTests/FrameworksServicesTests.apk |
| 29 | * Run: adb shell am instrument -w -e class com.android.server.wm.WindowContainerTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner |
| 30 | */ |
| 31 | @SmallTest |
| 32 | public class WindowContainerTests extends AndroidTestCase { |
| 33 | |
| 34 | public void testCreation() throws Exception { |
| 35 | final TestWindowContainer w = new TestWindowContainer(); |
| 36 | assertNull("window must have no parent", w.getParentWindow()); |
| 37 | assertEquals("window must have no children", 0, w.getChildrenCount()); |
| 38 | } |
| 39 | |
| 40 | public void testAdd() throws Exception { |
| 41 | final TestWindowContainer root = new TestWindowContainer(); |
| 42 | |
| 43 | final TestWindowContainer layer1 = root.addChildWindow(1); |
| 44 | final TestWindowContainer secondLayer1 = root.addChildWindow(1); |
| 45 | final TestWindowContainer layer2 = root.addChildWindow(2); |
| 46 | final TestWindowContainer layerNeg1 = root.addChildWindow(-1); |
| 47 | final TestWindowContainer layerNeg2 = root.addChildWindow(-2); |
| 48 | final TestWindowContainer secondLayerNeg1 = root.addChildWindow(-1); |
| 49 | final TestWindowContainer layer0 = root.addChildWindow(0); |
| 50 | |
| 51 | assertEquals(7, root.getChildrenCount()); |
| 52 | |
| 53 | assertEquals(root, layer1.getParentWindow()); |
| 54 | assertEquals(root, secondLayer1.getParentWindow()); |
| 55 | assertEquals(root, layer2.getParentWindow()); |
| 56 | assertEquals(root, layerNeg1.getParentWindow()); |
| 57 | assertEquals(root, layerNeg2.getParentWindow()); |
| 58 | assertEquals(root, secondLayerNeg1.getParentWindow()); |
| 59 | assertEquals(root, layer0.getParentWindow()); |
| 60 | |
| 61 | assertEquals(layerNeg2, root.getChildAt(0)); |
| 62 | assertEquals(secondLayerNeg1, root.getChildAt(1)); |
| 63 | assertEquals(layerNeg1, root.getChildAt(2)); |
| 64 | assertEquals(layer0, root.getChildAt(3)); |
| 65 | assertEquals(layer1, root.getChildAt(4)); |
| 66 | assertEquals(secondLayer1, root.getChildAt(5)); |
| 67 | assertEquals(layer2, root.getChildAt(6)); |
| 68 | } |
| 69 | |
| 70 | public void testHasChild() throws Exception { |
| 71 | final TestWindowContainer root = new TestWindowContainer(); |
| 72 | |
| 73 | final TestWindowContainer child1 = root.addChildWindow(1); |
| 74 | final TestWindowContainer child2 = root.addChildWindow(1); |
| 75 | final TestWindowContainer child11 = child1.addChildWindow(1); |
| 76 | final TestWindowContainer child12 = child1.addChildWindow(1); |
| 77 | final TestWindowContainer child21 = child2.addChildWindow(1); |
| 78 | |
| 79 | assertEquals(2, root.getChildrenCount()); |
| 80 | assertEquals(2, child1.getChildrenCount()); |
| 81 | assertEquals(1, child2.getChildrenCount()); |
| 82 | |
| 83 | assertTrue(root.hasChild(child1)); |
| 84 | assertTrue(root.hasChild(child2)); |
| 85 | assertTrue(root.hasChild(child11)); |
| 86 | assertTrue(root.hasChild(child12)); |
| 87 | assertTrue(root.hasChild(child21)); |
| 88 | |
| 89 | assertTrue(child1.hasChild(child11)); |
| 90 | assertTrue(child1.hasChild(child12)); |
| 91 | assertFalse(child1.hasChild(child21)); |
| 92 | |
| 93 | assertTrue(child2.hasChild(child21)); |
| 94 | assertFalse(child2.hasChild(child11)); |
| 95 | assertFalse(child2.hasChild(child12)); |
| 96 | } |
| 97 | |
| 98 | public void testRemove() throws Exception { |
| 99 | final TestWindowContainer root = new TestWindowContainer(); |
| 100 | |
| 101 | final TestWindowContainer child1 = root.addChildWindow(1); |
| 102 | final TestWindowContainer child2 = root.addChildWindow(1); |
| 103 | final TestWindowContainer child11 = child1.addChildWindow(1); |
| 104 | final TestWindowContainer child12 = child1.addChildWindow(1); |
| 105 | final TestWindowContainer child21 = child2.addChildWindow(1); |
| 106 | |
| 107 | child12.remove(); |
| 108 | assertNull(child12.getParentWindow()); |
| 109 | assertEquals(1, child1.getChildrenCount()); |
| 110 | assertFalse(child1.hasChild(child12)); |
| 111 | assertFalse(root.hasChild(child12)); |
| 112 | |
| 113 | child2.remove(); |
| 114 | assertNull(child2.getParentWindow()); |
| 115 | assertNull(child21.getParentWindow()); |
| 116 | assertEquals(0, child2.getChildrenCount()); |
| 117 | assertEquals(1, root.getChildrenCount()); |
| 118 | assertFalse(root.hasChild(child2)); |
| 119 | assertFalse(root.hasChild(child21)); |
| 120 | |
| 121 | assertTrue(root.hasChild(child1)); |
| 122 | assertTrue(root.hasChild(child11)); |
| 123 | |
| 124 | root.remove(); |
| 125 | assertEquals(0, root.getChildrenCount()); |
| 126 | } |
| 127 | |
| 128 | /* Used so we can gain access to some protected members of the {@link WindowContainer} class */ |
| 129 | private class TestWindowContainer extends WindowContainer { |
| 130 | private final int mLayer; |
| 131 | |
| 132 | /** |
| 133 | * Compares 2 window layers and returns -1 if the first is lesser than the second in terms |
| 134 | * of z-order and 1 otherwise. |
| 135 | */ |
| 136 | private final Comparator<WindowContainer> mWindowSubLayerComparator = (w1, w2) -> { |
| 137 | final int layer1 = ((TestWindowContainer)w1).mLayer; |
| 138 | final int layer2 = ((TestWindowContainer)w2).mLayer; |
| 139 | if (layer1 < layer2 || (layer1 == layer2 && layer2 < 0 )) { |
| 140 | // We insert the child window into the list ordered by the mLayer. |
| 141 | // For same layers, the negative one should go below others; the positive one should |
| 142 | // go above others. |
| 143 | return -1; |
| 144 | } |
| 145 | return 1; |
| 146 | }; |
| 147 | |
| 148 | TestWindowContainer() { |
| 149 | mLayer = 0; |
| 150 | } |
| 151 | |
| 152 | TestWindowContainer(int layer) { |
| 153 | mLayer = layer; |
| 154 | } |
| 155 | |
| 156 | TestWindowContainer getParentWindow() { |
| 157 | return (TestWindowContainer) getParent(); |
| 158 | } |
| 159 | |
| 160 | int getChildrenCount() { |
| 161 | return mChildren.size(); |
| 162 | } |
| 163 | |
| 164 | TestWindowContainer addChildWindow(int layer) { |
| 165 | TestWindowContainer child = new TestWindowContainer(layer); |
| 166 | addChild(child, mWindowSubLayerComparator); |
| 167 | return child; |
| 168 | } |
| 169 | |
| 170 | TestWindowContainer getChildAt(int index) { |
| 171 | return (TestWindowContainer) mChildren.get(index); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | } |