1 /* 2 * Copyright (C) 2023 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 com.android.server.display; 18 19 import static android.view.Surface.ROTATION_0; 20 import static android.view.Surface.ROTATION_180; 21 import static android.view.Surface.ROTATION_270; 22 import static android.view.Surface.ROTATION_90; 23 24 import static com.google.common.truth.Truth.assertThat; 25 26 import android.graphics.Point; 27 import android.graphics.Rect; 28 import android.platform.test.annotations.Presubmit; 29 import android.view.SurfaceControl; 30 31 import androidx.test.ext.junit.runners.AndroidJUnit4; 32 import androidx.test.filters.SmallTest; 33 import androidx.test.platform.app.InstrumentationRegistry; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 41 /** 42 * Tests for the {@link DisplayDevice} class. 43 * 44 * Build/Install/Run: 45 * atest DisplayServicesTests:DisplayDeviceTest 46 */ 47 @SmallTest 48 @Presubmit 49 @RunWith(AndroidJUnit4.class) 50 public class DisplayDeviceTest { 51 private final DisplayDeviceInfo mDisplayDeviceInfo = new DisplayDeviceInfo(); 52 private static final int WIDTH = 500; 53 private static final int HEIGHT = 900; 54 private static final Point PORTRAIT_SIZE = new Point(WIDTH, HEIGHT); 55 private static final Point LANDSCAPE_SIZE = new Point(HEIGHT, WIDTH); 56 57 @Mock 58 private SurfaceControl.Transaction mMockTransaction; 59 60 @Before setup()61 public void setup() { 62 MockitoAnnotations.initMocks(this); 63 mDisplayDeviceInfo.width = WIDTH; 64 mDisplayDeviceInfo.height = HEIGHT; 65 mDisplayDeviceInfo.rotation = ROTATION_0; 66 } 67 68 @Test testGetDisplaySurfaceDefaultSizeLocked_notRotated()69 public void testGetDisplaySurfaceDefaultSizeLocked_notRotated() { 70 DisplayDevice displayDevice = new FakeDisplayDevice(mDisplayDeviceInfo); 71 assertThat(displayDevice.getDisplaySurfaceDefaultSizeLocked()).isEqualTo(PORTRAIT_SIZE); 72 } 73 74 @Test testGetDisplaySurfaceDefaultSizeLocked_rotation0()75 public void testGetDisplaySurfaceDefaultSizeLocked_rotation0() { 76 DisplayDevice displayDevice = new FakeDisplayDevice(mDisplayDeviceInfo); 77 displayDevice.setProjectionLocked(mMockTransaction, ROTATION_0, new Rect(), new Rect()); 78 assertThat(displayDevice.getDisplaySurfaceDefaultSizeLocked()).isEqualTo(PORTRAIT_SIZE); 79 } 80 81 @Test testGetDisplaySurfaceDefaultSizeLocked_rotation90()82 public void testGetDisplaySurfaceDefaultSizeLocked_rotation90() { 83 DisplayDevice displayDevice = new FakeDisplayDevice(mDisplayDeviceInfo); 84 displayDevice.setProjectionLocked(mMockTransaction, ROTATION_90, new Rect(), new Rect()); 85 assertThat(displayDevice.getDisplaySurfaceDefaultSizeLocked()).isEqualTo(LANDSCAPE_SIZE); 86 } 87 88 @Test testGetDisplaySurfaceDefaultSizeLocked_rotation180()89 public void testGetDisplaySurfaceDefaultSizeLocked_rotation180() { 90 DisplayDevice displayDevice = new FakeDisplayDevice(mDisplayDeviceInfo); 91 displayDevice.setProjectionLocked(mMockTransaction, ROTATION_180, new Rect(), new Rect()); 92 assertThat(displayDevice.getDisplaySurfaceDefaultSizeLocked()).isEqualTo(PORTRAIT_SIZE); 93 } 94 95 @Test testGetDisplaySurfaceDefaultSizeLocked_rotation270()96 public void testGetDisplaySurfaceDefaultSizeLocked_rotation270() { 97 DisplayDevice displayDevice = new FakeDisplayDevice(mDisplayDeviceInfo); 98 displayDevice.setProjectionLocked(mMockTransaction, ROTATION_270, new Rect(), new Rect()); 99 assertThat(displayDevice.getDisplaySurfaceDefaultSizeLocked()).isEqualTo(LANDSCAPE_SIZE); 100 } 101 102 private static class FakeDisplayDevice extends DisplayDevice { 103 private final DisplayDeviceInfo mDisplayDeviceInfo; 104 FakeDisplayDevice(DisplayDeviceInfo displayDeviceInfo)105 FakeDisplayDevice(DisplayDeviceInfo displayDeviceInfo) { 106 super(null, null, "", InstrumentationRegistry.getInstrumentation().getContext()); 107 mDisplayDeviceInfo = displayDeviceInfo; 108 } 109 110 @Override hasStableUniqueId()111 public boolean hasStableUniqueId() { 112 return false; 113 } 114 115 @Override getDisplayDeviceInfoLocked()116 public DisplayDeviceInfo getDisplayDeviceInfoLocked() { 117 return mDisplayDeviceInfo; 118 } 119 } 120 } 121