• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ShadowRotationPolicy;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.robolectric.RobolectricTestRunner;
35 import org.robolectric.RuntimeEnvironment;
36 import org.robolectric.annotation.Config;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class LockScreenRotationPreferenceControllerTest {
40 
41     private Context mContext;
42     private SwitchPreference mPreference;
43     private LockScreenRotationPreferenceController mController;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = RuntimeEnvironment.application;
48         mPreference = new SwitchPreference(mContext);
49         mController = new LockScreenRotationPreferenceController(mContext, "lock_screen");
50     }
51 
52     @Test
53     @Config(shadows = {ShadowRotationPolicy.class})
getAvailabilityStatus_supportedRotation_shouldReturnAvailable()54     public void getAvailabilityStatus_supportedRotation_shouldReturnAvailable() {
55         ShadowRotationPolicy.setRotationSupported(true /* supported */);
56 
57         assertThat(mController.getAvailabilityStatus()).isEqualTo(
58                 BasePreferenceController.AVAILABLE);
59     }
60 
61     @Test
62     @Config(shadows = {ShadowRotationPolicy.class})
getAvailabilityStatus_unsupportedRotation_shouldReturnUnsupportedOnDevice()63     public void getAvailabilityStatus_unsupportedRotation_shouldReturnUnsupportedOnDevice() {
64         ShadowRotationPolicy.setRotationSupported(false /* supported */);
65 
66         assertThat(mController.getAvailabilityStatus()).isEqualTo(
67                 BasePreferenceController.UNSUPPORTED_ON_DEVICE);
68     }
69 
70     @Test
71     @Config(shadows = {ShadowRotationPolicy.class})
setChecked_enabled()72     public void setChecked_enabled() {
73         mController.setChecked(true /* isChecked */);
74 
75         assertThat(mController.isChecked()).isTrue();
76         assertThat(RotationPolicy.isRotationLocked(mContext)).isFalse();
77     }
78 
79     @Test
80     @Config(shadows = {ShadowRotationPolicy.class})
setChecked_disabled()81     public void setChecked_disabled() {
82         mController.setChecked(false /* isChecked */);
83 
84         assertThat(mController.isChecked()).isFalse();
85         assertThat(RotationPolicy.isRotationLocked(mContext)).isTrue();
86     }
87 
88     @Test
updateState_settingIsOn_shouldTurnOnToggle()89     public void updateState_settingIsOn_shouldTurnOnToggle() {
90         Settings.System.putIntForUser(mContext.getContentResolver(),
91                 Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);
92 
93         mController.updateState(mPreference);
94 
95         assertThat(mPreference.isChecked()).isTrue();
96     }
97 
98     @Test
updateState_settingIsOff_shouldTurnOffToggle()99     public void updateState_settingIsOff_shouldTurnOffToggle() {
100         Settings.System.putIntForUser(mContext.getContentResolver(),
101                 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT);
102 
103         mController.updateState(mPreference);
104 
105         assertThat(mPreference.isChecked()).isFalse();
106     }
107 }
108