blob: c5617d5a0fae38a968143eea398b31fe7c8359f8 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Paul Duffin1a393322020-11-18 16:36:47 +000018 "fmt"
Colin Cross72bb3632017-07-13 16:23:21 -070019 "io/ioutil"
20 "os"
21 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070022 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010023 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Paul Duffind6ceb862021-03-04 23:02:31 +000028 "android/soong/genrule"
Jeongik Cha28df2572019-11-11 10:46:36 +090029 "github.com/google/blueprint/proptools"
30
Colin Crossa4f08812018-10-02 22:03:40 -070031 "android/soong/android"
32 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080033 "android/soong/dexpreopt"
Liz Kammerdd849a82020-06-12 16:38:45 -070034 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070035)
36
37var buildDir string
38
39func setUp() {
40 var err error
41 buildDir, err = ioutil.TempDir("", "soong_java_test")
42 if err != nil {
43 panic(err)
44 }
45}
46
47func tearDown() {
48 os.RemoveAll(buildDir)
49}
50
Paul Duffin95bdab42021-03-08 21:48:46 +000051// Factory to use to create fixtures for tests in this package.
52var javaFixtureFactory = android.NewFixtureFactory(
53 &buildDir,
54 genrule.PrepareForTestWithGenRuleBuildComponents,
55 // Get the CC build components but not default modules.
56 cc.PrepareForTestWithCcBuildComponents,
57 // Include all the default java modules.
58 PrepareForTestWithJavaDefaultModules,
59 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
60 ctx.RegisterModuleType("java_plugin", PluginFactory)
61 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
62
63 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
64 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
65 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
66 }),
67 javaMockFS().AddToFixture(),
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)
Liz Kammerdd849a82020-06-12 16:38:45 -0700103 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Colin Cross89536d42017-07-07 14:35:50 -0700104 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +0100105 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +0000106
Liz Kammerdd849a82020-06-12 16:38:45 -0700107 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700108 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Paul Duffineafc16b2021-02-24 01:43:18 +0000109 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
110 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
Colin Crossa4f08812018-10-02 22:03:40 -0700111
Paul Duffin021f4e52020-07-30 16:04:17 +0100112 android.RegisterPrebuiltMutators(ctx)
113
Paul Duffind6ceb862021-03-04 23:02:31 +0000114 genrule.RegisterGenruleBuildComponents(ctx)
115
Colin Crossa4f08812018-10-02 22:03:40 -0700116 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000117 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700118
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700119 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
120 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
121 })
122
Colin Cross527012a2017-11-30 22:56:16 -0800123 return ctx
124}
125
Paul Duffin95bdab42021-03-08 21:48:46 +0000126// run is a legacy way of running tests of java modules.
127//
128// See testJava for an explanation as to how to stop using this deprecated method.
129//
130// deprecated
Colin Cross527012a2017-11-30 22:56:16 -0800131func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800132 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800133
Colin Cross98be1bb2019-12-13 20:41:13 -0800134 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000135 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800136
Colin Crossae8600b2020-10-29 17:09:13 -0700137 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100138 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800139 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700140 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800141 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800142}
143
Paul Duffin95bdab42021-03-08 21:48:46 +0000144// testJavaError is a legacy way of running tests of java modules that expect errors.
145//
146// See testJava for an explanation as to how to stop using this deprecated method.
147//
148// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900149func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900150 t.Helper()
Paul Duffin6bac49c2021-03-12 21:28:15 +0000151 result := javaFixtureFactory.
152 Extend(dexpreopt.PrepareForTestWithDexpreopt).
153 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
154 RunTestWithBp(t, bp)
155 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900156}
157
Paul Duffin95bdab42021-03-08 21:48:46 +0000158// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
159//
160// See testJava for an explanation as to how to stop using this deprecated method.
161//
162// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800163func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900164 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000165 // This must be done on the supplied config and not as part of the fixture because any changes to
166 // the fixture's config will be ignored when RunTestWithConfig replaces it.
Colin Cross98be1bb2019-12-13 20:41:13 -0800167 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000168 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Paul Duffin95bdab42021-03-08 21:48:46 +0000169 result := javaFixtureFactory.
170 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
171 RunTestWithConfig(t, config)
172 return result.TestContext, result.Config
Paul Duffinec0fe172021-02-25 15:34:13 +0000173}
174
Paul Duffin95bdab42021-03-08 21:48:46 +0000175// runWithErrors is a legacy way of running tests of java modules that expect errors.
176//
177// See testJava for an explanation as to how to stop using this deprecated method.
178//
179// deprecated
Paul Duffinec0fe172021-02-25 15:34:13 +0000180func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) {
Colin Crossae8600b2020-10-29 17:09:13 -0700181 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900182 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
183 if len(errs) > 0 {
184 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000185 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900186 }
187 _, errs = ctx.PrepareBuildActions(config)
188 if len(errs) > 0 {
189 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000190 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900191 }
192
193 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Paul Duffinec0fe172021-02-25 15:34:13 +0000194 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900195}
196
Paul Duffin95bdab42021-03-08 21:48:46 +0000197// testJavaWithFS runs tests using the javaFixtureFactory
198//
199// See testJava for an explanation as to how to stop using this deprecated method.
200//
201// deprecated
202func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -0700203 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000204 result := javaFixtureFactory.Extend(fs.AddToFixture()).RunTestWithBp(t, bp)
205 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -0700206}
207
Paul Duffin95bdab42021-03-08 21:48:46 +0000208// testJava runs tests using the javaFixtureFactory
209//
210// Do not add any new usages of this, instead use the javaFixtureFactory directly as it makes it
211// much easier to customize the test behavior.
212//
213// If it is necessary to customize the behavior of an existing test that uses this then please first
214// convert the test to using javaFixtureFactory first and then in a following change add the
215// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
216// that it did not change the test behavior unexpectedly.
217//
218// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700219func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800220 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000221 result := javaFixtureFactory.RunTestWithBp(t, bp)
222 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900223}
224
Paul Duffin95bdab42021-03-08 21:48:46 +0000225// testJavaWithConfig runs tests using the javaFixtureFactory
226//
227// See testJava for an explanation as to how to stop using this deprecated method.
228//
229// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -0800230func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900231 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000232 result := javaFixtureFactory.RunTestWithConfig(t, config)
233 return result.TestContext, result.Config
Colin Cross72bb3632017-07-13 16:23:21 -0700234}
235
Colin Cross2acdae82017-09-15 19:44:24 -0700236func moduleToPath(name string) string {
237 switch {
238 case name == `""`:
239 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700240 case strings.HasSuffix(name, ".jar"):
241 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700242 default:
243 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700244 }
245}
246
Paul Duffin95bdab42021-03-08 21:48:46 +0000247// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
248// is defined in PrepareForIntegrationTestWithJava
249func defaultModuleToPath(name string) string {
250 return filepath.Join(buildDir, ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
251}
252
Jeongik Chae403e9e2019-12-07 00:16:24 +0900253func TestJavaLinkType(t *testing.T) {
254 testJava(t, `
255 java_library {
256 name: "foo",
257 srcs: ["a.java"],
258 libs: ["bar"],
259 static_libs: ["baz"],
260 }
261
262 java_library {
263 name: "bar",
264 sdk_version: "current",
265 srcs: ["b.java"],
266 }
267
268 java_library {
269 name: "baz",
270 sdk_version: "system_current",
271 srcs: ["c.java"],
272 }
273 `)
274
Steven Moreland00298982020-11-17 21:44:36 +0000275 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900276 java_library {
277 name: "foo",
278 srcs: ["a.java"],
279 libs: ["bar"],
280 sdk_version: "current",
281 static_libs: ["baz"],
282 }
283
284 java_library {
285 name: "bar",
286 sdk_version: "current",
287 srcs: ["b.java"],
288 }
289
290 java_library {
291 name: "baz",
292 sdk_version: "system_current",
293 srcs: ["c.java"],
294 }
295 `)
296
297 testJava(t, `
298 java_library {
299 name: "foo",
300 srcs: ["a.java"],
301 libs: ["bar"],
302 sdk_version: "system_current",
303 static_libs: ["baz"],
304 }
305
306 java_library {
307 name: "bar",
308 sdk_version: "current",
309 srcs: ["b.java"],
310 }
311
312 java_library {
313 name: "baz",
314 sdk_version: "system_current",
315 srcs: ["c.java"],
316 }
317 `)
318
Steven Moreland00298982020-11-17 21:44:36 +0000319 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900320 java_library {
321 name: "foo",
322 srcs: ["a.java"],
323 libs: ["bar"],
324 sdk_version: "system_current",
325 static_libs: ["baz"],
326 }
327
328 java_library {
329 name: "bar",
330 sdk_version: "current",
331 srcs: ["b.java"],
332 }
333
334 java_library {
335 name: "baz",
336 srcs: ["c.java"],
337 }
338 `)
339}
340
Colin Cross72bb3632017-07-13 16:23:21 -0700341func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700342 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700343 java_library {
344 name: "foo",
345 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700346 libs: ["bar"],
347 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700348 }
349
350 java_library {
351 name: "bar",
352 srcs: ["b.java"],
353 }
354
355 java_library {
356 name: "baz",
357 srcs: ["c.java"],
358 }
Colin Crossd5934c82017-10-02 13:55:26 -0700359 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700360
Colin Cross4c428df2017-09-15 17:36:05 -0700361 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700362 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700363
364 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
365 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
366 }
367
Colin Cross1ee23172017-10-18 14:44:18 -0700368 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700369 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
370 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700371
Paul Duffin22b77cd2021-03-12 19:15:01 +0000372 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700373
Paul Duffin22b77cd2021-03-12 19:15:01 +0000374 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700375
376 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
377 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700378 }
379}
380
Artur Satayev9cf46692019-11-26 18:08:34 +0000381func TestExportedPlugins(t *testing.T) {
382 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800383 library string
384 processors string
385 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000386 }
387 var tests = []struct {
388 name string
389 extra string
390 results []Result
391 }{
392 {
393 name: "Exported plugin is not a direct plugin",
394 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
395 results: []Result{{library: "exports", processors: "-proc:none"}},
396 },
397 {
398 name: "Exports plugin to dependee",
399 extra: `
400 java_library{name: "exports", exported_plugins: ["plugin"]}
401 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
402 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
403 `,
404 results: []Result{
405 {library: "foo", processors: "-processor com.android.TestPlugin"},
406 {library: "bar", processors: "-processor com.android.TestPlugin"},
407 },
408 },
409 {
410 name: "Exports plugin to android_library",
411 extra: `
412 java_library{name: "exports", exported_plugins: ["plugin"]}
413 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
414 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
415 `,
416 results: []Result{
417 {library: "foo", processors: "-processor com.android.TestPlugin"},
418 {library: "bar", processors: "-processor com.android.TestPlugin"},
419 },
420 },
421 {
422 name: "Exports plugin is not propagated via transitive deps",
423 extra: `
424 java_library{name: "exports", exported_plugins: ["plugin"]}
425 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
426 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
427 `,
428 results: []Result{
429 {library: "foo", processors: "-processor com.android.TestPlugin"},
430 {library: "bar", processors: "-proc:none"},
431 },
432 },
433 {
434 name: "Exports plugin appends to plugins",
435 extra: `
436 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
437 java_library{name: "exports", exported_plugins: ["plugin"]}
438 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
439 `,
440 results: []Result{
441 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
442 },
443 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800444 {
445 name: "Exports plugin to with generates_api to dependee",
446 extra: `
447 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
448 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
449 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
450 `,
451 results: []Result{
452 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
453 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
454 },
455 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000456 }
457
458 for _, test := range tests {
459 t.Run(test.name, func(t *testing.T) {
460 ctx, _ := testJava(t, `
461 java_plugin {
462 name: "plugin",
463 processor_class: "com.android.TestPlugin",
464 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800465 java_plugin {
466 name: "plugin_generates_api",
467 generates_api: true,
468 processor_class: "com.android.TestPlugin",
469 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000470 `+test.extra)
471
472 for _, want := range test.results {
473 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
474 if javac.Args["processor"] != want.processors {
475 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
476 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800477 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
478 disableTurbine := turbine.BuildParams.Rule == nil
479 if disableTurbine != want.disableTurbine {
480 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
481 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000482 }
483 })
484 }
485}
486
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900487func TestSdkVersionByPartition(t *testing.T) {
488 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900489 java_library {
490 name: "foo",
491 srcs: ["a.java"],
492 vendor: true,
493 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900494 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900495
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900496 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900497 java_library {
498 name: "bar",
499 srcs: ["b.java"],
500 }
501 `)
502
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900503 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900504 bp := `
505 java_library {
506 name: "foo",
507 srcs: ["a.java"],
508 product_specific: true,
509 }
510 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800511
512 config := testConfig(nil, bp, nil)
513 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900514 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800515 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 +0900516 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800517 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900518 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900519 }
520}
521
Colin Crossd5934c82017-10-02 13:55:26 -0700522func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700523 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700524 java_library {
525 name: "foo",
526 srcs: ["a.java"],
527 target: {
528 android: {
529 srcs: ["b.java"],
530 },
531 },
532 }
533 `)
534
535 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
536 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
537 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
538 }
539}
540
Colin Cross6b4a32d2017-12-05 13:42:45 -0800541func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700542 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800543 java_library_host {
544 name: "foo",
545 srcs: ["a.java"],
546 }
547
548 java_binary_host {
549 name: "bar",
550 srcs: ["b.java"],
551 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700552 jni_libs: ["libjni"],
553 }
554
555 cc_library_shared {
556 name: "libjni",
557 host_supported: true,
558 device_supported: false,
559 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800560 }
561 `)
562
563 buildOS := android.BuildOs.String()
564
565 bar := ctx.ModuleForTests("bar", buildOS+"_common")
566 barJar := bar.Output("bar.jar").Output.String()
567 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
568 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
569
Colin Cross89226d92020-10-09 19:00:54 -0700570 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
571 libjniSO := libjni.Rule("Cp").Output.String()
572
Colin Cross6b4a32d2017-12-05 13:42:45 -0800573 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700574 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
575 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800576 }
Colin Cross89226d92020-10-09 19:00:54 -0700577
578 // Test that the install binary wrapper depends on the installed JNI libraries
579 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
580 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800581 }
Alex Humesky2070e322020-06-09 20:23:08 -0400582}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800583
Alex Humesky2070e322020-06-09 20:23:08 -0400584func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
585 bp := `
586 java_library {
587 name: "target_library",
588 srcs: ["a.java"],
589 }
590
591 java_binary_host {
592 name: "host_binary",
593 srcs: ["b.java"],
594 }
595 `
596 config := testConfig(nil, bp, nil)
597 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
598
599 ctx, _ := testJavaWithConfig(t, config)
600
Liz Kammer7941b302020-07-28 13:27:34 -0700601 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400602 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
603 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
604 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
605 t.Errorf("target library javac flags %v should contain "+
606 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
607 }
608
609 // check that -g is not overridden for host modules
610 buildOS := android.BuildOs.String()
611 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
612 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
613 if strings.Contains(hostJavaFlags, "-g:source,lines") {
614 t.Errorf("java_binary_host javac flags %v should not have "+
615 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
616 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800617}
618
Colin Cross72bb3632017-07-13 16:23:21 -0700619func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700620 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700621 java_library {
622 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000623 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000624 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700625 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700626 }
627
Colin Cross74d73e22017-08-02 11:05:49 -0700628 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700629 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700630 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700631 }
632
Colin Cross74d73e22017-08-02 11:05:49 -0700633 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700634 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700635 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700636 sdk_version: "current",
637 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700638 }
Colin Cross42be7612019-02-21 18:12:14 -0800639
640 dex_import {
641 name: "qux",
642 jars: ["b.jar"],
643 }
Colin Cross79c7c262019-04-17 11:11:46 -0700644
645 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000646 name: "sdklib",
647 public: {
648 jars: ["c.jar"],
649 },
650 }
651
Paul Duffin91547182019-11-12 19:39:36 +0000652 prebuilt_stubs_sources {
653 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000654 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000655 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000656
657 java_test_import {
658 name: "test",
659 jars: ["a.jar"],
660 test_suites: ["cts"],
661 test_config: "AndroidTest.xml",
662 }
Colin Cross72bb3632017-07-13 16:23:21 -0700663 `)
664
Paul Duffin9b478b02019-12-10 13:41:51 +0000665 fooModule := ctx.ModuleForTests("foo", "android_common")
666 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700667 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700668 barModule := ctx.ModuleForTests("bar", "android_common")
669 barJar := barModule.Rule("combineJar").Output
670 bazModule := ctx.ModuleForTests("baz", "android_common")
671 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700672 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700673
Paul Duffin9b478b02019-12-10 13:41:51 +0000674 fooLibrary := fooModule.Module().(*Library)
675 assertDeepEquals(t, "foo java sources incorrect",
676 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000677
Paul Duffin9b478b02019-12-10 13:41:51 +0000678 assertDeepEquals(t, "foo java source jars incorrect",
679 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
680 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000681
Colin Cross37f6d792018-07-12 12:28:41 -0700682 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
683 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700684 }
685
Liz Kammerd6c31d22020-08-05 15:40:41 -0700686 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
687 if barDexJar != nil {
688 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
689 }
690
Colin Cross79c7c262019-04-17 11:11:46 -0700691 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
692 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
693 }
694
Colin Cross37f6d792018-07-12 12:28:41 -0700695 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
696 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700697 }
Colin Cross42be7612019-02-21 18:12:14 -0800698
Liz Kammerd6c31d22020-08-05 15:40:41 -0700699 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
700 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
701 if bazDexJar != expectedDexJar {
702 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
703 }
704
Colin Cross42be7612019-02-21 18:12:14 -0800705 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700706}
707
Paul Duffin9b478b02019-12-10 13:41:51 +0000708func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
709 if !reflect.DeepEqual(expected, actual) {
710 t.Errorf("%s: expected %q, found %q", message, expected, actual)
711 }
712}
713
Paul Duffin1a393322020-11-18 16:36:47 +0000714func TestPrebuiltStubsSources(t *testing.T) {
715 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
716 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
717prebuilt_stubs_sources {
718 name: "stubs-source",
719 srcs: ["%s"],
720}`, sourcesPath), map[string][]byte{
721 "stubs/sources/pkg/A.java": nil,
722 "stubs/sources/pkg/B.java": nil,
723 })
724
725 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
726 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
727 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
728 }
729 }
730
731 t.Run("empty/missing directory", func(t *testing.T) {
732 test(t, "empty-directory", []string{})
733 })
734
735 t.Run("non-empty set of sources", func(t *testing.T) {
736 test(t, "stubs/sources", []string{
737 "stubs/sources/pkg/A.java",
738 "stubs/sources/pkg/B.java",
739 })
740 })
741}
742
Paul Duffin56d44902020-01-31 13:36:25 +0000743func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000744 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000745 java_library {
746 name: "foo",
747 srcs: ["a.java"],
748 libs: ["sdklib"],
749 sdk_version: "current",
750 }
751
752 java_library {
753 name: "foo.system",
754 srcs: ["a.java"],
755 libs: ["sdklib"],
756 sdk_version: "system_current",
757 }
758
759 java_library {
760 name: "foo.test",
761 srcs: ["a.java"],
762 libs: ["sdklib"],
763 sdk_version: "test_current",
764 }
765
766 java_sdk_library_import {
767 name: "sdklib",
768 public: {
769 jars: ["a.jar"],
770 },
771 system: {
772 jars: ["b.jar"],
773 },
774 test: {
775 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100776 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000777 },
778 }
779 `)
780
781 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000782 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000783 javac := fooModule.Rule("javac")
784
Paul Duffin22b77cd2021-03-12 19:15:01 +0000785 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
786 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000787 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100788
Paul Duffin22b77cd2021-03-12 19:15:01 +0000789 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100790 `prebuilt_sdklib.stubs`,
791 `prebuilt_sdklib.stubs.source.test`,
792 `prebuilt_sdklib.stubs.system`,
793 `prebuilt_sdklib.stubs.test`,
794 })
795}
796
797func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000798 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100799 java_sdk_library {
800 name: "sdklib",
801 srcs: ["a.java"],
802 sdk_version: "none",
803 system_modules: "none",
804 public: {
805 enabled: true,
806 },
807 }
808
809 java_sdk_library_import {
810 name: "sdklib",
811 public: {
812 jars: ["a.jar"],
813 },
814 }
815 `)
816
Paul Duffin22b77cd2021-03-12 19:15:01 +0000817 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100818 `dex2oatd`,
819 `prebuilt_sdklib`,
820 `sdklib.impl`,
821 `sdklib.stubs`,
822 `sdklib.stubs.source`,
823 `sdklib.xml`,
824 })
825
Paul Duffin22b77cd2021-03-12 19:15:01 +0000826 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100827 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100828 `sdklib.impl`,
829 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
830 // dependency is added after prebuilts may have been renamed and so has to use
831 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100832 `sdklib.xml`,
833 })
834}
835
836func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000837 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100838 java_sdk_library {
839 name: "sdklib",
840 srcs: ["a.java"],
841 sdk_version: "none",
842 system_modules: "none",
843 public: {
844 enabled: true,
845 },
846 }
847
848 java_sdk_library_import {
849 name: "sdklib",
850 prefer: true,
851 public: {
852 jars: ["a.jar"],
853 },
854 }
855 `)
856
Paul Duffin22b77cd2021-03-12 19:15:01 +0000857 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100858 `dex2oatd`,
859 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100860 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100861 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100862 `sdklib.stubs.source`,
863 `sdklib.xml`,
864 })
865
Paul Duffin22b77cd2021-03-12 19:15:01 +0000866 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100867 `prebuilt_sdklib.stubs`,
868 `sdklib.impl`,
869 `sdklib.xml`,
870 })
Paul Duffin56d44902020-01-31 13:36:25 +0000871}
872
JaeMan Parkff715562020-10-19 17:25:58 +0900873func TestJavaSdkLibraryEnforce(t *testing.T) {
874 partitionToBpOption := func(partition string) string {
875 switch partition {
876 case "system":
877 return ""
878 case "vendor":
879 return "soc_specific: true,"
880 case "product":
881 return "product_specific: true,"
882 default:
883 panic("Invalid partition group name: " + partition)
884 }
885 }
886
887 type testConfigInfo struct {
888 libraryType string
889 fromPartition string
890 toPartition string
891 enforceVendorInterface bool
892 enforceProductInterface bool
893 enforceJavaSdkLibraryCheck bool
894 allowList []string
895 }
896
Paul Duffin05f72de2021-03-12 21:28:51 +0000897 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900898 bpFileTemplate := `
899 java_library {
900 name: "foo",
901 srcs: ["foo.java"],
902 libs: ["bar"],
903 sdk_version: "current",
904 %s
905 }
906
907 %s {
908 name: "bar",
909 srcs: ["bar.java"],
910 sdk_version: "current",
911 %s
912 }
913 `
914
915 bpFile := fmt.Sprintf(bpFileTemplate,
916 partitionToBpOption(info.fromPartition),
917 info.libraryType,
918 partitionToBpOption(info.toPartition))
919
Paul Duffin05f72de2021-03-12 21:28:51 +0000920 return android.GroupFixturePreparers(
921 android.FixtureWithRootAndroidBp(bpFile),
922 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
923 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
924 if info.enforceVendorInterface {
925 variables.DeviceVndkVersion = proptools.StringPtr("current")
926 }
927 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
928 variables.InterPartitionJavaLibraryAllowList = info.allowList
929 }),
930 )
JaeMan Parkff715562020-10-19 17:25:58 +0900931 }
932
Paul Duffinaa6caa72021-03-13 16:50:15 +0000933 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
934 t.Run(fmt.Sprintf("%#v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000935 errorHandler := android.FixtureExpectsNoErrors
936 if expectedErrorPattern != "" {
937 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000938 }
Paul Duffin05f72de2021-03-12 21:28:51 +0000939 javaFixtureFactory.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
Paul Duffinaa6caa72021-03-13 16:50:15 +0000940 })
941 }
942
JaeMan Parkff715562020-10-19 17:25:58 +0900943 errorMessage := "is not allowed across the partitions"
944
Paul Duffinaa6caa72021-03-13 16:50:15 +0000945 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900946 libraryType: "java_library",
947 fromPartition: "product",
948 toPartition: "system",
949 enforceVendorInterface: true,
950 enforceProductInterface: true,
951 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000952 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900953
Paul Duffinaa6caa72021-03-13 16:50:15 +0000954 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900955 libraryType: "java_library",
956 fromPartition: "product",
957 toPartition: "system",
958 enforceVendorInterface: true,
959 enforceProductInterface: false,
960 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000961 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900962
Paul Duffinaa6caa72021-03-13 16:50:15 +0000963 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900964 libraryType: "java_library",
965 fromPartition: "product",
966 toPartition: "system",
967 enforceVendorInterface: true,
968 enforceProductInterface: true,
969 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000970 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900971
Paul Duffinaa6caa72021-03-13 16:50:15 +0000972 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900973 libraryType: "java_library",
974 fromPartition: "vendor",
975 toPartition: "system",
976 enforceVendorInterface: true,
977 enforceProductInterface: true,
978 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000979 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900980
Paul Duffinaa6caa72021-03-13 16:50:15 +0000981 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900982 libraryType: "java_library",
983 fromPartition: "vendor",
984 toPartition: "system",
985 enforceVendorInterface: true,
986 enforceProductInterface: true,
987 enforceJavaSdkLibraryCheck: true,
988 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +0000989 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900990
Paul Duffinaa6caa72021-03-13 16:50:15 +0000991 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900992 libraryType: "java_library",
993 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900994 toPartition: "product",
995 enforceVendorInterface: true,
996 enforceProductInterface: true,
997 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000998 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +0900999
Paul Duffinaa6caa72021-03-13 16:50:15 +00001000 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001001 libraryType: "java_sdk_library",
1002 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +09001003 toPartition: "system",
1004 enforceVendorInterface: true,
1005 enforceProductInterface: true,
1006 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001007 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001008
Paul Duffinaa6caa72021-03-13 16:50:15 +00001009 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001010 libraryType: "java_sdk_library",
1011 fromPartition: "vendor",
1012 toPartition: "system",
1013 enforceVendorInterface: true,
1014 enforceProductInterface: true,
1015 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001016 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001017
Paul Duffinaa6caa72021-03-13 16:50:15 +00001018 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001019 libraryType: "java_sdk_library",
1020 fromPartition: "vendor",
1021 toPartition: "product",
1022 enforceVendorInterface: true,
1023 enforceProductInterface: true,
1024 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001025 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +09001026}
1027
Colin Cross89536d42017-07-07 14:35:50 -07001028func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001029 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -07001030 java_defaults {
1031 name: "defaults",
1032 srcs: ["a.java"],
1033 libs: ["bar"],
1034 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -07001035 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -07001036 }
1037
1038 java_library {
1039 name: "foo",
1040 defaults: ["defaults"],
1041 }
1042
1043 java_library {
1044 name: "bar",
1045 srcs: ["b.java"],
1046 }
1047
1048 java_library {
1049 name: "baz",
1050 srcs: ["c.java"],
1051 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001052
1053 android_test {
1054 name: "atestOptimize",
1055 defaults: ["defaults"],
1056 optimize: {enabled: true},
1057 }
1058
1059 android_test {
1060 name: "atestNoOptimize",
1061 defaults: ["defaults"],
1062 }
1063
1064 android_test {
1065 name: "atestDefault",
1066 srcs: ["a.java"],
1067 }
Colin Cross89536d42017-07-07 14:35:50 -07001068 `)
1069
Colin Cross4c428df2017-09-15 17:36:05 -07001070 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001071 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001072
1073 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1074 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1075 }
1076
Nan Zhanged19fc32017-10-19 13:06:22 -07001077 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1078 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1079 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001080 }
1081
Colin Cross1ee23172017-10-18 14:44:18 -07001082 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001083 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1084 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001085 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001086
1087 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1088 if atestOptimize.Output == nil {
1089 t.Errorf("atestOptimize should optimize APK")
1090 }
1091
1092 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1093 if atestNoOptimize.Output == nil {
1094 t.Errorf("atestNoOptimize should not optimize APK")
1095 }
1096
1097 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1098 if atestDefault.Output == nil {
1099 t.Errorf("atestDefault should optimize APK")
1100 }
Colin Cross89536d42017-07-07 14:35:50 -07001101}
1102
Colin Cross0f37af02017-09-27 17:42:05 -07001103func TestResources(t *testing.T) {
1104 var table = []struct {
1105 name string
1106 prop string
1107 extra string
1108 args string
1109 }{
1110 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001111 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001112 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001113 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001114 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001115 },
1116 {
1117 // Test that a module with java_resources includes the files
1118 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001119 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1120 args: "-C . -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 a filegroup in java_resources includes the files with the
1124 // path prefix
1125 name: "resource filegroup",
1126 prop: `java_resources: [":foo-res"]`,
1127 extra: `
1128 filegroup {
1129 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001130 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001131 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001132 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001133 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001134 },
1135 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001136 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1137 name: "wildcard dirs",
1138 prop: `java_resource_dirs: ["java-res/*"]`,
1139 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1140 },
1141 {
1142 // Test that a module exclude_java_resource_dirs excludes the files
1143 name: "wildcard dirs",
1144 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1145 args: "-C java-res/a -f java-res/a/a",
1146 },
Colin Crosscedd4762018-09-13 11:26:19 -07001147 {
1148 // Test wildcards in java_resources
1149 name: "wildcard files",
1150 prop: `java_resources: ["java-res/**/*"]`,
1151 args: "-C . -f java-res/a/a -f java-res/b/b",
1152 },
1153 {
1154 // Test exclude_java_resources with java_resources
1155 name: "wildcard files with exclude",
1156 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1157 args: "-C . -f java-res/a/a",
1158 },
1159 {
1160 // Test exclude_java_resources with java_resource_dirs
1161 name: "resource dirs with exclude files",
1162 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1163 args: "-C java-res -f java-res/a/a",
1164 },
1165 {
1166 // Test exclude_java_resource_dirs with java_resource_dirs
1167 name: "resource dirs with exclude files",
1168 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1169 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1170 },
Colin Cross0f37af02017-09-27 17:42:05 -07001171 }
1172
1173 for _, test := range table {
1174 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001175 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001176 java_library {
1177 name: "foo",
1178 srcs: [
1179 "a.java",
1180 "b.java",
1181 "c.java",
1182 ],
1183 `+test.prop+`,
1184 }
Colin Cross238c1f32020-06-07 16:58:18 -07001185 `+test.extra,
1186 map[string][]byte{
1187 "java-res/a/a": nil,
1188 "java-res/b/b": nil,
1189 "java-res2/a": nil,
1190 },
1191 )
Colin Cross0f37af02017-09-27 17:42:05 -07001192
Colin Cross331a1212018-08-15 20:40:52 -07001193 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001194 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001195
1196 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1197 t.Errorf("foo combined jars %v does not contain %q",
1198 foo.Inputs.Strings(), fooRes.Output.String())
1199 }
1200
Colin Crossaf9c55b2017-10-03 14:50:08 -07001201 if fooRes.Args["jarArgs"] != test.args {
1202 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001203 fooRes.Args["jarArgs"], test.args)
1204 }
1205 })
1206 }
1207}
1208
Colin Cross0c4ce212019-05-03 15:28:19 -07001209func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001210 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001211 java_library {
1212 name: "foo",
1213 srcs: [
1214 "a.java",
1215 "b.java",
1216 "c.java",
1217 ],
1218 include_srcs: true,
1219 }
1220
1221 java_library {
1222 name: "bar",
1223 srcs: [
1224 "a.java",
1225 "b.java",
1226 "c.java",
1227 ],
1228 java_resource_dirs: ["java-res"],
1229 include_srcs: true,
1230 }
Colin Cross238c1f32020-06-07 16:58:18 -07001231 `, map[string][]byte{
1232 "java-res/a/a": nil,
1233 "java-res/b/b": nil,
1234 "java-res2/a": nil,
1235 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001236
1237 // Test a library with include_srcs: true
1238 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1239 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1240
1241 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1242 t.Errorf("foo combined jars %v does not contain %q", w, g)
1243 }
1244
1245 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1246 t.Errorf("foo source jar args %q is not %q", w, g)
1247 }
1248
1249 // Test a library with include_srcs: true and resources
1250 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1251 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1252 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1253 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1254
1255 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1256 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1257 }
1258
1259 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1260 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1261 }
1262
1263 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1264 t.Errorf("bar combined jars %v does not contain %q", w, g)
1265 }
1266
1267 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1268 t.Errorf("bar source jar args %q is not %q", w, g)
1269 }
1270
1271 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1272 t.Errorf("bar resource jar args %q is not %q", w, g)
1273 }
1274}
1275
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001276func TestJavaLint(t *testing.T) {
1277 ctx, _ := testJavaWithFS(t, `
1278 java_library {
1279 name: "foo",
1280 srcs: [
1281 "a.java",
1282 "b.java",
1283 "c.java",
1284 ],
1285 min_sdk_version: "29",
1286 sdk_version: "system_current",
1287 }
1288 `, map[string][]byte{
1289 "lint-baseline.xml": nil,
1290 })
1291
1292 foo := ctx.ModuleForTests("foo", "android_common")
1293 rule := foo.Rule("lint")
1294
1295 if !strings.Contains(rule.RuleParams.Command, "--baseline lint-baseline.xml") {
1296 t.Error("did not pass --baseline flag")
1297 }
1298}
1299
1300func TestJavaLintWithoutBaseline(t *testing.T) {
1301 ctx, _ := testJavaWithFS(t, `
1302 java_library {
1303 name: "foo",
1304 srcs: [
1305 "a.java",
1306 "b.java",
1307 "c.java",
1308 ],
1309 min_sdk_version: "29",
1310 sdk_version: "system_current",
1311 }
1312 `, map[string][]byte{})
1313
1314 foo := ctx.ModuleForTests("foo", "android_common")
1315 rule := foo.Rule("lint")
1316
1317 if strings.Contains(rule.RuleParams.Command, "--baseline") {
1318 t.Error("passed --baseline flag for non existent file")
1319 }
1320}
1321
1322func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
1323 config := testConfig(
1324 nil,
1325 `
1326 java_library {
1327 name: "foo",
1328 srcs: [
1329 ],
1330 min_sdk_version: "29",
1331 sdk_version: "system_current",
1332 lint: {
1333 baseline_filename: "mybaseline.xml",
1334 },
1335 }
1336 `, map[string][]byte{
1337 "build/soong/java/lint_defaults.txt": nil,
1338 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1339 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1340 "framework/aidl": nil,
1341 "a.java": nil,
1342 "AndroidManifest.xml": nil,
1343 "build/make/target/product/security": nil,
1344 })
1345 config.TestAllowNonExistentPaths = false
1346 testJavaErrorWithConfig(t,
1347 "source path \"mybaseline.xml\" does not exist",
1348 config,
1349 )
1350}
1351
1352func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1353 ctx, _ := testJavaWithFS(t, `
1354 java_library {
1355 name: "foo",
1356 srcs: [
1357 "a.java",
1358 "b.java",
1359 "c.java",
1360 ],
1361 min_sdk_version: "29",
1362 sdk_version: "system_current",
1363 lint: {
1364 error_checks: ["SomeCheck"],
1365 baseline_filename: "mybaseline.xml",
1366 },
1367 }
1368 `, map[string][]byte{
1369 "mybaseline.xml": nil,
1370 })
1371
1372 foo := ctx.ModuleForTests("foo", "android_common")
1373 rule := foo.Rule("lint")
1374
1375 if !strings.Contains(rule.RuleParams.Command, "--baseline mybaseline.xml") {
1376 t.Error("did not use the correct file for baseline")
1377 }
1378}
1379
Colin Cross54190b32017-10-09 15:34:10 -07001380func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001381 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001382 java_library {
1383 name: "foo",
1384 srcs: [
1385 "a*.java",
1386 ":gen",
1387 "b*.java",
1388 ],
1389 }
1390
1391 genrule {
1392 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001393 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001394 out: ["gen.java"],
1395 }
Colin Cross238c1f32020-06-07 16:58:18 -07001396 `, map[string][]byte{
1397 "a.java": nil,
1398 "b.java": nil,
1399 })
Colin Cross54190b32017-10-09 15:34:10 -07001400
1401 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1402 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1403
Colin Cross15e86d92017-10-20 15:07:08 -07001404 if filepath.Base(genrule.Output.String()) != "gen.java" {
1405 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001406 }
1407
1408 if len(javac.Inputs) != 3 ||
1409 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001410 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001411 javac.Inputs[2].String() != "b.java" {
1412 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1413 }
1414}
1415
Nan Zhang61eaedb2017-11-02 13:28:15 -07001416func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001417 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001418 java_library {
1419 name: "foo",
1420 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001421 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001422 }
1423
1424 java_library {
1425 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001426 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001427 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001428 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001429 }
1430
1431 java_library {
1432 name: "baz",
1433 srcs: ["c.java"],
1434 libs: ["bar"],
1435 sdk_version: "14",
1436 }
1437 `)
1438
1439 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1440 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1441 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1442 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1443 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1444
1445 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1446 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1447 }
1448
1449 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1450 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1451 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1452 }
1453 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1454 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1455 }
1456 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1457 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1458 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001459 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001460 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001461 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001462 }
1463}
1464
1465func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001466 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001467 java_library {
1468 name: "bar",
1469 srcs: ["a.java","b.java","c.java"],
1470 javac_shard_size: 1
1471 }
1472 `)
1473
1474 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1475 for i := 0; i < 3; i++ {
1476 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1477 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1478 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1479 }
1480 }
1481}
1482
Nan Zhang581fd212018-01-10 16:06:12 -08001483func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001484 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001485 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001486 name: "droiddoc-templates-sdk",
1487 path: ".",
1488 }
Jiyong Park29074592019-07-07 16:27:47 +09001489 filegroup {
1490 name: "bar-doc-aidl-srcs",
1491 srcs: ["bar-doc/IBar.aidl"],
1492 path: "bar-doc",
1493 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001494 droidstubs {
1495 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001496 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001497 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001498 ],
Steve Kim3666c702020-09-01 17:58:01 +00001499 exclude_srcs: [
1500 "bar-doc/b.java"
1501 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001502 api_levels_annotations_dirs: [
1503 "droiddoc-templates-sdk",
1504 ],
1505 api_levels_annotations_enabled: true,
1506 }
1507 droiddoc {
1508 name: "bar-doc",
1509 srcs: [
1510 ":bar-stubs",
1511 "bar-doc/IFoo.aidl",
1512 ":bar-doc-aidl-srcs",
1513 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001514 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001515 hdf: [
1516 "android.whichdoc offline",
1517 ],
1518 knowntags: [
1519 "bar-doc/known_oj_tags.txt",
1520 ],
1521 proofread_file: "libcore-proofread.txt",
1522 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001523 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001524 }
Colin Cross238c1f32020-06-07 16:58:18 -07001525 `,
1526 map[string][]byte{
1527 "bar-doc/a.java": nil,
1528 "bar-doc/b.java": nil,
1529 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001530 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1531 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1532 if err != nil {
1533 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1534 }
1535 if len(barStubsOutputs) != 1 {
1536 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001537 }
Nan Zhang581fd212018-01-10 16:06:12 -08001538
Liz Kammere1ab2502020-09-10 15:29:25 +00001539 barStubsOutput := barStubsOutputs[0]
1540 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1541 javaDoc := barDoc.Rule("javadoc")
1542 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1543 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001544 }
1545
Liz Kammere1ab2502020-09-10 15:29:25 +00001546 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1547 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1548 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1549 }
1550
1551 aidl := barDoc.Rule("aidl")
1552 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001553 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1554 }
1555
1556 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1557 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001558 }
Nan Zhang581fd212018-01-10 16:06:12 -08001559}
1560
Liz Kammer585cac22020-07-06 09:12:57 -07001561func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1562 testJavaError(t, "flags is set. Cannot set args", `
1563 droiddoc_exported_dir {
1564 name: "droiddoc-templates-sdk",
1565 path: ".",
1566 }
1567 filegroup {
1568 name: "bar-doc-aidl-srcs",
1569 srcs: ["bar-doc/IBar.aidl"],
1570 path: "bar-doc",
1571 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001572 droidstubs {
1573 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001574 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001575 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001576 ],
Steve Kim3666c702020-09-01 17:58:01 +00001577 exclude_srcs: [
1578 "bar-doc/b.java"
1579 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001580 api_levels_annotations_dirs: [
1581 "droiddoc-templates-sdk",
1582 ],
1583 api_levels_annotations_enabled: true,
1584 }
1585 droiddoc {
1586 name: "bar-doc",
1587 srcs: [
1588 ":bar-stubs",
1589 "bar-doc/IFoo.aidl",
1590 ":bar-doc-aidl-srcs",
1591 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001592 custom_template: "droiddoc-templates-sdk",
1593 hdf: [
1594 "android.whichdoc offline",
1595 ],
1596 knowntags: [
1597 "bar-doc/known_oj_tags.txt",
1598 ],
1599 proofread_file: "libcore-proofread.txt",
1600 todo_file: "libcore-docs-todo.html",
1601 flags: ["-offlinemode -title \"libcore\""],
1602 args: "-offlinemode -title \"libcore\"",
1603 }
1604 `)
1605}
1606
Liz Kammer3d894b72020-08-04 09:55:13 -07001607func TestDroidstubs(t *testing.T) {
1608 ctx, _ := testJavaWithFS(t, `
1609 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001610 name: "droiddoc-templates-sdk",
1611 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001612 }
1613
1614 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001615 name: "bar-stubs",
1616 srcs: ["bar-doc/a.java"],
1617 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1618 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001619 }
1620
1621 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001622 name: "bar-stubs-other",
1623 srcs: ["bar-doc/a.java"],
1624 high_mem: true,
1625 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1626 api_levels_annotations_enabled: true,
1627 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001628 }
1629 `,
1630 map[string][]byte{
1631 "bar-doc/a.java": nil,
1632 })
1633 testcases := []struct {
1634 moduleName string
1635 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001636 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001637 }{
1638 {
1639 moduleName: "bar-stubs",
1640 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001641 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001642 },
1643 {
1644 moduleName: "bar-stubs-other",
1645 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001646 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001647 },
1648 }
1649 for _, c := range testcases {
1650 m := ctx.ModuleForTests(c.moduleName, "android_common")
1651 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001652 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001653 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001654 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001655 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1656 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001657
1658 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1659 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1660 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001661 }
1662}
1663
Paul Duffin83a2d962019-11-19 19:44:10 +00001664func TestDroidstubsWithSystemModules(t *testing.T) {
1665 ctx, _ := testJava(t, `
1666 droidstubs {
1667 name: "stubs-source-system-modules",
1668 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001669 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001670 ],
1671 sdk_version: "none",
1672 system_modules: "source-system-modules",
1673 }
1674
1675 java_library {
1676 name: "source-jar",
1677 srcs: [
1678 "a.java",
1679 ],
1680 }
1681
1682 java_system_modules {
1683 name: "source-system-modules",
1684 libs: ["source-jar"],
1685 }
1686
1687 droidstubs {
1688 name: "stubs-prebuilt-system-modules",
1689 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001690 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001691 ],
1692 sdk_version: "none",
1693 system_modules: "prebuilt-system-modules",
1694 }
1695
1696 java_import {
1697 name: "prebuilt-jar",
1698 jars: ["a.jar"],
1699 }
1700
1701 java_system_modules_import {
1702 name: "prebuilt-system-modules",
1703 libs: ["prebuilt-jar"],
1704 }
1705 `)
1706
1707 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1708
1709 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1710}
1711
1712func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1713 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1714 var systemJars []string
1715 for _, i := range metalavaRule.Implicits {
1716 systemJars = append(systemJars, i.Base())
1717 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001718 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001719 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1720 }
1721}
1722
Colin Cross54250902017-12-05 09:28:08 -08001723func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001724 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001725 java_library {
1726 name: "foo",
1727 srcs: ["a.java"],
1728 }
1729
1730 java_genrule {
1731 name: "jargen",
1732 tool_files: ["b.java"],
1733 cmd: "$(location b.java) $(in) $(out)",
1734 out: ["jargen.jar"],
1735 srcs: [":foo"],
1736 }
1737
1738 java_library {
1739 name: "bar",
1740 static_libs: ["jargen"],
1741 srcs: ["c.java"],
1742 }
1743
1744 java_library {
1745 name: "baz",
1746 libs: ["jargen"],
1747 srcs: ["c.java"],
1748 }
1749 `)
1750
1751 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1752 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1753 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1754 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1755 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1756
Colin Cross3d680512020-11-13 16:23:53 -08001757 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1758 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001759 }
1760
1761 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1762 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1763 }
1764
1765 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1766 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1767 }
1768
1769 if len(barCombined.Inputs) != 2 ||
1770 barCombined.Inputs[0].String() != bar.Output.String() ||
1771 barCombined.Inputs[1].String() != jargen.Output.String() {
1772 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1773 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1774 }
1775}
1776
Nan Zhang27e284d2018-02-09 21:03:53 +00001777func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001778 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001779 java_library {
1780 name: "foo",
1781 srcs: ["a.java", ":foo-srcs"],
1782 exclude_srcs: ["a.java", ":foo-excludes"],
1783 }
1784
1785 filegroup {
1786 name: "foo-srcs",
1787 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1788 }
1789
1790 filegroup {
1791 name: "foo-excludes",
1792 srcs: ["java-fg/a.java", "java-fg/b.java"],
1793 }
1794 `)
1795
1796 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1797
1798 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1799 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1800 }
1801}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001802
Paul Duffin52d398a2019-06-11 12:31:14 +01001803func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001804 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001805 "libcore/Android.bp": []byte(`
1806 java_library {
1807 name: "core",
1808 sdk_version: "none",
1809 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001810 }
1811
1812 filegroup {
1813 name: "core-jar",
1814 srcs: [":core{.jar}"],
1815 }
1816`),
1817 })
Colin Crossae8600b2020-10-29 17:09:13 -07001818 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001819 run(t, ctx, config)
1820}
1821
1822func TestJavaImport(t *testing.T) {
1823 config := testConfig(nil, "", map[string][]byte{
1824 "libcore/Android.bp": []byte(`
1825 java_import {
1826 name: "core",
1827 sdk_version: "none",
1828 }
1829
1830 filegroup {
1831 name: "core-jar",
1832 srcs: [":core{.jar}"],
1833 }
1834`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001835 })
Colin Crossae8600b2020-10-29 17:09:13 -07001836 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001837 run(t, ctx, config)
1838}
1839
Jiyong Parkc678ad32018-04-10 13:07:10 +09001840func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001841 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001842 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001843 name: "droiddoc-templates-sdk",
1844 path: ".",
1845 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001846 java_sdk_library {
1847 name: "foo",
1848 srcs: ["a.java", "b.java"],
1849 api_packages: ["foo"],
1850 }
1851 java_sdk_library {
1852 name: "bar",
1853 srcs: ["a.java", "b.java"],
1854 api_packages: ["bar"],
1855 }
1856 java_library {
1857 name: "baz",
1858 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001859 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001860 sdk_version: "system_current",
1861 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001862 java_sdk_library {
1863 name: "barney",
1864 srcs: ["c.java"],
1865 api_only: true,
1866 }
1867 java_sdk_library {
1868 name: "betty",
1869 srcs: ["c.java"],
1870 shared_library: false,
1871 }
Paul Duffin859fe962020-05-15 10:20:31 +01001872 java_sdk_library_import {
1873 name: "quuz",
1874 public: {
1875 jars: ["c.jar"],
1876 },
1877 }
1878 java_sdk_library_import {
1879 name: "fred",
1880 public: {
1881 jars: ["b.jar"],
1882 },
1883 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001884 java_sdk_library_import {
1885 name: "wilma",
1886 public: {
1887 jars: ["b.jar"],
1888 },
1889 shared_library: false,
1890 }
Jiyong Park1be96912018-05-28 18:02:19 +09001891 java_library {
1892 name: "qux",
1893 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001894 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001895 sdk_version: "system_current",
1896 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001897 java_library {
1898 name: "baz-test",
1899 srcs: ["c.java"],
1900 libs: ["foo"],
1901 sdk_version: "test_current",
1902 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001903 java_library {
1904 name: "baz-29",
1905 srcs: ["c.java"],
1906 libs: ["foo"],
1907 sdk_version: "system_29",
1908 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001909 java_library {
1910 name: "baz-module-30",
1911 srcs: ["c.java"],
1912 libs: ["foo"],
1913 sdk_version: "module_30",
1914 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001915 `)
1916
1917 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001918 result.ModuleForTests("foo", "android_common")
1919 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1920 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1921 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1922 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1923 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1924 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1925 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1926 result.ModuleForTests("foo.api.public.28", "")
1927 result.ModuleForTests("foo.api.system.28", "")
1928 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001929
Paul Duffin22b77cd2021-03-12 19:15:01 +00001930 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001931 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001932 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001933 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001934 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001935 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001936 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001937
Paul Duffin22b77cd2021-03-12 19:15:01 +00001938 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001939 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001940 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001941
Paul Duffin22b77cd2021-03-12 19:15:01 +00001942 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001943 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001944 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001945
Paul Duffin22b77cd2021-03-12 19:15:01 +00001946 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001947 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001948 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 +01001949
Jiyong Park1be96912018-05-28 18:02:19 +09001950 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001951 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001952 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001953 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001954 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001955 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001956}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001957
Anton Hansson7f66efa2020-10-08 14:47:23 +01001958func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001959 result := javaFixtureFactory.RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001960 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001961 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001962 srcs: ["a.java"],
1963 impl_only_libs: ["foo"],
1964 stub_only_libs: ["bar"],
1965 }
1966 java_library {
1967 name: "foo",
1968 srcs: ["a.java"],
1969 sdk_version: "current",
1970 }
1971 java_library {
1972 name: "bar",
1973 srcs: ["a.java"],
1974 sdk_version: "current",
1975 }
1976 `)
1977
Anton Hanssondff2c782020-12-21 17:10:01 +00001978 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001979 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001980 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1981 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1982 }
1983 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001984 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00001985 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001986 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1987 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1988 }
1989}
1990
Paul Duffindaaa3322020-05-26 18:13:57 +01001991func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001992 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01001993 java_sdk_library {
1994 name: "foo",
1995 srcs: ["a.java"],
1996 api_only: true,
1997 public: {
1998 enabled: true,
1999 },
2000 }
2001
2002 java_library {
2003 name: "bar",
2004 srcs: ["b.java"],
2005 libs: ["foo"],
2006 }
2007 `)
2008
2009 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002010 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01002011 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2012 t.Errorf("expected %q, found %#q", expected, actual)
2013 }
2014}
2015
Paul Duffin46dc45a2020-05-14 15:39:10 +01002016func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002017 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002018 java_sdk_library {
2019 name: "foo",
2020 srcs: ["a.java"],
2021 api_packages: ["foo"],
2022 public: {
2023 enabled: true,
2024 },
2025 }
2026
2027 java_library {
2028 name: "bar",
2029 srcs: ["b.java", ":foo{.public.stubs.source}"],
2030 }
2031 `)
2032}
2033
2034func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002035 javaFixtureFactory.
2036 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
2037 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002038 java_sdk_library {
2039 name: "foo",
2040 srcs: ["a.java"],
2041 api_packages: ["foo"],
2042 public: {
2043 enabled: true,
2044 },
2045 }
2046
2047 java_library {
2048 name: "bar",
2049 srcs: ["b.java", ":foo{.system.stubs.source}"],
2050 }
2051 `)
2052}
2053
Paul Duffinca8d9a52020-06-26 22:20:25 +01002054func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002055 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01002056 java_sdk_library {
2057 name: "sdklib",
2058 srcs: ["a.java"],
2059 sdk_version: "none",
2060 system_modules: "none",
2061 public: {
2062 enabled: true,
2063 },
2064 }
2065 `)
2066
Paul Duffin22b77cd2021-03-12 19:15:01 +00002067 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01002068 `dex2oatd`,
2069 `sdklib.impl`,
2070 `sdklib.stubs`,
2071 `sdklib.stubs.source`,
2072 `sdklib.xml`,
2073 })
2074}
2075
Paul Duffin46dc45a2020-05-14 15:39:10 +01002076func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002077 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002078 java_sdk_library_import {
2079 name: "foo",
2080 public: {
2081 jars: ["a.jar"],
2082 stub_srcs: ["a.java"],
2083 current_api: "api/current.txt",
2084 removed_api: "api/removed.txt",
2085 },
2086 }
2087
2088 java_library {
2089 name: "bar",
2090 srcs: [":foo{.public.stubs.source}"],
2091 java_resources: [
2092 ":foo{.public.api.txt}",
2093 ":foo{.public.removed-api.txt}",
2094 ],
2095 }
2096 `)
2097}
2098
2099func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2100 bp := `
2101 java_sdk_library_import {
2102 name: "foo",
2103 public: {
2104 jars: ["a.jar"],
2105 },
2106 }
2107 `
2108
2109 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002110 javaFixtureFactory.
2111 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
2112 RunTestWithBp(t, bp+`
2113 java_library {
2114 name: "bar",
2115 srcs: [":foo{.public.stubs.source}"],
2116 java_resources: [
2117 ":foo{.public.api.txt}",
2118 ":foo{.public.removed-api.txt}",
2119 ],
2120 }
2121 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002122 })
2123
2124 t.Run("api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002125 javaFixtureFactory.
2126 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
2127 RunTestWithBp(t, bp+`
2128 java_library {
2129 name: "bar",
2130 srcs: ["a.java"],
2131 java_resources: [
2132 ":foo{.public.api.txt}",
2133 ],
2134 }
2135 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002136 })
2137
2138 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002139 javaFixtureFactory.
2140 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
2141 RunTestWithBp(t, bp+`
2142 java_library {
2143 name: "bar",
2144 srcs: ["a.java"],
2145 java_resources: [
2146 ":foo{.public.removed-api.txt}",
2147 ],
2148 }
2149 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002150 })
2151}
2152
Paul Duffin3375e352020-04-28 10:44:03 +01002153func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002154 javaFixtureFactory.
2155 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
2156 RunTestWithBp(t, `
2157 java_sdk_library {
2158 name: "foo",
2159 srcs: ["a.java", "b.java"],
2160 api_packages: ["foo"],
2161 // Explicitly disable public to test the check that ensures the set of enabled
2162 // scopes is consistent.
2163 public: {
2164 enabled: false,
2165 },
2166 system: {
2167 enabled: true,
2168 },
2169 }
Paul Duffin3375e352020-04-28 10:44:03 +01002170 `)
2171}
2172
Paul Duffin87a05a32020-05-12 11:50:28 +01002173func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002174 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002175 java_sdk_library {
2176 name: "foo",
2177 srcs: ["a.java", "b.java"],
2178 api_packages: ["foo"],
2179 system: {
2180 enabled: true,
2181 sdk_version: "module_current",
2182 },
2183 }
2184 `)
2185}
2186
Paul Duffin0c5bae52020-06-02 13:00:08 +01002187func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002188 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002189 java_sdk_library {
2190 name: "foo",
2191 srcs: ["a.java", "b.java"],
2192 api_packages: ["foo"],
2193 system: {
2194 enabled: true,
2195 },
2196 module_lib: {
2197 enabled: true,
2198 },
2199 }
2200 `)
2201}
2202
2203func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002204 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002205 java_sdk_library {
2206 name: "foo",
2207 srcs: ["a.java", "b.java"],
2208 api_packages: ["foo"],
2209 system: {
2210 enabled: true,
2211 },
2212 system_server: {
2213 enabled: true,
2214 },
2215 }
2216 `)
2217}
2218
Paul Duffin803a9562020-05-20 11:52:25 +01002219func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002220 javaFixtureFactory.
2221 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2222 RunTestWithBp(t, `
2223 java_sdk_library {
2224 name: "foo",
2225 srcs: ["a.java"],
2226 public: {
2227 enabled: false,
2228 },
2229 }
Paul Duffin803a9562020-05-20 11:52:25 +01002230
Paul Duffin22b77cd2021-03-12 19:15:01 +00002231 java_library {
2232 name: "baz",
2233 srcs: ["a.java"],
2234 libs: ["foo"],
2235 sdk_version: "module_current",
2236 }
Paul Duffin803a9562020-05-20 11:52:25 +01002237 `)
2238}
2239
2240func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002241 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002242 java_sdk_library {
2243 name: "foo",
2244 srcs: ["a.java"],
2245 system: {
2246 enabled: true,
2247 },
2248 }
2249
2250 java_library {
2251 name: "baz",
2252 srcs: ["a.java"],
2253 libs: ["foo"],
2254 // foo does not have module-lib scope so it should fallback to system
2255 sdk_version: "module_current",
2256 }
2257 `)
2258}
2259
Jiyong Park932cdfe2020-05-28 00:19:53 +09002260func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002261 result := javaFixtureFactory.RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002262 java_sdk_library {
2263 name: "foo",
2264 srcs: ["a.java"],
2265 system: {
2266 enabled: true,
2267 },
2268 default_to_stubs: true,
2269 }
2270
2271 java_library {
2272 name: "baz",
2273 srcs: ["a.java"],
2274 libs: ["foo"],
2275 // does not have sdk_version set, should fallback to module,
2276 // which will then fallback to system because the module scope
2277 // is not enabled.
2278 }
2279 `)
2280 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002281 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002282 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2283 t.Errorf("expected %q, found %#q", expected, actual)
2284 }
2285}
2286
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002287var compilerFlagsTestCases = []struct {
2288 in string
2289 out bool
2290}{
2291 {
2292 in: "a",
2293 out: false,
2294 },
2295 {
2296 in: "-a",
2297 out: true,
2298 },
2299 {
2300 in: "-no-jdk",
2301 out: false,
2302 },
2303 {
2304 in: "-no-stdlib",
2305 out: false,
2306 },
2307 {
2308 in: "-kotlin-home",
2309 out: false,
2310 },
2311 {
2312 in: "-kotlin-home /some/path",
2313 out: false,
2314 },
2315 {
2316 in: "-include-runtime",
2317 out: false,
2318 },
2319 {
2320 in: "-Xintellij-plugin-root",
2321 out: false,
2322 },
2323}
2324
2325type mockContext struct {
2326 android.ModuleContext
2327 result bool
2328}
2329
2330func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2331 // CheckBadCompilerFlags calls this function when the flag should be rejected
2332 ctx.result = false
2333}
2334
2335func TestCompilerFlags(t *testing.T) {
2336 for _, testCase := range compilerFlagsTestCases {
2337 ctx := &mockContext{result: true}
2338 CheckKotlincFlags(ctx, []string{testCase.in})
2339 if ctx.result != testCase.out {
2340 t.Errorf("incorrect output:")
2341 t.Errorf(" input: %#v", testCase.in)
2342 t.Errorf(" expected: %#v", testCase.out)
2343 t.Errorf(" got: %#v", ctx.result)
2344 }
2345 }
2346}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002347
2348// TODO(jungjw): Consider making this more robust by ignoring path order.
2349func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2350 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2351 flags := strings.Split(variables["javacFlags"], " ")
2352 got := ""
2353 for _, flag := range flags {
2354 keyEnd := strings.Index(flag, "=")
2355 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2356 got = flag[keyEnd+1:]
2357 break
2358 }
2359 }
2360 if expected != got {
2361 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2362 }
2363}
2364
2365func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002366 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002367 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002368 bp := `
2369 java_library {
2370 name: "foo",
2371 srcs: ["a.java"],
2372 java_version: "1.8",
2373 }
2374
2375 java_library {
2376 name: "bar",
2377 srcs: ["b.java"],
2378 sdk_version: "none",
2379 system_modules: "none",
2380 patch_module: "java.base",
2381 java_version: "1.8",
2382 }
2383
2384 java_library {
2385 name: "baz",
2386 srcs: ["c.java"],
2387 patch_module: "java.base",
2388 java_version: "1.8",
2389 }
2390 `
2391 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002392
2393 checkPatchModuleFlag(t, ctx, "foo", "")
2394 checkPatchModuleFlag(t, ctx, "bar", "")
2395 checkPatchModuleFlag(t, ctx, "baz", "")
2396 })
2397
Pete Gillin0c2143e2019-05-02 15:32:11 +01002398 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002399 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002400 bp := `
2401 java_library {
2402 name: "foo",
2403 srcs: ["a.java"],
2404 }
2405
2406 java_library {
2407 name: "bar",
2408 srcs: ["b.java"],
2409 sdk_version: "none",
2410 system_modules: "none",
2411 patch_module: "java.base",
2412 }
2413
2414 java_library {
2415 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002416 srcs: [
2417 "c.java",
2418 // Tests for b/150878007
2419 "dir/d.java",
2420 "dir2/e.java",
2421 "dir2/f.java",
2422 "nested/dir/g.java"
2423 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002424 patch_module: "java.base",
2425 }
2426 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002427 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002428
2429 checkPatchModuleFlag(t, ctx, "foo", "")
2430 expected := "java.base=.:" + buildDir
2431 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002432 expected = "java.base=" + strings.Join([]string{
Paul Duffin95bdab42021-03-08 21:48:46 +00002433 ".", buildDir, "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002434 checkPatchModuleFlag(t, ctx, "baz", expected)
2435 })
2436}
Paul Duffina7b9f422020-01-10 17:12:18 +00002437
Paul Duffin83a2d962019-11-19 19:44:10 +00002438func TestJavaLibraryWithSystemModules(t *testing.T) {
2439 ctx, _ := testJava(t, `
2440 java_library {
2441 name: "lib-with-source-system-modules",
2442 srcs: [
2443 "a.java",
2444 ],
2445 sdk_version: "none",
2446 system_modules: "source-system-modules",
2447 }
2448
2449 java_library {
2450 name: "source-jar",
2451 srcs: [
2452 "a.java",
2453 ],
2454 }
2455
2456 java_system_modules {
2457 name: "source-system-modules",
2458 libs: ["source-jar"],
2459 }
2460
2461 java_library {
2462 name: "lib-with-prebuilt-system-modules",
2463 srcs: [
2464 "a.java",
2465 ],
2466 sdk_version: "none",
2467 system_modules: "prebuilt-system-modules",
2468 }
2469
2470 java_import {
2471 name: "prebuilt-jar",
2472 jars: ["a.jar"],
2473 }
2474
2475 java_system_modules_import {
2476 name: "prebuilt-system-modules",
2477 libs: ["prebuilt-jar"],
2478 }
2479 `)
2480
2481 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2482
2483 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2484}
2485
2486func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2487 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2488 bootClasspath := javacRule.Args["bootClasspath"]
2489 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2490 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2491 }
2492}
Jiyong Park19604de2020-03-24 16:44:11 +09002493
2494func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2495 ctx, _ := testJava(t, `
2496 java_library {
2497 name: "foo",
2498 srcs: ["aidl/foo/IFoo.aidl"],
2499 libs: ["bar"],
2500 }
2501
2502 java_import {
2503 name: "bar",
2504 jars: ["a.jar"],
2505 aidl: {
2506 export_include_dirs: ["aidl/bar"],
2507 },
2508 }
2509 `)
2510
2511 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2512 expectedAidlFlag := "-Iaidl/bar"
2513 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2514 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2515 }
2516}
Liz Kammerdd849a82020-06-12 16:38:45 -07002517
Jooyung Hane197d8b2021-01-05 10:33:16 +09002518func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2519 ctx, _ := testJava(t, `
2520 java_library {
2521 name: "foo",
2522 srcs: ["aidl/foo/IFoo.aidl"],
2523 aidl: { flags: ["-Werror"], },
2524 }
2525 `)
2526
2527 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2528 expectedAidlFlag := "-Werror"
2529 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2530 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2531 }
2532}
2533
Liz Kammerdd849a82020-06-12 16:38:45 -07002534func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002535 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002536 java_test_host {
2537 name: "foo",
2538 srcs: ["a.java"],
2539 data_native_bins: ["bin"]
2540 }
2541
2542 python_binary_host {
2543 name: "bin",
2544 srcs: ["bin.py"],
2545 }
2546 `)
2547
2548 buildOS := android.BuildOs.String()
2549
2550 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002551 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002552 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2553 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2554 if !reflect.DeepEqual(expected, actual) {
2555 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2556 }
2557}