Try reconnecting to wifi if it doesn't automatically connect.
When meteredness of wifi changes during test, wifi disconnects. Try reconnecting if it automatically connects after this. Also, change the way we update metereness so that we can use callback mechanism to wait for the state change instead of polling for it regularly. Bug: 181686645 Test: atest ./tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java Merged-In: I31fb127ef333d39fe4697043876c7cef15d525e3 Change-Id: Icc0b37730201aebeab2713c6527a3195f6dcf97d
This commit is contained in:
@@ -22,12 +22,13 @@ import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELI
|
|||||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
|
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
|
||||||
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
||||||
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
||||||
|
import static android.net.wifi.WifiConfiguration.METERED_OVERRIDE_METERED;
|
||||||
|
import static android.net.wifi.WifiConfiguration.METERED_OVERRIDE_NONE;
|
||||||
|
|
||||||
import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
|
import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
|
||||||
import static com.android.cts.net.hostside.AbstractRestrictBackgroundNetworkTestCase.TAG;
|
import static com.android.cts.net.hostside.AbstractRestrictBackgroundNetworkTestCase.TAG;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
@@ -35,19 +36,21 @@ import static org.junit.Assert.fail;
|
|||||||
|
|
||||||
import android.app.ActivityManager;
|
import android.app.ActivityManager;
|
||||||
import android.app.Instrumentation;
|
import android.app.Instrumentation;
|
||||||
|
import android.app.UiAutomation;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.ConnectivityManager.NetworkCallback;
|
import android.net.ConnectivityManager.NetworkCallback;
|
||||||
import android.net.Network;
|
import android.net.Network;
|
||||||
import android.net.NetworkCapabilities;
|
import android.net.NetworkCapabilities;
|
||||||
|
import android.net.wifi.WifiConfiguration;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
|
import android.net.wifi.WifiManager.ActionListener;
|
||||||
import android.os.PersistableBundle;
|
import android.os.PersistableBundle;
|
||||||
import android.os.Process;
|
import android.os.Process;
|
||||||
import android.telephony.CarrierConfigManager;
|
import android.telephony.CarrierConfigManager;
|
||||||
import android.telephony.SubscriptionManager;
|
import android.telephony.SubscriptionManager;
|
||||||
import android.telephony.data.ApnSetting;
|
import android.telephony.data.ApnSetting;
|
||||||
import android.text.TextUtils;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.test.platform.app.InstrumentationRegistry;
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
@@ -57,7 +60,12 @@ import com.android.compatibility.common.util.BatteryUtils;
|
|||||||
import com.android.compatibility.common.util.ShellIdentityUtils;
|
import com.android.compatibility.common.util.ShellIdentityUtils;
|
||||||
import com.android.compatibility.common.util.ThrowingRunnable;
|
import com.android.compatibility.common.util.ThrowingRunnable;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class NetworkPolicyTestUtils {
|
public class NetworkPolicyTestUtils {
|
||||||
@@ -182,19 +190,14 @@ public class NetworkPolicyTestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static String getWifiSsid() {
|
private static String getWifiSsid() {
|
||||||
final boolean isLocationEnabled = isLocationEnabled();
|
final UiAutomation uiAutomation = getInstrumentation().getUiAutomation();
|
||||||
try {
|
try {
|
||||||
if (!isLocationEnabled) {
|
uiAutomation.adoptShellPermissionIdentity();
|
||||||
setLocationEnabled(true);
|
final String ssid = getWifiManager().getConnectionInfo().getSSID();
|
||||||
}
|
|
||||||
final String ssid = unquoteSSID(getWifiManager().getConnectionInfo().getSSID());
|
|
||||||
assertNotEquals(WifiManager.UNKNOWN_SSID, ssid);
|
assertNotEquals(WifiManager.UNKNOWN_SSID, ssid);
|
||||||
return ssid;
|
return ssid;
|
||||||
} finally {
|
} finally {
|
||||||
// Reset the location enabled state
|
uiAutomation.dropShellPermissionIdentity();
|
||||||
if (!isLocationEnabled) {
|
|
||||||
setLocationEnabled(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,18 +208,71 @@ public class NetworkPolicyTestUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void setWifiMeteredStatus(String ssid, boolean metered) throws Exception {
|
private static void setWifiMeteredStatus(String ssid, boolean metered) throws Exception {
|
||||||
assertFalse("SSID should not be empty", TextUtils.isEmpty(ssid));
|
final UiAutomation uiAutomation = getInstrumentation().getUiAutomation();
|
||||||
final String cmd = "cmd netpolicy set metered-network " + ssid + " " + metered;
|
try {
|
||||||
executeShellCommand(cmd);
|
uiAutomation.adoptShellPermissionIdentity();
|
||||||
assertWifiMeteredStatus(ssid, metered);
|
final WifiConfiguration currentConfig = getWifiConfiguration(ssid);
|
||||||
assertActiveNetworkMetered(metered);
|
currentConfig.meteredOverride = metered
|
||||||
|
? METERED_OVERRIDE_METERED : METERED_OVERRIDE_NONE;
|
||||||
|
BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();
|
||||||
|
getWifiManager().save(currentConfig, createActionListener(
|
||||||
|
blockingQueue, Integer.MAX_VALUE));
|
||||||
|
Integer resultCode = blockingQueue.poll(TIMEOUT_CHANGE_METEREDNESS_MS,
|
||||||
|
TimeUnit.MILLISECONDS);
|
||||||
|
if (resultCode == null) {
|
||||||
|
fail("Timed out waiting for meteredness to change; ssid=" + ssid
|
||||||
|
+ ", metered=" + metered);
|
||||||
|
} else if (resultCode != Integer.MAX_VALUE) {
|
||||||
|
fail("Error overriding the meteredness; ssid=" + ssid
|
||||||
|
+ ", metered=" + metered + ", error=" + resultCode);
|
||||||
|
}
|
||||||
|
final boolean success = assertActiveNetworkMetered(metered, false /* throwOnFailure */);
|
||||||
|
if (!success) {
|
||||||
|
Log.i(TAG, "Retry connecting to wifi; ssid=" + ssid);
|
||||||
|
blockingQueue = new LinkedBlockingQueue<>();
|
||||||
|
getWifiManager().connect(currentConfig, createActionListener(
|
||||||
|
blockingQueue, Integer.MAX_VALUE));
|
||||||
|
resultCode = blockingQueue.poll(TIMEOUT_CHANGE_METEREDNESS_MS,
|
||||||
|
TimeUnit.MILLISECONDS);
|
||||||
|
if (resultCode == null) {
|
||||||
|
fail("Timed out waiting for wifi to connect; ssid=" + ssid);
|
||||||
|
} else if (resultCode != Integer.MAX_VALUE) {
|
||||||
|
fail("Error connecting to wifi; ssid=" + ssid
|
||||||
|
+ ", error=" + resultCode);
|
||||||
|
}
|
||||||
|
assertActiveNetworkMetered(metered, true /* throwOnFailure */);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
uiAutomation.dropShellPermissionIdentity();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertWifiMeteredStatus(String ssid, boolean expectedMeteredStatus) {
|
private static WifiConfiguration getWifiConfiguration(String ssid) {
|
||||||
final String result = executeShellCommand("cmd netpolicy list wifi-networks");
|
final List<String> ssids = new ArrayList<>();
|
||||||
final String expectedLine = ssid + ";" + expectedMeteredStatus;
|
for (WifiConfiguration config : getWifiManager().getConfiguredNetworks()) {
|
||||||
assertTrue("Expected line: " + expectedLine + "; Actual result: " + result,
|
if (config.SSID.equals(ssid)) {
|
||||||
result.contains(expectedLine));
|
return config;
|
||||||
|
}
|
||||||
|
ssids.add(config.SSID);
|
||||||
|
}
|
||||||
|
fail("Couldn't find the wifi config; ssid=" + ssid
|
||||||
|
+ ", all=" + Arrays.toString(ssids.toArray()));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ActionListener createActionListener(BlockingQueue<Integer> blockingQueue,
|
||||||
|
int successCode) {
|
||||||
|
return new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess() {
|
||||||
|
blockingQueue.offer(successCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(int reason) {
|
||||||
|
blockingQueue.offer(reason);
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setCellularMeteredStatus(int subId, boolean metered) throws Exception {
|
private static void setCellularMeteredStatus(int subId, boolean metered) throws Exception {
|
||||||
@@ -225,11 +281,11 @@ public class NetworkPolicyTestUtils {
|
|||||||
new String[] {ApnSetting.TYPE_MMS_STRING});
|
new String[] {ApnSetting.TYPE_MMS_STRING});
|
||||||
ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(getCarrierConfigManager(),
|
ShellIdentityUtils.invokeMethodWithShellPermissionsNoReturn(getCarrierConfigManager(),
|
||||||
(cm) -> cm.overrideConfig(subId, metered ? null : bundle));
|
(cm) -> cm.overrideConfig(subId, metered ? null : bundle));
|
||||||
assertActiveNetworkMetered(metered);
|
assertActiveNetworkMetered(metered, true /* throwOnFailure */);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copied from cts/tests/tests/net/src/android/net/cts/ConnectivityManagerTest.java
|
private static boolean assertActiveNetworkMetered(boolean expectedMeteredStatus,
|
||||||
private static void assertActiveNetworkMetered(boolean expectedMeteredStatus) throws Exception {
|
boolean throwOnFailure) throws Exception {
|
||||||
final CountDownLatch latch = new CountDownLatch(1);
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
final NetworkCallback networkCallback = new NetworkCallback() {
|
final NetworkCallback networkCallback = new NetworkCallback() {
|
||||||
@Override
|
@Override
|
||||||
@@ -246,10 +302,16 @@ public class NetworkPolicyTestUtils {
|
|||||||
getConnectivityManager().registerDefaultNetworkCallback(networkCallback);
|
getConnectivityManager().registerDefaultNetworkCallback(networkCallback);
|
||||||
try {
|
try {
|
||||||
if (!latch.await(TIMEOUT_CHANGE_METEREDNESS_MS, TimeUnit.MILLISECONDS)) {
|
if (!latch.await(TIMEOUT_CHANGE_METEREDNESS_MS, TimeUnit.MILLISECONDS)) {
|
||||||
fail("Timed out waiting for active network metered status to change to "
|
final String errorMsg = "Timed out waiting for active network metered status "
|
||||||
+ expectedMeteredStatus + "; network = "
|
+ "to change to " + expectedMeteredStatus + "; network = "
|
||||||
+ getConnectivityManager().getActiveNetwork());
|
+ getConnectivityManager().getActiveNetwork();
|
||||||
|
if (throwOnFailure) {
|
||||||
|
fail(errorMsg);
|
||||||
}
|
}
|
||||||
|
Log.w(TAG, errorMsg);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
} finally {
|
} finally {
|
||||||
getConnectivityManager().unregisterNetworkCallback(networkCallback);
|
getConnectivityManager().unregisterNetworkCallback(networkCallback);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user