blob: 4d41718e4da075832d3152ceadccf56b493c4348 [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;
chaviwebcbc342018-02-07 13:19:00 -080039
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070040import static org.junit.Assert.assertEquals;
41import static org.junit.Assert.assertFalse;
42import static org.junit.Assert.assertNull;
43import static org.junit.Assert.assertTrue;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070044
45/**
46 * Tests for the {@link WindowState} class.
47 *
Jorim Jaggi72207752018-01-08 13:16:59 +010048 * atest FrameworksServicesTests:com.android.server.wm.WindowStateTests
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070049 */
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070050@SmallTest
Wale Ogunwale5fc70962016-09-09 22:36:19 -070051@Presubmit
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070052@RunWith(AndroidJUnit4.class)
Wale Ogunwale44fbdf52016-11-16 10:18:45 -080053public class WindowStateTests extends WindowTestsBase {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070054
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070055 @Test
Wale Ogunwale9d147902016-07-16 11:58:55 -070056 public void testIsParentWindowHidden() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080057 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
58 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
59 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
Wale Ogunwale9d147902016-07-16 11:58:55 -070060
chaviwebcbc342018-02-07 13:19:00 -080061 // parentWindow is initially set to hidden.
62 assertTrue(parentWindow.mHidden);
63 assertFalse(parentWindow.isParentWindowHidden());
64 assertTrue(child1.isParentWindowHidden());
65 assertTrue(child2.isParentWindowHidden());
66
67 parentWindow.mHidden = false;
Wale Ogunwale9d147902016-07-16 11:58:55 -070068 assertFalse(parentWindow.isParentWindowHidden());
69 assertFalse(child1.isParentWindowHidden());
70 assertFalse(child2.isParentWindowHidden());
71
Wale Ogunwale9d147902016-07-16 11:58:55 -070072 }
73
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070074 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070075 public void testIsChildWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080076 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
77 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
78 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
79 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070080
81 assertFalse(parentWindow.isChildWindow());
82 assertTrue(child1.isChildWindow());
83 assertTrue(child2.isChildWindow());
84 assertFalse(randomWindow.isChildWindow());
85 }
86
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070087 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070088 public void testHasChild() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -080089 final WindowState win1 = createWindow(null, TYPE_APPLICATION, "win1");
90 final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW, "win11");
91 final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW, "win12");
92 final WindowState win2 = createWindow(null, TYPE_APPLICATION, "win2");
93 final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW, "win21");
94 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070095
96 assertTrue(win1.hasChild(win11));
97 assertTrue(win1.hasChild(win12));
98 assertTrue(win2.hasChild(win21));
99
100 assertFalse(win1.hasChild(win21));
101 assertFalse(win1.hasChild(randomWindow));
102
103 assertFalse(win2.hasChild(win11));
104 assertFalse(win2.hasChild(win12));
105 assertFalse(win2.hasChild(randomWindow));
106 }
107
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700108 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700109 public void testGetParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800110 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
111 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
112 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700113
114 assertNull(parentWindow.getParentWindow());
115 assertEquals(parentWindow, child1.getParentWindow());
116 assertEquals(parentWindow, child2.getParentWindow());
117 }
118
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700119 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700120 public void testGetTopParentWindow() throws Exception {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800121 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
122 final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1");
123 final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2");
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700124
125 assertEquals(root, root.getTopParentWindow());
126 assertEquals(root, child1.getTopParentWindow());
127 assertEquals(child1, child2.getParentWindow());
128 assertEquals(root, child2.getTopParentWindow());
Wale Ogunwaleea92d972016-12-08 07:33:13 -0800129
130 // Test case were child is detached from parent.
131 root.removeChild(child1);
132 assertEquals(child1, child1.getTopParentWindow());
133 assertEquals(child1, child2.getParentWindow());
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700134 }
135
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800136 @Test
137 public void testIsOnScreen_hiddenByPolicy() {
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800138 final WindowState window = createWindow(null, TYPE_APPLICATION, "window");
Jorim Jaggiaf221d12016-11-15 14:59:57 -0800139 window.setHasSurface(true);
140 assertTrue(window.isOnScreen());
141 window.hideLw(false /* doAnimation */);
142 assertFalse(window.isOnScreen());
143 }
Wale Ogunwale805d9ec2016-12-07 12:22:08 -0800144
145 @Test
146 public void testCanBeImeTarget() throws Exception {
147 final WindowState appWindow = createWindow(null, TYPE_APPLICATION, "appWindow");
148 final WindowState imeWindow = createWindow(null, TYPE_INPUT_METHOD, "imeWindow");
149
150 // Setting FLAG_NOT_FOCUSABLE without FLAG_ALT_FOCUSABLE_IM prevents the window from being
151 // an IME target.
152 appWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
153 imeWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
154
155 // Make windows visible
156 appWindow.setHasSurface(true);
157 imeWindow.setHasSurface(true);
158
159 // Windows without flags (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM) can't be IME targets
160 assertFalse(appWindow.canBeImeTarget());
161 assertFalse(imeWindow.canBeImeTarget());
162
163 // Add IME target flags
164 appWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
165 imeWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
166
167 // Visible app window with flags can be IME target while an IME window can never be an IME
168 // target regardless of its visibility or flags.
169 assertTrue(appWindow.canBeImeTarget());
170 assertFalse(imeWindow.canBeImeTarget());
171
172 // Make windows invisible
173 appWindow.hideLw(false /* doAnimation */);
174 imeWindow.hideLw(false /* doAnimation */);
175
176 // Invisible window can't be IME targets even if they have the right flags.
177 assertFalse(appWindow.canBeImeTarget());
178 assertFalse(imeWindow.canBeImeTarget());
179 }
Wale Ogunwale34247952017-02-19 11:57:53 -0800180
181 @Test
182 public void testGetWindow() throws Exception {
183 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
184 final WindowState mediaChild = createWindow(root, TYPE_APPLICATION_MEDIA, "mediaChild");
185 final WindowState mediaOverlayChild = createWindow(root,
186 TYPE_APPLICATION_MEDIA_OVERLAY, "mediaOverlayChild");
187 final WindowState attachedDialogChild = createWindow(root,
188 TYPE_APPLICATION_ATTACHED_DIALOG, "attachedDialogChild");
189 final WindowState subPanelChild = createWindow(root,
190 TYPE_APPLICATION_SUB_PANEL, "subPanelChild");
191 final WindowState aboveSubPanelChild = createWindow(root,
192 TYPE_APPLICATION_ABOVE_SUB_PANEL, "aboveSubPanelChild");
193
194 final LinkedList<WindowState> windows = new LinkedList();
195
196 root.getWindow(w -> {
197 windows.addLast(w);
198 return false;
199 });
200
201 // getWindow should have returned candidate windows in z-order.
202 assertEquals(aboveSubPanelChild, windows.pollFirst());
203 assertEquals(subPanelChild, windows.pollFirst());
204 assertEquals(attachedDialogChild, windows.pollFirst());
205 assertEquals(root, windows.pollFirst());
206 assertEquals(mediaOverlayChild, windows.pollFirst());
207 assertEquals(mediaChild, windows.pollFirst());
208 assertTrue(windows.isEmpty());
209 }
Bryce Leeae73ba42017-05-05 09:58:25 -0700210
211 @Test
212 public void testPrepareWindowToDisplayDuringRelayout() throws Exception {
213 testPrepareWindowToDisplayDuringRelayout(false /*wasVisible*/);
214 testPrepareWindowToDisplayDuringRelayout(true /*wasVisible*/);
215 }
216
Jorim Jaggi72207752018-01-08 13:16:59 +0100217 @Test
218 public void testCanAffectSystemUiFlags() throws Exception {
219 final WindowState app = createWindow(null, TYPE_APPLICATION, "app");
220 app.mToken.setHidden(false);
221 assertTrue(app.canAffectSystemUiFlags());
222 app.mToken.setHidden(true);
223 assertFalse(app.canAffectSystemUiFlags());
224 app.mToken.setHidden(false);
225 app.mAttrs.alpha = 0.0f;
226 assertFalse(app.canAffectSystemUiFlags());
227 }
228
Jorim Jaggi4876b4a2018-01-11 15:43:49 +0100229 @Test
230 public void testIsSelfOrAncestorWindowAnimating() throws Exception {
231 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
232 final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1");
233 final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2");
234 assertFalse(child2.isSelfOrAncestorWindowAnimatingExit());
235 child2.mAnimatingExit = true;
236 assertTrue(child2.isSelfOrAncestorWindowAnimatingExit());
237 child2.mAnimatingExit = false;
238 root.mAnimatingExit = true;
239 assertTrue(child2.isSelfOrAncestorWindowAnimatingExit());
240 }
241
Bryce Leeae73ba42017-05-05 09:58:25 -0700242 private void testPrepareWindowToDisplayDuringRelayout(boolean wasVisible) {
243 final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
244 root.mAttrs.flags |= WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON;
245 root.mTurnOnScreen = false;
246
Bryce Leef858b572017-06-29 14:03:33 -0700247 root.prepareWindowToDisplayDuringRelayout(wasVisible /*wasVisible*/);
Bryce Leeae73ba42017-05-05 09:58:25 -0700248 assertTrue(root.mTurnOnScreen);
249 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700250}