diff options
author | Oscar Shu <xshu@google.com> | 2019-06-20 18:26:10 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2019-06-20 18:26:10 +0000 |
commit | 00398ec6ea8334a706a046e723d4fe8b2855adf2 (patch) | |
tree | d2eef66cfdfc766b37e5bf83c7455a3073c955a3 /tests | |
parent | 1b9909b212b93ab2c08026a8a2eecb11bb7d25d6 (diff) | |
parent | fae988bc1a3ff26b83e8d52a4893364ccd9898d5 (diff) |
Merge "Trigger bugreport for abnormally long connections" into qt-r1-dev
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java | 115 |
1 files changed, 113 insertions, 2 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java b/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java index 9ae382640..1117339c6 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiLastResortWatchdogTest.java @@ -21,16 +21,24 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.*; import static org.mockito.MockitoAnnotations.*; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.IntentFilter; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiSsid; +import android.os.Handler; import android.os.test.TestLooper; import android.util.Pair; import androidx.test.filters.SmallTest; +import com.google.android.gsf.Gservices; + import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatcher; import org.mockito.Mock; import java.util.ArrayList; @@ -42,6 +50,8 @@ import java.util.List; */ @SmallTest public class WifiLastResortWatchdogTest { + final ArgumentCaptor<BroadcastReceiver> mBroadcastReceiverCaptor = + ArgumentCaptor.forClass(BroadcastReceiver.class); WifiLastResortWatchdog mLastResortWatchdog; @Mock WifiInjector mWifiInjector; @Mock WifiMetrics mWifiMetrics; @@ -49,6 +59,8 @@ public class WifiLastResortWatchdogTest { @Mock ClientModeImpl mClientModeImpl; @Mock Clock mClock; @Mock WifiInfo mWifiInfo; + @Mock Context mContext; + @Mock GservicesFacade mGservicesFacade; private String[] mSsids = {"\"test1\"", "\"test2\"", "\"test3\"", "\"test4\""}; private String[] mBssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4", "de:ad:ba:b1:e5:55", @@ -61,17 +73,22 @@ public class WifiLastResortWatchdogTest { private boolean[] mHasEverConnected = {false, false, false, false}; private TestLooper mLooper; private static final String TEST_NETWORK_SSID = "\"test_ssid\""; + private static final int DEFAULT_ABNORMAL_CONNECTION_DURATION_MS = 30000; @Before public void setUp() throws Exception { initMocks(this); mLooper = new TestLooper(); when(mWifiInjector.getSelfRecovery()).thenReturn(mSelfRecovery); - mLastResortWatchdog = new WifiLastResortWatchdog(mWifiInjector, mClock, mWifiMetrics, - mClientModeImpl, mLooper.getLooper()); + when(mGservicesFacade.isAbnormalConnectionBugreportEnabled()).thenReturn(true); + when(mGservicesFacade.getAbnormalConnectionDurationMs()).thenReturn( + DEFAULT_ABNORMAL_CONNECTION_DURATION_MS); + mLastResortWatchdog = new WifiLastResortWatchdog(mWifiInjector, mContext, mClock, + mWifiMetrics, mClientModeImpl, mLooper.getLooper(), mGservicesFacade); mLastResortWatchdog.setBugReportProbability(1); when(mClientModeImpl.getWifiInfo()).thenReturn(mWifiInfo); when(mWifiInfo.getSSID()).thenReturn(TEST_NETWORK_SSID); + when(mWifiInjector.getClientModeImplHandler()).thenReturn(mLastResortWatchdog.getHandler()); } private List<Pair<ScanDetail, WifiConfiguration>> createFilteredQnsCandidates(String[] ssids, @@ -2152,4 +2169,98 @@ public class WifiLastResortWatchdogTest { verify(mWifiMetrics, times(1)).incrementNumLastResortWatchdogSuccesses(); } + /** + * Verifies that when a connection takes too long (time difference between + * StaEvent.TYPE_CMD_START_CONNECT and StaEvent.TYPE_NETWORK_CONNECTION_EVENT) a bugreport is + * taken. + */ + @Test + public void testAbnormalConnectionTimeTriggersBugreport() throws Exception { + // first verifies that bugreports are not taken when connection takes less than + // DEFAULT_ABNORMAL_CONNECTION_DURATION_MS + when(mClock.getElapsedSinceBootMillis()).thenReturn(1L); + mLastResortWatchdog.noteStartConnectTime(); + when(mClock.getElapsedSinceBootMillis()).thenReturn( + (long) DEFAULT_ABNORMAL_CONNECTION_DURATION_MS); + Handler handler = mLastResortWatchdog.getHandler(); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl, never()).takeBugReport(anyString(), anyString()); + + // Now verify that bugreport is taken + mLastResortWatchdog.noteStartConnectTime(); + when(mClock.getElapsedSinceBootMillis()).thenReturn( + 2L * DEFAULT_ABNORMAL_CONNECTION_DURATION_MS + 1); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl).takeBugReport(anyString(), anyString()); + + // Verify additional connections (without more TYPE_CMD_START_CONNECT) don't trigger more + // bugreports. + when(mClock.getElapsedSinceBootMillis()).thenReturn( + 4L * DEFAULT_ABNORMAL_CONNECTION_DURATION_MS); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl).takeBugReport(anyString(), anyString()); + } + + /** + * Changes |mAbnormalConnectionDurationMs| to a new value, and then verify that a bugreport is + * taken for a connection that takes longer than the new threshold. + * @throws Exception + */ + @Test + public void testGServicesSetDuration() throws Exception { + final int testDurationMs = 10 * 1000; // 10 seconds + // changes the abnormal connection duration to |testDurationMs|. + when(mGservicesFacade.getAbnormalConnectionDurationMs()).thenReturn(testDurationMs); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + (IntentFilter) argThat(new IntentFilterMatcher())); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, null); + + // verifies that bugreport is taken for connections that take longer than |testDurationMs|. + when(mClock.getElapsedSinceBootMillis()).thenReturn(1L); + mLastResortWatchdog.noteStartConnectTime(); + when(mClock.getElapsedSinceBootMillis()).thenReturn((long) testDurationMs + 2); + Handler handler = mLastResortWatchdog.getHandler(); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl).takeBugReport(anyString(), anyString()); + } + + /** + * Verifies that bugreports are not triggered even when conditions are met after the + * |mAbnormalConnectionBugreportEnabled| flag is changed to false. + * @throws Exception + */ + @Test + public void testGServicesFlagDisable() throws Exception { + // changes |mAbnormalConnectionBugreportEnabled| to false. + when(mGservicesFacade.isAbnormalConnectionBugreportEnabled()).thenReturn(false); + verify(mContext).registerReceiver(mBroadcastReceiverCaptor.capture(), + (IntentFilter) argThat(new IntentFilterMatcher())); + mBroadcastReceiverCaptor.getValue().onReceive(mContext, null); + + // verifies that bugreports are not taken. + when(mClock.getElapsedSinceBootMillis()).thenReturn(1L); + mLastResortWatchdog.noteStartConnectTime(); + when(mClock.getElapsedSinceBootMillis()).thenReturn( + (long) DEFAULT_ABNORMAL_CONNECTION_DURATION_MS + 2); + Handler handler = mLastResortWatchdog.getHandler(); + handler.sendMessage( + handler.obtainMessage(WifiMonitor.NETWORK_CONNECTION_EVENT, 0, 0, null)); + mLooper.dispatchAll(); + verify(mClientModeImpl, never()).takeBugReport(anyString(), anyString()); + } + + private class IntentFilterMatcher implements ArgumentMatcher<IntentFilter> { + @Override + public boolean matches(IntentFilter filter) { + return filter.hasAction(Gservices.CHANGED_ACTION); + } + } } |