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