blob: 6bebf37293e592fe2f6f434c8b2d5985bb0c0a69 [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
Jeongik Cha28df2572019-11-11 10:46:36 +090028 "github.com/google/blueprint/proptools"
29
Colin Crossa4f08812018-10-02 22:03:40 -070030 "android/soong/android"
31 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080032 "android/soong/dexpreopt"
Colin Cross1661aff2021-03-12 17:56:51 -080033 "android/soong/genrule"
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 Duffin70d3bee2021-03-21 11:26:05 +000051// Legacy factory to use to create fixtures for tests in this package.
52//
53// deprecated: See prepareForJavaTest
54var javaFixtureFactory = android.NewFixtureFactory(
55 &buildDir,
56 prepareForJavaTest,
57)
Paul Duffinafeee222021-03-17 00:36:35 +000058
Paul Duffin70d3bee2021-03-21 11:26:05 +000059// Legacy preparer used for running tests within the java package.
60//
61// This includes everything that was needed to run any test in the java package prior to the
62// introduction of the test fixtures. Tests that are being converted to use fixtures directly
63// rather than through the testJava...() methods should avoid using this and instead use the
64// various preparers directly, using android.GroupFixturePreparers(...) to group them when
65// necessary.
66//
67// deprecated
68var prepareForJavaTest = android.GroupFixturePreparers(
Paul Duffin95bdab42021-03-08 21:48:46 +000069 genrule.PrepareForTestWithGenRuleBuildComponents,
70 // Get the CC build components but not default modules.
71 cc.PrepareForTestWithCcBuildComponents,
72 // Include all the default java modules.
73 PrepareForTestWithJavaDefaultModules,
Paul Duffin42da69d2021-03-22 13:41:36 +000074 PrepareForTestWithOverlayBuildComponents,
Paul Duffin220ddd72021-03-17 23:54:30 +000075 python.PrepareForTestWithPythonBuildComponents,
Paul Duffin95bdab42021-03-08 21:48:46 +000076 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
77 ctx.RegisterModuleType("java_plugin", PluginFactory)
Paul Duffin95bdab42021-03-08 21:48:46 +000078
Paul Duffin95bdab42021-03-08 21:48:46 +000079 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
80 }),
Paul Duffin95bdab42021-03-08 21:48:46 +000081 dexpreopt.PrepareForTestWithDexpreopt,
82)
83
Colin Cross72bb3632017-07-13 16:23:21 -070084func TestMain(m *testing.M) {
85 run := func() int {
86 setUp()
87 defer tearDown()
88
89 return m.Run()
90 }
91
92 os.Exit(run())
93}
Colin Cross527012a2017-11-30 22:56:16 -080094
Paul Duffin95bdab42021-03-08 21:48:46 +000095// testConfig is a legacy way of creating a test Config for testing java modules.
96//
97// See testJava for an explanation as to how to stop using this deprecated method.
98//
99// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800100func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Paul Duffin9f045242021-01-21 15:05:11 +0000101 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -0700102}
103
Paul Duffin95bdab42021-03-08 21:48:46 +0000104// testContext is a legacy way of creating a TestContext for testing java modules.
105//
106// See testJava for an explanation as to how to stop using this deprecated method.
107//
108// deprecated
Colin Crossae8600b2020-10-29 17:09:13 -0700109func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -0700110
Colin Crossae8600b2020-10-29 17:09:13 -0700111 ctx := android.NewTestArchContext(config)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000112 RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -0800113 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -0800114 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Colin Cross89536d42017-07-07 14:35:50 -0700115 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100116 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +0000117
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700118 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Paul Duffineafc16b2021-02-24 01:43:18 +0000119 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
120 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
Colin Crossa4f08812018-10-02 22:03:40 -0700121
Paul Duffin021f4e52020-07-30 16:04:17 +0100122 android.RegisterPrebuiltMutators(ctx)
123
Paul Duffind6ceb862021-03-04 23:02:31 +0000124 genrule.RegisterGenruleBuildComponents(ctx)
125
Colin Crossa4f08812018-10-02 22:03:40 -0700126 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000127 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700128
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700129 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
130 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
131 })
132
Colin Cross527012a2017-11-30 22:56:16 -0800133 return ctx
134}
135
Paul Duffin95bdab42021-03-08 21:48:46 +0000136// run is a legacy way of running tests of java modules.
137//
138// See testJava for an explanation as to how to stop using this deprecated method.
139//
140// deprecated
Colin Cross527012a2017-11-30 22:56:16 -0800141func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800142 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800143
Colin Cross98be1bb2019-12-13 20:41:13 -0800144 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000145 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800146
Colin Crossae8600b2020-10-29 17:09:13 -0700147 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100148 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800149 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700150 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800151 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800152}
153
Paul Duffin95bdab42021-03-08 21:48:46 +0000154// testJavaError is a legacy way of running tests of java modules that expect errors.
155//
156// See testJava for an explanation as to how to stop using this deprecated method.
157//
158// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900159func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900160 t.Helper()
Paul Duffin6bac49c2021-03-12 21:28:15 +0000161 result := javaFixtureFactory.
162 Extend(dexpreopt.PrepareForTestWithDexpreopt).
163 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
164 RunTestWithBp(t, bp)
165 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900166}
167
Paul Duffin95bdab42021-03-08 21:48:46 +0000168// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
169//
170// See testJava for an explanation as to how to stop using this deprecated method.
171//
172// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800173func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900174 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000175 // This must be done on the supplied config and not as part of the fixture because any changes to
176 // the fixture's config will be ignored when RunTestWithConfig replaces it.
Colin Cross98be1bb2019-12-13 20:41:13 -0800177 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000178 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Paul Duffin95bdab42021-03-08 21:48:46 +0000179 result := javaFixtureFactory.
180 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
181 RunTestWithConfig(t, config)
182 return result.TestContext, result.Config
Paul Duffinec0fe172021-02-25 15:34:13 +0000183}
184
Paul Duffin95bdab42021-03-08 21:48:46 +0000185// runWithErrors is a legacy way of running tests of java modules that expect errors.
186//
187// See testJava for an explanation as to how to stop using this deprecated method.
188//
189// deprecated
Paul Duffinec0fe172021-02-25 15:34:13 +0000190func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) {
Colin Crossae8600b2020-10-29 17:09:13 -0700191 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900192 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
193 if len(errs) > 0 {
194 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000195 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900196 }
197 _, errs = ctx.PrepareBuildActions(config)
198 if len(errs) > 0 {
199 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000200 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900201 }
202
203 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Paul Duffinec0fe172021-02-25 15:34:13 +0000204 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900205}
206
Paul Duffin95bdab42021-03-08 21:48:46 +0000207// testJavaWithFS runs tests using the javaFixtureFactory
208//
209// See testJava for an explanation as to how to stop using this deprecated method.
210//
211// deprecated
212func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -0700213 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000214 result := javaFixtureFactory.Extend(fs.AddToFixture()).RunTestWithBp(t, bp)
215 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -0700216}
217
Paul Duffin95bdab42021-03-08 21:48:46 +0000218// testJava runs tests using the javaFixtureFactory
219//
220// Do not add any new usages of this, instead use the javaFixtureFactory directly as it makes it
221// much easier to customize the test behavior.
222//
223// If it is necessary to customize the behavior of an existing test that uses this then please first
224// convert the test to using javaFixtureFactory first and then in a following change add the
225// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
226// that it did not change the test behavior unexpectedly.
227//
228// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700229func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800230 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000231 result := javaFixtureFactory.RunTestWithBp(t, bp)
232 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900233}
234
Paul Duffin95bdab42021-03-08 21:48:46 +0000235// testJavaWithConfig runs tests using the javaFixtureFactory
236//
237// See testJava for an explanation as to how to stop using this deprecated method.
238//
239// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800240func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900241 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000242 result := javaFixtureFactory.RunTestWithConfig(t, config)
243 return result.TestContext, result.Config
Colin Cross72bb3632017-07-13 16:23:21 -0700244}
245
Colin Cross2acdae82017-09-15 19:44:24 -0700246func moduleToPath(name string) string {
247 switch {
248 case name == `""`:
249 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700250 case strings.HasSuffix(name, ".jar"):
251 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700252 default:
253 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700254 }
255}
256
Paul Duffin95bdab42021-03-08 21:48:46 +0000257// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
258// is defined in PrepareForIntegrationTestWithJava
259func defaultModuleToPath(name string) string {
Paul Duffin76101fa2021-03-13 09:55:25 +0000260 switch {
261 case name == `""`:
262 return name
263 case strings.HasSuffix(name, ".jar"):
264 return name
265 default:
266 return filepath.Join(buildDir, ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
267 }
Paul Duffin95bdab42021-03-08 21:48:46 +0000268}
269
Jeongik Chae403e9e2019-12-07 00:16:24 +0900270func TestJavaLinkType(t *testing.T) {
271 testJava(t, `
272 java_library {
273 name: "foo",
274 srcs: ["a.java"],
275 libs: ["bar"],
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
Steven Moreland00298982020-11-17 21:44:36 +0000292 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900293 java_library {
294 name: "foo",
295 srcs: ["a.java"],
296 libs: ["bar"],
297 sdk_version: "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
314 testJava(t, `
315 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 sdk_version: "system_current",
332 srcs: ["c.java"],
333 }
334 `)
335
Steven Moreland00298982020-11-17 21:44:36 +0000336 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900337 java_library {
338 name: "foo",
339 srcs: ["a.java"],
340 libs: ["bar"],
341 sdk_version: "system_current",
342 static_libs: ["baz"],
343 }
344
345 java_library {
346 name: "bar",
347 sdk_version: "current",
348 srcs: ["b.java"],
349 }
350
351 java_library {
352 name: "baz",
353 srcs: ["c.java"],
354 }
355 `)
356}
357
Colin Cross72bb3632017-07-13 16:23:21 -0700358func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700359 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700360 java_library {
361 name: "foo",
362 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700363 libs: ["bar"],
364 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700365 }
366
367 java_library {
368 name: "bar",
369 srcs: ["b.java"],
370 }
371
372 java_library {
373 name: "baz",
374 srcs: ["c.java"],
375 }
Colin Crossd5934c82017-10-02 13:55:26 -0700376 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700377
Colin Cross4c428df2017-09-15 17:36:05 -0700378 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700379 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700380
381 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
382 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
383 }
384
Colin Cross1ee23172017-10-18 14:44:18 -0700385 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700386 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
387 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700388
Paul Duffin22b77cd2021-03-12 19:15:01 +0000389 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700390
Paul Duffin22b77cd2021-03-12 19:15:01 +0000391 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700392
393 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
394 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700395 }
396}
397
Artur Satayev9cf46692019-11-26 18:08:34 +0000398func TestExportedPlugins(t *testing.T) {
399 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800400 library string
401 processors string
402 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000403 }
404 var tests = []struct {
405 name string
406 extra string
407 results []Result
408 }{
409 {
410 name: "Exported plugin is not a direct plugin",
411 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
412 results: []Result{{library: "exports", processors: "-proc:none"}},
413 },
414 {
415 name: "Exports plugin to dependee",
416 extra: `
417 java_library{name: "exports", exported_plugins: ["plugin"]}
418 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
419 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
420 `,
421 results: []Result{
422 {library: "foo", processors: "-processor com.android.TestPlugin"},
423 {library: "bar", processors: "-processor com.android.TestPlugin"},
424 },
425 },
426 {
427 name: "Exports plugin to android_library",
428 extra: `
429 java_library{name: "exports", exported_plugins: ["plugin"]}
430 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
431 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
432 `,
433 results: []Result{
434 {library: "foo", processors: "-processor com.android.TestPlugin"},
435 {library: "bar", processors: "-processor com.android.TestPlugin"},
436 },
437 },
438 {
439 name: "Exports plugin is not propagated via transitive deps",
440 extra: `
441 java_library{name: "exports", exported_plugins: ["plugin"]}
442 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
443 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
444 `,
445 results: []Result{
446 {library: "foo", processors: "-processor com.android.TestPlugin"},
447 {library: "bar", processors: "-proc:none"},
448 },
449 },
450 {
451 name: "Exports plugin appends to plugins",
452 extra: `
453 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
454 java_library{name: "exports", exported_plugins: ["plugin"]}
455 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
456 `,
457 results: []Result{
458 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
459 },
460 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800461 {
462 name: "Exports plugin to with generates_api to dependee",
463 extra: `
464 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
465 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
466 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
467 `,
468 results: []Result{
469 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
470 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
471 },
472 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000473 }
474
475 for _, test := range tests {
476 t.Run(test.name, func(t *testing.T) {
477 ctx, _ := testJava(t, `
478 java_plugin {
479 name: "plugin",
480 processor_class: "com.android.TestPlugin",
481 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800482 java_plugin {
483 name: "plugin_generates_api",
484 generates_api: true,
485 processor_class: "com.android.TestPlugin",
486 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000487 `+test.extra)
488
489 for _, want := range test.results {
490 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
491 if javac.Args["processor"] != want.processors {
492 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
493 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800494 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
495 disableTurbine := turbine.BuildParams.Rule == nil
496 if disableTurbine != want.disableTurbine {
497 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
498 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000499 }
500 })
501 }
502}
503
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900504func TestSdkVersionByPartition(t *testing.T) {
505 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900506 java_library {
507 name: "foo",
508 srcs: ["a.java"],
509 vendor: true,
510 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900511 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900512
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900513 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900514 java_library {
515 name: "bar",
516 srcs: ["b.java"],
517 }
518 `)
519
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900520 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900521 bp := `
522 java_library {
523 name: "foo",
524 srcs: ["a.java"],
525 product_specific: true,
526 }
527 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800528
529 config := testConfig(nil, bp, nil)
530 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900531 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800532 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 +0900533 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800534 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900535 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900536 }
537}
538
Colin Crossd5934c82017-10-02 13:55:26 -0700539func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700540 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700541 java_library {
542 name: "foo",
543 srcs: ["a.java"],
544 target: {
545 android: {
546 srcs: ["b.java"],
547 },
548 },
549 }
550 `)
551
552 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
553 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
554 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
555 }
556}
557
Colin Cross6b4a32d2017-12-05 13:42:45 -0800558func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700559 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800560 java_library_host {
561 name: "foo",
562 srcs: ["a.java"],
563 }
564
565 java_binary_host {
566 name: "bar",
567 srcs: ["b.java"],
568 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700569 jni_libs: ["libjni"],
570 }
571
572 cc_library_shared {
573 name: "libjni",
574 host_supported: true,
575 device_supported: false,
576 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800577 }
578 `)
579
580 buildOS := android.BuildOs.String()
581
582 bar := ctx.ModuleForTests("bar", buildOS+"_common")
583 barJar := bar.Output("bar.jar").Output.String()
584 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
585 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
586
Colin Cross89226d92020-10-09 19:00:54 -0700587 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
588 libjniSO := libjni.Rule("Cp").Output.String()
589
Colin Cross6b4a32d2017-12-05 13:42:45 -0800590 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700591 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
592 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800593 }
Colin Cross89226d92020-10-09 19:00:54 -0700594
595 // Test that the install binary wrapper depends on the installed JNI libraries
596 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
597 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800598 }
Alex Humesky2070e322020-06-09 20:23:08 -0400599}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800600
Alex Humesky2070e322020-06-09 20:23:08 -0400601func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
602 bp := `
603 java_library {
604 name: "target_library",
605 srcs: ["a.java"],
606 }
607
608 java_binary_host {
609 name: "host_binary",
610 srcs: ["b.java"],
611 }
612 `
613 config := testConfig(nil, bp, nil)
614 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
615
616 ctx, _ := testJavaWithConfig(t, config)
617
Liz Kammer7941b302020-07-28 13:27:34 -0700618 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400619 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
620 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
621 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
622 t.Errorf("target library javac flags %v should contain "+
623 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
624 }
625
626 // check that -g is not overridden for host modules
627 buildOS := android.BuildOs.String()
628 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
629 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
630 if strings.Contains(hostJavaFlags, "-g:source,lines") {
631 t.Errorf("java_binary_host javac flags %v should not have "+
632 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
633 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800634}
635
Colin Cross72bb3632017-07-13 16:23:21 -0700636func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700637 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700638 java_library {
639 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000640 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000641 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700642 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700643 }
644
Colin Cross74d73e22017-08-02 11:05:49 -0700645 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700646 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700647 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700648 }
649
Colin Cross74d73e22017-08-02 11:05:49 -0700650 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700651 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700652 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700653 sdk_version: "current",
654 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700655 }
Colin Cross42be7612019-02-21 18:12:14 -0800656
657 dex_import {
658 name: "qux",
659 jars: ["b.jar"],
660 }
Colin Cross79c7c262019-04-17 11:11:46 -0700661
662 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000663 name: "sdklib",
664 public: {
665 jars: ["c.jar"],
666 },
667 }
668
Paul Duffin91547182019-11-12 19:39:36 +0000669 prebuilt_stubs_sources {
670 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000671 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000672 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000673
674 java_test_import {
675 name: "test",
676 jars: ["a.jar"],
677 test_suites: ["cts"],
678 test_config: "AndroidTest.xml",
679 }
Colin Cross72bb3632017-07-13 16:23:21 -0700680 `)
681
Paul Duffin9b478b02019-12-10 13:41:51 +0000682 fooModule := ctx.ModuleForTests("foo", "android_common")
683 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700684 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700685 barModule := ctx.ModuleForTests("bar", "android_common")
686 barJar := barModule.Rule("combineJar").Output
687 bazModule := ctx.ModuleForTests("baz", "android_common")
688 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700689 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700690
Paul Duffin9b478b02019-12-10 13:41:51 +0000691 fooLibrary := fooModule.Module().(*Library)
692 assertDeepEquals(t, "foo java sources incorrect",
693 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000694
Paul Duffin9b478b02019-12-10 13:41:51 +0000695 assertDeepEquals(t, "foo java source jars incorrect",
696 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
697 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000698
Colin Cross37f6d792018-07-12 12:28:41 -0700699 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
700 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700701 }
702
Liz Kammerd6c31d22020-08-05 15:40:41 -0700703 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
704 if barDexJar != nil {
705 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
706 }
707
Colin Cross79c7c262019-04-17 11:11:46 -0700708 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
709 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
710 }
711
Colin Cross37f6d792018-07-12 12:28:41 -0700712 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
713 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700714 }
Colin Cross42be7612019-02-21 18:12:14 -0800715
Liz Kammerd6c31d22020-08-05 15:40:41 -0700716 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
717 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
718 if bazDexJar != expectedDexJar {
719 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
720 }
721
Colin Cross42be7612019-02-21 18:12:14 -0800722 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700723}
724
Paul Duffin9b478b02019-12-10 13:41:51 +0000725func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
726 if !reflect.DeepEqual(expected, actual) {
727 t.Errorf("%s: expected %q, found %q", message, expected, actual)
728 }
729}
730
Paul Duffin1a393322020-11-18 16:36:47 +0000731func TestPrebuiltStubsSources(t *testing.T) {
732 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
733 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
734prebuilt_stubs_sources {
735 name: "stubs-source",
736 srcs: ["%s"],
737}`, sourcesPath), map[string][]byte{
738 "stubs/sources/pkg/A.java": nil,
739 "stubs/sources/pkg/B.java": nil,
740 })
741
742 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
743 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
744 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
745 }
746 }
747
748 t.Run("empty/missing directory", func(t *testing.T) {
749 test(t, "empty-directory", []string{})
750 })
751
752 t.Run("non-empty set of sources", func(t *testing.T) {
753 test(t, "stubs/sources", []string{
754 "stubs/sources/pkg/A.java",
755 "stubs/sources/pkg/B.java",
756 })
757 })
758}
759
Paul Duffin56d44902020-01-31 13:36:25 +0000760func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000761 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000762 java_library {
763 name: "foo",
764 srcs: ["a.java"],
765 libs: ["sdklib"],
766 sdk_version: "current",
767 }
768
769 java_library {
770 name: "foo.system",
771 srcs: ["a.java"],
772 libs: ["sdklib"],
773 sdk_version: "system_current",
774 }
775
776 java_library {
777 name: "foo.test",
778 srcs: ["a.java"],
779 libs: ["sdklib"],
780 sdk_version: "test_current",
781 }
782
783 java_sdk_library_import {
784 name: "sdklib",
785 public: {
786 jars: ["a.jar"],
787 },
788 system: {
789 jars: ["b.jar"],
790 },
791 test: {
792 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100793 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000794 },
795 }
796 `)
797
798 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000799 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000800 javac := fooModule.Rule("javac")
801
Paul Duffin22b77cd2021-03-12 19:15:01 +0000802 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
803 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000804 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100805
Paul Duffin22b77cd2021-03-12 19:15:01 +0000806 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100807 `prebuilt_sdklib.stubs`,
808 `prebuilt_sdklib.stubs.source.test`,
809 `prebuilt_sdklib.stubs.system`,
810 `prebuilt_sdklib.stubs.test`,
811 })
812}
813
814func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000815 result := javaFixtureFactory.Extend(
816 PrepareForTestWithJavaSdkLibraryFiles,
817 FixtureWithLastReleaseApis("sdklib"),
818 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100819 java_sdk_library {
820 name: "sdklib",
821 srcs: ["a.java"],
822 sdk_version: "none",
823 system_modules: "none",
824 public: {
825 enabled: true,
826 },
827 }
828
829 java_sdk_library_import {
830 name: "sdklib",
831 public: {
832 jars: ["a.jar"],
833 },
834 }
835 `)
836
Paul Duffin22b77cd2021-03-12 19:15:01 +0000837 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100838 `dex2oatd`,
839 `prebuilt_sdklib`,
840 `sdklib.impl`,
841 `sdklib.stubs`,
842 `sdklib.stubs.source`,
843 `sdklib.xml`,
844 })
845
Paul Duffin22b77cd2021-03-12 19:15:01 +0000846 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100847 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100848 `sdklib.impl`,
849 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
850 // dependency is added after prebuilts may have been renamed and so has to use
851 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100852 `sdklib.xml`,
853 })
854}
855
856func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000857 result := javaFixtureFactory.Extend(
858 PrepareForTestWithJavaSdkLibraryFiles,
859 FixtureWithLastReleaseApis("sdklib"),
860 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100861 java_sdk_library {
862 name: "sdklib",
863 srcs: ["a.java"],
864 sdk_version: "none",
865 system_modules: "none",
866 public: {
867 enabled: true,
868 },
869 }
870
871 java_sdk_library_import {
872 name: "sdklib",
873 prefer: true,
874 public: {
875 jars: ["a.jar"],
876 },
877 }
878 `)
879
Paul Duffin22b77cd2021-03-12 19:15:01 +0000880 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100881 `dex2oatd`,
882 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100883 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100884 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100885 `sdklib.stubs.source`,
886 `sdklib.xml`,
887 })
888
Paul Duffin22b77cd2021-03-12 19:15:01 +0000889 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100890 `prebuilt_sdklib.stubs`,
891 `sdklib.impl`,
892 `sdklib.xml`,
893 })
Paul Duffin56d44902020-01-31 13:36:25 +0000894}
895
JaeMan Parkff715562020-10-19 17:25:58 +0900896func TestJavaSdkLibraryEnforce(t *testing.T) {
897 partitionToBpOption := func(partition string) string {
898 switch partition {
899 case "system":
900 return ""
901 case "vendor":
902 return "soc_specific: true,"
903 case "product":
904 return "product_specific: true,"
905 default:
906 panic("Invalid partition group name: " + partition)
907 }
908 }
909
910 type testConfigInfo struct {
911 libraryType string
912 fromPartition string
913 toPartition string
914 enforceVendorInterface bool
915 enforceProductInterface bool
916 enforceJavaSdkLibraryCheck bool
917 allowList []string
918 }
919
Paul Duffin05f72de2021-03-12 21:28:51 +0000920 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900921 bpFileTemplate := `
922 java_library {
923 name: "foo",
924 srcs: ["foo.java"],
925 libs: ["bar"],
926 sdk_version: "current",
927 %s
928 }
929
930 %s {
931 name: "bar",
932 srcs: ["bar.java"],
933 sdk_version: "current",
934 %s
935 }
936 `
937
938 bpFile := fmt.Sprintf(bpFileTemplate,
939 partitionToBpOption(info.fromPartition),
940 info.libraryType,
941 partitionToBpOption(info.toPartition))
942
Paul Duffin05f72de2021-03-12 21:28:51 +0000943 return android.GroupFixturePreparers(
Paul Duffin163043d2021-03-12 19:18:49 +0000944 PrepareForTestWithJavaSdkLibraryFiles,
945 FixtureWithLastReleaseApis("bar"),
Paul Duffin05f72de2021-03-12 21:28:51 +0000946 android.FixtureWithRootAndroidBp(bpFile),
947 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
948 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
949 if info.enforceVendorInterface {
950 variables.DeviceVndkVersion = proptools.StringPtr("current")
951 }
952 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
953 variables.InterPartitionJavaLibraryAllowList = info.allowList
954 }),
955 )
JaeMan Parkff715562020-10-19 17:25:58 +0900956 }
957
Paul Duffinaa6caa72021-03-13 16:50:15 +0000958 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
959 t.Run(fmt.Sprintf("%#v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000960 errorHandler := android.FixtureExpectsNoErrors
961 if expectedErrorPattern != "" {
962 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000963 }
Paul Duffin05f72de2021-03-12 21:28:51 +0000964 javaFixtureFactory.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
Paul Duffinaa6caa72021-03-13 16:50:15 +0000965 })
966 }
967
JaeMan Parkff715562020-10-19 17:25:58 +0900968 errorMessage := "is not allowed across the partitions"
969
Paul Duffinaa6caa72021-03-13 16:50:15 +0000970 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900971 libraryType: "java_library",
972 fromPartition: "product",
973 toPartition: "system",
974 enforceVendorInterface: true,
975 enforceProductInterface: true,
976 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000977 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900978
Paul Duffinaa6caa72021-03-13 16:50:15 +0000979 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900980 libraryType: "java_library",
981 fromPartition: "product",
982 toPartition: "system",
983 enforceVendorInterface: true,
984 enforceProductInterface: false,
985 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000986 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900987
Paul Duffinaa6caa72021-03-13 16:50:15 +0000988 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900989 libraryType: "java_library",
990 fromPartition: "product",
991 toPartition: "system",
992 enforceVendorInterface: true,
993 enforceProductInterface: true,
994 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000995 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900996
Paul Duffinaa6caa72021-03-13 16:50:15 +0000997 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900998 libraryType: "java_library",
999 fromPartition: "vendor",
1000 toPartition: "system",
1001 enforceVendorInterface: true,
1002 enforceProductInterface: true,
1003 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001004 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +09001005
Paul Duffinaa6caa72021-03-13 16:50:15 +00001006 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +09001007 libraryType: "java_library",
1008 fromPartition: "vendor",
1009 toPartition: "system",
1010 enforceVendorInterface: true,
1011 enforceProductInterface: true,
1012 enforceJavaSdkLibraryCheck: true,
1013 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +00001014 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +09001015
Paul Duffinaa6caa72021-03-13 16:50:15 +00001016 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +09001017 libraryType: "java_library",
1018 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +09001019 toPartition: "product",
1020 enforceVendorInterface: true,
1021 enforceProductInterface: true,
1022 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001023 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +09001024
Paul Duffinaa6caa72021-03-13 16:50:15 +00001025 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001026 libraryType: "java_sdk_library",
1027 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +09001028 toPartition: "system",
1029 enforceVendorInterface: true,
1030 enforceProductInterface: true,
1031 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001032 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001033
Paul Duffinaa6caa72021-03-13 16:50:15 +00001034 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001035 libraryType: "java_sdk_library",
1036 fromPartition: "vendor",
1037 toPartition: "system",
1038 enforceVendorInterface: true,
1039 enforceProductInterface: true,
1040 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001041 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001042
Paul Duffinaa6caa72021-03-13 16:50:15 +00001043 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001044 libraryType: "java_sdk_library",
1045 fromPartition: "vendor",
1046 toPartition: "product",
1047 enforceVendorInterface: true,
1048 enforceProductInterface: true,
1049 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001050 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +09001051}
1052
Colin Cross89536d42017-07-07 14:35:50 -07001053func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001054 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -07001055 java_defaults {
1056 name: "defaults",
1057 srcs: ["a.java"],
1058 libs: ["bar"],
1059 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -07001060 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -07001061 }
1062
1063 java_library {
1064 name: "foo",
1065 defaults: ["defaults"],
1066 }
1067
1068 java_library {
1069 name: "bar",
1070 srcs: ["b.java"],
1071 }
1072
1073 java_library {
1074 name: "baz",
1075 srcs: ["c.java"],
1076 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001077
1078 android_test {
1079 name: "atestOptimize",
1080 defaults: ["defaults"],
1081 optimize: {enabled: true},
1082 }
1083
1084 android_test {
1085 name: "atestNoOptimize",
1086 defaults: ["defaults"],
1087 }
1088
1089 android_test {
1090 name: "atestDefault",
1091 srcs: ["a.java"],
1092 }
Colin Cross89536d42017-07-07 14:35:50 -07001093 `)
1094
Colin Cross4c428df2017-09-15 17:36:05 -07001095 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001096 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001097
1098 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1099 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1100 }
1101
Nan Zhanged19fc32017-10-19 13:06:22 -07001102 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1103 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1104 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001105 }
1106
Colin Cross1ee23172017-10-18 14:44:18 -07001107 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001108 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1109 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001110 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001111
1112 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1113 if atestOptimize.Output == nil {
1114 t.Errorf("atestOptimize should optimize APK")
1115 }
1116
1117 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1118 if atestNoOptimize.Output == nil {
1119 t.Errorf("atestNoOptimize should not optimize APK")
1120 }
1121
1122 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1123 if atestDefault.Output == nil {
1124 t.Errorf("atestDefault should optimize APK")
1125 }
Colin Cross89536d42017-07-07 14:35:50 -07001126}
1127
Colin Cross0f37af02017-09-27 17:42:05 -07001128func TestResources(t *testing.T) {
1129 var table = []struct {
1130 name string
1131 prop string
1132 extra string
1133 args string
1134 }{
1135 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001136 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001137 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001138 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001139 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001140 },
1141 {
1142 // Test that a module with java_resources includes the files
1143 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001144 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1145 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001146 },
1147 {
1148 // Test that a module with a filegroup in java_resources includes the files with the
1149 // path prefix
1150 name: "resource filegroup",
1151 prop: `java_resources: [":foo-res"]`,
1152 extra: `
1153 filegroup {
1154 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001155 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001156 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001157 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001158 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001159 },
1160 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001161 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1162 name: "wildcard dirs",
1163 prop: `java_resource_dirs: ["java-res/*"]`,
1164 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1165 },
1166 {
1167 // Test that a module exclude_java_resource_dirs excludes the files
1168 name: "wildcard dirs",
1169 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1170 args: "-C java-res/a -f java-res/a/a",
1171 },
Colin Crosscedd4762018-09-13 11:26:19 -07001172 {
1173 // Test wildcards in java_resources
1174 name: "wildcard files",
1175 prop: `java_resources: ["java-res/**/*"]`,
1176 args: "-C . -f java-res/a/a -f java-res/b/b",
1177 },
1178 {
1179 // Test exclude_java_resources with java_resources
1180 name: "wildcard files with exclude",
1181 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1182 args: "-C . -f java-res/a/a",
1183 },
1184 {
1185 // Test exclude_java_resources with java_resource_dirs
1186 name: "resource dirs with exclude files",
1187 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1188 args: "-C java-res -f java-res/a/a",
1189 },
1190 {
1191 // Test exclude_java_resource_dirs with java_resource_dirs
1192 name: "resource dirs with exclude files",
1193 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1194 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1195 },
Colin Cross0f37af02017-09-27 17:42:05 -07001196 }
1197
1198 for _, test := range table {
1199 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001200 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001201 java_library {
1202 name: "foo",
1203 srcs: [
1204 "a.java",
1205 "b.java",
1206 "c.java",
1207 ],
1208 `+test.prop+`,
1209 }
Colin Cross238c1f32020-06-07 16:58:18 -07001210 `+test.extra,
1211 map[string][]byte{
1212 "java-res/a/a": nil,
1213 "java-res/b/b": nil,
1214 "java-res2/a": nil,
1215 },
1216 )
Colin Cross0f37af02017-09-27 17:42:05 -07001217
Colin Cross331a1212018-08-15 20:40:52 -07001218 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001219 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001220
1221 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1222 t.Errorf("foo combined jars %v does not contain %q",
1223 foo.Inputs.Strings(), fooRes.Output.String())
1224 }
1225
Colin Crossaf9c55b2017-10-03 14:50:08 -07001226 if fooRes.Args["jarArgs"] != test.args {
1227 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001228 fooRes.Args["jarArgs"], test.args)
1229 }
1230 })
1231 }
1232}
1233
Colin Cross0c4ce212019-05-03 15:28:19 -07001234func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001235 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001236 java_library {
1237 name: "foo",
1238 srcs: [
1239 "a.java",
1240 "b.java",
1241 "c.java",
1242 ],
1243 include_srcs: true,
1244 }
1245
1246 java_library {
1247 name: "bar",
1248 srcs: [
1249 "a.java",
1250 "b.java",
1251 "c.java",
1252 ],
1253 java_resource_dirs: ["java-res"],
1254 include_srcs: true,
1255 }
Colin Cross238c1f32020-06-07 16:58:18 -07001256 `, map[string][]byte{
1257 "java-res/a/a": nil,
1258 "java-res/b/b": nil,
1259 "java-res2/a": nil,
1260 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001261
1262 // Test a library with include_srcs: true
1263 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1264 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1265
1266 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1267 t.Errorf("foo combined jars %v does not contain %q", w, g)
1268 }
1269
1270 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1271 t.Errorf("foo source jar args %q is not %q", w, g)
1272 }
1273
1274 // Test a library with include_srcs: true and resources
1275 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1276 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1277 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1278 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1279
1280 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1281 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1282 }
1283
1284 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1285 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1286 }
1287
1288 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1289 t.Errorf("bar combined jars %v does not contain %q", w, g)
1290 }
1291
1292 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1293 t.Errorf("bar source jar args %q is not %q", w, g)
1294 }
1295
1296 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1297 t.Errorf("bar resource jar args %q is not %q", w, g)
1298 }
1299}
1300
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001301func TestJavaLint(t *testing.T) {
1302 ctx, _ := testJavaWithFS(t, `
1303 java_library {
1304 name: "foo",
1305 srcs: [
1306 "a.java",
1307 "b.java",
1308 "c.java",
1309 ],
1310 min_sdk_version: "29",
1311 sdk_version: "system_current",
1312 }
1313 `, map[string][]byte{
1314 "lint-baseline.xml": nil,
1315 })
1316
1317 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001318
Colin Cross1661aff2021-03-12 17:56:51 -08001319 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1320 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001321 t.Error("did not pass --baseline flag")
1322 }
1323}
1324
1325func TestJavaLintWithoutBaseline(t *testing.T) {
1326 ctx, _ := testJavaWithFS(t, `
1327 java_library {
1328 name: "foo",
1329 srcs: [
1330 "a.java",
1331 "b.java",
1332 "c.java",
1333 ],
1334 min_sdk_version: "29",
1335 sdk_version: "system_current",
1336 }
1337 `, map[string][]byte{})
1338
1339 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001340
Colin Cross1661aff2021-03-12 17:56:51 -08001341 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1342 if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001343 t.Error("passed --baseline flag for non existent file")
1344 }
1345}
1346
1347func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
1348 config := testConfig(
1349 nil,
1350 `
1351 java_library {
1352 name: "foo",
1353 srcs: [
1354 ],
1355 min_sdk_version: "29",
1356 sdk_version: "system_current",
1357 lint: {
1358 baseline_filename: "mybaseline.xml",
1359 },
1360 }
1361 `, map[string][]byte{
1362 "build/soong/java/lint_defaults.txt": nil,
1363 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1364 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1365 "framework/aidl": nil,
1366 "a.java": nil,
1367 "AndroidManifest.xml": nil,
1368 "build/make/target/product/security": nil,
1369 })
1370 config.TestAllowNonExistentPaths = false
1371 testJavaErrorWithConfig(t,
1372 "source path \"mybaseline.xml\" does not exist",
1373 config,
1374 )
1375}
1376
1377func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1378 ctx, _ := testJavaWithFS(t, `
1379 java_library {
1380 name: "foo",
1381 srcs: [
1382 "a.java",
1383 "b.java",
1384 "c.java",
1385 ],
1386 min_sdk_version: "29",
1387 sdk_version: "system_current",
1388 lint: {
1389 error_checks: ["SomeCheck"],
1390 baseline_filename: "mybaseline.xml",
1391 },
1392 }
1393 `, map[string][]byte{
1394 "mybaseline.xml": nil,
1395 })
1396
1397 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001398
Colin Cross1661aff2021-03-12 17:56:51 -08001399 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1400 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001401 t.Error("did not use the correct file for baseline")
1402 }
1403}
1404
Colin Cross54190b32017-10-09 15:34:10 -07001405func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001406 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001407 java_library {
1408 name: "foo",
1409 srcs: [
1410 "a*.java",
1411 ":gen",
1412 "b*.java",
1413 ],
1414 }
1415
1416 genrule {
1417 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001418 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001419 out: ["gen.java"],
1420 }
Colin Cross238c1f32020-06-07 16:58:18 -07001421 `, map[string][]byte{
1422 "a.java": nil,
1423 "b.java": nil,
1424 })
Colin Cross54190b32017-10-09 15:34:10 -07001425
1426 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1427 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1428
Colin Cross15e86d92017-10-20 15:07:08 -07001429 if filepath.Base(genrule.Output.String()) != "gen.java" {
1430 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001431 }
1432
1433 if len(javac.Inputs) != 3 ||
1434 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001435 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001436 javac.Inputs[2].String() != "b.java" {
1437 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1438 }
1439}
1440
Nan Zhang61eaedb2017-11-02 13:28:15 -07001441func TestTurbine(t *testing.T) {
Paul Duffin3d9f2682021-03-13 09:47:16 +00001442 result := javaFixtureFactory.
1443 Extend(FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
1444 RunTestWithBp(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001445 java_library {
1446 name: "foo",
1447 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001448 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001449 }
1450
1451 java_library {
1452 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001453 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001454 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001455 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001456 }
1457
1458 java_library {
1459 name: "baz",
1460 srcs: ["c.java"],
1461 libs: ["bar"],
1462 sdk_version: "14",
1463 }
1464 `)
1465
Paul Duffin3d9f2682021-03-13 09:47:16 +00001466 fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine")
1467 barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine")
1468 barJavac := result.ModuleForTests("bar", "android_common").Rule("javac")
1469 barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine")
1470 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001471
Paul Duffin3d9f2682021-03-13 09:47:16 +00001472 android.AssertArrayString(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs.Strings())
Nan Zhang61eaedb2017-11-02 13:28:15 -07001473
1474 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
Paul Duffin3d9f2682021-03-13 09:47:16 +00001475 barTurbineJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine", "bar.jar")
1476 android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
1477 android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
1478 android.AssertArrayString(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs.Strings())
1479 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001480}
1481
1482func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001483 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001484 java_library {
1485 name: "bar",
1486 srcs: ["a.java","b.java","c.java"],
1487 javac_shard_size: 1
1488 }
1489 `)
1490
1491 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1492 for i := 0; i < 3; i++ {
1493 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1494 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1495 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1496 }
1497 }
1498}
1499
Nan Zhang581fd212018-01-10 16:06:12 -08001500func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001501 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001502 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001503 name: "droiddoc-templates-sdk",
1504 path: ".",
1505 }
Jiyong Park29074592019-07-07 16:27:47 +09001506 filegroup {
1507 name: "bar-doc-aidl-srcs",
1508 srcs: ["bar-doc/IBar.aidl"],
1509 path: "bar-doc",
1510 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001511 droidstubs {
1512 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001513 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001514 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001515 ],
Steve Kim3666c702020-09-01 17:58:01 +00001516 exclude_srcs: [
1517 "bar-doc/b.java"
1518 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001519 api_levels_annotations_dirs: [
1520 "droiddoc-templates-sdk",
1521 ],
1522 api_levels_annotations_enabled: true,
1523 }
1524 droiddoc {
1525 name: "bar-doc",
1526 srcs: [
1527 ":bar-stubs",
1528 "bar-doc/IFoo.aidl",
1529 ":bar-doc-aidl-srcs",
1530 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001531 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001532 hdf: [
1533 "android.whichdoc offline",
1534 ],
1535 knowntags: [
1536 "bar-doc/known_oj_tags.txt",
1537 ],
1538 proofread_file: "libcore-proofread.txt",
1539 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001540 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001541 }
Colin Cross238c1f32020-06-07 16:58:18 -07001542 `,
1543 map[string][]byte{
1544 "bar-doc/a.java": nil,
1545 "bar-doc/b.java": nil,
1546 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001547 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1548 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1549 if err != nil {
1550 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1551 }
1552 if len(barStubsOutputs) != 1 {
1553 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001554 }
Nan Zhang581fd212018-01-10 16:06:12 -08001555
Liz Kammere1ab2502020-09-10 15:29:25 +00001556 barStubsOutput := barStubsOutputs[0]
1557 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1558 javaDoc := barDoc.Rule("javadoc")
1559 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1560 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001561 }
1562
Liz Kammere1ab2502020-09-10 15:29:25 +00001563 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1564 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1565 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1566 }
1567
1568 aidl := barDoc.Rule("aidl")
1569 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001570 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1571 }
1572
1573 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1574 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001575 }
Nan Zhang581fd212018-01-10 16:06:12 -08001576}
1577
Liz Kammer585cac22020-07-06 09:12:57 -07001578func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1579 testJavaError(t, "flags is set. Cannot set args", `
1580 droiddoc_exported_dir {
1581 name: "droiddoc-templates-sdk",
1582 path: ".",
1583 }
1584 filegroup {
1585 name: "bar-doc-aidl-srcs",
1586 srcs: ["bar-doc/IBar.aidl"],
1587 path: "bar-doc",
1588 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001589 droidstubs {
1590 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001591 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001592 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001593 ],
Steve Kim3666c702020-09-01 17:58:01 +00001594 exclude_srcs: [
1595 "bar-doc/b.java"
1596 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001597 api_levels_annotations_dirs: [
1598 "droiddoc-templates-sdk",
1599 ],
1600 api_levels_annotations_enabled: true,
1601 }
1602 droiddoc {
1603 name: "bar-doc",
1604 srcs: [
1605 ":bar-stubs",
1606 "bar-doc/IFoo.aidl",
1607 ":bar-doc-aidl-srcs",
1608 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001609 custom_template: "droiddoc-templates-sdk",
1610 hdf: [
1611 "android.whichdoc offline",
1612 ],
1613 knowntags: [
1614 "bar-doc/known_oj_tags.txt",
1615 ],
1616 proofread_file: "libcore-proofread.txt",
1617 todo_file: "libcore-docs-todo.html",
1618 flags: ["-offlinemode -title \"libcore\""],
1619 args: "-offlinemode -title \"libcore\"",
1620 }
1621 `)
1622}
1623
Liz Kammer3d894b72020-08-04 09:55:13 -07001624func TestDroidstubs(t *testing.T) {
1625 ctx, _ := testJavaWithFS(t, `
1626 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001627 name: "droiddoc-templates-sdk",
1628 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001629 }
1630
1631 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001632 name: "bar-stubs",
1633 srcs: ["bar-doc/a.java"],
1634 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1635 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001636 }
1637
1638 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001639 name: "bar-stubs-other",
1640 srcs: ["bar-doc/a.java"],
1641 high_mem: true,
1642 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1643 api_levels_annotations_enabled: true,
1644 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001645 }
1646 `,
1647 map[string][]byte{
1648 "bar-doc/a.java": nil,
1649 })
1650 testcases := []struct {
1651 moduleName string
1652 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001653 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001654 }{
1655 {
1656 moduleName: "bar-stubs",
1657 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001658 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001659 },
1660 {
1661 moduleName: "bar-stubs-other",
1662 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001663 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001664 },
1665 }
1666 for _, c := range testcases {
1667 m := ctx.ModuleForTests(c.moduleName, "android_common")
1668 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001669 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001670 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001671 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001672 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1673 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001674
1675 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1676 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1677 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001678 }
1679}
1680
Paul Duffin83a2d962019-11-19 19:44:10 +00001681func TestDroidstubsWithSystemModules(t *testing.T) {
1682 ctx, _ := testJava(t, `
1683 droidstubs {
1684 name: "stubs-source-system-modules",
1685 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001686 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001687 ],
1688 sdk_version: "none",
1689 system_modules: "source-system-modules",
1690 }
1691
1692 java_library {
1693 name: "source-jar",
1694 srcs: [
1695 "a.java",
1696 ],
1697 }
1698
1699 java_system_modules {
1700 name: "source-system-modules",
1701 libs: ["source-jar"],
1702 }
1703
1704 droidstubs {
1705 name: "stubs-prebuilt-system-modules",
1706 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001707 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001708 ],
1709 sdk_version: "none",
1710 system_modules: "prebuilt-system-modules",
1711 }
1712
1713 java_import {
1714 name: "prebuilt-jar",
1715 jars: ["a.jar"],
1716 }
1717
1718 java_system_modules_import {
1719 name: "prebuilt-system-modules",
1720 libs: ["prebuilt-jar"],
1721 }
1722 `)
1723
1724 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1725
1726 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1727}
1728
1729func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1730 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1731 var systemJars []string
1732 for _, i := range metalavaRule.Implicits {
1733 systemJars = append(systemJars, i.Base())
1734 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001735 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001736 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1737 }
1738}
1739
Colin Cross54250902017-12-05 09:28:08 -08001740func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001741 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001742 java_library {
1743 name: "foo",
1744 srcs: ["a.java"],
1745 }
1746
1747 java_genrule {
1748 name: "jargen",
1749 tool_files: ["b.java"],
1750 cmd: "$(location b.java) $(in) $(out)",
1751 out: ["jargen.jar"],
1752 srcs: [":foo"],
1753 }
1754
1755 java_library {
1756 name: "bar",
1757 static_libs: ["jargen"],
1758 srcs: ["c.java"],
1759 }
1760
1761 java_library {
1762 name: "baz",
1763 libs: ["jargen"],
1764 srcs: ["c.java"],
1765 }
1766 `)
1767
1768 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1769 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1770 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1771 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1772 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1773
Colin Cross3d680512020-11-13 16:23:53 -08001774 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1775 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001776 }
1777
1778 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1779 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1780 }
1781
1782 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1783 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1784 }
1785
1786 if len(barCombined.Inputs) != 2 ||
1787 barCombined.Inputs[0].String() != bar.Output.String() ||
1788 barCombined.Inputs[1].String() != jargen.Output.String() {
1789 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1790 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1791 }
1792}
1793
Nan Zhang27e284d2018-02-09 21:03:53 +00001794func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001795 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001796 java_library {
1797 name: "foo",
1798 srcs: ["a.java", ":foo-srcs"],
1799 exclude_srcs: ["a.java", ":foo-excludes"],
1800 }
1801
1802 filegroup {
1803 name: "foo-srcs",
1804 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1805 }
1806
1807 filegroup {
1808 name: "foo-excludes",
1809 srcs: ["java-fg/a.java", "java-fg/b.java"],
1810 }
1811 `)
1812
1813 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1814
1815 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1816 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1817 }
1818}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001819
Paul Duffin52d398a2019-06-11 12:31:14 +01001820func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001821 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001822 "libcore/Android.bp": []byte(`
1823 java_library {
1824 name: "core",
1825 sdk_version: "none",
1826 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001827 }
1828
1829 filegroup {
1830 name: "core-jar",
1831 srcs: [":core{.jar}"],
1832 }
1833`),
1834 })
Colin Crossae8600b2020-10-29 17:09:13 -07001835 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001836 run(t, ctx, config)
1837}
1838
1839func TestJavaImport(t *testing.T) {
1840 config := testConfig(nil, "", map[string][]byte{
1841 "libcore/Android.bp": []byte(`
1842 java_import {
1843 name: "core",
1844 sdk_version: "none",
1845 }
1846
1847 filegroup {
1848 name: "core-jar",
1849 srcs: [":core{.jar}"],
1850 }
1851`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001852 })
Colin Crossae8600b2020-10-29 17:09:13 -07001853 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001854 run(t, ctx, config)
1855}
1856
Jiyong Parkc678ad32018-04-10 13:07:10 +09001857func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001858 result := javaFixtureFactory.Extend(
1859 PrepareForTestWithJavaSdkLibraryFiles,
1860 FixtureWithPrebuiltApis(map[string][]string{
1861 "28": {"foo"},
1862 "29": {"foo"},
1863 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1864 }),
1865 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001866 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001867 name: "droiddoc-templates-sdk",
1868 path: ".",
1869 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001870 java_sdk_library {
1871 name: "foo",
1872 srcs: ["a.java", "b.java"],
1873 api_packages: ["foo"],
1874 }
1875 java_sdk_library {
1876 name: "bar",
1877 srcs: ["a.java", "b.java"],
1878 api_packages: ["bar"],
1879 }
1880 java_library {
1881 name: "baz",
1882 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001883 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001884 sdk_version: "system_current",
1885 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001886 java_sdk_library {
1887 name: "barney",
1888 srcs: ["c.java"],
1889 api_only: true,
1890 }
1891 java_sdk_library {
1892 name: "betty",
1893 srcs: ["c.java"],
1894 shared_library: false,
1895 }
Paul Duffin859fe962020-05-15 10:20:31 +01001896 java_sdk_library_import {
1897 name: "quuz",
1898 public: {
1899 jars: ["c.jar"],
1900 },
1901 }
1902 java_sdk_library_import {
1903 name: "fred",
1904 public: {
1905 jars: ["b.jar"],
1906 },
1907 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001908 java_sdk_library_import {
1909 name: "wilma",
1910 public: {
1911 jars: ["b.jar"],
1912 },
1913 shared_library: false,
1914 }
Jiyong Park1be96912018-05-28 18:02:19 +09001915 java_library {
1916 name: "qux",
1917 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001918 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001919 sdk_version: "system_current",
1920 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001921 java_library {
1922 name: "baz-test",
1923 srcs: ["c.java"],
1924 libs: ["foo"],
1925 sdk_version: "test_current",
1926 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001927 java_library {
1928 name: "baz-29",
1929 srcs: ["c.java"],
1930 libs: ["foo"],
1931 sdk_version: "system_29",
1932 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001933 java_library {
1934 name: "baz-module-30",
1935 srcs: ["c.java"],
1936 libs: ["foo"],
1937 sdk_version: "module_30",
1938 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001939 `)
1940
1941 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001942 result.ModuleForTests("foo", "android_common")
1943 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1944 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1945 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1946 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1947 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1948 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1949 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1950 result.ModuleForTests("foo.api.public.28", "")
1951 result.ModuleForTests("foo.api.system.28", "")
1952 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001953
Paul Duffin22b77cd2021-03-12 19:15:01 +00001954 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001955 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001956 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001957 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001958 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001959 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001960 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001961
Paul Duffin22b77cd2021-03-12 19:15:01 +00001962 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001963 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001964 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001965
Paul Duffin22b77cd2021-03-12 19:15:01 +00001966 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001967 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001968 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001969
Paul Duffin22b77cd2021-03-12 19:15:01 +00001970 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001971 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001972 android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001973
Jiyong Park1be96912018-05-28 18:02:19 +09001974 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001975 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001976 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001977 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001978 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001979 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001980}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001981
Anton Hansson7f66efa2020-10-08 14:47:23 +01001982func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001983 result := javaFixtureFactory.Extend(
1984 PrepareForTestWithJavaSdkLibraryFiles,
1985 FixtureWithLastReleaseApis("sdklib"),
1986 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001987 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001988 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001989 srcs: ["a.java"],
1990 impl_only_libs: ["foo"],
1991 stub_only_libs: ["bar"],
1992 }
1993 java_library {
1994 name: "foo",
1995 srcs: ["a.java"],
1996 sdk_version: "current",
1997 }
1998 java_library {
1999 name: "bar",
2000 srcs: ["a.java"],
2001 sdk_version: "current",
2002 }
2003 `)
2004
Anton Hanssondff2c782020-12-21 17:10:01 +00002005 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002006 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01002007 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
2008 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
2009 }
2010 }
Anton Hanssondff2c782020-12-21 17:10:01 +00002011 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00002012 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01002013 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
2014 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
2015 }
2016}
2017
Paul Duffindaaa3322020-05-26 18:13:57 +01002018func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002019 result := javaFixtureFactory.Extend(
2020 PrepareForTestWithJavaSdkLibraryFiles,
2021 FixtureWithLastReleaseApis("foo"),
2022 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01002023 java_sdk_library {
2024 name: "foo",
2025 srcs: ["a.java"],
2026 api_only: true,
2027 public: {
2028 enabled: true,
2029 },
2030 }
2031
2032 java_library {
2033 name: "bar",
2034 srcs: ["b.java"],
2035 libs: ["foo"],
2036 }
2037 `)
2038
2039 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002040 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01002041 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2042 t.Errorf("expected %q, found %#q", expected, actual)
2043 }
2044}
2045
Paul Duffin46dc45a2020-05-14 15:39:10 +01002046func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002047 javaFixtureFactory.Extend(
2048 PrepareForTestWithJavaSdkLibraryFiles,
2049 FixtureWithLastReleaseApis("foo"),
2050 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002051 java_sdk_library {
2052 name: "foo",
2053 srcs: ["a.java"],
2054 api_packages: ["foo"],
2055 public: {
2056 enabled: true,
2057 },
2058 }
2059
2060 java_library {
2061 name: "bar",
2062 srcs: ["b.java", ":foo{.public.stubs.source}"],
2063 }
2064 `)
2065}
2066
2067func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002068 javaFixtureFactory.
Paul Duffin163043d2021-03-12 19:18:49 +00002069 Extend(
2070 PrepareForTestWithJavaSdkLibraryFiles,
2071 FixtureWithLastReleaseApis("foo"),
2072 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00002073 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
2074 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002075 java_sdk_library {
2076 name: "foo",
2077 srcs: ["a.java"],
2078 api_packages: ["foo"],
2079 public: {
2080 enabled: true,
2081 },
2082 }
2083
2084 java_library {
2085 name: "bar",
2086 srcs: ["b.java", ":foo{.system.stubs.source}"],
2087 }
2088 `)
2089}
2090
Paul Duffinca8d9a52020-06-26 22:20:25 +01002091func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002092 result := javaFixtureFactory.Extend(
2093 PrepareForTestWithJavaSdkLibraryFiles,
2094 FixtureWithLastReleaseApis("sdklib"),
2095 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01002096 java_sdk_library {
2097 name: "sdklib",
2098 srcs: ["a.java"],
2099 sdk_version: "none",
2100 system_modules: "none",
2101 public: {
2102 enabled: true,
2103 },
2104 }
2105 `)
2106
Paul Duffin22b77cd2021-03-12 19:15:01 +00002107 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01002108 `dex2oatd`,
2109 `sdklib.impl`,
2110 `sdklib.stubs`,
2111 `sdklib.stubs.source`,
2112 `sdklib.xml`,
2113 })
2114}
2115
Paul Duffin46dc45a2020-05-14 15:39:10 +01002116func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002117 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002118 java_sdk_library_import {
2119 name: "foo",
2120 public: {
2121 jars: ["a.jar"],
2122 stub_srcs: ["a.java"],
2123 current_api: "api/current.txt",
2124 removed_api: "api/removed.txt",
2125 },
2126 }
2127
2128 java_library {
2129 name: "bar",
2130 srcs: [":foo{.public.stubs.source}"],
2131 java_resources: [
2132 ":foo{.public.api.txt}",
2133 ":foo{.public.removed-api.txt}",
2134 ],
2135 }
2136 `)
2137}
2138
2139func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2140 bp := `
2141 java_sdk_library_import {
2142 name: "foo",
2143 public: {
2144 jars: ["a.jar"],
2145 },
2146 }
2147 `
2148
2149 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002150 javaFixtureFactory.
2151 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
2152 RunTestWithBp(t, bp+`
2153 java_library {
2154 name: "bar",
2155 srcs: [":foo{.public.stubs.source}"],
2156 java_resources: [
2157 ":foo{.public.api.txt}",
2158 ":foo{.public.removed-api.txt}",
2159 ],
2160 }
2161 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002162 })
2163
2164 t.Run("api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002165 javaFixtureFactory.
2166 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
2167 RunTestWithBp(t, bp+`
2168 java_library {
2169 name: "bar",
2170 srcs: ["a.java"],
2171 java_resources: [
2172 ":foo{.public.api.txt}",
2173 ],
2174 }
2175 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002176 })
2177
2178 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002179 javaFixtureFactory.
2180 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
2181 RunTestWithBp(t, bp+`
2182 java_library {
2183 name: "bar",
2184 srcs: ["a.java"],
2185 java_resources: [
2186 ":foo{.public.removed-api.txt}",
2187 ],
2188 }
2189 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002190 })
2191}
2192
Paul Duffin3375e352020-04-28 10:44:03 +01002193func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002194 javaFixtureFactory.
2195 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
2196 RunTestWithBp(t, `
2197 java_sdk_library {
2198 name: "foo",
2199 srcs: ["a.java", "b.java"],
2200 api_packages: ["foo"],
2201 // Explicitly disable public to test the check that ensures the set of enabled
2202 // scopes is consistent.
2203 public: {
2204 enabled: false,
2205 },
2206 system: {
2207 enabled: true,
2208 },
2209 }
Paul Duffin3375e352020-04-28 10:44:03 +01002210 `)
2211}
2212
Paul Duffin87a05a32020-05-12 11:50:28 +01002213func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002214 javaFixtureFactory.Extend(
2215 PrepareForTestWithJavaSdkLibraryFiles,
2216 FixtureWithLastReleaseApis("foo"),
2217 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002218 java_sdk_library {
2219 name: "foo",
2220 srcs: ["a.java", "b.java"],
2221 api_packages: ["foo"],
2222 system: {
2223 enabled: true,
2224 sdk_version: "module_current",
2225 },
2226 }
2227 `)
2228}
2229
Paul Duffin0c5bae52020-06-02 13:00:08 +01002230func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002231 javaFixtureFactory.Extend(
2232 PrepareForTestWithJavaSdkLibraryFiles,
2233 FixtureWithLastReleaseApis("foo"),
2234 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002235 java_sdk_library {
2236 name: "foo",
2237 srcs: ["a.java", "b.java"],
2238 api_packages: ["foo"],
2239 system: {
2240 enabled: true,
2241 },
2242 module_lib: {
2243 enabled: true,
2244 },
2245 }
2246 `)
2247}
2248
2249func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002250 javaFixtureFactory.Extend(
2251 PrepareForTestWithJavaSdkLibraryFiles,
2252 FixtureWithLastReleaseApis("foo"),
2253 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002254 java_sdk_library {
2255 name: "foo",
2256 srcs: ["a.java", "b.java"],
2257 api_packages: ["foo"],
2258 system: {
2259 enabled: true,
2260 },
2261 system_server: {
2262 enabled: true,
2263 },
2264 }
2265 `)
2266}
2267
Paul Duffin803a9562020-05-20 11:52:25 +01002268func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002269 javaFixtureFactory.
2270 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2271 RunTestWithBp(t, `
2272 java_sdk_library {
2273 name: "foo",
2274 srcs: ["a.java"],
2275 public: {
2276 enabled: false,
2277 },
2278 }
Paul Duffin803a9562020-05-20 11:52:25 +01002279
Paul Duffin22b77cd2021-03-12 19:15:01 +00002280 java_library {
2281 name: "baz",
2282 srcs: ["a.java"],
2283 libs: ["foo"],
2284 sdk_version: "module_current",
2285 }
Paul Duffin803a9562020-05-20 11:52:25 +01002286 `)
2287}
2288
2289func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002290 javaFixtureFactory.Extend(
2291 PrepareForTestWithJavaSdkLibraryFiles,
2292 FixtureWithLastReleaseApis("foo"),
2293 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002294 java_sdk_library {
2295 name: "foo",
2296 srcs: ["a.java"],
2297 system: {
2298 enabled: true,
2299 },
2300 }
2301
2302 java_library {
2303 name: "baz",
2304 srcs: ["a.java"],
2305 libs: ["foo"],
2306 // foo does not have module-lib scope so it should fallback to system
2307 sdk_version: "module_current",
2308 }
2309 `)
2310}
2311
Jiyong Park932cdfe2020-05-28 00:19:53 +09002312func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002313 result := javaFixtureFactory.Extend(
2314 PrepareForTestWithJavaSdkLibraryFiles,
2315 FixtureWithLastReleaseApis("foo"),
2316 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002317 java_sdk_library {
2318 name: "foo",
2319 srcs: ["a.java"],
2320 system: {
2321 enabled: true,
2322 },
2323 default_to_stubs: true,
2324 }
2325
2326 java_library {
2327 name: "baz",
2328 srcs: ["a.java"],
2329 libs: ["foo"],
2330 // does not have sdk_version set, should fallback to module,
2331 // which will then fallback to system because the module scope
2332 // is not enabled.
2333 }
2334 `)
2335 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002336 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002337 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2338 t.Errorf("expected %q, found %#q", expected, actual)
2339 }
2340}
2341
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002342var compilerFlagsTestCases = []struct {
2343 in string
2344 out bool
2345}{
2346 {
2347 in: "a",
2348 out: false,
2349 },
2350 {
2351 in: "-a",
2352 out: true,
2353 },
2354 {
2355 in: "-no-jdk",
2356 out: false,
2357 },
2358 {
2359 in: "-no-stdlib",
2360 out: false,
2361 },
2362 {
2363 in: "-kotlin-home",
2364 out: false,
2365 },
2366 {
2367 in: "-kotlin-home /some/path",
2368 out: false,
2369 },
2370 {
2371 in: "-include-runtime",
2372 out: false,
2373 },
2374 {
2375 in: "-Xintellij-plugin-root",
2376 out: false,
2377 },
2378}
2379
2380type mockContext struct {
2381 android.ModuleContext
2382 result bool
2383}
2384
2385func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2386 // CheckBadCompilerFlags calls this function when the flag should be rejected
2387 ctx.result = false
2388}
2389
2390func TestCompilerFlags(t *testing.T) {
2391 for _, testCase := range compilerFlagsTestCases {
2392 ctx := &mockContext{result: true}
2393 CheckKotlincFlags(ctx, []string{testCase.in})
2394 if ctx.result != testCase.out {
2395 t.Errorf("incorrect output:")
2396 t.Errorf(" input: %#v", testCase.in)
2397 t.Errorf(" expected: %#v", testCase.out)
2398 t.Errorf(" got: %#v", ctx.result)
2399 }
2400 }
2401}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002402
2403// TODO(jungjw): Consider making this more robust by ignoring path order.
2404func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2405 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2406 flags := strings.Split(variables["javacFlags"], " ")
2407 got := ""
2408 for _, flag := range flags {
2409 keyEnd := strings.Index(flag, "=")
2410 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2411 got = flag[keyEnd+1:]
2412 break
2413 }
2414 }
2415 if expected != got {
2416 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2417 }
2418}
2419
2420func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002421 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002422 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002423 bp := `
2424 java_library {
2425 name: "foo",
2426 srcs: ["a.java"],
2427 java_version: "1.8",
2428 }
2429
2430 java_library {
2431 name: "bar",
2432 srcs: ["b.java"],
2433 sdk_version: "none",
2434 system_modules: "none",
2435 patch_module: "java.base",
2436 java_version: "1.8",
2437 }
2438
2439 java_library {
2440 name: "baz",
2441 srcs: ["c.java"],
2442 patch_module: "java.base",
2443 java_version: "1.8",
2444 }
2445 `
2446 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002447
2448 checkPatchModuleFlag(t, ctx, "foo", "")
2449 checkPatchModuleFlag(t, ctx, "bar", "")
2450 checkPatchModuleFlag(t, ctx, "baz", "")
2451 })
2452
Pete Gillin0c2143e2019-05-02 15:32:11 +01002453 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002454 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002455 bp := `
2456 java_library {
2457 name: "foo",
2458 srcs: ["a.java"],
2459 }
2460
2461 java_library {
2462 name: "bar",
2463 srcs: ["b.java"],
2464 sdk_version: "none",
2465 system_modules: "none",
2466 patch_module: "java.base",
2467 }
2468
2469 java_library {
2470 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002471 srcs: [
2472 "c.java",
2473 // Tests for b/150878007
2474 "dir/d.java",
2475 "dir2/e.java",
2476 "dir2/f.java",
2477 "nested/dir/g.java"
2478 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002479 patch_module: "java.base",
2480 }
2481 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002482 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002483
2484 checkPatchModuleFlag(t, ctx, "foo", "")
2485 expected := "java.base=.:" + buildDir
2486 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002487 expected = "java.base=" + strings.Join([]string{
Paul Duffin95bdab42021-03-08 21:48:46 +00002488 ".", buildDir, "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002489 checkPatchModuleFlag(t, ctx, "baz", expected)
2490 })
2491}
Paul Duffina7b9f422020-01-10 17:12:18 +00002492
Paul Duffin83a2d962019-11-19 19:44:10 +00002493func TestJavaLibraryWithSystemModules(t *testing.T) {
2494 ctx, _ := testJava(t, `
2495 java_library {
2496 name: "lib-with-source-system-modules",
2497 srcs: [
2498 "a.java",
2499 ],
2500 sdk_version: "none",
2501 system_modules: "source-system-modules",
2502 }
2503
2504 java_library {
2505 name: "source-jar",
2506 srcs: [
2507 "a.java",
2508 ],
2509 }
2510
2511 java_system_modules {
2512 name: "source-system-modules",
2513 libs: ["source-jar"],
2514 }
2515
2516 java_library {
2517 name: "lib-with-prebuilt-system-modules",
2518 srcs: [
2519 "a.java",
2520 ],
2521 sdk_version: "none",
2522 system_modules: "prebuilt-system-modules",
2523 }
2524
2525 java_import {
2526 name: "prebuilt-jar",
2527 jars: ["a.jar"],
2528 }
2529
2530 java_system_modules_import {
2531 name: "prebuilt-system-modules",
2532 libs: ["prebuilt-jar"],
2533 }
2534 `)
2535
2536 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2537
2538 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2539}
2540
2541func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2542 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2543 bootClasspath := javacRule.Args["bootClasspath"]
2544 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2545 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2546 }
2547}
Jiyong Park19604de2020-03-24 16:44:11 +09002548
2549func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2550 ctx, _ := testJava(t, `
2551 java_library {
2552 name: "foo",
2553 srcs: ["aidl/foo/IFoo.aidl"],
2554 libs: ["bar"],
2555 }
2556
2557 java_import {
2558 name: "bar",
2559 jars: ["a.jar"],
2560 aidl: {
2561 export_include_dirs: ["aidl/bar"],
2562 },
2563 }
2564 `)
2565
2566 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2567 expectedAidlFlag := "-Iaidl/bar"
2568 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2569 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2570 }
2571}
Liz Kammerdd849a82020-06-12 16:38:45 -07002572
Jooyung Hane197d8b2021-01-05 10:33:16 +09002573func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2574 ctx, _ := testJava(t, `
2575 java_library {
2576 name: "foo",
2577 srcs: ["aidl/foo/IFoo.aidl"],
2578 aidl: { flags: ["-Werror"], },
2579 }
2580 `)
2581
2582 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2583 expectedAidlFlag := "-Werror"
2584 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2585 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2586 }
2587}
2588
Liz Kammerdd849a82020-06-12 16:38:45 -07002589func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002590 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002591 java_test_host {
2592 name: "foo",
2593 srcs: ["a.java"],
2594 data_native_bins: ["bin"]
2595 }
2596
2597 python_binary_host {
2598 name: "bin",
2599 srcs: ["bin.py"],
2600 }
2601 `)
2602
2603 buildOS := android.BuildOs.String()
2604
2605 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002606 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002607 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2608 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2609 if !reflect.DeepEqual(expected, actual) {
2610 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2611 }
2612}
Yuexi Ma627263f2021-03-04 13:47:56 -08002613
2614func TestDefaultInstallable(t *testing.T) {
2615 ctx, _ := testJava(t, `
2616 java_test_host {
2617 name: "foo"
2618 }
2619 `)
2620
2621 buildOS := android.BuildOs.String()
2622 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2623 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2624 module.properties.Installable)
2625}