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 import static org.mockito.Matchers.anyInt; 21 import static org.mockito.Matchers.anyString; 22 import static org.mockito.Mockito.when; 23 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.os.UserHandle; 28 import android.provider.Settings; 29 import android.support.v14.preference.SwitchPreference; 30 31 import com.android.internal.view.RotationPolicy; 32 import com.android.settings.core.BasePreferenceController; 33 import com.android.settings.testutils.FakeFeatureFactory; 34 import com.android.settings.testutils.SettingsRobolectricTestRunner; 35 import com.android.settings.testutils.shadow.ShadowRotationPolicy; 36 import com.android.settings.testutils.shadow.ShadowSystemSettings; 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.RuntimeEnvironment; 46 import org.robolectric.annotation.Config; 47 48 @RunWith(SettingsRobolectricTestRunner.class) 49 @Config(shadows = ShadowSystemSettings.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 @After tearDown()73 public void tearDown() { 74 ShadowSystemSettings.reset(); 75 } 76 77 @Test isAvailableWhenPolicyAllows()78 public void isAvailableWhenPolicyAllows() { 79 assertThat(mController.isAvailable()).isFalse(); 80 81 enableAutoRotationPreference(); 82 83 assertThat(mController.isAvailable()).isTrue(); 84 } 85 86 @Test updatePreference_settingsIsOff_shouldTurnOffToggle()87 public void updatePreference_settingsIsOff_shouldTurnOffToggle() { 88 disableAutoRotation(); 89 90 mController.updateState(mPreference); 91 92 assertThat(mPreference.isChecked()).isFalse(); 93 } 94 95 @Test updatePreference_settingsIsOn_shouldTurnOnToggle()96 public void updatePreference_settingsIsOn_shouldTurnOnToggle() { 97 enableAutoRotation(); 98 99 mController.updateState(mPreference); 100 101 assertThat(mPreference.isChecked()).isTrue(); 102 } 103 104 @Test testGetAvailabilityStatus()105 public void testGetAvailabilityStatus() { 106 assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController 107 .UNSUPPORTED_ON_DEVICE); 108 109 enableAutoRotationPreference(); 110 111 assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController 112 .AVAILABLE); 113 114 disableAutoRotationPreference(); 115 116 assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController 117 .UNSUPPORTED_ON_DEVICE); 118 } 119 120 @Test testIsCheck()121 public void testIsCheck() { 122 assertThat(mController.isChecked()).isFalse(); 123 124 enableAutoRotation(); 125 126 assertThat(mController.isChecked()).isTrue(); 127 128 disableAutoRotation(); 129 130 assertThat(mController.isChecked()).isFalse(); 131 } 132 133 @Test 134 @Config(shadows = {ShadowRotationPolicy.class}) testSetCheck()135 public void testSetCheck() { 136 ShadowRotationPolicy.setRotationSupported(true); 137 138 mController.setChecked(false); 139 assertThat(mController.isChecked()).isFalse(); 140 assertThat(RotationPolicy.isRotationLocked(mContext)).isTrue(); 141 142 mController.setChecked(true); 143 assertThat(mController.isChecked()).isTrue(); 144 assertThat(RotationPolicy.isRotationLocked(mContext)).isFalse(); 145 } 146 147 @Test isSliceableCorrectKey_returnsTrue()148 public void isSliceableCorrectKey_returnsTrue() { 149 final AutoRotatePreferenceController controller = 150 new AutoRotatePreferenceController(mContext, "auto_rotate"); 151 assertThat(controller.isSliceable()).isTrue(); 152 } 153 154 @Test isSliceableIncorrectKey_returnsFalse()155 public void isSliceableIncorrectKey_returnsFalse() { 156 final AutoRotatePreferenceController controller = 157 new AutoRotatePreferenceController(mContext, "bad_key"); 158 assertThat(controller.isSliceable()).isFalse(); 159 } 160 enableAutoRotationPreference()161 private void enableAutoRotationPreference() { 162 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); 163 when(mContext.getResources().getBoolean(anyInt())).thenReturn(true); 164 Settings.System.putInt(mContentResolver, 165 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0); 166 } 167 disableAutoRotationPreference()168 private void disableAutoRotationPreference() { 169 when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true); 170 when(mContext.getResources().getBoolean(anyInt())).thenReturn(true); 171 Settings.System.putInt(mContentResolver, 172 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 1); 173 } 174 enableAutoRotation()175 private void enableAutoRotation() { 176 Settings.System.putIntForUser(mContentResolver, 177 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT); 178 } 179 disableAutoRotation()180 private void disableAutoRotation() { 181 Settings.System.putIntForUser(mContentResolver, 182 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT); 183 } 184 } 185