blob: 75813e03ca6ff898d8265068a56955ea4785a49c [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Paul Duffin1a393322020-11-18 16:36:47 +000018 "fmt"
Colin Cross72bb3632017-07-13 16:23:21 -070019 "io/ioutil"
20 "os"
21 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070022 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010023 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Jeongik Cha28df2572019-11-11 10:46:36 +090028 "github.com/google/blueprint/proptools"
29
Colin Crossa4f08812018-10-02 22:03:40 -070030 "android/soong/android"
31 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080032 "android/soong/dexpreopt"
Colin Cross1661aff2021-03-12 17:56:51 -080033 "android/soong/genrule"
Liz Kammerdd849a82020-06-12 16:38:45 -070034 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070035)
36
37var buildDir string
38
39func setUp() {
40 var err error
41 buildDir, err = ioutil.TempDir("", "soong_java_test")
42 if err != nil {
43 panic(err)
44 }
45}
46
47func tearDown() {
48 os.RemoveAll(buildDir)
49}
50
Paul Duffinafeee222021-03-17 00:36:35 +000051var emptyFixtureFactory = android.NewFixtureFactory(&buildDir)
52
Paul Duffin95bdab42021-03-08 21:48:46 +000053// Factory to use to create fixtures for tests in this package.
Paul Duffinafeee222021-03-17 00:36:35 +000054var javaFixtureFactory = emptyFixtureFactory.Extend(
Paul Duffin95bdab42021-03-08 21:48:46 +000055 genrule.PrepareForTestWithGenRuleBuildComponents,
56 // Get the CC build components but not default modules.
57 cc.PrepareForTestWithCcBuildComponents,
58 // Include all the default java modules.
59 PrepareForTestWithJavaDefaultModules,
Paul Duffin220ddd72021-03-17 23:54:30 +000060 python.PrepareForTestWithPythonBuildComponents,
Paul Duffin95bdab42021-03-08 21:48:46 +000061 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
62 ctx.RegisterModuleType("java_plugin", PluginFactory)
Paul Duffin95bdab42021-03-08 21:48:46 +000063
Paul Duffin95bdab42021-03-08 21:48:46 +000064 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
65 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
66 }),
67 javaMockFS().AddToFixture(),
Paul Duffinbf028b52021-03-13 22:19:17 +000068 PrepareForTestWithJavaSdkLibraryFiles,
Paul Duffin95bdab42021-03-08 21:48:46 +000069 dexpreopt.PrepareForTestWithDexpreopt,
70)
71
Colin Cross72bb3632017-07-13 16:23:21 -070072func TestMain(m *testing.M) {
73 run := func() int {
74 setUp()
75 defer tearDown()
76
77 return m.Run()
78 }
79
80 os.Exit(run())
81}
Colin Cross527012a2017-11-30 22:56:16 -080082
Paul Duffin95bdab42021-03-08 21:48:46 +000083// testConfig is a legacy way of creating a test Config for testing java modules.
84//
85// See testJava for an explanation as to how to stop using this deprecated method.
86//
87// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080088func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Paul Duffin9f045242021-01-21 15:05:11 +000089 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -070090}
91
Paul Duffin95bdab42021-03-08 21:48:46 +000092// testContext is a legacy way of creating a TestContext for testing java modules.
93//
94// See testJava for an explanation as to how to stop using this deprecated method.
95//
96// deprecated
Colin Crossae8600b2020-10-29 17:09:13 -070097func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070098
Colin Crossae8600b2020-10-29 17:09:13 -070099 ctx := android.NewTestArchContext(config)
Paul Duffinc059c8c2021-01-20 17:13:52 +0000100 RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -0800101 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -0800102 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Colin Cross89536d42017-07-07 14:35:50 -0700103 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100104 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +0000105
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700106 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Paul Duffineafc16b2021-02-24 01:43:18 +0000107 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
108 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
Colin Crossa4f08812018-10-02 22:03:40 -0700109
Paul Duffin021f4e52020-07-30 16:04:17 +0100110 android.RegisterPrebuiltMutators(ctx)
111
Paul Duffind6ceb862021-03-04 23:02:31 +0000112 genrule.RegisterGenruleBuildComponents(ctx)
113
Colin Crossa4f08812018-10-02 22:03:40 -0700114 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000115 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700116
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700117 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
118 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
119 })
120
Colin Cross527012a2017-11-30 22:56:16 -0800121 return ctx
122}
123
Paul Duffin95bdab42021-03-08 21:48:46 +0000124// run is a legacy way of running tests of java modules.
125//
126// See testJava for an explanation as to how to stop using this deprecated method.
127//
128// deprecated
Colin Cross527012a2017-11-30 22:56:16 -0800129func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800130 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800131
Colin Cross98be1bb2019-12-13 20:41:13 -0800132 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000133 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800134
Colin Crossae8600b2020-10-29 17:09:13 -0700135 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100136 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800137 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700138 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800139 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800140}
141
Paul Duffin95bdab42021-03-08 21:48:46 +0000142// testJavaError is a legacy way of running tests of java modules that expect errors.
143//
144// See testJava for an explanation as to how to stop using this deprecated method.
145//
146// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900147func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900148 t.Helper()
Paul Duffin6bac49c2021-03-12 21:28:15 +0000149 result := javaFixtureFactory.
150 Extend(dexpreopt.PrepareForTestWithDexpreopt).
151 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
152 RunTestWithBp(t, bp)
153 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900154}
155
Paul Duffin95bdab42021-03-08 21:48:46 +0000156// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
157//
158// See testJava for an explanation as to how to stop using this deprecated method.
159//
160// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800161func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900162 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000163 // This must be done on the supplied config and not as part of the fixture because any changes to
164 // the fixture's config will be ignored when RunTestWithConfig replaces it.
Colin Cross98be1bb2019-12-13 20:41:13 -0800165 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000166 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Paul Duffin95bdab42021-03-08 21:48:46 +0000167 result := javaFixtureFactory.
168 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
169 RunTestWithConfig(t, config)
170 return result.TestContext, result.Config
Paul Duffinec0fe172021-02-25 15:34:13 +0000171}
172
Paul Duffin95bdab42021-03-08 21:48:46 +0000173// runWithErrors is a legacy way of running tests of java modules that expect errors.
174//
175// See testJava for an explanation as to how to stop using this deprecated method.
176//
177// deprecated
Paul Duffinec0fe172021-02-25 15:34:13 +0000178func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) {
Colin Crossae8600b2020-10-29 17:09:13 -0700179 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900180 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
181 if len(errs) > 0 {
182 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000183 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900184 }
185 _, errs = ctx.PrepareBuildActions(config)
186 if len(errs) > 0 {
187 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000188 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900189 }
190
191 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Paul Duffinec0fe172021-02-25 15:34:13 +0000192 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900193}
194
Paul Duffin95bdab42021-03-08 21:48:46 +0000195// testJavaWithFS runs tests using the javaFixtureFactory
196//
197// See testJava for an explanation as to how to stop using this deprecated method.
198//
199// deprecated
200func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -0700201 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000202 result := javaFixtureFactory.Extend(fs.AddToFixture()).RunTestWithBp(t, bp)
203 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -0700204}
205
Paul Duffin95bdab42021-03-08 21:48:46 +0000206// testJava runs tests using the javaFixtureFactory
207//
208// Do not add any new usages of this, instead use the javaFixtureFactory directly as it makes it
209// much easier to customize the test behavior.
210//
211// If it is necessary to customize the behavior of an existing test that uses this then please first
212// convert the test to using javaFixtureFactory first and then in a following change add the
213// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
214// that it did not change the test behavior unexpectedly.
215//
216// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700217func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800218 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000219 result := javaFixtureFactory.RunTestWithBp(t, bp)
220 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900221}
222
Paul Duffin95bdab42021-03-08 21:48:46 +0000223// testJavaWithConfig runs tests using the javaFixtureFactory
224//
225// See testJava for an explanation as to how to stop using this deprecated method.
226//
227// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800228func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900229 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000230 result := javaFixtureFactory.RunTestWithConfig(t, config)
231 return result.TestContext, result.Config
Colin Cross72bb3632017-07-13 16:23:21 -0700232}
233
Colin Cross2acdae82017-09-15 19:44:24 -0700234func moduleToPath(name string) string {
235 switch {
236 case name == `""`:
237 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700238 case strings.HasSuffix(name, ".jar"):
239 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700240 default:
241 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700242 }
243}
244
Paul Duffin95bdab42021-03-08 21:48:46 +0000245// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
246// is defined in PrepareForIntegrationTestWithJava
247func defaultModuleToPath(name string) string {
248 return filepath.Join(buildDir, ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
249}
250
Jeongik Chae403e9e2019-12-07 00:16:24 +0900251func TestJavaLinkType(t *testing.T) {
252 testJava(t, `
253 java_library {
254 name: "foo",
255 srcs: ["a.java"],
256 libs: ["bar"],
257 static_libs: ["baz"],
258 }
259
260 java_library {
261 name: "bar",
262 sdk_version: "current",
263 srcs: ["b.java"],
264 }
265
266 java_library {
267 name: "baz",
268 sdk_version: "system_current",
269 srcs: ["c.java"],
270 }
271 `)
272
Steven Moreland00298982020-11-17 21:44:36 +0000273 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900274 java_library {
275 name: "foo",
276 srcs: ["a.java"],
277 libs: ["bar"],
278 sdk_version: "current",
279 static_libs: ["baz"],
280 }
281
282 java_library {
283 name: "bar",
284 sdk_version: "current",
285 srcs: ["b.java"],
286 }
287
288 java_library {
289 name: "baz",
290 sdk_version: "system_current",
291 srcs: ["c.java"],
292 }
293 `)
294
295 testJava(t, `
296 java_library {
297 name: "foo",
298 srcs: ["a.java"],
299 libs: ["bar"],
300 sdk_version: "system_current",
301 static_libs: ["baz"],
302 }
303
304 java_library {
305 name: "bar",
306 sdk_version: "current",
307 srcs: ["b.java"],
308 }
309
310 java_library {
311 name: "baz",
312 sdk_version: "system_current",
313 srcs: ["c.java"],
314 }
315 `)
316
Steven Moreland00298982020-11-17 21:44:36 +0000317 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900318 java_library {
319 name: "foo",
320 srcs: ["a.java"],
321 libs: ["bar"],
322 sdk_version: "system_current",
323 static_libs: ["baz"],
324 }
325
326 java_library {
327 name: "bar",
328 sdk_version: "current",
329 srcs: ["b.java"],
330 }
331
332 java_library {
333 name: "baz",
334 srcs: ["c.java"],
335 }
336 `)
337}
338
Colin Cross72bb3632017-07-13 16:23:21 -0700339func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700340 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700341 java_library {
342 name: "foo",
343 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700344 libs: ["bar"],
345 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700346 }
347
348 java_library {
349 name: "bar",
350 srcs: ["b.java"],
351 }
352
353 java_library {
354 name: "baz",
355 srcs: ["c.java"],
356 }
Colin Crossd5934c82017-10-02 13:55:26 -0700357 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700358
Colin Cross4c428df2017-09-15 17:36:05 -0700359 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700360 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700361
362 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
363 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
364 }
365
Colin Cross1ee23172017-10-18 14:44:18 -0700366 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700367 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
368 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700369
Paul Duffin22b77cd2021-03-12 19:15:01 +0000370 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700371
Paul Duffin22b77cd2021-03-12 19:15:01 +0000372 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700373
374 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
375 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700376 }
377}
378
Artur Satayev9cf46692019-11-26 18:08:34 +0000379func TestExportedPlugins(t *testing.T) {
380 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800381 library string
382 processors string
383 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000384 }
385 var tests = []struct {
386 name string
387 extra string
388 results []Result
389 }{
390 {
391 name: "Exported plugin is not a direct plugin",
392 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
393 results: []Result{{library: "exports", processors: "-proc:none"}},
394 },
395 {
396 name: "Exports plugin to dependee",
397 extra: `
398 java_library{name: "exports", exported_plugins: ["plugin"]}
399 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
400 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
401 `,
402 results: []Result{
403 {library: "foo", processors: "-processor com.android.TestPlugin"},
404 {library: "bar", processors: "-processor com.android.TestPlugin"},
405 },
406 },
407 {
408 name: "Exports plugin to android_library",
409 extra: `
410 java_library{name: "exports", exported_plugins: ["plugin"]}
411 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
412 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
413 `,
414 results: []Result{
415 {library: "foo", processors: "-processor com.android.TestPlugin"},
416 {library: "bar", processors: "-processor com.android.TestPlugin"},
417 },
418 },
419 {
420 name: "Exports plugin is not propagated via transitive deps",
421 extra: `
422 java_library{name: "exports", exported_plugins: ["plugin"]}
423 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
424 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
425 `,
426 results: []Result{
427 {library: "foo", processors: "-processor com.android.TestPlugin"},
428 {library: "bar", processors: "-proc:none"},
429 },
430 },
431 {
432 name: "Exports plugin appends to plugins",
433 extra: `
434 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
435 java_library{name: "exports", exported_plugins: ["plugin"]}
436 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
437 `,
438 results: []Result{
439 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
440 },
441 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800442 {
443 name: "Exports plugin to with generates_api to dependee",
444 extra: `
445 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
446 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
447 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
448 `,
449 results: []Result{
450 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
451 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
452 },
453 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000454 }
455
456 for _, test := range tests {
457 t.Run(test.name, func(t *testing.T) {
458 ctx, _ := testJava(t, `
459 java_plugin {
460 name: "plugin",
461 processor_class: "com.android.TestPlugin",
462 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800463 java_plugin {
464 name: "plugin_generates_api",
465 generates_api: true,
466 processor_class: "com.android.TestPlugin",
467 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000468 `+test.extra)
469
470 for _, want := range test.results {
471 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
472 if javac.Args["processor"] != want.processors {
473 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
474 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800475 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
476 disableTurbine := turbine.BuildParams.Rule == nil
477 if disableTurbine != want.disableTurbine {
478 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
479 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000480 }
481 })
482 }
483}
484
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900485func TestSdkVersionByPartition(t *testing.T) {
486 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900487 java_library {
488 name: "foo",
489 srcs: ["a.java"],
490 vendor: true,
491 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900492 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900493
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900494 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900495 java_library {
496 name: "bar",
497 srcs: ["b.java"],
498 }
499 `)
500
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900501 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900502 bp := `
503 java_library {
504 name: "foo",
505 srcs: ["a.java"],
506 product_specific: true,
507 }
508 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800509
510 config := testConfig(nil, bp, nil)
511 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900512 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800513 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 +0900514 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800515 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900516 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900517 }
518}
519
Colin Crossd5934c82017-10-02 13:55:26 -0700520func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700521 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700522 java_library {
523 name: "foo",
524 srcs: ["a.java"],
525 target: {
526 android: {
527 srcs: ["b.java"],
528 },
529 },
530 }
531 `)
532
533 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
534 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
535 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
536 }
537}
538
Colin Cross6b4a32d2017-12-05 13:42:45 -0800539func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700540 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800541 java_library_host {
542 name: "foo",
543 srcs: ["a.java"],
544 }
545
546 java_binary_host {
547 name: "bar",
548 srcs: ["b.java"],
549 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700550 jni_libs: ["libjni"],
551 }
552
553 cc_library_shared {
554 name: "libjni",
555 host_supported: true,
556 device_supported: false,
557 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800558 }
559 `)
560
561 buildOS := android.BuildOs.String()
562
563 bar := ctx.ModuleForTests("bar", buildOS+"_common")
564 barJar := bar.Output("bar.jar").Output.String()
565 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
566 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
567
Colin Cross89226d92020-10-09 19:00:54 -0700568 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
569 libjniSO := libjni.Rule("Cp").Output.String()
570
Colin Cross6b4a32d2017-12-05 13:42:45 -0800571 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700572 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
573 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800574 }
Colin Cross89226d92020-10-09 19:00:54 -0700575
576 // Test that the install binary wrapper depends on the installed JNI libraries
577 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
578 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800579 }
Alex Humesky2070e322020-06-09 20:23:08 -0400580}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800581
Alex Humesky2070e322020-06-09 20:23:08 -0400582func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
583 bp := `
584 java_library {
585 name: "target_library",
586 srcs: ["a.java"],
587 }
588
589 java_binary_host {
590 name: "host_binary",
591 srcs: ["b.java"],
592 }
593 `
594 config := testConfig(nil, bp, nil)
595 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
596
597 ctx, _ := testJavaWithConfig(t, config)
598
Liz Kammer7941b302020-07-28 13:27:34 -0700599 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400600 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
601 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
602 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
603 t.Errorf("target library javac flags %v should contain "+
604 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
605 }
606
607 // check that -g is not overridden for host modules
608 buildOS := android.BuildOs.String()
609 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
610 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
611 if strings.Contains(hostJavaFlags, "-g:source,lines") {
612 t.Errorf("java_binary_host javac flags %v should not have "+
613 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
614 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800615}
616
Colin Cross72bb3632017-07-13 16:23:21 -0700617func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700618 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700619 java_library {
620 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000621 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000622 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700623 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700624 }
625
Colin Cross74d73e22017-08-02 11:05:49 -0700626 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700627 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700628 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700629 }
630
Colin Cross74d73e22017-08-02 11:05:49 -0700631 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700632 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700633 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700634 sdk_version: "current",
635 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700636 }
Colin Cross42be7612019-02-21 18:12:14 -0800637
638 dex_import {
639 name: "qux",
640 jars: ["b.jar"],
641 }
Colin Cross79c7c262019-04-17 11:11:46 -0700642
643 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000644 name: "sdklib",
645 public: {
646 jars: ["c.jar"],
647 },
648 }
649
Paul Duffin91547182019-11-12 19:39:36 +0000650 prebuilt_stubs_sources {
651 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000652 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000653 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000654
655 java_test_import {
656 name: "test",
657 jars: ["a.jar"],
658 test_suites: ["cts"],
659 test_config: "AndroidTest.xml",
660 }
Colin Cross72bb3632017-07-13 16:23:21 -0700661 `)
662
Paul Duffin9b478b02019-12-10 13:41:51 +0000663 fooModule := ctx.ModuleForTests("foo", "android_common")
664 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700665 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700666 barModule := ctx.ModuleForTests("bar", "android_common")
667 barJar := barModule.Rule("combineJar").Output
668 bazModule := ctx.ModuleForTests("baz", "android_common")
669 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700670 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700671
Paul Duffin9b478b02019-12-10 13:41:51 +0000672 fooLibrary := fooModule.Module().(*Library)
673 assertDeepEquals(t, "foo java sources incorrect",
674 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000675
Paul Duffin9b478b02019-12-10 13:41:51 +0000676 assertDeepEquals(t, "foo java source jars incorrect",
677 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
678 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000679
Colin Cross37f6d792018-07-12 12:28:41 -0700680 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
681 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700682 }
683
Liz Kammerd6c31d22020-08-05 15:40:41 -0700684 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
685 if barDexJar != nil {
686 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
687 }
688
Colin Cross79c7c262019-04-17 11:11:46 -0700689 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
690 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
691 }
692
Colin Cross37f6d792018-07-12 12:28:41 -0700693 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
694 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700695 }
Colin Cross42be7612019-02-21 18:12:14 -0800696
Liz Kammerd6c31d22020-08-05 15:40:41 -0700697 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
698 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
699 if bazDexJar != expectedDexJar {
700 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
701 }
702
Colin Cross42be7612019-02-21 18:12:14 -0800703 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700704}
705
Paul Duffin9b478b02019-12-10 13:41:51 +0000706func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
707 if !reflect.DeepEqual(expected, actual) {
708 t.Errorf("%s: expected %q, found %q", message, expected, actual)
709 }
710}
711
Paul Duffin1a393322020-11-18 16:36:47 +0000712func TestPrebuiltStubsSources(t *testing.T) {
713 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
714 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
715prebuilt_stubs_sources {
716 name: "stubs-source",
717 srcs: ["%s"],
718}`, sourcesPath), map[string][]byte{
719 "stubs/sources/pkg/A.java": nil,
720 "stubs/sources/pkg/B.java": nil,
721 })
722
723 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
724 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
725 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
726 }
727 }
728
729 t.Run("empty/missing directory", func(t *testing.T) {
730 test(t, "empty-directory", []string{})
731 })
732
733 t.Run("non-empty set of sources", func(t *testing.T) {
734 test(t, "stubs/sources", []string{
735 "stubs/sources/pkg/A.java",
736 "stubs/sources/pkg/B.java",
737 })
738 })
739}
740
Paul Duffin56d44902020-01-31 13:36:25 +0000741func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000742 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000743 java_library {
744 name: "foo",
745 srcs: ["a.java"],
746 libs: ["sdklib"],
747 sdk_version: "current",
748 }
749
750 java_library {
751 name: "foo.system",
752 srcs: ["a.java"],
753 libs: ["sdklib"],
754 sdk_version: "system_current",
755 }
756
757 java_library {
758 name: "foo.test",
759 srcs: ["a.java"],
760 libs: ["sdklib"],
761 sdk_version: "test_current",
762 }
763
764 java_sdk_library_import {
765 name: "sdklib",
766 public: {
767 jars: ["a.jar"],
768 },
769 system: {
770 jars: ["b.jar"],
771 },
772 test: {
773 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100774 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000775 },
776 }
777 `)
778
779 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000780 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000781 javac := fooModule.Rule("javac")
782
Paul Duffin22b77cd2021-03-12 19:15:01 +0000783 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
784 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000785 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100786
Paul Duffin22b77cd2021-03-12 19:15:01 +0000787 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100788 `prebuilt_sdklib.stubs`,
789 `prebuilt_sdklib.stubs.source.test`,
790 `prebuilt_sdklib.stubs.system`,
791 `prebuilt_sdklib.stubs.test`,
792 })
793}
794
795func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000796 result := javaFixtureFactory.Extend(
797 PrepareForTestWithJavaSdkLibraryFiles,
798 FixtureWithLastReleaseApis("sdklib"),
799 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100800 java_sdk_library {
801 name: "sdklib",
802 srcs: ["a.java"],
803 sdk_version: "none",
804 system_modules: "none",
805 public: {
806 enabled: true,
807 },
808 }
809
810 java_sdk_library_import {
811 name: "sdklib",
812 public: {
813 jars: ["a.jar"],
814 },
815 }
816 `)
817
Paul Duffin22b77cd2021-03-12 19:15:01 +0000818 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100819 `dex2oatd`,
820 `prebuilt_sdklib`,
821 `sdklib.impl`,
822 `sdklib.stubs`,
823 `sdklib.stubs.source`,
824 `sdklib.xml`,
825 })
826
Paul Duffin22b77cd2021-03-12 19:15:01 +0000827 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100828 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100829 `sdklib.impl`,
830 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
831 // dependency is added after prebuilts may have been renamed and so has to use
832 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100833 `sdklib.xml`,
834 })
835}
836
837func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000838 result := javaFixtureFactory.Extend(
839 PrepareForTestWithJavaSdkLibraryFiles,
840 FixtureWithLastReleaseApis("sdklib"),
841 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100842 java_sdk_library {
843 name: "sdklib",
844 srcs: ["a.java"],
845 sdk_version: "none",
846 system_modules: "none",
847 public: {
848 enabled: true,
849 },
850 }
851
852 java_sdk_library_import {
853 name: "sdklib",
854 prefer: true,
855 public: {
856 jars: ["a.jar"],
857 },
858 }
859 `)
860
Paul Duffin22b77cd2021-03-12 19:15:01 +0000861 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100862 `dex2oatd`,
863 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100864 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100865 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100866 `sdklib.stubs.source`,
867 `sdklib.xml`,
868 })
869
Paul Duffin22b77cd2021-03-12 19:15:01 +0000870 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100871 `prebuilt_sdklib.stubs`,
872 `sdklib.impl`,
873 `sdklib.xml`,
874 })
Paul Duffin56d44902020-01-31 13:36:25 +0000875}
876
JaeMan Parkff715562020-10-19 17:25:58 +0900877func TestJavaSdkLibraryEnforce(t *testing.T) {
878 partitionToBpOption := func(partition string) string {
879 switch partition {
880 case "system":
881 return ""
882 case "vendor":
883 return "soc_specific: true,"
884 case "product":
885 return "product_specific: true,"
886 default:
887 panic("Invalid partition group name: " + partition)
888 }
889 }
890
891 type testConfigInfo struct {
892 libraryType string
893 fromPartition string
894 toPartition string
895 enforceVendorInterface bool
896 enforceProductInterface bool
897 enforceJavaSdkLibraryCheck bool
898 allowList []string
899 }
900
Paul Duffin05f72de2021-03-12 21:28:51 +0000901 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900902 bpFileTemplate := `
903 java_library {
904 name: "foo",
905 srcs: ["foo.java"],
906 libs: ["bar"],
907 sdk_version: "current",
908 %s
909 }
910
911 %s {
912 name: "bar",
913 srcs: ["bar.java"],
914 sdk_version: "current",
915 %s
916 }
917 `
918
919 bpFile := fmt.Sprintf(bpFileTemplate,
920 partitionToBpOption(info.fromPartition),
921 info.libraryType,
922 partitionToBpOption(info.toPartition))
923
Paul Duffin05f72de2021-03-12 21:28:51 +0000924 return android.GroupFixturePreparers(
Paul Duffin163043d2021-03-12 19:18:49 +0000925 PrepareForTestWithJavaSdkLibraryFiles,
926 FixtureWithLastReleaseApis("bar"),
Paul Duffin05f72de2021-03-12 21:28:51 +0000927 android.FixtureWithRootAndroidBp(bpFile),
928 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
929 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
930 if info.enforceVendorInterface {
931 variables.DeviceVndkVersion = proptools.StringPtr("current")
932 }
933 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
934 variables.InterPartitionJavaLibraryAllowList = info.allowList
935 }),
936 )
JaeMan Parkff715562020-10-19 17:25:58 +0900937 }
938
Paul Duffinaa6caa72021-03-13 16:50:15 +0000939 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
940 t.Run(fmt.Sprintf("%#v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000941 errorHandler := android.FixtureExpectsNoErrors
942 if expectedErrorPattern != "" {
943 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000944 }
Paul Duffin05f72de2021-03-12 21:28:51 +0000945 javaFixtureFactory.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
Paul Duffinaa6caa72021-03-13 16:50:15 +0000946 })
947 }
948
JaeMan Parkff715562020-10-19 17:25:58 +0900949 errorMessage := "is not allowed across the partitions"
950
Paul Duffinaa6caa72021-03-13 16:50:15 +0000951 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900952 libraryType: "java_library",
953 fromPartition: "product",
954 toPartition: "system",
955 enforceVendorInterface: true,
956 enforceProductInterface: true,
957 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000958 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900959
Paul Duffinaa6caa72021-03-13 16:50:15 +0000960 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900961 libraryType: "java_library",
962 fromPartition: "product",
963 toPartition: "system",
964 enforceVendorInterface: true,
965 enforceProductInterface: false,
966 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000967 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900968
Paul Duffinaa6caa72021-03-13 16:50:15 +0000969 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900970 libraryType: "java_library",
971 fromPartition: "product",
972 toPartition: "system",
973 enforceVendorInterface: true,
974 enforceProductInterface: true,
975 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000976 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900977
Paul Duffinaa6caa72021-03-13 16:50:15 +0000978 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900979 libraryType: "java_library",
980 fromPartition: "vendor",
981 toPartition: "system",
982 enforceVendorInterface: true,
983 enforceProductInterface: true,
984 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000985 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900986
Paul Duffinaa6caa72021-03-13 16:50:15 +0000987 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900988 libraryType: "java_library",
989 fromPartition: "vendor",
990 toPartition: "system",
991 enforceVendorInterface: true,
992 enforceProductInterface: true,
993 enforceJavaSdkLibraryCheck: true,
994 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +0000995 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900996
Paul Duffinaa6caa72021-03-13 16:50:15 +0000997 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900998 libraryType: "java_library",
999 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +09001000 toPartition: "product",
1001 enforceVendorInterface: true,
1002 enforceProductInterface: true,
1003 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001004 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +09001005
Paul Duffinaa6caa72021-03-13 16:50:15 +00001006 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001007 libraryType: "java_sdk_library",
1008 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +09001009 toPartition: "system",
1010 enforceVendorInterface: true,
1011 enforceProductInterface: true,
1012 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001013 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001014
Paul Duffinaa6caa72021-03-13 16:50:15 +00001015 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001016 libraryType: "java_sdk_library",
1017 fromPartition: "vendor",
1018 toPartition: "system",
1019 enforceVendorInterface: true,
1020 enforceProductInterface: true,
1021 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001022 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001023
Paul Duffinaa6caa72021-03-13 16:50:15 +00001024 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001025 libraryType: "java_sdk_library",
1026 fromPartition: "vendor",
1027 toPartition: "product",
1028 enforceVendorInterface: true,
1029 enforceProductInterface: true,
1030 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001031 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +09001032}
1033
Colin Cross89536d42017-07-07 14:35:50 -07001034func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001035 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -07001036 java_defaults {
1037 name: "defaults",
1038 srcs: ["a.java"],
1039 libs: ["bar"],
1040 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -07001041 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -07001042 }
1043
1044 java_library {
1045 name: "foo",
1046 defaults: ["defaults"],
1047 }
1048
1049 java_library {
1050 name: "bar",
1051 srcs: ["b.java"],
1052 }
1053
1054 java_library {
1055 name: "baz",
1056 srcs: ["c.java"],
1057 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001058
1059 android_test {
1060 name: "atestOptimize",
1061 defaults: ["defaults"],
1062 optimize: {enabled: true},
1063 }
1064
1065 android_test {
1066 name: "atestNoOptimize",
1067 defaults: ["defaults"],
1068 }
1069
1070 android_test {
1071 name: "atestDefault",
1072 srcs: ["a.java"],
1073 }
Colin Cross89536d42017-07-07 14:35:50 -07001074 `)
1075
Colin Cross4c428df2017-09-15 17:36:05 -07001076 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001077 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001078
1079 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1080 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1081 }
1082
Nan Zhanged19fc32017-10-19 13:06:22 -07001083 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1084 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1085 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001086 }
1087
Colin Cross1ee23172017-10-18 14:44:18 -07001088 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001089 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1090 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001091 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001092
1093 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1094 if atestOptimize.Output == nil {
1095 t.Errorf("atestOptimize should optimize APK")
1096 }
1097
1098 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1099 if atestNoOptimize.Output == nil {
1100 t.Errorf("atestNoOptimize should not optimize APK")
1101 }
1102
1103 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1104 if atestDefault.Output == nil {
1105 t.Errorf("atestDefault should optimize APK")
1106 }
Colin Cross89536d42017-07-07 14:35:50 -07001107}
1108
Colin Cross0f37af02017-09-27 17:42:05 -07001109func TestResources(t *testing.T) {
1110 var table = []struct {
1111 name string
1112 prop string
1113 extra string
1114 args string
1115 }{
1116 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001117 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001118 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001119 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001120 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001121 },
1122 {
1123 // Test that a module with java_resources includes the files
1124 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001125 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1126 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001127 },
1128 {
1129 // Test that a module with a filegroup in java_resources includes the files with the
1130 // path prefix
1131 name: "resource filegroup",
1132 prop: `java_resources: [":foo-res"]`,
1133 extra: `
1134 filegroup {
1135 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001136 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001137 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001138 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001139 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001140 },
1141 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001142 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1143 name: "wildcard dirs",
1144 prop: `java_resource_dirs: ["java-res/*"]`,
1145 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1146 },
1147 {
1148 // Test that a module exclude_java_resource_dirs excludes the files
1149 name: "wildcard dirs",
1150 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1151 args: "-C java-res/a -f java-res/a/a",
1152 },
Colin Crosscedd4762018-09-13 11:26:19 -07001153 {
1154 // Test wildcards in java_resources
1155 name: "wildcard files",
1156 prop: `java_resources: ["java-res/**/*"]`,
1157 args: "-C . -f java-res/a/a -f java-res/b/b",
1158 },
1159 {
1160 // Test exclude_java_resources with java_resources
1161 name: "wildcard files with exclude",
1162 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1163 args: "-C . -f java-res/a/a",
1164 },
1165 {
1166 // Test exclude_java_resources with java_resource_dirs
1167 name: "resource dirs with exclude files",
1168 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1169 args: "-C java-res -f java-res/a/a",
1170 },
1171 {
1172 // Test exclude_java_resource_dirs with java_resource_dirs
1173 name: "resource dirs with exclude files",
1174 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1175 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1176 },
Colin Cross0f37af02017-09-27 17:42:05 -07001177 }
1178
1179 for _, test := range table {
1180 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001181 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001182 java_library {
1183 name: "foo",
1184 srcs: [
1185 "a.java",
1186 "b.java",
1187 "c.java",
1188 ],
1189 `+test.prop+`,
1190 }
Colin Cross238c1f32020-06-07 16:58:18 -07001191 `+test.extra,
1192 map[string][]byte{
1193 "java-res/a/a": nil,
1194 "java-res/b/b": nil,
1195 "java-res2/a": nil,
1196 },
1197 )
Colin Cross0f37af02017-09-27 17:42:05 -07001198
Colin Cross331a1212018-08-15 20:40:52 -07001199 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001200 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001201
1202 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1203 t.Errorf("foo combined jars %v does not contain %q",
1204 foo.Inputs.Strings(), fooRes.Output.String())
1205 }
1206
Colin Crossaf9c55b2017-10-03 14:50:08 -07001207 if fooRes.Args["jarArgs"] != test.args {
1208 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001209 fooRes.Args["jarArgs"], test.args)
1210 }
1211 })
1212 }
1213}
1214
Colin Cross0c4ce212019-05-03 15:28:19 -07001215func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001216 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001217 java_library {
1218 name: "foo",
1219 srcs: [
1220 "a.java",
1221 "b.java",
1222 "c.java",
1223 ],
1224 include_srcs: true,
1225 }
1226
1227 java_library {
1228 name: "bar",
1229 srcs: [
1230 "a.java",
1231 "b.java",
1232 "c.java",
1233 ],
1234 java_resource_dirs: ["java-res"],
1235 include_srcs: true,
1236 }
Colin Cross238c1f32020-06-07 16:58:18 -07001237 `, map[string][]byte{
1238 "java-res/a/a": nil,
1239 "java-res/b/b": nil,
1240 "java-res2/a": nil,
1241 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001242
1243 // Test a library with include_srcs: true
1244 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1245 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1246
1247 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1248 t.Errorf("foo combined jars %v does not contain %q", w, g)
1249 }
1250
1251 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1252 t.Errorf("foo source jar args %q is not %q", w, g)
1253 }
1254
1255 // Test a library with include_srcs: true and resources
1256 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1257 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1258 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1259 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1260
1261 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1262 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1263 }
1264
1265 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1266 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1267 }
1268
1269 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1270 t.Errorf("bar combined jars %v does not contain %q", w, g)
1271 }
1272
1273 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1274 t.Errorf("bar source jar args %q is not %q", w, g)
1275 }
1276
1277 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1278 t.Errorf("bar resource jar args %q is not %q", w, g)
1279 }
1280}
1281
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001282func TestJavaLint(t *testing.T) {
1283 ctx, _ := testJavaWithFS(t, `
1284 java_library {
1285 name: "foo",
1286 srcs: [
1287 "a.java",
1288 "b.java",
1289 "c.java",
1290 ],
1291 min_sdk_version: "29",
1292 sdk_version: "system_current",
1293 }
1294 `, map[string][]byte{
1295 "lint-baseline.xml": nil,
1296 })
1297
1298 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001299
Colin Cross1661aff2021-03-12 17:56:51 -08001300 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1301 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001302 t.Error("did not pass --baseline flag")
1303 }
1304}
1305
1306func TestJavaLintWithoutBaseline(t *testing.T) {
1307 ctx, _ := testJavaWithFS(t, `
1308 java_library {
1309 name: "foo",
1310 srcs: [
1311 "a.java",
1312 "b.java",
1313 "c.java",
1314 ],
1315 min_sdk_version: "29",
1316 sdk_version: "system_current",
1317 }
1318 `, map[string][]byte{})
1319
1320 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001321
Colin Cross1661aff2021-03-12 17:56:51 -08001322 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1323 if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001324 t.Error("passed --baseline flag for non existent file")
1325 }
1326}
1327
1328func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
1329 config := testConfig(
1330 nil,
1331 `
1332 java_library {
1333 name: "foo",
1334 srcs: [
1335 ],
1336 min_sdk_version: "29",
1337 sdk_version: "system_current",
1338 lint: {
1339 baseline_filename: "mybaseline.xml",
1340 },
1341 }
1342 `, map[string][]byte{
1343 "build/soong/java/lint_defaults.txt": nil,
1344 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1345 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1346 "framework/aidl": nil,
1347 "a.java": nil,
1348 "AndroidManifest.xml": nil,
1349 "build/make/target/product/security": nil,
1350 })
1351 config.TestAllowNonExistentPaths = false
1352 testJavaErrorWithConfig(t,
1353 "source path \"mybaseline.xml\" does not exist",
1354 config,
1355 )
1356}
1357
1358func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1359 ctx, _ := testJavaWithFS(t, `
1360 java_library {
1361 name: "foo",
1362 srcs: [
1363 "a.java",
1364 "b.java",
1365 "c.java",
1366 ],
1367 min_sdk_version: "29",
1368 sdk_version: "system_current",
1369 lint: {
1370 error_checks: ["SomeCheck"],
1371 baseline_filename: "mybaseline.xml",
1372 },
1373 }
1374 `, map[string][]byte{
1375 "mybaseline.xml": nil,
1376 })
1377
1378 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001379
Colin Cross1661aff2021-03-12 17:56:51 -08001380 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1381 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001382 t.Error("did not use the correct file for baseline")
1383 }
1384}
1385
Colin Cross54190b32017-10-09 15:34:10 -07001386func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001387 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001388 java_library {
1389 name: "foo",
1390 srcs: [
1391 "a*.java",
1392 ":gen",
1393 "b*.java",
1394 ],
1395 }
1396
1397 genrule {
1398 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001399 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001400 out: ["gen.java"],
1401 }
Colin Cross238c1f32020-06-07 16:58:18 -07001402 `, map[string][]byte{
1403 "a.java": nil,
1404 "b.java": nil,
1405 })
Colin Cross54190b32017-10-09 15:34:10 -07001406
1407 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1408 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1409
Colin Cross15e86d92017-10-20 15:07:08 -07001410 if filepath.Base(genrule.Output.String()) != "gen.java" {
1411 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001412 }
1413
1414 if len(javac.Inputs) != 3 ||
1415 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001416 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001417 javac.Inputs[2].String() != "b.java" {
1418 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1419 }
1420}
1421
Nan Zhang61eaedb2017-11-02 13:28:15 -07001422func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001423 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001424 java_library {
1425 name: "foo",
1426 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001427 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001428 }
1429
1430 java_library {
1431 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001432 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001433 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001434 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001435 }
1436
1437 java_library {
1438 name: "baz",
1439 srcs: ["c.java"],
1440 libs: ["bar"],
1441 sdk_version: "14",
1442 }
1443 `)
1444
1445 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1446 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1447 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1448 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1449 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1450
1451 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1452 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1453 }
1454
1455 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1456 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1457 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1458 }
1459 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1460 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1461 }
1462 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1463 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1464 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001465 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001466 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001467 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001468 }
1469}
1470
1471func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001472 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001473 java_library {
1474 name: "bar",
1475 srcs: ["a.java","b.java","c.java"],
1476 javac_shard_size: 1
1477 }
1478 `)
1479
1480 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1481 for i := 0; i < 3; i++ {
1482 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1483 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1484 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1485 }
1486 }
1487}
1488
Nan Zhang581fd212018-01-10 16:06:12 -08001489func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001490 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001491 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001492 name: "droiddoc-templates-sdk",
1493 path: ".",
1494 }
Jiyong Park29074592019-07-07 16:27:47 +09001495 filegroup {
1496 name: "bar-doc-aidl-srcs",
1497 srcs: ["bar-doc/IBar.aidl"],
1498 path: "bar-doc",
1499 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001500 droidstubs {
1501 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001502 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001503 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001504 ],
Steve Kim3666c702020-09-01 17:58:01 +00001505 exclude_srcs: [
1506 "bar-doc/b.java"
1507 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001508 api_levels_annotations_dirs: [
1509 "droiddoc-templates-sdk",
1510 ],
1511 api_levels_annotations_enabled: true,
1512 }
1513 droiddoc {
1514 name: "bar-doc",
1515 srcs: [
1516 ":bar-stubs",
1517 "bar-doc/IFoo.aidl",
1518 ":bar-doc-aidl-srcs",
1519 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001520 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001521 hdf: [
1522 "android.whichdoc offline",
1523 ],
1524 knowntags: [
1525 "bar-doc/known_oj_tags.txt",
1526 ],
1527 proofread_file: "libcore-proofread.txt",
1528 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001529 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001530 }
Colin Cross238c1f32020-06-07 16:58:18 -07001531 `,
1532 map[string][]byte{
1533 "bar-doc/a.java": nil,
1534 "bar-doc/b.java": nil,
1535 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001536 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1537 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1538 if err != nil {
1539 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1540 }
1541 if len(barStubsOutputs) != 1 {
1542 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001543 }
Nan Zhang581fd212018-01-10 16:06:12 -08001544
Liz Kammere1ab2502020-09-10 15:29:25 +00001545 barStubsOutput := barStubsOutputs[0]
1546 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1547 javaDoc := barDoc.Rule("javadoc")
1548 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1549 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001550 }
1551
Liz Kammere1ab2502020-09-10 15:29:25 +00001552 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1553 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1554 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1555 }
1556
1557 aidl := barDoc.Rule("aidl")
1558 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001559 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1560 }
1561
1562 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1563 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001564 }
Nan Zhang581fd212018-01-10 16:06:12 -08001565}
1566
Liz Kammer585cac22020-07-06 09:12:57 -07001567func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1568 testJavaError(t, "flags is set. Cannot set args", `
1569 droiddoc_exported_dir {
1570 name: "droiddoc-templates-sdk",
1571 path: ".",
1572 }
1573 filegroup {
1574 name: "bar-doc-aidl-srcs",
1575 srcs: ["bar-doc/IBar.aidl"],
1576 path: "bar-doc",
1577 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001578 droidstubs {
1579 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001580 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001581 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001582 ],
Steve Kim3666c702020-09-01 17:58:01 +00001583 exclude_srcs: [
1584 "bar-doc/b.java"
1585 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001586 api_levels_annotations_dirs: [
1587 "droiddoc-templates-sdk",
1588 ],
1589 api_levels_annotations_enabled: true,
1590 }
1591 droiddoc {
1592 name: "bar-doc",
1593 srcs: [
1594 ":bar-stubs",
1595 "bar-doc/IFoo.aidl",
1596 ":bar-doc-aidl-srcs",
1597 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001598 custom_template: "droiddoc-templates-sdk",
1599 hdf: [
1600 "android.whichdoc offline",
1601 ],
1602 knowntags: [
1603 "bar-doc/known_oj_tags.txt",
1604 ],
1605 proofread_file: "libcore-proofread.txt",
1606 todo_file: "libcore-docs-todo.html",
1607 flags: ["-offlinemode -title \"libcore\""],
1608 args: "-offlinemode -title \"libcore\"",
1609 }
1610 `)
1611}
1612
Liz Kammer3d894b72020-08-04 09:55:13 -07001613func TestDroidstubs(t *testing.T) {
1614 ctx, _ := testJavaWithFS(t, `
1615 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001616 name: "droiddoc-templates-sdk",
1617 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001618 }
1619
1620 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001621 name: "bar-stubs",
1622 srcs: ["bar-doc/a.java"],
1623 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1624 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001625 }
1626
1627 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001628 name: "bar-stubs-other",
1629 srcs: ["bar-doc/a.java"],
1630 high_mem: true,
1631 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1632 api_levels_annotations_enabled: true,
1633 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001634 }
1635 `,
1636 map[string][]byte{
1637 "bar-doc/a.java": nil,
1638 })
1639 testcases := []struct {
1640 moduleName string
1641 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001642 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001643 }{
1644 {
1645 moduleName: "bar-stubs",
1646 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001647 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001648 },
1649 {
1650 moduleName: "bar-stubs-other",
1651 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001652 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001653 },
1654 }
1655 for _, c := range testcases {
1656 m := ctx.ModuleForTests(c.moduleName, "android_common")
1657 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001658 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001659 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001660 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001661 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1662 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001663
1664 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1665 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1666 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001667 }
1668}
1669
Paul Duffin83a2d962019-11-19 19:44:10 +00001670func TestDroidstubsWithSystemModules(t *testing.T) {
1671 ctx, _ := testJava(t, `
1672 droidstubs {
1673 name: "stubs-source-system-modules",
1674 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001675 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001676 ],
1677 sdk_version: "none",
1678 system_modules: "source-system-modules",
1679 }
1680
1681 java_library {
1682 name: "source-jar",
1683 srcs: [
1684 "a.java",
1685 ],
1686 }
1687
1688 java_system_modules {
1689 name: "source-system-modules",
1690 libs: ["source-jar"],
1691 }
1692
1693 droidstubs {
1694 name: "stubs-prebuilt-system-modules",
1695 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001696 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001697 ],
1698 sdk_version: "none",
1699 system_modules: "prebuilt-system-modules",
1700 }
1701
1702 java_import {
1703 name: "prebuilt-jar",
1704 jars: ["a.jar"],
1705 }
1706
1707 java_system_modules_import {
1708 name: "prebuilt-system-modules",
1709 libs: ["prebuilt-jar"],
1710 }
1711 `)
1712
1713 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1714
1715 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1716}
1717
1718func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1719 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1720 var systemJars []string
1721 for _, i := range metalavaRule.Implicits {
1722 systemJars = append(systemJars, i.Base())
1723 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001724 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001725 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1726 }
1727}
1728
Colin Cross54250902017-12-05 09:28:08 -08001729func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001730 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001731 java_library {
1732 name: "foo",
1733 srcs: ["a.java"],
1734 }
1735
1736 java_genrule {
1737 name: "jargen",
1738 tool_files: ["b.java"],
1739 cmd: "$(location b.java) $(in) $(out)",
1740 out: ["jargen.jar"],
1741 srcs: [":foo"],
1742 }
1743
1744 java_library {
1745 name: "bar",
1746 static_libs: ["jargen"],
1747 srcs: ["c.java"],
1748 }
1749
1750 java_library {
1751 name: "baz",
1752 libs: ["jargen"],
1753 srcs: ["c.java"],
1754 }
1755 `)
1756
1757 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1758 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1759 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1760 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1761 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1762
Colin Cross3d680512020-11-13 16:23:53 -08001763 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1764 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001765 }
1766
1767 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1768 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1769 }
1770
1771 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1772 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1773 }
1774
1775 if len(barCombined.Inputs) != 2 ||
1776 barCombined.Inputs[0].String() != bar.Output.String() ||
1777 barCombined.Inputs[1].String() != jargen.Output.String() {
1778 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1779 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1780 }
1781}
1782
Nan Zhang27e284d2018-02-09 21:03:53 +00001783func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001784 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001785 java_library {
1786 name: "foo",
1787 srcs: ["a.java", ":foo-srcs"],
1788 exclude_srcs: ["a.java", ":foo-excludes"],
1789 }
1790
1791 filegroup {
1792 name: "foo-srcs",
1793 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1794 }
1795
1796 filegroup {
1797 name: "foo-excludes",
1798 srcs: ["java-fg/a.java", "java-fg/b.java"],
1799 }
1800 `)
1801
1802 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1803
1804 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1805 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1806 }
1807}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001808
Paul Duffin52d398a2019-06-11 12:31:14 +01001809func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001810 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001811 "libcore/Android.bp": []byte(`
1812 java_library {
1813 name: "core",
1814 sdk_version: "none",
1815 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001816 }
1817
1818 filegroup {
1819 name: "core-jar",
1820 srcs: [":core{.jar}"],
1821 }
1822`),
1823 })
Colin Crossae8600b2020-10-29 17:09:13 -07001824 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001825 run(t, ctx, config)
1826}
1827
1828func TestJavaImport(t *testing.T) {
1829 config := testConfig(nil, "", map[string][]byte{
1830 "libcore/Android.bp": []byte(`
1831 java_import {
1832 name: "core",
1833 sdk_version: "none",
1834 }
1835
1836 filegroup {
1837 name: "core-jar",
1838 srcs: [":core{.jar}"],
1839 }
1840`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001841 })
Colin Crossae8600b2020-10-29 17:09:13 -07001842 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001843 run(t, ctx, config)
1844}
1845
Jiyong Parkc678ad32018-04-10 13:07:10 +09001846func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001847 result := javaFixtureFactory.Extend(
1848 PrepareForTestWithJavaSdkLibraryFiles,
1849 FixtureWithPrebuiltApis(map[string][]string{
1850 "28": {"foo"},
1851 "29": {"foo"},
1852 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1853 }),
1854 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001855 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001856 name: "droiddoc-templates-sdk",
1857 path: ".",
1858 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001859 java_sdk_library {
1860 name: "foo",
1861 srcs: ["a.java", "b.java"],
1862 api_packages: ["foo"],
1863 }
1864 java_sdk_library {
1865 name: "bar",
1866 srcs: ["a.java", "b.java"],
1867 api_packages: ["bar"],
1868 }
1869 java_library {
1870 name: "baz",
1871 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001872 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001873 sdk_version: "system_current",
1874 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001875 java_sdk_library {
1876 name: "barney",
1877 srcs: ["c.java"],
1878 api_only: true,
1879 }
1880 java_sdk_library {
1881 name: "betty",
1882 srcs: ["c.java"],
1883 shared_library: false,
1884 }
Paul Duffin859fe962020-05-15 10:20:31 +01001885 java_sdk_library_import {
1886 name: "quuz",
1887 public: {
1888 jars: ["c.jar"],
1889 },
1890 }
1891 java_sdk_library_import {
1892 name: "fred",
1893 public: {
1894 jars: ["b.jar"],
1895 },
1896 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001897 java_sdk_library_import {
1898 name: "wilma",
1899 public: {
1900 jars: ["b.jar"],
1901 },
1902 shared_library: false,
1903 }
Jiyong Park1be96912018-05-28 18:02:19 +09001904 java_library {
1905 name: "qux",
1906 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001907 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001908 sdk_version: "system_current",
1909 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001910 java_library {
1911 name: "baz-test",
1912 srcs: ["c.java"],
1913 libs: ["foo"],
1914 sdk_version: "test_current",
1915 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001916 java_library {
1917 name: "baz-29",
1918 srcs: ["c.java"],
1919 libs: ["foo"],
1920 sdk_version: "system_29",
1921 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001922 java_library {
1923 name: "baz-module-30",
1924 srcs: ["c.java"],
1925 libs: ["foo"],
1926 sdk_version: "module_30",
1927 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001928 `)
1929
1930 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001931 result.ModuleForTests("foo", "android_common")
1932 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1933 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1934 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1935 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1936 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1937 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1938 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1939 result.ModuleForTests("foo.api.public.28", "")
1940 result.ModuleForTests("foo.api.system.28", "")
1941 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001942
Paul Duffin22b77cd2021-03-12 19:15:01 +00001943 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001944 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001945 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001946 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001947 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001948 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001949 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001950
Paul Duffin22b77cd2021-03-12 19:15:01 +00001951 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001952 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001953 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001954
Paul Duffin22b77cd2021-03-12 19:15:01 +00001955 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001956 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001957 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001958
Paul Duffin22b77cd2021-03-12 19:15:01 +00001959 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001960 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001961 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 +01001962
Jiyong Park1be96912018-05-28 18:02:19 +09001963 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001964 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001965 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001966 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001967 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001968 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001969}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001970
Anton Hansson7f66efa2020-10-08 14:47:23 +01001971func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001972 result := javaFixtureFactory.Extend(
1973 PrepareForTestWithJavaSdkLibraryFiles,
1974 FixtureWithLastReleaseApis("sdklib"),
1975 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001976 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001977 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001978 srcs: ["a.java"],
1979 impl_only_libs: ["foo"],
1980 stub_only_libs: ["bar"],
1981 }
1982 java_library {
1983 name: "foo",
1984 srcs: ["a.java"],
1985 sdk_version: "current",
1986 }
1987 java_library {
1988 name: "bar",
1989 srcs: ["a.java"],
1990 sdk_version: "current",
1991 }
1992 `)
1993
Anton Hanssondff2c782020-12-21 17:10:01 +00001994 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001995 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001996 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1997 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1998 }
1999 }
Anton Hanssondff2c782020-12-21 17:10:01 +00002000 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00002001 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01002002 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
2003 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
2004 }
2005}
2006
Paul Duffindaaa3322020-05-26 18:13:57 +01002007func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002008 result := javaFixtureFactory.Extend(
2009 PrepareForTestWithJavaSdkLibraryFiles,
2010 FixtureWithLastReleaseApis("foo"),
2011 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01002012 java_sdk_library {
2013 name: "foo",
2014 srcs: ["a.java"],
2015 api_only: true,
2016 public: {
2017 enabled: true,
2018 },
2019 }
2020
2021 java_library {
2022 name: "bar",
2023 srcs: ["b.java"],
2024 libs: ["foo"],
2025 }
2026 `)
2027
2028 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002029 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01002030 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2031 t.Errorf("expected %q, found %#q", expected, actual)
2032 }
2033}
2034
Paul Duffin46dc45a2020-05-14 15:39:10 +01002035func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002036 javaFixtureFactory.Extend(
2037 PrepareForTestWithJavaSdkLibraryFiles,
2038 FixtureWithLastReleaseApis("foo"),
2039 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002040 java_sdk_library {
2041 name: "foo",
2042 srcs: ["a.java"],
2043 api_packages: ["foo"],
2044 public: {
2045 enabled: true,
2046 },
2047 }
2048
2049 java_library {
2050 name: "bar",
2051 srcs: ["b.java", ":foo{.public.stubs.source}"],
2052 }
2053 `)
2054}
2055
2056func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002057 javaFixtureFactory.
Paul Duffin163043d2021-03-12 19:18:49 +00002058 Extend(
2059 PrepareForTestWithJavaSdkLibraryFiles,
2060 FixtureWithLastReleaseApis("foo"),
2061 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00002062 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
2063 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002064 java_sdk_library {
2065 name: "foo",
2066 srcs: ["a.java"],
2067 api_packages: ["foo"],
2068 public: {
2069 enabled: true,
2070 },
2071 }
2072
2073 java_library {
2074 name: "bar",
2075 srcs: ["b.java", ":foo{.system.stubs.source}"],
2076 }
2077 `)
2078}
2079
Paul Duffinca8d9a52020-06-26 22:20:25 +01002080func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002081 result := javaFixtureFactory.Extend(
2082 PrepareForTestWithJavaSdkLibraryFiles,
2083 FixtureWithLastReleaseApis("sdklib"),
2084 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01002085 java_sdk_library {
2086 name: "sdklib",
2087 srcs: ["a.java"],
2088 sdk_version: "none",
2089 system_modules: "none",
2090 public: {
2091 enabled: true,
2092 },
2093 }
2094 `)
2095
Paul Duffin22b77cd2021-03-12 19:15:01 +00002096 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01002097 `dex2oatd`,
2098 `sdklib.impl`,
2099 `sdklib.stubs`,
2100 `sdklib.stubs.source`,
2101 `sdklib.xml`,
2102 })
2103}
2104
Paul Duffin46dc45a2020-05-14 15:39:10 +01002105func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002106 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002107 java_sdk_library_import {
2108 name: "foo",
2109 public: {
2110 jars: ["a.jar"],
2111 stub_srcs: ["a.java"],
2112 current_api: "api/current.txt",
2113 removed_api: "api/removed.txt",
2114 },
2115 }
2116
2117 java_library {
2118 name: "bar",
2119 srcs: [":foo{.public.stubs.source}"],
2120 java_resources: [
2121 ":foo{.public.api.txt}",
2122 ":foo{.public.removed-api.txt}",
2123 ],
2124 }
2125 `)
2126}
2127
2128func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2129 bp := `
2130 java_sdk_library_import {
2131 name: "foo",
2132 public: {
2133 jars: ["a.jar"],
2134 },
2135 }
2136 `
2137
2138 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002139 javaFixtureFactory.
2140 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
2141 RunTestWithBp(t, bp+`
2142 java_library {
2143 name: "bar",
2144 srcs: [":foo{.public.stubs.source}"],
2145 java_resources: [
2146 ":foo{.public.api.txt}",
2147 ":foo{.public.removed-api.txt}",
2148 ],
2149 }
2150 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002151 })
2152
2153 t.Run("api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002154 javaFixtureFactory.
2155 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
2156 RunTestWithBp(t, bp+`
2157 java_library {
2158 name: "bar",
2159 srcs: ["a.java"],
2160 java_resources: [
2161 ":foo{.public.api.txt}",
2162 ],
2163 }
2164 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002165 })
2166
2167 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002168 javaFixtureFactory.
2169 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
2170 RunTestWithBp(t, bp+`
2171 java_library {
2172 name: "bar",
2173 srcs: ["a.java"],
2174 java_resources: [
2175 ":foo{.public.removed-api.txt}",
2176 ],
2177 }
2178 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002179 })
2180}
2181
Paul Duffin3375e352020-04-28 10:44:03 +01002182func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002183 javaFixtureFactory.
2184 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
2185 RunTestWithBp(t, `
2186 java_sdk_library {
2187 name: "foo",
2188 srcs: ["a.java", "b.java"],
2189 api_packages: ["foo"],
2190 // Explicitly disable public to test the check that ensures the set of enabled
2191 // scopes is consistent.
2192 public: {
2193 enabled: false,
2194 },
2195 system: {
2196 enabled: true,
2197 },
2198 }
Paul Duffin3375e352020-04-28 10:44:03 +01002199 `)
2200}
2201
Paul Duffin87a05a32020-05-12 11:50:28 +01002202func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002203 javaFixtureFactory.Extend(
2204 PrepareForTestWithJavaSdkLibraryFiles,
2205 FixtureWithLastReleaseApis("foo"),
2206 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002207 java_sdk_library {
2208 name: "foo",
2209 srcs: ["a.java", "b.java"],
2210 api_packages: ["foo"],
2211 system: {
2212 enabled: true,
2213 sdk_version: "module_current",
2214 },
2215 }
2216 `)
2217}
2218
Paul Duffin0c5bae52020-06-02 13:00:08 +01002219func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002220 javaFixtureFactory.Extend(
2221 PrepareForTestWithJavaSdkLibraryFiles,
2222 FixtureWithLastReleaseApis("foo"),
2223 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002224 java_sdk_library {
2225 name: "foo",
2226 srcs: ["a.java", "b.java"],
2227 api_packages: ["foo"],
2228 system: {
2229 enabled: true,
2230 },
2231 module_lib: {
2232 enabled: true,
2233 },
2234 }
2235 `)
2236}
2237
2238func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002239 javaFixtureFactory.Extend(
2240 PrepareForTestWithJavaSdkLibraryFiles,
2241 FixtureWithLastReleaseApis("foo"),
2242 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002243 java_sdk_library {
2244 name: "foo",
2245 srcs: ["a.java", "b.java"],
2246 api_packages: ["foo"],
2247 system: {
2248 enabled: true,
2249 },
2250 system_server: {
2251 enabled: true,
2252 },
2253 }
2254 `)
2255}
2256
Paul Duffin803a9562020-05-20 11:52:25 +01002257func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002258 javaFixtureFactory.
2259 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2260 RunTestWithBp(t, `
2261 java_sdk_library {
2262 name: "foo",
2263 srcs: ["a.java"],
2264 public: {
2265 enabled: false,
2266 },
2267 }
Paul Duffin803a9562020-05-20 11:52:25 +01002268
Paul Duffin22b77cd2021-03-12 19:15:01 +00002269 java_library {
2270 name: "baz",
2271 srcs: ["a.java"],
2272 libs: ["foo"],
2273 sdk_version: "module_current",
2274 }
Paul Duffin803a9562020-05-20 11:52:25 +01002275 `)
2276}
2277
2278func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002279 javaFixtureFactory.Extend(
2280 PrepareForTestWithJavaSdkLibraryFiles,
2281 FixtureWithLastReleaseApis("foo"),
2282 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002283 java_sdk_library {
2284 name: "foo",
2285 srcs: ["a.java"],
2286 system: {
2287 enabled: true,
2288 },
2289 }
2290
2291 java_library {
2292 name: "baz",
2293 srcs: ["a.java"],
2294 libs: ["foo"],
2295 // foo does not have module-lib scope so it should fallback to system
2296 sdk_version: "module_current",
2297 }
2298 `)
2299}
2300
Jiyong Park932cdfe2020-05-28 00:19:53 +09002301func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002302 result := javaFixtureFactory.Extend(
2303 PrepareForTestWithJavaSdkLibraryFiles,
2304 FixtureWithLastReleaseApis("foo"),
2305 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002306 java_sdk_library {
2307 name: "foo",
2308 srcs: ["a.java"],
2309 system: {
2310 enabled: true,
2311 },
2312 default_to_stubs: true,
2313 }
2314
2315 java_library {
2316 name: "baz",
2317 srcs: ["a.java"],
2318 libs: ["foo"],
2319 // does not have sdk_version set, should fallback to module,
2320 // which will then fallback to system because the module scope
2321 // is not enabled.
2322 }
2323 `)
2324 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002325 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002326 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2327 t.Errorf("expected %q, found %#q", expected, actual)
2328 }
2329}
2330
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002331var compilerFlagsTestCases = []struct {
2332 in string
2333 out bool
2334}{
2335 {
2336 in: "a",
2337 out: false,
2338 },
2339 {
2340 in: "-a",
2341 out: true,
2342 },
2343 {
2344 in: "-no-jdk",
2345 out: false,
2346 },
2347 {
2348 in: "-no-stdlib",
2349 out: false,
2350 },
2351 {
2352 in: "-kotlin-home",
2353 out: false,
2354 },
2355 {
2356 in: "-kotlin-home /some/path",
2357 out: false,
2358 },
2359 {
2360 in: "-include-runtime",
2361 out: false,
2362 },
2363 {
2364 in: "-Xintellij-plugin-root",
2365 out: false,
2366 },
2367}
2368
2369type mockContext struct {
2370 android.ModuleContext
2371 result bool
2372}
2373
2374func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2375 // CheckBadCompilerFlags calls this function when the flag should be rejected
2376 ctx.result = false
2377}
2378
2379func TestCompilerFlags(t *testing.T) {
2380 for _, testCase := range compilerFlagsTestCases {
2381 ctx := &mockContext{result: true}
2382 CheckKotlincFlags(ctx, []string{testCase.in})
2383 if ctx.result != testCase.out {
2384 t.Errorf("incorrect output:")
2385 t.Errorf(" input: %#v", testCase.in)
2386 t.Errorf(" expected: %#v", testCase.out)
2387 t.Errorf(" got: %#v", ctx.result)
2388 }
2389 }
2390}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002391
2392// TODO(jungjw): Consider making this more robust by ignoring path order.
2393func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2394 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2395 flags := strings.Split(variables["javacFlags"], " ")
2396 got := ""
2397 for _, flag := range flags {
2398 keyEnd := strings.Index(flag, "=")
2399 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2400 got = flag[keyEnd+1:]
2401 break
2402 }
2403 }
2404 if expected != got {
2405 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2406 }
2407}
2408
2409func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002410 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002411 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002412 bp := `
2413 java_library {
2414 name: "foo",
2415 srcs: ["a.java"],
2416 java_version: "1.8",
2417 }
2418
2419 java_library {
2420 name: "bar",
2421 srcs: ["b.java"],
2422 sdk_version: "none",
2423 system_modules: "none",
2424 patch_module: "java.base",
2425 java_version: "1.8",
2426 }
2427
2428 java_library {
2429 name: "baz",
2430 srcs: ["c.java"],
2431 patch_module: "java.base",
2432 java_version: "1.8",
2433 }
2434 `
2435 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002436
2437 checkPatchModuleFlag(t, ctx, "foo", "")
2438 checkPatchModuleFlag(t, ctx, "bar", "")
2439 checkPatchModuleFlag(t, ctx, "baz", "")
2440 })
2441
Pete Gillin0c2143e2019-05-02 15:32:11 +01002442 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002443 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002444 bp := `
2445 java_library {
2446 name: "foo",
2447 srcs: ["a.java"],
2448 }
2449
2450 java_library {
2451 name: "bar",
2452 srcs: ["b.java"],
2453 sdk_version: "none",
2454 system_modules: "none",
2455 patch_module: "java.base",
2456 }
2457
2458 java_library {
2459 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002460 srcs: [
2461 "c.java",
2462 // Tests for b/150878007
2463 "dir/d.java",
2464 "dir2/e.java",
2465 "dir2/f.java",
2466 "nested/dir/g.java"
2467 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002468 patch_module: "java.base",
2469 }
2470 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002471 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002472
2473 checkPatchModuleFlag(t, ctx, "foo", "")
2474 expected := "java.base=.:" + buildDir
2475 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002476 expected = "java.base=" + strings.Join([]string{
Paul Duffin95bdab42021-03-08 21:48:46 +00002477 ".", buildDir, "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002478 checkPatchModuleFlag(t, ctx, "baz", expected)
2479 })
2480}
Paul Duffina7b9f422020-01-10 17:12:18 +00002481
Paul Duffin83a2d962019-11-19 19:44:10 +00002482func TestJavaLibraryWithSystemModules(t *testing.T) {
2483 ctx, _ := testJava(t, `
2484 java_library {
2485 name: "lib-with-source-system-modules",
2486 srcs: [
2487 "a.java",
2488 ],
2489 sdk_version: "none",
2490 system_modules: "source-system-modules",
2491 }
2492
2493 java_library {
2494 name: "source-jar",
2495 srcs: [
2496 "a.java",
2497 ],
2498 }
2499
2500 java_system_modules {
2501 name: "source-system-modules",
2502 libs: ["source-jar"],
2503 }
2504
2505 java_library {
2506 name: "lib-with-prebuilt-system-modules",
2507 srcs: [
2508 "a.java",
2509 ],
2510 sdk_version: "none",
2511 system_modules: "prebuilt-system-modules",
2512 }
2513
2514 java_import {
2515 name: "prebuilt-jar",
2516 jars: ["a.jar"],
2517 }
2518
2519 java_system_modules_import {
2520 name: "prebuilt-system-modules",
2521 libs: ["prebuilt-jar"],
2522 }
2523 `)
2524
2525 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2526
2527 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2528}
2529
2530func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2531 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2532 bootClasspath := javacRule.Args["bootClasspath"]
2533 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2534 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2535 }
2536}
Jiyong Park19604de2020-03-24 16:44:11 +09002537
2538func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2539 ctx, _ := testJava(t, `
2540 java_library {
2541 name: "foo",
2542 srcs: ["aidl/foo/IFoo.aidl"],
2543 libs: ["bar"],
2544 }
2545
2546 java_import {
2547 name: "bar",
2548 jars: ["a.jar"],
2549 aidl: {
2550 export_include_dirs: ["aidl/bar"],
2551 },
2552 }
2553 `)
2554
2555 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2556 expectedAidlFlag := "-Iaidl/bar"
2557 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2558 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2559 }
2560}
Liz Kammerdd849a82020-06-12 16:38:45 -07002561
Jooyung Hane197d8b2021-01-05 10:33:16 +09002562func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2563 ctx, _ := testJava(t, `
2564 java_library {
2565 name: "foo",
2566 srcs: ["aidl/foo/IFoo.aidl"],
2567 aidl: { flags: ["-Werror"], },
2568 }
2569 `)
2570
2571 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2572 expectedAidlFlag := "-Werror"
2573 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2574 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2575 }
2576}
2577
Liz Kammerdd849a82020-06-12 16:38:45 -07002578func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002579 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002580 java_test_host {
2581 name: "foo",
2582 srcs: ["a.java"],
2583 data_native_bins: ["bin"]
2584 }
2585
2586 python_binary_host {
2587 name: "bin",
2588 srcs: ["bin.py"],
2589 }
2590 `)
2591
2592 buildOS := android.BuildOs.String()
2593
2594 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002595 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002596 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2597 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2598 if !reflect.DeepEqual(expected, actual) {
2599 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2600 }
2601}
Yuexi Ma627263f2021-03-04 13:47:56 -08002602
2603func TestDefaultInstallable(t *testing.T) {
2604 ctx, _ := testJava(t, `
2605 java_test_host {
2606 name: "foo"
2607 }
2608 `)
2609
2610 buildOS := android.BuildOs.String()
2611 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2612 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2613 module.properties.Installable)
2614}