Merge "Merge "Don't require WiFi in CtsHostsideNetworkTests" into nougat-cts-dev am: dfc0ec200f" into nyc-dev
am: 2be6d78c13 Change-Id: I578972101d3b4c46132fc360be18a5dd947a4113
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -30,7 +30,6 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou
|
||||
if (!isSupported()) return;
|
||||
|
||||
// Set initial state.
|
||||
setUpMeteredNetwork();
|
||||
removePowerSaveModeWhitelist(TEST_APP2_PKG);
|
||||
setBatterySaverMode(false);
|
||||
|
||||
|
||||
@@ -31,7 +31,6 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor
|
||||
if (!isSupported()) return;
|
||||
|
||||
// Set initial state.
|
||||
setUpMeteredNetwork();
|
||||
removePowerSaveModeWhitelist(TEST_APP2_PKG);
|
||||
setDozeMode(false);
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
||||
protected WifiManager mWfm;
|
||||
protected int mUid;
|
||||
private String mMeteredWifi;
|
||||
private boolean mSupported;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
@@ -108,6 +109,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"
|
||||
@@ -203,7 +205,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() {
|
||||
@@ -213,7 +217,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
||||
* @return {@code true} by default.
|
||||
*/
|
||||
protected boolean isSupported() throws Exception {
|
||||
return true;
|
||||
return mSupported;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -426,15 +430,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);
|
||||
}
|
||||
@@ -445,31 +495,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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user