1 /*
2  * Copyright (C) 2018 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.Surface.ROTATION_0;
20 import static android.view.Surface.ROTATION_180;
21 import static android.view.Surface.ROTATION_270;
22 import static android.view.Surface.ROTATION_90;
23 
24 import static org.hamcrest.Matchers.equalTo;
25 
26 import android.graphics.Rect;
27 import android.platform.test.annotations.Presubmit;
28 import android.view.DisplayInfo;
29 
30 import androidx.test.filters.SmallTest;
31 
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.rules.ErrorCollector;
35 import org.junit.runner.RunWith;
36 
37 @SmallTest
38 @Presubmit
39 @RunWith(WindowTestRunner.class)
40 public class DisplayPolicyInsetsTests extends DisplayPolicyTestsBase {
41 
42     @Rule
43     public final ErrorCollector mErrorCollector = new ErrorCollector();
44 
45     @Test
portrait()46     public void portrait() {
47         final DisplayInfo di = displayInfoForRotation(ROTATION_0, false /* withCutout */);
48 
49         verifyStableInsets(di, 0, STATUS_BAR_HEIGHT, 0, NAV_BAR_HEIGHT);
50         verifyNonDecorInsets(di, 0, 0, 0, NAV_BAR_HEIGHT);
51         verifyConsistency(di);
52     }
53 
54     @Test
portrait_withCutout()55     public void portrait_withCutout() {
56         final DisplayInfo di = displayInfoForRotation(ROTATION_0, true /* withCutout */);
57 
58         verifyStableInsets(di, 0, STATUS_BAR_HEIGHT, 0, NAV_BAR_HEIGHT);
59         verifyNonDecorInsets(di, 0, DISPLAY_CUTOUT_HEIGHT, 0, NAV_BAR_HEIGHT);
60         verifyConsistency(di);
61     }
62 
63     @Test
landscape()64     public void landscape() {
65         final DisplayInfo di = displayInfoForRotation(ROTATION_90, false /* withCutout */);
66 
67         verifyStableInsets(di, 0, STATUS_BAR_HEIGHT, NAV_BAR_HEIGHT, 0);
68         verifyNonDecorInsets(di, 0, 0, NAV_BAR_HEIGHT, 0);
69         verifyConsistency(di);
70     }
71 
72     @Test
landscape_withCutout()73     public void landscape_withCutout() {
74         final DisplayInfo di = displayInfoForRotation(ROTATION_90, true /* withCutout */);
75 
76         verifyStableInsets(di, DISPLAY_CUTOUT_HEIGHT, STATUS_BAR_HEIGHT, NAV_BAR_HEIGHT, 0);
77         verifyNonDecorInsets(di, DISPLAY_CUTOUT_HEIGHT, 0, NAV_BAR_HEIGHT, 0);
78         verifyConsistency(di);
79     }
80 
81     @Test
seascape()82     public void seascape() {
83         final DisplayInfo di = displayInfoForRotation(ROTATION_270, false /* withCutout */);
84 
85         verifyStableInsets(di, NAV_BAR_HEIGHT, STATUS_BAR_HEIGHT, 0, 0);
86         verifyNonDecorInsets(di, NAV_BAR_HEIGHT, 0, 0, 0);
87         verifyConsistency(di);
88     }
89 
90     @Test
seascape_withCutout()91     public void seascape_withCutout() {
92         final DisplayInfo di = displayInfoForRotation(ROTATION_270, true /* withCutout */);
93 
94         verifyStableInsets(di, NAV_BAR_HEIGHT, STATUS_BAR_HEIGHT, DISPLAY_CUTOUT_HEIGHT, 0);
95         verifyNonDecorInsets(di, NAV_BAR_HEIGHT, 0, DISPLAY_CUTOUT_HEIGHT, 0);
96         verifyConsistency(di);
97     }
98 
99     @Test
upsideDown()100     public void upsideDown() {
101         final DisplayInfo di = displayInfoForRotation(ROTATION_180, false /* withCutout */);
102 
103         verifyStableInsets(di, 0, STATUS_BAR_HEIGHT, 0, NAV_BAR_HEIGHT);
104         verifyNonDecorInsets(di, 0, 0, 0, NAV_BAR_HEIGHT);
105         verifyConsistency(di);
106     }
107 
108     @Test
upsideDown_withCutout()109     public void upsideDown_withCutout() {
110         final DisplayInfo di = displayInfoForRotation(ROTATION_180, true /* withCutout */);
111 
112         verifyStableInsets(di, 0, STATUS_BAR_HEIGHT, 0, NAV_BAR_HEIGHT + DISPLAY_CUTOUT_HEIGHT);
113         verifyNonDecorInsets(di, 0, 0, 0, NAV_BAR_HEIGHT + DISPLAY_CUTOUT_HEIGHT);
114         verifyConsistency(di);
115     }
116 
verifyStableInsets(DisplayInfo di, int left, int top, int right, int bottom)117     private void verifyStableInsets(DisplayInfo di, int left, int top, int right, int bottom) {
118         mErrorCollector.checkThat("stableInsets", getStableInsetsLw(di), equalTo(new Rect(
119                 left, top, right, bottom)));
120     }
121 
verifyNonDecorInsets(DisplayInfo di, int left, int top, int right, int bottom)122     private void verifyNonDecorInsets(DisplayInfo di, int left, int top, int right, int bottom) {
123         mErrorCollector.checkThat("nonDecorInsets", getNonDecorInsetsLw(di), equalTo(new Rect(
124                 left, top, right, bottom)));
125     }
126 
verifyConsistency(DisplayInfo di)127     private void verifyConsistency(DisplayInfo di) {
128         verifyConsistency("configDisplay", di, getStableInsetsLw(di),
129                 getConfigDisplayWidth(di), getConfigDisplayHeight(di));
130         verifyConsistency("nonDecorDisplay", di, getNonDecorInsetsLw(di),
131                 getNonDecorDisplayWidth(di), getNonDecorDisplayHeight(di));
132     }
133 
verifyConsistency(String what, DisplayInfo di, Rect insets, int width, int height)134     private void verifyConsistency(String what, DisplayInfo di, Rect insets, int width,
135             int height) {
136         mErrorCollector.checkThat(what + ":width", width,
137                 equalTo(di.logicalWidth - insets.left - insets.right));
138         mErrorCollector.checkThat(what + ":height", height,
139                 equalTo(di.logicalHeight - insets.top - insets.bottom));
140     }
141 
getStableInsetsLw(DisplayInfo di)142     private Rect getStableInsetsLw(DisplayInfo di) {
143         Rect result = new Rect();
144         mDisplayPolicy.getStableInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
145                 di.displayCutout, result);
146         return result;
147     }
148 
getNonDecorInsetsLw(DisplayInfo di)149     private Rect getNonDecorInsetsLw(DisplayInfo di) {
150         Rect result = new Rect();
151         mDisplayPolicy.getNonDecorInsetsLw(di.rotation, di.logicalWidth, di.logicalHeight,
152                 di.displayCutout, result);
153         return result;
154     }
155 
getNonDecorDisplayWidth(DisplayInfo di)156     private int getNonDecorDisplayWidth(DisplayInfo di) {
157         return mDisplayPolicy.getNonDecorDisplayWidth(di.logicalWidth, di.logicalHeight,
158                 di.rotation, 0 /* ui */, di.displayCutout);
159     }
160 
getNonDecorDisplayHeight(DisplayInfo di)161     private int getNonDecorDisplayHeight(DisplayInfo di) {
162         return mDisplayPolicy.getNonDecorDisplayHeight(di.logicalWidth, di.logicalHeight,
163                 di.rotation, 0 /* ui */, di.displayCutout);
164     }
165 
getConfigDisplayWidth(DisplayInfo di)166     private int getConfigDisplayWidth(DisplayInfo di) {
167         return mDisplayPolicy.getConfigDisplayWidth(di.logicalWidth, di.logicalHeight,
168                 di.rotation, 0 /* ui */, di.displayCutout);
169     }
170 
getConfigDisplayHeight(DisplayInfo di)171     private int getConfigDisplayHeight(DisplayInfo di) {
172         return mDisplayPolicy.getConfigDisplayHeight(di.logicalWidth, di.logicalHeight,
173                 di.rotation, 0 /* ui */, di.displayCutout);
174     }
175 
displayInfoForRotation(int rotation, boolean withDisplayCutout)176     private static DisplayInfo displayInfoForRotation(int rotation, boolean withDisplayCutout) {
177         return displayInfoAndCutoutForRotation(rotation, withDisplayCutout, false).first;
178     }
179 }
180