1/* 2 * Copyright 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 17package androidx.core.view; 18 19import static android.os.Build.VERSION.SDK_INT; 20 21import static org.junit.Assert.assertEquals; 22import static org.junit.Assert.assertNotEquals; 23import static org.junit.Assert.assertNull; 24 25import android.graphics.Rect; 26import android.support.test.filters.SmallTest; 27import android.support.test.runner.AndroidJUnit4; 28 29import org.junit.Before; 30import org.junit.Test; 31import org.junit.runner.RunWith; 32 33import java.util.Arrays; 34 35@RunWith(AndroidJUnit4.class) 36@SmallTest 37public class DisplayCutoutCompatTest { 38 39 DisplayCutoutCompat mCutoutTop; 40 DisplayCutoutCompat mCutoutTopBottom; 41 DisplayCutoutCompat mCutoutTopBottomClone; 42 DisplayCutoutCompat mCutoutLeftRight; 43 44 @Before 45 public void setUp() throws Exception { 46 mCutoutTop = new DisplayCutoutCompat(new Rect(0, 10, 0, 0), Arrays.asList( 47 new Rect(50, 0, 60, 10))); 48 mCutoutTopBottom = new DisplayCutoutCompat(new Rect(0, 10, 0, 20), Arrays.asList( 49 new Rect(50, 0, 60, 10), 50 new Rect(50, 100, 60, 120))); 51 mCutoutTopBottomClone = new DisplayCutoutCompat(new Rect(0, 10, 0, 20), Arrays.asList( 52 new Rect(50, 0, 60, 10), 53 new Rect(50, 100, 60, 120))); 54 mCutoutLeftRight = new DisplayCutoutCompat(new Rect(30, 0, 40, 0), Arrays.asList( 55 new Rect(0, 50, 30, 60), 56 new Rect(100, 60, 140, 50))); 57 } 58 59 @Test 60 public void testSafeInsets() { 61 if (SDK_INT >= 28) { 62 assertEquals("left", 30, mCutoutLeftRight.getSafeInsetLeft()); 63 assertEquals("top", 10, mCutoutTopBottom.getSafeInsetTop()); 64 assertEquals("right", 40, mCutoutLeftRight.getSafeInsetRight()); 65 assertEquals("bottom", 20, mCutoutTopBottom.getSafeInsetBottom()); 66 } else { 67 assertEquals("left", 0, mCutoutLeftRight.getSafeInsetLeft()); 68 assertEquals("top", 0, mCutoutTopBottom.getSafeInsetTop()); 69 assertEquals("right", 0, mCutoutLeftRight.getSafeInsetRight()); 70 assertEquals("bottom", 0, mCutoutTopBottom.getSafeInsetBottom()); 71 } 72 } 73 74 @Test 75 public void testBoundingRects() { 76 if (SDK_INT >= 28) { 77 assertEquals(Arrays.asList(new Rect(50, 0, 60, 10)), mCutoutTop.getBoundingRects()); 78 } else { 79 assertNull(mCutoutTop.getBoundingRects()); 80 } 81 } 82 83 @Test 84 public void testEquals() { 85 assertEquals(mCutoutTopBottomClone, mCutoutTopBottom); 86 87 if (SDK_INT >= 28) { 88 assertNotEquals(mCutoutTopBottom, mCutoutLeftRight); 89 } 90 } 91 @Test 92 public void testHashCode() { 93 assertEquals(mCutoutTopBottomClone.hashCode(), mCutoutTopBottom.hashCode()); 94 } 95} 96