blob: 7c928c988b4f0c8d5e87bce9534885a85a47aaec [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;
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080041import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
chaviwebcbc342018-02-07 13:19:00 -080042
Adrian Roos5251b1d2018-03-23 18:57:43 +010043import static org.hamcrest.Matchers.is;
44import static org.hamcrest.Matchers.not;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070045import static org.junit.Assert.assertEquals;
46import static org.junit.Assert.assertFalse;
47import static org.junit.Assert.assertNull;
Adrian Roos5251b1d2018-03-23 18:57:43 +010048import static org.junit.Assert.assertThat;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070049import static org.junit.Assert.assertTrue;
chaviw40234662018-02-07 09:37:16 -080050import static org.mockito.ArgumentMatchers.anyLong;
51import static org.mockito.ArgumentMatchers.anyString;
chaviwb28de1f2018-03-02 10:42:36 -080052import static org.mockito.Mockito.never;
chaviw40234662018-02-07 09:37:16 -080053import static org.mockito.Mockito.reset;
54import static org.mockito.Mockito.verify;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070055
56/**
57 * Tests for the {@link WindowState} class.
58 *
Jorim Jaggi72207752018-01-08 13:16:59 +010059 * atest FrameworksServicesTests:com.android.server.wm.WindowStateTests
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070060 */
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070061@SmallTest
chaviw36d0a342018-03-01 17:29:21 -080062@FlakyTest(bugId = 74078662)
Wale Ogunwale5fc70962016-09-09 22:36:19 -070063@Presubmit
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070064@RunWith(AndroidJUnit4.class)
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080065public class WindowStateTests extends WindowTestsBase {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070066
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070067 @Test
Wale Ogunwale9d147902016-07-16 11:58:55 -070068 public void testIsParentWindowHidden() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080069 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
70 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
71 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
Wale Ogunwale9d147902016-07-16 11:58:55 -070072
chaviwebcbc342018-02-07 13:19:00 -080073 // parentWindow is initially set to hidden.
74 assertTrue(parentWindow.mHidden);
75 assertFalse(parentWindow.isParentWindowHidden());
76 assertTrue(child1.isParentWindowHidden());
77 assertTrue(child2.isParentWindowHidden());
78
79 parentWindow.mHidden = false;
Wale Ogunwale9d147902016-07-16 11:58:55 -070080 assertFalse(parentWindow.isParentWindowHidden());
81 assertFalse(child1.isParentWindowHidden());
82 assertFalse(child2.isParentWindowHidden());
83
Wale Ogunwale9d147902016-07-16 11:58:55 -070084 }
85
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070086 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070087 public void testIsChildWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080088 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
89 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
90 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
91 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070092
93 assertFalse(parentWindow.isChildWindow());
94 assertTrue(child1.isChildWindow());
95 assertTrue(child2.isChildWindow());
96 assertFalse(randomWindow.isChildWindow());
97 }
98
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070099 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700100 public void testHasChild() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800101 final WindowState win1 = createWindow(null, TYPE_APPLICATION, "win1");
102 final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW, "win11");
103 final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW, "win12");
104 final WindowState win2 = createWindow(null, TYPE_APPLICATION, "win2");
105 final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW, "win21");
106 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700107
108 assertTrue(win1.hasChild(win11));
109 assertTrue(win1.hasChild(win12));
110 assertTrue(win2.hasChild(win21));
111
112 assertFalse(win1.hasChild(win21));
113 assertFalse(win1.hasChild(randomWindow));
114
115 assertFalse(win2.hasChild(win11));
116 assertFalse(win2.hasChild(win12));
117 assertFalse(win2.hasChild(randomWindow));
118 }
119
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700120 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700121 public void testGetParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800122 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
123 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
124 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700125
126 assertNull(parentWindow.getParentWindow());
127 assertEquals(parentWindow, child1.getParentWindow());
128 assertEquals(parentWindow, child2.getParentWindow());
129 }
130
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700131 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700132 public void testGetTopParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800133 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
134 final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1");
135 final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2");
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700136
137 assertEquals(root, root.getTopParentWindow());
138 assertEquals(root, child1.getTopParentWindow());
139 assertEquals(child1, child2.getParentWindow());
140 assertEquals(root, child2.getTopParentWindow());
Wale Ogunwaleea92d972016-12-08 07:33:13 -0800141
142 // Test case were child is detached from parent.
143 root.removeChild(child1);
144 assertEquals(child1, child1.getTopParentWindow());
145 assertEquals(child1, child2.getParentWindow());
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700146 }
147
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800148 @Test
149 public void testIsOnScreen_hiddenByPolicy() {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800150 final WindowState window = createWindow(null, TYPE_APPLICATION, "window");
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800151 window.setHasSurface(true);
152 assertTrue(window.isOnScreen());
153 window.hideLw(false /* doAnimation */);
154 assertFalse(window.isOnScreen());
155 }
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800156
157 @Test
158 public void testCanBeImeTarget() throws Exception {
159 final WindowState appWindow = createWindow(null, TYPE_APPLICATION, "appWindow");
160 final WindowState imeWindow = createWindow(null, TYPE_INPUT_METHOD, "imeWindow");
161
162 // Setting FLAG_NOT_FOCUSABLE without FLAG_ALT_FOCUSABLE_IM prevents the window from being
163 // an IME target.
164 appWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
165 imeWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
166
167 // Make windows visible
168 appWindow.setHasSurface(true);
169 imeWindow.setHasSurface(true);
170
171 // Windows without flags (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM) can't be IME targets
172 assertFalse(appWindow.canBeImeTarget());
173 assertFalse(imeWindow.canBeImeTarget());
174
175 // Add IME target flags
176 appWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
177 imeWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
178
179 // Visible app window with flags can be IME target while an IME window can never be an IME
180 // target regardless of its visibility or flags.
181 assertTrue(appWindow.canBeImeTarget());
182 assertFalse(imeWindow.canBeImeTarget());
183
184 // Make windows invisible
185 appWindow.hideLw(false /* doAnimation */);
186 imeWindow.hideLw(false /* doAnimation */);
187
188 // Invisible window can't be IME targets even if they have the right flags.
189 assertFalse(appWindow.canBeImeTarget());
190 assertFalse(imeWindow.canBeImeTarget());
191 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800192
193 @Test
194 public void testGetWindow() throws Exception {
195 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
196 final WindowState mediaChild = createWindow(root, TYPE_APPLICATION_MEDIA, "mediaChild");
197 final WindowState mediaOverlayChild = createWindow(root,
198 TYPE_APPLICATION_MEDIA_OVERLAY, "mediaOverlayChild");
199 final WindowState attachedDialogChild = createWindow(root,
200 TYPE_APPLICATION_ATTACHED_DIALOG, "attachedDialogChild");
201 final WindowState subPanelChild = createWindow(root,
202 TYPE_APPLICATION_SUB_PANEL, "subPanelChild");
203 final WindowState aboveSubPanelChild = createWindow(root,
204 TYPE_APPLICATION_ABOVE_SUB_PANEL, "aboveSubPanelChild");
205
206 final LinkedList<WindowState> windows = new LinkedList();
207
208 root.getWindow(w -> {
209 windows.addLast(w);
210 return false;
211 });
212
213 // getWindow should have returned candidate windows in z-order.
214 assertEquals(aboveSubPanelChild, windows.pollFirst());
215 assertEquals(subPanelChild, windows.pollFirst());
216 assertEquals(attachedDialogChild, windows.pollFirst());
217 assertEquals(root, windows.pollFirst());
218 assertEquals(mediaOverlayChild, windows.pollFirst());
219 assertEquals(mediaChild, windows.pollFirst());
220 assertTrue(windows.isEmpty());
221 }
Bryce Leeae73ba42017-05-05 09:58:25 -0700222
223 @Test
224 public void testPrepareWindowToDisplayDuringRelayout() throws Exception {
225 testPrepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
226 testPrepareWindowToDisplayDuringRelayout(true /*wasVisible*/);
chaviwb28de1f2018-03-02 10:42:36 -0800227
228 // Call prepareWindowToDisplayDuringRelayout for a window without FLAG_TURN_SCREEN_ON
229 // before calling prepareWindowToDisplayDuringRelayout for windows with flag in the same
230 // appWindowToken.
231 final AppWindowToken appWindowToken = createAppWindowToken(mDisplayContent,
232 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD);
233 final WindowState first = createWindow(null, TYPE_APPLICATION, appWindowToken, "first");
234 final WindowState second = createWindow(null, TYPE_APPLICATION, appWindowToken, "second");
235 second.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
236
237 reset(mPowerManagerWrapper);
238 first.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
239 verify(mPowerManagerWrapper, never()).wakeUp(anyLong(), anyString());
240 assertTrue(appWindowToken.canTurnScreenOn());
241
242 reset(mPowerManagerWrapper);
243 second.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
244 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
245 assertFalse(appWindowToken.canTurnScreenOn());
246
247 // Call prepareWindowToDisplayDuringRelayout for two window that have FLAG_TURN_SCREEN_ON
248 // from the same appWindowToken. Only one should trigger the wakeup.
249 appWindowToken.setCanTurnScreenOn(true);
250 first.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
251 second.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
252
253 reset(mPowerManagerWrapper);
254 first.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
255 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
256 assertFalse(appWindowToken.canTurnScreenOn());
257
258 reset(mPowerManagerWrapper);
259 second.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
260 verify(mPowerManagerWrapper, never()).wakeUp(anyLong(), anyString());
261 assertFalse(appWindowToken.canTurnScreenOn());
262
263 // Call prepareWindowToDisplayDuringRelayout for a windows that are not children of an
264 // appWindowToken. Both windows have the FLAG_TURNS_SCREEN_ON so both should call wakeup
chaviw97d28202018-02-27 16:23:53 -0800265 final WindowToken windowToken = WindowTestUtils.createTestWindowToken(FIRST_SUB_WINDOW,
chaviwb28de1f2018-03-02 10:42:36 -0800266 mDisplayContent);
267 final WindowState firstWindow = createWindow(null, TYPE_APPLICATION, windowToken,
268 "firstWindow");
269 final WindowState secondWindow = createWindow(null, TYPE_APPLICATION, windowToken,
270 "secondWindow");
271 firstWindow.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
272 secondWindow.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
273
274 reset(mPowerManagerWrapper);
275 firstWindow.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
276 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
277
278 reset(mPowerManagerWrapper);
279 secondWindow.prepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
280 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
Bryce Leeae73ba42017-05-05 09:58:25 -0700281 }
282
Jorim Jaggi72207752018-01-08 13:16:59 +0100283 @Test
284 public void testCanAffectSystemUiFlags() throws Exception {
285 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
286 app.mToken.setHidden(false);
287 assertTrue(app.canAffectSystemUiFlags());
288 app.mToken.setHidden(true);
289 assertFalse(app.canAffectSystemUiFlags());
290 app.mToken.setHidden(false);
291 app.mAttrs.alpha = 0.0f;
292 assertFalse(app.canAffectSystemUiFlags());
Jorim Jaggi50bf59c2018-03-09 17:29:48 +0100293
294 }
295
296 @Test
297 public void testCanAffectSystemUiFlags_disallow() throws Exception {
298 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
299 app.mToken.setHidden(false);
300 assertTrue(app.canAffectSystemUiFlags());
301 app.getTask().setCanAffectSystemUiFlags(false);
302 assertFalse(app.canAffectSystemUiFlags());
Jorim Jaggi72207752018-01-08 13:16:59 +0100303 }
304
Jorim Jaggi4876b4a2018-01-11 15:43:49 +0100305 @Test
306 public void testIsSelfOrAncestorWindowAnimating() throws Exception {
307 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
308 final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1");
309 final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2");
310 assertFalse(child2.isSelfOrAncestorWindowAnimatingExit());
311 child2.mAnimatingExit = true;
312 assertTrue(child2.isSelfOrAncestorWindowAnimatingExit());
313 child2.mAnimatingExit = false;
314 root.mAnimatingExit = true;
315 assertTrue(child2.isSelfOrAncestorWindowAnimatingExit());
316 }
317
Adrian Roos5251b1d2018-03-23 18:57:43 +0100318 @Test
319 public void testLayoutSeqResetOnReparent() throws Exception {
320 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
321 app.mLayoutSeq = 1;
322 mDisplayContent.mLayoutSeq = 1;
323
324 app.onDisplayChanged(mDisplayContent);
325
326 assertThat(app.mLayoutSeq, not(is(mDisplayContent.mLayoutSeq)));
327 }
328
Brad Stenningaf596412018-04-02 12:03:19 -0700329 @Test
330 public void testDisplayIdUpdatedOnReparent() throws Exception {
331 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
332 // fake a different display
333 app.mInputWindowHandle.displayId = mDisplayContent.getDisplayId() + 1;
334 app.onDisplayChanged(mDisplayContent);
335
336 assertThat(app.mInputWindowHandle.displayId, is(mDisplayContent.getDisplayId()));
337 assertThat(app.getDisplayId(), is(mDisplayContent.getDisplayId()));
338 }
339
Bryce Leeae73ba42017-05-05 09:58:25 -0700340 private void testPrepareWindowToDisplayDuringRelayout(boolean wasVisible) {
chaviw40234662018-02-07 09:37:16 -0800341 reset(mPowerManagerWrapper);
Bryce Leeae73ba42017-05-05 09:58:25 -0700342 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
343 root.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
Bryce Leeae73ba42017-05-05 09:58:25 -0700344
Bryce Leef858b572017-06-29 14:03:33 -0700345 root.prepareWindowToDisplayDuringRelayout(wasVisible /*wasVisible*/);
chaviw40234662018-02-07 09:37:16 -0800346 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
Bryce Leeae73ba42017-05-05 09:58:25 -0700347 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700348}