blob: e8c4a0f5098d71868163d821264ae1e48c8e8c3f [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 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 (
Paul Duffin1a393322020-11-18 16:36:47 +000018 "fmt"
Colin Cross72bb3632017-07-13 16:23:21 -070019 "io/ioutil"
20 "os"
21 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070022 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010023 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Paul Duffind6ceb862021-03-04 23:02:31 +000028 "android/soong/genrule"
Jeongik Cha28df2572019-11-11 10:46:36 +090029 "github.com/google/blueprint/proptools"
30
Colin Crossa4f08812018-10-02 22:03:40 -070031 "android/soong/android"
32 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080033 "android/soong/dexpreopt"
Liz Kammerdd849a82020-06-12 16:38:45 -070034 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070035)
36
37var buildDir string
38
39func setUp() {
40 var err error
41 buildDir, err = ioutil.TempDir("", "soong_java_test")
42 if err != nil {
43 panic(err)
44 }
45}
46
47func tearDown() {
48 os.RemoveAll(buildDir)
49}
50
Paul Duffin95bdab42021-03-08 21:48:46 +000051// Factory to use to create fixtures for tests in this package.
52var javaFixtureFactory = android.NewFixtureFactory(
53 &buildDir,
54 genrule.PrepareForTestWithGenRuleBuildComponents,
55 // Get the CC build components but not default modules.
56 cc.PrepareForTestWithCcBuildComponents,
57 // Include all the default java modules.
58 PrepareForTestWithJavaDefaultModules,
59 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
60 ctx.RegisterModuleType("java_plugin", PluginFactory)
61 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
62
63 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
64 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
65 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
66 }),
67 javaMockFS().AddToFixture(),
68 dexpreopt.PrepareForTestWithDexpreopt,
69)
70
Colin Cross72bb3632017-07-13 16:23:21 -070071func TestMain(m *testing.M) {
72 run := func() int {
73 setUp()
74 defer tearDown()
75
76 return m.Run()
77 }
78
79 os.Exit(run())
80}
Colin Cross527012a2017-11-30 22:56:16 -080081
Paul Duffin95bdab42021-03-08 21:48:46 +000082// testConfig is a legacy way of creating a test Config for testing java modules.
83//
84// See testJava for an explanation as to how to stop using this deprecated method.
85//
86// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080087func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Paul Duffin9f045242021-01-21 15:05:11 +000088 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -070089}
90
Paul Duffin95bdab42021-03-08 21:48:46 +000091// testContext is a legacy way of creating a TestContext for testing java modules.
92//
93// See testJava for an explanation as to how to stop using this deprecated method.
94//
95// deprecated
Colin Crossae8600b2020-10-29 17:09:13 -070096func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070097
Colin Crossae8600b2020-10-29 17:09:13 -070098 ctx := android.NewTestArchContext(config)
Paul Duffinc059c8c2021-01-20 17:13:52 +000099 RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -0800100 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -0800101 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -0700102 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Colin Cross89536d42017-07-07 14:35:50 -0700103 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100104 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +0000105
Liz Kammerdd849a82020-06-12 16:38:45 -0700106 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700107 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Paul Duffineafc16b2021-02-24 01:43:18 +0000108 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
109 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
Colin Crossa4f08812018-10-02 22:03:40 -0700110
Paul Duffin021f4e52020-07-30 16:04:17 +0100111 android.RegisterPrebuiltMutators(ctx)
112
Paul Duffind6ceb862021-03-04 23:02:31 +0000113 genrule.RegisterGenruleBuildComponents(ctx)
114
Colin Crossa4f08812018-10-02 22:03:40 -0700115 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000116 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700117
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700118 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
119 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
120 })
121
Colin Cross527012a2017-11-30 22:56:16 -0800122 return ctx
123}
124
Paul Duffin95bdab42021-03-08 21:48:46 +0000125// run is a legacy way of running tests of java modules.
126//
127// See testJava for an explanation as to how to stop using this deprecated method.
128//
129// deprecated
Colin Cross527012a2017-11-30 22:56:16 -0800130func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800131 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800132
Colin Cross98be1bb2019-12-13 20:41:13 -0800133 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000134 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800135
Colin Crossae8600b2020-10-29 17:09:13 -0700136 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100137 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800138 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700139 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800140 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800141}
142
Paul Duffin95bdab42021-03-08 21:48:46 +0000143// testJavaError is a legacy way of running tests of java modules that expect errors.
144//
145// See testJava for an explanation as to how to stop using this deprecated method.
146//
147// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900148func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900149 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800150 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900151}
152
Paul Duffin95bdab42021-03-08 21:48:46 +0000153// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
154//
155// See testJava for an explanation as to how to stop using this deprecated method.
156//
157// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800158func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900159 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000160 // This must be done on the supplied config and not as part of the fixture because any changes to
161 // the fixture's config will be ignored when RunTestWithConfig replaces it.
Colin Cross98be1bb2019-12-13 20:41:13 -0800162 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000163 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Paul Duffin95bdab42021-03-08 21:48:46 +0000164 result := javaFixtureFactory.
165 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
166 RunTestWithConfig(t, config)
167 return result.TestContext, result.Config
Paul Duffinec0fe172021-02-25 15:34:13 +0000168}
169
Paul Duffin95bdab42021-03-08 21:48:46 +0000170// runWithErrors is a legacy way of running tests of java modules that expect errors.
171//
172// See testJava for an explanation as to how to stop using this deprecated method.
173//
174// deprecated
Paul Duffinec0fe172021-02-25 15:34:13 +0000175func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) {
Colin Crossae8600b2020-10-29 17:09:13 -0700176 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900177 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
178 if len(errs) > 0 {
179 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000180 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900181 }
182 _, errs = ctx.PrepareBuildActions(config)
183 if len(errs) > 0 {
184 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000185 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900186 }
187
188 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Paul Duffinec0fe172021-02-25 15:34:13 +0000189 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900190}
191
Paul Duffin95bdab42021-03-08 21:48:46 +0000192// testJavaWithFS runs tests using the javaFixtureFactory
193//
194// See testJava for an explanation as to how to stop using this deprecated method.
195//
196// deprecated
197func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -0700198 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000199 result := javaFixtureFactory.Extend(fs.AddToFixture()).RunTestWithBp(t, bp)
200 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -0700201}
202
Paul Duffin95bdab42021-03-08 21:48:46 +0000203// testJava runs tests using the javaFixtureFactory
204//
205// Do not add any new usages of this, instead use the javaFixtureFactory directly as it makes it
206// much easier to customize the test behavior.
207//
208// If it is necessary to customize the behavior of an existing test that uses this then please first
209// convert the test to using javaFixtureFactory first and then in a following change add the
210// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
211// that it did not change the test behavior unexpectedly.
212//
213// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700214func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800215 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000216 result := javaFixtureFactory.RunTestWithBp(t, bp)
217 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900218}
219
Paul Duffin95bdab42021-03-08 21:48:46 +0000220// testJavaWithConfig runs tests using the javaFixtureFactory
221//
222// See testJava for an explanation as to how to stop using this deprecated method.
223//
224// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800225func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900226 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000227 result := javaFixtureFactory.RunTestWithConfig(t, config)
228 return result.TestContext, result.Config
Colin Cross72bb3632017-07-13 16:23:21 -0700229}
230
Colin Cross2acdae82017-09-15 19:44:24 -0700231func moduleToPath(name string) string {
232 switch {
233 case name == `""`:
234 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700235 case strings.HasSuffix(name, ".jar"):
236 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700237 default:
238 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700239 }
240}
241
Paul Duffin95bdab42021-03-08 21:48:46 +0000242// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
243// is defined in PrepareForIntegrationTestWithJava
244func defaultModuleToPath(name string) string {
245 return filepath.Join(buildDir, ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
246}
247
Jeongik Chae403e9e2019-12-07 00:16:24 +0900248func TestJavaLinkType(t *testing.T) {
249 testJava(t, `
250 java_library {
251 name: "foo",
252 srcs: ["a.java"],
253 libs: ["bar"],
254 static_libs: ["baz"],
255 }
256
257 java_library {
258 name: "bar",
259 sdk_version: "current",
260 srcs: ["b.java"],
261 }
262
263 java_library {
264 name: "baz",
265 sdk_version: "system_current",
266 srcs: ["c.java"],
267 }
268 `)
269
Steven Moreland00298982020-11-17 21:44:36 +0000270 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900271 java_library {
272 name: "foo",
273 srcs: ["a.java"],
274 libs: ["bar"],
275 sdk_version: "current",
276 static_libs: ["baz"],
277 }
278
279 java_library {
280 name: "bar",
281 sdk_version: "current",
282 srcs: ["b.java"],
283 }
284
285 java_library {
286 name: "baz",
287 sdk_version: "system_current",
288 srcs: ["c.java"],
289 }
290 `)
291
292 testJava(t, `
293 java_library {
294 name: "foo",
295 srcs: ["a.java"],
296 libs: ["bar"],
297 sdk_version: "system_current",
298 static_libs: ["baz"],
299 }
300
301 java_library {
302 name: "bar",
303 sdk_version: "current",
304 srcs: ["b.java"],
305 }
306
307 java_library {
308 name: "baz",
309 sdk_version: "system_current",
310 srcs: ["c.java"],
311 }
312 `)
313
Steven Moreland00298982020-11-17 21:44:36 +0000314 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900315 java_library {
316 name: "foo",
317 srcs: ["a.java"],
318 libs: ["bar"],
319 sdk_version: "system_current",
320 static_libs: ["baz"],
321 }
322
323 java_library {
324 name: "bar",
325 sdk_version: "current",
326 srcs: ["b.java"],
327 }
328
329 java_library {
330 name: "baz",
331 srcs: ["c.java"],
332 }
333 `)
334}
335
Colin Cross72bb3632017-07-13 16:23:21 -0700336func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700337 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700338 java_library {
339 name: "foo",
340 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700341 libs: ["bar"],
342 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700343 }
344
345 java_library {
346 name: "bar",
347 srcs: ["b.java"],
348 }
349
350 java_library {
351 name: "baz",
352 srcs: ["c.java"],
353 }
Colin Crossd5934c82017-10-02 13:55:26 -0700354 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700355
Colin Cross4c428df2017-09-15 17:36:05 -0700356 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700357 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700358
359 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
360 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
361 }
362
Colin Cross1ee23172017-10-18 14:44:18 -0700363 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700364 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
365 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700366
Nan Zhanged19fc32017-10-19 13:06:22 -0700367 if !strings.Contains(javac.Args["classpath"], barTurbine) {
368 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700369 }
370
Nan Zhanged19fc32017-10-19 13:06:22 -0700371 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
372 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700373 }
374
375 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
376 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700377 }
378}
379
Artur Satayev9cf46692019-11-26 18:08:34 +0000380func TestExportedPlugins(t *testing.T) {
381 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800382 library string
383 processors string
384 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000385 }
386 var tests = []struct {
387 name string
388 extra string
389 results []Result
390 }{
391 {
392 name: "Exported plugin is not a direct plugin",
393 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
394 results: []Result{{library: "exports", processors: "-proc:none"}},
395 },
396 {
397 name: "Exports plugin to dependee",
398 extra: `
399 java_library{name: "exports", exported_plugins: ["plugin"]}
400 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
401 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
402 `,
403 results: []Result{
404 {library: "foo", processors: "-processor com.android.TestPlugin"},
405 {library: "bar", processors: "-processor com.android.TestPlugin"},
406 },
407 },
408 {
409 name: "Exports plugin to android_library",
410 extra: `
411 java_library{name: "exports", exported_plugins: ["plugin"]}
412 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
413 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
414 `,
415 results: []Result{
416 {library: "foo", processors: "-processor com.android.TestPlugin"},
417 {library: "bar", processors: "-processor com.android.TestPlugin"},
418 },
419 },
420 {
421 name: "Exports plugin is not propagated via transitive deps",
422 extra: `
423 java_library{name: "exports", exported_plugins: ["plugin"]}
424 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
425 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
426 `,
427 results: []Result{
428 {library: "foo", processors: "-processor com.android.TestPlugin"},
429 {library: "bar", processors: "-proc:none"},
430 },
431 },
432 {
433 name: "Exports plugin appends to plugins",
434 extra: `
435 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
436 java_library{name: "exports", exported_plugins: ["plugin"]}
437 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
438 `,
439 results: []Result{
440 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
441 },
442 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800443 {
444 name: "Exports plugin to with generates_api to dependee",
445 extra: `
446 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
447 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
448 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
449 `,
450 results: []Result{
451 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
452 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
453 },
454 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000455 }
456
457 for _, test := range tests {
458 t.Run(test.name, func(t *testing.T) {
459 ctx, _ := testJava(t, `
460 java_plugin {
461 name: "plugin",
462 processor_class: "com.android.TestPlugin",
463 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800464 java_plugin {
465 name: "plugin_generates_api",
466 generates_api: true,
467 processor_class: "com.android.TestPlugin",
468 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000469 `+test.extra)
470
471 for _, want := range test.results {
472 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
473 if javac.Args["processor"] != want.processors {
474 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
475 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800476 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
477 disableTurbine := turbine.BuildParams.Rule == nil
478 if disableTurbine != want.disableTurbine {
479 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
480 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000481 }
482 })
483 }
484}
485
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900486func TestSdkVersionByPartition(t *testing.T) {
487 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900488 java_library {
489 name: "foo",
490 srcs: ["a.java"],
491 vendor: true,
492 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900493 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900494
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900495 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900496 java_library {
497 name: "bar",
498 srcs: ["b.java"],
499 }
500 `)
501
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900502 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900503 bp := `
504 java_library {
505 name: "foo",
506 srcs: ["a.java"],
507 product_specific: true,
508 }
509 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800510
511 config := testConfig(nil, bp, nil)
512 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900513 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800514 testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900515 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800516 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900517 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900518 }
519}
520
Colin Crossd5934c82017-10-02 13:55:26 -0700521func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700522 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700523 java_library {
524 name: "foo",
525 srcs: ["a.java"],
526 target: {
527 android: {
528 srcs: ["b.java"],
529 },
530 },
531 }
532 `)
533
534 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
535 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
536 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
537 }
538}
539
Colin Cross6b4a32d2017-12-05 13:42:45 -0800540func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700541 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800542 java_library_host {
543 name: "foo",
544 srcs: ["a.java"],
545 }
546
547 java_binary_host {
548 name: "bar",
549 srcs: ["b.java"],
550 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700551 jni_libs: ["libjni"],
552 }
553
554 cc_library_shared {
555 name: "libjni",
556 host_supported: true,
557 device_supported: false,
558 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800559 }
560 `)
561
562 buildOS := android.BuildOs.String()
563
564 bar := ctx.ModuleForTests("bar", buildOS+"_common")
565 barJar := bar.Output("bar.jar").Output.String()
566 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
567 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
568
Colin Cross89226d92020-10-09 19:00:54 -0700569 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
570 libjniSO := libjni.Rule("Cp").Output.String()
571
Colin Cross6b4a32d2017-12-05 13:42:45 -0800572 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700573 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
574 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800575 }
Colin Cross89226d92020-10-09 19:00:54 -0700576
577 // Test that the install binary wrapper depends on the installed JNI libraries
578 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
579 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800580 }
Alex Humesky2070e322020-06-09 20:23:08 -0400581}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800582
Alex Humesky2070e322020-06-09 20:23:08 -0400583func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
584 bp := `
585 java_library {
586 name: "target_library",
587 srcs: ["a.java"],
588 }
589
590 java_binary_host {
591 name: "host_binary",
592 srcs: ["b.java"],
593 }
594 `
595 config := testConfig(nil, bp, nil)
596 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
597
598 ctx, _ := testJavaWithConfig(t, config)
599
Liz Kammer7941b302020-07-28 13:27:34 -0700600 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400601 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
602 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
603 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
604 t.Errorf("target library javac flags %v should contain "+
605 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
606 }
607
608 // check that -g is not overridden for host modules
609 buildOS := android.BuildOs.String()
610 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
611 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
612 if strings.Contains(hostJavaFlags, "-g:source,lines") {
613 t.Errorf("java_binary_host javac flags %v should not have "+
614 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
615 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800616}
617
Colin Cross72bb3632017-07-13 16:23:21 -0700618func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700619 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700620 java_library {
621 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000622 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000623 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700624 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700625 }
626
Colin Cross74d73e22017-08-02 11:05:49 -0700627 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700628 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700629 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700630 }
631
Colin Cross74d73e22017-08-02 11:05:49 -0700632 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700633 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700634 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700635 sdk_version: "current",
636 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700637 }
Colin Cross42be7612019-02-21 18:12:14 -0800638
639 dex_import {
640 name: "qux",
641 jars: ["b.jar"],
642 }
Colin Cross79c7c262019-04-17 11:11:46 -0700643
644 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000645 name: "sdklib",
646 public: {
647 jars: ["c.jar"],
648 },
649 }
650
Paul Duffin91547182019-11-12 19:39:36 +0000651 prebuilt_stubs_sources {
652 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000653 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000654 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000655
656 java_test_import {
657 name: "test",
658 jars: ["a.jar"],
659 test_suites: ["cts"],
660 test_config: "AndroidTest.xml",
661 }
Colin Cross72bb3632017-07-13 16:23:21 -0700662 `)
663
Paul Duffin9b478b02019-12-10 13:41:51 +0000664 fooModule := ctx.ModuleForTests("foo", "android_common")
665 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700666 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700667 barModule := ctx.ModuleForTests("bar", "android_common")
668 barJar := barModule.Rule("combineJar").Output
669 bazModule := ctx.ModuleForTests("baz", "android_common")
670 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700671 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700672
Paul Duffin9b478b02019-12-10 13:41:51 +0000673 fooLibrary := fooModule.Module().(*Library)
674 assertDeepEquals(t, "foo java sources incorrect",
675 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000676
Paul Duffin9b478b02019-12-10 13:41:51 +0000677 assertDeepEquals(t, "foo java source jars incorrect",
678 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
679 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000680
Colin Cross37f6d792018-07-12 12:28:41 -0700681 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
682 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700683 }
684
Liz Kammerd6c31d22020-08-05 15:40:41 -0700685 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
686 if barDexJar != nil {
687 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
688 }
689
Colin Cross79c7c262019-04-17 11:11:46 -0700690 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
691 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
692 }
693
Colin Cross37f6d792018-07-12 12:28:41 -0700694 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
695 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700696 }
Colin Cross42be7612019-02-21 18:12:14 -0800697
Liz Kammerd6c31d22020-08-05 15:40:41 -0700698 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
699 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
700 if bazDexJar != expectedDexJar {
701 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
702 }
703
Colin Cross42be7612019-02-21 18:12:14 -0800704 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700705}
706
Paul Duffin9b478b02019-12-10 13:41:51 +0000707func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
708 if !reflect.DeepEqual(expected, actual) {
709 t.Errorf("%s: expected %q, found %q", message, expected, actual)
710 }
711}
712
Paul Duffin1a393322020-11-18 16:36:47 +0000713func TestPrebuiltStubsSources(t *testing.T) {
714 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
715 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
716prebuilt_stubs_sources {
717 name: "stubs-source",
718 srcs: ["%s"],
719}`, sourcesPath), map[string][]byte{
720 "stubs/sources/pkg/A.java": nil,
721 "stubs/sources/pkg/B.java": nil,
722 })
723
724 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
725 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
726 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
727 }
728 }
729
730 t.Run("empty/missing directory", func(t *testing.T) {
731 test(t, "empty-directory", []string{})
732 })
733
734 t.Run("non-empty set of sources", func(t *testing.T) {
735 test(t, "stubs/sources", []string{
736 "stubs/sources/pkg/A.java",
737 "stubs/sources/pkg/B.java",
738 })
739 })
740}
741
Paul Duffin56d44902020-01-31 13:36:25 +0000742func TestJavaSdkLibraryImport(t *testing.T) {
743 ctx, _ := testJava(t, `
744 java_library {
745 name: "foo",
746 srcs: ["a.java"],
747 libs: ["sdklib"],
748 sdk_version: "current",
749 }
750
751 java_library {
752 name: "foo.system",
753 srcs: ["a.java"],
754 libs: ["sdklib"],
755 sdk_version: "system_current",
756 }
757
758 java_library {
759 name: "foo.test",
760 srcs: ["a.java"],
761 libs: ["sdklib"],
762 sdk_version: "test_current",
763 }
764
765 java_sdk_library_import {
766 name: "sdklib",
767 public: {
768 jars: ["a.jar"],
769 },
770 system: {
771 jars: ["b.jar"],
772 },
773 test: {
774 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100775 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000776 },
777 }
778 `)
779
780 for _, scope := range []string{"", ".system", ".test"} {
781 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
782 javac := fooModule.Rule("javac")
783
784 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
785 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
786 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
787 }
788 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100789
Paul Duffincee7e662020-07-09 17:32:57 +0100790 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100791 `prebuilt_sdklib.stubs`,
792 `prebuilt_sdklib.stubs.source.test`,
793 `prebuilt_sdklib.stubs.system`,
794 `prebuilt_sdklib.stubs.test`,
795 })
796}
797
798func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
799 ctx, _ := testJava(t, `
800 java_sdk_library {
801 name: "sdklib",
802 srcs: ["a.java"],
803 sdk_version: "none",
804 system_modules: "none",
805 public: {
806 enabled: true,
807 },
808 }
809
810 java_sdk_library_import {
811 name: "sdklib",
812 public: {
813 jars: ["a.jar"],
814 },
815 }
816 `)
817
Paul Duffincee7e662020-07-09 17:32:57 +0100818 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100819 `dex2oatd`,
820 `prebuilt_sdklib`,
821 `sdklib.impl`,
822 `sdklib.stubs`,
823 `sdklib.stubs.source`,
824 `sdklib.xml`,
825 })
826
Paul Duffincee7e662020-07-09 17:32:57 +0100827 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100828 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100829 `sdklib.impl`,
830 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
831 // dependency is added after prebuilts may have been renamed and so has to use
832 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100833 `sdklib.xml`,
834 })
835}
836
837func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
838 ctx, _ := testJava(t, `
839 java_sdk_library {
840 name: "sdklib",
841 srcs: ["a.java"],
842 sdk_version: "none",
843 system_modules: "none",
844 public: {
845 enabled: true,
846 },
847 }
848
849 java_sdk_library_import {
850 name: "sdklib",
851 prefer: true,
852 public: {
853 jars: ["a.jar"],
854 },
855 }
856 `)
857
Paul Duffincee7e662020-07-09 17:32:57 +0100858 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100859 `dex2oatd`,
860 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100861 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100862 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100863 `sdklib.stubs.source`,
864 `sdklib.xml`,
865 })
866
Paul Duffincee7e662020-07-09 17:32:57 +0100867 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100868 `prebuilt_sdklib.stubs`,
869 `sdklib.impl`,
870 `sdklib.xml`,
871 })
Paul Duffin56d44902020-01-31 13:36:25 +0000872}
873
JaeMan Parkff715562020-10-19 17:25:58 +0900874func TestJavaSdkLibraryEnforce(t *testing.T) {
875 partitionToBpOption := func(partition string) string {
876 switch partition {
877 case "system":
878 return ""
879 case "vendor":
880 return "soc_specific: true,"
881 case "product":
882 return "product_specific: true,"
883 default:
884 panic("Invalid partition group name: " + partition)
885 }
886 }
887
888 type testConfigInfo struct {
889 libraryType string
890 fromPartition string
891 toPartition string
892 enforceVendorInterface bool
893 enforceProductInterface bool
894 enforceJavaSdkLibraryCheck bool
895 allowList []string
896 }
897
898 createTestConfig := func(info testConfigInfo) android.Config {
899 bpFileTemplate := `
900 java_library {
901 name: "foo",
902 srcs: ["foo.java"],
903 libs: ["bar"],
904 sdk_version: "current",
905 %s
906 }
907
908 %s {
909 name: "bar",
910 srcs: ["bar.java"],
911 sdk_version: "current",
912 %s
913 }
914 `
915
916 bpFile := fmt.Sprintf(bpFileTemplate,
917 partitionToBpOption(info.fromPartition),
918 info.libraryType,
919 partitionToBpOption(info.toPartition))
920
921 config := testConfig(nil, bpFile, nil)
922 configVariables := config.TestProductVariables
923
924 configVariables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
925 if info.enforceVendorInterface {
926 configVariables.DeviceVndkVersion = proptools.StringPtr("current")
927 }
928 configVariables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
929 configVariables.InterPartitionJavaLibraryAllowList = info.allowList
930
931 return config
932 }
933
JaeMan Parkff715562020-10-19 17:25:58 +0900934 errorMessage := "is not allowed across the partitions"
935
JaeMan Park90e75352021-01-14 11:56:56 +0900936 testJavaWithConfig(t, createTestConfig(testConfigInfo{
937 libraryType: "java_library",
938 fromPartition: "product",
939 toPartition: "system",
940 enforceVendorInterface: true,
941 enforceProductInterface: true,
942 enforceJavaSdkLibraryCheck: false,
943 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900944
JaeMan Park90e75352021-01-14 11:56:56 +0900945 testJavaWithConfig(t, createTestConfig(testConfigInfo{
946 libraryType: "java_library",
947 fromPartition: "product",
948 toPartition: "system",
949 enforceVendorInterface: true,
950 enforceProductInterface: false,
951 enforceJavaSdkLibraryCheck: true,
952 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900953
JaeMan Park90e75352021-01-14 11:56:56 +0900954 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
955 libraryType: "java_library",
956 fromPartition: "product",
957 toPartition: "system",
958 enforceVendorInterface: true,
959 enforceProductInterface: true,
960 enforceJavaSdkLibraryCheck: true,
961 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900962
JaeMan Park90e75352021-01-14 11:56:56 +0900963 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
964 libraryType: "java_library",
965 fromPartition: "vendor",
966 toPartition: "system",
967 enforceVendorInterface: true,
968 enforceProductInterface: true,
969 enforceJavaSdkLibraryCheck: true,
970 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900971
972 testJavaWithConfig(t, createTestConfig(testConfigInfo{
973 libraryType: "java_library",
974 fromPartition: "vendor",
975 toPartition: "system",
976 enforceVendorInterface: true,
977 enforceProductInterface: true,
978 enforceJavaSdkLibraryCheck: true,
979 allowList: []string{"bar"},
980 }))
981
982 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
983 libraryType: "java_library",
984 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900985 toPartition: "product",
986 enforceVendorInterface: true,
987 enforceProductInterface: true,
988 enforceJavaSdkLibraryCheck: true,
989 }))
990
991 testJavaWithConfig(t, createTestConfig(testConfigInfo{
992 libraryType: "java_sdk_library",
993 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +0900994 toPartition: "system",
995 enforceVendorInterface: true,
996 enforceProductInterface: true,
997 enforceJavaSdkLibraryCheck: true,
JaeMan Park90e75352021-01-14 11:56:56 +0900998 }))
999
1000 testJavaWithConfig(t, createTestConfig(testConfigInfo{
1001 libraryType: "java_sdk_library",
1002 fromPartition: "vendor",
1003 toPartition: "system",
1004 enforceVendorInterface: true,
1005 enforceProductInterface: true,
1006 enforceJavaSdkLibraryCheck: true,
1007 }))
1008
1009 testJavaWithConfig(t, createTestConfig(testConfigInfo{
1010 libraryType: "java_sdk_library",
1011 fromPartition: "vendor",
1012 toPartition: "product",
1013 enforceVendorInterface: true,
1014 enforceProductInterface: true,
1015 enforceJavaSdkLibraryCheck: true,
JaeMan Parkff715562020-10-19 17:25:58 +09001016 }))
1017}
1018
Colin Cross89536d42017-07-07 14:35:50 -07001019func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001020 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -07001021 java_defaults {
1022 name: "defaults",
1023 srcs: ["a.java"],
1024 libs: ["bar"],
1025 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -07001026 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -07001027 }
1028
1029 java_library {
1030 name: "foo",
1031 defaults: ["defaults"],
1032 }
1033
1034 java_library {
1035 name: "bar",
1036 srcs: ["b.java"],
1037 }
1038
1039 java_library {
1040 name: "baz",
1041 srcs: ["c.java"],
1042 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001043
1044 android_test {
1045 name: "atestOptimize",
1046 defaults: ["defaults"],
1047 optimize: {enabled: true},
1048 }
1049
1050 android_test {
1051 name: "atestNoOptimize",
1052 defaults: ["defaults"],
1053 }
1054
1055 android_test {
1056 name: "atestDefault",
1057 srcs: ["a.java"],
1058 }
Colin Cross89536d42017-07-07 14:35:50 -07001059 `)
1060
Colin Cross4c428df2017-09-15 17:36:05 -07001061 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001062 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001063
1064 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1065 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1066 }
1067
Nan Zhanged19fc32017-10-19 13:06:22 -07001068 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1069 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1070 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001071 }
1072
Colin Cross1ee23172017-10-18 14:44:18 -07001073 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001074 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1075 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001076 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001077
1078 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1079 if atestOptimize.Output == nil {
1080 t.Errorf("atestOptimize should optimize APK")
1081 }
1082
1083 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1084 if atestNoOptimize.Output == nil {
1085 t.Errorf("atestNoOptimize should not optimize APK")
1086 }
1087
1088 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1089 if atestDefault.Output == nil {
1090 t.Errorf("atestDefault should optimize APK")
1091 }
Colin Cross89536d42017-07-07 14:35:50 -07001092}
1093
Colin Cross0f37af02017-09-27 17:42:05 -07001094func TestResources(t *testing.T) {
1095 var table = []struct {
1096 name string
1097 prop string
1098 extra string
1099 args string
1100 }{
1101 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001102 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001103 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001104 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001105 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001106 },
1107 {
1108 // Test that a module with java_resources includes the files
1109 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001110 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1111 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001112 },
1113 {
1114 // Test that a module with a filegroup in java_resources includes the files with the
1115 // path prefix
1116 name: "resource filegroup",
1117 prop: `java_resources: [":foo-res"]`,
1118 extra: `
1119 filegroup {
1120 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001121 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001122 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001123 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001124 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001125 },
1126 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001127 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1128 name: "wildcard dirs",
1129 prop: `java_resource_dirs: ["java-res/*"]`,
1130 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1131 },
1132 {
1133 // Test that a module exclude_java_resource_dirs excludes the files
1134 name: "wildcard dirs",
1135 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1136 args: "-C java-res/a -f java-res/a/a",
1137 },
Colin Crosscedd4762018-09-13 11:26:19 -07001138 {
1139 // Test wildcards in java_resources
1140 name: "wildcard files",
1141 prop: `java_resources: ["java-res/**/*"]`,
1142 args: "-C . -f java-res/a/a -f java-res/b/b",
1143 },
1144 {
1145 // Test exclude_java_resources with java_resources
1146 name: "wildcard files with exclude",
1147 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1148 args: "-C . -f java-res/a/a",
1149 },
1150 {
1151 // Test exclude_java_resources with java_resource_dirs
1152 name: "resource dirs with exclude files",
1153 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1154 args: "-C java-res -f java-res/a/a",
1155 },
1156 {
1157 // Test exclude_java_resource_dirs with java_resource_dirs
1158 name: "resource dirs with exclude files",
1159 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1160 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1161 },
Colin Cross0f37af02017-09-27 17:42:05 -07001162 }
1163
1164 for _, test := range table {
1165 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001166 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001167 java_library {
1168 name: "foo",
1169 srcs: [
1170 "a.java",
1171 "b.java",
1172 "c.java",
1173 ],
1174 `+test.prop+`,
1175 }
Colin Cross238c1f32020-06-07 16:58:18 -07001176 `+test.extra,
1177 map[string][]byte{
1178 "java-res/a/a": nil,
1179 "java-res/b/b": nil,
1180 "java-res2/a": nil,
1181 },
1182 )
Colin Cross0f37af02017-09-27 17:42:05 -07001183
Colin Cross331a1212018-08-15 20:40:52 -07001184 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001185 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001186
1187 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1188 t.Errorf("foo combined jars %v does not contain %q",
1189 foo.Inputs.Strings(), fooRes.Output.String())
1190 }
1191
Colin Crossaf9c55b2017-10-03 14:50:08 -07001192 if fooRes.Args["jarArgs"] != test.args {
1193 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001194 fooRes.Args["jarArgs"], test.args)
1195 }
1196 })
1197 }
1198}
1199
Colin Cross0c4ce212019-05-03 15:28:19 -07001200func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001201 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001202 java_library {
1203 name: "foo",
1204 srcs: [
1205 "a.java",
1206 "b.java",
1207 "c.java",
1208 ],
1209 include_srcs: true,
1210 }
1211
1212 java_library {
1213 name: "bar",
1214 srcs: [
1215 "a.java",
1216 "b.java",
1217 "c.java",
1218 ],
1219 java_resource_dirs: ["java-res"],
1220 include_srcs: true,
1221 }
Colin Cross238c1f32020-06-07 16:58:18 -07001222 `, map[string][]byte{
1223 "java-res/a/a": nil,
1224 "java-res/b/b": nil,
1225 "java-res2/a": nil,
1226 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001227
1228 // Test a library with include_srcs: true
1229 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1230 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1231
1232 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1233 t.Errorf("foo combined jars %v does not contain %q", w, g)
1234 }
1235
1236 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1237 t.Errorf("foo source jar args %q is not %q", w, g)
1238 }
1239
1240 // Test a library with include_srcs: true and resources
1241 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1242 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1243 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1244 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1245
1246 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1247 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1248 }
1249
1250 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1251 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1252 }
1253
1254 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1255 t.Errorf("bar combined jars %v does not contain %q", w, g)
1256 }
1257
1258 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1259 t.Errorf("bar source jar args %q is not %q", w, g)
1260 }
1261
1262 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1263 t.Errorf("bar resource jar args %q is not %q", w, g)
1264 }
1265}
1266
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001267func TestJavaLint(t *testing.T) {
1268 ctx, _ := testJavaWithFS(t, `
1269 java_library {
1270 name: "foo",
1271 srcs: [
1272 "a.java",
1273 "b.java",
1274 "c.java",
1275 ],
1276 min_sdk_version: "29",
1277 sdk_version: "system_current",
1278 }
1279 `, map[string][]byte{
1280 "lint-baseline.xml": nil,
1281 })
1282
1283 foo := ctx.ModuleForTests("foo", "android_common")
1284 rule := foo.Rule("lint")
1285
1286 if !strings.Contains(rule.RuleParams.Command, "--baseline lint-baseline.xml") {
1287 t.Error("did not pass --baseline flag")
1288 }
1289}
1290
1291func TestJavaLintWithoutBaseline(t *testing.T) {
1292 ctx, _ := testJavaWithFS(t, `
1293 java_library {
1294 name: "foo",
1295 srcs: [
1296 "a.java",
1297 "b.java",
1298 "c.java",
1299 ],
1300 min_sdk_version: "29",
1301 sdk_version: "system_current",
1302 }
1303 `, map[string][]byte{})
1304
1305 foo := ctx.ModuleForTests("foo", "android_common")
1306 rule := foo.Rule("lint")
1307
1308 if strings.Contains(rule.RuleParams.Command, "--baseline") {
1309 t.Error("passed --baseline flag for non existent file")
1310 }
1311}
1312
1313func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
1314 config := testConfig(
1315 nil,
1316 `
1317 java_library {
1318 name: "foo",
1319 srcs: [
1320 ],
1321 min_sdk_version: "29",
1322 sdk_version: "system_current",
1323 lint: {
1324 baseline_filename: "mybaseline.xml",
1325 },
1326 }
1327 `, map[string][]byte{
1328 "build/soong/java/lint_defaults.txt": nil,
1329 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1330 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1331 "framework/aidl": nil,
1332 "a.java": nil,
1333 "AndroidManifest.xml": nil,
1334 "build/make/target/product/security": nil,
1335 })
1336 config.TestAllowNonExistentPaths = false
1337 testJavaErrorWithConfig(t,
1338 "source path \"mybaseline.xml\" does not exist",
1339 config,
1340 )
1341}
1342
1343func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1344 ctx, _ := testJavaWithFS(t, `
1345 java_library {
1346 name: "foo",
1347 srcs: [
1348 "a.java",
1349 "b.java",
1350 "c.java",
1351 ],
1352 min_sdk_version: "29",
1353 sdk_version: "system_current",
1354 lint: {
1355 error_checks: ["SomeCheck"],
1356 baseline_filename: "mybaseline.xml",
1357 },
1358 }
1359 `, map[string][]byte{
1360 "mybaseline.xml": nil,
1361 })
1362
1363 foo := ctx.ModuleForTests("foo", "android_common")
1364 rule := foo.Rule("lint")
1365
1366 if !strings.Contains(rule.RuleParams.Command, "--baseline mybaseline.xml") {
1367 t.Error("did not use the correct file for baseline")
1368 }
1369}
1370
Colin Cross54190b32017-10-09 15:34:10 -07001371func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001372 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001373 java_library {
1374 name: "foo",
1375 srcs: [
1376 "a*.java",
1377 ":gen",
1378 "b*.java",
1379 ],
1380 }
1381
1382 genrule {
1383 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001384 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001385 out: ["gen.java"],
1386 }
Colin Cross238c1f32020-06-07 16:58:18 -07001387 `, map[string][]byte{
1388 "a.java": nil,
1389 "b.java": nil,
1390 })
Colin Cross54190b32017-10-09 15:34:10 -07001391
1392 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1393 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1394
Colin Cross15e86d92017-10-20 15:07:08 -07001395 if filepath.Base(genrule.Output.String()) != "gen.java" {
1396 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001397 }
1398
1399 if len(javac.Inputs) != 3 ||
1400 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001401 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001402 javac.Inputs[2].String() != "b.java" {
1403 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1404 }
1405}
1406
Nan Zhang61eaedb2017-11-02 13:28:15 -07001407func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001408 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001409 java_library {
1410 name: "foo",
1411 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001412 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001413 }
1414
1415 java_library {
1416 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001417 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001418 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001419 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001420 }
1421
1422 java_library {
1423 name: "baz",
1424 srcs: ["c.java"],
1425 libs: ["bar"],
1426 sdk_version: "14",
1427 }
1428 `)
1429
1430 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1431 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1432 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1433 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1434 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1435
1436 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1437 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1438 }
1439
1440 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1441 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1442 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1443 }
1444 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1445 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1446 }
1447 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1448 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1449 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001450 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001451 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001452 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001453 }
1454}
1455
1456func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001457 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001458 java_library {
1459 name: "bar",
1460 srcs: ["a.java","b.java","c.java"],
1461 javac_shard_size: 1
1462 }
1463 `)
1464
1465 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1466 for i := 0; i < 3; i++ {
1467 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1468 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1469 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1470 }
1471 }
1472}
1473
Nan Zhang581fd212018-01-10 16:06:12 -08001474func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001475 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001476 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001477 name: "droiddoc-templates-sdk",
1478 path: ".",
1479 }
Jiyong Park29074592019-07-07 16:27:47 +09001480 filegroup {
1481 name: "bar-doc-aidl-srcs",
1482 srcs: ["bar-doc/IBar.aidl"],
1483 path: "bar-doc",
1484 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001485 droidstubs {
1486 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001487 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001488 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001489 ],
Steve Kim3666c702020-09-01 17:58:01 +00001490 exclude_srcs: [
1491 "bar-doc/b.java"
1492 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001493 api_levels_annotations_dirs: [
1494 "droiddoc-templates-sdk",
1495 ],
1496 api_levels_annotations_enabled: true,
1497 }
1498 droiddoc {
1499 name: "bar-doc",
1500 srcs: [
1501 ":bar-stubs",
1502 "bar-doc/IFoo.aidl",
1503 ":bar-doc-aidl-srcs",
1504 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001505 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001506 hdf: [
1507 "android.whichdoc offline",
1508 ],
1509 knowntags: [
1510 "bar-doc/known_oj_tags.txt",
1511 ],
1512 proofread_file: "libcore-proofread.txt",
1513 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001514 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001515 }
Colin Cross238c1f32020-06-07 16:58:18 -07001516 `,
1517 map[string][]byte{
1518 "bar-doc/a.java": nil,
1519 "bar-doc/b.java": nil,
1520 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001521 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1522 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1523 if err != nil {
1524 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1525 }
1526 if len(barStubsOutputs) != 1 {
1527 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001528 }
Nan Zhang581fd212018-01-10 16:06:12 -08001529
Liz Kammere1ab2502020-09-10 15:29:25 +00001530 barStubsOutput := barStubsOutputs[0]
1531 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1532 javaDoc := barDoc.Rule("javadoc")
1533 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1534 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001535 }
1536
Liz Kammere1ab2502020-09-10 15:29:25 +00001537 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1538 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1539 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1540 }
1541
1542 aidl := barDoc.Rule("aidl")
1543 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001544 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1545 }
1546
1547 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1548 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001549 }
Nan Zhang581fd212018-01-10 16:06:12 -08001550}
1551
Liz Kammer585cac22020-07-06 09:12:57 -07001552func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1553 testJavaError(t, "flags is set. Cannot set args", `
1554 droiddoc_exported_dir {
1555 name: "droiddoc-templates-sdk",
1556 path: ".",
1557 }
1558 filegroup {
1559 name: "bar-doc-aidl-srcs",
1560 srcs: ["bar-doc/IBar.aidl"],
1561 path: "bar-doc",
1562 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001563 droidstubs {
1564 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001565 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001566 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001567 ],
Steve Kim3666c702020-09-01 17:58:01 +00001568 exclude_srcs: [
1569 "bar-doc/b.java"
1570 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001571 api_levels_annotations_dirs: [
1572 "droiddoc-templates-sdk",
1573 ],
1574 api_levels_annotations_enabled: true,
1575 }
1576 droiddoc {
1577 name: "bar-doc",
1578 srcs: [
1579 ":bar-stubs",
1580 "bar-doc/IFoo.aidl",
1581 ":bar-doc-aidl-srcs",
1582 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001583 custom_template: "droiddoc-templates-sdk",
1584 hdf: [
1585 "android.whichdoc offline",
1586 ],
1587 knowntags: [
1588 "bar-doc/known_oj_tags.txt",
1589 ],
1590 proofread_file: "libcore-proofread.txt",
1591 todo_file: "libcore-docs-todo.html",
1592 flags: ["-offlinemode -title \"libcore\""],
1593 args: "-offlinemode -title \"libcore\"",
1594 }
1595 `)
1596}
1597
Liz Kammer3d894b72020-08-04 09:55:13 -07001598func TestDroidstubs(t *testing.T) {
1599 ctx, _ := testJavaWithFS(t, `
1600 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001601 name: "droiddoc-templates-sdk",
1602 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001603 }
1604
1605 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001606 name: "bar-stubs",
1607 srcs: ["bar-doc/a.java"],
1608 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1609 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001610 }
1611
1612 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001613 name: "bar-stubs-other",
1614 srcs: ["bar-doc/a.java"],
1615 high_mem: true,
1616 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1617 api_levels_annotations_enabled: true,
1618 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001619 }
1620 `,
1621 map[string][]byte{
1622 "bar-doc/a.java": nil,
1623 })
1624 testcases := []struct {
1625 moduleName string
1626 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001627 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001628 }{
1629 {
1630 moduleName: "bar-stubs",
1631 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001632 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001633 },
1634 {
1635 moduleName: "bar-stubs-other",
1636 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001637 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001638 },
1639 }
1640 for _, c := range testcases {
1641 m := ctx.ModuleForTests(c.moduleName, "android_common")
1642 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001643 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001644 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001645 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001646 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1647 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001648
1649 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1650 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1651 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001652 }
1653}
1654
Paul Duffin83a2d962019-11-19 19:44:10 +00001655func TestDroidstubsWithSystemModules(t *testing.T) {
1656 ctx, _ := testJava(t, `
1657 droidstubs {
1658 name: "stubs-source-system-modules",
1659 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001660 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001661 ],
1662 sdk_version: "none",
1663 system_modules: "source-system-modules",
1664 }
1665
1666 java_library {
1667 name: "source-jar",
1668 srcs: [
1669 "a.java",
1670 ],
1671 }
1672
1673 java_system_modules {
1674 name: "source-system-modules",
1675 libs: ["source-jar"],
1676 }
1677
1678 droidstubs {
1679 name: "stubs-prebuilt-system-modules",
1680 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001681 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001682 ],
1683 sdk_version: "none",
1684 system_modules: "prebuilt-system-modules",
1685 }
1686
1687 java_import {
1688 name: "prebuilt-jar",
1689 jars: ["a.jar"],
1690 }
1691
1692 java_system_modules_import {
1693 name: "prebuilt-system-modules",
1694 libs: ["prebuilt-jar"],
1695 }
1696 `)
1697
1698 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1699
1700 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1701}
1702
1703func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1704 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1705 var systemJars []string
1706 for _, i := range metalavaRule.Implicits {
1707 systemJars = append(systemJars, i.Base())
1708 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001709 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001710 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1711 }
1712}
1713
Colin Cross54250902017-12-05 09:28:08 -08001714func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001715 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001716 java_library {
1717 name: "foo",
1718 srcs: ["a.java"],
1719 }
1720
1721 java_genrule {
1722 name: "jargen",
1723 tool_files: ["b.java"],
1724 cmd: "$(location b.java) $(in) $(out)",
1725 out: ["jargen.jar"],
1726 srcs: [":foo"],
1727 }
1728
1729 java_library {
1730 name: "bar",
1731 static_libs: ["jargen"],
1732 srcs: ["c.java"],
1733 }
1734
1735 java_library {
1736 name: "baz",
1737 libs: ["jargen"],
1738 srcs: ["c.java"],
1739 }
1740 `)
1741
1742 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1743 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1744 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1745 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1746 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1747
Colin Cross3d680512020-11-13 16:23:53 -08001748 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1749 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001750 }
1751
1752 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1753 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1754 }
1755
1756 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1757 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1758 }
1759
1760 if len(barCombined.Inputs) != 2 ||
1761 barCombined.Inputs[0].String() != bar.Output.String() ||
1762 barCombined.Inputs[1].String() != jargen.Output.String() {
1763 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1764 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1765 }
1766}
1767
Nan Zhang27e284d2018-02-09 21:03:53 +00001768func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001769 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001770 java_library {
1771 name: "foo",
1772 srcs: ["a.java", ":foo-srcs"],
1773 exclude_srcs: ["a.java", ":foo-excludes"],
1774 }
1775
1776 filegroup {
1777 name: "foo-srcs",
1778 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1779 }
1780
1781 filegroup {
1782 name: "foo-excludes",
1783 srcs: ["java-fg/a.java", "java-fg/b.java"],
1784 }
1785 `)
1786
1787 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1788
1789 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1790 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1791 }
1792}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001793
Paul Duffin52d398a2019-06-11 12:31:14 +01001794func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001795 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001796 "libcore/Android.bp": []byte(`
1797 java_library {
1798 name: "core",
1799 sdk_version: "none",
1800 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001801 }
1802
1803 filegroup {
1804 name: "core-jar",
1805 srcs: [":core{.jar}"],
1806 }
1807`),
1808 })
Colin Crossae8600b2020-10-29 17:09:13 -07001809 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001810 run(t, ctx, config)
1811}
1812
1813func TestJavaImport(t *testing.T) {
1814 config := testConfig(nil, "", map[string][]byte{
1815 "libcore/Android.bp": []byte(`
1816 java_import {
1817 name: "core",
1818 sdk_version: "none",
1819 }
1820
1821 filegroup {
1822 name: "core-jar",
1823 srcs: [":core{.jar}"],
1824 }
1825`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001826 })
Colin Crossae8600b2020-10-29 17:09:13 -07001827 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001828 run(t, ctx, config)
1829}
1830
Jiyong Parkc678ad32018-04-10 13:07:10 +09001831func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001832 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001833 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001834 name: "droiddoc-templates-sdk",
1835 path: ".",
1836 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001837 java_sdk_library {
1838 name: "foo",
1839 srcs: ["a.java", "b.java"],
1840 api_packages: ["foo"],
1841 }
1842 java_sdk_library {
1843 name: "bar",
1844 srcs: ["a.java", "b.java"],
1845 api_packages: ["bar"],
1846 }
1847 java_library {
1848 name: "baz",
1849 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001850 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001851 sdk_version: "system_current",
1852 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001853 java_sdk_library {
1854 name: "barney",
1855 srcs: ["c.java"],
1856 api_only: true,
1857 }
1858 java_sdk_library {
1859 name: "betty",
1860 srcs: ["c.java"],
1861 shared_library: false,
1862 }
Paul Duffin859fe962020-05-15 10:20:31 +01001863 java_sdk_library_import {
1864 name: "quuz",
1865 public: {
1866 jars: ["c.jar"],
1867 },
1868 }
1869 java_sdk_library_import {
1870 name: "fred",
1871 public: {
1872 jars: ["b.jar"],
1873 },
1874 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001875 java_sdk_library_import {
1876 name: "wilma",
1877 public: {
1878 jars: ["b.jar"],
1879 },
1880 shared_library: false,
1881 }
Jiyong Park1be96912018-05-28 18:02:19 +09001882 java_library {
1883 name: "qux",
1884 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001885 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001886 sdk_version: "system_current",
1887 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001888 java_library {
1889 name: "baz-test",
1890 srcs: ["c.java"],
1891 libs: ["foo"],
1892 sdk_version: "test_current",
1893 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001894 java_library {
1895 name: "baz-29",
1896 srcs: ["c.java"],
1897 libs: ["foo"],
1898 sdk_version: "system_29",
1899 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001900 java_library {
1901 name: "baz-module-30",
1902 srcs: ["c.java"],
1903 libs: ["foo"],
1904 sdk_version: "module_30",
1905 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001906 `)
1907
1908 // check the existence of the internal modules
1909 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001910 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1911 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1912 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1913 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1914 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1915 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001916 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001917 ctx.ModuleForTests("foo.api.public.28", "")
1918 ctx.ModuleForTests("foo.api.system.28", "")
1919 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001920
1921 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1922 // tests if baz is actually linked to the stubs lib
1923 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1924 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1925 "foo.stubs.system.jar")
1926 }
1927 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001928 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001929 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001930 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001931 }
1932 // test if baz is not linked to the system variant of foo
1933 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1934 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1935 "foo.stubs.jar")
1936 }
Jiyong Park1be96912018-05-28 18:02:19 +09001937
Paul Duffin726d23c2020-01-22 16:30:37 +00001938 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1939 // tests if baz-test is actually linked to the test stubs lib
1940 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1941 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1942 "foo.stubs.test.jar")
1943 }
1944
Paul Duffina2db18f2020-01-22 17:11:15 +00001945 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1946 // tests if baz-29 is actually linked to the system 29 stubs lib
1947 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1948 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1949 "prebuilts/sdk/29/system/foo.jar")
1950 }
1951
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001952 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1953 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1954 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1955 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1956 "prebuilts/sdk/30/module-lib/foo.jar")
1957 }
1958
Jiyong Park1be96912018-05-28 18:02:19 +09001959 // test if baz has exported SDK lib names foo and bar to qux
1960 qux := ctx.ModuleForTests("qux", "android_common")
1961 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001962 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001963 if w := []string{"foo", "bar", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
Paul Duffin859fe962020-05-15 10:20:31 +01001964 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001965 }
1966 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001967}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001968
Anton Hansson7f66efa2020-10-08 14:47:23 +01001969func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1970 ctx, _ := testJava(t, `
1971 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001972 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001973 srcs: ["a.java"],
1974 impl_only_libs: ["foo"],
1975 stub_only_libs: ["bar"],
1976 }
1977 java_library {
1978 name: "foo",
1979 srcs: ["a.java"],
1980 sdk_version: "current",
1981 }
1982 java_library {
1983 name: "bar",
1984 srcs: ["a.java"],
1985 sdk_version: "current",
1986 }
1987 `)
1988
Anton Hanssondff2c782020-12-21 17:10:01 +00001989 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Anton Hansson7f66efa2020-10-08 14:47:23 +01001990 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1991 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1992 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1993 }
1994 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001995 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Anton Hansson7f66efa2020-10-08 14:47:23 +01001996 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1997 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1998 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1999 }
2000}
2001
Paul Duffindaaa3322020-05-26 18:13:57 +01002002func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
2003 ctx, _ := testJava(t, `
2004 java_sdk_library {
2005 name: "foo",
2006 srcs: ["a.java"],
2007 api_only: true,
2008 public: {
2009 enabled: true,
2010 },
2011 }
2012
2013 java_library {
2014 name: "bar",
2015 srcs: ["b.java"],
2016 libs: ["foo"],
2017 }
2018 `)
2019
2020 // The bar library should depend on the stubs jar.
2021 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
2022 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2023 t.Errorf("expected %q, found %#q", expected, actual)
2024 }
2025}
2026
Paul Duffin46dc45a2020-05-14 15:39:10 +01002027func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
2028 testJava(t, `
2029 java_sdk_library {
2030 name: "foo",
2031 srcs: ["a.java"],
2032 api_packages: ["foo"],
2033 public: {
2034 enabled: true,
2035 },
2036 }
2037
2038 java_library {
2039 name: "bar",
2040 srcs: ["b.java", ":foo{.public.stubs.source}"],
2041 }
2042 `)
2043}
2044
2045func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
2046 testJavaError(t, `"foo" does not provide api scope system`, `
2047 java_sdk_library {
2048 name: "foo",
2049 srcs: ["a.java"],
2050 api_packages: ["foo"],
2051 public: {
2052 enabled: true,
2053 },
2054 }
2055
2056 java_library {
2057 name: "bar",
2058 srcs: ["b.java", ":foo{.system.stubs.source}"],
2059 }
2060 `)
2061}
2062
Paul Duffinca8d9a52020-06-26 22:20:25 +01002063func TestJavaSdkLibrary_Deps(t *testing.T) {
2064 ctx, _ := testJava(t, `
2065 java_sdk_library {
2066 name: "sdklib",
2067 srcs: ["a.java"],
2068 sdk_version: "none",
2069 system_modules: "none",
2070 public: {
2071 enabled: true,
2072 },
2073 }
2074 `)
2075
Paul Duffincee7e662020-07-09 17:32:57 +01002076 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01002077 `dex2oatd`,
2078 `sdklib.impl`,
2079 `sdklib.stubs`,
2080 `sdklib.stubs.source`,
2081 `sdklib.xml`,
2082 })
2083}
2084
Paul Duffin46dc45a2020-05-14 15:39:10 +01002085func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
2086 testJava(t, `
2087 java_sdk_library_import {
2088 name: "foo",
2089 public: {
2090 jars: ["a.jar"],
2091 stub_srcs: ["a.java"],
2092 current_api: "api/current.txt",
2093 removed_api: "api/removed.txt",
2094 },
2095 }
2096
2097 java_library {
2098 name: "bar",
2099 srcs: [":foo{.public.stubs.source}"],
2100 java_resources: [
2101 ":foo{.public.api.txt}",
2102 ":foo{.public.removed-api.txt}",
2103 ],
2104 }
2105 `)
2106}
2107
2108func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2109 bp := `
2110 java_sdk_library_import {
2111 name: "foo",
2112 public: {
2113 jars: ["a.jar"],
2114 },
2115 }
2116 `
2117
2118 t.Run("stubs.source", func(t *testing.T) {
2119 testJavaError(t, `stubs.source not available for api scope public`, bp+`
2120 java_library {
2121 name: "bar",
2122 srcs: [":foo{.public.stubs.source}"],
2123 java_resources: [
2124 ":foo{.public.api.txt}",
2125 ":foo{.public.removed-api.txt}",
2126 ],
2127 }
2128 `)
2129 })
2130
2131 t.Run("api.txt", func(t *testing.T) {
2132 testJavaError(t, `api.txt not available for api scope public`, bp+`
2133 java_library {
2134 name: "bar",
2135 srcs: ["a.java"],
2136 java_resources: [
2137 ":foo{.public.api.txt}",
2138 ],
2139 }
2140 `)
2141 })
2142
2143 t.Run("removed-api.txt", func(t *testing.T) {
2144 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
2145 java_library {
2146 name: "bar",
2147 srcs: ["a.java"],
2148 java_resources: [
2149 ":foo{.public.removed-api.txt}",
2150 ],
2151 }
2152 `)
2153 })
2154}
2155
Paul Duffin3375e352020-04-28 10:44:03 +01002156func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
2157 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
2158 java_sdk_library {
2159 name: "foo",
2160 srcs: ["a.java", "b.java"],
2161 api_packages: ["foo"],
2162 // Explicitly disable public to test the check that ensures the set of enabled
2163 // scopes is consistent.
2164 public: {
2165 enabled: false,
2166 },
2167 system: {
2168 enabled: true,
2169 },
2170 }
2171 `)
2172}
2173
Paul Duffin87a05a32020-05-12 11:50:28 +01002174func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
2175 testJava(t, `
2176 java_sdk_library {
2177 name: "foo",
2178 srcs: ["a.java", "b.java"],
2179 api_packages: ["foo"],
2180 system: {
2181 enabled: true,
2182 sdk_version: "module_current",
2183 },
2184 }
2185 `)
2186}
2187
Paul Duffin0c5bae52020-06-02 13:00:08 +01002188func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
2189 testJava(t, `
2190 java_sdk_library {
2191 name: "foo",
2192 srcs: ["a.java", "b.java"],
2193 api_packages: ["foo"],
2194 system: {
2195 enabled: true,
2196 },
2197 module_lib: {
2198 enabled: true,
2199 },
2200 }
2201 `)
2202}
2203
2204func TestJavaSdkLibrary_SystemServer(t *testing.T) {
2205 testJava(t, `
2206 java_sdk_library {
2207 name: "foo",
2208 srcs: ["a.java", "b.java"],
2209 api_packages: ["foo"],
2210 system: {
2211 enabled: true,
2212 },
2213 system_server: {
2214 enabled: true,
2215 },
2216 }
2217 `)
2218}
2219
Paul Duffin803a9562020-05-20 11:52:25 +01002220func TestJavaSdkLibrary_MissingScope(t *testing.T) {
2221 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
2222 java_sdk_library {
2223 name: "foo",
2224 srcs: ["a.java"],
2225 public: {
2226 enabled: false,
2227 },
2228 }
2229
2230 java_library {
2231 name: "baz",
2232 srcs: ["a.java"],
2233 libs: ["foo"],
2234 sdk_version: "module_current",
2235 }
2236 `)
2237}
2238
2239func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
2240 testJava(t, `
2241 java_sdk_library {
2242 name: "foo",
2243 srcs: ["a.java"],
2244 system: {
2245 enabled: true,
2246 },
2247 }
2248
2249 java_library {
2250 name: "baz",
2251 srcs: ["a.java"],
2252 libs: ["foo"],
2253 // foo does not have module-lib scope so it should fallback to system
2254 sdk_version: "module_current",
2255 }
2256 `)
2257}
2258
Jiyong Park932cdfe2020-05-28 00:19:53 +09002259func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
2260 ctx, _ := testJava(t, `
2261 java_sdk_library {
2262 name: "foo",
2263 srcs: ["a.java"],
2264 system: {
2265 enabled: true,
2266 },
2267 default_to_stubs: true,
2268 }
2269
2270 java_library {
2271 name: "baz",
2272 srcs: ["a.java"],
2273 libs: ["foo"],
2274 // does not have sdk_version set, should fallback to module,
2275 // which will then fallback to system because the module scope
2276 // is not enabled.
2277 }
2278 `)
2279 // The baz library should depend on the system stubs jar.
2280 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
2281 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2282 t.Errorf("expected %q, found %#q", expected, actual)
2283 }
2284}
2285
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002286var compilerFlagsTestCases = []struct {
2287 in string
2288 out bool
2289}{
2290 {
2291 in: "a",
2292 out: false,
2293 },
2294 {
2295 in: "-a",
2296 out: true,
2297 },
2298 {
2299 in: "-no-jdk",
2300 out: false,
2301 },
2302 {
2303 in: "-no-stdlib",
2304 out: false,
2305 },
2306 {
2307 in: "-kotlin-home",
2308 out: false,
2309 },
2310 {
2311 in: "-kotlin-home /some/path",
2312 out: false,
2313 },
2314 {
2315 in: "-include-runtime",
2316 out: false,
2317 },
2318 {
2319 in: "-Xintellij-plugin-root",
2320 out: false,
2321 },
2322}
2323
2324type mockContext struct {
2325 android.ModuleContext
2326 result bool
2327}
2328
2329func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2330 // CheckBadCompilerFlags calls this function when the flag should be rejected
2331 ctx.result = false
2332}
2333
2334func TestCompilerFlags(t *testing.T) {
2335 for _, testCase := range compilerFlagsTestCases {
2336 ctx := &mockContext{result: true}
2337 CheckKotlincFlags(ctx, []string{testCase.in})
2338 if ctx.result != testCase.out {
2339 t.Errorf("incorrect output:")
2340 t.Errorf(" input: %#v", testCase.in)
2341 t.Errorf(" expected: %#v", testCase.out)
2342 t.Errorf(" got: %#v", ctx.result)
2343 }
2344 }
2345}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002346
2347// TODO(jungjw): Consider making this more robust by ignoring path order.
2348func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2349 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2350 flags := strings.Split(variables["javacFlags"], " ")
2351 got := ""
2352 for _, flag := range flags {
2353 keyEnd := strings.Index(flag, "=")
2354 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2355 got = flag[keyEnd+1:]
2356 break
2357 }
2358 }
2359 if expected != got {
2360 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2361 }
2362}
2363
2364func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002365 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002366 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002367 bp := `
2368 java_library {
2369 name: "foo",
2370 srcs: ["a.java"],
2371 java_version: "1.8",
2372 }
2373
2374 java_library {
2375 name: "bar",
2376 srcs: ["b.java"],
2377 sdk_version: "none",
2378 system_modules: "none",
2379 patch_module: "java.base",
2380 java_version: "1.8",
2381 }
2382
2383 java_library {
2384 name: "baz",
2385 srcs: ["c.java"],
2386 patch_module: "java.base",
2387 java_version: "1.8",
2388 }
2389 `
2390 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002391
2392 checkPatchModuleFlag(t, ctx, "foo", "")
2393 checkPatchModuleFlag(t, ctx, "bar", "")
2394 checkPatchModuleFlag(t, ctx, "baz", "")
2395 })
2396
Pete Gillin0c2143e2019-05-02 15:32:11 +01002397 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002398 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002399 bp := `
2400 java_library {
2401 name: "foo",
2402 srcs: ["a.java"],
2403 }
2404
2405 java_library {
2406 name: "bar",
2407 srcs: ["b.java"],
2408 sdk_version: "none",
2409 system_modules: "none",
2410 patch_module: "java.base",
2411 }
2412
2413 java_library {
2414 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002415 srcs: [
2416 "c.java",
2417 // Tests for b/150878007
2418 "dir/d.java",
2419 "dir2/e.java",
2420 "dir2/f.java",
2421 "nested/dir/g.java"
2422 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002423 patch_module: "java.base",
2424 }
2425 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002426 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002427
2428 checkPatchModuleFlag(t, ctx, "foo", "")
2429 expected := "java.base=.:" + buildDir
2430 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002431 expected = "java.base=" + strings.Join([]string{
Paul Duffin95bdab42021-03-08 21:48:46 +00002432 ".", buildDir, "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002433 checkPatchModuleFlag(t, ctx, "baz", expected)
2434 })
2435}
Paul Duffina7b9f422020-01-10 17:12:18 +00002436
2437func TestJavaSystemModules(t *testing.T) {
2438 ctx, _ := testJava(t, `
2439 java_system_modules {
2440 name: "system-modules",
2441 libs: ["system-module1", "system-module2"],
2442 }
2443 java_library {
2444 name: "system-module1",
2445 srcs: ["a.java"],
2446 sdk_version: "none",
2447 system_modules: "none",
2448 }
2449 java_library {
2450 name: "system-module2",
2451 srcs: ["b.java"],
2452 sdk_version: "none",
2453 system_modules: "none",
2454 }
2455 `)
2456
2457 // check the existence of the module
2458 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2459
2460 cmd := systemModules.Rule("jarsTosystemModules")
2461
2462 // make sure the command compiles against the supplied modules.
2463 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2464 if !strings.Contains(cmd.Args["classpath"], module) {
2465 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2466 module)
2467 }
2468 }
2469}
Paul Duffin90169ba2020-01-10 17:16:44 +00002470
2471func TestJavaSystemModulesImport(t *testing.T) {
2472 ctx, _ := testJava(t, `
2473 java_system_modules_import {
2474 name: "system-modules",
2475 libs: ["system-module1", "system-module2"],
2476 }
2477 java_import {
2478 name: "system-module1",
2479 jars: ["a.jar"],
2480 }
2481 java_import {
2482 name: "system-module2",
2483 jars: ["b.jar"],
2484 }
2485 `)
2486
2487 // check the existence of the module
2488 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2489
2490 cmd := systemModules.Rule("jarsTosystemModules")
2491
2492 // make sure the command compiles against the supplied modules.
2493 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2494 if !strings.Contains(cmd.Args["classpath"], module) {
2495 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2496 module)
2497 }
2498 }
2499}
Paul Duffin83a2d962019-11-19 19:44:10 +00002500
2501func TestJavaLibraryWithSystemModules(t *testing.T) {
2502 ctx, _ := testJava(t, `
2503 java_library {
2504 name: "lib-with-source-system-modules",
2505 srcs: [
2506 "a.java",
2507 ],
2508 sdk_version: "none",
2509 system_modules: "source-system-modules",
2510 }
2511
2512 java_library {
2513 name: "source-jar",
2514 srcs: [
2515 "a.java",
2516 ],
2517 }
2518
2519 java_system_modules {
2520 name: "source-system-modules",
2521 libs: ["source-jar"],
2522 }
2523
2524 java_library {
2525 name: "lib-with-prebuilt-system-modules",
2526 srcs: [
2527 "a.java",
2528 ],
2529 sdk_version: "none",
2530 system_modules: "prebuilt-system-modules",
2531 }
2532
2533 java_import {
2534 name: "prebuilt-jar",
2535 jars: ["a.jar"],
2536 }
2537
2538 java_system_modules_import {
2539 name: "prebuilt-system-modules",
2540 libs: ["prebuilt-jar"],
2541 }
2542 `)
2543
2544 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2545
2546 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2547}
2548
2549func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2550 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2551 bootClasspath := javacRule.Args["bootClasspath"]
2552 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2553 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2554 }
2555}
Jiyong Park19604de2020-03-24 16:44:11 +09002556
2557func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2558 ctx, _ := testJava(t, `
2559 java_library {
2560 name: "foo",
2561 srcs: ["aidl/foo/IFoo.aidl"],
2562 libs: ["bar"],
2563 }
2564
2565 java_import {
2566 name: "bar",
2567 jars: ["a.jar"],
2568 aidl: {
2569 export_include_dirs: ["aidl/bar"],
2570 },
2571 }
2572 `)
2573
2574 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2575 expectedAidlFlag := "-Iaidl/bar"
2576 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2577 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2578 }
2579}
Liz Kammerdd849a82020-06-12 16:38:45 -07002580
Jooyung Hane197d8b2021-01-05 10:33:16 +09002581func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2582 ctx, _ := testJava(t, `
2583 java_library {
2584 name: "foo",
2585 srcs: ["aidl/foo/IFoo.aidl"],
2586 aidl: { flags: ["-Werror"], },
2587 }
2588 `)
2589
2590 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2591 expectedAidlFlag := "-Werror"
2592 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2593 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2594 }
2595}
2596
Liz Kammerdd849a82020-06-12 16:38:45 -07002597func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002598 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002599 java_test_host {
2600 name: "foo",
2601 srcs: ["a.java"],
2602 data_native_bins: ["bin"]
2603 }
2604
2605 python_binary_host {
2606 name: "bin",
2607 srcs: ["bin.py"],
2608 }
2609 `)
2610
2611 buildOS := android.BuildOs.String()
2612
2613 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002614 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002615 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2616 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2617 if !reflect.DeepEqual(expected, actual) {
2618 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2619 }
2620}