diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index 3f80b5a406..73a5d57e42 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -54,13 +54,16 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation "com.android.cts.net.hostside.app2.action.GET_COUNTERS"; private static final String ACTION_CHECK_NETWORK = "com.android.cts.net.hostside.app2.action.CHECK_NETWORK"; + private static final String ACTION_RECEIVER_READY = + "com.android.cts.net.hostside.app2.action.RECEIVER_READY"; private static final String EXTRA_ACTION = "com.android.cts.net.hostside.app2.extra.ACTION"; private static final String EXTRA_RECEIVER_NAME = "com.android.cts.net.hostside.app2.extra.RECEIVER_NAME"; private static final String RESULT_SEPARATOR = ";"; private static final String STATUS_NETWORK_UNAVAILABLE_PREFIX = "NetworkUnavailable:"; private static final String STATUS_NETWORK_AVAILABLE_PREFIX = "NetworkAvailable:"; - private static final int NETWORK_TIMEOUT_MS = 15 * 1000; + private static final int SECOND_IN_MS = 1000; + private static final int NETWORK_TIMEOUT_MS = 15 * SECOND_IN_MS; // Must be higher than NETWORK_TIMEOUT_MS private static final int ORDERED_BROADCAST_TIMEOUT_MS = NETWORK_TIMEOUT_MS * 4; @@ -116,13 +119,17 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation Log.d(TAG, "Expecting count " + expectedCount + " but actual is " + count + " after " + attempts + " attempts; sleeping " + SLEEP_TIME_SEC + " seconds before trying again"); - Thread.sleep(SLEEP_TIME_SEC * 1000); + Thread.sleep(SLEEP_TIME_SEC * SECOND_IN_MS); } while (attempts <= maxAttempts); assertEquals("Number of expected broadcasts for " + receiverName + " not reached after " + maxAttempts * SLEEP_TIME_SEC + " seconds", expectedCount, count); } protected String sendOrderedBroadcast(Intent intent) throws Exception { + return sendOrderedBroadcast(intent, ORDERED_BROADCAST_TIMEOUT_MS); + } + + protected String sendOrderedBroadcast(Intent intent, int timeoutMs) throws Exception { final LinkedBlockingQueue result = new LinkedBlockingQueue<>(1); Log.d(TAG, "Sending ordered broadcast: " + intent); mContext.sendOrderedBroadcast(intent, null, new BroadcastReceiver() { @@ -138,7 +145,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation } }, null, 0, null, null); - final String resultData = result.poll(ORDERED_BROADCAST_TIMEOUT_MS, TimeUnit.MILLISECONDS); + final String resultData = result.poll(timeoutMs, TimeUnit.MILLISECONDS); Log.d(TAG, "Ordered broadcast response: " + resultData); return resultData; } @@ -217,7 +224,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation return; Log.v(TAG, "Command '" + command + "' returned '" + result + " instead of '" + expectedResult + "' on attempt #; sleeping 1s before polling again"); - Thread.sleep(1000); + Thread.sleep(SECOND_IN_MS); } fail("Command '" + command + "' did not return '" + expectedResult + "' after " + maxTries + " attempts"); @@ -278,20 +285,38 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation } protected void assertRestrictBackgroundWhitelist(int uid, boolean expected) throws Exception { + assertRestrictBackground("restrict-background-whitelist", uid, expected); + } + + protected void addRestrictBackgroundBlacklist(int uid) throws Exception { + executeShellCommand("cmd netpolicy add restrict-background-blacklist " + uid); + assertRestrictBackgroundBlacklist(uid, true); + } + + protected void removeRestrictBackgroundBlacklist(int uid) throws Exception { + executeShellCommand("cmd netpolicy remove restrict-background-blacklist " + uid); + assertRestrictBackgroundBlacklist(uid, false); + } + + protected void assertRestrictBackgroundBlacklist(int uid, boolean expected) throws Exception { + assertRestrictBackground("restrict-background-blacklist", uid, expected); + } + + private void assertRestrictBackground(String list, int uid, boolean expected) throws Exception { final int maxTries = 5; boolean actual = false; for (int i = 1; i <= maxTries; i++) { final String output = - executeShellCommand("cmd netpolicy list restrict-background-whitelist "); + executeShellCommand("cmd netpolicy list " + list); actual = output.contains(Integer.toString(uid)); if (expected == actual) { return; } - Log.v(TAG, "whitelist check for uid " + uid + " doesn't match yet (expected " + Log.v(TAG, list + " check for uid " + uid + " doesn't match yet (expected " + expected + ", got " + actual + "); sleeping 1s before polling again"); - Thread.sleep(1000); + Thread.sleep(SECOND_IN_MS); } - fail("whitelist check for uid " + uid + " failed: expected " + expected + ", got " + actual); + fail(list + " check for uid " + uid + " failed: expected " + expected + ", got " + actual); } protected void assertPowerSaveModeWhitelist(String packageName, boolean expected) @@ -337,6 +362,19 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation */ protected void registerApp2BroadcastReceiver() throws Exception { executeShellCommand("am startservice com.android.cts.net.hostside.app2/.MyService"); + // Wait until receiver is ready. + final int maxTries = 5; + for (int i = 1; i <= maxTries; i++) { + final String message = + sendOrderedBroadcast(new Intent(ACTION_RECEIVER_READY), SECOND_IN_MS); + Log.d(TAG, "app2 receiver acked: " + message); + if (message != null) { + return; + } + Log.v(TAG, "app2 receiver is not ready yet; sleeping 1s before polling again"); + Thread.sleep(SECOND_IN_MS); + } + fail("app2 receiver is not ready"); } private String toString(int status) { diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java index ccb1fe9600..61593a1226 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java @@ -65,4 +65,28 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_ENABLED); assertRestrictBackgroundChangedReceived(1); } + + public void testGetRestrictBackgroundStatus_blacklisted() throws Exception { + addRestrictBackgroundBlacklist(mUid); + assertRestrictBackgroundChangedReceived(1); + + assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_ENABLED); + + // TODO: currently whitelist is prevailing, hence remaining of the test below is disabled + if (true) return; + + // Make sure blacklist prevails over whitelist. + setRestrictBackground(true); + assertRestrictBackgroundChangedReceived(2); + addRestrictBackgroundWhitelist(mUid); + assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_ENABLED); + + // Check status after removing blacklist. + removeRestrictBackgroundBlacklist(mUid); + assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_WHITELISTED); + assertRestrictBackgroundChangedReceived(3); + setRestrictBackground(false); + assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED); + assertRestrictBackgroundChangedReceived(4); + } } diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/Common.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/Common.java index 3be4261258..f5f5fafa31 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/Common.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/Common.java @@ -19,13 +19,16 @@ public final class Common { static final String TAG = "CtsNetApp2"; - // Constants below must match values defined on app's ConnectivityManagerTest.java + // Constants below must match values defined on app's + // AbstractRestrictBackgroundNetworkTestCase.java static final String MANIFEST_RECEIVER = "ManifestReceiver"; static final String DYNAMIC_RECEIVER = "DynamicReceiver"; static final String ACTION_GET_COUNTERS = "com.android.cts.net.hostside.app2.action.GET_COUNTERS"; static final String ACTION_CHECK_NETWORK = "com.android.cts.net.hostside.app2.action.CHECK_NETWORK"; + static final String ACTION_RECEIVER_READY = + "com.android.cts.net.hostside.app2.action.RECEIVER_READY"; static final String EXTRA_ACTION = "com.android.cts.net.hostside.app2.extra.ACTION"; static final String EXTRA_RECEIVER_NAME = "com.android.cts.net.hostside.app2.extra.RECEIVER_NAME"; diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java index f32bd44d79..94ec6af7ee 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyBroadcastReceiver.java @@ -19,6 +19,7 @@ package com.android.cts.net.hostside.app2; import static android.net.ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED; import static com.android.cts.net.hostside.app2.Common.ACTION_CHECK_NETWORK; import static com.android.cts.net.hostside.app2.Common.ACTION_GET_COUNTERS; +import static com.android.cts.net.hostside.app2.Common.ACTION_RECEIVER_READY; import static com.android.cts.net.hostside.app2.Common.EXTRA_ACTION; import static com.android.cts.net.hostside.app2.Common.EXTRA_RECEIVER_NAME; import static com.android.cts.net.hostside.app2.Common.MANIFEST_RECEIVER; @@ -76,6 +77,11 @@ public class MyBroadcastReceiver extends BroadcastReceiver { case ACTION_CHECK_NETWORK: checkNetwork(context, intent); break; + case ACTION_RECEIVER_READY: + final String message = mName + " is ready to rumble"; + Log.d(TAG, message); + setResultData(message); + break; default: Log.e(TAG, "received unexpected action: " + action); } diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java index 882bb62c2c..55249f2208 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyService.java @@ -16,9 +16,12 @@ package com.android.cts.net.hostside.app2; import static android.net.ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED; +import static com.android.cts.net.hostside.app2.Common.ACTION_RECEIVER_READY; import static com.android.cts.net.hostside.app2.Common.DYNAMIC_RECEIVER; import static com.android.cts.net.hostside.app2.Common.TAG; + import android.app.Service; +import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; @@ -37,8 +40,11 @@ public class MyService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand: " + intent); - getApplicationContext().registerReceiver(new MyBroadcastReceiver(DYNAMIC_RECEIVER), - new IntentFilter(ACTION_RESTRICT_BACKGROUND_CHANGED)); + final Context context = getApplicationContext(); + final MyBroadcastReceiver myReceiver = new MyBroadcastReceiver(DYNAMIC_RECEIVER); + context.registerReceiver(myReceiver, new IntentFilter(ACTION_RECEIVER_READY)); + context.registerReceiver(myReceiver, new IntentFilter(ACTION_RESTRICT_BACKGROUND_CHANGED)); + Log.d(TAG, "receiver registered"); return START_STICKY; } } diff --git a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java index 4786450c42..eade26184d 100644 --- a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java +++ b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java @@ -55,6 +55,11 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC "testGetRestrictBackgroundStatus_enabled"); } + public void testDataSaverMode_blacklisted() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".DataSaverModeTest", + "testGetRestrictBackgroundStatus_blacklisted"); + } + public void testDataSaverMode_reinstall() throws Exception { final int oldUid = getUid(TEST_PKG); testDataSaverMode_whitelisted();