diff --git a/tests/cts/hostside/OWNERS b/tests/cts/hostside/OWNERS new file mode 100644 index 0000000000..52c8053323 --- /dev/null +++ b/tests/cts/hostside/OWNERS @@ -0,0 +1,4 @@ +# Bug component: 61373 +sudheersai@google.com +lorenzo@google.com +jchalard@google.com diff --git a/tests/cts/hostside/app/AndroidManifest.xml b/tests/cts/hostside/app/AndroidManifest.xml index ba0e242ab9..f54b5c17cb 100644 --- a/tests/cts/hostside/app/AndroidManifest.xml +++ b/tests/cts/hostside/app/AndroidManifest.xml @@ -23,6 +23,8 @@ + + diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java index 0e141c05ad..55bd406c64 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java @@ -150,6 +150,11 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork } public void testAppIdleNetworkAccess_whenCharging() throws Exception { + if (!isBatterySaverSupported()) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support Battery saver mode"); + return; + } if (!isSupported()) return; // Check that app is paroled when charging @@ -175,6 +180,25 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork assertBackgroundNetworkAccess(true); } + public void testAppIdleNetworkAccess_idleWhitelisted() throws Exception { + if (!isSupported()) return; + + setAppIdle(true); + assertAppIdle(true); + assertBackgroundNetworkAccess(false); + + addAppIdleWhitelist(mUid); + assertBackgroundNetworkAccess(true); + + removeAppIdleWhitelist(mUid); + assertBackgroundNetworkAccess(false); + + // Make sure whitelisting a random app doesn't affect the tested app. + addAppIdleWhitelist(mUid + 1); + assertBackgroundNetworkAccess(false); + removeAppIdleWhitelist(mUid + 1); + } + public void testAppIdle_toast() throws Exception { if (!isSupported()) return; diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java index 28175b8784..931376b9fe 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java @@ -16,6 +16,7 @@ package com.android.cts.net.hostside; +import android.text.TextUtils; import android.util.Log; /** @@ -52,12 +53,19 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou @Override protected boolean isSupported() throws Exception { - boolean supported = isDozeModeEnabled(); - if (!supported) { - Log.i(TAG, "Skipping " + getClass() + "." + getName() - + "() because device does not support Doze Mode"); + String unSupported = ""; + if (!isDozeModeEnabled()) { + unSupported += "Doze mode,"; } - return supported; + if (!isBatterySaverSupported()) { + unSupported += "Battery saver mode,"; + } + if (!TextUtils.isEmpty(unSupported)) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support " + unSupported); + return false; + } + return true; } /** 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 bbc0354e2f..40d7e34fcc 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 @@ -26,10 +26,6 @@ import static android.os.BatteryManager.BATTERY_PLUGGED_WIRELESS; import static com.android.compatibility.common.util.SystemUtil.runShellCommand; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; - import android.app.ActivityManager; import android.app.Instrumentation; import android.app.NotificationManager; @@ -38,6 +34,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.DetailedState; @@ -47,12 +44,19 @@ import android.os.BatteryManager; import android.os.Binder; import android.os.Bundle; import android.os.SystemClock; +import android.os.SystemProperties; import android.provider.Settings; import android.service.notification.NotificationListenerService; import android.test.InstrumentationTestCase; import android.text.TextUtils; import android.util.Log; +import com.android.compatibility.common.util.BatteryUtils; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; + /** * Superclass for tests related to background network restrictions. */ @@ -92,7 +96,8 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation private static final String NETWORK_STATUS_SEPARATOR = "\\|"; private static final int SECOND_IN_MS = 1000; static final int NETWORK_TIMEOUT_MS = 15 * SECOND_IN_MS; - private static final int PROCESS_STATE_FOREGROUND_SERVICE = 3; + private static int PROCESS_STATE_FOREGROUND_SERVICE; + private static final int PROCESS_STATE_TOP = 2; private static final String KEY_NETWORK_STATE_OBSERVER = TEST_PKG + ".observer"; @@ -131,6 +136,8 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation protected void setUp() throws Exception { super.setUp(); + PROCESS_STATE_FOREGROUND_SERVICE = (Integer) ActivityManager.class + .getDeclaredField("PROCESS_STATE_FOREGROUND_SERVICE").get(null); mInstrumentation = getInstrumentation(); mContext = mInstrumentation.getContext(); mCm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); @@ -150,7 +157,14 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation Log.i(TAG, "Apps status on " + getName() + ":\n" + "\ttest app: uid=" + mMyUid + ", state=" + getProcessStateByUid(mMyUid) + "\n" + "\tapp2: uid=" + mUid + ", state=" + getProcessStateByUid(mUid)); - executeShellCommand("settings get global app_idle_constants"); + + // app_idle_constants set in NetPolicyTestsPreparer.setUp() is not always sucessful (suspect + // timing issue), here we set it again to make sure. + final String appIdleConstants = "parole_duration=0,stable_charging_threshold=0"; + executeShellCommand("settings put global app_idle_constants " + appIdleConstants); + final String currentConstants = + executeShellCommand("settings get global app_idle_constants"); + assertEquals(appIdleConstants, currentConstants); } @Override @@ -201,7 +215,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation do { attempts++; count = getNumberBroadcastsReceived(receiverName, ACTION_RESTRICT_BACKGROUND_CHANGED); - if (count == expectedCount) { + if (count >= expectedCount) { break; } Log.d(TAG, "Expecting count " + expectedCount + " but actual is " + count + " after " @@ -301,6 +315,10 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation return mSupported; } + protected boolean isBatterySaverSupported() { + return BatteryUtils.isBatterySaverSupported(); + } + /** * Asserts that an app always have access while on foreground or running a foreground service. * @@ -761,6 +779,20 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation assertRestrictBackground("restrict-background-blacklist", uid, expected); } + protected void addAppIdleWhitelist(int uid) throws Exception { + executeShellCommand("cmd netpolicy add app-idle-whitelist " + uid); + assertAppIdleWhitelist(uid, true); + } + + protected void removeAppIdleWhitelist(int uid) throws Exception { + executeShellCommand("cmd netpolicy remove app-idle-whitelist " + uid); + assertAppIdleWhitelist(uid, false); + } + + protected void assertAppIdleWhitelist(int uid, boolean expected) throws Exception { + assertRestrictBackground("app-idle-whitelist", uid, expected); + } + private void assertRestrictBackground(String list, int uid, boolean expected) throws Exception { final int maxTries = 5; boolean actual = false; 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 72563d499e..cfe6a73a0f 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 @@ -22,6 +22,8 @@ import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELI import android.util.Log; +import com.android.compatibility.common.util.CddTest; + public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase { private static final String[] REQUIRED_WHITELISTED_PACKAGES = { @@ -35,7 +37,6 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase super.setUp(); mIsDataSaverSupported = isDataSaverSupported(); - if (!isSupported()) return; // Set initial state. setRestrictBackground(false); @@ -201,6 +202,20 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase } } + @CddTest(requirement="7.4.7/C-2-2") + public void testBroadcastNotSentOnUnsupportedDevices() throws Exception { + if (isSupported()) return; + + setRestrictBackground(true); + assertRestrictBackgroundChangedReceived(0); + + setRestrictBackground(false); + assertRestrictBackgroundChangedReceived(0); + + setRestrictBackground(true); + assertRestrictBackgroundChangedReceived(0); + } + private void assertDataSaverStatusOnBackground(int expectedStatus) throws Exception { assertRestrictBackgroundStatus(expectedStatus); assertBackgroundNetworkAccess(expectedStatus != RESTRICT_BACKGROUND_STATUS_ENABLED); diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java index 87f9d7738d..b1a21867b3 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java @@ -71,6 +71,11 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase { * Tests all DS ON and BS ON scenarios from network-policy-restrictions.md on metered networks. */ public void testDataAndBatterySaverModes_meteredNetwork() throws Exception { + if (!isBatterySaverSupported()) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support Battery saver mode"); + return; + } if (!isSupported()) return; Log.i(TAG, "testDataAndBatterySaverModes_meteredNetwork() tests"); @@ -141,6 +146,11 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase { * networks. */ public void testDataAndBatterySaverModes_nonMeteredNetwork() throws Exception { + if (!isBatterySaverSupported()) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support Battery saver mode"); + return; + } if (!isSupported()) return; if (!setUnmeteredNetwork()) { @@ -206,6 +216,11 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase { * are enabled. */ public void testDozeAndBatterySaverMode_powerSaveWhitelists() throws Exception { + if (!isBatterySaverSupported()) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support Battery saver mode"); + return; + } if (!isSupported()) { return; } @@ -285,6 +300,11 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase { } public void testAppIdleAndBatterySaver_tempPowerSaveWhitelists() throws Exception { + if (!isBatterySaverSupported()) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support Battery saver mode"); + return; + } if (!isSupported()) { return; } @@ -306,4 +326,89 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase { setBatterySaverMode(false); } } + + /** + * Tests that the app idle whitelist works as expected when doze and appIdle mode are enabled. + */ + public void testDozeAndAppIdle_appIdleWhitelist() throws Exception { + if (!isSupported()) { + return; + } + + setDozeMode(true); + setAppIdle(true); + + try { + assertBackgroundNetworkAccess(false); + + // UID still shouldn't have access because of Doze. + addAppIdleWhitelist(mUid); + assertBackgroundNetworkAccess(false); + + removeAppIdleWhitelist(mUid); + assertBackgroundNetworkAccess(false); + } finally { + setAppIdle(false); + setDozeMode(false); + } + } + + public void testAppIdleAndDoze_tempPowerSaveAndAppIdleWhitelists() throws Exception { + if (!isSupported()) { + return; + } + + setDozeMode(true); + setAppIdle(true); + + try { + assertBackgroundNetworkAccess(false); + + addAppIdleWhitelist(mUid); + assertBackgroundNetworkAccess(false); + + addTempPowerSaveModeWhitelist(TEST_APP2_PKG, TEMP_POWERSAVE_WHITELIST_DURATION_MS); + assertBackgroundNetworkAccess(true); + + // Wait until the whitelist duration is expired. + SystemClock.sleep(TEMP_POWERSAVE_WHITELIST_DURATION_MS); + assertBackgroundNetworkAccess(false); + } finally { + setAppIdle(false); + setDozeMode(false); + removeAppIdleWhitelist(mUid); + } + } + + public void testAppIdleAndBatterySaver_tempPowerSaveAndAppIdleWhitelists() throws Exception { + if (!isBatterySaverSupported()) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support Battery saver mode"); + return; + } + if (!isSupported()) { + return; + } + + setBatterySaverMode(true); + setAppIdle(true); + + try { + assertBackgroundNetworkAccess(false); + + addAppIdleWhitelist(mUid); + assertBackgroundNetworkAccess(false); + + addTempPowerSaveModeWhitelist(TEST_APP2_PKG, TEMP_POWERSAVE_WHITELIST_DURATION_MS); + assertBackgroundNetworkAccess(true); + + // Wait until the whitelist duration is expired. + SystemClock.sleep(TEMP_POWERSAVE_WHITELIST_DURATION_MS); + assertBackgroundNetworkAccess(false); + } finally { + setAppIdle(false); + setBatterySaverMode(false); + removeAppIdleWhitelist(mUid); + } + } } 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 fe9d36ce1d..4598c3936b 100644 --- a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java +++ b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java @@ -81,6 +81,11 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC "testGetRestrictBackgroundStatus_requiredWhitelistedPackages"); } + public void testDataSaverMode_broadcastNotSentOnUnsupportedDevices() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".DataSaverModeTest", + "testBroadcastNotSentOnUnsupportedDevices"); + } + /***************************** * Battery Saver Mode tests. * *****************************/ @@ -156,6 +161,11 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC "testBackgroundNetworkAccess_enabled"); } + public void testAppIdleMetered_idleWhitelisted() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".AppIdleMeteredTest", + "testAppIdleNetworkAccess_idleWhitelisted"); + } + // TODO: currently power-save mode and idle uses the same whitelist, so this test would be // redundant (as it would be testing the same as testBatterySaverMode_reinstall()) // public void testAppIdle_reinstall() throws Exception { @@ -181,6 +191,11 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC "testBackgroundNetworkAccess_enabled"); } + public void testAppIdleNonMetered_idleWhitelisted() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".AppIdleNonMeteredTest", + "testAppIdleNetworkAccess_idleWhitelisted"); + } + public void testAppIdleNonMetered_whenCharging() throws Exception { runDeviceTests(TEST_PKG, TEST_PKG + ".AppIdleNonMeteredTest", "testAppIdleNetworkAccess_whenCharging"); @@ -281,6 +296,21 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC "testAppIdleAndBatterySaver_tempPowerSaveWhitelists"); } + public void testDozeAndAppIdle_appIdleWhitelist() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".MixedModesTest", + "testDozeAndAppIdle_appIdleWhitelist"); + } + + public void testAppIdleAndDoze_tempPowerSaveAndAppIdleWhitelists() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".MixedModesTest", + "testAppIdleAndDoze_tempPowerSaveAndAppIdleWhitelists"); + } + + public void testAppIdleAndBatterySaver_tempPowerSaveAndAppIdleWhitelists() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".MixedModesTest", + "testAppIdleAndBatterySaver_tempPowerSaveAndAppIdleWhitelists"); + } + /******************* * Helper methods. * *******************/ diff --git a/tests/cts/hostside/src/com/android/cts/net/NetPolicyTestsPreparer.java b/tests/cts/hostside/src/com/android/cts/net/NetPolicyTestsPreparer.java index ca14c27c20..bc2ee2cf7a 100644 --- a/tests/cts/hostside/src/com/android/cts/net/NetPolicyTestsPreparer.java +++ b/tests/cts/hostside/src/com/android/cts/net/NetPolicyTestsPreparer.java @@ -48,7 +48,7 @@ public class NetPolicyTestsPreparer implements ITargetPreparer, ITargetCleaner { } private void setAppIdleConstants(String appIdleConstants) throws DeviceNotAvailableException { - executeCmd("settings put global app_idle_constants " + appIdleConstants); + executeCmd("settings put global app_idle_constants \"" + appIdleConstants + "\""); } private String getAppIdleConstants() throws DeviceNotAvailableException { diff --git a/tests/cts/net/AndroidManifest.xml b/tests/cts/net/AndroidManifest.xml index 734d9de22f..c2b3bf77ad 100644 --- a/tests/cts/net/AndroidManifest.xml +++ b/tests/cts/net/AndroidManifest.xml @@ -23,14 +23,17 @@ + + + diff --git a/tests/cts/net/AndroidTest.xml b/tests/cts/net/AndroidTest.xml index 6a19456ef6..1807a6bee2 100644 --- a/tests/cts/net/AndroidTest.xml +++ b/tests/cts/net/AndroidTest.xml @@ -27,5 +27,6 @@