blob: 572a95b28cc1d794e9a34e003fa3be6501ad3905 [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{
34 "robolectric_android-all-stub",
35 "Robolectric_all-target",
36 "mockito-robolectric-prebuilt",
37 "truth-prebuilt",
Colin Cross8eebb132020-01-29 20:07:03 -080038 // TODO(ccross): this is not needed at link time
39 "junitxml",
Colin Cross0ef08162019-05-01 15:50:51 -070040}
41
Colin Cross3ec27ec2019-05-01 15:54:05 -070042var (
Colin Cross8eebb132020-01-29 20:07:03 -080043 roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"}
44 roboRuntimesTag = dependencyTag{name: "roboRuntimes"}
Colin Cross3ec27ec2019-05-01 15:54:05 -070045)
46
Colin Cross0ef08162019-05-01 15:50:51 -070047type robolectricProperties struct {
48 // The name of the android_app module that the tests will run against.
49 Instrumentation_for *string
50
Colin Cross3ec27ec2019-05-01 15:54:05 -070051 // Additional libraries for which coverage data should be generated
52 Coverage_libs []string
53
Colin Cross0ef08162019-05-01 15:50:51 -070054 Test_options struct {
55 // Timeout in seconds when running the tests.
Colin Cross2f9a7c82019-05-30 11:16:26 -070056 Timeout *int64
Colin Crossd2d11772019-05-30 11:17:23 -070057
58 // Number of shards to use when running the tests.
59 Shards *int64
Colin Cross0ef08162019-05-01 15:50:51 -070060 }
61}
62
63type robolectricTest struct {
64 Library
65
66 robolectricProperties robolectricProperties
Colin Cross8eebb132020-01-29 20:07:03 -080067 testProperties testProperties
Colin Cross0ef08162019-05-01 15:50:51 -070068
Colin Crossd2d11772019-05-30 11:17:23 -070069 libs []string
70 tests []string
Colin Cross3ec27ec2019-05-01 15:54:05 -070071
Colin Cross8eebb132020-01-29 20:07:03 -080072 manifest android.Path
73 resourceApk android.Path
74
75 combinedJar android.WritablePath
76
Colin Cross3ec27ec2019-05-01 15:54:05 -070077 roboSrcJar android.Path
Colin Cross8eebb132020-01-29 20:07:03 -080078
79 testConfig android.Path
80 data android.Paths
Colin Cross0ef08162019-05-01 15:50:51 -070081}
82
Colin Cross8eebb132020-01-29 20:07:03 -080083func (r *robolectricTest) TestSuites() []string {
84 return r.testProperties.Test_suites
85}
86
87var _ android.TestSuiteModule = (*robolectricTest)(nil)
88
Colin Cross0ef08162019-05-01 15:50:51 -070089func (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 Cross3ec27ec2019-05-01 15:54:05 -070099
100 ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...)
Colin Cross8eebb132020-01-29 20:07:03 -0800101
102 ctx.AddVariationDependencies(nil, 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) {
202 rule := android.NewRuleBuilder()
203
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
216 rule.Build(pctx, ctx, "generate_test_config", "generate test_config.properties")
217}
218
Colin Cross8eebb132020-01-29 20:07:03 -0800219func 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 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{
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 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
329func (r *robolectricTest) InstallBypassMake() bool { return true }
330func (r *robolectricTest) InstallInTestcases() bool { return true }
331func (r *robolectricTest) InstallForceOS() *android.OsType { return &android.BuildOs }
332
333func robolectricRuntimesFactory() android.Module {
334 module := &robolectricRuntimes{}
335 module.AddProperties(&module.props)
336 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
337 return module
338}
339
340type robolectricRuntimesProperties struct {
341 Jars []string `android:"path"`
342 Lib *string
343}
344
345type robolectricRuntimes struct {
346 android.ModuleBase
347
348 props robolectricRuntimesProperties
349
350 runtimes []android.InstallPath
351}
352
353func (r *robolectricRuntimes) TestSuites() []string {
354 return []string{"robolectric-tests"}
355}
356
357var _ android.TestSuiteModule = (*robolectricRuntimes)(nil)
358
359func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) {
Jeongik Cha816a23a2020-07-08 01:09:23 +0900360 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800361 ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib))
362 }
363}
364
365func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) {
366 files := android.PathsForModuleSrc(ctx, r.props.Jars)
367
368 androidAllDir := android.PathForModuleInstall(ctx, "android-all")
Colin Cross8eebb132020-01-29 20:07:03 -0800369 for _, from := range files {
370 installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from)
371 r.runtimes = append(r.runtimes, installedRuntime)
372 }
373
Jeongik Cha816a23a2020-07-08 01:09:23 +0900374 if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
Colin Cross8eebb132020-01-29 20:07:03 -0800375 runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag)
Jeongik Cha816a23a2020-07-08 01:09:23 +0900376 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 Cross8eebb132020-01-29 20:07:03 -0800384 runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
385
Joseph Murphybc98d2f2020-08-17 17:00:00 -0700386 // TODO(murj) Update this to ctx.Config().PlatformSdkCodename() once the platform
387 // classes like android.os.Build are updated to S.
Colin Cross8eebb132020-01-29 20:07:03 -0800388 runtimeName := fmt.Sprintf("android-all-%s-robolectric-r0.jar",
Joseph Murphybc98d2f2020-08-17 17:00:00 -0700389 "R")
Colin Cross8eebb132020-01-29 20:07:03 -0800390 installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
391 r.runtimes = append(r.runtimes, installedRuntime)
392 }
393}
394
395func (r *robolectricRuntimes) InstallBypassMake() bool { return true }
396func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
397func (r *robolectricRuntimes) InstallForceOS() *android.OsType { return &android.BuildOs }