1 /* 2 * Copyright (C) 2021 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.Display.DEFAULT_DISPLAY; 20 import static android.view.Surface.ROTATION_0; 21 import static android.view.Surface.ROTATION_90; 22 23 import static com.android.dx.mockito.inline.extended.ExtendedMockito.anyInt; 24 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; 25 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; 26 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; 27 28 import static com.google.common.truth.Truth.assertThat; 29 30 import android.app.WindowConfiguration; 31 import android.content.Context; 32 import android.content.res.Resources; 33 import android.graphics.Point; 34 import android.graphics.Rect; 35 import android.hardware.display.DisplayManagerGlobal; 36 import android.platform.test.annotations.Presubmit; 37 import android.util.DisplayMetrics; 38 39 import androidx.test.core.app.ApplicationProvider; 40 import androidx.test.ext.junit.runners.AndroidJUnit4; 41 import androidx.test.filters.SmallTest; 42 43 import com.android.dx.mockito.inline.extended.StaticMockitoSession; 44 45 import org.junit.After; 46 import org.junit.Before; 47 import org.junit.Test; 48 import org.junit.runner.RunWith; 49 import org.mockito.Mockito; 50 import org.mockito.quality.Strictness; 51 52 /** 53 * Tests for {@link Display}. 54 * 55 * <p>Build/Install/Run: 56 * 57 * atest FrameworksMockingCoreTests:android.view.DisplayTest 58 * 59 * <p>This test class is a part of Window Manager Service tests and specified in 60 * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}. 61 */ 62 @RunWith(AndroidJUnit4.class) 63 @SmallTest 64 @Presubmit 65 public class DisplayTest { 66 67 private static final int APP_WIDTH = 272; 68 private static final int APP_HEIGHT = 700; 69 // Tablet size device, ROTATION_0 corresponds to portrait. 70 private static final int LOGICAL_WIDTH = 700; 71 private static final int LOGICAL_HEIGHT = 1800; 72 73 // Bounds of the app when the device is in portrait mode. 74 private static Rect sAppBoundsPortrait = buildAppBounds(LOGICAL_WIDTH, LOGICAL_HEIGHT); 75 private static Rect sAppBoundsLandscape = buildAppBounds(LOGICAL_HEIGHT, LOGICAL_WIDTH); 76 77 // Bounds of the device. 78 private static Rect sDeviceBoundsPortrait = new Rect(0, 0, LOGICAL_WIDTH, LOGICAL_HEIGHT); 79 private static Rect sDeviceBoundsLandscape = new Rect(0, 0, LOGICAL_HEIGHT, LOGICAL_WIDTH); 80 81 82 private StaticMockitoSession mMockitoSession; 83 84 private DisplayManagerGlobal mDisplayManagerGlobal; 85 private Context mApplicationContext; 86 private DisplayInfo mDisplayInfo = new DisplayInfo(); 87 88 @Before setupTests()89 public void setupTests() { 90 mMockitoSession = mockitoSession() 91 .mockStatic(DisplayManagerGlobal.class) 92 .strictness(Strictness.LENIENT) 93 .startMocking(); 94 95 // Ensure no adjustments are set before each test. 96 mApplicationContext = ApplicationProvider.getApplicationContext(); 97 mApplicationContext.getResources().getConfiguration().windowConfiguration.setAppBounds( 98 null); 99 mApplicationContext.getResources().getConfiguration().windowConfiguration.setMaxBounds( 100 null); 101 mApplicationContext.getResources().getConfiguration().windowConfiguration 102 .setDisplayRotation(WindowConfiguration.ROTATION_UNDEFINED); 103 mDisplayInfo.rotation = ROTATION_0; 104 105 mDisplayManagerGlobal = mock(DisplayManagerGlobal.class); 106 doReturn(mDisplayInfo).when(mDisplayManagerGlobal).getDisplayInfo(anyInt()); 107 } 108 109 @After teardownTests()110 public void teardownTests() { 111 if (mMockitoSession != null) { 112 mMockitoSession.finishMocking(); 113 } 114 Mockito.framework().clearInlineMocks(); 115 } 116 117 @Test testConstructor_defaultDisplayAdjustments_matchesDisplayInfo()118 public void testConstructor_defaultDisplayAdjustments_matchesDisplayInfo() { 119 setDisplayInfoPortrait(mDisplayInfo); 120 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 121 DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); 122 assertThat(display.getDisplayAdjustments()).isEqualTo( 123 DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); 124 DisplayInfo actualDisplayInfo = new DisplayInfo(); 125 display.getDisplayInfo(actualDisplayInfo); 126 verifyDisplayInfo(actualDisplayInfo, mDisplayInfo); 127 } 128 129 @Test testConstructor_defaultResources_matchesDisplayInfo()130 public void testConstructor_defaultResources_matchesDisplayInfo() { 131 setDisplayInfoPortrait(mDisplayInfo); 132 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 133 mApplicationContext.getResources()); 134 assertThat(display.getDisplayAdjustments()).isEqualTo( 135 mApplicationContext.getResources().getDisplayAdjustments()); 136 DisplayInfo actualDisplayInfo = new DisplayInfo(); 137 display.getDisplayInfo(actualDisplayInfo); 138 verifyDisplayInfo(actualDisplayInfo, mDisplayInfo); 139 } 140 141 @Test testGetRotation_defaultDisplayAdjustments_rotationNotAdjusted()142 public void testGetRotation_defaultDisplayAdjustments_rotationNotAdjusted() { 143 setDisplayInfoPortrait(mDisplayInfo); 144 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 145 DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); 146 assertThat(display.getRotation()).isEqualTo(ROTATION_0); 147 } 148 149 @Test testGetRotation_resourcesWithOverrideDisplayAdjustments_rotationAdjusted()150 public void testGetRotation_resourcesWithOverrideDisplayAdjustments_rotationAdjusted() { 151 // GIVEN display is not rotated. 152 setDisplayInfoPortrait(mDisplayInfo); 153 // GIVEN fixed rotation adjustments are rotated, and an override is set. 154 setLocalDisplayInConfig(mApplicationContext.getResources(), ROTATION_90); 155 // GIVEN display is constructed with default resources. 156 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 157 mApplicationContext.getResources()); 158 // THEN rotation is adjusted since an override is set. 159 assertThat(display.getRotation()).isEqualTo(ROTATION_90); 160 } 161 162 @Test testGetRealSize_defaultResourcesPortrait_matchesLogicalSize()163 public void testGetRealSize_defaultResourcesPortrait_matchesLogicalSize() { 164 // GIVEN display is not rotated. 165 setDisplayInfoPortrait(mDisplayInfo); 166 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 167 mApplicationContext.getResources()); 168 // THEN real size matches display orientation. 169 verifyRealSizeIsPortrait(display); 170 } 171 172 @Test testGetRealSize_defaultResourcesLandscape_matchesRotatedLogicalSize()173 public void testGetRealSize_defaultResourcesLandscape_matchesRotatedLogicalSize() { 174 // GIVEN display is rotated. 175 setDisplayInfoLandscape(mDisplayInfo); 176 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 177 mApplicationContext.getResources()); 178 // THEN real size matches display orientation. 179 verifyRealSizeIsLandscape(display); 180 } 181 182 @Test testGetRealSize_defaultDisplayAdjustmentsPortrait_matchesLogicalSize()183 public void testGetRealSize_defaultDisplayAdjustmentsPortrait_matchesLogicalSize() { 184 // GIVEN display is not rotated. 185 setDisplayInfoPortrait(mDisplayInfo); 186 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 187 DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); 188 // THEN real size matches display orientation. 189 verifyRealSizeIsPortrait(display); 190 } 191 192 @Test testGetRealSize_defaultDisplayAdjustmentsLandscape_matchesLogicalSize()193 public void testGetRealSize_defaultDisplayAdjustmentsLandscape_matchesLogicalSize() { 194 // GIVEN display is rotated. 195 setDisplayInfoLandscape(mDisplayInfo); 196 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 197 DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); 198 // THEN real size matches display orientation. 199 verifyRealSizeIsLandscape(display); 200 } 201 202 @Test testGetRealSize_resourcesWithPortraitOverrideRotation_rotatedLogicalSize()203 public void testGetRealSize_resourcesWithPortraitOverrideRotation_rotatedLogicalSize() { 204 // GIVEN display is rotated. 205 setDisplayInfoLandscape(mDisplayInfo); 206 // GIVEN fixed rotation adjustments are rotated, and an override is set. 207 setLocalDisplayInConfig(mApplicationContext.getResources(), ROTATION_0); 208 // GIVEN display is constructed with default resources. 209 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 210 mApplicationContext.getResources()); 211 // THEN real size matches app orientation. 212 verifyRealSizeIsPortrait(display); 213 } 214 215 @Test testGetRealSize_resourcesWithLandscapeOverrideRotation_rotatedLogicalSize()216 public void testGetRealSize_resourcesWithLandscapeOverrideRotation_rotatedLogicalSize() { 217 // GIVEN display is not rotated. 218 setDisplayInfoPortrait(mDisplayInfo); 219 // GIVEN fixed rotation adjustments are rotated, and an override is set. 220 setLocalDisplayInConfig(mApplicationContext.getResources(), ROTATION_90); 221 // GIVEN display is constructed with default resources. 222 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 223 mApplicationContext.getResources()); 224 // THEN real size matches app orientation. 225 verifyRealSizeIsLandscape(display); 226 } 227 228 @Test testGetRealSize_resourcesPortraitSandboxed_matchesAppSandboxBounds()229 public void testGetRealSize_resourcesPortraitSandboxed_matchesAppSandboxBounds() { 230 // GIVEN display is not rotated. 231 setDisplayInfoPortrait(mDisplayInfo); 232 // GIVEN app is letterboxed. 233 setMaxBoundsSandboxed(mApplicationContext.getResources(), sAppBoundsPortrait); 234 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 235 mApplicationContext.getResources()); 236 // THEN real size matches app bounds. 237 verifyRealSizeMatchesBounds(display, sAppBoundsPortrait); 238 } 239 240 @Test testGetRealSize_resourcesPortraitSandboxed_matchesDisplayAreaSandboxBounds()241 public void testGetRealSize_resourcesPortraitSandboxed_matchesDisplayAreaSandboxBounds() { 242 // GIVEN display is not rotated. 243 setDisplayInfoPortrait(mDisplayInfo); 244 // GIVEN max bounds reflect DisplayArea size, which is the same size as the display. 245 setMaxBoundsSandboxed(mApplicationContext.getResources(), sDeviceBoundsPortrait); 246 // GIVEN app bounds do not stretch to include the full DisplayArea. 247 mApplicationContext.getResources().getConfiguration().windowConfiguration 248 .setAppBounds(buildAppBounds(LOGICAL_WIDTH, LOGICAL_HEIGHT - 10)); 249 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 250 mApplicationContext.getResources()); 251 // THEN real metrics matches max bounds for the DisplayArea. 252 verifyRealSizeMatchesBounds(display, sDeviceBoundsPortrait); 253 } 254 255 @Test testGetRealSize_resourcesLandscapeSandboxed_matchesAppSandboxBounds()256 public void testGetRealSize_resourcesLandscapeSandboxed_matchesAppSandboxBounds() { 257 // GIVEN display is rotated. 258 setDisplayInfoLandscape(mDisplayInfo); 259 // GIVEN app is letterboxed. 260 setMaxBoundsSandboxed(mApplicationContext.getResources(), sAppBoundsLandscape); 261 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 262 mApplicationContext.getResources()); 263 // THEN real size matches app bounds. 264 verifyRealSizeMatchesBounds(display, sAppBoundsLandscape); 265 } 266 267 @Test testGetRealSize_resourcesLandscapeSandboxed_matchesDisplayAreaSandboxBounds()268 public void testGetRealSize_resourcesLandscapeSandboxed_matchesDisplayAreaSandboxBounds() { 269 // GIVEN display is rotated. 270 setDisplayInfoLandscape(mDisplayInfo); 271 // GIVEN max bounds reflect DisplayArea size, which is the same size as the display. 272 setMaxBoundsSandboxed(mApplicationContext.getResources(), sDeviceBoundsLandscape); 273 // GIVEN app bounds do not stretch to include the full DisplayArea. 274 mApplicationContext.getResources().getConfiguration().windowConfiguration 275 .setAppBounds(buildAppBounds(LOGICAL_HEIGHT, LOGICAL_WIDTH - 10)); 276 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 277 mApplicationContext.getResources()); 278 // THEN real metrics matches max bounds for the DisplayArea. 279 verifyRealSizeMatchesBounds(display, sDeviceBoundsLandscape); 280 } 281 282 @Test testGetRealMetrics_defaultResourcesPortrait_matchesLogicalSize()283 public void testGetRealMetrics_defaultResourcesPortrait_matchesLogicalSize() { 284 // GIVEN display is not rotated. 285 setDisplayInfoPortrait(mDisplayInfo); 286 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 287 mApplicationContext.getResources()); 288 // THEN real metrics matches display orientation. 289 verifyRealMetricsIsPortrait(display); 290 } 291 292 @Test testGetRealMetrics_defaultResourcesLandscape_matchesRotatedLogicalSize()293 public void testGetRealMetrics_defaultResourcesLandscape_matchesRotatedLogicalSize() { 294 // GIVEN display is rotated. 295 setDisplayInfoLandscape(mDisplayInfo); 296 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 297 mApplicationContext.getResources()); 298 // THEN real metrics matches display orientation. 299 verifyRealMetricsIsLandscape(display); 300 } 301 302 @Test testGetRealMetrics_defaultDisplayAdjustmentsPortrait_matchesLogicalSize()303 public void testGetRealMetrics_defaultDisplayAdjustmentsPortrait_matchesLogicalSize() { 304 // GIVEN display is not rotated. 305 setDisplayInfoPortrait(mDisplayInfo); 306 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 307 DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); 308 // THEN real metrics matches display orientation. 309 verifyRealMetricsIsPortrait(display); 310 } 311 312 @Test testGetRealMetrics_defaultDisplayAdjustmentsLandscape_matchesLogicalSize()313 public void testGetRealMetrics_defaultDisplayAdjustmentsLandscape_matchesLogicalSize() { 314 // GIVEN display is rotated. 315 setDisplayInfoLandscape(mDisplayInfo); 316 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 317 DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); 318 // THEN real metrics matches display orientation. 319 verifyRealMetricsIsLandscape(display); 320 } 321 322 @Test testGetRealMetrics_resourcesWithPortraitOverrideRotation_rotatedLogicalSize()323 public void testGetRealMetrics_resourcesWithPortraitOverrideRotation_rotatedLogicalSize() { 324 // GIVEN display is rotated. 325 setDisplayInfoLandscape(mDisplayInfo); 326 // GIVEN fixed rotation adjustments are rotated with an override. 327 setLocalDisplayInConfig(mApplicationContext.getResources(), ROTATION_0); 328 // GIVEN display is constructed with default resources. 329 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 330 mApplicationContext.getResources()); 331 // THEN real metrics matches app orientation. 332 verifyRealMetricsIsPortrait(display); 333 } 334 335 @Test testGetRealMetrics_resourcesWithLandscapeOverrideRotation_rotatedLogicalSize()336 public void testGetRealMetrics_resourcesWithLandscapeOverrideRotation_rotatedLogicalSize() { 337 // GIVEN display is not rotated. 338 setDisplayInfoPortrait(mDisplayInfo); 339 // GIVEN fixed rotation adjustments are rotated. 340 setLocalDisplayInConfig(mApplicationContext.getResources(), ROTATION_90); 341 // GIVEN display is constructed with default resources. 342 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 343 mApplicationContext.getResources()); 344 // THEN real metrics matches app orientation. 345 verifyRealMetricsIsLandscape(display); 346 } 347 348 @Test testGetRealMetrics_resourcesPortraitSandboxed_matchesAppSandboxBounds()349 public void testGetRealMetrics_resourcesPortraitSandboxed_matchesAppSandboxBounds() { 350 // GIVEN display is not rotated. 351 setDisplayInfoPortrait(mDisplayInfo); 352 // GIVEN app is letterboxed. 353 setMaxBoundsSandboxed(mApplicationContext.getResources(), sAppBoundsPortrait); 354 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 355 mApplicationContext.getResources()); 356 // THEN real metrics matches app bounds. 357 verifyRealMetricsMatchesBounds(display, sAppBoundsPortrait); 358 } 359 360 @Test testGetRealMetrics_resourcesPortraitSandboxed_matchesDisplayAreaSandboxBounds()361 public void testGetRealMetrics_resourcesPortraitSandboxed_matchesDisplayAreaSandboxBounds() { 362 // GIVEN display is not rotated. 363 setDisplayInfoPortrait(mDisplayInfo); 364 // GIVEN max bounds reflect DisplayArea size, which is the same size as the display. 365 setMaxBoundsSandboxed(mApplicationContext.getResources(), sDeviceBoundsPortrait); 366 // GIVEN app bounds do not stretch to include the full DisplayArea. 367 mApplicationContext.getResources().getConfiguration().windowConfiguration 368 .setAppBounds(buildAppBounds(LOGICAL_WIDTH, LOGICAL_HEIGHT - 10)); 369 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 370 mApplicationContext.getResources()); 371 // THEN real metrics matches max bounds for the DisplayArea. 372 verifyRealMetricsMatchesBounds(display, sDeviceBoundsPortrait); 373 } 374 375 @Test testGetRealMetrics_resourcesLandscapeSandboxed_matchesAppSandboxBounds()376 public void testGetRealMetrics_resourcesLandscapeSandboxed_matchesAppSandboxBounds() { 377 // GIVEN display is rotated. 378 setDisplayInfoLandscape(mDisplayInfo); 379 // GIVEN app is letterboxed. 380 setMaxBoundsSandboxed(mApplicationContext.getResources(), sAppBoundsLandscape); 381 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 382 mApplicationContext.getResources()); 383 // THEN real metrics matches app bounds. 384 verifyRealMetricsMatchesBounds(display, sAppBoundsLandscape); 385 } 386 387 @Test testGetRealMetrics_resourcesLandscapeSandboxed_matchesDisplayAreaSandboxBounds()388 public void testGetRealMetrics_resourcesLandscapeSandboxed_matchesDisplayAreaSandboxBounds() { 389 // GIVEN display is rotated. 390 setDisplayInfoLandscape(mDisplayInfo); 391 // GIVEN max bounds reflect DisplayArea size, which is the same size as the display. 392 setMaxBoundsSandboxed(mApplicationContext.getResources(), sDeviceBoundsLandscape); 393 // GIVEN app bounds do not stretch to include the full DisplayArea. 394 mApplicationContext.getResources().getConfiguration().windowConfiguration 395 .setAppBounds(buildAppBounds(LOGICAL_HEIGHT, LOGICAL_WIDTH - 10)); 396 final Display display = new Display(mDisplayManagerGlobal, DEFAULT_DISPLAY, mDisplayInfo, 397 mApplicationContext.getResources()); 398 // THEN real metrics matches max bounds for the DisplayArea. 399 verifyRealMetricsMatchesBounds(display, sDeviceBoundsLandscape); 400 } 401 402 // Given rotated display dimensions, calculate the letterboxed app bounds. buildAppBounds(int displayWidth, int displayHeight)403 private static Rect buildAppBounds(int displayWidth, int displayHeight) { 404 final int midWidth = displayWidth / 2; 405 final int left = midWidth - (APP_WIDTH / 2); 406 final int right = midWidth + (APP_WIDTH / 2); 407 final int midHeight = displayHeight / 2; 408 // Coordinate system starts at top left. 409 final int top = midHeight - (APP_HEIGHT / 2); 410 final int bottom = midHeight + (APP_HEIGHT / 2); 411 return new Rect(left, top, right, bottom); 412 } 413 setDisplayInfoLandscape(DisplayInfo displayInfo)414 private static void setDisplayInfoLandscape(DisplayInfo displayInfo) { 415 displayInfo.rotation = ROTATION_90; 416 // Flip width & height assignment since the device is rotated. 417 displayInfo.logicalWidth = LOGICAL_HEIGHT; 418 displayInfo.logicalHeight = LOGICAL_WIDTH; 419 } 420 setDisplayInfoPortrait(DisplayInfo displayInfo)421 private static void setDisplayInfoPortrait(DisplayInfo displayInfo) { 422 displayInfo.rotation = ROTATION_0; 423 displayInfo.logicalWidth = LOGICAL_WIDTH; 424 displayInfo.logicalHeight = LOGICAL_HEIGHT; 425 } 426 427 /** 428 * Set max bounds to be sandboxed to the app bounds, indicating the app is in 429 * size compat mode or letterbox. 430 */ setMaxBoundsSandboxed(Resources resources, Rect bounds)431 private static void setMaxBoundsSandboxed(Resources resources, Rect bounds) { 432 resources.getConfiguration().windowConfiguration.setMaxBounds(bounds); 433 } 434 435 /** 436 * Do not compare entire display info, since it is updated to match display the test is run on. 437 */ verifyDisplayInfo(DisplayInfo actual, DisplayInfo expected)438 private static void verifyDisplayInfo(DisplayInfo actual, DisplayInfo expected) { 439 assertThat(actual.displayId).isEqualTo(expected.displayId); 440 assertThat(actual.rotation).isEqualTo(expected.rotation); 441 assertThat(actual.logicalWidth).isEqualTo(LOGICAL_WIDTH); 442 assertThat(actual.logicalHeight).isEqualTo(LOGICAL_HEIGHT); 443 } 444 verifyRealSizeIsLandscape(Display display)445 private static void verifyRealSizeIsLandscape(Display display) { 446 Point size = new Point(); 447 display.getRealSize(size); 448 // Flip the width and height check since the device is rotated. 449 assertThat(size).isEqualTo(new Point(LOGICAL_HEIGHT, LOGICAL_WIDTH)); 450 } 451 verifyRealMetricsIsLandscape(Display display)452 private static void verifyRealMetricsIsLandscape(Display display) { 453 DisplayMetrics metrics = new DisplayMetrics(); 454 display.getRealMetrics(metrics); 455 // Flip the width and height check since the device is rotated. 456 assertThat(metrics.widthPixels).isEqualTo(LOGICAL_HEIGHT); 457 assertThat(metrics.heightPixels).isEqualTo(LOGICAL_WIDTH); 458 } 459 verifyRealSizeIsPortrait(Display display)460 private static void verifyRealSizeIsPortrait(Display display) { 461 Point size = new Point(); 462 display.getRealSize(size); 463 assertThat(size).isEqualTo(new Point(LOGICAL_WIDTH, LOGICAL_HEIGHT)); 464 } 465 verifyRealMetricsIsPortrait(Display display)466 private static void verifyRealMetricsIsPortrait(Display display) { 467 DisplayMetrics metrics = new DisplayMetrics(); 468 display.getRealMetrics(metrics); 469 assertThat(metrics.widthPixels).isEqualTo(LOGICAL_WIDTH); 470 assertThat(metrics.heightPixels).isEqualTo(LOGICAL_HEIGHT); 471 } 472 verifyRealSizeMatchesBounds(Display display, Rect bounds)473 private static void verifyRealSizeMatchesBounds(Display display, Rect bounds) { 474 Point size = new Point(); 475 display.getRealSize(size); 476 assertThat(size).isEqualTo(new Point(bounds.width(), bounds.height())); 477 } 478 verifyRealMetricsMatchesBounds(Display display, Rect bounds)479 private static void verifyRealMetricsMatchesBounds(Display display, Rect bounds) { 480 DisplayMetrics metrics = new DisplayMetrics(); 481 display.getRealMetrics(metrics); 482 assertThat(metrics.widthPixels).isEqualTo(bounds.width()); 483 assertThat(metrics.heightPixels).isEqualTo(bounds.height()); 484 } 485 setLocalDisplayInConfig(Resources resources, @Surface.Rotation int rotation)486 private static void setLocalDisplayInConfig(Resources resources, 487 @Surface.Rotation int rotation) { 488 resources.getConfiguration().windowConfiguration.setDisplayRotation(rotation); 489 } 490 } 491