• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.UNSUPPORTED_ON_DEVICE;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.verify;
25 
26 import android.app.settings.SettingsEnums;
27 import android.content.Context;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceManager;
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settings.R;
34 import com.android.settings.testutils.shadow.ShadowDeviceStateRotationLockSettingsManager;
35 import com.android.settings.testutils.shadow.ShadowRotationPolicy;
36 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
37 import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager;
38 import com.android.settingslib.search.SearchIndexableRaw;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.robolectric.RobolectricTestRunner;
46 import org.robolectric.RuntimeEnvironment;
47 import org.robolectric.annotation.Config;
48 
49 import java.util.ArrayList;
50 import java.util.List;
51 
52 @RunWith(RobolectricTestRunner.class)
53 @Config(shadows = {
54         ShadowRotationPolicy.class,
55         ShadowDeviceStateRotationLockSettingsManager.class
56 })
57 public class DeviceStateAutoRotateSettingControllerTest {
58 
59     private static final int DEFAULT_DEVICE_STATE = 1;
60     private static final String DEFAULT_DEVICE_STATE_DESCRIPTION = "Device state description";
61     private static final int DEFAULT_ORDER = -10;
62 
63     private final Context mContext = RuntimeEnvironment.application;
64     private final DeviceStateRotationLockSettingsManager mAutoRotateSettingsManager =
65             DeviceStateRotationLockSettingsManager.getInstance(mContext);
66 
67     @Mock private MetricsFeatureProvider mMetricsFeatureProvider;
68 
69     private DeviceStateAutoRotateSettingController mController;
70 
71     @Before
setUp()72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74 
75         mController = new DeviceStateAutoRotateSettingController(
76                 mContext,
77                 DEFAULT_DEVICE_STATE,
78                 DEFAULT_DEVICE_STATE_DESCRIPTION,
79                 DEFAULT_ORDER,
80                 mMetricsFeatureProvider
81         );
82     }
83 
84     @Test
displayPreference_addsPreferenceToPreferenceScreen()85     public void displayPreference_addsPreferenceToPreferenceScreen() {
86         PreferenceScreen screen = new PreferenceManager(mContext).createPreferenceScreen(mContext);
87 
88         mController.displayPreference(screen);
89 
90         assertThat(screen.getPreferenceCount()).isEqualTo(1);
91         Preference preference = screen.getPreference(0);
92         assertThat(preference.getTitle().toString()).isEqualTo(DEFAULT_DEVICE_STATE_DESCRIPTION);
93         assertThat(preference.getOrder()).isEqualTo(DEFAULT_ORDER);
94         assertThat(preference.getKey()).isEqualTo(mController.getPreferenceKey());
95     }
96 
97     @Test
getAvailabilityStatus_rotationAndDeviceStateRotationEnabled_returnsAvailable()98     public void getAvailabilityStatus_rotationAndDeviceStateRotationEnabled_returnsAvailable() {
99         ShadowRotationPolicy.setRotationSupported(true);
100         ShadowDeviceStateRotationLockSettingsManager.setDeviceStateRotationLockEnabled(true);
101 
102         int availability = mController.getAvailabilityStatus();
103 
104         assertThat(availability).isEqualTo(AVAILABLE);
105     }
106 
107     @Test
getAvailabilityStatus_deviceStateRotationDisabled_returnsUnsupported()108     public void getAvailabilityStatus_deviceStateRotationDisabled_returnsUnsupported() {
109         ShadowRotationPolicy.setRotationSupported(true);
110         ShadowDeviceStateRotationLockSettingsManager.setDeviceStateRotationLockEnabled(false);
111 
112         int availability = mController.getAvailabilityStatus();
113 
114         assertThat(availability).isEqualTo(UNSUPPORTED_ON_DEVICE);
115     }
116 
117     @Test
getAvailabilityStatus_rotationDisabled_returnsUnsupported()118     public void getAvailabilityStatus_rotationDisabled_returnsUnsupported() {
119         ShadowRotationPolicy.setRotationSupported(false);
120         ShadowDeviceStateRotationLockSettingsManager.setDeviceStateRotationLockEnabled(true);
121 
122         int availability = mController.getAvailabilityStatus();
123 
124         assertThat(availability).isEqualTo(UNSUPPORTED_ON_DEVICE);
125     }
126 
127     @Test
getPreferenceKey_returnsKeyBasedOnDeviceState()128     public void getPreferenceKey_returnsKeyBasedOnDeviceState() {
129         String key = mController.getPreferenceKey();
130 
131         String expectedKey = "auto_rotate_device_state_" + DEFAULT_DEVICE_STATE;
132         assertThat(key).isEqualTo(expectedKey);
133     }
134 
135     @Test
isChecked_settingForStateIsUnlocked_returnsTrue()136     public void isChecked_settingForStateIsUnlocked_returnsTrue() {
137         mAutoRotateSettingsManager.updateSetting(DEFAULT_DEVICE_STATE, /* rotationLocked= */ false);
138 
139         assertThat(mController.isChecked()).isTrue();
140     }
141 
142     @Test
isChecked_settingForStateIsLocked_returnsFalse()143     public void isChecked_settingForStateIsLocked_returnsFalse() {
144         mAutoRotateSettingsManager.updateSetting(DEFAULT_DEVICE_STATE, /* rotationLocked= */ true);
145 
146         assertThat(mController.isChecked()).isFalse();
147     }
148 
149     @Test
setChecked_true_deviceStateSettingIsUnlocked()150     public void setChecked_true_deviceStateSettingIsUnlocked() {
151         mController.setChecked(true);
152 
153         boolean rotationLocked = mAutoRotateSettingsManager.isRotationLocked(DEFAULT_DEVICE_STATE);
154 
155         assertThat(rotationLocked).isFalse();
156     }
157 
158     @Test
setChecked_false_deviceStateSettingIsLocked()159     public void setChecked_false_deviceStateSettingIsLocked() {
160         mController.setChecked(false);
161 
162         boolean rotationLocked = mAutoRotateSettingsManager.isRotationLocked(DEFAULT_DEVICE_STATE);
163 
164         assertThat(rotationLocked).isTrue();
165     }
166 
167     @Test
setChecked_true_logsDeviceStateBasedSettingOn()168     public void setChecked_true_logsDeviceStateBasedSettingOn() {
169         mController.setChecked(true);
170 
171         verify(mMetricsFeatureProvider).action(mContext,
172                 SettingsEnums.ACTION_ENABLE_AUTO_ROTATION_DEVICE_STATE, DEFAULT_DEVICE_STATE);
173     }
174 
175     @Test
setChecked_false_logsDeviceStateBasedSettingOff()176     public void setChecked_false_logsDeviceStateBasedSettingOff() {
177         mController.setChecked(false);
178 
179         verify(mMetricsFeatureProvider).action(mContext,
180                 SettingsEnums.ACTION_DISABLE_AUTO_ROTATION_DEVICE_STATE, DEFAULT_DEVICE_STATE);
181     }
182 
183     @Test
updateRawDataToIndex_addsItemToList()184     public void updateRawDataToIndex_addsItemToList() {
185         List<SearchIndexableRaw> rawData = new ArrayList<>();
186 
187         mController.updateRawDataToIndex(rawData);
188 
189         assertThat(rawData).hasSize(1);
190         SearchIndexableRaw item = rawData.get(0);
191         assertThat(item.key).isEqualTo(mController.getPreferenceKey());
192         assertThat(item.title).isEqualTo(DEFAULT_DEVICE_STATE_DESCRIPTION);
193         assertThat(item.screenTitle).isEqualTo(mContext.getString(R.string.accelerometer_title));
194     }
195 
196     @Test
getSliceHighlightMenuRes_returnsMenuKeyDisplay()197     public void getSliceHighlightMenuRes_returnsMenuKeyDisplay() {
198         int sliceHighlightMenuRes = mController.getSliceHighlightMenuRes();
199 
200         assertThat(sliceHighlightMenuRes).isEqualTo(R.string.menu_key_display);
201     }
202 
203     @Test
isSliceable_returnsTrue()204     public void isSliceable_returnsTrue() {
205         assertThat(mController.isSliceable()).isTrue();
206     }
207 
208     @Test
isPublicSlice_returnsTrue()209     public void isPublicSlice_returnsTrue() {
210         assertThat(mController.isPublicSlice()).isTrue();
211     }
212 }
213