• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.server.wm;
18 
19 import static android.view.WindowInsets.Type.captionBar;
20 import static android.view.WindowInsets.Type.displayCutout;
21 import static android.view.WindowInsets.Type.ime;
22 import static android.view.WindowInsets.Type.mandatorySystemGestures;
23 import static android.view.WindowInsets.Type.navigationBars;
24 import static android.view.WindowInsets.Type.statusBars;
25 import static android.view.WindowInsets.Type.systemBars;
26 import static android.view.WindowInsets.Type.systemGestures;
27 import static android.view.WindowInsets.Type.systemOverlays;
28 import static android.view.WindowInsets.Type.tappableElement;
29 
30 import static org.junit.Assert.assertEquals;
31 import static org.junit.Assert.assertFalse;
32 import static org.junit.Assert.assertNotEquals;
33 import static org.junit.Assert.assertNotNull;
34 import static org.junit.Assert.assertNotSame;
35 import static org.junit.Assert.assertNull;
36 import static org.junit.Assert.assertSame;
37 import static org.junit.Assert.assertTrue;
38 
39 import android.graphics.Insets;
40 import android.graphics.Rect;
41 import android.platform.test.annotations.Presubmit;
42 import android.view.DisplayCutout;
43 import android.view.DisplayShape;
44 import android.view.RoundedCorner;
45 import android.view.WindowInsets;
46 import android.view.WindowInsets.Type;
47 
48 import androidx.test.filters.SmallTest;
49 import androidx.test.runner.AndroidJUnit4;
50 
51 import com.android.compatibility.common.util.ApiTest;
52 
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 
56 import java.util.Collections;
57 
58 /**
59  * Test {@link WindowInsets}.
60  */
61 @SmallTest
62 @RunWith(AndroidJUnit4.class)
63 @Presubmit
64 @android.server.wm.annotation.Group2
65 public class WindowInsetsTest {
66 
67     private static final DisplayCutout CUTOUT = new DisplayCutout(new Rect(0, 10, 0, 0),
68             Collections.singletonList(new Rect(5, 0, 15, 10)));
69     private static final DisplayCutout CUTOUT2 = new DisplayCutout(new Rect(0, 15, 0, 0),
70             Collections.singletonList(new Rect(5, 0, 15, 15)));
71     private static final RoundedCorner ROUNDED_CORNER_TOP_LEFT =
72             new RoundedCorner(RoundedCorner.POSITION_TOP_LEFT, 10, 10, 10);
73     private static final RoundedCorner ROUNDED_CORNER_TOP_RIGHT =
74             new RoundedCorner(RoundedCorner.POSITION_TOP_RIGHT, 10, 90, 10);
75     private static final RoundedCorner ROUNDED_CORNER_BOTTOM_RIGHT =
76             new RoundedCorner(RoundedCorner.POSITION_BOTTOM_RIGHT, 10, 90, 190);
77     private static final RoundedCorner ROUNDED_CORNER_BOTTOM_LEFT =
78             new RoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT, 10, 10, 190);
79     private static final int INSET_LEFT = 1;
80     private static final int INSET_TOP = 2;
81     private static final int INSET_RIGHT = 3;
82     private static final int INSET_BOTTOM = 4;
83     private static final DisplayShape DISPLAY_SHAPE =
84             DisplayShape.fromSpecString("M0,0 h100 v200 h-100 z", 1f, 100, 200);
85 
86     @Test
testBuilder()87     public void testBuilder() {
88         final WindowInsets insets = new WindowInsets.Builder()
89                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
90                 .setStableInsets(Insets.of(5, 6, 7, 8))
91                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
92                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
93                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
94                 .setDisplayCutout(CUTOUT)
95                 .setRoundedCorner(RoundedCorner.POSITION_TOP_LEFT, ROUNDED_CORNER_TOP_LEFT)
96                 .setRoundedCorner(RoundedCorner.POSITION_TOP_RIGHT, ROUNDED_CORNER_TOP_RIGHT)
97                 .setRoundedCorner(RoundedCorner.POSITION_BOTTOM_RIGHT, ROUNDED_CORNER_BOTTOM_RIGHT)
98                 .setRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT, ROUNDED_CORNER_BOTTOM_LEFT)
99                 .setDisplayShape(DISPLAY_SHAPE)
100                 .build();
101 
102         assertEquals(Insets.of(1, 2, 3, 4), insets.getSystemWindowInsets());
103         assertEquals(Insets.of(5, 6, 7, 8), insets.getStableInsets());
104         assertEquals(Insets.of(9, 10, 11, 12), insets.getSystemGestureInsets());
105         assertEquals(Insets.of(13, 14, 15, 16), insets.getMandatorySystemGestureInsets());
106         assertEquals(Insets.of(17, 18, 19, 20), insets.getTappableElementInsets());
107         assertSame(CUTOUT, insets.getDisplayCutout());
108         assertEquals(getCutoutSafeInsets(insets), insets.getInsets(Type.displayCutout()));
109         assertEquals(ROUNDED_CORNER_TOP_LEFT,
110                 insets.getRoundedCorner(RoundedCorner.POSITION_TOP_LEFT));
111         assertEquals(ROUNDED_CORNER_TOP_RIGHT,
112                 insets.getRoundedCorner(RoundedCorner.POSITION_TOP_RIGHT));
113         assertEquals(ROUNDED_CORNER_BOTTOM_RIGHT,
114                 insets.getRoundedCorner(RoundedCorner.POSITION_BOTTOM_RIGHT));
115         assertEquals(ROUNDED_CORNER_BOTTOM_LEFT,
116                 insets.getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT));
117         assertEquals(DISPLAY_SHAPE, insets.getDisplayShape());
118     }
119 
120     @Test
testBuilder_copy()121     public void testBuilder_copy() {
122         final WindowInsets insets = new WindowInsets.Builder()
123                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
124                 .setStableInsets(Insets.of(5, 6, 7, 8))
125                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
126                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
127                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
128                 .setDisplayCutout(CUTOUT)
129                 .setRoundedCorner(RoundedCorner.POSITION_TOP_LEFT, ROUNDED_CORNER_TOP_LEFT)
130                 .setRoundedCorner(RoundedCorner.POSITION_TOP_RIGHT, ROUNDED_CORNER_TOP_RIGHT)
131                 .setRoundedCorner(RoundedCorner.POSITION_BOTTOM_RIGHT, ROUNDED_CORNER_BOTTOM_RIGHT)
132                 .setRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT, ROUNDED_CORNER_BOTTOM_LEFT)
133                 .setDisplayShape(DISPLAY_SHAPE)
134                 .build();
135         final WindowInsets copy = new WindowInsets.Builder(insets).build();
136 
137         assertEquals(insets, copy);
138     }
139 
140     @Test
testBuilder_consumed()141     public void testBuilder_consumed() {
142         final WindowInsets insets = new WindowInsets.Builder()
143                 .build();
144 
145         assertFalse(insets.hasSystemWindowInsets());
146         assertFalse(insets.hasStableInsets());
147         assertEquals(Insets.NONE, insets.getSystemGestureInsets());
148         assertNull(insets.getDisplayCutout());
149         assertNull(insets.getRoundedCorner(RoundedCorner.POSITION_TOP_LEFT));
150         assertNull(insets.getRoundedCorner(RoundedCorner.POSITION_TOP_RIGHT));
151         assertNull(insets.getRoundedCorner(RoundedCorner.POSITION_BOTTOM_RIGHT));
152         assertNull(insets.getRoundedCorner(RoundedCorner.POSITION_BOTTOM_LEFT));
153         assertTrue(insets.isConsumed());
154     }
155 
156     @Test
testBuilder_emptyCutout()157     public void testBuilder_emptyCutout() {
158         final WindowInsets insets = new WindowInsets.Builder()
159                 .setDisplayCutout(null)
160                 .build();
161 
162         assertFalse(insets.hasSystemWindowInsets());
163         assertFalse(insets.hasStableInsets());
164         assertEquals(Insets.NONE, insets.getSystemGestureInsets());
165 
166         assertNull(insets.getDisplayCutout());
167         assertFalse(insets.isConsumed());
168         assertTrue(insets.consumeDisplayCutout().isConsumed());
169         assertEquals(Insets.NONE, insets.getInsets(Type.displayCutout()));
170     }
171 
172     @Test
testBuilder_producesImmutableWindowInsets()173     public void testBuilder_producesImmutableWindowInsets() {
174         final WindowInsets.Builder builder = new WindowInsets.Builder()
175                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
176                 .setStableInsets(Insets.of(5, 6, 7, 8))
177                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
178                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
179                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
180                 .setDisplayCutout(CUTOUT);
181         final WindowInsets insets = builder.build();
182 
183         builder.setSystemWindowInsets(Insets.NONE);
184         builder.setStableInsets(Insets.NONE);
185         builder.setDisplayCutout(null);
186         builder.setSystemGestureInsets(Insets.NONE);
187         builder.setMandatorySystemGestureInsets(Insets.NONE);
188         builder.setTappableElementInsets(Insets.NONE);
189 
190         assertEquals(Insets.of(1, 2, 3, 4), insets.getSystemWindowInsets());
191         assertEquals(Insets.of(5, 6, 7, 8), insets.getStableInsets());
192         assertSame(CUTOUT, insets.getDisplayCutout());
193         assertEquals(getCutoutSafeInsets(insets), insets.getInsets(Type.displayCutout()));
194     }
195 
196     @Test
testBuilder_typeMap()197     public void testBuilder_typeMap() {
198         final WindowInsets insets = new WindowInsets.Builder()
199                 .setInsets(statusBars(), Insets.of(0, 50, 0, 0))
200                 .setInsets(navigationBars(), Insets.of(0, 0, 0, 100))
201                 .setInsets(tappableElement(), Insets.of(0, 10, 0, 10))
202                 .setInsets(mandatorySystemGestures(), Insets.of(0, 20, 0, 20))
203                 .setInsets(systemGestures(), Insets.of(0, 30, 0, 30))
204                 .setInsets(displayCutout(), Insets.of(0, 5, 0, 0))
205                 .setInsets(captionBar(), Insets.of(0, 50, 0, 0))
206                 .setInsets(ime(), Insets.of(0, 0, 0, 300))
207                 .setInsets(systemOverlays(), Insets.of(10, 0, 0, 10))
208                 .build();
209         assertEquals(Insets.of(0, 50, 0, 0), insets.getInsets(statusBars()));
210         assertEquals(Insets.of(0, 0, 0, 100), insets.getInsets(navigationBars()));
211         assertEquals(Insets.of(0, 10, 0, 10), insets.getInsets(tappableElement()));
212         assertEquals(Insets.of(0, 20, 0, 20), insets.getInsets(mandatorySystemGestures()));
213         assertEquals(Insets.of(0, 30, 0, 30), insets.getInsets(systemGestures()));
214         assertEquals(Insets.of(0, 5, 0, 0), insets.getInsets(displayCutout()));
215         assertEquals(Insets.of(0, 50, 0, 0), insets.getInsets(captionBar()));
216         assertEquals(Insets.of(10, 50, 0, 100), insets.getSystemWindowInsets());
217         assertEquals(Insets.of(10, 50, 0, 100), insets.getInsets(systemBars()));
218         assertEquals(Insets.of(0, 0, 0, 300), insets.getInsets(ime()));
219         assertEquals(Insets.of(10, 0, 0, 10), insets.getInsets(systemOverlays()));
220     }
221 
222     @Test
testBuilder_typeMapIgnoringVisibility()223     public void testBuilder_typeMapIgnoringVisibility() {
224         final WindowInsets insets = new WindowInsets.Builder()
225                 .setInsetsIgnoringVisibility(statusBars(), Insets.of(0, 50, 0, 0))
226                 .setInsetsIgnoringVisibility(navigationBars(), Insets.of(0, 0, 0, 100))
227                 .setInsetsIgnoringVisibility(tappableElement(), Insets.of(0, 10, 0, 10))
228                 .setInsetsIgnoringVisibility(mandatorySystemGestures(), Insets.of(0, 20, 0, 20))
229                 .setInsetsIgnoringVisibility(systemGestures(), Insets.of(0, 30, 0, 30))
230                 .setInsetsIgnoringVisibility(displayCutout(), Insets.of(0, 5, 0, 0))
231                 .setInsetsIgnoringVisibility(captionBar(), Insets.of(0, 50, 0, 0))
232                 .setInsetsIgnoringVisibility(systemOverlays(), Insets.of(10, 0, 0, 10))
233                 .build();
234         assertEquals(Insets.of(0, 50, 0, 0),
235                 insets.getInsetsIgnoringVisibility(statusBars()));
236         assertEquals(Insets.of(0, 0, 0, 100),
237                 insets.getInsetsIgnoringVisibility(navigationBars()));
238         assertEquals(Insets.of(0, 10, 0, 10),
239                 insets.getInsetsIgnoringVisibility(tappableElement()));
240         assertEquals(Insets.of(0, 20, 0, 20),
241                 insets.getInsetsIgnoringVisibility(mandatorySystemGestures()));
242         assertEquals(Insets.of(0, 30, 0, 30),
243                 insets.getInsetsIgnoringVisibility(systemGestures()));
244         assertEquals(Insets.of(0, 5, 0, 0),
245                 insets.getInsetsIgnoringVisibility(displayCutout()));
246         assertEquals(Insets.of(0, 50, 0, 0),
247                 insets.getInsetsIgnoringVisibility(captionBar()));
248         assertEquals(Insets.of(10, 0, 0, 10),
249                 insets.getInsetsIgnoringVisibility(systemOverlays()));
250         assertEquals(Insets.of(10, 50, 0, 100), insets.getStableInsets());
251 
252         Exception exception = null;
253         try {
254             new WindowInsets.Builder().setInsetsIgnoringVisibility(ime(), Insets.NONE);
255         } catch (Exception e) {
256             exception = e;
257         }
258         assertNotNull(exception);
259 
260         exception = null;
261         try {
262             insets.getInsetsIgnoringVisibility(ime());
263         } catch (Exception e) {
264             exception = e;
265         }
266         assertNotNull(exception);
267     }
268 
269     @Test
testBuilder_compatInsets()270     public void testBuilder_compatInsets() {
271         final WindowInsets insets = new WindowInsets.Builder()
272                 .setSystemWindowInsets(Insets.of(0, 50, 30, 10))
273                 .build();
274         assertEquals(Insets.of(0, 50, 0, 0), insets.getInsets(statusBars()));
275         assertEquals(Insets.of(0, 0, 30, 10), insets.getInsets(navigationBars()));
276     }
277 
278     @Test
testBuilder_visibility()279     public void testBuilder_visibility() {
280         final WindowInsets insets = new WindowInsets.Builder()
281                 .setInsets(navigationBars(), Insets.of(0, 0, 0, 100))
282                 .setInsets(ime(), Insets.of(0, 0, 0, 300))
283                 .setVisible(navigationBars(), true)
284                 .setVisible(ime(), true)
285                 .build();
286         assertTrue(insets.isVisible(navigationBars()));
287         assertTrue(insets.isVisible(navigationBars() | ime()));
288         assertFalse(insets.isVisible(navigationBars() | statusBars()));
289     }
290 
291     @Test
testEquality()292     public void testEquality() {
293         final WindowInsets insets = new WindowInsets.Builder()
294                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
295                 .setStableInsets(Insets.of(5, 6, 7, 8))
296                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
297                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
298                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
299                 .setDisplayCutout(CUTOUT).build();
300 
301         final WindowInsets insets2 = new WindowInsets.Builder()
302                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
303                 .setStableInsets(Insets.of(5, 6, 7, 8))
304                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
305                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
306                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
307                 .setDisplayCutout(CUTOUT).build();
308 
309         assertNotSame("Test setup failed, insets and insets2 should not be identical",
310                 insets, insets2);
311 
312         assertEquals(insets, insets2);
313         assertEquals(insets.hashCode(), insets2.hashCode());
314     }
315 
316     @Test
testInEquality_consuming()317     public void testInEquality_consuming() {
318         final WindowInsets insets = new WindowInsets.Builder()
319                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
320                 .setStableInsets(Insets.of(5, 6, 7, 8))
321                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
322                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
323                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
324                 .setDisplayCutout(CUTOUT).build();
325 
326         assertNotEquals(insets, insets.consumeSystemWindowInsets());
327         assertNotEquals(insets, insets.consumeDisplayCutout());
328     }
329 
330     @Test
testConsume_doesntChangeVisibility()331     public void testConsume_doesntChangeVisibility() {
332         final WindowInsets insets = new WindowInsets.Builder()
333                 .setInsets(ime(), Insets.of(0, 0, 0, 300))
334                 .setVisible(ime(), true)
335                 .build();
336 
337         final WindowInsets consumed = insets.consumeSystemWindowInsets();
338 
339         assertTrue(consumed.isVisible(ime()));
340     }
341 
342     @Test
testConsume_systemWindowInsets()343     public void testConsume_systemWindowInsets() {
344         final WindowInsets insets = new WindowInsets.Builder()
345                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
346                 .setStableInsets(Insets.of(5, 6, 7, 8))
347                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
348                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
349                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
350                 .setDisplayCutout(CUTOUT).build();
351 
352         final WindowInsets consumed = insets.consumeSystemWindowInsets();
353 
354         assertEquals(Insets.NONE, consumed.getSystemWindowInsets());
355         assertEquals(Insets.NONE, consumed.getStableInsets());
356         assertEquals(insets.getDisplayCutout(), consumed.getDisplayCutout());
357         assertEquals(Insets.NONE, consumed.getSystemGestureInsets());
358         assertEquals(Insets.NONE, consumed.getMandatorySystemGestureInsets());
359         assertEquals(Insets.NONE, consumed.getTappableElementInsets());
360         assertEquals(Insets.NONE, consumed.getInsets(Type.displayCutout()));
361     }
362 
363     @Test
testConsume_stableInsets()364     public void testConsume_stableInsets() {
365         final WindowInsets insets = new WindowInsets.Builder()
366                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
367                 .setStableInsets(Insets.of(5, 6, 7, 8))
368                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
369                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
370                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
371                 .setDisplayCutout(CUTOUT).build();
372 
373         final WindowInsets consumed = insets.consumeStableInsets();
374         assertSame(insets, consumed);
375     }
376 
377     @Test
testConsume_displayCutout()378     public void testConsume_displayCutout() {
379         final WindowInsets insets = new WindowInsets.Builder()
380                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
381                 .setStableInsets(Insets.of(5, 6, 7, 8))
382                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
383                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
384                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
385                 .setDisplayCutout(CUTOUT).build();
386 
387         final WindowInsets consumed = insets.consumeDisplayCutout();
388 
389         assertEquals(insets.getSystemWindowInsets(), consumed.getSystemWindowInsets());
390         assertEquals(insets.getStableInsets(), consumed.getStableInsets());
391         assertNull(consumed.getDisplayCutout());
392         assertEquals(insets.getSystemGestureInsets(), consumed.getSystemGestureInsets());
393         assertEquals(insets.getMandatorySystemGestureInsets(), consumed.getMandatorySystemGestureInsets());
394         assertEquals(insets.getTappableElementInsets(), consumed.getTappableElementInsets());
395         assertEquals(
396                 insets.getInsets(Type.displayCutout()), consumed.getInsets(Type.displayCutout()));
397     }
398 
399     @Test
testConsistency_individualSides()400     public void testConsistency_individualSides() {
401         final WindowInsets insets = new WindowInsets.Builder()
402                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
403                 .setStableInsets(Insets.of(5, 6, 7, 8))
404                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
405                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
406                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
407                 .setDisplayCutout(CUTOUT).build();
408 
409         assertEquals(insets.getSystemWindowInsets(), Insets.of(
410                 insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(),
411                 insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom()));
412         assertEquals(insets.getStableInsets(), Insets.of(
413                 insets.getStableInsetLeft(), insets.getStableInsetTop(),
414                 insets.getStableInsetRight(), insets.getStableInsetBottom()));
415     }
416 
417     @Test
418     @SuppressWarnings("deprecation")
testReplacingConsumedSystemWindowInset_staysZeroAndConsumed()419     public void testReplacingConsumedSystemWindowInset_staysZeroAndConsumed() {
420         final WindowInsets consumed = new WindowInsets.Builder().build();
421         final WindowInsets replaced = consumed.replaceSystemWindowInsets(new Rect(1, 2, 3, 4));
422 
423         assertEquals(Insets.NONE, replaced.getSystemWindowInsets());
424         assertTrue(replaced.isConsumed());
425     }
426 
427     @Test
428     @SuppressWarnings("deprecation")
testReplacingSystemWindowInsets_works()429     public void testReplacingSystemWindowInsets_works() {
430         final WindowInsets replaced = new WindowInsets.Builder()
431                 .setSystemWindowInsets(Insets.of(100, 200, 300, 400))
432                 .setStableInsets(Insets.of(5, 6, 7, 8))
433                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
434                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
435                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
436                 .setDisplayCutout(CUTOUT).build()
437                 .replaceSystemWindowInsets(new Rect(1, 2, 3, 4));
438         final WindowInsets expected = new WindowInsets.Builder()
439                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
440                 .setStableInsets(Insets.of(5, 6, 7, 8))
441                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
442                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
443                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
444                 .setDisplayCutout(CUTOUT).build();
445 
446         assertEquals(expected, replaced);
447     }
448 
449     @Test
450     @SuppressWarnings("deprecation")
testReplacingSystemWindowInsets_consistencyAcrossOverloads()451     public void testReplacingSystemWindowInsets_consistencyAcrossOverloads() {
452         final Rect newInsets = new Rect(100, 200, 300, 400);
453         final WindowInsets insets = new WindowInsets.Builder()
454                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
455                 .setStableInsets(Insets.of(5, 6, 7, 8))
456                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
457                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
458                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
459                 .setDisplayCutout(CUTOUT).build();
460 
461         assertEquals(insets.replaceSystemWindowInsets(newInsets),
462                 insets.replaceSystemWindowInsets(newInsets.left, newInsets.top, newInsets.right,
463                         newInsets.bottom));
464     }
465 
466     @Test
testInEquality_difference()467     public void testInEquality_difference() {
468         final WindowInsets insets = new WindowInsets.Builder()
469                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
470                 .setStableInsets(Insets.of(5, 6, 7, 8))
471                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
472                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
473                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
474                 .setDisplayCutout(CUTOUT).build();
475 
476         final WindowInsets insetsChangedSysWindowInsets = new WindowInsets.Builder()
477                 .setSystemWindowInsets(Insets.of(10, 20, 30, 40))
478                 .setStableInsets(Insets.of(5, 6, 7, 8))
479                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
480                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
481                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
482                 .setDisplayCutout(CUTOUT).build();
483 
484         final WindowInsets insetsChangedStableInsets = new WindowInsets.Builder()
485                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
486                 .setStableInsets(Insets.of(50, 60, 70, 80))
487                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
488                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
489                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
490                 .setDisplayCutout(CUTOUT).build();
491 
492         final WindowInsets insetsChangedCutout = new WindowInsets.Builder()
493                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
494                 .setStableInsets(Insets.of(5, 6, 7, 8))
495                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
496                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
497                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
498                 .setDisplayCutout(CUTOUT2).build();
499 
500         final WindowInsets insetsChangedGesture = new WindowInsets.Builder()
501                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
502                 .setStableInsets(Insets.of(5, 6, 7, 8))
503                 .setSystemGestureInsets(Insets.of(90, 100, 110, 120))
504                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
505                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
506                 .setDisplayCutout(CUTOUT).build();
507 
508         final WindowInsets insetsChangedMandatoryGesture = new WindowInsets.Builder()
509                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
510                 .setStableInsets(Insets.of(5, 6, 7, 8))
511                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
512                 .setMandatorySystemGestureInsets(Insets.of(130, 140, 150, 160))
513                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
514                 .setDisplayCutout(CUTOUT).build();
515 
516         final WindowInsets insetsChangedTappableElement = new WindowInsets.Builder()
517                 .setSystemWindowInsets(Insets.of(1, 2, 3, 4))
518                 .setStableInsets(Insets.of(5, 6, 7, 8))
519                 .setSystemGestureInsets(Insets.of(9, 10, 11, 12))
520                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
521                 .setTappableElementInsets(Insets.of(170, 180, 190, 200))
522                 .setDisplayCutout(CUTOUT).build();
523 
524         assertNotEquals(insets, insetsChangedSysWindowInsets);
525         assertNotEquals(insets, insetsChangedStableInsets);
526         assertNotEquals(insets, insetsChangedCutout);
527         assertNotEquals(insets, insetsChangedGesture);
528         assertNotEquals(insets, insetsChangedMandatoryGesture);
529         assertNotEquals(insets, insetsChangedTappableElement);
530     }
531 
532     @Test
testInset()533     public void testInset() {
534         final WindowInsets insets = new WindowInsets.Builder()
535                 .setSystemWindowInsets(Insets.of(10, 20, 30, 40))
536                 .setStableInsets(Insets.of(50, 60, 70, 80))
537                 .setSystemGestureInsets(Insets.of(90, 100, 110, 120))
538                 .setMandatorySystemGestureInsets(Insets.of(130, 140, 150, 160))
539                 .setTappableElementInsets(Insets.of(170, 180, 190, 200))
540                 .setDisplayCutout(CUTOUT).build();
541 
542         final WindowInsets insetInsets = insets.inset(
543                 INSET_LEFT, INSET_TOP, INSET_RIGHT, INSET_BOTTOM);
544 
545         assertEquals(applyInset(insets.getSystemWindowInsets()),
546                 insetInsets.getSystemWindowInsets());
547         assertEquals(applyInset(insets.getStableInsets()), insetInsets.getStableInsets());
548         assertEquals(applyInset(insets.getSystemGestureInsets()),
549                 insetInsets.getSystemGestureInsets());
550         assertEquals(applyInset(insets.getMandatorySystemGestureInsets()),
551                 insetInsets.getMandatorySystemGestureInsets());
552         assertEquals(applyInset(insets.getTappableElementInsets()),
553                 insetInsets.getTappableElementInsets());
554         assertEquals(applyInset(getCutoutSafeInsets(insets)), getCutoutSafeInsets(insetInsets));
555         assertEquals(applyInset(getCutoutSafeInsets(insets)),
556                 insetInsets.getInsets(Type.displayCutout()));
557     }
558 
559     @Test
testInset_clipsToZero()560     public void testInset_clipsToZero() {
561         final WindowInsets insets = new WindowInsets.Builder()
562                 .setSystemWindowInsets(Insets.of(10, 20, 30, 40))
563                 .setStableInsets(Insets.of(50, 60, 70, 80))
564                 .setSystemGestureInsets(Insets.of(90, 100, 110, 120))
565                 .setMandatorySystemGestureInsets(Insets.of(13, 14, 15, 16))
566                 .setTappableElementInsets(Insets.of(17, 18, 19, 20))
567                 .setDisplayCutout(CUTOUT).build();
568 
569         final WindowInsets insetInsets = insets.inset(1000, 1000, 1000, 1000);
570 
571         assertEquals(Insets.NONE, insetInsets.getSystemWindowInsets());
572         assertEquals(Insets.NONE, insetInsets.getStableInsets());
573         assertEquals(Insets.NONE, insetInsets.getSystemGestureInsets());
574         assertEquals(Insets.NONE, insetInsets.getMandatorySystemGestureInsets());
575         assertEquals(Insets.NONE, insetInsets.getTappableElementInsets());
576         assertNull(insetInsets.getDisplayCutout());
577         assertEquals(Insets.NONE, insetInsets.getInsets(Type.displayCutout()));
578     }
579 
580     @Test
testConsumed_copy()581     public void testConsumed_copy() {
582         final WindowInsets insets = new WindowInsets.Builder()
583                 .setSystemWindowInsets(Insets.of(10, 20, 30, 40))
584                 .setStableInsets(Insets.of(50, 60, 70, 80))
585                 .build();
586 
587         final WindowInsets consumed = insets.consumeSystemWindowInsets().consumeStableInsets();
588         final WindowInsets copy = new WindowInsets(consumed);
589         assertTrue(copy.isConsumed());
590     }
591 
592     @Test
593     @ApiTest(apis = {"android.view.WindowInsets#CONSUMED"})
testConsumedInstance()594     public void testConsumedInstance() {
595         assertTrue(WindowInsets.CONSUMED.isConsumed());
596     }
597 
applyInset(Insets res)598     private static Insets applyInset(Insets res) {
599         return Insets.of(Math.max(0, res.left - INSET_LEFT),
600                 Math.max(0, res.top - INSET_TOP),
601                 Math.max(0, res.right - INSET_RIGHT),
602                 Math.max(0, res.bottom - INSET_BOTTOM));
603     }
604 
getCutoutSafeInsets(WindowInsets insets)605     private static Insets getCutoutSafeInsets(WindowInsets insets) {
606         final DisplayCutout dc = insets.getDisplayCutout();
607         return Insets.of(dc.getSafeInsetLeft(), dc.getSafeInsetTop(), dc.getSafeInsetRight(),
608                 dc.getSafeInsetBottom());
609     }
610 }
611