Wale Ogunwale | b699ce0 | 2016-07-18 12:05: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 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.junit.runner.RunWith; |
| 22 | |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 24 | import android.support.test.InstrumentationRegistry; |
| 25 | import android.support.test.filters.SmallTest; |
| 26 | import android.support.test.runner.AndroidJUnit4; |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 27 | import android.view.IWindow; |
| 28 | import android.view.WindowManager; |
| 29 | import android.view.WindowManagerPolicy; |
| 30 | |
| 31 | import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW; |
| 32 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 33 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY; |
| 34 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL; |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 35 | import static org.junit.Assert.assertEquals; |
| 36 | import static org.junit.Assert.assertFalse; |
| 37 | import static org.junit.Assert.assertNull; |
| 38 | import static org.junit.Assert.assertTrue; |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Tests for the {@link WindowState} class. |
| 42 | * |
| 43 | * Build: mmma -j32 frameworks/base/services/tests/servicestests |
Wale Ogunwale | d1c3791 | 2016-08-16 03:19:39 -0700 | [diff] [blame] | 44 | * Install: adb install -r out/target/product/$TARGET_PRODUCT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 45 | * Run: adb shell am instrument -w -e class com.android.server.wm.WindowStateTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner |
| 46 | */ |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 47 | @SmallTest |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 48 | @RunWith(AndroidJUnit4.class) |
| 49 | public class WindowStateTests { |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 50 | |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 51 | private static WindowManagerService sWm = null; |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 52 | private WindowToken mWindowToken; |
| 53 | private final WindowManagerPolicy mPolicy = new TestWindowManagerPolicy(); |
| 54 | private final IWindow mIWindow = new TestIWindow(); |
| 55 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 56 | @Before |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 57 | public void setUp() throws Exception { |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 58 | final Context context = InstrumentationRegistry.getTargetContext(); |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 59 | if (sWm == null) { |
| 60 | // We only want to do this once for the test process as we don't want WM to try to |
| 61 | // register a bunch of local services again. |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 62 | sWm = WindowManagerService.main(context, null, true, false, false, mPolicy); |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 63 | } |
| 64 | mWindowToken = new WindowToken(sWm, null, 0, false); |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 67 | @Test |
Wale Ogunwale | 9d14790 | 2016-07-16 11:58:55 -0700 | [diff] [blame] | 68 | public void testIsParentWindowHidden() throws Exception { |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 69 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION); |
| 70 | final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW); |
| 71 | final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW); |
Wale Ogunwale | 9d14790 | 2016-07-16 11:58:55 -0700 | [diff] [blame] | 72 | |
| 73 | assertFalse(parentWindow.mHidden); |
| 74 | assertFalse(parentWindow.isParentWindowHidden()); |
| 75 | assertFalse(child1.isParentWindowHidden()); |
| 76 | assertFalse(child2.isParentWindowHidden()); |
| 77 | |
| 78 | parentWindow.mHidden = true; |
| 79 | assertFalse(parentWindow.isParentWindowHidden()); |
| 80 | assertTrue(child1.isParentWindowHidden()); |
| 81 | assertTrue(child2.isParentWindowHidden()); |
| 82 | } |
| 83 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 84 | @Test |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 85 | public void testIsChildWindow() throws Exception { |
| 86 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION); |
| 87 | final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW); |
| 88 | final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW); |
| 89 | final WindowState randomWindow = createWindow(null, TYPE_APPLICATION); |
| 90 | |
| 91 | assertFalse(parentWindow.isChildWindow()); |
| 92 | assertTrue(child1.isChildWindow()); |
| 93 | assertTrue(child2.isChildWindow()); |
| 94 | assertFalse(randomWindow.isChildWindow()); |
| 95 | } |
| 96 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 97 | @Test |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 98 | public void testHasChild() throws Exception { |
| 99 | final WindowState win1 = createWindow(null, TYPE_APPLICATION); |
| 100 | final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW); |
| 101 | final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW); |
| 102 | final WindowState win2 = createWindow(null, TYPE_APPLICATION); |
| 103 | final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW); |
| 104 | final WindowState randomWindow = createWindow(null, TYPE_APPLICATION); |
| 105 | |
| 106 | assertTrue(win1.hasChild(win11)); |
| 107 | assertTrue(win1.hasChild(win12)); |
| 108 | assertTrue(win2.hasChild(win21)); |
| 109 | |
| 110 | assertFalse(win1.hasChild(win21)); |
| 111 | assertFalse(win1.hasChild(randomWindow)); |
| 112 | |
| 113 | assertFalse(win2.hasChild(win11)); |
| 114 | assertFalse(win2.hasChild(win12)); |
| 115 | assertFalse(win2.hasChild(randomWindow)); |
| 116 | } |
| 117 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 118 | @Test |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 119 | public void testGetBottomChild() throws Exception { |
| 120 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION); |
| 121 | assertNull(parentWindow.getBottomChild()); |
| 122 | |
| 123 | final WindowState child1 = createWindow(parentWindow, TYPE_APPLICATION_PANEL); |
| 124 | assertEquals(child1, parentWindow.getBottomChild()); |
| 125 | |
| 126 | final WindowState child2 = createWindow(parentWindow, TYPE_APPLICATION_PANEL); |
| 127 | // Since child1 and child2 are at the same layer, then child2 is expect to be added on top |
| 128 | // on child1 |
| 129 | assertEquals(child1, parentWindow.getBottomChild()); |
| 130 | |
| 131 | final WindowState child3 = createWindow(parentWindow, TYPE_APPLICATION_MEDIA_OVERLAY); |
| 132 | // Since child3 is a negative layer, we would expect it to be added below current children |
| 133 | // with positive layers. |
| 134 | assertEquals(child3, parentWindow.getBottomChild()); |
| 135 | |
| 136 | final WindowState child4 = createWindow(parentWindow, TYPE_APPLICATION_MEDIA_OVERLAY); |
| 137 | // We would also expect additional negative layers to be added below existing negative |
| 138 | // layers. |
| 139 | assertEquals(child4, parentWindow.getBottomChild()); |
| 140 | } |
| 141 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 142 | @Test |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 143 | public void testGetParentWindow() throws Exception { |
| 144 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION); |
| 145 | final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW); |
| 146 | final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW); |
| 147 | |
| 148 | assertNull(parentWindow.getParentWindow()); |
| 149 | assertEquals(parentWindow, child1.getParentWindow()); |
| 150 | assertEquals(parentWindow, child2.getParentWindow()); |
| 151 | } |
| 152 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame^] | 153 | @Test |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 154 | public void testGetTopParentWindow() throws Exception { |
| 155 | final WindowState root = createWindow(null, TYPE_APPLICATION); |
| 156 | final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW); |
| 157 | final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW); |
| 158 | |
| 159 | assertEquals(root, root.getTopParentWindow()); |
| 160 | assertEquals(root, child1.getTopParentWindow()); |
| 161 | assertEquals(child1, child2.getParentWindow()); |
| 162 | assertEquals(root, child2.getTopParentWindow()); |
| 163 | } |
| 164 | |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 165 | private WindowState createWindow(WindowState parent, int type) { |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 166 | final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type); |
| 167 | |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 168 | return new WindowState(sWm, null, mIWindow, mWindowToken, parent, 0, 0, attrs, 0, |
| 169 | sWm.getDefaultDisplayContentLocked(), 0); |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 170 | } |
| 171 | } |