robolectric.go 16 KB

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