2019-05-02 06:50:51 +08:00
|
|
|
// 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"
|
|
|
|
"io"
|
2019-05-31 02:17:23 +08:00
|
|
|
"strconv"
|
2019-05-02 06:50:51 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"android/soong/android"
|
2020-01-30 12:07:03 +08:00
|
|
|
"android/soong/java/config"
|
|
|
|
"android/soong/tradefed"
|
2019-05-02 06:50:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
|
2020-01-30 12:07:03 +08:00
|
|
|
android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory)
|
2019-05-02 06:50:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var robolectricDefaultLibs = []string{
|
|
|
|
"mockito-robolectric-prebuilt",
|
|
|
|
"truth-prebuilt",
|
2020-01-30 12:07:03 +08:00
|
|
|
// TODO(ccross): this is not needed at link time
|
|
|
|
"junitxml",
|
2019-05-02 06:50:51 +08:00
|
|
|
}
|
|
|
|
|
2021-03-06 03:16:20 +08:00
|
|
|
const robolectricCurrentLib = "Robolectric_all-target"
|
|
|
|
const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt"
|
|
|
|
|
2019-05-02 06:54:05 +08:00
|
|
|
var (
|
2020-01-30 12:07:03 +08:00
|
|
|
roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"}
|
|
|
|
roboRuntimesTag = dependencyTag{name: "roboRuntimes"}
|
2019-05-02 06:54:05 +08:00
|
|
|
)
|
|
|
|
|
2019-05-02 06:50:51 +08:00
|
|
|
type robolectricProperties struct {
|
|
|
|
// The name of the android_app module that the tests will run against.
|
|
|
|
Instrumentation_for *string
|
|
|
|
|
2019-05-02 06:54:05 +08:00
|
|
|
// Additional libraries for which coverage data should be generated
|
|
|
|
Coverage_libs []string
|
|
|
|
|
2019-05-02 06:50:51 +08:00
|
|
|
Test_options struct {
|
|
|
|
// Timeout in seconds when running the tests.
|
2019-05-31 02:16:26 +08:00
|
|
|
Timeout *int64
|
2019-05-31 02:17:23 +08:00
|
|
|
|
|
|
|
// Number of shards to use when running the tests.
|
|
|
|
Shards *int64
|
2019-05-02 06:50:51 +08:00
|
|
|
}
|
2021-03-06 03:16:20 +08:00
|
|
|
|
|
|
|
// The version number of a robolectric prebuilt to use from prebuilts/misc/common/robolectric
|
|
|
|
// instead of the one built from source in external/robolectric-shadows.
|
|
|
|
Robolectric_prebuilt_version *string
|
2019-05-02 06:50:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type robolectricTest struct {
|
|
|
|
Library
|
|
|
|
|
|
|
|
robolectricProperties robolectricProperties
|
2020-01-30 12:07:03 +08:00
|
|
|
testProperties testProperties
|
2019-05-02 06:50:51 +08:00
|
|
|
|
2019-05-31 02:17:23 +08:00
|
|
|
libs []string
|
|
|
|
tests []string
|
2019-05-02 06:54:05 +08:00
|
|
|
|
2020-01-30 12:07:03 +08:00
|
|
|
manifest android.Path
|
|
|
|
resourceApk android.Path
|
|
|
|
|
|
|
|
combinedJar android.WritablePath
|
|
|
|
|
2019-05-02 06:54:05 +08:00
|
|
|
roboSrcJar android.Path
|
2020-01-30 12:07:03 +08:00
|
|
|
|
|
|
|
testConfig android.Path
|
|
|
|
data android.Paths
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *robolectricTest) TestSuites() []string {
|
|
|
|
return r.testProperties.Test_suites
|
2019-05-02 06:50:51 +08:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:07:03 +08:00
|
|
|
var _ android.TestSuiteModule = (*robolectricTest)(nil)
|
|
|
|
|
2019-05-02 06:50:51 +08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-03-06 03:16:20 +08:00
|
|
|
if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
|
|
|
|
ctx.AddVariationDependencies(nil, libTag, fmt.Sprintf(robolectricPrebuiltLibPattern, v))
|
|
|
|
} else {
|
|
|
|
ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib)
|
|
|
|
}
|
|
|
|
|
2019-05-02 06:50:51 +08:00
|
|
|
ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...)
|
2019-05-02 06:54:05 +08:00
|
|
|
|
|
|
|
ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...)
|
2020-01-30 12:07:03 +08:00
|
|
|
|
2020-09-15 10:54:47 +08:00
|
|
|
ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
|
|
|
|
roboRuntimesTag, "robolectric-android-all-prebuilts")
|
2019-05-02 06:50:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2020-01-30 12:07:03 +08:00
|
|
|
r.testConfig = tradefed.AutoGenRobolectricTestConfig(ctx, r.testProperties.Test_config,
|
|
|
|
r.testProperties.Test_config_template, r.testProperties.Test_suites,
|
|
|
|
r.testProperties.Auto_gen_config)
|
|
|
|
r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data)
|
|
|
|
|
2019-05-02 06:54:05 +08:00
|
|
|
roboTestConfig := android.PathForModuleGen(ctx, "robolectric").
|
|
|
|
Join(ctx, "com/android/tools/test_config.properties")
|
|
|
|
|
|
|
|
// TODO: this inserts paths to built files into the test, it should really be inserting the contents.
|
|
|
|
instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag)
|
|
|
|
|
|
|
|
if len(instrumented) != 1 {
|
|
|
|
panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented)))
|
|
|
|
}
|
|
|
|
|
|
|
|
instrumentedApp, ok := instrumented[0].(*AndroidApp)
|
|
|
|
if !ok {
|
|
|
|
ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app")
|
|
|
|
}
|
|
|
|
|
2020-01-30 12:07:03 +08:00
|
|
|
r.manifest = instrumentedApp.mergedManifestFile
|
|
|
|
r.resourceApk = instrumentedApp.outputFile
|
|
|
|
|
2019-05-02 06:54:05 +08:00
|
|
|
generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp)
|
|
|
|
r.extraResources = android.Paths{roboTestConfig}
|
|
|
|
|
2019-05-02 06:50:51 +08:00
|
|
|
r.Library.GenerateAndroidBuildActions(ctx)
|
|
|
|
|
2019-05-02 06:54:05 +08:00
|
|
|
roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar")
|
|
|
|
r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp)
|
|
|
|
r.roboSrcJar = roboSrcJar
|
|
|
|
|
2020-01-30 12:07:03 +08:00
|
|
|
roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar")
|
|
|
|
generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar)
|
|
|
|
|
|
|
|
combinedJarJars := android.Paths{
|
|
|
|
// roboTestConfigJar comes first so that its com/android/tools/test_config.properties
|
|
|
|
// overrides the one from r.extraResources. The r.extraResources one can be removed
|
|
|
|
// once the Make test runner is removed.
|
|
|
|
roboTestConfigJar,
|
|
|
|
r.outputFile,
|
|
|
|
instrumentedApp.implementationAndResourcesJar,
|
|
|
|
}
|
|
|
|
|
2019-05-02 06:50:51 +08:00
|
|
|
for _, dep := range ctx.GetDirectDepsWithTag(libTag) {
|
2021-02-02 05:59:03 +08:00
|
|
|
m := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
|
|
|
|
r.libs = append(r.libs, ctx.OtherModuleName(dep))
|
|
|
|
if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) {
|
|
|
|
combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars...)
|
2020-01-30 12:07:03 +08:00
|
|
|
}
|
2019-05-02 06:50:51 +08:00
|
|
|
}
|
2019-05-31 02:17:23 +08:00
|
|
|
|
2020-01-30 12:07:03 +08:00
|
|
|
r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base())
|
|
|
|
TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{},
|
|
|
|
false, nil, nil)
|
|
|
|
|
2019-05-31 02:17:23 +08:00
|
|
|
// TODO: this could all be removed if tradefed was used as the test runner, it will find everything
|
|
|
|
// annotated as a test and run it.
|
|
|
|
for _, src := range r.compiledJavaSrcs {
|
|
|
|
s := src.Rel()
|
|
|
|
if !strings.HasSuffix(s, "Test.java") {
|
|
|
|
continue
|
|
|
|
} else if strings.HasSuffix(s, "/BaseRobolectricTest.java") {
|
|
|
|
continue
|
|
|
|
} else if strings.HasPrefix(s, "src/") {
|
|
|
|
s = strings.TrimPrefix(s, "src/")
|
|
|
|
}
|
|
|
|
r.tests = append(r.tests, s)
|
|
|
|
}
|
2020-01-30 12:07:03 +08:00
|
|
|
|
|
|
|
r.data = append(r.data, r.manifest, r.resourceApk)
|
|
|
|
|
|
|
|
runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag)
|
|
|
|
|
|
|
|
installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
|
|
|
|
|
|
|
|
installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk)
|
|
|
|
installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest)
|
|
|
|
installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
|
|
|
|
|
|
|
|
var installDeps android.Paths
|
|
|
|
for _, runtime := range runtimes.(*robolectricRuntimes).runtimes {
|
|
|
|
installDeps = append(installDeps, runtime)
|
|
|
|
}
|
|
|
|
installDeps = append(installDeps, installedResourceApk, installedManifest, installedConfig)
|
|
|
|
|
|
|
|
for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) {
|
|
|
|
installedData := ctx.InstallFile(installPath, data.Rel(), data)
|
|
|
|
installDeps = append(installDeps, installedData)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...)
|
2019-05-31 02:17:23 +08:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:07:03 +08:00
|
|
|
func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath,
|
|
|
|
instrumentedApp *AndroidApp) {
|
2020-11-17 09:32:30 +08:00
|
|
|
rule := android.NewRuleBuilder(pctx, ctx)
|
2020-01-30 12:07:03 +08:00
|
|
|
|
2019-05-02 06:54:05 +08:00
|
|
|
manifest := instrumentedApp.mergedManifestFile
|
|
|
|
resourceApk := instrumentedApp.outputFile
|
|
|
|
|
|
|
|
rule.Command().Text("rm -f").Output(outputFile)
|
|
|
|
rule.Command().
|
|
|
|
Textf(`echo "android_merged_manifest=%s" >>`, manifest.String()).Output(outputFile).Text("&&").
|
|
|
|
Textf(`echo "android_resource_apk=%s" >>`, resourceApk.String()).Output(outputFile).
|
|
|
|
// Make it depend on the files to which it points so the test file's timestamp is updated whenever the
|
|
|
|
// contents change
|
|
|
|
Implicit(manifest).
|
|
|
|
Implicit(resourceApk)
|
|
|
|
|
2020-11-17 09:32:30 +08:00
|
|
|
rule.Build("generate_test_config", "generate test_config.properties")
|
2019-05-02 06:54:05 +08:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:07:03 +08:00
|
|
|
func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) {
|
2020-11-17 09:32:30 +08:00
|
|
|
rule := android.NewRuleBuilder(pctx, ctx)
|
2020-01-30 12:07:03 +08:00
|
|
|
|
|
|
|
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().
|
2020-11-17 09:32:30 +08:00
|
|
|
BuiltTool("soong_zip").
|
2020-01-30 12:07:03 +08:00
|
|
|
FlagWithArg("-C ", outputDir.String()).
|
|
|
|
FlagWithInput("-f ", configFile).
|
|
|
|
FlagWithOutput("-o ", outputFile)
|
|
|
|
|
2020-11-17 09:32:30 +08:00
|
|
|
rule.Build("generate_test_config_samedir", "generate test_config.properties")
|
2020-01-30 12:07:03 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 06:54:05 +08:00
|
|
|
func (r *robolectricTest) generateRoboSrcJar(ctx android.ModuleContext, outputFile android.WritablePath,
|
|
|
|
instrumentedApp *AndroidApp) {
|
|
|
|
|
|
|
|
srcJarArgs := copyOf(instrumentedApp.srcJarArgs)
|
|
|
|
srcJarDeps := append(android.Paths(nil), instrumentedApp.srcJarDeps...)
|
|
|
|
|
|
|
|
for _, m := range ctx.GetDirectDepsWithTag(roboCoverageLibsTag) {
|
2021-02-02 05:59:03 +08:00
|
|
|
if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
|
|
|
|
dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
|
|
|
|
srcJarArgs = append(srcJarArgs, dep.SrcJarArgs...)
|
|
|
|
srcJarDeps = append(srcJarDeps, dep.SrcJarDeps...)
|
2019-05-02 06:54:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TransformResourcesToJar(ctx, outputFile, srcJarArgs, srcJarDeps)
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:24:29 +08:00
|
|
|
func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries {
|
|
|
|
entriesList := r.Library.AndroidMkEntries()
|
|
|
|
entries := &entriesList[0]
|
2019-08-30 05:56:03 +08:00
|
|
|
|
|
|
|
entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
|
2020-12-08 02:23:54 +08:00
|
|
|
func(w io.Writer, name, prefix, moduleDir string) {
|
2019-08-30 05:56:03 +08:00
|
|
|
if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 {
|
2019-09-24 05:33:09 +08:00
|
|
|
numShards := int(*s)
|
|
|
|
shardSize := (len(r.tests) + numShards - 1) / numShards
|
|
|
|
shards := android.ShardStrings(r.tests, shardSize)
|
2019-08-30 05:56:03 +08:00
|
|
|
for i, shard := range shards {
|
|
|
|
r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add rules to dist the outputs of the individual tests, or combine them together?
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, ".PHONY:", "Run"+name)
|
|
|
|
fmt.Fprintln(w, "Run"+name, ": \\")
|
|
|
|
for i := range shards {
|
|
|
|
fmt.Fprintln(w, " ", "Run"+name+strconv.Itoa(i), "\\")
|
|
|
|
}
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
} else {
|
|
|
|
r.writeTestRunner(w, name, "Run"+name, r.tests)
|
2019-05-31 02:17:23 +08:00
|
|
|
}
|
2019-08-30 05:56:03 +08:00
|
|
|
},
|
2019-05-02 06:50:51 +08:00
|
|
|
}
|
|
|
|
|
2019-12-03 12:24:29 +08:00
|
|
|
return entriesList
|
2019-05-02 06:50:51 +08:00
|
|
|
}
|
|
|
|
|
2019-05-31 02:17:23 +08:00
|
|
|
func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) {
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "include $(CLEAR_VARS)")
|
|
|
|
fmt.Fprintln(w, "LOCAL_MODULE :=", name)
|
|
|
|
fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES :=", module)
|
|
|
|
fmt.Fprintln(w, "LOCAL_JAVA_LIBRARIES += ", strings.Join(r.libs, " "))
|
|
|
|
fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for))
|
|
|
|
fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String())
|
|
|
|
fmt.Fprintln(w, "LOCAL_ROBOTEST_FILES :=", strings.Join(tests, " "))
|
|
|
|
if t := r.robolectricProperties.Test_options.Timeout; t != nil {
|
|
|
|
fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t)
|
|
|
|
}
|
2021-03-06 03:16:20 +08:00
|
|
|
if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
|
|
|
|
fmt.Fprintf(w, "-include prebuilts/misc/common/robolectric/%s/run_robotests.mk\n", v)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk")
|
|
|
|
}
|
2019-05-31 02:17:23 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 06:50:51 +08:00
|
|
|
// An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
|
|
|
|
// instead of on a device. It also generates a rule with the name of the module prefixed with "Run" that can be
|
|
|
|
// used to run the tests. Running the tests with build rule will eventually be deprecated and replaced with atest.
|
2019-05-31 02:17:23 +08:00
|
|
|
//
|
|
|
|
// The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless
|
|
|
|
// it is named BaseRobolectricTest.java. The path to the each source file must exactly match the package
|
|
|
|
// name, or match the package name when the prefix "src/" is removed.
|
2019-05-02 06:50:51 +08:00
|
|
|
func RobolectricTestFactory() android.Module {
|
|
|
|
module := &robolectricTest{}
|
|
|
|
|
2020-06-16 07:09:53 +08:00
|
|
|
module.addHostProperties()
|
2019-05-02 06:50:51 +08:00
|
|
|
module.AddProperties(
|
2019-09-18 06:34:09 +08:00
|
|
|
&module.Module.deviceProperties,
|
2020-01-30 12:07:03 +08:00
|
|
|
&module.robolectricProperties,
|
|
|
|
&module.testProperties)
|
2019-05-02 06:50:51 +08:00
|
|
|
|
|
|
|
module.Module.dexpreopter.isTest = true
|
2020-06-03 11:09:13 +08:00
|
|
|
module.Module.linter.test = true
|
2019-05-02 06:50:51 +08:00
|
|
|
|
2020-01-30 12:07:03 +08:00
|
|
|
module.testProperties.Test_suites = []string{"robolectric-tests"}
|
|
|
|
|
2019-05-02 06:50:51 +08:00
|
|
|
InitJavaModule(module, android.DeviceSupported)
|
|
|
|
return module
|
|
|
|
}
|
2020-01-30 12:07:03 +08:00
|
|
|
|
2020-09-01 11:37:45 +08:00
|
|
|
func (r *robolectricTest) InstallBypassMake() bool { return true }
|
|
|
|
func (r *robolectricTest) InstallInTestcases() bool { return true }
|
|
|
|
func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) {
|
|
|
|
return &android.BuildOs, &android.BuildArch
|
|
|
|
}
|
2020-01-30 12:07:03 +08:00
|
|
|
|
|
|
|
func robolectricRuntimesFactory() android.Module {
|
|
|
|
module := &robolectricRuntimes{}
|
|
|
|
module.AddProperties(&module.props)
|
2020-09-15 10:54:47 +08:00
|
|
|
android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon)
|
2020-01-30 12:07:03 +08:00
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
|
|
|
type robolectricRuntimesProperties struct {
|
|
|
|
Jars []string `android:"path"`
|
|
|
|
Lib *string
|
|
|
|
}
|
|
|
|
|
|
|
|
type robolectricRuntimes struct {
|
|
|
|
android.ModuleBase
|
|
|
|
|
|
|
|
props robolectricRuntimesProperties
|
|
|
|
|
|
|
|
runtimes []android.InstallPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *robolectricRuntimes) TestSuites() []string {
|
|
|
|
return []string{"robolectric-tests"}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ android.TestSuiteModule = (*robolectricRuntimes)(nil)
|
|
|
|
|
|
|
|
func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) {
|
2020-07-08 00:09:23 +08:00
|
|
|
if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
|
2020-01-30 12:07:03 +08:00
|
|
|
ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2020-09-15 10:54:47 +08:00
|
|
|
if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-30 12:07:03 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-08 00:09:23 +08:00
|
|
|
if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
|
2020-01-30 12:07:03 +08:00
|
|
|
runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag)
|
2020-07-08 00:09:23 +08:00
|
|
|
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
|
|
|
|
}
|
2020-01-30 12:07:03 +08:00
|
|
|
runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
|
|
|
|
|
|
|
|
runtimeName := fmt.Sprintf("android-all-%s-robolectric-r0.jar",
|
2020-11-19 08:59:38 +08:00
|
|
|
ctx.Config().PlatformSdkCodename())
|
2020-01-30 12:07:03 +08:00
|
|
|
installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
|
|
|
|
r.runtimes = append(r.runtimes, installedRuntime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 11:37:45 +08:00
|
|
|
func (r *robolectricRuntimes) InstallBypassMake() bool { return true }
|
|
|
|
func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
|
|
|
|
func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) {
|
|
|
|
return &android.BuildOs, &android.BuildArch
|
|
|
|
}
|