• 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 com.android.settings.core.BasePreferenceController.AVAILABLE;
20 import static com.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 import android.location.LocationManager;
30 import android.net.wifi.WifiManager;
31 import android.text.TextUtils;
32 
33 import androidx.preference.SwitchPreference;
34 
35 import com.android.settings.R;
36 import com.android.settings.dashboard.DashboardFragment;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RobolectricTestRunner;
44 import org.robolectric.RuntimeEnvironment;
45 
46 @RunWith(RobolectricTestRunner.class)
47 public class WifiWakeupPreferenceControllerTest {
48 
49     private Context mContext;
50     private WifiWakeupPreferenceController mController;
51     @Mock
52     private DashboardFragment mFragment;
53     @Mock
54     private LocationManager mLocationManager;
55     @Mock
56     private WifiManager mWifiManager;
57     @Mock
58     private SwitchPreference mPreference;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mContext = RuntimeEnvironment.application;
64         mController = new WifiWakeupPreferenceController(mContext);
65         mController.setFragment(mFragment);
66         mController.mLocationManager = mLocationManager;
67         mController.mPreference = mPreference;
68         mController.mWifiManager = mWifiManager;
69 
70         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(true);
71         doReturn(true).when(mLocationManager).isLocationEnabled();
72     }
73 
74     @Test
getAvailabilityStatus_fragmentIsNotNull_returnAvailable()75     public void getAvailabilityStatus_fragmentIsNotNull_returnAvailable() {
76         mController.setFragment(mFragment);
77 
78         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
79     }
80 
81     @Test
getAvailabilityStatus_fragmentIsNullAndLocationDisabled_returnDisabled()82     public void getAvailabilityStatus_fragmentIsNullAndLocationDisabled_returnDisabled() {
83         mController.setFragment(null);
84         when(mLocationManager.isLocationEnabled()).thenReturn(false);
85 
86         assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
87     }
88 
89     @Test
getAvailabilityStatus_fragmentIsNullAndWifiScanDisabled_returnDisabled()90     public void getAvailabilityStatus_fragmentIsNullAndWifiScanDisabled_returnDisabled() {
91         mController.setFragment(null);
92         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(false);
93 
94         assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
95     }
96 
97     @Test
setChecked_mFragmentIsNullLocationEnable_wifiWakeupEnable()98     public void setChecked_mFragmentIsNullLocationEnable_wifiWakeupEnable() {
99         mController.setFragment(null);
100         when(mLocationManager.isLocationEnabled()).thenReturn(true);
101         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(true);
102         when(mWifiManager.isAutoWakeupEnabled()).thenReturn(false);
103 
104         mController.setChecked(true);
105 
106         verify(mWifiManager).setAutoWakeupEnabled(true);
107     }
108 
109     @Test
setChecked_scanEnableLocationEnable_wifiWakeupEnable()110     public void setChecked_scanEnableLocationEnable_wifiWakeupEnable() {
111         when(mWifiManager.isAutoWakeupEnabled()).thenReturn(false);
112         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(true);
113         doReturn(true).when(mLocationManager).isLocationEnabled();
114 
115         mController.setChecked(true);
116 
117         verify(mWifiManager).setAutoWakeupEnabled(true);
118     }
119 
120     @Test
updateState_wifiWakeupEnableScanningDisable_wifiWakeupDisabled()121     public void updateState_wifiWakeupEnableScanningDisable_wifiWakeupDisabled() {
122         final SwitchPreference preference = new SwitchPreference(mContext);
123         when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
124         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(false);
125         doReturn(true).when(mLocationManager).isLocationEnabled();
126 
127         mController.updateState(preference);
128 
129         assertThat(preference.isChecked()).isFalse();
130         assertThat(preference.getSummary())
131             .isEqualTo(mContext.getString(R.string.wifi_wakeup_summary));
132     }
133 
134     @Test
updateState_preferenceSetCheckedWhenWakeupSettingEnabled()135     public void updateState_preferenceSetCheckedWhenWakeupSettingEnabled() {
136         final SwitchPreference preference = new SwitchPreference(mContext);
137         when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
138         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(true);
139         doReturn(true).when(mLocationManager).isLocationEnabled();
140 
141         mController.updateState(preference);
142 
143         assertThat(preference.isChecked()).isTrue();
144         assertThat(preference.getSummary())
145             .isEqualTo(mContext.getString(R.string.wifi_wakeup_summary));
146     }
147 
148     @Test
updateState_preferenceSetUncheckedWhenWakeupSettingDisabled()149     public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabled() {
150         final SwitchPreference preference = new SwitchPreference(mContext);
151         when(mWifiManager.isAutoWakeupEnabled()).thenReturn(false);
152 
153         mController.updateState(preference);
154 
155         assertThat(preference.isChecked()).isFalse();
156         assertThat(preference.getSummary())
157             .isEqualTo(mContext.getString(R.string.wifi_wakeup_summary));
158     }
159 
160     @Test
updateState_preferenceSetUncheckedWhenWifiScanningDisabled()161     public void updateState_preferenceSetUncheckedWhenWifiScanningDisabled() {
162         final SwitchPreference preference = new SwitchPreference(mContext);
163         when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
164         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(false);
165 
166         mController.updateState(preference);
167 
168         assertThat(preference.isChecked()).isFalse();
169     }
170 
171     @Test
updateState_preferenceSetUncheckedWhenWakeupSettingEnabledNoLocation()172     public void updateState_preferenceSetUncheckedWhenWakeupSettingEnabledNoLocation() {
173         final SwitchPreference preference = new SwitchPreference(mContext);
174         when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
175         doReturn(false).when(mLocationManager).isLocationEnabled();
176 
177         mController.updateState(preference);
178 
179         assertThat(preference.isChecked()).isFalse();
180         assertThat(TextUtils.equals(preference.getSummary(), mController.getNoLocationSummary()))
181             .isTrue();
182     }
183 
184     @Test
updateState_preferenceSetUncheckedWhenWakeupSettingDisabledLocationEnabled()185     public void updateState_preferenceSetUncheckedWhenWakeupSettingDisabledLocationEnabled() {
186         final SwitchPreference preference = new SwitchPreference(mContext);
187         when(mWifiManager.isAutoWakeupEnabled()).thenReturn(false);
188         doReturn(false).when(mLocationManager).isLocationEnabled();
189 
190         mController.updateState(preference);
191 
192         assertThat(preference.isChecked()).isFalse();
193         assertThat(TextUtils.equals(preference.getSummary(), mController.getNoLocationSummary()))
194             .isTrue();
195     }
196 
197     @Test
updateState_preferenceSetUncheckedWhenWifiScanningDisabledLocationEnabled()198     public void updateState_preferenceSetUncheckedWhenWifiScanningDisabledLocationEnabled() {
199         final SwitchPreference preference = new SwitchPreference(mContext);
200         when(mWifiManager.isAutoWakeupEnabled()).thenReturn(true);
201         when(mWifiManager.isScanAlwaysAvailable()).thenReturn(false);
202         doReturn(false).when(mLocationManager).isLocationEnabled();
203 
204         mController.updateState(preference);
205 
206         assertThat(preference.isChecked()).isFalse();
207         assertThat(TextUtils.equals(preference.getSummary(), mController.getNoLocationSummary()))
208             .isTrue();
209     }
210 }
211