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