1 /*
2  * Copyright (C) 2017 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.development;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.never;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.ContentResolver;
28 import android.content.Context;
29 import android.provider.Settings;
30 
31 import androidx.preference.PreferenceScreen;
32 
33 import com.android.settingslib.RestrictedLockUtils;
34 import com.android.settingslib.RestrictedSwitchPreference;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public class StayAwakePreferenceControllerTest {
48 
49     @Mock
50     private Context mContext;
51     @Mock
52     private RestrictedSwitchPreference mPreference;
53     @Mock
54     private PreferenceScreen mPreferenceScreen;
55     @Mock
56     private Lifecycle mLifecycle;
57     private ContentResolver mContentResolver;
58     private StayAwakePreferenceController mController;
59 
60     @Before
setup()61     public void setup() {
62         MockitoAnnotations.initMocks(this);
63         mContentResolver = RuntimeEnvironment.application.getContentResolver();
64         mController = new StayAwakePreferenceController(mContext, mLifecycle);
65         when(mContext.getContentResolver()).thenReturn(mContentResolver);
66         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
67                 mPreference);
68         mController.displayPreference(mPreferenceScreen);
69     }
70 
71     @Test
onPreferenceChanged_turnOnStayAwake()72     public void onPreferenceChanged_turnOnStayAwake() {
73         mController.onPreferenceChange(null, true);
74 
75         final int mode = Settings.Global.getInt(mContentResolver,
76                 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, -1);
77 
78         assertThat(mode).isEqualTo(StayAwakePreferenceController.SETTING_VALUE_ON);
79     }
80 
81     @Test
onPreferenceChanged_turnOffStayAwake()82     public void onPreferenceChanged_turnOffStayAwake() {
83         mController.onPreferenceChange(null, false);
84 
85         final int mode = Settings.Global.getInt(mContentResolver,
86                 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, -1);
87 
88         assertThat(mode).isEqualTo(StayAwakePreferenceController.SETTING_VALUE_OFF);
89     }
90 
91     @Test
updateState_preferenceShouldBeChecked()92     public void updateState_preferenceShouldBeChecked() {
93         Settings.Global.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
94                 StayAwakePreferenceController.SETTING_VALUE_ON);
95         mController.updateState(mPreference);
96 
97         verify(mPreference).setChecked(true);
98     }
99 
100     @Test
updateState_preferenceShouldNotBeChecked()101     public void updateState_preferenceShouldNotBeChecked() {
102         Settings.Global.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
103                 StayAwakePreferenceController.SETTING_VALUE_OFF);
104         mController.updateState(mPreference);
105 
106         verify(mPreference).setChecked(false);
107     }
108 
109     @Test
displayPreference_expectSetDisabledByAdminToBeCalled()110     public void displayPreference_expectSetDisabledByAdminToBeCalled() {
111         mController = spy(mController);
112         RestrictedLockUtils.EnforcedAdmin admin = Mockito.mock(
113                 RestrictedLockUtils.EnforcedAdmin.class);
114         doReturn(admin).when(mController).checkIfMaximumTimeToLockSetByAdmin();
115         mController.updateState(mPreference);
116 
117         verify(mPreference).setDisabledByAdmin(admin);
118     }
119 
120     @Test
observerOnChangeCalledWithSameUri_preferenceShouldBeUpdated()121     public void observerOnChangeCalledWithSameUri_preferenceShouldBeUpdated() {
122         Settings.Global.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
123                 StayAwakePreferenceController.SETTING_VALUE_ON);
124         mController.onResume();
125         mController.mSettingsObserver.onChange(false,
126                 Settings.Global.getUriFor(Settings.Global.STAY_ON_WHILE_PLUGGED_IN));
127 
128         verify(mPreference).setChecked(true);
129     }
130 
131     @Test
observerOnChangeCalledWithDifferentUri_preferenceShouldNotBeUpdated()132     public void observerOnChangeCalledWithDifferentUri_preferenceShouldNotBeUpdated() {
133         Settings.Global.putInt(mContentResolver, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
134                 StayAwakePreferenceController.SETTING_VALUE_ON);
135         mController.onResume();
136         mController.mSettingsObserver.onChange(false, null);
137 
138         verify(mPreference, never()).setChecked(true);
139     }
140 
141     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()142     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
143         mController.onDeveloperOptionsDisabled();
144 
145         verify(mPreference).setEnabled(false);
146         verify(mPreference).setChecked(false);
147     }
148 }
149