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