blob: 7ba1f6dc7cd94b193211f6681c1341bd035dd7b3 [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 (
Colin Cross1d2b6b32021-06-01 13:18:08 -070018 "fmt"
19 "path/filepath"
20 "regexp"
Paul Duffind0b9fca2022-09-30 18:11:41 +010021 "strings"
Colin Cross1d2b6b32021-06-01 13:18:08 -070022 "testing"
23
Pedro Loureiroc3621422021-09-28 15:40:23 +000024 "android/soong/android"
25
Colin Cross1d2b6b32021-06-01 13:18:08 -070026 "github.com/google/blueprint/proptools"
27)
28
29func TestJavaSdkLibrary(t *testing.T) {
30 result := android.GroupFixturePreparers(
31 prepareForJavaTest,
32 PrepareForTestWithJavaSdkLibraryFiles,
33 FixtureWithPrebuiltApis(map[string][]string{
34 "28": {"foo"},
35 "29": {"foo"},
36 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
37 }),
Jihoon Kang1c92c3e2023-03-23 17:44:51 +000038 android.FixtureModifyConfig(func(config android.Config) {
39 config.SetApiLibraries([]string{"foo"})
40 }),
Colin Cross1d2b6b32021-06-01 13:18:08 -070041 ).RunTestWithBp(t, `
42 droiddoc_exported_dir {
43 name: "droiddoc-templates-sdk",
44 path: ".",
45 }
46 java_sdk_library {
47 name: "foo",
48 srcs: ["a.java", "b.java"],
49 api_packages: ["foo"],
50 }
51 java_sdk_library {
52 name: "bar",
53 srcs: ["a.java", "b.java"],
54 api_packages: ["bar"],
Wei Li1e73c652021-12-06 13:35:11 -080055 exclude_kotlinc_generated_files: true,
Colin Cross1d2b6b32021-06-01 13:18:08 -070056 }
57 java_library {
58 name: "baz",
59 srcs: ["c.java"],
60 libs: ["foo", "bar.stubs"],
61 sdk_version: "system_current",
62 }
63 java_sdk_library {
64 name: "barney",
65 srcs: ["c.java"],
66 api_only: true,
67 }
68 java_sdk_library {
69 name: "betty",
70 srcs: ["c.java"],
71 shared_library: false,
72 }
73 java_sdk_library_import {
74 name: "quuz",
75 public: {
76 jars: ["c.jar"],
77 },
78 }
79 java_sdk_library_import {
80 name: "fred",
81 public: {
82 jars: ["b.jar"],
83 },
84 }
85 java_sdk_library_import {
86 name: "wilma",
87 public: {
88 jars: ["b.jar"],
89 },
90 shared_library: false,
91 }
92 java_library {
93 name: "qux",
94 srcs: ["c.java"],
95 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
96 sdk_version: "system_current",
97 }
98 java_library {
99 name: "baz-test",
100 srcs: ["c.java"],
101 libs: ["foo"],
102 sdk_version: "test_current",
103 }
104 java_library {
105 name: "baz-29",
106 srcs: ["c.java"],
107 libs: ["foo"],
108 sdk_version: "system_29",
109 }
110 java_library {
111 name: "baz-module-30",
112 srcs: ["c.java"],
113 libs: ["foo"],
114 sdk_version: "module_30",
115 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000116 `)
Colin Cross1d2b6b32021-06-01 13:18:08 -0700117
118 // check the existence of the internal modules
Paul Duffinb97b1572021-04-29 21:50:40 +0100119 foo := result.ModuleForTests("foo", "android_common")
Colin Cross1d2b6b32021-06-01 13:18:08 -0700120 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
121 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
122 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
123 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
124 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
125 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jihoon Kangd48abd52023-02-02 22:32:31 +0000126 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo")+".api.contribution", "")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000127 result.ModuleForTests(apiScopePublic.apiLibraryModuleName("foo"), "android_common")
Colin Cross1d2b6b32021-06-01 13:18:08 -0700128 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
129 result.ModuleForTests("foo.api.public.28", "")
130 result.ModuleForTests("foo.api.system.28", "")
131 result.ModuleForTests("foo.api.test.28", "")
132
Paul Duffin9fc208e2021-06-27 20:42:04 +0100133 exportedComponentsInfo := result.ModuleProvider(foo.Module(), android.ExportedComponentsInfoProvider).(android.ExportedComponentsInfo)
Paul Duffinb97b1572021-04-29 21:50:40 +0100134 expectedFooExportedComponents := []string{
Paul Duffin958806b2022-05-16 13:10:47 +0000135 "foo-removed.api.public.latest",
136 "foo-removed.api.system.latest",
137 "foo.api.public.latest",
138 "foo.api.system.latest",
Paul Duffinb97b1572021-04-29 21:50:40 +0100139 "foo.stubs",
140 "foo.stubs.source",
141 "foo.stubs.source.system",
142 "foo.stubs.source.test",
143 "foo.stubs.system",
144 "foo.stubs.test",
145 }
146 android.AssertArrayString(t, "foo exported components", expectedFooExportedComponents, exportedComponentsInfo.Components)
147
Colin Cross1d2b6b32021-06-01 13:18:08 -0700148 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
149 // tests if baz is actually linked to the stubs lib
150 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
151 // ... and not to the impl lib
152 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
153 // test if baz is not linked to the system variant of foo
154 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
155
156 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
157 // tests if baz-test is actually linked to the test stubs lib
158 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
159
160 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
161 // tests if baz-29 is actually linked to the system 29 stubs lib
162 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
163
164 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
165 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
166 android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar")
167
168 // test if baz has exported SDK lib names foo and bar to qux
169 qux := result.ModuleForTests("qux", "android_common")
170 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +0100171 requiredSdkLibs, optionalSdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100172 android.AssertDeepEquals(t, "qux exports (required)", []string{"fred", "quuz", "foo", "bar"}, requiredSdkLibs)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +0100173 android.AssertDeepEquals(t, "qux exports (optional)", []string{}, optionalSdkLibs)
Colin Cross1d2b6b32021-06-01 13:18:08 -0700174 }
Wei Li1e73c652021-12-06 13:35:11 -0800175
176 fooDexJar := result.ModuleForTests("foo", "android_common").Rule("d8")
177 // tests if kotlinc generated files are NOT excluded from output of foo.
178 android.AssertStringDoesNotContain(t, "foo dex", fooDexJar.BuildParams.Args["mergeZipsFlags"], "-stripFile META-INF/*.kotlin_module")
179
180 barDexJar := result.ModuleForTests("bar", "android_common").Rule("d8")
181 // tests if kotlinc generated files are excluded from output of bar.
182 android.AssertStringDoesContain(t, "bar dex", barDexJar.BuildParams.Args["mergeZipsFlags"], "-stripFile META-INF/*.kotlin_module")
Colin Cross1d2b6b32021-06-01 13:18:08 -0700183}
184
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000185func TestJavaSdkLibrary_UpdatableLibrary(t *testing.T) {
186 result := android.GroupFixturePreparers(
187 prepareForJavaTest,
188 PrepareForTestWithJavaSdkLibraryFiles,
189 FixtureWithPrebuiltApis(map[string][]string{
190 "28": {"foo"},
191 "29": {"foo"},
192 "30": {"foo", "fooUpdatable", "fooUpdatableErr"},
193 }),
Pedro Loureiroc3621422021-09-28 15:40:23 +0000194 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Pedro Loureirob638c622021-12-22 15:28:05 +0000195 variables.Platform_version_active_codenames = []string{"Tiramisu", "U", "V", "W", "X"}
Pedro Loureiroc3621422021-09-28 15:40:23 +0000196 }),
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000197 ).RunTestWithBp(t,
198 `
199 java_sdk_library {
200 name: "fooUpdatable",
201 srcs: ["a.java", "b.java"],
202 api_packages: ["foo"],
Pedro Loureiroc3621422021-09-28 15:40:23 +0000203 on_bootclasspath_since: "U",
204 on_bootclasspath_before: "V",
205 min_device_sdk: "W",
Pedro Loureirob638c622021-12-22 15:28:05 +0000206 max_device_sdk: "X",
Pedro Loureiroc3621422021-09-28 15:40:23 +0000207 min_sdk_version: "S",
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000208 }
209 java_sdk_library {
210 name: "foo",
211 srcs: ["a.java", "b.java"],
212 api_packages: ["foo"],
213 }
214`)
Pedro Loureirob638c622021-12-22 15:28:05 +0000215
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000216 // test that updatability attributes are passed on correctly
217 fooUpdatable := result.ModuleForTests("fooUpdatable.xml", "android_common").Rule("java_sdk_xml")
Pedro Loureirob638c622021-12-22 15:28:05 +0000218 android.AssertStringDoesContain(t, "fooUpdatable.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `on-bootclasspath-since=\"U\"`)
219 android.AssertStringDoesContain(t, "fooUpdatable.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `on-bootclasspath-before=\"V\"`)
220 android.AssertStringDoesContain(t, "fooUpdatable.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `min-device-sdk=\"W\"`)
221 android.AssertStringDoesContain(t, "fooUpdatable.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `max-device-sdk=\"X\"`)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000222
223 // double check that updatability attributes are not written if they don't exist in the bp file
224 // the permissions file for the foo library defined above
225 fooPermissions := result.ModuleForTests("foo.xml", "android_common").Rule("java_sdk_xml")
Pedro Loureiroba6682f2021-10-29 09:32:32 +0000226 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooPermissions.RuleParams.Command, `on-bootclasspath-since`)
227 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooPermissions.RuleParams.Command, `on-bootclasspath-before`)
228 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooPermissions.RuleParams.Command, `min-device-sdk`)
229 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooPermissions.RuleParams.Command, `max-device-sdk`)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000230}
231
Pedro Loureiroc3621422021-09-28 15:40:23 +0000232func TestJavaSdkLibrary_UpdatableLibrary_Validation_ValidVersion(t *testing.T) {
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000233 android.GroupFixturePreparers(
234 prepareForJavaTest,
235 PrepareForTestWithJavaSdkLibraryFiles,
236 FixtureWithPrebuiltApis(map[string][]string{
237 "30": {"fooUpdatable", "fooUpdatableErr"},
238 }),
239 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
240 []string{
241 `on_bootclasspath_since: "aaa" could not be parsed as an integer and is not a recognized codename`,
242 `on_bootclasspath_before: "bbc" could not be parsed as an integer and is not a recognized codename`,
243 `min_device_sdk: "ccc" could not be parsed as an integer and is not a recognized codename`,
Pedro Loureirob638c622021-12-22 15:28:05 +0000244 `max_device_sdk: "current" is not an allowed value for this attribute`,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000245 })).RunTestWithBp(t,
246 `
247 java_sdk_library {
248 name: "fooUpdatableErr",
249 srcs: ["a.java", "b.java"],
250 api_packages: ["foo"],
Pedro Loureiroc3621422021-09-28 15:40:23 +0000251 on_bootclasspath_since: "aaa",
252 on_bootclasspath_before: "bbc",
253 min_device_sdk: "ccc",
Pedro Loureirob638c622021-12-22 15:28:05 +0000254 max_device_sdk: "current",
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000255 }
256`)
257}
258
Pedro Loureiroc3621422021-09-28 15:40:23 +0000259func TestJavaSdkLibrary_UpdatableLibrary_Validation_AtLeastTAttributes(t *testing.T) {
260 android.GroupFixturePreparers(
261 prepareForJavaTest,
262 PrepareForTestWithJavaSdkLibraryFiles,
263 FixtureWithPrebuiltApis(map[string][]string{
264 "28": {"foo"},
265 }),
266 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
267 []string{
268 "on_bootclasspath_since: Attribute value needs to be at least T",
269 "on_bootclasspath_before: Attribute value needs to be at least T",
270 "min_device_sdk: Attribute value needs to be at least T",
271 "max_device_sdk: Attribute value needs to be at least T",
272 },
273 )).RunTestWithBp(t,
274 `
275 java_sdk_library {
276 name: "foo",
277 srcs: ["a.java", "b.java"],
278 api_packages: ["foo"],
279 on_bootclasspath_since: "S",
280 on_bootclasspath_before: "S",
281 min_device_sdk: "S",
282 max_device_sdk: "S",
283 min_sdk_version: "S",
284 }
285`)
286}
287
288func TestJavaSdkLibrary_UpdatableLibrary_Validation_MinAndMaxDeviceSdk(t *testing.T) {
289 android.GroupFixturePreparers(
290 prepareForJavaTest,
291 PrepareForTestWithJavaSdkLibraryFiles,
292 FixtureWithPrebuiltApis(map[string][]string{
293 "28": {"foo"},
294 }),
295 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
296 variables.Platform_version_active_codenames = []string{"Tiramisu", "U", "V"}
297 }),
298 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
299 []string{
300 "min_device_sdk can't be greater than max_device_sdk",
301 },
302 )).RunTestWithBp(t,
303 `
304 java_sdk_library {
305 name: "foo",
306 srcs: ["a.java", "b.java"],
307 api_packages: ["foo"],
308 min_device_sdk: "V",
309 max_device_sdk: "U",
310 min_sdk_version: "S",
311 }
312`)
313}
314
315func TestJavaSdkLibrary_UpdatableLibrary_Validation_MinAndMaxDeviceSdkAndModuleMinSdk(t *testing.T) {
316 android.GroupFixturePreparers(
317 prepareForJavaTest,
318 PrepareForTestWithJavaSdkLibraryFiles,
319 FixtureWithPrebuiltApis(map[string][]string{
320 "28": {"foo"},
321 }),
322 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
323 variables.Platform_version_active_codenames = []string{"Tiramisu", "U", "V"}
324 }),
325 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
326 []string{
327 regexp.QuoteMeta("min_device_sdk: Can't be less than module's min sdk (V)"),
328 regexp.QuoteMeta("max_device_sdk: Can't be less than module's min sdk (V)"),
329 },
330 )).RunTestWithBp(t,
331 `
332 java_sdk_library {
333 name: "foo",
334 srcs: ["a.java", "b.java"],
335 api_packages: ["foo"],
336 min_device_sdk: "U",
337 max_device_sdk: "U",
338 min_sdk_version: "V",
339 }
340`)
341}
342
343func TestJavaSdkLibrary_UpdatableLibrary_usesNewTag(t *testing.T) {
344 result := android.GroupFixturePreparers(
345 prepareForJavaTest,
346 PrepareForTestWithJavaSdkLibraryFiles,
347 FixtureWithPrebuiltApis(map[string][]string{
348 "30": {"foo"},
349 }),
350 ).RunTestWithBp(t,
351 `
352 java_sdk_library {
353 name: "foo",
354 srcs: ["a.java", "b.java"],
355 min_device_sdk: "Tiramisu",
356 min_sdk_version: "S",
357 }
358`)
359 // test that updatability attributes are passed on correctly
360 fooUpdatable := result.ModuleForTests("foo.xml", "android_common").Rule("java_sdk_xml")
Pedro Loureiro196d3e62021-12-22 19:53:01 +0000361 android.AssertStringDoesContain(t, "foo.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `<apex-library`)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000362 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `<library`)
363}
364
Colin Cross1d2b6b32021-06-01 13:18:08 -0700365func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
366 result := android.GroupFixturePreparers(
367 prepareForJavaTest,
368 PrepareForTestWithJavaSdkLibraryFiles,
369 FixtureWithLastReleaseApis("sdklib"),
370 ).RunTestWithBp(t, `
371 java_sdk_library {
372 name: "sdklib",
373 srcs: ["a.java"],
374 libs: ["lib"],
375 static_libs: ["static-lib"],
376 impl_only_libs: ["impl-only-lib"],
377 stub_only_libs: ["stub-only-lib"],
378 stub_only_static_libs: ["stub-only-static-lib"],
379 }
380 java_defaults {
381 name: "defaults",
382 srcs: ["a.java"],
383 sdk_version: "current",
384 }
385 java_library { name: "lib", defaults: ["defaults"] }
386 java_library { name: "static-lib", defaults: ["defaults"] }
387 java_library { name: "impl-only-lib", defaults: ["defaults"] }
388 java_library { name: "stub-only-lib", defaults: ["defaults"] }
389 java_library { name: "stub-only-static-lib", defaults: ["defaults"] }
390 `)
391 var expectations = []struct {
392 lib string
393 on_impl_classpath bool
394 on_stub_classpath bool
395 in_impl_combined bool
396 in_stub_combined bool
397 }{
398 {lib: "lib", on_impl_classpath: true},
399 {lib: "static-lib", in_impl_combined: true},
400 {lib: "impl-only-lib", on_impl_classpath: true},
401 {lib: "stub-only-lib", on_stub_classpath: true},
402 {lib: "stub-only-static-lib", in_stub_combined: true},
403 }
404 verify := func(sdklib, dep string, cp, combined bool) {
405 sdklibCp := result.ModuleForTests(sdklib, "android_common").Rule("javac").Args["classpath"]
406 expected := cp || combined // Every combined jar is also on the classpath.
407 android.AssertStringContainsEquals(t, "bad classpath for "+sdklib, sdklibCp, "/"+dep+".jar", expected)
408
409 combineJarInputs := result.ModuleForTests(sdklib, "android_common").Rule("combineJar").Inputs.Strings()
410 depPath := filepath.Join("out", "soong", ".intermediates", dep, "android_common", "turbine-combined", dep+".jar")
411 android.AssertStringListContainsEquals(t, "bad combined inputs for "+sdklib, combineJarInputs, depPath, combined)
412 }
413 for _, expectation := range expectations {
Colin Cross1d2b6b32021-06-01 13:18:08 -0700414 verify("sdklib.impl", expectation.lib, expectation.on_impl_classpath, expectation.in_impl_combined)
415
Jihoon Kang1147b312023-06-08 23:25:57 +0000416 stubName := apiScopePublic.sourceStubLibraryModuleName("sdklib")
Colin Cross1d2b6b32021-06-01 13:18:08 -0700417 verify(stubName, expectation.lib, expectation.on_stub_classpath, expectation.in_stub_combined)
418 }
419}
420
421func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
422 result := android.GroupFixturePreparers(
423 prepareForJavaTest,
424 PrepareForTestWithJavaSdkLibraryFiles,
425 FixtureWithLastReleaseApis("foo"),
426 ).RunTestWithBp(t, `
427 java_sdk_library {
428 name: "foo",
429 srcs: ["a.java"],
430 api_only: true,
431 public: {
432 enabled: true,
433 },
434 }
435
436 java_library {
437 name: "bar",
438 srcs: ["b.java"],
439 libs: ["foo"],
440 }
441 `)
442
443 // The bar library should depend on the stubs jar.
444 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
445 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
446 t.Errorf("expected %q, found %#q", expected, actual)
447 }
448}
449
Anton Hanssond78eb762021-09-21 15:25:12 +0100450func TestJavaSdkLibrary_AccessOutputFiles(t *testing.T) {
Colin Cross1d2b6b32021-06-01 13:18:08 -0700451 android.GroupFixturePreparers(
452 prepareForJavaTest,
453 PrepareForTestWithJavaSdkLibraryFiles,
454 FixtureWithLastReleaseApis("foo"),
455 ).RunTestWithBp(t, `
456 java_sdk_library {
457 name: "foo",
458 srcs: ["a.java"],
459 api_packages: ["foo"],
Anton Hanssond78eb762021-09-21 15:25:12 +0100460 annotations_enabled: true,
461 public: {
462 enabled: true,
463 },
464 }
465 java_library {
466 name: "bar",
467 srcs: ["b.java", ":foo{.public.stubs.source}"],
468 java_resources: [":foo{.public.annotations.zip}"],
469 }
470 `)
471}
472
473func TestJavaSdkLibrary_AccessOutputFiles_NoAnnotations(t *testing.T) {
474 android.GroupFixturePreparers(
475 prepareForJavaTest,
476 PrepareForTestWithJavaSdkLibraryFiles,
477 FixtureWithLastReleaseApis("foo"),
478 ).
479 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "bar" variant "android_common": path dependency ":foo{.public.annotations.zip}": annotations.zip not available for api scope public`)).
480 RunTestWithBp(t, `
481 java_sdk_library {
482 name: "foo",
483 srcs: ["a.java"],
484 api_packages: ["foo"],
Colin Cross1d2b6b32021-06-01 13:18:08 -0700485 public: {
486 enabled: true,
487 },
488 }
489
490 java_library {
491 name: "bar",
492 srcs: ["b.java", ":foo{.public.stubs.source}"],
Anton Hanssond78eb762021-09-21 15:25:12 +0100493 java_resources: [":foo{.public.annotations.zip}"],
Colin Cross1d2b6b32021-06-01 13:18:08 -0700494 }
495 `)
496}
497
498func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
499 android.GroupFixturePreparers(
500 prepareForJavaTest,
501 PrepareForTestWithJavaSdkLibraryFiles,
502 FixtureWithLastReleaseApis("foo"),
503 ).
504 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
505 RunTestWithBp(t, `
506 java_sdk_library {
507 name: "foo",
508 srcs: ["a.java"],
509 api_packages: ["foo"],
510 public: {
511 enabled: true,
512 },
513 }
514
515 java_library {
516 name: "bar",
517 srcs: ["b.java", ":foo{.system.stubs.source}"],
518 }
519 `)
520}
521
522func TestJavaSdkLibrary_Deps(t *testing.T) {
523 result := android.GroupFixturePreparers(
524 prepareForJavaTest,
525 PrepareForTestWithJavaSdkLibraryFiles,
526 FixtureWithLastReleaseApis("sdklib"),
527 ).RunTestWithBp(t, `
528 java_sdk_library {
529 name: "sdklib",
530 srcs: ["a.java"],
531 sdk_version: "none",
532 system_modules: "none",
533 public: {
534 enabled: true,
535 },
536 }
537 `)
538
539 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
540 `dex2oatd`,
Paul Duffin958806b2022-05-16 13:10:47 +0000541 `sdklib-removed.api.public.latest`,
542 `sdklib.api.public.latest`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700543 `sdklib.impl`,
544 `sdklib.stubs`,
545 `sdklib.stubs.source`,
546 `sdklib.xml`,
547 })
548}
549
550func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
551 prepareForJavaTest.RunTestWithBp(t, `
552 java_sdk_library_import {
553 name: "foo",
554 public: {
555 jars: ["a.jar"],
556 stub_srcs: ["a.java"],
557 current_api: "api/current.txt",
558 removed_api: "api/removed.txt",
Anton Hanssond78eb762021-09-21 15:25:12 +0100559 annotations: "x/annotations.zip",
Colin Cross1d2b6b32021-06-01 13:18:08 -0700560 },
561 }
562
563 java_library {
564 name: "bar",
565 srcs: [":foo{.public.stubs.source}"],
566 java_resources: [
567 ":foo{.public.api.txt}",
568 ":foo{.public.removed-api.txt}",
Anton Hanssond78eb762021-09-21 15:25:12 +0100569 ":foo{.public.annotations.zip}",
Colin Cross1d2b6b32021-06-01 13:18:08 -0700570 ],
571 }
572 `)
573}
574
575func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
576 bp := `
577 java_sdk_library_import {
578 name: "foo",
579 public: {
580 jars: ["a.jar"],
581 },
582 }
583 `
584
585 t.Run("stubs.source", func(t *testing.T) {
586 prepareForJavaTest.
587 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
588 RunTestWithBp(t, bp+`
589 java_library {
590 name: "bar",
591 srcs: [":foo{.public.stubs.source}"],
592 java_resources: [
593 ":foo{.public.api.txt}",
594 ":foo{.public.removed-api.txt}",
595 ],
596 }
597 `)
598 })
599
600 t.Run("api.txt", func(t *testing.T) {
601 prepareForJavaTest.
602 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
603 RunTestWithBp(t, bp+`
604 java_library {
605 name: "bar",
606 srcs: ["a.java"],
607 java_resources: [
608 ":foo{.public.api.txt}",
609 ],
610 }
611 `)
612 })
613
614 t.Run("removed-api.txt", func(t *testing.T) {
615 prepareForJavaTest.
616 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
617 RunTestWithBp(t, bp+`
618 java_library {
619 name: "bar",
620 srcs: ["a.java"],
621 java_resources: [
622 ":foo{.public.removed-api.txt}",
623 ],
624 }
625 `)
626 })
627}
628
629func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
630 prepareForJavaTest.
631 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
632 RunTestWithBp(t, `
633 java_sdk_library {
634 name: "foo",
635 srcs: ["a.java", "b.java"],
636 api_packages: ["foo"],
637 // Explicitly disable public to test the check that ensures the set of enabled
638 // scopes is consistent.
639 public: {
640 enabled: false,
641 },
642 system: {
643 enabled: true,
644 },
645 }
646 `)
647}
648
649func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
650 android.GroupFixturePreparers(
651 prepareForJavaTest,
652 PrepareForTestWithJavaSdkLibraryFiles,
653 FixtureWithLastReleaseApis("foo"),
654 ).RunTestWithBp(t, `
655 java_sdk_library {
656 name: "foo",
657 srcs: ["a.java", "b.java"],
658 api_packages: ["foo"],
659 system: {
660 enabled: true,
661 sdk_version: "module_current",
662 },
663 }
664 `)
665}
666
667func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
668 android.GroupFixturePreparers(
669 prepareForJavaTest,
670 PrepareForTestWithJavaSdkLibraryFiles,
671 FixtureWithLastReleaseApis("foo"),
672 ).RunTestWithBp(t, `
673 java_sdk_library {
674 name: "foo",
675 srcs: ["a.java", "b.java"],
676 api_packages: ["foo"],
677 system: {
678 enabled: true,
679 },
680 module_lib: {
681 enabled: true,
682 },
683 }
684 `)
685}
686
687func TestJavaSdkLibrary_SystemServer(t *testing.T) {
688 android.GroupFixturePreparers(
689 prepareForJavaTest,
690 PrepareForTestWithJavaSdkLibraryFiles,
691 FixtureWithLastReleaseApis("foo"),
692 ).RunTestWithBp(t, `
693 java_sdk_library {
694 name: "foo",
695 srcs: ["a.java", "b.java"],
696 api_packages: ["foo"],
697 system: {
698 enabled: true,
699 },
700 system_server: {
701 enabled: true,
702 },
703 }
704 `)
705}
706
Paul Duffind0b9fca2022-09-30 18:11:41 +0100707func TestJavaSdkLibrary_SystemServer_AccessToStubScopeLibs(t *testing.T) {
708 result := android.GroupFixturePreparers(
709 prepareForJavaTest,
710 PrepareForTestWithJavaSdkLibraryFiles,
711 FixtureWithLastReleaseApis("foo-public", "foo-system", "foo-module-lib", "foo-system-server"),
712 ).RunTestWithBp(t, `
713 java_sdk_library {
714 name: "foo-public",
715 srcs: ["a.java"],
716 api_packages: ["foo"],
717 public: {
718 enabled: true,
719 },
720 }
721
722 java_sdk_library {
723 name: "foo-system",
724 srcs: ["a.java"],
725 api_packages: ["foo"],
726 system: {
727 enabled: true,
728 },
729 }
730
731 java_sdk_library {
732 name: "foo-module-lib",
733 srcs: ["a.java"],
734 api_packages: ["foo"],
735 system: {
736 enabled: true,
737 },
738 module_lib: {
739 enabled: true,
740 },
741 }
742
743 java_sdk_library {
744 name: "foo-system-server",
745 srcs: ["a.java"],
746 api_packages: ["foo"],
747 system_server: {
748 enabled: true,
749 },
750 }
751
752 java_library {
753 name: "bar",
754 srcs: ["a.java"],
755 libs: ["foo-public", "foo-system", "foo-module-lib", "foo-system-server"],
756 sdk_version: "system_server_current",
757 }
758 `)
759
760 stubsPath := func(name string, scope *apiScope) string {
761 name = scope.stubsLibraryModuleName(name)
762 return fmt.Sprintf("out/soong/.intermediates/%[1]s/android_common/turbine-combined/%[1]s.jar", name)
763 }
764
765 // The bar library should depend on the highest (where system server is highest and public is
766 // lowest) API scopes provided by each of the foo-* modules. The highest API scope provided by the
767 // foo-<x> module is <x>.
768 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
769 stubLibraries := []string{
770 stubsPath("foo-public", apiScopePublic),
771 stubsPath("foo-system", apiScopeSystem),
772 stubsPath("foo-module-lib", apiScopeModuleLib),
773 stubsPath("foo-system-server", apiScopeSystemServer),
774 }
775 expectedPattern := fmt.Sprintf(`^-classpath .*:\Q%s\E$`, strings.Join(stubLibraries, ":"))
776 if expected, actual := expectedPattern, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
777 t.Errorf("expected pattern %q to match %#q", expected, actual)
778 }
779}
780
Colin Cross1d2b6b32021-06-01 13:18:08 -0700781func TestJavaSdkLibrary_MissingScope(t *testing.T) {
782 prepareForJavaTest.
783 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
784 RunTestWithBp(t, `
785 java_sdk_library {
786 name: "foo",
787 srcs: ["a.java"],
788 public: {
789 enabled: false,
790 },
791 }
792
793 java_library {
794 name: "baz",
795 srcs: ["a.java"],
796 libs: ["foo"],
797 sdk_version: "module_current",
798 }
799 `)
800}
801
802func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
803 android.GroupFixturePreparers(
804 prepareForJavaTest,
805 PrepareForTestWithJavaSdkLibraryFiles,
806 FixtureWithLastReleaseApis("foo"),
807 ).RunTestWithBp(t, `
808 java_sdk_library {
809 name: "foo",
810 srcs: ["a.java"],
811 system: {
812 enabled: true,
813 },
814 }
815
816 java_library {
817 name: "baz",
818 srcs: ["a.java"],
819 libs: ["foo"],
820 // foo does not have module-lib scope so it should fallback to system
821 sdk_version: "module_current",
822 }
823 `)
824}
825
826func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
827 result := android.GroupFixturePreparers(
828 prepareForJavaTest,
829 PrepareForTestWithJavaSdkLibraryFiles,
830 FixtureWithLastReleaseApis("foo"),
831 ).RunTestWithBp(t, `
832 java_sdk_library {
833 name: "foo",
834 srcs: ["a.java"],
835 system: {
836 enabled: true,
837 },
838 default_to_stubs: true,
839 }
840
841 java_library {
842 name: "baz",
843 srcs: ["a.java"],
844 libs: ["foo"],
845 // does not have sdk_version set, should fallback to module,
846 // which will then fallback to system because the module scope
847 // is not enabled.
848 }
849 `)
850 // The baz library should depend on the system stubs jar.
851 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
852 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
853 t.Errorf("expected %q, found %#q", expected, actual)
854 }
855}
856
857func TestJavaSdkLibraryImport(t *testing.T) {
858 result := prepareForJavaTest.RunTestWithBp(t, `
859 java_library {
860 name: "foo",
861 srcs: ["a.java"],
862 libs: ["sdklib"],
863 sdk_version: "current",
864 }
865
866 java_library {
867 name: "foo.system",
868 srcs: ["a.java"],
869 libs: ["sdklib"],
870 sdk_version: "system_current",
871 }
872
873 java_library {
874 name: "foo.test",
875 srcs: ["a.java"],
876 libs: ["sdklib"],
877 sdk_version: "test_current",
878 }
879
880 java_sdk_library_import {
881 name: "sdklib",
882 public: {
883 jars: ["a.jar"],
884 },
885 system: {
886 jars: ["b.jar"],
887 },
888 test: {
889 jars: ["c.jar"],
890 stub_srcs: ["c.java"],
891 },
892 }
893 `)
894
895 for _, scope := range []string{"", ".system", ".test"} {
896 fooModule := result.ModuleForTests("foo"+scope, "android_common")
897 javac := fooModule.Rule("javac")
898
899 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
900 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
901 }
902
903 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Jiakai Zhang204356f2021-09-09 08:12:46 +0000904 `dex2oatd`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700905 `prebuilt_sdklib.stubs`,
906 `prebuilt_sdklib.stubs.source.test`,
907 `prebuilt_sdklib.stubs.system`,
908 `prebuilt_sdklib.stubs.test`,
909 })
910}
911
912func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
913 result := android.GroupFixturePreparers(
914 prepareForJavaTest,
915 PrepareForTestWithJavaSdkLibraryFiles,
916 FixtureWithLastReleaseApis("sdklib"),
917 ).RunTestWithBp(t, `
918 java_sdk_library {
919 name: "sdklib",
920 srcs: ["a.java"],
921 sdk_version: "none",
922 system_modules: "none",
923 public: {
924 enabled: true,
925 },
926 }
927
928 java_sdk_library_import {
929 name: "sdklib",
930 public: {
931 jars: ["a.jar"],
932 },
933 }
934 `)
935
936 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
937 `dex2oatd`,
938 `prebuilt_sdklib`,
Paul Duffin958806b2022-05-16 13:10:47 +0000939 `sdklib-removed.api.public.latest`,
940 `sdklib.api.public.latest`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700941 `sdklib.impl`,
942 `sdklib.stubs`,
943 `sdklib.stubs.source`,
944 `sdklib.xml`,
945 })
946
947 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
948 `prebuilt_sdklib.stubs`,
949 `sdklib.impl`,
950 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
951 // dependency is added after prebuilts may have been renamed and so has to use
952 // the renamed name.
953 `sdklib.xml`,
954 })
955}
956
Paul Duffinbf4de042022-09-27 12:41:52 +0100957func testJavaSdkLibraryImport_Preferred(t *testing.T, prefer string, preparer android.FixturePreparer) {
Colin Cross1d2b6b32021-06-01 13:18:08 -0700958 result := android.GroupFixturePreparers(
959 prepareForJavaTest,
960 PrepareForTestWithJavaSdkLibraryFiles,
961 FixtureWithLastReleaseApis("sdklib"),
Paul Duffinbf4de042022-09-27 12:41:52 +0100962 preparer,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700963 ).RunTestWithBp(t, `
964 java_sdk_library {
965 name: "sdklib",
966 srcs: ["a.java"],
967 sdk_version: "none",
968 system_modules: "none",
969 public: {
970 enabled: true,
971 },
972 }
973
974 java_sdk_library_import {
975 name: "sdklib",
Paul Duffinbf4de042022-09-27 12:41:52 +0100976 `+prefer+`
Colin Cross1d2b6b32021-06-01 13:18:08 -0700977 public: {
978 jars: ["a.jar"],
Paul Duffinbf4de042022-09-27 12:41:52 +0100979 stub_srcs: ["a.java"],
980 current_api: "current.txt",
981 removed_api: "removed.txt",
982 annotations: "annotations.zip",
Colin Cross1d2b6b32021-06-01 13:18:08 -0700983 },
984 }
Paul Duffinbf4de042022-09-27 12:41:52 +0100985
986 java_library {
987 name: "combined",
988 static_libs: [
989 "sdklib.stubs",
990 ],
991 java_resources: [
992 ":sdklib.stubs.source",
993 ":sdklib{.public.api.txt}",
994 ":sdklib{.public.removed-api.txt}",
995 ":sdklib{.public.annotations.zip}",
996 ],
997 sdk_version: "none",
998 system_modules: "none",
999 }
1000
1001 java_library {
1002 name: "public",
1003 srcs: ["a.java"],
1004 libs: ["sdklib"],
1005 sdk_version: "current",
1006 }
Colin Cross1d2b6b32021-06-01 13:18:08 -07001007 `)
1008
1009 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Colin Cross1d2b6b32021-06-01 13:18:08 -07001010 `prebuilt_sdklib`,
Paul Duffin958806b2022-05-16 13:10:47 +00001011 `sdklib-removed.api.public.latest`,
1012 `sdklib.api.public.latest`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001013 `sdklib.impl`,
1014 `sdklib.stubs`,
1015 `sdklib.stubs.source`,
1016 `sdklib.xml`,
1017 })
1018
1019 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Jiakai Zhang204356f2021-09-09 08:12:46 +00001020 `dex2oatd`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001021 `prebuilt_sdklib.stubs`,
Paul Duffinbf4de042022-09-27 12:41:52 +01001022 `prebuilt_sdklib.stubs.source`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001023 `sdklib.impl`,
1024 `sdklib.xml`,
1025 })
Paul Duffinbf4de042022-09-27 12:41:52 +01001026
1027 // Make sure that dependencies on child modules use the prebuilt when preferred.
1028 CheckModuleDependencies(t, result.TestContext, "combined", "android_common", []string{
1029 // Each use of :sdklib{...} adds a dependency onto prebuilt_sdklib.
1030 `prebuilt_sdklib`,
1031 `prebuilt_sdklib`,
1032 `prebuilt_sdklib`,
1033 `prebuilt_sdklib.stubs`,
1034 `prebuilt_sdklib.stubs.source`,
1035 })
1036
1037 // Make sure that dependencies on sdklib that resolve to one of the child libraries use the
1038 // prebuilt library.
1039 public := result.ModuleForTests("public", "android_common")
1040 rule := public.Output("javac/public.jar")
1041 inputs := rule.Implicits.Strings()
1042 expected := "out/soong/.intermediates/prebuilt_sdklib.stubs/android_common/combined/sdklib.stubs.jar"
1043 if !android.InList(expected, inputs) {
1044 t.Errorf("expected %q to contain %q", inputs, expected)
1045 }
1046}
1047
1048func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
1049 t.Run("prefer", func(t *testing.T) {
1050 testJavaSdkLibraryImport_Preferred(t, "prefer: true,", android.NullFixturePreparer)
1051 })
1052
1053 t.Run("use_source_config_var", func(t *testing.T) {
1054 testJavaSdkLibraryImport_Preferred(t,
1055 "use_source_config_var: {config_namespace: \"acme\", var_name: \"use_source\"},",
1056 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1057 variables.VendorVars = map[string]map[string]string{
1058 "acme": {
1059 "use_source": "false",
1060 },
1061 }
1062 }))
1063 })
Colin Cross1d2b6b32021-06-01 13:18:08 -07001064}
1065
1066func TestJavaSdkLibraryEnforce(t *testing.T) {
1067 partitionToBpOption := func(partition string) string {
1068 switch partition {
1069 case "system":
1070 return ""
1071 case "vendor":
1072 return "soc_specific: true,"
1073 case "product":
1074 return "product_specific: true,"
1075 default:
1076 panic("Invalid partition group name: " + partition)
1077 }
1078 }
1079
1080 type testConfigInfo struct {
1081 libraryType string
1082 fromPartition string
1083 toPartition string
1084 enforceVendorInterface bool
1085 enforceProductInterface bool
1086 enforceJavaSdkLibraryCheck bool
1087 allowList []string
1088 }
1089
1090 createPreparer := func(info testConfigInfo) android.FixturePreparer {
1091 bpFileTemplate := `
1092 java_library {
1093 name: "foo",
1094 srcs: ["foo.java"],
1095 libs: ["bar"],
1096 sdk_version: "current",
1097 %s
1098 }
1099
1100 %s {
1101 name: "bar",
1102 srcs: ["bar.java"],
1103 sdk_version: "current",
1104 %s
1105 }
1106 `
1107
1108 bpFile := fmt.Sprintf(bpFileTemplate,
1109 partitionToBpOption(info.fromPartition),
1110 info.libraryType,
1111 partitionToBpOption(info.toPartition))
1112
1113 return android.GroupFixturePreparers(
1114 PrepareForTestWithJavaSdkLibraryFiles,
1115 FixtureWithLastReleaseApis("bar"),
1116 android.FixtureWithRootAndroidBp(bpFile),
1117 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1118 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
1119 if info.enforceVendorInterface {
1120 variables.DeviceVndkVersion = proptools.StringPtr("current")
1121 }
1122 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
1123 variables.InterPartitionJavaLibraryAllowList = info.allowList
1124 }),
1125 )
1126 }
1127
1128 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
1129 t.Run(fmt.Sprintf("%v", info), func(t *testing.T) {
1130 errorHandler := android.FixtureExpectsNoErrors
1131 if expectedErrorPattern != "" {
1132 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
1133 }
1134 android.GroupFixturePreparers(
1135 prepareForJavaTest,
1136 createPreparer(info),
1137 ).
1138 ExtendWithErrorHandler(errorHandler).
1139 RunTest(t)
1140 })
1141 }
1142
1143 errorMessage := "is not allowed across the partitions"
1144
1145 runTest(t, testConfigInfo{
1146 libraryType: "java_library",
1147 fromPartition: "product",
1148 toPartition: "system",
1149 enforceVendorInterface: true,
1150 enforceProductInterface: true,
1151 enforceJavaSdkLibraryCheck: false,
1152 }, "")
1153
1154 runTest(t, testConfigInfo{
1155 libraryType: "java_library",
1156 fromPartition: "product",
1157 toPartition: "system",
1158 enforceVendorInterface: true,
1159 enforceProductInterface: false,
1160 enforceJavaSdkLibraryCheck: true,
1161 }, "")
1162
1163 runTest(t, testConfigInfo{
1164 libraryType: "java_library",
1165 fromPartition: "product",
1166 toPartition: "system",
1167 enforceVendorInterface: true,
1168 enforceProductInterface: true,
1169 enforceJavaSdkLibraryCheck: true,
1170 }, errorMessage)
1171
1172 runTest(t, testConfigInfo{
1173 libraryType: "java_library",
1174 fromPartition: "vendor",
1175 toPartition: "system",
1176 enforceVendorInterface: true,
1177 enforceProductInterface: true,
1178 enforceJavaSdkLibraryCheck: true,
1179 }, errorMessage)
1180
1181 runTest(t, testConfigInfo{
1182 libraryType: "java_library",
1183 fromPartition: "vendor",
1184 toPartition: "system",
1185 enforceVendorInterface: true,
1186 enforceProductInterface: true,
1187 enforceJavaSdkLibraryCheck: true,
1188 allowList: []string{"bar"},
1189 }, "")
1190
1191 runTest(t, testConfigInfo{
1192 libraryType: "java_library",
1193 fromPartition: "vendor",
1194 toPartition: "product",
1195 enforceVendorInterface: true,
1196 enforceProductInterface: true,
1197 enforceJavaSdkLibraryCheck: true,
1198 }, errorMessage)
1199
1200 runTest(t, testConfigInfo{
1201 libraryType: "java_sdk_library",
1202 fromPartition: "product",
1203 toPartition: "system",
1204 enforceVendorInterface: true,
1205 enforceProductInterface: true,
1206 enforceJavaSdkLibraryCheck: true,
1207 }, "")
1208
1209 runTest(t, testConfigInfo{
1210 libraryType: "java_sdk_library",
1211 fromPartition: "vendor",
1212 toPartition: "system",
1213 enforceVendorInterface: true,
1214 enforceProductInterface: true,
1215 enforceJavaSdkLibraryCheck: true,
1216 }, "")
1217
1218 runTest(t, testConfigInfo{
1219 libraryType: "java_sdk_library",
1220 fromPartition: "vendor",
1221 toPartition: "product",
1222 enforceVendorInterface: true,
1223 enforceProductInterface: true,
1224 enforceJavaSdkLibraryCheck: true,
1225 }, "")
1226}
Colin Cross30c491b2021-06-01 13:39:09 -07001227
1228func TestJavaSdkLibraryDist(t *testing.T) {
1229 result := android.GroupFixturePreparers(
1230 PrepareForTestWithJavaBuildComponents,
1231 PrepareForTestWithJavaDefaultModules,
1232 PrepareForTestWithJavaSdkLibraryFiles,
Colin Cross67c17ae2021-06-02 13:02:51 -07001233 FixtureWithLastReleaseApis(
1234 "sdklib_no_group",
1235 "sdklib_group_foo",
1236 "sdklib_owner_foo",
1237 "foo"),
Colin Cross30c491b2021-06-01 13:39:09 -07001238 ).RunTestWithBp(t, `
1239 java_sdk_library {
Colin Cross59b92bf2021-06-01 14:07:56 -07001240 name: "sdklib_no_group",
Colin Cross30c491b2021-06-01 13:39:09 -07001241 srcs: ["foo.java"],
1242 }
1243
1244 java_sdk_library {
Colin Cross986b69a2021-06-01 13:13:40 -07001245 name: "sdklib_group_foo",
Colin Cross986b69a2021-06-01 13:13:40 -07001246 srcs: ["foo.java"],
1247 dist_group: "foo",
1248 }
1249
1250 java_sdk_library {
Colin Cross30c491b2021-06-01 13:39:09 -07001251 name: "sdklib_owner_foo",
Colin Cross30c491b2021-06-01 13:39:09 -07001252 srcs: ["foo.java"],
1253 owner: "foo",
1254 }
1255
1256 java_sdk_library {
1257 name: "sdklib_stem_foo",
Colin Cross30c491b2021-06-01 13:39:09 -07001258 srcs: ["foo.java"],
1259 dist_stem: "foo",
1260 }
Colin Cross30c491b2021-06-01 13:39:09 -07001261 `)
1262
1263 type testCase struct {
1264 module string
1265 distDir string
1266 distStem string
1267 }
1268 testCases := []testCase{
1269 {
Colin Cross59b92bf2021-06-01 14:07:56 -07001270 module: "sdklib_no_group",
Colin Cross3dd66252021-06-01 14:05:09 -07001271 distDir: "apistubs/unknown/public",
Colin Cross59b92bf2021-06-01 14:07:56 -07001272 distStem: "sdklib_no_group.jar",
Colin Cross30c491b2021-06-01 13:39:09 -07001273 },
1274 {
Colin Cross986b69a2021-06-01 13:13:40 -07001275 module: "sdklib_group_foo",
1276 distDir: "apistubs/foo/public",
1277 distStem: "sdklib_group_foo.jar",
1278 },
1279 {
Colin Cross59b92bf2021-06-01 14:07:56 -07001280 // Owner doesn't affect distDir after b/186723288.
Colin Cross30c491b2021-06-01 13:39:09 -07001281 module: "sdklib_owner_foo",
Colin Cross59b92bf2021-06-01 14:07:56 -07001282 distDir: "apistubs/unknown/public",
Colin Cross30c491b2021-06-01 13:39:09 -07001283 distStem: "sdklib_owner_foo.jar",
1284 },
1285 {
1286 module: "sdklib_stem_foo",
Colin Cross3dd66252021-06-01 14:05:09 -07001287 distDir: "apistubs/unknown/public",
Colin Cross30c491b2021-06-01 13:39:09 -07001288 distStem: "foo.jar",
1289 },
Colin Cross30c491b2021-06-01 13:39:09 -07001290 }
1291
1292 for _, tt := range testCases {
1293 t.Run(tt.module, func(t *testing.T) {
1294 m := result.ModuleForTests(tt.module+".stubs", "android_common").Module().(*Library)
1295 dists := m.Dists()
1296 if len(dists) != 1 {
1297 t.Fatalf("expected exactly 1 dist entry, got %d", len(dists))
1298 }
1299 if g, w := String(dists[0].Dir), tt.distDir; g != w {
1300 t.Errorf("expected dist dir %q, got %q", w, g)
1301 }
1302 if g, w := String(dists[0].Dest), tt.distStem; g != w {
1303 t.Errorf("expected dist stem %q, got %q", w, g)
1304 }
1305 })
1306 }
1307}
satayev8f088b02021-12-06 11:40:46 +00001308
1309func TestSdkLibrary_CheckMinSdkVersion(t *testing.T) {
1310 preparer := android.GroupFixturePreparers(
1311 PrepareForTestWithJavaBuildComponents,
1312 PrepareForTestWithJavaDefaultModules,
1313 PrepareForTestWithJavaSdkLibraryFiles,
1314 )
1315
1316 preparer.RunTestWithBp(t, `
1317 java_sdk_library {
1318 name: "sdklib",
1319 srcs: ["a.java"],
1320 static_libs: ["util"],
1321 min_sdk_version: "30",
1322 unsafe_ignore_missing_latest_api: true,
1323 }
1324
1325 java_library {
1326 name: "util",
1327 srcs: ["a.java"],
1328 min_sdk_version: "30",
1329 }
1330 `)
1331
1332 preparer.
1333 RunTestWithBp(t, `
1334 java_sdk_library {
1335 name: "sdklib",
1336 srcs: ["a.java"],
1337 libs: ["util"],
1338 impl_only_libs: ["util"],
1339 stub_only_libs: ["util"],
1340 stub_only_static_libs: ["util"],
1341 min_sdk_version: "30",
1342 unsafe_ignore_missing_latest_api: true,
1343 }
1344
1345 java_library {
1346 name: "util",
1347 srcs: ["a.java"],
1348 }
1349 `)
1350
1351 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "util".*should support min_sdk_version\(30\)`)).
1352 RunTestWithBp(t, `
1353 java_sdk_library {
1354 name: "sdklib",
1355 srcs: ["a.java"],
1356 static_libs: ["util"],
1357 min_sdk_version: "30",
1358 unsafe_ignore_missing_latest_api: true,
1359 }
1360
1361 java_library {
1362 name: "util",
1363 srcs: ["a.java"],
1364 min_sdk_version: "31",
1365 }
1366 `)
1367
1368 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "another_util".*should support min_sdk_version\(30\)`)).
1369 RunTestWithBp(t, `
1370 java_sdk_library {
1371 name: "sdklib",
1372 srcs: ["a.java"],
1373 static_libs: ["util"],
1374 min_sdk_version: "30",
1375 unsafe_ignore_missing_latest_api: true,
1376 }
1377
1378 java_library {
1379 name: "util",
1380 srcs: ["a.java"],
1381 static_libs: ["another_util"],
1382 min_sdk_version: "30",
1383 }
1384
1385 java_library {
1386 name: "another_util",
1387 srcs: ["a.java"],
1388 min_sdk_version: "31",
1389 }
1390 `)
1391}
Nikita Ioffed732da72022-11-21 12:38:25 +00001392
1393func TestJavaSdkLibrary_StubOnlyLibs_PassedToDroidstubs(t *testing.T) {
1394 result := android.GroupFixturePreparers(
1395 prepareForJavaTest,
1396 PrepareForTestWithJavaSdkLibraryFiles,
1397 FixtureWithLastReleaseApis("foo"),
1398 ).RunTestWithBp(t, `
1399 java_sdk_library {
1400 name: "foo",
1401 srcs: ["a.java"],
1402 public: {
1403 enabled: true,
1404 },
1405 stub_only_libs: ["bar-lib"],
1406 }
1407
1408 java_library {
1409 name: "bar-lib",
1410 srcs: ["b.java"],
1411 }
1412 `)
1413
1414 // The foo.stubs.source should depend on bar-lib
1415 fooStubsSources := result.ModuleForTests("foo.stubs.source", "android_common").Module().(*Droidstubs)
1416 android.AssertStringListContains(t, "foo stubs should depend on bar-lib", fooStubsSources.Javadoc.properties.Libs, "bar-lib")
1417}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001418
1419func TestJavaSdkLibrary_ApiLibrary(t *testing.T) {
1420 result := android.GroupFixturePreparers(
1421 prepareForJavaTest,
1422 PrepareForTestWithJavaSdkLibraryFiles,
1423 FixtureWithLastReleaseApis("foo"),
1424 android.FixtureModifyConfig(func(config android.Config) {
1425 config.SetApiLibraries([]string{"foo"})
1426 }),
1427 ).RunTestWithBp(t, `
1428 java_sdk_library {
1429 name: "foo",
1430 srcs: ["a.java", "b.java"],
1431 api_packages: ["foo"],
1432 system: {
1433 enabled: true,
1434 },
1435 module_lib: {
1436 enabled: true,
1437 },
1438 test: {
1439 enabled: true,
1440 },
1441 }
1442 `)
1443
1444 testCases := []struct {
1445 scope *apiScope
1446 apiContributions []string
1447 depApiSrcs string
1448 }{
1449 {
1450 scope: apiScopePublic,
1451 apiContributions: []string{"foo.stubs.source.api.contribution"},
1452 depApiSrcs: "android_stubs_current.from-text",
1453 },
1454 {
1455 scope: apiScopeSystem,
1456 apiContributions: []string{"foo.stubs.source.system.api.contribution", "foo.stubs.source.api.contribution"},
1457 depApiSrcs: "android_system_stubs_current.from-text",
1458 },
1459 {
1460 scope: apiScopeTest,
1461 apiContributions: []string{"foo.stubs.source.test.api.contribution", "foo.stubs.source.system.api.contribution", "foo.stubs.source.api.contribution"},
1462 depApiSrcs: "android_test_stubs_current.from-text",
1463 },
1464 {
1465 scope: apiScopeModuleLib,
1466 apiContributions: []string{"foo.stubs.source.module_lib.api.contribution", "foo.stubs.source.system.api.contribution", "foo.stubs.source.api.contribution"},
1467 depApiSrcs: "android_module_lib_stubs_current_full.from-text",
1468 },
1469 }
1470
1471 for _, c := range testCases {
1472 m := result.ModuleForTests(c.scope.apiLibraryModuleName("foo"), "android_common").Module().(*ApiLibrary)
1473 android.AssertArrayString(t, "Module expected to contain api contributions", c.apiContributions, m.properties.Api_contributions)
1474 android.AssertStringEquals(t, "Module expected to contain full api surface api library", c.depApiSrcs, *m.properties.Dep_api_srcs)
1475 }
1476}