dexpreopt_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2018 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. "runtime"
  18. "testing"
  19. "android/soong/android"
  20. "android/soong/cc"
  21. "android/soong/dexpreopt"
  22. )
  23. func TestDexpreoptEnabled(t *testing.T) {
  24. tests := []struct {
  25. name string
  26. bp string
  27. enabled bool
  28. }{
  29. {
  30. name: "app",
  31. bp: `
  32. android_app {
  33. name: "foo",
  34. srcs: ["a.java"],
  35. sdk_version: "current",
  36. }`,
  37. enabled: true,
  38. },
  39. {
  40. name: "installable java library",
  41. bp: `
  42. java_library {
  43. name: "foo",
  44. installable: true,
  45. srcs: ["a.java"],
  46. }`,
  47. enabled: true,
  48. },
  49. {
  50. name: "java binary",
  51. bp: `
  52. java_binary {
  53. name: "foo",
  54. srcs: ["a.java"],
  55. }`,
  56. enabled: true,
  57. },
  58. {
  59. name: "app without sources",
  60. bp: `
  61. android_app {
  62. name: "foo",
  63. sdk_version: "current",
  64. }`,
  65. enabled: false,
  66. },
  67. {
  68. name: "app with libraries",
  69. bp: `
  70. android_app {
  71. name: "foo",
  72. static_libs: ["lib"],
  73. sdk_version: "current",
  74. }
  75. java_library {
  76. name: "lib",
  77. srcs: ["a.java"],
  78. sdk_version: "current",
  79. }`,
  80. enabled: true,
  81. },
  82. {
  83. name: "installable java library without sources",
  84. bp: `
  85. java_library {
  86. name: "foo",
  87. installable: true,
  88. }`,
  89. enabled: false,
  90. },
  91. {
  92. name: "static java library",
  93. bp: `
  94. java_library {
  95. name: "foo",
  96. srcs: ["a.java"],
  97. }`,
  98. enabled: false,
  99. },
  100. {
  101. name: "java test",
  102. bp: `
  103. java_test {
  104. name: "foo",
  105. srcs: ["a.java"],
  106. }`,
  107. enabled: false,
  108. },
  109. {
  110. name: "android test",
  111. bp: `
  112. android_test {
  113. name: "foo",
  114. srcs: ["a.java"],
  115. }`,
  116. enabled: false,
  117. },
  118. {
  119. name: "android test helper app",
  120. bp: `
  121. android_test_helper_app {
  122. name: "foo",
  123. srcs: ["a.java"],
  124. }`,
  125. enabled: false,
  126. },
  127. {
  128. name: "compile_dex",
  129. bp: `
  130. java_library {
  131. name: "foo",
  132. srcs: ["a.java"],
  133. compile_dex: true,
  134. }`,
  135. enabled: false,
  136. },
  137. {
  138. name: "dex_import",
  139. bp: `
  140. dex_import {
  141. name: "foo",
  142. jars: ["a.jar"],
  143. }`,
  144. enabled: true,
  145. },
  146. }
  147. for _, test := range tests {
  148. t.Run(test.name, func(t *testing.T) {
  149. ctx, _ := testJava(t, test.bp)
  150. dexpreopt := ctx.ModuleForTests("foo", "android_common").MaybeRule("dexpreopt")
  151. enabled := dexpreopt.Rule != nil
  152. if enabled != test.enabled {
  153. t.Fatalf("want dexpreopt %s, got %s", enabledString(test.enabled), enabledString(enabled))
  154. }
  155. })
  156. }
  157. }
  158. func enabledString(enabled bool) string {
  159. if enabled {
  160. return "enabled"
  161. } else {
  162. return "disabled"
  163. }
  164. }
  165. func TestDex2oatToolDeps(t *testing.T) {
  166. if runtime.GOOS != "linux" {
  167. // The host binary paths checked below are build OS dependent.
  168. t.Skipf("Unsupported build OS %s", runtime.GOOS)
  169. }
  170. preparers := android.GroupFixturePreparers(
  171. cc.PrepareForTestWithCcDefaultModules,
  172. PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd,
  173. dexpreopt.PrepareForTestByEnablingDexpreopt)
  174. testDex2oatToolDep := func(sourceEnabled, prebuiltEnabled, prebuiltPreferred bool,
  175. expectedDex2oatPath string) {
  176. name := fmt.Sprintf("sourceEnabled:%t,prebuiltEnabled:%t,prebuiltPreferred:%t",
  177. sourceEnabled, prebuiltEnabled, prebuiltPreferred)
  178. t.Run(name, func(t *testing.T) {
  179. result := preparers.RunTestWithBp(t, fmt.Sprintf(`
  180. cc_binary {
  181. name: "dex2oatd",
  182. enabled: %t,
  183. host_supported: true,
  184. }
  185. cc_prebuilt_binary {
  186. name: "dex2oatd",
  187. enabled: %t,
  188. prefer: %t,
  189. host_supported: true,
  190. srcs: ["x86_64/bin/dex2oatd"],
  191. }
  192. java_library {
  193. name: "myjavalib",
  194. }
  195. `, sourceEnabled, prebuiltEnabled, prebuiltPreferred))
  196. pathContext := android.PathContextForTesting(result.Config)
  197. dex2oatPath := dexpreopt.GetCachedGlobalSoongConfig(pathContext).Dex2oat
  198. android.AssertStringEquals(t, "Testing "+name, expectedDex2oatPath, android.NormalizePathForTesting(dex2oatPath))
  199. })
  200. }
  201. sourceDex2oatPath := "host/linux-x86/bin/dex2oatd"
  202. prebuiltDex2oatPath := ".intermediates/prebuilt_dex2oatd/linux_glibc_x86_64/dex2oatd"
  203. testDex2oatToolDep(true, false, false, sourceDex2oatPath)
  204. testDex2oatToolDep(true, true, false, sourceDex2oatPath)
  205. testDex2oatToolDep(true, true, true, prebuiltDex2oatPath)
  206. testDex2oatToolDep(false, true, false, prebuiltDex2oatPath)
  207. }