1 /* 2 * Copyright (C) 2021 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.android.settings.core.BasePreferenceController.AVAILABLE; 20 import static com.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING; 21 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; 22 23 import static com.google.common.truth.Truth.assertThat; 24 25 import static org.mockito.ArgumentMatchers.any; 26 import static org.mockito.ArgumentMatchers.anyInt; 27 import static org.mockito.Mockito.doReturn; 28 import static org.mockito.Mockito.when; 29 30 import android.Manifest; 31 import android.content.ContentResolver; 32 import android.content.Context; 33 import android.content.pm.PackageManager; 34 import android.content.pm.ResolveInfo; 35 import android.content.pm.ServiceInfo; 36 import android.os.UserHandle; 37 import android.provider.Settings; 38 39 import androidx.preference.Preference; 40 41 import com.android.settings.testutils.ResolveInfoBuilder; 42 import com.android.settings.testutils.shadow.ShadowDeviceStateRotationLockSettingsManager; 43 import com.android.settings.testutils.shadow.ShadowRotationPolicy; 44 import com.android.settings.testutils.shadow.ShadowSensorPrivacyManager; 45 import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager; 46 47 import org.junit.Before; 48 import org.junit.Test; 49 import org.junit.runner.RunWith; 50 import org.mockito.Mock; 51 import org.mockito.Mockito; 52 import org.mockito.MockitoAnnotations; 53 import org.robolectric.RobolectricTestRunner; 54 import org.robolectric.RuntimeEnvironment; 55 import org.robolectric.annotation.Config; 56 57 @RunWith(RobolectricTestRunner.class) 58 @Config(shadows = ShadowSensorPrivacyManager.class) 59 public class SmartAutoRotateControllerTest { 60 61 private static final String PACKAGE_NAME = "package_name"; 62 63 private SmartAutoRotateController mController; 64 @Mock 65 private PackageManager mPackageManager; 66 @Mock 67 private Preference mPreference; 68 private ContentResolver mContentResolver; 69 private final DeviceStateRotationLockSettingsManager mDeviceStateAutoRotateSettingsManager = 70 DeviceStateRotationLockSettingsManager.getInstance(RuntimeEnvironment.application); 71 72 @Before setUp()73 public void setUp() { 74 MockitoAnnotations.initMocks(this); 75 final Context context = Mockito.spy(RuntimeEnvironment.application); 76 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 77 when(context.getPackageManager()).thenReturn(mPackageManager); 78 when(context.getContentResolver()).thenReturn(mContentResolver); 79 doReturn(PACKAGE_NAME).when(mPackageManager).getRotationResolverPackageName(); 80 doReturn(PackageManager.PERMISSION_GRANTED).when(mPackageManager).checkPermission( 81 Manifest.permission.CAMERA, PACKAGE_NAME); 82 mController = Mockito.spy(new SmartAutoRotateController(context, "test_key")); 83 when(mController.isCameraLocked()).thenReturn(false); 84 when(mController.isPowerSaveMode()).thenReturn(false); 85 doReturn(mController.getPreferenceKey()).when(mPreference).getKey(); 86 87 final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build(); 88 resolveInfo.serviceInfo = new ServiceInfo(); 89 when(mPackageManager.resolveService(any(), anyInt())).thenReturn(resolveInfo); 90 enableAutoRotation(); 91 } 92 93 @Test getAvailabilityStatus_returnAvailable()94 public void getAvailabilityStatus_returnAvailable() { 95 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 96 } 97 98 @Test getAvailabilityStatus_resolveInfoIsNull_returnUnsupportedOnDevice()99 public void getAvailabilityStatus_resolveInfoIsNull_returnUnsupportedOnDevice() { 100 when(mPackageManager.resolveService(any(), anyInt())).thenReturn(null); 101 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 102 } 103 104 @Test getAvailabilityStatus_noCameraPermission_returnDisableDependentSetting()105 public void getAvailabilityStatus_noCameraPermission_returnDisableDependentSetting() { 106 doReturn(PackageManager.PERMISSION_DENIED).when(mPackageManager).checkPermission( 107 Manifest.permission.CAMERA, PACKAGE_NAME); 108 109 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 110 } 111 112 @Test getAvailabilityStatus_rotationLocked_returnDisableDependentSetting()113 public void getAvailabilityStatus_rotationLocked_returnDisableDependentSetting() { 114 disableAutoRotation(); 115 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 116 } 117 118 @Test getAvailabilityStatus_cameraDisabled_returnDisableDependentSetting()119 public void getAvailabilityStatus_cameraDisabled_returnDisableDependentSetting() { 120 when(mController.isCameraLocked()).thenReturn(true); 121 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 122 } 123 124 @Test getAvailabilityStatus_powerSaveEnabled_returnDisableDependentSetting()125 public void getAvailabilityStatus_powerSaveEnabled_returnDisableDependentSetting() { 126 when(mController.isPowerSaveMode()).thenReturn(true); 127 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 128 } 129 130 @Test 131 @Config(shadows = { 132 ShadowDeviceStateRotationLockSettingsManager.class, 133 ShadowRotationPolicy.class 134 }) getAvailabilityStatus_deviceStateRotationLocked_returnDisableDependentSetting()135 public void getAvailabilityStatus_deviceStateRotationLocked_returnDisableDependentSetting() { 136 enableDeviceStateRotation(); 137 lockDeviceStateRotation(); 138 139 int availabilityStatus = mController.getAvailabilityStatus(); 140 141 assertThat(availabilityStatus).isEqualTo(DISABLED_DEPENDENT_SETTING); 142 } 143 144 @Test 145 @Config(shadows = { 146 ShadowDeviceStateRotationLockSettingsManager.class, 147 ShadowRotationPolicy.class 148 }) getAvailabilityStatus_deviceStateRotationUnlocked_returnAvailable()149 public void getAvailabilityStatus_deviceStateRotationUnlocked_returnAvailable() { 150 enableDeviceStateRotation(); 151 unlockDeviceStateRotation(); 152 153 int availabilityStatus = mController.getAvailabilityStatus(); 154 155 assertThat(availabilityStatus).isEqualTo(AVAILABLE); 156 } 157 enableAutoRotation()158 private void enableAutoRotation() { 159 Settings.System.putIntForUser(mContentResolver, 160 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT); 161 } 162 disableAutoRotation()163 private void disableAutoRotation() { 164 Settings.System.putIntForUser(mContentResolver, 165 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT); 166 } 167 enableDeviceStateRotation()168 private void enableDeviceStateRotation() { 169 ShadowRotationPolicy.setRotationSupported(true); 170 ShadowDeviceStateRotationLockSettingsManager.setDeviceStateRotationLockEnabled(true); 171 } 172 lockDeviceStateRotation()173 private void lockDeviceStateRotation() { 174 mDeviceStateAutoRotateSettingsManager.updateSetting( 175 /* deviceState= */0, /* rotationLocked= */ true); 176 mDeviceStateAutoRotateSettingsManager.updateSetting( 177 /* deviceState= */1, /* rotationLocked= */ true); 178 } 179 unlockDeviceStateRotation()180 private void unlockDeviceStateRotation() { 181 mDeviceStateAutoRotateSettingsManager.updateSetting( 182 /* deviceState= */0, /* rotationLocked= */ false); 183 mDeviceStateAutoRotateSettingsManager.updateSetting( 184 /* deviceState= */1, /* rotationLocked= */ true); 185 } 186 } 187