1 /* 2 * Copyright (C) 2019 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.accessibility; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.content.Context; 22 import android.os.UserHandle; 23 import android.provider.Settings; 24 25 import androidx.preference.SwitchPreference; 26 27 import com.android.internal.view.RotationPolicy; 28 import com.android.settings.core.BasePreferenceController; 29 import com.android.settings.testutils.shadow.ShadowDeviceStateRotationLockSettingsManager; 30 import com.android.settings.testutils.shadow.ShadowRotationPolicy; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.robolectric.RobolectricTestRunner; 36 import org.robolectric.RuntimeEnvironment; 37 import org.robolectric.annotation.Config; 38 39 @RunWith(RobolectricTestRunner.class) 40 public class LockScreenRotationPreferenceControllerTest { 41 42 private Context mContext; 43 private SwitchPreference mPreference; 44 private LockScreenRotationPreferenceController mController; 45 46 @Before setUp()47 public void setUp() { 48 mContext = RuntimeEnvironment.application; 49 mPreference = new SwitchPreference(mContext); 50 mController = new LockScreenRotationPreferenceController(mContext, "lock_screen"); 51 } 52 53 @Test 54 @Config(shadows = { 55 ShadowRotationPolicy.class, 56 ShadowDeviceStateRotationLockSettingsManager.class 57 }) getAvailabilityStatus_supportedRotation_shouldReturnAvailable()58 public void getAvailabilityStatus_supportedRotation_shouldReturnAvailable() { 59 ShadowRotationPolicy.setRotationSupported(true /* supported */); 60 61 assertThat(mController.getAvailabilityStatus()).isEqualTo( 62 BasePreferenceController.AVAILABLE); 63 } 64 65 @Test 66 @Config(shadows = { 67 ShadowRotationPolicy.class, 68 ShadowDeviceStateRotationLockSettingsManager.class 69 }) getAvailabilityStatus_deviceStateRotationEnabled_returnsUnsupported()70 public void getAvailabilityStatus_deviceStateRotationEnabled_returnsUnsupported() { 71 ShadowRotationPolicy.setRotationSupported(true /* supported */); 72 ShadowDeviceStateRotationLockSettingsManager.setDeviceStateRotationLockEnabled(true); 73 74 assertThat(mController.getAvailabilityStatus()).isEqualTo( 75 BasePreferenceController.UNSUPPORTED_ON_DEVICE); 76 } 77 78 @Test 79 @Config(shadows = { 80 ShadowRotationPolicy.class, 81 ShadowDeviceStateRotationLockSettingsManager.class getAvailabilityStatus_unsupportedRotation_shouldReturnUnsupportedOnDevice()82 }) public void getAvailabilityStatus_unsupportedRotation_shouldReturnUnsupportedOnDevice() { 83 ShadowRotationPolicy.setRotationSupported(false /* supported */); 84 85 assertThat(mController.getAvailabilityStatus()).isEqualTo( 86 BasePreferenceController.UNSUPPORTED_ON_DEVICE); 87 } 88 89 @Test 90 @Config(shadows = {ShadowRotationPolicy.class}) setChecked_enabled()91 public void setChecked_enabled() { 92 mController.setChecked(true /* isChecked */); 93 94 assertThat(mController.isChecked()).isTrue(); 95 assertThat(RotationPolicy.isRotationLocked(mContext)).isFalse(); 96 } 97 98 @Test 99 @Config(shadows = {ShadowRotationPolicy.class}) setChecked_disabled()100 public void setChecked_disabled() { 101 mController.setChecked(false /* isChecked */); 102 103 assertThat(mController.isChecked()).isFalse(); 104 assertThat(RotationPolicy.isRotationLocked(mContext)).isTrue(); 105 } 106 107 @Test updateState_settingIsOn_shouldTurnOnToggle()108 public void updateState_settingIsOn_shouldTurnOnToggle() { 109 Settings.System.putIntForUser(mContext.getContentResolver(), 110 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT); 111 112 mController.updateState(mPreference); 113 114 assertThat(mPreference.isChecked()).isTrue(); 115 } 116 117 @Test updateState_settingIsOff_shouldTurnOffToggle()118 public void updateState_settingIsOff_shouldTurnOffToggle() { 119 Settings.System.putIntForUser(mContext.getContentResolver(), 120 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT); 121 122 mController.updateState(mPreference); 123 124 assertThat(mPreference.isChecked()).isFalse(); 125 } 126 } 127