Merge "Skip data saver related tests if the device doesn't support it." into oc-dev

This commit is contained in:
Sudheer Shanka
2017-04-18 04:23:18 +00:00
committed by Android (Google) Code Review
2 changed files with 49 additions and 2 deletions

View File

@@ -100,6 +100,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
protected ConnectivityManager mCm; protected ConnectivityManager mCm;
protected WifiManager mWfm; protected WifiManager mWfm;
protected int mUid; protected int mUid;
private int mMyUid;
private String mMeteredWifi; private String mMeteredWifi;
private MyServiceClient mServiceClient; private MyServiceClient mServiceClient;
private boolean mHasWatch; private boolean mHasWatch;
@@ -115,7 +116,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
mWfm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); mWfm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
mUid = getUid(TEST_APP2_PKG); mUid = getUid(TEST_APP2_PKG);
final int myUid = getUid(mContext.getPackageName()); mMyUid = getUid(mContext.getPackageName());
mServiceClient = new MyServiceClient(mContext); mServiceClient = new MyServiceClient(mContext);
mServiceClient.bind(); mServiceClient.bind();
mHasWatch = mContext.getPackageManager().hasSystemFeature( mHasWatch = mContext.getPackageManager().hasSystemFeature(
@@ -128,7 +129,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
mSupported = setUpActiveNetworkMeteringState(); mSupported = setUpActiveNetworkMeteringState();
Log.i(TAG, "Apps status on " + getName() + ":\n" 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)); + "\tapp2: uid=" + mUid + ", state=" + getProcessStateByUid(mUid));
} }
@@ -204,6 +205,21 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
assertEquals("wrong status", toString(expectedStatus), actualStatus); 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 { protected void assertBackgroundNetworkAccess(boolean expectAllowed) throws Exception {
assertBackgroundState(); // Sanity check. assertBackgroundState(); // Sanity check.
assertNetworkAccess(expectAllowed); assertNetworkAccess(expectAllowed);

View File

@@ -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_ENABLED;
import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED; import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED;
import android.util.Log;
public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase { public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase {
private static final String[] REQUIRED_WHITELISTED_PACKAGES = { private static final String[] REQUIRED_WHITELISTED_PACKAGES = {
"com.android.providers.downloads" "com.android.providers.downloads"
}; };
private boolean mIsDataSaverSupported;
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
mIsDataSaverSupported = isDataSaverSupported();
if (!isSupported()) return; if (!isSupported()) return;
// Set initial state. // Set initial state.
@@ -59,6 +64,32 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
return setMeteredNetwork(); 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 { public void testGetRestrictBackgroundStatus_disabled() throws Exception {
if (!isSupported()) return; if (!isSupported()) return;