robolectric.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
  103. TestConfigProp: r.testProperties.Test_config,
  104. TestConfigTemplateProp: r.testProperties.Test_config_template,
  105. TestSuites: r.testProperties.Test_suites,
  106. AutoGenConfig: r.testProperties.Auto_gen_config,
  107. DeviceTemplate: "${RobolectricTestConfigTemplate}",
  108. HostTemplate: "${RobolectricTestConfigTemplate}",
  109. })
  110. r.data = android.PathsForModuleSrc(ctx, r.testProperties.Data)
  111. roboTestConfig := android.PathForModuleGen(ctx, "robolectric").
  112. Join(ctx, "com/android/tools/test_config.properties")
  113. var ok bool
  114. var instrumentedApp *AndroidApp
  115. // TODO: this inserts paths to built files into the test, it should really be inserting the contents.
  116. instrumented := ctx.GetDirectDepsWithTag(instrumentationForTag)
  117. if len(instrumented) == 1 {
  118. instrumentedApp, ok = instrumented[0].(*AndroidApp)
  119. if !ok {
  120. ctx.PropertyErrorf("instrumentation_for", "dependency must be an android_app")
  121. }
  122. } else if !ctx.Config().AllowMissingDependencies() {
  123. panic(fmt.Errorf("expected exactly 1 instrumented dependency, got %d", len(instrumented)))
  124. }
  125. if instrumentedApp != nil {
  126. r.manifest = instrumentedApp.mergedManifestFile
  127. r.resourceApk = instrumentedApp.outputFile
  128. generateRoboTestConfig(ctx, roboTestConfig, instrumentedApp)
  129. r.extraResources = android.Paths{roboTestConfig}
  130. }
  131. r.Library.GenerateAndroidBuildActions(ctx)
  132. roboSrcJar := android.PathForModuleGen(ctx, "robolectric", ctx.ModuleName()+".srcjar")
  133. if instrumentedApp != nil {
  134. r.generateRoboSrcJar(ctx, roboSrcJar, instrumentedApp)
  135. r.roboSrcJar = roboSrcJar
  136. }
  137. roboTestConfigJar := android.PathForModuleOut(ctx, "robolectric_samedir", "samedir_config.jar")
  138. generateSameDirRoboTestConfigJar(ctx, roboTestConfigJar)
  139. combinedJarJars := android.Paths{
  140. // roboTestConfigJar comes first so that its com/android/tools/test_config.properties
  141. // overrides the one from r.extraResources. The r.extraResources one can be removed
  142. // once the Make test runner is removed.
  143. roboTestConfigJar,
  144. r.outputFile,
  145. }
  146. if instrumentedApp != nil {
  147. combinedJarJars = append(combinedJarJars, instrumentedApp.implementationAndResourcesJar)
  148. }
  149. handleLibDeps := func(dep android.Module) {
  150. m := ctx.OtherModuleProvider(dep, JavaInfoProvider).(JavaInfo)
  151. r.libs = append(r.libs, ctx.OtherModuleName(dep))
  152. if !android.InList(ctx.OtherModuleName(dep), config.FrameworkLibraries) {
  153. combinedJarJars = append(combinedJarJars, m.ImplementationAndResourcesJars...)
  154. }
  155. }
  156. for _, dep := range ctx.GetDirectDepsWithTag(libTag) {
  157. handleLibDeps(dep)
  158. }
  159. for _, dep := range ctx.GetDirectDepsWithTag(sdkLibTag) {
  160. handleLibDeps(dep)
  161. }
  162. r.combinedJar = android.PathForModuleOut(ctx, "robolectric_combined", r.outputFile.Base())
  163. TransformJarsToJar(ctx, r.combinedJar, "combine jars", combinedJarJars, android.OptionalPath{},
  164. false, nil, nil)
  165. // TODO: this could all be removed if tradefed was used as the test runner, it will find everything
  166. // annotated as a test and run it.
  167. for _, src := range r.uniqueSrcFiles {
  168. s := src.Rel()
  169. if !strings.HasSuffix(s, "Test.java") && !strings.HasSuffix(s, "Test.kt") {
  170. continue
  171. } else if strings.HasSuffix(s, "/BaseRobolectricTest.java") {
  172. continue
  173. } else {
  174. s = strings.TrimPrefix(s, "src/")
  175. }
  176. r.tests = append(r.tests, s)
  177. }
  178. installPath := android.PathForModuleInstall(ctx, r.BaseModuleName())
  179. var installDeps android.Paths
  180. if r.manifest != nil {
  181. r.data = append(r.data, r.manifest)
  182. installedManifest := ctx.InstallFile(installPath, ctx.ModuleName()+"-AndroidManifest.xml", r.manifest)
  183. installDeps = append(installDeps, installedManifest)
  184. }
  185. if r.resourceApk != nil {
  186. r.data = append(r.data, r.resourceApk)
  187. installedResourceApk := ctx.InstallFile(installPath, ctx.ModuleName()+".apk", r.resourceApk)
  188. installDeps = append(installDeps, installedResourceApk)
  189. }
  190. runtimes := ctx.GetDirectDepWithTag("robolectric-android-all-prebuilts", roboRuntimesTag)
  191. for _, runtime := range runtimes.(*robolectricRuntimes).runtimes {
  192. installDeps = append(installDeps, runtime)
  193. }
  194. installedConfig := ctx.InstallFile(installPath, ctx.ModuleName()+".config", r.testConfig)
  195. installDeps = append(installDeps, installedConfig)
  196. for _, data := range android.PathsForModuleSrc(ctx, r.testProperties.Data) {
  197. installedData := ctx.InstallFile(installPath, data.Rel(), data)
  198. installDeps = append(installDeps, installedData)
  199. }
  200. r.installFile = ctx.InstallFile(installPath, ctx.ModuleName()+".jar", r.combinedJar, installDeps...)
  201. }
  202. func generateRoboTestConfig(ctx android.ModuleContext, outputFile android.WritablePath,
  203. instrumentedApp *AndroidApp) {
  204. rule := android.NewRuleBuilder(pctx, ctx)
  205. manifest := instrumentedApp.mergedManifestFile
  206. resourceApk := instrumentedApp.outputFile
  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. rule.Build("generate_test_config", "generate test_config.properties")
  216. }
  217. func generateSameDirRoboTestConfigJar(ctx android.ModuleContext, outputFile android.ModuleOutPath) {
  218. rule := android.NewRuleBuilder(pctx, ctx)
  219. outputDir := outputFile.InSameDir(ctx)
  220. configFile := outputDir.Join(ctx, "com/android/tools/test_config.properties")
  221. rule.Temporary(configFile)
  222. rule.Command().Text("rm -f").Output(outputFile).Output(configFile)
  223. rule.Command().Textf("mkdir -p $(dirname %s)", configFile.String())
  224. rule.Command().
  225. Text("(").
  226. Textf(`echo "android_merged_manifest=%s-AndroidManifest.xml" &&`, ctx.ModuleName()).
  227. Textf(`echo "android_resource_apk=%s.apk"`, ctx.ModuleName()).
  228. Text(") >>").Output(configFile)
  229. rule.Command().
  230. BuiltTool("soong_zip").
  231. FlagWithArg("-C ", outputDir.String()).
  232. FlagWithInput("-f ", configFile).
  233. FlagWithOutput("-o ", outputFile)
  234. rule.Build("generate_test_config_samedir", "generate test_config.properties")
  235. }
  236. func (r *robolectricTest) generateRoboSrcJar(ctx android.ModuleContext, outputFile android.WritablePath,
  237. instrumentedApp *AndroidApp) {
  238. srcJarArgs := android.CopyOf(instrumentedApp.srcJarArgs)
  239. srcJarDeps := append(android.Paths(nil), instrumentedApp.srcJarDeps...)
  240. for _, m := range ctx.GetDirectDepsWithTag(roboCoverageLibsTag) {
  241. if ctx.OtherModuleHasProvider(m, JavaInfoProvider) {
  242. dep := ctx.OtherModuleProvider(m, JavaInfoProvider).(JavaInfo)
  243. srcJarArgs = append(srcJarArgs, dep.SrcJarArgs...)
  244. srcJarDeps = append(srcJarDeps, dep.SrcJarDeps...)
  245. }
  246. }
  247. TransformResourcesToJar(ctx, outputFile, srcJarArgs, srcJarDeps)
  248. }
  249. func (r *robolectricTest) AndroidMkEntries() []android.AndroidMkEntries {
  250. entriesList := r.Library.AndroidMkEntries()
  251. entries := &entriesList[0]
  252. entries.ExtraEntries = append(entries.ExtraEntries,
  253. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  254. entries.SetBool("LOCAL_UNINSTALLABLE_MODULE", true)
  255. entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", "robolectric-tests")
  256. if r.testConfig != nil {
  257. entries.SetPath("LOCAL_FULL_TEST_CONFIG", r.testConfig)
  258. }
  259. })
  260. entries.ExtraFooters = []android.AndroidMkExtraFootersFunc{
  261. func(w io.Writer, name, prefix, moduleDir string) {
  262. if s := r.robolectricProperties.Test_options.Shards; s != nil && *s > 1 {
  263. numShards := int(*s)
  264. shardSize := (len(r.tests) + numShards - 1) / numShards
  265. shards := android.ShardStrings(r.tests, shardSize)
  266. for i, shard := range shards {
  267. r.writeTestRunner(w, name, "Run"+name+strconv.Itoa(i), shard)
  268. }
  269. // TODO: add rules to dist the outputs of the individual tests, or combine them together?
  270. fmt.Fprintln(w, "")
  271. fmt.Fprintln(w, ".PHONY:", "Run"+name)
  272. fmt.Fprintln(w, "Run"+name, ": \\")
  273. for i := range shards {
  274. fmt.Fprintln(w, " ", "Run"+name+strconv.Itoa(i), "\\")
  275. }
  276. fmt.Fprintln(w, "")
  277. } else {
  278. r.writeTestRunner(w, name, "Run"+name, r.tests)
  279. }
  280. },
  281. }
  282. return entriesList
  283. }
  284. func (r *robolectricTest) writeTestRunner(w io.Writer, module, name string, tests []string) {
  285. fmt.Fprintln(w, "")
  286. fmt.Fprintln(w, "include $(CLEAR_VARS)", " # java.robolectricTest")
  287. fmt.Fprintln(w, "LOCAL_MODULE :=", name)
  288. android.AndroidMkEmitAssignList(w, "LOCAL_JAVA_LIBRARIES", []string{module}, r.libs)
  289. fmt.Fprintln(w, "LOCAL_TEST_PACKAGE :=", String(r.robolectricProperties.Instrumentation_for))
  290. if r.roboSrcJar != nil {
  291. fmt.Fprintln(w, "LOCAL_INSTRUMENT_SRCJARS :=", r.roboSrcJar.String())
  292. }
  293. android.AndroidMkEmitAssignList(w, "LOCAL_ROBOTEST_FILES", tests)
  294. if t := r.robolectricProperties.Test_options.Timeout; t != nil {
  295. fmt.Fprintln(w, "LOCAL_ROBOTEST_TIMEOUT :=", *t)
  296. }
  297. if v := String(r.robolectricProperties.Robolectric_prebuilt_version); v != "" {
  298. fmt.Fprintf(w, "-include prebuilts/misc/common/robolectric/%s/run_robotests.mk\n", v)
  299. } else {
  300. fmt.Fprintln(w, "-include external/robolectric-shadows/run_robotests.mk")
  301. }
  302. }
  303. // An android_robolectric_test module compiles tests against the Robolectric framework that can run on the local host
  304. // instead of on a device. It also generates a rule with the name of the module prefixed with "Run" that can be
  305. // used to run the tests. Running the tests with build rule will eventually be deprecated and replaced with atest.
  306. //
  307. // The test runner considers any file listed in srcs whose name ends with Test.java to be a test class, unless
  308. // it is named BaseRobolectricTest.java. The path to the each source file must exactly match the package
  309. // name, or match the package name when the prefix "src/" is removed.
  310. func RobolectricTestFactory() android.Module {
  311. module := &robolectricTest{}
  312. module.addHostProperties()
  313. module.AddProperties(
  314. &module.Module.deviceProperties,
  315. &module.robolectricProperties,
  316. &module.testProperties)
  317. module.Module.dexpreopter.isTest = true
  318. module.Module.linter.properties.Lint.Test = proptools.BoolPtr(true)
  319. module.testProperties.Test_suites = []string{"robolectric-tests"}
  320. InitJavaModule(module, android.DeviceSupported)
  321. return module
  322. }
  323. func (r *robolectricTest) InstallInTestcases() bool { return true }
  324. func (r *robolectricTest) InstallForceOS() (*android.OsType, *android.ArchType) {
  325. return &r.forceOSType, &r.forceArchType
  326. }
  327. func robolectricRuntimesFactory() android.Module {
  328. module := &robolectricRuntimes{}
  329. module.AddProperties(&module.props)
  330. android.InitAndroidArchModule(module, android.HostSupportedNoCross, android.MultilibCommon)
  331. return module
  332. }
  333. type robolectricRuntimesProperties struct {
  334. Jars []string `android:"path"`
  335. Lib *string
  336. }
  337. type robolectricRuntimes struct {
  338. android.ModuleBase
  339. props robolectricRuntimesProperties
  340. runtimes []android.InstallPath
  341. forceOSType android.OsType
  342. forceArchType android.ArchType
  343. }
  344. func (r *robolectricRuntimes) TestSuites() []string {
  345. return []string{"robolectric-tests"}
  346. }
  347. var _ android.TestSuiteModule = (*robolectricRuntimes)(nil)
  348. func (r *robolectricRuntimes) DepsMutator(ctx android.BottomUpMutatorContext) {
  349. if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
  350. ctx.AddVariationDependencies(nil, libTag, String(r.props.Lib))
  351. }
  352. }
  353. func (r *robolectricRuntimes) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  354. if ctx.Target().Os != ctx.Config().BuildOSCommonTarget.Os {
  355. return
  356. }
  357. r.forceOSType = ctx.Config().BuildOS
  358. r.forceArchType = ctx.Config().BuildArch
  359. files := android.PathsForModuleSrc(ctx, r.props.Jars)
  360. androidAllDir := android.PathForModuleInstall(ctx, "android-all")
  361. for _, from := range files {
  362. installedRuntime := ctx.InstallFile(androidAllDir, from.Base(), from)
  363. r.runtimes = append(r.runtimes, installedRuntime)
  364. }
  365. if !ctx.Config().AlwaysUsePrebuiltSdks() && r.props.Lib != nil {
  366. runtimeFromSourceModule := ctx.GetDirectDepWithTag(String(r.props.Lib), libTag)
  367. if runtimeFromSourceModule == nil {
  368. if ctx.Config().AllowMissingDependencies() {
  369. ctx.AddMissingDependencies([]string{String(r.props.Lib)})
  370. } else {
  371. ctx.PropertyErrorf("lib", "missing dependency %q", String(r.props.Lib))
  372. }
  373. return
  374. }
  375. runtimeFromSourceJar := android.OutputFileForModule(ctx, runtimeFromSourceModule, "")
  376. // "TREE" name is essential here because it hooks into the "TREE" name in
  377. // Robolectric's SdkConfig.java that will always correspond to the NEWEST_SDK
  378. // in Robolectric configs.
  379. runtimeName := "android-all-current-robolectric-r0.jar"
  380. installedRuntime := ctx.InstallFile(androidAllDir, runtimeName, runtimeFromSourceJar)
  381. r.runtimes = append(r.runtimes, installedRuntime)
  382. }
  383. }
  384. func (r *robolectricRuntimes) InstallInTestcases() bool { return true }
  385. func (r *robolectricRuntimes) InstallForceOS() (*android.OsType, *android.ArchType) {
  386. return &r.forceOSType, &r.forceArchType
  387. }