Add tests to verify app has network access in onResume.

We already have tests that verify this but these new tests
launch the activity repeatedly a few times and verify the
network access as there are multiple race conditions
involving AMS, NPMS and the app that could result in an
inconsistent network rules state.

Bug: 226299593
Test: atest atest tests/cts/hostside/src/com/android/cts/net/HostsideConnOnActivityStartTest
Change-Id: I6372820fd2b21ebd759a3c44c7280c6699726bde
Merged-In: I6372820fd2b21ebd759a3c44c7280c6699726bde
This commit is contained in:
Sudheer Shanka 2022-04-05 17:28:29 -07:00
commit fc8ba515da
5 changed files with 182 additions and 15 deletions

View file

@ -916,7 +916,7 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
final Intent intent = new Intent();
if (type == TYPE_COMPONENT_ACTIVTIY) {
intent.setComponent(new ComponentName(TEST_APP2_PKG, TEST_APP2_ACTIVITY_CLASS))
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
} else if (type == TYPE_COMPONENT_FOREGROUND_SERVICE) {
intent.setComponent(new ComponentName(TEST_APP2_PKG, TEST_APP2_SERVICE_CLASS))
.setFlags(1);

View file

@ -0,0 +1,98 @@
/*
* Copyright (C) 2022 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.getUiDevice;
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 static com.android.cts.net.hostside.Property.NON_METERED_NETWORK;
import android.util.Log;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@RequiredProperties({NON_METERED_NETWORK})
public class ConnOnActivityStartTest extends AbstractRestrictBackgroundNetworkTestCase {
private static final int TEST_ITERATION_COUNT = 5;
@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 testStartActivity_batterySaver() throws Exception {
setBatterySaverMode(true);
assertLaunchedActivityHasNetworkAccess("testStartActivity_batterySaver");
}
@Test
@RequiredProperties({DATA_SAVER_MODE, METERED_NETWORK})
public void testStartActivity_dataSaver() throws Exception {
setRestrictBackground(true);
assertLaunchedActivityHasNetworkAccess("testStartActivity_dataSaver");
}
@Test
@RequiredProperties({DOZE_MODE})
public void testStartActivity_doze() throws Exception {
setDozeMode(true);
assertLaunchedActivityHasNetworkAccess("testStartActivity_doze");
}
@Test
@RequiredProperties({APP_STANDBY_MODE})
public void testStartActivity_appStandby() throws Exception {
turnBatteryOn();
setAppIdle(true);
assertLaunchedActivityHasNetworkAccess("testStartActivity_appStandby");
}
private void assertLaunchedActivityHasNetworkAccess(String testName) throws Exception {
for (int i = 0; i < TEST_ITERATION_COUNT; ++i) {
Log.i(TAG, testName + " start #" + i);
launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_ACTIVTIY);
getUiDevice().pressHome();
assertBackgroundState();
Log.i(TAG, testName + " end #" + i);
}
}
}

View file

@ -57,6 +57,7 @@ import android.telephony.data.ApnSetting;
import android.util.Log;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.UiDevice;
import com.android.compatibility.common.util.AppStandbyUtils;
import com.android.compatibility.common.util.BatteryUtils;
@ -438,6 +439,10 @@ public class NetworkPolicyTestUtils {
return InstrumentationRegistry.getInstrumentation();
}
public static UiDevice getUiDevice() {
return UiDevice.getInstance(getInstrumentation());
}
// When power saver mode or restrict background enabled or adding any white/black list into
// those modes, NetworkPolicy may need to take some time to update the rules of uids. So having
// this function and using PollingCheck to try to make sure the uid has updated and reduce the

View file

@ -39,6 +39,33 @@ public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "MyActivity.onCreate()");
}
@Override
public void finish() {
if (finishCommandReceiver != null) {
unregisterReceiver(finishCommandReceiver);
}
super.finish();
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "MyActivity.onStart()");
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "MyActivity.onNewIntent()");
setIntent(intent);
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "MyActivity.onResume(): " + getIntent());
Common.notifyNetworkStateObserver(this, getIntent(), TYPE_COMPONENT_ACTIVTY);
finishCommandReceiver = new BroadcastReceiver() {
@Override
@ -56,20 +83,6 @@ public class MyActivity extends Activity {
}
}
@Override
public void finish() {
if (finishCommandReceiver != null) {
unregisterReceiver(finishCommandReceiver);
}
super.finish();
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "MyActivity.onStart()");
}
@Override
protected void onDestroy() {
Log.d(TAG, "MyActivity.onDestroy()");

View file

@ -0,0 +1,51 @@
/*
* Copyright (C) 2022 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;
public class HostsideConnOnActivityStartTest extends HostsideNetworkTestCase {
private static final String TEST_CLASS = TEST_PKG + ".ConnOnActivityStartTest";
@Override
public void setUp() throws Exception {
super.setUp();
uninstallPackage(TEST_APP2_PKG, false);
installPackage(TEST_APP2_APK);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
uninstallPackage(TEST_APP2_PKG, true);
}
public void testStartActivity_batterySaver() throws Exception {
runDeviceTests(TEST_PKG, TEST_CLASS, "testStartActivity_batterySaver");
}
public void testStartActivity_dataSaver() throws Exception {
runDeviceTests(TEST_PKG, TEST_CLASS, "testStartActivity_dataSaver");
}
public void testStartActivity_doze() throws Exception {
runDeviceTests(TEST_PKG, TEST_CLASS, "testStartActivity_doze");
}
public void testStartActivity_appStandby() throws Exception {
runDeviceTests(TEST_PKG, TEST_CLASS, "testStartActivity_appStandby");
}
}