blob: 4b29a60680ddf63e2c9f51c2671118a23e97a808 [file] [log] [blame]
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001/*
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
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070019import org.junit.Before;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070023import android.content.Context;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070024import android.support.test.InstrumentationRegistry;
25import android.support.test.filters.SmallTest;
26import android.support.test.runner.AndroidJUnit4;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070027import 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;
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070033import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
34import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070035import static org.junit.Assert.assertEquals;
36import static org.junit.Assert.assertFalse;
37import static org.junit.Assert.assertNull;
38import static org.junit.Assert.assertTrue;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070039
40/**
41 * Tests for the {@link WindowState} class.
42 *
43 * Build: mmma -j32 frameworks/base/services/tests/servicestests
Wale Ogunwaled1c37912016-08-16 03:19:39 -070044 * Install: adb install -r out/target/product/$TARGET_PRODUCT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070045 * Run: adb shell am instrument -w -e class com.android.server.wm.WindowStateTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
46 */
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070047@SmallTest
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070048@RunWith(AndroidJUnit4.class)
49public class WindowStateTests {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070050
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070051 private static WindowManagerService sWm = null;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070052 private WindowToken mWindowToken;
53 private final WindowManagerPolicy mPolicy = new TestWindowManagerPolicy();
54 private final IWindow mIWindow = new TestIWindow();
55
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070056 @Before
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070057 public void setUp() throws Exception {
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070058 final Context context = InstrumentationRegistry.getTargetContext();
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070059 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.
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070062 sWm = WindowManagerService.main(context, null, true, false, false, mPolicy);
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070063 }
64 mWindowToken = new WindowToken(sWm, null, 0, false);
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070065 }
66
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070067 @Test
Wale Ogunwale9d147902016-07-16 11:58:55 -070068 public void testIsParentWindowHidden() throws Exception {
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070069 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);
Wale Ogunwale9d147902016-07-16 11:58:55 -070072
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
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070084 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070085 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
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070097 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070098 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
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700118 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700119 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
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700142 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700143 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
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700153 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700154 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
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700165 private WindowState createWindow(WindowState parent, int type) {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700166 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
167
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700168 return new WindowState(sWm, null, mIWindow, mWindowToken, parent, 0, 0, attrs, 0,
169 sWm.getDefaultDisplayContentLocked(), 0);
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700170 }
171}