blob: 34c63ac61d5cdf286318b1a680fb4fe2ffeaf8fe [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
Paul Duffin1816cde2024-04-10 10:58:21 +0100230 fooUpdatable := result.ModuleForTests("fooUpdatable.xml", "android_common").Output("fooUpdatable.xml")
231 fooUpdatableContents := android.ContentFromFileRuleForTests(t, result.TestContext, fooUpdatable)
232 android.AssertStringDoesContain(t, "fooUpdatable.xml contents", fooUpdatableContents, `on-bootclasspath-since="U"`)
233 android.AssertStringDoesContain(t, "fooUpdatable.xml contents", fooUpdatableContents, `on-bootclasspath-before="V"`)
234 android.AssertStringDoesContain(t, "fooUpdatable.xml contents", fooUpdatableContents, `min-device-sdk="W"`)
235 android.AssertStringDoesContain(t, "fooUpdatable.xml contents", fooUpdatableContents, `max-device-sdk="X"`)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000236
237 // double check that updatability attributes are not written if they don't exist in the bp file
238 // the permissions file for the foo library defined above
Paul Duffin1816cde2024-04-10 10:58:21 +0100239 fooPermissions := result.ModuleForTests("foo.xml", "android_common").Output("foo.xml")
240 fooPermissionsContents := android.ContentFromFileRuleForTests(t, result.TestContext, fooPermissions)
241 android.AssertStringDoesNotContain(t, "foo.xml contents", fooPermissionsContents, `on-bootclasspath-since`)
242 android.AssertStringDoesNotContain(t, "foo.xml contents", fooPermissionsContents, `on-bootclasspath-before`)
243 android.AssertStringDoesNotContain(t, "foo.xml contents", fooPermissionsContents, `min-device-sdk`)
244 android.AssertStringDoesNotContain(t, "foo.xml contents", fooPermissionsContents, `max-device-sdk`)
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000245}
246
Pedro Loureiroc3621422021-09-28 15:40:23 +0000247func TestJavaSdkLibrary_UpdatableLibrary_Validation_ValidVersion(t *testing.T) {
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000248 android.GroupFixturePreparers(
249 prepareForJavaTest,
250 PrepareForTestWithJavaSdkLibraryFiles,
251 FixtureWithPrebuiltApis(map[string][]string{
252 "30": {"fooUpdatable", "fooUpdatableErr"},
253 }),
254 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
255 []string{
256 `on_bootclasspath_since: "aaa" could not be parsed as an integer and is not a recognized codename`,
257 `on_bootclasspath_before: "bbc" could not be parsed as an integer and is not a recognized codename`,
258 `min_device_sdk: "ccc" could not be parsed as an integer and is not a recognized codename`,
Pedro Loureirob638c622021-12-22 15:28:05 +0000259 `max_device_sdk: "current" is not an allowed value for this attribute`,
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000260 })).RunTestWithBp(t,
261 `
262 java_sdk_library {
263 name: "fooUpdatableErr",
264 srcs: ["a.java", "b.java"],
265 api_packages: ["foo"],
Pedro Loureiroc3621422021-09-28 15:40:23 +0000266 on_bootclasspath_since: "aaa",
267 on_bootclasspath_before: "bbc",
268 min_device_sdk: "ccc",
Pedro Loureirob638c622021-12-22 15:28:05 +0000269 max_device_sdk: "current",
Pedro Loureiro9956e5e2021-09-07 17:21:59 +0000270 }
271`)
272}
273
Pedro Loureiroc3621422021-09-28 15:40:23 +0000274func TestJavaSdkLibrary_UpdatableLibrary_Validation_AtLeastTAttributes(t *testing.T) {
275 android.GroupFixturePreparers(
276 prepareForJavaTest,
277 PrepareForTestWithJavaSdkLibraryFiles,
278 FixtureWithPrebuiltApis(map[string][]string{
279 "28": {"foo"},
280 }),
281 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
282 []string{
283 "on_bootclasspath_since: Attribute value needs to be at least T",
284 "on_bootclasspath_before: Attribute value needs to be at least T",
285 "min_device_sdk: Attribute value needs to be at least T",
286 "max_device_sdk: Attribute value needs to be at least T",
287 },
288 )).RunTestWithBp(t,
289 `
290 java_sdk_library {
291 name: "foo",
292 srcs: ["a.java", "b.java"],
293 api_packages: ["foo"],
294 on_bootclasspath_since: "S",
295 on_bootclasspath_before: "S",
296 min_device_sdk: "S",
297 max_device_sdk: "S",
298 min_sdk_version: "S",
299 }
300`)
301}
302
303func TestJavaSdkLibrary_UpdatableLibrary_Validation_MinAndMaxDeviceSdk(t *testing.T) {
304 android.GroupFixturePreparers(
305 prepareForJavaTest,
306 PrepareForTestWithJavaSdkLibraryFiles,
307 FixtureWithPrebuiltApis(map[string][]string{
308 "28": {"foo"},
309 }),
310 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
311 variables.Platform_version_active_codenames = []string{"Tiramisu", "U", "V"}
312 }),
313 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
314 []string{
315 "min_device_sdk can't be greater than max_device_sdk",
316 },
317 )).RunTestWithBp(t,
318 `
319 java_sdk_library {
320 name: "foo",
321 srcs: ["a.java", "b.java"],
322 api_packages: ["foo"],
323 min_device_sdk: "V",
324 max_device_sdk: "U",
325 min_sdk_version: "S",
326 }
327`)
328}
329
330func TestJavaSdkLibrary_UpdatableLibrary_Validation_MinAndMaxDeviceSdkAndModuleMinSdk(t *testing.T) {
331 android.GroupFixturePreparers(
332 prepareForJavaTest,
333 PrepareForTestWithJavaSdkLibraryFiles,
334 FixtureWithPrebuiltApis(map[string][]string{
335 "28": {"foo"},
336 }),
337 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
338 variables.Platform_version_active_codenames = []string{"Tiramisu", "U", "V"}
339 }),
340 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(
341 []string{
342 regexp.QuoteMeta("min_device_sdk: Can't be less than module's min sdk (V)"),
343 regexp.QuoteMeta("max_device_sdk: Can't be less than module's min sdk (V)"),
344 },
345 )).RunTestWithBp(t,
346 `
347 java_sdk_library {
348 name: "foo",
349 srcs: ["a.java", "b.java"],
350 api_packages: ["foo"],
351 min_device_sdk: "U",
352 max_device_sdk: "U",
353 min_sdk_version: "V",
354 }
355`)
356}
357
358func TestJavaSdkLibrary_UpdatableLibrary_usesNewTag(t *testing.T) {
359 result := android.GroupFixturePreparers(
360 prepareForJavaTest,
361 PrepareForTestWithJavaSdkLibraryFiles,
362 FixtureWithPrebuiltApis(map[string][]string{
363 "30": {"foo"},
364 }),
365 ).RunTestWithBp(t,
366 `
367 java_sdk_library {
368 name: "foo",
369 srcs: ["a.java", "b.java"],
370 min_device_sdk: "Tiramisu",
371 min_sdk_version: "S",
372 }
373`)
374 // test that updatability attributes are passed on correctly
Paul Duffin1816cde2024-04-10 10:58:21 +0100375 fooUpdatable := result.ModuleForTests("foo.xml", "android_common").Output("foo.xml")
376 fooUpdatableContents := android.ContentFromFileRuleForTests(t, result.TestContext, fooUpdatable)
377 android.AssertStringDoesContain(t, "foo.xml contents", fooUpdatableContents, `<apex-library`)
378 android.AssertStringDoesNotContain(t, "foo.xml contents", fooUpdatableContents, `<library`)
Pedro Loureiroc3621422021-09-28 15:40:23 +0000379}
380
Colin Cross1d2b6b32021-06-01 13:18:08 -0700381func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
382 result := android.GroupFixturePreparers(
383 prepareForJavaTest,
384 PrepareForTestWithJavaSdkLibraryFiles,
385 FixtureWithLastReleaseApis("sdklib"),
386 ).RunTestWithBp(t, `
387 java_sdk_library {
388 name: "sdklib",
389 srcs: ["a.java"],
390 libs: ["lib"],
391 static_libs: ["static-lib"],
392 impl_only_libs: ["impl-only-lib"],
393 stub_only_libs: ["stub-only-lib"],
394 stub_only_static_libs: ["stub-only-static-lib"],
395 }
396 java_defaults {
397 name: "defaults",
398 srcs: ["a.java"],
399 sdk_version: "current",
400 }
401 java_library { name: "lib", defaults: ["defaults"] }
402 java_library { name: "static-lib", defaults: ["defaults"] }
403 java_library { name: "impl-only-lib", defaults: ["defaults"] }
404 java_library { name: "stub-only-lib", defaults: ["defaults"] }
405 java_library { name: "stub-only-static-lib", defaults: ["defaults"] }
406 `)
407 var expectations = []struct {
408 lib string
409 on_impl_classpath bool
410 on_stub_classpath bool
411 in_impl_combined bool
412 in_stub_combined bool
413 }{
414 {lib: "lib", on_impl_classpath: true},
415 {lib: "static-lib", in_impl_combined: true},
416 {lib: "impl-only-lib", on_impl_classpath: true},
417 {lib: "stub-only-lib", on_stub_classpath: true},
418 {lib: "stub-only-static-lib", in_stub_combined: true},
419 }
420 verify := func(sdklib, dep string, cp, combined bool) {
421 sdklibCp := result.ModuleForTests(sdklib, "android_common").Rule("javac").Args["classpath"]
422 expected := cp || combined // Every combined jar is also on the classpath.
423 android.AssertStringContainsEquals(t, "bad classpath for "+sdklib, sdklibCp, "/"+dep+".jar", expected)
424
425 combineJarInputs := result.ModuleForTests(sdklib, "android_common").Rule("combineJar").Inputs.Strings()
426 depPath := filepath.Join("out", "soong", ".intermediates", dep, "android_common", "turbine-combined", dep+".jar")
427 android.AssertStringListContainsEquals(t, "bad combined inputs for "+sdklib, combineJarInputs, depPath, combined)
428 }
429 for _, expectation := range expectations {
Colin Cross1d2b6b32021-06-01 13:18:08 -0700430 verify("sdklib.impl", expectation.lib, expectation.on_impl_classpath, expectation.in_impl_combined)
431
Jihoon Kang1147b312023-06-08 23:25:57 +0000432 stubName := apiScopePublic.sourceStubLibraryModuleName("sdklib")
Colin Cross1d2b6b32021-06-01 13:18:08 -0700433 verify(stubName, expectation.lib, expectation.on_stub_classpath, expectation.in_stub_combined)
434 }
435}
436
437func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
438 result := android.GroupFixturePreparers(
439 prepareForJavaTest,
440 PrepareForTestWithJavaSdkLibraryFiles,
441 FixtureWithLastReleaseApis("foo"),
442 ).RunTestWithBp(t, `
443 java_sdk_library {
444 name: "foo",
445 srcs: ["a.java"],
446 api_only: true,
447 public: {
448 enabled: true,
449 },
450 }
451
452 java_library {
453 name: "bar",
454 srcs: ["b.java"],
455 libs: ["foo"],
456 }
457 `)
458
459 // The bar library should depend on the stubs jar.
460 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
461 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
462 t.Errorf("expected %q, found %#q", expected, actual)
463 }
464}
465
Anton Hanssond78eb762021-09-21 15:25:12 +0100466func TestJavaSdkLibrary_AccessOutputFiles(t *testing.T) {
Colin Cross1d2b6b32021-06-01 13:18:08 -0700467 android.GroupFixturePreparers(
468 prepareForJavaTest,
469 PrepareForTestWithJavaSdkLibraryFiles,
470 FixtureWithLastReleaseApis("foo"),
471 ).RunTestWithBp(t, `
472 java_sdk_library {
473 name: "foo",
474 srcs: ["a.java"],
475 api_packages: ["foo"],
Anton Hanssond78eb762021-09-21 15:25:12 +0100476 annotations_enabled: true,
477 public: {
478 enabled: true,
479 },
480 }
481 java_library {
482 name: "bar",
483 srcs: ["b.java", ":foo{.public.stubs.source}"],
484 java_resources: [":foo{.public.annotations.zip}"],
485 }
486 `)
487}
488
489func TestJavaSdkLibrary_AccessOutputFiles_NoAnnotations(t *testing.T) {
490 android.GroupFixturePreparers(
491 prepareForJavaTest,
492 PrepareForTestWithJavaSdkLibraryFiles,
493 FixtureWithLastReleaseApis("foo"),
494 ).
495 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "bar" variant "android_common": path dependency ":foo{.public.annotations.zip}": annotations.zip not available for api scope public`)).
496 RunTestWithBp(t, `
497 java_sdk_library {
498 name: "foo",
499 srcs: ["a.java"],
500 api_packages: ["foo"],
Colin Cross1d2b6b32021-06-01 13:18:08 -0700501 public: {
502 enabled: true,
503 },
504 }
505
506 java_library {
507 name: "bar",
508 srcs: ["b.java", ":foo{.public.stubs.source}"],
Anton Hanssond78eb762021-09-21 15:25:12 +0100509 java_resources: [":foo{.public.annotations.zip}"],
Colin Cross1d2b6b32021-06-01 13:18:08 -0700510 }
511 `)
512}
513
514func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
515 android.GroupFixturePreparers(
516 prepareForJavaTest,
517 PrepareForTestWithJavaSdkLibraryFiles,
518 FixtureWithLastReleaseApis("foo"),
519 ).
520 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
521 RunTestWithBp(t, `
522 java_sdk_library {
523 name: "foo",
524 srcs: ["a.java"],
525 api_packages: ["foo"],
526 public: {
527 enabled: true,
528 },
529 }
530
531 java_library {
532 name: "bar",
533 srcs: ["b.java", ":foo{.system.stubs.source}"],
534 }
535 `)
536}
537
538func TestJavaSdkLibrary_Deps(t *testing.T) {
539 result := android.GroupFixturePreparers(
540 prepareForJavaTest,
541 PrepareForTestWithJavaSdkLibraryFiles,
542 FixtureWithLastReleaseApis("sdklib"),
Jihoon Kangbd093452023-12-26 19:08:01 +0000543 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
544 variables.BuildFlags = map[string]string{
545 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
546 }
547 }),
Colin Cross1d2b6b32021-06-01 13:18:08 -0700548 ).RunTestWithBp(t, `
549 java_sdk_library {
550 name: "sdklib",
551 srcs: ["a.java"],
552 sdk_version: "none",
553 system_modules: "none",
554 public: {
555 enabled: true,
556 },
557 }
558 `)
559
560 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
561 `dex2oatd`,
Jihoon Kang748a24d2024-03-20 21:29:39 +0000562 `sdklib-removed.api.combined.public.latest`,
563 `sdklib.api.combined.public.latest`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700564 `sdklib.impl`,
565 `sdklib.stubs`,
Jihoon Kangbd093452023-12-26 19:08:01 +0000566 `sdklib.stubs.exportable`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700567 `sdklib.stubs.source`,
568 `sdklib.xml`,
569 })
570}
571
572func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
573 prepareForJavaTest.RunTestWithBp(t, `
574 java_sdk_library_import {
575 name: "foo",
576 public: {
577 jars: ["a.jar"],
578 stub_srcs: ["a.java"],
579 current_api: "api/current.txt",
580 removed_api: "api/removed.txt",
Anton Hanssond78eb762021-09-21 15:25:12 +0100581 annotations: "x/annotations.zip",
Colin Cross1d2b6b32021-06-01 13:18:08 -0700582 },
583 }
584
585 java_library {
586 name: "bar",
587 srcs: [":foo{.public.stubs.source}"],
588 java_resources: [
589 ":foo{.public.api.txt}",
590 ":foo{.public.removed-api.txt}",
Anton Hanssond78eb762021-09-21 15:25:12 +0100591 ":foo{.public.annotations.zip}",
Colin Cross1d2b6b32021-06-01 13:18:08 -0700592 ],
593 }
594 `)
595}
596
597func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
598 bp := `
599 java_sdk_library_import {
600 name: "foo",
601 public: {
602 jars: ["a.jar"],
603 },
604 }
605 `
606
607 t.Run("stubs.source", func(t *testing.T) {
608 prepareForJavaTest.
609 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
610 RunTestWithBp(t, bp+`
611 java_library {
612 name: "bar",
613 srcs: [":foo{.public.stubs.source}"],
614 java_resources: [
615 ":foo{.public.api.txt}",
616 ":foo{.public.removed-api.txt}",
617 ],
618 }
619 `)
620 })
621
622 t.Run("api.txt", func(t *testing.T) {
623 prepareForJavaTest.
624 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
625 RunTestWithBp(t, bp+`
626 java_library {
627 name: "bar",
628 srcs: ["a.java"],
629 java_resources: [
630 ":foo{.public.api.txt}",
631 ],
632 }
633 `)
634 })
635
636 t.Run("removed-api.txt", func(t *testing.T) {
637 prepareForJavaTest.
638 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
639 RunTestWithBp(t, bp+`
640 java_library {
641 name: "bar",
642 srcs: ["a.java"],
643 java_resources: [
644 ":foo{.public.removed-api.txt}",
645 ],
646 }
647 `)
648 })
649}
650
651func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
652 prepareForJavaTest.
653 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
654 RunTestWithBp(t, `
655 java_sdk_library {
656 name: "foo",
657 srcs: ["a.java", "b.java"],
658 api_packages: ["foo"],
659 // Explicitly disable public to test the check that ensures the set of enabled
660 // scopes is consistent.
661 public: {
662 enabled: false,
663 },
664 system: {
665 enabled: true,
666 },
667 }
668 `)
669}
670
671func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
672 android.GroupFixturePreparers(
673 prepareForJavaTest,
674 PrepareForTestWithJavaSdkLibraryFiles,
675 FixtureWithLastReleaseApis("foo"),
676 ).RunTestWithBp(t, `
677 java_sdk_library {
678 name: "foo",
679 srcs: ["a.java", "b.java"],
680 api_packages: ["foo"],
681 system: {
682 enabled: true,
683 sdk_version: "module_current",
684 },
685 }
686 `)
687}
688
689func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
690 android.GroupFixturePreparers(
691 prepareForJavaTest,
692 PrepareForTestWithJavaSdkLibraryFiles,
693 FixtureWithLastReleaseApis("foo"),
694 ).RunTestWithBp(t, `
695 java_sdk_library {
696 name: "foo",
697 srcs: ["a.java", "b.java"],
698 api_packages: ["foo"],
699 system: {
700 enabled: true,
701 },
702 module_lib: {
703 enabled: true,
704 },
705 }
706 `)
707}
708
709func TestJavaSdkLibrary_SystemServer(t *testing.T) {
710 android.GroupFixturePreparers(
711 prepareForJavaTest,
712 PrepareForTestWithJavaSdkLibraryFiles,
713 FixtureWithLastReleaseApis("foo"),
714 ).RunTestWithBp(t, `
715 java_sdk_library {
716 name: "foo",
717 srcs: ["a.java", "b.java"],
718 api_packages: ["foo"],
719 system: {
720 enabled: true,
721 },
722 system_server: {
723 enabled: true,
724 },
725 }
726 `)
727}
728
Paul Duffind0b9fca2022-09-30 18:11:41 +0100729func TestJavaSdkLibrary_SystemServer_AccessToStubScopeLibs(t *testing.T) {
730 result := android.GroupFixturePreparers(
731 prepareForJavaTest,
732 PrepareForTestWithJavaSdkLibraryFiles,
733 FixtureWithLastReleaseApis("foo-public", "foo-system", "foo-module-lib", "foo-system-server"),
734 ).RunTestWithBp(t, `
735 java_sdk_library {
736 name: "foo-public",
737 srcs: ["a.java"],
738 api_packages: ["foo"],
739 public: {
740 enabled: true,
741 },
742 }
743
744 java_sdk_library {
745 name: "foo-system",
746 srcs: ["a.java"],
747 api_packages: ["foo"],
748 system: {
749 enabled: true,
750 },
751 }
752
753 java_sdk_library {
754 name: "foo-module-lib",
755 srcs: ["a.java"],
756 api_packages: ["foo"],
757 system: {
758 enabled: true,
759 },
760 module_lib: {
761 enabled: true,
762 },
763 }
764
765 java_sdk_library {
766 name: "foo-system-server",
767 srcs: ["a.java"],
768 api_packages: ["foo"],
769 system_server: {
770 enabled: true,
771 },
772 }
773
774 java_library {
775 name: "bar",
776 srcs: ["a.java"],
777 libs: ["foo-public", "foo-system", "foo-module-lib", "foo-system-server"],
778 sdk_version: "system_server_current",
779 }
780 `)
781
782 stubsPath := func(name string, scope *apiScope) string {
783 name = scope.stubsLibraryModuleName(name)
784 return fmt.Sprintf("out/soong/.intermediates/%[1]s/android_common/turbine-combined/%[1]s.jar", name)
785 }
786
787 // The bar library should depend on the highest (where system server is highest and public is
788 // lowest) API scopes provided by each of the foo-* modules. The highest API scope provided by the
789 // foo-<x> module is <x>.
790 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
791 stubLibraries := []string{
792 stubsPath("foo-public", apiScopePublic),
793 stubsPath("foo-system", apiScopeSystem),
794 stubsPath("foo-module-lib", apiScopeModuleLib),
795 stubsPath("foo-system-server", apiScopeSystemServer),
796 }
797 expectedPattern := fmt.Sprintf(`^-classpath .*:\Q%s\E$`, strings.Join(stubLibraries, ":"))
798 if expected, actual := expectedPattern, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
799 t.Errorf("expected pattern %q to match %#q", expected, actual)
800 }
801}
802
Colin Cross1d2b6b32021-06-01 13:18:08 -0700803func TestJavaSdkLibrary_MissingScope(t *testing.T) {
804 prepareForJavaTest.
805 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
806 RunTestWithBp(t, `
807 java_sdk_library {
808 name: "foo",
809 srcs: ["a.java"],
810 public: {
811 enabled: false,
812 },
813 }
814
815 java_library {
816 name: "baz",
817 srcs: ["a.java"],
818 libs: ["foo"],
819 sdk_version: "module_current",
820 }
821 `)
822}
823
824func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
825 android.GroupFixturePreparers(
826 prepareForJavaTest,
827 PrepareForTestWithJavaSdkLibraryFiles,
828 FixtureWithLastReleaseApis("foo"),
829 ).RunTestWithBp(t, `
830 java_sdk_library {
831 name: "foo",
832 srcs: ["a.java"],
833 system: {
834 enabled: true,
835 },
836 }
837
838 java_library {
839 name: "baz",
840 srcs: ["a.java"],
841 libs: ["foo"],
842 // foo does not have module-lib scope so it should fallback to system
843 sdk_version: "module_current",
844 }
845 `)
846}
847
848func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
849 result := android.GroupFixturePreparers(
850 prepareForJavaTest,
851 PrepareForTestWithJavaSdkLibraryFiles,
852 FixtureWithLastReleaseApis("foo"),
853 ).RunTestWithBp(t, `
854 java_sdk_library {
855 name: "foo",
856 srcs: ["a.java"],
857 system: {
858 enabled: true,
859 },
860 default_to_stubs: true,
861 }
862
863 java_library {
864 name: "baz",
865 srcs: ["a.java"],
866 libs: ["foo"],
867 // does not have sdk_version set, should fallback to module,
868 // which will then fallback to system because the module scope
869 // is not enabled.
870 }
871 `)
872 // The baz library should depend on the system stubs jar.
873 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
874 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
875 t.Errorf("expected %q, found %#q", expected, actual)
876 }
877}
878
879func TestJavaSdkLibraryImport(t *testing.T) {
880 result := prepareForJavaTest.RunTestWithBp(t, `
881 java_library {
882 name: "foo",
883 srcs: ["a.java"],
884 libs: ["sdklib"],
885 sdk_version: "current",
886 }
887
888 java_library {
889 name: "foo.system",
890 srcs: ["a.java"],
891 libs: ["sdklib"],
892 sdk_version: "system_current",
893 }
894
895 java_library {
896 name: "foo.test",
897 srcs: ["a.java"],
898 libs: ["sdklib"],
899 sdk_version: "test_current",
900 }
901
902 java_sdk_library_import {
903 name: "sdklib",
904 public: {
905 jars: ["a.jar"],
906 },
907 system: {
908 jars: ["b.jar"],
909 },
910 test: {
911 jars: ["c.jar"],
912 stub_srcs: ["c.java"],
913 },
914 }
915 `)
916
917 for _, scope := range []string{"", ".system", ".test"} {
918 fooModule := result.ModuleForTests("foo"+scope, "android_common")
919 javac := fooModule.Rule("javac")
920
921 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
922 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
923 }
924
925 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Jiakai Zhang204356f2021-09-09 08:12:46 +0000926 `dex2oatd`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700927 `prebuilt_sdklib.stubs`,
928 `prebuilt_sdklib.stubs.source.test`,
929 `prebuilt_sdklib.stubs.system`,
930 `prebuilt_sdklib.stubs.test`,
931 })
932}
933
934func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
935 result := android.GroupFixturePreparers(
936 prepareForJavaTest,
937 PrepareForTestWithJavaSdkLibraryFiles,
938 FixtureWithLastReleaseApis("sdklib"),
Jihoon Kangbd093452023-12-26 19:08:01 +0000939 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
940 variables.BuildFlags = map[string]string{
941 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
942 }
943 }),
Colin Cross1d2b6b32021-06-01 13:18:08 -0700944 ).RunTestWithBp(t, `
945 java_sdk_library {
946 name: "sdklib",
947 srcs: ["a.java"],
948 sdk_version: "none",
949 system_modules: "none",
950 public: {
951 enabled: true,
952 },
953 }
954
955 java_sdk_library_import {
956 name: "sdklib",
957 public: {
958 jars: ["a.jar"],
959 },
960 }
961 `)
962
963 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
964 `dex2oatd`,
965 `prebuilt_sdklib`,
Jihoon Kang748a24d2024-03-20 21:29:39 +0000966 `sdklib-removed.api.combined.public.latest`,
967 `sdklib.api.combined.public.latest`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700968 `sdklib.impl`,
969 `sdklib.stubs`,
Jihoon Kangbd093452023-12-26 19:08:01 +0000970 `sdklib.stubs.exportable`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700971 `sdklib.stubs.source`,
972 `sdklib.xml`,
973 })
974
975 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Spandan Dasda739a32023-12-13 00:06:32 +0000976 `all_apex_contributions`,
Colin Cross1d2b6b32021-06-01 13:18:08 -0700977 `prebuilt_sdklib.stubs`,
978 `sdklib.impl`,
979 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
980 // dependency is added after prebuilts may have been renamed and so has to use
981 // the renamed name.
982 `sdklib.xml`,
983 })
984}
985
Paul Duffinbf4de042022-09-27 12:41:52 +0100986func testJavaSdkLibraryImport_Preferred(t *testing.T, prefer string, preparer android.FixturePreparer) {
Colin Cross1d2b6b32021-06-01 13:18:08 -0700987 result := android.GroupFixturePreparers(
988 prepareForJavaTest,
989 PrepareForTestWithJavaSdkLibraryFiles,
990 FixtureWithLastReleaseApis("sdklib"),
Paul Duffinbf4de042022-09-27 12:41:52 +0100991 preparer,
Jihoon Kangbd093452023-12-26 19:08:01 +0000992 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
993 variables.BuildFlags = map[string]string{
994 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
995 }
996 }),
Colin Cross1d2b6b32021-06-01 13:18:08 -0700997 ).RunTestWithBp(t, `
998 java_sdk_library {
999 name: "sdklib",
1000 srcs: ["a.java"],
1001 sdk_version: "none",
1002 system_modules: "none",
1003 public: {
1004 enabled: true,
1005 },
1006 }
1007
1008 java_sdk_library_import {
1009 name: "sdklib",
Paul Duffinbf4de042022-09-27 12:41:52 +01001010 `+prefer+`
Colin Cross1d2b6b32021-06-01 13:18:08 -07001011 public: {
1012 jars: ["a.jar"],
Paul Duffinbf4de042022-09-27 12:41:52 +01001013 stub_srcs: ["a.java"],
1014 current_api: "current.txt",
1015 removed_api: "removed.txt",
1016 annotations: "annotations.zip",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001017 },
1018 }
Paul Duffinbf4de042022-09-27 12:41:52 +01001019
1020 java_library {
1021 name: "combined",
1022 static_libs: [
1023 "sdklib.stubs",
1024 ],
1025 java_resources: [
1026 ":sdklib.stubs.source",
1027 ":sdklib{.public.api.txt}",
1028 ":sdklib{.public.removed-api.txt}",
1029 ":sdklib{.public.annotations.zip}",
1030 ],
1031 sdk_version: "none",
1032 system_modules: "none",
1033 }
1034
1035 java_library {
1036 name: "public",
1037 srcs: ["a.java"],
1038 libs: ["sdklib"],
1039 sdk_version: "current",
1040 }
Colin Cross1d2b6b32021-06-01 13:18:08 -07001041 `)
1042
1043 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Colin Cross1d2b6b32021-06-01 13:18:08 -07001044 `prebuilt_sdklib`,
Jihoon Kang748a24d2024-03-20 21:29:39 +00001045 `sdklib-removed.api.combined.public.latest`,
1046 `sdklib.api.combined.public.latest`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001047 `sdklib.impl`,
1048 `sdklib.stubs`,
Jihoon Kangbd093452023-12-26 19:08:01 +00001049 `sdklib.stubs.exportable`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001050 `sdklib.stubs.source`,
1051 `sdklib.xml`,
1052 })
1053
1054 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Spandan Dasda739a32023-12-13 00:06:32 +00001055 `all_apex_contributions`,
Jiakai Zhang204356f2021-09-09 08:12:46 +00001056 `dex2oatd`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001057 `prebuilt_sdklib.stubs`,
Paul Duffinbf4de042022-09-27 12:41:52 +01001058 `prebuilt_sdklib.stubs.source`,
Colin Cross1d2b6b32021-06-01 13:18:08 -07001059 `sdklib.impl`,
1060 `sdklib.xml`,
1061 })
Paul Duffinbf4de042022-09-27 12:41:52 +01001062
1063 // Make sure that dependencies on child modules use the prebuilt when preferred.
1064 CheckModuleDependencies(t, result.TestContext, "combined", "android_common", []string{
1065 // Each use of :sdklib{...} adds a dependency onto prebuilt_sdklib.
1066 `prebuilt_sdklib`,
1067 `prebuilt_sdklib`,
1068 `prebuilt_sdklib`,
1069 `prebuilt_sdklib.stubs`,
1070 `prebuilt_sdklib.stubs.source`,
1071 })
1072
1073 // Make sure that dependencies on sdklib that resolve to one of the child libraries use the
1074 // prebuilt library.
1075 public := result.ModuleForTests("public", "android_common")
1076 rule := public.Output("javac/public.jar")
1077 inputs := rule.Implicits.Strings()
1078 expected := "out/soong/.intermediates/prebuilt_sdklib.stubs/android_common/combined/sdklib.stubs.jar"
1079 if !android.InList(expected, inputs) {
1080 t.Errorf("expected %q to contain %q", inputs, expected)
1081 }
1082}
1083
1084func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
1085 t.Run("prefer", func(t *testing.T) {
1086 testJavaSdkLibraryImport_Preferred(t, "prefer: true,", android.NullFixturePreparer)
1087 })
Colin Cross1d2b6b32021-06-01 13:18:08 -07001088}
1089
Spandan Dasfc12d2f2023-10-27 20:06:32 +00001090// If a module is listed in `mainline_module_contributions, it should be used
1091// It will supersede any other source vs prebuilt selection mechanism like `prefer` attribute
1092func TestSdkLibraryImport_MetadataModuleSupersedesPreferred(t *testing.T) {
1093 bp := `
1094 apex_contributions {
1095 name: "my_mainline_module_contributions",
1096 api_domain: "my_mainline_module",
1097 contents: [
1098 // legacy mechanism prefers the prebuilt
1099 // mainline_module_contributions supersedes this since source is listed explicitly
1100 "sdklib.prebuilt_preferred_using_legacy_flags",
1101
1102 // legacy mechanism prefers the source
1103 // mainline_module_contributions supersedes this since prebuilt is listed explicitly
1104 "prebuilt_sdklib.source_preferred_using_legacy_flags",
1105 ],
1106 }
Spandan Dasfc12d2f2023-10-27 20:06:32 +00001107 java_sdk_library {
1108 name: "sdklib.prebuilt_preferred_using_legacy_flags",
1109 srcs: ["a.java"],
1110 sdk_version: "none",
1111 system_modules: "none",
1112 public: {
1113 enabled: true,
1114 },
1115 system: {
1116 enabled: true,
1117 }
1118 }
1119 java_sdk_library_import {
1120 name: "sdklib.prebuilt_preferred_using_legacy_flags",
1121 prefer: true, // prebuilt is preferred using legacy mechanism
1122 public: {
1123 jars: ["a.jar"],
1124 stub_srcs: ["a.java"],
1125 current_api: "current.txt",
1126 removed_api: "removed.txt",
1127 annotations: "annotations.zip",
1128 },
1129 system: {
1130 jars: ["a.jar"],
1131 stub_srcs: ["a.java"],
1132 current_api: "current.txt",
1133 removed_api: "removed.txt",
1134 annotations: "annotations.zip",
1135 },
1136 }
1137 java_sdk_library {
1138 name: "sdklib.source_preferred_using_legacy_flags",
1139 srcs: ["a.java"],
1140 sdk_version: "none",
1141 system_modules: "none",
1142 public: {
1143 enabled: true,
1144 },
1145 system: {
1146 enabled: true,
1147 }
1148 }
1149 java_sdk_library_import {
1150 name: "sdklib.source_preferred_using_legacy_flags",
1151 prefer: false, // source is preferred using legacy mechanism
1152 public: {
1153 jars: ["a.jar"],
1154 stub_srcs: ["a.java"],
1155 current_api: "current.txt",
1156 removed_api: "removed.txt",
1157 annotations: "annotations.zip",
1158 },
1159 system: {
1160 jars: ["a.jar"],
1161 stub_srcs: ["a.java"],
1162 current_api: "current.txt",
1163 removed_api: "removed.txt",
1164 annotations: "annotations.zip",
1165 },
1166 }
1167
1168 // rdeps
1169 java_library {
1170 name: "public",
1171 srcs: ["a.java"],
1172 libs: [
1173 // this should get source since source is listed in my_mainline_module_contributions
1174 "sdklib.prebuilt_preferred_using_legacy_flags.stubs",
1175 "sdklib.prebuilt_preferred_using_legacy_flags.stubs.system",
1176
1177 // this should get prebuilt since source is listed in my_mainline_module_contributions
1178 "sdklib.source_preferred_using_legacy_flags.stubs",
1179 "sdklib.source_preferred_using_legacy_flags.stubs.system",
1180
1181 ],
1182 }
1183 `
1184 result := android.GroupFixturePreparers(
1185 prepareForJavaTest,
1186 PrepareForTestWithJavaSdkLibraryFiles,
1187 FixtureWithLastReleaseApis("sdklib.source_preferred_using_legacy_flags", "sdklib.prebuilt_preferred_using_legacy_flags"),
Spandan Dasfc12d2f2023-10-27 20:06:32 +00001188 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1189 variables.BuildFlags = map[string]string{
1190 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "my_mainline_module_contributions",
1191 }
1192 }),
1193 ).RunTestWithBp(t, bp)
1194
1195 // Make sure that rdeps get the correct source vs prebuilt based on mainline_module_contributions
1196 public := result.ModuleForTests("public", "android_common")
1197 rule := public.Output("javac/public.jar")
1198 inputs := rule.Implicits.Strings()
1199 expectedInputs := []string{
1200 // source
1201 "out/soong/.intermediates/sdklib.prebuilt_preferred_using_legacy_flags.stubs/android_common/turbine-combined/sdklib.prebuilt_preferred_using_legacy_flags.stubs.jar",
1202 "out/soong/.intermediates/sdklib.prebuilt_preferred_using_legacy_flags.stubs.system/android_common/turbine-combined/sdklib.prebuilt_preferred_using_legacy_flags.stubs.system.jar",
1203
1204 // prebuilt
1205 "out/soong/.intermediates/prebuilt_sdklib.source_preferred_using_legacy_flags.stubs/android_common/combined/sdklib.source_preferred_using_legacy_flags.stubs.jar",
1206 "out/soong/.intermediates/prebuilt_sdklib.source_preferred_using_legacy_flags.stubs.system/android_common/combined/sdklib.source_preferred_using_legacy_flags.stubs.system.jar",
1207 }
1208 for _, expected := range expectedInputs {
1209 if !android.InList(expected, inputs) {
1210 t.Errorf("expected %q to contain %q", inputs, expected)
1211 }
1212 }
1213}
1214
Colin Cross1d2b6b32021-06-01 13:18:08 -07001215func TestJavaSdkLibraryEnforce(t *testing.T) {
1216 partitionToBpOption := func(partition string) string {
1217 switch partition {
1218 case "system":
1219 return ""
1220 case "vendor":
1221 return "soc_specific: true,"
1222 case "product":
1223 return "product_specific: true,"
1224 default:
1225 panic("Invalid partition group name: " + partition)
1226 }
1227 }
1228
1229 type testConfigInfo struct {
1230 libraryType string
1231 fromPartition string
1232 toPartition string
Colin Cross1d2b6b32021-06-01 13:18:08 -07001233 enforceProductInterface bool
1234 enforceJavaSdkLibraryCheck bool
1235 allowList []string
1236 }
1237
1238 createPreparer := func(info testConfigInfo) android.FixturePreparer {
1239 bpFileTemplate := `
1240 java_library {
1241 name: "foo",
1242 srcs: ["foo.java"],
1243 libs: ["bar"],
1244 sdk_version: "current",
1245 %s
1246 }
1247
1248 %s {
1249 name: "bar",
1250 srcs: ["bar.java"],
1251 sdk_version: "current",
1252 %s
1253 }
1254 `
1255
1256 bpFile := fmt.Sprintf(bpFileTemplate,
1257 partitionToBpOption(info.fromPartition),
1258 info.libraryType,
1259 partitionToBpOption(info.toPartition))
1260
1261 return android.GroupFixturePreparers(
1262 PrepareForTestWithJavaSdkLibraryFiles,
1263 FixtureWithLastReleaseApis("bar"),
1264 android.FixtureWithRootAndroidBp(bpFile),
1265 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1266 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
Colin Cross1d2b6b32021-06-01 13:18:08 -07001267 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
1268 variables.InterPartitionJavaLibraryAllowList = info.allowList
1269 }),
1270 )
1271 }
1272
1273 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
1274 t.Run(fmt.Sprintf("%v", info), func(t *testing.T) {
1275 errorHandler := android.FixtureExpectsNoErrors
1276 if expectedErrorPattern != "" {
1277 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
1278 }
1279 android.GroupFixturePreparers(
1280 prepareForJavaTest,
1281 createPreparer(info),
1282 ).
1283 ExtendWithErrorHandler(errorHandler).
1284 RunTest(t)
1285 })
1286 }
1287
1288 errorMessage := "is not allowed across the partitions"
1289
1290 runTest(t, testConfigInfo{
1291 libraryType: "java_library",
1292 fromPartition: "product",
1293 toPartition: "system",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001294 enforceProductInterface: true,
1295 enforceJavaSdkLibraryCheck: false,
1296 }, "")
1297
1298 runTest(t, testConfigInfo{
1299 libraryType: "java_library",
1300 fromPartition: "product",
1301 toPartition: "system",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001302 enforceProductInterface: false,
1303 enforceJavaSdkLibraryCheck: true,
1304 }, "")
1305
1306 runTest(t, testConfigInfo{
1307 libraryType: "java_library",
1308 fromPartition: "product",
1309 toPartition: "system",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001310 enforceProductInterface: true,
1311 enforceJavaSdkLibraryCheck: true,
1312 }, errorMessage)
1313
1314 runTest(t, testConfigInfo{
1315 libraryType: "java_library",
1316 fromPartition: "vendor",
1317 toPartition: "system",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001318 enforceProductInterface: true,
1319 enforceJavaSdkLibraryCheck: true,
1320 }, errorMessage)
1321
1322 runTest(t, testConfigInfo{
1323 libraryType: "java_library",
1324 fromPartition: "vendor",
1325 toPartition: "system",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001326 enforceProductInterface: true,
1327 enforceJavaSdkLibraryCheck: true,
1328 allowList: []string{"bar"},
1329 }, "")
1330
1331 runTest(t, testConfigInfo{
1332 libraryType: "java_library",
1333 fromPartition: "vendor",
1334 toPartition: "product",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001335 enforceProductInterface: true,
1336 enforceJavaSdkLibraryCheck: true,
1337 }, errorMessage)
1338
1339 runTest(t, testConfigInfo{
1340 libraryType: "java_sdk_library",
1341 fromPartition: "product",
1342 toPartition: "system",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001343 enforceProductInterface: true,
1344 enforceJavaSdkLibraryCheck: true,
1345 }, "")
1346
1347 runTest(t, testConfigInfo{
1348 libraryType: "java_sdk_library",
1349 fromPartition: "vendor",
1350 toPartition: "system",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001351 enforceProductInterface: true,
1352 enforceJavaSdkLibraryCheck: true,
1353 }, "")
1354
1355 runTest(t, testConfigInfo{
1356 libraryType: "java_sdk_library",
1357 fromPartition: "vendor",
1358 toPartition: "product",
Colin Cross1d2b6b32021-06-01 13:18:08 -07001359 enforceProductInterface: true,
1360 enforceJavaSdkLibraryCheck: true,
1361 }, "")
1362}
Colin Cross30c491b2021-06-01 13:39:09 -07001363
1364func TestJavaSdkLibraryDist(t *testing.T) {
1365 result := android.GroupFixturePreparers(
1366 PrepareForTestWithJavaBuildComponents,
1367 PrepareForTestWithJavaDefaultModules,
1368 PrepareForTestWithJavaSdkLibraryFiles,
Colin Cross67c17ae2021-06-02 13:02:51 -07001369 FixtureWithLastReleaseApis(
1370 "sdklib_no_group",
1371 "sdklib_group_foo",
1372 "sdklib_owner_foo",
1373 "foo"),
Jihoon Kang02168052024-03-20 00:44:54 +00001374 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1375 variables.BuildFlags = map[string]string{
1376 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
1377 }
1378 }),
Colin Cross30c491b2021-06-01 13:39:09 -07001379 ).RunTestWithBp(t, `
1380 java_sdk_library {
Colin Cross59b92bf2021-06-01 14:07:56 -07001381 name: "sdklib_no_group",
Colin Cross30c491b2021-06-01 13:39:09 -07001382 srcs: ["foo.java"],
1383 }
1384
1385 java_sdk_library {
Colin Cross986b69a2021-06-01 13:13:40 -07001386 name: "sdklib_group_foo",
Colin Cross986b69a2021-06-01 13:13:40 -07001387 srcs: ["foo.java"],
1388 dist_group: "foo",
1389 }
1390
1391 java_sdk_library {
Colin Cross30c491b2021-06-01 13:39:09 -07001392 name: "sdklib_owner_foo",
Colin Cross30c491b2021-06-01 13:39:09 -07001393 srcs: ["foo.java"],
1394 owner: "foo",
1395 }
1396
1397 java_sdk_library {
1398 name: "sdklib_stem_foo",
Colin Cross30c491b2021-06-01 13:39:09 -07001399 srcs: ["foo.java"],
1400 dist_stem: "foo",
1401 }
Colin Cross30c491b2021-06-01 13:39:09 -07001402 `)
1403
1404 type testCase struct {
1405 module string
1406 distDir string
1407 distStem string
1408 }
1409 testCases := []testCase{
1410 {
Colin Cross59b92bf2021-06-01 14:07:56 -07001411 module: "sdklib_no_group",
Colin Cross3dd66252021-06-01 14:05:09 -07001412 distDir: "apistubs/unknown/public",
Colin Cross59b92bf2021-06-01 14:07:56 -07001413 distStem: "sdklib_no_group.jar",
Colin Cross30c491b2021-06-01 13:39:09 -07001414 },
1415 {
Colin Cross986b69a2021-06-01 13:13:40 -07001416 module: "sdklib_group_foo",
1417 distDir: "apistubs/foo/public",
1418 distStem: "sdklib_group_foo.jar",
1419 },
1420 {
Colin Cross59b92bf2021-06-01 14:07:56 -07001421 // Owner doesn't affect distDir after b/186723288.
Colin Cross30c491b2021-06-01 13:39:09 -07001422 module: "sdklib_owner_foo",
Colin Cross59b92bf2021-06-01 14:07:56 -07001423 distDir: "apistubs/unknown/public",
Colin Cross30c491b2021-06-01 13:39:09 -07001424 distStem: "sdklib_owner_foo.jar",
1425 },
1426 {
1427 module: "sdklib_stem_foo",
Colin Cross3dd66252021-06-01 14:05:09 -07001428 distDir: "apistubs/unknown/public",
Colin Cross30c491b2021-06-01 13:39:09 -07001429 distStem: "foo.jar",
1430 },
Colin Cross30c491b2021-06-01 13:39:09 -07001431 }
1432
1433 for _, tt := range testCases {
1434 t.Run(tt.module, func(t *testing.T) {
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001435 m := result.ModuleForTests(apiScopePublic.exportableStubsLibraryModuleName(tt.module), "android_common").Module().(*Library)
Colin Cross30c491b2021-06-01 13:39:09 -07001436 dists := m.Dists()
1437 if len(dists) != 1 {
1438 t.Fatalf("expected exactly 1 dist entry, got %d", len(dists))
1439 }
1440 if g, w := String(dists[0].Dir), tt.distDir; g != w {
1441 t.Errorf("expected dist dir %q, got %q", w, g)
1442 }
1443 if g, w := String(dists[0].Dest), tt.distStem; g != w {
1444 t.Errorf("expected dist stem %q, got %q", w, g)
1445 }
1446 })
1447 }
1448}
satayev8f088b02021-12-06 11:40:46 +00001449
1450func TestSdkLibrary_CheckMinSdkVersion(t *testing.T) {
1451 preparer := android.GroupFixturePreparers(
1452 PrepareForTestWithJavaBuildComponents,
1453 PrepareForTestWithJavaDefaultModules,
1454 PrepareForTestWithJavaSdkLibraryFiles,
1455 )
1456
1457 preparer.RunTestWithBp(t, `
1458 java_sdk_library {
1459 name: "sdklib",
1460 srcs: ["a.java"],
1461 static_libs: ["util"],
1462 min_sdk_version: "30",
1463 unsafe_ignore_missing_latest_api: true,
1464 }
1465
1466 java_library {
1467 name: "util",
1468 srcs: ["a.java"],
1469 min_sdk_version: "30",
1470 }
1471 `)
1472
1473 preparer.
1474 RunTestWithBp(t, `
1475 java_sdk_library {
1476 name: "sdklib",
1477 srcs: ["a.java"],
1478 libs: ["util"],
1479 impl_only_libs: ["util"],
1480 stub_only_libs: ["util"],
1481 stub_only_static_libs: ["util"],
1482 min_sdk_version: "30",
1483 unsafe_ignore_missing_latest_api: true,
1484 }
1485
1486 java_library {
1487 name: "util",
1488 srcs: ["a.java"],
1489 }
1490 `)
1491
1492 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "util".*should support min_sdk_version\(30\)`)).
1493 RunTestWithBp(t, `
1494 java_sdk_library {
1495 name: "sdklib",
1496 srcs: ["a.java"],
1497 static_libs: ["util"],
1498 min_sdk_version: "30",
1499 unsafe_ignore_missing_latest_api: true,
1500 }
1501
1502 java_library {
1503 name: "util",
1504 srcs: ["a.java"],
1505 min_sdk_version: "31",
1506 }
1507 `)
1508
1509 preparer.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "another_util".*should support min_sdk_version\(30\)`)).
1510 RunTestWithBp(t, `
1511 java_sdk_library {
1512 name: "sdklib",
1513 srcs: ["a.java"],
1514 static_libs: ["util"],
1515 min_sdk_version: "30",
1516 unsafe_ignore_missing_latest_api: true,
1517 }
1518
1519 java_library {
1520 name: "util",
1521 srcs: ["a.java"],
1522 static_libs: ["another_util"],
1523 min_sdk_version: "30",
1524 }
1525
1526 java_library {
1527 name: "another_util",
1528 srcs: ["a.java"],
1529 min_sdk_version: "31",
1530 }
1531 `)
1532}
Nikita Ioffed732da72022-11-21 12:38:25 +00001533
1534func TestJavaSdkLibrary_StubOnlyLibs_PassedToDroidstubs(t *testing.T) {
1535 result := android.GroupFixturePreparers(
1536 prepareForJavaTest,
1537 PrepareForTestWithJavaSdkLibraryFiles,
1538 FixtureWithLastReleaseApis("foo"),
1539 ).RunTestWithBp(t, `
1540 java_sdk_library {
1541 name: "foo",
1542 srcs: ["a.java"],
1543 public: {
1544 enabled: true,
1545 },
1546 stub_only_libs: ["bar-lib"],
1547 }
1548
1549 java_library {
1550 name: "bar-lib",
1551 srcs: ["b.java"],
1552 }
1553 `)
1554
1555 // The foo.stubs.source should depend on bar-lib
1556 fooStubsSources := result.ModuleForTests("foo.stubs.source", "android_common").Module().(*Droidstubs)
1557 android.AssertStringListContains(t, "foo stubs should depend on bar-lib", fooStubsSources.Javadoc.properties.Libs, "bar-lib")
1558}
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001559
Mark White9421c4c2023-08-10 00:07:03 +00001560func TestJavaSdkLibrary_Scope_Libs_PassedToDroidstubs(t *testing.T) {
1561 result := android.GroupFixturePreparers(
1562 prepareForJavaTest,
1563 PrepareForTestWithJavaSdkLibraryFiles,
1564 FixtureWithLastReleaseApis("foo"),
1565 ).RunTestWithBp(t, `
1566 java_sdk_library {
1567 name: "foo",
1568 srcs: ["a.java"],
1569 public: {
1570 enabled: true,
1571 libs: ["bar-lib"],
1572 },
1573 }
1574
1575 java_library {
1576 name: "bar-lib",
1577 srcs: ["b.java"],
1578 }
1579 `)
1580
1581 // The foo.stubs.source should depend on bar-lib
1582 fooStubsSources := result.ModuleForTests("foo.stubs.source", "android_common").Module().(*Droidstubs)
1583 android.AssertStringListContains(t, "foo stubs should depend on bar-lib", fooStubsSources.Javadoc.properties.Libs, "bar-lib")
1584}
1585
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001586func TestJavaSdkLibrary_ApiLibrary(t *testing.T) {
1587 result := android.GroupFixturePreparers(
1588 prepareForJavaTest,
1589 PrepareForTestWithJavaSdkLibraryFiles,
1590 FixtureWithLastReleaseApis("foo"),
1591 android.FixtureModifyConfig(func(config android.Config) {
1592 config.SetApiLibraries([]string{"foo"})
1593 }),
1594 ).RunTestWithBp(t, `
1595 java_sdk_library {
1596 name: "foo",
1597 srcs: ["a.java", "b.java"],
1598 api_packages: ["foo"],
1599 system: {
1600 enabled: true,
1601 },
1602 module_lib: {
1603 enabled: true,
1604 },
1605 test: {
1606 enabled: true,
1607 },
1608 }
1609 `)
1610
1611 testCases := []struct {
Jihoon Kangca198c22023-06-22 23:13:51 +00001612 scope *apiScope
1613 apiContributions []string
1614 fullApiSurfaceStub string
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001615 }{
1616 {
Jihoon Kangca198c22023-06-22 23:13:51 +00001617 scope: apiScopePublic,
1618 apiContributions: []string{"foo.stubs.source.api.contribution"},
Jihoon Kange7ee2562023-07-25 05:51:46 +00001619 fullApiSurfaceStub: "android_stubs_current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001620 },
1621 {
Jihoon Kangca198c22023-06-22 23:13:51 +00001622 scope: apiScopeSystem,
1623 apiContributions: []string{"foo.stubs.source.system.api.contribution", "foo.stubs.source.api.contribution"},
Jihoon Kange7ee2562023-07-25 05:51:46 +00001624 fullApiSurfaceStub: "android_system_stubs_current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001625 },
1626 {
Jihoon Kangca198c22023-06-22 23:13:51 +00001627 scope: apiScopeTest,
1628 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 +00001629 fullApiSurfaceStub: "android_test_stubs_current",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001630 },
1631 {
Jihoon Kangca198c22023-06-22 23:13:51 +00001632 scope: apiScopeModuleLib,
1633 apiContributions: []string{"foo.stubs.source.module_lib.api.contribution", "foo.stubs.source.system.api.contribution", "foo.stubs.source.api.contribution"},
1634 fullApiSurfaceStub: "android_module_lib_stubs_current_full.from-text",
Jihoon Kang1c92c3e2023-03-23 17:44:51 +00001635 },
1636 }
1637
1638 for _, c := range testCases {
1639 m := result.ModuleForTests(c.scope.apiLibraryModuleName("foo"), "android_common").Module().(*ApiLibrary)
1640 android.AssertArrayString(t, "Module expected to contain api contributions", c.apiContributions, m.properties.Api_contributions)
Jihoon Kangca198c22023-06-22 23:13:51 +00001641 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 +00001642 }
1643}
Jihoon Kang786df932023-09-07 01:18:31 +00001644
1645func TestStaticDepStubLibrariesVisibility(t *testing.T) {
1646 android.GroupFixturePreparers(
1647 prepareForJavaTest,
1648 PrepareForTestWithJavaSdkLibraryFiles,
1649 FixtureWithLastReleaseApis("foo"),
1650 android.FixtureMergeMockFs(
1651 map[string][]byte{
1652 "A.java": nil,
1653 "dir/Android.bp": []byte(
1654 `
1655 java_library {
1656 name: "bar",
1657 srcs: ["A.java"],
1658 libs: ["foo.stubs.from-source"],
1659 }
1660 `),
1661 "dir/A.java": nil,
1662 },
1663 ).ExtendWithErrorHandler(
1664 android.FixtureExpectsAtLeastOneErrorMatchingPattern(
1665 `module "bar" variant "android_common": depends on //.:foo.stubs.from-source which is not visible to this module`)),
1666 ).RunTestWithBp(t, `
1667 java_sdk_library {
1668 name: "foo",
1669 srcs: ["A.java"],
1670 }
1671 `)
1672}
Jamie Garsidee570ace2023-11-27 12:07:36 +00001673
1674func TestSdkLibraryDependency(t *testing.T) {
1675 result := android.GroupFixturePreparers(
1676 prepareForJavaTest,
1677 PrepareForTestWithJavaSdkLibraryFiles,
1678 FixtureWithPrebuiltApis(map[string][]string{
1679 "30": {"bar", "foo"},
1680 }),
1681 ).RunTestWithBp(t,
1682 `
1683 java_sdk_library {
1684 name: "foo",
1685 srcs: ["a.java", "b.java"],
1686 api_packages: ["foo"],
1687 }
1688
1689 java_sdk_library {
1690 name: "bar",
1691 srcs: ["c.java", "b.java"],
1692 libs: [
1693 "foo",
1694 ],
1695 uses_libs: [
1696 "foo",
1697 ],
1698 }
1699`)
1700
Paul Duffin1816cde2024-04-10 10:58:21 +01001701 barPermissions := result.ModuleForTests("bar.xml", "android_common").Output("bar.xml")
1702 barContents := android.ContentFromFileRuleForTests(t, result.TestContext, barPermissions)
1703 android.AssertStringDoesContain(t, "bar.xml java_sdk_xml command", barContents, `dependency="foo"`)
Jamie Garsidee570ace2023-11-27 12:07:36 +00001704}
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001705
1706func TestSdkLibraryExportableStubsLibrary(t *testing.T) {
1707 result := android.GroupFixturePreparers(
1708 prepareForJavaTest,
1709 PrepareForTestWithJavaSdkLibraryFiles,
1710 FixtureWithLastReleaseApis("foo"),
1711 android.FixtureModifyConfig(func(config android.Config) {
1712 config.SetApiLibraries([]string{"foo"})
1713 }),
1714 ).RunTestWithBp(t, `
1715 aconfig_declarations {
1716 name: "bar",
1717 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +00001718 container: "com.android.foo",
Jihoon Kangfa4a90d2023-12-20 02:53:38 +00001719 srcs: [
1720 "bar.aconfig",
1721 ],
1722 }
1723 java_sdk_library {
1724 name: "foo",
1725 srcs: ["a.java", "b.java"],
1726 api_packages: ["foo"],
1727 system: {
1728 enabled: true,
1729 },
1730 module_lib: {
1731 enabled: true,
1732 },
1733 test: {
1734 enabled: true,
1735 },
1736 aconfig_declarations: [
1737 "bar",
1738 ],
1739 }
1740 `)
1741
1742 exportableStubsLibraryModuleName := apiScopePublic.exportableStubsLibraryModuleName("foo")
1743 exportableSourceStubsLibraryModuleName := apiScopePublic.exportableSourceStubsLibraryModuleName("foo")
1744
1745 // Check modules generation
1746 topLevelModule := result.ModuleForTests(exportableStubsLibraryModuleName, "android_common")
1747 result.ModuleForTests(exportableSourceStubsLibraryModuleName, "android_common")
1748
1749 // Check static lib dependency
1750 android.AssertBoolEquals(t, "exportable top level stubs library module depends on the"+
1751 "exportable source stubs library module", true,
1752 CheckModuleHasDependency(t, result.TestContext, exportableStubsLibraryModuleName,
1753 "android_common", exportableSourceStubsLibraryModuleName),
1754 )
1755 android.AssertArrayString(t, "exportable source stub library is a static lib of the"+
1756 "top level exportable stubs library", []string{exportableSourceStubsLibraryModuleName},
1757 topLevelModule.Module().(*Library).properties.Static_libs)
1758}
Spandan Das81d95c52024-02-01 23:41:11 +00001759
1760// For java libraries depending on java_sdk_library(_import) via libs, assert that
1761// rdep gets stubs of source if source is listed in apex_contributions and prebuilt has prefer (legacy mechanism)
1762func TestStubResolutionOfJavaSdkLibraryInLibs(t *testing.T) {
1763 bp := `
1764 apex_contributions {
1765 name: "my_mainline_module_contributions",
1766 api_domain: "my_mainline_module",
1767 contents: ["sdklib"], // source is selected using apex_contributions, but prebuilt is selected using prefer
1768 }
1769 java_sdk_library {
1770 name: "sdklib",
1771 srcs: ["a.java"],
1772 sdk_version: "none",
1773 system_modules: "none",
1774 public: {
1775 enabled: true,
1776 },
1777 }
1778 java_sdk_library_import {
1779 name: "sdklib",
1780 public: {
1781 jars: ["a.jar"],
1782 stub_srcs: ["a.java"],
1783 current_api: "current.txt",
1784 removed_api: "removed.txt",
1785 annotations: "annotations.zip",
1786 },
1787 prefer: true, // Set prefer explicitly on the prebuilt. We will assert that rdep gets source in a test case.
1788 }
1789 // rdeps
1790 java_library {
1791 name: "mymodule",
1792 srcs: ["a.java"],
1793 sdk_version: "current",
1794 libs: ["sdklib",], // this should be dynamically resolved to sdklib.stubs (source) or prebuilt_sdklib.stubs (prebuilt)
1795 }
1796 `
1797
1798 fixture := android.GroupFixturePreparers(
1799 prepareForJavaTest,
1800 PrepareForTestWithJavaSdkLibraryFiles,
1801 FixtureWithLastReleaseApis("sdklib"),
1802 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1803 variables.BuildFlags = map[string]string{
1804 // We can use any of the apex contribution build flags from build/soong/android/config.go#mainlineApexContributionBuildFlags here
1805 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "my_mainline_module_contributions",
1806 }
1807 }),
1808 )
1809
1810 result := fixture.RunTestWithBp(t, bp)
1811 // Make sure that rdeps get the correct source vs prebuilt based on mainline_module_contributions
1812 public := result.ModuleForTests("mymodule", "android_common")
1813 rule := public.Output("javac/mymodule.jar")
1814 inputs := rule.Implicits.Strings()
1815 android.AssertStringListContains(t, "Could not find the expected stub on classpath", inputs, "out/soong/.intermediates/sdklib.stubs/android_common/turbine-combined/sdklib.stubs.jar")
1816}
Spandan Das23956d12024-01-19 00:22:22 +00001817
1818// test that rdep gets resolved to the correct version of a java_sdk_library (source or a specific prebuilt)
1819func TestMultipleSdkLibraryPrebuilts(t *testing.T) {
1820 bp := `
1821 apex_contributions {
1822 name: "my_mainline_module_contributions",
1823 api_domain: "my_mainline_module",
1824 contents: ["%s"],
1825 }
1826 java_sdk_library {
1827 name: "sdklib",
1828 srcs: ["a.java"],
1829 sdk_version: "none",
1830 system_modules: "none",
1831 public: {
1832 enabled: true,
1833 },
1834 }
1835 java_sdk_library_import {
1836 name: "sdklib.v1", //prebuilt
1837 source_module_name: "sdklib",
1838 public: {
1839 jars: ["a.jar"],
1840 stub_srcs: ["a.java"],
1841 current_api: "current.txt",
1842 removed_api: "removed.txt",
1843 annotations: "annotations.zip",
1844 },
1845 }
1846 java_sdk_library_import {
1847 name: "sdklib.v2", //prebuilt
1848 source_module_name: "sdklib",
1849 public: {
1850 jars: ["a.jar"],
1851 stub_srcs: ["a.java"],
1852 current_api: "current.txt",
1853 removed_api: "removed.txt",
1854 annotations: "annotations.zip",
1855 },
1856 }
1857 // rdeps
1858 java_library {
1859 name: "mymodule",
1860 srcs: ["a.java"],
1861 libs: ["sdklib.stubs",],
1862 }
1863 `
1864 testCases := []struct {
1865 desc string
1866 selectedDependencyName string
1867 expectedStubPath string
1868 }{
1869 {
1870 desc: "Source library is selected using apex_contributions",
1871 selectedDependencyName: "sdklib",
1872 expectedStubPath: "out/soong/.intermediates/sdklib.stubs/android_common/turbine-combined/sdklib.stubs.jar",
1873 },
1874 {
1875 desc: "Prebuilt library v1 is selected using apex_contributions",
1876 selectedDependencyName: "prebuilt_sdklib.v1",
1877 expectedStubPath: "out/soong/.intermediates/prebuilt_sdklib.v1.stubs/android_common/combined/sdklib.stubs.jar",
1878 },
1879 {
1880 desc: "Prebuilt library v2 is selected using apex_contributions",
1881 selectedDependencyName: "prebuilt_sdklib.v2",
1882 expectedStubPath: "out/soong/.intermediates/prebuilt_sdklib.v2.stubs/android_common/combined/sdklib.stubs.jar",
1883 },
1884 }
1885
1886 fixture := android.GroupFixturePreparers(
1887 prepareForJavaTest,
1888 PrepareForTestWithJavaSdkLibraryFiles,
1889 FixtureWithLastReleaseApis("sdklib", "sdklib.v1", "sdklib.v2"),
1890 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
1891 variables.BuildFlags = map[string]string{
1892 "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "my_mainline_module_contributions",
1893 }
1894 }),
1895 )
1896
1897 for _, tc := range testCases {
1898 result := fixture.RunTestWithBp(t, fmt.Sprintf(bp, tc.selectedDependencyName))
1899
1900 // Make sure that rdeps get the correct source vs prebuilt based on mainline_module_contributions
1901 public := result.ModuleForTests("mymodule", "android_common")
1902 rule := public.Output("javac/mymodule.jar")
1903 inputs := rule.Implicits.Strings()
1904 android.AssertStringListContains(t, "Could not find the expected stub on classpath", inputs, tc.expectedStubPath)
1905 }
1906}