Provide a way to override the avoid bad wifi configuration

ConnectivityManager.setAvoidUnvalidated only works if the
config_networkAvoidBadWifi configuration is set to 0 and the
NETWORK_AVOID_BAD_WIFI setting is unset. There is no easy way
for a testing app to temporary set a test value to verify the
behavior of the API. Thus, add a mechanism to allow test app
to set a period of time to temporary unstrict the resource
configuration, i.e. Temporary simulate config_networkAvoidBadWifi
configured to 0.

Bug: 186061922
Test: atest CtsNetTestCases FrameworksNetTests
Change-Id: If772078c61a9b12926f104d5dfc9c9071e844732
This commit is contained in:
Chiachang Wang
2021-06-17 22:11:30 +08:00
parent 27586f8210
commit 6eac9fb787
5 changed files with 119 additions and 25 deletions

View File

@@ -1496,8 +1496,7 @@ public class ConnectivityServiceTest {
return mService.getNetworkAgentInfoForNetwork(mna.getNetwork()).clatd;
}
private static class WrappedMultinetworkPolicyTracker extends MultinetworkPolicyTracker {
volatile boolean mConfigRestrictsAvoidBadWifi;
private class WrappedMultinetworkPolicyTracker extends MultinetworkPolicyTracker {
volatile int mConfigMeteredMultipathPreference;
WrappedMultinetworkPolicyTracker(Context c, Handler h, Runnable r) {
@@ -1505,8 +1504,8 @@ public class ConnectivityServiceTest {
}
@Override
public boolean configRestrictsAvoidBadWifi() {
return mConfigRestrictsAvoidBadWifi;
protected Resources getResourcesForActiveSubId() {
return mResources;
}
@Override
@@ -1723,7 +1722,9 @@ public class ConnectivityServiceTest {
.getIdentifier(eq("config_networkSupportedKeepaliveCount"), eq("array"), any());
doReturn(R.array.network_switch_type_name).when(mResources)
.getIdentifier(eq("network_switch_type_name"), eq("array"), any());
doReturn(R.integer.config_networkAvoidBadWifi).when(mResources)
.getIdentifier(eq("config_networkAvoidBadWifi"), eq("integer"), any());
doReturn(1).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
final ConnectivityResources connRes = mock(ConnectivityResources.class);
doReturn(mResources).when(connRes).get();
@@ -4645,30 +4646,29 @@ public class ConnectivityServiceTest {
}
@Test
public void testAvoidBadWifiSetting() throws Exception {
public void testSetAllowBadWifiUntil() throws Exception {
runAsShell(NETWORK_SETTINGS,
() -> mService.setTestAllowBadWifiUntil(System.currentTimeMillis() + 5_000L));
waitForIdle();
testAvoidBadWifiConfig_controlledBySettings();
runAsShell(NETWORK_SETTINGS,
() -> mService.setTestAllowBadWifiUntil(System.currentTimeMillis() - 5_000L));
waitForIdle();
testAvoidBadWifiConfig_ignoreSettings();
}
private void testAvoidBadWifiConfig_controlledBySettings() {
final ContentResolver cr = mServiceContext.getContentResolver();
final String settingName = ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI;
mPolicyTracker.mConfigRestrictsAvoidBadWifi = false;
String[] values = new String[] {null, "0", "1"};
for (int i = 0; i < values.length; i++) {
Settings.Global.putInt(cr, settingName, 1);
mPolicyTracker.reevaluate();
waitForIdle();
String msg = String.format("config=false, setting=%s", values[i]);
assertTrue(mService.avoidBadWifi());
assertFalse(msg, mPolicyTracker.shouldNotifyWifiUnvalidated());
}
mPolicyTracker.mConfigRestrictsAvoidBadWifi = true;
Settings.Global.putInt(cr, settingName, 0);
Settings.Global.putString(cr, settingName, "0");
mPolicyTracker.reevaluate();
waitForIdle();
assertFalse(mService.avoidBadWifi());
assertFalse(mPolicyTracker.shouldNotifyWifiUnvalidated());
Settings.Global.putInt(cr, settingName, 1);
Settings.Global.putString(cr, settingName, "1");
mPolicyTracker.reevaluate();
waitForIdle();
assertTrue(mService.avoidBadWifi());
@@ -4681,13 +4681,40 @@ public class ConnectivityServiceTest {
assertTrue(mPolicyTracker.shouldNotifyWifiUnvalidated());
}
private void testAvoidBadWifiConfig_ignoreSettings() {
final ContentResolver cr = mServiceContext.getContentResolver();
final String settingName = ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI;
String[] values = new String[] {null, "0", "1"};
for (int i = 0; i < values.length; i++) {
Settings.Global.putString(cr, settingName, values[i]);
mPolicyTracker.reevaluate();
waitForIdle();
String msg = String.format("config=false, setting=%s", values[i]);
assertTrue(mService.avoidBadWifi());
assertFalse(msg, mPolicyTracker.shouldNotifyWifiUnvalidated());
}
}
@Test
public void testAvoidBadWifiSetting() throws Exception {
final ContentResolver cr = mServiceContext.getContentResolver();
final String settingName = ConnectivitySettingsManager.NETWORK_AVOID_BAD_WIFI;
doReturn(1).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
testAvoidBadWifiConfig_ignoreSettings();
doReturn(0).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
testAvoidBadWifiConfig_controlledBySettings();
}
@Ignore("Refactoring in progress b/178071397")
@Test
public void testAvoidBadWifi() throws Exception {
final ContentResolver cr = mServiceContext.getContentResolver();
// Pretend we're on a carrier that restricts switching away from bad wifi.
mPolicyTracker.mConfigRestrictsAvoidBadWifi = true;
doReturn(0).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
// File a request for cell to ensure it doesn't go down.
final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
@@ -4738,13 +4765,13 @@ public class ConnectivityServiceTest {
// Simulate switching to a carrier that does not restrict avoiding bad wifi, and expect
// that we switch back to cell.
mPolicyTracker.mConfigRestrictsAvoidBadWifi = false;
doReturn(1).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
mPolicyTracker.reevaluate();
defaultCallback.expectAvailableCallbacksValidated(mCellNetworkAgent);
assertEquals(mCm.getActiveNetwork(), cellNetwork);
// Switch back to a restrictive carrier.
mPolicyTracker.mConfigRestrictsAvoidBadWifi = true;
doReturn(0).when(mResources).getInteger(R.integer.config_networkAvoidBadWifi);
mPolicyTracker.reevaluate();
defaultCallback.expectAvailableCallbacksUnvalidated(mWiFiNetworkAgent);
assertEquals(mCm.getActiveNetwork(), wifiNetwork);