blob: 075b7aa6f5a7120023ca2d7e3b1f7ec0ebc617cf [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) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -070026 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090027 java_library {
28 name: "foo",
29 srcs: ["a.java"],
30 required: ["libfoo"],
31 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070032 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090033
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070034 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park0b0e1b92019-12-03 13:24:29 +090035 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070036
37 expected := []string{"libfoo"}
38 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
39 if !reflect.DeepEqual(expected, actual) {
40 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
41 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090042}
43
44func TestHostdex(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -070045 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090046 java_library {
47 name: "foo",
48 srcs: ["a.java"],
49 hostdex: true,
50 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070051 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090052
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070053 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park55bd98b2019-12-11 17:27:07 +090054 entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
55 if len(entriesList) != 2 {
56 t.Errorf("two entries are expected, but got %d", len(entriesList))
57 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070058
Jiyong Park55bd98b2019-12-11 17:27:07 +090059 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070060 expected := []string{"foo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090061 actual := mainEntries.EntryMap["LOCAL_MODULE"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070062 if !reflect.DeepEqual(expected, actual) {
63 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
64 }
65
Jiyong Park55bd98b2019-12-11 17:27:07 +090066 subEntries := &entriesList[1]
67 expected = []string{"foo-hostdex"}
68 actual = subEntries.EntryMap["LOCAL_MODULE"]
69 if !reflect.DeepEqual(expected, actual) {
70 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070071 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090072}
73
74func TestHostdexRequired(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -070075 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090076 java_library {
77 name: "foo",
78 srcs: ["a.java"],
79 hostdex: true,
80 required: ["libfoo"],
81 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070082 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090083
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070084 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park55bd98b2019-12-11 17:27:07 +090085 entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
86 if len(entriesList) != 2 {
87 t.Errorf("two entries are expected, but got %d", len(entriesList))
88 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070089
Jiyong Park55bd98b2019-12-11 17:27:07 +090090 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070091 expected := []string{"libfoo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090092 actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070093 if !reflect.DeepEqual(expected, actual) {
94 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
95 }
96
Jiyong Park55bd98b2019-12-11 17:27:07 +090097 subEntries := &entriesList[1]
98 expected = []string{"libfoo"}
99 actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
100 if !reflect.DeepEqual(expected, actual) {
101 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700102 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900103}
104
105func TestHostdexSpecificRequired(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700106 ctx, config := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900107 java_library {
108 name: "foo",
109 srcs: ["a.java"],
110 hostdex: true,
111 target: {
112 hostdex: {
113 required: ["libfoo"],
114 },
115 },
116 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700117 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900118
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700119 mod := ctx.ModuleForTests("foo", "android_common").Module()
Jiyong Park55bd98b2019-12-11 17:27:07 +0900120 entriesList := android.AndroidMkEntriesForTest(t, config, "", mod)
121 if len(entriesList) != 2 {
122 t.Errorf("two entries are expected, but got %d", len(entriesList))
123 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700124
Jiyong Park55bd98b2019-12-11 17:27:07 +0900125 mainEntries := &entriesList[0]
126 if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700127 t.Errorf("Unexpected required modules: %q", r)
128 }
129
Jiyong Park55bd98b2019-12-11 17:27:07 +0900130 subEntries := &entriesList[1]
131 expected := []string{"libfoo"}
132 actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
133 if !reflect.DeepEqual(expected, actual) {
134 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700135 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900136}
Anton Hansson78156ef2020-03-27 19:39:48 +0000137
138func TestDistWithTag(t *testing.T) {
139 ctx, config := testJava(t, `
140 java_library {
141 name: "foo_without_tag",
142 srcs: ["a.java"],
143 compile_dex: true,
144 dist: {
145 targets: ["hi"],
146 },
147 }
148 java_library {
149 name: "foo_with_tag",
150 srcs: ["a.java"],
151 compile_dex: true,
152 dist: {
153 targets: ["hi"],
154 tag: ".jar",
155 },
156 }
157 `)
158
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000159 withoutTagEntries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_without_tag", "android_common").Module())
160 withTagEntries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_with_tag", "android_common").Module())
Anton Hansson78156ef2020-03-27 19:39:48 +0000161
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000162 if len(withoutTagEntries) != 2 || len(withTagEntries) != 2 {
163 t.Errorf("two mk entries per module expected, got %d and %d", len(withoutTagEntries), len(withTagEntries))
Anton Hansson78156ef2020-03-27 19:39:48 +0000164 }
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000165 if len(withTagEntries[0].DistFiles[".jar"]) != 1 ||
166 !strings.Contains(withTagEntries[0].DistFiles[".jar"][0].String(), "/javac/foo_with_tag.jar") {
167 t.Errorf("expected DistFiles to contain classes.jar, got %v", withTagEntries[0].DistFiles)
Anton Hansson78156ef2020-03-27 19:39:48 +0000168 }
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000169 if len(withoutTagEntries[0].DistFiles[".jar"]) > 0 {
170 t.Errorf("did not expect explicit DistFile for .jar tag, got %v", withoutTagEntries[0].DistFiles[".jar"])
171 }
172}
173
174func TestDistWithDest(t *testing.T) {
175 ctx, config := testJava(t, `
176 java_library {
177 name: "foo",
178 srcs: ["a.java"],
179 compile_dex: true,
180 dist: {
181 targets: ["my_goal"],
182 dest: "my/custom/dest/dir",
183 },
184 }
185 `)
186
187 module := ctx.ModuleForTests("foo", "android_common").Module()
188 entries := android.AndroidMkEntriesForTest(t, config, "", module)
189 if len(entries) != 2 {
190 t.Errorf("Expected 2 AndroidMk entries, got %d", len(entries))
191 }
192
193 distStrings := entries[0].GetDistForGoals(module)
194
195 if len(distStrings) != 2 {
196 t.Errorf("Expected 2 entries for dist: PHONY and dist-for-goals, but got %q", distStrings)
197 }
198
199 if distStrings[0] != ".PHONY: my_goal\n" {
200 t.Errorf("Expected .PHONY entry to declare my_goal, but got: %s", distStrings[0])
201 }
202
203 if !strings.Contains(distStrings[1], "$(call dist-for-goals,my_goal") ||
204 !strings.Contains(distStrings[1], ".intermediates/foo/android_common/dex/foo.jar:my/custom/dest/dir") {
205 t.Errorf(
206 "Expected dist-for-goals entry to contain my_goal and new dest dir, but got: %s", distStrings[1])
207 }
208}
209
210func TestDistsWithAllProperties(t *testing.T) {
211 ctx, config := testJava(t, `
212 java_library {
213 name: "foo",
214 srcs: ["a.java"],
215 compile_dex: true,
216 dist: {
217 targets: ["baz"],
218 },
219 dists: [
220 {
221 targets: ["bar"],
222 tag: ".jar",
223 dest: "bar.jar",
224 dir: "bar/dir",
225 suffix: ".qux",
226 },
227 ]
228 }
229 `)
230
231 module := ctx.ModuleForTests("foo", "android_common").Module()
232 entries := android.AndroidMkEntriesForTest(t, config, "", module)
233 if len(entries) != 2 {
234 t.Errorf("Expected 2 AndroidMk entries, got %d", len(entries))
235 }
236
237 distStrings := entries[0].GetDistForGoals(module)
238
239 if len(distStrings) != 4 {
240 t.Errorf("Expected 4 entries for dist: PHONY and dist-for-goals, but got %d", len(distStrings))
241 }
242
243 if distStrings[0] != ".PHONY: bar\n" {
244 t.Errorf("Expected .PHONY entry to declare bar, but got: %s", distStrings[0])
245 }
246
247 if !strings.Contains(distStrings[1], "$(call dist-for-goals,bar") ||
248 !strings.Contains(
249 distStrings[1],
250 ".intermediates/foo/android_common/javac/foo.jar:bar/dir/bar.qux.jar") {
251 t.Errorf(
252 "Expected dist-for-goals entry to contain bar and new dest dir, but got: %s", distStrings[1])
253 }
254
255 if distStrings[2] != ".PHONY: baz\n" {
256 t.Errorf("Expected .PHONY entry to declare baz, but got: %s", distStrings[2])
257 }
258
259 if !strings.Contains(distStrings[3], "$(call dist-for-goals,baz") ||
260 !strings.Contains(distStrings[3], ".intermediates/foo/android_common/dex/foo.jar:foo.jar") {
261 t.Errorf(
262 "Expected dist-for-goals entry to contain my_other_goal and new dest dir, but got: %s",
263 distStrings[3])
264 }
265}
266
267func TestDistsWithTag(t *testing.T) {
268 ctx, config := testJava(t, `
269 java_library {
270 name: "foo_without_tag",
271 srcs: ["a.java"],
272 compile_dex: true,
273 dists: [
274 {
275 targets: ["hi"],
276 },
277 ],
278 }
279 java_library {
280 name: "foo_with_tag",
281 srcs: ["a.java"],
282 compile_dex: true,
283 dists: [
284 {
285 targets: ["hi"],
286 tag: ".jar",
287 },
288 ],
289 }
290 `)
291
292 moduleWithoutTag := ctx.ModuleForTests("foo_without_tag", "android_common").Module()
293 moduleWithTag := ctx.ModuleForTests("foo_with_tag", "android_common").Module()
294
295 withoutTagEntries := android.AndroidMkEntriesForTest(t, config, "", moduleWithoutTag)
296 withTagEntries := android.AndroidMkEntriesForTest(t, config, "", moduleWithTag)
297
298 if len(withoutTagEntries) != 2 || len(withTagEntries) != 2 {
299 t.Errorf("two mk entries per module expected, got %d and %d", len(withoutTagEntries), len(withTagEntries))
300 }
301
302 distFilesWithoutTag := withoutTagEntries[0].DistFiles
303 distFilesWithTag := withTagEntries[0].DistFiles
304
305 if len(distFilesWithTag[".jar"]) != 1 ||
306 !strings.Contains(distFilesWithTag[".jar"][0].String(), "/javac/foo_with_tag.jar") {
307 t.Errorf("expected foo_with_tag's .jar-tagged DistFiles to contain classes.jar, got %v", distFilesWithTag[".jar"])
308 }
309 if len(distFilesWithoutTag[".jar"]) > 0 {
310 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 +0000311 }
312}
Yo Chiang07d75072020-06-05 17:43:19 +0800313
314func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
315 ctx, config := testJava(t, `
316 java_sdk_library {
317 name: "foo-shared_library",
318 srcs: ["a.java"],
319 }
320 java_sdk_library {
321 name: "foo-no_shared_library",
322 srcs: ["a.java"],
323 shared_library: false,
324 }
325 `)
326
327 // Verify the existence of internal modules
328 ctx.ModuleForTests("foo-shared_library.xml", "android_common")
329
330 testCases := []struct {
331 moduleName string
332 expected []string
333 }{
334 {"foo-shared_library", []string{"foo-shared_library.xml"}},
335 {"foo-no_shared_library", nil},
336 }
337 for _, tc := range testCases {
338 mod := ctx.ModuleForTests(tc.moduleName, "android_common").Module()
339 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
340 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
341 if !reflect.DeepEqual(tc.expected, actual) {
342 t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
343 }
344 }
345}