1 /*
2  * Copyright (C) 2019 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.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.os.UserHandle;
23 import android.provider.Settings;
24 
25 import androidx.preference.SwitchPreference;
26 
27 import com.android.internal.view.RotationPolicy;
28 import com.android.settings.core.BasePreferenceController;
29 import com.android.settings.testutils.shadow.ShadowDeviceStateRotationLockSettingsManager;
30 import com.android.settings.testutils.shadow.ShadowRotationPolicy;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 import org.robolectric.annotation.Config;
38 
39 @RunWith(RobolectricTestRunner.class)
40 @Config(shadows = {
41         com.android.settings.testutils.shadow.ShadowSystemSettings.class,
42 })
43 public class LockScreenRotationPreferenceControllerTest {
44 
45     private Context mContext;
46     private SwitchPreference mPreference;
47     private LockScreenRotationPreferenceController mController;
48 
49     @Before
setUp()50     public void setUp() {
51         mContext = RuntimeEnvironment.application;
52         mPreference = new SwitchPreference(mContext);
53         mController = new LockScreenRotationPreferenceController(mContext, "lock_screen");
54     }
55 
56     @Test
57     @Config(shadows = {
58             ShadowRotationPolicy.class,
59             ShadowDeviceStateRotationLockSettingsManager.class
60     })
getAvailabilityStatus_supportedRotation_shouldReturnAvailable()61     public void getAvailabilityStatus_supportedRotation_shouldReturnAvailable() {
62         ShadowRotationPolicy.setRotationSupported(true /* supported */);
63 
64         assertThat(mController.getAvailabilityStatus()).isEqualTo(
65                 BasePreferenceController.AVAILABLE);
66     }
67 
68     @Test
69     @Config(shadows = {
70             ShadowRotationPolicy.class,
71             ShadowDeviceStateRotationLockSettingsManager.class
72     })
getAvailabilityStatus_deviceStateRotationEnabled_returnsUnsupported()73     public void getAvailabilityStatus_deviceStateRotationEnabled_returnsUnsupported() {
74         ShadowRotationPolicy.setRotationSupported(true /* supported */);
75         ShadowDeviceStateRotationLockSettingsManager.setDeviceStateRotationLockEnabled(true);
76 
77         assertThat(mController.getAvailabilityStatus()).isEqualTo(
78                 BasePreferenceController.UNSUPPORTED_ON_DEVICE);
79     }
80 
81     @Test
82     @Config(shadows = {
83             ShadowRotationPolicy.class,
84             ShadowDeviceStateRotationLockSettingsManager.class
getAvailabilityStatus_unsupportedRotation_shouldReturnUnsupportedOnDevice()85     })    public void getAvailabilityStatus_unsupportedRotation_shouldReturnUnsupportedOnDevice() {
86         ShadowRotationPolicy.setRotationSupported(false /* supported */);
87 
88         assertThat(mController.getAvailabilityStatus()).isEqualTo(
89                 BasePreferenceController.UNSUPPORTED_ON_DEVICE);
90     }
91 
92     @Test
93     @Config(shadows = {ShadowRotationPolicy.class})
setChecked_enabled()94     public void setChecked_enabled() {
95         mController.setChecked(true /* isChecked */);
96 
97         assertThat(mController.isChecked()).isTrue();
98         assertThat(RotationPolicy.isRotationLocked(mContext)).isFalse();
99     }
100 
101     @Test
102     @Config(shadows = {ShadowRotationPolicy.class})
setChecked_disabled()103     public void setChecked_disabled() {
104         mController.setChecked(false /* isChecked */);
105 
106         assertThat(mController.isChecked()).isFalse();
107         assertThat(RotationPolicy.isRotationLocked(mContext)).isTrue();
108     }
109 
110     @Test
updateState_settingIsOn_shouldTurnOnToggle()111     public void updateState_settingIsOn_shouldTurnOnToggle() {
112         Settings.System.putIntForUser(mContext.getContentResolver(),
113                 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);
114 
115         mController.updateState(mPreference);
116 
117         assertThat(mPreference.isChecked()).isTrue();
118     }
119 
120     @Test
updateState_settingIsOff_shouldTurnOffToggle()121     public void updateState_settingIsOff_shouldTurnOffToggle() {
122         Settings.System.putIntForUser(mContext.getContentResolver(),
123                 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
124 
125         mController.updateState(mPreference);
126 
127         assertThat(mPreference.isChecked()).isFalse();
128     }
129 }
130