blob: 24911b461b7c63510f246d931ef672c0f1f84088 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Paul Duffin1a393322020-11-18 16:36:47 +000018 "fmt"
Colin Cross72bb3632017-07-13 16:23:21 -070019 "io/ioutil"
20 "os"
21 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070022 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010023 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Paul Duffind6ceb862021-03-04 23:02:31 +000028 "android/soong/genrule"
Jeongik Cha28df2572019-11-11 10:46:36 +090029 "github.com/google/blueprint/proptools"
30
Colin Crossa4f08812018-10-02 22:03:40 -070031 "android/soong/android"
32 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080033 "android/soong/dexpreopt"
Liz Kammerdd849a82020-06-12 16:38:45 -070034 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070035)
36
37var buildDir string
38
39func setUp() {
40 var err error
41 buildDir, err = ioutil.TempDir("", "soong_java_test")
42 if err != nil {
43 panic(err)
44 }
45}
46
47func tearDown() {
48 os.RemoveAll(buildDir)
49}
50
Paul Duffin95bdab42021-03-08 21:48:46 +000051// Factory to use to create fixtures for tests in this package.
52var javaFixtureFactory = android.NewFixtureFactory(
53 &buildDir,
54 genrule.PrepareForTestWithGenRuleBuildComponents,
55 // Get the CC build components but not default modules.
56 cc.PrepareForTestWithCcBuildComponents,
57 // Include all the default java modules.
58 PrepareForTestWithJavaDefaultModules,
Paul Duffin220ddd72021-03-17 23:54:30 +000059 python.PrepareForTestWithPythonBuildComponents,
Paul Duffin95bdab42021-03-08 21:48:46 +000060 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
61 ctx.RegisterModuleType("java_plugin", PluginFactory)
Paul Duffin95bdab42021-03-08 21:48:46 +000062
Paul Duffin95bdab42021-03-08 21:48:46 +000063 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
64 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
65 }),
66 javaMockFS().AddToFixture(),
Paul Duffinbf028b52021-03-13 22:19:17 +000067 PrepareForTestWithJavaSdkLibraryFiles,
Paul Duffin95bdab42021-03-08 21:48:46 +000068 dexpreopt.PrepareForTestWithDexpreopt,
69)
70
Colin Cross72bb3632017-07-13 16:23:21 -070071func TestMain(m *testing.M) {
72 run := func() int {
73 setUp()
74 defer tearDown()
75
76 return m.Run()
77 }
78
79 os.Exit(run())
80}
Colin Cross527012a2017-11-30 22:56:16 -080081
Paul Duffin95bdab42021-03-08 21:48:46 +000082// testConfig is a legacy way of creating a test Config for testing java modules.
83//
84// See testJava for an explanation as to how to stop using this deprecated method.
85//
86// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080087func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Paul Duffin9f045242021-01-21 15:05:11 +000088 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -070089}
90
Paul Duffin95bdab42021-03-08 21:48:46 +000091// testContext is a legacy way of creating a TestContext for testing java modules.
92//
93// See testJava for an explanation as to how to stop using this deprecated method.
94//
95// deprecated
Colin Crossae8600b2020-10-29 17:09:13 -070096func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070097
Colin Crossae8600b2020-10-29 17:09:13 -070098 ctx := android.NewTestArchContext(config)
Paul Duffinc059c8c2021-01-20 17:13:52 +000099 RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -0800100 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -0800101 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Colin Cross89536d42017-07-07 14:35:50 -0700102 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100103 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +0000104
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700105 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Paul Duffineafc16b2021-02-24 01:43:18 +0000106 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
107 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
Colin Crossa4f08812018-10-02 22:03:40 -0700108
Paul Duffin021f4e52020-07-30 16:04:17 +0100109 android.RegisterPrebuiltMutators(ctx)
110
Paul Duffind6ceb862021-03-04 23:02:31 +0000111 genrule.RegisterGenruleBuildComponents(ctx)
112
Colin Crossa4f08812018-10-02 22:03:40 -0700113 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000114 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700115
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700116 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
117 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
118 })
119
Colin Cross527012a2017-11-30 22:56:16 -0800120 return ctx
121}
122
Paul Duffin95bdab42021-03-08 21:48:46 +0000123// run is a legacy way of running tests of java modules.
124//
125// See testJava for an explanation as to how to stop using this deprecated method.
126//
127// deprecated
Colin Cross527012a2017-11-30 22:56:16 -0800128func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800129 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800130
Colin Cross98be1bb2019-12-13 20:41:13 -0800131 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000132 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800133
Colin Crossae8600b2020-10-29 17:09:13 -0700134 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100135 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800136 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700137 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800138 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800139}
140
Paul Duffin95bdab42021-03-08 21:48:46 +0000141// testJavaError is a legacy way of running tests of java modules that expect errors.
142//
143// See testJava for an explanation as to how to stop using this deprecated method.
144//
145// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900146func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900147 t.Helper()
Paul Duffin6bac49c2021-03-12 21:28:15 +0000148 result := javaFixtureFactory.
149 Extend(dexpreopt.PrepareForTestWithDexpreopt).
150 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
151 RunTestWithBp(t, bp)
152 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900153}
154
Paul Duffin95bdab42021-03-08 21:48:46 +0000155// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
156//
157// See testJava for an explanation as to how to stop using this deprecated method.
158//
159// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800160func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900161 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000162 // This must be done on the supplied config and not as part of the fixture because any changes to
163 // the fixture's config will be ignored when RunTestWithConfig replaces it.
Colin Cross98be1bb2019-12-13 20:41:13 -0800164 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000165 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Paul Duffin95bdab42021-03-08 21:48:46 +0000166 result := javaFixtureFactory.
167 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
168 RunTestWithConfig(t, config)
169 return result.TestContext, result.Config
Paul Duffinec0fe172021-02-25 15:34:13 +0000170}
171
Paul Duffin95bdab42021-03-08 21:48:46 +0000172// runWithErrors is a legacy way of running tests of java modules that expect errors.
173//
174// See testJava for an explanation as to how to stop using this deprecated method.
175//
176// deprecated
Paul Duffinec0fe172021-02-25 15:34:13 +0000177func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) {
Colin Crossae8600b2020-10-29 17:09:13 -0700178 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900179 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
180 if len(errs) > 0 {
181 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000182 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900183 }
184 _, errs = ctx.PrepareBuildActions(config)
185 if len(errs) > 0 {
186 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000187 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900188 }
189
190 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Paul Duffinec0fe172021-02-25 15:34:13 +0000191 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900192}
193
Paul Duffin95bdab42021-03-08 21:48:46 +0000194// testJavaWithFS runs tests using the javaFixtureFactory
195//
196// See testJava for an explanation as to how to stop using this deprecated method.
197//
198// deprecated
199func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -0700200 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000201 result := javaFixtureFactory.Extend(fs.AddToFixture()).RunTestWithBp(t, bp)
202 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -0700203}
204
Paul Duffin95bdab42021-03-08 21:48:46 +0000205// testJava runs tests using the javaFixtureFactory
206//
207// Do not add any new usages of this, instead use the javaFixtureFactory directly as it makes it
208// much easier to customize the test behavior.
209//
210// If it is necessary to customize the behavior of an existing test that uses this then please first
211// convert the test to using javaFixtureFactory first and then in a following change add the
212// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
213// that it did not change the test behavior unexpectedly.
214//
215// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700216func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800217 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000218 result := javaFixtureFactory.RunTestWithBp(t, bp)
219 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900220}
221
Paul Duffin95bdab42021-03-08 21:48:46 +0000222// testJavaWithConfig runs tests using the javaFixtureFactory
223//
224// See testJava for an explanation as to how to stop using this deprecated method.
225//
226// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800227func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900228 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000229 result := javaFixtureFactory.RunTestWithConfig(t, config)
230 return result.TestContext, result.Config
Colin Cross72bb3632017-07-13 16:23:21 -0700231}
232
Colin Cross2acdae82017-09-15 19:44:24 -0700233func moduleToPath(name string) string {
234 switch {
235 case name == `""`:
236 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700237 case strings.HasSuffix(name, ".jar"):
238 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700239 default:
240 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700241 }
242}
243
Paul Duffin95bdab42021-03-08 21:48:46 +0000244// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
245// is defined in PrepareForIntegrationTestWithJava
246func defaultModuleToPath(name string) string {
247 return filepath.Join(buildDir, ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
248}
249
Jeongik Chae403e9e2019-12-07 00:16:24 +0900250func TestJavaLinkType(t *testing.T) {
251 testJava(t, `
252 java_library {
253 name: "foo",
254 srcs: ["a.java"],
255 libs: ["bar"],
256 static_libs: ["baz"],
257 }
258
259 java_library {
260 name: "bar",
261 sdk_version: "current",
262 srcs: ["b.java"],
263 }
264
265 java_library {
266 name: "baz",
267 sdk_version: "system_current",
268 srcs: ["c.java"],
269 }
270 `)
271
Steven Moreland00298982020-11-17 21:44:36 +0000272 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900273 java_library {
274 name: "foo",
275 srcs: ["a.java"],
276 libs: ["bar"],
277 sdk_version: "current",
278 static_libs: ["baz"],
279 }
280
281 java_library {
282 name: "bar",
283 sdk_version: "current",
284 srcs: ["b.java"],
285 }
286
287 java_library {
288 name: "baz",
289 sdk_version: "system_current",
290 srcs: ["c.java"],
291 }
292 `)
293
294 testJava(t, `
295 java_library {
296 name: "foo",
297 srcs: ["a.java"],
298 libs: ["bar"],
299 sdk_version: "system_current",
300 static_libs: ["baz"],
301 }
302
303 java_library {
304 name: "bar",
305 sdk_version: "current",
306 srcs: ["b.java"],
307 }
308
309 java_library {
310 name: "baz",
311 sdk_version: "system_current",
312 srcs: ["c.java"],
313 }
314 `)
315
Steven Moreland00298982020-11-17 21:44:36 +0000316 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900317 java_library {
318 name: "foo",
319 srcs: ["a.java"],
320 libs: ["bar"],
321 sdk_version: "system_current",
322 static_libs: ["baz"],
323 }
324
325 java_library {
326 name: "bar",
327 sdk_version: "current",
328 srcs: ["b.java"],
329 }
330
331 java_library {
332 name: "baz",
333 srcs: ["c.java"],
334 }
335 `)
336}
337
Colin Cross72bb3632017-07-13 16:23:21 -0700338func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700339 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700340 java_library {
341 name: "foo",
342 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700343 libs: ["bar"],
344 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700345 }
346
347 java_library {
348 name: "bar",
349 srcs: ["b.java"],
350 }
351
352 java_library {
353 name: "baz",
354 srcs: ["c.java"],
355 }
Colin Crossd5934c82017-10-02 13:55:26 -0700356 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700357
Colin Cross4c428df2017-09-15 17:36:05 -0700358 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700359 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700360
361 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
362 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
363 }
364
Colin Cross1ee23172017-10-18 14:44:18 -0700365 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700366 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
367 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700368
Paul Duffin22b77cd2021-03-12 19:15:01 +0000369 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700370
Paul Duffin22b77cd2021-03-12 19:15:01 +0000371 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700372
373 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
374 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700375 }
376}
377
Artur Satayev9cf46692019-11-26 18:08:34 +0000378func TestExportedPlugins(t *testing.T) {
379 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800380 library string
381 processors string
382 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000383 }
384 var tests = []struct {
385 name string
386 extra string
387 results []Result
388 }{
389 {
390 name: "Exported plugin is not a direct plugin",
391 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
392 results: []Result{{library: "exports", processors: "-proc:none"}},
393 },
394 {
395 name: "Exports plugin to dependee",
396 extra: `
397 java_library{name: "exports", exported_plugins: ["plugin"]}
398 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
399 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
400 `,
401 results: []Result{
402 {library: "foo", processors: "-processor com.android.TestPlugin"},
403 {library: "bar", processors: "-processor com.android.TestPlugin"},
404 },
405 },
406 {
407 name: "Exports plugin to android_library",
408 extra: `
409 java_library{name: "exports", exported_plugins: ["plugin"]}
410 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
411 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
412 `,
413 results: []Result{
414 {library: "foo", processors: "-processor com.android.TestPlugin"},
415 {library: "bar", processors: "-processor com.android.TestPlugin"},
416 },
417 },
418 {
419 name: "Exports plugin is not propagated via transitive deps",
420 extra: `
421 java_library{name: "exports", exported_plugins: ["plugin"]}
422 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
423 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
424 `,
425 results: []Result{
426 {library: "foo", processors: "-processor com.android.TestPlugin"},
427 {library: "bar", processors: "-proc:none"},
428 },
429 },
430 {
431 name: "Exports plugin appends to plugins",
432 extra: `
433 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
434 java_library{name: "exports", exported_plugins: ["plugin"]}
435 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
436 `,
437 results: []Result{
438 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
439 },
440 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800441 {
442 name: "Exports plugin to with generates_api to dependee",
443 extra: `
444 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
445 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
446 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
447 `,
448 results: []Result{
449 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
450 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
451 },
452 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000453 }
454
455 for _, test := range tests {
456 t.Run(test.name, func(t *testing.T) {
457 ctx, _ := testJava(t, `
458 java_plugin {
459 name: "plugin",
460 processor_class: "com.android.TestPlugin",
461 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800462 java_plugin {
463 name: "plugin_generates_api",
464 generates_api: true,
465 processor_class: "com.android.TestPlugin",
466 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000467 `+test.extra)
468
469 for _, want := range test.results {
470 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
471 if javac.Args["processor"] != want.processors {
472 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
473 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800474 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
475 disableTurbine := turbine.BuildParams.Rule == nil
476 if disableTurbine != want.disableTurbine {
477 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
478 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000479 }
480 })
481 }
482}
483
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900484func TestSdkVersionByPartition(t *testing.T) {
485 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900486 java_library {
487 name: "foo",
488 srcs: ["a.java"],
489 vendor: true,
490 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900491 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900492
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900493 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900494 java_library {
495 name: "bar",
496 srcs: ["b.java"],
497 }
498 `)
499
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900500 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900501 bp := `
502 java_library {
503 name: "foo",
504 srcs: ["a.java"],
505 product_specific: true,
506 }
507 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800508
509 config := testConfig(nil, bp, nil)
510 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900511 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800512 testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900513 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800514 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900515 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900516 }
517}
518
Colin Crossd5934c82017-10-02 13:55:26 -0700519func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700520 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700521 java_library {
522 name: "foo",
523 srcs: ["a.java"],
524 target: {
525 android: {
526 srcs: ["b.java"],
527 },
528 },
529 }
530 `)
531
532 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
533 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
534 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
535 }
536}
537
Colin Cross6b4a32d2017-12-05 13:42:45 -0800538func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700539 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800540 java_library_host {
541 name: "foo",
542 srcs: ["a.java"],
543 }
544
545 java_binary_host {
546 name: "bar",
547 srcs: ["b.java"],
548 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700549 jni_libs: ["libjni"],
550 }
551
552 cc_library_shared {
553 name: "libjni",
554 host_supported: true,
555 device_supported: false,
556 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800557 }
558 `)
559
560 buildOS := android.BuildOs.String()
561
562 bar := ctx.ModuleForTests("bar", buildOS+"_common")
563 barJar := bar.Output("bar.jar").Output.String()
564 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
565 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
566
Colin Cross89226d92020-10-09 19:00:54 -0700567 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
568 libjniSO := libjni.Rule("Cp").Output.String()
569
Colin Cross6b4a32d2017-12-05 13:42:45 -0800570 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700571 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
572 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800573 }
Colin Cross89226d92020-10-09 19:00:54 -0700574
575 // Test that the install binary wrapper depends on the installed JNI libraries
576 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
577 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800578 }
Alex Humesky2070e322020-06-09 20:23:08 -0400579}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800580
Alex Humesky2070e322020-06-09 20:23:08 -0400581func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
582 bp := `
583 java_library {
584 name: "target_library",
585 srcs: ["a.java"],
586 }
587
588 java_binary_host {
589 name: "host_binary",
590 srcs: ["b.java"],
591 }
592 `
593 config := testConfig(nil, bp, nil)
594 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
595
596 ctx, _ := testJavaWithConfig(t, config)
597
Liz Kammer7941b302020-07-28 13:27:34 -0700598 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400599 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
600 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
601 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
602 t.Errorf("target library javac flags %v should contain "+
603 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
604 }
605
606 // check that -g is not overridden for host modules
607 buildOS := android.BuildOs.String()
608 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
609 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
610 if strings.Contains(hostJavaFlags, "-g:source,lines") {
611 t.Errorf("java_binary_host javac flags %v should not have "+
612 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
613 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800614}
615
Colin Cross72bb3632017-07-13 16:23:21 -0700616func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700617 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700618 java_library {
619 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000620 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000621 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700622 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700623 }
624
Colin Cross74d73e22017-08-02 11:05:49 -0700625 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700626 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700627 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700628 }
629
Colin Cross74d73e22017-08-02 11:05:49 -0700630 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700631 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700632 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700633 sdk_version: "current",
634 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700635 }
Colin Cross42be7612019-02-21 18:12:14 -0800636
637 dex_import {
638 name: "qux",
639 jars: ["b.jar"],
640 }
Colin Cross79c7c262019-04-17 11:11:46 -0700641
642 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000643 name: "sdklib",
644 public: {
645 jars: ["c.jar"],
646 },
647 }
648
Paul Duffin91547182019-11-12 19:39:36 +0000649 prebuilt_stubs_sources {
650 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000651 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000652 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000653
654 java_test_import {
655 name: "test",
656 jars: ["a.jar"],
657 test_suites: ["cts"],
658 test_config: "AndroidTest.xml",
659 }
Colin Cross72bb3632017-07-13 16:23:21 -0700660 `)
661
Paul Duffin9b478b02019-12-10 13:41:51 +0000662 fooModule := ctx.ModuleForTests("foo", "android_common")
663 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700664 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700665 barModule := ctx.ModuleForTests("bar", "android_common")
666 barJar := barModule.Rule("combineJar").Output
667 bazModule := ctx.ModuleForTests("baz", "android_common")
668 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700669 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700670
Paul Duffin9b478b02019-12-10 13:41:51 +0000671 fooLibrary := fooModule.Module().(*Library)
672 assertDeepEquals(t, "foo java sources incorrect",
673 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000674
Paul Duffin9b478b02019-12-10 13:41:51 +0000675 assertDeepEquals(t, "foo java source jars incorrect",
676 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
677 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000678
Colin Cross37f6d792018-07-12 12:28:41 -0700679 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
680 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700681 }
682
Liz Kammerd6c31d22020-08-05 15:40:41 -0700683 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
684 if barDexJar != nil {
685 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
686 }
687
Colin Cross79c7c262019-04-17 11:11:46 -0700688 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
689 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
690 }
691
Colin Cross37f6d792018-07-12 12:28:41 -0700692 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
693 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700694 }
Colin Cross42be7612019-02-21 18:12:14 -0800695
Liz Kammerd6c31d22020-08-05 15:40:41 -0700696 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
697 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
698 if bazDexJar != expectedDexJar {
699 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
700 }
701
Colin Cross42be7612019-02-21 18:12:14 -0800702 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700703}
704
Paul Duffin9b478b02019-12-10 13:41:51 +0000705func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
706 if !reflect.DeepEqual(expected, actual) {
707 t.Errorf("%s: expected %q, found %q", message, expected, actual)
708 }
709}
710
Paul Duffin1a393322020-11-18 16:36:47 +0000711func TestPrebuiltStubsSources(t *testing.T) {
712 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
713 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
714prebuilt_stubs_sources {
715 name: "stubs-source",
716 srcs: ["%s"],
717}`, sourcesPath), map[string][]byte{
718 "stubs/sources/pkg/A.java": nil,
719 "stubs/sources/pkg/B.java": nil,
720 })
721
722 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
723 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
724 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
725 }
726 }
727
728 t.Run("empty/missing directory", func(t *testing.T) {
729 test(t, "empty-directory", []string{})
730 })
731
732 t.Run("non-empty set of sources", func(t *testing.T) {
733 test(t, "stubs/sources", []string{
734 "stubs/sources/pkg/A.java",
735 "stubs/sources/pkg/B.java",
736 })
737 })
738}
739
Paul Duffin56d44902020-01-31 13:36:25 +0000740func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000741 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000742 java_library {
743 name: "foo",
744 srcs: ["a.java"],
745 libs: ["sdklib"],
746 sdk_version: "current",
747 }
748
749 java_library {
750 name: "foo.system",
751 srcs: ["a.java"],
752 libs: ["sdklib"],
753 sdk_version: "system_current",
754 }
755
756 java_library {
757 name: "foo.test",
758 srcs: ["a.java"],
759 libs: ["sdklib"],
760 sdk_version: "test_current",
761 }
762
763 java_sdk_library_import {
764 name: "sdklib",
765 public: {
766 jars: ["a.jar"],
767 },
768 system: {
769 jars: ["b.jar"],
770 },
771 test: {
772 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100773 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000774 },
775 }
776 `)
777
778 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000779 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000780 javac := fooModule.Rule("javac")
781
Paul Duffin22b77cd2021-03-12 19:15:01 +0000782 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
783 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000784 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100785
Paul Duffin22b77cd2021-03-12 19:15:01 +0000786 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100787 `prebuilt_sdklib.stubs`,
788 `prebuilt_sdklib.stubs.source.test`,
789 `prebuilt_sdklib.stubs.system`,
790 `prebuilt_sdklib.stubs.test`,
791 })
792}
793
794func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000795 result := javaFixtureFactory.Extend(
796 PrepareForTestWithJavaSdkLibraryFiles,
797 FixtureWithLastReleaseApis("sdklib"),
798 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100799 java_sdk_library {
800 name: "sdklib",
801 srcs: ["a.java"],
802 sdk_version: "none",
803 system_modules: "none",
804 public: {
805 enabled: true,
806 },
807 }
808
809 java_sdk_library_import {
810 name: "sdklib",
811 public: {
812 jars: ["a.jar"],
813 },
814 }
815 `)
816
Paul Duffin22b77cd2021-03-12 19:15:01 +0000817 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100818 `dex2oatd`,
819 `prebuilt_sdklib`,
820 `sdklib.impl`,
821 `sdklib.stubs`,
822 `sdklib.stubs.source`,
823 `sdklib.xml`,
824 })
825
Paul Duffin22b77cd2021-03-12 19:15:01 +0000826 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100827 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100828 `sdklib.impl`,
829 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
830 // dependency is added after prebuilts may have been renamed and so has to use
831 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100832 `sdklib.xml`,
833 })
834}
835
836func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000837 result := javaFixtureFactory.Extend(
838 PrepareForTestWithJavaSdkLibraryFiles,
839 FixtureWithLastReleaseApis("sdklib"),
840 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100841 java_sdk_library {
842 name: "sdklib",
843 srcs: ["a.java"],
844 sdk_version: "none",
845 system_modules: "none",
846 public: {
847 enabled: true,
848 },
849 }
850
851 java_sdk_library_import {
852 name: "sdklib",
853 prefer: true,
854 public: {
855 jars: ["a.jar"],
856 },
857 }
858 `)
859
Paul Duffin22b77cd2021-03-12 19:15:01 +0000860 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100861 `dex2oatd`,
862 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100863 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100864 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100865 `sdklib.stubs.source`,
866 `sdklib.xml`,
867 })
868
Paul Duffin22b77cd2021-03-12 19:15:01 +0000869 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100870 `prebuilt_sdklib.stubs`,
871 `sdklib.impl`,
872 `sdklib.xml`,
873 })
Paul Duffin56d44902020-01-31 13:36:25 +0000874}
875
JaeMan Parkff715562020-10-19 17:25:58 +0900876func TestJavaSdkLibraryEnforce(t *testing.T) {
877 partitionToBpOption := func(partition string) string {
878 switch partition {
879 case "system":
880 return ""
881 case "vendor":
882 return "soc_specific: true,"
883 case "product":
884 return "product_specific: true,"
885 default:
886 panic("Invalid partition group name: " + partition)
887 }
888 }
889
890 type testConfigInfo struct {
891 libraryType string
892 fromPartition string
893 toPartition string
894 enforceVendorInterface bool
895 enforceProductInterface bool
896 enforceJavaSdkLibraryCheck bool
897 allowList []string
898 }
899
Paul Duffin05f72de2021-03-12 21:28:51 +0000900 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900901 bpFileTemplate := `
902 java_library {
903 name: "foo",
904 srcs: ["foo.java"],
905 libs: ["bar"],
906 sdk_version: "current",
907 %s
908 }
909
910 %s {
911 name: "bar",
912 srcs: ["bar.java"],
913 sdk_version: "current",
914 %s
915 }
916 `
917
918 bpFile := fmt.Sprintf(bpFileTemplate,
919 partitionToBpOption(info.fromPartition),
920 info.libraryType,
921 partitionToBpOption(info.toPartition))
922
Paul Duffin05f72de2021-03-12 21:28:51 +0000923 return android.GroupFixturePreparers(
Paul Duffin163043d2021-03-12 19:18:49 +0000924 PrepareForTestWithJavaSdkLibraryFiles,
925 FixtureWithLastReleaseApis("bar"),
Paul Duffin05f72de2021-03-12 21:28:51 +0000926 android.FixtureWithRootAndroidBp(bpFile),
927 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
928 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
929 if info.enforceVendorInterface {
930 variables.DeviceVndkVersion = proptools.StringPtr("current")
931 }
932 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
933 variables.InterPartitionJavaLibraryAllowList = info.allowList
934 }),
935 )
JaeMan Parkff715562020-10-19 17:25:58 +0900936 }
937
Paul Duffinaa6caa72021-03-13 16:50:15 +0000938 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
939 t.Run(fmt.Sprintf("%#v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000940 errorHandler := android.FixtureExpectsNoErrors
941 if expectedErrorPattern != "" {
942 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000943 }
Paul Duffin05f72de2021-03-12 21:28:51 +0000944 javaFixtureFactory.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
Paul Duffinaa6caa72021-03-13 16:50:15 +0000945 })
946 }
947
JaeMan Parkff715562020-10-19 17:25:58 +0900948 errorMessage := "is not allowed across the partitions"
949
Paul Duffinaa6caa72021-03-13 16:50:15 +0000950 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900951 libraryType: "java_library",
952 fromPartition: "product",
953 toPartition: "system",
954 enforceVendorInterface: true,
955 enforceProductInterface: true,
956 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000957 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900958
Paul Duffinaa6caa72021-03-13 16:50:15 +0000959 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900960 libraryType: "java_library",
961 fromPartition: "product",
962 toPartition: "system",
963 enforceVendorInterface: true,
964 enforceProductInterface: false,
965 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000966 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900967
Paul Duffinaa6caa72021-03-13 16:50:15 +0000968 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900969 libraryType: "java_library",
970 fromPartition: "product",
971 toPartition: "system",
972 enforceVendorInterface: true,
973 enforceProductInterface: true,
974 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000975 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900976
Paul Duffinaa6caa72021-03-13 16:50:15 +0000977 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900978 libraryType: "java_library",
979 fromPartition: "vendor",
980 toPartition: "system",
981 enforceVendorInterface: true,
982 enforceProductInterface: true,
983 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000984 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900985
Paul Duffinaa6caa72021-03-13 16:50:15 +0000986 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900987 libraryType: "java_library",
988 fromPartition: "vendor",
989 toPartition: "system",
990 enforceVendorInterface: true,
991 enforceProductInterface: true,
992 enforceJavaSdkLibraryCheck: true,
993 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +0000994 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900995
Paul Duffinaa6caa72021-03-13 16:50:15 +0000996 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900997 libraryType: "java_library",
998 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900999 toPartition: "product",
1000 enforceVendorInterface: true,
1001 enforceProductInterface: true,
1002 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001003 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +09001004
Paul Duffinaa6caa72021-03-13 16:50:15 +00001005 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001006 libraryType: "java_sdk_library",
1007 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +09001008 toPartition: "system",
1009 enforceVendorInterface: true,
1010 enforceProductInterface: true,
1011 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001012 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001013
Paul Duffinaa6caa72021-03-13 16:50:15 +00001014 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001015 libraryType: "java_sdk_library",
1016 fromPartition: "vendor",
1017 toPartition: "system",
1018 enforceVendorInterface: true,
1019 enforceProductInterface: true,
1020 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001021 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001022
Paul Duffinaa6caa72021-03-13 16:50:15 +00001023 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001024 libraryType: "java_sdk_library",
1025 fromPartition: "vendor",
1026 toPartition: "product",
1027 enforceVendorInterface: true,
1028 enforceProductInterface: true,
1029 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001030 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +09001031}
1032
Colin Cross89536d42017-07-07 14:35:50 -07001033func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001034 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -07001035 java_defaults {
1036 name: "defaults",
1037 srcs: ["a.java"],
1038 libs: ["bar"],
1039 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -07001040 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -07001041 }
1042
1043 java_library {
1044 name: "foo",
1045 defaults: ["defaults"],
1046 }
1047
1048 java_library {
1049 name: "bar",
1050 srcs: ["b.java"],
1051 }
1052
1053 java_library {
1054 name: "baz",
1055 srcs: ["c.java"],
1056 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001057
1058 android_test {
1059 name: "atestOptimize",
1060 defaults: ["defaults"],
1061 optimize: {enabled: true},
1062 }
1063
1064 android_test {
1065 name: "atestNoOptimize",
1066 defaults: ["defaults"],
1067 }
1068
1069 android_test {
1070 name: "atestDefault",
1071 srcs: ["a.java"],
1072 }
Colin Cross89536d42017-07-07 14:35:50 -07001073 `)
1074
Colin Cross4c428df2017-09-15 17:36:05 -07001075 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001076 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001077
1078 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1079 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1080 }
1081
Nan Zhanged19fc32017-10-19 13:06:22 -07001082 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1083 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1084 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001085 }
1086
Colin Cross1ee23172017-10-18 14:44:18 -07001087 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001088 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1089 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001090 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001091
1092 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1093 if atestOptimize.Output == nil {
1094 t.Errorf("atestOptimize should optimize APK")
1095 }
1096
1097 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1098 if atestNoOptimize.Output == nil {
1099 t.Errorf("atestNoOptimize should not optimize APK")
1100 }
1101
1102 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1103 if atestDefault.Output == nil {
1104 t.Errorf("atestDefault should optimize APK")
1105 }
Colin Cross89536d42017-07-07 14:35:50 -07001106}
1107
Colin Cross0f37af02017-09-27 17:42:05 -07001108func TestResources(t *testing.T) {
1109 var table = []struct {
1110 name string
1111 prop string
1112 extra string
1113 args string
1114 }{
1115 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001116 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001117 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001118 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001119 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001120 },
1121 {
1122 // Test that a module with java_resources includes the files
1123 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001124 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1125 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001126 },
1127 {
1128 // Test that a module with a filegroup in java_resources includes the files with the
1129 // path prefix
1130 name: "resource filegroup",
1131 prop: `java_resources: [":foo-res"]`,
1132 extra: `
1133 filegroup {
1134 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001135 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001136 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001137 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001138 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001139 },
1140 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001141 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1142 name: "wildcard dirs",
1143 prop: `java_resource_dirs: ["java-res/*"]`,
1144 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1145 },
1146 {
1147 // Test that a module exclude_java_resource_dirs excludes the files
1148 name: "wildcard dirs",
1149 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1150 args: "-C java-res/a -f java-res/a/a",
1151 },
Colin Crosscedd4762018-09-13 11:26:19 -07001152 {
1153 // Test wildcards in java_resources
1154 name: "wildcard files",
1155 prop: `java_resources: ["java-res/**/*"]`,
1156 args: "-C . -f java-res/a/a -f java-res/b/b",
1157 },
1158 {
1159 // Test exclude_java_resources with java_resources
1160 name: "wildcard files with exclude",
1161 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1162 args: "-C . -f java-res/a/a",
1163 },
1164 {
1165 // Test exclude_java_resources with java_resource_dirs
1166 name: "resource dirs with exclude files",
1167 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1168 args: "-C java-res -f java-res/a/a",
1169 },
1170 {
1171 // Test exclude_java_resource_dirs with java_resource_dirs
1172 name: "resource dirs with exclude files",
1173 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1174 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1175 },
Colin Cross0f37af02017-09-27 17:42:05 -07001176 }
1177
1178 for _, test := range table {
1179 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001180 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001181 java_library {
1182 name: "foo",
1183 srcs: [
1184 "a.java",
1185 "b.java",
1186 "c.java",
1187 ],
1188 `+test.prop+`,
1189 }
Colin Cross238c1f32020-06-07 16:58:18 -07001190 `+test.extra,
1191 map[string][]byte{
1192 "java-res/a/a": nil,
1193 "java-res/b/b": nil,
1194 "java-res2/a": nil,
1195 },
1196 )
Colin Cross0f37af02017-09-27 17:42:05 -07001197
Colin Cross331a1212018-08-15 20:40:52 -07001198 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001199 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001200
1201 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1202 t.Errorf("foo combined jars %v does not contain %q",
1203 foo.Inputs.Strings(), fooRes.Output.String())
1204 }
1205
Colin Crossaf9c55b2017-10-03 14:50:08 -07001206 if fooRes.Args["jarArgs"] != test.args {
1207 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001208 fooRes.Args["jarArgs"], test.args)
1209 }
1210 })
1211 }
1212}
1213
Colin Cross0c4ce212019-05-03 15:28:19 -07001214func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001215 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001216 java_library {
1217 name: "foo",
1218 srcs: [
1219 "a.java",
1220 "b.java",
1221 "c.java",
1222 ],
1223 include_srcs: true,
1224 }
1225
1226 java_library {
1227 name: "bar",
1228 srcs: [
1229 "a.java",
1230 "b.java",
1231 "c.java",
1232 ],
1233 java_resource_dirs: ["java-res"],
1234 include_srcs: true,
1235 }
Colin Cross238c1f32020-06-07 16:58:18 -07001236 `, map[string][]byte{
1237 "java-res/a/a": nil,
1238 "java-res/b/b": nil,
1239 "java-res2/a": nil,
1240 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001241
1242 // Test a library with include_srcs: true
1243 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1244 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1245
1246 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1247 t.Errorf("foo combined jars %v does not contain %q", w, g)
1248 }
1249
1250 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1251 t.Errorf("foo source jar args %q is not %q", w, g)
1252 }
1253
1254 // Test a library with include_srcs: true and resources
1255 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1256 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1257 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1258 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1259
1260 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1261 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1262 }
1263
1264 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1265 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1266 }
1267
1268 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1269 t.Errorf("bar combined jars %v does not contain %q", w, g)
1270 }
1271
1272 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1273 t.Errorf("bar source jar args %q is not %q", w, g)
1274 }
1275
1276 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1277 t.Errorf("bar resource jar args %q is not %q", w, g)
1278 }
1279}
1280
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001281func TestJavaLint(t *testing.T) {
1282 ctx, _ := testJavaWithFS(t, `
1283 java_library {
1284 name: "foo",
1285 srcs: [
1286 "a.java",
1287 "b.java",
1288 "c.java",
1289 ],
1290 min_sdk_version: "29",
1291 sdk_version: "system_current",
1292 }
1293 `, map[string][]byte{
1294 "lint-baseline.xml": nil,
1295 })
1296
1297 foo := ctx.ModuleForTests("foo", "android_common")
1298 rule := foo.Rule("lint")
1299
1300 if !strings.Contains(rule.RuleParams.Command, "--baseline lint-baseline.xml") {
1301 t.Error("did not pass --baseline flag")
1302 }
1303}
1304
1305func TestJavaLintWithoutBaseline(t *testing.T) {
1306 ctx, _ := testJavaWithFS(t, `
1307 java_library {
1308 name: "foo",
1309 srcs: [
1310 "a.java",
1311 "b.java",
1312 "c.java",
1313 ],
1314 min_sdk_version: "29",
1315 sdk_version: "system_current",
1316 }
1317 `, map[string][]byte{})
1318
1319 foo := ctx.ModuleForTests("foo", "android_common")
1320 rule := foo.Rule("lint")
1321
1322 if strings.Contains(rule.RuleParams.Command, "--baseline") {
1323 t.Error("passed --baseline flag for non existent file")
1324 }
1325}
1326
1327func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
1328 config := testConfig(
1329 nil,
1330 `
1331 java_library {
1332 name: "foo",
1333 srcs: [
1334 ],
1335 min_sdk_version: "29",
1336 sdk_version: "system_current",
1337 lint: {
1338 baseline_filename: "mybaseline.xml",
1339 },
1340 }
1341 `, map[string][]byte{
1342 "build/soong/java/lint_defaults.txt": nil,
1343 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1344 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1345 "framework/aidl": nil,
1346 "a.java": nil,
1347 "AndroidManifest.xml": nil,
1348 "build/make/target/product/security": nil,
1349 })
1350 config.TestAllowNonExistentPaths = false
1351 testJavaErrorWithConfig(t,
1352 "source path \"mybaseline.xml\" does not exist",
1353 config,
1354 )
1355}
1356
1357func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1358 ctx, _ := testJavaWithFS(t, `
1359 java_library {
1360 name: "foo",
1361 srcs: [
1362 "a.java",
1363 "b.java",
1364 "c.java",
1365 ],
1366 min_sdk_version: "29",
1367 sdk_version: "system_current",
1368 lint: {
1369 error_checks: ["SomeCheck"],
1370 baseline_filename: "mybaseline.xml",
1371 },
1372 }
1373 `, map[string][]byte{
1374 "mybaseline.xml": nil,
1375 })
1376
1377 foo := ctx.ModuleForTests("foo", "android_common")
1378 rule := foo.Rule("lint")
1379
1380 if !strings.Contains(rule.RuleParams.Command, "--baseline mybaseline.xml") {
1381 t.Error("did not use the correct file for baseline")
1382 }
1383}
1384
Colin Cross54190b32017-10-09 15:34:10 -07001385func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001386 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001387 java_library {
1388 name: "foo",
1389 srcs: [
1390 "a*.java",
1391 ":gen",
1392 "b*.java",
1393 ],
1394 }
1395
1396 genrule {
1397 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001398 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001399 out: ["gen.java"],
1400 }
Colin Cross238c1f32020-06-07 16:58:18 -07001401 `, map[string][]byte{
1402 "a.java": nil,
1403 "b.java": nil,
1404 })
Colin Cross54190b32017-10-09 15:34:10 -07001405
1406 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1407 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1408
Colin Cross15e86d92017-10-20 15:07:08 -07001409 if filepath.Base(genrule.Output.String()) != "gen.java" {
1410 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001411 }
1412
1413 if len(javac.Inputs) != 3 ||
1414 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001415 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001416 javac.Inputs[2].String() != "b.java" {
1417 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1418 }
1419}
1420
Nan Zhang61eaedb2017-11-02 13:28:15 -07001421func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001422 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001423 java_library {
1424 name: "foo",
1425 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001426 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001427 }
1428
1429 java_library {
1430 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001431 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001432 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001433 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001434 }
1435
1436 java_library {
1437 name: "baz",
1438 srcs: ["c.java"],
1439 libs: ["bar"],
1440 sdk_version: "14",
1441 }
1442 `)
1443
1444 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1445 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1446 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1447 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1448 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1449
1450 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1451 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1452 }
1453
1454 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1455 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1456 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1457 }
1458 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1459 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1460 }
1461 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1462 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1463 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001464 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001465 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001466 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001467 }
1468}
1469
1470func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001471 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001472 java_library {
1473 name: "bar",
1474 srcs: ["a.java","b.java","c.java"],
1475 javac_shard_size: 1
1476 }
1477 `)
1478
1479 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1480 for i := 0; i < 3; i++ {
1481 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1482 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1483 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1484 }
1485 }
1486}
1487
Nan Zhang581fd212018-01-10 16:06:12 -08001488func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001489 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001490 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001491 name: "droiddoc-templates-sdk",
1492 path: ".",
1493 }
Jiyong Park29074592019-07-07 16:27:47 +09001494 filegroup {
1495 name: "bar-doc-aidl-srcs",
1496 srcs: ["bar-doc/IBar.aidl"],
1497 path: "bar-doc",
1498 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001499 droidstubs {
1500 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001501 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001502 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001503 ],
Steve Kim3666c702020-09-01 17:58:01 +00001504 exclude_srcs: [
1505 "bar-doc/b.java"
1506 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001507 api_levels_annotations_dirs: [
1508 "droiddoc-templates-sdk",
1509 ],
1510 api_levels_annotations_enabled: true,
1511 }
1512 droiddoc {
1513 name: "bar-doc",
1514 srcs: [
1515 ":bar-stubs",
1516 "bar-doc/IFoo.aidl",
1517 ":bar-doc-aidl-srcs",
1518 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001519 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001520 hdf: [
1521 "android.whichdoc offline",
1522 ],
1523 knowntags: [
1524 "bar-doc/known_oj_tags.txt",
1525 ],
1526 proofread_file: "libcore-proofread.txt",
1527 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001528 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001529 }
Colin Cross238c1f32020-06-07 16:58:18 -07001530 `,
1531 map[string][]byte{
1532 "bar-doc/a.java": nil,
1533 "bar-doc/b.java": nil,
1534 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001535 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1536 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1537 if err != nil {
1538 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1539 }
1540 if len(barStubsOutputs) != 1 {
1541 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001542 }
Nan Zhang581fd212018-01-10 16:06:12 -08001543
Liz Kammere1ab2502020-09-10 15:29:25 +00001544 barStubsOutput := barStubsOutputs[0]
1545 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1546 javaDoc := barDoc.Rule("javadoc")
1547 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1548 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001549 }
1550
Liz Kammere1ab2502020-09-10 15:29:25 +00001551 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1552 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1553 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1554 }
1555
1556 aidl := barDoc.Rule("aidl")
1557 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001558 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1559 }
1560
1561 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1562 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001563 }
Nan Zhang581fd212018-01-10 16:06:12 -08001564}
1565
Liz Kammer585cac22020-07-06 09:12:57 -07001566func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1567 testJavaError(t, "flags is set. Cannot set args", `
1568 droiddoc_exported_dir {
1569 name: "droiddoc-templates-sdk",
1570 path: ".",
1571 }
1572 filegroup {
1573 name: "bar-doc-aidl-srcs",
1574 srcs: ["bar-doc/IBar.aidl"],
1575 path: "bar-doc",
1576 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001577 droidstubs {
1578 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001579 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001580 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001581 ],
Steve Kim3666c702020-09-01 17:58:01 +00001582 exclude_srcs: [
1583 "bar-doc/b.java"
1584 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001585 api_levels_annotations_dirs: [
1586 "droiddoc-templates-sdk",
1587 ],
1588 api_levels_annotations_enabled: true,
1589 }
1590 droiddoc {
1591 name: "bar-doc",
1592 srcs: [
1593 ":bar-stubs",
1594 "bar-doc/IFoo.aidl",
1595 ":bar-doc-aidl-srcs",
1596 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001597 custom_template: "droiddoc-templates-sdk",
1598 hdf: [
1599 "android.whichdoc offline",
1600 ],
1601 knowntags: [
1602 "bar-doc/known_oj_tags.txt",
1603 ],
1604 proofread_file: "libcore-proofread.txt",
1605 todo_file: "libcore-docs-todo.html",
1606 flags: ["-offlinemode -title \"libcore\""],
1607 args: "-offlinemode -title \"libcore\"",
1608 }
1609 `)
1610}
1611
Liz Kammer3d894b72020-08-04 09:55:13 -07001612func TestDroidstubs(t *testing.T) {
1613 ctx, _ := testJavaWithFS(t, `
1614 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001615 name: "droiddoc-templates-sdk",
1616 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001617 }
1618
1619 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001620 name: "bar-stubs",
1621 srcs: ["bar-doc/a.java"],
1622 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1623 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001624 }
1625
1626 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001627 name: "bar-stubs-other",
1628 srcs: ["bar-doc/a.java"],
1629 high_mem: true,
1630 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1631 api_levels_annotations_enabled: true,
1632 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001633 }
1634 `,
1635 map[string][]byte{
1636 "bar-doc/a.java": nil,
1637 })
1638 testcases := []struct {
1639 moduleName string
1640 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001641 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001642 }{
1643 {
1644 moduleName: "bar-stubs",
1645 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001646 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001647 },
1648 {
1649 moduleName: "bar-stubs-other",
1650 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001651 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001652 },
1653 }
1654 for _, c := range testcases {
1655 m := ctx.ModuleForTests(c.moduleName, "android_common")
1656 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001657 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001658 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001659 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001660 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1661 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001662
1663 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1664 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1665 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001666 }
1667}
1668
Paul Duffin83a2d962019-11-19 19:44:10 +00001669func TestDroidstubsWithSystemModules(t *testing.T) {
1670 ctx, _ := testJava(t, `
1671 droidstubs {
1672 name: "stubs-source-system-modules",
1673 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001674 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001675 ],
1676 sdk_version: "none",
1677 system_modules: "source-system-modules",
1678 }
1679
1680 java_library {
1681 name: "source-jar",
1682 srcs: [
1683 "a.java",
1684 ],
1685 }
1686
1687 java_system_modules {
1688 name: "source-system-modules",
1689 libs: ["source-jar"],
1690 }
1691
1692 droidstubs {
1693 name: "stubs-prebuilt-system-modules",
1694 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001695 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001696 ],
1697 sdk_version: "none",
1698 system_modules: "prebuilt-system-modules",
1699 }
1700
1701 java_import {
1702 name: "prebuilt-jar",
1703 jars: ["a.jar"],
1704 }
1705
1706 java_system_modules_import {
1707 name: "prebuilt-system-modules",
1708 libs: ["prebuilt-jar"],
1709 }
1710 `)
1711
1712 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1713
1714 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1715}
1716
1717func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1718 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1719 var systemJars []string
1720 for _, i := range metalavaRule.Implicits {
1721 systemJars = append(systemJars, i.Base())
1722 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001723 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001724 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1725 }
1726}
1727
Colin Cross54250902017-12-05 09:28:08 -08001728func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001729 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001730 java_library {
1731 name: "foo",
1732 srcs: ["a.java"],
1733 }
1734
1735 java_genrule {
1736 name: "jargen",
1737 tool_files: ["b.java"],
1738 cmd: "$(location b.java) $(in) $(out)",
1739 out: ["jargen.jar"],
1740 srcs: [":foo"],
1741 }
1742
1743 java_library {
1744 name: "bar",
1745 static_libs: ["jargen"],
1746 srcs: ["c.java"],
1747 }
1748
1749 java_library {
1750 name: "baz",
1751 libs: ["jargen"],
1752 srcs: ["c.java"],
1753 }
1754 `)
1755
1756 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1757 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1758 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1759 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1760 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1761
Colin Cross3d680512020-11-13 16:23:53 -08001762 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1763 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001764 }
1765
1766 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1767 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1768 }
1769
1770 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1771 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1772 }
1773
1774 if len(barCombined.Inputs) != 2 ||
1775 barCombined.Inputs[0].String() != bar.Output.String() ||
1776 barCombined.Inputs[1].String() != jargen.Output.String() {
1777 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1778 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1779 }
1780}
1781
Nan Zhang27e284d2018-02-09 21:03:53 +00001782func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001783 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001784 java_library {
1785 name: "foo",
1786 srcs: ["a.java", ":foo-srcs"],
1787 exclude_srcs: ["a.java", ":foo-excludes"],
1788 }
1789
1790 filegroup {
1791 name: "foo-srcs",
1792 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1793 }
1794
1795 filegroup {
1796 name: "foo-excludes",
1797 srcs: ["java-fg/a.java", "java-fg/b.java"],
1798 }
1799 `)
1800
1801 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1802
1803 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1804 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1805 }
1806}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001807
Paul Duffin52d398a2019-06-11 12:31:14 +01001808func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001809 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001810 "libcore/Android.bp": []byte(`
1811 java_library {
1812 name: "core",
1813 sdk_version: "none",
1814 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001815 }
1816
1817 filegroup {
1818 name: "core-jar",
1819 srcs: [":core{.jar}"],
1820 }
1821`),
1822 })
Colin Crossae8600b2020-10-29 17:09:13 -07001823 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001824 run(t, ctx, config)
1825}
1826
1827func TestJavaImport(t *testing.T) {
1828 config := testConfig(nil, "", map[string][]byte{
1829 "libcore/Android.bp": []byte(`
1830 java_import {
1831 name: "core",
1832 sdk_version: "none",
1833 }
1834
1835 filegroup {
1836 name: "core-jar",
1837 srcs: [":core{.jar}"],
1838 }
1839`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001840 })
Colin Crossae8600b2020-10-29 17:09:13 -07001841 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001842 run(t, ctx, config)
1843}
1844
Jiyong Parkc678ad32018-04-10 13:07:10 +09001845func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001846 result := javaFixtureFactory.Extend(
1847 PrepareForTestWithJavaSdkLibraryFiles,
1848 FixtureWithPrebuiltApis(map[string][]string{
1849 "28": {"foo"},
1850 "29": {"foo"},
1851 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1852 }),
1853 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001854 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001855 name: "droiddoc-templates-sdk",
1856 path: ".",
1857 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001858 java_sdk_library {
1859 name: "foo",
1860 srcs: ["a.java", "b.java"],
1861 api_packages: ["foo"],
1862 }
1863 java_sdk_library {
1864 name: "bar",
1865 srcs: ["a.java", "b.java"],
1866 api_packages: ["bar"],
1867 }
1868 java_library {
1869 name: "baz",
1870 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001871 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001872 sdk_version: "system_current",
1873 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001874 java_sdk_library {
1875 name: "barney",
1876 srcs: ["c.java"],
1877 api_only: true,
1878 }
1879 java_sdk_library {
1880 name: "betty",
1881 srcs: ["c.java"],
1882 shared_library: false,
1883 }
Paul Duffin859fe962020-05-15 10:20:31 +01001884 java_sdk_library_import {
1885 name: "quuz",
1886 public: {
1887 jars: ["c.jar"],
1888 },
1889 }
1890 java_sdk_library_import {
1891 name: "fred",
1892 public: {
1893 jars: ["b.jar"],
1894 },
1895 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001896 java_sdk_library_import {
1897 name: "wilma",
1898 public: {
1899 jars: ["b.jar"],
1900 },
1901 shared_library: false,
1902 }
Jiyong Park1be96912018-05-28 18:02:19 +09001903 java_library {
1904 name: "qux",
1905 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001906 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001907 sdk_version: "system_current",
1908 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001909 java_library {
1910 name: "baz-test",
1911 srcs: ["c.java"],
1912 libs: ["foo"],
1913 sdk_version: "test_current",
1914 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001915 java_library {
1916 name: "baz-29",
1917 srcs: ["c.java"],
1918 libs: ["foo"],
1919 sdk_version: "system_29",
1920 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001921 java_library {
1922 name: "baz-module-30",
1923 srcs: ["c.java"],
1924 libs: ["foo"],
1925 sdk_version: "module_30",
1926 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001927 `)
1928
1929 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001930 result.ModuleForTests("foo", "android_common")
1931 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1932 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1933 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1934 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1935 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1936 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1937 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1938 result.ModuleForTests("foo.api.public.28", "")
1939 result.ModuleForTests("foo.api.system.28", "")
1940 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001941
Paul Duffin22b77cd2021-03-12 19:15:01 +00001942 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001943 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001944 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001945 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001946 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001947 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001948 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001949
Paul Duffin22b77cd2021-03-12 19:15:01 +00001950 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001951 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001952 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001953
Paul Duffin22b77cd2021-03-12 19:15:01 +00001954 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001955 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001956 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001957
Paul Duffin22b77cd2021-03-12 19:15:01 +00001958 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001959 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001960 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 +01001961
Jiyong Park1be96912018-05-28 18:02:19 +09001962 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001963 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001964 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001965 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001966 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001967 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001968}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001969
Anton Hansson7f66efa2020-10-08 14:47:23 +01001970func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001971 result := javaFixtureFactory.Extend(
1972 PrepareForTestWithJavaSdkLibraryFiles,
1973 FixtureWithLastReleaseApis("sdklib"),
1974 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001975 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001976 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001977 srcs: ["a.java"],
1978 impl_only_libs: ["foo"],
1979 stub_only_libs: ["bar"],
1980 }
1981 java_library {
1982 name: "foo",
1983 srcs: ["a.java"],
1984 sdk_version: "current",
1985 }
1986 java_library {
1987 name: "bar",
1988 srcs: ["a.java"],
1989 sdk_version: "current",
1990 }
1991 `)
1992
Anton Hanssondff2c782020-12-21 17:10:01 +00001993 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001994 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001995 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1996 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1997 }
1998 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001999 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00002000 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01002001 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
2002 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
2003 }
2004}
2005
Paul Duffindaaa3322020-05-26 18:13:57 +01002006func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002007 result := javaFixtureFactory.Extend(
2008 PrepareForTestWithJavaSdkLibraryFiles,
2009 FixtureWithLastReleaseApis("foo"),
2010 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01002011 java_sdk_library {
2012 name: "foo",
2013 srcs: ["a.java"],
2014 api_only: true,
2015 public: {
2016 enabled: true,
2017 },
2018 }
2019
2020 java_library {
2021 name: "bar",
2022 srcs: ["b.java"],
2023 libs: ["foo"],
2024 }
2025 `)
2026
2027 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002028 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01002029 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2030 t.Errorf("expected %q, found %#q", expected, actual)
2031 }
2032}
2033
Paul Duffin46dc45a2020-05-14 15:39:10 +01002034func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002035 javaFixtureFactory.Extend(
2036 PrepareForTestWithJavaSdkLibraryFiles,
2037 FixtureWithLastReleaseApis("foo"),
2038 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002039 java_sdk_library {
2040 name: "foo",
2041 srcs: ["a.java"],
2042 api_packages: ["foo"],
2043 public: {
2044 enabled: true,
2045 },
2046 }
2047
2048 java_library {
2049 name: "bar",
2050 srcs: ["b.java", ":foo{.public.stubs.source}"],
2051 }
2052 `)
2053}
2054
2055func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002056 javaFixtureFactory.
Paul Duffin163043d2021-03-12 19:18:49 +00002057 Extend(
2058 PrepareForTestWithJavaSdkLibraryFiles,
2059 FixtureWithLastReleaseApis("foo"),
2060 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00002061 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
2062 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002063 java_sdk_library {
2064 name: "foo",
2065 srcs: ["a.java"],
2066 api_packages: ["foo"],
2067 public: {
2068 enabled: true,
2069 },
2070 }
2071
2072 java_library {
2073 name: "bar",
2074 srcs: ["b.java", ":foo{.system.stubs.source}"],
2075 }
2076 `)
2077}
2078
Paul Duffinca8d9a52020-06-26 22:20:25 +01002079func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002080 result := javaFixtureFactory.Extend(
2081 PrepareForTestWithJavaSdkLibraryFiles,
2082 FixtureWithLastReleaseApis("sdklib"),
2083 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01002084 java_sdk_library {
2085 name: "sdklib",
2086 srcs: ["a.java"],
2087 sdk_version: "none",
2088 system_modules: "none",
2089 public: {
2090 enabled: true,
2091 },
2092 }
2093 `)
2094
Paul Duffin22b77cd2021-03-12 19:15:01 +00002095 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01002096 `dex2oatd`,
2097 `sdklib.impl`,
2098 `sdklib.stubs`,
2099 `sdklib.stubs.source`,
2100 `sdklib.xml`,
2101 })
2102}
2103
Paul Duffin46dc45a2020-05-14 15:39:10 +01002104func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002105 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002106 java_sdk_library_import {
2107 name: "foo",
2108 public: {
2109 jars: ["a.jar"],
2110 stub_srcs: ["a.java"],
2111 current_api: "api/current.txt",
2112 removed_api: "api/removed.txt",
2113 },
2114 }
2115
2116 java_library {
2117 name: "bar",
2118 srcs: [":foo{.public.stubs.source}"],
2119 java_resources: [
2120 ":foo{.public.api.txt}",
2121 ":foo{.public.removed-api.txt}",
2122 ],
2123 }
2124 `)
2125}
2126
2127func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2128 bp := `
2129 java_sdk_library_import {
2130 name: "foo",
2131 public: {
2132 jars: ["a.jar"],
2133 },
2134 }
2135 `
2136
2137 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002138 javaFixtureFactory.
2139 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
2140 RunTestWithBp(t, bp+`
2141 java_library {
2142 name: "bar",
2143 srcs: [":foo{.public.stubs.source}"],
2144 java_resources: [
2145 ":foo{.public.api.txt}",
2146 ":foo{.public.removed-api.txt}",
2147 ],
2148 }
2149 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002150 })
2151
2152 t.Run("api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002153 javaFixtureFactory.
2154 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
2155 RunTestWithBp(t, bp+`
2156 java_library {
2157 name: "bar",
2158 srcs: ["a.java"],
2159 java_resources: [
2160 ":foo{.public.api.txt}",
2161 ],
2162 }
2163 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002164 })
2165
2166 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002167 javaFixtureFactory.
2168 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
2169 RunTestWithBp(t, bp+`
2170 java_library {
2171 name: "bar",
2172 srcs: ["a.java"],
2173 java_resources: [
2174 ":foo{.public.removed-api.txt}",
2175 ],
2176 }
2177 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002178 })
2179}
2180
Paul Duffin3375e352020-04-28 10:44:03 +01002181func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002182 javaFixtureFactory.
2183 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
2184 RunTestWithBp(t, `
2185 java_sdk_library {
2186 name: "foo",
2187 srcs: ["a.java", "b.java"],
2188 api_packages: ["foo"],
2189 // Explicitly disable public to test the check that ensures the set of enabled
2190 // scopes is consistent.
2191 public: {
2192 enabled: false,
2193 },
2194 system: {
2195 enabled: true,
2196 },
2197 }
Paul Duffin3375e352020-04-28 10:44:03 +01002198 `)
2199}
2200
Paul Duffin87a05a32020-05-12 11:50:28 +01002201func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002202 javaFixtureFactory.Extend(
2203 PrepareForTestWithJavaSdkLibraryFiles,
2204 FixtureWithLastReleaseApis("foo"),
2205 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002206 java_sdk_library {
2207 name: "foo",
2208 srcs: ["a.java", "b.java"],
2209 api_packages: ["foo"],
2210 system: {
2211 enabled: true,
2212 sdk_version: "module_current",
2213 },
2214 }
2215 `)
2216}
2217
Paul Duffin0c5bae52020-06-02 13:00:08 +01002218func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002219 javaFixtureFactory.Extend(
2220 PrepareForTestWithJavaSdkLibraryFiles,
2221 FixtureWithLastReleaseApis("foo"),
2222 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002223 java_sdk_library {
2224 name: "foo",
2225 srcs: ["a.java", "b.java"],
2226 api_packages: ["foo"],
2227 system: {
2228 enabled: true,
2229 },
2230 module_lib: {
2231 enabled: true,
2232 },
2233 }
2234 `)
2235}
2236
2237func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002238 javaFixtureFactory.Extend(
2239 PrepareForTestWithJavaSdkLibraryFiles,
2240 FixtureWithLastReleaseApis("foo"),
2241 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002242 java_sdk_library {
2243 name: "foo",
2244 srcs: ["a.java", "b.java"],
2245 api_packages: ["foo"],
2246 system: {
2247 enabled: true,
2248 },
2249 system_server: {
2250 enabled: true,
2251 },
2252 }
2253 `)
2254}
2255
Paul Duffin803a9562020-05-20 11:52:25 +01002256func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002257 javaFixtureFactory.
2258 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2259 RunTestWithBp(t, `
2260 java_sdk_library {
2261 name: "foo",
2262 srcs: ["a.java"],
2263 public: {
2264 enabled: false,
2265 },
2266 }
Paul Duffin803a9562020-05-20 11:52:25 +01002267
Paul Duffin22b77cd2021-03-12 19:15:01 +00002268 java_library {
2269 name: "baz",
2270 srcs: ["a.java"],
2271 libs: ["foo"],
2272 sdk_version: "module_current",
2273 }
Paul Duffin803a9562020-05-20 11:52:25 +01002274 `)
2275}
2276
2277func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002278 javaFixtureFactory.Extend(
2279 PrepareForTestWithJavaSdkLibraryFiles,
2280 FixtureWithLastReleaseApis("foo"),
2281 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002282 java_sdk_library {
2283 name: "foo",
2284 srcs: ["a.java"],
2285 system: {
2286 enabled: true,
2287 },
2288 }
2289
2290 java_library {
2291 name: "baz",
2292 srcs: ["a.java"],
2293 libs: ["foo"],
2294 // foo does not have module-lib scope so it should fallback to system
2295 sdk_version: "module_current",
2296 }
2297 `)
2298}
2299
Jiyong Park932cdfe2020-05-28 00:19:53 +09002300func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002301 result := javaFixtureFactory.Extend(
2302 PrepareForTestWithJavaSdkLibraryFiles,
2303 FixtureWithLastReleaseApis("foo"),
2304 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002305 java_sdk_library {
2306 name: "foo",
2307 srcs: ["a.java"],
2308 system: {
2309 enabled: true,
2310 },
2311 default_to_stubs: true,
2312 }
2313
2314 java_library {
2315 name: "baz",
2316 srcs: ["a.java"],
2317 libs: ["foo"],
2318 // does not have sdk_version set, should fallback to module,
2319 // which will then fallback to system because the module scope
2320 // is not enabled.
2321 }
2322 `)
2323 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002324 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002325 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2326 t.Errorf("expected %q, found %#q", expected, actual)
2327 }
2328}
2329
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002330var compilerFlagsTestCases = []struct {
2331 in string
2332 out bool
2333}{
2334 {
2335 in: "a",
2336 out: false,
2337 },
2338 {
2339 in: "-a",
2340 out: true,
2341 },
2342 {
2343 in: "-no-jdk",
2344 out: false,
2345 },
2346 {
2347 in: "-no-stdlib",
2348 out: false,
2349 },
2350 {
2351 in: "-kotlin-home",
2352 out: false,
2353 },
2354 {
2355 in: "-kotlin-home /some/path",
2356 out: false,
2357 },
2358 {
2359 in: "-include-runtime",
2360 out: false,
2361 },
2362 {
2363 in: "-Xintellij-plugin-root",
2364 out: false,
2365 },
2366}
2367
2368type mockContext struct {
2369 android.ModuleContext
2370 result bool
2371}
2372
2373func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2374 // CheckBadCompilerFlags calls this function when the flag should be rejected
2375 ctx.result = false
2376}
2377
2378func TestCompilerFlags(t *testing.T) {
2379 for _, testCase := range compilerFlagsTestCases {
2380 ctx := &mockContext{result: true}
2381 CheckKotlincFlags(ctx, []string{testCase.in})
2382 if ctx.result != testCase.out {
2383 t.Errorf("incorrect output:")
2384 t.Errorf(" input: %#v", testCase.in)
2385 t.Errorf(" expected: %#v", testCase.out)
2386 t.Errorf(" got: %#v", ctx.result)
2387 }
2388 }
2389}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002390
2391// TODO(jungjw): Consider making this more robust by ignoring path order.
2392func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2393 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2394 flags := strings.Split(variables["javacFlags"], " ")
2395 got := ""
2396 for _, flag := range flags {
2397 keyEnd := strings.Index(flag, "=")
2398 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2399 got = flag[keyEnd+1:]
2400 break
2401 }
2402 }
2403 if expected != got {
2404 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2405 }
2406}
2407
2408func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002409 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002410 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002411 bp := `
2412 java_library {
2413 name: "foo",
2414 srcs: ["a.java"],
2415 java_version: "1.8",
2416 }
2417
2418 java_library {
2419 name: "bar",
2420 srcs: ["b.java"],
2421 sdk_version: "none",
2422 system_modules: "none",
2423 patch_module: "java.base",
2424 java_version: "1.8",
2425 }
2426
2427 java_library {
2428 name: "baz",
2429 srcs: ["c.java"],
2430 patch_module: "java.base",
2431 java_version: "1.8",
2432 }
2433 `
2434 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002435
2436 checkPatchModuleFlag(t, ctx, "foo", "")
2437 checkPatchModuleFlag(t, ctx, "bar", "")
2438 checkPatchModuleFlag(t, ctx, "baz", "")
2439 })
2440
Pete Gillin0c2143e2019-05-02 15:32:11 +01002441 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002442 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002443 bp := `
2444 java_library {
2445 name: "foo",
2446 srcs: ["a.java"],
2447 }
2448
2449 java_library {
2450 name: "bar",
2451 srcs: ["b.java"],
2452 sdk_version: "none",
2453 system_modules: "none",
2454 patch_module: "java.base",
2455 }
2456
2457 java_library {
2458 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002459 srcs: [
2460 "c.java",
2461 // Tests for b/150878007
2462 "dir/d.java",
2463 "dir2/e.java",
2464 "dir2/f.java",
2465 "nested/dir/g.java"
2466 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002467 patch_module: "java.base",
2468 }
2469 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002470 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002471
2472 checkPatchModuleFlag(t, ctx, "foo", "")
2473 expected := "java.base=.:" + buildDir
2474 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002475 expected = "java.base=" + strings.Join([]string{
Paul Duffin95bdab42021-03-08 21:48:46 +00002476 ".", buildDir, "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002477 checkPatchModuleFlag(t, ctx, "baz", expected)
2478 })
2479}
Paul Duffina7b9f422020-01-10 17:12:18 +00002480
Paul Duffin83a2d962019-11-19 19:44:10 +00002481func TestJavaLibraryWithSystemModules(t *testing.T) {
2482 ctx, _ := testJava(t, `
2483 java_library {
2484 name: "lib-with-source-system-modules",
2485 srcs: [
2486 "a.java",
2487 ],
2488 sdk_version: "none",
2489 system_modules: "source-system-modules",
2490 }
2491
2492 java_library {
2493 name: "source-jar",
2494 srcs: [
2495 "a.java",
2496 ],
2497 }
2498
2499 java_system_modules {
2500 name: "source-system-modules",
2501 libs: ["source-jar"],
2502 }
2503
2504 java_library {
2505 name: "lib-with-prebuilt-system-modules",
2506 srcs: [
2507 "a.java",
2508 ],
2509 sdk_version: "none",
2510 system_modules: "prebuilt-system-modules",
2511 }
2512
2513 java_import {
2514 name: "prebuilt-jar",
2515 jars: ["a.jar"],
2516 }
2517
2518 java_system_modules_import {
2519 name: "prebuilt-system-modules",
2520 libs: ["prebuilt-jar"],
2521 }
2522 `)
2523
2524 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2525
2526 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2527}
2528
2529func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2530 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2531 bootClasspath := javacRule.Args["bootClasspath"]
2532 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2533 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2534 }
2535}
Jiyong Park19604de2020-03-24 16:44:11 +09002536
2537func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2538 ctx, _ := testJava(t, `
2539 java_library {
2540 name: "foo",
2541 srcs: ["aidl/foo/IFoo.aidl"],
2542 libs: ["bar"],
2543 }
2544
2545 java_import {
2546 name: "bar",
2547 jars: ["a.jar"],
2548 aidl: {
2549 export_include_dirs: ["aidl/bar"],
2550 },
2551 }
2552 `)
2553
2554 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2555 expectedAidlFlag := "-Iaidl/bar"
2556 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2557 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2558 }
2559}
Liz Kammerdd849a82020-06-12 16:38:45 -07002560
Jooyung Hane197d8b2021-01-05 10:33:16 +09002561func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2562 ctx, _ := testJava(t, `
2563 java_library {
2564 name: "foo",
2565 srcs: ["aidl/foo/IFoo.aidl"],
2566 aidl: { flags: ["-Werror"], },
2567 }
2568 `)
2569
2570 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2571 expectedAidlFlag := "-Werror"
2572 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2573 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2574 }
2575}
2576
Liz Kammerdd849a82020-06-12 16:38:45 -07002577func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002578 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002579 java_test_host {
2580 name: "foo",
2581 srcs: ["a.java"],
2582 data_native_bins: ["bin"]
2583 }
2584
2585 python_binary_host {
2586 name: "bin",
2587 srcs: ["bin.py"],
2588 }
2589 `)
2590
2591 buildOS := android.BuildOs.String()
2592
2593 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002594 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002595 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2596 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2597 if !reflect.DeepEqual(expected, actual) {
2598 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2599 }
2600}
Yuexi Ma627263f2021-03-04 13:47:56 -08002601
2602func TestDefaultInstallable(t *testing.T) {
2603 ctx, _ := testJava(t, `
2604 java_test_host {
2605 name: "foo"
2606 }
2607 `)
2608
2609 buildOS := android.BuildOs.String()
2610 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2611 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2612 module.properties.Installable)
2613}