androidmk_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. "reflect"
  17. "strings"
  18. "testing"
  19. "android/soong/android"
  20. )
  21. func TestRequired(t *testing.T) {
  22. ctx, config := testJava(t, `
  23. java_library {
  24. name: "foo",
  25. srcs: ["a.java"],
  26. required: ["libfoo"],
  27. }
  28. `)
  29. mod := ctx.ModuleForTests("foo", "android_common").Module()
  30. entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
  31. expected := []string{"libfoo"}
  32. actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
  33. if !reflect.DeepEqual(expected, actual) {
  34. t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
  35. }
  36. }
  37. func TestHostdex(t *testing.T) {
  38. ctx, config := testJava(t, `
  39. java_library {
  40. name: "foo",
  41. srcs: ["a.java"],
  42. hostdex: true,
  43. }
  44. `)
  45. mod := ctx.ModuleForTests("foo", "android_common").Module()
  46. entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
  47. if len(entriesList) != 2 {
  48. t.Errorf("two entries are expected, but got %d", len(entriesList))
  49. }
  50. mainEntries := &entriesList[0]
  51. expected := []string{"foo"}
  52. actual := mainEntries.EntryMap["LOCAL_MODULE"]
  53. if !reflect.DeepEqual(expected, actual) {
  54. t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
  55. }
  56. subEntries := &entriesList[1]
  57. expected = []string{"foo-hostdex"}
  58. actual = subEntries.EntryMap["LOCAL_MODULE"]
  59. if !reflect.DeepEqual(expected, actual) {
  60. t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
  61. }
  62. }
  63. func TestHostdexRequired(t *testing.T) {
  64. ctx, config := testJava(t, `
  65. java_library {
  66. name: "foo",
  67. srcs: ["a.java"],
  68. hostdex: true,
  69. required: ["libfoo"],
  70. }
  71. `)
  72. mod := ctx.ModuleForTests("foo", "android_common").Module()
  73. entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
  74. if len(entriesList) != 2 {
  75. t.Errorf("two entries are expected, but got %d", len(entriesList))
  76. }
  77. mainEntries := &entriesList[0]
  78. expected := []string{"libfoo"}
  79. actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
  80. if !reflect.DeepEqual(expected, actual) {
  81. t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
  82. }
  83. subEntries := &entriesList[1]
  84. expected = []string{"libfoo"}
  85. actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
  86. if !reflect.DeepEqual(expected, actual) {
  87. t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
  88. }
  89. }
  90. func TestHostdexSpecificRequired(t *testing.T) {
  91. ctx, config := testJava(t, `
  92. java_library {
  93. name: "foo",
  94. srcs: ["a.java"],
  95. hostdex: true,
  96. target: {
  97. hostdex: {
  98. required: ["libfoo"],
  99. },
  100. },
  101. }
  102. `)
  103. mod := ctx.ModuleForTests("foo", "android_common").Module()
  104. entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
  105. if len(entriesList) != 2 {
  106. t.Errorf("two entries are expected, but got %d", len(entriesList))
  107. }
  108. mainEntries := &entriesList[0]
  109. if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
  110. t.Errorf("Unexpected required modules: %q", r)
  111. }
  112. subEntries := &entriesList[1]
  113. expected := []string{"libfoo"}
  114. actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
  115. if !reflect.DeepEqual(expected, actual) {
  116. t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
  117. }
  118. }
  119. func TestDistWithTag(t *testing.T) {
  120. ctx, config := testJava(t, `
  121. java_library {
  122. name: "foo_without_tag",
  123. srcs: ["a.java"],
  124. compile_dex: true,
  125. dist: {
  126. targets: ["hi"],
  127. },
  128. }
  129. java_library {
  130. name: "foo_with_tag",
  131. srcs: ["a.java"],
  132. compile_dex: true,
  133. dist: {
  134. targets: ["hi"],
  135. tag: ".jar",
  136. },
  137. }
  138. `)
  139. withoutTagEntries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_without_tag", "android_common").Module())
  140. withTagEntries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_with_tag", "android_common").Module())
  141. if len(withoutTagEntries) != 2 || len(withTagEntries) != 2 {
  142. t.Errorf("two mk entries per module expected, got %d and %d", len(withoutTagEntries), len(withTagEntries))
  143. }
  144. if len(withTagEntries[0].DistFiles[".jar"]) != 1 ||
  145. !strings.Contains(withTagEntries[0].DistFiles[".jar"][0].String(), "/javac/foo_with_tag.jar") {
  146. t.Errorf("expected DistFiles to contain classes.jar, got %v", withTagEntries[0].DistFiles)
  147. }
  148. if len(withoutTagEntries[0].DistFiles[".jar"]) > 0 {
  149. t.Errorf("did not expect explicit DistFile for .jar tag, got %v", withoutTagEntries[0].DistFiles[".jar"])
  150. }
  151. }
  152. func TestDistWithDest(t *testing.T) {
  153. ctx, config := testJava(t, `
  154. java_library {
  155. name: "foo",
  156. srcs: ["a.java"],
  157. compile_dex: true,
  158. dist: {
  159. targets: ["my_goal"],
  160. dest: "my/custom/dest/dir",
  161. },
  162. }
  163. `)
  164. module := ctx.ModuleForTests("foo", "android_common").Module()
  165. entries := android.AndroidMkEntriesForTest(t, config, "", module)
  166. if len(entries) != 2 {
  167. t.Errorf("Expected 2 AndroidMk entries, got %d", len(entries))
  168. }
  169. distStrings := entries[0].GetDistForGoals(module)
  170. if len(distStrings) != 2 {
  171. t.Errorf("Expected 2 entries for dist: PHONY and dist-for-goals, but got %q", distStrings)
  172. }
  173. if distStrings[0] != ".PHONY: my_goal\n" {
  174. t.Errorf("Expected .PHONY entry to declare my_goal, but got: %s", distStrings[0])
  175. }
  176. if !strings.Contains(distStrings[1], "$(call dist-for-goals,my_goal") ||
  177. !strings.Contains(distStrings[1], ".intermediates/foo/android_common/dex/foo.jar:my/custom/dest/dir") {
  178. t.Errorf(
  179. "Expected dist-for-goals entry to contain my_goal and new dest dir, but got: %s", distStrings[1])
  180. }
  181. }
  182. func TestDistsWithAllProperties(t *testing.T) {
  183. ctx, config := testJava(t, `
  184. java_library {
  185. name: "foo",
  186. srcs: ["a.java"],
  187. compile_dex: true,
  188. dist: {
  189. targets: ["baz"],
  190. },
  191. dists: [
  192. {
  193. targets: ["bar"],
  194. tag: ".jar",
  195. dest: "bar.jar",
  196. dir: "bar/dir",
  197. suffix: ".qux",
  198. },
  199. ]
  200. }
  201. `)
  202. module := ctx.ModuleForTests("foo", "android_common").Module()
  203. entries := android.AndroidMkEntriesForTest(t, config, "", module)
  204. if len(entries) != 2 {
  205. t.Errorf("Expected 2 AndroidMk entries, got %d", len(entries))
  206. }
  207. distStrings := entries[0].GetDistForGoals(module)
  208. if len(distStrings) != 4 {
  209. t.Errorf("Expected 4 entries for dist: PHONY and dist-for-goals, but got %d", len(distStrings))
  210. }
  211. if distStrings[0] != ".PHONY: bar\n" {
  212. t.Errorf("Expected .PHONY entry to declare bar, but got: %s", distStrings[0])
  213. }
  214. if !strings.Contains(distStrings[1], "$(call dist-for-goals,bar") ||
  215. !strings.Contains(
  216. distStrings[1],
  217. ".intermediates/foo/android_common/javac/foo.jar:bar/dir/bar.qux.jar") {
  218. t.Errorf(
  219. "Expected dist-for-goals entry to contain bar and new dest dir, but got: %s", distStrings[1])
  220. }
  221. if distStrings[2] != ".PHONY: baz\n" {
  222. t.Errorf("Expected .PHONY entry to declare baz, but got: %s", distStrings[2])
  223. }
  224. if !strings.Contains(distStrings[3], "$(call dist-for-goals,baz") ||
  225. !strings.Contains(distStrings[3], ".intermediates/foo/android_common/dex/foo.jar:foo.jar") {
  226. t.Errorf(
  227. "Expected dist-for-goals entry to contain my_other_goal and new dest dir, but got: %s",
  228. distStrings[3])
  229. }
  230. }
  231. func TestDistsWithTag(t *testing.T) {
  232. ctx, config := testJava(t, `
  233. java_library {
  234. name: "foo_without_tag",
  235. srcs: ["a.java"],
  236. compile_dex: true,
  237. dists: [
  238. {
  239. targets: ["hi"],
  240. },
  241. ],
  242. }
  243. java_library {
  244. name: "foo_with_tag",
  245. srcs: ["a.java"],
  246. compile_dex: true,
  247. dists: [
  248. {
  249. targets: ["hi"],
  250. tag: ".jar",
  251. },
  252. ],
  253. }
  254. `)
  255. moduleWithoutTag := ctx.ModuleForTests("foo_without_tag", "android_common").Module()
  256. moduleWithTag := ctx.ModuleForTests("foo_with_tag", "android_common").Module()
  257. withoutTagEntries := android.AndroidMkEntriesForTest(t, config, "", moduleWithoutTag)
  258. withTagEntries := android.AndroidMkEntriesForTest(t, config, "", moduleWithTag)
  259. if len(withoutTagEntries) != 2 || len(withTagEntries) != 2 {
  260. t.Errorf("two mk entries per module expected, got %d and %d", len(withoutTagEntries), len(withTagEntries))
  261. }
  262. distFilesWithoutTag := withoutTagEntries[0].DistFiles
  263. distFilesWithTag := withTagEntries[0].DistFiles
  264. if len(distFilesWithTag[".jar"]) != 1 ||
  265. !strings.Contains(distFilesWithTag[".jar"][0].String(), "/javac/foo_with_tag.jar") {
  266. t.Errorf("expected foo_with_tag's .jar-tagged DistFiles to contain classes.jar, got %v", distFilesWithTag[".jar"])
  267. }
  268. if len(distFilesWithoutTag[".jar"]) > 0 {
  269. t.Errorf("did not expect foo_without_tag's .jar-tagged DistFiles to contain files, but got %v", distFilesWithoutTag[".jar"])
  270. }
  271. }
  272. func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
  273. ctx, config := testJava(t, `
  274. java_sdk_library {
  275. name: "foo-shared_library",
  276. srcs: ["a.java"],
  277. }
  278. java_sdk_library {
  279. name: "foo-no_shared_library",
  280. srcs: ["a.java"],
  281. shared_library: false,
  282. }
  283. `)
  284. // Verify the existence of internal modules
  285. ctx.ModuleForTests("foo-shared_library.xml", "android_common")
  286. testCases := []struct {
  287. moduleName string
  288. expected []string
  289. }{
  290. {"foo-shared_library", []string{"foo-shared_library.xml"}},
  291. {"foo-no_shared_library", nil},
  292. }
  293. for _, tc := range testCases {
  294. mod := ctx.ModuleForTests(tc.moduleName, "android_common").Module()
  295. entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
  296. actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
  297. if !reflect.DeepEqual(tc.expected, actual) {
  298. t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
  299. }
  300. }
  301. }