• 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 package com.android.settings.location;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.when;
21 
22 import android.content.ContentResolver;
23 import android.provider.Settings;
24 import android.provider.Settings.Global;
25 import android.support.v14.preference.SwitchPreference;
26 
27 import com.android.settings.testutils.SettingsRobolectricTestRunner;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.robolectric.RuntimeEnvironment;
35 
36 @RunWith(SettingsRobolectricTestRunner.class)
37 public class WifiScanningPreferenceControllerTest {
38 
39     @Mock
40     private SwitchPreference mPreference;
41 
42     private ContentResolver mContentResolver;
43     private WifiScanningPreferenceController mController;
44 
45     @Before
setUp()46     public void setUp() {
47         MockitoAnnotations.initMocks(this);
48         mContentResolver = RuntimeEnvironment.application.getContentResolver();
49         mController = new WifiScanningPreferenceController(RuntimeEnvironment.application);
50         when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
51     }
52 
53     @Test
updateState_wifiScanningEnabled_shouldCheckedPreference()54     public void updateState_wifiScanningEnabled_shouldCheckedPreference() {
55         Settings.Global.putInt(mContentResolver, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1);
56 
57         mController.updateState(mPreference);
58 
59         verify(mPreference).setChecked(true);
60     }
61 
62     @Test
updateState_wifiScanningDisabled_shouldUncheckedPreference()63     public void updateState_wifiScanningDisabled_shouldUncheckedPreference() {
64         Settings.Global.putInt(mContentResolver, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0);
65 
66         mController.updateState(mPreference);
67 
68         verify(mPreference).setChecked(false);
69     }
70 
71     @Test
handlePreferenceTreeClick_checked_shouldEnableWifiScanning()72     public void handlePreferenceTreeClick_checked_shouldEnableWifiScanning() {
73         when(mPreference.isChecked()).thenReturn(true);
74 
75         mController.handlePreferenceTreeClick(mPreference);
76 
77         final int scanAlways =
78             Settings.Global.getInt(mContentResolver, Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0);
79         assertThat(scanAlways).isEqualTo(1);
80     }
81 
82     @Test
handlePreferenceTreeClick_unchecked_shouldDisableWifiScanning()83     public void handlePreferenceTreeClick_unchecked_shouldDisableWifiScanning() {
84         when(mPreference.isChecked()).thenReturn(false);
85 
86         mController.handlePreferenceTreeClick(mPreference);
87 
88         final int scanAlways =
89             Settings.Global.getInt(mContentResolver, Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1);
90         assertThat(scanAlways).isEqualTo(0);
91     }
92 }
93