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