• 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.wifi;
18 
19 import static android.provider.Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE;
20 import static android.provider.Settings.Global.WIFI_WAKEUP_ENABLED;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.times;
26 
27 import android.content.Context;
28 import android.location.LocationManager;
29 import android.provider.Settings;
30 
31 import android.text.TextUtils;
32 import androidx.preference.Preference;
33 import androidx.preference.SwitchPreference;
34 
35 import com.android.settings.R;
36 import com.android.settings.dashboard.DashboardFragment;
37 
38 import com.android.settingslib.core.lifecycle.Lifecycle;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.robolectric.RobolectricTestRunner;
45 import org.robolectric.RuntimeEnvironment;
46 
47 @RunWith(RobolectricTestRunner.class)
48 public class WifiWakeupPreferenceControllerTest {
49 
50     private Context mContext;
51     private WifiWakeupPreferenceController mController;
52     @Mock
53     private DashboardFragment mFragment;
54     @Mock
55     private LocationManager mLocationManager;
56     @Mock
57     private SwitchPreference mPreference;
58     @Mock
59     private Lifecycle mLifecycle;
60 
61     @Before
setUp()62     public void setUp() {
63         MockitoAnnotations.initMocks(this);
64         mContext = RuntimeEnvironment.application;
65         mController = new WifiWakeupPreferenceController(mContext, mFragment, mLifecycle);
66         mController.mLocationManager = mLocationManager;
67         mController.mPreference = mPreference;
68 
69         Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
70         doReturn(true).when(mLocationManager).isLocationEnabled();
71     }
72 
73     @Test
handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing()74     public void handlePreferenceTreeClick_nonMatchingKey_shouldDoNothing() {
75         final SwitchPreference pref = new SwitchPreference(mContext);
76 
77         assertThat(mController.handlePreferenceTreeClick(pref)).isFalse();
78     }
79 
80     @Test
handlePreferenceTreeClick_nonMatchingType_shouldDoNothing()81     public void handlePreferenceTreeClick_nonMatchingType_shouldDoNothing() {
82         final Preference pref = new Preference(mContext);
83         pref.setKey(mController.getPreferenceKey());
84 
85         assertThat(mController.handlePreferenceTreeClick(pref)).isFalse();
86     }
87 
88     @Test
handlePreferenceTreeClick_matchingKeyAndType_shouldUpdateSetting()89     public void handlePreferenceTreeClick_matchingKeyAndType_shouldUpdateSetting() {
90         final SwitchPreference pref = new SwitchPreference(mContext);
91         pref.setChecked(true);
92         pref.setKey(mController.getPreferenceKey());
93 
94         assertThat(mController.handlePreferenceTreeClick(pref)).isTrue();
95         assertThat(Settings.Global.getInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0))
96                 .isEqualTo(1);
97     }
98 
99     @Test
handlePreferenceTreeClick_wifiWakeupEnableScanningDisable_wifiWakeupEnable()100     public void handlePreferenceTreeClick_wifiWakeupEnableScanningDisable_wifiWakeupEnable() {
101         Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
102         Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
103         doReturn(true).when(mLocationManager).isLocationEnabled();
104 
105         mController.handlePreferenceTreeClick(mPreference);
106         final boolean isWifiWakeupEnabled = Settings.Global.getInt(mContext.getContentResolver(),
107                 Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1;
108 
109         assertThat(isWifiWakeupEnabled).isTrue();
110     }
111 
112     @Test
updateState_preferenceSetCheckedWhenWakeupSettingEnabled()113     public void updateState_preferenceSetCheckedWhenWakeupSettingEnabled() {
114         final SwitchPreference preference = new SwitchPreference(mContext);
115         Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
116         Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 1);
117 
118         mController.updateState(preference);
119 
120         assertThat(preference.isChecked()).isTrue();
121         assertThat(preference.getSummary())
122             .isEqualTo(mContext.getString(R.string.wifi_wakeup_summary));
123     }
124 
125     @Test
updateState_preferenceSetUncheckedWhenWakeupSettingDisabled()126     public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabled() {
127         final SwitchPreference preference = new SwitchPreference(mContext);
128         Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
129 
130         mController.updateState(preference);
131 
132         assertThat(preference.isChecked()).isFalse();
133         assertThat(preference.getSummary())
134             .isEqualTo(mContext.getString(R.string.wifi_wakeup_summary));
135     }
136 
137     @Test
updateState_preferenceSetUncheckedWhenWifiScanningDisabled()138     public void updateState_preferenceSetUncheckedWhenWifiScanningDisabled() {
139         final SwitchPreference preference = new SwitchPreference(mContext);
140         Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
141         Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
142 
143         mController.updateState(preference);
144 
145         assertThat(preference.isChecked()).isFalse();
146     }
147 
148     @Test
updateState_preferenceSetUncheckedWhenWakeupSettingEnabledNoLocation()149     public void updateState_preferenceSetUncheckedWhenWakeupSettingEnabledNoLocation() {
150         final SwitchPreference preference = new SwitchPreference(mContext);
151         Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
152         doReturn(false).when(mLocationManager).isLocationEnabled();
153 
154         mController.updateState(preference);
155 
156         assertThat(preference.isChecked()).isFalse();
157         assertThat(TextUtils.equals(preference.getSummary(), mController.getNoLocationSummary()))
158             .isTrue();
159     }
160 
161     @Test
updateState_preferenceSetUncheckedWhenWakeupSettingDisabledLocationEnabled()162     public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabledLocationEnabled() {
163         final SwitchPreference preference = new SwitchPreference(mContext);
164         Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 0);
165         doReturn(false).when(mLocationManager).isLocationEnabled();
166 
167         mController.updateState(preference);
168 
169         assertThat(preference.isChecked()).isFalse();
170         assertThat(TextUtils.equals(preference.getSummary(), mController.getNoLocationSummary()))
171             .isTrue();
172     }
173 
174     @Test
updateState_preferenceSetUncheckedWhenWifiScanningDisabledLocationEnabled()175     public void updateState_preferenceSetUncheckedWhenWifiScanningDisabledLocationEnabled() {
176         final SwitchPreference preference = new SwitchPreference(mContext);
177         Settings.Global.putInt(mContext.getContentResolver(), WIFI_WAKEUP_ENABLED, 1);
178         Settings.Global.putInt(mContext.getContentResolver(), WIFI_SCAN_ALWAYS_AVAILABLE, 0);
179         doReturn(false).when(mLocationManager).isLocationEnabled();
180 
181         mController.updateState(preference);
182 
183         assertThat(preference.isChecked()).isFalse();
184         assertThat(TextUtils.equals(preference.getSummary(), mController.getNoLocationSummary()))
185             .isTrue();
186     }
187 }
188