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