blob: df2dab9de456707a6e2483dfe270388292b34554 [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) {
Paul Duffin95bdab42021-03-08 21:48:46 +000077 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
78 }),
Paul Duffin95bdab42021-03-08 21:48:46 +000079 dexpreopt.PrepareForTestWithDexpreopt,
80)
81
Colin Cross72bb3632017-07-13 16:23:21 -070082func TestMain(m *testing.M) {
83 run := func() int {
84 setUp()
85 defer tearDown()
86
87 return m.Run()
88 }
89
90 os.Exit(run())
91}
Colin Cross527012a2017-11-30 22:56:16 -080092
Paul Duffin95bdab42021-03-08 21:48:46 +000093// testConfig is a legacy way of creating a test Config for testing java modules.
94//
95// See testJava for an explanation as to how to stop using this deprecated method.
96//
97// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080098func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Paul Duffin9f045242021-01-21 15:05:11 +000099 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -0700100}
101
Paul Duffin95bdab42021-03-08 21:48:46 +0000102// testContext is a legacy way of creating a TestContext for testing java modules.
103//
104// See testJava for an explanation as to how to stop using this deprecated method.
105//
106// deprecated
Colin Crossae8600b2020-10-29 17:09:13 -0700107func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -0700108
Colin Crossae8600b2020-10-29 17:09:13 -0700109 ctx := android.NewTestArchContext(config)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000110 RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -0800111 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -0800112 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Colin Cross89536d42017-07-07 14:35:50 -0700113 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100114 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +0000115
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700116 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Paul Duffineafc16b2021-02-24 01:43:18 +0000117 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
118 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
Colin Crossa4f08812018-10-02 22:03:40 -0700119
Paul Duffin021f4e52020-07-30 16:04:17 +0100120 android.RegisterPrebuiltMutators(ctx)
121
Paul Duffind6ceb862021-03-04 23:02:31 +0000122 genrule.RegisterGenruleBuildComponents(ctx)
123
Colin Crossa4f08812018-10-02 22:03:40 -0700124 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000125 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700126
Colin Cross527012a2017-11-30 22:56:16 -0800127 return ctx
128}
129
Paul Duffin95bdab42021-03-08 21:48:46 +0000130// run is a legacy way of running tests of java modules.
131//
132// See testJava for an explanation as to how to stop using this deprecated method.
133//
134// deprecated
Colin Cross527012a2017-11-30 22:56:16 -0800135func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800136 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800137
Colin Cross98be1bb2019-12-13 20:41:13 -0800138 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000139 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800140
Colin Crossae8600b2020-10-29 17:09:13 -0700141 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100142 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800143 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700144 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800145 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800146}
147
Paul Duffin95bdab42021-03-08 21:48:46 +0000148// testJavaError is a legacy way of running tests of java modules that expect errors.
149//
150// See testJava for an explanation as to how to stop using this deprecated method.
151//
152// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900153func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900154 t.Helper()
Paul Duffin6bac49c2021-03-12 21:28:15 +0000155 result := javaFixtureFactory.
156 Extend(dexpreopt.PrepareForTestWithDexpreopt).
157 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
158 RunTestWithBp(t, bp)
159 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900160}
161
Paul Duffin95bdab42021-03-08 21:48:46 +0000162// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
163//
164// See testJava for an explanation as to how to stop using this deprecated method.
165//
166// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800167func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900168 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000169 // This must be done on the supplied config and not as part of the fixture because any changes to
170 // the fixture's config will be ignored when RunTestWithConfig replaces it.
Colin Cross98be1bb2019-12-13 20:41:13 -0800171 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000172 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Paul Duffin95bdab42021-03-08 21:48:46 +0000173 result := javaFixtureFactory.
174 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
175 RunTestWithConfig(t, config)
176 return result.TestContext, result.Config
Paul Duffinec0fe172021-02-25 15:34:13 +0000177}
178
Paul Duffin95bdab42021-03-08 21:48:46 +0000179// runWithErrors is a legacy way of running tests of java modules that expect errors.
180//
181// See testJava for an explanation as to how to stop using this deprecated method.
182//
183// deprecated
Paul Duffinec0fe172021-02-25 15:34:13 +0000184func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) {
Colin Crossae8600b2020-10-29 17:09:13 -0700185 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900186 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
187 if len(errs) > 0 {
188 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000189 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900190 }
191 _, errs = ctx.PrepareBuildActions(config)
192 if len(errs) > 0 {
193 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000194 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900195 }
196
197 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Paul Duffinec0fe172021-02-25 15:34:13 +0000198 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900199}
200
Paul Duffin95bdab42021-03-08 21:48:46 +0000201// testJavaWithFS runs tests using the javaFixtureFactory
202//
203// See testJava for an explanation as to how to stop using this deprecated method.
204//
205// deprecated
206func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -0700207 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000208 result := javaFixtureFactory.Extend(fs.AddToFixture()).RunTestWithBp(t, bp)
209 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -0700210}
211
Paul Duffin95bdab42021-03-08 21:48:46 +0000212// testJava runs tests using the javaFixtureFactory
213//
214// Do not add any new usages of this, instead use the javaFixtureFactory directly as it makes it
215// much easier to customize the test behavior.
216//
217// If it is necessary to customize the behavior of an existing test that uses this then please first
218// convert the test to using javaFixtureFactory first and then in a following change add the
219// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
220// that it did not change the test behavior unexpectedly.
221//
222// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700223func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800224 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000225 result := javaFixtureFactory.RunTestWithBp(t, bp)
226 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900227}
228
Paul Duffin95bdab42021-03-08 21:48:46 +0000229// testJavaWithConfig runs tests using the javaFixtureFactory
230//
231// See testJava for an explanation as to how to stop using this deprecated method.
232//
233// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800234func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900235 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000236 result := javaFixtureFactory.RunTestWithConfig(t, config)
237 return result.TestContext, result.Config
Colin Cross72bb3632017-07-13 16:23:21 -0700238}
239
Colin Cross2acdae82017-09-15 19:44:24 -0700240func moduleToPath(name string) string {
241 switch {
242 case name == `""`:
243 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700244 case strings.HasSuffix(name, ".jar"):
245 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700246 default:
247 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700248 }
249}
250
Paul Duffin95bdab42021-03-08 21:48:46 +0000251// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
252// is defined in PrepareForIntegrationTestWithJava
253func defaultModuleToPath(name string) string {
Paul Duffin76101fa2021-03-13 09:55:25 +0000254 switch {
255 case name == `""`:
256 return name
257 case strings.HasSuffix(name, ".jar"):
258 return name
259 default:
Paul Duffin414ea5e2021-03-22 17:31:52 +0000260 return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
Paul Duffin76101fa2021-03-13 09:55:25 +0000261 }
Paul Duffin95bdab42021-03-08 21:48:46 +0000262}
263
Jeongik Chae403e9e2019-12-07 00:16:24 +0900264func TestJavaLinkType(t *testing.T) {
265 testJava(t, `
266 java_library {
267 name: "foo",
268 srcs: ["a.java"],
269 libs: ["bar"],
270 static_libs: ["baz"],
271 }
272
273 java_library {
274 name: "bar",
275 sdk_version: "current",
276 srcs: ["b.java"],
277 }
278
279 java_library {
280 name: "baz",
281 sdk_version: "system_current",
282 srcs: ["c.java"],
283 }
284 `)
285
Steven Moreland00298982020-11-17 21:44:36 +0000286 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900287 java_library {
288 name: "foo",
289 srcs: ["a.java"],
290 libs: ["bar"],
291 sdk_version: "current",
292 static_libs: ["baz"],
293 }
294
295 java_library {
296 name: "bar",
297 sdk_version: "current",
298 srcs: ["b.java"],
299 }
300
301 java_library {
302 name: "baz",
303 sdk_version: "system_current",
304 srcs: ["c.java"],
305 }
306 `)
307
308 testJava(t, `
309 java_library {
310 name: "foo",
311 srcs: ["a.java"],
312 libs: ["bar"],
313 sdk_version: "system_current",
314 static_libs: ["baz"],
315 }
316
317 java_library {
318 name: "bar",
319 sdk_version: "current",
320 srcs: ["b.java"],
321 }
322
323 java_library {
324 name: "baz",
325 sdk_version: "system_current",
326 srcs: ["c.java"],
327 }
328 `)
329
Steven Moreland00298982020-11-17 21:44:36 +0000330 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900331 java_library {
332 name: "foo",
333 srcs: ["a.java"],
334 libs: ["bar"],
335 sdk_version: "system_current",
336 static_libs: ["baz"],
337 }
338
339 java_library {
340 name: "bar",
341 sdk_version: "current",
342 srcs: ["b.java"],
343 }
344
345 java_library {
346 name: "baz",
347 srcs: ["c.java"],
348 }
349 `)
350}
351
Colin Cross72bb3632017-07-13 16:23:21 -0700352func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700353 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700354 java_library {
355 name: "foo",
356 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700357 libs: ["bar"],
358 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700359 }
360
361 java_library {
362 name: "bar",
363 srcs: ["b.java"],
364 }
365
366 java_library {
367 name: "baz",
368 srcs: ["c.java"],
369 }
Colin Crossd5934c82017-10-02 13:55:26 -0700370 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700371
Paul Duffin414ea5e2021-03-22 17:31:52 +0000372 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop()
Nan Zhanged19fc32017-10-19 13:06:22 -0700373 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700374
375 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
376 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
377 }
378
Colin Cross1ee23172017-10-18 14:44:18 -0700379 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000380 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
381 bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700382
Paul Duffin22b77cd2021-03-12 19:15:01 +0000383 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700384
Paul Duffin22b77cd2021-03-12 19:15:01 +0000385 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700386
387 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
388 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700389 }
390}
391
Artur Satayev9cf46692019-11-26 18:08:34 +0000392func TestExportedPlugins(t *testing.T) {
393 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800394 library string
395 processors string
396 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000397 }
398 var tests = []struct {
399 name string
400 extra string
401 results []Result
402 }{
403 {
404 name: "Exported plugin is not a direct plugin",
405 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
406 results: []Result{{library: "exports", processors: "-proc:none"}},
407 },
408 {
409 name: "Exports plugin to dependee",
410 extra: `
411 java_library{name: "exports", exported_plugins: ["plugin"]}
412 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
413 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
414 `,
415 results: []Result{
416 {library: "foo", processors: "-processor com.android.TestPlugin"},
417 {library: "bar", processors: "-processor com.android.TestPlugin"},
418 },
419 },
420 {
421 name: "Exports plugin to android_library",
422 extra: `
423 java_library{name: "exports", exported_plugins: ["plugin"]}
424 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
425 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
426 `,
427 results: []Result{
428 {library: "foo", processors: "-processor com.android.TestPlugin"},
429 {library: "bar", processors: "-processor com.android.TestPlugin"},
430 },
431 },
432 {
433 name: "Exports plugin is not propagated via transitive deps",
434 extra: `
435 java_library{name: "exports", exported_plugins: ["plugin"]}
436 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
437 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
438 `,
439 results: []Result{
440 {library: "foo", processors: "-processor com.android.TestPlugin"},
441 {library: "bar", processors: "-proc:none"},
442 },
443 },
444 {
445 name: "Exports plugin appends to plugins",
446 extra: `
447 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
448 java_library{name: "exports", exported_plugins: ["plugin"]}
449 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
450 `,
451 results: []Result{
452 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
453 },
454 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800455 {
456 name: "Exports plugin to with generates_api to dependee",
457 extra: `
458 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
459 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
460 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
461 `,
462 results: []Result{
463 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
464 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
465 },
466 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000467 }
468
469 for _, test := range tests {
470 t.Run(test.name, func(t *testing.T) {
471 ctx, _ := testJava(t, `
472 java_plugin {
473 name: "plugin",
474 processor_class: "com.android.TestPlugin",
475 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800476 java_plugin {
477 name: "plugin_generates_api",
478 generates_api: true,
479 processor_class: "com.android.TestPlugin",
480 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000481 `+test.extra)
482
483 for _, want := range test.results {
484 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
485 if javac.Args["processor"] != want.processors {
486 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
487 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800488 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
489 disableTurbine := turbine.BuildParams.Rule == nil
490 if disableTurbine != want.disableTurbine {
491 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
492 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000493 }
494 })
495 }
496}
497
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900498func TestSdkVersionByPartition(t *testing.T) {
499 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900500 java_library {
501 name: "foo",
502 srcs: ["a.java"],
503 vendor: true,
504 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900505 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900506
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900507 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900508 java_library {
509 name: "bar",
510 srcs: ["b.java"],
511 }
512 `)
513
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900514 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900515 bp := `
516 java_library {
517 name: "foo",
518 srcs: ["a.java"],
519 product_specific: true,
520 }
521 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800522
Paul Duffin414ea5e2021-03-22 17:31:52 +0000523 errorHandler := android.FixtureExpectsNoErrors
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900524 if enforce {
Paul Duffin414ea5e2021-03-22 17:31:52 +0000525 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern("sdk_version must have a value when the module is located at vendor or product")
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900526 }
Paul Duffin414ea5e2021-03-22 17:31:52 +0000527
528 android.GroupFixturePreparers(
529 PrepareForTestWithJavaDefaultModules,
530 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
531 variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
532 }),
533 ).
534 ExtendWithErrorHandler(errorHandler).
535 RunTestWithBp(t, bp)
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 `
Alex Humesky2070e322020-06-09 20:23:08 -0400613
Paul Duffin414ea5e2021-03-22 17:31:52 +0000614 result := android.GroupFixturePreparers(
615 PrepareForTestWithJavaDefaultModules,
616 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
617 variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
618 }),
619 ).RunTestWithBp(t, bp)
Alex Humesky2070e322020-06-09 20:23:08 -0400620
Liz Kammer7941b302020-07-28 13:27:34 -0700621 // first, check that the -g flag is added to target modules
Paul Duffin414ea5e2021-03-22 17:31:52 +0000622 targetLibrary := result.ModuleForTests("target_library", "android_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400623 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
624 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
625 t.Errorf("target library javac flags %v should contain "+
626 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
627 }
628
629 // check that -g is not overridden for host modules
630 buildOS := android.BuildOs.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000631 hostBinary := result.ModuleForTests("host_binary", buildOS+"_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400632 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
633 if strings.Contains(hostJavaFlags, "-g:source,lines") {
634 t.Errorf("java_binary_host javac flags %v should not have "+
635 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
636 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800637}
638
Colin Cross72bb3632017-07-13 16:23:21 -0700639func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700640 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700641 java_library {
642 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000643 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000644 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700645 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700646 }
647
Colin Cross74d73e22017-08-02 11:05:49 -0700648 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700649 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700650 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700651 }
652
Colin Cross74d73e22017-08-02 11:05:49 -0700653 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700654 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700655 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700656 sdk_version: "current",
657 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700658 }
Colin Cross42be7612019-02-21 18:12:14 -0800659
660 dex_import {
661 name: "qux",
662 jars: ["b.jar"],
663 }
Colin Cross79c7c262019-04-17 11:11:46 -0700664
665 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000666 name: "sdklib",
667 public: {
668 jars: ["c.jar"],
669 },
670 }
671
Paul Duffin91547182019-11-12 19:39:36 +0000672 prebuilt_stubs_sources {
673 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000674 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000675 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000676
677 java_test_import {
678 name: "test",
679 jars: ["a.jar"],
680 test_suites: ["cts"],
681 test_config: "AndroidTest.xml",
682 }
Colin Cross72bb3632017-07-13 16:23:21 -0700683 `)
684
Paul Duffin9b478b02019-12-10 13:41:51 +0000685 fooModule := ctx.ModuleForTests("foo", "android_common")
686 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700687 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700688 barModule := ctx.ModuleForTests("bar", "android_common")
689 barJar := barModule.Rule("combineJar").Output
690 bazModule := ctx.ModuleForTests("baz", "android_common")
691 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700692 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700693
Paul Duffin9b478b02019-12-10 13:41:51 +0000694 fooLibrary := fooModule.Module().(*Library)
695 assertDeepEquals(t, "foo java sources incorrect",
696 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000697
Paul Duffin9b478b02019-12-10 13:41:51 +0000698 assertDeepEquals(t, "foo java source jars incorrect",
699 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
700 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000701
Colin Cross37f6d792018-07-12 12:28:41 -0700702 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
703 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700704 }
705
Liz Kammerd6c31d22020-08-05 15:40:41 -0700706 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
707 if barDexJar != nil {
708 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
709 }
710
Colin Cross79c7c262019-04-17 11:11:46 -0700711 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
712 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
713 }
714
Colin Cross37f6d792018-07-12 12:28:41 -0700715 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
716 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700717 }
Colin Cross42be7612019-02-21 18:12:14 -0800718
Paul Duffin414ea5e2021-03-22 17:31:52 +0000719 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath()
720 expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar"
721 android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar)
Liz Kammerd6c31d22020-08-05 15:40:41 -0700722
Colin Cross42be7612019-02-21 18:12:14 -0800723 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700724}
725
Paul Duffin9b478b02019-12-10 13:41:51 +0000726func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
727 if !reflect.DeepEqual(expected, actual) {
728 t.Errorf("%s: expected %q, found %q", message, expected, actual)
729 }
730}
731
Paul Duffin1a393322020-11-18 16:36:47 +0000732func TestPrebuiltStubsSources(t *testing.T) {
733 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
734 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
735prebuilt_stubs_sources {
736 name: "stubs-source",
737 srcs: ["%s"],
738}`, sourcesPath), map[string][]byte{
739 "stubs/sources/pkg/A.java": nil,
740 "stubs/sources/pkg/B.java": nil,
741 })
742
743 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
744 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
745 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
746 }
747 }
748
749 t.Run("empty/missing directory", func(t *testing.T) {
750 test(t, "empty-directory", []string{})
751 })
752
753 t.Run("non-empty set of sources", func(t *testing.T) {
754 test(t, "stubs/sources", []string{
755 "stubs/sources/pkg/A.java",
756 "stubs/sources/pkg/B.java",
757 })
758 })
759}
760
Paul Duffin56d44902020-01-31 13:36:25 +0000761func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000762 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000763 java_library {
764 name: "foo",
765 srcs: ["a.java"],
766 libs: ["sdklib"],
767 sdk_version: "current",
768 }
769
770 java_library {
771 name: "foo.system",
772 srcs: ["a.java"],
773 libs: ["sdklib"],
774 sdk_version: "system_current",
775 }
776
777 java_library {
778 name: "foo.test",
779 srcs: ["a.java"],
780 libs: ["sdklib"],
781 sdk_version: "test_current",
782 }
783
784 java_sdk_library_import {
785 name: "sdklib",
786 public: {
787 jars: ["a.jar"],
788 },
789 system: {
790 jars: ["b.jar"],
791 },
792 test: {
793 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100794 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000795 },
796 }
797 `)
798
799 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000800 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000801 javac := fooModule.Rule("javac")
802
Paul Duffin22b77cd2021-03-12 19:15:01 +0000803 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
804 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000805 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100806
Paul Duffin22b77cd2021-03-12 19:15:01 +0000807 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100808 `prebuilt_sdklib.stubs`,
809 `prebuilt_sdklib.stubs.source.test`,
810 `prebuilt_sdklib.stubs.system`,
811 `prebuilt_sdklib.stubs.test`,
812 })
813}
814
815func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000816 result := javaFixtureFactory.Extend(
817 PrepareForTestWithJavaSdkLibraryFiles,
818 FixtureWithLastReleaseApis("sdklib"),
819 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100820 java_sdk_library {
821 name: "sdklib",
822 srcs: ["a.java"],
823 sdk_version: "none",
824 system_modules: "none",
825 public: {
826 enabled: true,
827 },
828 }
829
830 java_sdk_library_import {
831 name: "sdklib",
832 public: {
833 jars: ["a.jar"],
834 },
835 }
836 `)
837
Paul Duffin22b77cd2021-03-12 19:15:01 +0000838 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100839 `dex2oatd`,
840 `prebuilt_sdklib`,
841 `sdklib.impl`,
842 `sdklib.stubs`,
843 `sdklib.stubs.source`,
844 `sdklib.xml`,
845 })
846
Paul Duffin22b77cd2021-03-12 19:15:01 +0000847 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100848 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100849 `sdklib.impl`,
850 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
851 // dependency is added after prebuilts may have been renamed and so has to use
852 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100853 `sdklib.xml`,
854 })
855}
856
857func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000858 result := javaFixtureFactory.Extend(
859 PrepareForTestWithJavaSdkLibraryFiles,
860 FixtureWithLastReleaseApis("sdklib"),
861 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100862 java_sdk_library {
863 name: "sdklib",
864 srcs: ["a.java"],
865 sdk_version: "none",
866 system_modules: "none",
867 public: {
868 enabled: true,
869 },
870 }
871
872 java_sdk_library_import {
873 name: "sdklib",
874 prefer: true,
875 public: {
876 jars: ["a.jar"],
877 },
878 }
879 `)
880
Paul Duffin22b77cd2021-03-12 19:15:01 +0000881 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100882 `dex2oatd`,
883 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100884 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100885 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100886 `sdklib.stubs.source`,
887 `sdklib.xml`,
888 })
889
Paul Duffin22b77cd2021-03-12 19:15:01 +0000890 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100891 `prebuilt_sdklib.stubs`,
892 `sdklib.impl`,
893 `sdklib.xml`,
894 })
Paul Duffin56d44902020-01-31 13:36:25 +0000895}
896
JaeMan Parkff715562020-10-19 17:25:58 +0900897func TestJavaSdkLibraryEnforce(t *testing.T) {
898 partitionToBpOption := func(partition string) string {
899 switch partition {
900 case "system":
901 return ""
902 case "vendor":
903 return "soc_specific: true,"
904 case "product":
905 return "product_specific: true,"
906 default:
907 panic("Invalid partition group name: " + partition)
908 }
909 }
910
911 type testConfigInfo struct {
912 libraryType string
913 fromPartition string
914 toPartition string
915 enforceVendorInterface bool
916 enforceProductInterface bool
917 enforceJavaSdkLibraryCheck bool
918 allowList []string
919 }
920
Paul Duffin05f72de2021-03-12 21:28:51 +0000921 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900922 bpFileTemplate := `
923 java_library {
924 name: "foo",
925 srcs: ["foo.java"],
926 libs: ["bar"],
927 sdk_version: "current",
928 %s
929 }
930
931 %s {
932 name: "bar",
933 srcs: ["bar.java"],
934 sdk_version: "current",
935 %s
936 }
937 `
938
939 bpFile := fmt.Sprintf(bpFileTemplate,
940 partitionToBpOption(info.fromPartition),
941 info.libraryType,
942 partitionToBpOption(info.toPartition))
943
Paul Duffin05f72de2021-03-12 21:28:51 +0000944 return android.GroupFixturePreparers(
Paul Duffin163043d2021-03-12 19:18:49 +0000945 PrepareForTestWithJavaSdkLibraryFiles,
946 FixtureWithLastReleaseApis("bar"),
Paul Duffin05f72de2021-03-12 21:28:51 +0000947 android.FixtureWithRootAndroidBp(bpFile),
948 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
949 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
950 if info.enforceVendorInterface {
951 variables.DeviceVndkVersion = proptools.StringPtr("current")
952 }
953 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
954 variables.InterPartitionJavaLibraryAllowList = info.allowList
955 }),
956 )
JaeMan Parkff715562020-10-19 17:25:58 +0900957 }
958
Paul Duffinaa6caa72021-03-13 16:50:15 +0000959 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
960 t.Run(fmt.Sprintf("%#v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000961 errorHandler := android.FixtureExpectsNoErrors
962 if expectedErrorPattern != "" {
963 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000964 }
Paul Duffin05f72de2021-03-12 21:28:51 +0000965 javaFixtureFactory.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
Paul Duffinaa6caa72021-03-13 16:50:15 +0000966 })
967 }
968
JaeMan Parkff715562020-10-19 17:25:58 +0900969 errorMessage := "is not allowed across the partitions"
970
Paul Duffinaa6caa72021-03-13 16:50:15 +0000971 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900972 libraryType: "java_library",
973 fromPartition: "product",
974 toPartition: "system",
975 enforceVendorInterface: true,
976 enforceProductInterface: true,
977 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000978 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900979
Paul Duffinaa6caa72021-03-13 16:50:15 +0000980 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900981 libraryType: "java_library",
982 fromPartition: "product",
983 toPartition: "system",
984 enforceVendorInterface: true,
985 enforceProductInterface: false,
986 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000987 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900988
Paul Duffinaa6caa72021-03-13 16:50:15 +0000989 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900990 libraryType: "java_library",
991 fromPartition: "product",
992 toPartition: "system",
993 enforceVendorInterface: true,
994 enforceProductInterface: true,
995 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000996 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900997
Paul Duffinaa6caa72021-03-13 16:50:15 +0000998 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900999 libraryType: "java_library",
1000 fromPartition: "vendor",
1001 toPartition: "system",
1002 enforceVendorInterface: true,
1003 enforceProductInterface: true,
1004 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001005 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +09001006
Paul Duffinaa6caa72021-03-13 16:50:15 +00001007 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +09001008 libraryType: "java_library",
1009 fromPartition: "vendor",
1010 toPartition: "system",
1011 enforceVendorInterface: true,
1012 enforceProductInterface: true,
1013 enforceJavaSdkLibraryCheck: true,
1014 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +00001015 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +09001016
Paul Duffinaa6caa72021-03-13 16:50:15 +00001017 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +09001018 libraryType: "java_library",
1019 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +09001020 toPartition: "product",
1021 enforceVendorInterface: true,
1022 enforceProductInterface: true,
1023 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001024 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +09001025
Paul Duffinaa6caa72021-03-13 16:50:15 +00001026 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001027 libraryType: "java_sdk_library",
1028 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +09001029 toPartition: "system",
1030 enforceVendorInterface: true,
1031 enforceProductInterface: true,
1032 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001033 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001034
Paul Duffinaa6caa72021-03-13 16:50:15 +00001035 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001036 libraryType: "java_sdk_library",
1037 fromPartition: "vendor",
1038 toPartition: "system",
1039 enforceVendorInterface: true,
1040 enforceProductInterface: true,
1041 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001042 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001043
Paul Duffinaa6caa72021-03-13 16:50:15 +00001044 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001045 libraryType: "java_sdk_library",
1046 fromPartition: "vendor",
1047 toPartition: "product",
1048 enforceVendorInterface: true,
1049 enforceProductInterface: true,
1050 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001051 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +09001052}
1053
Colin Cross89536d42017-07-07 14:35:50 -07001054func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001055 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -07001056 java_defaults {
1057 name: "defaults",
1058 srcs: ["a.java"],
1059 libs: ["bar"],
1060 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -07001061 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -07001062 }
1063
1064 java_library {
1065 name: "foo",
1066 defaults: ["defaults"],
1067 }
1068
1069 java_library {
1070 name: "bar",
1071 srcs: ["b.java"],
1072 }
1073
1074 java_library {
1075 name: "baz",
1076 srcs: ["c.java"],
1077 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001078
1079 android_test {
1080 name: "atestOptimize",
1081 defaults: ["defaults"],
1082 optimize: {enabled: true},
1083 }
1084
1085 android_test {
1086 name: "atestNoOptimize",
1087 defaults: ["defaults"],
1088 }
1089
1090 android_test {
1091 name: "atestDefault",
1092 srcs: ["a.java"],
1093 }
Colin Cross89536d42017-07-07 14:35:50 -07001094 `)
1095
Paul Duffin414ea5e2021-03-22 17:31:52 +00001096 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop()
Nan Zhanged19fc32017-10-19 13:06:22 -07001097 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001098
1099 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1100 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1101 }
1102
Paul Duffin414ea5e2021-03-22 17:31:52 +00001103 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001104 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1105 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001106 }
1107
Colin Cross1ee23172017-10-18 14:44:18 -07001108 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001109 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1110 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001111 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001112
1113 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1114 if atestOptimize.Output == nil {
1115 t.Errorf("atestOptimize should optimize APK")
1116 }
1117
1118 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1119 if atestNoOptimize.Output == nil {
1120 t.Errorf("atestNoOptimize should not optimize APK")
1121 }
1122
1123 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1124 if atestDefault.Output == nil {
1125 t.Errorf("atestDefault should optimize APK")
1126 }
Colin Cross89536d42017-07-07 14:35:50 -07001127}
1128
Colin Cross0f37af02017-09-27 17:42:05 -07001129func TestResources(t *testing.T) {
1130 var table = []struct {
1131 name string
1132 prop string
1133 extra string
1134 args string
1135 }{
1136 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001137 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001138 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001139 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001140 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001141 },
1142 {
1143 // Test that a module with java_resources includes the files
1144 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001145 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1146 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001147 },
1148 {
1149 // Test that a module with a filegroup in java_resources includes the files with the
1150 // path prefix
1151 name: "resource filegroup",
1152 prop: `java_resources: [":foo-res"]`,
1153 extra: `
1154 filegroup {
1155 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001156 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001157 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001158 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001159 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001160 },
1161 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001162 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1163 name: "wildcard dirs",
1164 prop: `java_resource_dirs: ["java-res/*"]`,
1165 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1166 },
1167 {
1168 // Test that a module exclude_java_resource_dirs excludes the files
1169 name: "wildcard dirs",
1170 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1171 args: "-C java-res/a -f java-res/a/a",
1172 },
Colin Crosscedd4762018-09-13 11:26:19 -07001173 {
1174 // Test wildcards in java_resources
1175 name: "wildcard files",
1176 prop: `java_resources: ["java-res/**/*"]`,
1177 args: "-C . -f java-res/a/a -f java-res/b/b",
1178 },
1179 {
1180 // Test exclude_java_resources with java_resources
1181 name: "wildcard files with exclude",
1182 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1183 args: "-C . -f java-res/a/a",
1184 },
1185 {
1186 // Test exclude_java_resources with java_resource_dirs
1187 name: "resource dirs with exclude files",
1188 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1189 args: "-C java-res -f java-res/a/a",
1190 },
1191 {
1192 // Test exclude_java_resource_dirs with java_resource_dirs
1193 name: "resource dirs with exclude files",
1194 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1195 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1196 },
Colin Cross0f37af02017-09-27 17:42:05 -07001197 }
1198
1199 for _, test := range table {
1200 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001201 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001202 java_library {
1203 name: "foo",
1204 srcs: [
1205 "a.java",
1206 "b.java",
1207 "c.java",
1208 ],
1209 `+test.prop+`,
1210 }
Colin Cross238c1f32020-06-07 16:58:18 -07001211 `+test.extra,
1212 map[string][]byte{
1213 "java-res/a/a": nil,
1214 "java-res/b/b": nil,
1215 "java-res2/a": nil,
1216 },
1217 )
Colin Cross0f37af02017-09-27 17:42:05 -07001218
Colin Cross331a1212018-08-15 20:40:52 -07001219 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001220 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001221
1222 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1223 t.Errorf("foo combined jars %v does not contain %q",
1224 foo.Inputs.Strings(), fooRes.Output.String())
1225 }
1226
Colin Crossaf9c55b2017-10-03 14:50:08 -07001227 if fooRes.Args["jarArgs"] != test.args {
1228 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001229 fooRes.Args["jarArgs"], test.args)
1230 }
1231 })
1232 }
1233}
1234
Colin Cross0c4ce212019-05-03 15:28:19 -07001235func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001236 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001237 java_library {
1238 name: "foo",
1239 srcs: [
1240 "a.java",
1241 "b.java",
1242 "c.java",
1243 ],
1244 include_srcs: true,
1245 }
1246
1247 java_library {
1248 name: "bar",
1249 srcs: [
1250 "a.java",
1251 "b.java",
1252 "c.java",
1253 ],
1254 java_resource_dirs: ["java-res"],
1255 include_srcs: true,
1256 }
Colin Cross238c1f32020-06-07 16:58:18 -07001257 `, map[string][]byte{
1258 "java-res/a/a": nil,
1259 "java-res/b/b": nil,
1260 "java-res2/a": nil,
1261 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001262
1263 // Test a library with include_srcs: true
1264 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1265 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1266
1267 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1268 t.Errorf("foo combined jars %v does not contain %q", w, g)
1269 }
1270
1271 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1272 t.Errorf("foo source jar args %q is not %q", w, g)
1273 }
1274
1275 // Test a library with include_srcs: true and resources
1276 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1277 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1278 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1279 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1280
1281 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1282 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1283 }
1284
1285 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1286 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1287 }
1288
1289 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1290 t.Errorf("bar combined jars %v does not contain %q", w, g)
1291 }
1292
1293 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1294 t.Errorf("bar source jar args %q is not %q", w, g)
1295 }
1296
1297 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1298 t.Errorf("bar resource jar args %q is not %q", w, g)
1299 }
1300}
1301
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001302func TestJavaLint(t *testing.T) {
1303 ctx, _ := testJavaWithFS(t, `
1304 java_library {
1305 name: "foo",
1306 srcs: [
1307 "a.java",
1308 "b.java",
1309 "c.java",
1310 ],
1311 min_sdk_version: "29",
1312 sdk_version: "system_current",
1313 }
1314 `, map[string][]byte{
1315 "lint-baseline.xml": nil,
1316 })
1317
1318 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001319
Colin Cross1661aff2021-03-12 17:56:51 -08001320 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1321 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001322 t.Error("did not pass --baseline flag")
1323 }
1324}
1325
1326func TestJavaLintWithoutBaseline(t *testing.T) {
1327 ctx, _ := testJavaWithFS(t, `
1328 java_library {
1329 name: "foo",
1330 srcs: [
1331 "a.java",
1332 "b.java",
1333 "c.java",
1334 ],
1335 min_sdk_version: "29",
1336 sdk_version: "system_current",
1337 }
1338 `, map[string][]byte{})
1339
1340 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001341
Colin Cross1661aff2021-03-12 17:56:51 -08001342 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1343 if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001344 t.Error("passed --baseline flag for non existent file")
1345 }
1346}
1347
1348func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001349 config := TestConfig(t.TempDir(),
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001350 nil,
1351 `
1352 java_library {
1353 name: "foo",
1354 srcs: [
1355 ],
1356 min_sdk_version: "29",
1357 sdk_version: "system_current",
1358 lint: {
1359 baseline_filename: "mybaseline.xml",
1360 },
1361 }
1362 `, map[string][]byte{
1363 "build/soong/java/lint_defaults.txt": nil,
1364 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1365 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1366 "framework/aidl": nil,
1367 "a.java": nil,
1368 "AndroidManifest.xml": nil,
1369 "build/make/target/product/security": nil,
1370 })
1371 config.TestAllowNonExistentPaths = false
1372 testJavaErrorWithConfig(t,
1373 "source path \"mybaseline.xml\" does not exist",
1374 config,
1375 )
1376}
1377
1378func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1379 ctx, _ := testJavaWithFS(t, `
1380 java_library {
1381 name: "foo",
1382 srcs: [
1383 "a.java",
1384 "b.java",
1385 "c.java",
1386 ],
1387 min_sdk_version: "29",
1388 sdk_version: "system_current",
1389 lint: {
1390 error_checks: ["SomeCheck"],
1391 baseline_filename: "mybaseline.xml",
1392 },
1393 }
1394 `, map[string][]byte{
1395 "mybaseline.xml": nil,
1396 })
1397
1398 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001399
Colin Cross1661aff2021-03-12 17:56:51 -08001400 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1401 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001402 t.Error("did not use the correct file for baseline")
1403 }
1404}
1405
Colin Cross54190b32017-10-09 15:34:10 -07001406func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001407 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001408 java_library {
1409 name: "foo",
1410 srcs: [
1411 "a*.java",
1412 ":gen",
1413 "b*.java",
1414 ],
1415 }
1416
1417 genrule {
1418 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001419 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001420 out: ["gen.java"],
1421 }
Colin Cross238c1f32020-06-07 16:58:18 -07001422 `, map[string][]byte{
1423 "a.java": nil,
1424 "b.java": nil,
1425 })
Colin Cross54190b32017-10-09 15:34:10 -07001426
1427 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1428 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1429
Colin Cross15e86d92017-10-20 15:07:08 -07001430 if filepath.Base(genrule.Output.String()) != "gen.java" {
1431 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001432 }
1433
1434 if len(javac.Inputs) != 3 ||
1435 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001436 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001437 javac.Inputs[2].String() != "b.java" {
1438 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1439 }
1440}
1441
Nan Zhang61eaedb2017-11-02 13:28:15 -07001442func TestTurbine(t *testing.T) {
Paul Duffin3d9f2682021-03-13 09:47:16 +00001443 result := javaFixtureFactory.
1444 Extend(FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
1445 RunTestWithBp(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001446 java_library {
1447 name: "foo",
1448 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001449 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001450 }
1451
1452 java_library {
1453 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001454 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001455 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001456 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001457 }
1458
1459 java_library {
1460 name: "baz",
1461 srcs: ["c.java"],
1462 libs: ["bar"],
1463 sdk_version: "14",
1464 }
1465 `)
1466
Paul Duffin414ea5e2021-03-22 17:31:52 +00001467 fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine").RelativeToTop()
1468 barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine").RelativeToTop()
1469 barJavac := result.ModuleForTests("bar", "android_common").Rule("javac").RelativeToTop()
1470 barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine").RelativeToTop()
1471 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac").RelativeToTop()
Nan Zhang61eaedb2017-11-02 13:28:15 -07001472
Paul Duffin414ea5e2021-03-22 17:31:52 +00001473 android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001474
Paul Duffin414ea5e2021-03-22 17:31:52 +00001475 fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1476 barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar")
Paul Duffin3d9f2682021-03-13 09:47:16 +00001477 android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
1478 android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
Paul Duffin414ea5e2021-03-22 17:31:52 +00001479 android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs)
Paul Duffin3d9f2682021-03-13 09:47:16 +00001480 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001481}
1482
1483func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001484 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001485 java_library {
1486 name: "bar",
1487 srcs: ["a.java","b.java","c.java"],
1488 javac_shard_size: 1
1489 }
1490 `)
1491
Paul Duffin414ea5e2021-03-22 17:31:52 +00001492 barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001493 for i := 0; i < 3; i++ {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001494 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)).RelativeToTop()
Nan Zhang61eaedb2017-11-02 13:28:15 -07001495 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1496 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1497 }
1498 }
1499}
1500
Nan Zhang581fd212018-01-10 16:06:12 -08001501func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001502 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001503 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001504 name: "droiddoc-templates-sdk",
1505 path: ".",
1506 }
Jiyong Park29074592019-07-07 16:27:47 +09001507 filegroup {
1508 name: "bar-doc-aidl-srcs",
1509 srcs: ["bar-doc/IBar.aidl"],
1510 path: "bar-doc",
1511 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001512 droidstubs {
1513 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001514 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001515 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001516 ],
Steve Kim3666c702020-09-01 17:58:01 +00001517 exclude_srcs: [
1518 "bar-doc/b.java"
1519 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001520 api_levels_annotations_dirs: [
1521 "droiddoc-templates-sdk",
1522 ],
1523 api_levels_annotations_enabled: true,
1524 }
1525 droiddoc {
1526 name: "bar-doc",
1527 srcs: [
1528 ":bar-stubs",
1529 "bar-doc/IFoo.aidl",
1530 ":bar-doc-aidl-srcs",
1531 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001532 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001533 hdf: [
1534 "android.whichdoc offline",
1535 ],
1536 knowntags: [
1537 "bar-doc/known_oj_tags.txt",
1538 ],
1539 proofread_file: "libcore-proofread.txt",
1540 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001541 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001542 }
Colin Cross238c1f32020-06-07 16:58:18 -07001543 `,
1544 map[string][]byte{
1545 "bar-doc/a.java": nil,
1546 "bar-doc/b.java": nil,
1547 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001548 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1549 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1550 if err != nil {
1551 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1552 }
1553 if len(barStubsOutputs) != 1 {
1554 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001555 }
Nan Zhang581fd212018-01-10 16:06:12 -08001556
Liz Kammere1ab2502020-09-10 15:29:25 +00001557 barStubsOutput := barStubsOutputs[0]
1558 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
Paul Duffin414ea5e2021-03-22 17:31:52 +00001559 javaDoc := barDoc.Rule("javadoc").RelativeToTop()
Liz Kammere1ab2502020-09-10 15:29:25 +00001560 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1561 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001562 }
1563
Paul Duffin414ea5e2021-03-22 17:31:52 +00001564 expected := "-sourcepath out/soong/.intermediates/bar-doc/android_common/srcjars "
Liz Kammere1ab2502020-09-10 15:29:25 +00001565 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1566 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1567 }
1568
1569 aidl := barDoc.Rule("aidl")
1570 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001571 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1572 }
1573
1574 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1575 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001576 }
Nan Zhang581fd212018-01-10 16:06:12 -08001577}
1578
Liz Kammer585cac22020-07-06 09:12:57 -07001579func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1580 testJavaError(t, "flags is set. Cannot set args", `
1581 droiddoc_exported_dir {
1582 name: "droiddoc-templates-sdk",
1583 path: ".",
1584 }
1585 filegroup {
1586 name: "bar-doc-aidl-srcs",
1587 srcs: ["bar-doc/IBar.aidl"],
1588 path: "bar-doc",
1589 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001590 droidstubs {
1591 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001592 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001593 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001594 ],
Steve Kim3666c702020-09-01 17:58:01 +00001595 exclude_srcs: [
1596 "bar-doc/b.java"
1597 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001598 api_levels_annotations_dirs: [
1599 "droiddoc-templates-sdk",
1600 ],
1601 api_levels_annotations_enabled: true,
1602 }
1603 droiddoc {
1604 name: "bar-doc",
1605 srcs: [
1606 ":bar-stubs",
1607 "bar-doc/IFoo.aidl",
1608 ":bar-doc-aidl-srcs",
1609 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001610 custom_template: "droiddoc-templates-sdk",
1611 hdf: [
1612 "android.whichdoc offline",
1613 ],
1614 knowntags: [
1615 "bar-doc/known_oj_tags.txt",
1616 ],
1617 proofread_file: "libcore-proofread.txt",
1618 todo_file: "libcore-docs-todo.html",
1619 flags: ["-offlinemode -title \"libcore\""],
1620 args: "-offlinemode -title \"libcore\"",
1621 }
1622 `)
1623}
1624
Liz Kammer3d894b72020-08-04 09:55:13 -07001625func TestDroidstubs(t *testing.T) {
1626 ctx, _ := testJavaWithFS(t, `
1627 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001628 name: "droiddoc-templates-sdk",
1629 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001630 }
1631
1632 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001633 name: "bar-stubs",
1634 srcs: ["bar-doc/a.java"],
1635 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1636 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001637 }
1638
1639 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001640 name: "bar-stubs-other",
1641 srcs: ["bar-doc/a.java"],
1642 high_mem: true,
1643 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1644 api_levels_annotations_enabled: true,
1645 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001646 }
1647 `,
1648 map[string][]byte{
1649 "bar-doc/a.java": nil,
1650 })
1651 testcases := []struct {
1652 moduleName string
1653 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001654 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001655 }{
1656 {
1657 moduleName: "bar-stubs",
1658 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001659 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001660 },
1661 {
1662 moduleName: "bar-stubs-other",
1663 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001664 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001665 },
1666 }
1667 for _, c := range testcases {
1668 m := ctx.ModuleForTests(c.moduleName, "android_common")
1669 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001670 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001671 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001672 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001673 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1674 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001675
1676 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1677 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1678 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001679 }
1680}
1681
Paul Duffin83a2d962019-11-19 19:44:10 +00001682func TestDroidstubsWithSystemModules(t *testing.T) {
1683 ctx, _ := testJava(t, `
1684 droidstubs {
1685 name: "stubs-source-system-modules",
1686 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001687 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001688 ],
1689 sdk_version: "none",
1690 system_modules: "source-system-modules",
1691 }
1692
1693 java_library {
1694 name: "source-jar",
1695 srcs: [
1696 "a.java",
1697 ],
1698 }
1699
1700 java_system_modules {
1701 name: "source-system-modules",
1702 libs: ["source-jar"],
1703 }
1704
1705 droidstubs {
1706 name: "stubs-prebuilt-system-modules",
1707 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001708 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001709 ],
1710 sdk_version: "none",
1711 system_modules: "prebuilt-system-modules",
1712 }
1713
1714 java_import {
1715 name: "prebuilt-jar",
1716 jars: ["a.jar"],
1717 }
1718
1719 java_system_modules_import {
1720 name: "prebuilt-system-modules",
1721 libs: ["prebuilt-jar"],
1722 }
1723 `)
1724
1725 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1726
1727 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1728}
1729
1730func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1731 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1732 var systemJars []string
1733 for _, i := range metalavaRule.Implicits {
1734 systemJars = append(systemJars, i.Base())
1735 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001736 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001737 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1738 }
1739}
1740
Colin Cross54250902017-12-05 09:28:08 -08001741func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001742 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001743 java_library {
1744 name: "foo",
1745 srcs: ["a.java"],
1746 }
1747
1748 java_genrule {
1749 name: "jargen",
1750 tool_files: ["b.java"],
1751 cmd: "$(location b.java) $(in) $(out)",
1752 out: ["jargen.jar"],
1753 srcs: [":foo"],
1754 }
1755
1756 java_library {
1757 name: "bar",
1758 static_libs: ["jargen"],
1759 srcs: ["c.java"],
1760 }
1761
1762 java_library {
1763 name: "baz",
1764 libs: ["jargen"],
1765 srcs: ["c.java"],
1766 }
1767 `)
1768
1769 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1770 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1771 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1772 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1773 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1774
Colin Cross3d680512020-11-13 16:23:53 -08001775 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1776 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001777 }
1778
1779 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1780 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1781 }
1782
1783 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1784 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1785 }
1786
1787 if len(barCombined.Inputs) != 2 ||
1788 barCombined.Inputs[0].String() != bar.Output.String() ||
1789 barCombined.Inputs[1].String() != jargen.Output.String() {
1790 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1791 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1792 }
1793}
1794
Nan Zhang27e284d2018-02-09 21:03:53 +00001795func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001796 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001797 java_library {
1798 name: "foo",
1799 srcs: ["a.java", ":foo-srcs"],
1800 exclude_srcs: ["a.java", ":foo-excludes"],
1801 }
1802
1803 filegroup {
1804 name: "foo-srcs",
1805 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1806 }
1807
1808 filegroup {
1809 name: "foo-excludes",
1810 srcs: ["java-fg/a.java", "java-fg/b.java"],
1811 }
1812 `)
1813
1814 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1815
1816 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1817 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1818 }
1819}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001820
Paul Duffin52d398a2019-06-11 12:31:14 +01001821func TestJavaLibrary(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001822 testJavaWithFS(t, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001823 "libcore/Android.bp": []byte(`
1824 java_library {
1825 name: "core",
1826 sdk_version: "none",
1827 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001828 }
1829
1830 filegroup {
1831 name: "core-jar",
1832 srcs: [":core{.jar}"],
1833 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001834 `),
Paul Duffinaa55f742020-10-06 17:20:13 +01001835 })
Paul Duffinaa55f742020-10-06 17:20:13 +01001836}
1837
1838func TestJavaImport(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001839 testJavaWithFS(t, "", map[string][]byte{
Paul Duffinaa55f742020-10-06 17:20:13 +01001840 "libcore/Android.bp": []byte(`
1841 java_import {
1842 name: "core",
1843 sdk_version: "none",
1844 }
1845
1846 filegroup {
1847 name: "core-jar",
1848 srcs: [":core{.jar}"],
1849 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001850 `),
Paul Duffin52d398a2019-06-11 12:31:14 +01001851 })
Paul Duffin52d398a2019-06-11 12:31:14 +01001852}
1853
Jiyong Parkc678ad32018-04-10 13:07:10 +09001854func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001855 result := javaFixtureFactory.Extend(
1856 PrepareForTestWithJavaSdkLibraryFiles,
1857 FixtureWithPrebuiltApis(map[string][]string{
1858 "28": {"foo"},
1859 "29": {"foo"},
1860 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1861 }),
1862 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001863 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001864 name: "droiddoc-templates-sdk",
1865 path: ".",
1866 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001867 java_sdk_library {
1868 name: "foo",
1869 srcs: ["a.java", "b.java"],
1870 api_packages: ["foo"],
1871 }
1872 java_sdk_library {
1873 name: "bar",
1874 srcs: ["a.java", "b.java"],
1875 api_packages: ["bar"],
1876 }
1877 java_library {
1878 name: "baz",
1879 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001880 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001881 sdk_version: "system_current",
1882 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001883 java_sdk_library {
1884 name: "barney",
1885 srcs: ["c.java"],
1886 api_only: true,
1887 }
1888 java_sdk_library {
1889 name: "betty",
1890 srcs: ["c.java"],
1891 shared_library: false,
1892 }
Paul Duffin859fe962020-05-15 10:20:31 +01001893 java_sdk_library_import {
1894 name: "quuz",
1895 public: {
1896 jars: ["c.jar"],
1897 },
1898 }
1899 java_sdk_library_import {
1900 name: "fred",
1901 public: {
1902 jars: ["b.jar"],
1903 },
1904 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001905 java_sdk_library_import {
1906 name: "wilma",
1907 public: {
1908 jars: ["b.jar"],
1909 },
1910 shared_library: false,
1911 }
Jiyong Park1be96912018-05-28 18:02:19 +09001912 java_library {
1913 name: "qux",
1914 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001915 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001916 sdk_version: "system_current",
1917 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001918 java_library {
1919 name: "baz-test",
1920 srcs: ["c.java"],
1921 libs: ["foo"],
1922 sdk_version: "test_current",
1923 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001924 java_library {
1925 name: "baz-29",
1926 srcs: ["c.java"],
1927 libs: ["foo"],
1928 sdk_version: "system_29",
1929 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001930 java_library {
1931 name: "baz-module-30",
1932 srcs: ["c.java"],
1933 libs: ["foo"],
1934 sdk_version: "module_30",
1935 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001936 `)
1937
1938 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001939 result.ModuleForTests("foo", "android_common")
1940 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1941 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1942 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1943 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1944 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1945 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1946 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1947 result.ModuleForTests("foo.api.public.28", "")
1948 result.ModuleForTests("foo.api.system.28", "")
1949 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001950
Paul Duffin22b77cd2021-03-12 19:15:01 +00001951 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001952 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001953 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001954 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001955 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001956 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001957 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001958
Paul Duffin22b77cd2021-03-12 19:15:01 +00001959 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001960 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001961 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001962
Paul Duffin22b77cd2021-03-12 19:15:01 +00001963 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001964 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001965 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001966
Paul Duffin22b77cd2021-03-12 19:15:01 +00001967 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001968 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001969 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 +01001970
Jiyong Park1be96912018-05-28 18:02:19 +09001971 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001972 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001973 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001974 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001975 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001976 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001977}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001978
Anton Hansson7f66efa2020-10-08 14:47:23 +01001979func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001980 result := javaFixtureFactory.Extend(
1981 PrepareForTestWithJavaSdkLibraryFiles,
1982 FixtureWithLastReleaseApis("sdklib"),
1983 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001984 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001985 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001986 srcs: ["a.java"],
1987 impl_only_libs: ["foo"],
1988 stub_only_libs: ["bar"],
1989 }
1990 java_library {
1991 name: "foo",
1992 srcs: ["a.java"],
1993 sdk_version: "current",
1994 }
1995 java_library {
1996 name: "bar",
1997 srcs: ["a.java"],
1998 sdk_version: "current",
1999 }
2000 `)
2001
Anton Hanssondff2c782020-12-21 17:10:01 +00002002 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002003 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01002004 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
2005 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
2006 }
2007 }
Anton Hanssondff2c782020-12-21 17:10:01 +00002008 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00002009 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01002010 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
2011 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
2012 }
2013}
2014
Paul Duffindaaa3322020-05-26 18:13:57 +01002015func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002016 result := javaFixtureFactory.Extend(
2017 PrepareForTestWithJavaSdkLibraryFiles,
2018 FixtureWithLastReleaseApis("foo"),
2019 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01002020 java_sdk_library {
2021 name: "foo",
2022 srcs: ["a.java"],
2023 api_only: true,
2024 public: {
2025 enabled: true,
2026 },
2027 }
2028
2029 java_library {
2030 name: "bar",
2031 srcs: ["b.java"],
2032 libs: ["foo"],
2033 }
2034 `)
2035
2036 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002037 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01002038 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2039 t.Errorf("expected %q, found %#q", expected, actual)
2040 }
2041}
2042
Paul Duffin46dc45a2020-05-14 15:39:10 +01002043func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002044 javaFixtureFactory.Extend(
2045 PrepareForTestWithJavaSdkLibraryFiles,
2046 FixtureWithLastReleaseApis("foo"),
2047 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002048 java_sdk_library {
2049 name: "foo",
2050 srcs: ["a.java"],
2051 api_packages: ["foo"],
2052 public: {
2053 enabled: true,
2054 },
2055 }
2056
2057 java_library {
2058 name: "bar",
2059 srcs: ["b.java", ":foo{.public.stubs.source}"],
2060 }
2061 `)
2062}
2063
2064func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002065 javaFixtureFactory.
Paul Duffin163043d2021-03-12 19:18:49 +00002066 Extend(
2067 PrepareForTestWithJavaSdkLibraryFiles,
2068 FixtureWithLastReleaseApis("foo"),
2069 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00002070 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
2071 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002072 java_sdk_library {
2073 name: "foo",
2074 srcs: ["a.java"],
2075 api_packages: ["foo"],
2076 public: {
2077 enabled: true,
2078 },
2079 }
2080
2081 java_library {
2082 name: "bar",
2083 srcs: ["b.java", ":foo{.system.stubs.source}"],
2084 }
2085 `)
2086}
2087
Paul Duffinca8d9a52020-06-26 22:20:25 +01002088func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002089 result := javaFixtureFactory.Extend(
2090 PrepareForTestWithJavaSdkLibraryFiles,
2091 FixtureWithLastReleaseApis("sdklib"),
2092 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01002093 java_sdk_library {
2094 name: "sdklib",
2095 srcs: ["a.java"],
2096 sdk_version: "none",
2097 system_modules: "none",
2098 public: {
2099 enabled: true,
2100 },
2101 }
2102 `)
2103
Paul Duffin22b77cd2021-03-12 19:15:01 +00002104 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01002105 `dex2oatd`,
2106 `sdklib.impl`,
2107 `sdklib.stubs`,
2108 `sdklib.stubs.source`,
2109 `sdklib.xml`,
2110 })
2111}
2112
Paul Duffin46dc45a2020-05-14 15:39:10 +01002113func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002114 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002115 java_sdk_library_import {
2116 name: "foo",
2117 public: {
2118 jars: ["a.jar"],
2119 stub_srcs: ["a.java"],
2120 current_api: "api/current.txt",
2121 removed_api: "api/removed.txt",
2122 },
2123 }
2124
2125 java_library {
2126 name: "bar",
2127 srcs: [":foo{.public.stubs.source}"],
2128 java_resources: [
2129 ":foo{.public.api.txt}",
2130 ":foo{.public.removed-api.txt}",
2131 ],
2132 }
2133 `)
2134}
2135
2136func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2137 bp := `
2138 java_sdk_library_import {
2139 name: "foo",
2140 public: {
2141 jars: ["a.jar"],
2142 },
2143 }
2144 `
2145
2146 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002147 javaFixtureFactory.
2148 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
2149 RunTestWithBp(t, bp+`
2150 java_library {
2151 name: "bar",
2152 srcs: [":foo{.public.stubs.source}"],
2153 java_resources: [
2154 ":foo{.public.api.txt}",
2155 ":foo{.public.removed-api.txt}",
2156 ],
2157 }
2158 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002159 })
2160
2161 t.Run("api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002162 javaFixtureFactory.
2163 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
2164 RunTestWithBp(t, bp+`
2165 java_library {
2166 name: "bar",
2167 srcs: ["a.java"],
2168 java_resources: [
2169 ":foo{.public.api.txt}",
2170 ],
2171 }
2172 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002173 })
2174
2175 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002176 javaFixtureFactory.
2177 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
2178 RunTestWithBp(t, bp+`
2179 java_library {
2180 name: "bar",
2181 srcs: ["a.java"],
2182 java_resources: [
2183 ":foo{.public.removed-api.txt}",
2184 ],
2185 }
2186 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002187 })
2188}
2189
Paul Duffin3375e352020-04-28 10:44:03 +01002190func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002191 javaFixtureFactory.
2192 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
2193 RunTestWithBp(t, `
2194 java_sdk_library {
2195 name: "foo",
2196 srcs: ["a.java", "b.java"],
2197 api_packages: ["foo"],
2198 // Explicitly disable public to test the check that ensures the set of enabled
2199 // scopes is consistent.
2200 public: {
2201 enabled: false,
2202 },
2203 system: {
2204 enabled: true,
2205 },
2206 }
Paul Duffin3375e352020-04-28 10:44:03 +01002207 `)
2208}
2209
Paul Duffin87a05a32020-05-12 11:50:28 +01002210func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002211 javaFixtureFactory.Extend(
2212 PrepareForTestWithJavaSdkLibraryFiles,
2213 FixtureWithLastReleaseApis("foo"),
2214 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002215 java_sdk_library {
2216 name: "foo",
2217 srcs: ["a.java", "b.java"],
2218 api_packages: ["foo"],
2219 system: {
2220 enabled: true,
2221 sdk_version: "module_current",
2222 },
2223 }
2224 `)
2225}
2226
Paul Duffin0c5bae52020-06-02 13:00:08 +01002227func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002228 javaFixtureFactory.Extend(
2229 PrepareForTestWithJavaSdkLibraryFiles,
2230 FixtureWithLastReleaseApis("foo"),
2231 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002232 java_sdk_library {
2233 name: "foo",
2234 srcs: ["a.java", "b.java"],
2235 api_packages: ["foo"],
2236 system: {
2237 enabled: true,
2238 },
2239 module_lib: {
2240 enabled: true,
2241 },
2242 }
2243 `)
2244}
2245
2246func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002247 javaFixtureFactory.Extend(
2248 PrepareForTestWithJavaSdkLibraryFiles,
2249 FixtureWithLastReleaseApis("foo"),
2250 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002251 java_sdk_library {
2252 name: "foo",
2253 srcs: ["a.java", "b.java"],
2254 api_packages: ["foo"],
2255 system: {
2256 enabled: true,
2257 },
2258 system_server: {
2259 enabled: true,
2260 },
2261 }
2262 `)
2263}
2264
Paul Duffin803a9562020-05-20 11:52:25 +01002265func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002266 javaFixtureFactory.
2267 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2268 RunTestWithBp(t, `
2269 java_sdk_library {
2270 name: "foo",
2271 srcs: ["a.java"],
2272 public: {
2273 enabled: false,
2274 },
2275 }
Paul Duffin803a9562020-05-20 11:52:25 +01002276
Paul Duffin22b77cd2021-03-12 19:15:01 +00002277 java_library {
2278 name: "baz",
2279 srcs: ["a.java"],
2280 libs: ["foo"],
2281 sdk_version: "module_current",
2282 }
Paul Duffin803a9562020-05-20 11:52:25 +01002283 `)
2284}
2285
2286func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002287 javaFixtureFactory.Extend(
2288 PrepareForTestWithJavaSdkLibraryFiles,
2289 FixtureWithLastReleaseApis("foo"),
2290 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002291 java_sdk_library {
2292 name: "foo",
2293 srcs: ["a.java"],
2294 system: {
2295 enabled: true,
2296 },
2297 }
2298
2299 java_library {
2300 name: "baz",
2301 srcs: ["a.java"],
2302 libs: ["foo"],
2303 // foo does not have module-lib scope so it should fallback to system
2304 sdk_version: "module_current",
2305 }
2306 `)
2307}
2308
Jiyong Park932cdfe2020-05-28 00:19:53 +09002309func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002310 result := javaFixtureFactory.Extend(
2311 PrepareForTestWithJavaSdkLibraryFiles,
2312 FixtureWithLastReleaseApis("foo"),
2313 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002314 java_sdk_library {
2315 name: "foo",
2316 srcs: ["a.java"],
2317 system: {
2318 enabled: true,
2319 },
2320 default_to_stubs: true,
2321 }
2322
2323 java_library {
2324 name: "baz",
2325 srcs: ["a.java"],
2326 libs: ["foo"],
2327 // does not have sdk_version set, should fallback to module,
2328 // which will then fallback to system because the module scope
2329 // is not enabled.
2330 }
2331 `)
2332 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002333 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002334 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2335 t.Errorf("expected %q, found %#q", expected, actual)
2336 }
2337}
2338
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002339var compilerFlagsTestCases = []struct {
2340 in string
2341 out bool
2342}{
2343 {
2344 in: "a",
2345 out: false,
2346 },
2347 {
2348 in: "-a",
2349 out: true,
2350 },
2351 {
2352 in: "-no-jdk",
2353 out: false,
2354 },
2355 {
2356 in: "-no-stdlib",
2357 out: false,
2358 },
2359 {
2360 in: "-kotlin-home",
2361 out: false,
2362 },
2363 {
2364 in: "-kotlin-home /some/path",
2365 out: false,
2366 },
2367 {
2368 in: "-include-runtime",
2369 out: false,
2370 },
2371 {
2372 in: "-Xintellij-plugin-root",
2373 out: false,
2374 },
2375}
2376
2377type mockContext struct {
2378 android.ModuleContext
2379 result bool
2380}
2381
2382func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2383 // CheckBadCompilerFlags calls this function when the flag should be rejected
2384 ctx.result = false
2385}
2386
2387func TestCompilerFlags(t *testing.T) {
2388 for _, testCase := range compilerFlagsTestCases {
2389 ctx := &mockContext{result: true}
2390 CheckKotlincFlags(ctx, []string{testCase.in})
2391 if ctx.result != testCase.out {
2392 t.Errorf("incorrect output:")
2393 t.Errorf(" input: %#v", testCase.in)
2394 t.Errorf(" expected: %#v", testCase.out)
2395 t.Errorf(" got: %#v", ctx.result)
2396 }
2397 }
2398}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002399
2400// TODO(jungjw): Consider making this more robust by ignoring path order.
2401func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00002402 variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop()
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002403 flags := strings.Split(variables["javacFlags"], " ")
2404 got := ""
2405 for _, flag := range flags {
2406 keyEnd := strings.Index(flag, "=")
2407 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2408 got = flag[keyEnd+1:]
2409 break
2410 }
2411 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00002412 if expected != android.StringPathRelativeToTop(ctx.Config().BuildDir(), got) {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002413 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2414 }
2415}
2416
2417func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002418 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002419 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002420 bp := `
2421 java_library {
2422 name: "foo",
2423 srcs: ["a.java"],
2424 java_version: "1.8",
2425 }
2426
2427 java_library {
2428 name: "bar",
2429 srcs: ["b.java"],
2430 sdk_version: "none",
2431 system_modules: "none",
2432 patch_module: "java.base",
2433 java_version: "1.8",
2434 }
2435
2436 java_library {
2437 name: "baz",
2438 srcs: ["c.java"],
2439 patch_module: "java.base",
2440 java_version: "1.8",
2441 }
2442 `
2443 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002444
2445 checkPatchModuleFlag(t, ctx, "foo", "")
2446 checkPatchModuleFlag(t, ctx, "bar", "")
2447 checkPatchModuleFlag(t, ctx, "baz", "")
2448 })
2449
Pete Gillin0c2143e2019-05-02 15:32:11 +01002450 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002451 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002452 bp := `
2453 java_library {
2454 name: "foo",
2455 srcs: ["a.java"],
2456 }
2457
2458 java_library {
2459 name: "bar",
2460 srcs: ["b.java"],
2461 sdk_version: "none",
2462 system_modules: "none",
2463 patch_module: "java.base",
2464 }
2465
2466 java_library {
2467 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002468 srcs: [
2469 "c.java",
2470 // Tests for b/150878007
2471 "dir/d.java",
2472 "dir2/e.java",
2473 "dir2/f.java",
2474 "nested/dir/g.java"
2475 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002476 patch_module: "java.base",
2477 }
2478 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002479 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002480
2481 checkPatchModuleFlag(t, ctx, "foo", "")
Paul Duffin414ea5e2021-03-22 17:31:52 +00002482 expected := "java.base=.:out/soong"
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002483 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002484 expected = "java.base=" + strings.Join([]string{
Paul Duffin414ea5e2021-03-22 17:31:52 +00002485 ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002486 checkPatchModuleFlag(t, ctx, "baz", expected)
2487 })
2488}
Paul Duffina7b9f422020-01-10 17:12:18 +00002489
Paul Duffin83a2d962019-11-19 19:44:10 +00002490func TestJavaLibraryWithSystemModules(t *testing.T) {
2491 ctx, _ := testJava(t, `
2492 java_library {
2493 name: "lib-with-source-system-modules",
2494 srcs: [
2495 "a.java",
2496 ],
2497 sdk_version: "none",
2498 system_modules: "source-system-modules",
2499 }
2500
2501 java_library {
2502 name: "source-jar",
2503 srcs: [
2504 "a.java",
2505 ],
2506 }
2507
2508 java_system_modules {
2509 name: "source-system-modules",
2510 libs: ["source-jar"],
2511 }
2512
2513 java_library {
2514 name: "lib-with-prebuilt-system-modules",
2515 srcs: [
2516 "a.java",
2517 ],
2518 sdk_version: "none",
2519 system_modules: "prebuilt-system-modules",
2520 }
2521
2522 java_import {
2523 name: "prebuilt-jar",
2524 jars: ["a.jar"],
2525 }
2526
2527 java_system_modules_import {
2528 name: "prebuilt-system-modules",
2529 libs: ["prebuilt-jar"],
2530 }
2531 `)
2532
2533 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2534
2535 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2536}
2537
2538func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2539 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2540 bootClasspath := javacRule.Args["bootClasspath"]
2541 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2542 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2543 }
2544}
Jiyong Park19604de2020-03-24 16:44:11 +09002545
2546func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2547 ctx, _ := testJava(t, `
2548 java_library {
2549 name: "foo",
2550 srcs: ["aidl/foo/IFoo.aidl"],
2551 libs: ["bar"],
2552 }
2553
2554 java_import {
2555 name: "bar",
2556 jars: ["a.jar"],
2557 aidl: {
2558 export_include_dirs: ["aidl/bar"],
2559 },
2560 }
2561 `)
2562
2563 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2564 expectedAidlFlag := "-Iaidl/bar"
2565 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2566 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2567 }
2568}
Liz Kammerdd849a82020-06-12 16:38:45 -07002569
Jooyung Hane197d8b2021-01-05 10:33:16 +09002570func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2571 ctx, _ := testJava(t, `
2572 java_library {
2573 name: "foo",
2574 srcs: ["aidl/foo/IFoo.aidl"],
2575 aidl: { flags: ["-Werror"], },
2576 }
2577 `)
2578
2579 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2580 expectedAidlFlag := "-Werror"
2581 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2582 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2583 }
2584}
2585
Liz Kammerdd849a82020-06-12 16:38:45 -07002586func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002587 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002588 java_test_host {
2589 name: "foo",
2590 srcs: ["a.java"],
2591 data_native_bins: ["bin"]
2592 }
2593
2594 python_binary_host {
2595 name: "bin",
2596 srcs: ["bin.py"],
2597 }
2598 `)
2599
2600 buildOS := android.BuildOs.String()
2601
2602 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002603 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002604 expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
Liz Kammerdd849a82020-06-12 16:38:45 -07002605 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002606 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual)
Liz Kammerdd849a82020-06-12 16:38:45 -07002607}
Yuexi Ma627263f2021-03-04 13:47:56 -08002608
2609func TestDefaultInstallable(t *testing.T) {
2610 ctx, _ := testJava(t, `
2611 java_test_host {
2612 name: "foo"
2613 }
2614 `)
2615
2616 buildOS := android.BuildOs.String()
2617 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2618 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2619 module.properties.Installable)
2620}