diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2017-08-16 20:31:41 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-08-16 20:31:41 +0000 |
commit | 69fa2e7fa12b8f4c24a27b3291a0744cd808a324 (patch) | |
tree | ba39a6d972ed25f35ed653e3b1308a6a74811b19 /tests | |
parent | 0af8182c7a421108aff122440724ecc5d1e4c6d7 (diff) | |
parent | 579066a6f282b9d6729a93b7cce3b18980a0a61d (diff) |
Merge changes Ifa1eef1f,If59ecbd3,Ie3e640f6 into oc-mr1-dev
* changes:
ONA: Refactor notification builder and register broadcast receiver.
ONA: Rename WifiNotificationController to OpenNetworkNotifier
ONA: Recommend a network using rssi strength.
Diffstat (limited to 'tests')
4 files changed, 433 insertions, 221 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/OpenNetworkNotifierTest.java b/tests/wifitests/src/com/android/server/wifi/OpenNetworkNotifierTest.java new file mode 100644 index 000000000..29c068ddc --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/OpenNetworkNotifierTest.java @@ -0,0 +1,322 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static com.android.server.wifi.OpenNetworkNotifier.DEFAULT_REPEAT_DELAY_SEC; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +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.Intent; +import android.content.res.Resources; +import android.net.wifi.ScanResult; +import android.os.UserHandle; +import android.os.UserManager; +import android.os.test.TestLooper; +import android.provider.Settings; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Answers; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.List; + +/** + * Unit tests for {@link OpenNetworkNotifier}. + */ +public class OpenNetworkNotifierTest { + + private static final String TEST_SSID_1 = "Test SSID 1"; + private static final int MIN_RSSI_LEVEL = -127; + + @Mock private Context mContext; + @Mock private Resources mResources; + @Mock private FrameworkFacade mFrameworkFacade; + @Mock private Clock mClock; + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Notification.Builder mNotificationBuilder; + @Mock private NotificationManager mNotificationManager; + @Mock private OpenNetworkRecommender mOpenNetworkRecommender; + @Mock private UserManager mUserManager; + private OpenNetworkNotifier mNotificationController; + private BroadcastReceiver mBroadcastReceiver; + private ScanResult mDummyNetwork; + + + /** Initialize objects before each test run. */ + @Before + public void setUp() throws Exception { + 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(mFrameworkFacade.getIntegerSetting(mContext, + Settings.Global.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, DEFAULT_REPEAT_DELAY_SEC)) + .thenReturn(DEFAULT_REPEAT_DELAY_SEC); + when(mFrameworkFacade.makeNotificationBuilder(any(), anyString())) + .thenReturn(mNotificationBuilder); + when(mContext.getSystemService(Context.USER_SERVICE)) + .thenReturn(mUserManager); + when(mContext.getResources()).thenReturn(mResources); + mDummyNetwork = new ScanResult(); + mDummyNetwork.SSID = TEST_SSID_1; + mDummyNetwork.capabilities = "[ESS]"; + mDummyNetwork.level = MIN_RSSI_LEVEL; + when(mOpenNetworkRecommender.recommendNetwork(any(), any())).thenReturn(mDummyNetwork); + + TestLooper mock_looper = new TestLooper(); + mNotificationController = new OpenNetworkNotifier( + mContext, mock_looper.getLooper(), mFrameworkFacade, + mClock, mOpenNetworkRecommender); + ArgumentCaptor<BroadcastReceiver> broadcastReceiverCaptor = + ArgumentCaptor.forClass(BroadcastReceiver.class); + verify(mContext).registerReceiver(broadcastReceiverCaptor.capture(), any(), any(), any()); + mBroadcastReceiver = broadcastReceiverCaptor.getValue(); + mNotificationController.handleScreenStateChanged(true); + } + + private List<ScanDetail> createOpenScanResults() { + List<ScanDetail> scanResults = new ArrayList<>(); + scanResults.add(new ScanDetail(mDummyNetwork, 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(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), 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(mOpenNetworkRecommender, never()).recommendNetwork(any(), any()); + verify(mNotificationManager, never()).notify(anyInt(), any()); + } + + /** + * When a notification is showing and scan results with no open networks are handled, the + * notification is cleared. + */ + @Test + public void handleScanResults_notificationShown_emptyList_notificationCleared() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + + mNotificationController.handleScanResults(new ArrayList<>()); + + verify(mNotificationManager).cancel(anyInt()); + } + /** + * When a notification is showing, screen is off, and scan results with no open networks are + * handled, the notification is cleared. + */ + @Test + public void handleScanResults_notificationShown_screenOff_emptyList_notificationCleared() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + + mNotificationController.handleScreenStateChanged(false); + mNotificationController.handleScanResults(new ArrayList<>()); + + verify(mNotificationManager).cancel(anyInt()); + } + + /** + * If notification is showing, do not post another notification. + */ + @Test + public void handleScanResults_notificationShowing_doesNotRepostNotification() { + mNotificationController.handleScanResults(createOpenScanResults()); + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + } + + /** + * When {@link OpenNetworkNotifier#clearPendingNotification(boolean)} is called and a + * notification is shown, clear the notification. + */ + @Test + public void clearPendingNotification_clearsNotificationIfOneIsShowing() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + + mNotificationController.clearPendingNotification(true); + + verify(mNotificationManager).cancel(anyInt()); + } + + /** + * When {@link OpenNetworkNotifier#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()).cancel(anyInt()); + } + + /** + * When screen is off and notification is not displayed, notification is not posted on handling + * new scan results with open networks. + */ + @Test + public void screenOff_handleScanResults_notificationNotDisplayed() { + mNotificationController.handleScreenStateChanged(false); + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender, never()).recommendNetwork(any(), any()); + verify(mNotificationManager, never()).notify(anyInt(), any()); + } + + /** + * When a notification is posted and cleared without reseting delay, the next scan with open + * networks should not post another notification. + */ + @Test + public void postNotification_clearNotificationWithoutDelayReset_shouldNotPostNotification() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + + mNotificationController.clearPendingNotification(false); + + mNotificationController.handleScanResults(createOpenScanResults()); + + // Recommendation made twice but no new notification posted. + verify(mOpenNetworkRecommender, times(2)).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + verify(mNotificationManager).cancel(anyInt()); + } + + /** + * When a notification is posted and cleared without reseting delay, the next scan with open + * networks should post a notification. + */ + @Test + public void postNotification_clearNotificationWithDelayReset_shouldPostNotification() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + + mNotificationController.clearPendingNotification(true); + + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender, times(2)).recommendNetwork(any(), any()); + verify(mNotificationManager, times(2)).notify(anyInt(), any()); + } + + /** + * When a notification is tapped, open Wi-Fi settings. + */ + @Test + public void notificationTap_opensWifiSettings() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + + mBroadcastReceiver.onReceive( + mContext, new Intent(OpenNetworkNotifier.ACTION_USER_TAPPED_CONTENT)); + + verify(mContext).startActivity(any()); + } + + /** + * When a notification is posted and cleared without reseting delay, after the delay has passed + * the next scan with open networks should post a notification. + */ + @Test + public void delaySet_delayPassed_shouldPostNotification() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + + mNotificationController.clearPendingNotification(false); + + // twice the delay time passed + when(mClock.getWallClockMillis()).thenReturn(DEFAULT_REPEAT_DELAY_SEC * 1000L * 2); + + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender, times(2)).recommendNetwork(any(), any()); + verify(mNotificationManager, times(2)).notify(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); + + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender, never()).recommendNetwork(any(), any()); + verify(mNotificationManager, never()).notify(anyInt(), any()); + } + + /** Verifies that {@link UserManager#DISALLOW_CONFIG_WIFI} clears the showing notification. */ + @Test + public void userHasDisallowConfigWifiRestriction_showingNotificationIsCleared() { + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mOpenNetworkRecommender).recommendNetwork(any(), any()); + verify(mNotificationManager).notify(anyInt(), any()); + + when(mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, UserHandle.CURRENT)) + .thenReturn(true); + + mNotificationController.handleScanResults(createOpenScanResults()); + + verify(mNotificationManager).cancel(anyInt()); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/OpenNetworkRecommenderTest.java b/tests/wifitests/src/com/android/server/wifi/OpenNetworkRecommenderTest.java new file mode 100644 index 000000000..becc1d2e8 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/OpenNetworkRecommenderTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static org.junit.Assert.assertEquals; + +import android.net.wifi.ScanResult; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +/** + * Tests for {@link OpenNetworkRecommender}. + */ +public class OpenNetworkRecommenderTest { + + private static final String TEST_SSID_1 = "Test SSID 1"; + private static final String TEST_SSID_2 = "Test SSID 2"; + private static final int MIN_RSSI_LEVEL = -127; + + private OpenNetworkRecommender mOpenNetworkRecommender; + + @Before + public void setUp() throws Exception { + mOpenNetworkRecommender = new OpenNetworkRecommender(); + } + + private List<ScanDetail> createOpenScanResults(String... ssids) { + List<ScanDetail> scanResults = new ArrayList<>(); + for (String ssid : ssids) { + ScanResult scanResult = new ScanResult(); + scanResult.SSID = ssid; + scanResult.capabilities = "[ESS]"; + scanResults.add(new ScanDetail(scanResult, null /* networkDetail */)); + } + return scanResults; + } + + /** If list of open networks contain only one network, that network should be returned. */ + @Test + public void onlyNetworkIsRecommended() { + List<ScanDetail> scanResults = createOpenScanResults(TEST_SSID_1); + scanResults.get(0).getScanResult().level = MIN_RSSI_LEVEL; + + ScanResult actual = mOpenNetworkRecommender.recommendNetwork(scanResults, null); + ScanResult expected = scanResults.get(0).getScanResult(); + assertEquals(expected, actual); + } + + /** Verifies that the network with the highest rssi is recommended. */ + @Test + public void networkWithHighestRssiIsRecommended() { + List<ScanDetail> scanResults = createOpenScanResults(TEST_SSID_1, TEST_SSID_2); + scanResults.get(0).getScanResult().level = MIN_RSSI_LEVEL; + scanResults.get(1).getScanResult().level = MIN_RSSI_LEVEL + 1; + + ScanResult actual = mOpenNetworkRecommender.recommendNetwork(scanResults, null); + ScanResult expected = scanResults.get(1).getScanResult(); + assertEquals(expected, actual); + } + + /** + * If the current recommended network is present in the list for the next recommendation and has + * an equal RSSI, the recommendation should not change. + */ + @Test + public void currentRecommendationHasEquallyHighRssi_shouldNotChangeRecommendation() { + List<ScanDetail> scanResults = createOpenScanResults(TEST_SSID_1, TEST_SSID_2); + scanResults.get(0).getScanResult().level = MIN_RSSI_LEVEL + 1; + scanResults.get(1).getScanResult().level = MIN_RSSI_LEVEL + 1; + + ScanResult currentRecommendation = new ScanResult(scanResults.get(1).getScanResult()); + // next recommendation does not depend on the rssi of the input recommendation. + currentRecommendation.level = MIN_RSSI_LEVEL; + + ScanResult expected = scanResults.get(1).getScanResult(); + ScanResult actual = mOpenNetworkRecommender.recommendNetwork( + scanResults, currentRecommendation); + assertEquals(expected, actual); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index bdb6b14ed..a8278d365 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -123,7 +123,7 @@ public class WifiConnectivityManagerTest { @Mock private NetworkScoreManager mNetworkScoreManager; @Mock private Clock mClock; @Mock private WifiLastResortWatchdog mWifiLastResortWatchdog; - @Mock private WifiNotificationController mWifiNotificationController; + @Mock private OpenNetworkNotifier mOpenNetworkNotifier; @Mock private WifiMetrics mWifiMetrics; @Mock private WifiNetworkScoreCache mScoreCache; @Captor ArgumentCaptor<ScanResult> mCandidateScanResultCaptor; @@ -294,7 +294,7 @@ public class WifiConnectivityManagerTest { WifiConnectivityManager createConnectivityManager() { return new WifiConnectivityManager(mContext, mWifiStateMachine, mWifiScanner, mWifiConfigManager, mWifiInfo, mWifiNS, mWifiConnectivityHelper, - mWifiLastResortWatchdog, mWifiNotificationController, mWifiMetrics, + mWifiLastResortWatchdog, mOpenNetworkNotifier, mWifiMetrics, mLooper.getLooper(), mClock, mLocalLog, true, mFrameworkFacade, null, null, null); } @@ -609,7 +609,7 @@ public class WifiConnectivityManagerTest { } /** - * {@link WifiNotificationController} handles scan results on network selection. + * {@link OpenNetworkNotifier} handles scan results on network selection. * * Expected behavior: ONA handles scan results */ @@ -633,11 +633,11 @@ public class WifiConnectivityManagerTest { mWifiConnectivityManager.handleConnectionStateChanged( WifiConnectivityManager.WIFI_STATE_DISCONNECTED); - verify(mWifiNotificationController).handleScanResults(expectedOpenNetworks); + verify(mOpenNetworkNotifier).handleScanResults(expectedOpenNetworks); } /** - * When wifi is connected, {@link WifiNotificationController} tries to clear the pending + * When wifi is connected, {@link OpenNetworkNotifier} 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. @@ -648,11 +648,11 @@ public class WifiConnectivityManagerTest { mWifiConnectivityManager.handleConnectionStateChanged( WifiConnectivityManager.WIFI_STATE_CONNECTED); - verify(mWifiNotificationController).clearPendingNotification(false /* isRepeatDelayReset*/); + verify(mOpenNetworkNotifier).clearPendingNotification(false /* isRepeatDelayReset*/); } /** - * When wifi is connected, {@link WifiNotificationController} handles connection state + * When wifi is connected, {@link OpenNetworkNotifier} handles connection state * change. * * Expected behavior: ONA does not clear pending notification. @@ -663,7 +663,7 @@ public class WifiConnectivityManagerTest { mWifiConnectivityManager.handleConnectionStateChanged( WifiConnectivityManager.WIFI_STATE_DISCONNECTED); - verify(mWifiNotificationController, never()).clearPendingNotification(anyBoolean()); + verify(mOpenNetworkNotifier, never()).clearPendingNotification(anyBoolean()); } /** @@ -675,7 +675,7 @@ public class WifiConnectivityManagerTest { public void openNetworkNotificationControllerToggledOnWifiStateChanges() { mWifiConnectivityManager.setWifiEnabled(false); - verify(mWifiNotificationController).clearPendingNotification(true /* isRepeatDelayReset */); + verify(mOpenNetworkNotifier).clearPendingNotification(true /* isRepeatDelayReset */); } /** @@ -685,11 +685,11 @@ public class WifiConnectivityManagerTest { public void openNetworkNotificationControllerTracksScreenStateChanges() { mWifiConnectivityManager.handleScreenStateChanged(false); - verify(mWifiNotificationController).handleScreenStateChanged(false); + verify(mOpenNetworkNotifier).handleScreenStateChanged(false); mWifiConnectivityManager.handleScreenStateChanged(true); - verify(mWifiNotificationController).handleScreenStateChanged(true); + verify(mOpenNetworkNotifier).handleScreenStateChanged(true); } /** @@ -1652,7 +1652,7 @@ public class WifiConnectivityManagerTest { /** * Dump ONA controller. * - * Expected behavior: {@link WifiNotificationController#dump(FileDescriptor, PrintWriter, + * Expected behavior: {@link OpenNetworkNotifier#dump(FileDescriptor, PrintWriter, * String[])} is invoked. */ @Test @@ -1661,6 +1661,6 @@ public class WifiConnectivityManagerTest { PrintWriter pw = new PrintWriter(sw); mWifiConnectivityManager.dump(new FileDescriptor(), pw, new String[]{}); - verify(mWifiNotificationController).dump(any(), any(), any()); + verify(mOpenNetworkNotifier).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 deleted file mode 100644 index 27055a885..000000000 --- a/tests/wifitests/src/com/android/server/wifi/WifiNotificationControllerTest.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Copyright (C) 2016 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.server.wifi; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.anyInt; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.app.Notification; -import android.app.NotificationManager; -import android.content.Context; -import android.content.res.Resources; -import android.net.wifi.ScanResult; -import android.os.UserHandle; -import android.os.UserManager; -import android.os.test.TestLooper; -import android.provider.Settings; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.util.ArrayList; -import java.util.List; - -/** - * Unit tests for {@link WifiNotificationController}. - */ -public class WifiNotificationControllerTest { - - @Mock private Context mContext; - @Mock private Resources mResources; - @Mock private FrameworkFacade mFrameworkFacade; - @Mock private NotificationManager mNotificationManager; - @Mock private UserManager mUserManager; - private WifiNotificationController mNotificationController; - - - /** Initialize objects before each test run. */ - @Before - public void setUp() throws Exception { - 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); - - TestLooper mock_looper = new TestLooper(); - mNotificationController = new WifiNotificationController( - mContext, mock_looper.getLooper(), mFrameworkFacade, - mock(Notification.Builder.class)); - mNotificationController.handleScreenStateChanged(true); - } - - private List<ScanDetail> createOpenScanResults() { - List<ScanDetail> scanResults = new ArrayList<>(); - ScanResult scanResult = new ScanResult(); - scanResult.capabilities = "[ESS]"; - 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()); - } - - /** - * When a notification is showing and scan results with no open networks are handled, the - * notification is cleared. - */ - @Test - 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()); - } - /** - * When a notification is showing, screen is off, and scan results with no open networks are - * handled, the notification is cleared. - */ - @Test - public void handleScanResults_notificationShown_screenOff_emptyList_notificationCleared() { - mNotificationController.handleScanResults(createOpenScanResults()); - - verify(mNotificationManager).notifyAsUser(any(), anyInt(), any(), any()); - - mNotificationController.handleScreenStateChanged(false); - mNotificationController.handleScanResults(new ArrayList<>()); - - verify(mNotificationManager).cancelAsUser(any(), anyInt(), any()); - } - - /** - * If notification is showing, do not post another notification. - */ - @Test - 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()); - } - - /** - * When screen is off and notification is not displayed, notification is not posted on handling - * new scan results with open networks. - */ - @Test - public void screenOff_handleScanResults_notificationNotDisplayed() { - mNotificationController.handleScreenStateChanged(false); - mNotificationController.handleScanResults(createOpenScanResults()); - - verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), 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); - - 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); - - mNotificationController.handleScanResults(createOpenScanResults()); - - verify(mNotificationManager).cancelAsUser(any(), anyInt(), any()); - } -} |