dexpreopt_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. "testing"
  17. )
  18. func TestDexpreoptEnabled(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. bp string
  22. enabled bool
  23. }{
  24. {
  25. name: "app",
  26. bp: `
  27. android_app {
  28. name: "foo",
  29. srcs: ["a.java"],
  30. sdk_version: "current",
  31. }`,
  32. enabled: true,
  33. },
  34. {
  35. name: "installable java library",
  36. bp: `
  37. java_library {
  38. name: "foo",
  39. installable: true,
  40. srcs: ["a.java"],
  41. }`,
  42. enabled: true,
  43. },
  44. {
  45. name: "java binary",
  46. bp: `
  47. java_binary {
  48. name: "foo",
  49. srcs: ["a.java"],
  50. }`,
  51. enabled: true,
  52. },
  53. {
  54. name: "app without sources",
  55. bp: `
  56. android_app {
  57. name: "foo",
  58. sdk_version: "current",
  59. }`,
  60. enabled: false,
  61. },
  62. {
  63. name: "app with libraries",
  64. bp: `
  65. android_app {
  66. name: "foo",
  67. static_libs: ["lib"],
  68. sdk_version: "current",
  69. }
  70. java_library {
  71. name: "lib",
  72. srcs: ["a.java"],
  73. sdk_version: "current",
  74. }`,
  75. enabled: true,
  76. },
  77. {
  78. name: "installable java library without sources",
  79. bp: `
  80. java_library {
  81. name: "foo",
  82. installable: true,
  83. }`,
  84. enabled: false,
  85. },
  86. {
  87. name: "static java library",
  88. bp: `
  89. java_library {
  90. name: "foo",
  91. srcs: ["a.java"],
  92. }`,
  93. enabled: false,
  94. },
  95. {
  96. name: "java test",
  97. bp: `
  98. java_test {
  99. name: "foo",
  100. srcs: ["a.java"],
  101. }`,
  102. enabled: false,
  103. },
  104. {
  105. name: "android test",
  106. bp: `
  107. android_test {
  108. name: "foo",
  109. srcs: ["a.java"],
  110. }`,
  111. enabled: false,
  112. },
  113. {
  114. name: "android test helper app",
  115. bp: `
  116. android_test_helper_app {
  117. name: "foo",
  118. srcs: ["a.java"],
  119. }`,
  120. enabled: false,
  121. },
  122. {
  123. name: "compile_dex",
  124. bp: `
  125. java_library {
  126. name: "foo",
  127. srcs: ["a.java"],
  128. compile_dex: true,
  129. }`,
  130. enabled: false,
  131. },
  132. {
  133. name: "dex_import",
  134. bp: `
  135. dex_import {
  136. name: "foo",
  137. jars: ["a.jar"],
  138. }`,
  139. enabled: true,
  140. },
  141. }
  142. for _, test := range tests {
  143. t.Run(test.name, func(t *testing.T) {
  144. ctx, _ := testJava(t, test.bp)
  145. dexpreopt := ctx.ModuleForTests("foo", "android_common").MaybeDescription("dexpreopt")
  146. enabled := dexpreopt.Rule != nil
  147. if enabled != test.enabled {
  148. t.Fatalf("want dexpreopt %s, got %s", enabledString(test.enabled), enabledString(enabled))
  149. }
  150. })
  151. }
  152. }
  153. func enabledString(enabled bool) string {
  154. if enabled {
  155. return "enabled"
  156. } else {
  157. return "disabled"
  158. }
  159. }