blob: 85e846db60b74b8491e5de1ba658dae507ce06f2 [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;
Suprabh Shukla69c71422018-04-02 18:39:01 -070040import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
Wale Ogunwale34247952017-02-19 11:57:53 -080041import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080042import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
chaviwebcbc342018-02-07 13:19:00 -080043
Adrian Roos5251b1d2018-03-23 18:57:43 +010044import static org.hamcrest.Matchers.is;
45import static org.hamcrest.Matchers.not;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070046import static org.junit.Assert.assertEquals;
47import static org.junit.Assert.assertFalse;
48import static org.junit.Assert.assertNull;
Adrian Roos5251b1d2018-03-23 18:57:43 +010049import static org.junit.Assert.assertThat;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070050import static org.junit.Assert.assertTrue;
chaviw40234662018-02-07 09:37:16 -080051import static org.mockito.ArgumentMatchers.anyLong;
52import static org.mockito.ArgumentMatchers.anyString;
chaviwb28de1f2018-03-02 10:42:36 -080053import static org.mockito.Mockito.never;
chaviw40234662018-02-07 09:37:16 -080054import static org.mockito.Mockito.reset;
Suprabh Shukla69c71422018-04-02 18:39:01 -070055import static org.mockito.Mockito.spy;
chaviw40234662018-02-07 09:37:16 -080056import static org.mockito.Mockito.verify;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070057
58/**
59 * Tests for the {@link WindowState} class.
60 *
Jorim Jaggi72207752018-01-08 13:16:59 +010061 * atest FrameworksServicesTests:com.android.server.wm.WindowStateTests
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070062 */
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070063@SmallTest
chaviw36d0a342018-03-01 17:29:21 -080064@FlakyTest(bugId = 74078662)
Wale Ogunwale5fc70962016-09-09 22:36:19 -070065@Presubmit
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070066@RunWith(AndroidJUnit4.class)
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080067public class WindowStateTests extends WindowTestsBase {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070068
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070069 @Test
Wale Ogunwale9d147902016-07-16 11:58:55 -070070 public void testIsParentWindowHidden() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080071 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
72 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
73 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
Wale Ogunwale9d147902016-07-16 11:58:55 -070074
chaviwebcbc342018-02-07 13:19:00 -080075 // parentWindow is initially set to hidden.
76 assertTrue(parentWindow.mHidden);
77 assertFalse(parentWindow.isParentWindowHidden());
78 assertTrue(child1.isParentWindowHidden());
79 assertTrue(child2.isParentWindowHidden());
80
81 parentWindow.mHidden = false;
Wale Ogunwale9d147902016-07-16 11:58:55 -070082 assertFalse(parentWindow.isParentWindowHidden());
83 assertFalse(child1.isParentWindowHidden());
84 assertFalse(child2.isParentWindowHidden());
85
Wale Ogunwale9d147902016-07-16 11:58:55 -070086 }
87
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070088 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070089 public void testIsChildWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080090 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
91 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
92 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
93 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070094
95 assertFalse(parentWindow.isChildWindow());
96 assertTrue(child1.isChildWindow());
97 assertTrue(child2.isChildWindow());
98 assertFalse(randomWindow.isChildWindow());
99 }
100
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700101 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700102 public void testHasChild() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800103 final WindowState win1 = createWindow(null, TYPE_APPLICATION, "win1");
104 final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW, "win11");
105 final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW, "win12");
106 final WindowState win2 = createWindow(null, TYPE_APPLICATION, "win2");
107 final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW, "win21");
108 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700109
110 assertTrue(win1.hasChild(win11));
111 assertTrue(win1.hasChild(win12));
112 assertTrue(win2.hasChild(win21));
113
114 assertFalse(win1.hasChild(win21));
115 assertFalse(win1.hasChild(randomWindow));
116
117 assertFalse(win2.hasChild(win11));
118 assertFalse(win2.hasChild(win12));
119 assertFalse(win2.hasChild(randomWindow));
120 }
121
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700122 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700123 public void testGetParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800124 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
125 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
126 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700127
128 assertNull(parentWindow.getParentWindow());
129 assertEquals(parentWindow, child1.getParentWindow());
130 assertEquals(parentWindow, child2.getParentWindow());
131 }
132
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700133 @Test
Suprabh Shukla69c71422018-04-02 18:39:01 -0700134 public void testOverlayWindowHiddenWhenSuspended() {
135 final WindowState overlayWindow = spy(createWindow(null, TYPE_APPLICATION_OVERLAY,
136 "overlayWindow"));
137 overlayWindow.setHiddenWhileSuspended(true);
138 verify(overlayWindow).hideLw(true, true);
139 overlayWindow.setHiddenWhileSuspended(false);
140 verify(overlayWindow).showLw(true, true);
141 }
142
143 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700144 public void testGetTopParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800145 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
146 final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1");
147 final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2");
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700148
149 assertEquals(root, root.getTopParentWindow());
150 assertEquals(root, child1.getTopParentWindow());
151 assertEquals(child1, child2.getParentWindow());
152 assertEquals(root, child2.getTopParentWindow());
Wale Ogunwaleea92d972016-12-08 07:33:13 -0800153
154 // Test case were child is detached from parent.
155 root.removeChild(child1);
156 assertEquals(child1, child1.getTopParentWindow());
157 assertEquals(child1, child2.getParentWindow());
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700158 }
159
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800160 @Test
161 public void testIsOnScreen_hiddenByPolicy() {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800162 final WindowState window = createWindow(null, TYPE_APPLICATION, "window");
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800163 window.setHasSurface(true);
164 assertTrue(window.isOnScreen());
165 window.hideLw(false /* doAnimation */);
166 assertFalse(window.isOnScreen());
167 }
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800168
169 @Test
170 public void testCanBeImeTarget() throws Exception {
171 final WindowState appWindow = createWindow(null, TYPE_APPLICATION, "appWindow");
172 final WindowState imeWindow = createWindow(null, TYPE_INPUT_METHOD, "imeWindow");
173
174 // Setting FLAG_NOT_FOCUSABLE without FLAG_ALT_FOCUSABLE_IM prevents the window from being
175 // an IME target.
176 appWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
177 imeWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
178
179 // Make windows visible
180 appWindow.setHasSurface(true);
181 imeWindow.setHasSurface(true);
182
183 // Windows without flags (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM) can't be IME targets
184 assertFalse(appWindow.canBeImeTarget());
185 assertFalse(imeWindow.canBeImeTarget());
186
187 // Add IME target flags
188 appWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
189 imeWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
190
191 // Visible app window with flags can be IME target while an IME window can never be an IME
192 // target regardless of its visibility or flags.
193 assertTrue(appWindow.canBeImeTarget());
194 assertFalse(imeWindow.canBeImeTarget());
195
196 // Make windows invisible
197 appWindow.hideLw(false /* doAnimation */);
198 imeWindow.hideLw(false /* doAnimation */);
199
200 // Invisible window can't be IME targets even if they have the right flags.
201 assertFalse(appWindow.canBeImeTarget());
202 assertFalse(imeWindow.canBeImeTarget());
203 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800204
205 @Test
206 public void testGetWindow() throws Exception {
207 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
208 final WindowState mediaChild = createWindow(root, TYPE_APPLICATION_MEDIA, "mediaChild");
209 final WindowState mediaOverlayChild = createWindow(root,
210 TYPE_APPLICATION_MEDIA_OVERLAY, "mediaOverlayChild");
211 final WindowState attachedDialogChild = createWindow(root,
212 TYPE_APPLICATION_ATTACHED_DIALOG, "attachedDialogChild");
213 final WindowState subPanelChild = createWindow(root,
214 TYPE_APPLICATION_SUB_PANEL, "subPanelChild");
215 final WindowState aboveSubPanelChild = createWindow(root,
216 TYPE_APPLICATION_ABOVE_SUB_PANEL, "aboveSubPanelChild");
217
218 final LinkedList<WindowState> windows = new LinkedList();
219
220 root.getWindow(w -> {
221 windows.addLast(w);
222 return false;
223 });
224
225 // getWindow should have returned candidate windows in z-order.
226 assertEquals(aboveSubPanelChild, windows.pollFirst());
227 assertEquals(subPanelChild, windows.pollFirst());
228 assertEquals(attachedDialogChild, windows.pollFirst());
229 assertEquals(root, windows.pollFirst());
230 assertEquals(mediaOverlayChild, windows.pollFirst());
231 assertEquals(mediaChild, windows.pollFirst());
232 assertTrue(windows.isEmpty());
233 }
Bryce Leeae73ba42017-05-05 09:58:25 -0700234
235 @Test
236 public void testPrepareWindowToDisplayDuringRelayout() throws Exception {
237 testPrepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
238 testPrepareWindowToDisplayDuringRelayout(true /*wasVisible*/);
chaviwb28de1f2018-03-02 10:42:36 -0800239
240 // Call prepareWindowToDisplayDuringRelayout for a window without FLAG_TURN_SCREEN_ON
241 // before calling prepareWindowToDisplayDuringRelayout for windows with flag in the same
242 // appWindowToken.
243 final AppWindowToken appWindowToken = createAppWindowToken(mDisplayContent,
244 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD);
245 final WindowState first = createWindow(null, TYPE_APPLICATION, appWindowToken, "first");
246 final WindowState second = createWindow(null, TYPE_APPLICATION, appWindowToken, "second");
247 second.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
248
249 reset(mPowerManagerWrapper);
250 first.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
251 verify(mPowerManagerWrapper, never()).wakeUp(anyLong(), anyString());
252 assertTrue(appWindowToken.canTurnScreenOn());
253
254 reset(mPowerManagerWrapper);
255 second.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
256 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
257 assertFalse(appWindowToken.canTurnScreenOn());
258
259 // Call prepareWindowToDisplayDuringRelayout for two window that have FLAG_TURN_SCREEN_ON
260 // from the same appWindowToken. Only one should trigger the wakeup.
261 appWindowToken.setCanTurnScreenOn(true);
262 first.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
263 second.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
264
265 reset(mPowerManagerWrapper);
266 first.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
267 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
268 assertFalse(appWindowToken.canTurnScreenOn());
269
270 reset(mPowerManagerWrapper);
271 second.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
272 verify(mPowerManagerWrapper, never()).wakeUp(anyLong(), anyString());
273 assertFalse(appWindowToken.canTurnScreenOn());
274
275 // Call prepareWindowToDisplayDuringRelayout for a windows that are not children of an
276 // appWindowToken. Both windows have the FLAG_TURNS_SCREEN_ON so both should call wakeup
chaviw97d28202018-02-27 16:23:53 -0800277 final WindowToken windowToken = WindowTestUtils.createTestWindowToken(FIRST_SUB_WINDOW,
chaviwb28de1f2018-03-02 10:42:36 -0800278 mDisplayContent);
279 final WindowState firstWindow = createWindow(null, TYPE_APPLICATION, windowToken,
280 "firstWindow");
281 final WindowState secondWindow = createWindow(null, TYPE_APPLICATION, windowToken,
282 "secondWindow");
283 firstWindow.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
284 secondWindow.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
285
286 reset(mPowerManagerWrapper);
287 firstWindow.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
288 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
289
290 reset(mPowerManagerWrapper);
291 secondWindow.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
292 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
Bryce Leeae73ba42017-05-05 09:58:25 -0700293 }
294
Jorim Jaggi72207752018-01-08 13:16:59 +0100295 @Test
296 public void testCanAffectSystemUiFlags() throws Exception {
297 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
298 app.mToken.setHidden(false);
299 assertTrue(app.canAffectSystemUiFlags());
300 app.mToken.setHidden(true);
301 assertFalse(app.canAffectSystemUiFlags());
302 app.mToken.setHidden(false);
303 app.mAttrs.alpha = 0.0f;
304 assertFalse(app.canAffectSystemUiFlags());
Jorim Jaggi50bf59c2018-03-09 17:29:48 +0100305
306 }
307
308 @Test
309 public void testCanAffectSystemUiFlags_disallow() throws Exception {
310 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
311 app.mToken.setHidden(false);
312 assertTrue(app.canAffectSystemUiFlags());
313 app.getTask().setCanAffectSystemUiFlags(false);
314 assertFalse(app.canAffectSystemUiFlags());
Jorim Jaggi72207752018-01-08 13:16:59 +0100315 }
316
Jorim Jaggi4876b4a2018-01-11 15:43:49 +0100317 @Test
318 public void testIsSelfOrAncestorWindowAnimating() throws Exception {
319 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
320 final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1");
321 final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2");
322 assertFalse(child2.isSelfOrAncestorWindowAnimatingExit());
323 child2.mAnimatingExit = true;
324 assertTrue(child2.isSelfOrAncestorWindowAnimatingExit());
325 child2.mAnimatingExit = false;
326 root.mAnimatingExit = true;
327 assertTrue(child2.isSelfOrAncestorWindowAnimatingExit());
328 }
329
Adrian Roos5251b1d2018-03-23 18:57:43 +0100330 @Test
331 public void testLayoutSeqResetOnReparent() throws Exception {
332 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
333 app.mLayoutSeq = 1;
334 mDisplayContent.mLayoutSeq = 1;
335
336 app.onDisplayChanged(mDisplayContent);
337
338 assertThat(app.mLayoutSeq, not(is(mDisplayContent.mLayoutSeq)));
339 }
340
Brad Stenningaf596412018-04-02 12:03:19 -0700341 @Test
342 public void testDisplayIdUpdatedOnReparent() throws Exception {
343 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
344 // fake a different display
345 app.mInputWindowHandle.displayId = mDisplayContent.getDisplayId() + 1;
346 app.onDisplayChanged(mDisplayContent);
347
348 assertThat(app.mInputWindowHandle.displayId, is(mDisplayContent.getDisplayId()));
349 assertThat(app.getDisplayId(), is(mDisplayContent.getDisplayId()));
350 }
351
Bryce Leeae73ba42017-05-05 09:58:25 -0700352 private void testPrepareWindowToDisplayDuringRelayout(boolean wasVisible) {
chaviw40234662018-02-07 09:37:16 -0800353 reset(mPowerManagerWrapper);
Bryce Leeae73ba42017-05-05 09:58:25 -0700354 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
355 root.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
Bryce Leeae73ba42017-05-05 09:58:25 -0700356
Bryce Leef858b572017-06-29 14:03:33 -0700357 root.prepareWindowToDisplayDuringRelayout(wasVisible /*wasVisible*/);
chaviw40234662018-02-07 09:37:16 -0800358 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
Bryce Leeae73ba42017-05-05 09:58:25 -0700359 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700360}