blob: afbd87a7949d0fa4145b2635a224943661ec51ac [file] [log] [blame]
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -08001/*
2 * Copyright 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
17package com.android.server.wifi;
18
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
Eric Schwarzenbacha57806d2017-11-21 17:16:08 -080021import static org.mockito.ArgumentMatchers.any;
Eric Schwarzenbach115c8922017-12-05 13:34:31 -080022import static org.mockito.ArgumentMatchers.eq;
23import static org.mockito.Mockito.never;
Eric Schwarzenbacha57806d2017-11-21 17:16:08 -080024import static org.mockito.Mockito.verify;
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -080025import static org.mockito.Mockito.when;
26
27import android.content.Context;
Eric Schwarzenbach115c8922017-12-05 13:34:31 -080028import android.net.wifi.ScanResult;
29import android.net.wifi.WifiConfiguration;
30import android.net.wifi.WifiScanner;
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -080031import android.os.test.TestLooper;
32import android.provider.Settings;
33
Eric Schwarzenbach115c8922017-12-05 13:34:31 -080034import com.android.server.wifi.util.ScanResultUtil;
35
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -080036import org.junit.Before;
37import org.junit.Test;
Eric Schwarzenbach115c8922017-12-05 13:34:31 -080038import org.mockito.InOrder;
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -080039import org.mockito.Mock;
Eric Schwarzenbach115c8922017-12-05 13:34:31 -080040import org.mockito.Mockito;
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -080041import org.mockito.MockitoAnnotations;
42
Eric Schwarzenbach04263762017-12-01 11:23:14 -080043import java.io.ByteArrayOutputStream;
44import java.io.PrintWriter;
Eric Schwarzenbach115c8922017-12-05 13:34:31 -080045import java.util.Arrays;
46import java.util.Collection;
47import java.util.Collections;
Eric Schwarzenbach04263762017-12-01 11:23:14 -080048
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -080049/**
50 * Unit tests for {@link WakeupController}.
51 */
52public class WakeupControllerTest {
53
54 @Mock private Context mContext;
Eric Schwarzenbacha57806d2017-11-21 17:16:08 -080055 @Mock private WakeupLock mWakeupLock;
Eric Schwarzenbacha57806d2017-11-21 17:16:08 -080056 @Mock private WifiConfigStore mWifiConfigStore;
Eric Schwarzenbach115c8922017-12-05 13:34:31 -080057 @Mock private WifiInjector mWifiInjector;
58 @Mock private WifiScanner mWifiScanner;
59 @Mock private WifiConfigManager mWifiConfigManager;
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -080060 @Mock private FrameworkFacade mFrameworkFacade;
61
62 private TestLooper mLooper;
63 private WakeupController mWakeupController;
64
65 /** Initialize objects before each test run. */
66 @Before
67 public void setUp() {
68 MockitoAnnotations.initMocks(this);
Eric Schwarzenbach115c8922017-12-05 13:34:31 -080069
70 when(mWifiInjector.getWifiScanner()).thenReturn(mWifiScanner);
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -080071 mLooper = new TestLooper();
72 }
73
Eric Schwarzenbach115c8922017-12-05 13:34:31 -080074 /** Initializes the wakeupcontroller in the given `enabled` state. */
75 private void initializeWakeupController(boolean enabled) {
76 int settingsValue = enabled ? 1 : 0;
77 when(mFrameworkFacade.getIntegerSetting(mContext,
78 Settings.Global.WIFI_WAKEUP_ENABLED, 0)).thenReturn(settingsValue);
79 mWakeupController = new WakeupController(mContext,
80 mLooper.getLooper(),
81 mWakeupLock,
82 mWifiConfigManager,
83 mWifiConfigStore,
84 mWifiInjector,
85 mFrameworkFacade);
86 }
87
88 private ScanResult createOpenScanResult(String ssid) {
89 ScanResult scanResult = new ScanResult();
90 scanResult.SSID = ssid;
91 scanResult.capabilities = "";
92 return scanResult;
Eric Schwarzenbacha57806d2017-11-21 17:16:08 -080093 }
94
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -080095 /**
96 * Verify WakeupController is enabled when the settings toggle is true.
97 */
98 @Test
99 public void verifyEnabledWhenToggledOn() {
Eric Schwarzenbach115c8922017-12-05 13:34:31 -0800100 initializeWakeupController(true /* enabled */);
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -0800101
102 assertTrue(mWakeupController.isEnabled());
103 }
104
105 /**
106 * Verify WakeupController is disabled when the settings toggle is false.
107 */
108 @Test
109 public void verifyDisabledWhenToggledOff() {
Eric Schwarzenbach115c8922017-12-05 13:34:31 -0800110 initializeWakeupController(false /* enabled */);
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -0800111
112 assertFalse(mWakeupController.isEnabled());
113 }
Eric Schwarzenbacha57806d2017-11-21 17:16:08 -0800114
115 /**
116 * Verify WakeupController registers its store data with the WifiConfigStore on construction.
117 */
118 @Test
119 public void registersWakeupConfigStoreData() {
Eric Schwarzenbach115c8922017-12-05 13:34:31 -0800120 initializeWakeupController(true /* enabled */);
Eric Schwarzenbacha57806d2017-11-21 17:16:08 -0800121 verify(mWifiConfigStore).registerStoreData(any(WakeupConfigStoreData.class));
122 }
Eric Schwarzenbach04263762017-12-01 11:23:14 -0800123
124 /**
125 * Verify that dump calls also dump the state of the WakeupLock.
126 */
127 @Test
128 public void dumpIncludesWakeupLock() {
Eric Schwarzenbach115c8922017-12-05 13:34:31 -0800129 initializeWakeupController(true /* enabled */);
Eric Schwarzenbach04263762017-12-01 11:23:14 -0800130 ByteArrayOutputStream stream = new ByteArrayOutputStream();
131 PrintWriter writer = new PrintWriter(stream);
132 mWakeupController.dump(null, writer, null);
133
134 verify(mWakeupLock).dump(null, writer, null);
135 }
Eric Schwarzenbach115c8922017-12-05 13:34:31 -0800136
137 /**
138 * Verify that start initializes the wakeup lock.
139 */
140 @Test
141 public void startInitializesWakeupLock() {
142 initializeWakeupController(true /* enabled */);
143 mWakeupController.start();
144 verify(mWakeupLock).initialize(any());
145 }
146
147 /**
148 * Verify that start does not initialize the wakeup lock when feature is disabled.
149 */
150 @Test
151 public void startDoesNotInitializeWakeupLockWhenDisabled() {
152 initializeWakeupController(false /* enabled */);
153 mWakeupController.start();
154 verify(mWakeupLock, never()).initialize(any());
155 }
156
157 /**
158 * Verify that start does not re-initialize the wakeup lock if the controller is already active.
159 */
160 @Test
161 public void startDoesNotInitializeWakeupLockIfAlreadyActive() {
162 initializeWakeupController(true /* enabled */);
163 InOrder inOrder = Mockito.inOrder(mWakeupLock);
164
165 mWakeupController.start();
166 inOrder.verify(mWakeupLock).initialize(any());
167
168 mWakeupController.stop();
169 mWakeupController.start();
170 inOrder.verify(mWakeupLock, never()).initialize(any());
171 }
172
173 /**
174 * Verify that start registers the scan listener on the wifi scanner.
175 */
176 @Test
177 public void startRegistersScanListener() {
178 initializeWakeupController(true /* enabled */);
179 mWakeupController.start();
180 verify(mWifiScanner).registerScanListener(any());
181 }
182
183 /**
184 * Verify that stop deregisters the scan listener from the wifi scanner.
185 */
186 @Test
187 public void stopDeresgistersScanListener() {
188 initializeWakeupController(true /* enabled */);
189 mWakeupController.start();
190 mWakeupController.stop();
191 verify(mWifiScanner).deregisterScanListener(any());
192 }
193
194 /**
195 * Verify that reset sets active to false.
196 *
197 * <p>This is accomplished by initiating another call to start and verifying that the wakeup
198 * lock is re-initialized.
199 */
200 @Test
201 public void resetSetsActiveToFalse() {
202 initializeWakeupController(true /* enabled */);
203 InOrder inOrder = Mockito.inOrder(mWakeupLock);
204
205 mWakeupController.start();
206 inOrder.verify(mWakeupLock).initialize(any());
207
208 mWakeupController.stop();
209 mWakeupController.reset();
210 mWakeupController.start();
211 inOrder.verify(mWakeupLock).initialize(any());
212 }
213
214 /**
215 * Verify that the wakeup lock is initialized with the intersection of ScanResults and saved
216 * networks.
217 */
218 @Test
219 public void startInitializesWakeupLockWithSavedScanResults() {
220 String ssid1 = "ssid 1";
221 String ssid2 = "ssid 2";
222 String quotedSsid = ScanResultUtil.createQuotedSSID(ssid1);
223
224 // saved configs
225 WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(quotedSsid);
226 openNetwork.getNetworkSelectionStatus().setHasEverConnected(true);
227 WifiConfiguration wepNetwork = WifiConfigurationTestUtil.createWepNetwork();
228 wepNetwork.getNetworkSelectionStatus().setHasEverConnected(true);
229 when(mWifiConfigManager.getSavedNetworks())
230 .thenReturn(Arrays.asList(openNetwork, wepNetwork));
231
232 // scan results from most recent scan
233 ScanResult savedScanResult = createOpenScanResult(ssid1);
234 ScanResult unsavedScanResult = createOpenScanResult(ssid2);
235 when(mWifiScanner.getSingleScanResults())
236 .thenReturn(Arrays.asList(savedScanResult, unsavedScanResult));
237
238 // intersection of most recent scan + saved configs
239 Collection<ScanResultMatchInfo> expectedMatchInfos =
240 Collections.singleton(ScanResultMatchInfo.fromScanResult(savedScanResult));
241
242 initializeWakeupController(true /* enabled */);
243 mWakeupController.start();
244 verify(mWakeupLock).initialize(eq(expectedMatchInfos));
245 }
Eric Schwarzenbacha55e8d72017-11-14 11:17:32 -0800246}