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
15package java
16
17import (
18	"fmt"
19	"io"
20	"strconv"
21	"strings"
22
23	"android/soong/android"
24	"android/soong/java/config"
25	"android/soong/tradefed"
26)
27
28func init() {
29	android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
30	android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory)
31}
32
33var robolectricDefaultLibs = []string{
34	"robolectric_android-all-stub",
35	"Robolectric_all-target",
36	"mockito-robolectric-prebuilt",
37	"truth-prebuilt",
38	// TODO(ccross): this is not needed at link time
39	"junitxml",
40}
41
42var (
43	roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"}
44	roboRuntimesTag     = dependencyTag{name: "roboRuntimes"}
45)
46
47type robolectricProperties struct {
48	// The name of the android_app module that the tests will run against.
49	Instrumentation_for *string
50
51	// Additional libraries for which coverage data should be generated
52	Coverage_libs []string
53
54	Test_options struct {
55		// Timeout in seconds when running the tests.
56		Timeout *int64
57
58		// Number of shards to use when running the tests.
59		Shards *int64
60	}
61}
62
63type robolectricTest struct {
64	Library
65
66	robolectricProperties robolectricProperties
67	testProperties        testProperties
68
69	libs  []string
70	tests []string
71
72	manifest    android.Path
73	resourceApk android.Path
74
75	combinedJar android.WritablePath
76
77	roboSrcJar android.Path
78
79	testConfig android.Path
80	data       android.Paths
81}
82
83func (r *robolectricTest) TestSuites() []string {
84	return r.testProperties.Test_suites
85}
86
87var _ android.TestSuiteModule = (*robolectricTest)(nil)
88
89func (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...)
99
100	ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...)
101
102	ctx.AddVariationDependencies(nil, roboRuntimesTag, "robolectric-android-all-prebuilts")
103}
104
105func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
106	r.testConfig = tradefed.AutoGenRobolectricTestConfig(ctx, r.testProperties.Test_config,
107		r.testProperties.Test_config_template, r.testProperties.Test_suites,
108		r.testProperties.Auto_gen_config)
109	r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data)
110
111	roboTestConfig := android.PathForModuleGen(ctx, "robolectric").
112		Join(ctx, "com/android/tools/test_config.properties")
113
114	// TODO: this inserts paths to built files into the test, it should really be inserting the contents.
115	instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag)
116
117	if len(instrumented) != 1 {
118		panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented)))
119	}
120
121	instrumentedApp, ok := instrumented[0].(*AndroidApp)
122	if !ok {
123		ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app")
124	}
125
126	r.manifest = instrumentedApp.mergedManifestFile
127	r.resourceApk = instrumentedApp.outputFile
128
129	generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp)
130	r.extraResources = android.Paths{roboTestConfig}
131
132	r.Library.GenerateAndroidBuildActions(ctx)
133
134	roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar")
135	r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp)
136	r.roboSrcJar = roboSrcJar
137
138	roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar")
139	generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar)
140
141	combinedJarJars := android.Paths{
142		// roboTestConfigJar comes first so that its com/android/tools/test_config.properties
143		// overrides the one from r.extraResources.  The r.extraResources one can be removed
144		// once the Make test runner is removed.
145		roboTestConfigJar,
146		r.outputFile,
147		instrumentedApp.implementationAndResourcesJar,
148	}
149
150	for _, dep := range ctx.GetDirectDepsWithTag(libTag) {
151		m := dep.(Dependency)
152		r.libs = append(r.libs, m.BaseModuleName())
153		if !android.InList(m.BaseModuleName(), config.FrameworkLibraries) {
154			combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars()...)
155		}
156	}
157
158	r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base())
159	TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{},
160		false, nil, nil)
161
162	// TODO: this could all be removed if tradefed was used as the test runner, it will find everything
163	// annotated as a test and run it.
164	for _, src := range r.compiledJavaSrcs {
165		s := src.Rel()
166		if !strings.HasSuffix(s, "Test.java") {
167			continue
168		} else if strings.HasSuffix(s, "/BaseRobolectricTest.java") {
169			continue
170		} else if strings.HasPrefix(s, "src/") {
171			s = strings.TrimPrefix(s, "src/")
172		}
173		r.tests = append(r.tests, s)
174	}
175
176	r.data = append(r.data, r.manifest, r.resourceApk)
177
178	runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag)
179
180	installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
181
182	installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk)
183	installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest)
184	installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
185
186	var installDeps android.Paths
187	for _, runtime := range runtimes.(*robolectricRuntimes).runtimes {
188		installDeps = append(installDeps, runtime)
189	}
190	installDeps = append(installDeps, installedResourceApk, installedManifest, installedConfig)
191
192	for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) {
193		installedData := ctx.InstallFile(installPath, data.Rel(), data)
194		installDeps = append(installDeps, installedData)
195	}
196
197	ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...)
198}
199
200func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath,
201	instrumentedApp *AndroidApp) {
202	rule := android.NewRuleBuilder()
203
204	manifest := instrumentedApp.mergedManifestFile
205	resourceApk := instrumentedApp.outputFile
206
207	rule.Command().Text("rm -f").Output(outputFile)
208	rule.Command().
209		Textf(`echo "android_merged_manifest=%s" >>`, manifest.String()).Output(outputFile).Text("&&").
210		Textf(`echo "android_resource_apk=%s" >>`, resourceApk.String()).Output(outputFile).
211		// Make it depend on the files to which it points so the test file's timestamp is updated whenever the
212		// contents change
213		Implicit(manifest).
214		Implicit(resourceApk)
215
216	rule.Build(pctx, ctx, "generate_test_config", "generate test_config.properties")
217}
218
219func 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
241func (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
258func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries {
259	entriesList := r.Library.AndroidMkEntries()
260	entries := &entriesList[0]
261
262	entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
263		func(w io.Writer, name, prefix, moduleDir string, entries *android.AndroidMkEntries) {
264			if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 {
265				numShards := int(*s)
266				shardSize := (len(r.tests) + numShards - 1) / numShards
267				shards := android.ShardStrings(r.tests, shardSize)
268				for i, shard := range shards {
269					r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard)
270				}
271
272				// TODO: add rules to dist the outputs of the individual tests, or combine them together?
273				fmt.Fprintln(w, "")
274				fmt.Fprintln(w, ".PHONY:", "Run"+name)
275				fmt.Fprintln(w, "Run"+name, ": \\")
276				for i := range shards {
277					fmt.Fprintln(w, "   ", "Run"+name+strconv.Itoa(i), "\\")
278				}
279				fmt.Fprintln(w, "")
280			} else {
281				r.writeTestRunner(w, name, "Run"+name, r.tests)
282			}
283		},
284	}
285
286	return entriesList
287}
288
289func (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")
302}
303
304// An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
305// instead of on a device.  It also generates a rule with the name of the module prefixed with "Run" that can be
306// used to run the tests.  Running the tests with build rule will eventually be deprecated and replaced with atest.
307//
308// The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless
309// it is named BaseRobolectricTest.java.  The path to the each source file must exactly match the package
310// name, or match the package name when the prefix "src/" is removed.
311func RobolectricTestFactory() android.Module {
312	module := &robolectricTest{}
313
314	module.addHostProperties()
315	module.AddProperties(
316		&module.Module.deviceProperties,
317		&module.robolectricProperties,
318		&module.testProperties)
319
320	module.Module.dexpreopter.isTest = true
321	module.Module.linter.test = true
322
323	module.testProperties.Test_suites = []string{"robolectric-tests"}
324
325	InitJavaModule(module, android.DeviceSupported)
326	return module
327}
328
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) {
360	if !ctx.Config().UnbundledBuildUsePrebuiltSdks() && r.props.Lib != nil {
361		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")
369	for _, from := range files {
370		installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from)
371		r.runtimes = append(r.runtimes, installedRuntime)
372	}
373
374	if !ctx.Config().UnbundledBuildUsePrebuiltSdks() && r.props.Lib != nil {
375		runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag)
376		runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
377
378		runtimeName := fmt.Sprintf("android-all-%s-robolectric-r0.jar",
379			ctx.Config().PlatformSdkCodename())
380		installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
381		r.runtimes = append(r.runtimes, installedRuntime)
382	}
383}
384
385func (r *robolectricRuntimes) InstallBypassMake() bool         { return true }
386func (r *robolectricRuntimes) InstallInTestcases() bool        { return true }
387func (r *robolectricRuntimes) InstallForceOS() *android.OsType { return &android.BuildOs }
388