blob: c821e5bd35bbeb6bbed1c8266e98d22e5c893808 [file] [log] [blame]
Colin Cross0ef08162019-05-01 15:50:51 -07001// 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
15package java
16
17import (
18 "fmt"
19 "io"
Colin Crossd2d11772019-05-30 11:17:23 -070020 "strconv"
Colin Cross0ef08162019-05-01 15:50:51 -070021 "strings"
22
23 "android/soong/android"
Colin Cross8eebb132020-01-29 20:07:03 -080024 "android/soong/java/config"
25 "android/soong/tradefed"
Colin Cross0ef08162019-05-01 15:50:51 -070026)
27
28func init() {
29 android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
Colin Cross8eebb132020-01-29 20:07:03 -080030 android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory)
Colin Cross0ef08162019-05-01 15:50:51 -070031}
32
33var robolectricDefaultLibs = []string{
Colin Cross0ef08162019-05-01 15:50:51 -070034 "Robolectric_all-target",
35 "mockito-robolectric-prebuilt",
36 "truth-prebuilt",
Colin Cross8eebb132020-01-29 20:07:03 -080037 // TODO(ccross): this is not needed at link time
38 "junitxml",
Colin Cross0ef08162019-05-01 15:50:51 -070039}
40
Colin Cross3ec27ec2019-05-01 15:54:05 -070041var (
Colin Cross8eebb132020-01-29 20:07:03 -080042 roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"}
43 roboRuntimesTag = dependencyTag{name: "roboRuntimes"}
Colin Cross3ec27ec2019-05-01 15:54:05 -070044)
45
Colin Cross0ef08162019-05-01 15:50:51 -070046type robolectricProperties struct {
47 // The name of the android_app module that the tests will run against.
48 Instrumentation_for *string
49
Colin Cross3ec27ec2019-05-01 15:54:05 -070050 // Additional libraries for which coverage data should be generated
51 Coverage_libs []string
52
Colin Cross0ef08162019-05-01 15:50:51 -070053 Test_options struct {
54 // Timeout in seconds when running the tests.
Colin Cross2f9a7c82019-05-30 11:16:26 -070055 Timeout *int64
Colin Crossd2d11772019-05-30 11:17:23 -070056
57 // Number of shards to use when running the tests.
58 Shards *int64
Colin Cross0ef08162019-05-01 15:50:51 -070059 }
60}
61
62type robolectricTest struct {
63 Library
64
65 robolectricProperties robolectricProperties
Colin Cross8eebb132020-01-29 20:07:03 -080066 testProperties testProperties
Colin Cross0ef08162019-05-01 15:50:51 -070067
Colin Crossd2d11772019-05-30 11:17:23 -070068 libs []string
69 tests []string
Colin Cross3ec27ec2019-05-01 15:54:05 -070070
Colin Cross8eebb132020-01-29 20:07:03 -080071 manifest android.Path
72 resourceApk android.Path
73
74 combinedJar android.WritablePath
75
Colin Cross3ec27ec2019-05-01 15:54:05 -070076 roboSrcJar android.Path
Colin Cross8eebb132020-01-29 20:07:03 -080077
78 testConfig android.Path
79 data android.Paths
Colin Cross0ef08162019-05-01 15:50:51 -070080}
81
Colin Cross8eebb132020-01-29 20:07:03 -080082func (r *robolectricTest) TestSuites() []string {
83 return r.testProperties.Test_suites
84}
85
86var _ android.TestSuiteModule = (*robolectricTest)(nil)
87
Colin Cross0ef08162019-05-01 15:50:51 -070088func (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 Cross3ec27ec2019-05-01 15:54:05 -070098
99 ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...)
Colin Cross8eebb132020-01-29 20:07:03 -0800100
Colin Cross5aa29a72020-09-14 19:54:47 -0700101 ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
102 roboRuntimesTag, "robolectric-android-all-prebuilts")
Colin Cross0ef08162019-05-01 15:50:51 -0700103}
104
105func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross8eebb132020-01-29 20:07:03 -0800106 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 Cross3ec27ec2019-05-01 15:54:05 -0700111 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 Cross8eebb132020-01-29 20:07:03 -0800126 r.manifest = instrumentedApp.mergedManifestFile
127 r.resourceApk = instrumentedApp.outputFile
128
Colin Cross3ec27ec2019-05-01 15:54:05 -0700129 generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp)
130 r.extraResources = android.Paths{roboTestConfig}
131
Colin Cross0ef08162019-05-01 15:50:51 -0700132 r.Library.GenerateAndroidBuildActions(ctx)
133
Colin Cross3ec27ec2019-05-01 15:54:05 -0700134 roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar")
135 r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp)
136 r.roboSrcJar = roboSrcJar
137
Colin Cross8eebb132020-01-29 20:07:03 -0800138 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 Cross0ef08162019-05-01 15:50:51 -0700148 }
Colin Crossd2d11772019-05-30 11:17:23 -0700149
Colin Cross8eebb132020-01-29 20:07:03 -0800150 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 Crossd2d11772019-05-30 11:17:23 -0700162 // 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 Cross8eebb132020-01-29 20:07:03 -0800175
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 Crossd2d11772019-05-30 11:17:23 -0700198}
199
Colin Cross8eebb132020-01-29 20:07:03 -0800200func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath,
201 instrumentedApp *AndroidApp) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800202 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross8eebb132020-01-29 20:07:03 -0800203
Colin Cross3ec27ec2019-05-01 15:54:05 -0700204 manifest := instrumentedApp.mergedManifestFile
205 resourceApk := instrumentedApp.outputFile
206
Colin Cross3ec27ec2019-05-01 15:54:05 -0700207 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
Colin Crossf1a035e2020-11-16 17:32:30 -0800216 rule.Build("generate_test_config", "generate test_config.properties")
Colin Cross3ec27ec2019-05-01 15:54:05 -0700217}
218
Colin Cross8eebb132020-01-29 20:07:03 -0800219func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) {
Colin Crossf1a035e2020-11-16 17:32:30 -0800220 rule := android.NewRuleBuilder(pctx, ctx)
Colin Cross8eebb132020-01-29 20:07:03 -0800221
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().
Colin Crossf1a035e2020-11-16 17:32:30 -0800233 BuiltTool("soong_zip").
Colin Cross8eebb132020-01-29 20:07:03 -0800234 FlagWithArg("-C ", outputDir.String()).
235 FlagWithInput("-f ", configFile).
236 FlagWithOutput("-o ", outputFile)
237
Colin Crossf1a035e2020-11-16 17:32:30 -0800238 rule.Build("generate_test_config_samedir", "generate test_config.properties")
Colin Cross8eebb132020-01-29 20:07:03 -0800239}
240
Colin Cross3ec27ec2019-05-01 15:54:05 -0700241func (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 Park0b0e1b92019-12-03 13:24:29 +0900258func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries {
259 entriesList := r.Library.AndroidMkEntries()
260 entries := &entriesList[0]
Colin Cross0ef08162019-05-01 15:50:51 -0700261
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700262 entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
Jaewoong Jung02b11a62020-12-07 10:23:54 -0800263 func(w io.Writer, name, prefix, moduleDir string) {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700264 if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 {
Colin Cross0a2f7192019-09-23 14:33:09 -0700265 numShards := int(*s)
266 shardSize := (len(r.tests) + numShards - 1) / numShards
267 shards := android.ShardStrings(r.tests, shardSize)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700268 for i, shard := range shards {
269 r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard)
270 }
Colin Cross0ef08162019-05-01 15:50:51 -0700271
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700272 // 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 Crossd2d11772019-05-30 11:17:23 -0700282 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700283 },
Colin Cross0ef08162019-05-01 15:50:51 -0700284 }
285
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900286 return entriesList
Colin Cross0ef08162019-05-01 15:50:51 -0700287}
288
Colin Crossd2d11772019-05-30 11:17:23 -0700289func (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 Crossd2d11772019-05-30 11:17:23 -0700302}
303
Colin Cross0ef08162019-05-01 15:50:51 -0700304// 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 Crossd2d11772019-05-30 11:17:23 -0700307//
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 Cross0ef08162019-05-01 15:50:51 -0700311func RobolectricTestFactory() android.Module {
312 module := &robolectricTest{}
313
Colin Crossce6734e2020-06-15 16:09:53 -0700314 module.addHostProperties()
Colin Cross0ef08162019-05-01 15:50:51 -0700315 module.AddProperties(
Colin Crosse323f3c2019-09-17 15:34:09 -0700316 &module.Module.deviceProperties,
Colin Cross8eebb132020-01-29 20:07:03 -0800317 &module.robolectricProperties,
318 &module.testProperties)
Colin Cross0ef08162019-05-01 15:50:51 -0700319
320 module.Module.dexpreopter.isTest = true
Colin Cross014489c2020-06-02 20:09:13 -0700321 module.Module.linter.test = true
Colin Cross0ef08162019-05-01 15:50:51 -0700322
Colin Cross8eebb132020-01-29 20:07:03 -0800323 module.testProperties.Test_suites = []string{"robolectric-tests"}
324
Colin Cross0ef08162019-05-01 15:50:51 -0700325 InitJavaModule(module, android.DeviceSupported)
326 return module
327}
Colin Cross8eebb132020-01-29 20:07:03 -0800328
Jiyong Park87788b52020-09-01 12:37:45 +0900329func (r *robolectricTest) InstallBypassMake() bool { return true }
330func (r *robolectricTest) InstallInTestcases() bool { return true }
331func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) {
332 return &android.BuildOs, &android.BuildArch
333}
Colin Cross8eebb132020-01-29 20:07:03 -0800334
335func robolectricRuntimesFactory() android.Module {
336 module := &robolectricRuntimes{}
337 module.AddProperties(&module.props)
Colin Cross5aa29a72020-09-14 19:54:47 -0700338 android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon)
Colin Cross8eebb132020-01-29 20:07:03 -0800339 return module
340}
341
342type robolectricRuntimesProperties struct {
343 Jars []string `android:"path"`
344 Lib *string
345}
346
347type robolectricRuntimes struct {
348 android.ModuleBase
349
350 props robolectricRuntimesProperties
351
352 runtimes []android.InstallPath
353}
354
355func (r *robolectricRuntimes) TestSuites() []string {
356 return []string{"robolectric-tests"}
357}
358
359var _ android.TestSuiteModule = (*robolectricRuntimes)(nil)
360
361func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +0900362 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800363 ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib))
364 }
365}
366
367func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross5aa29a72020-09-14 19:54:47 -0700368 if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os {
369 return
370 }
371
Colin Cross8eebb132020-01-29 20:07:03 -0800372 files := android.PathsForModuleSrc(ctx, r.props.Jars)
373
374 androidAllDir := android.PathForModuleInstall(ctx, "android-all")
Colin Cross8eebb132020-01-29 20:07:03 -0800375 for _, from := range files {
376 installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from)
377 r.runtimes = append(r.runtimes, installedRuntime)
378 }
379
Jeongik Cha816a23a2020-07-08 01:09:23 +0900380 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800381 runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag)
Jeongik Cha816a23a2020-07-08 01:09:23 +0900382 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 Cross8eebb132020-01-29 20:07:03 -0800390 runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
391
Joseph Murphybc98d2f2020-08-17 17:00:00 -0700392 // TODO(murj) Update this to ctx.Config().PlatformSdkCodename() once the platform
393 // classes like android.os.Build are updated to S.
Colin Cross8eebb132020-01-29 20:07:03 -0800394 runtimeName := fmt.Sprintf("android-all-%s-robolectric-r0.jar",
Joseph Murphybc98d2f2020-08-17 17:00:00 -0700395 "R")
Colin Cross8eebb132020-01-29 20:07:03 -0800396 installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
397 r.runtimes = append(r.runtimes, installedRuntime)
398 }
399}
400
Jiyong Park87788b52020-09-01 12:37:45 +0900401func (r *robolectricRuntimes) InstallBypassMake() bool { return true }
402func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
403func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) {
404 return &android.BuildOs, &android.BuildArch
405}