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