blob: 12bdec6ec1e1aec084c32ef747840008c77b1838 [file] [log] [blame]
Adrian Roos81163582020-01-08 23:21:16 +01001/*
2 * Copyright (C) 2020 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 static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
20import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
21import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
22import static android.view.WindowManager.LayoutParams.TYPE_PRESENTATION;
23import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
24import static android.view.WindowManagerPolicyConstants.APPLICATION_LAYER;
25
26import static com.android.server.wm.DisplayArea.Type.ABOVE_TASKS;
27import static com.android.server.wm.DisplayArea.Type.ANY;
28import static com.android.server.wm.DisplayAreaPolicyBuilder.Feature;
29
30import static org.hamcrest.Matchers.empty;
31import static org.hamcrest.Matchers.equalTo;
32import static org.hamcrest.Matchers.is;
33import static org.hamcrest.Matchers.not;
34import static org.junit.Assert.assertThat;
35import static org.mockito.Mockito.mock;
36import static org.mockito.Mockito.when;
37
38import static java.util.stream.Collectors.toList;
39
40import android.platform.test.annotations.Presubmit;
41import android.view.SurfaceControl;
42
Vishnu Naircf786c02020-02-18 11:18:46 -080043import androidx.test.filters.FlakyTest;
44
Adrian Roos81163582020-01-08 23:21:16 +010045import org.hamcrest.CustomTypeSafeMatcher;
46import org.hamcrest.Description;
47import org.hamcrest.Matcher;
48import org.junit.Rule;
49import org.junit.Test;
50
51import java.util.ArrayList;
52import java.util.HashMap;
53import java.util.HashSet;
54import java.util.List;
55import java.util.Map;
56import java.util.Set;
57import java.util.function.Consumer;
58import java.util.function.Function;
59import java.util.stream.Collectors;
60
61@Presubmit
62public class DisplayAreaPolicyBuilderTest {
63
64 @Rule
65 public final SystemServicesTestRule mSystemServices = new SystemServicesTestRule();
66
67 private TestWindowManagerPolicy mPolicy = new TestWindowManagerPolicy(null, null);
68
69 @Test
Vishnu Naircf786c02020-02-18 11:18:46 -080070 @FlakyTest(bugId = 149760939)
Adrian Roos81163582020-01-08 23:21:16 +010071 public void testBuilder() {
72 WindowManagerService wms = mSystemServices.getWindowManagerService();
73 DisplayArea.Root root = new SurfacelessDisplayAreaRoot(wms);
74 DisplayArea<WindowContainer> ime = new DisplayArea<>(wms, ABOVE_TASKS, "Ime");
75 DisplayArea<ActivityStack> tasks = new DisplayArea<>(wms, ANY, "Tasks");
76
77 final Feature foo;
78 final Feature bar;
79
80 DisplayAreaPolicyBuilder.Result policy = new DisplayAreaPolicyBuilder()
Wale Ogunwaledec34082020-03-22 09:45:00 -070081 .addFeature(foo = new Feature.Builder(mPolicy, "Foo", 0)
Adrian Roos81163582020-01-08 23:21:16 +010082 .upTo(TYPE_STATUS_BAR)
83 .and(TYPE_NAVIGATION_BAR)
84 .build())
Wale Ogunwaledec34082020-03-22 09:45:00 -070085 .addFeature(bar = new Feature.Builder(mPolicy, "Bar", 1)
Adrian Roos81163582020-01-08 23:21:16 +010086 .all()
87 .except(TYPE_STATUS_BAR)
88 .build())
89 .build(wms, mock(DisplayContent.class), root, ime, tasks);
90
91 policy.attachDisplayAreas();
92
93 assertThat(policy.getDisplayAreas(foo), is(not(empty())));
94 assertThat(policy.getDisplayAreas(bar), is(not(empty())));
95
96 assertThat(policy.findAreaForToken(tokenOfType(TYPE_STATUS_BAR)),
97 is(decendantOfOneOf(policy.getDisplayAreas(foo))));
98 assertThat(policy.findAreaForToken(tokenOfType(TYPE_STATUS_BAR)),
99 is(not(decendantOfOneOf(policy.getDisplayAreas(bar)))));
100
101 assertThat(tasks,
102 is(decendantOfOneOf(policy.getDisplayAreas(foo))));
103 assertThat(tasks,
104 is(decendantOfOneOf(policy.getDisplayAreas(bar))));
105
106 assertThat(ime,
107 is(decendantOfOneOf(policy.getDisplayAreas(foo))));
108 assertThat(ime,
109 is(decendantOfOneOf(policy.getDisplayAreas(bar))));
110
111 List<DisplayArea<?>> actualOrder = collectLeafAreas(root);
112 Map<DisplayArea<?>, Set<Integer>> zSets = calculateZSets(policy, root, ime, tasks);
113 actualOrder = actualOrder.stream().filter(zSets::containsKey).collect(toList());
114
115 Map<DisplayArea<?>, Integer> expectedByMinLayer = mapValues(zSets,
116 v -> v.stream().min(Integer::compareTo).get());
117 Map<DisplayArea<?>, Integer> expectedByMaxLayer = mapValues(zSets,
118 v -> v.stream().max(Integer::compareTo).get());
119
120 assertThat(expectedByMinLayer, is(equalTo(expectedByMaxLayer)));
121 assertThat(actualOrder, is(equalTo(expectedByMaxLayer)));
122 }
123
124 private <K, V, R> Map<K, R> mapValues(Map<K, V> zSets, Function<V, R> f) {
125 return zSets.entrySet().stream().collect(Collectors.toMap(
126 Map.Entry::getKey,
127 e -> f.apply(e.getValue())));
128 }
129
130 private List<DisplayArea<?>> collectLeafAreas(DisplayArea<?> root) {
131 ArrayList<DisplayArea<?>> leafs = new ArrayList<>();
132 traverseLeafAreas(root, leafs::add);
133 return leafs;
134 }
135
136 private Map<DisplayArea<?>, Set<Integer>> calculateZSets(
137 DisplayAreaPolicyBuilder.Result policy, DisplayArea.Root root,
138 DisplayArea<WindowContainer> ime,
139 DisplayArea<ActivityStack> tasks) {
140 Map<DisplayArea<?>, Set<Integer>> zSets = new HashMap<>();
141 int[] types = {TYPE_STATUS_BAR, TYPE_NAVIGATION_BAR, TYPE_PRESENTATION,
142 TYPE_APPLICATION_OVERLAY};
143 for (int type : types) {
144 WindowToken token = tokenOfType(type);
145 recordLayer(policy.findAreaForToken(token), token.getWindowLayerFromType(), zSets);
146 }
147 recordLayer(tasks, APPLICATION_LAYER, zSets);
148 recordLayer(ime, mPolicy.getWindowLayerFromTypeLw(TYPE_INPUT_METHOD), zSets);
149 return zSets;
150 }
151
152 private void recordLayer(DisplayArea<?> area, int layer,
153 Map<DisplayArea<?>, Set<Integer>> zSets) {
154 zSets.computeIfAbsent(area, k -> new HashSet<>()).add(layer);
155 }
156
157 private Matcher<WindowContainer> decendantOfOneOf(List<? extends WindowContainer> expected) {
158 return new CustomTypeSafeMatcher<WindowContainer>("descendant of one of " + expected) {
159 @Override
160 protected boolean matchesSafely(WindowContainer actual) {
161 for (WindowContainer expected : expected) {
162 WindowContainer candidate = actual;
163 while (candidate != null && candidate.getParent() != candidate) {
164 if (candidate.getParent() == expected) {
165 return true;
166 }
167 candidate = candidate.getParent();
168 }
169 }
170 return false;
171 }
172
173 @Override
174 protected void describeMismatchSafely(WindowContainer item,
175 Description description) {
176 description.appendText("was ").appendValue(item);
177 while (item != null && item.getParent() != item) {
178 item = item.getParent();
179 description.appendText(", child of ").appendValue(item);
180 }
181 }
182 };
183 }
184
185 private WindowToken tokenOfType(int type) {
186 WindowToken m = mock(WindowToken.class);
187 when(m.getWindowLayerFromType()).thenReturn(mPolicy.getWindowLayerFromTypeLw(type));
188 return m;
189 }
190
191 private static void traverseLeafAreas(DisplayArea<?> root, Consumer<DisplayArea<?>> consumer) {
192 boolean leaf = true;
193 for (int i = 0; i < root.getChildCount(); i++) {
194 WindowContainer child = root.getChildAt(i);
195 if (child instanceof DisplayArea<?>) {
196 traverseLeafAreas((DisplayArea<?>) child, consumer);
197 leaf = false;
198 }
199 }
200 if (leaf) {
201 consumer.accept(root);
202 }
203 }
204
205 private static class SurfacelessDisplayAreaRoot extends DisplayArea.Root {
206
207 SurfacelessDisplayAreaRoot(WindowManagerService wms) {
208 super(wms);
209 }
210
211 @Override
212 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
213 return new MockSurfaceControlBuilder();
214 }
215 }
216
217}