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 | |
Bryce Lee | ae73ba4 | 2017-05-05 09:58:25 -0700 | [diff] [blame] | 19 | import android.view.WindowManager; |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | import org.junit.runner.RunWith; |
| 22 | |
Wale Ogunwale | 5fc7096 | 2016-09-09 22:36:19 -0700 | [diff] [blame] | 23 | import android.platform.test.annotations.Presubmit; |
chaviw | 4f0f348 | 2018-03-01 15:47:56 -0800 | [diff] [blame] | 24 | import android.support.test.filters.FlakyTest; |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 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 | |
Wale Ogunwale | 3424795 | 2017-02-19 11:57:53 -0800 | [diff] [blame] | 28 | import java.util.LinkedList; |
| 29 | |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 30 | import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW; |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 31 | import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 32 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 33 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; |
Wale Ogunwale | 3424795 | 2017-02-19 11:57:53 -0800 | [diff] [blame] | 34 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL; |
| 35 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG; |
| 36 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA; |
| 37 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY; |
| 38 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL; |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 39 | import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD; |
chaviw | ebcbc34 | 2018-02-07 13:19:00 -0800 | [diff] [blame] | 40 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 41 | import static org.junit.Assert.assertEquals; |
| 42 | import static org.junit.Assert.assertFalse; |
| 43 | import static org.junit.Assert.assertNull; |
| 44 | import static org.junit.Assert.assertTrue; |
chaviw | 4023466 | 2018-02-07 09:37:16 -0800 | [diff] [blame] | 45 | import static org.mockito.ArgumentMatchers.anyLong; |
| 46 | import static org.mockito.ArgumentMatchers.anyString; |
| 47 | import static org.mockito.Mockito.reset; |
| 48 | import static org.mockito.Mockito.verify; |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Tests for the {@link WindowState} class. |
| 52 | * |
Jorim Jaggi | 7220775 | 2018-01-08 13:16:59 +0100 | [diff] [blame] | 53 | * atest FrameworksServicesTests:com.android.server.wm.WindowStateTests |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 54 | */ |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 55 | @SmallTest |
chaviw | 36d0a34 | 2018-03-01 17:29:21 -0800 | [diff] [blame^] | 56 | @FlakyTest(bugId = 74078662) |
Wale Ogunwale | 5fc7096 | 2016-09-09 22:36:19 -0700 | [diff] [blame] | 57 | @Presubmit |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 58 | @RunWith(AndroidJUnit4.class) |
Wale Ogunwale | 44fbdf5 | 2016-11-16 10:18:45 -0800 | [diff] [blame] | 59 | public class WindowStateTests extends WindowTestsBase { |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 60 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 61 | @Test |
Wale Ogunwale | 9d14790 | 2016-07-16 11:58:55 -0700 | [diff] [blame] | 62 | public void testIsParentWindowHidden() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 63 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 64 | final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1"); |
| 65 | final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2"); |
Wale Ogunwale | 9d14790 | 2016-07-16 11:58:55 -0700 | [diff] [blame] | 66 | |
chaviw | ebcbc34 | 2018-02-07 13:19:00 -0800 | [diff] [blame] | 67 | // parentWindow is initially set to hidden. |
| 68 | assertTrue(parentWindow.mHidden); |
| 69 | assertFalse(parentWindow.isParentWindowHidden()); |
| 70 | assertTrue(child1.isParentWindowHidden()); |
| 71 | assertTrue(child2.isParentWindowHidden()); |
| 72 | |
| 73 | parentWindow.mHidden = false; |
Wale Ogunwale | 9d14790 | 2016-07-16 11:58:55 -0700 | [diff] [blame] | 74 | assertFalse(parentWindow.isParentWindowHidden()); |
| 75 | assertFalse(child1.isParentWindowHidden()); |
| 76 | assertFalse(child2.isParentWindowHidden()); |
| 77 | |
Wale Ogunwale | 9d14790 | 2016-07-16 11:58:55 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 80 | @Test |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 81 | public void testIsChildWindow() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 82 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 83 | final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1"); |
| 84 | final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2"); |
| 85 | final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow"); |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 86 | |
| 87 | assertFalse(parentWindow.isChildWindow()); |
| 88 | assertTrue(child1.isChildWindow()); |
| 89 | assertTrue(child2.isChildWindow()); |
| 90 | assertFalse(randomWindow.isChildWindow()); |
| 91 | } |
| 92 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 93 | @Test |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 94 | public void testHasChild() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 95 | final WindowState win1 = createWindow(null, TYPE_APPLICATION, "win1"); |
| 96 | final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW, "win11"); |
| 97 | final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW, "win12"); |
| 98 | final WindowState win2 = createWindow(null, TYPE_APPLICATION, "win2"); |
| 99 | final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW, "win21"); |
| 100 | final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow"); |
Wale Ogunwale | adde52e | 2016-07-16 13:11:55 -0700 | [diff] [blame] | 101 | |
| 102 | assertTrue(win1.hasChild(win11)); |
| 103 | assertTrue(win1.hasChild(win12)); |
| 104 | assertTrue(win2.hasChild(win21)); |
| 105 | |
| 106 | assertFalse(win1.hasChild(win21)); |
| 107 | assertFalse(win1.hasChild(randomWindow)); |
| 108 | |
| 109 | assertFalse(win2.hasChild(win11)); |
| 110 | assertFalse(win2.hasChild(win12)); |
| 111 | assertFalse(win2.hasChild(randomWindow)); |
| 112 | } |
| 113 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 114 | @Test |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 115 | public void testGetParentWindow() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 116 | final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 117 | final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1"); |
| 118 | final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2"); |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 119 | |
| 120 | assertNull(parentWindow.getParentWindow()); |
| 121 | assertEquals(parentWindow, child1.getParentWindow()); |
| 122 | assertEquals(parentWindow, child2.getParentWindow()); |
| 123 | } |
| 124 | |
Wale Ogunwale | a7e3b64 | 2016-08-29 10:15:34 -0700 | [diff] [blame] | 125 | @Test |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 126 | public void testGetTopParentWindow() throws Exception { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 127 | final WindowState root = createWindow(null, TYPE_APPLICATION, "root"); |
| 128 | final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1"); |
| 129 | final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2"); |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 130 | |
| 131 | assertEquals(root, root.getTopParentWindow()); |
| 132 | assertEquals(root, child1.getTopParentWindow()); |
| 133 | assertEquals(child1, child2.getParentWindow()); |
| 134 | assertEquals(root, child2.getTopParentWindow()); |
Wale Ogunwale | ea92d97 | 2016-12-08 07:33:13 -0800 | [diff] [blame] | 135 | |
| 136 | // Test case were child is detached from parent. |
| 137 | root.removeChild(child1); |
| 138 | assertEquals(child1, child1.getTopParentWindow()); |
| 139 | assertEquals(child1, child2.getParentWindow()); |
Wale Ogunwale | caa53af | 2016-07-17 14:50:26 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Jorim Jaggi | af221d1 | 2016-11-15 14:59:57 -0800 | [diff] [blame] | 142 | @Test |
| 143 | public void testIsOnScreen_hiddenByPolicy() { |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 144 | final WindowState window = createWindow(null, TYPE_APPLICATION, "window"); |
Jorim Jaggi | af221d1 | 2016-11-15 14:59:57 -0800 | [diff] [blame] | 145 | window.setHasSurface(true); |
| 146 | assertTrue(window.isOnScreen()); |
| 147 | window.hideLw(false /* doAnimation */); |
| 148 | assertFalse(window.isOnScreen()); |
| 149 | } |
Wale Ogunwale | 805d9ec | 2016-12-07 12:22:08 -0800 | [diff] [blame] | 150 | |
| 151 | @Test |
| 152 | public void testCanBeImeTarget() throws Exception { |
| 153 | final WindowState appWindow = createWindow(null, TYPE_APPLICATION, "appWindow"); |
| 154 | final WindowState imeWindow = createWindow(null, TYPE_INPUT_METHOD, "imeWindow"); |
| 155 | |
| 156 | // Setting FLAG_NOT_FOCUSABLE without FLAG_ALT_FOCUSABLE_IM prevents the window from being |
| 157 | // an IME target. |
| 158 | appWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE; |
| 159 | imeWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE; |
| 160 | |
| 161 | // Make windows visible |
| 162 | appWindow.setHasSurface(true); |
| 163 | imeWindow.setHasSurface(true); |
| 164 | |
| 165 | // Windows without flags (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM) can't be IME targets |
| 166 | assertFalse(appWindow.canBeImeTarget()); |
| 167 | assertFalse(imeWindow.canBeImeTarget()); |
| 168 | |
| 169 | // Add IME target flags |
| 170 | appWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM); |
| 171 | imeWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM); |
| 172 | |
| 173 | // Visible app window with flags can be IME target while an IME window can never be an IME |
| 174 | // target regardless of its visibility or flags. |
| 175 | assertTrue(appWindow.canBeImeTarget()); |
| 176 | assertFalse(imeWindow.canBeImeTarget()); |
| 177 | |
| 178 | // Make windows invisible |
| 179 | appWindow.hideLw(false /* doAnimation */); |
| 180 | imeWindow.hideLw(false /* doAnimation */); |
| 181 | |
| 182 | // Invisible window can't be IME targets even if they have the right flags. |
| 183 | assertFalse(appWindow.canBeImeTarget()); |
| 184 | assertFalse(imeWindow.canBeImeTarget()); |
| 185 | } |
Wale Ogunwale | 3424795 | 2017-02-19 11:57:53 -0800 | [diff] [blame] | 186 | |
| 187 | @Test |
| 188 | public void testGetWindow() throws Exception { |
| 189 | final WindowState root = createWindow(null, TYPE_APPLICATION, "root"); |
| 190 | final WindowState mediaChild = createWindow(root, TYPE_APPLICATION_MEDIA, "mediaChild"); |
| 191 | final WindowState mediaOverlayChild = createWindow(root, |
| 192 | TYPE_APPLICATION_MEDIA_OVERLAY, "mediaOverlayChild"); |
| 193 | final WindowState attachedDialogChild = createWindow(root, |
| 194 | TYPE_APPLICATION_ATTACHED_DIALOG, "attachedDialogChild"); |
| 195 | final WindowState subPanelChild = createWindow(root, |
| 196 | TYPE_APPLICATION_SUB_PANEL, "subPanelChild"); |
| 197 | final WindowState aboveSubPanelChild = createWindow(root, |
| 198 | TYPE_APPLICATION_ABOVE_SUB_PANEL, "aboveSubPanelChild"); |
| 199 | |
| 200 | final LinkedList<WindowState> windows = new LinkedList(); |
| 201 | |
| 202 | root.getWindow(w -> { |
| 203 | windows.addLast(w); |
| 204 | return false; |
| 205 | }); |
| 206 | |
| 207 | // getWindow should have returned candidate windows in z-order. |
| 208 | assertEquals(aboveSubPanelChild, windows.pollFirst()); |
| 209 | assertEquals(subPanelChild, windows.pollFirst()); |
| 210 | assertEquals(attachedDialogChild, windows.pollFirst()); |
| 211 | assertEquals(root, windows.pollFirst()); |
| 212 | assertEquals(mediaOverlayChild, windows.pollFirst()); |
| 213 | assertEquals(mediaChild, windows.pollFirst()); |
| 214 | assertTrue(windows.isEmpty()); |
| 215 | } |
Bryce Lee | ae73ba4 | 2017-05-05 09:58:25 -0700 | [diff] [blame] | 216 | |
| 217 | @Test |
| 218 | public void testPrepareWindowToDisplayDuringRelayout() throws Exception { |
| 219 | testPrepareWindowToDisplayDuringRelayout(false /*wasVisible*/); |
| 220 | testPrepareWindowToDisplayDuringRelayout(true /*wasVisible*/); |
| 221 | } |
| 222 | |
Jorim Jaggi | 7220775 | 2018-01-08 13:16:59 +0100 | [diff] [blame] | 223 | @Test |
| 224 | public void testCanAffectSystemUiFlags() throws Exception { |
| 225 | final WindowState app = createWindow(null, TYPE_APPLICATION, "app"); |
| 226 | app.mToken.setHidden(false); |
| 227 | assertTrue(app.canAffectSystemUiFlags()); |
| 228 | app.mToken.setHidden(true); |
| 229 | assertFalse(app.canAffectSystemUiFlags()); |
| 230 | app.mToken.setHidden(false); |
| 231 | app.mAttrs.alpha = 0.0f; |
| 232 | assertFalse(app.canAffectSystemUiFlags()); |
| 233 | } |
| 234 | |
Jorim Jaggi | 4876b4a | 2018-01-11 15:43:49 +0100 | [diff] [blame] | 235 | @Test |
| 236 | public void testIsSelfOrAncestorWindowAnimating() throws Exception { |
| 237 | final WindowState root = createWindow(null, TYPE_APPLICATION, "root"); |
| 238 | final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1"); |
| 239 | final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2"); |
| 240 | assertFalse(child2.isSelfOrAncestorWindowAnimatingExit()); |
| 241 | child2.mAnimatingExit = true; |
| 242 | assertTrue(child2.isSelfOrAncestorWindowAnimatingExit()); |
| 243 | child2.mAnimatingExit = false; |
| 244 | root.mAnimatingExit = true; |
| 245 | assertTrue(child2.isSelfOrAncestorWindowAnimatingExit()); |
| 246 | } |
| 247 | |
Bryce Lee | ae73ba4 | 2017-05-05 09:58:25 -0700 | [diff] [blame] | 248 | private void testPrepareWindowToDisplayDuringRelayout(boolean wasVisible) { |
chaviw | 4023466 | 2018-02-07 09:37:16 -0800 | [diff] [blame] | 249 | reset(mPowerManagerWrapper); |
Bryce Lee | ae73ba4 | 2017-05-05 09:58:25 -0700 | [diff] [blame] | 250 | final WindowState root = createWindow(null, TYPE_APPLICATION, "root"); |
| 251 | root.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON; |
Bryce Lee | ae73ba4 | 2017-05-05 09:58:25 -0700 | [diff] [blame] | 252 | |
Bryce Lee | f858b57 | 2017-06-29 14:03:33 -0700 | [diff] [blame] | 253 | root.prepareWindowToDisplayDuringRelayout(wasVisible /*wasVisible*/); |
chaviw | 4023466 | 2018-02-07 09:37:16 -0800 | [diff] [blame] | 254 | verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString()); |
Bryce Lee | ae73ba4 | 2017-05-05 09:58:25 -0700 | [diff] [blame] | 255 | } |
Wale Ogunwale | b699ce0 | 2016-07-18 12:05:30 -0700 | [diff] [blame] | 256 | } |