blob: 692c2051e71c3b2394eb70ad023629788a10b58d [file] [log] [blame]
Paul Duffina80fdec2019-12-03 15:25:00 +00001// Copyright (C) 2019 The Android Open Source Project
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 sdk
16
17import (
18 "testing"
19)
20
Paul Duffind835daa2019-11-30 17:49:09 +000021func testSdkWithJava(t *testing.T, bp string) *testSdkResult {
22 t.Helper()
23
24 fs := map[string][]byte{
25 "Test.java": nil,
26 "aidl/foo/bar/Test.aidl": nil,
27 }
28 return testSdkWithFs(t, bp, fs)
29}
30
Paul Duffina80fdec2019-12-03 15:25:00 +000031// Contains tests for SDK members provided by the java package.
32
33func TestBasicSdkWithJavaLibrary(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +000034 result := testSdkWithJava(t, `
Paul Duffina80fdec2019-12-03 15:25:00 +000035 sdk {
36 name: "mysdk",
Paul Duffina0dbf432019-12-05 11:25:53 +000037 java_header_libs: ["myjavalib"],
Paul Duffina80fdec2019-12-03 15:25:00 +000038 }
39
40 sdk_snapshot {
41 name: "mysdk@1",
Paul Duffina0dbf432019-12-05 11:25:53 +000042 java_header_libs: ["sdkmember_mysdk_1"],
Paul Duffina80fdec2019-12-03 15:25:00 +000043 }
44
45 sdk_snapshot {
46 name: "mysdk@2",
Paul Duffina0dbf432019-12-05 11:25:53 +000047 java_header_libs: ["sdkmember_mysdk_2"],
Paul Duffina80fdec2019-12-03 15:25:00 +000048 }
49
50 java_import {
51 name: "sdkmember",
52 prefer: false,
53 host_supported: true,
54 }
55
56 java_import {
57 name: "sdkmember_mysdk_1",
58 sdk_member_name: "sdkmember",
59 host_supported: true,
60 }
61
62 java_import {
63 name: "sdkmember_mysdk_2",
64 sdk_member_name: "sdkmember",
65 host_supported: true,
66 }
67
68 java_library {
69 name: "myjavalib",
70 srcs: ["Test.java"],
71 libs: ["sdkmember"],
72 system_modules: "none",
73 sdk_version: "none",
74 compile_dex: true,
75 host_supported: true,
Anton Hanssoneec79eb2020-01-10 15:12:39 +000076 apex_available: [
77 "myapex",
78 "myapex2",
79 ],
Paul Duffina80fdec2019-12-03 15:25:00 +000080 }
81
82 apex {
83 name: "myapex",
84 java_libs: ["myjavalib"],
85 uses_sdks: ["mysdk@1"],
86 key: "myapex.key",
87 certificate: ":myapex.cert",
88 }
89
90 apex {
91 name: "myapex2",
92 java_libs: ["myjavalib"],
93 uses_sdks: ["mysdk@2"],
94 key: "myapex.key",
95 certificate: ":myapex.cert",
96 }
97 `)
98
99 sdkMemberV1 := result.ctx.ModuleForTests("sdkmember_mysdk_1", "android_common_myapex").Rule("combineJar").Output
100 sdkMemberV2 := result.ctx.ModuleForTests("sdkmember_mysdk_2", "android_common_myapex2").Rule("combineJar").Output
101
102 javalibForMyApex := result.ctx.ModuleForTests("myjavalib", "android_common_myapex")
103 javalibForMyApex2 := result.ctx.ModuleForTests("myjavalib", "android_common_myapex2")
104
105 // Depending on the uses_sdks value, different libs are linked
106 ensureListContains(t, pathsToStrings(javalibForMyApex.Rule("javac").Implicits), sdkMemberV1.String())
107 ensureListContains(t, pathsToStrings(javalibForMyApex2.Rule("javac").Implicits), sdkMemberV2.String())
108}
109
Paul Duffina0dbf432019-12-05 11:25:53 +0000110func TestSnapshotWithJavaHeaderLibrary(t *testing.T) {
111 result := testSdkWithJava(t, `
112 sdk {
113 name: "mysdk",
114 java_header_libs: ["myjavalib"],
115 }
116
117 java_library {
118 name: "myjavalib",
119 srcs: ["Test.java"],
120 aidl: {
121 export_include_dirs: ["aidl"],
122 },
123 system_modules: "none",
124 sdk_version: "none",
125 compile_dex: true,
126 host_supported: true,
127 }
128 `)
129
Paul Duffin593b3c92019-12-05 14:31:48 +0000130 result.CheckSnapshot("mysdk", "android_common", "",
Paul Duffina0dbf432019-12-05 11:25:53 +0000131 checkAndroidBpContents(`
132// This is auto-generated. DO NOT EDIT.
133
134java_import {
135 name: "mysdk_myjavalib@current",
136 sdk_member_name: "myjavalib",
137 jars: ["java/myjavalib.jar"],
138}
139
140java_import {
141 name: "myjavalib",
142 prefer: false,
143 jars: ["java/myjavalib.jar"],
144}
145
146sdk_snapshot {
147 name: "mysdk@current",
148 java_header_libs: ["mysdk_myjavalib@current"],
149}
150
151`),
152 checkAllCopyRules(`
153.intermediates/myjavalib/android_common/turbine-combined/myjavalib.jar -> java/myjavalib.jar
154aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
155`),
156 )
157}
158
159func TestHostSnapshotWithJavaHeaderLibrary(t *testing.T) {
160 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
161 SkipIfNotLinux(t)
162
163 result := testSdkWithJava(t, `
164 sdk {
165 name: "mysdk",
166 device_supported: false,
167 host_supported: true,
168 java_header_libs: ["myjavalib"],
169 }
170
171 java_library {
172 name: "myjavalib",
173 device_supported: false,
174 host_supported: true,
175 srcs: ["Test.java"],
176 aidl: {
177 export_include_dirs: ["aidl"],
178 },
179 system_modules: "none",
180 sdk_version: "none",
181 compile_dex: true,
182 }
183 `)
184
Paul Duffin593b3c92019-12-05 14:31:48 +0000185 result.CheckSnapshot("mysdk", "linux_glibc_common", "",
Paul Duffina0dbf432019-12-05 11:25:53 +0000186 checkAndroidBpContents(`
187// This is auto-generated. DO NOT EDIT.
188
189java_import {
190 name: "mysdk_myjavalib@current",
191 sdk_member_name: "myjavalib",
192 device_supported: false,
193 host_supported: true,
194 jars: ["java/myjavalib.jar"],
195}
196
197java_import {
198 name: "myjavalib",
199 prefer: false,
200 device_supported: false,
201 host_supported: true,
202 jars: ["java/myjavalib.jar"],
203}
204
205sdk_snapshot {
206 name: "mysdk@current",
207 device_supported: false,
208 host_supported: true,
209 java_header_libs: ["mysdk_myjavalib@current"],
210}
211`),
212 checkAllCopyRules(`
213.intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar -> java/myjavalib.jar
214aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
215`),
216 )
217}
218
219func TestSnapshotWithJavaImplLibrary(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +0000220 result := testSdkWithJava(t, `
Paul Duffine6029182019-12-16 17:43:48 +0000221 module_exports {
222 name: "myexports",
Paul Duffina80fdec2019-12-03 15:25:00 +0000223 java_libs: ["myjavalib"],
224 }
225
226 java_library {
227 name: "myjavalib",
228 srcs: ["Test.java"],
229 aidl: {
230 export_include_dirs: ["aidl"],
231 },
232 system_modules: "none",
233 sdk_version: "none",
234 compile_dex: true,
235 host_supported: true,
236 }
237 `)
238
Paul Duffine6029182019-12-16 17:43:48 +0000239 result.CheckSnapshot("myexports", "android_common", "",
Paul Duffina80fdec2019-12-03 15:25:00 +0000240 checkAndroidBpContents(`
241// This is auto-generated. DO NOT EDIT.
242
243java_import {
Paul Duffine6029182019-12-16 17:43:48 +0000244 name: "myexports_myjavalib@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000245 sdk_member_name: "myjavalib",
246 jars: ["java/myjavalib.jar"],
247}
248
249java_import {
250 name: "myjavalib",
251 prefer: false,
252 jars: ["java/myjavalib.jar"],
253}
254
Paul Duffine6029182019-12-16 17:43:48 +0000255module_exports_snapshot {
256 name: "myexports@current",
257 java_libs: ["myexports_myjavalib@current"],
Paul Duffina80fdec2019-12-03 15:25:00 +0000258}
259
260`),
261 checkAllCopyRules(`
Paul Duffina0dbf432019-12-05 11:25:53 +0000262.intermediates/myjavalib/android_common/javac/myjavalib.jar -> java/myjavalib.jar
Paul Duffina80fdec2019-12-03 15:25:00 +0000263aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
264`),
265 )
266}
267
Paul Duffina0dbf432019-12-05 11:25:53 +0000268func TestHostSnapshotWithJavaImplLibrary(t *testing.T) {
Paul Duffina80fdec2019-12-03 15:25:00 +0000269 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
270 SkipIfNotLinux(t)
271
Paul Duffind835daa2019-11-30 17:49:09 +0000272 result := testSdkWithJava(t, `
Paul Duffine6029182019-12-16 17:43:48 +0000273 module_exports {
274 name: "myexports",
Paul Duffina80fdec2019-12-03 15:25:00 +0000275 device_supported: false,
276 host_supported: true,
277 java_libs: ["myjavalib"],
278 }
279
280 java_library {
281 name: "myjavalib",
282 device_supported: false,
283 host_supported: true,
284 srcs: ["Test.java"],
285 aidl: {
286 export_include_dirs: ["aidl"],
287 },
288 system_modules: "none",
289 sdk_version: "none",
290 compile_dex: true,
291 }
292 `)
293
Paul Duffine6029182019-12-16 17:43:48 +0000294 result.CheckSnapshot("myexports", "linux_glibc_common", "",
Paul Duffina80fdec2019-12-03 15:25:00 +0000295 checkAndroidBpContents(`
296// This is auto-generated. DO NOT EDIT.
297
298java_import {
Paul Duffine6029182019-12-16 17:43:48 +0000299 name: "myexports_myjavalib@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000300 sdk_member_name: "myjavalib",
301 device_supported: false,
302 host_supported: true,
303 jars: ["java/myjavalib.jar"],
304}
305
306java_import {
307 name: "myjavalib",
308 prefer: false,
309 device_supported: false,
310 host_supported: true,
311 jars: ["java/myjavalib.jar"],
312}
313
Paul Duffine6029182019-12-16 17:43:48 +0000314module_exports_snapshot {
315 name: "myexports@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000316 device_supported: false,
317 host_supported: true,
Paul Duffine6029182019-12-16 17:43:48 +0000318 java_libs: ["myexports_myjavalib@current"],
Paul Duffina80fdec2019-12-03 15:25:00 +0000319}
320`),
321 checkAllCopyRules(`
322.intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar -> java/myjavalib.jar
323aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
324`),
325 )
326}
327
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000328func TestSnapshotWithJavaTest(t *testing.T) {
329 result := testSdkWithJava(t, `
330 module_exports {
331 name: "myexports",
332 java_tests: ["myjavatests"],
333 }
334
335 java_test {
336 name: "myjavatests",
337 srcs: ["Test.java"],
338 system_modules: "none",
339 sdk_version: "none",
340 compile_dex: true,
341 host_supported: true,
342 }
343 `)
344
345 result.CheckSnapshot("myexports", "android_common", "",
346 checkAndroidBpContents(`
347// This is auto-generated. DO NOT EDIT.
348
349java_test_import {
350 name: "myexports_myjavatests@current",
351 sdk_member_name: "myjavatests",
352 jars: ["java/myjavatests.jar"],
353 test_config: "java/myjavatests-AndroidTest.xml",
354}
355
356java_test_import {
357 name: "myjavatests",
358 prefer: false,
359 jars: ["java/myjavatests.jar"],
360 test_config: "java/myjavatests-AndroidTest.xml",
361}
362
363module_exports_snapshot {
364 name: "myexports@current",
365 java_tests: ["myexports_myjavatests@current"],
366}
367`),
368 checkAllCopyRules(`
369.intermediates/myjavatests/android_common/javac/myjavatests.jar -> java/myjavatests.jar
370.intermediates/myjavatests/android_common/myjavatests.config -> java/myjavatests-AndroidTest.xml
371`),
372 )
373}
374
375func TestHostSnapshotWithJavaTest(t *testing.T) {
376 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
377 SkipIfNotLinux(t)
378
379 result := testSdkWithJava(t, `
380 module_exports {
381 name: "myexports",
382 device_supported: false,
383 host_supported: true,
384 java_tests: ["myjavatests"],
385 }
386
387 java_test {
388 name: "myjavatests",
389 device_supported: false,
390 host_supported: true,
391 srcs: ["Test.java"],
392 system_modules: "none",
393 sdk_version: "none",
394 compile_dex: true,
395 }
396 `)
397
398 result.CheckSnapshot("myexports", "linux_glibc_common", "",
399 checkAndroidBpContents(`
400// This is auto-generated. DO NOT EDIT.
401
402java_test_import {
403 name: "myexports_myjavatests@current",
404 sdk_member_name: "myjavatests",
405 device_supported: false,
406 host_supported: true,
407 jars: ["java/myjavatests.jar"],
408 test_config: "java/myjavatests-AndroidTest.xml",
409}
410
411java_test_import {
412 name: "myjavatests",
413 prefer: false,
414 device_supported: false,
415 host_supported: true,
416 jars: ["java/myjavatests.jar"],
417 test_config: "java/myjavatests-AndroidTest.xml",
418}
419
420module_exports_snapshot {
421 name: "myexports@current",
422 device_supported: false,
423 host_supported: true,
424 java_tests: ["myexports_myjavatests@current"],
425}
426`),
427 checkAllCopyRules(`
428.intermediates/myjavatests/linux_glibc_common/javac/myjavatests.jar -> java/myjavatests.jar
429.intermediates/myjavatests/linux_glibc_common/myjavatests.config -> java/myjavatests-AndroidTest.xml
430`),
431 )
432}
433
Paul Duffind835daa2019-11-30 17:49:09 +0000434func testSdkWithDroidstubs(t *testing.T, bp string) *testSdkResult {
435 t.Helper()
436
437 fs := map[string][]byte{
438 "foo/bar/Foo.java": nil,
439 "stubs-sources/foo/bar/Foo.java": nil,
440 }
441 return testSdkWithFs(t, bp, fs)
442}
443
Paul Duffina80fdec2019-12-03 15:25:00 +0000444// Note: This test does not verify that a droidstubs can be referenced, either
445// directly or indirectly from an APEX as droidstubs can never be a part of an
446// apex.
447func TestBasicSdkWithDroidstubs(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +0000448 testSdkWithDroidstubs(t, `
Paul Duffina80fdec2019-12-03 15:25:00 +0000449 sdk {
450 name: "mysdk",
451 stubs_sources: ["mystub"],
452 }
453 sdk_snapshot {
454 name: "mysdk@10",
455 stubs_sources: ["mystub_mysdk@10"],
456 }
457 prebuilt_stubs_sources {
458 name: "mystub_mysdk@10",
459 sdk_member_name: "mystub",
460 srcs: ["stubs-sources/foo/bar/Foo.java"],
461 }
462 droidstubs {
463 name: "mystub",
464 srcs: ["foo/bar/Foo.java"],
465 sdk_version: "none",
466 system_modules: "none",
467 }
468 java_library {
469 name: "myjavalib",
470 srcs: [":mystub"],
471 sdk_version: "none",
472 system_modules: "none",
473 }
474 `)
475}
476
477func TestSnapshotWithDroidstubs(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +0000478 result := testSdkWithDroidstubs(t, `
Paul Duffine6029182019-12-16 17:43:48 +0000479 module_exports {
480 name: "myexports",
Paul Duffina80fdec2019-12-03 15:25:00 +0000481 stubs_sources: ["myjavaapistubs"],
482 }
483
484 droidstubs {
485 name: "myjavaapistubs",
486 srcs: ["foo/bar/Foo.java"],
487 system_modules: "none",
488 sdk_version: "none",
489 }
490 `)
491
Paul Duffine6029182019-12-16 17:43:48 +0000492 result.CheckSnapshot("myexports", "android_common", "",
Paul Duffina80fdec2019-12-03 15:25:00 +0000493 checkAndroidBpContents(`
494// This is auto-generated. DO NOT EDIT.
495
496prebuilt_stubs_sources {
Paul Duffine6029182019-12-16 17:43:48 +0000497 name: "myexports_myjavaapistubs@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000498 sdk_member_name: "myjavaapistubs",
499 srcs: ["java/myjavaapistubs_stubs_sources"],
500}
501
502prebuilt_stubs_sources {
503 name: "myjavaapistubs",
504 prefer: false,
505 srcs: ["java/myjavaapistubs_stubs_sources"],
506}
507
Paul Duffine6029182019-12-16 17:43:48 +0000508module_exports_snapshot {
509 name: "myexports@current",
510 stubs_sources: ["myexports_myjavaapistubs@current"],
Paul Duffina80fdec2019-12-03 15:25:00 +0000511}
512
513`),
514 checkAllCopyRules(""),
Paul Duffine6029182019-12-16 17:43:48 +0000515 checkMergeZip(".intermediates/myexports/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"),
Paul Duffina80fdec2019-12-03 15:25:00 +0000516 )
517}
518
519func TestHostSnapshotWithDroidstubs(t *testing.T) {
520 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
521 SkipIfNotLinux(t)
522
Paul Duffind835daa2019-11-30 17:49:09 +0000523 result := testSdkWithDroidstubs(t, `
Paul Duffine6029182019-12-16 17:43:48 +0000524 module_exports {
525 name: "myexports",
Paul Duffina80fdec2019-12-03 15:25:00 +0000526 device_supported: false,
527 host_supported: true,
528 stubs_sources: ["myjavaapistubs"],
529 }
530
531 droidstubs {
532 name: "myjavaapistubs",
533 device_supported: false,
534 host_supported: true,
535 srcs: ["foo/bar/Foo.java"],
536 system_modules: "none",
537 sdk_version: "none",
538 }
539 `)
540
Paul Duffine6029182019-12-16 17:43:48 +0000541 result.CheckSnapshot("myexports", "linux_glibc_common", "",
Paul Duffina80fdec2019-12-03 15:25:00 +0000542 checkAndroidBpContents(`
543// This is auto-generated. DO NOT EDIT.
544
545prebuilt_stubs_sources {
Paul Duffine6029182019-12-16 17:43:48 +0000546 name: "myexports_myjavaapistubs@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000547 sdk_member_name: "myjavaapistubs",
548 device_supported: false,
549 host_supported: true,
550 srcs: ["java/myjavaapistubs_stubs_sources"],
551}
552
553prebuilt_stubs_sources {
554 name: "myjavaapistubs",
555 prefer: false,
556 device_supported: false,
557 host_supported: true,
558 srcs: ["java/myjavaapistubs_stubs_sources"],
559}
560
Paul Duffine6029182019-12-16 17:43:48 +0000561module_exports_snapshot {
562 name: "myexports@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000563 device_supported: false,
564 host_supported: true,
Paul Duffine6029182019-12-16 17:43:48 +0000565 stubs_sources: ["myexports_myjavaapistubs@current"],
Paul Duffina80fdec2019-12-03 15:25:00 +0000566}
567`),
568 checkAllCopyRules(""),
Paul Duffine6029182019-12-16 17:43:48 +0000569 checkMergeZip(".intermediates/myexports/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"),
Paul Duffina80fdec2019-12-03 15:25:00 +0000570 )
571}