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