Don't require WiFi in CtsHostsideNetworkTests

WiFi is not a CDD requirement so these tests should not fail when the
device under test does not have WiFi. The behavior is changed so that if
there is WiFi then both metered and unmetered tests will run. If there
is no WiFi and the current connection is metered then only metered tests
will run. If there is no WiFi and the current connection is not metered
then only unmetered tests will run.

Test: Successfully ran CTS test on both emulator and shamu.

BUG: 31648368
Change-Id: Ic643d2490e0a7e69b57a44599f1a4c57c67da873
This commit is contained in:
Bjoern Johansson
2016-12-07 14:42:38 -08:00
parent 516b0616e9
commit 68ec71d954
13 changed files with 84 additions and 50 deletions

View File

@@ -30,7 +30,6 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork
if (!isSupported()) return;
// Set initial state.
setUpMeteredNetwork();
removePowerSaveModeWhitelist(TEST_APP2_PKG);
setAppIdle(false);
turnBatteryOff();
@@ -62,14 +61,6 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork
return supported;
}
/**
* Sets the initial (non) metered network state.
*
* <p>By default is empty - it's up to subclasses to override.
*/
protected void setUpMeteredNetwork() throws Exception {
}
/**
* Resets the (non) metered network state.
*

View File

@@ -30,7 +30,6 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou
if (!isSupported()) return;
// Set initial state.
setUpMeteredNetwork();
removePowerSaveModeWhitelist(TEST_APP2_PKG);
setBatterySaverMode(false);

View File

@@ -31,7 +31,6 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor
if (!isSupported()) return;
// Set initial state.
setUpMeteredNetwork();
removePowerSaveModeWhitelist(TEST_APP2_PKG);
setDozeMode(false);

View File

@@ -85,6 +85,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
protected WifiManager mWfm;
protected int mUid;
private String mMeteredWifi;
private boolean mSupported;
@Override
protected void setUp() throws Exception {
@@ -96,6 +97,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
mWfm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
mUid = getUid(TEST_APP2_PKG);
final int myUid = getUid(mContext.getPackageName());
mSupported = setUpActiveNetworkMeteringState();
Log.i(TAG, "Apps status on " + getName() + ":\n"
+ "\ttest app: uid=" + myUid + ", state=" + getProcessStateByUid(myUid) + "\n"
@@ -191,7 +193,9 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
/**
* Whether this device suport this type of test.
*
* <p>Should be overridden when necessary, and explicitly used before each test. Example:
* <p>Should be overridden when necessary (but always calling
* {@code super.isSupported()} first), and explicitly used before each test
* Example:
*
* <pre><code>
* public void testSomething() {
@@ -201,7 +205,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
* @return {@code true} by default.
*/
protected boolean isSupported() throws Exception {
return true;
return mSupported;
}
/**
@@ -399,15 +403,61 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
}
/**
* Puts the device in a state where the active network is metered, or fail if it can't achieve
* that state.
* Sets the initial metering state for the active network.
*
* <p>It's called on setup and by default does nothing - it's up to the
* subclasses to override.
*
* @return whether the tests in the subclass are supported on this device.
*/
protected void setMeteredNetwork() throws Exception {
protected boolean setUpActiveNetworkMeteringState() throws Exception {
return true;
}
/**
* Makes sure the active network is not metered.
*
* <p>If the device does not supoprt un-metered networks (for example if it
* only has cellular data but not wi-fi), it should return {@code false};
* otherwise, it should return {@code true} (or fail if the un-metered
* network could not be set).
*
* @return {@code true} if the network is now unmetered.
*/
protected boolean setUnmeteredNetwork() throws Exception {
final NetworkInfo info = mCm.getActiveNetworkInfo();
assertNotNull("Could not get active network", info);
if (!mCm.isActiveNetworkMetered()) {
Log.d(TAG, "Active network is not metered: " + info);
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
Log.i(TAG, "Setting active WI-FI network as not metered: " + info );
setWifiMeteredStatus(false);
} else {
Log.d(TAG, "Active network cannot be set to un-metered: " + info);
return false;
}
assertActiveNetworkMetered(false); // Sanity check.
return true;
}
/**
* Enables metering on the active network if supported.
*
* <p>If the device does not support metered networks it should return
* {@code false}; otherwise, it should return {@code true} (or fail if the
* metered network could not be set).
*
* @return {@code true} if the network is now metered.
*/
protected boolean setMeteredNetwork() throws Exception {
final NetworkInfo info = mCm.getActiveNetworkInfo();
final boolean metered = mCm.isActiveNetworkMetered();
if (metered) {
Log.d(TAG, "Active network already metered: " + info);
return;
return true;
} else if (info.getType() != ConnectivityManager.TYPE_WIFI) {
Log.w(TAG, "Active network does not support metering: " + info);
return false;
} else {
Log.w(TAG, "Active network not metered: " + info);
}
@@ -418,31 +468,21 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
// Sanity check.
assertWifiMeteredStatus(netId, true);
assertActiveNetworkMetered(true);
return true;
}
/**
* Puts the device in a state where the active network is not metered, or fail if it can't
* achieve that state.
* <p>It assumes the device has a valid WI-FI connection.
* Resets the device metering state to what it was before the test started.
*
* <p>This reverts any metering changes made by {@code setMeteredNetwork}.
*/
protected void resetMeteredNetwork() throws Exception {
if (mMeteredWifi != null) {
Log.i(TAG, "resetMeteredNetwork(): SID '" + mMeteredWifi
+ "' was set as metered by test case; resetting it");
setWifiMeteredStatus(mMeteredWifi, false);
} else {
final NetworkInfo info = mCm.getActiveNetworkInfo();
assertNotNull("Could not get active network", info);
if (!mCm.isActiveNetworkMetered()) {
Log.d(TAG, "Active network is not metered: " + info);
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
Log.i(TAG, "Setting active WI-FI network as metered: " + info );
setWifiMeteredStatus(false);
} else {
fail("Active network is not WI-FI hence cannot be set as non-metered: " + info);
}
assertActiveNetworkMetered(false); // Sanity check.
}
assertActiveNetworkMetered(false); // Sanity check.
}
private void assertActiveNetworkMetered(boolean expected) throws Exception {

View File

@@ -19,8 +19,8 @@ package com.android.cts.net.hostside;
public class AppIdleMeteredTest extends AbstractAppIdleTestCase {
@Override
protected void setUpMeteredNetwork() throws Exception {
setMeteredNetwork();
protected boolean setUpActiveNetworkMeteringState() throws Exception {
return setMeteredNetwork();
}
@Override

View File

@@ -17,9 +17,8 @@
package com.android.cts.net.hostside;
public class AppIdleNonMeteredTest extends AbstractAppIdleTestCase {
@Override
protected void setUpMeteredNetwork() throws Exception {
resetMeteredNetwork();
protected boolean setUpActiveNetworkMeteringState() throws Exception {
return setUnmeteredNetwork();
}
}

View File

@@ -19,8 +19,8 @@ package com.android.cts.net.hostside;
public class BatterySaverModeMeteredTest extends AbstractBatterySaverModeTestCase {
@Override
protected void setUpMeteredNetwork() throws Exception {
setMeteredNetwork();
protected boolean setUpActiveNetworkMeteringState() throws Exception {
return setMeteredNetwork();
}
@Override

View File

@@ -19,7 +19,7 @@ package com.android.cts.net.hostside;
public class BatterySaverModeNonMeteredTest extends AbstractBatterySaverModeTestCase {
@Override
protected void setUpMeteredNetwork() throws Exception {
resetMeteredNetwork();
protected boolean setUpActiveNetworkMeteringState() throws Exception {
return setUnmeteredNetwork();
}
}

View File

@@ -33,7 +33,6 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
if (!isSupported()) return;
// Set initial state.
setMeteredNetwork();
setRestrictBackground(false);
removeRestrictBackgroundWhitelist(mUid);
removeRestrictBackgroundBlacklist(mUid);
@@ -55,6 +54,11 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
}
}
@Override
protected boolean setUpActiveNetworkMeteringState() throws Exception {
return setMeteredNetwork();
}
public void testGetRestrictBackgroundStatus_disabled() throws Exception {
if (!isSupported()) return;

View File

@@ -19,8 +19,8 @@ package com.android.cts.net.hostside;
public class DozeModeMeteredTest extends AbstractDozeModeTestCase {
@Override
protected void setUpMeteredNetwork() throws Exception {
setMeteredNetwork();
protected boolean setUpActiveNetworkMeteringState() throws Exception {
return setMeteredNetwork();
}
@Override

View File

@@ -19,7 +19,7 @@ package com.android.cts.net.hostside;
public class DozeModeNonMeteredTest extends AbstractDozeModeTestCase {
@Override
protected void setUpMeteredNetwork() throws Exception {
resetMeteredNetwork();
protected boolean setUpActiveNetworkMeteringState() throws Exception {
return setUnmeteredNetwork();
}
}

View File

@@ -68,7 +68,11 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase {
}
Log.i(TAG, "testDataAndBatterySaverModes_meteredNetwork() tests");
setMeteredNetwork();
if (!setMeteredNetwork()) {
Log.w(TAG, "testDataAndBatterySaverModes_meteredNetwork() skipped because "
+ "device cannot use a metered network");
return;
}
try {
setRestrictBackground(true);
@@ -139,7 +143,7 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase {
return;
}
if (mCm.isActiveNetworkMetered()) {
if (!setUnmeteredNetwork()) {
Log.w(TAG, "testDataAndBatterySaverModes_nonMeteredNetwork() skipped because network"
+ " is metered");
return;

View File

@@ -65,8 +65,6 @@ abstract class HostsideNetworkTestCase extends DeviceTestCase implements IAbiRec
assertNotNull(mAbi);
assertNotNull(mCtsBuild);
assertTrue("wi-fi not enabled", getDevice().isWifiEnabled());
uninstallPackage(TEST_PKG, false);
installPackage(TEST_APK);
}