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 com.android.settings.display; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.when; 26 27 import android.app.admin.DevicePolicyManager; 28 import android.content.ContentResolver; 29 import android.content.Context; 30 import android.content.pm.PackageManager; 31 import android.os.UserHandle; 32 import android.provider.Settings; 33 34 import androidx.preference.SwitchPreference; 35 36 import com.android.internal.R; 37 import com.android.internal.view.RotationPolicy; 38 import com.android.settings.core.BasePreferenceController; 39 import com.android.settings.testutils.FakeFeatureFactory; 40 import com.android.settings.testutils.shadow.ShadowRotationPolicy; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Answers; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.RobolectricTestRunner; 49 import org.robolectric.RuntimeEnvironment; 50 import org.robolectric.annotation.Config; 51 52 @RunWith(RobolectricTestRunner.class) 53 public class AutoRotatePreferenceControllerTest { 54 55 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 56 private Context mContext; 57 @Mock 58 private PackageManager mPackageManager; 59 private SwitchPreference mPreference; 60 private ContentResolver mContentResolver; 61 private AutoRotatePreferenceController mController; 62 63 @Before setUp()64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 doReturn(mock(DevicePolicyManager.class)).when(mContext) 67 .getSystemService(Context.DEVICE_POLICY_SERVICE); 68 FakeFeatureFactory.setupForTest(); 69 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 70 mPreference = new SwitchPreference(RuntimeEnvironment.application); 71 when(mContext.getPackageManager()).thenReturn(mPackageManager); 72 when(mContext.getContentResolver()).thenReturn(mContentResolver); 73 disableDeviceStateRotation(); 74 75 mController = new AutoRotatePreferenceController(mContext, "auto_rotate"); 76 } 77 78 @Test isAvailableWhenPolicyAllows()79 public void isAvailableWhenPolicyAllows() { 80 assertThat(mController.isAvailable()).isFalse(); 81 82 enableAutoRotationPreference(); 83 84 assertThat(mController.isAvailable()).isTrue(); 85 } 86 87 @Test updatePreference_settingsIsOff_shouldTurnOffToggle()88 public void updatePreference_settingsIsOff_shouldTurnOffToggle() { 89 disableAutoRotation(); 90 91 mController.updateState(mPreference); 92 93 assertThat(mPreference.isChecked()).isFalse(); 94 } 95 96 @Test updatePreference_settingsIsOn_shouldTurnOnToggle()97 public void updatePreference_settingsIsOn_shouldTurnOnToggle() { 98 enableAutoRotation(); 99 100 mController.updateState(mPreference); 101 102 assertThat(mPreference.isChecked()).isTrue(); 103 } 104 105 @Test testGetAvailabilityStatus()106 public void testGetAvailabilityStatus() { 107 assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController 108 .UNSUPPORTED_ON_DEVICE); 109 110 enableAutoRotationPreference(); 111 112 assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController 113 .AVAILABLE); 114 115 disableAutoRotationPreference(); 116 117 assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController 118 .UNSUPPORTED_ON_DEVICE); 119 } 120 121 @Test getAvailabilityStatus_deviceRotationDisabled_returnsAvailable()122 public void getAvailabilityStatus_deviceRotationDisabled_returnsAvailable() { 123 enableAutoRotationPreference(); 124 disableDeviceStateRotation(); 125 126 int availability = mController.getAvailabilityStatus(); 127 128 assertThat(availability).isEqualTo(BasePreferenceController.AVAILABLE); 129 } 130 131 @Test getAvailabilityStatus_deviceRotationEnabled_returnsUnsupported()132 public void getAvailabilityStatus_deviceRotationEnabled_returnsUnsupported() { 133 enableAutoRotationPreference(); 134 enableDeviceStateRotation(); 135 136 int availability = mController.getAvailabilityStatus(); 137 138 assertThat(availability).isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); 139 } 140 141 @Test testIsCheck()142 public void testIsCheck() { 143 assertThat(mController.isChecked()).isFalse(); 144 145 enableAutoRotation(); 146 147 assertThat(mController.isChecked()).isTrue(); 148 149 disableAutoRotation(); 150 151 assertThat(mController.isChecked()).isFalse(); 152 } 153 154 @Test 155 @Config(shadows = {ShadowRotationPolicy.class}) testSetCheck()156 public void testSetCheck() { 157 ShadowRotationPolicy.setRotationSupported(true); 158 159 mController.setChecked(false); 160 assertThat(mController.isChecked()).isFalse(); 161 assertThat(RotationPolicy.isRotationLocked(mContext)).isTrue(); 162 163 mController.setChecked(true); 164 assertThat(mController.isChecked()).isTrue(); 165 assertThat(RotationPolicy.isRotationLocked(mContext)).isFalse(); 166 } 167 168 @Test isSliceableCorrectKey_returnsTrue()169 public void isSliceableCorrectKey_returnsTrue() { 170 final AutoRotatePreferenceController controller = 171 new AutoRotatePreferenceController(mContext, "auto_rotate"); 172 assertThat(controller.isSliceable()).isTrue(); 173 } 174 175 @Test isSliceableIncorrectKey_returnsFalse()176 public void isSliceableIncorrectKey_returnsFalse() { 177 final AutoRotatePreferenceController controller = 178 new AutoRotatePreferenceController(mContext, "bad_key"); 179 assertThat(controller.isSliceable()).isFalse(); 180 } 181 182 @Test isPublicSlice_returnTrue()183 public void isPublicSlice_returnTrue() { 184 assertThat(mController.isPublicSlice()).isTrue(); 185 } 186 enableAutoRotationPreference()187 private void enableAutoRotationPreference() { 188 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); 189 when(mContext.getResources().getBoolean(anyInt())).thenReturn(true); 190 Settings.System.putInt(mContentResolver, 191 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0); 192 } 193 disableAutoRotationPreference()194 private void disableAutoRotationPreference() { 195 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); 196 when(mContext.getResources().getBoolean(anyInt())).thenReturn(true); 197 Settings.System.putInt(mContentResolver, 198 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 1); 199 } 200 enableAutoRotation()201 private void enableAutoRotation() { 202 Settings.System.putIntForUser(mContentResolver, 203 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT); 204 } 205 disableAutoRotation()206 private void disableAutoRotation() { 207 Settings.System.putIntForUser(mContentResolver, 208 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT); 209 } 210 enableDeviceStateRotation()211 private void enableDeviceStateRotation() { 212 when(mContext.getResources().getStringArray( 213 R.array.config_perDeviceStateRotationLockDefaults)).thenReturn( 214 new String[]{"0:0", "1:1", "2:2"}); 215 } 216 disableDeviceStateRotation()217 private void disableDeviceStateRotation() { 218 when(mContext.getResources().getStringArray( 219 R.array.config_perDeviceStateRotationLockDefaults)).thenReturn(new String[]{}); 220 } 221 } 222