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