From a4a48ef75937964487c345c948dcdfcf91e41e0f Mon Sep 17 00:00:00 2001 From: David Su Date: Tue, 17 Mar 2020 17:39:35 -0700 Subject: [PATCH] CTS: Test PNO Scanning Test that PNO scans reconnects us to a saved network when Wifi is disconnected and screen is off. This also adds coverage for WifiNl80211Manager.startPnoScan. Bug: 150978929 Test: atest android.net.wifi.cts.WifiManagerTest Change-Id: Id0487f4df48ea013d67e19faecd96b5a469f09da --- .../android/net/wifi/cts/WifiManagerTest.java | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java b/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java index 51a4f32a64..fc4f2eaaac 100644 --- a/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java +++ b/tests/cts/net/src/android/net/wifi/cts/WifiManagerTest.java @@ -81,9 +81,11 @@ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Objects; +import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.Executor; @@ -1212,8 +1214,12 @@ public class WifiManagerTest extends AndroidTestCase { Thread.sleep(DURATION_SCREEN_TOGGLE); } - private void turnScreenOff() throws Exception { + private void turnScreenOffNoDelay() throws Exception { mUiDevice.executeShellCommand("input keyevent KEYCODE_SLEEP"); + } + + private void turnScreenOff() throws Exception { + turnScreenOffNoDelay(); // Since the screen on/off intent is ordered, they will not be sent right now. Thread.sleep(DURATION_SCREEN_TOGGLE); } @@ -1799,6 +1805,59 @@ public class WifiManagerTest extends AndroidTestCase { mWifiManager.isPreferredNetworkOffloadSupported(); } + /** Test that PNO scans reconnects us when the device is disconnected and the screen is off. */ + public void testPnoScan() throws Exception { + if (!WifiFeature.isWifiSupported(getContext())) { + // skip the test if WiFi is not supported + return; + } + if (!mWifiManager.isPreferredNetworkOffloadSupported()) { + // skip the test if PNO scanning is not supported + return; + } + + // make sure we're connected + waitForConnection(); + + WifiInfo currentNetwork = ShellIdentityUtils.invokeWithShellPermissions( + mWifiManager::getConnectionInfo); + + // disable all networks that aren't already disabled + List savedNetworks = ShellIdentityUtils.invokeWithShellPermissions( + mWifiManager::getConfiguredNetworks); + Set disabledNetworkIds = new HashSet<>(); + for (WifiConfiguration config : savedNetworks) { + if (config.getNetworkSelectionStatus().getNetworkSelectionDisableReason() + == WifiConfiguration.NetworkSelectionStatus.DISABLED_NONE) { + ShellIdentityUtils.invokeWithShellPermissions( + () -> mWifiManager.disableNetwork(config.networkId)); + disabledNetworkIds.add(config.networkId); + } + } + + try { + // wait for disconnection from current network + waitForDisconnection(); + + // turn screen off + turnScreenOffNoDelay(); + + // re-enable the current network - this will trigger PNO + ShellIdentityUtils.invokeWithShellPermissions( + () -> mWifiManager.enableNetwork(currentNetwork.getNetworkId(), false)); + disabledNetworkIds.remove(currentNetwork.getNetworkId()); + + // PNO should reconnect us back to the network we disconnected from + waitForConnection(); + } finally { + // re-enable disabled networks + for (int disabledNetworkId : disabledNetworkIds) { + ShellIdentityUtils.invokeWithShellPermissions( + () -> mWifiManager.enableNetwork(disabledNetworkId, false)); + } + } + } + /** * Tests {@link WifiManager#isTdlsSupported()} does not crash. */