robolectric.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. package java
  15. import (
  16. "fmt"
  17. "io"
  18. "strconv"
  19. "strings"
  20. "android/soong/android"
  21. "android/soong/java/config"
  22. "android/soong/tradefed"
  23. "github.com/google/blueprint/proptools"
  24. )
  25. func init() {
  26. android.RegisterModuleType("android_robolectric_test", RobolectricTestFactory)
  27. android.RegisterModuleType("android_robolectric_runtimes", robolectricRuntimesFactory)
  28. }
  29. var robolectricDefaultLibs = []string{
  30. "mockito-robolectric-prebuilt",
  31. "truth-prebuilt",
  32. // TODO(ccross): this is not needed at link time
  33. "junitxml",
  34. }
  35. const robolectricCurrentLib = "Robolectric_all-target"
  36. const robolectricPrebuiltLibPattern = "platform-robolectric-%s-prebuilt"
  37. var (
  38. roboCoverageLibsTag = dependencyTag{name: "roboCoverageLibs"}
  39. roboRuntimesTag = dependencyTag{name: "roboRuntimes"}
  40. )
  41. type robolectricProperties struct {
  42. // The name of the android_app module that the tests will run against.
  43. Instrumentation_for *string
  44. // Additional libraries for which coverage data should be generated
  45. Coverage_libs []string
  46. Test_options struct {
  47. // Timeout in seconds when running the tests.
  48. Timeout *int64
  49. // Number of shards to use when running the tests.
  50. Shards *int64
  51. }
  52. // The version number of a robolectric prebuilt to use from prebuilts/misc/common/robolectric
  53. // instead of the one built from source in external/robolectric-shadows.
  54. Robolectric_prebuilt_version *string
  55. // Use /external/robolectric rather than /external/robolectric-shadows as the version of robolectri
  56. // to use. /external/robolectric closely tracks github's master, and will fully replace /external/robolectric-shadows
  57. Upstream *bool
  58. }
  59. type robolectricTest struct {
  60. Library
  61. robolectricProperties robolectricProperties
  62. testProperties testProperties
  63. libs []string
  64. tests []string
  65. manifest android.Path
  66. resourceApk android.Path
  67. combinedJar android.WritablePath
  68. roboSrcJar android.Path
  69. testConfig android.Path
  70. data android.Paths
  71. forceOSType android.OsType
  72. forceArchType android.ArchType
  73. }
  74. func (r *robolectricTest) TestSuites() []string {
  75. return r.testProperties.Test_suites
  76. }
  77. var _ android.TestSuiteModule = (*robolectricTest)(nil)
  78. func (r *robolectricTest) DepsMutator(ctx android.BottomUpMutatorContext) {
  79. r.Library.DepsMutator(ctx)
  80. if r.robolectricProperties.Instrumentation_for != nil {
  81. ctx.AddVariationDependencies(nil, instrumentationForTag, String(r.robolectricProperties.Instrumentation_for))
  82. } else {
  83. ctx.PropertyErrorf("instrumentation_for", "missing required instrumented module")
  84. }
  85. if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
  86. ctx.AddVariationDependencies(nil, libTag, fmt.Sprintf(robolectricPrebuiltLibPattern, v))
  87. } else {
  88. if proptools.Bool(r.robolectricProperties.Upstream) {
  89. ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib+"_upstream")
  90. } else {
  91. ctx.AddVariationDependencies(nil, libTag, robolectricCurrentLib)
  92. }
  93. }
  94. ctx.AddVariationDependencies(nil, libTag, robolectricDefaultLibs...)
  95. ctx.AddVariationDependencies(nil, roboCoverageLibsTag, r.robolectricProperties.Coverage_libs...)
  96. ctx.AddFarVariationDependencies(ctx.Config().BuildOSCommonTarget.Variations(),
  97. roboRuntimesTag, "robolectric-android-all-prebuilts")
  98. }
  99. func (r *robolectricTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  100. r.forceOSType = ctx.Config().BuildOS
  101. r.forceArchType = ctx.Config().BuildArch
  102. r.testConfig = tradefed.AutoGenRobolectricTestConfig(ctx, r.testProperties.Test_config,
  103. r.testProperties.Test_config_template, r.testProperties.Test_suites,
  104. r.testProperties.Auto_gen_config)
  105. r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data)
  106. roboTestConfig := android.PathForModuleGen(ctx, "robolectric").
  107. Join(ctx, "com/android/tools/test_config.properties")
  108. // TODO: this inserts paths to built files into the test, it should really be inserting the contents.
  109. instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag)
  110. if len(instrumented) != 1 {
  111. panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented)))
  112. }
  113. instrumentedApp, ok := instrumented[0].(*AndroidApp)
  114. if !ok {
  115. ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app")
  116. }
  117. r.manifest = instrumentedApp.mergedManifestFile
  118. r.resourceApk = instrumentedApp.outputFile
  119. generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp)
  120. r.extraResources = android.Paths{roboTestConfig}
  121. r.Library.GenerateAndroidBuildActions(ctx)
  122. roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar")
  123. r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp)
  124. r.roboSrcJar = roboSrcJar
  125. roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar")
  126. generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar)
  127. combinedJarJars := android.Paths{
  128. // roboTestConfigJar comes first so that its com/android/tools/test_config.properties
  129. // overrides the one from r.extraResources. The r.extraResources one can be removed
  130. // once the Make test runner is removed.
  131. roboTestConfigJar,
  132. r.outputFile,
  133. instrumentedApp.implementationAndResourcesJar,
  134. }
  135. handleLibDeps := func(dep android.Module) {
  136. m := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
  137. r.libs = append(r.libs, ctx.OtherModuleName(dep))
  138. if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) {
  139. combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars...)
  140. }
  141. }
  142. for _, dep := range ctx.GetDirectDepsWithTag(libTag) {
  143. handleLibDeps(dep)
  144. }
  145. for _, dep := range ctx.GetDirectDepsWithTag(sdkLibTag) {
  146. handleLibDeps(dep)
  147. }
  148. r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base())
  149. TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{},
  150. false, nil, nil)
  151. // TODO: this could all be removed if tradefed was used as the test runner, it will find everything
  152. // annotated as a test and run it.
  153. for _, src := range r.uniqueSrcFiles {
  154. s := src.Rel()
  155. if !strings.HasSuffix(s, "Test.java") && !strings.HasSuffix(s, "Test.kt") {
  156. continue
  157. } else if strings.HasSuffix(s, "/BaseRobolectricTest.java") {
  158. continue
  159. } else {
  160. s = strings.TrimPrefix(s, "src/")
  161. }
  162. r.tests = append(r.tests, s)
  163. }
  164. r.data = append(r.data, r.manifest, r.resourceApk)
  165. runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag)
  166. installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
  167. installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk)
  168. installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest)
  169. installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
  170. var installDeps android.Paths
  171. for _, runtime := range runtimes.(*robolectricRuntimes).runtimes {
  172. installDeps = append(installDeps, runtime)
  173. }
  174. installDeps = append(installDeps, installedResourceApk, installedManifest, installedConfig)
  175. for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) {
  176. installedData := ctx.InstallFile(installPath, data.Rel(), data)
  177. installDeps = append(installDeps, installedData)
  178. }
  179. r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...)
  180. }
  181. func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath,
  182. instrumentedApp *AndroidApp) {
  183. rule := android.NewRuleBuilder(pctx, ctx)
  184. manifest := instrumentedApp.mergedManifestFile
  185. resourceApk := instrumentedApp.outputFile
  186. rule.Command().Text("rm -f").Output(outputFile)
  187. rule.Command().
  188. Textf(`echo "android_merged_manifest=%s" >>`, manifest.String()).Output(outputFile).Text("&&").
  189. Textf(`echo "android_resource_apk=%s" >>`, resourceApk.String()).Output(outputFile).
  190. // Make it depend on the files to which it points so the test file's timestamp is updated whenever the
  191. // contents change
  192. Implicit(manifest).
  193. Implicit(resourceApk)
  194. rule.Build("generate_test_config", "generate test_config.properties")
  195. }
  196. func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) {
  197. rule := android.NewRuleBuilder(pctx, ctx)
  198. outputDir := outputFile.InSameDir(ctx)
  199. configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties")
  200. rule.Temporary(configFile)
  201. rule.Command().Text("rm -f").Output(outputFile).Output(configFile)
  202. rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String())
  203. rule.Command().
  204. Text("(").
  205. Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()).
  206. Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()).
  207. Text(") >>").Output(configFile)
  208. rule.Command().
  209. BuiltTool("soong_zip").
  210. FlagWithArg("-C ", outputDir.String()).
  211. FlagWithInput("-f ", configFile).
  212. FlagWithOutput("-o ", outputFile)
  213. rule.Build("generate_test_config_samedir", "generate test_config.properties")
  214. }
  215. func (r *robolectricTest) generateRoboSrcJar(ctx android.ModuleContext, outputFile android.WritablePath,
  216. instrumentedApp *AndroidApp) {
  217. srcJarArgs := copyOf(instrumentedApp.srcJarArgs)
  218. srcJarDeps := append(android.Paths(nil), instrumentedApp.srcJarDeps...)
  219. for _, m := range ctx.GetDirectDepsWithTag(roboCoverageLibsTag) {
  220. if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
  221. dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
  222. srcJarArgs = append(srcJarArgs, dep.SrcJarArgs...)
  223. srcJarDeps = append(srcJarDeps, dep.SrcJarDeps...)
  224. }
  225. }
  226. TransformResourcesToJar(ctx, outputFile, srcJarArgs, srcJarDeps)
  227. }
  228. func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries {
  229. entriesList := r.Library.AndroidMkEntries()
  230. entries := &entriesList[0]
  231. entries.ExtraEntries = append(entries.ExtraEntries,
  232. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  233. entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
  234. entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", "robolectric-tests")
  235. })
  236. entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
  237. func(w io.Writer, name, prefix, moduleDir string) {
  238. if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 {
  239. numShards := int(*s)
  240. shardSize := (len(r.tests) + numShards - 1) / numShards
  241. shards := android.ShardStrings(r.tests, shardSize)
  242. for i, shard := range shards {
  243. r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard)
  244. }
  245. // TODO: add rules to dist the outputs of the individual tests, or combine them together?
  246. fmt.Fprintln(w, "")
  247. fmt.Fprintln(w, ".PHONY:", "Run"+name)
  248. fmt.Fprintln(w, "Run"+name, ": \\")
  249. for i := range shards {
  250. fmt.Fprintln(w, " ", "Run"+name+strconv.Itoa(i), "\\")
  251. }
  252. fmt.Fprintln(w, "")
  253. } else {
  254. r.writeTestRunner(w, name, "Run"+name, r.tests)
  255. }
  256. },
  257. }
  258. return entriesList
  259. }
  260. func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) {
  261. fmt.Fprintln(w, "")
  262. fmt.Fprintln(w, "include $(CLEAR_VARS)", " # java.robolectricTest")
  263. fmt.Fprintln(w, "LOCAL_MODULE :=", name)
  264. android.AndroidMkEmitAssignList(w, "LOCAL_JAVA_LIBRARIES", []string{module}, r.libs)
  265. fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for))
  266. fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String())
  267. android.AndroidMkEmitAssignList(w, "LOCAL_ROBOTEST_FILES", tests)
  268. if t := r.robolectricProperties.Test_options.Timeout; t != nil {
  269. fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t)
  270. }
  271. if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
  272. fmt.Fprintf(w, "-include prebuilts/misc/common/robolectric/%s/run_robotests.mk\n", v)
  273. } else {
  274. fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk")
  275. }
  276. }
  277. // An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
  278. // instead of on a device. It also generates a rule with the name of the module prefixed with "Run" that can be
  279. // used to run the tests. Running the tests with build rule will eventually be deprecated and replaced with atest.
  280. //
  281. // The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless
  282. // it is named BaseRobolectricTest.java. The path to the each source file must exactly match the package
  283. // name, or match the package name when the prefix "src/" is removed.
  284. func RobolectricTestFactory() android.Module {
  285. module := &robolectricTest{}
  286. module.addHostProperties()
  287. module.AddProperties(
  288. &module.Module.deviceProperties,
  289. &module.robolectricProperties,
  290. &module.testProperties)
  291. module.Module.dexpreopter.isTest = true
  292. module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true)
  293. module.testProperties.Test_suites = []string{"robolectric-tests"}
  294. InitJavaModule(module, android.DeviceSupported)
  295. return module
  296. }
  297. func (r *robolectricTest) InstallInTestcases() bool { return true }
  298. func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) {
  299. return &r.forceOSType, &r.forceArchType
  300. }
  301. func robolectricRuntimesFactory() android.Module {
  302. module := &robolectricRuntimes{}
  303. module.AddProperties(&module.props)
  304. android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon)
  305. return module
  306. }
  307. type robolectricRuntimesProperties struct {
  308. Jars []string `android:"path"`
  309. Lib *string
  310. }
  311. type robolectricRuntimes struct {
  312. android.ModuleBase
  313. props robolectricRuntimesProperties
  314. runtimes []android.InstallPath
  315. forceOSType android.OsType
  316. forceArchType android.ArchType
  317. }
  318. func (r *robolectricRuntimes) TestSuites() []string {
  319. return []string{"robolectric-tests"}
  320. }
  321. var _ android.TestSuiteModule = (*robolectricRuntimes)(nil)
  322. func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) {
  323. if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
  324. ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib))
  325. }
  326. }
  327. func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  328. if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os {
  329. return
  330. }
  331. r.forceOSType = ctx.Config().BuildOS
  332. r.forceArchType = ctx.Config().BuildArch
  333. files := android.PathsForModuleSrc(ctx, r.props.Jars)
  334. androidAllDir := android.PathForModuleInstall(ctx, "android-all")
  335. for _, from := range files {
  336. installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from)
  337. r.runtimes = append(r.runtimes, installedRuntime)
  338. }
  339. if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
  340. runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag)
  341. if runtimeFromSourceModule == nil {
  342. if ctx.Config().AllowMissingDependencies() {
  343. ctx.AddMissingDependencies([]string{String(r.props.Lib)})
  344. } else {
  345. ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib))
  346. }
  347. return
  348. }
  349. runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
  350. // "TREE" name is essential here because it hooks into the "TREE" name in
  351. // Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK
  352. // in Robolectric configs.
  353. runtimeName := "android-all-current-robolectric-r0.jar"
  354. installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
  355. r.runtimes = append(r.runtimes, installedRuntime)
  356. }
  357. }
  358. func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
  359. func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) {
  360. return &r.forceOSType, &r.forceArchType
  361. }