WifiMetricsTest.java revision 7a0b9ffe794113c4a5ed528d8aadeb3303d1b089
1/* 2 * Copyright (C) 2016 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 */ 16package com.android.server.wifi; 17 18import static org.junit.Assert.assertEquals; 19import static org.junit.Assert.assertTrue; 20import static org.mockito.Mockito.*; 21 22import android.net.wifi.ScanResult; 23import android.net.wifi.WifiConfiguration; 24import android.test.suitebuilder.annotation.SmallTest; 25import android.util.Base64; 26 27import com.android.server.wifi.hotspot2.NetworkDetail; 28 29import org.junit.Before; 30import org.junit.Test; 31import org.mockito.Mock; 32import org.mockito.MockitoAnnotations; 33 34import java.io.ByteArrayOutputStream; 35import java.io.PrintWriter; 36import java.util.regex.Matcher; 37import java.util.regex.Pattern; 38 39/** 40 * Unit tests for {@link com.android.server.wifi.WifiMetrics}. 41 */ 42@SmallTest 43public class WifiMetricsTest { 44 45 WifiMetrics mWifiMetrics; 46 WifiMetricsProto.WifiLog mDeserializedWifiMetrics; 47 @Mock Clock mClock; 48 49 @Before 50 public void setUp() throws Exception { 51 MockitoAnnotations.initMocks(this); 52 mDeserializedWifiMetrics = null; 53 when(mClock.elapsedRealtime()).thenReturn((long) 0); 54 mWifiMetrics = new WifiMetrics(mClock); 55 } 56 57 /** 58 * Test that startConnectionEvent and endConnectionEvent can be called repeatedly and out of 59 * order. Only tests no exception occurs. Creates 3 ConnectionEvents. 60 */ 61 @Test 62 public void startAndEndConnectionEventSucceeds() throws Exception { 63 //Start and end Connection event 64 mWifiMetrics.startConnectionEvent(null, "RED", 65 WifiMetricsProto.ConnectionEvent.ROAM_ENTERPRISE); 66 mWifiMetrics.endConnectionEvent( 67 WifiMetrics.ConnectionEvent.FAILURE_AUTHENTICATION_FAILURE, 68 WifiMetricsProto.ConnectionEvent.HLF_DHCP); 69 //end Connection event without starting one 70 mWifiMetrics.endConnectionEvent( 71 WifiMetrics.ConnectionEvent.FAILURE_AUTHENTICATION_FAILURE, 72 WifiMetricsProto.ConnectionEvent.HLF_DHCP); 73 //start two ConnectionEvents in a row 74 mWifiMetrics.startConnectionEvent(null, "BLUE", 75 WifiMetricsProto.ConnectionEvent.ROAM_ENTERPRISE); 76 mWifiMetrics.startConnectionEvent(null, "GREEN", 77 WifiMetricsProto.ConnectionEvent.ROAM_ENTERPRISE); 78 } 79 80 private static final long TEST_RECORD_DURATION_SEC = 12 * 60 * 60; 81 private static final long TEST_RECORD_DURATION_MILLIS = TEST_RECORD_DURATION_SEC * 1000; 82 83 /** 84 * Simulate how dumpsys gets the proto from mWifiMetrics, filter the proto bytes out and 85 * deserialize them into mDeserializedWifiMetrics 86 */ 87 public void dumpProtoAndDeserialize() throws Exception { 88 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 89 PrintWriter writer = new PrintWriter(stream); 90 String[] args = new String[0]; 91 92 when(mClock.elapsedRealtime()).thenReturn(TEST_RECORD_DURATION_MILLIS); 93 //Test proto dump, by passing in proto arg option 94 args = new String[]{WifiMetrics.PROTO_DUMP_ARG}; 95 mWifiMetrics.dump(null, writer, args); 96 writer.flush(); 97 Pattern pattern = Pattern.compile( 98 "(?<=WifiMetrics:\\n)([\\s\\S]*)(?=EndWifiMetrics)"); 99 Matcher matcher = pattern.matcher(stream.toString()); 100 assertTrue("Proto Byte string found in WifiMetrics.dump():\n" + stream.toString(), 101 matcher.find()); 102 String protoByteString = matcher.group(1); 103 byte[] protoBytes = Base64.decode(protoByteString, Base64.DEFAULT); 104 mDeserializedWifiMetrics = WifiMetricsProto.WifiLog.parseFrom(protoBytes); 105 } 106 107 @Test 108 public void dumpHumanReadable() throws Exception { 109 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 110 PrintWriter writer = new PrintWriter(stream); 111 String[] args = new String[0]; 112 mWifiMetrics.dump(null, writer, args); 113 writer.flush(); 114 assertTrue("stream.toString().contains(\"WifiMetrics\")", 115 stream.toString().contains("WifiMetrics")); 116 } 117 118 @Test 119 public void testDumpProtoAndDeserialize() throws Exception { 120 setAndIncrementMetrics(); 121 dumpProtoAndDeserialize(); 122 assertDeserializedMetricsCorrect(); 123 } 124 125 private static final int NUM_SAVED_NETWORKS = 1; 126 private static final int NUM_OPEN_NETWORKS = 2; 127 private static final int NUM_PERSONAL_NETWORKS = 3; 128 private static final int NUM_ENTERPRISE_NETWORKS = 5; 129 private static final boolean TEST_VAL_IS_LOCATION_ENABLED = true; 130 private static final boolean IS_SCANNING_ALWAYS_ENABLED = true; 131 private static final int NUM_NEWTORKS_ADDED_BY_USER = 13; 132 private static final int NUM_NEWTORKS_ADDED_BY_APPS = 17; 133 private static final int NUM_EMPTY_SCAN_RESULTS = 19; 134 private static final int NUM_NON_EMPTY_SCAN_RESULTS = 23; 135 private static final int NUM_SCAN_UNKNOWN = 1; 136 private static final int NUM_SCAN_SUCCESS = 2; 137 private static final int NUM_SCAN_FAILURE_INTERRUPTED = 3; 138 private static final int NUM_SCAN_FAILURE_INVALID_CONFIGURATION = 5; 139 private static final int NUM_WIFI_UNKNOWN_SCREEN_OFF = 3; 140 private static final int NUM_WIFI_UNKNOWN_SCREEN_ON = 5; 141 private static final int NUM_WIFI_ASSOCIATED_SCREEN_OFF = 7; 142 private static final int NUM_WIFI_ASSOCIATED_SCREEN_ON = 11; 143 private static final int NUM_CONNECTIVITY_WATCHDOG_PNO_GOOD = 11; 144 private static final int NUM_CONNECTIVITY_WATCHDOG_PNO_BAD = 12; 145 private static final int NUM_CONNECTIVITY_WATCHDOG_BACKGROUND_GOOD = 13; 146 private static final int NUM_CONNECTIVITY_WATCHDOG_BACKGROUND_BAD = 14; 147 private static final int NUM_LAST_RESORT_WATCHDOG_TRIGGERS = 1; 148 private static final int NUM_LAST_RESORT_WATCHDOG_BAD_ASSOCIATION_NETWORKS_TOTAL = 2; 149 private static final int NUM_LAST_RESORT_WATCHDOG_BAD_AUTHENTICATION_NETWORKS_TOTAL = 3; 150 private static final int NUM_LAST_RESORT_WATCHDOG_BAD_DHCP_NETWORKS_TOTAL = 4; 151 private static final int NUM_LAST_RESORT_WATCHDOG_BAD_OTHER_NETWORKS_TOTAL = 5; 152 private static final int NUM_LAST_RESORT_WATCHDOG_AVAILABLE_NETWORKS_TOTAL = 6; 153 private static final int NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_ASSOCIATION = 7; 154 private static final int NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_AUTHENTICATION = 8; 155 private static final int NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_DHCP = 9; 156 private static final int NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_OTHER = 10; 157 private static final int NUM_RSSI_LEVELS_TO_INCREMENT = 20; 158 private static final int FIRST_RSSI_LEVEL = -80; 159 /** 160 * Set simple metrics, increment others 161 */ 162 public void setAndIncrementMetrics() throws Exception { 163 mWifiMetrics.setNumSavedNetworks(NUM_SAVED_NETWORKS); 164 mWifiMetrics.setNumOpenNetworks(NUM_OPEN_NETWORKS); 165 mWifiMetrics.setNumPersonalNetworks(NUM_PERSONAL_NETWORKS); 166 mWifiMetrics.setNumEnterpriseNetworks(NUM_ENTERPRISE_NETWORKS); 167 mWifiMetrics.setNumNetworksAddedByUser(NUM_NEWTORKS_ADDED_BY_USER); 168 mWifiMetrics.setNumNetworksAddedByApps(NUM_NEWTORKS_ADDED_BY_APPS); 169 mWifiMetrics.setIsLocationEnabled(TEST_VAL_IS_LOCATION_ENABLED); 170 mWifiMetrics.setIsScanningAlwaysEnabled(IS_SCANNING_ALWAYS_ENABLED); 171 172 for (int i = 0; i < NUM_EMPTY_SCAN_RESULTS; i++) { 173 mWifiMetrics.incrementEmptyScanResultCount(); 174 } 175 for (int i = 0; i < NUM_NON_EMPTY_SCAN_RESULTS; i++) { 176 mWifiMetrics.incrementNonEmptyScanResultCount(); 177 } 178 mWifiMetrics.incrementScanReturnEntry(WifiMetricsProto.WifiLog.SCAN_UNKNOWN, 179 NUM_SCAN_UNKNOWN); 180 mWifiMetrics.incrementScanReturnEntry(WifiMetricsProto.WifiLog.SCAN_SUCCESS, 181 NUM_SCAN_SUCCESS); 182 mWifiMetrics.incrementScanReturnEntry( 183 WifiMetricsProto.WifiLog.SCAN_FAILURE_INTERRUPTED, 184 NUM_SCAN_FAILURE_INTERRUPTED); 185 mWifiMetrics.incrementScanReturnEntry( 186 WifiMetricsProto.WifiLog.SCAN_FAILURE_INVALID_CONFIGURATION, 187 NUM_SCAN_FAILURE_INVALID_CONFIGURATION); 188 for (int i = 0; i < NUM_WIFI_UNKNOWN_SCREEN_OFF; i++) { 189 mWifiMetrics.incrementWifiSystemScanStateCount(WifiMetricsProto.WifiLog.WIFI_UNKNOWN, 190 false); 191 } 192 for (int i = 0; i < NUM_WIFI_UNKNOWN_SCREEN_ON; i++) { 193 mWifiMetrics.incrementWifiSystemScanStateCount(WifiMetricsProto.WifiLog.WIFI_UNKNOWN, 194 true); 195 } 196 for (int i = 0; i < NUM_WIFI_ASSOCIATED_SCREEN_OFF; i++) { 197 mWifiMetrics.incrementWifiSystemScanStateCount(WifiMetricsProto.WifiLog.WIFI_ASSOCIATED, 198 false); 199 } 200 for (int i = 0; i < NUM_WIFI_ASSOCIATED_SCREEN_ON; i++) { 201 mWifiMetrics.incrementWifiSystemScanStateCount(WifiMetricsProto.WifiLog.WIFI_ASSOCIATED, 202 true); 203 } 204 for (int i = 0; i < NUM_CONNECTIVITY_WATCHDOG_PNO_GOOD; i++) { 205 mWifiMetrics.incrementNumConnectivityWatchdogPnoGood(); 206 } 207 for (int i = 0; i < NUM_CONNECTIVITY_WATCHDOG_PNO_BAD; i++) { 208 mWifiMetrics.incrementNumConnectivityWatchdogPnoBad(); 209 } 210 for (int i = 0; i < NUM_CONNECTIVITY_WATCHDOG_BACKGROUND_GOOD; i++) { 211 mWifiMetrics.incrementNumConnectivityWatchdogBackgroundGood(); 212 } 213 for (int i = 0; i < NUM_CONNECTIVITY_WATCHDOG_BACKGROUND_BAD; i++) { 214 mWifiMetrics.incrementNumConnectivityWatchdogBackgroundBad(); 215 } 216 for (int i = 0; i < NUM_LAST_RESORT_WATCHDOG_TRIGGERS; i++) { 217 mWifiMetrics.incrementNumLastResortWatchdogTriggers(); 218 } 219 mWifiMetrics.addCountToNumLastResortWatchdogBadAssociationNetworksTotal( 220 NUM_LAST_RESORT_WATCHDOG_BAD_ASSOCIATION_NETWORKS_TOTAL); 221 mWifiMetrics.addCountToNumLastResortWatchdogBadAuthenticationNetworksTotal( 222 NUM_LAST_RESORT_WATCHDOG_BAD_AUTHENTICATION_NETWORKS_TOTAL); 223 mWifiMetrics.addCountToNumLastResortWatchdogBadDhcpNetworksTotal( 224 NUM_LAST_RESORT_WATCHDOG_BAD_DHCP_NETWORKS_TOTAL); 225 mWifiMetrics.addCountToNumLastResortWatchdogBadOtherNetworksTotal( 226 NUM_LAST_RESORT_WATCHDOG_BAD_OTHER_NETWORKS_TOTAL); 227 mWifiMetrics.addCountToNumLastResortWatchdogAvailableNetworksTotal( 228 NUM_LAST_RESORT_WATCHDOG_AVAILABLE_NETWORKS_TOTAL); 229 for (int i = 0; i < NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_ASSOCIATION; i++) { 230 mWifiMetrics.incrementNumLastResortWatchdogTriggersWithBadAssociation(); 231 } 232 for (int i = 0; i < NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_AUTHENTICATION; i++) { 233 mWifiMetrics.incrementNumLastResortWatchdogTriggersWithBadAuthentication(); 234 } 235 for (int i = 0; i < NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_DHCP; i++) { 236 mWifiMetrics.incrementNumLastResortWatchdogTriggersWithBadDhcp(); 237 } 238 for (int i = 0; i < NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_OTHER; i++) { 239 mWifiMetrics.incrementNumLastResortWatchdogTriggersWithBadOther(); 240 } 241 for (int i = 0; i < NUM_RSSI_LEVELS_TO_INCREMENT; i++) { 242 for (int j = 0; j <= i; j++) { 243 mWifiMetrics.incrementRssiPollRssiCount(FIRST_RSSI_LEVEL + i); 244 } 245 } 246 } 247 248 /** 249 * Assert that values in deserializedWifiMetrics match those set in 'setAndIncrementMetrics' 250 */ 251 public void assertDeserializedMetricsCorrect() throws Exception { 252 assertEquals("mDeserializedWifiMetrics.numSavedNetworks == NUM_SAVED_NETWORKS", 253 mDeserializedWifiMetrics.numSavedNetworks, NUM_SAVED_NETWORKS); 254 assertEquals("mDeserializedWifiMetrics.numOpenNetworks == NUM_OPEN_NETWORKS", 255 mDeserializedWifiMetrics.numOpenNetworks, NUM_OPEN_NETWORKS); 256 assertEquals("mDeserializedWifiMetrics.numPersonalNetworks == NUM_PERSONAL_NETWORKS", 257 mDeserializedWifiMetrics.numPersonalNetworks, NUM_PERSONAL_NETWORKS); 258 assertEquals("mDeserializedWifiMetrics.numEnterpriseNetworks " 259 + "== NUM_ENTERPRISE_NETWORKS", 260 mDeserializedWifiMetrics.numEnterpriseNetworks, NUM_ENTERPRISE_NETWORKS); 261 assertEquals("mDeserializedWifiMetrics.numNetworksAddedByUser " 262 + "== NUM_NEWTORKS_ADDED_BY_USER", 263 mDeserializedWifiMetrics.numNetworksAddedByUser, NUM_NEWTORKS_ADDED_BY_USER); 264 assertEquals("mDeserializedWifiMetrics.numNetworksAddedByApps " 265 + "== NUM_NEWTORKS_ADDED_BY_APPS", 266 mDeserializedWifiMetrics.numNetworksAddedByApps, NUM_NEWTORKS_ADDED_BY_APPS); 267 assertEquals("mDeserializedWifiMetrics.isLocationEnabled == TEST_VAL_IS_LOCATION_ENABLED", 268 mDeserializedWifiMetrics.isLocationEnabled, TEST_VAL_IS_LOCATION_ENABLED); 269 assertEquals("mDeserializedWifiMetrics.isScanningAlwaysEnabled " 270 + "== IS_SCANNING_ALWAYS_ENABLED", 271 mDeserializedWifiMetrics.isScanningAlwaysEnabled, IS_SCANNING_ALWAYS_ENABLED); 272 assertEquals("mDeserializedWifiMetrics.numEmptyScanResults == NUM_EMPTY_SCAN_RESULTS", 273 mDeserializedWifiMetrics.numEmptyScanResults, NUM_EMPTY_SCAN_RESULTS); 274 assertEquals("mDeserializedWifiMetrics.numNonEmptyScanResults == " 275 + "NUM_NON_EMPTY_SCAN_RESULTS", 276 mDeserializedWifiMetrics.numNonEmptyScanResults, NUM_NON_EMPTY_SCAN_RESULTS); 277 assertScanReturnEntryEquals(WifiMetricsProto.WifiLog.SCAN_UNKNOWN, 278 NUM_SCAN_UNKNOWN); 279 assertScanReturnEntryEquals(WifiMetricsProto.WifiLog.SCAN_SUCCESS, 280 NUM_SCAN_SUCCESS); 281 assertScanReturnEntryEquals(WifiMetricsProto.WifiLog.SCAN_FAILURE_INTERRUPTED, 282 NUM_SCAN_FAILURE_INTERRUPTED); 283 assertScanReturnEntryEquals(WifiMetricsProto.WifiLog.SCAN_FAILURE_INVALID_CONFIGURATION, 284 NUM_SCAN_FAILURE_INVALID_CONFIGURATION); 285 assertSystemStateEntryEquals(WifiMetricsProto.WifiLog.WIFI_UNKNOWN, false, 286 NUM_WIFI_UNKNOWN_SCREEN_OFF); 287 assertSystemStateEntryEquals(WifiMetricsProto.WifiLog.WIFI_UNKNOWN, true, 288 NUM_WIFI_UNKNOWN_SCREEN_ON); 289 assertSystemStateEntryEquals( 290 WifiMetricsProto.WifiLog.WIFI_ASSOCIATED, false, NUM_WIFI_ASSOCIATED_SCREEN_OFF); 291 assertSystemStateEntryEquals(WifiMetricsProto.WifiLog.WIFI_ASSOCIATED, true, 292 NUM_WIFI_ASSOCIATED_SCREEN_ON); 293 assertEquals(mDeserializedWifiMetrics.numConnectivityWatchdogPnoGood, 294 NUM_CONNECTIVITY_WATCHDOG_PNO_GOOD); 295 assertEquals(mDeserializedWifiMetrics.numConnectivityWatchdogPnoBad, 296 NUM_CONNECTIVITY_WATCHDOG_PNO_BAD); 297 assertEquals(mDeserializedWifiMetrics.numConnectivityWatchdogBackgroundGood, 298 NUM_CONNECTIVITY_WATCHDOG_BACKGROUND_GOOD); 299 assertEquals(mDeserializedWifiMetrics.numConnectivityWatchdogBackgroundBad, 300 NUM_CONNECTIVITY_WATCHDOG_BACKGROUND_BAD); 301 assertEquals(NUM_LAST_RESORT_WATCHDOG_TRIGGERS, 302 mDeserializedWifiMetrics.numLastResortWatchdogTriggers); 303 assertEquals(NUM_LAST_RESORT_WATCHDOG_BAD_ASSOCIATION_NETWORKS_TOTAL, 304 mDeserializedWifiMetrics.numLastResortWatchdogBadAssociationNetworksTotal); 305 assertEquals(NUM_LAST_RESORT_WATCHDOG_BAD_AUTHENTICATION_NETWORKS_TOTAL, 306 mDeserializedWifiMetrics.numLastResortWatchdogBadAuthenticationNetworksTotal); 307 assertEquals(NUM_LAST_RESORT_WATCHDOG_BAD_DHCP_NETWORKS_TOTAL, 308 mDeserializedWifiMetrics.numLastResortWatchdogBadDhcpNetworksTotal); 309 assertEquals(NUM_LAST_RESORT_WATCHDOG_BAD_OTHER_NETWORKS_TOTAL, 310 mDeserializedWifiMetrics.numLastResortWatchdogBadOtherNetworksTotal); 311 assertEquals(NUM_LAST_RESORT_WATCHDOG_AVAILABLE_NETWORKS_TOTAL, 312 mDeserializedWifiMetrics.numLastResortWatchdogAvailableNetworksTotal); 313 assertEquals(NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_ASSOCIATION, 314 mDeserializedWifiMetrics.numLastResortWatchdogTriggersWithBadAssociation); 315 assertEquals(NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_AUTHENTICATION, 316 mDeserializedWifiMetrics.numLastResortWatchdogTriggersWithBadAuthentication); 317 assertEquals(NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_DHCP, 318 mDeserializedWifiMetrics.numLastResortWatchdogTriggersWithBadDhcp); 319 assertEquals(NUM_LAST_RESORT_WATCHDOG_TRIGGERS_WITH_BAD_OTHER, 320 mDeserializedWifiMetrics.numLastResortWatchdogTriggersWithBadOther); 321 assertEquals(TEST_RECORD_DURATION_SEC, 322 mDeserializedWifiMetrics.recordDurationSec); 323 for (int i = 0; i < NUM_RSSI_LEVELS_TO_INCREMENT; i++) { 324 assertEquals(FIRST_RSSI_LEVEL + i, mDeserializedWifiMetrics.rssiPollRssiCount[i].rssi); 325 assertEquals(i + 1, mDeserializedWifiMetrics.rssiPollRssiCount[i].count); 326 } 327 } 328 329 /** 330 * Assert deserialized metrics Scan Return Entry equals count 331 */ 332 public void assertScanReturnEntryEquals(int returnCode, int count) { 333 for (int i = 0; i < mDeserializedWifiMetrics.scanReturnEntries.length; i++) { 334 if (mDeserializedWifiMetrics.scanReturnEntries[i].scanReturnCode == returnCode) { 335 assertEquals(mDeserializedWifiMetrics.scanReturnEntries[i].scanResultsCount, count); 336 return; 337 } 338 } 339 assertEquals(null, count); 340 } 341 342 /** 343 * Assert deserialized metrics SystemState entry equals count 344 */ 345 public void assertSystemStateEntryEquals(int state, boolean screenOn, int count) { 346 for (int i = 0; i < mDeserializedWifiMetrics.wifiSystemStateEntries.length; i++) { 347 if (mDeserializedWifiMetrics.wifiSystemStateEntries[i].wifiState == state 348 && mDeserializedWifiMetrics.wifiSystemStateEntries[i].isScreenOn == screenOn) { 349 assertEquals(mDeserializedWifiMetrics.wifiSystemStateEntries[i].wifiStateCount, 350 count); 351 return; 352 } 353 } 354 assertEquals(null, count); 355 } 356 /** 357 * Combination of all other WifiMetrics unit tests, an internal-integration test, or functional 358 * test 359 */ 360 @Test 361 public void setMetricsSerializeDeserializeAssertMetricsSame() throws Exception { 362 setAndIncrementMetrics(); 363 startAndEndConnectionEventSucceeds(); 364 dumpProtoAndDeserialize(); 365 assertDeserializedMetricsCorrect(); 366 assertEquals("mDeserializedWifiMetrics.connectionEvent.length", 367 2, mDeserializedWifiMetrics.connectionEvent.length); 368 //<TODO> test individual connectionEvents for correctness, 369 // check scanReturnEntries & wifiSystemStateEntries counts and individual elements 370 // pending their implementation</TODO> 371 } 372 373 private static final String SSID = "red"; 374 private static final int CONFIG_DTIM = 3; 375 private static final int NETWORK_DETAIL_WIFIMODE = 5; 376 private static final int NETWORK_DETAIL_DTIM = 7; 377 private static final int SCAN_RESULT_LEVEL = -30; 378 /** 379 * Test that WifiMetrics is correctly getting data from ScanDetail and WifiConfiguration 380 */ 381 @Test 382 public void testScanDetailAndWifiConfigurationUsage() throws Exception { 383 //Setup mock configs and scan details 384 NetworkDetail networkDetail = mock(NetworkDetail.class); 385 when(networkDetail.getWifiMode()).thenReturn(NETWORK_DETAIL_WIFIMODE); 386 when(networkDetail.getSSID()).thenReturn(SSID); 387 when(networkDetail.getDtimInterval()).thenReturn(NETWORK_DETAIL_DTIM); 388 ScanResult scanResult = mock(ScanResult.class); 389 scanResult.level = SCAN_RESULT_LEVEL; 390 WifiConfiguration config = mock(WifiConfiguration.class); 391 config.SSID = "\"" + SSID + "\""; 392 config.dtimInterval = CONFIG_DTIM; 393 WifiConfiguration.NetworkSelectionStatus networkSelectionStat = 394 mock(WifiConfiguration.NetworkSelectionStatus.class); 395 when(networkSelectionStat.getCandidate()).thenReturn(scanResult); 396 when(config.getNetworkSelectionStatus()).thenReturn(networkSelectionStat); 397 ScanDetail scanDetail = mock(ScanDetail.class); 398 when(scanDetail.getNetworkDetail()).thenReturn(networkDetail); 399 when(scanDetail.getScanResult()).thenReturn(scanResult); 400 401 //Create a connection event using only the config 402 mWifiMetrics.startConnectionEvent(config, "Red", 403 WifiMetricsProto.ConnectionEvent.ROAM_NONE); 404 mWifiMetrics.endConnectionEvent( 405 WifiMetrics.ConnectionEvent.FAILURE_NONE, 406 WifiMetricsProto.ConnectionEvent.HLF_NONE); 407 408 //Create a connection event using the config and a scan detail 409 mWifiMetrics.startConnectionEvent(config, "Green", 410 WifiMetricsProto.ConnectionEvent.ROAM_NONE); 411 mWifiMetrics.setConnectionScanDetail(scanDetail); 412 mWifiMetrics.endConnectionEvent( 413 WifiMetrics.ConnectionEvent.FAILURE_NONE, 414 WifiMetricsProto.ConnectionEvent.HLF_NONE); 415 416 //Dump proto from mWifiMetrics and deserialize it to mDeserializedWifiMetrics 417 dumpProtoAndDeserialize(); 418 419 //Check that the correct values are being flowed through 420 assertEquals(mDeserializedWifiMetrics.connectionEvent.length, 2); 421 assertEquals(mDeserializedWifiMetrics.connectionEvent[0].routerFingerprint.dtim, 422 CONFIG_DTIM); 423 assertEquals(mDeserializedWifiMetrics.connectionEvent[0].signalStrength, SCAN_RESULT_LEVEL); 424 assertEquals(mDeserializedWifiMetrics.connectionEvent[1].routerFingerprint.dtim, 425 NETWORK_DETAIL_DTIM); 426 assertEquals(mDeserializedWifiMetrics.connectionEvent[1].signalStrength, 427 SCAN_RESULT_LEVEL); 428 assertEquals(mDeserializedWifiMetrics.connectionEvent[1].routerFingerprint.routerTechnology, 429 NETWORK_DETAIL_WIFIMODE); 430 } 431 432 /** 433 * Test that WifiMetrics is being cleared after dumping via proto 434 */ 435 @Test 436 public void testMetricsClearedAfterProtoRequested() throws Exception { 437 // Create 3 ConnectionEvents 438 mWifiMetrics.startConnectionEvent(null, "RED", 439 WifiMetricsProto.ConnectionEvent.ROAM_ENTERPRISE); 440 mWifiMetrics.endConnectionEvent( 441 WifiMetrics.ConnectionEvent.FAILURE_NONE, 442 WifiMetricsProto.ConnectionEvent.HLF_NONE); 443 mWifiMetrics.startConnectionEvent(null, "YELLOW", 444 WifiMetricsProto.ConnectionEvent.ROAM_ENTERPRISE); 445 mWifiMetrics.endConnectionEvent( 446 WifiMetrics.ConnectionEvent.FAILURE_NONE, 447 WifiMetricsProto.ConnectionEvent.HLF_NONE); 448 mWifiMetrics.startConnectionEvent(null, "GREEN", 449 WifiMetricsProto.ConnectionEvent.ROAM_ENTERPRISE); 450 mWifiMetrics.endConnectionEvent( 451 WifiMetrics.ConnectionEvent.FAILURE_NONE, 452 WifiMetricsProto.ConnectionEvent.HLF_NONE); 453 mWifiMetrics.startConnectionEvent(null, "ORANGE", 454 WifiMetricsProto.ConnectionEvent.ROAM_ENTERPRISE); 455 mWifiMetrics.endConnectionEvent( 456 WifiMetrics.ConnectionEvent.FAILURE_NONE, 457 WifiMetricsProto.ConnectionEvent.HLF_NONE); 458 459 //Dump proto and deserialize 460 //This should clear all the metrics in mWifiMetrics, 461 dumpProtoAndDeserialize(); 462 //Check there are only 3 connection events 463 assertEquals(mDeserializedWifiMetrics.connectionEvent.length, 4); 464 465 // Create 2 ConnectionEvents 466 mWifiMetrics.startConnectionEvent(null, "BLUE", 467 WifiMetricsProto.ConnectionEvent.ROAM_ENTERPRISE); 468 mWifiMetrics.endConnectionEvent( 469 WifiMetrics.ConnectionEvent.FAILURE_NONE, 470 WifiMetricsProto.ConnectionEvent.HLF_NONE); 471 mWifiMetrics.startConnectionEvent(null, "RED", 472 WifiMetricsProto.ConnectionEvent.ROAM_ENTERPRISE); 473 mWifiMetrics.endConnectionEvent( 474 WifiMetrics.ConnectionEvent.FAILURE_NONE, 475 WifiMetricsProto.ConnectionEvent.HLF_NONE); 476 477 //Dump proto and deserialize 478 dumpProtoAndDeserialize(); 479 //Check there are only 2 connection events 480 assertEquals(mDeserializedWifiMetrics.connectionEvent.length, 2); 481 } 482} 483