blob: 1259e0f7f9ca1a02d5c87b12a1c26ab2f78deca8 [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 Ogunwale51362492016-09-08 17:49:17 -070024import android.os.Binder;
Wale Ogunwale5fc70962016-09-09 22:36:19 -070025import android.platform.test.annotations.Presubmit;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070026import android.support.test.InstrumentationRegistry;
27import android.support.test.filters.SmallTest;
28import android.support.test.runner.AndroidJUnit4;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070029import android.view.IWindow;
30import android.view.WindowManager;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070031
32import static android.view.WindowManager.LayoutParams.FIRST_SUB_WINDOW;
33import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070034import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
35import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070036import static org.junit.Assert.assertEquals;
37import static org.junit.Assert.assertFalse;
38import static org.junit.Assert.assertNull;
39import static org.junit.Assert.assertTrue;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070040
41/**
42 * Tests for the {@link WindowState} class.
43 *
44 * Build: mmma -j32 frameworks/base/services/tests/servicestests
Wale Ogunwaled1c37912016-08-16 03:19:39 -070045 * Install: adb install -r out/target/product/$TARGET_PRODUCT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070046 * Run: adb shell am instrument -w -e class com.android.server.wm.WindowStateTests com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
47 */
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070048@SmallTest
Wale Ogunwale5fc70962016-09-09 22:36:19 -070049@Presubmit
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070050@RunWith(AndroidJUnit4.class)
51public class WindowStateTests {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070052
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070053 private static WindowManagerService sWm = null;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070054 private WindowToken mWindowToken;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070055 private final IWindow mIWindow = new TestIWindow();
56
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070057 @Before
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070058 public void setUp() throws Exception {
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070059 final Context context = InstrumentationRegistry.getTargetContext();
Wale Ogunwale51362492016-09-08 17:49:17 -070060 sWm = TestWindowManagerPolicy.getWindowManagerService(context);
61 mWindowToken = new WindowToken(sWm, new Binder(), 0, false);
Wale Ogunwaleb699ce02016-07-18 12:05:30 -070062 }
63
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070064 @Test
Wale Ogunwale9d147902016-07-16 11:58:55 -070065 public void testIsParentWindowHidden() throws Exception {
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070066 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
67 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW);
68 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW);
Wale Ogunwale9d147902016-07-16 11:58:55 -070069
70 assertFalse(parentWindow.mHidden);
71 assertFalse(parentWindow.isParentWindowHidden());
72 assertFalse(child1.isParentWindowHidden());
73 assertFalse(child2.isParentWindowHidden());
74
75 parentWindow.mHidden = true;
76 assertFalse(parentWindow.isParentWindowHidden());
77 assertTrue(child1.isParentWindowHidden());
78 assertTrue(child2.isParentWindowHidden());
79 }
80
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070081 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070082 public void testIsChildWindow() throws Exception {
83 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
84 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW);
85 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW);
86 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION);
87
88 assertFalse(parentWindow.isChildWindow());
89 assertTrue(child1.isChildWindow());
90 assertTrue(child2.isChildWindow());
91 assertFalse(randomWindow.isChildWindow());
92 }
93
Wale Ogunwalea7e3b642016-08-29 10:15:34 -070094 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -070095 public void testHasChild() throws Exception {
96 final WindowState win1 = createWindow(null, TYPE_APPLICATION);
97 final WindowState win11 = createWindow(win1, FIRST_SUB_WINDOW);
98 final WindowState win12 = createWindow(win1, FIRST_SUB_WINDOW);
99 final WindowState win2 = createWindow(null, TYPE_APPLICATION);
100 final WindowState win21 = createWindow(win2, FIRST_SUB_WINDOW);
101 final WindowState randomWindow = createWindow(null, TYPE_APPLICATION);
102
103 assertTrue(win1.hasChild(win11));
104 assertTrue(win1.hasChild(win12));
105 assertTrue(win2.hasChild(win21));
106
107 assertFalse(win1.hasChild(win21));
108 assertFalse(win1.hasChild(randomWindow));
109
110 assertFalse(win2.hasChild(win11));
111 assertFalse(win2.hasChild(win12));
112 assertFalse(win2.hasChild(randomWindow));
113 }
114
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700115 @Test
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700116 public void testGetBottomChild() throws Exception {
117 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
118 assertNull(parentWindow.getBottomChild());
119
120 final WindowState child1 = createWindow(parentWindow, TYPE_APPLICATION_PANEL);
121 assertEquals(child1, parentWindow.getBottomChild());
122
123 final WindowState child2 = createWindow(parentWindow, TYPE_APPLICATION_PANEL);
124 // Since child1 and child2 are at the same layer, then child2 is expect to be added on top
125 // on child1
126 assertEquals(child1, parentWindow.getBottomChild());
127
128 final WindowState child3 = createWindow(parentWindow, TYPE_APPLICATION_MEDIA_OVERLAY);
129 // Since child3 is a negative layer, we would expect it to be added below current children
130 // with positive layers.
131 assertEquals(child3, parentWindow.getBottomChild());
132
133 final WindowState child4 = createWindow(parentWindow, TYPE_APPLICATION_MEDIA_OVERLAY);
134 // We would also expect additional negative layers to be added below existing negative
135 // layers.
136 assertEquals(child4, parentWindow.getBottomChild());
137 }
138
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700139 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700140 public void testGetParentWindow() throws Exception {
141 final WindowState parentWindow = createWindow(null, TYPE_APPLICATION);
142 final WindowState child1 = createWindow(parentWindow, FIRST_SUB_WINDOW);
143 final WindowState child2 = createWindow(parentWindow, FIRST_SUB_WINDOW);
144
145 assertNull(parentWindow.getParentWindow());
146 assertEquals(parentWindow, child1.getParentWindow());
147 assertEquals(parentWindow, child2.getParentWindow());
148 }
149
Wale Ogunwalea7e3b642016-08-29 10:15:34 -0700150 @Test
Wale Ogunwalecaa53af2016-07-17 14:50:26 -0700151 public void testGetTopParentWindow() throws Exception {
152 final WindowState root = createWindow(null, TYPE_APPLICATION);
153 final WindowState child1 = createWindow(root, FIRST_SUB_WINDOW);
154 final WindowState child2 = createWindow(child1, FIRST_SUB_WINDOW);
155
156 assertEquals(root, root.getTopParentWindow());
157 assertEquals(root, child1.getTopParentWindow());
158 assertEquals(child1, child2.getParentWindow());
159 assertEquals(root, child2.getTopParentWindow());
160 }
161
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700162 private WindowState createWindow(WindowState parent, int type) {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700163 final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(type);
164
Wale Ogunwaleadde52e2016-07-16 13:11:55 -0700165 return new WindowState(sWm, null, mIWindow, mWindowToken, parent, 0, 0, attrs, 0,
166 sWm.getDefaultDisplayContentLocked(), 0);
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700167 }
168}