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