blob: 0fe6e72efb17b3f075799341527477900ead4eb5 [file] [log] [blame]
Colin Cross1d2b6b32021-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
113 result.ModuleForTests("foo", "android_common")
114 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
125 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
126 // tests if baz is actually linked to the stubs lib
127 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
128 // ... and not to the impl lib
129 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
130 // test if baz is not linked to the system variant of foo
131 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
132
133 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
134 // tests if baz-test is actually linked to the test stubs lib
135 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
136
137 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
138 // tests if baz-29 is actually linked to the system 29 stubs lib
139 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
140
141 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
142 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
143 android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar")
144
145 // test if baz has exported SDK lib names foo and bar to qux
146 qux := result.ModuleForTests("qux", "android_common")
147 if quxLib, ok := qux.Module().(*Library); ok {
148 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
149 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
150 }
151}
152
153func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
154 result := android.GroupFixturePreparers(
155 prepareForJavaTest,
156 PrepareForTestWithJavaSdkLibraryFiles,
157 FixtureWithLastReleaseApis("sdklib"),
158 ).RunTestWithBp(t, `
159 java_sdk_library {
160 name: "sdklib",
161 srcs: ["a.java"],
162 libs: ["lib"],
163 static_libs: ["static-lib"],
164 impl_only_libs: ["impl-only-lib"],
165 stub_only_libs: ["stub-only-lib"],
166 stub_only_static_libs: ["stub-only-static-lib"],
167 }
168 java_defaults {
169 name: "defaults",
170 srcs: ["a.java"],
171 sdk_version: "current",
172 }
173 java_library { name: "lib", defaults: ["defaults"] }
174 java_library { name: "static-lib", defaults: ["defaults"] }
175 java_library { name: "impl-only-lib", defaults: ["defaults"] }
176 java_library { name: "stub-only-lib", defaults: ["defaults"] }
177 java_library { name: "stub-only-static-lib", defaults: ["defaults"] }
178 `)
179 var expectations = []struct {
180 lib string
181 on_impl_classpath bool
182 on_stub_classpath bool
183 in_impl_combined bool
184 in_stub_combined bool
185 }{
186 {lib: "lib", on_impl_classpath: true},
187 {lib: "static-lib", in_impl_combined: true},
188 {lib: "impl-only-lib", on_impl_classpath: true},
189 {lib: "stub-only-lib", on_stub_classpath: true},
190 {lib: "stub-only-static-lib", in_stub_combined: true},
191 }
192 verify := func(sdklib, dep string, cp, combined bool) {
193 sdklibCp := result.ModuleForTests(sdklib, "android_common").Rule("javac").Args["classpath"]
194 expected := cp || combined // Every combined jar is also on the classpath.
195 android.AssertStringContainsEquals(t, "bad classpath for "+sdklib, sdklibCp, "/"+dep+".jar", expected)
196
197 combineJarInputs := result.ModuleForTests(sdklib, "android_common").Rule("combineJar").Inputs.Strings()
198 depPath := filepath.Join("out", "soong", ".intermediates", dep, "android_common", "turbine-combined", dep+".jar")
199 android.AssertStringListContainsEquals(t, "bad combined inputs for "+sdklib, combineJarInputs, depPath, combined)
200 }
201 for _, expectation := range expectations {
202 verify("sdklib", expectation.lib, expectation.on_impl_classpath, expectation.in_impl_combined)
203 verify("sdklib.impl", expectation.lib, expectation.on_impl_classpath, expectation.in_impl_combined)
204
205 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
206 verify(stubName, expectation.lib, expectation.on_stub_classpath, expectation.in_stub_combined)
207 }
208}
209
210func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
211 result := android.GroupFixturePreparers(
212 prepareForJavaTest,
213 PrepareForTestWithJavaSdkLibraryFiles,
214 FixtureWithLastReleaseApis("foo"),
215 ).RunTestWithBp(t, `
216 java_sdk_library {
217 name: "foo",
218 srcs: ["a.java"],
219 api_only: true,
220 public: {
221 enabled: true,
222 },
223 }
224
225 java_library {
226 name: "bar",
227 srcs: ["b.java"],
228 libs: ["foo"],
229 }
230 `)
231
232 // The bar library should depend on the stubs jar.
233 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
234 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
235 t.Errorf("expected %q, found %#q", expected, actual)
236 }
237}
238
239func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
240 android.GroupFixturePreparers(
241 prepareForJavaTest,
242 PrepareForTestWithJavaSdkLibraryFiles,
243 FixtureWithLastReleaseApis("foo"),
244 ).RunTestWithBp(t, `
245 java_sdk_library {
246 name: "foo",
247 srcs: ["a.java"],
248 api_packages: ["foo"],
249 public: {
250 enabled: true,
251 },
252 }
253
254 java_library {
255 name: "bar",
256 srcs: ["b.java", ":foo{.public.stubs.source}"],
257 }
258 `)
259}
260
261func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
262 android.GroupFixturePreparers(
263 prepareForJavaTest,
264 PrepareForTestWithJavaSdkLibraryFiles,
265 FixtureWithLastReleaseApis("foo"),
266 ).
267 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
268 RunTestWithBp(t, `
269 java_sdk_library {
270 name: "foo",
271 srcs: ["a.java"],
272 api_packages: ["foo"],
273 public: {
274 enabled: true,
275 },
276 }
277
278 java_library {
279 name: "bar",
280 srcs: ["b.java", ":foo{.system.stubs.source}"],
281 }
282 `)
283}
284
285func TestJavaSdkLibrary_Deps(t *testing.T) {
286 result := android.GroupFixturePreparers(
287 prepareForJavaTest,
288 PrepareForTestWithJavaSdkLibraryFiles,
289 FixtureWithLastReleaseApis("sdklib"),
290 ).RunTestWithBp(t, `
291 java_sdk_library {
292 name: "sdklib",
293 srcs: ["a.java"],
294 sdk_version: "none",
295 system_modules: "none",
296 public: {
297 enabled: true,
298 },
299 }
300 `)
301
302 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
303 `dex2oatd`,
304 `sdklib.impl`,
305 `sdklib.stubs`,
306 `sdklib.stubs.source`,
307 `sdklib.xml`,
308 })
309}
310
311func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
312 prepareForJavaTest.RunTestWithBp(t, `
313 java_sdk_library_import {
314 name: "foo",
315 public: {
316 jars: ["a.jar"],
317 stub_srcs: ["a.java"],
318 current_api: "api/current.txt",
319 removed_api: "api/removed.txt",
320 },
321 }
322
323 java_library {
324 name: "bar",
325 srcs: [":foo{.public.stubs.source}"],
326 java_resources: [
327 ":foo{.public.api.txt}",
328 ":foo{.public.removed-api.txt}",
329 ],
330 }
331 `)
332}
333
334func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
335 bp := `
336 java_sdk_library_import {
337 name: "foo",
338 public: {
339 jars: ["a.jar"],
340 },
341 }
342 `
343
344 t.Run("stubs.source", func(t *testing.T) {
345 prepareForJavaTest.
346 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
347 RunTestWithBp(t, bp+`
348 java_library {
349 name: "bar",
350 srcs: [":foo{.public.stubs.source}"],
351 java_resources: [
352 ":foo{.public.api.txt}",
353 ":foo{.public.removed-api.txt}",
354 ],
355 }
356 `)
357 })
358
359 t.Run("api.txt", func(t *testing.T) {
360 prepareForJavaTest.
361 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
362 RunTestWithBp(t, bp+`
363 java_library {
364 name: "bar",
365 srcs: ["a.java"],
366 java_resources: [
367 ":foo{.public.api.txt}",
368 ],
369 }
370 `)
371 })
372
373 t.Run("removed-api.txt", func(t *testing.T) {
374 prepareForJavaTest.
375 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
376 RunTestWithBp(t, bp+`
377 java_library {
378 name: "bar",
379 srcs: ["a.java"],
380 java_resources: [
381 ":foo{.public.removed-api.txt}",
382 ],
383 }
384 `)
385 })
386}
387
388func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
389 prepareForJavaTest.
390 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
391 RunTestWithBp(t, `
392 java_sdk_library {
393 name: "foo",
394 srcs: ["a.java", "b.java"],
395 api_packages: ["foo"],
396 // Explicitly disable public to test the check that ensures the set of enabled
397 // scopes is consistent.
398 public: {
399 enabled: false,
400 },
401 system: {
402 enabled: true,
403 },
404 }
405 `)
406}
407
408func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
409 android.GroupFixturePreparers(
410 prepareForJavaTest,
411 PrepareForTestWithJavaSdkLibraryFiles,
412 FixtureWithLastReleaseApis("foo"),
413 ).RunTestWithBp(t, `
414 java_sdk_library {
415 name: "foo",
416 srcs: ["a.java", "b.java"],
417 api_packages: ["foo"],
418 system: {
419 enabled: true,
420 sdk_version: "module_current",
421 },
422 }
423 `)
424}
425
426func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
427 android.GroupFixturePreparers(
428 prepareForJavaTest,
429 PrepareForTestWithJavaSdkLibraryFiles,
430 FixtureWithLastReleaseApis("foo"),
431 ).RunTestWithBp(t, `
432 java_sdk_library {
433 name: "foo",
434 srcs: ["a.java", "b.java"],
435 api_packages: ["foo"],
436 system: {
437 enabled: true,
438 },
439 module_lib: {
440 enabled: true,
441 },
442 }
443 `)
444}
445
446func TestJavaSdkLibrary_SystemServer(t *testing.T) {
447 android.GroupFixturePreparers(
448 prepareForJavaTest,
449 PrepareForTestWithJavaSdkLibraryFiles,
450 FixtureWithLastReleaseApis("foo"),
451 ).RunTestWithBp(t, `
452 java_sdk_library {
453 name: "foo",
454 srcs: ["a.java", "b.java"],
455 api_packages: ["foo"],
456 system: {
457 enabled: true,
458 },
459 system_server: {
460 enabled: true,
461 },
462 }
463 `)
464}
465
466func TestJavaSdkLibrary_MissingScope(t *testing.T) {
467 prepareForJavaTest.
468 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
469 RunTestWithBp(t, `
470 java_sdk_library {
471 name: "foo",
472 srcs: ["a.java"],
473 public: {
474 enabled: false,
475 },
476 }
477
478 java_library {
479 name: "baz",
480 srcs: ["a.java"],
481 libs: ["foo"],
482 sdk_version: "module_current",
483 }
484 `)
485}
486
487func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
488 android.GroupFixturePreparers(
489 prepareForJavaTest,
490 PrepareForTestWithJavaSdkLibraryFiles,
491 FixtureWithLastReleaseApis("foo"),
492 ).RunTestWithBp(t, `
493 java_sdk_library {
494 name: "foo",
495 srcs: ["a.java"],
496 system: {
497 enabled: true,
498 },
499 }
500
501 java_library {
502 name: "baz",
503 srcs: ["a.java"],
504 libs: ["foo"],
505 // foo does not have module-lib scope so it should fallback to system
506 sdk_version: "module_current",
507 }
508 `)
509}
510
511func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
512 result := android.GroupFixturePreparers(
513 prepareForJavaTest,
514 PrepareForTestWithJavaSdkLibraryFiles,
515 FixtureWithLastReleaseApis("foo"),
516 ).RunTestWithBp(t, `
517 java_sdk_library {
518 name: "foo",
519 srcs: ["a.java"],
520 system: {
521 enabled: true,
522 },
523 default_to_stubs: true,
524 }
525
526 java_library {
527 name: "baz",
528 srcs: ["a.java"],
529 libs: ["foo"],
530 // does not have sdk_version set, should fallback to module,
531 // which will then fallback to system because the module scope
532 // is not enabled.
533 }
534 `)
535 // The baz library should depend on the system stubs jar.
536 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
537 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
538 t.Errorf("expected %q, found %#q", expected, actual)
539 }
540}
541
542func TestJavaSdkLibraryImport(t *testing.T) {
543 result := prepareForJavaTest.RunTestWithBp(t, `
544 java_library {
545 name: "foo",
546 srcs: ["a.java"],
547 libs: ["sdklib"],
548 sdk_version: "current",
549 }
550
551 java_library {
552 name: "foo.system",
553 srcs: ["a.java"],
554 libs: ["sdklib"],
555 sdk_version: "system_current",
556 }
557
558 java_library {
559 name: "foo.test",
560 srcs: ["a.java"],
561 libs: ["sdklib"],
562 sdk_version: "test_current",
563 }
564
565 java_sdk_library_import {
566 name: "sdklib",
567 public: {
568 jars: ["a.jar"],
569 },
570 system: {
571 jars: ["b.jar"],
572 },
573 test: {
574 jars: ["c.jar"],
575 stub_srcs: ["c.java"],
576 },
577 }
578 `)
579
580 for _, scope := range []string{"", ".system", ".test"} {
581 fooModule := result.ModuleForTests("foo"+scope, "android_common")
582 javac := fooModule.Rule("javac")
583
584 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
585 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
586 }
587
588 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
589 `prebuilt_sdklib.stubs`,
590 `prebuilt_sdklib.stubs.source.test`,
591 `prebuilt_sdklib.stubs.system`,
592 `prebuilt_sdklib.stubs.test`,
593 })
594}
595
596func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
597 result := android.GroupFixturePreparers(
598 prepareForJavaTest,
599 PrepareForTestWithJavaSdkLibraryFiles,
600 FixtureWithLastReleaseApis("sdklib"),
601 ).RunTestWithBp(t, `
602 java_sdk_library {
603 name: "sdklib",
604 srcs: ["a.java"],
605 sdk_version: "none",
606 system_modules: "none",
607 public: {
608 enabled: true,
609 },
610 }
611
612 java_sdk_library_import {
613 name: "sdklib",
614 public: {
615 jars: ["a.jar"],
616 },
617 }
618 `)
619
620 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
621 `dex2oatd`,
622 `prebuilt_sdklib`,
623 `sdklib.impl`,
624 `sdklib.stubs`,
625 `sdklib.stubs.source`,
626 `sdklib.xml`,
627 })
628
629 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
630 `prebuilt_sdklib.stubs`,
631 `sdklib.impl`,
632 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
633 // dependency is added after prebuilts may have been renamed and so has to use
634 // the renamed name.
635 `sdklib.xml`,
636 })
637}
638
639func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
640 result := android.GroupFixturePreparers(
641 prepareForJavaTest,
642 PrepareForTestWithJavaSdkLibraryFiles,
643 FixtureWithLastReleaseApis("sdklib"),
644 ).RunTestWithBp(t, `
645 java_sdk_library {
646 name: "sdklib",
647 srcs: ["a.java"],
648 sdk_version: "none",
649 system_modules: "none",
650 public: {
651 enabled: true,
652 },
653 }
654
655 java_sdk_library_import {
656 name: "sdklib",
657 prefer: true,
658 public: {
659 jars: ["a.jar"],
660 },
661 }
662 `)
663
664 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
665 `dex2oatd`,
666 `prebuilt_sdklib`,
667 `sdklib.impl`,
668 `sdklib.stubs`,
669 `sdklib.stubs.source`,
670 `sdklib.xml`,
671 })
672
673 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
674 `prebuilt_sdklib.stubs`,
675 `sdklib.impl`,
676 `sdklib.xml`,
677 })
678}
679
680func TestJavaSdkLibraryEnforce(t *testing.T) {
681 partitionToBpOption := func(partition string) string {
682 switch partition {
683 case "system":
684 return ""
685 case "vendor":
686 return "soc_specific: true,"
687 case "product":
688 return "product_specific: true,"
689 default:
690 panic("Invalid partition group name: " + partition)
691 }
692 }
693
694 type testConfigInfo struct {
695 libraryType string
696 fromPartition string
697 toPartition string
698 enforceVendorInterface bool
699 enforceProductInterface bool
700 enforceJavaSdkLibraryCheck bool
701 allowList []string
702 }
703
704 createPreparer := func(info testConfigInfo) android.FixturePreparer {
705 bpFileTemplate := `
706 java_library {
707 name: "foo",
708 srcs: ["foo.java"],
709 libs: ["bar"],
710 sdk_version: "current",
711 %s
712 }
713
714 %s {
715 name: "bar",
716 srcs: ["bar.java"],
717 sdk_version: "current",
718 %s
719 }
720 `
721
722 bpFile := fmt.Sprintf(bpFileTemplate,
723 partitionToBpOption(info.fromPartition),
724 info.libraryType,
725 partitionToBpOption(info.toPartition))
726
727 return android.GroupFixturePreparers(
728 PrepareForTestWithJavaSdkLibraryFiles,
729 FixtureWithLastReleaseApis("bar"),
730 android.FixtureWithRootAndroidBp(bpFile),
731 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
732 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
733 if info.enforceVendorInterface {
734 variables.DeviceVndkVersion = proptools.StringPtr("current")
735 }
736 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
737 variables.InterPartitionJavaLibraryAllowList = info.allowList
738 }),
739 )
740 }
741
742 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
743 t.Run(fmt.Sprintf("%v", info), func(t *testing.T) {
744 errorHandler := android.FixtureExpectsNoErrors
745 if expectedErrorPattern != "" {
746 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
747 }
748 android.GroupFixturePreparers(
749 prepareForJavaTest,
750 createPreparer(info),
751 ).
752 ExtendWithErrorHandler(errorHandler).
753 RunTest(t)
754 })
755 }
756
757 errorMessage := "is not allowed across the partitions"
758
759 runTest(t, testConfigInfo{
760 libraryType: "java_library",
761 fromPartition: "product",
762 toPartition: "system",
763 enforceVendorInterface: true,
764 enforceProductInterface: true,
765 enforceJavaSdkLibraryCheck: false,
766 }, "")
767
768 runTest(t, testConfigInfo{
769 libraryType: "java_library",
770 fromPartition: "product",
771 toPartition: "system",
772 enforceVendorInterface: true,
773 enforceProductInterface: false,
774 enforceJavaSdkLibraryCheck: true,
775 }, "")
776
777 runTest(t, testConfigInfo{
778 libraryType: "java_library",
779 fromPartition: "product",
780 toPartition: "system",
781 enforceVendorInterface: true,
782 enforceProductInterface: true,
783 enforceJavaSdkLibraryCheck: true,
784 }, errorMessage)
785
786 runTest(t, testConfigInfo{
787 libraryType: "java_library",
788 fromPartition: "vendor",
789 toPartition: "system",
790 enforceVendorInterface: true,
791 enforceProductInterface: true,
792 enforceJavaSdkLibraryCheck: true,
793 }, errorMessage)
794
795 runTest(t, testConfigInfo{
796 libraryType: "java_library",
797 fromPartition: "vendor",
798 toPartition: "system",
799 enforceVendorInterface: true,
800 enforceProductInterface: true,
801 enforceJavaSdkLibraryCheck: true,
802 allowList: []string{"bar"},
803 }, "")
804
805 runTest(t, testConfigInfo{
806 libraryType: "java_library",
807 fromPartition: "vendor",
808 toPartition: "product",
809 enforceVendorInterface: true,
810 enforceProductInterface: true,
811 enforceJavaSdkLibraryCheck: true,
812 }, errorMessage)
813
814 runTest(t, testConfigInfo{
815 libraryType: "java_sdk_library",
816 fromPartition: "product",
817 toPartition: "system",
818 enforceVendorInterface: true,
819 enforceProductInterface: true,
820 enforceJavaSdkLibraryCheck: true,
821 }, "")
822
823 runTest(t, testConfigInfo{
824 libraryType: "java_sdk_library",
825 fromPartition: "vendor",
826 toPartition: "system",
827 enforceVendorInterface: true,
828 enforceProductInterface: true,
829 enforceJavaSdkLibraryCheck: true,
830 }, "")
831
832 runTest(t, testConfigInfo{
833 libraryType: "java_sdk_library",
834 fromPartition: "vendor",
835 toPartition: "product",
836 enforceVendorInterface: true,
837 enforceProductInterface: true,
838 enforceJavaSdkLibraryCheck: true,
839 }, "")
840}
Colin Cross30c491b2021-06-01 13:39:09 -0700841
842func TestJavaSdkLibraryDist(t *testing.T) {
843 result := android.GroupFixturePreparers(
844 PrepareForTestWithJavaBuildComponents,
845 PrepareForTestWithJavaDefaultModules,
846 PrepareForTestWithJavaSdkLibraryFiles,
847 ).RunTestWithBp(t, `
848 java_sdk_library {
849 name: "sdklib_no_owner",
850 unsafe_ignore_missing_latest_api: true,
851 srcs: ["foo.java"],
852 }
853
854 java_sdk_library {
Colin Cross986b69a2021-06-01 13:13:40 -0700855 name: "sdklib_group_foo",
856 unsafe_ignore_missing_latest_api: true,
857 srcs: ["foo.java"],
858 dist_group: "foo",
859 }
860
861 java_sdk_library {
Colin Cross30c491b2021-06-01 13:39:09 -0700862 name: "sdklib_owner_foo",
863 unsafe_ignore_missing_latest_api: true,
864 srcs: ["foo.java"],
865 owner: "foo",
866 }
867
868 java_sdk_library {
869 name: "sdklib_stem_foo",
870 unsafe_ignore_missing_latest_api: true,
871 srcs: ["foo.java"],
872 dist_stem: "foo",
873 }
874
875 java_sdk_library {
876 name: "sdklib_core_lib",
877 unsafe_ignore_missing_latest_api: true,
878 srcs: ["foo.java"],
879 core_lib: true,
880 }
881 `)
882
883 type testCase struct {
884 module string
885 distDir string
886 distStem string
887 }
888 testCases := []testCase{
889 {
890 module: "sdklib_no_owner",
891 distDir: "apistubs/android/public",
892 distStem: "sdklib_no_owner.jar",
893 },
894 {
Colin Cross986b69a2021-06-01 13:13:40 -0700895 module: "sdklib_group_foo",
896 distDir: "apistubs/foo/public",
897 distStem: "sdklib_group_foo.jar",
898 },
899 {
Colin Cross30c491b2021-06-01 13:39:09 -0700900 module: "sdklib_owner_foo",
901 distDir: "apistubs/foo/public",
902 distStem: "sdklib_owner_foo.jar",
903 },
904 {
905 module: "sdklib_stem_foo",
906 distDir: "apistubs/android/public",
907 distStem: "foo.jar",
908 },
909 {
910 module: "sdklib_core_lib",
911 distDir: "apistubs/core/public",
912 distStem: "sdklib_core_lib.jar",
913 },
914 }
915
916 for _, tt := range testCases {
917 t.Run(tt.module, func(t *testing.T) {
918 m := result.ModuleForTests(tt.module+".stubs", "android_common").Module().(*Library)
919 dists := m.Dists()
920 if len(dists) != 1 {
921 t.Fatalf("expected exactly 1 dist entry, got %d", len(dists))
922 }
923 if g, w := String(dists[0].Dir), tt.distDir; g != w {
924 t.Errorf("expected dist dir %q, got %q", w, g)
925 }
926 if g, w := String(dists[0].Dest), tt.distStem; g != w {
927 t.Errorf("expected dist stem %q, got %q", w, g)
928 }
929 })
930 }
931}