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