WindowStateTests.java revision a7e3b64507250ab19a1294d58febea03cfa2b327
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.Before;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22
23import android.content.Context;
24import android.support.test.InstrumentationRegistry;
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
27import android.view.IWindow;
28import android.view.WindowManager;
29import android.view.WindowManagerPolicy;
30
31import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
32import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
33import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
34import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
35import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertFalse;
37import static org.junit.Assert.assertNull;
38import static org.junit.Assert.assertTrue;
39
40/**
41 * Tests for the {@link WindowState} class.
42 *
43 * Build: mmma -j32 frameworks/base/services/tests/servicestests
44 * Install: adb install -r out/target/product/$TARGET_PRODUCT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk
45 * Run: adb shell am instrument -w -e class com.android.server.wm.WindowStateTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
46 */
47@SmallTest
48@RunWith(AndroidJUnit4.class)
49public class WindowStateTests {
50
51    private static WindowManagerService sWm = null;
52    private WindowToken mWindowToken;
53    private final WindowManagerPolicy mPolicy = new TestWindowManagerPolicy();
54    private final IWindow mIWindow = new TestIWindow();
55
56    @Before
57    public void setUp() throws Exception {
58        final Context context = InstrumentationRegistry.getTargetContext();
59        if (sWm == null) {
60            // We only want to do this once for the test process as we don't want WM to try to
61            // register a bunch of local services again.
62            sWm = WindowManagerService.main(context, null, true, false, false, mPolicy);
63        }
64        mWindowToken = new WindowToken(sWm, null, 0, false);
65    }
66
67    @Test
68    public void testIsParentWindowHidden() throws Exception {
69        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
70        final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW);
71        final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW);
72
73        assertFalse(parentWindow.mHidden);
74        assertFalse(parentWindow.isParentWindowHidden());
75        assertFalse(child1.isParentWindowHidden());
76        assertFalse(child2.isParentWindowHidden());
77
78        parentWindow.mHidden = true;
79        assertFalse(parentWindow.isParentWindowHidden());
80        assertTrue(child1.isParentWindowHidden());
81        assertTrue(child2.isParentWindowHidden());
82    }
83
84    @Test
85    public void testIsChildWindow() throws Exception {
86        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
87        final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW);
88        final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW);
89        final WindowState randomWindow = createWindow(null, TYPE_APPLICATION);
90
91        assertFalse(parentWindow.isChildWindow());
92        assertTrue(child1.isChildWindow());
93        assertTrue(child2.isChildWindow());
94        assertFalse(randomWindow.isChildWindow());
95    }
96
97    @Test
98    public void testHasChild() throws Exception {
99        final WindowState win1 = createWindow(null, TYPE_APPLICATION);
100        final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW);
101        final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW);
102        final WindowState win2 = createWindow(null, TYPE_APPLICATION);
103        final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW);
104        final WindowState randomWindow = createWindow(null, TYPE_APPLICATION);
105
106        assertTrue(win1.hasChild(win11));
107        assertTrue(win1.hasChild(win12));
108        assertTrue(win2.hasChild(win21));
109
110        assertFalse(win1.hasChild(win21));
111        assertFalse(win1.hasChild(randomWindow));
112
113        assertFalse(win2.hasChild(win11));
114        assertFalse(win2.hasChild(win12));
115        assertFalse(win2.hasChild(randomWindow));
116    }
117
118    @Test
119    public void testGetBottomChild() throws Exception {
120        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
121        assertNull(parentWindow.getBottomChild());
122
123        final WindowState child1 = createWindow(parentWindow, TYPE_APPLICATION_PANEL);
124        assertEquals(child1, parentWindow.getBottomChild());
125
126        final WindowState child2 = createWindow(parentWindow, TYPE_APPLICATION_PANEL);
127        // Since child1 and child2 are at the same layer, then child2 is expect to be added on top
128        // on child1
129        assertEquals(child1, parentWindow.getBottomChild());
130
131        final WindowState child3 = createWindow(parentWindow, TYPE_APPLICATION_MEDIA_OVERLAY);
132        // Since child3 is a negative layer, we would expect it to be added below current children
133        // with positive layers.
134        assertEquals(child3, parentWindow.getBottomChild());
135
136        final WindowState child4 = createWindow(parentWindow, TYPE_APPLICATION_MEDIA_OVERLAY);
137        // We would also expect additional negative layers to be added below existing negative
138        // layers.
139        assertEquals(child4, parentWindow.getBottomChild());
140    }
141
142    @Test
143    public void testGetParentWindow() throws Exception {
144        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
145        final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW);
146        final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW);
147
148        assertNull(parentWindow.getParentWindow());
149        assertEquals(parentWindow, child1.getParentWindow());
150        assertEquals(parentWindow, child2.getParentWindow());
151    }
152
153    @Test
154    public void testGetTopParentWindow() throws Exception {
155        final WindowState root = createWindow(null, TYPE_APPLICATION);
156        final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW);
157        final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW);
158
159        assertEquals(root, root.getTopParentWindow());
160        assertEquals(root, child1.getTopParentWindow());
161        assertEquals(child1, child2.getParentWindow());
162        assertEquals(root, child2.getTopParentWindow());
163    }
164
165    private WindowState createWindow(WindowState parent, int type) {
166        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
167
168        return new WindowState(sWm, null, mIWindow, mWindowToken, parent, 0, 0, attrs, 0,
169                sWm.getDefaultDisplayContentLocked(), 0);
170    }
171}
172