Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
| 19 | import static android.view.DisplayCutout.NO_CUTOUT; |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 20 | import static android.view.DisplayCutout.fromBoundingRect; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 21 | import static android.view.DisplayCutout.fromSpec; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 22 | |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 23 | import static org.hamcrest.Matchers.not; |
| 24 | import static org.hamcrest.Matchers.sameInstance; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
| 27 | import static org.junit.Assert.assertNotEquals; |
Adrian Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 28 | import static org.junit.Assert.assertThat; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 29 | import static org.junit.Assert.assertTrue; |
| 30 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 31 | import android.graphics.Rect; |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 32 | import android.graphics.Region; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 33 | import android.os.Parcel; |
| 34 | import android.platform.test.annotations.Presubmit; |
| 35 | import android.support.test.filters.SmallTest; |
| 36 | import android.support.test.runner.AndroidJUnit4; |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 37 | import android.util.Size; |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 38 | import android.view.DisplayCutout.ParcelableWrapper; |
| 39 | |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 43 | @RunWith(AndroidJUnit4.class) |
| 44 | @SmallTest |
| 45 | @Presubmit |
| 46 | public 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 Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 51 | new Region(5, 6, 7, 8), new Size(9, 10)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 52 | |
| 53 | final DisplayCutout mCutoutTop = createCutoutTop(); |
| 54 | |
| 55 | @Test |
| 56 | public void hasCutout() throws Exception { |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 57 | assertTrue(NO_CUTOUT.isEmpty()); |
| 58 | assertFalse(mCutoutTop.isEmpty()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 59 | } |
| 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 Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 68 | assertEquals(new Rect(1, 2, 3, 4), mCutoutNumbers.getSafeInsets()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void getBoundingRect() throws Exception { |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 73 | assertEquals(new Rect(50, 0, 75, 100), mCutoutTop.getBoundingRect()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 74 | } |
| 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 Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 150 | assertTrue(cutout.isEmpty()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void inset_bounds() throws Exception { |
| 155 | DisplayCutout cutout = mCutoutTop.inset(1, 2, 3, 4); |
| 156 | |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 157 | assertEquals(new Rect(49, -2, 74, 98), cutout.getBoundingRect()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Test |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 161 | public void computeSafeInsets_top() throws Exception { |
| 162 | DisplayCutout cutout = fromBoundingRect(0, 0, 100, 20) |
| 163 | .computeSafeInsets(200, 400); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 164 | |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 165 | 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 Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Test |
| 209 | public void calculateRelativeTo_left() throws Exception { |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 210 | DisplayCutout cutout = fromBoundingRect(0, 0, 20, 100) |
| 211 | .computeSafeInsets(400, 200) |
| 212 | .calculateRelativeTo(new Rect(5, 5, 195, 95)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 213 | |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 214 | assertEquals(new Rect(15, 0, 0, 0), cutout.getSafeInsets()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Test |
| 218 | public void calculateRelativeTo_bottom() throws Exception { |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 219 | DisplayCutout cutout = fromBoundingRect(0, 180, 100, 200) |
| 220 | .computeSafeInsets(100, 200) |
| 221 | .calculateRelativeTo(new Rect(5, 5, 95, 195)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 222 | |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 223 | assertEquals(new Rect(0, 0, 0, 15), cutout.getSafeInsets()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Test |
| 227 | public void calculateRelativeTo_right() throws Exception { |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 228 | DisplayCutout cutout = fromBoundingRect(180, 0, 200, 100) |
| 229 | .computeSafeInsets(200, 100) |
| 230 | .calculateRelativeTo(new Rect(5, 5, 195, 95)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 231 | |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 232 | assertEquals(new Rect(0, 0, 15, 0), cutout.getSafeInsets()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Test |
| 236 | public void calculateRelativeTo_bounds() throws Exception { |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 237 | DisplayCutout cutout = fromBoundingRect(0, 0, 100, 20) |
| 238 | .computeSafeInsets(200, 400) |
| 239 | .calculateRelativeTo(new Rect(5, 10, 95, 180)); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 240 | |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 241 | assertEquals(new Rect(-5, -10, 95, 10), cutout.getBounds().getBounds()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void fromBoundingPolygon() throws Exception { |
| 246 | assertEquals( |
Adrian Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 247 | new Rect(50, 0, 75, 100), |
Adrian Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 248 | DisplayCutout.fromBoundingRect(50, 0, 75, 100).getBounds().getBounds()); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 249 | } |
| 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 Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 265 | 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 Roos | 8d13bf1 | 2018-02-21 15:17:07 +0100 | [diff] [blame] | 278 | 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 Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 302 | 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 Roos | d07bafd | 2017-12-11 17:30:56 +0100 | [diff] [blame] | 345 | return createCutoutWithInsets(0, 100, 0, 0); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 346 | } |
| 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 Roos | 2426421 | 2018-02-19 16:26:15 +0100 | [diff] [blame] | 351 | new Region(50, 0, 75, 100), |
| 352 | null); |
Adrian Roos | d4970af | 2017-11-10 15:48:01 +0100 | [diff] [blame] | 353 | } |
| 354 | } |