blob: b4b13b11afcdcc16aad5be5fa91980a79ef06f78 [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"
Yu Shanb7646e42024-05-17 20:09:23 +000022 "android/soong/cc"
Jooyung Han12df5fb2019-07-11 16:18:47 +090023)
24
Jooyung Han12df5fb2019-07-11 16:18:47 +090025func TestRequired(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -080026 t.Parallel()
Colin Crossaa255532020-07-03 13:18:24 -070027 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090028 java_library {
29 name: "foo",
30 srcs: ["a.java"],
31 required: ["libfoo"],
32 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070033 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090034
Colin Cross90607e92025-02-11 14:58:07 -080035 mod := ctx.ModuleForTests(t, "foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070036 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070037
38 expected := []string{"libfoo"}
39 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
40 if !reflect.DeepEqual(expected, actual) {
41 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
42 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090043}
44
45func TestHostdex(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -080046 t.Parallel()
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
Colin Cross90607e92025-02-11 14:58:07 -080055 mod := ctx.ModuleForTests(t, "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 Cross844cb6a2025-01-29 15:53:21 -080077 t.Parallel()
Colin Crossaa255532020-07-03 13:18:24 -070078 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090079 java_library {
80 name: "foo",
81 srcs: ["a.java"],
82 hostdex: true,
83 required: ["libfoo"],
84 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070085 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090086
Colin Cross90607e92025-02-11 14:58:07 -080087 mod := ctx.ModuleForTests(t, "foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070088 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +090089 if len(entriesList) != 2 {
90 t.Errorf("two entries are expected, but got %d", len(entriesList))
91 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070092
Jiyong Park55bd98b2019-12-11 17:27:07 +090093 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070094 expected := []string{"libfoo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090095 actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070096 if !reflect.DeepEqual(expected, actual) {
97 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
98 }
99
Jiyong Park55bd98b2019-12-11 17:27:07 +0900100 subEntries := &entriesList[1]
101 expected = []string{"libfoo"}
102 actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
103 if !reflect.DeepEqual(expected, actual) {
104 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700105 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900106}
107
108func TestHostdexSpecificRequired(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800109 t.Parallel()
Colin Crossaa255532020-07-03 13:18:24 -0700110 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900111 java_library {
112 name: "foo",
113 srcs: ["a.java"],
114 hostdex: true,
115 target: {
116 hostdex: {
117 required: ["libfoo"],
118 },
119 },
120 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700121 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900122
Colin Cross90607e92025-02-11 14:58:07 -0800123 mod := ctx.ModuleForTests(t, "foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -0700124 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +0900125 if len(entriesList) != 2 {
126 t.Errorf("two entries are expected, but got %d", len(entriesList))
127 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700128
Jiyong Park55bd98b2019-12-11 17:27:07 +0900129 mainEntries := &entriesList[0]
130 if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700131 t.Errorf("Unexpected required modules: %q", r)
132 }
133
Jiyong Park55bd98b2019-12-11 17:27:07 +0900134 subEntries := &entriesList[1]
135 expected := []string{"libfoo"}
136 actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
137 if !reflect.DeepEqual(expected, actual) {
138 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700139 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900140}
Anton Hansson78156ef2020-03-27 19:39:48 +0000141
Yo Chiang07d75072020-06-05 17:43:19 +0800142func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800143 t.Parallel()
Paul Duffin71ae5942021-03-22 15:36:52 +0000144 result := android.GroupFixturePreparers(
145 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000146 PrepareForTestWithJavaSdkLibraryFiles,
147 FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"),
148 ).RunTestWithBp(t, `
Yo Chiang07d75072020-06-05 17:43:19 +0800149 java_sdk_library {
150 name: "foo-shared_library",
151 srcs: ["a.java"],
152 }
153 java_sdk_library {
154 name: "foo-no_shared_library",
155 srcs: ["a.java"],
156 shared_library: false,
157 }
158 `)
159
160 // Verify the existence of internal modules
Colin Cross90607e92025-02-11 14:58:07 -0800161 result.ModuleForTests(t, "foo-shared_library.xml", "android_common")
Yo Chiang07d75072020-06-05 17:43:19 +0800162
163 testCases := []struct {
164 moduleName string
165 expected []string
166 }{
Jihoon Kanga3a05462024-04-05 00:36:44 +0000167 {"foo-shared_library", []string{"foo-shared_library.impl", "foo-shared_library.xml"}},
168 {"foo-no_shared_library", []string{"foo-no_shared_library.impl"}},
Yo Chiang07d75072020-06-05 17:43:19 +0800169 }
170 for _, tc := range testCases {
Colin Cross90607e92025-02-11 14:58:07 -0800171 mod := result.ModuleForTests(t, tc.moduleName, "android_common").Module()
Paul Duffin22b77cd2021-03-12 19:15:01 +0000172 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Yo Chiang07d75072020-06-05 17:43:19 +0800173 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
174 if !reflect.DeepEqual(tc.expected, actual) {
175 t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
176 }
177 }
178}
Bill Peckhamfb04df42021-01-11 12:27:24 -0800179
180func TestImportSoongDexJar(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800181 t.Parallel()
Paul Duffinf04311c2021-03-22 17:31:51 +0000182 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, `
Bill Peckhamfb04df42021-01-11 12:27:24 -0800183 java_import {
184 name: "my-java-import",
185 jars: ["a.jar"],
186 prefer: true,
187 compile_dex: true,
188 }
189 `)
190
Paul Duffinf04311c2021-03-22 17:31:51 +0000191 mod := result.Module("my-java-import", "android_common")
192 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
193 expectedSoongDexJar := "out/soong/.intermediates/my-java-import/android_common/dex/my-java-import.jar"
Bill Peckhamfb04df42021-01-11 12:27:24 -0800194 actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"]
195
Paul Duffinf04311c2021-03-22 17:31:51 +0000196 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar)
Bill Peckhamfb04df42021-01-11 12:27:24 -0800197}
Yuntao Xu7a318552021-05-27 10:30:26 -0700198
199func TestAndroidTestHelperApp_LocalDisableTestConfig(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800200 t.Parallel()
Yuntao Xu7a318552021-05-27 10:30:26 -0700201 ctx, _ := testJava(t, `
202 android_test_helper_app {
203 name: "foo",
204 srcs: ["a.java"],
205 }
206 `)
207
Colin Cross90607e92025-02-11 14:58:07 -0800208 mod := ctx.ModuleForTests(t, "foo", "android_common").Module()
Yuntao Xu7a318552021-05-27 10:30:26 -0700209 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
210
211 expected := []string{"true"}
212 actual := entries.EntryMap["LOCAL_DISABLE_TEST_CONFIG"]
213 if !reflect.DeepEqual(expected, actual) {
214 t.Errorf("Unexpected flag value - expected: %q, actual: %q", expected, actual)
215 }
216}
zhidou198f5892022-02-17 02:33:12 +0000217
218func TestGetOverriddenPackages(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800219 t.Parallel()
zhidou198f5892022-02-17 02:33:12 +0000220 ctx, _ := testJava(
221 t, `
222 android_app {
223 name: "foo",
224 srcs: ["a.java"],
225 sdk_version: "current",
226 overrides: ["qux"]
227 }
228
229 override_android_app {
230 name: "foo_override",
231 base: "foo",
232 overrides: ["bar"]
233 }
234 `)
235
236 expectedVariants := []struct {
237 name string
238 moduleName string
239 variantName string
240 overrides []string
241 }{
242 {
243 name: "foo",
244 moduleName: "foo",
245 variantName: "android_common",
246 overrides: []string{"qux"},
247 },
248 {
249 name: "foo",
250 moduleName: "foo_override",
251 variantName: "android_common_foo_override",
252 overrides: []string{"bar", "foo"},
253 },
254 }
255
256 for _, expected := range expectedVariants {
Colin Cross90607e92025-02-11 14:58:07 -0800257 mod := ctx.ModuleForTests(t, expected.name, expected.variantName).Module()
zhidou198f5892022-02-17 02:33:12 +0000258 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
259 actual := entries.EntryMap["LOCAL_OVERRIDES_PACKAGES"]
260
261 android.AssertDeepEquals(t, "overrides property", expected.overrides, actual)
262 }
263}
Yu Shanb7646e42024-05-17 20:09:23 +0000264
Jiyong Park25b92222024-05-17 22:58:54 +0000265func TestJniAsRequiredDeps(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800266 t.Parallel()
Yu Shanb7646e42024-05-17 20:09:23 +0000267 ctx := android.GroupFixturePreparers(
268 PrepareForTestWithJavaDefaultModules,
269 cc.PrepareForTestWithCcDefaultModules,
270 android.PrepareForTestWithAndroidMk,
Jiyong Park25b92222024-05-17 22:58:54 +0000271 ).RunTestWithBp(t, `
272 android_app {
273 name: "app",
274 jni_libs: ["libjni"],
275 platform_apis: true,
276 }
277
278 android_app {
279 name: "app_embedded",
280 jni_libs: ["libjni"],
281 platform_apis: true,
282 use_embedded_native_libs: true,
283 }
284
285 cc_library {
286 name: "libjni",
287 system_shared_libs: [],
288 stl: "none",
289 }
290 `)
291
292 testcases := []struct {
293 name string
294 expected []string
Yu Shanb7646e42024-05-17 20:09:23 +0000295 }{
Jiyong Park25b92222024-05-17 22:58:54 +0000296 {
297 name: "app",
Spandan Dasd4530d62024-09-26 00:46:12 +0000298 expected: []string{"libjni:64"},
Jiyong Park25b92222024-05-17 22:58:54 +0000299 },
300 {
301 name: "app_embedded",
302 expected: nil,
303 },
Yu Shanb7646e42024-05-17 20:09:23 +0000304 }
305
Jiyong Park25b92222024-05-17 22:58:54 +0000306 for _, tc := range testcases {
Colin Cross90607e92025-02-11 14:58:07 -0800307 mod := ctx.ModuleForTests(t, tc.name, "android_common").Module()
Jiyong Park25b92222024-05-17 22:58:54 +0000308 entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, mod)[0]
309 required := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
310 android.AssertDeepEquals(t, "unexpected required deps", tc.expected, required)
Yu Shanb7646e42024-05-17 20:09:23 +0000311 }
312}