diff options
author | Stephen Chen <stewchen@google.com> | 2017-06-06 12:16:18 -0700 |
---|---|---|
committer | Stephen Chen <stewchen@google.com> | 2017-07-24 11:08:23 -0700 |
commit | 3f8483262cf59572d0c0b3c42e72d8ce65a096ff (patch) | |
tree | ca2f60d78435b1fde76b5261c901884430e0673e /tests | |
parent | d739840cdb84bb0312d0cc83f00a4aa64a984949 (diff) |
ONA: Change ONA controller to directly handle scan results from WifiConnectivityManager.
The controller will post and clear ONA notifications based on calls made
from WifiConnectivityManager.
Bug: 38460614
Bug: 37357441
Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Change-Id: Ib3483e8974d37b165249a211306118b83dc4edc4
Diffstat (limited to 'tests')
3 files changed, 185 insertions, 81 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index d4eef041e..11e220e7f 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -63,6 +63,7 @@ import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashSet; +import java.util.List; /** * Unit tests for {@link com.android.server.wifi.WifiConnectivityManager}. @@ -122,6 +123,7 @@ public class WifiConnectivityManagerTest { @Mock private NetworkScoreManager mNetworkScoreManager; @Mock private Clock mClock; @Mock private WifiLastResortWatchdog mWifiLastResortWatchdog; + @Mock private WifiNotificationController mWifiNotificationController; @Mock private WifiMetrics mWifiMetrics; @Mock private WifiNetworkScoreCache mScoreCache; @Captor ArgumentCaptor<ScanResult> mCandidateScanResultCaptor; @@ -292,8 +294,8 @@ public class WifiConnectivityManagerTest { WifiConnectivityManager createConnectivityManager() { return new WifiConnectivityManager(mContext, mWifiStateMachine, mWifiScanner, mWifiConfigManager, mWifiInfo, mWifiNS, mWifiConnectivityHelper, - mWifiLastResortWatchdog, mWifiMetrics, mLooper.getLooper(), mClock, - mLocalLog, true, mFrameworkFacade, null, null, null); + mWifiLastResortWatchdog, mWifiNotificationController, mWifiMetrics, + mLooper.getLooper(), mClock, mLocalLog, true, mFrameworkFacade, null, null, null); } /** @@ -607,6 +609,76 @@ public class WifiConnectivityManagerTest { } /** + * {@link WifiNotificationController} handles scan results on network selection. + * + * Expected behavior: ONA handles scan results + */ + @Test + public void wifiDisconnected_noConnectionCandidate_openNetworkNotificationScanResultsHandled() { + // no connection candidate selected + when(mWifiNS.selectNetwork(anyObject(), anyObject(), anyObject(), anyBoolean(), + anyBoolean(), anyBoolean())).thenReturn(null); + + List<ScanDetail> expectedOpenNetworks = new ArrayList<>(); + expectedOpenNetworks.add( + new ScanDetail( + new ScanResult(WifiSsid.createFromAsciiEncoded(CANDIDATE_SSID), + CANDIDATE_SSID, CANDIDATE_BSSID, 1245, 0, "some caps", -78, 2450, + 1025, 22, 33, 20, 0, 0, true), null)); + + when(mWifiNS.getFilteredScanDetailsForOpenUnsavedNetworks()) + .thenReturn(expectedOpenNetworks); + + // Set WiFi to disconnected state to trigger PNO scan + mWifiConnectivityManager.handleConnectionStateChanged( + WifiConnectivityManager.WIFI_STATE_DISCONNECTED); + + verify(mWifiNotificationController).handleScanResults(expectedOpenNetworks); + } + + /** + * When wifi is connected, {@link WifiNotificationController} tries to clear the pending + * notification and does not reset notification repeat delay. + * + * Expected behavior: ONA clears pending notification and does not reset repeat delay. + */ + @Test + public void wifiConnected_openNetworkNotificationClearsPendingNotification() { + // Set WiFi to connected state + mWifiConnectivityManager.handleConnectionStateChanged( + WifiConnectivityManager.WIFI_STATE_CONNECTED); + + verify(mWifiNotificationController).clearPendingNotification(false /* isRepeatDelayReset*/); + } + + /** + * When wifi is connected, {@link WifiNotificationController} handles connection state + * change. + * + * Expected behavior: ONA does not clear pending notification. + */ + @Test + public void wifiDisconnected_openNetworkNotificationDoesNotClearPendingNotification() { + // Set WiFi to disconnected state + mWifiConnectivityManager.handleConnectionStateChanged( + WifiConnectivityManager.WIFI_STATE_DISCONNECTED); + + verify(mWifiNotificationController, never()).clearPendingNotification(anyBoolean()); + } + + /** + * When Wi-Fi is disabled, clear the pending notification and reset notification repeat delay. + * + * Expected behavior: clear pending notification and reset notification repeat delay + * */ + @Test + public void openNetworkNotificationControllerToggledOnWifiStateChanges() { + mWifiConnectivityManager.setWifiEnabled(false); + + verify(mWifiNotificationController).clearPendingNotification(true /* isRepeatDelayReset */); + } + + /** * Verify that scan interval for screen on and wifi disconnected scenario * is in the exponential backoff fashion. * @@ -1562,4 +1634,19 @@ public class WifiConnectivityManagerTest { mWifiConnectivityManager.dump(new FileDescriptor(), pw, new String[]{}); assertTrue(sw.toString().contains(localLogMessage)); } + + /** + * Dump ONA controller. + * + * Expected behavior: {@link WifiNotificationController#dump(FileDescriptor, PrintWriter, + * String[])} is invoked. + */ + @Test + public void dumpNotificationController() { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + mWifiConnectivityManager.dump(new FileDescriptor(), pw, new String[]{}); + + verify(mWifiNotificationController).dump(any(), any(), any()); + } } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNotificationControllerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNotificationControllerTest.java index f7b3bf6a1..9aa5ee938 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNotificationControllerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNotificationControllerTest.java @@ -16,7 +16,7 @@ package com.android.server.wifi; -import static org.mockito.Mockito.any; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -25,23 +25,16 @@ import static org.mockito.Mockito.when; import android.app.Notification; import android.app.NotificationManager; -import android.content.BroadcastReceiver; import android.content.Context; -import android.content.IntentFilter; import android.content.res.Resources; -import android.net.NetworkInfo; import android.net.wifi.ScanResult; -import android.net.wifi.WifiManager; -import android.net.wifi.WifiScanner; import android.os.UserHandle; import android.os.UserManager; import android.os.test.TestLooper; import android.provider.Settings; -import android.test.suitebuilder.annotation.SmallTest; import org.junit.Before; import org.junit.Test; -import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -49,26 +42,17 @@ import java.util.ArrayList; import java.util.List; /** - * Unit tests for {@link com.android.server.wifi.WifiScanningServiceImpl}. + * Unit tests for {@link WifiNotificationController}. */ -@SmallTest public class WifiNotificationControllerTest { - public static final String TAG = "WifiScanningServiceTest"; @Mock private Context mContext; @Mock private Resources mResources; @Mock private FrameworkFacade mFrameworkFacade; @Mock private NotificationManager mNotificationManager; @Mock private UserManager mUserManager; - @Mock private WifiInjector mWifiInjector; - @Mock private WifiScanner mWifiScanner; - WifiNotificationController mWifiNotificationController; + private WifiNotificationController mNotificationController; - /** - * Internal BroadcastReceiver that WifiNotificationController uses to listen for broadcasts - * this is initialized by calling startServiceAndLoadDriver - */ - BroadcastReceiver mBroadcastReceiver; /** Initialize objects before each test run. */ @Before @@ -76,86 +60,121 @@ public class WifiNotificationControllerTest { MockitoAnnotations.initMocks(this); when(mContext.getSystemService(Context.NOTIFICATION_SERVICE)) .thenReturn(mNotificationManager); - when(mFrameworkFacade.getIntegerSetting(mContext, Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1)).thenReturn(1); - when(mContext.getSystemService(Context.USER_SERVICE)) .thenReturn(mUserManager); when(mContext.getResources()).thenReturn(mResources); - when(mWifiInjector.getWifiScanner()).thenReturn(mWifiScanner); - TestLooper mock_looper = new TestLooper(); - mWifiNotificationController = new WifiNotificationController( + mNotificationController = new WifiNotificationController( mContext, mock_looper.getLooper(), mFrameworkFacade, - mock(Notification.Builder.class), mWifiInjector); - ArgumentCaptor<BroadcastReceiver> broadcastReceiverCaptor = - ArgumentCaptor.forClass(BroadcastReceiver.class); - - verify(mContext) - .registerReceiver(broadcastReceiverCaptor.capture(), any(IntentFilter.class)); - mBroadcastReceiver = broadcastReceiverCaptor.getValue(); + mock(Notification.Builder.class)); } - private void setOpenAccessPoint() { - List<ScanResult> scanResults = new ArrayList<>(); + private List<ScanDetail> createOpenScanResults() { + List<ScanDetail> scanResults = new ArrayList<>(); ScanResult scanResult = new ScanResult(); scanResult.capabilities = "[ESS]"; - scanResults.add(scanResult); - when(mWifiScanner.getSingleScanResults()).thenReturn(scanResults); + scanResults.add(new ScanDetail(scanResult, null /* networkDetail */)); + return scanResults; + } + + /** + * When scan results with open networks are handled, a notification is posted. + */ + @Test + public void handleScanResults_hasOpenNetworks_notificationDisplayed() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mNotificationManager).notifyAsUser(any(), anyInt(), any(), any()); + } + + /** + * When scan results with no open networks are handled, a notification is not posted. + */ + @Test + public void handleScanResults_emptyList_notificationNotDisplayed() { + mNotificationController.handleScanResults(new ArrayList<>()); + + verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any()); } - /** Verifies that a notification is displayed (and retracted) given system events. */ + /** + * When a notification is showing and scan results with no open networks are handled, the + * notification is cleared. + */ @Test - public void verifyNotificationDisplayed() throws Exception { - TestUtil.sendWifiStateChanged(mBroadcastReceiver, mContext, WifiManager.WIFI_STATE_ENABLED); - TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext, - NetworkInfo.DetailedState.DISCONNECTED); - setOpenAccessPoint(); - - // The notification should not be displayed after only two scan results. - TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext); - TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext); - verify(mNotificationManager, never()) - .notifyAsUser(any(), anyInt(), any(), any(UserHandle.class)); - - // Changing to and from "SCANNING" state should not affect the counter. - TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext, - NetworkInfo.DetailedState.SCANNING); - TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext, - NetworkInfo.DetailedState.DISCONNECTED); - - // The third scan result notification will trigger the notification. - TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext); - verify(mNotificationManager) - .notifyAsUser(any(), anyInt(), any(), any(UserHandle.class)); - verify(mNotificationManager, never()) - .cancelAsUser(any(), anyInt(), any(UserHandle.class)); - - // Changing network state should cause the notification to go away. - TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext, - NetworkInfo.DetailedState.CONNECTED); - verify(mNotificationManager) - .cancelAsUser(any(), anyInt(), any(UserHandle.class)); + public void handleScanResults_notificationShown_emptyList_notificationCleared() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mNotificationManager).notifyAsUser(any(), anyInt(), any(), any()); + + mNotificationController.handleScanResults(new ArrayList<>()); + + verify(mNotificationManager).cancelAsUser(any(), anyInt(), any()); } + /** + * If notification is showing, do not post another notification. + */ @Test - public void verifyNotificationNotDisplayed_userHasDisallowConfigWifiRestriction() { + public void handleScanResults_notificationShowing_doesNotRepostNotification() { + mNotificationController.handleScanResults(createOpenScanResults()); + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mNotificationManager).notifyAsUser(any(), anyInt(), any(), any()); + } + + /** + * When {@link WifiNotificationController#clearPendingNotification(boolean)} is called and a + * notification is shown, clear the notification. + */ + @Test + public void clearPendingNotification_clearsNotificationIfOneIsShowing() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mNotificationManager).notifyAsUser(any(), anyInt(), any(), any()); + + mNotificationController.clearPendingNotification(true); + + verify(mNotificationManager).cancelAsUser(any(), anyInt(), any()); + } + + /** + * When {@link WifiNotificationController#clearPendingNotification(boolean)} is called and a + * notification was not previously shown, do not clear the notification. + */ + @Test + public void clearPendingNotification_doesNotClearNotificationIfNoneShowing() { + mNotificationController.clearPendingNotification(true); + + verify(mNotificationManager, never()).cancelAsUser(any(), anyInt(), any()); + } + + /** Verifies that {@link UserManager#DISALLOW_CONFIG_WIFI} disables the feature. */ + @Test + public void userHasDisallowConfigWifiRestriction_notificationNotDisplayed() { when(mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, UserHandle.CURRENT)) .thenReturn(true); - TestUtil.sendWifiStateChanged(mBroadcastReceiver, mContext, WifiManager.WIFI_STATE_ENABLED); - TestUtil.sendNetworkStateChanged(mBroadcastReceiver, mContext, - NetworkInfo.DetailedState.DISCONNECTED); - setOpenAccessPoint(); + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any()); + } + + /** Verifies that {@link UserManager#DISALLOW_CONFIG_WIFI} clears the showing notification. */ + @Test + public void userHasDisallowConfigWifiRestriction_showingNotificationIsCleared() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mNotificationManager).notifyAsUser(any(), anyInt(), any(), any()); + + when(mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, UserHandle.CURRENT)) + .thenReturn(true); - // The notification should be displayed after three scan results. - TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext); - TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext); - TestUtil.sendScanResultsAvailable(mBroadcastReceiver, mContext); + mNotificationController.handleScanResults(createOpenScanResults()); - verify(mNotificationManager, never()) - .notifyAsUser(any(), anyInt(), any(), any(UserHandle.class)); + verify(mNotificationManager).cancelAsUser(any(), anyInt(), any()); } } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index ce4556d50..3b11d28c9 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -161,7 +161,6 @@ public class WifiServiceImplTest { @Mock ActivityManager mActivityManager; @Mock AppOpsManager mAppOpsManager; @Mock IBinder mAppBinder; - @Mock WifiNotificationController mWifiNotificationController; @Mock LocalOnlyHotspotRequestInfo mRequestInfo; @Mock LocalOnlyHotspotRequestInfo mRequestInfo2; @@ -278,7 +277,6 @@ public class WifiServiceImplTest { when(mWifiInjector.getWifiPermissionsUtil()).thenReturn(mWifiPermissionsUtil); when(mWifiInjector.getWifiSettingsStore()).thenReturn(mSettingsStore); when(mWifiInjector.getClock()).thenReturn(mClock); - when(mWifiInjector.getWifiNotificationController()).thenReturn(mWifiNotificationController); mWifiServiceImpl = new WifiServiceImpl(mContext, mWifiInjector, mAsyncChannel); mWifiServiceImpl.setWifiHandlerLogForTest(mLog); } |