blob: cdcb949b2a3c35aa10dd73cbffe20087729a4a08 [file] [log] [blame]
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001/*
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
17package com.android.server.wm;
18
Bryce Leeae73ba42017-05-05 09:58:25 -070019import android.view.WindowManager;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070020import org.junit.Test;
21import org.junit.runner.RunWith;
22
Wale Ogunwale5fc70962016-09-09 22:36:19 -070023import android.platform.test.annotations.Presubmit;
chaviw4f0f3482018-03-01 15:47:56 -080024import android.support.test.filters.FlakyTest;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070025import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070027
Wale Ogunwale34247952017-02-19 11:57:53 -080028import java.util.LinkedList;
29
chaviwb28de1f2018-03-02 10:42:36 -080030import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
31import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070032import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080033import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
34import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070035import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale34247952017-02-19 11:57:53 -080036import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL;
37import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA;
39import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
40import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
chaviwb28de1f2018-03-02 10:42:36 -080041import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080042import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
chaviwebcbc342018-02-07 13:19:00 -080043
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070044import static org.junit.Assert.assertEquals;
45import static org.junit.Assert.assertFalse;
46import static org.junit.Assert.assertNull;
47import static org.junit.Assert.assertTrue;
chaviw40234662018-02-07 09:37:16 -080048import static org.mockito.ArgumentMatchers.anyLong;
49import static org.mockito.ArgumentMatchers.anyString;
chaviwb28de1f2018-03-02 10:42:36 -080050import static org.mockito.Mockito.never;
chaviw40234662018-02-07 09:37:16 -080051import static org.mockito.Mockito.reset;
52import static org.mockito.Mockito.verify;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070053
54/**
55 * Tests for the {@link WindowState} class.
56 *
Jorim Jaggi72207752018-01-08 13:16:59 +010057 * atest FrameworksServicesTests:com.android.server.wm.WindowStateTests
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070058 */
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070059@SmallTest
chaviw36d0a342018-03-01 17:29:21 -080060@FlakyTest(bugId = 74078662)
Wale Ogunwale5fc70962016-09-09 22:36:19 -070061@Presubmit
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070062@RunWith(AndroidJUnit4.class)
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080063public class WindowStateTests extends WindowTestsBase {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070064
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070065 @Test
Wale Ogunwale9d147902016-07-16 11:58:55 -070066 public void testIsParentWindowHidden() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080067 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
68 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
69 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
Wale Ogunwale9d147902016-07-16 11:58:55 -070070
chaviwebcbc342018-02-07 13:19:00 -080071 // parentWindow is initially set to hidden.
72 assertTrue(parentWindow.mHidden);
73 assertFalse(parentWindow.isParentWindowHidden());
74 assertTrue(child1.isParentWindowHidden());
75 assertTrue(child2.isParentWindowHidden());
76
77 parentWindow.mHidden = false;
Wale Ogunwale9d147902016-07-16 11:58:55 -070078 assertFalse(parentWindow.isParentWindowHidden());
79 assertFalse(child1.isParentWindowHidden());
80 assertFalse(child2.isParentWindowHidden());
81
Wale Ogunwale9d147902016-07-16 11:58:55 -070082 }
83
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070084 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070085 public void testIsChildWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080086 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
87 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
88 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
89 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070090
91 assertFalse(parentWindow.isChildWindow());
92 assertTrue(child1.isChildWindow());
93 assertTrue(child2.isChildWindow());
94 assertFalse(randomWindow.isChildWindow());
95 }
96
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070097 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070098 public void testHasChild() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080099 final WindowState win1 = createWindow(null, TYPE_APPLICATION, "win1");
100 final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW, "win11");
101 final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW, "win12");
102 final WindowState win2 = createWindow(null, TYPE_APPLICATION, "win2");
103 final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW, "win21");
104 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700105
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 Ogunwalea7e3b642016-08-29 10:15:34 -0700118 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700119 public void testGetParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800120 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
121 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
122 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700123
124 assertNull(parentWindow.getParentWindow());
125 assertEquals(parentWindow, child1.getParentWindow());
126 assertEquals(parentWindow, child2.getParentWindow());
127 }
128
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700129 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700130 public void testGetTopParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800131 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
132 final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1");
133 final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2");
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700134
135 assertEquals(root, root.getTopParentWindow());
136 assertEquals(root, child1.getTopParentWindow());
137 assertEquals(child1, child2.getParentWindow());
138 assertEquals(root, child2.getTopParentWindow());
Wale Ogunwaleea92d972016-12-08 07:33:13 -0800139
140 // Test case were child is detached from parent.
141 root.removeChild(child1);
142 assertEquals(child1, child1.getTopParentWindow());
143 assertEquals(child1, child2.getParentWindow());
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700144 }
145
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800146 @Test
147 public void testIsOnScreen_hiddenByPolicy() {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800148 final WindowState window = createWindow(null, TYPE_APPLICATION, "window");
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800149 window.setHasSurface(true);
150 assertTrue(window.isOnScreen());
151 window.hideLw(false /* doAnimation */);
152 assertFalse(window.isOnScreen());
153 }
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800154
155 @Test
156 public void testCanBeImeTarget() throws Exception {
157 final WindowState appWindow = createWindow(null, TYPE_APPLICATION, "appWindow");
158 final WindowState imeWindow = createWindow(null, TYPE_INPUT_METHOD, "imeWindow");
159
160 // Setting FLAG_NOT_FOCUSABLE without FLAG_ALT_FOCUSABLE_IM prevents the window from being
161 // an IME target.
162 appWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
163 imeWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
164
165 // Make windows visible
166 appWindow.setHasSurface(true);
167 imeWindow.setHasSurface(true);
168
169 // Windows without flags (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM) can't be IME targets
170 assertFalse(appWindow.canBeImeTarget());
171 assertFalse(imeWindow.canBeImeTarget());
172
173 // Add IME target flags
174 appWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
175 imeWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
176
177 // Visible app window with flags can be IME target while an IME window can never be an IME
178 // target regardless of its visibility or flags.
179 assertTrue(appWindow.canBeImeTarget());
180 assertFalse(imeWindow.canBeImeTarget());
181
182 // Make windows invisible
183 appWindow.hideLw(false /* doAnimation */);
184 imeWindow.hideLw(false /* doAnimation */);
185
186 // Invisible window can't be IME targets even if they have the right flags.
187 assertFalse(appWindow.canBeImeTarget());
188 assertFalse(imeWindow.canBeImeTarget());
189 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800190
191 @Test
192 public void testGetWindow() throws Exception {
193 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
194 final WindowState mediaChild = createWindow(root, TYPE_APPLICATION_MEDIA, "mediaChild");
195 final WindowState mediaOverlayChild = createWindow(root,
196 TYPE_APPLICATION_MEDIA_OVERLAY, "mediaOverlayChild");
197 final WindowState attachedDialogChild = createWindow(root,
198 TYPE_APPLICATION_ATTACHED_DIALOG, "attachedDialogChild");
199 final WindowState subPanelChild = createWindow(root,
200 TYPE_APPLICATION_SUB_PANEL, "subPanelChild");
201 final WindowState aboveSubPanelChild = createWindow(root,
202 TYPE_APPLICATION_ABOVE_SUB_PANEL, "aboveSubPanelChild");
203
204 final LinkedList<WindowState> windows = new LinkedList();
205
206 root.getWindow(w -> {
207 windows.addLast(w);
208 return false;
209 });
210
211 // getWindow should have returned candidate windows in z-order.
212 assertEquals(aboveSubPanelChild, windows.pollFirst());
213 assertEquals(subPanelChild, windows.pollFirst());
214 assertEquals(attachedDialogChild, windows.pollFirst());
215 assertEquals(root, windows.pollFirst());
216 assertEquals(mediaOverlayChild, windows.pollFirst());
217 assertEquals(mediaChild, windows.pollFirst());
218 assertTrue(windows.isEmpty());
219 }
Bryce Leeae73ba42017-05-05 09:58:25 -0700220
221 @Test
222 public void testPrepareWindowToDisplayDuringRelayout() throws Exception {
223 testPrepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
224 testPrepareWindowToDisplayDuringRelayout(true /*wasVisible*/);
chaviwb28de1f2018-03-02 10:42:36 -0800225
226 // Call prepareWindowToDisplayDuringRelayout for a window without FLAG_TURN_SCREEN_ON
227 // before calling prepareWindowToDisplayDuringRelayout for windows with flag in the same
228 // appWindowToken.
229 final AppWindowToken appWindowToken = createAppWindowToken(mDisplayContent,
230 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD);
231 final WindowState first = createWindow(null, TYPE_APPLICATION, appWindowToken, "first");
232 final WindowState second = createWindow(null, TYPE_APPLICATION, appWindowToken, "second");
233 second.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
234
235 reset(mPowerManagerWrapper);
236 first.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
237 verify(mPowerManagerWrapper, never()).wakeUp(anyLong(), anyString());
238 assertTrue(appWindowToken.canTurnScreenOn());
239
240 reset(mPowerManagerWrapper);
241 second.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
242 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
243 assertFalse(appWindowToken.canTurnScreenOn());
244
245 // Call prepareWindowToDisplayDuringRelayout for two window that have FLAG_TURN_SCREEN_ON
246 // from the same appWindowToken. Only one should trigger the wakeup.
247 appWindowToken.setCanTurnScreenOn(true);
248 first.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
249 second.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
250
251 reset(mPowerManagerWrapper);
252 first.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
253 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
254 assertFalse(appWindowToken.canTurnScreenOn());
255
256 reset(mPowerManagerWrapper);
257 second.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
258 verify(mPowerManagerWrapper, never()).wakeUp(anyLong(), anyString());
259 assertFalse(appWindowToken.canTurnScreenOn());
260
261 // Call prepareWindowToDisplayDuringRelayout for a windows that are not children of an
262 // appWindowToken. Both windows have the FLAG_TURNS_SCREEN_ON so both should call wakeup
chaviw97d28202018-02-27 16:23:53 -0800263 final WindowToken windowToken = WindowTestUtils.createTestWindowToken(FIRST_SUB_WINDOW,
chaviwb28de1f2018-03-02 10:42:36 -0800264 mDisplayContent);
265 final WindowState firstWindow = createWindow(null, TYPE_APPLICATION, windowToken,
266 "firstWindow");
267 final WindowState secondWindow = createWindow(null, TYPE_APPLICATION, windowToken,
268 "secondWindow");
269 firstWindow.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
270 secondWindow.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
271
272 reset(mPowerManagerWrapper);
273 firstWindow.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
274 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
275
276 reset(mPowerManagerWrapper);
277 secondWindow.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
278 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
Bryce Leeae73ba42017-05-05 09:58:25 -0700279 }
280
Jorim Jaggi72207752018-01-08 13:16:59 +0100281 @Test
282 public void testCanAffectSystemUiFlags() throws Exception {
283 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
284 app.mToken.setHidden(false);
285 assertTrue(app.canAffectSystemUiFlags());
286 app.mToken.setHidden(true);
287 assertFalse(app.canAffectSystemUiFlags());
288 app.mToken.setHidden(false);
289 app.mAttrs.alpha = 0.0f;
290 assertFalse(app.canAffectSystemUiFlags());
291 }
292
Jorim Jaggi4876b4a2018-01-11 15:43:49 +0100293 @Test
294 public void testIsSelfOrAncestorWindowAnimating() throws Exception {
295 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
296 final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1");
297 final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2");
298 assertFalse(child2.isSelfOrAncestorWindowAnimatingExit());
299 child2.mAnimatingExit = true;
300 assertTrue(child2.isSelfOrAncestorWindowAnimatingExit());
301 child2.mAnimatingExit = false;
302 root.mAnimatingExit = true;
303 assertTrue(child2.isSelfOrAncestorWindowAnimatingExit());
304 }
305
Bryce Leeae73ba42017-05-05 09:58:25 -0700306 private void testPrepareWindowToDisplayDuringRelayout(boolean wasVisible) {
chaviw40234662018-02-07 09:37:16 -0800307 reset(mPowerManagerWrapper);
Bryce Leeae73ba42017-05-05 09:58:25 -0700308 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
309 root.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
Bryce Leeae73ba42017-05-05 09:58:25 -0700310
Bryce Leef858b572017-06-29 14:03:33 -0700311 root.prepareWindowToDisplayDuringRelayout(wasVisible /*wasVisible*/);
chaviw40234662018-02-07 09:37:16 -0800312 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
Bryce Leeae73ba42017-05-05 09:58:25 -0700313 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700314}