Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wifi; |
| 18 | |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
Eric Schwarzenbach | a57806d | 2017-11-21 17:16:08 -0800 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.any; |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.eq; |
| 23 | import static org.mockito.Mockito.never; |
Eric Schwarzenbach | a57806d | 2017-11-21 17:16:08 -0800 | [diff] [blame] | 24 | import static org.mockito.Mockito.verify; |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 25 | import static org.mockito.Mockito.when; |
| 26 | |
| 27 | import android.content.Context; |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 28 | import android.net.wifi.ScanResult; |
| 29 | import android.net.wifi.WifiConfiguration; |
| 30 | import android.net.wifi.WifiScanner; |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 31 | import android.os.test.TestLooper; |
| 32 | import android.provider.Settings; |
| 33 | |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 34 | import com.android.server.wifi.util.ScanResultUtil; |
| 35 | |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 38 | import org.mockito.InOrder; |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 39 | import org.mockito.Mock; |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 40 | import org.mockito.Mockito; |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 41 | import org.mockito.MockitoAnnotations; |
| 42 | |
Eric Schwarzenbach | 0426376 | 2017-12-01 11:23:14 -0800 | [diff] [blame] | 43 | import java.io.ByteArrayOutputStream; |
| 44 | import java.io.PrintWriter; |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 45 | import java.util.Arrays; |
| 46 | import java.util.Collection; |
| 47 | import java.util.Collections; |
Eric Schwarzenbach | 0426376 | 2017-12-01 11:23:14 -0800 | [diff] [blame] | 48 | |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 49 | /** |
| 50 | * Unit tests for {@link WakeupController}. |
| 51 | */ |
| 52 | public class WakeupControllerTest { |
| 53 | |
| 54 | @Mock private Context mContext; |
Eric Schwarzenbach | a57806d | 2017-11-21 17:16:08 -0800 | [diff] [blame] | 55 | @Mock private WakeupLock mWakeupLock; |
Eric Schwarzenbach | a57806d | 2017-11-21 17:16:08 -0800 | [diff] [blame] | 56 | @Mock private WifiConfigStore mWifiConfigStore; |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 57 | @Mock private WifiInjector mWifiInjector; |
| 58 | @Mock private WifiScanner mWifiScanner; |
| 59 | @Mock private WifiConfigManager mWifiConfigManager; |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 60 | @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 Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 69 | |
| 70 | when(mWifiInjector.getWifiScanner()).thenReturn(mWifiScanner); |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 71 | mLooper = new TestLooper(); |
| 72 | } |
| 73 | |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 74 | /** 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 Schwarzenbach | a57806d | 2017-11-21 17:16:08 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Verify WakeupController is enabled when the settings toggle is true. |
| 97 | */ |
| 98 | @Test |
| 99 | public void verifyEnabledWhenToggledOn() { |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 100 | initializeWakeupController(true /* enabled */); |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 101 | |
| 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 Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 110 | initializeWakeupController(false /* enabled */); |
Eric Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 111 | |
| 112 | assertFalse(mWakeupController.isEnabled()); |
| 113 | } |
Eric Schwarzenbach | a57806d | 2017-11-21 17:16:08 -0800 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Verify WakeupController registers its store data with the WifiConfigStore on construction. |
| 117 | */ |
| 118 | @Test |
| 119 | public void registersWakeupConfigStoreData() { |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 120 | initializeWakeupController(true /* enabled */); |
Eric Schwarzenbach | a57806d | 2017-11-21 17:16:08 -0800 | [diff] [blame] | 121 | verify(mWifiConfigStore).registerStoreData(any(WakeupConfigStoreData.class)); |
| 122 | } |
Eric Schwarzenbach | 0426376 | 2017-12-01 11:23:14 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Verify that dump calls also dump the state of the WakeupLock. |
| 126 | */ |
| 127 | @Test |
| 128 | public void dumpIncludesWakeupLock() { |
Eric Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 129 | initializeWakeupController(true /* enabled */); |
Eric Schwarzenbach | 0426376 | 2017-12-01 11:23:14 -0800 | [diff] [blame] | 130 | 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 Schwarzenbach | 115c892 | 2017-12-05 13:34:31 -0800 | [diff] [blame] | 136 | |
| 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 Schwarzenbach | a55e8d7 | 2017-11-14 11:17:32 -0800 | [diff] [blame] | 246 | } |