1 /*
2  * Copyright (C) 2021 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 android.server.wm;
18 
19 import static android.window.DisplayAreaOrganizer.FEATURE_DEFAULT_TASK_CONTAINER;
20 import static android.window.DisplayAreaOrganizer.FEATURE_ROOT;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import android.platform.test.annotations.Presubmit;
25 import android.server.wm.WindowManagerState.DisplayArea;
26 import android.server.wm.WindowManagerState.DisplayContent;
27 import android.view.Display;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 
32 import java.util.ArrayList;
33 import java.util.HashSet;
34 import java.util.List;
35 import java.util.Set;
36 
37 /**
38  * Build/Install/Run:
39  *     atest CtsWindowManagerDeviceTestCases:DisplayAreaPolicyTests
40  */
41 @Presubmit
42 public class DisplayAreaPolicyTests extends ActivityManagerTestBase {
43 
44     private List<DisplayContent> mDisplays;
45 
46     @Before
47     @Override
setUp()48     public void setUp() throws Exception {
49         super.setUp();
50 
51         // Verify on all displays.
52         mWmState.computeState();
53         mDisplays = mWmState.getDisplays();
54     }
55 
isTrustedDisplay(DisplayContent displayContent)56     private boolean isTrustedDisplay(DisplayContent displayContent) {
57         return (displayContent.getFlags() & Display.FLAG_TRUSTED) != 0;
58     }
59 
60     /**
61      * DisplayContent should have feature id of FEATURE_ROOT. It should be organized.
62      */
63     @Test
testDisplayContent()64     public void testDisplayContent() {
65         for (DisplayContent displayContent : mDisplays) {
66             assertThat(displayContent.getFeatureId()).isEqualTo(FEATURE_ROOT);
67             // DisplayAreaOrganizerController registers the organizer for the trusted displays only.
68             if (isTrustedDisplay(displayContent)) {
69                 assertThat(displayContent.isOrganized()).isTrue();
70             }
71         }
72     }
73 
74     /**
75      * There must be exactly one default TaskDisplayArea per display.
76      */
77     @Test
testDefaultTaskDisplayArea()78     public void testDefaultTaskDisplayArea() {
79         for (DisplayContent displayContent : mDisplays) {
80             final List<DisplayArea> taskDisplayAreas = displayContent.getAllTaskDisplayAreas();
81             final List<DisplayArea> defaultTda = new ArrayList<>();
82             for (DisplayArea taskDisplayArea : taskDisplayAreas) {
83                 if (taskDisplayArea.getFeatureId() == FEATURE_DEFAULT_TASK_CONTAINER) {
84                     defaultTda.add(taskDisplayArea);
85                 }
86             }
87             assertThat(defaultTda).hasSize(1);
88         }
89     }
90 
91     /**
92      * TaskDisplayArea and RootDisplayArea must have unique feature ids per display.
93      */
94     @Test
testDisplayAreaUniqueId()95     public void testDisplayAreaUniqueId() {
96         for (DisplayContent displayContent : mDisplays) {
97             // TaskDisplayArea and RootDisplayArea must have unique feature ids.
98             final Set<Integer> uniqueIds = new HashSet<>();
99             uniqueIds.add(displayContent.getFeatureId());
100             // Other DisplayAreas can have non-unique feature ids, but shouldn't be the same with
101             // any TaskDisplayArea or RootDisplayArea.
102             final Set<Integer> nonUniqueIds = new HashSet<>();
103             final List<DisplayArea> displayAreas = displayContent.getAllChildDisplayAreas();
104 
105             for (DisplayArea displayArea : displayAreas) {
106                 final int featureId = displayArea.getFeatureId();
107                 if (displayArea.isRootDisplayArea() || displayArea.isTaskDisplayArea()) {
108                     assertThat(uniqueIds.add(featureId)).isTrue();
109                     assertThat(nonUniqueIds).doesNotContain(featureId);
110                 } else {
111                     nonUniqueIds.add(featureId);
112                     assertThat(uniqueIds).doesNotContain(featureId);
113                 }
114             }
115         }
116     }
117 }
118