blob: 359d8d7c4bd4fc681d4ba31761c3971740186d14 [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"
Anton Hansson78156ef2020-03-27 19:39:48 +000019 "strings"
Jooyung Han12df5fb2019-07-11 16:18:47 +090020 "testing"
Jaewoong Jungf9a04432019-07-17 11:15:09 -070021
22 "android/soong/android"
Jooyung Han12df5fb2019-07-11 16:18:47 +090023)
24
Jooyung Han12df5fb2019-07-11 16:18:47 +090025func TestRequired(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070026 t.Parallel()
Jaewoong Jungf9a04432019-07-17 11:15:09 -070027 ctx, config := 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
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070035 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park0b0e1b92019-12-03 13:24:29 +090036 entries := android.AndroidMkEntriesForTest(t, config, "", 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 Cross323dc602020-09-18 14:25:31 -070046 t.Parallel()
Jaewoong Jungf9a04432019-07-17 11:15:09 -070047 ctx, config := 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()
Jiyong Park55bd98b2019-12-11 17:27:07 +090056 entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
57 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 Cross323dc602020-09-18 14:25:31 -070077 t.Parallel()
Jaewoong Jungf9a04432019-07-17 11:15:09 -070078 ctx, config := 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
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070087 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park55bd98b2019-12-11 17:27:07 +090088 entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
89 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 Cross323dc602020-09-18 14:25:31 -0700109 t.Parallel()
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700110 ctx, config := 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
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700123 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park55bd98b2019-12-11 17:27:07 +0900124 entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
125 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
142func TestDistWithTag(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700143 t.Parallel()
Anton Hansson78156ef2020-03-27 19:39:48 +0000144 ctx, config := testJava(t, `
145 java_library {
146 name: "foo_without_tag",
147 srcs: ["a.java"],
148 compile_dex: true,
149 dist: {
150 targets: ["hi"],
151 },
152 }
153 java_library {
154 name: "foo_with_tag",
155 srcs: ["a.java"],
156 compile_dex: true,
157 dist: {
158 targets: ["hi"],
159 tag: ".jar",
160 },
161 }
162 `)
163
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000164 withoutTagEntries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_without_tag", "android_common").Module())
165 withTagEntries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_with_tag", "android_common").Module())
Anton Hansson78156ef2020-03-27 19:39:48 +0000166
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000167 if len(withoutTagEntries) != 2 || len(withTagEntries) != 2 {
168 t.Errorf("two mk entries per module expected, got %d and %d", len(withoutTagEntries), len(withTagEntries))
Anton Hansson78156ef2020-03-27 19:39:48 +0000169 }
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000170 if len(withTagEntries[0].DistFiles[".jar"]) != 1 ||
171 !strings.Contains(withTagEntries[0].DistFiles[".jar"][0].String(), "/javac/foo_with_tag.jar") {
172 t.Errorf("expected DistFiles to contain classes.jar, got %v", withTagEntries[0].DistFiles)
Anton Hansson78156ef2020-03-27 19:39:48 +0000173 }
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000174 if len(withoutTagEntries[0].DistFiles[".jar"]) > 0 {
175 t.Errorf("did not expect explicit DistFile for .jar tag, got %v", withoutTagEntries[0].DistFiles[".jar"])
176 }
177}
178
179func TestDistWithDest(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700180 t.Parallel()
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000181 ctx, config := testJava(t, `
182 java_library {
183 name: "foo",
184 srcs: ["a.java"],
185 compile_dex: true,
186 dist: {
187 targets: ["my_goal"],
188 dest: "my/custom/dest/dir",
189 },
190 }
191 `)
192
193 module := ctx.ModuleForTests("foo", "android_common").Module()
194 entries := android.AndroidMkEntriesForTest(t, config, "", module)
195 if len(entries) != 2 {
196 t.Errorf("Expected 2 AndroidMk entries, got %d", len(entries))
197 }
198
199 distStrings := entries[0].GetDistForGoals(module)
200
201 if len(distStrings) != 2 {
202 t.Errorf("Expected 2 entries for dist: PHONY and dist-for-goals, but got %q", distStrings)
203 }
204
205 if distStrings[0] != ".PHONY: my_goal\n" {
206 t.Errorf("Expected .PHONY entry to declare my_goal, but got: %s", distStrings[0])
207 }
208
209 if !strings.Contains(distStrings[1], "$(call dist-for-goals,my_goal") ||
210 !strings.Contains(distStrings[1], ".intermediates/foo/android_common/dex/foo.jar:my/custom/dest/dir") {
211 t.Errorf(
212 "Expected dist-for-goals entry to contain my_goal and new dest dir, but got: %s", distStrings[1])
213 }
214}
215
216func TestDistsWithAllProperties(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700217 t.Parallel()
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000218 ctx, config := testJava(t, `
219 java_library {
220 name: "foo",
221 srcs: ["a.java"],
222 compile_dex: true,
223 dist: {
224 targets: ["baz"],
225 },
226 dists: [
227 {
228 targets: ["bar"],
229 tag: ".jar",
230 dest: "bar.jar",
231 dir: "bar/dir",
232 suffix: ".qux",
233 },
234 ]
235 }
236 `)
237
238 module := ctx.ModuleForTests("foo", "android_common").Module()
239 entries := android.AndroidMkEntriesForTest(t, config, "", module)
240 if len(entries) != 2 {
241 t.Errorf("Expected 2 AndroidMk entries, got %d", len(entries))
242 }
243
244 distStrings := entries[0].GetDistForGoals(module)
245
246 if len(distStrings) != 4 {
247 t.Errorf("Expected 4 entries for dist: PHONY and dist-for-goals, but got %d", len(distStrings))
248 }
249
250 if distStrings[0] != ".PHONY: bar\n" {
251 t.Errorf("Expected .PHONY entry to declare bar, but got: %s", distStrings[0])
252 }
253
254 if !strings.Contains(distStrings[1], "$(call dist-for-goals,bar") ||
255 !strings.Contains(
256 distStrings[1],
257 ".intermediates/foo/android_common/javac/foo.jar:bar/dir/bar.qux.jar") {
258 t.Errorf(
259 "Expected dist-for-goals entry to contain bar and new dest dir, but got: %s", distStrings[1])
260 }
261
262 if distStrings[2] != ".PHONY: baz\n" {
263 t.Errorf("Expected .PHONY entry to declare baz, but got: %s", distStrings[2])
264 }
265
266 if !strings.Contains(distStrings[3], "$(call dist-for-goals,baz") ||
267 !strings.Contains(distStrings[3], ".intermediates/foo/android_common/dex/foo.jar:foo.jar") {
268 t.Errorf(
269 "Expected dist-for-goals entry to contain my_other_goal and new dest dir, but got: %s",
270 distStrings[3])
271 }
272}
273
274func TestDistsWithTag(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700275 t.Parallel()
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000276 ctx, config := testJava(t, `
277 java_library {
278 name: "foo_without_tag",
279 srcs: ["a.java"],
280 compile_dex: true,
281 dists: [
282 {
283 targets: ["hi"],
284 },
285 ],
286 }
287 java_library {
288 name: "foo_with_tag",
289 srcs: ["a.java"],
290 compile_dex: true,
291 dists: [
292 {
293 targets: ["hi"],
294 tag: ".jar",
295 },
296 ],
297 }
298 `)
299
300 moduleWithoutTag := ctx.ModuleForTests("foo_without_tag", "android_common").Module()
301 moduleWithTag := ctx.ModuleForTests("foo_with_tag", "android_common").Module()
302
303 withoutTagEntries := android.AndroidMkEntriesForTest(t, config, "", moduleWithoutTag)
304 withTagEntries := android.AndroidMkEntriesForTest(t, config, "", moduleWithTag)
305
306 if len(withoutTagEntries) != 2 || len(withTagEntries) != 2 {
307 t.Errorf("two mk entries per module expected, got %d and %d", len(withoutTagEntries), len(withTagEntries))
308 }
309
310 distFilesWithoutTag := withoutTagEntries[0].DistFiles
311 distFilesWithTag := withTagEntries[0].DistFiles
312
313 if len(distFilesWithTag[".jar"]) != 1 ||
314 !strings.Contains(distFilesWithTag[".jar"][0].String(), "/javac/foo_with_tag.jar") {
315 t.Errorf("expected foo_with_tag's .jar-tagged DistFiles to contain classes.jar, got %v", distFilesWithTag[".jar"])
316 }
317 if len(distFilesWithoutTag[".jar"]) > 0 {
318 t.Errorf("did not expect foo_without_tag's .jar-tagged DistFiles to contain files, but got %v", distFilesWithoutTag[".jar"])
Anton Hansson78156ef2020-03-27 19:39:48 +0000319 }
320}
Yo Chiang07d75072020-06-05 17:43:19 +0800321
322func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700323 t.Parallel()
Yo Chiang07d75072020-06-05 17:43:19 +0800324 ctx, config := testJava(t, `
325 java_sdk_library {
326 name: "foo-shared_library",
327 srcs: ["a.java"],
328 }
329 java_sdk_library {
330 name: "foo-no_shared_library",
331 srcs: ["a.java"],
332 shared_library: false,
333 }
334 `)
335
336 // Verify the existence of internal modules
337 ctx.ModuleForTests("foo-shared_library.xml", "android_common")
338
339 testCases := []struct {
340 moduleName string
341 expected []string
342 }{
343 {"foo-shared_library", []string{"foo-shared_library.xml"}},
344 {"foo-no_shared_library", nil},
345 }
346 for _, tc := range testCases {
347 mod := ctx.ModuleForTests(tc.moduleName, "android_common").Module()
348 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
349 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
350 if !reflect.DeepEqual(tc.expected, actual) {
351 t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
352 }
353 }
354}