DO NOT MERGE: Skip data saver related tests if the device doesn't support it.
Bug: 36007771 Test: manual Change-Id: I817d2cc23e5c8d460367ede749ff35e799f48ee0
This commit is contained in:
@@ -84,6 +84,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
||||
protected ConnectivityManager mCm;
|
||||
protected WifiManager mWfm;
|
||||
protected int mUid;
|
||||
private int mMyUid;
|
||||
private String mMeteredWifi;
|
||||
private boolean mSupported;
|
||||
|
||||
@@ -96,11 +97,11 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
||||
mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
mWfm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
|
||||
mUid = getUid(TEST_APP2_PKG);
|
||||
final int myUid = getUid(mContext.getPackageName());
|
||||
mMyUid = getUid(mContext.getPackageName());
|
||||
mSupported = setUpActiveNetworkMeteringState();
|
||||
|
||||
Log.i(TAG, "Apps status on " + getName() + ":\n"
|
||||
+ "\ttest app: uid=" + myUid + ", state=" + getProcessStateByUid(myUid) + "\n"
|
||||
+ "\ttest app: uid=" + mMyUid + ", state=" + getProcessStateByUid(mMyUid) + "\n"
|
||||
+ "\tapp2: uid=" + mUid + ", state=" + getProcessStateByUid(mUid));
|
||||
}
|
||||
|
||||
@@ -175,6 +176,21 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
|
||||
assertEquals("wrong status", toString(expectedStatus), actualStatus);
|
||||
}
|
||||
|
||||
protected void assertMyRestrictBackgroundStatus(int expectedStatus) throws Exception {
|
||||
final int actualStatus = mCm.getRestrictBackgroundStatus();
|
||||
assertEquals("Wrong status", toString(expectedStatus), toString(actualStatus));
|
||||
}
|
||||
|
||||
protected boolean isMyRestrictBackgroundStatus(int expectedStatus) throws Exception {
|
||||
final int actualStatus = mCm.getRestrictBackgroundStatus();
|
||||
if (expectedStatus != actualStatus) {
|
||||
Log.d(TAG, "Expected: " + toString(expectedStatus)
|
||||
+ " but actual: " + toString(actualStatus));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void assertBackgroundNetworkAccess(boolean expectAllowed) throws Exception {
|
||||
assertBackgroundState(); // Sanity check.
|
||||
assertNetworkAccess(expectAllowed);
|
||||
|
||||
@@ -20,16 +20,21 @@ import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLE
|
||||
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED;
|
||||
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase {
|
||||
|
||||
private static final String[] REQUIRED_WHITELISTED_PACKAGES = {
|
||||
"com.android.providers.downloads"
|
||||
};
|
||||
|
||||
private boolean mIsDataSaverSupported;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
mIsDataSaverSupported = isDataSaverSupported();
|
||||
if (!isSupported()) return;
|
||||
|
||||
// Set initial state.
|
||||
@@ -59,6 +64,32 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
|
||||
return setMeteredNetwork();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isSupported() throws Exception {
|
||||
if (!mIsDataSaverSupported) {
|
||||
Log.i(TAG, "Skipping " + getClass() + "." + getName()
|
||||
+ "() because device does not support Data Saver Mode");
|
||||
}
|
||||
return mIsDataSaverSupported && super.isSupported();
|
||||
}
|
||||
|
||||
/**
|
||||
* As per CDD requirements, if the device doesn't support data saver mode then
|
||||
* ConnectivityManager.getRestrictBackgroundStatus() will always return
|
||||
* RESTRICT_BACKGROUND_STATUS_DISABLED. So, enable the data saver mode and check if
|
||||
* ConnectivityManager.getRestrictBackgroundStatus() for an app in background returns
|
||||
* RESTRICT_BACKGROUND_STATUS_DISABLED or not.
|
||||
*/
|
||||
private boolean isDataSaverSupported() throws Exception {
|
||||
assertMyRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED);
|
||||
try {
|
||||
setRestrictBackground(true);
|
||||
return !isMyRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED);
|
||||
} finally {
|
||||
setRestrictBackground(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetRestrictBackgroundStatus_disabled() throws Exception {
|
||||
if (!isSupported()) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user