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 f9e30b6b20..d9ff53955c 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 @@ -50,7 +50,7 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork public final void tearDown() throws Exception { super.tearDown(); - executeSilentShellCommand("cmd battery reset"); + resetBatteryState(); setAppIdle(false); } diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractExpeditedJobTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractExpeditedJobTest.java new file mode 100644 index 0000000000..404c95fa5e --- /dev/null +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractExpeditedJobTest.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.cts.net.hostside; + +import static com.android.cts.net.hostside.NetworkPolicyTestUtils.setRestrictBackground; +import static com.android.cts.net.hostside.Property.APP_STANDBY_MODE; +import static com.android.cts.net.hostside.Property.BATTERY_SAVER_MODE; +import static com.android.cts.net.hostside.Property.DATA_SAVER_MODE; +import static com.android.cts.net.hostside.Property.DOZE_MODE; +import static com.android.cts.net.hostside.Property.METERED_NETWORK; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class AbstractExpeditedJobTest extends AbstractRestrictBackgroundNetworkTestCase { + @Before + public final void setUp() throws Exception { + super.setUp(); + resetDeviceState(); + } + + @After + public final void tearDown() throws Exception { + super.tearDown(); + resetDeviceState(); + } + + private void resetDeviceState() throws Exception { + resetBatteryState(); + setBatterySaverMode(false); + setRestrictBackground(false); + setAppIdle(false); + setDozeMode(false); + } + + @Test + @RequiredProperties({BATTERY_SAVER_MODE}) + public void testNetworkAccess_batterySaverMode() throws Exception { + assertBackgroundNetworkAccess(true); + assertExpeditedJobHasNetworkAccess(); + + setBatterySaverMode(true); + assertBackgroundNetworkAccess(false); + assertExpeditedJobHasNetworkAccess(); + } + + @Test + @RequiredProperties({DATA_SAVER_MODE, METERED_NETWORK}) + public void testNetworkAccess_dataSaverMode() throws Exception { + assertBackgroundNetworkAccess(true); + assertExpeditedJobHasNetworkAccess(); + + setRestrictBackground(true); + assertBackgroundNetworkAccess(false); + assertExpeditedJobHasNetworkAccess(); + } + + @Test + @RequiredProperties({APP_STANDBY_MODE}) + public void testNetworkAccess_appIdleState() throws Exception { + turnBatteryOn(); + assertBackgroundNetworkAccess(true); + assertExpeditedJobHasNetworkAccess(); + + setAppIdle(true); + assertBackgroundNetworkAccess(false); + assertExpeditedJobHasNetworkAccess(); + } + + @Test + @RequiredProperties({DOZE_MODE}) + public void testNetworkAccess_dozeMode() throws Exception { + assertBackgroundNetworkAccess(true); + assertExpeditedJobHasNetworkAccess(); + + setDozeMode(true); + assertBackgroundNetworkAccess(false); + assertExpeditedJobHasNetworkAccess(); + } + + @Test + @RequiredProperties({DATA_SAVER_MODE, BATTERY_SAVER_MODE, METERED_NETWORK}) + public void testNetworkAccess_dataAndBatterySaverMode() throws Exception { + assertBackgroundNetworkAccess(true); + assertExpeditedJobHasNetworkAccess(); + + setRestrictBackground(true); + setBatterySaverMode(true); + assertBackgroundNetworkAccess(false); + assertExpeditedJobHasNetworkAccess(); + } + + @Test + @RequiredProperties({DOZE_MODE, DATA_SAVER_MODE, METERED_NETWORK}) + public void testNetworkAccess_dozeAndDataSaverMode() throws Exception { + assertBackgroundNetworkAccess(true); + assertExpeditedJobHasNetworkAccess(); + + setRestrictBackground(true); + setDozeMode(true); + assertBackgroundNetworkAccess(false); + assertExpeditedJobHasNetworkAccess(); + } + + @Test + @RequiredProperties({DATA_SAVER_MODE, BATTERY_SAVER_MODE, METERED_NETWORK, DOZE_MODE, + APP_STANDBY_MODE}) + public void testNetworkAccess_allRestrictionsEnabled() throws Exception { + assertBackgroundNetworkAccess(true); + assertExpeditedJobHasNetworkAccess(); + + setRestrictBackground(true); + setBatterySaverMode(true); + setAppIdle(true); + setDozeMode(true); + assertBackgroundNetworkAccess(false); + assertExpeditedJobHasNetworkAccess(); + } +} 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 effe76b32a..f67de4baf0 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 @@ -55,6 +55,7 @@ import android.provider.DeviceConfig; import android.service.notification.NotificationListenerService; import android.util.Log; +import com.android.compatibility.common.util.BatteryUtils; import com.android.compatibility.common.util.DeviceConfigStateHelper; import org.junit.Rule; @@ -267,11 +268,10 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { } /** - * Asserts that an app always have access while on foreground or running a foreground service - * or an expedited job. + * Asserts that an app always have access while on foreground or running a foreground service. * - *

This method will launch an activity, a foreground service, and an expedited job to make - * the assertion, but will finish the activity / stop the service / finish the job afterwards. + *

This method will launch an activity, a foreground service to make + * the assertion, but will finish the activity / stop the service afterwards. */ protected void assertsForegroundAlwaysHasNetworkAccess() throws Exception{ // Checks foreground first. @@ -281,7 +281,9 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { // Then foreground service launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_FOREGROUND_SERVICE); stopForegroundService(); + } + protected void assertExpeditedJobHasNetworkAccess() throws Exception { launchComponentAndAssertNetworkAccess(TYPE_EXPEDITED_JOB); finishExpeditedJob(); } @@ -621,6 +623,10 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { assertBatteryState(true); } + protected void resetBatteryState() { + BatteryUtils.runDumpsysBatteryReset(); + } + private void assertBatteryState(boolean pluggedIn) throws Exception { final long endTime = SystemClock.elapsedRealtime() + BATTERY_STATE_TIMEOUT_MS; while (isDevicePluggedIn() != pluggedIn && SystemClock.elapsedRealtime() <= endTime) { diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/ExpeditedJobMeteredTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/ExpeditedJobMeteredTest.java new file mode 100644 index 0000000000..3809534e21 --- /dev/null +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/ExpeditedJobMeteredTest.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.cts.net.hostside; + +import static com.android.cts.net.hostside.Property.METERED_NETWORK; + +@RequiredProperties({METERED_NETWORK}) +public class ExpeditedJobMeteredTest extends AbstractExpeditedJobTest { +} diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/ExpeditedJobNonMeteredTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/ExpeditedJobNonMeteredTest.java new file mode 100644 index 0000000000..6596269ceb --- /dev/null +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/ExpeditedJobNonMeteredTest.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.cts.net.hostside; + +import static com.android.cts.net.hostside.Property.NON_METERED_NETWORK; + +@RequiredProperties({NON_METERED_NETWORK}) +public class ExpeditedJobNonMeteredTest extends AbstractExpeditedJobTest { +} 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 0e25d5e8b4..d026fe00c7 100644 --- a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java +++ b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java @@ -318,11 +318,23 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC /************************** * Restricted mode tests. * **************************/ - public void testRestrictedMode_networkAccess() throws Exception { + public void testNetworkAccess_restrictedMode() throws Exception { runDeviceTests(TEST_PKG, TEST_PKG + ".RestrictedModeTest", "testNetworkAccess"); } + /************************ + * Expedited job tests. * + ************************/ + + public void testMeteredNetworkAccess_expeditedJob() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".ExpeditedJobMeteredTest"); + } + + public void testNonMeteredNetworkAccess_expeditedJob() throws Exception { + runDeviceTests(TEST_PKG, TEST_PKG + ".ExpeditedJobNonMeteredTest"); + } + /******************* * Helper methods. * *******************/