testing.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2020 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 dexpreopt
  15. import (
  16. "fmt"
  17. "android/soong/android"
  18. )
  19. type fakeToolBinary struct {
  20. android.ModuleBase
  21. }
  22. func (m *fakeToolBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
  23. func (m *fakeToolBinary) HostToolPath() android.OptionalPath {
  24. return android.OptionalPathForPath(android.PathForTesting("dex2oat"))
  25. }
  26. func fakeToolBinaryFactory() android.Module {
  27. module := &fakeToolBinary{}
  28. android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
  29. return module
  30. }
  31. func RegisterToolModulesForTest(ctx android.RegistrationContext) {
  32. ctx.RegisterModuleType("fake_tool_binary", fakeToolBinaryFactory)
  33. }
  34. func BpToolModulesForTest() string {
  35. return `
  36. fake_tool_binary {
  37. name: "dex2oatd",
  38. }
  39. `
  40. }
  41. func CompatLibDefinitionsForTest() string {
  42. bp := ""
  43. // For class loader context and <uses-library> tests.
  44. dexpreoptModules := []string{"android.test.runner"}
  45. dexpreoptModules = append(dexpreoptModules, CompatUsesLibs...)
  46. dexpreoptModules = append(dexpreoptModules, OptionalCompatUsesLibs...)
  47. for _, extra := range dexpreoptModules {
  48. bp += fmt.Sprintf(`
  49. java_library {
  50. name: "%s",
  51. srcs: ["a.java"],
  52. sdk_version: "none",
  53. system_modules: "stable-core-platform-api-stubs-system-modules",
  54. compile_dex: true,
  55. installable: true,
  56. }
  57. `, extra)
  58. }
  59. return bp
  60. }
  61. var PrepareForTestWithDexpreoptCompatLibs = android.GroupFixturePreparers(
  62. android.FixtureAddFile("defaults/dexpreopt/compat/a.java", nil),
  63. android.FixtureAddTextFile("defaults/dexpreopt/compat/Android.bp", CompatLibDefinitionsForTest()),
  64. )
  65. var PrepareForTestWithFakeDex2oatd = android.GroupFixturePreparers(
  66. android.FixtureRegisterWithContext(RegisterToolModulesForTest),
  67. android.FixtureAddTextFile("defaults/dexpreopt/Android.bp", BpToolModulesForTest()),
  68. )
  69. // Prepares a test fixture by enabling dexpreopt, registering the fake_tool_binary module type and
  70. // using that to define the `dex2oatd` module.
  71. var PrepareForTestByEnablingDexpreopt = android.GroupFixturePreparers(
  72. FixtureModifyGlobalConfig(func(android.PathContext, *GlobalConfig) {}),
  73. )
  74. // FixtureModifyGlobalConfig enables dexpreopt (unless modified by the mutator) and modifies the
  75. // configuration.
  76. func FixtureModifyGlobalConfig(configModifier func(ctx android.PathContext, dexpreoptConfig *GlobalConfig)) android.FixturePreparer {
  77. return android.FixtureModifyConfig(func(config android.Config) {
  78. // Initialize the dexpreopt GlobalConfig to an empty structure. This has no effect if it has
  79. // already been set.
  80. pathCtx := android.PathContextForTesting(config)
  81. dexpreoptConfig := GlobalConfigForTests(pathCtx)
  82. SetTestGlobalConfig(config, dexpreoptConfig)
  83. // Retrieve the existing configuration and modify it.
  84. dexpreoptConfig = GetGlobalConfig(pathCtx)
  85. configModifier(pathCtx, dexpreoptConfig)
  86. })
  87. }
  88. // FixtureSetArtBootJars enables dexpreopt and sets the ArtApexJars property.
  89. func FixtureSetArtBootJars(bootJars ...string) android.FixturePreparer {
  90. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  91. dexpreoptConfig.ArtApexJars = android.CreateTestConfiguredJarList(bootJars)
  92. })
  93. }
  94. // FixtureSetBootJars enables dexpreopt and sets the BootJars property.
  95. func FixtureSetBootJars(bootJars ...string) android.FixturePreparer {
  96. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  97. dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList(bootJars)
  98. })
  99. }
  100. // FixtureSetApexBootJars sets the ApexBootJars property in the global config.
  101. func FixtureSetApexBootJars(bootJars ...string) android.FixturePreparer {
  102. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  103. dexpreoptConfig.ApexBootJars = android.CreateTestConfiguredJarList(bootJars)
  104. })
  105. }
  106. // FixtureSetStandaloneSystemServerJars sets the StandaloneSystemServerJars property.
  107. func FixtureSetStandaloneSystemServerJars(jars ...string) android.FixturePreparer {
  108. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  109. dexpreoptConfig.StandaloneSystemServerJars = android.CreateTestConfiguredJarList(jars)
  110. })
  111. }
  112. // FixtureSetSystemServerJars sets the SystemServerJars property.
  113. func FixtureSetSystemServerJars(jars ...string) android.FixturePreparer {
  114. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  115. dexpreoptConfig.SystemServerJars = android.CreateTestConfiguredJarList(jars)
  116. })
  117. }
  118. // FixtureSetApexSystemServerJars sets the ApexSystemServerJars property in the global config.
  119. func FixtureSetApexSystemServerJars(jars ...string) android.FixturePreparer {
  120. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  121. dexpreoptConfig.ApexSystemServerJars = android.CreateTestConfiguredJarList(jars)
  122. })
  123. }
  124. // FixtureSetApexStandaloneSystemServerJars sets the ApexStandaloneSystemServerJars property in the
  125. // global config.
  126. func FixtureSetApexStandaloneSystemServerJars(jars ...string) android.FixturePreparer {
  127. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  128. dexpreoptConfig.ApexStandaloneSystemServerJars = android.CreateTestConfiguredJarList(jars)
  129. })
  130. }
  131. // FixtureSetPreoptWithUpdatableBcp sets the PreoptWithUpdatableBcp property in the global config.
  132. func FixtureSetPreoptWithUpdatableBcp(value bool) android.FixturePreparer {
  133. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  134. dexpreoptConfig.PreoptWithUpdatableBcp = value
  135. })
  136. }
  137. // FixtureSetBootImageProfiles sets the BootImageProfiles property in the global config.
  138. func FixtureSetBootImageProfiles(profiles ...string) android.FixturePreparer {
  139. return FixtureModifyGlobalConfig(func(ctx android.PathContext, dexpreoptConfig *GlobalConfig) {
  140. dexpreoptConfig.BootImageProfiles = android.PathsForSource(ctx, profiles)
  141. })
  142. }
  143. // FixtureDisableGenerateProfile sets the DisableGenerateProfile property in the global config.
  144. func FixtureDisableGenerateProfile(disable bool) android.FixturePreparer {
  145. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  146. dexpreoptConfig.DisableGenerateProfile = disable
  147. })
  148. }
  149. // FixtureDisableDexpreoptBootImages sets the DisablePreoptBootImages property in the global config.
  150. func FixtureDisableDexpreoptBootImages(disable bool) android.FixturePreparer {
  151. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  152. dexpreoptConfig.DisablePreoptBootImages = disable
  153. })
  154. }
  155. // FixtureDisableDexpreopt sets the DisablePreopt property in the global config.
  156. func FixtureDisableDexpreopt(disable bool) android.FixturePreparer {
  157. return FixtureModifyGlobalConfig(func(_ android.PathContext, dexpreoptConfig *GlobalConfig) {
  158. dexpreoptConfig.DisablePreopt = disable
  159. })
  160. }