Merge "Toggle wifi when running CaptivePortalTest" am: 1ae290c1a6
Original change: https://android-review.googlesource.com/c/platform/cts/+/1336114 Change-Id: I4a878a7d328a7a001355e092b81e1e3635dcb853
This commit is contained in:
@@ -41,6 +41,7 @@ import android.text.TextUtils
|
|||||||
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
|
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
|
||||||
import androidx.test.runner.AndroidJUnit4
|
import androidx.test.runner.AndroidJUnit4
|
||||||
import com.android.compatibility.common.util.SystemUtil
|
import com.android.compatibility.common.util.SystemUtil
|
||||||
|
import com.android.testutils.isDevSdkInRange
|
||||||
import fi.iki.elonen.NanoHTTPD
|
import fi.iki.elonen.NanoHTTPD
|
||||||
import fi.iki.elonen.NanoHTTPD.Response.IStatus
|
import fi.iki.elonen.NanoHTTPD.Response.IStatus
|
||||||
import fi.iki.elonen.NanoHTTPD.Response.Status
|
import fi.iki.elonen.NanoHTTPD.Response.Status
|
||||||
@@ -105,6 +106,9 @@ class CaptivePortalTest {
|
|||||||
@After
|
@After
|
||||||
fun tearDown() {
|
fun tearDown() {
|
||||||
clearTestUrls()
|
clearTestUrls()
|
||||||
|
if (pm.hasSystemFeature(FEATURE_WIFI)) {
|
||||||
|
reconnectWifi()
|
||||||
|
}
|
||||||
server.stop()
|
server.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +171,7 @@ class CaptivePortalTest {
|
|||||||
assertNotEquals(network, cm.activeNetwork, wifiDefaultMessage)
|
assertNotEquals(network, cm.activeNetwork, wifiDefaultMessage)
|
||||||
|
|
||||||
val startPortalAppPermission =
|
val startPortalAppPermission =
|
||||||
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) CONNECTIVITY_INTERNAL
|
if (isDevSdkInRange(0, Build.VERSION_CODES.Q)) CONNECTIVITY_INTERNAL
|
||||||
else NETWORK_SETTINGS
|
else NETWORK_SETTINGS
|
||||||
doAsShell(startPortalAppPermission) { cm.startCaptivePortalApp(network) }
|
doAsShell(startPortalAppPermission) { cm.startCaptivePortalApp(network) }
|
||||||
assertTrue(portalContentRequestCv.block(TEST_TIMEOUT_MS), "The captive portal login " +
|
assertTrue(portalContentRequestCv.block(TEST_TIMEOUT_MS), "The captive portal login " +
|
||||||
@@ -180,9 +184,6 @@ class CaptivePortalTest {
|
|||||||
// disconnectFromCell should be called after connectToCell
|
// disconnectFromCell should be called after connectToCell
|
||||||
utils.disconnectFromCell()
|
utils.disconnectFromCell()
|
||||||
}
|
}
|
||||||
|
|
||||||
clearTestUrls()
|
|
||||||
reconnectWifi()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setHttpsUrl(url: String?) = setConfig(TEST_CAPTIVE_PORTAL_HTTPS_URL_SETTING, url)
|
private fun setHttpsUrl(url: String?) = setConfig(TEST_CAPTIVE_PORTAL_HTTPS_URL_SETTING, url)
|
||||||
@@ -203,10 +204,8 @@ class CaptivePortalTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun reconnectWifi() {
|
private fun reconnectWifi() {
|
||||||
doAsShell(NETWORK_SETTINGS) {
|
utils.ensureWifiDisconnected(null /* wifiNetworkToCheck */)
|
||||||
assertTrue(wm.disconnect())
|
utils.ensureWifiConnected()
|
||||||
assertTrue(wm.reconnect())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -157,8 +157,36 @@ public final class CtsNetUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Enable WiFi and wait for it to become connected to a network. */
|
/**
|
||||||
|
* Enable WiFi and wait for it to become connected to a network.
|
||||||
|
*
|
||||||
|
* This method expects to receive a legacy broadcast on connect, which may not be sent if the
|
||||||
|
* network does not become default or if it is not the first network.
|
||||||
|
*/
|
||||||
public Network connectToWifi() {
|
public Network connectToWifi() {
|
||||||
|
return connectToWifi(true /* expectLegacyBroadcast */);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable WiFi and wait for it to become connected to a network.
|
||||||
|
*
|
||||||
|
* A network is considered connected when a {@link NetworkCallback#onAvailable(Network)}
|
||||||
|
* callback is received.
|
||||||
|
*/
|
||||||
|
public Network ensureWifiConnected() {
|
||||||
|
return connectToWifi(false /* expectLegacyBroadcast */);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable WiFi and wait for it to become connected to a network.
|
||||||
|
*
|
||||||
|
* @param expectLegacyBroadcast Whether to check for a legacy CONNECTIVITY_ACTION connected
|
||||||
|
* broadcast. The broadcast is typically not sent if the network
|
||||||
|
* does not become the default network, and is not the first
|
||||||
|
* network to appear.
|
||||||
|
* @return The network that was newly connected.
|
||||||
|
*/
|
||||||
|
private Network connectToWifi(boolean expectLegacyBroadcast) {
|
||||||
final TestNetworkCallback callback = new TestNetworkCallback();
|
final TestNetworkCallback callback = new TestNetworkCallback();
|
||||||
mCm.registerNetworkCallback(makeWifiNetworkRequest(), callback);
|
mCm.registerNetworkCallback(makeWifiNetworkRequest(), callback);
|
||||||
Network wifiNetwork = null;
|
Network wifiNetwork = null;
|
||||||
@@ -170,15 +198,16 @@ public final class CtsNetUtils {
|
|||||||
mContext.registerReceiver(receiver, filter);
|
mContext.registerReceiver(receiver, filter);
|
||||||
|
|
||||||
boolean connected = false;
|
boolean connected = false;
|
||||||
|
final String err = "Wifi must be configured to connect to an access point for this test.";
|
||||||
try {
|
try {
|
||||||
clearWifiBlacklist();
|
clearWifiBlacklist();
|
||||||
SystemUtil.runShellCommand("svc wifi enable");
|
SystemUtil.runShellCommand("svc wifi enable");
|
||||||
SystemUtil.runWithShellPermissionIdentity(() -> mWifiManager.reconnect(),
|
SystemUtil.runWithShellPermissionIdentity(() -> mWifiManager.reconnect(),
|
||||||
NETWORK_SETTINGS);
|
NETWORK_SETTINGS);
|
||||||
// Ensure we get both an onAvailable callback and a CONNECTIVITY_ACTION.
|
// Ensure we get an onAvailable callback and possibly a CONNECTIVITY_ACTION.
|
||||||
wifiNetwork = callback.waitForAvailable();
|
wifiNetwork = callback.waitForAvailable();
|
||||||
assertNotNull(wifiNetwork);
|
assertNotNull(err, wifiNetwork);
|
||||||
connected = receiver.waitForState();
|
connected = !expectLegacyBroadcast || receiver.waitForState();
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
fail("connectToWifi was interrupted");
|
fail("connectToWifi was interrupted");
|
||||||
} finally {
|
} finally {
|
||||||
@@ -186,8 +215,7 @@ public final class CtsNetUtils {
|
|||||||
mContext.unregisterReceiver(receiver);
|
mContext.unregisterReceiver(receiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
assertTrue("Wifi must be configured to connect to an access point for this test.",
|
assertTrue(err, connected);
|
||||||
connected);
|
|
||||||
return wifiNetwork;
|
return wifiNetwork;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,8 +232,47 @@ public final class CtsNetUtils {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Disable WiFi and wait for it to become disconnected from the network. */
|
/**
|
||||||
|
* Disable WiFi and wait for it to become disconnected from the network.
|
||||||
|
*
|
||||||
|
* This method expects to receive a legacy broadcast on disconnect, which may not be sent if the
|
||||||
|
* network was not default, or was not the first network.
|
||||||
|
*
|
||||||
|
* @param wifiNetworkToCheck If non-null, a network that should be disconnected. This network
|
||||||
|
* is expected to be able to establish a TCP connection to a remote
|
||||||
|
* server before disconnecting, and to have that connection closed in
|
||||||
|
* the process.
|
||||||
|
*/
|
||||||
public void disconnectFromWifi(Network wifiNetworkToCheck) {
|
public void disconnectFromWifi(Network wifiNetworkToCheck) {
|
||||||
|
disconnectFromWifi(wifiNetworkToCheck, true /* expectLegacyBroadcast */);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable WiFi and wait for it to become disconnected from the network.
|
||||||
|
*
|
||||||
|
* @param wifiNetworkToCheck If non-null, a network that should be disconnected. This network
|
||||||
|
* is expected to be able to establish a TCP connection to a remote
|
||||||
|
* server before disconnecting, and to have that connection closed in
|
||||||
|
* the process.
|
||||||
|
*/
|
||||||
|
public void ensureWifiDisconnected(Network wifiNetworkToCheck) {
|
||||||
|
disconnectFromWifi(wifiNetworkToCheck, false /* expectLegacyBroadcast */);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable WiFi and wait for it to become disconnected from the network.
|
||||||
|
*
|
||||||
|
* @param wifiNetworkToCheck If non-null, a network that should be disconnected. This network
|
||||||
|
* is expected to be able to establish a TCP connection to a remote
|
||||||
|
* server before disconnecting, and to have that connection closed in
|
||||||
|
* the process.
|
||||||
|
* @param expectLegacyBroadcast Whether to check for a legacy CONNECTIVITY_ACTION disconnected
|
||||||
|
* broadcast. The broadcast is typically not sent if the network
|
||||||
|
* was not the default network and not the first network to appear.
|
||||||
|
* The check will always be skipped if the device was not connected
|
||||||
|
* to wifi in the first place.
|
||||||
|
*/
|
||||||
|
private void disconnectFromWifi(Network wifiNetworkToCheck, boolean expectLegacyBroadcast) {
|
||||||
final TestNetworkCallback callback = new TestNetworkCallback();
|
final TestNetworkCallback callback = new TestNetworkCallback();
|
||||||
mCm.registerNetworkCallback(makeWifiNetworkRequest(), callback);
|
mCm.registerNetworkCallback(makeWifiNetworkRequest(), callback);
|
||||||
|
|
||||||
@@ -238,6 +305,8 @@ public final class CtsNetUtils {
|
|||||||
// Ensure we get both an onLost callback and a CONNECTIVITY_ACTION.
|
// Ensure we get both an onLost callback and a CONNECTIVITY_ACTION.
|
||||||
assertNotNull("Did not receive onLost callback after disabling wifi",
|
assertNotNull("Did not receive onLost callback after disabling wifi",
|
||||||
callback.waitForLost());
|
callback.waitForLost());
|
||||||
|
}
|
||||||
|
if (wasWifiConnected && expectLegacyBroadcast) {
|
||||||
assertTrue("Wifi failed to reach DISCONNECTED state.", receiver.waitForState());
|
assertTrue("Wifi failed to reach DISCONNECTED state.", receiver.waitForState());
|
||||||
}
|
}
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
|
|||||||
Reference in New Issue
Block a user