robolectric.go 15 KB

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