androidmk_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. "testing"
  18. "android/soong/android"
  19. "android/soong/cc"
  20. "github.com/google/blueprint/proptools"
  21. )
  22. func TestRequired(t *testing.T) {
  23. ctx, _ := testJava(t, `
  24. java_library {
  25. name: "foo",
  26. srcs: ["a.java"],
  27. required: ["libfoo"],
  28. }
  29. `)
  30. mod := ctx.ModuleForTests("foo", "android_common").Module()
  31. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  32. expected := []string{"libfoo"}
  33. actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
  34. if !reflect.DeepEqual(expected, actual) {
  35. t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
  36. }
  37. }
  38. func TestHostdex(t *testing.T) {
  39. ctx, _ := testJava(t, `
  40. java_library {
  41. name: "foo",
  42. srcs: ["a.java"],
  43. hostdex: true,
  44. }
  45. `)
  46. mod := ctx.ModuleForTests("foo", "android_common").Module()
  47. entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
  48. if len(entriesList) != 2 {
  49. t.Errorf("two entries are expected, but got %d", len(entriesList))
  50. }
  51. mainEntries := &entriesList[0]
  52. expected := []string{"foo"}
  53. actual := mainEntries.EntryMap["LOCAL_MODULE"]
  54. if !reflect.DeepEqual(expected, actual) {
  55. t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
  56. }
  57. subEntries := &entriesList[1]
  58. expected = []string{"foo-hostdex"}
  59. actual = subEntries.EntryMap["LOCAL_MODULE"]
  60. if !reflect.DeepEqual(expected, actual) {
  61. t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
  62. }
  63. }
  64. func TestHostdexRequired(t *testing.T) {
  65. ctx, _ := testJava(t, `
  66. java_library {
  67. name: "foo",
  68. srcs: ["a.java"],
  69. hostdex: true,
  70. required: ["libfoo"],
  71. }
  72. `)
  73. mod := ctx.ModuleForTests("foo", "android_common").Module()
  74. entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
  75. if len(entriesList) != 2 {
  76. t.Errorf("two entries are expected, but got %d", len(entriesList))
  77. }
  78. mainEntries := &entriesList[0]
  79. expected := []string{"libfoo"}
  80. actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
  81. if !reflect.DeepEqual(expected, actual) {
  82. t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
  83. }
  84. subEntries := &entriesList[1]
  85. expected = []string{"libfoo"}
  86. actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
  87. if !reflect.DeepEqual(expected, actual) {
  88. t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
  89. }
  90. }
  91. func TestHostdexSpecificRequired(t *testing.T) {
  92. ctx, _ := testJava(t, `
  93. java_library {
  94. name: "foo",
  95. srcs: ["a.java"],
  96. hostdex: true,
  97. target: {
  98. hostdex: {
  99. required: ["libfoo"],
  100. },
  101. },
  102. }
  103. `)
  104. mod := ctx.ModuleForTests("foo", "android_common").Module()
  105. entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
  106. if len(entriesList) != 2 {
  107. t.Errorf("two entries are expected, but got %d", len(entriesList))
  108. }
  109. mainEntries := &entriesList[0]
  110. if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
  111. t.Errorf("Unexpected required modules: %q", r)
  112. }
  113. subEntries := &entriesList[1]
  114. expected := []string{"libfoo"}
  115. actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
  116. if !reflect.DeepEqual(expected, actual) {
  117. t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
  118. }
  119. }
  120. func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
  121. result := android.GroupFixturePreparers(
  122. prepareForJavaTest,
  123. PrepareForTestWithJavaSdkLibraryFiles,
  124. FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"),
  125. ).RunTestWithBp(t, `
  126. java_sdk_library {
  127. name: "foo-shared_library",
  128. srcs: ["a.java"],
  129. }
  130. java_sdk_library {
  131. name: "foo-no_shared_library",
  132. srcs: ["a.java"],
  133. shared_library: false,
  134. }
  135. `)
  136. // Verify the existence of internal modules
  137. result.ModuleForTests("foo-shared_library.xml", "android_common")
  138. testCases := []struct {
  139. moduleName string
  140. expected []string
  141. }{
  142. {"foo-shared_library", []string{"foo-shared_library.xml"}},
  143. {"foo-no_shared_library", nil},
  144. }
  145. for _, tc := range testCases {
  146. mod := result.ModuleForTests(tc.moduleName, "android_common").Module()
  147. entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
  148. actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
  149. if !reflect.DeepEqual(tc.expected, actual) {
  150. t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
  151. }
  152. }
  153. }
  154. func TestImportSoongDexJar(t *testing.T) {
  155. result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, `
  156. java_import {
  157. name: "my-java-import",
  158. jars: ["a.jar"],
  159. prefer: true,
  160. compile_dex: true,
  161. }
  162. `)
  163. mod := result.Module("my-java-import", "android_common")
  164. entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
  165. expectedSoongDexJar := "out/soong/.intermediates/my-java-import/android_common/dex/my-java-import.jar"
  166. actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"]
  167. android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar)
  168. }
  169. func TestAndroidTestHelperApp_LocalDisableTestConfig(t *testing.T) {
  170. ctx, _ := testJava(t, `
  171. android_test_helper_app {
  172. name: "foo",
  173. srcs: ["a.java"],
  174. }
  175. `)
  176. mod := ctx.ModuleForTests("foo", "android_common").Module()
  177. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  178. expected := []string{"true"}
  179. actual := entries.EntryMap["LOCAL_DISABLE_TEST_CONFIG"]
  180. if !reflect.DeepEqual(expected, actual) {
  181. t.Errorf("Unexpected flag value - expected: %q, actual: %q", expected, actual)
  182. }
  183. }
  184. func TestGetOverriddenPackages(t *testing.T) {
  185. ctx, _ := testJava(
  186. t, `
  187. android_app {
  188. name: "foo",
  189. srcs: ["a.java"],
  190. sdk_version: "current",
  191. overrides: ["qux"]
  192. }
  193. override_android_app {
  194. name: "foo_override",
  195. base: "foo",
  196. overrides: ["bar"]
  197. }
  198. `)
  199. expectedVariants := []struct {
  200. name string
  201. moduleName string
  202. variantName string
  203. overrides []string
  204. }{
  205. {
  206. name: "foo",
  207. moduleName: "foo",
  208. variantName: "android_common",
  209. overrides: []string{"qux"},
  210. },
  211. {
  212. name: "foo",
  213. moduleName: "foo_override",
  214. variantName: "android_common_foo_override",
  215. overrides: []string{"bar", "foo"},
  216. },
  217. }
  218. for _, expected := range expectedVariants {
  219. mod := ctx.ModuleForTests(expected.name, expected.variantName).Module()
  220. entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
  221. actual := entries.EntryMap["LOCAL_OVERRIDES_PACKAGES"]
  222. android.AssertDeepEquals(t, "overrides property", expected.overrides, actual)
  223. }
  224. }
  225. func TestJniPartition(t *testing.T) {
  226. bp := `
  227. cc_library {
  228. name: "libjni_system",
  229. system_shared_libs: [],
  230. sdk_version: "current",
  231. stl: "none",
  232. }
  233. cc_library {
  234. name: "libjni_system_ext",
  235. system_shared_libs: [],
  236. sdk_version: "current",
  237. stl: "none",
  238. system_ext_specific: true,
  239. }
  240. cc_library {
  241. name: "libjni_odm",
  242. system_shared_libs: [],
  243. sdk_version: "current",
  244. stl: "none",
  245. device_specific: true,
  246. }
  247. cc_library {
  248. name: "libjni_product",
  249. system_shared_libs: [],
  250. sdk_version: "current",
  251. stl: "none",
  252. product_specific: true,
  253. }
  254. cc_library {
  255. name: "libjni_vendor",
  256. system_shared_libs: [],
  257. sdk_version: "current",
  258. stl: "none",
  259. soc_specific: true,
  260. }
  261. android_app {
  262. name: "test_app_system_jni_system",
  263. privileged: true,
  264. platform_apis: true,
  265. certificate: "platform",
  266. jni_libs: ["libjni_system"],
  267. }
  268. android_app {
  269. name: "test_app_system_jni_system_ext",
  270. privileged: true,
  271. platform_apis: true,
  272. certificate: "platform",
  273. jni_libs: ["libjni_system_ext"],
  274. }
  275. android_app {
  276. name: "test_app_system_ext_jni_system",
  277. privileged: true,
  278. platform_apis: true,
  279. certificate: "platform",
  280. jni_libs: ["libjni_system"],
  281. system_ext_specific: true
  282. }
  283. android_app {
  284. name: "test_app_system_ext_jni_system_ext",
  285. sdk_version: "core_platform",
  286. jni_libs: ["libjni_system_ext"],
  287. system_ext_specific: true
  288. }
  289. android_app {
  290. name: "test_app_product_jni_product",
  291. sdk_version: "core_platform",
  292. jni_libs: ["libjni_product"],
  293. product_specific: true
  294. }
  295. android_app {
  296. name: "test_app_vendor_jni_odm",
  297. sdk_version: "core_platform",
  298. jni_libs: ["libjni_odm"],
  299. soc_specific: true
  300. }
  301. android_app {
  302. name: "test_app_odm_jni_vendor",
  303. sdk_version: "core_platform",
  304. jni_libs: ["libjni_vendor"],
  305. device_specific: true
  306. }
  307. android_app {
  308. name: "test_app_system_jni_multiple",
  309. privileged: true,
  310. platform_apis: true,
  311. certificate: "platform",
  312. jni_libs: ["libjni_system", "libjni_system_ext"],
  313. }
  314. android_app {
  315. name: "test_app_vendor_jni_multiple",
  316. sdk_version: "core_platform",
  317. jni_libs: ["libjni_odm", "libjni_vendor"],
  318. soc_specific: true
  319. }
  320. `
  321. arch := "arm64"
  322. ctx := android.GroupFixturePreparers(
  323. PrepareForTestWithJavaDefaultModules,
  324. cc.PrepareForTestWithCcDefaultModules,
  325. android.PrepareForTestWithAndroidMk,
  326. android.FixtureModifyConfig(func(config android.Config) {
  327. config.TestProductVariables.DeviceArch = proptools.StringPtr(arch)
  328. }),
  329. ).
  330. RunTestWithBp(t, bp)
  331. testCases := []struct {
  332. name string
  333. partitionNames []string
  334. partitionTags []string
  335. }{
  336. {"test_app_system_jni_system", []string{"libjni_system"}, []string{""}},
  337. {"test_app_system_jni_system_ext", []string{"libjni_system_ext"}, []string{"_SYSTEM_EXT"}},
  338. {"test_app_system_ext_jni_system", []string{"libjni_system"}, []string{""}},
  339. {"test_app_system_ext_jni_system_ext", []string{"libjni_system_ext"}, []string{"_SYSTEM_EXT"}},
  340. {"test_app_product_jni_product", []string{"libjni_product"}, []string{"_PRODUCT"}},
  341. {"test_app_vendor_jni_odm", []string{"libjni_odm"}, []string{"_ODM"}},
  342. {"test_app_odm_jni_vendor", []string{"libjni_vendor"}, []string{"_VENDOR"}},
  343. {"test_app_system_jni_multiple", []string{"libjni_system", "libjni_system_ext"}, []string{"", "_SYSTEM_EXT"}},
  344. {"test_app_vendor_jni_multiple", []string{"libjni_odm", "libjni_vendor"}, []string{"_ODM", "_VENDOR"}},
  345. }
  346. for _, test := range testCases {
  347. t.Run(test.name, func(t *testing.T) {
  348. mod := ctx.ModuleForTests(test.name, "android_common").Module()
  349. entry := android.AndroidMkEntriesForTest(t, ctx.TestContext, mod)[0]
  350. for i := range test.partitionNames {
  351. actual := entry.EntryMap["LOCAL_SOONG_JNI_LIBS_PARTITION_"+arch][i]
  352. expected := test.partitionNames[i] + ":" + test.partitionTags[i]
  353. android.AssertStringEquals(t, "Expected and actual differ", expected, actual)
  354. }
  355. })
  356. }
  357. }