From b32b649c815b0b50fd0127d9d4a4563c54a536fb Mon Sep 17 00:00:00 2001 From: Randy Pan Date: Mon, 16 May 2016 10:58:20 -0700 Subject: WifiConnectivityManagerTest: exponential backoff periodic scan Add a couple of unit tests to cover the periodic scan exponential backoff feature. Extended MockAlarmManager class such that users can query a pending alarm's trigger time. Bug: 28750989 Change-Id: I9520477180786fce2937e5fa87f790f60f12a3da --- .../com/android/server/wifi/MockAlarmManager.java | 18 ++++ .../server/wifi/WifiConnectivityManagerTest.java | 99 ++++++++++++++++++++++ 2 files changed, 117 insertions(+) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/MockAlarmManager.java b/tests/wifitests/src/com/android/server/wifi/MockAlarmManager.java index 2bb06cc30..02af28122 100644 --- a/tests/wifitests/src/com/android/server/wifi/MockAlarmManager.java +++ b/tests/wifitests/src/com/android/server/wifi/MockAlarmManager.java @@ -90,6 +90,20 @@ public class MockAlarmManager { return false; } + /** + * @return trigger time of an pending alarm with the given tag + * -1 if no pending alram with the given tag + */ + public long getTriggerTimeMillis(String tag) { + for (int i = 0; i < mPendingAlarms.size(); ++i) { + PendingAlarm alarm = mPendingAlarms.get(i); + if (Objects.equals(tag, alarm.getTag())) { + return alarm.getTriggerTimeMillis(); + } + } + return -1; + } + private static class PendingAlarm { private final int mType; private final long mTriggerAtMillis; @@ -116,6 +130,10 @@ public class MockAlarmManager { public String getTag() { return mTag; } + + public long getTriggerTimeMillis() { + return mTriggerAtMillis; + } } private class SetListenerAnswer extends AnswerWithArguments { diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 872430fd8..2127a0145 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -102,6 +102,7 @@ public class WifiConnectivityManagerTest { private static final String CANDIDATE_SSID = "\"AnSsid\""; private static final String CANDIDATE_BSSID = "6c:f3:7f:ae:8c:f3"; private static final String TAG = "WifiConnectivityManager Unit Test"; + private static final long CURRENT_SYSTEM_TIME_MS = 1000; Resources mockResource() { Resources resource = mock(Resources.class); @@ -519,4 +520,102 @@ public class WifiConnectivityManagerTest { verify(mWifiMetrics).incrementNumConnectivityWatchdogPnoGood(); verify(mWifiMetrics, never()).incrementNumConnectivityWatchdogPnoBad(); } + + /** + * Verify that scan interval for screen on and wifi disconnected scenario + * is in the exponential backoff fashion. + * + * Expected behavior: WifiConnectivityManager doubles periodic + * scan interval. + */ + @Test + public void checkPeriodicScanIntervalWhenDisconnected() { + when(mClock.currentTimeMillis()).thenReturn(CURRENT_SYSTEM_TIME_MS); + + // Set screen to ON + mWifiConnectivityManager.handleScreenStateChanged(true); + + // Set WiFi to disconnected state to trigger periodic scan + mWifiConnectivityManager.handleConnectionStateChanged( + WifiConnectivityManager.WIFI_STATE_DISCONNECTED); + + // Get the first periodic scan interval + long firstIntervalMs = mAlarmManager + .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG) + - CURRENT_SYSTEM_TIME_MS; + assertEquals(firstIntervalMs, WifiConnectivityManager.PERIODIC_SCAN_INTERVAL_MS); + + // Now fire the first periodic scan alarm timer + mAlarmManager.dispatch(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG); + mLooper.dispatchAll(); + + // Get the second periodic scan interval + long secondIntervalMs = mAlarmManager + .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG) + - CURRENT_SYSTEM_TIME_MS; + + // Verify the intervals are exponential back off + assertEquals(firstIntervalMs * 2, secondIntervalMs); + + // Make sure we eventually stay at the maximum scan interval. + long intervalMs = 0; + for (int i = 0; i < 5; i++) { + mAlarmManager.dispatch(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG); + mLooper.dispatchAll(); + intervalMs = mAlarmManager + .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG) + - CURRENT_SYSTEM_TIME_MS; + } + + assertEquals(intervalMs, WifiConnectivityManager.MAX_PERIODIC_SCAN_INTERVAL_MS); + } + + /** + * Verify that scan interval for screen on and wifi connected scenario + * is in the exponential backoff fashion. + * + * Expected behavior: WifiConnectivityManager doubles periodic + * scan interval. + */ + @Test + public void checkPeriodicScanIntervalWhenConnected() { + when(mClock.currentTimeMillis()).thenReturn(CURRENT_SYSTEM_TIME_MS); + + // Set screen to ON + mWifiConnectivityManager.handleScreenStateChanged(true); + + // Set WiFi to connected state to trigger periodic scan + mWifiConnectivityManager.handleConnectionStateChanged( + WifiConnectivityManager.WIFI_STATE_CONNECTED); + + // Get the first periodic scan interval + long firstIntervalMs = mAlarmManager + .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG) + - CURRENT_SYSTEM_TIME_MS; + assertEquals(firstIntervalMs, WifiConnectivityManager.PERIODIC_SCAN_INTERVAL_MS); + + // Now fire the first periodic scan alarm timer + mAlarmManager.dispatch(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG); + mLooper.dispatchAll(); + + // Get the second periodic scan interval + long secondIntervalMs = mAlarmManager + .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG) + - CURRENT_SYSTEM_TIME_MS; + + // Verify the intervals are exponential back off + assertEquals(firstIntervalMs * 2, secondIntervalMs); + + // Make sure we eventually stay at the maximum scan interval. + long intervalMs = 0; + for (int i = 0; i < 5; i++) { + mAlarmManager.dispatch(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG); + mLooper.dispatchAll(); + intervalMs = mAlarmManager + .getTriggerTimeMillis(WifiConnectivityManager.PERIODIC_SCAN_TIMER_TAG) + - CURRENT_SYSTEM_TIME_MS; + } + + assertEquals(intervalMs, WifiConnectivityManager.MAX_PERIODIC_SCAN_INTERVAL_MS); + } } -- cgit v1.2.3