blob: 218a16a89f8880710c8f7e254422031a184f4a63 [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,
76 }
77
78 apex {
79 name: "myapex",
80 java_libs: ["myjavalib"],
81 uses_sdks: ["mysdk@1"],
82 key: "myapex.key",
83 certificate: ":myapex.cert",
84 }
85
86 apex {
87 name: "myapex2",
88 java_libs: ["myjavalib"],
89 uses_sdks: ["mysdk@2"],
90 key: "myapex.key",
91 certificate: ":myapex.cert",
92 }
93 `)
94
95 sdkMemberV1 := result.ctx.ModuleForTests("sdkmember_mysdk_1", "android_common_myapex").Rule("combineJar").Output
96 sdkMemberV2 := result.ctx.ModuleForTests("sdkmember_mysdk_2", "android_common_myapex2").Rule("combineJar").Output
97
98 javalibForMyApex := result.ctx.ModuleForTests("myjavalib", "android_common_myapex")
99 javalibForMyApex2 := result.ctx.ModuleForTests("myjavalib", "android_common_myapex2")
100
101 // Depending on the uses_sdks value, different libs are linked
102 ensureListContains(t, pathsToStrings(javalibForMyApex.Rule("javac").Implicits), sdkMemberV1.String())
103 ensureListContains(t, pathsToStrings(javalibForMyApex2.Rule("javac").Implicits), sdkMemberV2.String())
104}
105
Paul Duffina0dbf432019-12-05 11:25:53 +0000106func TestSnapshotWithJavaHeaderLibrary(t *testing.T) {
107 result := testSdkWithJava(t, `
108 sdk {
109 name: "mysdk",
110 java_header_libs: ["myjavalib"],
111 }
112
113 java_library {
114 name: "myjavalib",
115 srcs: ["Test.java"],
116 aidl: {
117 export_include_dirs: ["aidl"],
118 },
119 system_modules: "none",
120 sdk_version: "none",
121 compile_dex: true,
122 host_supported: true,
123 }
124 `)
125
Paul Duffin593b3c92019-12-05 14:31:48 +0000126 result.CheckSnapshot("mysdk", "android_common", "",
Paul Duffina0dbf432019-12-05 11:25:53 +0000127 checkAndroidBpContents(`
128// This is auto-generated. DO NOT EDIT.
129
130java_import {
131 name: "mysdk_myjavalib@current",
132 sdk_member_name: "myjavalib",
133 jars: ["java/myjavalib.jar"],
134}
135
136java_import {
137 name: "myjavalib",
138 prefer: false,
139 jars: ["java/myjavalib.jar"],
140}
141
142sdk_snapshot {
143 name: "mysdk@current",
144 java_header_libs: ["mysdk_myjavalib@current"],
145}
146
147`),
148 checkAllCopyRules(`
149.intermediates/myjavalib/android_common/turbine-combined/myjavalib.jar -> java/myjavalib.jar
150aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
151`),
152 )
153}
154
155func TestHostSnapshotWithJavaHeaderLibrary(t *testing.T) {
156 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
157 SkipIfNotLinux(t)
158
159 result := testSdkWithJava(t, `
160 sdk {
161 name: "mysdk",
162 device_supported: false,
163 host_supported: true,
164 java_header_libs: ["myjavalib"],
165 }
166
167 java_library {
168 name: "myjavalib",
169 device_supported: false,
170 host_supported: true,
171 srcs: ["Test.java"],
172 aidl: {
173 export_include_dirs: ["aidl"],
174 },
175 system_modules: "none",
176 sdk_version: "none",
177 compile_dex: true,
178 }
179 `)
180
Paul Duffin593b3c92019-12-05 14:31:48 +0000181 result.CheckSnapshot("mysdk", "linux_glibc_common", "",
Paul Duffina0dbf432019-12-05 11:25:53 +0000182 checkAndroidBpContents(`
183// This is auto-generated. DO NOT EDIT.
184
185java_import {
186 name: "mysdk_myjavalib@current",
187 sdk_member_name: "myjavalib",
188 device_supported: false,
189 host_supported: true,
190 jars: ["java/myjavalib.jar"],
191}
192
193java_import {
194 name: "myjavalib",
195 prefer: false,
196 device_supported: false,
197 host_supported: true,
198 jars: ["java/myjavalib.jar"],
199}
200
201sdk_snapshot {
202 name: "mysdk@current",
203 device_supported: false,
204 host_supported: true,
205 java_header_libs: ["mysdk_myjavalib@current"],
206}
207`),
208 checkAllCopyRules(`
209.intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar -> java/myjavalib.jar
210aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
211`),
212 )
213}
214
215func TestSnapshotWithJavaImplLibrary(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +0000216 result := testSdkWithJava(t, `
Paul Duffine6029182019-12-16 17:43:48 +0000217 module_exports {
218 name: "myexports",
Paul Duffina80fdec2019-12-03 15:25:00 +0000219 java_libs: ["myjavalib"],
220 }
221
222 java_library {
223 name: "myjavalib",
224 srcs: ["Test.java"],
225 aidl: {
226 export_include_dirs: ["aidl"],
227 },
228 system_modules: "none",
229 sdk_version: "none",
230 compile_dex: true,
231 host_supported: true,
232 }
233 `)
234
Paul Duffine6029182019-12-16 17:43:48 +0000235 result.CheckSnapshot("myexports", "android_common", "",
Paul Duffina80fdec2019-12-03 15:25:00 +0000236 checkAndroidBpContents(`
237// This is auto-generated. DO NOT EDIT.
238
239java_import {
Paul Duffine6029182019-12-16 17:43:48 +0000240 name: "myexports_myjavalib@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000241 sdk_member_name: "myjavalib",
242 jars: ["java/myjavalib.jar"],
243}
244
245java_import {
246 name: "myjavalib",
247 prefer: false,
248 jars: ["java/myjavalib.jar"],
249}
250
Paul Duffine6029182019-12-16 17:43:48 +0000251module_exports_snapshot {
252 name: "myexports@current",
253 java_libs: ["myexports_myjavalib@current"],
Paul Duffina80fdec2019-12-03 15:25:00 +0000254}
255
256`),
257 checkAllCopyRules(`
Paul Duffina0dbf432019-12-05 11:25:53 +0000258.intermediates/myjavalib/android_common/javac/myjavalib.jar -> java/myjavalib.jar
Paul Duffina80fdec2019-12-03 15:25:00 +0000259aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
260`),
261 )
262}
263
Paul Duffina0dbf432019-12-05 11:25:53 +0000264func TestHostSnapshotWithJavaImplLibrary(t *testing.T) {
Paul Duffina80fdec2019-12-03 15:25:00 +0000265 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
266 SkipIfNotLinux(t)
267
Paul Duffind835daa2019-11-30 17:49:09 +0000268 result := testSdkWithJava(t, `
Paul Duffine6029182019-12-16 17:43:48 +0000269 module_exports {
270 name: "myexports",
Paul Duffina80fdec2019-12-03 15:25:00 +0000271 device_supported: false,
272 host_supported: true,
273 java_libs: ["myjavalib"],
274 }
275
276 java_library {
277 name: "myjavalib",
278 device_supported: false,
279 host_supported: true,
280 srcs: ["Test.java"],
281 aidl: {
282 export_include_dirs: ["aidl"],
283 },
284 system_modules: "none",
285 sdk_version: "none",
286 compile_dex: true,
287 }
288 `)
289
Paul Duffine6029182019-12-16 17:43:48 +0000290 result.CheckSnapshot("myexports", "linux_glibc_common", "",
Paul Duffina80fdec2019-12-03 15:25:00 +0000291 checkAndroidBpContents(`
292// This is auto-generated. DO NOT EDIT.
293
294java_import {
Paul Duffine6029182019-12-16 17:43:48 +0000295 name: "myexports_myjavalib@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000296 sdk_member_name: "myjavalib",
297 device_supported: false,
298 host_supported: true,
299 jars: ["java/myjavalib.jar"],
300}
301
302java_import {
303 name: "myjavalib",
304 prefer: false,
305 device_supported: false,
306 host_supported: true,
307 jars: ["java/myjavalib.jar"],
308}
309
Paul Duffine6029182019-12-16 17:43:48 +0000310module_exports_snapshot {
311 name: "myexports@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000312 device_supported: false,
313 host_supported: true,
Paul Duffine6029182019-12-16 17:43:48 +0000314 java_libs: ["myexports_myjavalib@current"],
Paul Duffina80fdec2019-12-03 15:25:00 +0000315}
316`),
317 checkAllCopyRules(`
318.intermediates/myjavalib/linux_glibc_common/javac/myjavalib.jar -> java/myjavalib.jar
319aidl/foo/bar/Test.aidl -> aidl/aidl/foo/bar/Test.aidl
320`),
321 )
322}
323
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000324func TestSnapshotWithJavaTest(t *testing.T) {
325 result := testSdkWithJava(t, `
326 module_exports {
327 name: "myexports",
328 java_tests: ["myjavatests"],
329 }
330
331 java_test {
332 name: "myjavatests",
333 srcs: ["Test.java"],
334 system_modules: "none",
335 sdk_version: "none",
336 compile_dex: true,
337 host_supported: true,
338 }
339 `)
340
341 result.CheckSnapshot("myexports", "android_common", "",
342 checkAndroidBpContents(`
343// This is auto-generated. DO NOT EDIT.
344
345java_test_import {
346 name: "myexports_myjavatests@current",
347 sdk_member_name: "myjavatests",
348 jars: ["java/myjavatests.jar"],
349 test_config: "java/myjavatests-AndroidTest.xml",
350}
351
352java_test_import {
353 name: "myjavatests",
354 prefer: false,
355 jars: ["java/myjavatests.jar"],
356 test_config: "java/myjavatests-AndroidTest.xml",
357}
358
359module_exports_snapshot {
360 name: "myexports@current",
361 java_tests: ["myexports_myjavatests@current"],
362}
363`),
364 checkAllCopyRules(`
365.intermediates/myjavatests/android_common/javac/myjavatests.jar -> java/myjavatests.jar
366.intermediates/myjavatests/android_common/myjavatests.config -> java/myjavatests-AndroidTest.xml
367`),
368 )
369}
370
371func TestHostSnapshotWithJavaTest(t *testing.T) {
372 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
373 SkipIfNotLinux(t)
374
375 result := testSdkWithJava(t, `
376 module_exports {
377 name: "myexports",
378 device_supported: false,
379 host_supported: true,
380 java_tests: ["myjavatests"],
381 }
382
383 java_test {
384 name: "myjavatests",
385 device_supported: false,
386 host_supported: true,
387 srcs: ["Test.java"],
388 system_modules: "none",
389 sdk_version: "none",
390 compile_dex: true,
391 }
392 `)
393
394 result.CheckSnapshot("myexports", "linux_glibc_common", "",
395 checkAndroidBpContents(`
396// This is auto-generated. DO NOT EDIT.
397
398java_test_import {
399 name: "myexports_myjavatests@current",
400 sdk_member_name: "myjavatests",
401 device_supported: false,
402 host_supported: true,
403 jars: ["java/myjavatests.jar"],
404 test_config: "java/myjavatests-AndroidTest.xml",
405}
406
407java_test_import {
408 name: "myjavatests",
409 prefer: false,
410 device_supported: false,
411 host_supported: true,
412 jars: ["java/myjavatests.jar"],
413 test_config: "java/myjavatests-AndroidTest.xml",
414}
415
416module_exports_snapshot {
417 name: "myexports@current",
418 device_supported: false,
419 host_supported: true,
420 java_tests: ["myexports_myjavatests@current"],
421}
422`),
423 checkAllCopyRules(`
424.intermediates/myjavatests/linux_glibc_common/javac/myjavatests.jar -> java/myjavatests.jar
425.intermediates/myjavatests/linux_glibc_common/myjavatests.config -> java/myjavatests-AndroidTest.xml
426`),
427 )
428}
429
Paul Duffind835daa2019-11-30 17:49:09 +0000430func testSdkWithDroidstubs(t *testing.T, bp string) *testSdkResult {
431 t.Helper()
432
433 fs := map[string][]byte{
434 "foo/bar/Foo.java": nil,
435 "stubs-sources/foo/bar/Foo.java": nil,
436 }
437 return testSdkWithFs(t, bp, fs)
438}
439
Paul Duffina80fdec2019-12-03 15:25:00 +0000440// Note: This test does not verify that a droidstubs can be referenced, either
441// directly or indirectly from an APEX as droidstubs can never be a part of an
442// apex.
443func TestBasicSdkWithDroidstubs(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +0000444 testSdkWithDroidstubs(t, `
Paul Duffina80fdec2019-12-03 15:25:00 +0000445 sdk {
446 name: "mysdk",
447 stubs_sources: ["mystub"],
448 }
449 sdk_snapshot {
450 name: "mysdk@10",
451 stubs_sources: ["mystub_mysdk@10"],
452 }
453 prebuilt_stubs_sources {
454 name: "mystub_mysdk@10",
455 sdk_member_name: "mystub",
456 srcs: ["stubs-sources/foo/bar/Foo.java"],
457 }
458 droidstubs {
459 name: "mystub",
460 srcs: ["foo/bar/Foo.java"],
461 sdk_version: "none",
462 system_modules: "none",
463 }
464 java_library {
465 name: "myjavalib",
466 srcs: [":mystub"],
467 sdk_version: "none",
468 system_modules: "none",
469 }
470 `)
471}
472
473func TestSnapshotWithDroidstubs(t *testing.T) {
Paul Duffind835daa2019-11-30 17:49:09 +0000474 result := testSdkWithDroidstubs(t, `
Paul Duffine6029182019-12-16 17:43:48 +0000475 module_exports {
476 name: "myexports",
Paul Duffina80fdec2019-12-03 15:25:00 +0000477 stubs_sources: ["myjavaapistubs"],
478 }
479
480 droidstubs {
481 name: "myjavaapistubs",
482 srcs: ["foo/bar/Foo.java"],
483 system_modules: "none",
484 sdk_version: "none",
485 }
486 `)
487
Paul Duffine6029182019-12-16 17:43:48 +0000488 result.CheckSnapshot("myexports", "android_common", "",
Paul Duffina80fdec2019-12-03 15:25:00 +0000489 checkAndroidBpContents(`
490// This is auto-generated. DO NOT EDIT.
491
492prebuilt_stubs_sources {
Paul Duffine6029182019-12-16 17:43:48 +0000493 name: "myexports_myjavaapistubs@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000494 sdk_member_name: "myjavaapistubs",
495 srcs: ["java/myjavaapistubs_stubs_sources"],
496}
497
498prebuilt_stubs_sources {
499 name: "myjavaapistubs",
500 prefer: false,
501 srcs: ["java/myjavaapistubs_stubs_sources"],
502}
503
Paul Duffine6029182019-12-16 17:43:48 +0000504module_exports_snapshot {
505 name: "myexports@current",
506 stubs_sources: ["myexports_myjavaapistubs@current"],
Paul Duffina80fdec2019-12-03 15:25:00 +0000507}
508
509`),
510 checkAllCopyRules(""),
Paul Duffine6029182019-12-16 17:43:48 +0000511 checkMergeZip(".intermediates/myexports/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"),
Paul Duffina80fdec2019-12-03 15:25:00 +0000512 )
513}
514
515func TestHostSnapshotWithDroidstubs(t *testing.T) {
516 // b/145598135 - Generating host snapshots for anything other than linux is not supported.
517 SkipIfNotLinux(t)
518
Paul Duffind835daa2019-11-30 17:49:09 +0000519 result := testSdkWithDroidstubs(t, `
Paul Duffine6029182019-12-16 17:43:48 +0000520 module_exports {
521 name: "myexports",
Paul Duffina80fdec2019-12-03 15:25:00 +0000522 device_supported: false,
523 host_supported: true,
524 stubs_sources: ["myjavaapistubs"],
525 }
526
527 droidstubs {
528 name: "myjavaapistubs",
529 device_supported: false,
530 host_supported: true,
531 srcs: ["foo/bar/Foo.java"],
532 system_modules: "none",
533 sdk_version: "none",
534 }
535 `)
536
Paul Duffine6029182019-12-16 17:43:48 +0000537 result.CheckSnapshot("myexports", "linux_glibc_common", "",
Paul Duffina80fdec2019-12-03 15:25:00 +0000538 checkAndroidBpContents(`
539// This is auto-generated. DO NOT EDIT.
540
541prebuilt_stubs_sources {
Paul Duffine6029182019-12-16 17:43:48 +0000542 name: "myexports_myjavaapistubs@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000543 sdk_member_name: "myjavaapistubs",
544 device_supported: false,
545 host_supported: true,
546 srcs: ["java/myjavaapistubs_stubs_sources"],
547}
548
549prebuilt_stubs_sources {
550 name: "myjavaapistubs",
551 prefer: false,
552 device_supported: false,
553 host_supported: true,
554 srcs: ["java/myjavaapistubs_stubs_sources"],
555}
556
Paul Duffine6029182019-12-16 17:43:48 +0000557module_exports_snapshot {
558 name: "myexports@current",
Paul Duffina80fdec2019-12-03 15:25:00 +0000559 device_supported: false,
560 host_supported: true,
Paul Duffine6029182019-12-16 17:43:48 +0000561 stubs_sources: ["myexports_myjavaapistubs@current"],
Paul Duffina80fdec2019-12-03 15:25:00 +0000562}
563`),
564 checkAllCopyRules(""),
Paul Duffine6029182019-12-16 17:43:48 +0000565 checkMergeZip(".intermediates/myexports/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"),
Paul Duffina80fdec2019-12-03 15:25:00 +0000566 )
567}