blob: caa83453582293fa3bb579ca36b0911d99a9fdbe [file] [log] [blame]
Colin Cross2207f872021-03-24 12:39: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 (
Pedro Loureirocc203502021-10-04 17:24:00 +000018 "fmt"
Colin Cross6aa5c402021-03-24 12:28:50 -070019 "reflect"
satayev783195c2021-06-23 21:49:57 +010020 "regexp"
Colin Cross2207f872021-03-24 12:39:08 -070021 "strings"
22 "testing"
23
24 "android/soong/android"
25)
26
27func TestDroidstubs(t *testing.T) {
28 ctx, _ := testJavaWithFS(t, `
29 droiddoc_exported_dir {
30 name: "droiddoc-templates-sdk",
31 path: ".",
32 }
33
34 droidstubs {
35 name: "bar-stubs",
36 srcs: ["bar-doc/a.java"],
37 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
38 api_levels_annotations_enabled: true,
39 }
40
41 droidstubs {
42 name: "bar-stubs-other",
43 srcs: ["bar-doc/a.java"],
44 high_mem: true,
45 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
46 api_levels_annotations_enabled: true,
47 api_levels_jar_filename: "android.other.jar",
48 }
Anton Hanssonc04a16e2022-05-09 09:30:26 +000049
50 droidstubs {
51 name: "stubs-applying-api-versions",
52 srcs: ["bar-doc/a.java"],
53 api_levels_module: "bar-stubs-other",
54 }
Colin Cross2207f872021-03-24 12:39:08 -070055 `,
56 map[string][]byte{
57 "bar-doc/a.java": nil,
58 })
59 testcases := []struct {
60 moduleName string
61 expectedJarFilename string
Anton Hanssonc04a16e2022-05-09 09:30:26 +000062 generate_xml bool
Colin Cross2207f872021-03-24 12:39:08 -070063 high_mem bool
64 }{
65 {
66 moduleName: "bar-stubs",
Anton Hanssonc04a16e2022-05-09 09:30:26 +000067 generate_xml: true,
Colin Cross2207f872021-03-24 12:39:08 -070068 expectedJarFilename: "android.jar",
69 high_mem: false,
70 },
71 {
72 moduleName: "bar-stubs-other",
Anton Hanssonc04a16e2022-05-09 09:30:26 +000073 generate_xml: true,
Colin Cross2207f872021-03-24 12:39:08 -070074 expectedJarFilename: "android.other.jar",
75 high_mem: true,
76 },
Anton Hanssonc04a16e2022-05-09 09:30:26 +000077 {
78 moduleName: "stubs-applying-api-versions",
79 generate_xml: false,
80 },
Colin Cross2207f872021-03-24 12:39:08 -070081 }
82 for _, c := range testcases {
83 m := ctx.ModuleForTests(c.moduleName, "android_common")
Colin Cross8095c292021-03-30 16:40:48 -070084 manifest := m.Output("metalava.sbox.textproto")
Colin Crossf61d03d2023-11-02 16:56:39 -070085 sboxProto := android.RuleBuilderSboxProtoForTests(t, ctx, manifest)
Anton Hanssonc04a16e2022-05-09 09:30:26 +000086 cmdline := String(sboxProto.Commands[0].Command)
87 android.AssertStringContainsEquals(t, "api-versions generation flag", cmdline, "--generate-api-levels", c.generate_xml)
88 if c.expectedJarFilename != "" {
89 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
90 if !strings.Contains(cmdline, expected) {
91 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, cmdline)
92 }
Colin Cross2207f872021-03-24 12:39:08 -070093 }
94
Colin Cross8095c292021-03-30 16:40:48 -070095 metalava := m.Rule("metalava")
96 rp := metalava.RuleParams
Colin Cross2207f872021-03-24 12:39:08 -070097 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
98 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
99 }
100 }
101}
102
Pedro Loureirocc203502021-10-04 17:24:00 +0000103// runs a test for droidstubs with a customizable sdkType argument and returns
104// the list of jar patterns that is passed as `--android-jar-pattern`
105func getAndroidJarPatternsForDroidstubs(t *testing.T, sdkType string) []string {
106 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
satayev783195c2021-06-23 21:49:57 +0100107 droiddoc_exported_dir {
108 name: "some-exported-dir",
109 path: "somedir",
110 }
111
112 droiddoc_exported_dir {
113 name: "some-other-exported-dir",
114 path: "someotherdir",
115 }
116
117 droidstubs {
118 name: "foo-stubs",
119 srcs: ["foo-doc/a.java"],
120 api_levels_annotations_dirs: [
121 "some-exported-dir",
122 "some-other-exported-dir",
123 ],
124 api_levels_annotations_enabled: true,
Cole Faust051fa912022-10-05 12:45:42 -0700125 api_levels_sdk_type: "%s",
satayev783195c2021-06-23 21:49:57 +0100126 }
Pedro Loureirocc203502021-10-04 17:24:00 +0000127 `, sdkType),
satayev783195c2021-06-23 21:49:57 +0100128 map[string][]byte{
129 "foo-doc/a.java": nil,
130 })
131
132 m := ctx.ModuleForTests("foo-stubs", "android_common")
133 manifest := m.Output("metalava.sbox.textproto")
Colin Crossf61d03d2023-11-02 16:56:39 -0700134 cmd := String(android.RuleBuilderSboxProtoForTests(t, ctx, manifest).Commands[0].Command)
satayev783195c2021-06-23 21:49:57 +0100135 r := regexp.MustCompile(`--android-jar-pattern [^ ]+/android.jar`)
Pedro Loureirocc203502021-10-04 17:24:00 +0000136 return r.FindAllString(cmd, -1)
137}
138
139func TestPublicDroidstubs(t *testing.T) {
140 patterns := getAndroidJarPatternsForDroidstubs(t, "public")
141
142 android.AssertArrayString(t, "order of patterns", []string{
143 "--android-jar-pattern somedir/%/public/android.jar",
144 "--android-jar-pattern someotherdir/%/public/android.jar",
145 }, patterns)
146}
147
148func TestSystemDroidstubs(t *testing.T) {
149 patterns := getAndroidJarPatternsForDroidstubs(t, "system")
150
satayev783195c2021-06-23 21:49:57 +0100151 android.AssertArrayString(t, "order of patterns", []string{
152 "--android-jar-pattern somedir/%/system/android.jar",
153 "--android-jar-pattern someotherdir/%/system/android.jar",
154 "--android-jar-pattern somedir/%/public/android.jar",
155 "--android-jar-pattern someotherdir/%/public/android.jar",
Pedro Loureirocc203502021-10-04 17:24:00 +0000156 }, patterns)
157}
158
159func TestModuleLibDroidstubs(t *testing.T) {
160 patterns := getAndroidJarPatternsForDroidstubs(t, "module-lib")
161
162 android.AssertArrayString(t, "order of patterns", []string{
163 "--android-jar-pattern somedir/%/module-lib/android.jar",
164 "--android-jar-pattern someotherdir/%/module-lib/android.jar",
165 "--android-jar-pattern somedir/%/system/android.jar",
166 "--android-jar-pattern someotherdir/%/system/android.jar",
167 "--android-jar-pattern somedir/%/public/android.jar",
168 "--android-jar-pattern someotherdir/%/public/android.jar",
169 }, patterns)
satayev783195c2021-06-23 21:49:57 +0100170}
171
Cole Faust051fa912022-10-05 12:45:42 -0700172func TestSystemServerDroidstubs(t *testing.T) {
173 patterns := getAndroidJarPatternsForDroidstubs(t, "system-server")
174
175 android.AssertArrayString(t, "order of patterns", []string{
176 "--android-jar-pattern somedir/%/system-server/android.jar",
177 "--android-jar-pattern someotherdir/%/system-server/android.jar",
178 "--android-jar-pattern somedir/%/module-lib/android.jar",
179 "--android-jar-pattern someotherdir/%/module-lib/android.jar",
180 "--android-jar-pattern somedir/%/system/android.jar",
181 "--android-jar-pattern someotherdir/%/system/android.jar",
182 "--android-jar-pattern somedir/%/public/android.jar",
183 "--android-jar-pattern someotherdir/%/public/android.jar",
184 }, patterns)
185}
186
Colin Cross6aa5c402021-03-24 12:28:50 -0700187func TestDroidstubsSandbox(t *testing.T) {
188 ctx, _ := testJavaWithFS(t, `
Colin Crossbc139922021-03-25 18:33:16 -0700189 genrule {
190 name: "foo",
191 out: ["foo.txt"],
192 cmd: "touch $(out)",
193 }
194
Colin Cross6aa5c402021-03-24 12:28:50 -0700195 droidstubs {
196 name: "bar-stubs",
197 srcs: ["bar-doc/a.java"],
Colin Crossbc139922021-03-25 18:33:16 -0700198
199 args: "--reference $(location :foo)",
200 arg_files: [":foo"],
Colin Cross6aa5c402021-03-24 12:28:50 -0700201 }
202 `,
203 map[string][]byte{
204 "bar-doc/a.java": nil,
205 })
206
207 m := ctx.ModuleForTests("bar-stubs", "android_common")
208 metalava := m.Rule("metalava")
209 if g, w := metalava.Inputs.Strings(), []string{"bar-doc/a.java"}; !reflect.DeepEqual(w, g) {
210 t.Errorf("Expected inputs %q, got %q", w, g)
211 }
Colin Crossbc139922021-03-25 18:33:16 -0700212
Colin Crossf61d03d2023-11-02 16:56:39 -0700213 manifest := android.RuleBuilderSboxProtoForTests(t, ctx, m.Output("metalava.sbox.textproto"))
Cole Fauste8561c62023-11-30 17:26:37 -0800214 if g, w := manifest.Commands[0].GetCommand(), "reference __SBOX_SANDBOX_DIR__/out/soong/.intermediates/foo/gen/foo.txt"; !strings.Contains(g, w) {
Colin Crossbc139922021-03-25 18:33:16 -0700215 t.Errorf("Expected command to contain %q, got %q", w, g)
216 }
Colin Cross6aa5c402021-03-24 12:28:50 -0700217}
218
Colin Cross2207f872021-03-24 12:39:08 -0700219func TestDroidstubsWithSystemModules(t *testing.T) {
220 ctx, _ := testJava(t, `
221 droidstubs {
222 name: "stubs-source-system-modules",
223 srcs: [
224 "bar-doc/a.java",
225 ],
226 sdk_version: "none",
227 system_modules: "source-system-modules",
228 }
229
230 java_library {
231 name: "source-jar",
232 srcs: [
233 "a.java",
234 ],
235 }
236
237 java_system_modules {
238 name: "source-system-modules",
239 libs: ["source-jar"],
240 }
241
242 droidstubs {
243 name: "stubs-prebuilt-system-modules",
244 srcs: [
245 "bar-doc/a.java",
246 ],
247 sdk_version: "none",
248 system_modules: "prebuilt-system-modules",
249 }
250
251 java_import {
252 name: "prebuilt-jar",
253 jars: ["a.jar"],
254 }
255
256 java_system_modules_import {
257 name: "prebuilt-system-modules",
258 libs: ["prebuilt-jar"],
259 }
260 `)
261
262 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
263
264 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
265}
266
Colin Cross2207f872021-03-24 12:39:08 -0700267func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
268 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
269 var systemJars []string
270 for _, i := range metalavaRule.Implicits {
271 systemJars = append(systemJars, i.Base())
272 }
273 if len(systemJars) < 1 || systemJars[0] != systemJar {
274 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
275 }
276}
Mårten Kongstad802ae0f2022-07-27 13:47:32 +0200277
278func TestDroidstubsWithSdkExtensions(t *testing.T) {
279 ctx, _ := testJavaWithFS(t, `
280 droiddoc_exported_dir {
281 name: "sdk-dir",
282 path: "sdk",
283 }
284
285 droidstubs {
286 name: "baz-stubs",
287 api_levels_annotations_dirs: ["sdk-dir"],
288 api_levels_annotations_enabled: true,
289 extensions_info_file: ":info-file",
290 }
291
292 filegroup {
293 name: "info-file",
294 srcs: ["sdk/extensions/info.txt"],
295 }
296 `,
297 map[string][]byte{
298 "sdk/extensions/1/public/some-mainline-module-stubs.jar": nil,
299 "sdk/extensions/info.txt": nil,
300 })
301 m := ctx.ModuleForTests("baz-stubs", "android_common")
302 manifest := m.Output("metalava.sbox.textproto")
Colin Crossf61d03d2023-11-02 16:56:39 -0700303 cmdline := String(android.RuleBuilderSboxProtoForTests(t, ctx, manifest).Commands[0].Command)
Mårten Kongstad802ae0f2022-07-27 13:47:32 +0200304 android.AssertStringDoesContain(t, "sdk-extensions-root present", cmdline, "--sdk-extensions-root sdk/extensions")
305 android.AssertStringDoesContain(t, "sdk-extensions-info present", cmdline, "--sdk-extensions-info sdk/extensions/info.txt")
306}
Spandan Das0b555e32022-11-28 18:48:51 +0000307
Jihoon Kang3198f3c2023-01-26 08:08:52 +0000308func TestDroidStubsApiContributionGeneration(t *testing.T) {
309 ctx, _ := testJavaWithFS(t, `
310 droidstubs {
311 name: "foo",
312 srcs: ["A/a.java"],
313 api_surface: "public",
314 check_api: {
315 current: {
316 api_file: "A/current.txt",
317 removed_api_file: "A/removed.txt",
318 }
319 }
320 }
321 `,
322 map[string][]byte{
323 "A/a.java": nil,
324 "A/current.txt": nil,
325 "A/removed.txt": nil,
326 },
327 )
328
329 ctx.ModuleForTests("foo.api.contribution", "")
330}
Jihoon Kang42b589c2023-02-03 22:56:13 +0000331
332func TestGeneratedApiContributionVisibilityTest(t *testing.T) {
333 library_bp := `
334 java_api_library {
335 name: "bar",
336 api_surface: "public",
337 api_contributions: ["foo.api.contribution"],
338 }
339 `
340 ctx, _ := testJavaWithFS(t, `
341 droidstubs {
342 name: "foo",
343 srcs: ["A/a.java"],
344 api_surface: "public",
345 check_api: {
346 current: {
347 api_file: "A/current.txt",
348 removed_api_file: "A/removed.txt",
349 }
350 },
Jihoon Kang063ec002023-06-28 01:16:23 +0000351 visibility: ["//a", "//b"],
Jihoon Kang42b589c2023-02-03 22:56:13 +0000352 }
353 `,
354 map[string][]byte{
355 "a/a.java": nil,
356 "a/current.txt": nil,
357 "a/removed.txt": nil,
358 "b/Android.bp": []byte(library_bp),
359 },
360 )
361
362 ctx.ModuleForTests("bar", "android_common")
363}
Jihoon Kangc8313892023-09-20 00:54:47 +0000364
Jihoon Kang6592e872023-12-19 01:13:16 +0000365func TestAconfigDeclarations(t *testing.T) {
366 result := android.GroupFixturePreparers(
367 prepareForJavaTest,
368 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
369 }),
370 android.FixtureMergeMockFs(map[string][]byte{
371 "a/A.java": nil,
372 "a/current.txt": nil,
373 "a/removed.txt": nil,
374 }),
375 ).RunTestWithBp(t, `
376 aconfig_declarations {
377 name: "bar",
378 package: "com.example.package",
379 srcs: [
380 "bar.aconfig",
381 ],
382 }
383 droidstubs {
384 name: "foo",
385 srcs: ["a/A.java"],
386 api_surface: "public",
387 check_api: {
388 current: {
389 api_file: "a/current.txt",
390 removed_api_file: "a/removed.txt",
391 }
392 },
393 aconfig_declarations: [
394 "bar",
395 ],
396 }
397 `)
398
399 // Check that droidstubs depend on aconfig_declarations
400 android.AssertBoolEquals(t, "foo expected to depend on bar",
401 CheckModuleHasDependency(t, result.TestContext, "foo", "android_common", "bar"), true)
402
403 m := result.ModuleForTests("foo", "android_common")
404 android.AssertStringDoesContain(t, "foo generates revert annotations file",
405 strings.Join(m.AllOutputs(), ""), "revert-annotations-exportable.txt")
Jihoon Kang3c89f042023-12-19 02:40:22 +0000406
407 // revert-annotations.txt passed to exportable stubs generation metalava command
408 manifest := m.Output("metalava_exportable.sbox.textproto")
409 cmdline := String(android.RuleBuilderSboxProtoForTests(t, result.TestContext, manifest).Commands[0].Command)
410 android.AssertStringDoesContain(t, "flagged api hide command not included", cmdline, "revert-annotations-exportable.txt")
411
412 android.AssertStringDoesContain(t, "foo generates exportable stubs jar",
413 strings.Join(m.AllOutputs(), ""), "exportable/foo-stubs.srcjar")
Jihoon Kang6592e872023-12-19 01:13:16 +0000414}
Jihoon Kang59198152024-02-06 22:43:18 +0000415
416func TestReleaseExportRuntimeApis(t *testing.T) {
417 result := android.GroupFixturePreparers(
418 prepareForJavaTest,
419 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
420 variables.BuildFlags = map[string]string{
421 "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true",
422 "RELEASE_EXPORT_RUNTIME_APIS": "true",
423 }
424 }),
425 android.FixtureMergeMockFs(map[string][]byte{
426 "a/A.java": nil,
427 "a/current.txt": nil,
428 "a/removed.txt": nil,
429 }),
430 ).RunTestWithBp(t, `
431 aconfig_declarations {
432 name: "bar",
433 package: "com.example.package",
434 srcs: [
435 "bar.aconfig",
436 ],
437 }
438 droidstubs {
439 name: "foo",
440 srcs: ["a/A.java"],
441 api_surface: "public",
442 check_api: {
443 current: {
444 api_file: "a/current.txt",
445 removed_api_file: "a/removed.txt",
446 }
447 },
448 aconfig_declarations: [
449 "bar",
450 ],
451 }
452 `)
453
454 m := result.ModuleForTests("foo", "android_common")
455
456 rule := m.Output("released-flagged-apis-exportable.txt")
457 exposeWritableApisFilter := "--filter='state:ENABLED+permission:READ_ONLY' --filter='permission:READ_WRITE'"
458 android.AssertStringEquals(t, "Filter argument expected to contain READ_WRITE permissions", exposeWritableApisFilter, rule.Args["filter_args"])
459}