blob: fb584c5c70d525718557e769974d3098144d733a [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 }),
Jihoon Kangbd093452023-12-26 19:08:01 +000041 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
42 variables.BuildFlags = map[string]string{
43 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
44 }
45 }),
Colin Cross1d2b6b32021-06-01 13:18:08 -070046 ).RunTestWithBp(t, `
47 droiddoc_exported_dir {
48 name: "droiddoc-templates-sdk",
49 path: ".",
50 }
51 java_sdk_library {
52 name: "foo",
53 srcs: ["a.java", "b.java"],
54 api_packages: ["foo"],
55 }
56 java_sdk_library {
57 name: "bar",
58 srcs: ["a.java", "b.java"],
59 api_packages: ["bar"],
Wei Li1e73c652021-12-06 13:35:11 -080060 exclude_kotlinc_generated_files: true,
Colin Cross1d2b6b32021-06-01 13:18:08 -070061 }
62 java_library {
63 name: "baz",
64 srcs: ["c.java"],
65 libs: ["foo", "bar.stubs"],
66 sdk_version: "system_current",
67 }
68 java_sdk_library {
69 name: "barney",
70 srcs: ["c.java"],
71 api_only: true,
72 }
73 java_sdk_library {
74 name: "betty",
75 srcs: ["c.java"],
76 shared_library: false,
77 }
78 java_sdk_library_import {
79 name: "quuz",
80 public: {
81 jars: ["c.jar"],
Jihoon Kang71c86832023-09-13 01:01:53 +000082 current_api: "api/current.txt",
83 removed_api: "api/removed.txt",
Colin Cross1d2b6b32021-06-01 13:18:08 -070084 },
85 }
86 java_sdk_library_import {
87 name: "fred",
88 public: {
89 jars: ["b.jar"],
90 },
91 }
92 java_sdk_library_import {
93 name: "wilma",
94 public: {
95 jars: ["b.jar"],
96 },
97 shared_library: false,
98 }
99 java_library {
100 name: "qux",
101 srcs: ["c.java"],
102 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
103 sdk_version: "system_current",
104 }
105 java_library {
106 name: "baz-test",
107 srcs: ["c.java"],
108 libs: ["foo"],
109 sdk_version: "test_current",
110 }
111 java_library {
112 name: "baz-29",
113 srcs: ["c.java"],
114 libs: ["foo"],
115 sdk_version: "system_29",
116 }
117 java_library {
118 name: "baz-module-30",
119 srcs: ["c.java"],
120 libs: ["foo"],
121 sdk_version: "module_30",
122 }
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000123 `)
Colin Cross1d2b6b32021-06-01 13:18:08 -0700124
125 // check the existence of the internal modules
Paul Duffinb97b1572021-04-29 21:50:40 +0100126 foo := result.ModuleForTests("foo", "android_common")
Colin Cross1d2b6b32021-06-01 13:18:08 -0700127 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
128 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
129 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
130 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
131 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
132 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jihoon Kangd48abd52023-02-02 22:32:31 +0000133 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo")+".api.contribution", "")
Jihoon Kang1c92c3e2023-03-23 17:44:51 +0000134 result.ModuleForTests(apiScopePublic.apiLibraryModuleName("foo"), "android_common")
Colin Cross1d2b6b32021-06-01 13:18:08 -0700135 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
136 result.ModuleForTests("foo.api.public.28", "")
137 result.ModuleForTests("foo.api.system.28", "")
138 result.ModuleForTests("foo.api.test.28", "")
139
Colin Cross5a377182023-12-14 14:46:23 -0800140 exportedComponentsInfo, _ := android.SingletonModuleProvider(result, foo.Module(), android.ExportedComponentsInfoProvider)
Paul Duffinb97b1572021-04-29 21:50:40 +0100141 expectedFooExportedComponents := []string{
Jihoon Kang748a24d2024-03-20 21:29:39 +0000142 "foo-removed.api.combined.public.latest",
143 "foo-removed.api.combined.system.latest",
144 "foo.api.combined.public.latest",
145 "foo.api.combined.system.latest",
Paul Duffinb97b1572021-04-29 21:50:40 +0100146 "foo.stubs",
Jihoon Kangbd093452023-12-26 19:08:01 +0000147 "foo.stubs.exportable",
148 "foo.stubs.exportable.system",
149 "foo.stubs.exportable.test",
Paul Duffinb97b1572021-04-29 21:50:40 +0100150 "foo.stubs.source",
151 "foo.stubs.source.system",
152 "foo.stubs.source.test",
153 "foo.stubs.system",
154 "foo.stubs.test",
155 }
156 android.AssertArrayString(t, "foo exported components", expectedFooExportedComponents, exportedComponentsInfo.Components)
157
Colin Cross1d2b6b32021-06-01 13:18:08 -0700158 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
159 // tests if baz is actually linked to the stubs lib
160 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
161 // ... and not to the impl lib
162 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
163 // test if baz is not linked to the system variant of foo
164 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
165
166 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
167 // tests if baz-test is actually linked to the test stubs lib
168 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
169
170 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
171 // tests if baz-29 is actually linked to the system 29 stubs lib
172 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
173
174 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
175 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
176 android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar")
177
178 // test if baz has exported SDK lib names foo and bar to qux
179 qux := result.ModuleForTests("qux", "android_common")
180 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +0100181 requiredSdkLibs, optionalSdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimoviche4432872021-08-18 16:57:11 +0100182 android.AssertDeepEquals(t, "qux exports (required)", []string{"fred", "quuz", "foo", "bar"}, requiredSdkLibs)
Ulya Trafimovichfc0f6e32021-08-12 16:16:11 +0100183 android.AssertDeepEquals(t, "qux exports (optional)", []string{}, optionalSdkLibs)
Colin Cross1d2b6b32021-06-01 13:18:08 -0700184 }
Wei Li1e73c652021-12-06 13:35:11 -0800185
Jihoon Kang71c86832023-09-13 01:01:53 +0000186 // test if quuz have created the api_contribution module
187 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("quuz")+".api.contribution", "")
188
Wei Li1e73c652021-12-06 13:35:11 -0800189 fooDexJar := result.ModuleForTests("foo", "android_common").Rule("d8")
190 // tests if kotlinc generated files are NOT excluded from output of foo.
191 android.AssertStringDoesNotContain(t, "foo dex", fooDexJar.BuildParams.Args["mergeZipsFlags"], "-stripFile META-INF/*.kotlin_module")
192
193 barDexJar := result.ModuleForTests("bar", "android_common").Rule("d8")
194 // tests if kotlinc generated files are excluded from output of bar.
195 android.AssertStringDoesContain(t, "bar dex", barDexJar.BuildParams.Args["mergeZipsFlags"], "-stripFile META-INF/*.kotlin_module")
Colin Cross1d2b6b32021-06-01 13:18:08 -0700196}
197
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000198func TestJavaSdkLibrary_UpdatableLibrary(t *testing.T) {
199 result := android.GroupFixturePreparers(
200 prepareForJavaTest,
201 PrepareForTestWithJavaSdkLibraryFiles,
202 FixtureWithPrebuiltApis(map[string][]string{
203 "28": {"foo"},
204 "29": {"foo"},
205 "30": {"foo", "fooUpdatable", "fooUpdatableErr"},
206 }),
Pedro Loureiroc3621422021-09-28 15:40:23 +0000207 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Pedro Loureirob638c622021-12-22 15:28:05 +0000208 variables.Platform_version_active_codenames = []string{"Tiramisu", "U", "V", "W", "X"}
Pedro Loureiroc3621422021-09-28 15:40:23 +0000209 }),
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000210 ).RunTestWithBp(t,
211 `
212 java_sdk_library {
213 name: "fooUpdatable",
214 srcs: ["a.java", "b.java"],
215 api_packages: ["foo"],
Pedro Loureiroc3621422021-09-28 15:40:23 +0000216 on_bootclasspath_since: "U",
217 on_bootclasspath_before: "V",
218 min_device_sdk: "W",
Pedro Loureirob638c622021-12-22 15:28:05 +0000219 max_device_sdk: "X",
Pedro Loureiroc3621422021-09-28 15:40:23 +0000220 min_sdk_version: "S",
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000221 }
222 java_sdk_library {
223 name: "foo",
224 srcs: ["a.java", "b.java"],
225 api_packages: ["foo"],
226 }
227`)
Pedro Loureirob638c622021-12-22 15:28:05 +0000228
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000229 // test that updatability attributes are passed on correctly
230 fooUpdatable := result.ModuleForTests("fooUpdatable.xml", "android_common").Rule("java_sdk_xml")
Pedro Loureirob638c622021-12-22 15:28:05 +0000231 android.AssertStringDoesContain(t, "fooUpdatable.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `on-bootclasspath-since=\"U\"`)
232 android.AssertStringDoesContain(t, "fooUpdatable.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `on-bootclasspath-before=\"V\"`)
233 android.AssertStringDoesContain(t, "fooUpdatable.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `min-device-sdk=\"W\"`)
234 android.AssertStringDoesContain(t, "fooUpdatable.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `max-device-sdk=\"X\"`)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000235
236 // double check that updatability attributes are not written if they don't exist in the bp file
237 // the permissions file for the foo library defined above
238 fooPermissions := result.ModuleForTests("foo.xml", "android_common").Rule("java_sdk_xml")
Pedro Loureiroba6682f2021-10-29 09:32:32 +0000239 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooPermissions.RuleParams.Command, `on-bootclasspath-since`)
240 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooPermissions.RuleParams.Command, `on-bootclasspath-before`)
241 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooPermissions.RuleParams.Command, `min-device-sdk`)
242 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooPermissions.RuleParams.Command, `max-device-sdk`)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000243}
244
Pedro Loureiroc3621422021-09-28 15:40:23 +0000245func TestJavaSdkLibrary_UpdatableLibrary_Validation_ValidVersion(t *testing.T) {
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000246 android.GroupFixturePreparers(
247 prepareForJavaTest,
248 PrepareForTestWithJavaSdkLibraryFiles,
249 FixtureWithPrebuiltApis(map[string][]string{
250 "30": {"fooUpdatable", "fooUpdatableErr"},
251 }),
252 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
253 []string{
254 `on_bootclasspath_since: "aaa" could not be parsed as an integer and is not a recognized codename`,
255 `on_bootclasspath_before: "bbc" could not be parsed as an integer and is not a recognized codename`,
256 `min_device_sdk: "ccc" could not be parsed as an integer and is not a recognized codename`,
Pedro Loureirob638c622021-12-22 15:28:05 +0000257 `max_device_sdk: "current" is not an allowed value for this attribute`,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000258 })).RunTestWithBp(t,
259 `
260 java_sdk_library {
261 name: "fooUpdatableErr",
262 srcs: ["a.java", "b.java"],
263 api_packages: ["foo"],
Pedro Loureiroc3621422021-09-28 15:40:23 +0000264 on_bootclasspath_since: "aaa",
265 on_bootclasspath_before: "bbc",
266 min_device_sdk: "ccc",
Pedro Loureirob638c622021-12-22 15:28:05 +0000267 max_device_sdk: "current",
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000268 }
269`)
270}
271
Pedro Loureiroc3621422021-09-28 15:40:23 +0000272func TestJavaSdkLibrary_UpdatableLibrary_Validation_AtLeastTAttributes(t *testing.T) {
273 android.GroupFixturePreparers(
274 prepareForJavaTest,
275 PrepareForTestWithJavaSdkLibraryFiles,
276 FixtureWithPrebuiltApis(map[string][]string{
277 "28": {"foo"},
278 }),
279 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
280 []string{
281 "on_bootclasspath_since: Attribute value needs to be at least T",
282 "on_bootclasspath_before: Attribute value needs to be at least T",
283 "min_device_sdk: Attribute value needs to be at least T",
284 "max_device_sdk: Attribute value needs to be at least T",
285 },
286 )).RunTestWithBp(t,
287 `
288 java_sdk_library {
289 name: "foo",
290 srcs: ["a.java", "b.java"],
291 api_packages: ["foo"],
292 on_bootclasspath_since: "S",
293 on_bootclasspath_before: "S",
294 min_device_sdk: "S",
295 max_device_sdk: "S",
296 min_sdk_version: "S",
297 }
298`)
299}
300
301func TestJavaSdkLibrary_UpdatableLibrary_Validation_MinAndMaxDeviceSdk(t *testing.T) {
302 android.GroupFixturePreparers(
303 prepareForJavaTest,
304 PrepareForTestWithJavaSdkLibraryFiles,
305 FixtureWithPrebuiltApis(map[string][]string{
306 "28": {"foo"},
307 }),
308 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
309 variables.Platform_version_active_codenames = []string{"Tiramisu", "U", "V"}
310 }),
311 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
312 []string{
313 "min_device_sdk can't be greater than max_device_sdk",
314 },
315 )).RunTestWithBp(t,
316 `
317 java_sdk_library {
318 name: "foo",
319 srcs: ["a.java", "b.java"],
320 api_packages: ["foo"],
321 min_device_sdk: "V",
322 max_device_sdk: "U",
323 min_sdk_version: "S",
324 }
325`)
326}
327
328func TestJavaSdkLibrary_UpdatableLibrary_Validation_MinAndMaxDeviceSdkAndModuleMinSdk(t *testing.T) {
329 android.GroupFixturePreparers(
330 prepareForJavaTest,
331 PrepareForTestWithJavaSdkLibraryFiles,
332 FixtureWithPrebuiltApis(map[string][]string{
333 "28": {"foo"},
334 }),
335 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
336 variables.Platform_version_active_codenames = []string{"Tiramisu", "U", "V"}
337 }),
338 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
339 []string{
340 regexp.QuoteMeta("min_device_sdk: Can't be less than module's min sdk (V)"),
341 regexp.QuoteMeta("max_device_sdk: Can't be less than module's min sdk (V)"),
342 },
343 )).RunTestWithBp(t,
344 `
345 java_sdk_library {
346 name: "foo",
347 srcs: ["a.java", "b.java"],
348 api_packages: ["foo"],
349 min_device_sdk: "U",
350 max_device_sdk: "U",
351 min_sdk_version: "V",
352 }
353`)
354}
355
356func TestJavaSdkLibrary_UpdatableLibrary_usesNewTag(t *testing.T) {
357 result := android.GroupFixturePreparers(
358 prepareForJavaTest,
359 PrepareForTestWithJavaSdkLibraryFiles,
360 FixtureWithPrebuiltApis(map[string][]string{
361 "30": {"foo"},
362 }),
363 ).RunTestWithBp(t,
364 `
365 java_sdk_library {
366 name: "foo",
367 srcs: ["a.java", "b.java"],
368 min_device_sdk: "Tiramisu",
369 min_sdk_version: "S",
370 }
371`)
372 // test that updatability attributes are passed on correctly
373 fooUpdatable := result.ModuleForTests("foo.xml", "android_common").Rule("java_sdk_xml")
Pedro Loureiro196d3e62021-12-22 19:53:01 +0000374 android.AssertStringDoesContain(t, "foo.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `<apex-library`)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000375 android.AssertStringDoesNotContain(t, "foo.xml java_sdk_xml command", fooUpdatable.RuleParams.Command, `<library`)
376}
377
Colin Cross1d2b6b32021-06-01 13:18:08 -0700378func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
379 result := android.GroupFixturePreparers(
380 prepareForJavaTest,
381 PrepareForTestWithJavaSdkLibraryFiles,
382 FixtureWithLastReleaseApis("sdklib"),
383 ).RunTestWithBp(t, `
384 java_sdk_library {
385 name: "sdklib",
386 srcs: ["a.java"],
387 libs: ["lib"],
388 static_libs: ["static-lib"],
389 impl_only_libs: ["impl-only-lib"],
390 stub_only_libs: ["stub-only-lib"],
391 stub_only_static_libs: ["stub-only-static-lib"],
392 }
393 java_defaults {
394 name: "defaults",
395 srcs: ["a.java"],
396 sdk_version: "current",
397 }
398 java_library { name: "lib", defaults: ["defaults"] }
399 java_library { name: "static-lib", defaults: ["defaults"] }
400 java_library { name: "impl-only-lib", defaults: ["defaults"] }
401 java_library { name: "stub-only-lib", defaults: ["defaults"] }
402 java_library { name: "stub-only-static-lib", defaults: ["defaults"] }
403 `)
404 var expectations = []struct {
405 lib string
406 on_impl_classpath bool
407 on_stub_classpath bool
408 in_impl_combined bool
409 in_stub_combined bool
410 }{
411 {lib: "lib", on_impl_classpath: true},
412 {lib: "static-lib", in_impl_combined: true},
413 {lib: "impl-only-lib", on_impl_classpath: true},
414 {lib: "stub-only-lib", on_stub_classpath: true},
415 {lib: "stub-only-static-lib", in_stub_combined: true},
416 }
417 verify := func(sdklib, dep string, cp, combined bool) {
418 sdklibCp := result.ModuleForTests(sdklib, "android_common").Rule("javac").Args["classpath"]
419 expected := cp || combined // Every combined jar is also on the classpath.
420 android.AssertStringContainsEquals(t, "bad classpath for "+sdklib, sdklibCp, "/"+dep+".jar", expected)
421
422 combineJarInputs := result.ModuleForTests(sdklib, "android_common").Rule("combineJar").Inputs.Strings()
423 depPath := filepath.Join("out", "soong", ".intermediates", dep, "android_common", "turbine-combined", dep+".jar")
424 android.AssertStringListContainsEquals(t, "bad combined inputs for "+sdklib, combineJarInputs, depPath, combined)
425 }
426 for _, expectation := range expectations {
Colin Cross1d2b6b32021-06-01 13:18:08 -0700427 verify("sdklib.impl", expectation.lib, expectation.on_impl_classpath, expectation.in_impl_combined)
428
Jihoon Kang1147b312023-06-08 23:25:57 +0000429 stubName := apiScopePublic.sourceStubLibraryModuleName("sdklib")
Colin Cross1d2b6b32021-06-01 13:18:08 -0700430 verify(stubName, expectation.lib, expectation.on_stub_classpath, expectation.in_stub_combined)
431 }
432}
433
434func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
435 result := android.GroupFixturePreparers(
436 prepareForJavaTest,
437 PrepareForTestWithJavaSdkLibraryFiles,
438 FixtureWithLastReleaseApis("foo"),
439 ).RunTestWithBp(t, `
440 java_sdk_library {
441 name: "foo",
442 srcs: ["a.java"],
443 api_only: true,
444 public: {
445 enabled: true,
446 },
447 }
448
449 java_library {
450 name: "bar",
451 srcs: ["b.java"],
452 libs: ["foo"],
453 }
454 `)
455
456 // The bar library should depend on the stubs jar.
457 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
458 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
459 t.Errorf("expected %q, found %#q", expected, actual)
460 }
461}
462
Anton Hanssond78eb762021-09-21 15:25:12 +0100463func TestJavaSdkLibrary_AccessOutputFiles(t *testing.T) {
Colin Cross1d2b6b32021-06-01 13:18:08 -0700464 android.GroupFixturePreparers(
465 prepareForJavaTest,
466 PrepareForTestWithJavaSdkLibraryFiles,
467 FixtureWithLastReleaseApis("foo"),
468 ).RunTestWithBp(t, `
469 java_sdk_library {
470 name: "foo",
471 srcs: ["a.java"],
472 api_packages: ["foo"],
Anton Hanssond78eb762021-09-21 15:25:12 +0100473 annotations_enabled: true,
474 public: {
475 enabled: true,
476 },
477 }
478 java_library {
479 name: "bar",
480 srcs: ["b.java", ":foo{.public.stubs.source}"],
481 java_resources: [":foo{.public.annotations.zip}"],
482 }
483 `)
484}
485
486func TestJavaSdkLibrary_AccessOutputFiles_NoAnnotations(t *testing.T) {
487 android.GroupFixturePreparers(
488 prepareForJavaTest,
489 PrepareForTestWithJavaSdkLibraryFiles,
490 FixtureWithLastReleaseApis("foo"),
491 ).
492 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "bar" variant "android_common": path dependency ":foo{.public.annotations.zip}": annotations.zip not available for api scope public`)).
493 RunTestWithBp(t, `
494 java_sdk_library {
495 name: "foo",
496 srcs: ["a.java"],
497 api_packages: ["foo"],
Colin Cross1d2b6b32021-06-01 13:18:08 -0700498 public: {
499 enabled: true,
500 },
501 }
502
503 java_library {
504 name: "bar",
505 srcs: ["b.java", ":foo{.public.stubs.source}"],
Anton Hanssond78eb762021-09-21 15:25:12 +0100506 java_resources: [":foo{.public.annotations.zip}"],
Colin Cross1d2b6b32021-06-01 13:18:08 -0700507 }
508 `)
509}
510
511func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
512 android.GroupFixturePreparers(
513 prepareForJavaTest,
514 PrepareForTestWithJavaSdkLibraryFiles,
515 FixtureWithLastReleaseApis("foo"),
516 ).
517 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
518 RunTestWithBp(t, `
519 java_sdk_library {
520 name: "foo",
521 srcs: ["a.java"],
522 api_packages: ["foo"],
523 public: {
524 enabled: true,
525 },
526 }
527
528 java_library {
529 name: "bar",
530 srcs: ["b.java", ":foo{.system.stubs.source}"],
531 }
532 `)
533}
534
535func TestJavaSdkLibrary_Deps(t *testing.T) {
536 result := android.GroupFixturePreparers(
537 prepareForJavaTest,
538 PrepareForTestWithJavaSdkLibraryFiles,
539 FixtureWithLastReleaseApis("sdklib"),
Jihoon Kangbd093452023-12-26 19:08:01 +0000540 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
541 variables.BuildFlags = map[string]string{
542 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
543 }
544 }),
Colin Cross1d2b6b32021-06-01 13:18:08 -0700545 ).RunTestWithBp(t, `
546 java_sdk_library {
547 name: "sdklib",
548 srcs: ["a.java"],
549 sdk_version: "none",
550 system_modules: "none",
551 public: {
552 enabled: true,
553 },
554 }
555 `)
556
557 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
558 `dex2oatd`,
Jihoon Kang748a24d2024-03-20 21:29:39 +0000559 `sdklib-removed.api.combined.public.latest`,
560 `sdklib.api.combined.public.latest`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700561 `sdklib.impl`,
562 `sdklib.stubs`,
Jihoon Kangbd093452023-12-26 19:08:01 +0000563 `sdklib.stubs.exportable`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700564 `sdklib.stubs.source`,
565 `sdklib.xml`,
566 })
567}
568
569func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
570 prepareForJavaTest.RunTestWithBp(t, `
571 java_sdk_library_import {
572 name: "foo",
573 public: {
574 jars: ["a.jar"],
575 stub_srcs: ["a.java"],
576 current_api: "api/current.txt",
577 removed_api: "api/removed.txt",
Anton Hanssond78eb762021-09-21 15:25:12 +0100578 annotations: "x/annotations.zip",
Colin Cross1d2b6b32021-06-01 13:18:08 -0700579 },
580 }
581
582 java_library {
583 name: "bar",
584 srcs: [":foo{.public.stubs.source}"],
585 java_resources: [
586 ":foo{.public.api.txt}",
587 ":foo{.public.removed-api.txt}",
Anton Hanssond78eb762021-09-21 15:25:12 +0100588 ":foo{.public.annotations.zip}",
Colin Cross1d2b6b32021-06-01 13:18:08 -0700589 ],
590 }
591 `)
592}
593
594func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
595 bp := `
596 java_sdk_library_import {
597 name: "foo",
598 public: {
599 jars: ["a.jar"],
600 },
601 }
602 `
603
604 t.Run("stubs.source", func(t *testing.T) {
605 prepareForJavaTest.
606 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
607 RunTestWithBp(t, bp+`
608 java_library {
609 name: "bar",
610 srcs: [":foo{.public.stubs.source}"],
611 java_resources: [
612 ":foo{.public.api.txt}",
613 ":foo{.public.removed-api.txt}",
614 ],
615 }
616 `)
617 })
618
619 t.Run("api.txt", func(t *testing.T) {
620 prepareForJavaTest.
621 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
622 RunTestWithBp(t, bp+`
623 java_library {
624 name: "bar",
625 srcs: ["a.java"],
626 java_resources: [
627 ":foo{.public.api.txt}",
628 ],
629 }
630 `)
631 })
632
633 t.Run("removed-api.txt", func(t *testing.T) {
634 prepareForJavaTest.
635 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
636 RunTestWithBp(t, bp+`
637 java_library {
638 name: "bar",
639 srcs: ["a.java"],
640 java_resources: [
641 ":foo{.public.removed-api.txt}",
642 ],
643 }
644 `)
645 })
646}
647
648func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
649 prepareForJavaTest.
650 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
651 RunTestWithBp(t, `
652 java_sdk_library {
653 name: "foo",
654 srcs: ["a.java", "b.java"],
655 api_packages: ["foo"],
656 // Explicitly disable public to test the check that ensures the set of enabled
657 // scopes is consistent.
658 public: {
659 enabled: false,
660 },
661 system: {
662 enabled: true,
663 },
664 }
665 `)
666}
667
668func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
669 android.GroupFixturePreparers(
670 prepareForJavaTest,
671 PrepareForTestWithJavaSdkLibraryFiles,
672 FixtureWithLastReleaseApis("foo"),
673 ).RunTestWithBp(t, `
674 java_sdk_library {
675 name: "foo",
676 srcs: ["a.java", "b.java"],
677 api_packages: ["foo"],
678 system: {
679 enabled: true,
680 sdk_version: "module_current",
681 },
682 }
683 `)
684}
685
686func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
687 android.GroupFixturePreparers(
688 prepareForJavaTest,
689 PrepareForTestWithJavaSdkLibraryFiles,
690 FixtureWithLastReleaseApis("foo"),
691 ).RunTestWithBp(t, `
692 java_sdk_library {
693 name: "foo",
694 srcs: ["a.java", "b.java"],
695 api_packages: ["foo"],
696 system: {
697 enabled: true,
698 },
699 module_lib: {
700 enabled: true,
701 },
702 }
703 `)
704}
705
706func TestJavaSdkLibrary_SystemServer(t *testing.T) {
707 android.GroupFixturePreparers(
708 prepareForJavaTest,
709 PrepareForTestWithJavaSdkLibraryFiles,
710 FixtureWithLastReleaseApis("foo"),
711 ).RunTestWithBp(t, `
712 java_sdk_library {
713 name: "foo",
714 srcs: ["a.java", "b.java"],
715 api_packages: ["foo"],
716 system: {
717 enabled: true,
718 },
719 system_server: {
720 enabled: true,
721 },
722 }
723 `)
724}
725
Paul Duffind0b9fca2022-09-30 18:11:41 +0100726func TestJavaSdkLibrary_SystemServer_AccessToStubScopeLibs(t *testing.T) {
727 result := android.GroupFixturePreparers(
728 prepareForJavaTest,
729 PrepareForTestWithJavaSdkLibraryFiles,
730 FixtureWithLastReleaseApis("foo-public", "foo-system", "foo-module-lib", "foo-system-server"),
731 ).RunTestWithBp(t, `
732 java_sdk_library {
733 name: "foo-public",
734 srcs: ["a.java"],
735 api_packages: ["foo"],
736 public: {
737 enabled: true,
738 },
739 }
740
741 java_sdk_library {
742 name: "foo-system",
743 srcs: ["a.java"],
744 api_packages: ["foo"],
745 system: {
746 enabled: true,
747 },
748 }
749
750 java_sdk_library {
751 name: "foo-module-lib",
752 srcs: ["a.java"],
753 api_packages: ["foo"],
754 system: {
755 enabled: true,
756 },
757 module_lib: {
758 enabled: true,
759 },
760 }
761
762 java_sdk_library {
763 name: "foo-system-server",
764 srcs: ["a.java"],
765 api_packages: ["foo"],
766 system_server: {
767 enabled: true,
768 },
769 }
770
771 java_library {
772 name: "bar",
773 srcs: ["a.java"],
774 libs: ["foo-public", "foo-system", "foo-module-lib", "foo-system-server"],
775 sdk_version: "system_server_current",
776 }
777 `)
778
779 stubsPath := func(name string, scope *apiScope) string {
780 name = scope.stubsLibraryModuleName(name)
781 return fmt.Sprintf("out/soong/.intermediates/%[1]s/android_common/turbine-combined/%[1]s.jar", name)
782 }
783
784 // The bar library should depend on the highest (where system server is highest and public is
785 // lowest) API scopes provided by each of the foo-* modules. The highest API scope provided by the
786 // foo-<x> module is <x>.
787 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
788 stubLibraries := []string{
789 stubsPath("foo-public", apiScopePublic),
790 stubsPath("foo-system", apiScopeSystem),
791 stubsPath("foo-module-lib", apiScopeModuleLib),
792 stubsPath("foo-system-server", apiScopeSystemServer),
793 }
794 expectedPattern := fmt.Sprintf(`^-classpath .*:\Q%s\E$`, strings.Join(stubLibraries, ":"))
795 if expected, actual := expectedPattern, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
796 t.Errorf("expected pattern %q to match %#q", expected, actual)
797 }
798}
799
Colin Cross1d2b6b32021-06-01 13:18:08 -0700800func TestJavaSdkLibrary_MissingScope(t *testing.T) {
801 prepareForJavaTest.
802 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
803 RunTestWithBp(t, `
804 java_sdk_library {
805 name: "foo",
806 srcs: ["a.java"],
807 public: {
808 enabled: false,
809 },
810 }
811
812 java_library {
813 name: "baz",
814 srcs: ["a.java"],
815 libs: ["foo"],
816 sdk_version: "module_current",
817 }
818 `)
819}
820
821func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
822 android.GroupFixturePreparers(
823 prepareForJavaTest,
824 PrepareForTestWithJavaSdkLibraryFiles,
825 FixtureWithLastReleaseApis("foo"),
826 ).RunTestWithBp(t, `
827 java_sdk_library {
828 name: "foo",
829 srcs: ["a.java"],
830 system: {
831 enabled: true,
832 },
833 }
834
835 java_library {
836 name: "baz",
837 srcs: ["a.java"],
838 libs: ["foo"],
839 // foo does not have module-lib scope so it should fallback to system
840 sdk_version: "module_current",
841 }
842 `)
843}
844
845func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
846 result := android.GroupFixturePreparers(
847 prepareForJavaTest,
848 PrepareForTestWithJavaSdkLibraryFiles,
849 FixtureWithLastReleaseApis("foo"),
850 ).RunTestWithBp(t, `
851 java_sdk_library {
852 name: "foo",
853 srcs: ["a.java"],
854 system: {
855 enabled: true,
856 },
857 default_to_stubs: true,
858 }
859
860 java_library {
861 name: "baz",
862 srcs: ["a.java"],
863 libs: ["foo"],
864 // does not have sdk_version set, should fallback to module,
865 // which will then fallback to system because the module scope
866 // is not enabled.
867 }
868 `)
869 // The baz library should depend on the system stubs jar.
870 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
871 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
872 t.Errorf("expected %q, found %#q", expected, actual)
873 }
874}
875
876func TestJavaSdkLibraryImport(t *testing.T) {
877 result := prepareForJavaTest.RunTestWithBp(t, `
878 java_library {
879 name: "foo",
880 srcs: ["a.java"],
881 libs: ["sdklib"],
882 sdk_version: "current",
883 }
884
885 java_library {
886 name: "foo.system",
887 srcs: ["a.java"],
888 libs: ["sdklib"],
889 sdk_version: "system_current",
890 }
891
892 java_library {
893 name: "foo.test",
894 srcs: ["a.java"],
895 libs: ["sdklib"],
896 sdk_version: "test_current",
897 }
898
899 java_sdk_library_import {
900 name: "sdklib",
901 public: {
902 jars: ["a.jar"],
903 },
904 system: {
905 jars: ["b.jar"],
906 },
907 test: {
908 jars: ["c.jar"],
909 stub_srcs: ["c.java"],
910 },
911 }
912 `)
913
914 for _, scope := range []string{"", ".system", ".test"} {
915 fooModule := result.ModuleForTests("foo"+scope, "android_common")
916 javac := fooModule.Rule("javac")
917
918 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
919 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
920 }
921
922 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Jiakai Zhang204356f2021-09-09 08:12:46 +0000923 `dex2oatd`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700924 `prebuilt_sdklib.stubs`,
925 `prebuilt_sdklib.stubs.source.test`,
926 `prebuilt_sdklib.stubs.system`,
927 `prebuilt_sdklib.stubs.test`,
928 })
929}
930
931func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
932 result := android.GroupFixturePreparers(
933 prepareForJavaTest,
934 PrepareForTestWithJavaSdkLibraryFiles,
935 FixtureWithLastReleaseApis("sdklib"),
Jihoon Kangbd093452023-12-26 19:08:01 +0000936 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
937 variables.BuildFlags = map[string]string{
938 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
939 }
940 }),
Colin Cross1d2b6b32021-06-01 13:18:08 -0700941 ).RunTestWithBp(t, `
942 java_sdk_library {
943 name: "sdklib",
944 srcs: ["a.java"],
945 sdk_version: "none",
946 system_modules: "none",
947 public: {
948 enabled: true,
949 },
950 }
951
952 java_sdk_library_import {
953 name: "sdklib",
954 public: {
955 jars: ["a.jar"],
956 },
957 }
958 `)
959
960 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
961 `dex2oatd`,
962 `prebuilt_sdklib`,
Jihoon Kang748a24d2024-03-20 21:29:39 +0000963 `sdklib-removed.api.combined.public.latest`,
964 `sdklib.api.combined.public.latest`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700965 `sdklib.impl`,
966 `sdklib.stubs`,
Jihoon Kangbd093452023-12-26 19:08:01 +0000967 `sdklib.stubs.exportable`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700968 `sdklib.stubs.source`,
969 `sdklib.xml`,
970 })
971
972 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Spandan Dasda739a32023-12-13 00:06:32 +0000973 `all_apex_contributions`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700974 `prebuilt_sdklib.stubs`,
975 `sdklib.impl`,
976 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
977 // dependency is added after prebuilts may have been renamed and so has to use
978 // the renamed name.
979 `sdklib.xml`,
980 })
981}
982
Paul Duffinbf4de042022-09-27 12:41:52 +0100983func testJavaSdkLibraryImport_Preferred(t *testing.T, prefer string, preparer android.FixturePreparer) {
Colin Cross1d2b6b32021-06-01 13:18:08 -0700984 result := android.GroupFixturePreparers(
985 prepareForJavaTest,
986 PrepareForTestWithJavaSdkLibraryFiles,
987 FixtureWithLastReleaseApis("sdklib"),
Paul Duffinbf4de042022-09-27 12:41:52 +0100988 preparer,
Jihoon Kangbd093452023-12-26 19:08:01 +0000989 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
990 variables.BuildFlags = map[string]string{
991 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
992 }
993 }),
Colin Cross1d2b6b32021-06-01 13:18:08 -0700994 ).RunTestWithBp(t, `
995 java_sdk_library {
996 name: "sdklib",
997 srcs: ["a.java"],
998 sdk_version: "none",
999 system_modules: "none",
1000 public: {
1001 enabled: true,
1002 },
1003 }
1004
1005 java_sdk_library_import {
1006 name: "sdklib",
Paul Duffinbf4de042022-09-27 12:41:52 +01001007 `+prefer+`
Colin Cross1d2b6b32021-06-01 13:18:08 -07001008 public: {
1009 jars: ["a.jar"],
Paul Duffinbf4de042022-09-27 12:41:52 +01001010 stub_srcs: ["a.java"],
1011 current_api: "current.txt",
1012 removed_api: "removed.txt",
1013 annotations: "annotations.zip",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001014 },
1015 }
Paul Duffinbf4de042022-09-27 12:41:52 +01001016
1017 java_library {
1018 name: "combined",
1019 static_libs: [
1020 "sdklib.stubs",
1021 ],
1022 java_resources: [
1023 ":sdklib.stubs.source",
1024 ":sdklib{.public.api.txt}",
1025 ":sdklib{.public.removed-api.txt}",
1026 ":sdklib{.public.annotations.zip}",
1027 ],
1028 sdk_version: "none",
1029 system_modules: "none",
1030 }
1031
1032 java_library {
1033 name: "public",
1034 srcs: ["a.java"],
1035 libs: ["sdklib"],
1036 sdk_version: "current",
1037 }
Colin Cross1d2b6b32021-06-01 13:18:08 -07001038 `)
1039
1040 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Colin Cross1d2b6b32021-06-01 13:18:08 -07001041 `prebuilt_sdklib`,
Jihoon Kang748a24d2024-03-20 21:29:39 +00001042 `sdklib-removed.api.combined.public.latest`,
1043 `sdklib.api.combined.public.latest`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001044 `sdklib.impl`,
1045 `sdklib.stubs`,
Jihoon Kangbd093452023-12-26 19:08:01 +00001046 `sdklib.stubs.exportable`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001047 `sdklib.stubs.source`,
1048 `sdklib.xml`,
1049 })
1050
1051 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Spandan Dasda739a32023-12-13 00:06:32 +00001052 `all_apex_contributions`,
Jiakai Zhang204356f2021-09-09 08:12:46 +00001053 `dex2oatd`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001054 `prebuilt_sdklib.stubs`,
Paul Duffinbf4de042022-09-27 12:41:52 +01001055 `prebuilt_sdklib.stubs.source`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001056 `sdklib.impl`,
1057 `sdklib.xml`,
1058 })
Paul Duffinbf4de042022-09-27 12:41:52 +01001059
1060 // Make sure that dependencies on child modules use the prebuilt when preferred.
1061 CheckModuleDependencies(t, result.TestContext, "combined", "android_common", []string{
1062 // Each use of :sdklib{...} adds a dependency onto prebuilt_sdklib.
1063 `prebuilt_sdklib`,
1064 `prebuilt_sdklib`,
1065 `prebuilt_sdklib`,
1066 `prebuilt_sdklib.stubs`,
1067 `prebuilt_sdklib.stubs.source`,
1068 })
1069
1070 // Make sure that dependencies on sdklib that resolve to one of the child libraries use the
1071 // prebuilt library.
1072 public := result.ModuleForTests("public", "android_common")
1073 rule := public.Output("javac/public.jar")
1074 inputs := rule.Implicits.Strings()
1075 expected := "out/soong/.intermediates/prebuilt_sdklib.stubs/android_common/combined/sdklib.stubs.jar"
1076 if !android.InList(expected, inputs) {
1077 t.Errorf("expected %q to contain %q", inputs, expected)
1078 }
1079}
1080
1081func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
1082 t.Run("prefer", func(t *testing.T) {
1083 testJavaSdkLibraryImport_Preferred(t, "prefer: true,", android.NullFixturePreparer)
1084 })
1085
1086 t.Run("use_source_config_var", func(t *testing.T) {
1087 testJavaSdkLibraryImport_Preferred(t,
1088 "use_source_config_var: {config_namespace: \"acme\", var_name: \"use_source\"},",
1089 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1090 variables.VendorVars = map[string]map[string]string{
1091 "acme": {
1092 "use_source": "false",
1093 },
1094 }
1095 }))
1096 })
Colin Cross1d2b6b32021-06-01 13:18:08 -07001097}
1098
Spandan Dasfc12d2f2023-10-27 20:06:32 +00001099// If a module is listed in `mainline_module_contributions, it should be used
1100// It will supersede any other source vs prebuilt selection mechanism like `prefer` attribute
1101func TestSdkLibraryImport_MetadataModuleSupersedesPreferred(t *testing.T) {
1102 bp := `
1103 apex_contributions {
1104 name: "my_mainline_module_contributions",
1105 api_domain: "my_mainline_module",
1106 contents: [
1107 // legacy mechanism prefers the prebuilt
1108 // mainline_module_contributions supersedes this since source is listed explicitly
1109 "sdklib.prebuilt_preferred_using_legacy_flags",
1110
1111 // legacy mechanism prefers the source
1112 // mainline_module_contributions supersedes this since prebuilt is listed explicitly
1113 "prebuilt_sdklib.source_preferred_using_legacy_flags",
1114 ],
1115 }
Spandan Dasfc12d2f2023-10-27 20:06:32 +00001116 java_sdk_library {
1117 name: "sdklib.prebuilt_preferred_using_legacy_flags",
1118 srcs: ["a.java"],
1119 sdk_version: "none",
1120 system_modules: "none",
1121 public: {
1122 enabled: true,
1123 },
1124 system: {
1125 enabled: true,
1126 }
1127 }
1128 java_sdk_library_import {
1129 name: "sdklib.prebuilt_preferred_using_legacy_flags",
1130 prefer: true, // prebuilt is preferred using legacy mechanism
1131 public: {
1132 jars: ["a.jar"],
1133 stub_srcs: ["a.java"],
1134 current_api: "current.txt",
1135 removed_api: "removed.txt",
1136 annotations: "annotations.zip",
1137 },
1138 system: {
1139 jars: ["a.jar"],
1140 stub_srcs: ["a.java"],
1141 current_api: "current.txt",
1142 removed_api: "removed.txt",
1143 annotations: "annotations.zip",
1144 },
1145 }
1146 java_sdk_library {
1147 name: "sdklib.source_preferred_using_legacy_flags",
1148 srcs: ["a.java"],
1149 sdk_version: "none",
1150 system_modules: "none",
1151 public: {
1152 enabled: true,
1153 },
1154 system: {
1155 enabled: true,
1156 }
1157 }
1158 java_sdk_library_import {
1159 name: "sdklib.source_preferred_using_legacy_flags",
1160 prefer: false, // source is preferred using legacy mechanism
1161 public: {
1162 jars: ["a.jar"],
1163 stub_srcs: ["a.java"],
1164 current_api: "current.txt",
1165 removed_api: "removed.txt",
1166 annotations: "annotations.zip",
1167 },
1168 system: {
1169 jars: ["a.jar"],
1170 stub_srcs: ["a.java"],
1171 current_api: "current.txt",
1172 removed_api: "removed.txt",
1173 annotations: "annotations.zip",
1174 },
1175 }
1176
1177 // rdeps
1178 java_library {
1179 name: "public",
1180 srcs: ["a.java"],
1181 libs: [
1182 // this should get source since source is listed in my_mainline_module_contributions
1183 "sdklib.prebuilt_preferred_using_legacy_flags.stubs",
1184 "sdklib.prebuilt_preferred_using_legacy_flags.stubs.system",
1185
1186 // this should get prebuilt since source is listed in my_mainline_module_contributions
1187 "sdklib.source_preferred_using_legacy_flags.stubs",
1188 "sdklib.source_preferred_using_legacy_flags.stubs.system",
1189
1190 ],
1191 }
1192 `
1193 result := android.GroupFixturePreparers(
1194 prepareForJavaTest,
1195 PrepareForTestWithJavaSdkLibraryFiles,
1196 FixtureWithLastReleaseApis("sdklib.source_preferred_using_legacy_flags", "sdklib.prebuilt_preferred_using_legacy_flags"),
Spandan Dasfc12d2f2023-10-27 20:06:32 +00001197 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1198 variables.BuildFlags = map[string]string{
1199 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "my_mainline_module_contributions",
1200 }
1201 }),
1202 ).RunTestWithBp(t, bp)
1203
1204 // Make sure that rdeps get the correct source vs prebuilt based on mainline_module_contributions
1205 public := result.ModuleForTests("public", "android_common")
1206 rule := public.Output("javac/public.jar")
1207 inputs := rule.Implicits.Strings()
1208 expectedInputs := []string{
1209 // source
1210 "out/soong/.intermediates/sdklib.prebuilt_preferred_using_legacy_flags.stubs/android_common/turbine-combined/sdklib.prebuilt_preferred_using_legacy_flags.stubs.jar",
1211 "out/soong/.intermediates/sdklib.prebuilt_preferred_using_legacy_flags.stubs.system/android_common/turbine-combined/sdklib.prebuilt_preferred_using_legacy_flags.stubs.system.jar",
1212
1213 // prebuilt
1214 "out/soong/.intermediates/prebuilt_sdklib.source_preferred_using_legacy_flags.stubs/android_common/combined/sdklib.source_preferred_using_legacy_flags.stubs.jar",
1215 "out/soong/.intermediates/prebuilt_sdklib.source_preferred_using_legacy_flags.stubs.system/android_common/combined/sdklib.source_preferred_using_legacy_flags.stubs.system.jar",
1216 }
1217 for _, expected := range expectedInputs {
1218 if !android.InList(expected, inputs) {
1219 t.Errorf("expected %q to contain %q", inputs, expected)
1220 }
1221 }
1222}
1223
Colin Cross1d2b6b32021-06-01 13:18:08 -07001224func TestJavaSdkLibraryEnforce(t *testing.T) {
1225 partitionToBpOption := func(partition string) string {
1226 switch partition {
1227 case "system":
1228 return ""
1229 case "vendor":
1230 return "soc_specific: true,"
1231 case "product":
1232 return "product_specific: true,"
1233 default:
1234 panic("Invalid partition group name: " + partition)
1235 }
1236 }
1237
1238 type testConfigInfo struct {
1239 libraryType string
1240 fromPartition string
1241 toPartition string
1242 enforceVendorInterface bool
1243 enforceProductInterface bool
1244 enforceJavaSdkLibraryCheck bool
1245 allowList []string
1246 }
1247
1248 createPreparer := func(info testConfigInfo) android.FixturePreparer {
1249 bpFileTemplate := `
1250 java_library {
1251 name: "foo",
1252 srcs: ["foo.java"],
1253 libs: ["bar"],
1254 sdk_version: "current",
1255 %s
1256 }
1257
1258 %s {
1259 name: "bar",
1260 srcs: ["bar.java"],
1261 sdk_version: "current",
1262 %s
1263 }
1264 `
1265
1266 bpFile := fmt.Sprintf(bpFileTemplate,
1267 partitionToBpOption(info.fromPartition),
1268 info.libraryType,
1269 partitionToBpOption(info.toPartition))
1270
1271 return android.GroupFixturePreparers(
1272 PrepareForTestWithJavaSdkLibraryFiles,
1273 FixtureWithLastReleaseApis("bar"),
1274 android.FixtureWithRootAndroidBp(bpFile),
1275 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1276 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
1277 if info.enforceVendorInterface {
1278 variables.DeviceVndkVersion = proptools.StringPtr("current")
1279 }
1280 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
1281 variables.InterPartitionJavaLibraryAllowList = info.allowList
1282 }),
1283 )
1284 }
1285
1286 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
1287 t.Run(fmt.Sprintf("%v", info), func(t *testing.T) {
1288 errorHandler := android.FixtureExpectsNoErrors
1289 if expectedErrorPattern != "" {
1290 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
1291 }
1292 android.GroupFixturePreparers(
1293 prepareForJavaTest,
1294 createPreparer(info),
1295 ).
1296 ExtendWithErrorHandler(errorHandler).
1297 RunTest(t)
1298 })
1299 }
1300
1301 errorMessage := "is not allowed across the partitions"
1302
1303 runTest(t, testConfigInfo{
1304 libraryType: "java_library",
1305 fromPartition: "product",
1306 toPartition: "system",
1307 enforceVendorInterface: true,
1308 enforceProductInterface: true,
1309 enforceJavaSdkLibraryCheck: false,
1310 }, "")
1311
1312 runTest(t, testConfigInfo{
1313 libraryType: "java_library",
1314 fromPartition: "product",
1315 toPartition: "system",
1316 enforceVendorInterface: true,
1317 enforceProductInterface: false,
1318 enforceJavaSdkLibraryCheck: true,
1319 }, "")
1320
1321 runTest(t, testConfigInfo{
1322 libraryType: "java_library",
1323 fromPartition: "product",
1324 toPartition: "system",
1325 enforceVendorInterface: true,
1326 enforceProductInterface: true,
1327 enforceJavaSdkLibraryCheck: true,
1328 }, errorMessage)
1329
1330 runTest(t, testConfigInfo{
1331 libraryType: "java_library",
1332 fromPartition: "vendor",
1333 toPartition: "system",
1334 enforceVendorInterface: true,
1335 enforceProductInterface: true,
1336 enforceJavaSdkLibraryCheck: true,
1337 }, errorMessage)
1338
1339 runTest(t, testConfigInfo{
1340 libraryType: "java_library",
1341 fromPartition: "vendor",
1342 toPartition: "system",
1343 enforceVendorInterface: true,
1344 enforceProductInterface: true,
1345 enforceJavaSdkLibraryCheck: true,
1346 allowList: []string{"bar"},
1347 }, "")
1348
1349 runTest(t, testConfigInfo{
1350 libraryType: "java_library",
1351 fromPartition: "vendor",
1352 toPartition: "product",
1353 enforceVendorInterface: true,
1354 enforceProductInterface: true,
1355 enforceJavaSdkLibraryCheck: true,
1356 }, errorMessage)
1357
1358 runTest(t, testConfigInfo{
1359 libraryType: "java_sdk_library",
1360 fromPartition: "product",
1361 toPartition: "system",
1362 enforceVendorInterface: true,
1363 enforceProductInterface: true,
1364 enforceJavaSdkLibraryCheck: true,
1365 }, "")
1366
1367 runTest(t, testConfigInfo{
1368 libraryType: "java_sdk_library",
1369 fromPartition: "vendor",
1370 toPartition: "system",
1371 enforceVendorInterface: true,
1372 enforceProductInterface: true,
1373 enforceJavaSdkLibraryCheck: true,
1374 }, "")
1375
1376 runTest(t, testConfigInfo{
1377 libraryType: "java_sdk_library",
1378 fromPartition: "vendor",
1379 toPartition: "product",
1380 enforceVendorInterface: true,
1381 enforceProductInterface: true,
1382 enforceJavaSdkLibraryCheck: true,
1383 }, "")
1384}
Colin Cross30c491b2021-06-01 13:39:09 -07001385
1386func TestJavaSdkLibraryDist(t *testing.T) {
1387 result := android.GroupFixturePreparers(
1388 PrepareForTestWithJavaBuildComponents,
1389 PrepareForTestWithJavaDefaultModules,
1390 PrepareForTestWithJavaSdkLibraryFiles,
Colin Cross67c17ae2021-06-02 13:02:51 -07001391 FixtureWithLastReleaseApis(
1392 "sdklib_no_group",
1393 "sdklib_group_foo",
1394 "sdklib_owner_foo",
1395 "foo"),
Jihoon Kang02168052024-03-20 00:44:54 +00001396 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1397 variables.BuildFlags = map[string]string{
1398 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
1399 }
1400 }),
Colin Cross30c491b2021-06-01 13:39:09 -07001401 ).RunTestWithBp(t, `
1402 java_sdk_library {
Colin Cross59b92bf2021-06-01 14:07:56 -07001403 name: "sdklib_no_group",
Colin Cross30c491b2021-06-01 13:39:09 -07001404 srcs: ["foo.java"],
1405 }
1406
1407 java_sdk_library {
Colin Cross986b69a2021-06-01 13:13:40 -07001408 name: "sdklib_group_foo",
Colin Cross986b69a2021-06-01 13:13:40 -07001409 srcs: ["foo.java"],
1410 dist_group: "foo",
1411 }
1412
1413 java_sdk_library {
Colin Cross30c491b2021-06-01 13:39:09 -07001414 name: "sdklib_owner_foo",
Colin Cross30c491b2021-06-01 13:39:09 -07001415 srcs: ["foo.java"],
1416 owner: "foo",
1417 }
1418
1419 java_sdk_library {
1420 name: "sdklib_stem_foo",
Colin Cross30c491b2021-06-01 13:39:09 -07001421 srcs: ["foo.java"],
1422 dist_stem: "foo",
1423 }
Colin Cross30c491b2021-06-01 13:39:09 -07001424 `)
1425
1426 type testCase struct {
1427 module string
1428 distDir string
1429 distStem string
1430 }
1431 testCases := []testCase{
1432 {
Colin Cross59b92bf2021-06-01 14:07:56 -07001433 module: "sdklib_no_group",
Colin Cross3dd66252021-06-01 14:05:09 -07001434 distDir: "apistubs/unknown/public",
Colin Cross59b92bf2021-06-01 14:07:56 -07001435 distStem: "sdklib_no_group.jar",
Colin Cross30c491b2021-06-01 13:39:09 -07001436 },
1437 {
Colin Cross986b69a2021-06-01 13:13:40 -07001438 module: "sdklib_group_foo",
1439 distDir: "apistubs/foo/public",
1440 distStem: "sdklib_group_foo.jar",
1441 },
1442 {
Colin Cross59b92bf2021-06-01 14:07:56 -07001443 // Owner doesn't affect distDir after b/186723288.
Colin Cross30c491b2021-06-01 13:39:09 -07001444 module: "sdklib_owner_foo",
Colin Cross59b92bf2021-06-01 14:07:56 -07001445 distDir: "apistubs/unknown/public",
Colin Cross30c491b2021-06-01 13:39:09 -07001446 distStem: "sdklib_owner_foo.jar",
1447 },
1448 {
1449 module: "sdklib_stem_foo",
Colin Cross3dd66252021-06-01 14:05:09 -07001450 distDir: "apistubs/unknown/public",
Colin Cross30c491b2021-06-01 13:39:09 -07001451 distStem: "foo.jar",
1452 },
Colin Cross30c491b2021-06-01 13:39:09 -07001453 }
1454
1455 for _, tt := range testCases {
1456 t.Run(tt.module, func(t *testing.T) {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001457 m := result.ModuleForTests(apiScopePublic.exportableStubsLibraryModuleName(tt.module), "android_common").Module().(*Library)
Colin Cross30c491b2021-06-01 13:39:09 -07001458 dists := m.Dists()
1459 if len(dists) != 1 {
1460 t.Fatalf("expected exactly 1 dist entry, got %d", len(dists))
1461 }
1462 if g, w := String(dists[0].Dir), tt.distDir; g != w {
1463 t.Errorf("expected dist dir %q, got %q", w, g)
1464 }
1465 if g, w := String(dists[0].Dest), tt.distStem; g != w {
1466 t.Errorf("expected dist stem %q, got %q", w, g)
1467 }
1468 })
1469 }
1470}
satayev8f088b02021-12-06 11:40:46 +00001471
1472func TestSdkLibrary_CheckMinSdkVersion(t *testing.T) {
1473 preparer := android.GroupFixturePreparers(
1474 PrepareForTestWithJavaBuildComponents,
1475 PrepareForTestWithJavaDefaultModules,
1476 PrepareForTestWithJavaSdkLibraryFiles,
1477 )
1478
1479 preparer.RunTestWithBp(t, `
1480 java_sdk_library {
1481 name: "sdklib",
1482 srcs: ["a.java"],
1483 static_libs: ["util"],
1484 min_sdk_version: "30",
1485 unsafe_ignore_missing_latest_api: true,
1486 }
1487
1488 java_library {
1489 name: "util",
1490 srcs: ["a.java"],
1491 min_sdk_version: "30",
1492 }
1493 `)
1494
1495 preparer.
1496 RunTestWithBp(t, `
1497 java_sdk_library {
1498 name: "sdklib",
1499 srcs: ["a.java"],
1500 libs: ["util"],
1501 impl_only_libs: ["util"],
1502 stub_only_libs: ["util"],
1503 stub_only_static_libs: ["util"],
1504 min_sdk_version: "30",
1505 unsafe_ignore_missing_latest_api: true,
1506 }
1507
1508 java_library {
1509 name: "util",
1510 srcs: ["a.java"],
1511 }
1512 `)
1513
1514 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "util".*should support min_sdk_version\(30\)`)).
1515 RunTestWithBp(t, `
1516 java_sdk_library {
1517 name: "sdklib",
1518 srcs: ["a.java"],
1519 static_libs: ["util"],
1520 min_sdk_version: "30",
1521 unsafe_ignore_missing_latest_api: true,
1522 }
1523
1524 java_library {
1525 name: "util",
1526 srcs: ["a.java"],
1527 min_sdk_version: "31",
1528 }
1529 `)
1530
1531 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "another_util".*should support min_sdk_version\(30\)`)).
1532 RunTestWithBp(t, `
1533 java_sdk_library {
1534 name: "sdklib",
1535 srcs: ["a.java"],
1536 static_libs: ["util"],
1537 min_sdk_version: "30",
1538 unsafe_ignore_missing_latest_api: true,
1539 }
1540
1541 java_library {
1542 name: "util",
1543 srcs: ["a.java"],
1544 static_libs: ["another_util"],
1545 min_sdk_version: "30",
1546 }
1547
1548 java_library {
1549 name: "another_util",
1550 srcs: ["a.java"],
1551 min_sdk_version: "31",
1552 }
1553 `)
1554}
Nikita Ioffed732da72022-11-21 12:38:25 +00001555
1556func TestJavaSdkLibrary_StubOnlyLibs_PassedToDroidstubs(t *testing.T) {
1557 result := android.GroupFixturePreparers(
1558 prepareForJavaTest,
1559 PrepareForTestWithJavaSdkLibraryFiles,
1560 FixtureWithLastReleaseApis("foo"),
1561 ).RunTestWithBp(t, `
1562 java_sdk_library {
1563 name: "foo",
1564 srcs: ["a.java"],
1565 public: {
1566 enabled: true,
1567 },
1568 stub_only_libs: ["bar-lib"],
1569 }
1570
1571 java_library {
1572 name: "bar-lib",
1573 srcs: ["b.java"],
1574 }
1575 `)
1576
1577 // The foo.stubs.source should depend on bar-lib
1578 fooStubsSources := result.ModuleForTests("foo.stubs.source", "android_common").Module().(*Droidstubs)
1579 android.AssertStringListContains(t, "foo stubs should depend on bar-lib", fooStubsSources.Javadoc.properties.Libs, "bar-lib")
1580}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001581
Mark White9421c4c2023-08-10 00:07:03 +00001582func TestJavaSdkLibrary_Scope_Libs_PassedToDroidstubs(t *testing.T) {
1583 result := android.GroupFixturePreparers(
1584 prepareForJavaTest,
1585 PrepareForTestWithJavaSdkLibraryFiles,
1586 FixtureWithLastReleaseApis("foo"),
1587 ).RunTestWithBp(t, `
1588 java_sdk_library {
1589 name: "foo",
1590 srcs: ["a.java"],
1591 public: {
1592 enabled: true,
1593 libs: ["bar-lib"],
1594 },
1595 }
1596
1597 java_library {
1598 name: "bar-lib",
1599 srcs: ["b.java"],
1600 }
1601 `)
1602
1603 // The foo.stubs.source should depend on bar-lib
1604 fooStubsSources := result.ModuleForTests("foo.stubs.source", "android_common").Module().(*Droidstubs)
1605 android.AssertStringListContains(t, "foo stubs should depend on bar-lib", fooStubsSources.Javadoc.properties.Libs, "bar-lib")
1606}
1607
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001608func TestJavaSdkLibrary_ApiLibrary(t *testing.T) {
1609 result := android.GroupFixturePreparers(
1610 prepareForJavaTest,
1611 PrepareForTestWithJavaSdkLibraryFiles,
1612 FixtureWithLastReleaseApis("foo"),
1613 android.FixtureModifyConfig(func(config android.Config) {
1614 config.SetApiLibraries([]string{"foo"})
1615 }),
1616 ).RunTestWithBp(t, `
1617 java_sdk_library {
1618 name: "foo",
1619 srcs: ["a.java", "b.java"],
1620 api_packages: ["foo"],
1621 system: {
1622 enabled: true,
1623 },
1624 module_lib: {
1625 enabled: true,
1626 },
1627 test: {
1628 enabled: true,
1629 },
1630 }
1631 `)
1632
1633 testCases := []struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001634 scope *apiScope
1635 apiContributions []string
1636 fullApiSurfaceStub string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001637 }{
1638 {
Jihoon Kangca198c22023-06-22 23:13:51 +00001639 scope: apiScopePublic,
1640 apiContributions: []string{"foo.stubs.source.api.contribution"},
Jihoon Kange7ee2562023-07-25 05:51:46 +00001641 fullApiSurfaceStub: "android_stubs_current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001642 },
1643 {
Jihoon Kangca198c22023-06-22 23:13:51 +00001644 scope: apiScopeSystem,
1645 apiContributions: []string{"foo.stubs.source.system.api.contribution", "foo.stubs.source.api.contribution"},
Jihoon Kange7ee2562023-07-25 05:51:46 +00001646 fullApiSurfaceStub: "android_system_stubs_current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001647 },
1648 {
Jihoon Kangca198c22023-06-22 23:13:51 +00001649 scope: apiScopeTest,
1650 apiContributions: []string{"foo.stubs.source.test.api.contribution", "foo.stubs.source.system.api.contribution", "foo.stubs.source.api.contribution"},
Jihoon Kange7ee2562023-07-25 05:51:46 +00001651 fullApiSurfaceStub: "android_test_stubs_current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001652 },
1653 {
Jihoon Kangca198c22023-06-22 23:13:51 +00001654 scope: apiScopeModuleLib,
1655 apiContributions: []string{"foo.stubs.source.module_lib.api.contribution", "foo.stubs.source.system.api.contribution", "foo.stubs.source.api.contribution"},
1656 fullApiSurfaceStub: "android_module_lib_stubs_current_full.from-text",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001657 },
1658 }
1659
1660 for _, c := range testCases {
1661 m := result.ModuleForTests(c.scope.apiLibraryModuleName("foo"), "android_common").Module().(*ApiLibrary)
1662 android.AssertArrayString(t, "Module expected to contain api contributions", c.apiContributions, m.properties.Api_contributions)
Jihoon Kangca198c22023-06-22 23:13:51 +00001663 android.AssertStringEquals(t, "Module expected to contain full api surface api library", c.fullApiSurfaceStub, *m.properties.Full_api_surface_stub)
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001664 }
1665}
Jihoon Kang786df932023-09-07 01:18:31 +00001666
1667func TestStaticDepStubLibrariesVisibility(t *testing.T) {
1668 android.GroupFixturePreparers(
1669 prepareForJavaTest,
1670 PrepareForTestWithJavaSdkLibraryFiles,
1671 FixtureWithLastReleaseApis("foo"),
1672 android.FixtureMergeMockFs(
1673 map[string][]byte{
1674 "A.java": nil,
1675 "dir/Android.bp": []byte(
1676 `
1677 java_library {
1678 name: "bar",
1679 srcs: ["A.java"],
1680 libs: ["foo.stubs.from-source"],
1681 }
1682 `),
1683 "dir/A.java": nil,
1684 },
1685 ).ExtendWithErrorHandler(
1686 android.FixtureExpectsAtLeastOneErrorMatchingPattern(
1687 `module "bar" variant "android_common": depends on //.:foo.stubs.from-source which is not visible to this module`)),
1688 ).RunTestWithBp(t, `
1689 java_sdk_library {
1690 name: "foo",
1691 srcs: ["A.java"],
1692 }
1693 `)
1694}
Jamie Garsidee570ace2023-11-27 12:07:36 +00001695
1696func TestSdkLibraryDependency(t *testing.T) {
1697 result := android.GroupFixturePreparers(
1698 prepareForJavaTest,
1699 PrepareForTestWithJavaSdkLibraryFiles,
1700 FixtureWithPrebuiltApis(map[string][]string{
1701 "30": {"bar", "foo"},
1702 }),
1703 ).RunTestWithBp(t,
1704 `
1705 java_sdk_library {
1706 name: "foo",
1707 srcs: ["a.java", "b.java"],
1708 api_packages: ["foo"],
1709 }
1710
1711 java_sdk_library {
1712 name: "bar",
1713 srcs: ["c.java", "b.java"],
1714 libs: [
1715 "foo",
1716 ],
1717 uses_libs: [
1718 "foo",
1719 ],
1720 }
1721`)
1722
1723 barPermissions := result.ModuleForTests("bar.xml", "android_common").Rule("java_sdk_xml")
1724
1725 android.AssertStringDoesContain(t, "bar.xml java_sdk_xml command", barPermissions.RuleParams.Command, `dependency=\"foo\"`)
1726}
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001727
1728func TestSdkLibraryExportableStubsLibrary(t *testing.T) {
1729 result := android.GroupFixturePreparers(
1730 prepareForJavaTest,
1731 PrepareForTestWithJavaSdkLibraryFiles,
1732 FixtureWithLastReleaseApis("foo"),
1733 android.FixtureModifyConfig(func(config android.Config) {
1734 config.SetApiLibraries([]string{"foo"})
1735 }),
1736 ).RunTestWithBp(t, `
1737 aconfig_declarations {
1738 name: "bar",
1739 package: "com.example.package",
1740 srcs: [
1741 "bar.aconfig",
1742 ],
1743 }
1744 java_sdk_library {
1745 name: "foo",
1746 srcs: ["a.java", "b.java"],
1747 api_packages: ["foo"],
1748 system: {
1749 enabled: true,
1750 },
1751 module_lib: {
1752 enabled: true,
1753 },
1754 test: {
1755 enabled: true,
1756 },
1757 aconfig_declarations: [
1758 "bar",
1759 ],
1760 }
1761 `)
1762
1763 exportableStubsLibraryModuleName := apiScopePublic.exportableStubsLibraryModuleName("foo")
1764 exportableSourceStubsLibraryModuleName := apiScopePublic.exportableSourceStubsLibraryModuleName("foo")
1765
1766 // Check modules generation
1767 topLevelModule := result.ModuleForTests(exportableStubsLibraryModuleName, "android_common")
1768 result.ModuleForTests(exportableSourceStubsLibraryModuleName, "android_common")
1769
1770 // Check static lib dependency
1771 android.AssertBoolEquals(t, "exportable top level stubs library module depends on the"+
1772 "exportable source stubs library module", true,
1773 CheckModuleHasDependency(t, result.TestContext, exportableStubsLibraryModuleName,
1774 "android_common", exportableSourceStubsLibraryModuleName),
1775 )
1776 android.AssertArrayString(t, "exportable source stub library is a static lib of the"+
1777 "top level exportable stubs library", []string{exportableSourceStubsLibraryModuleName},
1778 topLevelModule.Module().(*Library).properties.Static_libs)
1779}
Spandan Das81d95c52024-02-01 23:41:11 +00001780
1781// For java libraries depending on java_sdk_library(_import) via libs, assert that
1782// rdep gets stubs of source if source is listed in apex_contributions and prebuilt has prefer (legacy mechanism)
1783func TestStubResolutionOfJavaSdkLibraryInLibs(t *testing.T) {
1784 bp := `
1785 apex_contributions {
1786 name: "my_mainline_module_contributions",
1787 api_domain: "my_mainline_module",
1788 contents: ["sdklib"], // source is selected using apex_contributions, but prebuilt is selected using prefer
1789 }
1790 java_sdk_library {
1791 name: "sdklib",
1792 srcs: ["a.java"],
1793 sdk_version: "none",
1794 system_modules: "none",
1795 public: {
1796 enabled: true,
1797 },
1798 }
1799 java_sdk_library_import {
1800 name: "sdklib",
1801 public: {
1802 jars: ["a.jar"],
1803 stub_srcs: ["a.java"],
1804 current_api: "current.txt",
1805 removed_api: "removed.txt",
1806 annotations: "annotations.zip",
1807 },
1808 prefer: true, // Set prefer explicitly on the prebuilt. We will assert that rdep gets source in a test case.
1809 }
1810 // rdeps
1811 java_library {
1812 name: "mymodule",
1813 srcs: ["a.java"],
1814 sdk_version: "current",
1815 libs: ["sdklib",], // this should be dynamically resolved to sdklib.stubs (source) or prebuilt_sdklib.stubs (prebuilt)
1816 }
1817 `
1818
1819 fixture := android.GroupFixturePreparers(
1820 prepareForJavaTest,
1821 PrepareForTestWithJavaSdkLibraryFiles,
1822 FixtureWithLastReleaseApis("sdklib"),
1823 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1824 variables.BuildFlags = map[string]string{
1825 // We can use any of the apex contribution build flags from build/soong/android/config.go#mainlineApexContributionBuildFlags here
1826 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "my_mainline_module_contributions",
1827 }
1828 }),
1829 )
1830
1831 result := fixture.RunTestWithBp(t, bp)
1832 // Make sure that rdeps get the correct source vs prebuilt based on mainline_module_contributions
1833 public := result.ModuleForTests("mymodule", "android_common")
1834 rule := public.Output("javac/mymodule.jar")
1835 inputs := rule.Implicits.Strings()
1836 android.AssertStringListContains(t, "Could not find the expected stub on classpath", inputs, "out/soong/.intermediates/sdklib.stubs/android_common/turbine-combined/sdklib.stubs.jar")
1837}
Spandan Das23956d12024-01-19 00:22:22 +00001838
1839// test that rdep gets resolved to the correct version of a java_sdk_library (source or a specific prebuilt)
1840func TestMultipleSdkLibraryPrebuilts(t *testing.T) {
1841 bp := `
1842 apex_contributions {
1843 name: "my_mainline_module_contributions",
1844 api_domain: "my_mainline_module",
1845 contents: ["%s"],
1846 }
1847 java_sdk_library {
1848 name: "sdklib",
1849 srcs: ["a.java"],
1850 sdk_version: "none",
1851 system_modules: "none",
1852 public: {
1853 enabled: true,
1854 },
1855 }
1856 java_sdk_library_import {
1857 name: "sdklib.v1", //prebuilt
1858 source_module_name: "sdklib",
1859 public: {
1860 jars: ["a.jar"],
1861 stub_srcs: ["a.java"],
1862 current_api: "current.txt",
1863 removed_api: "removed.txt",
1864 annotations: "annotations.zip",
1865 },
1866 }
1867 java_sdk_library_import {
1868 name: "sdklib.v2", //prebuilt
1869 source_module_name: "sdklib",
1870 public: {
1871 jars: ["a.jar"],
1872 stub_srcs: ["a.java"],
1873 current_api: "current.txt",
1874 removed_api: "removed.txt",
1875 annotations: "annotations.zip",
1876 },
1877 }
1878 // rdeps
1879 java_library {
1880 name: "mymodule",
1881 srcs: ["a.java"],
1882 libs: ["sdklib.stubs",],
1883 }
1884 `
1885 testCases := []struct {
1886 desc string
1887 selectedDependencyName string
1888 expectedStubPath string
1889 }{
1890 {
1891 desc: "Source library is selected using apex_contributions",
1892 selectedDependencyName: "sdklib",
1893 expectedStubPath: "out/soong/.intermediates/sdklib.stubs/android_common/turbine-combined/sdklib.stubs.jar",
1894 },
1895 {
1896 desc: "Prebuilt library v1 is selected using apex_contributions",
1897 selectedDependencyName: "prebuilt_sdklib.v1",
1898 expectedStubPath: "out/soong/.intermediates/prebuilt_sdklib.v1.stubs/android_common/combined/sdklib.stubs.jar",
1899 },
1900 {
1901 desc: "Prebuilt library v2 is selected using apex_contributions",
1902 selectedDependencyName: "prebuilt_sdklib.v2",
1903 expectedStubPath: "out/soong/.intermediates/prebuilt_sdklib.v2.stubs/android_common/combined/sdklib.stubs.jar",
1904 },
1905 }
1906
1907 fixture := android.GroupFixturePreparers(
1908 prepareForJavaTest,
1909 PrepareForTestWithJavaSdkLibraryFiles,
1910 FixtureWithLastReleaseApis("sdklib", "sdklib.v1", "sdklib.v2"),
1911 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1912 variables.BuildFlags = map[string]string{
1913 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "my_mainline_module_contributions",
1914 }
1915 }),
1916 )
1917
1918 for _, tc := range testCases {
1919 result := fixture.RunTestWithBp(t, fmt.Sprintf(bp, tc.selectedDependencyName))
1920
1921 // Make sure that rdeps get the correct source vs prebuilt based on mainline_module_contributions
1922 public := result.ModuleForTests("mymodule", "android_common")
1923 rule := public.Output("javac/mymodule.jar")
1924 inputs := rule.Implicits.Strings()
1925 android.AssertStringListContains(t, "Could not find the expected stub on classpath", inputs, tc.expectedStubPath)
1926 }
1927}