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.when; 24 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.content.pm.PackageManager; 28 import android.os.UserHandle; 29 import android.provider.Settings; 30 31 import androidx.preference.SwitchPreference; 32 33 import com.android.internal.view.RotationPolicy; 34 import com.android.settings.core.BasePreferenceController; 35 import com.android.settings.testutils.FakeFeatureFactory; 36 import com.android.settings.testutils.shadow.ShadowRotationPolicy; 37 38 import org.junit.After; 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Answers; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.RobolectricTestRunner; 46 import org.robolectric.RuntimeEnvironment; 47 import org.robolectric.annotation.Config; 48 49 @RunWith(RobolectricTestRunner.class) 50 public class AutoRotatePreferenceControllerTest { 51 52 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 53 private Context mContext; 54 @Mock 55 private PackageManager mPackageManager; 56 private SwitchPreference mPreference; 57 private ContentResolver mContentResolver; 58 private AutoRotatePreferenceController mController; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 FakeFeatureFactory.setupForTest(); 64 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 65 mPreference = new SwitchPreference(RuntimeEnvironment.application); 66 when(mContext.getPackageManager()).thenReturn(mPackageManager); 67 when(mContext.getContentResolver()).thenReturn(mContentResolver); 68 69 mController = new AutoRotatePreferenceController(mContext, "auto_rotate"); 70 } 71 72 @Test isAvailableWhenPolicyAllows()73 public void isAvailableWhenPolicyAllows() { 74 assertThat(mController.isAvailable()).isFalse(); 75 76 enableAutoRotationPreference(); 77 78 assertThat(mController.isAvailable()).isTrue(); 79 } 80 81 @Test updatePreference_settingsIsOff_shouldTurnOffToggle()82 public void updatePreference_settingsIsOff_shouldTurnOffToggle() { 83 disableAutoRotation(); 84 85 mController.updateState(mPreference); 86 87 assertThat(mPreference.isChecked()).isFalse(); 88 } 89 90 @Test updatePreference_settingsIsOn_shouldTurnOnToggle()91 public void updatePreference_settingsIsOn_shouldTurnOnToggle() { 92 enableAutoRotation(); 93 94 mController.updateState(mPreference); 95 96 assertThat(mPreference.isChecked()).isTrue(); 97 } 98 99 @Test testGetAvailabilityStatus()100 public void testGetAvailabilityStatus() { 101 assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController 102 .UNSUPPORTED_ON_DEVICE); 103 104 enableAutoRotationPreference(); 105 106 assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController 107 .AVAILABLE); 108 109 disableAutoRotationPreference(); 110 111 assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController 112 .UNSUPPORTED_ON_DEVICE); 113 } 114 115 @Test testIsCheck()116 public void testIsCheck() { 117 assertThat(mController.isChecked()).isFalse(); 118 119 enableAutoRotation(); 120 121 assertThat(mController.isChecked()).isTrue(); 122 123 disableAutoRotation(); 124 125 assertThat(mController.isChecked()).isFalse(); 126 } 127 128 @Test 129 @Config(shadows = {ShadowRotationPolicy.class}) testSetCheck()130 public void testSetCheck() { 131 ShadowRotationPolicy.setRotationSupported(true); 132 133 mController.setChecked(false); 134 assertThat(mController.isChecked()).isFalse(); 135 assertThat(RotationPolicy.isRotationLocked(mContext)).isTrue(); 136 137 mController.setChecked(true); 138 assertThat(mController.isChecked()).isTrue(); 139 assertThat(RotationPolicy.isRotationLocked(mContext)).isFalse(); 140 } 141 142 @Test isSliceableCorrectKey_returnsTrue()143 public void isSliceableCorrectKey_returnsTrue() { 144 final AutoRotatePreferenceController controller = 145 new AutoRotatePreferenceController(mContext, "auto_rotate"); 146 assertThat(controller.isSliceable()).isTrue(); 147 } 148 149 @Test isSliceableIncorrectKey_returnsFalse()150 public void isSliceableIncorrectKey_returnsFalse() { 151 final AutoRotatePreferenceController controller = 152 new AutoRotatePreferenceController(mContext, "bad_key"); 153 assertThat(controller.isSliceable()).isFalse(); 154 } 155 enableAutoRotationPreference()156 private void enableAutoRotationPreference() { 157 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); 158 when(mContext.getResources().getBoolean(anyInt())).thenReturn(true); 159 Settings.System.putInt(mContentResolver, 160 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0); 161 } 162 disableAutoRotationPreference()163 private void disableAutoRotationPreference() { 164 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); 165 when(mContext.getResources().getBoolean(anyInt())).thenReturn(true); 166 Settings.System.putInt(mContentResolver, 167 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 1); 168 } 169 enableAutoRotation()170 private void enableAutoRotation() { 171 Settings.System.putIntForUser(mContentResolver, 172 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT); 173 } 174 disableAutoRotation()175 private void disableAutoRotation() { 176 Settings.System.putIntForUser(mContentResolver, 177 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT); 178 } 179 } 180