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.wm; 18 19 import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock; 20 import static com.android.dx.mockito.inline.extended.ExtendedMockito.never; 21 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; 22 23 import static org.junit.Assert.assertEquals; 24 25 import android.annotation.NonNull; 26 import android.platform.test.annotations.Presubmit; 27 import android.view.Surface; 28 29 import androidx.test.filters.SmallTest; 30 31 import org.junit.Test; 32 33 /** 34 * Test class for {@link DisplayRotationCoordinator} 35 * 36 * Build/Install/Run: 37 * atest DisplayRotationCoordinatorTests 38 */ 39 @SmallTest 40 @Presubmit 41 public class DisplayRotationCoordinatorTests { 42 43 private static final int FIRST_DISPLAY_ID = 1; 44 private static final int SECOND_DISPLAY_ID = 2; 45 46 @NonNull 47 private final DisplayRotationCoordinator mCoordinator = new DisplayRotationCoordinator(); 48 49 @Test testDefaultDisplayRotationChangedWhenNoCallbackRegistered()50 public void testDefaultDisplayRotationChangedWhenNoCallbackRegistered() { 51 // Does not cause NPE 52 mCoordinator.onDefaultDisplayRotationChanged(Surface.ROTATION_90); 53 } 54 55 @Test (expected = UnsupportedOperationException.class) testSecondRegistrationWithoutRemovingFirstWhenDifferentDisplay()56 public void testSecondRegistrationWithoutRemovingFirstWhenDifferentDisplay() { 57 Runnable callback1 = mock(Runnable.class); 58 Runnable callback2 = mock(Runnable.class); 59 mCoordinator.setDefaultDisplayRotationChangedCallback(FIRST_DISPLAY_ID, callback1); 60 mCoordinator.setDefaultDisplayRotationChangedCallback(SECOND_DISPLAY_ID, callback2); 61 assertEquals(callback1, mCoordinator.mDefaultDisplayRotationChangedCallback); 62 } 63 64 @Test testSecondRegistrationWithoutRemovingFirstWhenSameDisplay()65 public void testSecondRegistrationWithoutRemovingFirstWhenSameDisplay() { 66 Runnable callback1 = mock(Runnable.class); 67 Runnable callback2 = mock(Runnable.class); 68 mCoordinator.setDefaultDisplayRotationChangedCallback(FIRST_DISPLAY_ID, callback1); 69 mCoordinator.setDefaultDisplayRotationChangedCallback(FIRST_DISPLAY_ID, callback2); 70 assertEquals(callback2, mCoordinator.mDefaultDisplayRotationChangedCallback); 71 } 72 73 @Test testRemoveIncorrectRegistration()74 public void testRemoveIncorrectRegistration() { 75 Runnable callback1 = mock(Runnable.class); 76 Runnable callback2 = mock(Runnable.class); 77 mCoordinator.setDefaultDisplayRotationChangedCallback(FIRST_DISPLAY_ID, callback1); 78 mCoordinator.removeDefaultDisplayRotationChangedCallback(callback2); 79 assertEquals(callback1, mCoordinator.mDefaultDisplayRotationChangedCallback); 80 81 // FIRST_DISPLAY_ID is still able to register another callback because the previous 82 // removal should not have succeeded. 83 mCoordinator.setDefaultDisplayRotationChangedCallback(FIRST_DISPLAY_ID, callback2); 84 assertEquals(callback2, mCoordinator.mDefaultDisplayRotationChangedCallback); 85 } 86 87 @Test testSecondRegistrationAfterRemovingFirst()88 public void testSecondRegistrationAfterRemovingFirst() { 89 Runnable callback1 = mock(Runnable.class); 90 mCoordinator.setDefaultDisplayRotationChangedCallback(FIRST_DISPLAY_ID, callback1); 91 mCoordinator.removeDefaultDisplayRotationChangedCallback(callback1); 92 93 Runnable callback2 = mock(Runnable.class); 94 mCoordinator.setDefaultDisplayRotationChangedCallback(SECOND_DISPLAY_ID, callback2); 95 96 mCoordinator.onDefaultDisplayRotationChanged(Surface.ROTATION_90); 97 verify(callback2).run(); 98 verify(callback1, never()).run(); 99 } 100 101 @Test testRegisterThenDefaultDisplayRotationChanged()102 public void testRegisterThenDefaultDisplayRotationChanged() { 103 Runnable callback = mock(Runnable.class); 104 mCoordinator.setDefaultDisplayRotationChangedCallback(FIRST_DISPLAY_ID, callback); 105 assertEquals(Surface.ROTATION_0, mCoordinator.getDefaultDisplayCurrentRotation()); 106 verify(callback, never()).run(); 107 108 mCoordinator.onDefaultDisplayRotationChanged(Surface.ROTATION_90); 109 verify(callback).run(); 110 assertEquals(Surface.ROTATION_90, mCoordinator.getDefaultDisplayCurrentRotation()); 111 } 112 113 @Test testDefaultDisplayRotationChangedThenRegister()114 public void testDefaultDisplayRotationChangedThenRegister() { 115 mCoordinator.onDefaultDisplayRotationChanged(Surface.ROTATION_90); 116 Runnable callback = mock(Runnable.class); 117 mCoordinator.setDefaultDisplayRotationChangedCallback(FIRST_DISPLAY_ID, callback); 118 verify(callback).run(); 119 assertEquals(Surface.ROTATION_90, mCoordinator.getDefaultDisplayCurrentRotation()); 120 } 121 } 122