blob: d80735326568adc74d7b2e89f583061568007319 [file] [log] [blame]
Adrian Roosd4970af2017-11-10 15:48:01 +01001/*
2 * Copyright (C) 2017 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 android.view;
18
19import static android.view.DisplayCutout.NO_CUTOUT;
Adrian Roos24264212018-02-19 16:26:15 +010020import static android.view.DisplayCutout.fromBoundingRect;
Adrian Roos8d13bf12018-02-21 15:17:07 +010021import static android.view.DisplayCutout.fromSpec;
Adrian Roosd4970af2017-11-10 15:48:01 +010022
Adrian Roos8d13bf12018-02-21 15:17:07 +010023import static org.hamcrest.Matchers.not;
24import static org.hamcrest.Matchers.sameInstance;
Adrian Roosd4970af2017-11-10 15:48:01 +010025import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertNotEquals;
Adrian Roos8d13bf12018-02-21 15:17:07 +010028import static org.junit.Assert.assertThat;
Adrian Roosd4970af2017-11-10 15:48:01 +010029import static org.junit.Assert.assertTrue;
30
Adrian Roosd4970af2017-11-10 15:48:01 +010031import android.graphics.Rect;
Adrian Roosd07bafd2017-12-11 17:30:56 +010032import android.graphics.Region;
Adrian Roosd4970af2017-11-10 15:48:01 +010033import android.os.Parcel;
34import android.platform.test.annotations.Presubmit;
35import android.support.test.filters.SmallTest;
36import android.support.test.runner.AndroidJUnit4;
Adrian Roos24264212018-02-19 16:26:15 +010037import android.util.Size;
Adrian Roosd4970af2017-11-10 15:48:01 +010038import android.view.DisplayCutout.ParcelableWrapper;
39
40import org.junit.Test;
41import org.junit.runner.RunWith;
42
Adrian Roosd4970af2017-11-10 15:48:01 +010043@RunWith(AndroidJUnit4.class)
44@SmallTest
45@Presubmit
46public class DisplayCutoutTest {
47
48 /** This is not a consistent cutout. Useful for verifying insets in one go though. */
49 final DisplayCutout mCutoutNumbers = new DisplayCutout(
50 new Rect(1, 2, 3, 4),
Adrian Roos24264212018-02-19 16:26:15 +010051 new Region(5, 6, 7, 8), new Size(9, 10));
Adrian Roosd4970af2017-11-10 15:48:01 +010052
53 final DisplayCutout mCutoutTop = createCutoutTop();
54
55 @Test
56 public void hasCutout() throws Exception {
Adrian Roosd07bafd2017-12-11 17:30:56 +010057 assertTrue(NO_CUTOUT.isEmpty());
58 assertFalse(mCutoutTop.isEmpty());
Adrian Roosd4970af2017-11-10 15:48:01 +010059 }
60
61 @Test
62 public void getSafeInsets() throws Exception {
63 assertEquals(1, mCutoutNumbers.getSafeInsetLeft());
64 assertEquals(2, mCutoutNumbers.getSafeInsetTop());
65 assertEquals(3, mCutoutNumbers.getSafeInsetRight());
66 assertEquals(4, mCutoutNumbers.getSafeInsetBottom());
67
Adrian Roosd07bafd2017-12-11 17:30:56 +010068 assertEquals(new Rect(1, 2, 3, 4), mCutoutNumbers.getSafeInsets());
Adrian Roosd4970af2017-11-10 15:48:01 +010069 }
70
71 @Test
72 public void getBoundingRect() throws Exception {
Adrian Roosd07bafd2017-12-11 17:30:56 +010073 assertEquals(new Rect(50, 0, 75, 100), mCutoutTop.getBoundingRect());
Adrian Roosd4970af2017-11-10 15:48:01 +010074 }
75
76 @Test
77 public void testHashCode() throws Exception {
78 assertEquals(mCutoutTop.hashCode(), createCutoutTop().hashCode());
79 assertNotEquals(mCutoutTop.hashCode(), mCutoutNumbers.hashCode());
80 }
81
82 @Test
83 public void testEquals() throws Exception {
84 assertEquals(mCutoutTop, createCutoutTop());
85 assertNotEquals(mCutoutTop, mCutoutNumbers);
86 }
87
88 @Test
89 public void testToString() throws Exception {
90 assertFalse(mCutoutTop.toString().isEmpty());
91 assertFalse(mCutoutNumbers.toString().isEmpty());
92 }
93
94 @Test
95 public void inset_immutable() throws Exception {
96 DisplayCutout cutout = mCutoutTop.inset(1, 2, 3, 4);
97
98 assertEquals("original instance must not be mutated", createCutoutTop(), mCutoutTop);
99 }
100
101 @Test
102 public void inset_insets_withLeftCutout() throws Exception {
103 DisplayCutout cutout = createCutoutWithInsets(100, 0, 0, 0).inset(1, 2, 3, 4);
104
105 assertEquals(cutout.getSafeInsetLeft(), 99);
106 assertEquals(cutout.getSafeInsetTop(), 0);
107 assertEquals(cutout.getSafeInsetRight(), 0);
108 assertEquals(cutout.getSafeInsetBottom(), 0);
109 }
110
111 @Test
112 public void inset_insets_withTopCutout() throws Exception {
113 DisplayCutout cutout = mCutoutTop.inset(1, 2, 3, 4);
114
115 assertEquals(cutout.getSafeInsetLeft(), 0);
116 assertEquals(cutout.getSafeInsetTop(), 98);
117 assertEquals(cutout.getSafeInsetRight(), 0);
118 assertEquals(cutout.getSafeInsetBottom(), 0);
119 }
120
121 @Test
122 public void inset_insets_withRightCutout() throws Exception {
123 DisplayCutout cutout = createCutoutWithInsets(0, 0, 100, 0).inset(1, 2, 3, 4);
124
125 assertEquals(cutout.getSafeInsetLeft(), 0);
126 assertEquals(cutout.getSafeInsetTop(), 0);
127 assertEquals(cutout.getSafeInsetRight(), 97);
128 assertEquals(cutout.getSafeInsetBottom(), 0);
129 }
130
131 @Test
132 public void inset_insets_withBottomCutout() throws Exception {
133 DisplayCutout cutout = createCutoutWithInsets(0, 0, 0, 100).inset(1, 2, 3, 4);
134
135 assertEquals(cutout.getSafeInsetLeft(), 0);
136 assertEquals(cutout.getSafeInsetTop(), 0);
137 assertEquals(cutout.getSafeInsetRight(), 0);
138 assertEquals(cutout.getSafeInsetBottom(), 96);
139 }
140
141 @Test
142 public void inset_insets_consumeInset() throws Exception {
143 DisplayCutout cutout = mCutoutTop.inset(0, 1000, 0, 0);
144
145 assertEquals(cutout.getSafeInsetLeft(), 0);
146 assertEquals(cutout.getSafeInsetTop(), 0);
147 assertEquals(cutout.getSafeInsetRight(), 0);
148 assertEquals(cutout.getSafeInsetBottom(), 0);
149
Adrian Roosd07bafd2017-12-11 17:30:56 +0100150 assertTrue(cutout.isEmpty());
Adrian Roosd4970af2017-11-10 15:48:01 +0100151 }
152
153 @Test
154 public void inset_bounds() throws Exception {
155 DisplayCutout cutout = mCutoutTop.inset(1, 2, 3, 4);
156
Adrian Roosd07bafd2017-12-11 17:30:56 +0100157 assertEquals(new Rect(49, -2, 74, 98), cutout.getBoundingRect());
Adrian Roosd4970af2017-11-10 15:48:01 +0100158 }
159
160 @Test
Adrian Roos24264212018-02-19 16:26:15 +0100161 public void computeSafeInsets_top() throws Exception {
162 DisplayCutout cutout = fromBoundingRect(0, 0, 100, 20)
163 .computeSafeInsets(200, 400);
Adrian Roosd4970af2017-11-10 15:48:01 +0100164
Adrian Roos24264212018-02-19 16:26:15 +0100165 assertEquals(new Rect(0, 20, 0, 0), cutout.getSafeInsets());
166 }
167
168 @Test
169 public void computeSafeInsets_left() throws Exception {
170 DisplayCutout cutout = fromBoundingRect(0, 0, 20, 100)
171 .computeSafeInsets(400, 200);
172
173 assertEquals(new Rect(20, 0, 0, 0), cutout.getSafeInsets());
174 }
175
176 @Test
177 public void computeSafeInsets_bottom() throws Exception {
178 DisplayCutout cutout = fromBoundingRect(0, 180, 100, 200)
179 .computeSafeInsets(100, 200);
180
181 assertEquals(new Rect(0, 0, 0, 20), cutout.getSafeInsets());
182 }
183
184 @Test
185 public void computeSafeInsets_right() throws Exception {
186 DisplayCutout cutout = fromBoundingRect(180, 0, 200, 100)
187 .computeSafeInsets(200, 100);
188
189 assertEquals(new Rect(0, 0, 20, 0), cutout.getSafeInsets());
190 }
191
192 @Test
193 public void computeSafeInsets_bounds() throws Exception {
194 DisplayCutout cutout = mCutoutTop.computeSafeInsets(1000, 2000);
195
196 assertEquals(mCutoutTop.getBoundingRect(), cutout.getBounds().getBounds());
197 }
198
199 @Test
200 public void calculateRelativeTo_top() throws Exception {
201 DisplayCutout cutout = fromBoundingRect(0, 0, 100, 20)
202 .computeSafeInsets(200, 400)
203 .calculateRelativeTo(new Rect(5, 5, 95, 195));
204
205 assertEquals(new Rect(0, 15, 0, 0), cutout.getSafeInsets());
Adrian Roosd4970af2017-11-10 15:48:01 +0100206 }
207
208 @Test
209 public void calculateRelativeTo_left() throws Exception {
Adrian Roos24264212018-02-19 16:26:15 +0100210 DisplayCutout cutout = fromBoundingRect(0, 0, 20, 100)
211 .computeSafeInsets(400, 200)
212 .calculateRelativeTo(new Rect(5, 5, 195, 95));
Adrian Roosd4970af2017-11-10 15:48:01 +0100213
Adrian Roos24264212018-02-19 16:26:15 +0100214 assertEquals(new Rect(15, 0, 0, 0), cutout.getSafeInsets());
Adrian Roosd4970af2017-11-10 15:48:01 +0100215 }
216
217 @Test
218 public void calculateRelativeTo_bottom() throws Exception {
Adrian Roos24264212018-02-19 16:26:15 +0100219 DisplayCutout cutout = fromBoundingRect(0, 180, 100, 200)
220 .computeSafeInsets(100, 200)
221 .calculateRelativeTo(new Rect(5, 5, 95, 195));
Adrian Roosd4970af2017-11-10 15:48:01 +0100222
Adrian Roos24264212018-02-19 16:26:15 +0100223 assertEquals(new Rect(0, 0, 0, 15), cutout.getSafeInsets());
Adrian Roosd4970af2017-11-10 15:48:01 +0100224 }
225
226 @Test
227 public void calculateRelativeTo_right() throws Exception {
Adrian Roos24264212018-02-19 16:26:15 +0100228 DisplayCutout cutout = fromBoundingRect(180, 0, 200, 100)
229 .computeSafeInsets(200, 100)
230 .calculateRelativeTo(new Rect(5, 5, 195, 95));
Adrian Roosd4970af2017-11-10 15:48:01 +0100231
Adrian Roos24264212018-02-19 16:26:15 +0100232 assertEquals(new Rect(0, 0, 15, 0), cutout.getSafeInsets());
Adrian Roosd4970af2017-11-10 15:48:01 +0100233 }
234
235 @Test
236 public void calculateRelativeTo_bounds() throws Exception {
Adrian Roos24264212018-02-19 16:26:15 +0100237 DisplayCutout cutout = fromBoundingRect(0, 0, 100, 20)
238 .computeSafeInsets(200, 400)
239 .calculateRelativeTo(new Rect(5, 10, 95, 180));
Adrian Roosd4970af2017-11-10 15:48:01 +0100240
Adrian Roos24264212018-02-19 16:26:15 +0100241 assertEquals(new Rect(-5, -10, 95, 10), cutout.getBounds().getBounds());
Adrian Roosd4970af2017-11-10 15:48:01 +0100242 }
243
244 @Test
245 public void fromBoundingPolygon() throws Exception {
246 assertEquals(
Adrian Roosd07bafd2017-12-11 17:30:56 +0100247 new Rect(50, 0, 75, 100),
Adrian Roos24264212018-02-19 16:26:15 +0100248 DisplayCutout.fromBoundingRect(50, 0, 75, 100).getBounds().getBounds());
Adrian Roosd4970af2017-11-10 15:48:01 +0100249 }
250
251 @Test
252 public void parcel_unparcel_regular() {
253 Parcel p = Parcel.obtain();
254
255 new ParcelableWrapper(mCutoutTop).writeToParcel(p, 0);
256 int posAfterWrite = p.dataPosition();
257
258 p.setDataPosition(0);
259
260 assertEquals(mCutoutTop, ParcelableWrapper.CREATOR.createFromParcel(p).get());
261 assertEquals(posAfterWrite, p.dataPosition());
262 }
263
264 @Test
Adrian Roos24264212018-02-19 16:26:15 +0100265 public void parcel_unparcel_withFrame() {
266 Parcel p = Parcel.obtain();
267
268 new ParcelableWrapper(mCutoutNumbers).writeToParcel(p, 0);
269 int posAfterWrite = p.dataPosition();
270
271 p.setDataPosition(0);
272
273 assertEquals(mCutoutNumbers, ParcelableWrapper.CREATOR.createFromParcel(p).get());
274 assertEquals(posAfterWrite, p.dataPosition());
275 }
276
277 @Test
Adrian Roos8d13bf12018-02-21 15:17:07 +0100278 public void fromSpec_caches() {
279 DisplayCutout cached = fromSpec("L1,0 L1,1 L0,1 z", 200, 1f);
280 assertThat(fromSpec("L1,0 L1,1 L0,1 z", 200, 1f), sameInstance(cached));
281 }
282
283 @Test
284 public void fromSpec_wontCacheIfSpecChanges() {
285 DisplayCutout cached = fromSpec("L1,0 L1000,1000 L0,1 z", 200, 1f);
286 assertThat(fromSpec("L1,0 L1,1 L0,1 z", 200, 1f), not(sameInstance(cached)));
287 }
288
289 @Test
290 public void fromSpec_wontCacheIfScreenWidthChanges() {
291 DisplayCutout cached = fromSpec("L1,0 L1,1 L0,1 z", 2000, 1f);
292 assertThat(fromSpec("L1,0 L1,1 L0,1 z", 200, 1f), not(sameInstance(cached)));
293 }
294
295 @Test
296 public void fromSpec_wontCacheIfDensityChanges() {
297 DisplayCutout cached = fromSpec("L1,0 L1,1 L0,1 z", 200, 2f);
298 assertThat(fromSpec("L1,0 L1,1 L0,1 z", 200, 1f), not(sameInstance(cached)));
299 }
300
301 @Test
Adrian Roosd4970af2017-11-10 15:48:01 +0100302 public void parcel_unparcel_nocutout() {
303 Parcel p = Parcel.obtain();
304
305 new ParcelableWrapper(NO_CUTOUT).writeToParcel(p, 0);
306 int posAfterWrite = p.dataPosition();
307
308 p.setDataPosition(0);
309
310 assertEquals(NO_CUTOUT, ParcelableWrapper.CREATOR.createFromParcel(p).get());
311 assertEquals(posAfterWrite, p.dataPosition());
312 }
313
314 @Test
315 public void parcel_unparcel_inplace() {
316 Parcel p = Parcel.obtain();
317
318 new ParcelableWrapper(mCutoutTop).writeToParcel(p, 0);
319 int posAfterWrite = p.dataPosition();
320
321 p.setDataPosition(0);
322
323 ParcelableWrapper wrapper = new ParcelableWrapper();
324 wrapper.readFromParcel(p);
325
326 assertEquals(mCutoutTop, wrapper.get());
327 assertEquals(posAfterWrite, p.dataPosition());
328 }
329
330 @Test
331 public void wrapper_hashcode() throws Exception {
332 assertEquals(new ParcelableWrapper(mCutoutTop).hashCode(),
333 new ParcelableWrapper(createCutoutTop()).hashCode());
334 assertNotEquals(new ParcelableWrapper(mCutoutTop).hashCode(),
335 new ParcelableWrapper(mCutoutNumbers).hashCode());
336 }
337
338 @Test
339 public void wrapper_equals() throws Exception {
340 assertEquals(new ParcelableWrapper(mCutoutTop), new ParcelableWrapper(createCutoutTop()));
341 assertNotEquals(new ParcelableWrapper(mCutoutTop), new ParcelableWrapper(mCutoutNumbers));
342 }
343
344 private static DisplayCutout createCutoutTop() {
Adrian Roosd07bafd2017-12-11 17:30:56 +0100345 return createCutoutWithInsets(0, 100, 0, 0);
Adrian Roosd4970af2017-11-10 15:48:01 +0100346 }
347
348 private static DisplayCutout createCutoutWithInsets(int left, int top, int right, int bottom) {
349 return new DisplayCutout(
350 new Rect(left, top, right, bottom),
Adrian Roos24264212018-02-19 16:26:15 +0100351 new Region(50, 0, 75, 100),
352 null);
Adrian Roosd4970af2017-11-10 15:48:01 +0100353 }
354}