WindowStateTests.java revision 805d9ecc476134ffafc85a07b05e94a14b1d398c
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
121    @Test
122    public void testIsOnScreen_hiddenByPolicy() {
123        final WindowState window = createWindow(null, TYPE_APPLICATION, "window");
124        window.setHasSurface(true);
125        assertTrue(window.isOnScreen());
126        window.hideLw(false /* doAnimation */);
127        assertFalse(window.isOnScreen());
128    }
129
130    @Test
131    public void testCanBeImeTarget() throws Exception {
132        final WindowState appWindow = createWindow(null, TYPE_APPLICATION, "appWindow");
133        final WindowState imeWindow = createWindow(null, TYPE_INPUT_METHOD, "imeWindow");
134
135        // Setting FLAG_NOT_FOCUSABLE without FLAG_ALT_FOCUSABLE_IM prevents the window from being
136        // an IME target.
137        appWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
138        imeWindow.mAttrs.flags |= FLAG_NOT_FOCUSABLE;
139
140        // Make windows visible
141        appWindow.setHasSurface(true);
142        imeWindow.setHasSurface(true);
143
144        // Windows without flags (FLAG_NOT_FOCUSABLE|FLAG_ALT_FOCUSABLE_IM) can't be IME targets
145        assertFalse(appWindow.canBeImeTarget());
146        assertFalse(imeWindow.canBeImeTarget());
147
148        // Add IME target flags
149        appWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
150        imeWindow.mAttrs.flags |= (FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM);
151
152        // Visible app window with flags can be IME target while an IME window can never be an IME
153        // target regardless of its visibility or flags.
154        assertTrue(appWindow.canBeImeTarget());
155        assertFalse(imeWindow.canBeImeTarget());
156
157        // Make windows invisible
158        appWindow.hideLw(false /* doAnimation */);
159        imeWindow.hideLw(false /* doAnimation */);
160
161        // Invisible window can't be IME targets even if they have the right flags.
162        assertFalse(appWindow.canBeImeTarget());
163        assertFalse(imeWindow.canBeImeTarget());
164    }
165}
166