• 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.display;
18 
19 import static android.provider.Settings.Secure.ADAPTIVE_SLEEP;
20 
21 import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
22 
23 import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyInt;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.ArgumentMatchers.isA;
31 import static org.mockito.Mockito.doReturn;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
35 
36 import android.content.ContentResolver;
37 import android.content.Context;
38 import android.content.Intent;
39 import android.content.pm.PackageManager;
40 import android.os.UserManager;
41 import android.provider.Settings;
42 
43 import androidx.preference.PreferenceScreen;
44 
45 import com.android.settings.bluetooth.RestrictionUtils;
46 import com.android.settings.testutils.shadow.ShadowSensorPrivacyManager;
47 import com.android.settingslib.RestrictedLockUtils;
48 
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.Mock;
53 import org.mockito.MockitoAnnotations;
54 import org.robolectric.RobolectricTestRunner;
55 import org.robolectric.annotation.Config;
56 
57 @RunWith(RobolectricTestRunner.class)
58 @Config(shadows = ShadowSensorPrivacyManager.class)
59 public class AdaptiveSleepPreferenceControllerTest {
60     private Context mContext;
61     private AdaptiveSleepPreferenceController mController;
62     private ContentResolver mContentResolver;
63 
64     @Mock
65     private PackageManager mPackageManager;
66     @Mock
67     private PreferenceScreen mScreen;
68     @Mock
69     private RestrictionUtils mRestrictionUtils;
70     @Mock
71     private RestrictedLockUtils.EnforcedAdmin mEnforcedAdmin;
72 
73     @Before
setUp()74     public void setUp() {
75         MockitoAnnotations.initMocks(this);
76         mContext = spy(getApplicationContext());
77         mContentResolver = mContext.getContentResolver();
78 
79         doReturn(mPackageManager).when(mContext).getPackageManager();
80         when(mPackageManager.getAttentionServicePackageName()).thenReturn("some.package");
81         when(mPackageManager.checkPermission(any(), any())).thenReturn(
82                 PackageManager.PERMISSION_GRANTED);
83         when(mRestrictionUtils.checkIfRestrictionEnforced(any(),
84                 eq(UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT))).thenReturn(null);
85 
86         mController = spy(new AdaptiveSleepPreferenceController(mContext, mRestrictionUtils));
87         mController.initializePreference();
88         when(mController.isCameraLocked()).thenReturn(false);
89         when(mController.isPowerSaveMode()).thenReturn(false);
90     }
91 
92     @Test
controlSetting_preferenceChecked_FeatureTurnOn()93     public void controlSetting_preferenceChecked_FeatureTurnOn() {
94         mController.mPreference.setChecked(false);
95 
96         mController.mPreference.performClick();
97 
98         int mode = Settings.Secure.getInt(mContentResolver, ADAPTIVE_SLEEP, 0);
99         assertThat(mode).isEqualTo(1);
100     }
101 
102     @Test
controlSetting_preferenceNotChecked_FeatureTurnOff()103     public void controlSetting_preferenceNotChecked_FeatureTurnOff() {
104         mController.mPreference.setChecked(true);
105 
106         mController.mPreference.performClick();
107 
108         int mode = Settings.Secure.getInt(mContentResolver, ADAPTIVE_SLEEP, 1);
109         assertThat(mode).isEqualTo(0);
110     }
111 
112     @Test
isControllerAvailable_serviceNotSupported_returnUnsupportedCode()113     public void isControllerAvailable_serviceNotSupported_returnUnsupportedCode() {
114         when(mPackageManager.resolveService(isA(Intent.class), anyInt())).thenReturn(null);
115 
116         assertThat(AdaptiveSleepPreferenceController.isControllerAvailable(mContext)).isEqualTo(
117                 UNSUPPORTED_ON_DEVICE);
118     }
119 
120     @Test
hasSufficientPermission_permissionGranted_returnTrue()121     public void hasSufficientPermission_permissionGranted_returnTrue() {
122         assertThat(AdaptiveSleepPreferenceController.hasSufficientPermission(
123                 mPackageManager)).isTrue();
124     }
125 
126     @Test
hasSufficientPermission_permissionNotGranted_returnFalse()127     public void hasSufficientPermission_permissionNotGranted_returnFalse() {
128         when(mPackageManager.checkPermission(any(), any())).thenReturn(
129                 PackageManager.PERMISSION_DENIED);
130 
131         assertThat(AdaptiveSleepPreferenceController.hasSufficientPermission(
132                 mPackageManager)).isFalse();
133     }
134 
135     @Test
addToScreen_normalCase_enablePreference()136     public void addToScreen_normalCase_enablePreference() {
137         mController.mPreference.setEnabled(false);
138         when(mPackageManager.checkPermission(any(), any())).thenReturn(
139                 PackageManager.PERMISSION_GRANTED);
140 
141         mController.addToScreen(mScreen);
142 
143         assertThat(mController.mPreference.isEnabled()).isTrue();
144         verify(mScreen).addPreference(mController.mPreference);
145     }
146 
147     @Test
addToScreen_permissionNotGranted_disablePreference()148     public void addToScreen_permissionNotGranted_disablePreference() {
149         mController.mPreference.setEnabled(true);
150         when(mPackageManager.checkPermission(any(), any())).thenReturn(
151                 PackageManager.PERMISSION_DENIED);
152 
153         mController.addToScreen(mScreen);
154 
155         assertThat(mController.mPreference.isEnabled()).isFalse();
156     }
157 
158     @Test
addToScreen_enforcedAdmin_disablePreference()159     public void addToScreen_enforcedAdmin_disablePreference() {
160         mController.mPreference.setEnabled(true);
161 
162         when(mRestrictionUtils.checkIfRestrictionEnforced(any(),
163                 eq(UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT))).thenReturn(mEnforcedAdmin);
164 
165         mController.addToScreen(mScreen);
166 
167         assertThat(mController.mPreference.isEnabled()).isFalse();
168     }
169 
170     @Test
addToScreen_cameraIsLocked_disablePreference()171     public void addToScreen_cameraIsLocked_disablePreference() {
172         when(mController.isCameraLocked()).thenReturn(true);
173 
174         mController.addToScreen(mScreen);
175 
176         assertThat(mController.mPreference.isEnabled()).isFalse();
177     }
178 
179     @Test
addToScreen_powerSaveEnabled_disablePreference()180     public void addToScreen_powerSaveEnabled_disablePreference() {
181         when(mController.isPowerSaveMode()).thenReturn(true);
182 
183         mController.addToScreen(mScreen);
184 
185         assertThat(mController.mPreference.isEnabled()).isFalse();
186     }
187 
188 }
189