blob: 1232cd1ee0f0575398c68c93e7bbf92e1ebfda0a [file] [log] [blame]
Jooyung Han12df5fb2019-07-11 16:18:47 +09001// 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
15package java
16
17import (
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070018 "reflect"
Jooyung Han12df5fb2019-07-11 16:18:47 +090019 "testing"
Jaewoong Jungf9a04432019-07-17 11:15:09 -070020
21 "android/soong/android"
Jihoon Kangf78a8902022-09-01 22:47:07 +000022 "android/soong/cc"
23
24 "github.com/google/blueprint/proptools"
Jooyung Han12df5fb2019-07-11 16:18:47 +090025)
26
Jooyung Han12df5fb2019-07-11 16:18:47 +090027func TestRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070028 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090029 java_library {
30 name: "foo",
31 srcs: ["a.java"],
32 required: ["libfoo"],
33 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070034 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090035
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070036 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070037 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070038
39 expected := []string{"libfoo"}
40 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
41 if !reflect.DeepEqual(expected, actual) {
42 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
43 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090044}
45
46func TestHostdex(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070047 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090048 java_library {
49 name: "foo",
50 srcs: ["a.java"],
51 hostdex: true,
52 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070053 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090054
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070055 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070056 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +090057 if len(entriesList) != 2 {
58 t.Errorf("two entries are expected, but got %d", len(entriesList))
59 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070060
Jiyong Park55bd98b2019-12-11 17:27:07 +090061 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070062 expected := []string{"foo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090063 actual := mainEntries.EntryMap["LOCAL_MODULE"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070064 if !reflect.DeepEqual(expected, actual) {
65 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
66 }
67
Jiyong Park55bd98b2019-12-11 17:27:07 +090068 subEntries := &entriesList[1]
69 expected = []string{"foo-hostdex"}
70 actual = subEntries.EntryMap["LOCAL_MODULE"]
71 if !reflect.DeepEqual(expected, actual) {
72 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070073 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090074}
75
76func TestHostdexRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070077 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090078 java_library {
79 name: "foo",
80 srcs: ["a.java"],
81 hostdex: true,
82 required: ["libfoo"],
83 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070084 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090085
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070086 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070087 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +090088 if len(entriesList) != 2 {
89 t.Errorf("two entries are expected, but got %d", len(entriesList))
90 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070091
Jiyong Park55bd98b2019-12-11 17:27:07 +090092 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070093 expected := []string{"libfoo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090094 actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070095 if !reflect.DeepEqual(expected, actual) {
96 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
97 }
98
Jiyong Park55bd98b2019-12-11 17:27:07 +090099 subEntries := &entriesList[1]
100 expected = []string{"libfoo"}
101 actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
102 if !reflect.DeepEqual(expected, actual) {
103 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700104 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900105}
106
107func TestHostdexSpecificRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700108 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900109 java_library {
110 name: "foo",
111 srcs: ["a.java"],
112 hostdex: true,
113 target: {
114 hostdex: {
115 required: ["libfoo"],
116 },
117 },
118 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700119 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900120
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700121 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -0700122 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +0900123 if len(entriesList) != 2 {
124 t.Errorf("two entries are expected, but got %d", len(entriesList))
125 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700126
Jiyong Park55bd98b2019-12-11 17:27:07 +0900127 mainEntries := &entriesList[0]
128 if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700129 t.Errorf("Unexpected required modules: %q", r)
130 }
131
Jiyong Park55bd98b2019-12-11 17:27:07 +0900132 subEntries := &entriesList[1]
133 expected := []string{"libfoo"}
134 actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
135 if !reflect.DeepEqual(expected, actual) {
136 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700137 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900138}
Anton Hansson78156ef2020-03-27 19:39:48 +0000139
Yo Chiang07d75072020-06-05 17:43:19 +0800140func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000141 result := android.GroupFixturePreparers(
142 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000143 PrepareForTestWithJavaSdkLibraryFiles,
144 FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"),
145 ).RunTestWithBp(t, `
Yo Chiang07d75072020-06-05 17:43:19 +0800146 java_sdk_library {
147 name: "foo-shared_library",
148 srcs: ["a.java"],
149 }
150 java_sdk_library {
151 name: "foo-no_shared_library",
152 srcs: ["a.java"],
153 shared_library: false,
154 }
155 `)
156
157 // Verify the existence of internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +0000158 result.ModuleForTests("foo-shared_library.xml", "android_common")
Yo Chiang07d75072020-06-05 17:43:19 +0800159
160 testCases := []struct {
161 moduleName string
162 expected []string
163 }{
164 {"foo-shared_library", []string{"foo-shared_library.xml"}},
165 {"foo-no_shared_library", nil},
166 }
167 for _, tc := range testCases {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000168 mod := result.ModuleForTests(tc.moduleName, "android_common").Module()
169 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Yo Chiang07d75072020-06-05 17:43:19 +0800170 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
171 if !reflect.DeepEqual(tc.expected, actual) {
172 t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
173 }
174 }
175}
Bill Peckhamfb04df42021-01-11 12:27:24 -0800176
177func TestImportSoongDexJar(t *testing.T) {
Paul Duffinf04311c2021-03-22 17:31:51 +0000178 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, `
Bill Peckhamfb04df42021-01-11 12:27:24 -0800179 java_import {
180 name: "my-java-import",
181 jars: ["a.jar"],
182 prefer: true,
183 compile_dex: true,
184 }
185 `)
186
Paul Duffinf04311c2021-03-22 17:31:51 +0000187 mod := result.Module("my-java-import", "android_common")
188 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
189 expectedSoongDexJar := "out/soong/.intermediates/my-java-import/android_common/dex/my-java-import.jar"
Bill Peckhamfb04df42021-01-11 12:27:24 -0800190 actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"]
191
Paul Duffinf04311c2021-03-22 17:31:51 +0000192 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar)
Bill Peckhamfb04df42021-01-11 12:27:24 -0800193}
Yuntao Xu7a318552021-05-27 10:30:26 -0700194
195func TestAndroidTestHelperApp_LocalDisableTestConfig(t *testing.T) {
196 ctx, _ := testJava(t, `
197 android_test_helper_app {
198 name: "foo",
199 srcs: ["a.java"],
200 }
201 `)
202
203 mod := ctx.ModuleForTests("foo", "android_common").Module()
204 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
205
206 expected := []string{"true"}
207 actual := entries.EntryMap["LOCAL_DISABLE_TEST_CONFIG"]
208 if !reflect.DeepEqual(expected, actual) {
209 t.Errorf("Unexpected flag value - expected: %q, actual: %q", expected, actual)
210 }
211}
zhidou198f5892022-02-17 02:33:12 +0000212
213func TestGetOverriddenPackages(t *testing.T) {
214 ctx, _ := testJava(
215 t, `
216 android_app {
217 name: "foo",
218 srcs: ["a.java"],
219 sdk_version: "current",
220 overrides: ["qux"]
221 }
222
223 override_android_app {
224 name: "foo_override",
225 base: "foo",
226 overrides: ["bar"]
227 }
228 `)
229
230 expectedVariants := []struct {
231 name string
232 moduleName string
233 variantName string
234 overrides []string
235 }{
236 {
237 name: "foo",
238 moduleName: "foo",
239 variantName: "android_common",
240 overrides: []string{"qux"},
241 },
242 {
243 name: "foo",
244 moduleName: "foo_override",
245 variantName: "android_common_foo_override",
246 overrides: []string{"bar", "foo"},
247 },
248 }
249
250 for _, expected := range expectedVariants {
251 mod := ctx.ModuleForTests(expected.name, expected.variantName).Module()
252 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
253 actual := entries.EntryMap["LOCAL_OVERRIDES_PACKAGES"]
254
255 android.AssertDeepEquals(t, "overrides property", expected.overrides, actual)
256 }
257}
Jihoon Kangf78a8902022-09-01 22:47:07 +0000258
259func TestJniPartition(t *testing.T) {
260 bp := `
261 cc_library {
262 name: "libjni_system",
263 system_shared_libs: [],
264 sdk_version: "current",
265 stl: "none",
266 }
267
268 cc_library {
269 name: "libjni_system_ext",
270 system_shared_libs: [],
271 sdk_version: "current",
272 stl: "none",
273 system_ext_specific: true,
274 }
275
276 cc_library {
277 name: "libjni_odm",
278 system_shared_libs: [],
279 sdk_version: "current",
280 stl: "none",
281 device_specific: true,
282 }
283
284 cc_library {
285 name: "libjni_product",
286 system_shared_libs: [],
287 sdk_version: "current",
288 stl: "none",
289 product_specific: true,
290 }
291
292 cc_library {
293 name: "libjni_vendor",
294 system_shared_libs: [],
295 sdk_version: "current",
296 stl: "none",
297 soc_specific: true,
298 }
299
300 android_app {
301 name: "test_app_system_jni_system",
302 privileged: true,
303 platform_apis: true,
304 certificate: "platform",
305 jni_libs: ["libjni_system"],
306 }
307
308 android_app {
309 name: "test_app_system_jni_system_ext",
310 privileged: true,
311 platform_apis: true,
312 certificate: "platform",
313 jni_libs: ["libjni_system_ext"],
314 }
315
316 android_app {
317 name: "test_app_system_ext_jni_system",
318 privileged: true,
319 platform_apis: true,
320 certificate: "platform",
321 jni_libs: ["libjni_system"],
322 system_ext_specific: true
323 }
324
325 android_app {
326 name: "test_app_system_ext_jni_system_ext",
327 sdk_version: "core_platform",
328 jni_libs: ["libjni_system_ext"],
329 system_ext_specific: true
330 }
331
332 android_app {
333 name: "test_app_product_jni_product",
334 sdk_version: "core_platform",
335 jni_libs: ["libjni_product"],
336 product_specific: true
337 }
338
339 android_app {
340 name: "test_app_vendor_jni_odm",
341 sdk_version: "core_platform",
342 jni_libs: ["libjni_odm"],
343 soc_specific: true
344 }
345
346 android_app {
347 name: "test_app_odm_jni_vendor",
348 sdk_version: "core_platform",
349 jni_libs: ["libjni_vendor"],
350 device_specific: true
351 }
352 android_app {
353 name: "test_app_system_jni_multiple",
354 privileged: true,
355 platform_apis: true,
356 certificate: "platform",
357 jni_libs: ["libjni_system", "libjni_system_ext"],
358 }
359 android_app {
360 name: "test_app_vendor_jni_multiple",
361 sdk_version: "core_platform",
362 jni_libs: ["libjni_odm", "libjni_vendor"],
363 soc_specific: true
364 }
365 `
366 arch := "arm64"
367 ctx := android.GroupFixturePreparers(
368 PrepareForTestWithJavaDefaultModules,
369 cc.PrepareForTestWithCcDefaultModules,
370 android.PrepareForTestWithAndroidMk,
371 android.FixtureModifyConfig(func(config android.Config) {
372 config.TestProductVariables.DeviceArch = proptools.StringPtr(arch)
373 }),
374 ).
375 RunTestWithBp(t, bp)
376 testCases := []struct {
377 name string
378 partitionNames []string
379 partitionTags []string
380 }{
381 {"test_app_system_jni_system", []string{"libjni_system"}, []string{""}},
382 {"test_app_system_jni_system_ext", []string{"libjni_system_ext"}, []string{"_SYSTEM_EXT"}},
383 {"test_app_system_ext_jni_system", []string{"libjni_system"}, []string{""}},
384 {"test_app_system_ext_jni_system_ext", []string{"libjni_system_ext"}, []string{"_SYSTEM_EXT"}},
385 {"test_app_product_jni_product", []string{"libjni_product"}, []string{"_PRODUCT"}},
386 {"test_app_vendor_jni_odm", []string{"libjni_odm"}, []string{"_ODM"}},
387 {"test_app_odm_jni_vendor", []string{"libjni_vendor"}, []string{"_VENDOR"}},
388 {"test_app_system_jni_multiple", []string{"libjni_system", "libjni_system_ext"}, []string{"", "_SYSTEM_EXT"}},
389 {"test_app_vendor_jni_multiple", []string{"libjni_odm", "libjni_vendor"}, []string{"_ODM", "_VENDOR"}},
390 }
391
392 for _, test := range testCases {
393 t.Run(test.name, func(t *testing.T) {
394 mod := ctx.ModuleForTests(test.name, "android_common").Module()
395 entry := android.AndroidMkEntriesForTest(t, ctx.TestContext, mod)[0]
396 for i := range test.partitionNames {
397 actual := entry.EntryMap["LOCAL_SOONG_JNI_LIBS_PARTITION_"+arch][i]
398 expected := test.partitionNames[i] + ":" + test.partitionTags[i]
399 android.AssertStringEquals(t, "Expected and actual differ", expected, actual)
400 }
401 })
402 }
403}