blob: 6a4710bb06a4e1c6a97c839e22c6f275cd5ec3b7 [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;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070024import android.support.test.filters.SmallTest;
25import android.support.test.runner.AndroidJUnit4;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070026
Wale Ogunwale34247952017-02-19 11:57:53 -080027import java.util.LinkedList;
28
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070029import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080030import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
31import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070032import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale34247952017-02-19 11:57:53 -080033import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL;
34import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
35import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA;
36import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
37import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080038import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070039import static org.junit.Assert.assertEquals;
40import static org.junit.Assert.assertFalse;
41import static org.junit.Assert.assertNull;
42import static org.junit.Assert.assertTrue;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070043
44/**
45 * Tests for the {@link WindowState} class.
46 *
Jorim Jaggi72207752018-01-08 13:16:59 +010047 * atest FrameworksServicesTests:com.android.server.wm.WindowStateTests
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070048 */
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070049@SmallTest
Wale Ogunwale5fc70962016-09-09 22:36:19 -070050@Presubmit
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070051@RunWith(AndroidJUnit4.class)
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080052public class WindowStateTests extends WindowTestsBase {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070053
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070054 @Test
Wale Ogunwale9d147902016-07-16 11:58:55 -070055 public void testIsParentWindowHidden() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080056 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 Ogunwale9d147902016-07-16 11:58:55 -070059
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 Ogunwalea7e3b642016-08-29 10:15:34 -070071 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070072 public void testIsChildWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080073 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 Ogunwaleadde52e2016-07-16 13:11:55 -070077
78 assertFalse(parentWindow.isChildWindow());
79 assertTrue(child1.isChildWindow());
80 assertTrue(child2.isChildWindow());
81 assertFalse(randomWindow.isChildWindow());
82 }
83
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070084 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070085 public void testHasChild() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080086 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 Ogunwaleadde52e2016-07-16 13:11:55 -070092
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 Ogunwalea7e3b642016-08-29 10:15:34 -0700105 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700106 public void testGetParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800107 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 Ogunwalecaa53af2016-07-17 14:50:26 -0700110
111 assertNull(parentWindow.getParentWindow());
112 assertEquals(parentWindow, child1.getParentWindow());
113 assertEquals(parentWindow, child2.getParentWindow());
114 }
115
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700116 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700117 public void testGetTopParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800118 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 Ogunwalecaa53af2016-07-17 14:50:26 -0700121
122 assertEquals(root, root.getTopParentWindow());
123 assertEquals(root, child1.getTopParentWindow());
124 assertEquals(child1, child2.getParentWindow());
125 assertEquals(root, child2.getTopParentWindow());
Wale Ogunwaleea92d972016-12-08 07:33:13 -0800126
127 // Test case were child is detached from parent.
128 root.removeChild(child1);
129 assertEquals(child1, child1.getTopParentWindow());
130 assertEquals(child1, child2.getParentWindow());
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700131 }
132
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800133 @Test
134 public void testIsOnScreen_hiddenByPolicy() {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800135 final WindowState window = createWindow(null, TYPE_APPLICATION, "window");
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800136 window.setHasSurface(true);
137 assertTrue(window.isOnScreen());
138 window.hideLw(false /* doAnimation */);
139 assertFalse(window.isOnScreen());
140 }
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800141
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 Ogunwale34247952017-02-19 11:57:53 -0800177
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 Leeae73ba42017-05-05 09:58:25 -0700207
208 @Test
209 public void testPrepareWindowToDisplayDuringRelayout() throws Exception {
210 testPrepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
211 testPrepareWindowToDisplayDuringRelayout(true /*wasVisible*/);
212 }
213
Jorim Jaggi72207752018-01-08 13:16:59 +0100214 @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 Jaggi4876b4a2018-01-11 15:43:49 +0100226 @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 Leeae73ba42017-05-05 09:58:25 -0700239 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 Leef858b572017-06-29 14:03:33 -0700244 root.prepareWindowToDisplayDuringRelayout(wasVisible /*wasVisible*/);
Bryce Leeae73ba42017-05-05 09:58:25 -0700245 assertTrue(root.mTurnOnScreen);
246 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700247}