blob: 0c7b79853b622bdc60eadf8a8d82e06152cfd3c9 [file] [log] [blame]
Colin Cross76069a32021-06-01 13:18:08 -07001// Copyright 2021 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 (
18 "android/soong/android"
19 "fmt"
20 "path/filepath"
21 "regexp"
22 "testing"
23
24 "github.com/google/blueprint/proptools"
25)
26
27func TestJavaSdkLibrary(t *testing.T) {
28 result := android.GroupFixturePreparers(
29 prepareForJavaTest,
30 PrepareForTestWithJavaSdkLibraryFiles,
31 FixtureWithPrebuiltApis(map[string][]string{
32 "28": {"foo"},
33 "29": {"foo"},
34 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
35 }),
36 ).RunTestWithBp(t, `
37 droiddoc_exported_dir {
38 name: "droiddoc-templates-sdk",
39 path: ".",
40 }
41 java_sdk_library {
42 name: "foo",
43 srcs: ["a.java", "b.java"],
44 api_packages: ["foo"],
45 }
46 java_sdk_library {
47 name: "bar",
48 srcs: ["a.java", "b.java"],
49 api_packages: ["bar"],
50 }
51 java_library {
52 name: "baz",
53 srcs: ["c.java"],
54 libs: ["foo", "bar.stubs"],
55 sdk_version: "system_current",
56 }
57 java_sdk_library {
58 name: "barney",
59 srcs: ["c.java"],
60 api_only: true,
61 }
62 java_sdk_library {
63 name: "betty",
64 srcs: ["c.java"],
65 shared_library: false,
66 }
67 java_sdk_library_import {
68 name: "quuz",
69 public: {
70 jars: ["c.jar"],
71 },
72 }
73 java_sdk_library_import {
74 name: "fred",
75 public: {
76 jars: ["b.jar"],
77 },
78 }
79 java_sdk_library_import {
80 name: "wilma",
81 public: {
82 jars: ["b.jar"],
83 },
84 shared_library: false,
85 }
86 java_library {
87 name: "qux",
88 srcs: ["c.java"],
89 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
90 sdk_version: "system_current",
91 }
92 java_library {
93 name: "baz-test",
94 srcs: ["c.java"],
95 libs: ["foo"],
96 sdk_version: "test_current",
97 }
98 java_library {
99 name: "baz-29",
100 srcs: ["c.java"],
101 libs: ["foo"],
102 sdk_version: "system_29",
103 }
104 java_library {
105 name: "baz-module-30",
106 srcs: ["c.java"],
107 libs: ["foo"],
108 sdk_version: "module_30",
109 }
110 `)
111
112 // check the existence of the internal modules
Paul Duffina1aa7382021-04-29 21:50:40 +0100113 foo := result.ModuleForTests("foo", "android_common")
Colin Cross76069a32021-06-01 13:18:08 -0700114 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
115 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
116 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
117 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
118 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
119 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
120 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
121 result.ModuleForTests("foo.api.public.28", "")
122 result.ModuleForTests("foo.api.system.28", "")
123 result.ModuleForTests("foo.api.test.28", "")
124
Paul Duffina1aa7382021-04-29 21:50:40 +0100125 exportedComponentsInfo := result.ModuleProvider(foo.Module(), ExportedComponentsInfoProvider).(ExportedComponentsInfo)
126 expectedFooExportedComponents := []string{
127 "foo.stubs",
128 "foo.stubs.source",
129 "foo.stubs.source.system",
130 "foo.stubs.source.test",
131 "foo.stubs.system",
132 "foo.stubs.test",
133 }
134 android.AssertArrayString(t, "foo exported components", expectedFooExportedComponents, exportedComponentsInfo.Components)
135
Colin Cross76069a32021-06-01 13:18:08 -0700136 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
137 // tests if baz is actually linked to the stubs lib
138 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
139 // ... and not to the impl lib
140 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
141 // test if baz is not linked to the system variant of foo
142 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
143
144 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
145 // tests if baz-test is actually linked to the test stubs lib
146 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
147
148 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
149 // tests if baz-29 is actually linked to the system 29 stubs lib
150 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
151
152 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
153 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
154 android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar")
155
156 // test if baz has exported SDK lib names foo and bar to qux
157 qux := result.ModuleForTests("qux", "android_common")
158 if quxLib, ok := qux.Module().(*Library); ok {
159 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
160 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
161 }
162}
163
164func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
165 result := android.GroupFixturePreparers(
166 prepareForJavaTest,
167 PrepareForTestWithJavaSdkLibraryFiles,
168 FixtureWithLastReleaseApis("sdklib"),
169 ).RunTestWithBp(t, `
170 java_sdk_library {
171 name: "sdklib",
172 srcs: ["a.java"],
173 libs: ["lib"],
174 static_libs: ["static-lib"],
175 impl_only_libs: ["impl-only-lib"],
176 stub_only_libs: ["stub-only-lib"],
177 stub_only_static_libs: ["stub-only-static-lib"],
178 }
179 java_defaults {
180 name: "defaults",
181 srcs: ["a.java"],
182 sdk_version: "current",
183 }
184 java_library { name: "lib", defaults: ["defaults"] }
185 java_library { name: "static-lib", defaults: ["defaults"] }
186 java_library { name: "impl-only-lib", defaults: ["defaults"] }
187 java_library { name: "stub-only-lib", defaults: ["defaults"] }
188 java_library { name: "stub-only-static-lib", defaults: ["defaults"] }
189 `)
190 var expectations = []struct {
191 lib string
192 on_impl_classpath bool
193 on_stub_classpath bool
194 in_impl_combined bool
195 in_stub_combined bool
196 }{
197 {lib: "lib", on_impl_classpath: true},
198 {lib: "static-lib", in_impl_combined: true},
199 {lib: "impl-only-lib", on_impl_classpath: true},
200 {lib: "stub-only-lib", on_stub_classpath: true},
201 {lib: "stub-only-static-lib", in_stub_combined: true},
202 }
203 verify := func(sdklib, dep string, cp, combined bool) {
204 sdklibCp := result.ModuleForTests(sdklib, "android_common").Rule("javac").Args["classpath"]
205 expected := cp || combined // Every combined jar is also on the classpath.
206 android.AssertStringContainsEquals(t, "bad classpath for "+sdklib, sdklibCp, "/"+dep+".jar", expected)
207
208 combineJarInputs := result.ModuleForTests(sdklib, "android_common").Rule("combineJar").Inputs.Strings()
209 depPath := filepath.Join("out", "soong", ".intermediates", dep, "android_common", "turbine-combined", dep+".jar")
210 android.AssertStringListContainsEquals(t, "bad combined inputs for "+sdklib, combineJarInputs, depPath, combined)
211 }
212 for _, expectation := range expectations {
213 verify("sdklib", expectation.lib, expectation.on_impl_classpath, expectation.in_impl_combined)
214 verify("sdklib.impl", expectation.lib, expectation.on_impl_classpath, expectation.in_impl_combined)
215
216 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
217 verify(stubName, expectation.lib, expectation.on_stub_classpath, expectation.in_stub_combined)
218 }
219}
220
221func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
222 result := android.GroupFixturePreparers(
223 prepareForJavaTest,
224 PrepareForTestWithJavaSdkLibraryFiles,
225 FixtureWithLastReleaseApis("foo"),
226 ).RunTestWithBp(t, `
227 java_sdk_library {
228 name: "foo",
229 srcs: ["a.java"],
230 api_only: true,
231 public: {
232 enabled: true,
233 },
234 }
235
236 java_library {
237 name: "bar",
238 srcs: ["b.java"],
239 libs: ["foo"],
240 }
241 `)
242
243 // The bar library should depend on the stubs jar.
244 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
245 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
246 t.Errorf("expected %q, found %#q", expected, actual)
247 }
248}
249
Anton Hansson3adf3c52021-09-21 15:25:12 +0100250func TestJavaSdkLibrary_AccessOutputFiles(t *testing.T) {
Colin Cross76069a32021-06-01 13:18:08 -0700251 android.GroupFixturePreparers(
252 prepareForJavaTest,
253 PrepareForTestWithJavaSdkLibraryFiles,
254 FixtureWithLastReleaseApis("foo"),
255 ).RunTestWithBp(t, `
256 java_sdk_library {
257 name: "foo",
258 srcs: ["a.java"],
259 api_packages: ["foo"],
Anton Hansson3adf3c52021-09-21 15:25:12 +0100260 annotations_enabled: true,
261 public: {
262 enabled: true,
263 },
264 }
265 java_library {
266 name: "bar",
267 srcs: ["b.java", ":foo{.public.stubs.source}"],
268 java_resources: [":foo{.public.annotations.zip}"],
269 }
270 `)
271}
272
273func TestJavaSdkLibrary_AccessOutputFiles_NoAnnotations(t *testing.T) {
274 android.GroupFixturePreparers(
275 prepareForJavaTest,
276 PrepareForTestWithJavaSdkLibraryFiles,
277 FixtureWithLastReleaseApis("foo"),
278 ).
279 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "bar" variant "android_common": path dependency ":foo{.public.annotations.zip}": annotations.zip not available for api scope public`)).
280 RunTestWithBp(t, `
281 java_sdk_library {
282 name: "foo",
283 srcs: ["a.java"],
284 api_packages: ["foo"],
Colin Cross76069a32021-06-01 13:18:08 -0700285 public: {
286 enabled: true,
287 },
288 }
289
290 java_library {
291 name: "bar",
292 srcs: ["b.java", ":foo{.public.stubs.source}"],
Anton Hansson3adf3c52021-09-21 15:25:12 +0100293 java_resources: [":foo{.public.annotations.zip}"],
Colin Cross76069a32021-06-01 13:18:08 -0700294 }
295 `)
296}
297
298func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
299 android.GroupFixturePreparers(
300 prepareForJavaTest,
301 PrepareForTestWithJavaSdkLibraryFiles,
302 FixtureWithLastReleaseApis("foo"),
303 ).
304 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
305 RunTestWithBp(t, `
306 java_sdk_library {
307 name: "foo",
308 srcs: ["a.java"],
309 api_packages: ["foo"],
310 public: {
311 enabled: true,
312 },
313 }
314
315 java_library {
316 name: "bar",
317 srcs: ["b.java", ":foo{.system.stubs.source}"],
318 }
319 `)
320}
321
322func TestJavaSdkLibrary_Deps(t *testing.T) {
323 result := android.GroupFixturePreparers(
324 prepareForJavaTest,
325 PrepareForTestWithJavaSdkLibraryFiles,
326 FixtureWithLastReleaseApis("sdklib"),
327 ).RunTestWithBp(t, `
328 java_sdk_library {
329 name: "sdklib",
330 srcs: ["a.java"],
331 sdk_version: "none",
332 system_modules: "none",
333 public: {
334 enabled: true,
335 },
336 }
337 `)
338
339 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
340 `dex2oatd`,
341 `sdklib.impl`,
342 `sdklib.stubs`,
343 `sdklib.stubs.source`,
344 `sdklib.xml`,
345 })
346}
347
348func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
349 prepareForJavaTest.RunTestWithBp(t, `
350 java_sdk_library_import {
351 name: "foo",
352 public: {
353 jars: ["a.jar"],
354 stub_srcs: ["a.java"],
355 current_api: "api/current.txt",
356 removed_api: "api/removed.txt",
Anton Hansson3adf3c52021-09-21 15:25:12 +0100357 annotations: "x/annotations.zip",
Colin Cross76069a32021-06-01 13:18:08 -0700358 },
359 }
360
361 java_library {
362 name: "bar",
363 srcs: [":foo{.public.stubs.source}"],
364 java_resources: [
365 ":foo{.public.api.txt}",
366 ":foo{.public.removed-api.txt}",
Anton Hansson3adf3c52021-09-21 15:25:12 +0100367 ":foo{.public.annotations.zip}",
Colin Cross76069a32021-06-01 13:18:08 -0700368 ],
369 }
370 `)
371}
372
373func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
374 bp := `
375 java_sdk_library_import {
376 name: "foo",
377 public: {
378 jars: ["a.jar"],
379 },
380 }
381 `
382
383 t.Run("stubs.source", func(t *testing.T) {
384 prepareForJavaTest.
385 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
386 RunTestWithBp(t, bp+`
387 java_library {
388 name: "bar",
389 srcs: [":foo{.public.stubs.source}"],
390 java_resources: [
391 ":foo{.public.api.txt}",
392 ":foo{.public.removed-api.txt}",
393 ],
394 }
395 `)
396 })
397
398 t.Run("api.txt", func(t *testing.T) {
399 prepareForJavaTest.
400 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
401 RunTestWithBp(t, bp+`
402 java_library {
403 name: "bar",
404 srcs: ["a.java"],
405 java_resources: [
406 ":foo{.public.api.txt}",
407 ],
408 }
409 `)
410 })
411
412 t.Run("removed-api.txt", func(t *testing.T) {
413 prepareForJavaTest.
414 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
415 RunTestWithBp(t, bp+`
416 java_library {
417 name: "bar",
418 srcs: ["a.java"],
419 java_resources: [
420 ":foo{.public.removed-api.txt}",
421 ],
422 }
423 `)
424 })
425}
426
427func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
428 prepareForJavaTest.
429 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
430 RunTestWithBp(t, `
431 java_sdk_library {
432 name: "foo",
433 srcs: ["a.java", "b.java"],
434 api_packages: ["foo"],
435 // Explicitly disable public to test the check that ensures the set of enabled
436 // scopes is consistent.
437 public: {
438 enabled: false,
439 },
440 system: {
441 enabled: true,
442 },
443 }
444 `)
445}
446
447func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
448 android.GroupFixturePreparers(
449 prepareForJavaTest,
450 PrepareForTestWithJavaSdkLibraryFiles,
451 FixtureWithLastReleaseApis("foo"),
452 ).RunTestWithBp(t, `
453 java_sdk_library {
454 name: "foo",
455 srcs: ["a.java", "b.java"],
456 api_packages: ["foo"],
457 system: {
458 enabled: true,
459 sdk_version: "module_current",
460 },
461 }
462 `)
463}
464
465func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
466 android.GroupFixturePreparers(
467 prepareForJavaTest,
468 PrepareForTestWithJavaSdkLibraryFiles,
469 FixtureWithLastReleaseApis("foo"),
470 ).RunTestWithBp(t, `
471 java_sdk_library {
472 name: "foo",
473 srcs: ["a.java", "b.java"],
474 api_packages: ["foo"],
475 system: {
476 enabled: true,
477 },
478 module_lib: {
479 enabled: true,
480 },
481 }
482 `)
483}
484
485func TestJavaSdkLibrary_SystemServer(t *testing.T) {
486 android.GroupFixturePreparers(
487 prepareForJavaTest,
488 PrepareForTestWithJavaSdkLibraryFiles,
489 FixtureWithLastReleaseApis("foo"),
490 ).RunTestWithBp(t, `
491 java_sdk_library {
492 name: "foo",
493 srcs: ["a.java", "b.java"],
494 api_packages: ["foo"],
495 system: {
496 enabled: true,
497 },
498 system_server: {
499 enabled: true,
500 },
501 }
502 `)
503}
504
505func TestJavaSdkLibrary_MissingScope(t *testing.T) {
506 prepareForJavaTest.
507 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
508 RunTestWithBp(t, `
509 java_sdk_library {
510 name: "foo",
511 srcs: ["a.java"],
512 public: {
513 enabled: false,
514 },
515 }
516
517 java_library {
518 name: "baz",
519 srcs: ["a.java"],
520 libs: ["foo"],
521 sdk_version: "module_current",
522 }
523 `)
524}
525
526func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
527 android.GroupFixturePreparers(
528 prepareForJavaTest,
529 PrepareForTestWithJavaSdkLibraryFiles,
530 FixtureWithLastReleaseApis("foo"),
531 ).RunTestWithBp(t, `
532 java_sdk_library {
533 name: "foo",
534 srcs: ["a.java"],
535 system: {
536 enabled: true,
537 },
538 }
539
540 java_library {
541 name: "baz",
542 srcs: ["a.java"],
543 libs: ["foo"],
544 // foo does not have module-lib scope so it should fallback to system
545 sdk_version: "module_current",
546 }
547 `)
548}
549
550func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
551 result := android.GroupFixturePreparers(
552 prepareForJavaTest,
553 PrepareForTestWithJavaSdkLibraryFiles,
554 FixtureWithLastReleaseApis("foo"),
555 ).RunTestWithBp(t, `
556 java_sdk_library {
557 name: "foo",
558 srcs: ["a.java"],
559 system: {
560 enabled: true,
561 },
562 default_to_stubs: true,
563 }
564
565 java_library {
566 name: "baz",
567 srcs: ["a.java"],
568 libs: ["foo"],
569 // does not have sdk_version set, should fallback to module,
570 // which will then fallback to system because the module scope
571 // is not enabled.
572 }
573 `)
574 // The baz library should depend on the system stubs jar.
575 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
576 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
577 t.Errorf("expected %q, found %#q", expected, actual)
578 }
579}
580
581func TestJavaSdkLibraryImport(t *testing.T) {
582 result := prepareForJavaTest.RunTestWithBp(t, `
583 java_library {
584 name: "foo",
585 srcs: ["a.java"],
586 libs: ["sdklib"],
587 sdk_version: "current",
588 }
589
590 java_library {
591 name: "foo.system",
592 srcs: ["a.java"],
593 libs: ["sdklib"],
594 sdk_version: "system_current",
595 }
596
597 java_library {
598 name: "foo.test",
599 srcs: ["a.java"],
600 libs: ["sdklib"],
601 sdk_version: "test_current",
602 }
603
604 java_sdk_library_import {
605 name: "sdklib",
606 public: {
607 jars: ["a.jar"],
608 },
609 system: {
610 jars: ["b.jar"],
611 },
612 test: {
613 jars: ["c.jar"],
614 stub_srcs: ["c.java"],
615 },
616 }
617 `)
618
619 for _, scope := range []string{"", ".system", ".test"} {
620 fooModule := result.ModuleForTests("foo"+scope, "android_common")
621 javac := fooModule.Rule("javac")
622
623 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
624 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
625 }
626
627 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
628 `prebuilt_sdklib.stubs`,
629 `prebuilt_sdklib.stubs.source.test`,
630 `prebuilt_sdklib.stubs.system`,
631 `prebuilt_sdklib.stubs.test`,
632 })
633}
634
635func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
636 result := android.GroupFixturePreparers(
637 prepareForJavaTest,
638 PrepareForTestWithJavaSdkLibraryFiles,
639 FixtureWithLastReleaseApis("sdklib"),
640 ).RunTestWithBp(t, `
641 java_sdk_library {
642 name: "sdklib",
643 srcs: ["a.java"],
644 sdk_version: "none",
645 system_modules: "none",
646 public: {
647 enabled: true,
648 },
649 }
650
651 java_sdk_library_import {
652 name: "sdklib",
653 public: {
654 jars: ["a.jar"],
655 },
656 }
657 `)
658
659 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
660 `dex2oatd`,
661 `prebuilt_sdklib`,
662 `sdklib.impl`,
663 `sdklib.stubs`,
664 `sdklib.stubs.source`,
665 `sdklib.xml`,
666 })
667
668 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
669 `prebuilt_sdklib.stubs`,
670 `sdklib.impl`,
671 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
672 // dependency is added after prebuilts may have been renamed and so has to use
673 // the renamed name.
674 `sdklib.xml`,
675 })
676}
677
678func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
679 result := android.GroupFixturePreparers(
680 prepareForJavaTest,
681 PrepareForTestWithJavaSdkLibraryFiles,
682 FixtureWithLastReleaseApis("sdklib"),
683 ).RunTestWithBp(t, `
684 java_sdk_library {
685 name: "sdklib",
686 srcs: ["a.java"],
687 sdk_version: "none",
688 system_modules: "none",
689 public: {
690 enabled: true,
691 },
692 }
693
694 java_sdk_library_import {
695 name: "sdklib",
696 prefer: true,
697 public: {
698 jars: ["a.jar"],
699 },
700 }
701 `)
702
703 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
704 `dex2oatd`,
705 `prebuilt_sdklib`,
706 `sdklib.impl`,
707 `sdklib.stubs`,
708 `sdklib.stubs.source`,
709 `sdklib.xml`,
710 })
711
712 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
713 `prebuilt_sdklib.stubs`,
714 `sdklib.impl`,
715 `sdklib.xml`,
716 })
717}
718
719func TestJavaSdkLibraryEnforce(t *testing.T) {
720 partitionToBpOption := func(partition string) string {
721 switch partition {
722 case "system":
723 return ""
724 case "vendor":
725 return "soc_specific: true,"
726 case "product":
727 return "product_specific: true,"
728 default:
729 panic("Invalid partition group name: " + partition)
730 }
731 }
732
733 type testConfigInfo struct {
734 libraryType string
735 fromPartition string
736 toPartition string
737 enforceVendorInterface bool
738 enforceProductInterface bool
739 enforceJavaSdkLibraryCheck bool
740 allowList []string
741 }
742
743 createPreparer := func(info testConfigInfo) android.FixturePreparer {
744 bpFileTemplate := `
745 java_library {
746 name: "foo",
747 srcs: ["foo.java"],
748 libs: ["bar"],
749 sdk_version: "current",
750 %s
751 }
752
753 %s {
754 name: "bar",
755 srcs: ["bar.java"],
756 sdk_version: "current",
757 %s
758 }
759 `
760
761 bpFile := fmt.Sprintf(bpFileTemplate,
762 partitionToBpOption(info.fromPartition),
763 info.libraryType,
764 partitionToBpOption(info.toPartition))
765
766 return android.GroupFixturePreparers(
767 PrepareForTestWithJavaSdkLibraryFiles,
768 FixtureWithLastReleaseApis("bar"),
769 android.FixtureWithRootAndroidBp(bpFile),
770 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
771 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
772 if info.enforceVendorInterface {
773 variables.DeviceVndkVersion = proptools.StringPtr("current")
774 }
775 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
776 variables.InterPartitionJavaLibraryAllowList = info.allowList
777 }),
778 )
779 }
780
781 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
782 t.Run(fmt.Sprintf("%v", info), func(t *testing.T) {
783 errorHandler := android.FixtureExpectsNoErrors
784 if expectedErrorPattern != "" {
785 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
786 }
787 android.GroupFixturePreparers(
788 prepareForJavaTest,
789 createPreparer(info),
790 ).
791 ExtendWithErrorHandler(errorHandler).
792 RunTest(t)
793 })
794 }
795
796 errorMessage := "is not allowed across the partitions"
797
798 runTest(t, testConfigInfo{
799 libraryType: "java_library",
800 fromPartition: "product",
801 toPartition: "system",
802 enforceVendorInterface: true,
803 enforceProductInterface: true,
804 enforceJavaSdkLibraryCheck: false,
805 }, "")
806
807 runTest(t, testConfigInfo{
808 libraryType: "java_library",
809 fromPartition: "product",
810 toPartition: "system",
811 enforceVendorInterface: true,
812 enforceProductInterface: false,
813 enforceJavaSdkLibraryCheck: true,
814 }, "")
815
816 runTest(t, testConfigInfo{
817 libraryType: "java_library",
818 fromPartition: "product",
819 toPartition: "system",
820 enforceVendorInterface: true,
821 enforceProductInterface: true,
822 enforceJavaSdkLibraryCheck: true,
823 }, errorMessage)
824
825 runTest(t, testConfigInfo{
826 libraryType: "java_library",
827 fromPartition: "vendor",
828 toPartition: "system",
829 enforceVendorInterface: true,
830 enforceProductInterface: true,
831 enforceJavaSdkLibraryCheck: true,
832 }, errorMessage)
833
834 runTest(t, testConfigInfo{
835 libraryType: "java_library",
836 fromPartition: "vendor",
837 toPartition: "system",
838 enforceVendorInterface: true,
839 enforceProductInterface: true,
840 enforceJavaSdkLibraryCheck: true,
841 allowList: []string{"bar"},
842 }, "")
843
844 runTest(t, testConfigInfo{
845 libraryType: "java_library",
846 fromPartition: "vendor",
847 toPartition: "product",
848 enforceVendorInterface: true,
849 enforceProductInterface: true,
850 enforceJavaSdkLibraryCheck: true,
851 }, errorMessage)
852
853 runTest(t, testConfigInfo{
854 libraryType: "java_sdk_library",
855 fromPartition: "product",
856 toPartition: "system",
857 enforceVendorInterface: true,
858 enforceProductInterface: true,
859 enforceJavaSdkLibraryCheck: true,
860 }, "")
861
862 runTest(t, testConfigInfo{
863 libraryType: "java_sdk_library",
864 fromPartition: "vendor",
865 toPartition: "system",
866 enforceVendorInterface: true,
867 enforceProductInterface: true,
868 enforceJavaSdkLibraryCheck: true,
869 }, "")
870
871 runTest(t, testConfigInfo{
872 libraryType: "java_sdk_library",
873 fromPartition: "vendor",
874 toPartition: "product",
875 enforceVendorInterface: true,
876 enforceProductInterface: true,
877 enforceJavaSdkLibraryCheck: true,
878 }, "")
879}
Colin Cross3b538082021-06-01 13:39:09 -0700880
881func TestJavaSdkLibraryDist(t *testing.T) {
882 result := android.GroupFixturePreparers(
883 PrepareForTestWithJavaBuildComponents,
884 PrepareForTestWithJavaDefaultModules,
885 PrepareForTestWithJavaSdkLibraryFiles,
Colin Cross7104aae2021-06-02 13:02:51 -0700886 FixtureWithLastReleaseApis(
887 "sdklib_no_group",
888 "sdklib_group_foo",
889 "sdklib_owner_foo",
890 "foo"),
Colin Cross3b538082021-06-01 13:39:09 -0700891 ).RunTestWithBp(t, `
892 java_sdk_library {
Colin Cross1072a712021-06-01 14:07:56 -0700893 name: "sdklib_no_group",
Colin Cross3b538082021-06-01 13:39:09 -0700894 srcs: ["foo.java"],
895 }
896
897 java_sdk_library {
Colin Cross0d3dd062021-06-01 13:13:40 -0700898 name: "sdklib_group_foo",
Colin Cross0d3dd062021-06-01 13:13:40 -0700899 srcs: ["foo.java"],
900 dist_group: "foo",
901 }
902
903 java_sdk_library {
Colin Cross3b538082021-06-01 13:39:09 -0700904 name: "sdklib_owner_foo",
Colin Cross3b538082021-06-01 13:39:09 -0700905 srcs: ["foo.java"],
906 owner: "foo",
907 }
908
909 java_sdk_library {
910 name: "sdklib_stem_foo",
Colin Cross3b538082021-06-01 13:39:09 -0700911 srcs: ["foo.java"],
912 dist_stem: "foo",
913 }
Colin Cross3b538082021-06-01 13:39:09 -0700914 `)
915
916 type testCase struct {
917 module string
918 distDir string
919 distStem string
920 }
921 testCases := []testCase{
922 {
Colin Cross1072a712021-06-01 14:07:56 -0700923 module: "sdklib_no_group",
Colin Cross0f9eeb72021-06-01 14:05:09 -0700924 distDir: "apistubs/unknown/public",
Colin Cross1072a712021-06-01 14:07:56 -0700925 distStem: "sdklib_no_group.jar",
Colin Cross3b538082021-06-01 13:39:09 -0700926 },
927 {
Colin Cross0d3dd062021-06-01 13:13:40 -0700928 module: "sdklib_group_foo",
929 distDir: "apistubs/foo/public",
930 distStem: "sdklib_group_foo.jar",
931 },
932 {
Colin Cross1072a712021-06-01 14:07:56 -0700933 // Owner doesn't affect distDir after b/186723288.
Colin Cross3b538082021-06-01 13:39:09 -0700934 module: "sdklib_owner_foo",
Colin Cross1072a712021-06-01 14:07:56 -0700935 distDir: "apistubs/unknown/public",
Colin Cross3b538082021-06-01 13:39:09 -0700936 distStem: "sdklib_owner_foo.jar",
937 },
938 {
939 module: "sdklib_stem_foo",
Colin Cross0f9eeb72021-06-01 14:05:09 -0700940 distDir: "apistubs/unknown/public",
Colin Cross3b538082021-06-01 13:39:09 -0700941 distStem: "foo.jar",
942 },
Colin Cross3b538082021-06-01 13:39:09 -0700943 }
944
945 for _, tt := range testCases {
946 t.Run(tt.module, func(t *testing.T) {
947 m := result.ModuleForTests(tt.module+".stubs", "android_common").Module().(*Library)
948 dists := m.Dists()
949 if len(dists) != 1 {
950 t.Fatalf("expected exactly 1 dist entry, got %d", len(dists))
951 }
952 if g, w := String(dists[0].Dir), tt.distDir; g != w {
953 t.Errorf("expected dist dir %q, got %q", w, g)
954 }
955 if g, w := String(dists[0].Dest), tt.distStem; g != w {
956 t.Errorf("expected dist stem %q, got %q", w, g)
957 }
958 })
959 }
960}
satayev531330e2021-12-06 11:40:46 +0000961
962func TestSdkLibrary_CheckMinSdkVersion(t *testing.T) {
963 preparer := android.GroupFixturePreparers(
964 PrepareForTestWithJavaBuildComponents,
965 PrepareForTestWithJavaDefaultModules,
966 PrepareForTestWithJavaSdkLibraryFiles,
967 )
968
969 preparer.RunTestWithBp(t, `
970 java_sdk_library {
971 name: "sdklib",
972 srcs: ["a.java"],
973 static_libs: ["util"],
974 min_sdk_version: "30",
975 unsafe_ignore_missing_latest_api: true,
976 }
977
978 java_library {
979 name: "util",
980 srcs: ["a.java"],
981 min_sdk_version: "30",
982 }
983 `)
984
985 preparer.
986 RunTestWithBp(t, `
987 java_sdk_library {
988 name: "sdklib",
989 srcs: ["a.java"],
990 libs: ["util"],
991 impl_only_libs: ["util"],
992 stub_only_libs: ["util"],
993 stub_only_static_libs: ["util"],
994 min_sdk_version: "30",
995 unsafe_ignore_missing_latest_api: true,
996 }
997
998 java_library {
999 name: "util",
1000 srcs: ["a.java"],
1001 }
1002 `)
1003
1004 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "util".*should support min_sdk_version\(30\)`)).
1005 RunTestWithBp(t, `
1006 java_sdk_library {
1007 name: "sdklib",
1008 srcs: ["a.java"],
1009 static_libs: ["util"],
1010 min_sdk_version: "30",
1011 unsafe_ignore_missing_latest_api: true,
1012 }
1013
1014 java_library {
1015 name: "util",
1016 srcs: ["a.java"],
1017 min_sdk_version: "31",
1018 }
1019 `)
1020
1021 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "another_util".*should support min_sdk_version\(30\)`)).
1022 RunTestWithBp(t, `
1023 java_sdk_library {
1024 name: "sdklib",
1025 srcs: ["a.java"],
1026 static_libs: ["util"],
1027 min_sdk_version: "30",
1028 unsafe_ignore_missing_latest_api: true,
1029 }
1030
1031 java_library {
1032 name: "util",
1033 srcs: ["a.java"],
1034 static_libs: ["another_util"],
1035 min_sdk_version: "30",
1036 }
1037
1038 java_library {
1039 name: "another_util",
1040 srcs: ["a.java"],
1041 min_sdk_version: "31",
1042 }
1043 `)
1044}