/* * Copyright 2020 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 androidx.build.testConfiguration import com.google.gson.GsonBuilder import groovy.xml.XmlUtil class ConfigBuilder { lateinit var configName: String lateinit var configType: TestConfigType var appApksModel: AppApksModel? = null lateinit var applicationId: String var isMicrobenchmark: Boolean = false var isMacrobenchmark: Boolean = false var isPostsubmit: Boolean = true lateinit var minSdk: String val tags = mutableListOf() lateinit var testApkName: String lateinit var testApkSha256: String lateinit var testRunner: String val additionalApkKeys = mutableListOf() val instrumentationArgsMap = mutableMapOf() fun configName(configName: String) = apply { this.configName = configName } fun configType(configType: TestConfigType) = apply { this.configType = configType } fun appApksModel(appApksModel: AppApksModel) = apply { this.appApksModel = appApksModel } fun applicationId(applicationId: String) = apply { this.applicationId = applicationId } fun isMicrobenchmark(isMicrobenchmark: Boolean) = apply { this.isMicrobenchmark = isMicrobenchmark } fun isMacrobenchmark(isMacrobenchmark: Boolean) = apply { this.isMacrobenchmark = isMacrobenchmark } fun isPostsubmit(isPostsubmit: Boolean) = apply { this.isPostsubmit = isPostsubmit } fun minSdk(minSdk: String) = apply { this.minSdk = minSdk } fun tag(tag: String) = apply { this.tags.add(tag) } fun additionalApkKeys(keys: List) = apply { additionalApkKeys.addAll(keys) } fun testApkName(testApkName: String) = apply { this.testApkName = testApkName } fun testApkSha256(testApkSha256: String) = apply { this.testApkSha256 = testApkSha256 } fun testRunner(testRunner: String) = apply { this.testRunner = testRunner } fun buildJson(): String { val gson = GsonBuilder().setPrettyPrinting().create() val instrumentationArgsList = mutableListOf() instrumentationArgsMap .filter { it.key !in INST_ARG_BLOCKLIST } .forEach { (key, value) -> instrumentationArgsList.add(InstrumentationArg(key, value)) } instrumentationArgsList.addAll( if (isMicrobenchmark && !isPostsubmit) { listOf( InstrumentationArg("notAnnotation", "androidx.test.filters.FlakyTest"), InstrumentationArg("androidx.benchmark.dryRunMode.enable", "true"), ) } else { listOf(InstrumentationArg("notAnnotation", "androidx.test.filters.FlakyTest")) } ) if (configType.isAddedToInstrumentationArgs()) { instrumentationArgsList.add( InstrumentationArg("androidx.testConfigType", configType.toString()) ) } val appApk = singleAppApk() val values = mapOf( "name" to configName, "minSdkVersion" to minSdk, "testSuiteTags" to tags, "testApk" to testApkName, "testApkSha256" to testApkSha256, "appApk" to appApk?.name, "appApkSha256" to appApk?.sha256, "instrumentationArgs" to instrumentationArgsList, "additionalApkKeys" to additionalApkKeys ) return gson.toJson(values) } fun buildXml(): String { val sb = StringBuilder() sb.append(XML_HEADER_AND_LICENSE) sb.append(CONFIGURATION_OPEN) .append(MIN_API_LEVEL_CONTROLLER_OBJECT.replace("MIN_SDK", minSdk)) tags.forEach { tag -> sb.append(TEST_SUITE_TAG_OPTION.replace("TEST_SUITE_TAG", tag)) } sb.append(MODULE_METADATA_TAG_OPTION.replace("APPLICATION_ID", applicationId)) .append(WIFI_DISABLE_OPTION) .append(FLAKY_TEST_OPTION) if (!isPostsubmit && (isMicrobenchmark || isMacrobenchmark)) { sb.append(BENCHMARK_PRESUBMIT_INST_ARGS) } val instrumentationArgsList = mutableListOf() instrumentationArgsMap .filter { it.key !in INST_ARG_BLOCKLIST } .forEach { (key, value) -> instrumentationArgsList.add(InstrumentationArg(key, value)) } if (isMicrobenchmark || isMacrobenchmark) { instrumentationArgsList.add( InstrumentationArg("androidx.benchmark.output.payload.testApkSha256", testApkSha256) ) if (isMacrobenchmark) { instrumentationArgsList.addAll( listOf( InstrumentationArg( "androidx.benchmark.output.payload.appApkSha256", checkNotNull(appApksModel?.sha256()) { "app apk sha should be provided for macrobenchmarks." } ), // suppress BaselineProfileRule in CI to save time InstrumentationArg("androidx.benchmark.enabledRules", "Macrobenchmark") ) ) } } if (configType.isAddedToInstrumentationArgs()) { instrumentationArgsList.add( InstrumentationArg("androidx.testConfigType", configType.toString()) ) } instrumentationArgsList.forEach { (key, value) -> sb.append( """