• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.display.SmartAutoRotatePreferenceFragment.AUTO_ROTATE_MAIN_SWITCH_PREFERENCE_KEY;
20 import static com.android.settings.display.SmartAutoRotatePreferenceFragment.AUTO_ROTATE_SWITCH_PREFERENCE_KEY;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyInt;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32 
33 import android.Manifest;
34 import android.content.ContentResolver;
35 import android.content.Context;
36 import android.content.pm.PackageManager;
37 import android.content.pm.ResolveInfo;
38 import android.content.pm.ServiceInfo;
39 import android.content.res.Resources;
40 import android.view.View;
41 
42 import androidx.preference.Preference;
43 
44 import com.android.settings.R;
45 import com.android.settings.SettingsActivity;
46 import com.android.settings.testutils.ResolveInfoBuilder;
47 import com.android.settings.testutils.shadow.ShadowDeviceStateRotationLockSettingsManager;
48 import com.android.settings.testutils.shadow.ShadowRotationPolicy;
49 import com.android.settingslib.core.AbstractPreferenceController;
50 import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManager;
51 
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55 import org.mockito.Mock;
56 import org.mockito.MockitoAnnotations;
57 import org.robolectric.RobolectricTestRunner;
58 import org.robolectric.RuntimeEnvironment;
59 import org.robolectric.annotation.Config;
60 
61 import java.util.List;
62 
63 @RunWith(RobolectricTestRunner.class)
64 @Config(shadows = {
65         ShadowDeviceStateRotationLockSettingsManager.class,
66         ShadowRotationPolicy.class
67 })
68 public class SmartAutoRotatePreferenceFragmentTest {
69 
70     private static final String PACKAGE_NAME = "package_name";
71 
72     private SmartAutoRotatePreferenceFragment mFragment;
73 
74     @Mock
75     private PackageManager mPackageManager;
76 
77     @Mock
78     private View mView;
79 
80     @Mock
81     private SettingsActivity mActivity;
82 
83     @Mock
84     private Preference mRotateSwitchPreference;
85     private Resources mResources;
86     private Context mContext;
87 
88     @Mock
89     private Preference mRotateMainSwitchPreference;
90 
91     @Before
setUp()92     public void setUp() {
93         MockitoAnnotations.initMocks(this);
94 
95         mContext = spy(RuntimeEnvironment.application);
96         ContentResolver mContentResolver = RuntimeEnvironment.application.getContentResolver();
97         when(mContext.getPackageManager()).thenReturn(mPackageManager);
98         when(mContext.getContentResolver()).thenReturn(mContentResolver);
99         doReturn(PACKAGE_NAME).when(mPackageManager).getRotationResolverPackageName();
100         doReturn(PackageManager.PERMISSION_GRANTED).when(mPackageManager).checkPermission(
101                 Manifest.permission.CAMERA, PACKAGE_NAME);
102 
103         mResources = spy(mContext.getResources());
104         when(mContext.getResources()).thenReturn(mResources);
105 
106         final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build();
107         resolveInfo.serviceInfo = new ServiceInfo();
108         when(mPackageManager.resolveService(any(), anyInt())).thenReturn(resolveInfo);
109 
110         mFragment = spy(new SmartAutoRotatePreferenceFragment());
111         when(mActivity.getPackageManager()).thenReturn(mPackageManager);
112         when(mFragment.getActivity()).thenReturn(mActivity);
113         when(mFragment.getContext()).thenReturn(mContext);
114         when(mActivity.getResources()).thenReturn(mResources);
115 
116         doReturn(mView).when(mFragment).getView();
117 
118         when(mFragment.findPreference(AUTO_ROTATE_SWITCH_PREFERENCE_KEY)).thenReturn(
119                 mRotateSwitchPreference);
120 
121 
122         when(mFragment.findPreference(AUTO_ROTATE_MAIN_SWITCH_PREFERENCE_KEY)).thenReturn(
123                 mRotateMainSwitchPreference);
124     }
125 
126 
127     @Test
createHeader_faceDetectionSupported_switchBarIsEnabled()128     public void createHeader_faceDetectionSupported_switchBarIsEnabled() {
129         ShadowDeviceStateRotationLockSettingsManager.setDeviceStateRotationLockEnabled(false);
130         mFragment.createHeader(mActivity);
131 
132         verify(mRotateMainSwitchPreference, never()).setVisible(false);
133         verify(mRotateSwitchPreference, times(1)).setVisible(false);
134     }
135 
136     @Test
createHeader_deviceStateRotationSupported_switchBarIsDisabled()137     public void createHeader_deviceStateRotationSupported_switchBarIsDisabled() {
138         ShadowRotationPolicy.setRotationSupported(true);
139         ShadowDeviceStateRotationLockSettingsManager.setDeviceStateRotationLockEnabled(true);
140 
141         mFragment.createHeader(mActivity);
142 
143         verify(mRotateMainSwitchPreference, times(1)).setVisible(false);
144         verify(mRotateSwitchPreference, never()).setVisible(false);
145     }
146 
147     @Test
createHeader_faceDetectionUnSupported_switchBarIsDisabled()148     public void createHeader_faceDetectionUnSupported_switchBarIsDisabled() {
149         doReturn(null).when(mPackageManager).getRotationResolverPackageName();
150 
151         mFragment.createHeader(mActivity);
152 
153         verify(mRotateMainSwitchPreference, times(1)).setVisible(false);
154         verify(mRotateSwitchPreference, never()).setVisible(false);
155     }
156 
157     @Test
createHeader_faceDetectionNotEnabledByConfig_switchBarIsDisabled()158     public void createHeader_faceDetectionNotEnabledByConfig_switchBarIsDisabled() {
159         doReturn(false).when(mResources).getBoolean(
160                 R.bool.config_auto_rotate_face_detection_available);
161 
162         mFragment.createHeader(mActivity);
163 
164         verify(mRotateMainSwitchPreference, times(1)).setVisible(false);
165         verify(mRotateSwitchPreference, never()).setVisible(false);
166     }
167 
168     @Test
createPreferenceControllers_noSettableDeviceStates_returnsEmptyList()169     public void createPreferenceControllers_noSettableDeviceStates_returnsEmptyList() {
170         enableDeviceStateSettableRotationStates(new String[]{}, new String[]{});
171 
172         List<AbstractPreferenceController> preferenceControllers =
173                 mFragment.createPreferenceControllers(mContext);
174 
175         assertThat(preferenceControllers).isEmpty();
176     }
177 
178     @Test
createPreferenceControllers_settableDeviceStates_returnsDeviceStateControllers()179     public void createPreferenceControllers_settableDeviceStates_returnsDeviceStateControllers() {
180         enableDeviceStateSettableRotationStates(new String[]{"0:1", "1:1"},
181                 new String[]{"Folded", "Unfolded"});
182 
183         List<AbstractPreferenceController> preferenceControllers =
184                 mFragment.createPreferenceControllers(mContext);
185 
186         assertThat(preferenceControllers).hasSize(2);
187         assertThat(preferenceControllers.get(0)).isInstanceOf(
188                 DeviceStateAutoRotateSettingController.class);
189         assertThat(preferenceControllers.get(1)).isInstanceOf(
190                 DeviceStateAutoRotateSettingController.class);
191     }
192 
193     @Test
setupFooter_linkAddedWhenAppropriate()194     public void setupFooter_linkAddedWhenAppropriate() {
195         doReturn("").when(mFragment).getText(anyInt());
196         doReturn("").when(mFragment).getString(anyInt());
197         mFragment.setupFooter();
198         verify(mFragment, never()).addHelpLink();
199 
200         doReturn("testString").when(mFragment).getText(anyInt());
201         doReturn("testString").when(mFragment).getString(anyInt());
202         mFragment.setupFooter();
203         verify(mFragment, times(1)).addHelpLink();
204     }
205 
enableDeviceStateSettableRotationStates(String[] settableStates, String[] settableStatesDescriptions)206     private void enableDeviceStateSettableRotationStates(String[] settableStates,
207             String[] settableStatesDescriptions) {
208         when(mResources.getStringArray(
209                 com.android.internal.R.array.config_perDeviceStateRotationLockDefaults)).thenReturn(
210                 settableStates);
211         when(mResources.getStringArray(
212                 R.array.config_settableAutoRotationDeviceStatesDescriptions)).thenReturn(
213                 settableStatesDescriptions);
214         when(mResources.getBoolean(R.bool.config_auto_rotate_face_detection_available)).thenReturn(
215                 true);
216         DeviceStateRotationLockSettingsManager.resetInstance();
217         DeviceStateRotationLockSettingsManager.getInstance(mContext)
218                 .resetStateForTesting(mResources);
219     }
220 }
221