| 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{ | 
|  | 34 | "robolectric_android-all-stub", | 
|  | 35 | "Robolectric_all-target", | 
|  | 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 | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 42 | var ( | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 43 | roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"} | 
|  | 44 | roboRuntimesTag     = dependencyTag{name: "roboRuntimes"} | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 45 | ) | 
|  | 46 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 47 | type robolectricProperties struct { | 
|  | 48 | // The name of the android_app module that the tests will run against. | 
|  | 49 | Instrumentation_for *string | 
|  | 50 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 51 | // Additional libraries for which coverage data should be generated | 
|  | 52 | Coverage_libs []string | 
|  | 53 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 54 | Test_options struct { | 
|  | 55 | // Timeout in seconds when running the tests. | 
| Colin Cross | 2f9a7c8 | 2019-05-30 11:16:26 -0700 | [diff] [blame] | 56 | Timeout *int64 | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 57 |  | 
|  | 58 | // Number of shards to use when running the tests. | 
|  | 59 | Shards *int64 | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 60 | } | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | type robolectricTest struct { | 
|  | 64 | Library | 
|  | 65 |  | 
|  | 66 | robolectricProperties robolectricProperties | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 67 | testProperties        testProperties | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 68 |  | 
| Colin Cross | d2d1177 | 2019-05-30 11:17:23 -0700 | [diff] [blame] | 69 | libs  []string | 
|  | 70 | tests []string | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 71 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 72 | manifest    android.Path | 
|  | 73 | resourceApk android.Path | 
|  | 74 |  | 
|  | 75 | combinedJar android.WritablePath | 
|  | 76 |  | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 77 | roboSrcJar android.Path | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 78 |  | 
|  | 79 | testConfig android.Path | 
|  | 80 | data       android.Paths | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 83 | func (r *robolectricTest) TestSuites() []string { | 
|  | 84 | return r.testProperties.Test_suites | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | var _ android.TestSuiteModule = (*robolectricTest)(nil) | 
|  | 88 |  | 
| Colin Cross | 0ef0816 | 2019-05-01 15:50:51 -0700 | [diff] [blame] | 89 | func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 90 | r.Library.DepsMutator(ctx) | 
|  | 91 |  | 
|  | 92 | if r.robolectricProperties.Instrumentation_for != nil { | 
|  | 93 | ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for)) | 
|  | 94 | } else { | 
|  | 95 | ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module") | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...) | 
| Colin Cross | 3ec27ec | 2019-05-01 15:54:05 -0700 | [diff] [blame] | 99 |  | 
|  | 100 | ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...) | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 101 |  | 
|  | 102 | ctx.AddVariationDependencies(nil, 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 |  | 
|  | 329 | func (r *robolectricTest) InstallBypassMake() bool         { return true } | 
|  | 330 | func (r *robolectricTest) InstallInTestcases() bool        { return true } | 
|  | 331 | func (r *robolectricTest) InstallForceOS() *android.OsType { return &android.BuildOs } | 
|  | 332 |  | 
|  | 333 | func robolectricRuntimesFactory() android.Module { | 
|  | 334 | module := &robolectricRuntimes{} | 
|  | 335 | module.AddProperties(&module.props) | 
|  | 336 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
|  | 337 | return module | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | type robolectricRuntimesProperties struct { | 
|  | 341 | Jars []string `android:"path"` | 
|  | 342 | Lib  *string | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | type robolectricRuntimes struct { | 
|  | 346 | android.ModuleBase | 
|  | 347 |  | 
|  | 348 | props robolectricRuntimesProperties | 
|  | 349 |  | 
|  | 350 | runtimes []android.InstallPath | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | func (r *robolectricRuntimes) TestSuites() []string { | 
|  | 354 | return []string{"robolectric-tests"} | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | var _ android.TestSuiteModule = (*robolectricRuntimes)(nil) | 
|  | 358 |  | 
|  | 359 | func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 360 | if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 361 | ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib)) | 
|  | 362 | } | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 366 | files := android.PathsForModuleSrc(ctx, r.props.Jars) | 
|  | 367 |  | 
|  | 368 | androidAllDir := android.PathForModuleInstall(ctx, "android-all") | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 369 | for _, from := range files { | 
|  | 370 | installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from) | 
|  | 371 | r.runtimes = append(r.runtimes, installedRuntime) | 
|  | 372 | } | 
|  | 373 |  | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 374 | if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil { | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 375 | runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag) | 
| Jeongik Cha | 816a23a | 2020-07-08 01:09:23 +0900 | [diff] [blame] | 376 | if runtimeFromSourceModule == nil { | 
|  | 377 | if ctx.Config().AllowMissingDependencies() { | 
|  | 378 | ctx.AddMissingDependencies([]string{String(r.props.Lib)}) | 
|  | 379 | } else { | 
|  | 380 | ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib)) | 
|  | 381 | } | 
|  | 382 | return | 
|  | 383 | } | 
| Colin Cross | 8eebb13 | 2020-01-29 20:07:03 -0800 | [diff] [blame] | 384 | runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "") | 
|  | 385 |  | 
|  | 386 | runtimeName := fmt.Sprintf("android-all-%s-robolectric-r0.jar", | 
|  | 387 | ctx.Config().PlatformSdkCodename()) | 
|  | 388 | installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar) | 
|  | 389 | r.runtimes = append(r.runtimes, installedRuntime) | 
|  | 390 | } | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | func (r *robolectricRuntimes) InstallBypassMake() bool         { return true } | 
|  | 394 | func (r *robolectricRuntimes) InstallInTestcases() bool        { return true } | 
|  | 395 | func (r *robolectricRuntimes) InstallForceOS() *android.OsType { return &android.BuildOs } |