1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
// Copyright 2019 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package java
import (
"fmt"
"android/soong/android"
"android/soong/java/config"
"android/soong/tradefed"
"github.com/google/blueprint"
"github.com/google/blueprint/proptools"
)
func init() {
RegisterRobolectricBuildComponents(android.InitRegistrationContext)
}
type RobolectricRuntimesInfo struct {
Runtimes []android.InstallPath
}
var RobolectricRuntimesInfoProvider = blueprint.NewProvider[RobolectricRuntimesInfo]()
type roboRuntimeOnlyDependencyTag struct {
blueprint.BaseDependencyTag
}
var roboRuntimeOnlyDepTag roboRuntimeOnlyDependencyTag
// Mark this tag so dependencies that use it are excluded from visibility enforcement.
func (t roboRuntimeOnlyDependencyTag) ExcludeFromVisibilityEnforcement() {}
var _ android.ExcludeFromVisibilityEnforcementTag = roboRuntimeOnlyDepTag
func RegisterRobolectricBuildComponents(ctx android.RegistrationContext) {
ctx.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
ctx.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory)
}
var robolectricDefaultLibs = []string{
"mockito-robolectric-prebuilt",
"truth",
// TODO(ccross): this is not needed at link time
"junitxml",
}
const robolectricCurrentLib = "Robolectric_all-target"
const clearcutJunitLib = "ClearcutJunitListenerAar"
const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt"
var (
roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"}
roboRuntimesTag = dependencyTag{name: "roboRuntimes"}
)
type robolectricProperties struct {
// The name of the android_app module that the tests will run against.
Instrumentation_for *string
// Additional libraries for which coverage data should be generated
Coverage_libs []string
Test_options struct {
// Timeout in seconds when running the tests.
Timeout *int64
// Number of shards to use when running the tests.
Shards *int64
}
// Use /external/robolectric rather than /external/robolectric-shadows as the version of robolectric
// to use. /external/robolectric closely tracks github's master, and will fully replace /external/robolectric-shadows
Upstream *bool
// Use strict mode to limit access of Robolectric API directly. See go/roboStrictMode
Strict_mode *bool
Jni_libs proptools.Configurable[[]string]
}
type robolectricTest struct {
Library
robolectricProperties robolectricProperties
testProperties testProperties
testConfig android.Path
data android.Paths
forceOSType android.OsType
forceArchType android.ArchType
}
func (r *robolectricTest) TestSuites() []string {
return r.testProperties.Test_suites
}
var _ android.TestSuiteModule = (*robolectricTest)(nil)
func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) {
r.Library.DepsMutator(ctx)
if r.robolectricProperties.Instrumentation_for != nil {
ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for))
} else {
ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module")
}
ctx.AddVariationDependencies(nil, roboRuntimeOnlyDepTag, clearcutJunitLib)
if proptools.BoolDefault(r.robolectricProperties.Strict_mode, true) {
ctx.AddVariationDependencies(nil, roboRuntimeOnlyDepTag, robolectricCurrentLib)
} else {
ctx.AddVariationDependencies(nil, staticLibTag, robolectricCurrentLib)
}
ctx.AddVariationDependencies(nil, staticLibTag, robolectricDefaultLibs...)
ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...)
ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
roboRuntimesTag, "robolectric-android-all-prebuilts")
for _, lib := range r.robolectricProperties.Jni_libs.GetOrDefault(ctx, nil) {
ctx.AddVariationDependencies(ctx.Config().BuildOSTarget.Variations(), jniLibTag, lib)
}
}
func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
r.forceOSType = ctx.Config().BuildOS
r.forceArchType = ctx.Config().BuildArch
var options []tradefed.Option
options = append(options, tradefed.Option{Name: "java-flags", Value: "-Drobolectric=true"})
if proptools.BoolDefault(r.robolectricProperties.Strict_mode, true) {
options = append(options, tradefed.Option{Name: "java-flags", Value: "-Drobolectric.strict.mode=true"})
}
r.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
TestConfigProp: r.testProperties.Test_config,
TestConfigTemplateProp: r.testProperties.Test_config_template,
TestSuites: r.testProperties.Test_suites,
TestRunnerOptions: options,
AutoGenConfig: r.testProperties.Auto_gen_config,
DeviceTemplate: "${RobolectricTestConfigTemplate}",
HostTemplate: "${RobolectricTestConfigTemplate}",
})
r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data)
r.data = append(r.data, android.PathsForModuleSrc(ctx, r.testProperties.Device_common_data)...)
r.data = append(r.data, android.PathsForModuleSrc(ctx, r.testProperties.Device_first_data)...)
r.data = append(r.data, android.PathsForModuleSrc(ctx, r.testProperties.Device_first_prefer32_data)...)
var ok bool
var instrumentedApp *JavaInfo
var appInfo *AppInfo
// TODO: this inserts paths to built files into the test, it should really be inserting the contents.
instrumented := ctx.GetDirectDepsProxyWithTag(instrumentationForTag)
if len(instrumented) == 1 {
appInfo, ok = android.OtherModuleProvider(ctx, instrumented[0], AppInfoProvider)
if !ok {
ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app")
}
instrumentedApp = android.OtherModuleProviderOrDefault(ctx, instrumented[0], JavaInfoProvider)
} else if !ctx.Config().AllowMissingDependencies() {
panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented)))
}
var resourceApk android.Path
var manifest android.Path
if appInfo != nil {
manifest = appInfo.MergedManifestFile
resourceApk = instrumentedApp.OutputFile
}
roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar")
generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar)
extraCombinedJars := android.Paths{roboTestConfigJar}
handleLibDeps := func(dep android.ModuleProxy) {
if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) {
if m, ok := android.OtherModuleProvider(ctx, dep, JavaInfoProvider); ok {
extraCombinedJars = append(extraCombinedJars, m.ImplementationAndResourcesJars...)
}
}
}
for _, dep := range ctx.GetDirectDepsProxyWithTag(libTag) {
handleLibDeps(dep)
}
for _, dep := range ctx.GetDirectDepsProxyWithTag(sdkLibTag) {
handleLibDeps(dep)
}
// handle the runtimeOnly tag for strict_mode
for _, dep := range ctx.GetDirectDepsProxyWithTag(roboRuntimeOnlyDepTag) {
handleLibDeps(dep)
}
if appInfo != nil {
extraCombinedJars = append(extraCombinedJars, instrumentedApp.ImplementationAndResourcesJars...)
}
r.stem = proptools.StringDefault(r.overridableProperties.Stem, ctx.ModuleName())
r.classLoaderContexts = r.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
r.dexpreopter.disableDexpreopt()
javaInfo := r.compile(ctx, nil, nil, nil, extraCombinedJars)
installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
var installDeps android.InstallPaths
for _, data := range r.data {
installedData := ctx.InstallFile(installPath, data.Rel(), data)
installDeps = append(installDeps, installedData)
}
if manifest != nil {
r.data = append(r.data, manifest)
installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", manifest)
installDeps = append(installDeps, installedManifest)
}
if resourceApk != nil {
r.data = append(r.data, resourceApk)
installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", resourceApk)
installDeps = append(installDeps, installedResourceApk)
}
runtimes := ctx.GetDirectDepProxyWithTag("robolectric-android-all-prebuilts", roboRuntimesTag)
for _, runtime := range android.OtherModuleProviderOrDefault(ctx, runtimes, RobolectricRuntimesInfoProvider).Runtimes {
installDeps = append(installDeps, runtime)
}
installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
installDeps = append(installDeps, installedConfig)
soInstallPath := installPath.Join(ctx, getLibPath(r.forceArchType))
for _, jniLib := range collectTransitiveJniDeps(ctx) {
installJni := ctx.InstallFile(soInstallPath, jniLib.path.Base(), jniLib.path)
installDeps = append(installDeps, installJni)
}
r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.outputFile, installDeps...)
if javaInfo != nil {
setExtraJavaInfo(ctx, r, javaInfo)
android.SetProvider(ctx, JavaInfoProvider, javaInfo)
}
}
func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) {
rule := android.NewRuleBuilder(pctx, ctx)
outputDir := outputFile.InSameDir(ctx)
configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties")
rule.Temporary(configFile)
rule.Command().Text("rm -f").Output(outputFile).Output(configFile)
rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String())
rule.Command().
Text("(").
Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()).
Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()).
Text(") >>").Output(configFile)
rule.Command().
BuiltTool("soong_zip").
FlagWithArg("-C ", outputDir.String()).
FlagWithInput("-f ", configFile).
FlagWithOutput("-o ", outputFile)
rule.Build("generate_test_config_samedir", "generate test_config.properties")
}
func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries {
entriesList := r.Library.AndroidMkEntries()
entries := &entriesList[0]
entries.ExtraEntries = append(entries.ExtraEntries,
func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", "robolectric-tests")
if r.testConfig != nil {
entries.SetPath("LOCAL_FULL_TEST_CONFIG", r.testConfig)
}
})
return entriesList
}
// An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
// instead of on a device.
func RobolectricTestFactory() android.Module {
module := &robolectricTest{}
module.addHostProperties()
module.AddProperties(
&module.Module.deviceProperties,
&module.robolectricProperties,
&module.testProperties)
module.Module.dexpreopter.isTest = true
module.Module.linter.properties.Lint.Test_module_type = proptools.BoolPtr(true)
module.testProperties.Test_suites = []string{"robolectric-tests"}
InitJavaModule(module, android.DeviceSupported)
return module
}
func (r *robolectricTest) InstallInTestcases() bool { return true }
func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) {
return &r.forceOSType, &r.forceArchType
}
func robolectricRuntimesFactory() android.Module {
module := &robolectricRuntimes{}
module.AddProperties(&module.props)
android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon)
return module
}
type robolectricRuntimesProperties struct {
Jars []string `android:"path"`
Lib *string
}
type robolectricRuntimes struct {
android.ModuleBase
props robolectricRuntimesProperties
runtimes []android.InstallPath
forceOSType android.OsType
forceArchType android.ArchType
}
func (r *robolectricRuntimes) TestSuites() []string {
return []string{"robolectric-tests"}
}
var _ android.TestSuiteModule = (*robolectricRuntimes)(nil)
func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) {
if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib))
}
}
func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) {
if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os {
return
}
r.forceOSType = ctx.Config().BuildOS
r.forceArchType = ctx.Config().BuildArch
files := android.PathsForModuleSrc(ctx, r.props.Jars)
androidAllDir := android.PathForModuleInstall(ctx, "android-all")
for _, from := range files {
installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from)
r.runtimes = append(r.runtimes, installedRuntime)
}
if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
runtimeFromSourceModule := ctx.GetDirectDepProxyWithTag(String(r.props.Lib), libTag)
if runtimeFromSourceModule == nil {
if ctx.Config().AllowMissingDependencies() {
ctx.AddMissingDependencies([]string{String(r.props.Lib)})
} else {
ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib))
}
return
}
runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
// "TREE" name is essential here because it hooks into the "TREE" name in
// Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK
// in Robolectric configs.
runtimeName := "android-all-current-robolectric-r0.jar"
installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
r.runtimes = append(r.runtimes, installedRuntime)
}
android.SetProvider(ctx, RobolectricRuntimesInfoProvider, RobolectricRuntimesInfo{
Runtimes: r.runtimes,
})
}
func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) {
return &r.forceOSType, &r.forceArchType
}
|