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 org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.when;
21 
22 import android.content.Context;
23 import android.net.wifi.WifiManager;
24 
25 import androidx.preference.PreferenceScreen;
26 import androidx.preference.SwitchPreference;
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.robolectric.RobolectricTestRunner;
34 
35 @RunWith(RobolectricTestRunner.class)
36 public class WifiScanThrottlingPreferenceControllerTest {
37 
38     @Mock
39     private SwitchPreference mPreference;
40     @Mock
41     private PreferenceScreen mPreferenceScreen;
42     @Mock
43     private Context mContext;
44     @Mock
45     private WifiManager mWifiManager;
46     private WifiScanThrottlingPreferenceController mController;
47 
48     @Before
setup()49     public void setup() {
50         MockitoAnnotations.initMocks(this);
51         when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
52         mController = new WifiScanThrottlingPreferenceController(mContext);
53         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
54                 .thenReturn(mPreference);
55         mController.displayPreference(mPreferenceScreen);
56     }
57 
58     @Test
onPreferenceChanged_turnOnScanThrottling()59     public void onPreferenceChanged_turnOnScanThrottling() {
60         mController.onPreferenceChange(mPreference, true /* new value */);
61 
62         verify(mWifiManager).setScanThrottleEnabled(true);
63     }
64 
65     @Test
onPreferenceChanged_turnOffScanThrottling()66     public void onPreferenceChanged_turnOffScanThrottling() {
67         mController.onPreferenceChange(mPreference, false /* new value */);
68 
69         verify(mWifiManager).setScanThrottleEnabled(false);
70     }
71 
72     @Test
updateState_preferenceShouldBeChecked()73     public void updateState_preferenceShouldBeChecked() {
74         when(mWifiManager.isScanThrottleEnabled()).thenReturn(true);
75         mController.updateState(mPreference);
76 
77         verify(mPreference).setChecked(true);
78     }
79 
80     @Test
updateState_preferenceShouldNotBeChecked()81     public void updateState_preferenceShouldNotBeChecked() {
82         when(mWifiManager.isScanThrottleEnabled()).thenReturn(false);
83         mController.updateState(mPreference);
84 
85         verify(mPreference).setChecked(false);
86     }
87 
88     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()89     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
90         mController.onDeveloperOptionsDisabled();
91         verify(mWifiManager).setScanThrottleEnabled(true);
92         verify(mPreference).setEnabled(false);
93         verify(mPreference).setChecked(true);
94     }
95 }
96