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
17package com.android.settings.development;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Mockito.doReturn;
21import static org.mockito.Mockito.never;
22import static org.mockito.Mockito.spy;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.content.ContentResolver;
27import android.content.Context;
28import android.provider.Settings;
29import android.support.v7.preference.PreferenceScreen;
30
31import com.android.settings.testutils.SettingsRobolectricTestRunner;
32import com.android.settingslib.RestrictedLockUtils;
33import com.android.settingslib.RestrictedSwitchPreference;
34import com.android.settingslib.core.lifecycle.Lifecycle;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Mock;
40import org.mockito.Mockito;
41import org.mockito.MockitoAnnotations;
42import org.robolectric.RuntimeEnvironment;
43
44@RunWith(SettingsRobolectricTestRunner.class)
45public 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
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
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
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
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
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
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
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
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
140    public void onDeveloperOptionsDisabled_shouldDisablePreference() {
141        mController.onDeveloperOptionsDisabled();
142
143        verify(mPreference).setEnabled(false);
144        verify(mPreference).setChecked(false);
145    }
146}
147