From e2b5213774beb3e6007731cae2e2b09a233a455f Mon Sep 17 00:00:00 2001 From: Remi NGUYEN VAN Date: Tue, 25 Apr 2023 15:49:10 +0900 Subject: [PATCH] Make ConnectivityCheckTargetPreparer more generic Rename ConnectivityCheckTargetPreparer to ConnectivityTestTargetPreparer, and the ConnectivityChecker app to ConnectivityTestPreparer, and remove the assumption that the app only does connectivity checks. This will allow overriding the app with versions that have more specific setup steps in downstream branches. Bug: 263665773 Bug: 267413275 Test: tests using this preparer Merged-In: Icb9b1eb8eb1c75f89cb751e0ca696ea96359e6d0 Change-Id: Icb9b1eb8eb1c75f89cb751e0ca696ea96359e6d0 --- staticlibs/testutils/Android.bp | 2 +- .../app/connectivitychecker/Android.bp | 2 +- .../connectivitychecker/AndroidManifest.xml | 6 +-- .../ConnectivityCheckTest.kt | 9 +++-- ...r.kt => ConnectivityTestTargetPreparer.kt} | 39 +++++++++++++------ 5 files changed, 38 insertions(+), 20 deletions(-) rename staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/{connectivitychecker => connectivitypreparer}/ConnectivityCheckTest.kt (92%) rename staticlibs/testutils/host/com/android/testutils/{ConnectivityCheckTargetPreparer.kt => ConnectivityTestTargetPreparer.kt} (69%) diff --git a/staticlibs/testutils/Android.bp b/staticlibs/testutils/Android.bp index 33821564d7..be4ccfed03 100644 --- a/staticlibs/testutils/Android.bp +++ b/staticlibs/testutils/Android.bp @@ -83,5 +83,5 @@ java_test_host { ], libs: ["tradefed"], test_suites: ["ats", "device-tests", "general-tests", "cts", "mts-networking"], - data: [":ConnectivityChecker"], + data: [":ConnectivityTestPreparer"], } diff --git a/staticlibs/testutils/app/connectivitychecker/Android.bp b/staticlibs/testutils/app/connectivitychecker/Android.bp index 58f22db7c5..f7118cfaa0 100644 --- a/staticlibs/testutils/app/connectivitychecker/Android.bp +++ b/staticlibs/testutils/app/connectivitychecker/Android.bp @@ -17,7 +17,7 @@ package { } android_test_helper_app { - name: "ConnectivityChecker", + name: "ConnectivityTestPreparer", srcs: ["src/**/*.kt"], sdk_version: "system_current", // Allow running the test on any device with SDK Q+, even when built from a branch that uses diff --git a/staticlibs/testutils/app/connectivitychecker/AndroidManifest.xml b/staticlibs/testutils/app/connectivitychecker/AndroidManifest.xml index 8e5958c316..015b41f9ba 100644 --- a/staticlibs/testutils/app/connectivitychecker/AndroidManifest.xml +++ b/staticlibs/testutils/app/connectivitychecker/AndroidManifest.xml @@ -15,7 +15,7 @@ ~ limitations under the License. --> + package="com.android.testutils.connectivitypreparer"> @@ -27,6 +27,6 @@ + android:targetPackage="com.android.testutils.connectivitypreparer" + android:label="Connectivity test target preparer" /> diff --git a/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitychecker/ConnectivityCheckTest.kt b/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt similarity index 92% rename from staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitychecker/ConnectivityCheckTest.kt rename to staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt index f72938d945..6bcb8fc182 100644 --- a/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitychecker/ConnectivityCheckTest.kt +++ b/staticlibs/testutils/app/connectivitychecker/src/com/android/testutils/connectivitypreparer/ConnectivityCheckTest.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.android.testutils.connectivitychecker +package com.android.testutils.connectivitypreparer import android.content.pm.PackageManager.FEATURE_TELEPHONY import android.content.pm.PackageManager.FEATURE_WIFI @@ -40,7 +40,7 @@ class ConnectivityCheckTest { val pm by lazy { context.packageManager } @Test - fun testCheckDeviceSetup() { + fun testCheckConnectivity() { checkWifiSetup() checkTelephonySetup() } @@ -57,10 +57,11 @@ class ConnectivityCheckTest { val commonError = "Check the test bench. To run the tests anyway for quick & dirty local " + "testing, you can use atest X -- " + - "--test-arg com.android.testutils.ConnectivityCheckTargetPreparer:disable:true" + "--test-arg com.android.testutils.ConnectivityTestTargetPreparer" + + ":ignore-connectivity-check:true" // Do not use assertEquals: it outputs "expected X, was Y", which looks like a test failure if (tm.simState == TelephonyManager.SIM_STATE_ABSENT) { - fail("The device has no SIM card inserted. " + commonError) + fail("The device has no SIM card inserted. $commonError") } else if (tm.simState != TelephonyManager.SIM_STATE_READY) { fail("The device is not setup with a usable SIM card. Sim state was ${tm.simState}. " + commonError) diff --git a/staticlibs/testutils/host/com/android/testutils/ConnectivityCheckTargetPreparer.kt b/staticlibs/testutils/host/com/android/testutils/ConnectivityTestTargetPreparer.kt similarity index 69% rename from staticlibs/testutils/host/com/android/testutils/ConnectivityCheckTargetPreparer.kt rename to staticlibs/testutils/host/com/android/testutils/ConnectivityTestTargetPreparer.kt index ccc40641a2..fd837aa1a0 100644 --- a/staticlibs/testutils/host/com/android/testutils/ConnectivityCheckTargetPreparer.kt +++ b/staticlibs/testutils/host/com/android/testutils/ConnectivityTestTargetPreparer.kt @@ -17,6 +17,7 @@ package com.android.testutils import com.android.ddmlib.testrunner.TestResult +import com.android.tradefed.config.Option import com.android.tradefed.invoker.TestInformation import com.android.tradefed.result.CollectingTestListener import com.android.tradefed.result.ddmlib.DefaultRemoteAndroidTestRunner @@ -24,19 +25,26 @@ import com.android.tradefed.targetprep.BaseTargetPreparer import com.android.tradefed.targetprep.TargetSetupError import com.android.tradefed.targetprep.suite.SuiteApkInstaller -private const val CONNECTIVITY_CHECKER_APK = "ConnectivityChecker.apk" -private const val CONNECTIVITY_PKG_NAME = "com.android.testutils.connectivitychecker" +private const val CONNECTIVITY_CHECKER_APK = "ConnectivityTestPreparer.apk" +private const val CONNECTIVITY_PKG_NAME = "com.android.testutils.connectivitypreparer" +private const val CONNECTIVITY_CHECK_CLASS = "$CONNECTIVITY_PKG_NAME.ConnectivityCheckTest" // As per the defined in the checker manifest private const val CONNECTIVITY_CHECK_RUNNER_NAME = "androidx.test.runner.AndroidJUnitRunner" +private const val IGNORE_CONN_CHECK_OPTION = "ignore-connectivity-check" /** - * A target preparer that verifies that the device was setup correctly for connectivity tests. + * A target preparer that sets up and verifies a device for connectivity tests. * - * For quick and dirty local testing, can be disabled by running tests with - * "atest -- --test-arg com.android.testutils.ConnectivityCheckTargetPreparer:disable:true". + * For quick and dirty local testing, the connectivity check can be disabled by running tests with + * "atest -- \ + * --test-arg com.android.testutils.ConnectivityTestTargetPreparer:ignore-connectivity-check:true". */ -class ConnectivityCheckTargetPreparer : BaseTargetPreparer() { - val installer = SuiteApkInstaller() +open class ConnectivityTestTargetPreparer : BaseTargetPreparer() { + private val installer = SuiteApkInstaller() + + @Option(name = IGNORE_CONN_CHECK_OPTION, + description = "Disables the check for mobile data and wifi") + private var ignoreConnectivityCheck = false override fun setUp(testInformation: TestInformation) { if (isDisabled) return @@ -63,11 +71,20 @@ class ConnectivityCheckTargetPreparer : BaseTargetPreparer() { runResult.runFailureMessage, testInformation.device.deviceDescriptor) } - if (!runResult.hasFailedTests()) return + val ignoredTestClasses = mutableSetOf() + if (ignoreConnectivityCheck) { + ignoredTestClasses.add(CONNECTIVITY_CHECK_CLASS) + } + val errorMsg = runResult.testResults.mapNotNull { (testDescription, testResult) -> - if (TestResult.TestStatus.FAILURE != testResult.status) null - else "$testDescription: ${testResult.stackTrace}" + if (TestResult.TestStatus.FAILURE != testResult.status || + ignoredTestClasses.contains(testDescription.className)) { + null + } else { + "$testDescription: ${testResult.stackTrace}" + } }.joinToString("\n") + if (errorMsg.isBlank()) return throw TargetSetupError("Device setup checks failed. Check the test bench: \n$errorMsg", testInformation.device.deviceDescriptor) @@ -77,4 +94,4 @@ class ConnectivityCheckTargetPreparer : BaseTargetPreparer() { if (isTearDownDisabled) return installer.tearDown(testInformation, e) } -} \ No newline at end of file +}