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