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 android.provider.Settings.Secure.DEVICE_STATE_ROTATION_LOCK_LOCKED;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.when;
25 
26 import android.app.settings.SettingsEnums;
27 import android.content.Context;
28 import android.content.res.Resources;
29 
30 import com.android.settings.R;
31 import com.android.settingslib.core.AbstractPreferenceController;
32 import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 import java.util.List;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class DeviceStateAutoRotateDetailsFragmentTest {
44     private static final int FOLDED_STATE = 0;
45     private static final int HALF_FOLDED_STATE = 1;
46     private static final int UNFOLDED_STATE = 2;
47     private static final int REAR_DISPLAY_STATE = 3;
48 
49     private final DeviceStateAutoRotateDetailsFragment mFragment =
50             spy(new DeviceStateAutoRotateDetailsFragment());
51     private final Context mContext = spy(RuntimeEnvironment.application);
52     private final Resources mResources = spy(mContext.getResources());
53 
54     @Before
setUp()55     public void setUp() throws Exception {
56         when(mContext.getResources()).thenReturn(mResources);
57         when(mContext.getApplicationContext()).thenReturn(mContext);
58         when(mFragment.getContext()).thenReturn(mContext);
59         when(mFragment.getResources()).thenReturn(mResources);
60         setUpPostureMappings();
61     }
62 
63     @Test
getMetricsCategory_returnsAutoRotateSettings()64     public void getMetricsCategory_returnsAutoRotateSettings() {
65         assertThat(mFragment.getMetricsCategory()).isEqualTo(
66                 SettingsEnums.DISPLAY_DEVICE_STATE_AUTO_ROTATE_SETTINGS);
67     }
68 
69     @Test
getPreferenceScreenResId_returnsDeviceStateAutoRotationSettings()70     public void getPreferenceScreenResId_returnsDeviceStateAutoRotationSettings() {
71         assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(
72                 R.xml.device_state_auto_rotate_settings);
73     }
74 
75     @Test
createPreferenceControllers_settableDeviceStates_returnsDeviceStateControllers()76     public void createPreferenceControllers_settableDeviceStates_returnsDeviceStateControllers() {
77         enableDeviceStateSettableRotationStates(
78                 new String[]{FOLDED_STATE + ":" + DEVICE_STATE_ROTATION_LOCK_LOCKED,
79                         UNFOLDED_STATE + ":" + DEVICE_STATE_ROTATION_LOCK_LOCKED},
80                 new String[]{"Folded", "Unfolded"});
81 
82         List<AbstractPreferenceController> preferenceControllers =
83                 mFragment.createPreferenceControllers(mContext);
84 
85         assertThat(preferenceControllers).hasSize(2);
86         assertThat(preferenceControllers.get(0)).isInstanceOf(
87                 DeviceStateAutoRotateSettingController.class);
88         assertThat(preferenceControllers.get(1)).isInstanceOf(
89                 DeviceStateAutoRotateSettingController.class);
90     }
91 
92     @Test
createPreferenceControllers_noSettableDeviceStates_returnsEmptyList()93     public void createPreferenceControllers_noSettableDeviceStates_returnsEmptyList() {
94         enableDeviceStateSettableRotationStates(new String[]{}, new String[]{});
95 
96         List<AbstractPreferenceController> preferenceControllers =
97                 mFragment.createPreferenceControllers(mContext);
98 
99         assertThat(preferenceControllers).isEmpty();
100     }
101 
enableDeviceStateSettableRotationStates(String[] settableStates, String[] settableStatesDescriptions)102     private void enableDeviceStateSettableRotationStates(String[] settableStates,
103             String[] settableStatesDescriptions) {
104         when(mResources.getStringArray(
105                 com.android.internal.R.array.config_perDeviceStateRotationLockDefaults)).thenReturn(
106                 settableStates);
107         when(mResources.getStringArray(
108                 R.array.config_settableAutoRotationDeviceStatesDescriptions)).thenReturn(
109                 settableStatesDescriptions);
110         DeviceStateRotationLockSettingsManager.resetInstance();
111         DeviceStateRotationLockSettingsManager.getInstance(mContext)
112                 .resetStateForTesting(mResources);
113     }
114 
setUpPostureMappings()115     private void setUpPostureMappings() {
116         when(mResources.getIntArray(
117                 com.android.internal.R.array.config_foldedDeviceStates)).thenReturn(
118                 new int[]{FOLDED_STATE});
119         when(mResources.getIntArray(
120                 com.android.internal.R.array.config_halfFoldedDeviceStates)).thenReturn(
121                 new int[]{HALF_FOLDED_STATE});
122         when(mResources.getIntArray(
123                 com.android.internal.R.array.config_openDeviceStates)).thenReturn(
124                 new int[]{UNFOLDED_STATE});
125         when(mResources.getIntArray(
126                 com.android.internal.R.array.config_rearDisplayDeviceStates)).thenReturn(
127                 new int[]{REAR_DISPLAY_STATE});
128     }
129 }
130