blob: 83868d6f2049980fe45cfffe3614900842f53960 [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
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070030import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080031import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
32import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070033import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale34247952017-02-19 11:57:53 -080034import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL;
35import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
36import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA;
37import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
38import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080039import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
chaviwebcbc342018-02-07 13:19:00 -080040
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070041import static org.junit.Assert.assertEquals;
42import static org.junit.Assert.assertFalse;
43import static org.junit.Assert.assertNull;
44import static org.junit.Assert.assertTrue;
chaviw40234662018-02-07 09:37:16 -080045import static org.mockito.ArgumentMatchers.anyLong;
46import static org.mockito.ArgumentMatchers.anyString;
47import static org.mockito.Mockito.reset;
48import static org.mockito.Mockito.verify;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070049
50/**
51 * Tests for the {@link WindowState} class.
52 *
Jorim Jaggi72207752018-01-08 13:16:59 +010053 * atest FrameworksServicesTests:com.android.server.wm.WindowStateTests
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070054 */
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070055@SmallTest
chaviw36d0a342018-03-01 17:29:21 -080056@FlakyTest(bugId = 74078662)
Wale Ogunwale5fc70962016-09-09 22:36:19 -070057@Presubmit
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070058@RunWith(AndroidJUnit4.class)
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080059public class WindowStateTests extends WindowTestsBase {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070060
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070061 @Test
Wale Ogunwale9d147902016-07-16 11:58:55 -070062 public void testIsParentWindowHidden() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080063 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 Ogunwale9d147902016-07-16 11:58:55 -070066
chaviwebcbc342018-02-07 13:19:00 -080067 // 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 Ogunwale9d147902016-07-16 11:58:55 -070074 assertFalse(parentWindow.isParentWindowHidden());
75 assertFalse(child1.isParentWindowHidden());
76 assertFalse(child2.isParentWindowHidden());
77
Wale Ogunwale9d147902016-07-16 11:58:55 -070078 }
79
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070080 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070081 public void testIsChildWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080082 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 Ogunwaleadde52e2016-07-16 13:11:55 -070086
87 assertFalse(parentWindow.isChildWindow());
88 assertTrue(child1.isChildWindow());
89 assertTrue(child2.isChildWindow());
90 assertFalse(randomWindow.isChildWindow());
91 }
92
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070093 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070094 public void testHasChild() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080095 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 Ogunwaleadde52e2016-07-16 13:11:55 -0700101
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 Ogunwalea7e3b642016-08-29 10:15:34 -0700114 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700115 public void testGetParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800116 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 Ogunwalecaa53af2016-07-17 14:50:26 -0700119
120 assertNull(parentWindow.getParentWindow());
121 assertEquals(parentWindow, child1.getParentWindow());
122 assertEquals(parentWindow, child2.getParentWindow());
123 }
124
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700125 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700126 public void testGetTopParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800127 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 Ogunwalecaa53af2016-07-17 14:50:26 -0700130
131 assertEquals(root, root.getTopParentWindow());
132 assertEquals(root, child1.getTopParentWindow());
133 assertEquals(child1, child2.getParentWindow());
134 assertEquals(root, child2.getTopParentWindow());
Wale Ogunwaleea92d972016-12-08 07:33:13 -0800135
136 // Test case were child is detached from parent.
137 root.removeChild(child1);
138 assertEquals(child1, child1.getTopParentWindow());
139 assertEquals(child1, child2.getParentWindow());
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700140 }
141
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800142 @Test
143 public void testIsOnScreen_hiddenByPolicy() {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800144 final WindowState window = createWindow(null, TYPE_APPLICATION, "window");
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800145 window.setHasSurface(true);
146 assertTrue(window.isOnScreen());
147 window.hideLw(false /* doAnimation */);
148 assertFalse(window.isOnScreen());
149 }
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800150
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 Ogunwale34247952017-02-19 11:57:53 -0800186
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 Leeae73ba42017-05-05 09:58:25 -0700216
217 @Test
218 public void testPrepareWindowToDisplayDuringRelayout() throws Exception {
219 testPrepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
220 testPrepareWindowToDisplayDuringRelayout(true /*wasVisible*/);
221 }
222
Jorim Jaggi72207752018-01-08 13:16:59 +0100223 @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 Jaggi4876b4a2018-01-11 15:43:49 +0100235 @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 Leeae73ba42017-05-05 09:58:25 -0700248 private void testPrepareWindowToDisplayDuringRelayout(boolean wasVisible) {
chaviw40234662018-02-07 09:37:16 -0800249 reset(mPowerManagerWrapper);
Bryce Leeae73ba42017-05-05 09:58:25 -0700250 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
251 root.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
Bryce Leeae73ba42017-05-05 09:58:25 -0700252
Bryce Leef858b572017-06-29 14:03:33 -0700253 root.prepareWindowToDisplayDuringRelayout(wasVisible /*wasVisible*/);
chaviw40234662018-02-07 09:37:16 -0800254 verify(mPowerManagerWrapper).wakeUp(anyLong(), anyString());
Bryce Leeae73ba42017-05-05 09:58:25 -0700255 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700256}