| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. | 
|  | 2 | // | 
|  | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | // you may not use this file except in compliance with the License. | 
|  | 5 | // You may obtain a copy of the License at | 
|  | 6 | // | 
|  | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | // | 
|  | 9 | // Unless required by applicable law or agreed to in writing, software | 
|  | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | // See the License for the specific language governing permissions and | 
|  | 13 | // limitations under the License. | 
|  | 14 |  | 
|  | 15 | package java | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "fmt" | 
|  | 19 | "io" | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 20 | "strconv" | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 21 | "strings" | 
|  | 22 |  | 
|  | 23 | "android/soong/android" | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 24 | "android/soong/java/config" | 
|  | 25 | "android/soong/tradefed" | 
| Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 26 |  | 
| Cole Faust | d57e8b2 | 2022-08-11 11:59:04 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint/proptools" | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 28 | ) | 
|  | 29 |  | 
|  | 30 | func init() { | 
|  | 31 | android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 32 | android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 33 | } | 
|  | 34 |  | 
|  | 35 | var robolectricDefaultLibs = []string{ | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 36 | "mockito-robolectric-prebuilt", | 
|  | 37 | "truth-prebuilt", | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 38 | // TODO(ccross): this is not needed at link time | 
|  | 39 | "junitxml", | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 40 | } | 
|  | 41 |  | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 42 | const robolectricCurrentLib = "Robolectric_all-target" | 
|  | 43 | const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt" | 
|  | 44 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 45 | var ( | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 46 | roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"} | 
|  | 47 | roboRuntimesTag     = dependencyTag{name: "roboRuntimes"} | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 48 | ) | 
|  | 49 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 50 | type robolectricProperties struct { | 
|  | 51 | // The name of the android_app module that the tests will run against. | 
|  | 52 | Instrumentation_for *string | 
|  | 53 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 54 | // Additional libraries for which coverage data should be generated | 
|  | 55 | Coverage_libs []string | 
|  | 56 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 57 | Test_options struct { | 
|  | 58 | // Timeout in seconds when running the tests. | 
| Colin Cross | 2f9a7c8 | 2019-05-30 11:16:26 -0700 | [diff] [blame] | 59 | Timeout *int64 | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | // Number of shards to use when running the tests. | 
|  | 62 | Shards *int64 | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 63 | } | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 64 |  | 
|  | 65 | // The version number of a robolectric prebuilt to use from prebuilts/misc/common/robolectric | 
|  | 66 | // instead of the one built from source in external/robolectric-shadows. | 
|  | 67 | Robolectric_prebuilt_version *string | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
|  | 70 | type robolectricTest struct { | 
|  | 71 | Library | 
|  | 72 |  | 
|  | 73 | robolectricProperties robolectricProperties | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 74 | testProperties        testProperties | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 75 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 76 | libs  []string | 
|  | 77 | tests []string | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 78 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 79 | manifest    android.Path | 
|  | 80 | resourceApk android.Path | 
|  | 81 |  | 
|  | 82 | combinedJar android.WritablePath | 
|  | 83 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 84 | roboSrcJar android.Path | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 85 |  | 
|  | 86 | testConfig android.Path | 
|  | 87 | data       android.Paths | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 88 |  | 
|  | 89 | forceOSType   android.OsType | 
|  | 90 | forceArchType android.ArchType | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 93 | func (r *robolectricTest) TestSuites() []string { | 
|  | 94 | return r.testProperties.Test_suites | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | var _ android.TestSuiteModule = (*robolectricTest)(nil) | 
|  | 98 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 99 | func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 100 | r.Library.DepsMutator(ctx) | 
|  | 101 |  | 
|  | 102 | if r.robolectricProperties.Instrumentation_for != nil { | 
|  | 103 | ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for)) | 
|  | 104 | } else { | 
|  | 105 | ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module") | 
|  | 106 | } | 
|  | 107 |  | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 108 | if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" { | 
|  | 109 | ctx.AddVariationDependencies(nil, libTag, fmt.Sprintf(robolectricPrebuiltLibPattern, v)) | 
|  | 110 | } else { | 
|  | 111 | ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib) | 
|  | 112 | } | 
|  | 113 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 114 | ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...) | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 117 |  | 
| Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 118 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(), | 
|  | 119 | roboRuntimesTag, "robolectric-android-all-prebuilts") | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 120 | } | 
|  | 121 |  | 
|  | 122 | func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 123 | r.forceOSType = ctx.Config().BuildOS | 
|  | 124 | r.forceArchType = ctx.Config().BuildArch | 
|  | 125 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 126 | r.testConfig = tradefed.AutoGenRobolectricTestConfig(ctx, r.testProperties.Test_config, | 
|  | 127 | r.testProperties.Test_config_template, r.testProperties.Test_suites, | 
|  | 128 | r.testProperties.Auto_gen_config) | 
|  | 129 | r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data) | 
|  | 130 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 131 | roboTestConfig := android.PathForModuleGen(ctx, "robolectric"). | 
|  | 132 | Join(ctx, "com/android/tools/test_config.properties") | 
|  | 133 |  | 
|  | 134 | // TODO: this inserts paths to built files into the test, it should really be inserting the contents. | 
|  | 135 | instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag) | 
|  | 136 |  | 
|  | 137 | if len(instrumented) != 1 { | 
|  | 138 | panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented))) | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | instrumentedApp, ok := instrumented[0].(*AndroidApp) | 
|  | 142 | if !ok { | 
|  | 143 | ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app") | 
|  | 144 | } | 
|  | 145 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 146 | r.manifest = instrumentedApp.mergedManifestFile | 
|  | 147 | r.resourceApk = instrumentedApp.outputFile | 
|  | 148 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 149 | generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp) | 
|  | 150 | r.extraResources = android.Paths{roboTestConfig} | 
|  | 151 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 152 | r.Library.GenerateAndroidBuildActions(ctx) | 
|  | 153 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 154 | roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar") | 
|  | 155 | r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp) | 
|  | 156 | r.roboSrcJar = roboSrcJar | 
|  | 157 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 158 | roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar") | 
|  | 159 | generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar) | 
|  | 160 |  | 
|  | 161 | combinedJarJars := android.Paths{ | 
|  | 162 | // roboTestConfigJar comes first so that its com/android/tools/test_config.properties | 
|  | 163 | // overrides the one from r.extraResources.  The r.extraResources one can be removed | 
|  | 164 | // once the Make test runner is removed. | 
|  | 165 | roboTestConfigJar, | 
|  | 166 | r.outputFile, | 
|  | 167 | instrumentedApp.implementationAndResourcesJar, | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 168 | } | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 169 |  | 
| Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 170 | handleLibDeps := func(dep android.Module) { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 171 | m := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo) | 
|  | 172 | r.libs = append(r.libs, ctx.OtherModuleName(dep)) | 
|  | 173 | if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) { | 
|  | 174 | combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars...) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 175 | } | 
|  | 176 | } | 
|  | 177 |  | 
| Liz Kammer | ef28a4c | 2022-09-23 16:50:56 -0400 | [diff] [blame] | 178 | for _, dep := range ctx.GetDirectDepsWithTag(libTag) { | 
|  | 179 | handleLibDeps(dep) | 
|  | 180 | } | 
|  | 181 | for _, dep := range ctx.GetDirectDepsWithTag(sdkLibTag) { | 
|  | 182 | handleLibDeps(dep) | 
|  | 183 | } | 
|  | 184 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 185 | r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base()) | 
|  | 186 | TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{}, | 
|  | 187 | false, nil, nil) | 
|  | 188 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 189 | // TODO: this could all be removed if tradefed was used as the test runner, it will find everything | 
|  | 190 | // annotated as a test and run it. | 
|  | 191 | for _, src := range r.compiledJavaSrcs { | 
|  | 192 | s := src.Rel() | 
|  | 193 | if !strings.HasSuffix(s, "Test.java") { | 
|  | 194 | continue | 
|  | 195 | } else if strings.HasSuffix(s, "/BaseRobolectricTest.java") { | 
|  | 196 | continue | 
| Ulya Trafimovich | 497a093 | 2021-07-14 16:35:33 +0100 | [diff] [blame] | 197 | } else { | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 198 | s = strings.TrimPrefix(s, "src/") | 
|  | 199 | } | 
|  | 200 | r.tests = append(r.tests, s) | 
|  | 201 | } | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 202 |  | 
|  | 203 | r.data = append(r.data, r.manifest, r.resourceApk) | 
|  | 204 |  | 
|  | 205 | runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag) | 
|  | 206 |  | 
|  | 207 | installPath := android.PathForModuleInstall(ctx, r.BaseModuleName()) | 
|  | 208 |  | 
|  | 209 | installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk) | 
|  | 210 | installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest) | 
|  | 211 | installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig) | 
|  | 212 |  | 
|  | 213 | var installDeps android.Paths | 
|  | 214 | for _, runtime := range runtimes.(*robolectricRuntimes).runtimes { | 
|  | 215 | installDeps = append(installDeps, runtime) | 
|  | 216 | } | 
|  | 217 | installDeps = append(installDeps, installedResourceApk, installedManifest, installedConfig) | 
|  | 218 |  | 
|  | 219 | for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) { | 
|  | 220 | installedData := ctx.InstallFile(installPath, data.Rel(), data) | 
|  | 221 | installDeps = append(installDeps, installedData) | 
|  | 222 | } | 
|  | 223 |  | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 224 | r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...) | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 227 | func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath, | 
|  | 228 | instrumentedApp *AndroidApp) { | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 229 | rule := android.NewRuleBuilder(pctx, ctx) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 230 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 231 | manifest := instrumentedApp.mergedManifestFile | 
|  | 232 | resourceApk := instrumentedApp.outputFile | 
|  | 233 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 234 | rule.Command().Text("rm -f").Output(outputFile) | 
|  | 235 | rule.Command(). | 
|  | 236 | Textf(`echo "android_merged_manifest=%s" >>`, manifest.String()).Output(outputFile).Text("&&"). | 
|  | 237 | Textf(`echo "android_resource_apk=%s" >>`, resourceApk.String()).Output(outputFile). | 
|  | 238 | // Make it depend on the files to which it points so the test file's timestamp is updated whenever the | 
|  | 239 | // contents change | 
|  | 240 | Implicit(manifest). | 
|  | 241 | Implicit(resourceApk) | 
|  | 242 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 243 | rule.Build("generate_test_config", "generate test_config.properties") | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 246 | func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) { | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 247 | rule := android.NewRuleBuilder(pctx, ctx) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 248 |  | 
|  | 249 | outputDir := outputFile.InSameDir(ctx) | 
|  | 250 | configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties") | 
|  | 251 | rule.Temporary(configFile) | 
|  | 252 | rule.Command().Text("rm -f").Output(outputFile).Output(configFile) | 
|  | 253 | rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String()) | 
|  | 254 | rule.Command(). | 
|  | 255 | Text("("). | 
|  | 256 | Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()). | 
|  | 257 | Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()). | 
|  | 258 | Text(") >>").Output(configFile) | 
|  | 259 | rule.Command(). | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 260 | BuiltTool("soong_zip"). | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 261 | FlagWithArg("-C ", outputDir.String()). | 
|  | 262 | FlagWithInput("-f ", configFile). | 
|  | 263 | FlagWithOutput("-o ", outputFile) | 
|  | 264 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 265 | rule.Build("generate_test_config_samedir", "generate test_config.properties") | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 268 | func (r *robolectricTest) generateRoboSrcJar(ctx android.ModuleContext, outputFile android.WritablePath, | 
|  | 269 | instrumentedApp *AndroidApp) { | 
|  | 270 |  | 
|  | 271 | srcJarArgs := copyOf(instrumentedApp.srcJarArgs) | 
|  | 272 | srcJarDeps := append(android.Paths(nil), instrumentedApp.srcJarDeps...) | 
|  | 273 |  | 
|  | 274 | for _, m := range ctx.GetDirectDepsWithTag(roboCoverageLibsTag) { | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 275 | if ctx.OtherModuleHasProvider(m, JavaInfoProvider) { | 
|  | 276 | dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo) | 
|  | 277 | srcJarArgs = append(srcJarArgs, dep.SrcJarArgs...) | 
|  | 278 | srcJarDeps = append(srcJarDeps, dep.SrcJarDeps...) | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 279 | } | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | TransformResourcesToJar(ctx, outputFile, srcJarArgs, srcJarDeps) | 
|  | 283 | } | 
|  | 284 |  | 
| Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 285 | func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries { | 
|  | 286 | entriesList := r.Library.AndroidMkEntries() | 
|  | 287 | entries := &entriesList[0] | 
| Colin Cross | 6301c3c | 2021-09-28 17:40:21 -0700 | [diff] [blame] | 288 | entries.ExtraEntries = append(entries.ExtraEntries, | 
|  | 289 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { | 
|  | 290 | entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true) | 
|  | 291 | }) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 292 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 293 | entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{ | 
| Jaewoong Jung | 02b11a6 | 2020-12-07 10:23:54 -0800 | [diff] [blame] | 294 | func(w io.Writer, name, prefix, moduleDir string) { | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 295 | if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 { | 
| Colin Cross | 0a2f719 | 2019-09-23 14:33:09 -0700 | [diff] [blame] | 296 | numShards := int(*s) | 
|  | 297 | shardSize := (len(r.tests) + numShards - 1) / numShards | 
|  | 298 | shards := android.ShardStrings(r.tests, shardSize) | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 299 | for i, shard := range shards { | 
|  | 300 | r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard) | 
|  | 301 | } | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 302 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 303 | // TODO: add rules to dist the outputs of the individual tests, or combine them together? | 
|  | 304 | fmt.Fprintln(w, "") | 
|  | 305 | fmt.Fprintln(w, ".PHONY:", "Run"+name) | 
|  | 306 | fmt.Fprintln(w, "Run"+name, ": \\") | 
|  | 307 | for i := range shards { | 
|  | 308 | fmt.Fprintln(w, "   ", "Run"+name+strconv.Itoa(i), "\\") | 
|  | 309 | } | 
|  | 310 | fmt.Fprintln(w, "") | 
|  | 311 | } else { | 
|  | 312 | r.writeTestRunner(w, name, "Run"+name, r.tests) | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 313 | } | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 314 | }, | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 315 | } | 
|  | 316 |  | 
| Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 317 | return entriesList | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 318 | } | 
|  | 319 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 320 | func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) { | 
|  | 321 | fmt.Fprintln(w, "") | 
|  | 322 | fmt.Fprintln(w, "include $(CLEAR_VARS)") | 
|  | 323 | fmt.Fprintln(w, "LOCAL_MODULE :=", name) | 
|  | 324 | fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES :=", module) | 
|  | 325 | fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES += ", strings.Join(r.libs, " ")) | 
|  | 326 | fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for)) | 
|  | 327 | fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String()) | 
|  | 328 | fmt.Fprintln(w, "LOCAL_ROBOTEST_FILES :=", strings.Join(tests, " ")) | 
|  | 329 | if t := r.robolectricProperties.Test_options.Timeout; t != nil { | 
|  | 330 | fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t) | 
|  | 331 | } | 
| Colin Cross | 2787e8e | 2021-03-05 11:16:20 -0800 | [diff] [blame] | 332 | if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" { | 
|  | 333 | fmt.Fprintf(w, "-include prebuilts/misc/common/robolectric/%s/run_robotests.mk\n", v) | 
|  | 334 | } else { | 
|  | 335 | fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk") | 
|  | 336 | } | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 339 | // An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host | 
|  | 340 | // instead of on a device.  It also generates a rule with the name of the module prefixed with "Run" that can be | 
|  | 341 | // used to run the tests.  Running the tests with build rule will eventually be deprecated and replaced with atest. | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 342 | // | 
|  | 343 | // The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless | 
|  | 344 | // it is named BaseRobolectricTest.java.  The path to the each source file must exactly match the package | 
|  | 345 | // name, or match the package name when the prefix "src/" is removed. | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 346 | func RobolectricTestFactory() android.Module { | 
|  | 347 | module := &robolectricTest{} | 
|  | 348 |  | 
| Colin Cross | ce6734e | 2020-06-15 16:09:53 -0700 | [diff] [blame] | 349 | module.addHostProperties() | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 350 | module.AddProperties( | 
| Colin Cross | e323f3c | 2019-09-17 15:34:09 -0700 | [diff] [blame] | 351 | &module.Module.deviceProperties, | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 352 | &module.robolectricProperties, | 
|  | 353 | &module.testProperties) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 354 |  | 
|  | 355 | module.Module.dexpreopter.isTest = true | 
| Cole Faust | d57e8b2 | 2022-08-11 11:59:04 -0700 | [diff] [blame] | 356 | module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true) | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 357 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 358 | module.testProperties.Test_suites = []string{"robolectric-tests"} | 
|  | 359 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 360 | InitJavaModule(module, android.DeviceSupported) | 
|  | 361 | return module | 
|  | 362 | } | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 363 |  | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 364 | func (r *robolectricTest) InstallInTestcases() bool { return true } | 
|  | 365 | func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) { | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 366 | return &r.forceOSType, &r.forceArchType | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 367 | } | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 368 |  | 
|  | 369 | func robolectricRuntimesFactory() android.Module { | 
|  | 370 | module := &robolectricRuntimes{} | 
|  | 371 | module.AddProperties(&module.props) | 
| Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 372 | android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 373 | return module | 
|  | 374 | } | 
|  | 375 |  | 
|  | 376 | type robolectricRuntimesProperties struct { | 
|  | 377 | Jars []string `android:"path"` | 
|  | 378 | Lib  *string | 
|  | 379 | } | 
|  | 380 |  | 
|  | 381 | type robolectricRuntimes struct { | 
|  | 382 | android.ModuleBase | 
|  | 383 |  | 
|  | 384 | props robolectricRuntimesProperties | 
|  | 385 |  | 
|  | 386 | runtimes []android.InstallPath | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 387 |  | 
|  | 388 | forceOSType   android.OsType | 
|  | 389 | forceArchType android.ArchType | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 390 | } | 
|  | 391 |  | 
|  | 392 | func (r *robolectricRuntimes) TestSuites() []string { | 
|  | 393 | return []string{"robolectric-tests"} | 
|  | 394 | } | 
|  | 395 |  | 
|  | 396 | var _ android.TestSuiteModule = (*robolectricRuntimes)(nil) | 
|  | 397 |  | 
|  | 398 | func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 399 | if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 400 | ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib)) | 
|  | 401 | } | 
|  | 402 | } | 
|  | 403 |  | 
|  | 404 | func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Colin Cross | 5aa29a7 | 2020-09-14 19:54:47 -0700 | [diff] [blame] | 405 | if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os { | 
|  | 406 | return | 
|  | 407 | } | 
|  | 408 |  | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 409 | r.forceOSType = ctx.Config().BuildOS | 
|  | 410 | r.forceArchType = ctx.Config().BuildArch | 
|  | 411 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 412 | files := android.PathsForModuleSrc(ctx, r.props.Jars) | 
|  | 413 |  | 
|  | 414 | androidAllDir := android.PathForModuleInstall(ctx, "android-all") | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 415 | for _, from := range files { | 
|  | 416 | installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from) | 
|  | 417 | r.runtimes = append(r.runtimes, installedRuntime) | 
|  | 418 | } | 
|  | 419 |  | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 420 | if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 421 | runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag) | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 422 | if runtimeFromSourceModule == nil { | 
|  | 423 | if ctx.Config().AllowMissingDependencies() { | 
|  | 424 | ctx.AddMissingDependencies([]string{String(r.props.Lib)}) | 
|  | 425 | } else { | 
|  | 426 | ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib)) | 
|  | 427 | } | 
|  | 428 | return | 
|  | 429 | } | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 430 | runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "") | 
|  | 431 |  | 
| Joseph Murphy | c964841 | 2021-07-20 13:58:15 -0700 | [diff] [blame] | 432 | // "TREE" name is essential here because it hooks into the "TREE" name in | 
|  | 433 | // Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK | 
|  | 434 | // in Robolectric configs. | 
|  | 435 | runtimeName := "android-all-current-robolectric-r0.jar" | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 436 | installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar) | 
|  | 437 | r.runtimes = append(r.runtimes, installedRuntime) | 
|  | 438 | } | 
|  | 439 | } | 
|  | 440 |  | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 441 | func (r *robolectricRuntimes) InstallInTestcases() bool { return true } | 
|  | 442 | func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) { | 
| Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 443 | return &r.forceOSType, &r.forceArchType | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 444 | } |