Merge "Install test app1 per test class" into main

This commit is contained in:
Junyu Lai
2023-07-12 09:07:29 +00:00
committed by Gerrit Code Review
6 changed files with 47 additions and 31 deletions

View File

@@ -26,16 +26,12 @@ public class HostsideConnOnActivityStartTest extends HostsideNetworkTestCase {
private static final String TEST_CLASS = TEST_PKG + ".ConnOnActivityStartTest"; private static final String TEST_CLASS = TEST_PKG + ".ConnOnActivityStartTest";
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
uninstallPackage(TEST_APP2_PKG, false); uninstallPackage(TEST_APP2_PKG, false);
installPackage(TEST_APP2_APK); installPackage(TEST_APP2_APK);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
uninstallPackage(TEST_APP2_PKG, true); uninstallPackage(TEST_APP2_PKG, true);
} }

View File

@@ -23,14 +23,12 @@ public class HostsideNetworkCallbackTests extends HostsideNetworkTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
uninstallPackage(TEST_APP2_PKG, false); uninstallPackage(TEST_APP2_PKG, false);
installPackage(TEST_APP2_APK); installPackage(TEST_APP2_APK);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
uninstallPackage(TEST_APP2_PKG, true); uninstallPackage(TEST_APP2_PKG, true);
} }

View File

@@ -23,14 +23,12 @@ import org.junit.Test;
public class HostsideNetworkPolicyManagerTests extends HostsideNetworkTestCase { public class HostsideNetworkPolicyManagerTests extends HostsideNetworkTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
uninstallPackage(TEST_APP2_PKG, false); uninstallPackage(TEST_APP2_PKG, false);
installPackage(TEST_APP2_APK); installPackage(TEST_APP2_APK);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
uninstallPackage(TEST_APP2_PKG, true); uninstallPackage(TEST_APP2_PKG, true);
} }

View File

@@ -16,16 +16,21 @@
package com.android.cts.net; package com.android.cts.net;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import com.android.ddmlib.Log; import com.android.ddmlib.Log;
import com.android.modules.utils.build.testing.DeviceSdkLevel; import com.android.modules.utils.build.testing.DeviceSdkLevel;
import com.android.tradefed.device.DeviceNotAvailableException; import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.invoker.TestInformation;
import com.android.tradefed.targetprep.BuildError;
import com.android.tradefed.targetprep.TargetSetupError; import com.android.tradefed.targetprep.TargetSetupError;
import com.android.tradefed.targetprep.suite.SuiteApkInstaller;
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
import com.android.tradefed.testtype.junit4.AfterClassWithInfo;
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
import com.android.tradefed.testtype.junit4.DeviceTestRunOptions; import com.android.tradefed.testtype.junit4.BeforeClassWithInfo;
import com.android.tradefed.util.RunUtil; import com.android.tradefed.util.RunUtil;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -40,34 +45,61 @@ abstract class HostsideNetworkTestCase extends BaseHostJUnit4Test {
protected static final String TEST_APP2_PKG = "com.android.cts.net.hostside.app2"; protected static final String TEST_APP2_PKG = "com.android.cts.net.hostside.app2";
protected static final String TEST_APP2_APK = "CtsHostsideNetworkTestsApp2.apk"; protected static final String TEST_APP2_APK = "CtsHostsideNetworkTestsApp2.apk";
protected void setUp() throws Exception { @BeforeClassWithInfo
DeviceSdkLevel deviceSdkLevel = new DeviceSdkLevel(getDevice()); public static void setUpOnce(TestInformation testInfo) throws Exception {
String testApk = deviceSdkLevel.isDeviceAtLeastT() ? TEST_APK_NEXT DeviceSdkLevel deviceSdkLevel = new DeviceSdkLevel(testInfo.getDevice());
: TEST_APK; String testApk = deviceSdkLevel.isDeviceAtLeastT() ? TEST_APK_NEXT : TEST_APK;
uninstallPackage(TEST_PKG, false); uninstallPackage(testInfo, TEST_PKG, false);
installPackage(testApk); installPackage(testInfo, testApk);
} }
protected void tearDown() throws Exception { @AfterClassWithInfo
uninstallPackage(TEST_PKG, true); public static void tearDownOnce(TestInformation testInfo) throws DeviceNotAvailableException {
uninstallPackage(testInfo, TEST_PKG, true);
}
// Custom static method to install the specified package, this is used to bypass auto-cleanup
// per test in BaseHostJUnit4.
protected static void installPackage(TestInformation testInfo, String apk)
throws DeviceNotAvailableException, TargetSetupError {
assertNotNull(testInfo);
final int userId = testInfo.getDevice().getCurrentUser();
final SuiteApkInstaller installer = new SuiteApkInstaller();
// Force the apk clean up
installer.setCleanApk(true);
installer.addTestFileName(apk);
installer.setUserId(userId);
installer.setShouldGrantPermission(true);
installer.addInstallArg("-t");
try {
installer.setUp(testInfo);
} catch (BuildError e) {
throw new TargetSetupError(
e.getMessage(), e, testInfo.getDevice().getDeviceDescriptor(), e.getErrorId());
}
} }
protected void installPackage(String apk) throws DeviceNotAvailableException, TargetSetupError { protected void installPackage(String apk) throws DeviceNotAvailableException, TargetSetupError {
final DeviceTestRunOptions installOptions = new DeviceTestRunOptions( installPackage(getTestInformation(), apk);
null /* packageName */);
final int userId = getDevice().getCurrentUser();
installPackageAsUser(apk, true /* grantPermission */, userId, "-t");
} }
protected void uninstallPackage(String packageName, boolean shouldSucceed) protected static void uninstallPackage(TestInformation testInfo, String packageName,
boolean shouldSucceed)
throws DeviceNotAvailableException { throws DeviceNotAvailableException {
final String result = uninstallPackage(packageName); assertNotNull(testInfo);
final String result = testInfo.getDevice().uninstallPackage(packageName);
if (shouldSucceed) { if (shouldSucceed) {
assertNull("uninstallPackage(" + packageName + ") failed: " + result, result); assertNull("uninstallPackage(" + packageName + ") failed: " + result, result);
} }
} }
protected void uninstallPackage(String packageName,
boolean shouldSucceed)
throws DeviceNotAvailableException {
uninstallPackage(getTestInformation(), packageName, shouldSucceed);
}
protected void assertPackageUninstalled(String packageName) throws DeviceNotAvailableException, protected void assertPackageUninstalled(String packageName) throws DeviceNotAvailableException,
InterruptedException { InterruptedException {
final String command = "cmd package list packages " + packageName; final String command = "cmd package list packages " + packageName;

View File

@@ -32,16 +32,12 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
uninstallPackage(TEST_APP2_PKG, false); uninstallPackage(TEST_APP2_PKG, false);
installPackage(TEST_APP2_APK); installPackage(TEST_APP2_APK);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
uninstallPackage(TEST_APP2_PKG, true); uninstallPackage(TEST_APP2_PKG, true);
} }

View File

@@ -26,16 +26,12 @@ public class HostsideVpnTests extends HostsideNetworkTestCase {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
uninstallPackage(TEST_APP2_PKG, false); uninstallPackage(TEST_APP2_PKG, false);
installPackage(TEST_APP2_APK); installPackage(TEST_APP2_APK);
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
super.tearDown();
uninstallPackage(TEST_APP2_PKG, true); uninstallPackage(TEST_APP2_PKG, true);
} }