• 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.development;
18 
19 import static com.android.settings.development.WifiScanThrottlingPreferenceController.SETTING_THROTTLING_ENABLE_VALUE_OFF;
20 import static com.android.settings.development.WifiScanThrottlingPreferenceController.SETTING_THROTTLING_ENABLE_VALUE_ON;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.provider.Settings;
29 
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.SwitchPreference;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class WifiScanThrottlingPreferenceControllerTest {
43 
44     @Mock
45     private SwitchPreference mPreference;
46     @Mock
47     private PreferenceScreen mPreferenceScreen;
48 
49     private Context mContext;
50     private WifiScanThrottlingPreferenceController mController;
51 
52     @Before
setup()53     public void setup() {
54         MockitoAnnotations.initMocks(this);
55         mContext = RuntimeEnvironment.application;
56         mController = new WifiScanThrottlingPreferenceController(mContext);
57         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
58                 .thenReturn(mPreference);
59         mController.displayPreference(mPreferenceScreen);
60     }
61 
62     @Test
onPreferenceChanged_turnOnScanThrottling()63     public void onPreferenceChanged_turnOnScanThrottling() {
64         mController.onPreferenceChange(mPreference, true /* new value */);
65 
66         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
67                 Settings.Global.WIFI_SCAN_THROTTLE_ENABLED, 1 /* default */);
68 
69         assertThat(mode).isEqualTo(SETTING_THROTTLING_ENABLE_VALUE_ON);
70     }
71 
72     @Test
onPreferenceChanged_turnOffScanThrottling()73     public void onPreferenceChanged_turnOffScanThrottling() {
74         mController.onPreferenceChange(mPreference, false /* new value */);
75 
76         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
77                 Settings.Global.WIFI_SCAN_THROTTLE_ENABLED, 1 /* default */);
78 
79         assertThat(mode).isEqualTo(SETTING_THROTTLING_ENABLE_VALUE_OFF);
80     }
81 
82     @Test
updateState_preferenceShouldBeChecked()83     public void updateState_preferenceShouldBeChecked() {
84         Settings.Global.putInt(mContext.getContentResolver(),
85                 Settings.Global.WIFI_SCAN_THROTTLE_ENABLED,
86                 SETTING_THROTTLING_ENABLE_VALUE_ON);
87         mController.updateState(mPreference);
88 
89         verify(mPreference).setChecked(true);
90     }
91 
92     @Test
updateState_preferenceShouldNotBeChecked()93     public void updateState_preferenceShouldNotBeChecked() {
94         Settings.Global.putInt(mContext.getContentResolver(),
95                 Settings.Global.WIFI_SCAN_THROTTLE_ENABLED,
96                 SETTING_THROTTLING_ENABLE_VALUE_OFF);
97         mController.updateState(mPreference);
98 
99         verify(mPreference).setChecked(false);
100     }
101 
102     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()103     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
104         mController.onDeveloperOptionsDisabled();
105         final int mode = Settings.Global.getInt(mContext.getContentResolver(),
106                 Settings.Global.WIFI_SCAN_THROTTLE_ENABLED, 1 /* default */);
107 
108         assertThat(mode).isEqualTo(SETTING_THROTTLING_ENABLE_VALUE_ON);
109         verify(mPreference).setEnabled(false);
110         verify(mPreference).setChecked(true);
111     }
112 }
113