Adrian Roos | 8116358 | 2020-01-08 23:21:16 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
| 19 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; |
| 20 | import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD; |
| 21 | import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR; |
| 22 | import static android.view.WindowManager.LayoutParams.TYPE_PRESENTATION; |
| 23 | import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR; |
| 24 | import static android.view.WindowManagerPolicyConstants.APPLICATION_LAYER; |
| 25 | |
| 26 | import static com.android.server.wm.DisplayArea.Type.ABOVE_TASKS; |
| 27 | import static com.android.server.wm.DisplayArea.Type.ANY; |
| 28 | import static com.android.server.wm.DisplayAreaPolicyBuilder.Feature; |
| 29 | |
| 30 | import static org.hamcrest.Matchers.empty; |
| 31 | import static org.hamcrest.Matchers.equalTo; |
| 32 | import static org.hamcrest.Matchers.is; |
| 33 | import static org.hamcrest.Matchers.not; |
| 34 | import static org.junit.Assert.assertThat; |
| 35 | import static org.mockito.Mockito.mock; |
| 36 | import static org.mockito.Mockito.when; |
| 37 | |
| 38 | import static java.util.stream.Collectors.toList; |
| 39 | |
| 40 | import android.platform.test.annotations.Presubmit; |
| 41 | import android.view.SurfaceControl; |
| 42 | |
Vishnu Nair | cf786c0 | 2020-02-18 11:18:46 -0800 | [diff] [blame] | 43 | import androidx.test.filters.FlakyTest; |
| 44 | |
Adrian Roos | 8116358 | 2020-01-08 23:21:16 +0100 | [diff] [blame] | 45 | import org.hamcrest.CustomTypeSafeMatcher; |
| 46 | import org.hamcrest.Description; |
| 47 | import org.hamcrest.Matcher; |
| 48 | import org.junit.Rule; |
| 49 | import org.junit.Test; |
| 50 | |
| 51 | import java.util.ArrayList; |
| 52 | import java.util.HashMap; |
| 53 | import java.util.HashSet; |
| 54 | import java.util.List; |
| 55 | import java.util.Map; |
| 56 | import java.util.Set; |
| 57 | import java.util.function.Consumer; |
| 58 | import java.util.function.Function; |
| 59 | import java.util.stream.Collectors; |
| 60 | |
| 61 | @Presubmit |
| 62 | public 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 Nair | cf786c0 | 2020-02-18 11:18:46 -0800 | [diff] [blame] | 70 | @FlakyTest(bugId = 149760939) |
Adrian Roos | 8116358 | 2020-01-08 23:21:16 +0100 | [diff] [blame] | 71 | 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 Ogunwale | dec3408 | 2020-03-22 09:45:00 -0700 | [diff] [blame] | 81 | .addFeature(foo = new Feature.Builder(mPolicy, "Foo", 0) |
Adrian Roos | 8116358 | 2020-01-08 23:21:16 +0100 | [diff] [blame] | 82 | .upTo(TYPE_STATUS_BAR) |
| 83 | .and(TYPE_NAVIGATION_BAR) |
| 84 | .build()) |
Wale Ogunwale | dec3408 | 2020-03-22 09:45:00 -0700 | [diff] [blame] | 85 | .addFeature(bar = new Feature.Builder(mPolicy, "Bar", 1) |
Adrian Roos | 8116358 | 2020-01-08 23:21:16 +0100 | [diff] [blame] | 86 | .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 | } |