Merge "Add ignore-mobile-data-check option to preparer" into main

This commit is contained in:
Remi NGUYEN VAN 2024-06-14 07:58:20 +00:00 • committed by Gerrit Code Review
commit 36bc46d171
2 changed files with 77 additions and 44 deletions

View file

@ -34,17 +34,13 @@ class ConnectivityCheckTest {
private val connectUtil by lazy { ConnectUtil(context) }
@Test
fun testCheckConnectivity() {
checkWifiSetup()
checkTelephonySetup()
}
private fun checkWifiSetup() {
fun testCheckWifiSetup() {
if (!pm.hasSystemFeature(FEATURE_WIFI)) return
connectUtil.ensureWifiValidated()
}
private fun checkTelephonySetup() {
@Test
fun testCheckTelephonySetup() {
if (!pm.hasSystemFeature(FEATURE_TELEPHONY)) return
val tm = context.getSystemService(TelephonyManager::class.java)
?: fail("Could not get telephony service")
@ -52,7 +48,7 @@ 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.ConnectivityTestTargetPreparer" +
":ignore-connectivity-check:true"
":ignore-mobile-data-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")

View file

@ -28,9 +28,11 @@ import com.android.tradefed.targetprep.suite.SuiteApkInstaller
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 <instrumentation> 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"
private const val IGNORE_WIFI_CHECK = "ignore-wifi-check"
private const val IGNORE_MOBILE_DATA_CHECK = "ignore-mobile-data-check"
// The default updater package names, which might be updating packages while the CTS
// are running
@ -41,14 +43,23 @@ private val UPDATER_PKGS = arrayOf("com.google.android.gms", "com.android.vendin
*
* 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".
* --test-arg com.android.testutils.ConnectivityTestTargetPreparer:ignore-mobile-data-check:true". \
* --test-arg com.android.testutils.ConnectivityTestTargetPreparer:ignore-wifi-check:true".
*/
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
@Option(
name = IGNORE_WIFI_CHECK,
description = "Disables the check for wifi"
)
private var ignoreWifiCheck = false
@Option(
name = IGNORE_MOBILE_DATA_CHECK,
description = "Disables the check for mobile data"
)
private var ignoreMobileDataCheck = false
// The default value is never used, but false is a reasonable default
private var originalTestChainEnabled = false
private val originalUpdaterPkgsStatus = HashMap<String, Boolean>()
@ -58,44 +69,62 @@ open class ConnectivityTestTargetPreparer : BaseTargetPreparer() {
disableGmsUpdate(testInfo)
originalTestChainEnabled = getTestChainEnabled(testInfo)
originalUpdaterPkgsStatus.putAll(getUpdaterPkgsStatus(testInfo))
setUpdaterNetworkingEnabled(testInfo, enableChain = true,
enablePkgs = UPDATER_PKGS.associateWith { false })
runPreparerApk(testInfo)
setUpdaterNetworkingEnabled(
testInfo,
enableChain = true,
enablePkgs = UPDATER_PKGS.associateWith { false }
)
runConnectivityCheckApk(testInfo)
refreshTime(testInfo)
}
private fun runPreparerApk(testInfo: TestInformation) {
private fun runConnectivityCheckApk(testInfo: TestInformation) {
installer.setCleanApk(true)
installer.addTestFileName(CONNECTIVITY_CHECKER_APK)
installer.setShouldGrantPermission(true)
installer.setUp(testInfo)
val testMethods = mutableListOf<String>()
if (!ignoreWifiCheck) {
testMethods.add("testCheckWifiSetup")
}
if (!ignoreMobileDataCheck) {
testMethods.add("testCheckTelephonySetup")
}
testMethods.forEach {
runTestMethod(testInfo, it)
}
}
private fun runTestMethod(testInfo: TestInformation, method: String) {
val runner = DefaultRemoteAndroidTestRunner(
CONNECTIVITY_PKG_NAME,
CONNECTIVITY_CHECK_RUNNER_NAME,
testInfo.device.iDevice)
CONNECTIVITY_PKG_NAME,
CONNECTIVITY_CHECK_RUNNER_NAME,
testInfo.device.iDevice
)
runner.runOptions = "--no-hidden-api-checks"
runner.setMethodName(CONNECTIVITY_CHECK_CLASS, method)
val receiver = CollectingTestListener()
if (!testInfo.device.runInstrumentationTests(runner, receiver)) {
throw TargetSetupError("Device state check failed to complete",
testInfo.device.deviceDescriptor)
throw TargetSetupError(
"Device state check failed to complete",
testInfo.device.deviceDescriptor
)
}
val runResult = receiver.currentRunResults
if (runResult.isRunFailure) {
throw TargetSetupError("Failed to check device state before the test: " +
runResult.runFailureMessage, testInfo.device.deviceDescriptor)
}
val ignoredTestClasses = mutableSetOf<String>()
if (ignoreConnectivityCheck) {
ignoredTestClasses.add(CONNECTIVITY_CHECK_CLASS)
throw TargetSetupError(
"Failed to check device state before the test: " +
runResult.runFailureMessage,
testInfo.device.deviceDescriptor
)
}
val errorMsg = runResult.testResults.mapNotNull { (testDescription, testResult) ->
if (TestResult.TestStatus.FAILURE != testResult.status ||
ignoredTestClasses.contains(testDescription.className)) {
if (TestResult.TestStatus.FAILURE != testResult.status) {
null
} else {
"$testDescription: ${testResult.stackTrace}"
@ -103,21 +132,27 @@ open class ConnectivityTestTargetPreparer : BaseTargetPreparer() {
}.joinToString("\n")
if (errorMsg.isBlank()) return
throw TargetSetupError("Device setup checks failed. Check the test bench: \n$errorMsg",
testInfo.device.deviceDescriptor)
throw TargetSetupError(
"Device setup checks failed. Check the test bench: \n$errorMsg",
testInfo.device.deviceDescriptor
)
}
private fun disableGmsUpdate(testInfo: TestInformation) {
// This will be a no-op on devices without root (su) or not using gservices, but that's OK.
testInfo.exec("su 0 am broadcast " +
testInfo.exec(
"su 0 am broadcast " +
"-a com.google.gservices.intent.action.GSERVICES_OVERRIDE " +
"-e finsky.play_services_auto_update_enabled false")
"-e finsky.play_services_auto_update_enabled false"
)
}
private fun clearGmsUpdateOverride(testInfo: TestInformation) {
testInfo.exec("su 0 am broadcast " +
testInfo.exec(
"su 0 am broadcast " +
"-a com.google.gservices.intent.action.GSERVICES_OVERRIDE " +
"--esn finsky.play_services_auto_update_enabled")
"--esn finsky.play_services_auto_update_enabled"
)
}
private fun setUpdaterNetworkingEnabled(
@ -137,10 +172,10 @@ open class ConnectivityTestTargetPreparer : BaseTargetPreparer() {
testInfo.exec("cmd connectivity get-chain3-enabled").contains("chain:enabled")
private fun getUpdaterPkgsStatus(testInfo: TestInformation) =
UPDATER_PKGS.associateWith { pkg ->
!testInfo.exec("cmd connectivity get-package-networking-enabled $pkg")
.contains(":deny")
}
UPDATER_PKGS.associateWith { pkg ->
!testInfo.exec("cmd connectivity get-package-networking-enabled $pkg")
.contains(":deny")
}
private fun refreshTime(testInfo: TestInformation,) {
// Forces a synchronous time refresh using the network. Time is fetched synchronously but
@ -153,9 +188,11 @@ open class ConnectivityTestTargetPreparer : BaseTargetPreparer() {
override fun tearDown(testInfo: TestInformation, e: Throwable?) {
if (isTearDownDisabled) return
installer.tearDown(testInfo, e)
setUpdaterNetworkingEnabled(testInfo,
enableChain = originalTestChainEnabled,
enablePkgs = originalUpdaterPkgsStatus)
setUpdaterNetworkingEnabled(
testInfo,
enableChain = originalTestChainEnabled,
enablePkgs = originalUpdaterPkgsStatus
)
clearGmsUpdateOverride(testInfo)
}
}