WindowStateTests.java revision 360a8bce7386dc9b231c698af1730e04362b6c2e
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.platform.test.annotations.Presubmit;
26import android.support.test.InstrumentationRegistry;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
29import android.view.IWindow;
30import android.view.WindowManager;
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@Presubmit
50@RunWith(AndroidJUnit4.class)
51public class WindowStateTests {
52
53    private static WindowManagerService sWm = null;
54    private WindowToken mWindowToken;
55    private final IWindow mIWindow = new TestIWindow();
56
57    @Before
58    public void setUp() throws Exception {
59        final Context context = InstrumentationRegistry.getTargetContext();
60        sWm = TestWindowManagerPolicy.getWindowManagerService(context);
61        mWindowToken = new WindowToken(sWm, new Binder(), 0, false,
62                sWm.getDefaultDisplayContentLocked());
63    }
64
65    @Test
66    public void testIsParentWindowHidden() throws Exception {
67        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
68        final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW);
69        final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW);
70
71        assertFalse(parentWindow.mHidden);
72        assertFalse(parentWindow.isParentWindowHidden());
73        assertFalse(child1.isParentWindowHidden());
74        assertFalse(child2.isParentWindowHidden());
75
76        parentWindow.mHidden = true;
77        assertFalse(parentWindow.isParentWindowHidden());
78        assertTrue(child1.isParentWindowHidden());
79        assertTrue(child2.isParentWindowHidden());
80    }
81
82    @Test
83    public void testIsChildWindow() throws Exception {
84        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
85        final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW);
86        final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW);
87        final WindowState randomWindow = createWindow(null, TYPE_APPLICATION);
88
89        assertFalse(parentWindow.isChildWindow());
90        assertTrue(child1.isChildWindow());
91        assertTrue(child2.isChildWindow());
92        assertFalse(randomWindow.isChildWindow());
93    }
94
95    @Test
96    public void testHasChild() throws Exception {
97        final WindowState win1 = createWindow(null, TYPE_APPLICATION);
98        final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW);
99        final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW);
100        final WindowState win2 = createWindow(null, TYPE_APPLICATION);
101        final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW);
102        final WindowState randomWindow = createWindow(null, TYPE_APPLICATION);
103
104        assertTrue(win1.hasChild(win11));
105        assertTrue(win1.hasChild(win12));
106        assertTrue(win2.hasChild(win21));
107
108        assertFalse(win1.hasChild(win21));
109        assertFalse(win1.hasChild(randomWindow));
110
111        assertFalse(win2.hasChild(win11));
112        assertFalse(win2.hasChild(win12));
113        assertFalse(win2.hasChild(randomWindow));
114    }
115
116    @Test
117    public void testGetBottomChild() throws Exception {
118        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
119        assertNull(parentWindow.getBottomChild());
120
121        final WindowState child1 = createWindow(parentWindow, TYPE_APPLICATION_PANEL);
122        assertEquals(child1, parentWindow.getBottomChild());
123
124        final WindowState child2 = createWindow(parentWindow, TYPE_APPLICATION_PANEL);
125        // Since child1 and child2 are at the same layer, then child2 is expect to be added on top
126        // on child1
127        assertEquals(child1, parentWindow.getBottomChild());
128
129        final WindowState child3 = createWindow(parentWindow, TYPE_APPLICATION_MEDIA_OVERLAY);
130        // Since child3 is a negative layer, we would expect it to be added below current children
131        // with positive layers.
132        assertEquals(child3, parentWindow.getBottomChild());
133
134        final WindowState child4 = createWindow(parentWindow, TYPE_APPLICATION_MEDIA_OVERLAY);
135        // We would also expect additional negative layers to be added below existing negative
136        // layers.
137        assertEquals(child4, parentWindow.getBottomChild());
138    }
139
140    @Test
141    public void testGetParentWindow() throws Exception {
142        final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
143        final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW);
144        final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW);
145
146        assertNull(parentWindow.getParentWindow());
147        assertEquals(parentWindow, child1.getParentWindow());
148        assertEquals(parentWindow, child2.getParentWindow());
149    }
150
151    @Test
152    public void testGetTopParentWindow() throws Exception {
153        final WindowState root = createWindow(null, TYPE_APPLICATION);
154        final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW);
155        final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW);
156
157        assertEquals(root, root.getTopParentWindow());
158        assertEquals(root, child1.getTopParentWindow());
159        assertEquals(child1, child2.getParentWindow());
160        assertEquals(root, child2.getTopParentWindow());
161    }
162
163    private WindowState createWindow(WindowState parent, int type) {
164        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
165
166        return new WindowState(sWm, null, mIWindow, mWindowToken, parent, 0, 0, attrs, 0, 0);
167    }
168}
169