WindowStateTests.java revision ea92d977333bef11c6ab243d785a1b2d9c1f66ff
1/*
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
19import org.junit.Test;
20import org.junit.runner.RunWith;
21
22import android.platform.test.annotations.Presubmit;
23import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25
26import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
27import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
28import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
29import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
30import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertFalse;
33import static org.junit.Assert.assertNull;
34import static org.junit.Assert.assertTrue;
35
36/**
37 * Tests for the {@link WindowState} class.
38 *
39 * Build/Install/Run:
40 *  bit FrameworksServicesTests:com.android.server.wm.WindowStateTests
41 */
42@SmallTest
43@Presubmit
44@RunWith(AndroidJUnit4.class)
45public class WindowStateTests extends WindowTestsBase {
46
47    @Test
48    public void testIsParentWindowHidden() throws Exception {
49        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
50        final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
51        final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
52
53        assertFalse(parentWindow.mHidden);
54        assertFalse(parentWindow.isParentWindowHidden());
55        assertFalse(child1.isParentWindowHidden());
56        assertFalse(child2.isParentWindowHidden());
57
58        parentWindow.mHidden = true;
59        assertFalse(parentWindow.isParentWindowHidden());
60        assertTrue(child1.isParentWindowHidden());
61        assertTrue(child2.isParentWindowHidden());
62    }
63
64    @Test
65    public void testIsChildWindow() throws Exception {
66        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
67        final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
68        final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
69        final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
70
71        assertFalse(parentWindow.isChildWindow());
72        assertTrue(child1.isChildWindow());
73        assertTrue(child2.isChildWindow());
74        assertFalse(randomWindow.isChildWindow());
75    }
76
77    @Test
78    public void testHasChild() throws Exception {
79        final WindowState win1 = createWindow(null, TYPE_APPLICATION, "win1");
80        final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW, "win11");
81        final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW, "win12");
82        final WindowState win2 = createWindow(null, TYPE_APPLICATION, "win2");
83        final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW, "win21");
84        final WindowState randomWindow = createWindow(null, TYPE_APPLICATION, "randomWindow");
85
86        assertTrue(win1.hasChild(win11));
87        assertTrue(win1.hasChild(win12));
88        assertTrue(win2.hasChild(win21));
89
90        assertFalse(win1.hasChild(win21));
91        assertFalse(win1.hasChild(randomWindow));
92
93        assertFalse(win2.hasChild(win11));
94        assertFalse(win2.hasChild(win12));
95        assertFalse(win2.hasChild(randomWindow));
96    }
97
98    @Test
99    public void testGetParentWindow() throws Exception {
100        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION, "parentWindow");
101        final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child1");
102        final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW, "child2");
103
104        assertNull(parentWindow.getParentWindow());
105        assertEquals(parentWindow, child1.getParentWindow());
106        assertEquals(parentWindow, child2.getParentWindow());
107    }
108
109    @Test
110    public void testGetTopParentWindow() throws Exception {
111        final WindowState root = createWindow(null, TYPE_APPLICATION, "root");
112        final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW, "child1");
113        final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW, "child2");
114
115        assertEquals(root, root.getTopParentWindow());
116        assertEquals(root, child1.getTopParentWindow());
117        assertEquals(child1, child2.getParentWindow());
118        assertEquals(root, child2.getTopParentWindow());
119
120        // Test case were child is detached from parent.
121        root.removeChild(child1);
122        assertEquals(child1, child1.getTopParentWindow());
123        assertEquals(child1, child2.getParentWindow());
124    }
125
126    @Test
127    public void testIsOnScreen_hiddenByPolicy() {
128        final WindowState window = createWindow(null, TYPE_APPLICATION, "window");
129        window.setHasSurface(true);
130        assertTrue(window.isOnScreen());
131        window.hideLw(false /* doAnimation */);
132        assertFalse(window.isOnScreen());
133    }
134
135    @Test
136    public void testCanBeImeTarget() throws Exception {
137        final WindowState appWindow = createWindow(null, TYPE_APPLICATION, "appWindow");
138        final WindowState imeWindow = createWindow(null, TYPE_INPUT_METHOD, "imeWindow");
139
140        // Setting FLAG_NOT_FOCUSABLE without FLAG_ALT_FOCUSABLE_IM prevents the window from being
141        // an IME target.
142        appWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
143        imeWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
144
145        // Make windows visible
146        appWindow.setHasSurface(true);
147        imeWindow.setHasSurface(true);
148
149        // Windows without flags (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM) can't be IME targets
150        assertFalse(appWindow.canBeImeTarget());
151        assertFalse(imeWindow.canBeImeTarget());
152
153        // Add IME target flags
154        appWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
155        imeWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
156
157        // Visible app window with flags can be IME target while an IME window can never be an IME
158        // target regardless of its visibility or flags.
159        assertTrue(appWindow.canBeImeTarget());
160        assertFalse(imeWindow.canBeImeTarget());
161
162        // Make windows invisible
163        appWindow.hideLw(false /* doAnimation */);
164        imeWindow.hideLw(false /* doAnimation */);
165
166        // Invisible window can't be IME targets even if they have the right flags.
167        assertFalse(appWindow.canBeImeTarget());
168        assertFalse(imeWindow.canBeImeTarget());
169    }
170}
171