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