Improvements on test case setup.
- On hostside, checks if wi-fi is on instead of checking for connectivity (which can be very slow). - Don't automatically reset metered network on superclass' tearDown(). - Make sure tearDown() cleans up all state changes. BUG: 27808364 Change-Id: I4818047c5fb8f6f430b0aab5ecfa77717f860db3
This commit is contained in:
@@ -76,11 +76,12 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
||||
protected ConnectivityManager mCm;
|
||||
protected WifiManager mWfm;
|
||||
protected int mUid;
|
||||
private boolean mResetMeteredWifi = false;
|
||||
private String mMeteredWifi;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
mInstrumentation = getInstrumentation();
|
||||
mContext = mInstrumentation.getContext();
|
||||
mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
@@ -93,15 +94,6 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
||||
+ "\tapp2: uid=" + mUid + ", state=" + getProcessStateByUid(mUid));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
|
||||
if (mResetMeteredWifi) {
|
||||
setWifiMeteredStatus(false);
|
||||
}
|
||||
}
|
||||
|
||||
protected int getUid(String packageName) throws Exception {
|
||||
return mContext.getPackageManager().getPackageUid(packageName, 0);
|
||||
}
|
||||
@@ -289,12 +281,21 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
||||
final String netId = setWifiMeteredStatus(true);
|
||||
assertTrue("Could not set wifi '" + netId + "' as metered ("
|
||||
+ mCm.getActiveNetworkInfo() +")", mCm.isActiveNetworkMetered());
|
||||
// Set flag so status is reverted on teardown.
|
||||
mResetMeteredWifi = true;
|
||||
// Set flag so status is reverted on resetMeteredNetwork();
|
||||
mMeteredWifi = netId;
|
||||
// Sanity check.
|
||||
assertMeteredNetwork(netId, true);
|
||||
}
|
||||
|
||||
protected void resetMeteredNetwork() throws Exception {
|
||||
if (mMeteredWifi == null) {
|
||||
Log.d(TAG, "resetMeteredNetwork(): wifi not set as metered");
|
||||
return;
|
||||
}
|
||||
Log.i(TAG, "resetMeteredNetwork(): resetting " + mMeteredWifi);
|
||||
setWifiMeteredStatus(mMeteredWifi, false);
|
||||
}
|
||||
|
||||
private String setWifiMeteredStatus(boolean metered) throws Exception {
|
||||
// We could call setWifiEnabled() here, but it might take sometime to be in a consistent
|
||||
// state (for example, if one of the saved network is not properly authenticated), so it's
|
||||
@@ -303,6 +304,10 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
||||
// TODO: if it's not guaranteed the device has wi-fi, we need to change the tests
|
||||
// to make the actual verification of restrictions optional.
|
||||
final String ssid = mWfm.getConnectionInfo().getSSID();
|
||||
return setWifiMeteredStatus(ssid, metered);
|
||||
}
|
||||
|
||||
private String setWifiMeteredStatus(String ssid, boolean metered) throws Exception {
|
||||
assertNotNull("null SSID", ssid);
|
||||
final String netId = ssid.trim().replaceAll("\"", ""); // remove quotes, if any.
|
||||
assertFalse("empty SSID", ssid.isEmpty());
|
||||
|
||||
@@ -33,7 +33,11 @@ public class BatterySaverModeTest extends AbstractRestrictBackgroundNetworkTestC
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
|
||||
setPowerSaveMode(false);
|
||||
try {
|
||||
resetMeteredNetwork();
|
||||
} finally {
|
||||
setPowerSaveMode(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void testBackgroundNetworkAccess_enabled() throws Exception {
|
||||
|
||||
@@ -44,7 +44,11 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
|
||||
setRestrictBackground(false);
|
||||
try {
|
||||
resetMeteredNetwork();
|
||||
} finally {
|
||||
setRestrictBackground(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetRestrictBackgroundStatus_disabled() throws Exception {
|
||||
|
||||
@@ -63,7 +63,7 @@ abstract class HostsideNetworkTestCase extends DeviceTestCase implements IAbiRec
|
||||
assertNotNull(mAbi);
|
||||
assertNotNull(mCtsBuild);
|
||||
|
||||
assertTrue("device not connected to network", getDevice().checkConnectivity());
|
||||
assertTrue("wi-fi not enabled", getDevice().isWifiEnabled());
|
||||
|
||||
uninstallPackage(TEST_PKG, false);
|
||||
installPackage(TEST_APK);
|
||||
|
||||
Reference in New Issue
Block a user