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