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 android.provider.Settings.Secure.CAMERA_AUTOROTATE;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.when;
28 
29 import android.Manifest;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.content.pm.PackageManager;
33 import android.content.pm.ResolveInfo;
34 import android.content.pm.ServiceInfo;
35 import android.content.res.Resources;
36 import android.os.UserHandle;
37 import android.provider.Settings;
38 
39 import com.android.settings.R;
40 import com.android.settings.core.BasePreferenceController;
41 import com.android.settings.testutils.FakeFeatureFactory;
42 import com.android.settings.testutils.ResolveInfoBuilder;
43 import com.android.settings.testutils.shadow.ShadowSensorPrivacyManager;
44 
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.Mock;
49 import org.mockito.Mockito;
50 import org.mockito.MockitoAnnotations;
51 import org.robolectric.RobolectricTestRunner;
52 import org.robolectric.RuntimeEnvironment;
53 import org.robolectric.annotation.Config;
54 
55 @RunWith(RobolectricTestRunner.class)
56 @Config(shadows = ShadowSensorPrivacyManager.class)
57 public class SmartAutoRotatePreferenceControllerTest {
58 
59     private static final String PACKAGE_NAME = "package_name";
60     @Mock
61     private PackageManager mPackageManager;
62     @Mock
63     private Resources mResources;
64     private Context mContext;
65     private ContentResolver mContentResolver;
66     private SmartAutoRotatePreferenceController mController;
67 
68     @Before
setUp()69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71         mContext = Mockito.spy(RuntimeEnvironment.application);
72         FakeFeatureFactory.setupForTest();
73         mContentResolver = RuntimeEnvironment.application.getContentResolver();
74 
75         when(mContext.getPackageManager()).thenReturn(mPackageManager);
76         when(mContext.getResources()).thenReturn(mResources);
77         when(mContext.getContentResolver()).thenReturn(mContentResolver);
78 
79         doReturn(PACKAGE_NAME).when(mPackageManager).getRotationResolverPackageName();
80         doReturn(PackageManager.PERMISSION_GRANTED).when(mPackageManager).checkPermission(
81                 Manifest.permission.CAMERA, PACKAGE_NAME);
82         when(mContext.getString(R.string.auto_rotate_option_off))
83                 .thenReturn("Off");
84         when(mContext.getString(R.string.auto_rotate_option_on))
85                 .thenReturn("On");
86         when(mContext.getString(R.string.auto_rotate_option_face_based))
87                 .thenReturn("On - Face-based");
88 
89         disableCameraBasedRotation();
90         final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build();
91         resolveInfo.serviceInfo = new ServiceInfo();
92         when(mPackageManager.resolveService(any(), anyInt())).thenReturn(resolveInfo);
93 
94         mController = Mockito.spy(
95                 new SmartAutoRotatePreferenceController(mContext, "smart_auto_rotate"));
96         when(mController.isCameraLocked()).thenReturn(false);
97         when(mController.isPowerSaveMode()).thenReturn(false);
98     }
99 
100     @Test
isAvailableWhenPolicyAllows()101     public void isAvailableWhenPolicyAllows() {
102         assertThat(mController.isAvailable()).isFalse();
103 
104         enableAutoRotationPreference();
105 
106         assertThat(mController.isAvailable()).isTrue();
107     }
108 
109     @Test
getSummary_settingsIsOff_returnsOff()110     public void getSummary_settingsIsOff_returnsOff() {
111         disableAutoRotation();
112 
113         assertThat(mController.getSummary()).isEqualTo("Off");
114     }
115 
116     @Test
getSummary_settingsIsOn_returnsOn()117     public void getSummary_settingsIsOn_returnsOn() {
118         enableAutoRotation();
119 
120         assertThat(mController.getSummary()).isEqualTo("On");
121     }
122 
123     @Test
getSummary_autoRotateOffSmartAutoRotateOn_returnsOff()124     public void getSummary_autoRotateOffSmartAutoRotateOn_returnsOff() {
125         enableCameraBasedRotation();
126         disableAutoRotation();
127 
128         assertThat(mController.getSummary()).isEqualTo("Off");
129     }
130 
131     @Test
updatePreference_smartAutoRotateOn_returnsFaceBased()132     public void updatePreference_smartAutoRotateOn_returnsFaceBased() {
133         enableCameraBasedRotation();
134         enableAutoRotation();
135 
136         assertThat(mController.getSummary()).isEqualTo("On - Face-based");
137     }
138 
139     @Test
getSummary_noSmartAuto_returnsOff()140     public void getSummary_noSmartAuto_returnsOff() {
141         disableAutoRotation();
142         Settings.Secure.putStringForUser(mContentResolver,
143                 CAMERA_AUTOROTATE, null, UserHandle.USER_CURRENT);
144 
145         assertThat(mController.getSummary()).isEqualTo("Off");
146 
147     }
148 
149     @Test
getSummary_noSmartAuto_returnsOn()150     public void getSummary_noSmartAuto_returnsOn() {
151         enableAutoRotation();
152         Settings.Secure.putStringForUser(mContentResolver,
153                 CAMERA_AUTOROTATE, null, UserHandle.USER_CURRENT);
154 
155         assertThat(mController.getSummary()).isEqualTo("On");
156     }
157 
158     @Test
getSummary_noCameraPermission_returnsOn()159     public void getSummary_noCameraPermission_returnsOn() {
160         enableAutoRotation();
161         enableCameraBasedRotation();
162         doReturn(PackageManager.PERMISSION_DENIED).when(mPackageManager).checkPermission(
163                 Manifest.permission.CAMERA, PACKAGE_NAME);
164 
165         assertThat(mController.getSummary()).isEqualTo("On");
166     }
167 
168     @Test
getSummary_cameraDisabled_returnsOn()169     public void getSummary_cameraDisabled_returnsOn() {
170         enableAutoRotation();
171         enableCameraBasedRotation();
172         when(mController.isCameraLocked()).thenReturn(true);
173 
174         assertThat(mController.getSummary()).isEqualTo("On");
175     }
176 
177     @Test
getSummary_powerSaveEnabled_returnsOn()178     public void getSummary_powerSaveEnabled_returnsOn() {
179         enableAutoRotation();
180         enableCameraBasedRotation();
181         when(mController.isPowerSaveMode()).thenReturn(true);
182 
183         assertThat(mController.getSummary()).isEqualTo("On");
184     }
185 
186     @Test
testGetAvailabilityStatus()187     public void testGetAvailabilityStatus() {
188         assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController
189                 .UNSUPPORTED_ON_DEVICE);
190 
191         enableAutoRotationPreference();
192 
193         assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController
194                 .AVAILABLE);
195 
196         disableAutoRotationPreference();
197 
198         assertThat(mController.getAvailabilityStatus()).isEqualTo(BasePreferenceController
199                 .UNSUPPORTED_ON_DEVICE);
200     }
201 
202     @Test
isSliceableCorrectKey_returnsTrue()203     public void isSliceableCorrectKey_returnsTrue() {
204         final AutoRotatePreferenceController controller =
205                 new AutoRotatePreferenceController(mContext, "auto_rotate");
206         assertThat(controller.isSliceable()).isTrue();
207     }
208 
209     @Test
isSliceableIncorrectKey_returnsFalse()210     public void isSliceableIncorrectKey_returnsFalse() {
211         final AutoRotatePreferenceController controller =
212                 new AutoRotatePreferenceController(mContext, "bad_key");
213         assertThat(controller.isSliceable()).isFalse();
214     }
215 
enableAutoRotationPreference()216     private void enableAutoRotationPreference() {
217         when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
218         when(mResources.getBoolean(anyInt())).thenReturn(true);
219         Settings.System.putInt(mContentResolver,
220                 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 0);
221     }
222 
disableAutoRotationPreference()223     private void disableAutoRotationPreference() {
224         when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
225         when(mResources.getBoolean(anyInt())).thenReturn(true);
226         Settings.System.putInt(mContentResolver,
227                 Settings.System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, 1);
228     }
229 
enableAutoRotation()230     private void enableAutoRotation() {
231         Settings.System.putIntForUser(mContentResolver,
232                 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);
233     }
234 
disableAutoRotation()235     private void disableAutoRotation() {
236         Settings.System.putIntForUser(mContentResolver,
237                 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
238     }
239 
enableCameraBasedRotation()240     private void enableCameraBasedRotation() {
241         Settings.Secure.putIntForUser(mContentResolver,
242                 CAMERA_AUTOROTATE, 1, UserHandle.USER_CURRENT);
243     }
244 
disableCameraBasedRotation()245     private void disableCameraBasedRotation() {
246         Settings.Secure.putIntForUser(mContentResolver,
247                 CAMERA_AUTOROTATE, 0, UserHandle.USER_CURRENT);
248     }
249 }
250