Merge "Adjust background restriction transition delays in tests" into main

This commit is contained in:
Treehugger Robot 2024-06-12 21:08:34 +00:00 • committed by Gerrit Code Review
commit 99f1201b0f
5 changed files with 48 additions and 19 deletions

View file

@ -16,7 +16,7 @@
package com.android.cts.netpolicy.hostside;
import static android.app.ActivityManager.PROCESS_STATE_TOP_SLEEPING;
import static android.app.ActivityManager.PROCESS_STATE_LAST_ACTIVITY;
import static org.junit.Assume.assumeTrue;
@ -46,14 +46,15 @@ abstract class AbstractDefaultRestrictionsTest extends AbstractRestrictBackgroun
public final void tearDown() throws Exception {
super.tearDown();
stopApp();
removePowerSaveModeWhitelist(TEST_APP2_PKG);
removePowerSaveModeExceptIdleWhitelist(TEST_APP2_PKG);
}
@Test
public void testFgsNetworkAccess() throws Exception {
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
SystemClock.sleep(PROCESS_STATE_TRANSITION_DELAY_MS);
assertProcessStateBelow(PROCESS_STATE_LAST_ACTIVITY);
SystemClock.sleep(mProcessStateTransitionShortDelayMs);
assertNetworkAccess(false, null);
launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_FOREGROUND_SERVICE);
@ -61,8 +62,8 @@ abstract class AbstractDefaultRestrictionsTest extends AbstractRestrictBackgroun
@Test
public void testActivityNetworkAccess() throws Exception {
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
SystemClock.sleep(PROCESS_STATE_TRANSITION_DELAY_MS);
assertProcessStateBelow(PROCESS_STATE_LAST_ACTIVITY);
SystemClock.sleep(mProcessStateTransitionShortDelayMs);
assertNetworkAccess(false, null);
launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_ACTIVTIY);
@ -70,23 +71,23 @@ abstract class AbstractDefaultRestrictionsTest extends AbstractRestrictBackgroun
@Test
public void testBackgroundNetworkAccess_inFullAllowlist() throws Exception {
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
SystemClock.sleep(PROCESS_STATE_TRANSITION_DELAY_MS);
assertProcessStateBelow(PROCESS_STATE_LAST_ACTIVITY);
SystemClock.sleep(mProcessStateTransitionShortDelayMs);
assertNetworkAccess(false, null);
addPowerSaveModeWhitelist(TEST_APP2_PKG);
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
assertProcessStateBelow(PROCESS_STATE_LAST_ACTIVITY);
assertNetworkAccess(true, null);
}
@Test
public void testBackgroundNetworkAccess_inExceptIdleAllowlist() throws Exception {
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
SystemClock.sleep(PROCESS_STATE_TRANSITION_DELAY_MS);
assertProcessStateBelow(PROCESS_STATE_LAST_ACTIVITY);
SystemClock.sleep(mProcessStateTransitionShortDelayMs);
assertNetworkAccess(false, null);
addPowerSaveModeExceptIdleWhitelist(TEST_APP2_PKG);
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
assertProcessStateBelow(PROCESS_STATE_LAST_ACTIVITY);
assertNetworkAccess(true, null);
}
}

View file

@ -96,7 +96,12 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
protected static final String TEST_PKG = "com.android.cts.netpolicy.hostside";
protected static final String TEST_APP2_PKG = "com.android.cts.netpolicy.hostside.app2";
// TODO(b/321797685): Configure it via device-config once it is available.
protected static final long PROCESS_STATE_TRANSITION_DELAY_MS = TimeUnit.SECONDS.toMillis(5);
protected final long mProcessStateTransitionLongDelayMs =
useDifferentDelaysForBackgroundChain() ? TimeUnit.SECONDS.toMillis(20)
: TimeUnit.SECONDS.toMillis(5);
protected final long mProcessStateTransitionShortDelayMs =
useDifferentDelaysForBackgroundChain() ? TimeUnit.SECONDS.toMillis(2)
: TimeUnit.SECONDS.toMillis(5);
private static final String TEST_APP2_ACTIVITY_CLASS = TEST_APP2_PKG + ".MyActivity";
private static final String TEST_APP2_SERVICE_CLASS = TEST_APP2_PKG + ".MyForegroundService";
@ -241,6 +246,22 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
return Boolean.parseBoolean(output);
}
/**
* Check if the flag to use different delays for sensitive proc-states is enabled.
* This is a manual check because the feature flag infrastructure may not be available
* in all the branches that will get this code.
* TODO: b/322115994 - Use @RequiresFlagsEnabled with
* Flags.FLAG_USE_DIFFERENT_DELAYS_FOR_BACKGROUND_CHAIN once the tests are moved to cts.
*/
private boolean useDifferentDelaysForBackgroundChain() {
if (!SdkLevel.isAtLeastV()) {
return false;
}
final String output = executeShellCommand("device_config get backstage_power"
+ " com.android.server.net.use_different_delays_for_background_chain");
return Boolean.parseBoolean(output);
}
protected int getUid(String packageName) throws Exception {
return mContext.getPackageManager().getPackageUid(packageName, 0);
}
@ -824,6 +845,10 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
assertDelayedShellCommand("dumpsys deviceidle get deep", enabled ? "IDLE" : "ACTIVE");
}
protected void stopApp() {
executeSilentShellCommand("am stop-app " + TEST_APP2_PKG);
}
protected void setAppIdle(boolean isIdle) throws Exception {
setAppIdleNoAssert(isIdle);
assertAppIdle(isIdle);

View file

@ -53,7 +53,7 @@ public class ConnOnActivityStartTest extends AbstractRestrictBackgroundNetworkTe
@After
public final void tearDown() throws Exception {
super.tearDown();
finishActivity();
stopApp();
resetDeviceState();
}
@ -108,7 +108,7 @@ public class ConnOnActivityStartTest extends AbstractRestrictBackgroundNetworkTe
assumeTrue("Feature not enabled", isNetworkBlockedForTopSleepingAndAbove());
assertLaunchedActivityHasNetworkAccess("testStartActivity_default", () -> {
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
SystemClock.sleep(PROCESS_STATE_TRANSITION_DELAY_MS);
SystemClock.sleep(mProcessStateTransitionLongDelayMs);
assertNetworkAccess(false, null);
});
}

View file

@ -268,6 +268,7 @@ public class NetworkCallbackTest extends AbstractRestrictBackgroundNetworkTestCa
setRestrictBackground(false);
setBatterySaverMode(false);
unregisterNetworkCallback();
stopApp();
if (SdkLevel.isAtLeastT() && (mCtsNetUtils != null)) {
mCtsNetUtils.restorePrivateDnsSetting();
@ -387,7 +388,7 @@ public class NetworkCallbackTest extends AbstractRestrictBackgroundNetworkTestCa
finishActivity();
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
SystemClock.sleep(PROCESS_STATE_TRANSITION_DELAY_MS);
SystemClock.sleep(mProcessStateTransitionLongDelayMs);
assertNetworkAccess(false, null);
mTestNetworkCallback.expectBlockedStatusCallbackEventually(mNetwork, true);
assertNetworkAccessBlockedByBpf(true, mUid, true /* metered */);
@ -413,7 +414,7 @@ public class NetworkCallbackTest extends AbstractRestrictBackgroundNetworkTestCa
finishActivity();
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
SystemClock.sleep(PROCESS_STATE_TRANSITION_DELAY_MS);
SystemClock.sleep(mProcessStateTransitionLongDelayMs);
assertNetworkAccess(false, null);
mTestNetworkCallback.expectBlockedStatusCallbackEventually(mNetwork, true);
assertNetworkAccessBlockedByBpf(true, mUid, false /* metered */);

View file

@ -17,6 +17,7 @@
package com.android.cts.netpolicy.hostside;
import static android.app.ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
import static android.app.ActivityManager.PROCESS_STATE_LAST_ACTIVITY;
import static android.app.ActivityManager.PROCESS_STATE_TOP_SLEEPING;
import static android.os.Process.SYSTEM_UID;
@ -65,6 +66,7 @@ public class NetworkPolicyManagerTest extends AbstractRestrictBackgroundNetworkT
setRestrictBackground(false);
setRestrictedNetworkingMode(false);
unregisterNetworkCallback();
stopApp();
}
@Test
@ -248,8 +250,8 @@ public class NetworkPolicyManagerTest extends AbstractRestrictBackgroundNetworkT
assumeTrue("Feature not enabled", isNetworkBlockedForTopSleepingAndAbove());
try {
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
SystemClock.sleep(PROCESS_STATE_TRANSITION_DELAY_MS);
assertProcessStateBelow(PROCESS_STATE_LAST_ACTIVITY);
SystemClock.sleep(mProcessStateTransitionShortDelayMs);
assertNetworkingBlockedStatusForUid(mUid, METERED, true /* expectedResult */);
assertTrue(isUidNetworkingBlocked(mUid, NON_METERED));
@ -260,7 +262,7 @@ public class NetworkPolicyManagerTest extends AbstractRestrictBackgroundNetworkT
finishActivity();
assertProcessStateBelow(PROCESS_STATE_TOP_SLEEPING);
SystemClock.sleep(PROCESS_STATE_TRANSITION_DELAY_MS);
SystemClock.sleep(mProcessStateTransitionLongDelayMs);
assertNetworkingBlockedStatusForUid(mUid, METERED, true /* expectedResult */);
assertTrue(isUidNetworkingBlocked(mUid, NON_METERED));