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.ShadowSensorPrivacyManager; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Mock; 48 import org.mockito.Mockito; 49 import org.mockito.MockitoAnnotations; 50 import org.robolectric.RobolectricTestRunner; 51 import org.robolectric.RuntimeEnvironment; 52 import org.robolectric.annotation.Config; 53 54 @RunWith(RobolectricTestRunner.class) 55 @Config(shadows = ShadowSensorPrivacyManager.class) 56 public class SmartAutoRotateControllerTest { 57 58 private static final String PACKAGE_NAME = "package_name"; 59 60 private SmartAutoRotateController mController; 61 @Mock 62 private PackageManager mPackageManager; 63 @Mock 64 private Preference mPreference; 65 private ContentResolver mContentResolver; 66 67 @Before setUp()68 public void setUp() { 69 MockitoAnnotations.initMocks(this); 70 final Context context = Mockito.spy(RuntimeEnvironment.application); 71 mContentResolver = RuntimeEnvironment.application.getContentResolver(); 72 when(context.getPackageManager()).thenReturn(mPackageManager); 73 when(context.getContentResolver()).thenReturn(mContentResolver); 74 doReturn(PACKAGE_NAME).when(mPackageManager).getRotationResolverPackageName(); 75 doReturn(PackageManager.PERMISSION_GRANTED).when(mPackageManager).checkPermission( 76 Manifest.permission.CAMERA, PACKAGE_NAME); 77 mController = Mockito.spy(new SmartAutoRotateController(context, "test_key")); 78 when(mController.isCameraLocked()).thenReturn(false); 79 when(mController.isPowerSaveMode()).thenReturn(false); 80 doReturn(mController.getPreferenceKey()).when(mPreference).getKey(); 81 82 final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build(); 83 resolveInfo.serviceInfo = new ServiceInfo(); 84 when(mPackageManager.resolveService(any(), anyInt())).thenReturn(resolveInfo); 85 enableAutoRotation(); 86 } 87 88 @Test getAvailabilityStatus_returnAvailable()89 public void getAvailabilityStatus_returnAvailable() { 90 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); 91 } 92 93 @Test getAvailabilityStatus_resolveInfoIsNull_returnUnsupportedOnDevice()94 public void getAvailabilityStatus_resolveInfoIsNull_returnUnsupportedOnDevice() { 95 when(mPackageManager.resolveService(any(), anyInt())).thenReturn(null); 96 assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); 97 } 98 99 @Test getAvailabilityStatus_noCameraPermission_returnDisableDependentSetting()100 public void getAvailabilityStatus_noCameraPermission_returnDisableDependentSetting() { 101 doReturn(PackageManager.PERMISSION_DENIED).when(mPackageManager).checkPermission( 102 Manifest.permission.CAMERA, PACKAGE_NAME); 103 104 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 105 } 106 107 @Test getAvailabilityStatus_rotationLocked_returnDisableDependentSetting()108 public void getAvailabilityStatus_rotationLocked_returnDisableDependentSetting() { 109 disableAutoRotation(); 110 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 111 } 112 113 @Test getAvailabilityStatus_cameraDisabled_returnDisableDependentSetting()114 public void getAvailabilityStatus_cameraDisabled_returnDisableDependentSetting() { 115 when(mController.isCameraLocked()).thenReturn(true); 116 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 117 } 118 119 @Test getAvailabilityStatus_powerSaveEnabled_returnDisableDependentSetting()120 public void getAvailabilityStatus_powerSaveEnabled_returnDisableDependentSetting() { 121 when(mController.isPowerSaveMode()).thenReturn(true); 122 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING); 123 } 124 enableAutoRotation()125 private void enableAutoRotation() { 126 Settings.System.putIntForUser(mContentResolver, 127 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT); 128 } 129 disableAutoRotation()130 private void disableAutoRotation() { 131 Settings.System.putIntForUser(mContentResolver, 132 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT); 133 } 134 } 135