blob: 533055618fc198aa44433242e683924d200b6d87 [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 {
Paul Duffin76101fa2021-03-13 09:55:25 +0000250 switch {
251 case name == `""`:
252 return name
253 case strings.HasSuffix(name, ".jar"):
254 return name
255 default:
256 return filepath.Join(buildDir, ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
257 }
Paul Duffin95bdab42021-03-08 21:48:46 +0000258}
259
Jeongik Chae403e9e2019-12-07 00:16:24 +0900260func TestJavaLinkType(t *testing.T) {
261 testJava(t, `
262 java_library {
263 name: "foo",
264 srcs: ["a.java"],
265 libs: ["bar"],
266 static_libs: ["baz"],
267 }
268
269 java_library {
270 name: "bar",
271 sdk_version: "current",
272 srcs: ["b.java"],
273 }
274
275 java_library {
276 name: "baz",
277 sdk_version: "system_current",
278 srcs: ["c.java"],
279 }
280 `)
281
Steven Moreland00298982020-11-17 21:44:36 +0000282 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900283 java_library {
284 name: "foo",
285 srcs: ["a.java"],
286 libs: ["bar"],
287 sdk_version: "current",
288 static_libs: ["baz"],
289 }
290
291 java_library {
292 name: "bar",
293 sdk_version: "current",
294 srcs: ["b.java"],
295 }
296
297 java_library {
298 name: "baz",
299 sdk_version: "system_current",
300 srcs: ["c.java"],
301 }
302 `)
303
304 testJava(t, `
305 java_library {
306 name: "foo",
307 srcs: ["a.java"],
308 libs: ["bar"],
309 sdk_version: "system_current",
310 static_libs: ["baz"],
311 }
312
313 java_library {
314 name: "bar",
315 sdk_version: "current",
316 srcs: ["b.java"],
317 }
318
319 java_library {
320 name: "baz",
321 sdk_version: "system_current",
322 srcs: ["c.java"],
323 }
324 `)
325
Steven Moreland00298982020-11-17 21:44:36 +0000326 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900327 java_library {
328 name: "foo",
329 srcs: ["a.java"],
330 libs: ["bar"],
331 sdk_version: "system_current",
332 static_libs: ["baz"],
333 }
334
335 java_library {
336 name: "bar",
337 sdk_version: "current",
338 srcs: ["b.java"],
339 }
340
341 java_library {
342 name: "baz",
343 srcs: ["c.java"],
344 }
345 `)
346}
347
Colin Cross72bb3632017-07-13 16:23:21 -0700348func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700349 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700350 java_library {
351 name: "foo",
352 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700353 libs: ["bar"],
354 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700355 }
356
357 java_library {
358 name: "bar",
359 srcs: ["b.java"],
360 }
361
362 java_library {
363 name: "baz",
364 srcs: ["c.java"],
365 }
Colin Crossd5934c82017-10-02 13:55:26 -0700366 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700367
Colin Cross4c428df2017-09-15 17:36:05 -0700368 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700369 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700370
371 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
372 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
373 }
374
Colin Cross1ee23172017-10-18 14:44:18 -0700375 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700376 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
377 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700378
Paul Duffin22b77cd2021-03-12 19:15:01 +0000379 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700380
Paul Duffin22b77cd2021-03-12 19:15:01 +0000381 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700382
383 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
384 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700385 }
386}
387
Artur Satayev9cf46692019-11-26 18:08:34 +0000388func TestExportedPlugins(t *testing.T) {
389 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800390 library string
391 processors string
392 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000393 }
394 var tests = []struct {
395 name string
396 extra string
397 results []Result
398 }{
399 {
400 name: "Exported plugin is not a direct plugin",
401 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
402 results: []Result{{library: "exports", processors: "-proc:none"}},
403 },
404 {
405 name: "Exports plugin to dependee",
406 extra: `
407 java_library{name: "exports", exported_plugins: ["plugin"]}
408 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
409 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
410 `,
411 results: []Result{
412 {library: "foo", processors: "-processor com.android.TestPlugin"},
413 {library: "bar", processors: "-processor com.android.TestPlugin"},
414 },
415 },
416 {
417 name: "Exports plugin to android_library",
418 extra: `
419 java_library{name: "exports", exported_plugins: ["plugin"]}
420 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
421 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
422 `,
423 results: []Result{
424 {library: "foo", processors: "-processor com.android.TestPlugin"},
425 {library: "bar", processors: "-processor com.android.TestPlugin"},
426 },
427 },
428 {
429 name: "Exports plugin is not propagated via transitive deps",
430 extra: `
431 java_library{name: "exports", exported_plugins: ["plugin"]}
432 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
433 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
434 `,
435 results: []Result{
436 {library: "foo", processors: "-processor com.android.TestPlugin"},
437 {library: "bar", processors: "-proc:none"},
438 },
439 },
440 {
441 name: "Exports plugin appends to plugins",
442 extra: `
443 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
444 java_library{name: "exports", exported_plugins: ["plugin"]}
445 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
446 `,
447 results: []Result{
448 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
449 },
450 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800451 {
452 name: "Exports plugin to with generates_api to dependee",
453 extra: `
454 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
455 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
456 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
457 `,
458 results: []Result{
459 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
460 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
461 },
462 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000463 }
464
465 for _, test := range tests {
466 t.Run(test.name, func(t *testing.T) {
467 ctx, _ := testJava(t, `
468 java_plugin {
469 name: "plugin",
470 processor_class: "com.android.TestPlugin",
471 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800472 java_plugin {
473 name: "plugin_generates_api",
474 generates_api: true,
475 processor_class: "com.android.TestPlugin",
476 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000477 `+test.extra)
478
479 for _, want := range test.results {
480 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
481 if javac.Args["processor"] != want.processors {
482 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
483 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800484 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
485 disableTurbine := turbine.BuildParams.Rule == nil
486 if disableTurbine != want.disableTurbine {
487 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
488 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000489 }
490 })
491 }
492}
493
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900494func TestSdkVersionByPartition(t *testing.T) {
495 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900496 java_library {
497 name: "foo",
498 srcs: ["a.java"],
499 vendor: true,
500 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900501 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900502
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900503 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900504 java_library {
505 name: "bar",
506 srcs: ["b.java"],
507 }
508 `)
509
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900510 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900511 bp := `
512 java_library {
513 name: "foo",
514 srcs: ["a.java"],
515 product_specific: true,
516 }
517 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800518
519 config := testConfig(nil, bp, nil)
520 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900521 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800522 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 +0900523 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800524 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900525 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900526 }
527}
528
Colin Crossd5934c82017-10-02 13:55:26 -0700529func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700530 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700531 java_library {
532 name: "foo",
533 srcs: ["a.java"],
534 target: {
535 android: {
536 srcs: ["b.java"],
537 },
538 },
539 }
540 `)
541
542 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
543 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
544 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
545 }
546}
547
Colin Cross6b4a32d2017-12-05 13:42:45 -0800548func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700549 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800550 java_library_host {
551 name: "foo",
552 srcs: ["a.java"],
553 }
554
555 java_binary_host {
556 name: "bar",
557 srcs: ["b.java"],
558 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700559 jni_libs: ["libjni"],
560 }
561
562 cc_library_shared {
563 name: "libjni",
564 host_supported: true,
565 device_supported: false,
566 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800567 }
568 `)
569
570 buildOS := android.BuildOs.String()
571
572 bar := ctx.ModuleForTests("bar", buildOS+"_common")
573 barJar := bar.Output("bar.jar").Output.String()
574 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
575 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
576
Colin Cross89226d92020-10-09 19:00:54 -0700577 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
578 libjniSO := libjni.Rule("Cp").Output.String()
579
Colin Cross6b4a32d2017-12-05 13:42:45 -0800580 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700581 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
582 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800583 }
Colin Cross89226d92020-10-09 19:00:54 -0700584
585 // Test that the install binary wrapper depends on the installed JNI libraries
586 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
587 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800588 }
Alex Humesky2070e322020-06-09 20:23:08 -0400589}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800590
Alex Humesky2070e322020-06-09 20:23:08 -0400591func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
592 bp := `
593 java_library {
594 name: "target_library",
595 srcs: ["a.java"],
596 }
597
598 java_binary_host {
599 name: "host_binary",
600 srcs: ["b.java"],
601 }
602 `
603 config := testConfig(nil, bp, nil)
604 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
605
606 ctx, _ := testJavaWithConfig(t, config)
607
Liz Kammer7941b302020-07-28 13:27:34 -0700608 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400609 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
610 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
611 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
612 t.Errorf("target library javac flags %v should contain "+
613 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
614 }
615
616 // check that -g is not overridden for host modules
617 buildOS := android.BuildOs.String()
618 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
619 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
620 if strings.Contains(hostJavaFlags, "-g:source,lines") {
621 t.Errorf("java_binary_host javac flags %v should not have "+
622 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
623 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800624}
625
Colin Cross72bb3632017-07-13 16:23:21 -0700626func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700627 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700628 java_library {
629 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000630 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000631 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700632 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700633 }
634
Colin Cross74d73e22017-08-02 11:05:49 -0700635 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700636 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700637 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700638 }
639
Colin Cross74d73e22017-08-02 11:05:49 -0700640 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700641 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700642 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700643 sdk_version: "current",
644 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700645 }
Colin Cross42be7612019-02-21 18:12:14 -0800646
647 dex_import {
648 name: "qux",
649 jars: ["b.jar"],
650 }
Colin Cross79c7c262019-04-17 11:11:46 -0700651
652 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000653 name: "sdklib",
654 public: {
655 jars: ["c.jar"],
656 },
657 }
658
Paul Duffin91547182019-11-12 19:39:36 +0000659 prebuilt_stubs_sources {
660 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000661 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000662 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000663
664 java_test_import {
665 name: "test",
666 jars: ["a.jar"],
667 test_suites: ["cts"],
668 test_config: "AndroidTest.xml",
669 }
Colin Cross72bb3632017-07-13 16:23:21 -0700670 `)
671
Paul Duffin9b478b02019-12-10 13:41:51 +0000672 fooModule := ctx.ModuleForTests("foo", "android_common")
673 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700674 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700675 barModule := ctx.ModuleForTests("bar", "android_common")
676 barJar := barModule.Rule("combineJar").Output
677 bazModule := ctx.ModuleForTests("baz", "android_common")
678 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700679 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700680
Paul Duffin9b478b02019-12-10 13:41:51 +0000681 fooLibrary := fooModule.Module().(*Library)
682 assertDeepEquals(t, "foo java sources incorrect",
683 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000684
Paul Duffin9b478b02019-12-10 13:41:51 +0000685 assertDeepEquals(t, "foo java source jars incorrect",
686 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
687 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000688
Colin Cross37f6d792018-07-12 12:28:41 -0700689 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
690 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700691 }
692
Liz Kammerd6c31d22020-08-05 15:40:41 -0700693 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
694 if barDexJar != nil {
695 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
696 }
697
Colin Cross79c7c262019-04-17 11:11:46 -0700698 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
699 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
700 }
701
Colin Cross37f6d792018-07-12 12:28:41 -0700702 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
703 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700704 }
Colin Cross42be7612019-02-21 18:12:14 -0800705
Liz Kammerd6c31d22020-08-05 15:40:41 -0700706 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
707 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
708 if bazDexJar != expectedDexJar {
709 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
710 }
711
Colin Cross42be7612019-02-21 18:12:14 -0800712 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700713}
714
Paul Duffin9b478b02019-12-10 13:41:51 +0000715func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
716 if !reflect.DeepEqual(expected, actual) {
717 t.Errorf("%s: expected %q, found %q", message, expected, actual)
718 }
719}
720
Paul Duffin1a393322020-11-18 16:36:47 +0000721func TestPrebuiltStubsSources(t *testing.T) {
722 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
723 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
724prebuilt_stubs_sources {
725 name: "stubs-source",
726 srcs: ["%s"],
727}`, sourcesPath), map[string][]byte{
728 "stubs/sources/pkg/A.java": nil,
729 "stubs/sources/pkg/B.java": nil,
730 })
731
732 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
733 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
734 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
735 }
736 }
737
738 t.Run("empty/missing directory", func(t *testing.T) {
739 test(t, "empty-directory", []string{})
740 })
741
742 t.Run("non-empty set of sources", func(t *testing.T) {
743 test(t, "stubs/sources", []string{
744 "stubs/sources/pkg/A.java",
745 "stubs/sources/pkg/B.java",
746 })
747 })
748}
749
Paul Duffin56d44902020-01-31 13:36:25 +0000750func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000751 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000752 java_library {
753 name: "foo",
754 srcs: ["a.java"],
755 libs: ["sdklib"],
756 sdk_version: "current",
757 }
758
759 java_library {
760 name: "foo.system",
761 srcs: ["a.java"],
762 libs: ["sdklib"],
763 sdk_version: "system_current",
764 }
765
766 java_library {
767 name: "foo.test",
768 srcs: ["a.java"],
769 libs: ["sdklib"],
770 sdk_version: "test_current",
771 }
772
773 java_sdk_library_import {
774 name: "sdklib",
775 public: {
776 jars: ["a.jar"],
777 },
778 system: {
779 jars: ["b.jar"],
780 },
781 test: {
782 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100783 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000784 },
785 }
786 `)
787
788 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000789 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000790 javac := fooModule.Rule("javac")
791
Paul Duffin22b77cd2021-03-12 19:15:01 +0000792 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
793 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000794 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100795
Paul Duffin22b77cd2021-03-12 19:15:01 +0000796 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100797 `prebuilt_sdklib.stubs`,
798 `prebuilt_sdklib.stubs.source.test`,
799 `prebuilt_sdklib.stubs.system`,
800 `prebuilt_sdklib.stubs.test`,
801 })
802}
803
804func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000805 result := javaFixtureFactory.Extend(
806 PrepareForTestWithJavaSdkLibraryFiles,
807 FixtureWithLastReleaseApis("sdklib"),
808 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100809 java_sdk_library {
810 name: "sdklib",
811 srcs: ["a.java"],
812 sdk_version: "none",
813 system_modules: "none",
814 public: {
815 enabled: true,
816 },
817 }
818
819 java_sdk_library_import {
820 name: "sdklib",
821 public: {
822 jars: ["a.jar"],
823 },
824 }
825 `)
826
Paul Duffin22b77cd2021-03-12 19:15:01 +0000827 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100828 `dex2oatd`,
829 `prebuilt_sdklib`,
830 `sdklib.impl`,
831 `sdklib.stubs`,
832 `sdklib.stubs.source`,
833 `sdklib.xml`,
834 })
835
Paul Duffin22b77cd2021-03-12 19:15:01 +0000836 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100837 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100838 `sdklib.impl`,
839 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
840 // dependency is added after prebuilts may have been renamed and so has to use
841 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100842 `sdklib.xml`,
843 })
844}
845
846func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000847 result := javaFixtureFactory.Extend(
848 PrepareForTestWithJavaSdkLibraryFiles,
849 FixtureWithLastReleaseApis("sdklib"),
850 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100851 java_sdk_library {
852 name: "sdklib",
853 srcs: ["a.java"],
854 sdk_version: "none",
855 system_modules: "none",
856 public: {
857 enabled: true,
858 },
859 }
860
861 java_sdk_library_import {
862 name: "sdklib",
863 prefer: true,
864 public: {
865 jars: ["a.jar"],
866 },
867 }
868 `)
869
Paul Duffin22b77cd2021-03-12 19:15:01 +0000870 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100871 `dex2oatd`,
872 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100873 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100874 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100875 `sdklib.stubs.source`,
876 `sdklib.xml`,
877 })
878
Paul Duffin22b77cd2021-03-12 19:15:01 +0000879 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100880 `prebuilt_sdklib.stubs`,
881 `sdklib.impl`,
882 `sdklib.xml`,
883 })
Paul Duffin56d44902020-01-31 13:36:25 +0000884}
885
JaeMan Parkff715562020-10-19 17:25:58 +0900886func TestJavaSdkLibraryEnforce(t *testing.T) {
887 partitionToBpOption := func(partition string) string {
888 switch partition {
889 case "system":
890 return ""
891 case "vendor":
892 return "soc_specific: true,"
893 case "product":
894 return "product_specific: true,"
895 default:
896 panic("Invalid partition group name: " + partition)
897 }
898 }
899
900 type testConfigInfo struct {
901 libraryType string
902 fromPartition string
903 toPartition string
904 enforceVendorInterface bool
905 enforceProductInterface bool
906 enforceJavaSdkLibraryCheck bool
907 allowList []string
908 }
909
Paul Duffin05f72de2021-03-12 21:28:51 +0000910 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900911 bpFileTemplate := `
912 java_library {
913 name: "foo",
914 srcs: ["foo.java"],
915 libs: ["bar"],
916 sdk_version: "current",
917 %s
918 }
919
920 %s {
921 name: "bar",
922 srcs: ["bar.java"],
923 sdk_version: "current",
924 %s
925 }
926 `
927
928 bpFile := fmt.Sprintf(bpFileTemplate,
929 partitionToBpOption(info.fromPartition),
930 info.libraryType,
931 partitionToBpOption(info.toPartition))
932
Paul Duffin05f72de2021-03-12 21:28:51 +0000933 return android.GroupFixturePreparers(
Paul Duffin163043d2021-03-12 19:18:49 +0000934 PrepareForTestWithJavaSdkLibraryFiles,
935 FixtureWithLastReleaseApis("bar"),
Paul Duffin05f72de2021-03-12 21:28:51 +0000936 android.FixtureWithRootAndroidBp(bpFile),
937 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
938 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
939 if info.enforceVendorInterface {
940 variables.DeviceVndkVersion = proptools.StringPtr("current")
941 }
942 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
943 variables.InterPartitionJavaLibraryAllowList = info.allowList
944 }),
945 )
JaeMan Parkff715562020-10-19 17:25:58 +0900946 }
947
Paul Duffinaa6caa72021-03-13 16:50:15 +0000948 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
949 t.Run(fmt.Sprintf("%#v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000950 errorHandler := android.FixtureExpectsNoErrors
951 if expectedErrorPattern != "" {
952 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000953 }
Paul Duffin05f72de2021-03-12 21:28:51 +0000954 javaFixtureFactory.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
Paul Duffinaa6caa72021-03-13 16:50:15 +0000955 })
956 }
957
JaeMan Parkff715562020-10-19 17:25:58 +0900958 errorMessage := "is not allowed across the partitions"
959
Paul Duffinaa6caa72021-03-13 16:50:15 +0000960 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900961 libraryType: "java_library",
962 fromPartition: "product",
963 toPartition: "system",
964 enforceVendorInterface: true,
965 enforceProductInterface: true,
966 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000967 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900968
Paul Duffinaa6caa72021-03-13 16:50:15 +0000969 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900970 libraryType: "java_library",
971 fromPartition: "product",
972 toPartition: "system",
973 enforceVendorInterface: true,
974 enforceProductInterface: false,
975 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000976 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900977
Paul Duffinaa6caa72021-03-13 16:50:15 +0000978 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900979 libraryType: "java_library",
980 fromPartition: "product",
981 toPartition: "system",
982 enforceVendorInterface: true,
983 enforceProductInterface: true,
984 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000985 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900986
Paul Duffinaa6caa72021-03-13 16:50:15 +0000987 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900988 libraryType: "java_library",
989 fromPartition: "vendor",
990 toPartition: "system",
991 enforceVendorInterface: true,
992 enforceProductInterface: true,
993 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000994 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900995
Paul Duffinaa6caa72021-03-13 16:50:15 +0000996 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900997 libraryType: "java_library",
998 fromPartition: "vendor",
999 toPartition: "system",
1000 enforceVendorInterface: true,
1001 enforceProductInterface: true,
1002 enforceJavaSdkLibraryCheck: true,
1003 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +00001004 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +09001005
Paul Duffinaa6caa72021-03-13 16:50:15 +00001006 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +09001007 libraryType: "java_library",
1008 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +09001009 toPartition: "product",
1010 enforceVendorInterface: true,
1011 enforceProductInterface: true,
1012 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001013 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +09001014
Paul Duffinaa6caa72021-03-13 16:50:15 +00001015 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001016 libraryType: "java_sdk_library",
1017 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +09001018 toPartition: "system",
1019 enforceVendorInterface: true,
1020 enforceProductInterface: true,
1021 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001022 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001023
Paul Duffinaa6caa72021-03-13 16:50:15 +00001024 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001025 libraryType: "java_sdk_library",
1026 fromPartition: "vendor",
1027 toPartition: "system",
1028 enforceVendorInterface: true,
1029 enforceProductInterface: true,
1030 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001031 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +09001032
Paul Duffinaa6caa72021-03-13 16:50:15 +00001033 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +09001034 libraryType: "java_sdk_library",
1035 fromPartition: "vendor",
1036 toPartition: "product",
1037 enforceVendorInterface: true,
1038 enforceProductInterface: true,
1039 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +00001040 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +09001041}
1042
Colin Cross89536d42017-07-07 14:35:50 -07001043func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001044 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -07001045 java_defaults {
1046 name: "defaults",
1047 srcs: ["a.java"],
1048 libs: ["bar"],
1049 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -07001050 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -07001051 }
1052
1053 java_library {
1054 name: "foo",
1055 defaults: ["defaults"],
1056 }
1057
1058 java_library {
1059 name: "bar",
1060 srcs: ["b.java"],
1061 }
1062
1063 java_library {
1064 name: "baz",
1065 srcs: ["c.java"],
1066 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001067
1068 android_test {
1069 name: "atestOptimize",
1070 defaults: ["defaults"],
1071 optimize: {enabled: true},
1072 }
1073
1074 android_test {
1075 name: "atestNoOptimize",
1076 defaults: ["defaults"],
1077 }
1078
1079 android_test {
1080 name: "atestDefault",
1081 srcs: ["a.java"],
1082 }
Colin Cross89536d42017-07-07 14:35:50 -07001083 `)
1084
Colin Cross4c428df2017-09-15 17:36:05 -07001085 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001086 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001087
1088 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1089 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1090 }
1091
Nan Zhanged19fc32017-10-19 13:06:22 -07001092 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1093 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1094 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001095 }
1096
Colin Cross1ee23172017-10-18 14:44:18 -07001097 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001098 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1099 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001100 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001101
1102 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1103 if atestOptimize.Output == nil {
1104 t.Errorf("atestOptimize should optimize APK")
1105 }
1106
1107 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1108 if atestNoOptimize.Output == nil {
1109 t.Errorf("atestNoOptimize should not optimize APK")
1110 }
1111
1112 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1113 if atestDefault.Output == nil {
1114 t.Errorf("atestDefault should optimize APK")
1115 }
Colin Cross89536d42017-07-07 14:35:50 -07001116}
1117
Colin Cross0f37af02017-09-27 17:42:05 -07001118func TestResources(t *testing.T) {
1119 var table = []struct {
1120 name string
1121 prop string
1122 extra string
1123 args string
1124 }{
1125 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001126 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001127 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001128 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001129 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001130 },
1131 {
1132 // Test that a module with java_resources includes the files
1133 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001134 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1135 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001136 },
1137 {
1138 // Test that a module with a filegroup in java_resources includes the files with the
1139 // path prefix
1140 name: "resource filegroup",
1141 prop: `java_resources: [":foo-res"]`,
1142 extra: `
1143 filegroup {
1144 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001145 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001146 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001147 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001148 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001149 },
1150 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001151 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1152 name: "wildcard dirs",
1153 prop: `java_resource_dirs: ["java-res/*"]`,
1154 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1155 },
1156 {
1157 // Test that a module exclude_java_resource_dirs excludes the files
1158 name: "wildcard dirs",
1159 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1160 args: "-C java-res/a -f java-res/a/a",
1161 },
Colin Crosscedd4762018-09-13 11:26:19 -07001162 {
1163 // Test wildcards in java_resources
1164 name: "wildcard files",
1165 prop: `java_resources: ["java-res/**/*"]`,
1166 args: "-C . -f java-res/a/a -f java-res/b/b",
1167 },
1168 {
1169 // Test exclude_java_resources with java_resources
1170 name: "wildcard files with exclude",
1171 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1172 args: "-C . -f java-res/a/a",
1173 },
1174 {
1175 // Test exclude_java_resources with java_resource_dirs
1176 name: "resource dirs with exclude files",
1177 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1178 args: "-C java-res -f java-res/a/a",
1179 },
1180 {
1181 // Test exclude_java_resource_dirs with java_resource_dirs
1182 name: "resource dirs with exclude files",
1183 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1184 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1185 },
Colin Cross0f37af02017-09-27 17:42:05 -07001186 }
1187
1188 for _, test := range table {
1189 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001190 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001191 java_library {
1192 name: "foo",
1193 srcs: [
1194 "a.java",
1195 "b.java",
1196 "c.java",
1197 ],
1198 `+test.prop+`,
1199 }
Colin Cross238c1f32020-06-07 16:58:18 -07001200 `+test.extra,
1201 map[string][]byte{
1202 "java-res/a/a": nil,
1203 "java-res/b/b": nil,
1204 "java-res2/a": nil,
1205 },
1206 )
Colin Cross0f37af02017-09-27 17:42:05 -07001207
Colin Cross331a1212018-08-15 20:40:52 -07001208 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001209 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001210
1211 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1212 t.Errorf("foo combined jars %v does not contain %q",
1213 foo.Inputs.Strings(), fooRes.Output.String())
1214 }
1215
Colin Crossaf9c55b2017-10-03 14:50:08 -07001216 if fooRes.Args["jarArgs"] != test.args {
1217 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001218 fooRes.Args["jarArgs"], test.args)
1219 }
1220 })
1221 }
1222}
1223
Colin Cross0c4ce212019-05-03 15:28:19 -07001224func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001225 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001226 java_library {
1227 name: "foo",
1228 srcs: [
1229 "a.java",
1230 "b.java",
1231 "c.java",
1232 ],
1233 include_srcs: true,
1234 }
1235
1236 java_library {
1237 name: "bar",
1238 srcs: [
1239 "a.java",
1240 "b.java",
1241 "c.java",
1242 ],
1243 java_resource_dirs: ["java-res"],
1244 include_srcs: true,
1245 }
Colin Cross238c1f32020-06-07 16:58:18 -07001246 `, map[string][]byte{
1247 "java-res/a/a": nil,
1248 "java-res/b/b": nil,
1249 "java-res2/a": nil,
1250 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001251
1252 // Test a library with include_srcs: true
1253 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1254 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1255
1256 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1257 t.Errorf("foo combined jars %v does not contain %q", w, g)
1258 }
1259
1260 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1261 t.Errorf("foo source jar args %q is not %q", w, g)
1262 }
1263
1264 // Test a library with include_srcs: true and resources
1265 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1266 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1267 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1268 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1269
1270 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1271 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1272 }
1273
1274 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1275 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1276 }
1277
1278 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1279 t.Errorf("bar combined jars %v does not contain %q", w, g)
1280 }
1281
1282 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1283 t.Errorf("bar source jar args %q is not %q", w, g)
1284 }
1285
1286 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1287 t.Errorf("bar resource jar args %q is not %q", w, g)
1288 }
1289}
1290
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001291func TestJavaLint(t *testing.T) {
1292 ctx, _ := testJavaWithFS(t, `
1293 java_library {
1294 name: "foo",
1295 srcs: [
1296 "a.java",
1297 "b.java",
1298 "c.java",
1299 ],
1300 min_sdk_version: "29",
1301 sdk_version: "system_current",
1302 }
1303 `, map[string][]byte{
1304 "lint-baseline.xml": nil,
1305 })
1306
1307 foo := ctx.ModuleForTests("foo", "android_common")
1308 rule := foo.Rule("lint")
1309
1310 if !strings.Contains(rule.RuleParams.Command, "--baseline lint-baseline.xml") {
1311 t.Error("did not pass --baseline flag")
1312 }
1313}
1314
1315func TestJavaLintWithoutBaseline(t *testing.T) {
1316 ctx, _ := testJavaWithFS(t, `
1317 java_library {
1318 name: "foo",
1319 srcs: [
1320 "a.java",
1321 "b.java",
1322 "c.java",
1323 ],
1324 min_sdk_version: "29",
1325 sdk_version: "system_current",
1326 }
1327 `, map[string][]byte{})
1328
1329 foo := ctx.ModuleForTests("foo", "android_common")
1330 rule := foo.Rule("lint")
1331
1332 if strings.Contains(rule.RuleParams.Command, "--baseline") {
1333 t.Error("passed --baseline flag for non existent file")
1334 }
1335}
1336
1337func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
1338 config := testConfig(
1339 nil,
1340 `
1341 java_library {
1342 name: "foo",
1343 srcs: [
1344 ],
1345 min_sdk_version: "29",
1346 sdk_version: "system_current",
1347 lint: {
1348 baseline_filename: "mybaseline.xml",
1349 },
1350 }
1351 `, map[string][]byte{
1352 "build/soong/java/lint_defaults.txt": nil,
1353 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1354 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1355 "framework/aidl": nil,
1356 "a.java": nil,
1357 "AndroidManifest.xml": nil,
1358 "build/make/target/product/security": nil,
1359 })
1360 config.TestAllowNonExistentPaths = false
1361 testJavaErrorWithConfig(t,
1362 "source path \"mybaseline.xml\" does not exist",
1363 config,
1364 )
1365}
1366
1367func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1368 ctx, _ := testJavaWithFS(t, `
1369 java_library {
1370 name: "foo",
1371 srcs: [
1372 "a.java",
1373 "b.java",
1374 "c.java",
1375 ],
1376 min_sdk_version: "29",
1377 sdk_version: "system_current",
1378 lint: {
1379 error_checks: ["SomeCheck"],
1380 baseline_filename: "mybaseline.xml",
1381 },
1382 }
1383 `, map[string][]byte{
1384 "mybaseline.xml": nil,
1385 })
1386
1387 foo := ctx.ModuleForTests("foo", "android_common")
1388 rule := foo.Rule("lint")
1389
1390 if !strings.Contains(rule.RuleParams.Command, "--baseline mybaseline.xml") {
1391 t.Error("did not use the correct file for baseline")
1392 }
1393}
1394
Colin Cross54190b32017-10-09 15:34:10 -07001395func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001396 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001397 java_library {
1398 name: "foo",
1399 srcs: [
1400 "a*.java",
1401 ":gen",
1402 "b*.java",
1403 ],
1404 }
1405
1406 genrule {
1407 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001408 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001409 out: ["gen.java"],
1410 }
Colin Cross238c1f32020-06-07 16:58:18 -07001411 `, map[string][]byte{
1412 "a.java": nil,
1413 "b.java": nil,
1414 })
Colin Cross54190b32017-10-09 15:34:10 -07001415
1416 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1417 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1418
Colin Cross15e86d92017-10-20 15:07:08 -07001419 if filepath.Base(genrule.Output.String()) != "gen.java" {
1420 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001421 }
1422
1423 if len(javac.Inputs) != 3 ||
1424 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001425 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001426 javac.Inputs[2].String() != "b.java" {
1427 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1428 }
1429}
1430
Nan Zhang61eaedb2017-11-02 13:28:15 -07001431func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001432 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001433 java_library {
1434 name: "foo",
1435 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001436 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001437 }
1438
1439 java_library {
1440 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001441 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001442 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001443 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001444 }
1445
1446 java_library {
1447 name: "baz",
1448 srcs: ["c.java"],
1449 libs: ["bar"],
1450 sdk_version: "14",
1451 }
1452 `)
1453
1454 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1455 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1456 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1457 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1458 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1459
1460 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1461 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1462 }
1463
1464 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1465 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1466 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1467 }
1468 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1469 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1470 }
1471 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1472 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1473 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001474 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001475 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001476 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001477 }
1478}
1479
1480func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001481 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001482 java_library {
1483 name: "bar",
1484 srcs: ["a.java","b.java","c.java"],
1485 javac_shard_size: 1
1486 }
1487 `)
1488
1489 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1490 for i := 0; i < 3; i++ {
1491 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1492 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1493 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1494 }
1495 }
1496}
1497
Nan Zhang581fd212018-01-10 16:06:12 -08001498func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001499 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001500 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001501 name: "droiddoc-templates-sdk",
1502 path: ".",
1503 }
Jiyong Park29074592019-07-07 16:27:47 +09001504 filegroup {
1505 name: "bar-doc-aidl-srcs",
1506 srcs: ["bar-doc/IBar.aidl"],
1507 path: "bar-doc",
1508 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001509 droidstubs {
1510 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001511 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001512 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001513 ],
Steve Kim3666c702020-09-01 17:58:01 +00001514 exclude_srcs: [
1515 "bar-doc/b.java"
1516 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001517 api_levels_annotations_dirs: [
1518 "droiddoc-templates-sdk",
1519 ],
1520 api_levels_annotations_enabled: true,
1521 }
1522 droiddoc {
1523 name: "bar-doc",
1524 srcs: [
1525 ":bar-stubs",
1526 "bar-doc/IFoo.aidl",
1527 ":bar-doc-aidl-srcs",
1528 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001529 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001530 hdf: [
1531 "android.whichdoc offline",
1532 ],
1533 knowntags: [
1534 "bar-doc/known_oj_tags.txt",
1535 ],
1536 proofread_file: "libcore-proofread.txt",
1537 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001538 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001539 }
Colin Cross238c1f32020-06-07 16:58:18 -07001540 `,
1541 map[string][]byte{
1542 "bar-doc/a.java": nil,
1543 "bar-doc/b.java": nil,
1544 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001545 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1546 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1547 if err != nil {
1548 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1549 }
1550 if len(barStubsOutputs) != 1 {
1551 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001552 }
Nan Zhang581fd212018-01-10 16:06:12 -08001553
Liz Kammere1ab2502020-09-10 15:29:25 +00001554 barStubsOutput := barStubsOutputs[0]
1555 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1556 javaDoc := barDoc.Rule("javadoc")
1557 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1558 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001559 }
1560
Liz Kammere1ab2502020-09-10 15:29:25 +00001561 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1562 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1563 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1564 }
1565
1566 aidl := barDoc.Rule("aidl")
1567 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001568 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1569 }
1570
1571 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1572 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001573 }
Nan Zhang581fd212018-01-10 16:06:12 -08001574}
1575
Liz Kammer585cac22020-07-06 09:12:57 -07001576func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1577 testJavaError(t, "flags is set. Cannot set args", `
1578 droiddoc_exported_dir {
1579 name: "droiddoc-templates-sdk",
1580 path: ".",
1581 }
1582 filegroup {
1583 name: "bar-doc-aidl-srcs",
1584 srcs: ["bar-doc/IBar.aidl"],
1585 path: "bar-doc",
1586 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001587 droidstubs {
1588 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001589 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001590 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001591 ],
Steve Kim3666c702020-09-01 17:58:01 +00001592 exclude_srcs: [
1593 "bar-doc/b.java"
1594 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001595 api_levels_annotations_dirs: [
1596 "droiddoc-templates-sdk",
1597 ],
1598 api_levels_annotations_enabled: true,
1599 }
1600 droiddoc {
1601 name: "bar-doc",
1602 srcs: [
1603 ":bar-stubs",
1604 "bar-doc/IFoo.aidl",
1605 ":bar-doc-aidl-srcs",
1606 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001607 custom_template: "droiddoc-templates-sdk",
1608 hdf: [
1609 "android.whichdoc offline",
1610 ],
1611 knowntags: [
1612 "bar-doc/known_oj_tags.txt",
1613 ],
1614 proofread_file: "libcore-proofread.txt",
1615 todo_file: "libcore-docs-todo.html",
1616 flags: ["-offlinemode -title \"libcore\""],
1617 args: "-offlinemode -title \"libcore\"",
1618 }
1619 `)
1620}
1621
Liz Kammer3d894b72020-08-04 09:55:13 -07001622func TestDroidstubs(t *testing.T) {
1623 ctx, _ := testJavaWithFS(t, `
1624 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001625 name: "droiddoc-templates-sdk",
1626 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001627 }
1628
1629 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001630 name: "bar-stubs",
1631 srcs: ["bar-doc/a.java"],
1632 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1633 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001634 }
1635
1636 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001637 name: "bar-stubs-other",
1638 srcs: ["bar-doc/a.java"],
1639 high_mem: true,
1640 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1641 api_levels_annotations_enabled: true,
1642 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001643 }
1644 `,
1645 map[string][]byte{
1646 "bar-doc/a.java": nil,
1647 })
1648 testcases := []struct {
1649 moduleName string
1650 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001651 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001652 }{
1653 {
1654 moduleName: "bar-stubs",
1655 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001656 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001657 },
1658 {
1659 moduleName: "bar-stubs-other",
1660 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001661 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001662 },
1663 }
1664 for _, c := range testcases {
1665 m := ctx.ModuleForTests(c.moduleName, "android_common")
1666 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001667 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001668 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001669 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001670 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1671 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001672
1673 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1674 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1675 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001676 }
1677}
1678
Paul Duffin83a2d962019-11-19 19:44:10 +00001679func TestDroidstubsWithSystemModules(t *testing.T) {
1680 ctx, _ := testJava(t, `
1681 droidstubs {
1682 name: "stubs-source-system-modules",
1683 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001684 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001685 ],
1686 sdk_version: "none",
1687 system_modules: "source-system-modules",
1688 }
1689
1690 java_library {
1691 name: "source-jar",
1692 srcs: [
1693 "a.java",
1694 ],
1695 }
1696
1697 java_system_modules {
1698 name: "source-system-modules",
1699 libs: ["source-jar"],
1700 }
1701
1702 droidstubs {
1703 name: "stubs-prebuilt-system-modules",
1704 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001705 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001706 ],
1707 sdk_version: "none",
1708 system_modules: "prebuilt-system-modules",
1709 }
1710
1711 java_import {
1712 name: "prebuilt-jar",
1713 jars: ["a.jar"],
1714 }
1715
1716 java_system_modules_import {
1717 name: "prebuilt-system-modules",
1718 libs: ["prebuilt-jar"],
1719 }
1720 `)
1721
1722 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1723
1724 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1725}
1726
1727func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1728 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1729 var systemJars []string
1730 for _, i := range metalavaRule.Implicits {
1731 systemJars = append(systemJars, i.Base())
1732 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001733 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001734 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1735 }
1736}
1737
Colin Cross54250902017-12-05 09:28:08 -08001738func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001739 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001740 java_library {
1741 name: "foo",
1742 srcs: ["a.java"],
1743 }
1744
1745 java_genrule {
1746 name: "jargen",
1747 tool_files: ["b.java"],
1748 cmd: "$(location b.java) $(in) $(out)",
1749 out: ["jargen.jar"],
1750 srcs: [":foo"],
1751 }
1752
1753 java_library {
1754 name: "bar",
1755 static_libs: ["jargen"],
1756 srcs: ["c.java"],
1757 }
1758
1759 java_library {
1760 name: "baz",
1761 libs: ["jargen"],
1762 srcs: ["c.java"],
1763 }
1764 `)
1765
1766 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1767 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1768 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1769 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1770 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1771
Colin Cross3d680512020-11-13 16:23:53 -08001772 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1773 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001774 }
1775
1776 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1777 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1778 }
1779
1780 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1781 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1782 }
1783
1784 if len(barCombined.Inputs) != 2 ||
1785 barCombined.Inputs[0].String() != bar.Output.String() ||
1786 barCombined.Inputs[1].String() != jargen.Output.String() {
1787 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1788 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1789 }
1790}
1791
Nan Zhang27e284d2018-02-09 21:03:53 +00001792func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001793 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001794 java_library {
1795 name: "foo",
1796 srcs: ["a.java", ":foo-srcs"],
1797 exclude_srcs: ["a.java", ":foo-excludes"],
1798 }
1799
1800 filegroup {
1801 name: "foo-srcs",
1802 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1803 }
1804
1805 filegroup {
1806 name: "foo-excludes",
1807 srcs: ["java-fg/a.java", "java-fg/b.java"],
1808 }
1809 `)
1810
1811 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1812
1813 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1814 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1815 }
1816}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001817
Paul Duffin52d398a2019-06-11 12:31:14 +01001818func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001819 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001820 "libcore/Android.bp": []byte(`
1821 java_library {
1822 name: "core",
1823 sdk_version: "none",
1824 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001825 }
1826
1827 filegroup {
1828 name: "core-jar",
1829 srcs: [":core{.jar}"],
1830 }
1831`),
1832 })
Colin Crossae8600b2020-10-29 17:09:13 -07001833 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001834 run(t, ctx, config)
1835}
1836
1837func TestJavaImport(t *testing.T) {
1838 config := testConfig(nil, "", map[string][]byte{
1839 "libcore/Android.bp": []byte(`
1840 java_import {
1841 name: "core",
1842 sdk_version: "none",
1843 }
1844
1845 filegroup {
1846 name: "core-jar",
1847 srcs: [":core{.jar}"],
1848 }
1849`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001850 })
Colin Crossae8600b2020-10-29 17:09:13 -07001851 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001852 run(t, ctx, config)
1853}
1854
Jiyong Parkc678ad32018-04-10 13:07:10 +09001855func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001856 result := javaFixtureFactory.Extend(
1857 PrepareForTestWithJavaSdkLibraryFiles,
1858 FixtureWithPrebuiltApis(map[string][]string{
1859 "28": {"foo"},
1860 "29": {"foo"},
1861 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1862 }),
1863 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001864 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001865 name: "droiddoc-templates-sdk",
1866 path: ".",
1867 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001868 java_sdk_library {
1869 name: "foo",
1870 srcs: ["a.java", "b.java"],
1871 api_packages: ["foo"],
1872 }
1873 java_sdk_library {
1874 name: "bar",
1875 srcs: ["a.java", "b.java"],
1876 api_packages: ["bar"],
1877 }
1878 java_library {
1879 name: "baz",
1880 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001881 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001882 sdk_version: "system_current",
1883 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001884 java_sdk_library {
1885 name: "barney",
1886 srcs: ["c.java"],
1887 api_only: true,
1888 }
1889 java_sdk_library {
1890 name: "betty",
1891 srcs: ["c.java"],
1892 shared_library: false,
1893 }
Paul Duffin859fe962020-05-15 10:20:31 +01001894 java_sdk_library_import {
1895 name: "quuz",
1896 public: {
1897 jars: ["c.jar"],
1898 },
1899 }
1900 java_sdk_library_import {
1901 name: "fred",
1902 public: {
1903 jars: ["b.jar"],
1904 },
1905 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001906 java_sdk_library_import {
1907 name: "wilma",
1908 public: {
1909 jars: ["b.jar"],
1910 },
1911 shared_library: false,
1912 }
Jiyong Park1be96912018-05-28 18:02:19 +09001913 java_library {
1914 name: "qux",
1915 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001916 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001917 sdk_version: "system_current",
1918 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001919 java_library {
1920 name: "baz-test",
1921 srcs: ["c.java"],
1922 libs: ["foo"],
1923 sdk_version: "test_current",
1924 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001925 java_library {
1926 name: "baz-29",
1927 srcs: ["c.java"],
1928 libs: ["foo"],
1929 sdk_version: "system_29",
1930 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001931 java_library {
1932 name: "baz-module-30",
1933 srcs: ["c.java"],
1934 libs: ["foo"],
1935 sdk_version: "module_30",
1936 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001937 `)
1938
1939 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001940 result.ModuleForTests("foo", "android_common")
1941 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1942 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1943 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1944 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1945 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1946 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1947 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1948 result.ModuleForTests("foo.api.public.28", "")
1949 result.ModuleForTests("foo.api.system.28", "")
1950 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001951
Paul Duffin22b77cd2021-03-12 19:15:01 +00001952 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001953 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001954 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001955 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001956 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001957 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001958 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001959
Paul Duffin22b77cd2021-03-12 19:15:01 +00001960 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001961 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001962 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001963
Paul Duffin22b77cd2021-03-12 19:15:01 +00001964 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001965 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001966 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001967
Paul Duffin22b77cd2021-03-12 19:15:01 +00001968 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001969 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001970 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 +01001971
Jiyong Park1be96912018-05-28 18:02:19 +09001972 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001973 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001974 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001975 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001976 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001977 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001978}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001979
Anton Hansson7f66efa2020-10-08 14:47:23 +01001980func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001981 result := javaFixtureFactory.Extend(
1982 PrepareForTestWithJavaSdkLibraryFiles,
1983 FixtureWithLastReleaseApis("sdklib"),
1984 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001985 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001986 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001987 srcs: ["a.java"],
1988 impl_only_libs: ["foo"],
1989 stub_only_libs: ["bar"],
1990 }
1991 java_library {
1992 name: "foo",
1993 srcs: ["a.java"],
1994 sdk_version: "current",
1995 }
1996 java_library {
1997 name: "bar",
1998 srcs: ["a.java"],
1999 sdk_version: "current",
2000 }
2001 `)
2002
Anton Hanssondff2c782020-12-21 17:10:01 +00002003 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002004 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01002005 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
2006 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
2007 }
2008 }
Anton Hanssondff2c782020-12-21 17:10:01 +00002009 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00002010 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01002011 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
2012 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
2013 }
2014}
2015
Paul Duffindaaa3322020-05-26 18:13:57 +01002016func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002017 result := javaFixtureFactory.Extend(
2018 PrepareForTestWithJavaSdkLibraryFiles,
2019 FixtureWithLastReleaseApis("foo"),
2020 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01002021 java_sdk_library {
2022 name: "foo",
2023 srcs: ["a.java"],
2024 api_only: true,
2025 public: {
2026 enabled: true,
2027 },
2028 }
2029
2030 java_library {
2031 name: "bar",
2032 srcs: ["b.java"],
2033 libs: ["foo"],
2034 }
2035 `)
2036
2037 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002038 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01002039 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2040 t.Errorf("expected %q, found %#q", expected, actual)
2041 }
2042}
2043
Paul Duffin46dc45a2020-05-14 15:39:10 +01002044func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002045 javaFixtureFactory.Extend(
2046 PrepareForTestWithJavaSdkLibraryFiles,
2047 FixtureWithLastReleaseApis("foo"),
2048 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002049 java_sdk_library {
2050 name: "foo",
2051 srcs: ["a.java"],
2052 api_packages: ["foo"],
2053 public: {
2054 enabled: true,
2055 },
2056 }
2057
2058 java_library {
2059 name: "bar",
2060 srcs: ["b.java", ":foo{.public.stubs.source}"],
2061 }
2062 `)
2063}
2064
2065func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002066 javaFixtureFactory.
Paul Duffin163043d2021-03-12 19:18:49 +00002067 Extend(
2068 PrepareForTestWithJavaSdkLibraryFiles,
2069 FixtureWithLastReleaseApis("foo"),
2070 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00002071 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
2072 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002073 java_sdk_library {
2074 name: "foo",
2075 srcs: ["a.java"],
2076 api_packages: ["foo"],
2077 public: {
2078 enabled: true,
2079 },
2080 }
2081
2082 java_library {
2083 name: "bar",
2084 srcs: ["b.java", ":foo{.system.stubs.source}"],
2085 }
2086 `)
2087}
2088
Paul Duffinca8d9a52020-06-26 22:20:25 +01002089func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002090 result := javaFixtureFactory.Extend(
2091 PrepareForTestWithJavaSdkLibraryFiles,
2092 FixtureWithLastReleaseApis("sdklib"),
2093 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01002094 java_sdk_library {
2095 name: "sdklib",
2096 srcs: ["a.java"],
2097 sdk_version: "none",
2098 system_modules: "none",
2099 public: {
2100 enabled: true,
2101 },
2102 }
2103 `)
2104
Paul Duffin22b77cd2021-03-12 19:15:01 +00002105 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01002106 `dex2oatd`,
2107 `sdklib.impl`,
2108 `sdklib.stubs`,
2109 `sdklib.stubs.source`,
2110 `sdklib.xml`,
2111 })
2112}
2113
Paul Duffin46dc45a2020-05-14 15:39:10 +01002114func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002115 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002116 java_sdk_library_import {
2117 name: "foo",
2118 public: {
2119 jars: ["a.jar"],
2120 stub_srcs: ["a.java"],
2121 current_api: "api/current.txt",
2122 removed_api: "api/removed.txt",
2123 },
2124 }
2125
2126 java_library {
2127 name: "bar",
2128 srcs: [":foo{.public.stubs.source}"],
2129 java_resources: [
2130 ":foo{.public.api.txt}",
2131 ":foo{.public.removed-api.txt}",
2132 ],
2133 }
2134 `)
2135}
2136
2137func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2138 bp := `
2139 java_sdk_library_import {
2140 name: "foo",
2141 public: {
2142 jars: ["a.jar"],
2143 },
2144 }
2145 `
2146
2147 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002148 javaFixtureFactory.
2149 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
2150 RunTestWithBp(t, bp+`
2151 java_library {
2152 name: "bar",
2153 srcs: [":foo{.public.stubs.source}"],
2154 java_resources: [
2155 ":foo{.public.api.txt}",
2156 ":foo{.public.removed-api.txt}",
2157 ],
2158 }
2159 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002160 })
2161
2162 t.Run("api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002163 javaFixtureFactory.
2164 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
2165 RunTestWithBp(t, bp+`
2166 java_library {
2167 name: "bar",
2168 srcs: ["a.java"],
2169 java_resources: [
2170 ":foo{.public.api.txt}",
2171 ],
2172 }
2173 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002174 })
2175
2176 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002177 javaFixtureFactory.
2178 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
2179 RunTestWithBp(t, bp+`
2180 java_library {
2181 name: "bar",
2182 srcs: ["a.java"],
2183 java_resources: [
2184 ":foo{.public.removed-api.txt}",
2185 ],
2186 }
2187 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002188 })
2189}
2190
Paul Duffin3375e352020-04-28 10:44:03 +01002191func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002192 javaFixtureFactory.
2193 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
2194 RunTestWithBp(t, `
2195 java_sdk_library {
2196 name: "foo",
2197 srcs: ["a.java", "b.java"],
2198 api_packages: ["foo"],
2199 // Explicitly disable public to test the check that ensures the set of enabled
2200 // scopes is consistent.
2201 public: {
2202 enabled: false,
2203 },
2204 system: {
2205 enabled: true,
2206 },
2207 }
Paul Duffin3375e352020-04-28 10:44:03 +01002208 `)
2209}
2210
Paul Duffin87a05a32020-05-12 11:50:28 +01002211func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002212 javaFixtureFactory.Extend(
2213 PrepareForTestWithJavaSdkLibraryFiles,
2214 FixtureWithLastReleaseApis("foo"),
2215 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002216 java_sdk_library {
2217 name: "foo",
2218 srcs: ["a.java", "b.java"],
2219 api_packages: ["foo"],
2220 system: {
2221 enabled: true,
2222 sdk_version: "module_current",
2223 },
2224 }
2225 `)
2226}
2227
Paul Duffin0c5bae52020-06-02 13:00:08 +01002228func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002229 javaFixtureFactory.Extend(
2230 PrepareForTestWithJavaSdkLibraryFiles,
2231 FixtureWithLastReleaseApis("foo"),
2232 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002233 java_sdk_library {
2234 name: "foo",
2235 srcs: ["a.java", "b.java"],
2236 api_packages: ["foo"],
2237 system: {
2238 enabled: true,
2239 },
2240 module_lib: {
2241 enabled: true,
2242 },
2243 }
2244 `)
2245}
2246
2247func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002248 javaFixtureFactory.Extend(
2249 PrepareForTestWithJavaSdkLibraryFiles,
2250 FixtureWithLastReleaseApis("foo"),
2251 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002252 java_sdk_library {
2253 name: "foo",
2254 srcs: ["a.java", "b.java"],
2255 api_packages: ["foo"],
2256 system: {
2257 enabled: true,
2258 },
2259 system_server: {
2260 enabled: true,
2261 },
2262 }
2263 `)
2264}
2265
Paul Duffin803a9562020-05-20 11:52:25 +01002266func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002267 javaFixtureFactory.
2268 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2269 RunTestWithBp(t, `
2270 java_sdk_library {
2271 name: "foo",
2272 srcs: ["a.java"],
2273 public: {
2274 enabled: false,
2275 },
2276 }
Paul Duffin803a9562020-05-20 11:52:25 +01002277
Paul Duffin22b77cd2021-03-12 19:15:01 +00002278 java_library {
2279 name: "baz",
2280 srcs: ["a.java"],
2281 libs: ["foo"],
2282 sdk_version: "module_current",
2283 }
Paul Duffin803a9562020-05-20 11:52:25 +01002284 `)
2285}
2286
2287func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002288 javaFixtureFactory.Extend(
2289 PrepareForTestWithJavaSdkLibraryFiles,
2290 FixtureWithLastReleaseApis("foo"),
2291 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002292 java_sdk_library {
2293 name: "foo",
2294 srcs: ["a.java"],
2295 system: {
2296 enabled: true,
2297 },
2298 }
2299
2300 java_library {
2301 name: "baz",
2302 srcs: ["a.java"],
2303 libs: ["foo"],
2304 // foo does not have module-lib scope so it should fallback to system
2305 sdk_version: "module_current",
2306 }
2307 `)
2308}
2309
Jiyong Park932cdfe2020-05-28 00:19:53 +09002310func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002311 result := javaFixtureFactory.Extend(
2312 PrepareForTestWithJavaSdkLibraryFiles,
2313 FixtureWithLastReleaseApis("foo"),
2314 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002315 java_sdk_library {
2316 name: "foo",
2317 srcs: ["a.java"],
2318 system: {
2319 enabled: true,
2320 },
2321 default_to_stubs: true,
2322 }
2323
2324 java_library {
2325 name: "baz",
2326 srcs: ["a.java"],
2327 libs: ["foo"],
2328 // does not have sdk_version set, should fallback to module,
2329 // which will then fallback to system because the module scope
2330 // is not enabled.
2331 }
2332 `)
2333 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002334 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002335 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2336 t.Errorf("expected %q, found %#q", expected, actual)
2337 }
2338}
2339
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002340var compilerFlagsTestCases = []struct {
2341 in string
2342 out bool
2343}{
2344 {
2345 in: "a",
2346 out: false,
2347 },
2348 {
2349 in: "-a",
2350 out: true,
2351 },
2352 {
2353 in: "-no-jdk",
2354 out: false,
2355 },
2356 {
2357 in: "-no-stdlib",
2358 out: false,
2359 },
2360 {
2361 in: "-kotlin-home",
2362 out: false,
2363 },
2364 {
2365 in: "-kotlin-home /some/path",
2366 out: false,
2367 },
2368 {
2369 in: "-include-runtime",
2370 out: false,
2371 },
2372 {
2373 in: "-Xintellij-plugin-root",
2374 out: false,
2375 },
2376}
2377
2378type mockContext struct {
2379 android.ModuleContext
2380 result bool
2381}
2382
2383func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2384 // CheckBadCompilerFlags calls this function when the flag should be rejected
2385 ctx.result = false
2386}
2387
2388func TestCompilerFlags(t *testing.T) {
2389 for _, testCase := range compilerFlagsTestCases {
2390 ctx := &mockContext{result: true}
2391 CheckKotlincFlags(ctx, []string{testCase.in})
2392 if ctx.result != testCase.out {
2393 t.Errorf("incorrect output:")
2394 t.Errorf(" input: %#v", testCase.in)
2395 t.Errorf(" expected: %#v", testCase.out)
2396 t.Errorf(" got: %#v", ctx.result)
2397 }
2398 }
2399}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002400
2401// TODO(jungjw): Consider making this more robust by ignoring path order.
2402func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2403 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2404 flags := strings.Split(variables["javacFlags"], " ")
2405 got := ""
2406 for _, flag := range flags {
2407 keyEnd := strings.Index(flag, "=")
2408 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2409 got = flag[keyEnd+1:]
2410 break
2411 }
2412 }
2413 if expected != got {
2414 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2415 }
2416}
2417
2418func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002419 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002420 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002421 bp := `
2422 java_library {
2423 name: "foo",
2424 srcs: ["a.java"],
2425 java_version: "1.8",
2426 }
2427
2428 java_library {
2429 name: "bar",
2430 srcs: ["b.java"],
2431 sdk_version: "none",
2432 system_modules: "none",
2433 patch_module: "java.base",
2434 java_version: "1.8",
2435 }
2436
2437 java_library {
2438 name: "baz",
2439 srcs: ["c.java"],
2440 patch_module: "java.base",
2441 java_version: "1.8",
2442 }
2443 `
2444 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002445
2446 checkPatchModuleFlag(t, ctx, "foo", "")
2447 checkPatchModuleFlag(t, ctx, "bar", "")
2448 checkPatchModuleFlag(t, ctx, "baz", "")
2449 })
2450
Pete Gillin0c2143e2019-05-02 15:32:11 +01002451 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002452 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002453 bp := `
2454 java_library {
2455 name: "foo",
2456 srcs: ["a.java"],
2457 }
2458
2459 java_library {
2460 name: "bar",
2461 srcs: ["b.java"],
2462 sdk_version: "none",
2463 system_modules: "none",
2464 patch_module: "java.base",
2465 }
2466
2467 java_library {
2468 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002469 srcs: [
2470 "c.java",
2471 // Tests for b/150878007
2472 "dir/d.java",
2473 "dir2/e.java",
2474 "dir2/f.java",
2475 "nested/dir/g.java"
2476 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002477 patch_module: "java.base",
2478 }
2479 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002480 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002481
2482 checkPatchModuleFlag(t, ctx, "foo", "")
2483 expected := "java.base=.:" + buildDir
2484 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002485 expected = "java.base=" + strings.Join([]string{
Paul Duffin95bdab42021-03-08 21:48:46 +00002486 ".", buildDir, "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002487 checkPatchModuleFlag(t, ctx, "baz", expected)
2488 })
2489}
Paul Duffina7b9f422020-01-10 17:12:18 +00002490
Paul Duffin83a2d962019-11-19 19:44:10 +00002491func TestJavaLibraryWithSystemModules(t *testing.T) {
2492 ctx, _ := testJava(t, `
2493 java_library {
2494 name: "lib-with-source-system-modules",
2495 srcs: [
2496 "a.java",
2497 ],
2498 sdk_version: "none",
2499 system_modules: "source-system-modules",
2500 }
2501
2502 java_library {
2503 name: "source-jar",
2504 srcs: [
2505 "a.java",
2506 ],
2507 }
2508
2509 java_system_modules {
2510 name: "source-system-modules",
2511 libs: ["source-jar"],
2512 }
2513
2514 java_library {
2515 name: "lib-with-prebuilt-system-modules",
2516 srcs: [
2517 "a.java",
2518 ],
2519 sdk_version: "none",
2520 system_modules: "prebuilt-system-modules",
2521 }
2522
2523 java_import {
2524 name: "prebuilt-jar",
2525 jars: ["a.jar"],
2526 }
2527
2528 java_system_modules_import {
2529 name: "prebuilt-system-modules",
2530 libs: ["prebuilt-jar"],
2531 }
2532 `)
2533
2534 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2535
2536 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2537}
2538
2539func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2540 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2541 bootClasspath := javacRule.Args["bootClasspath"]
2542 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2543 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2544 }
2545}
Jiyong Park19604de2020-03-24 16:44:11 +09002546
2547func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2548 ctx, _ := testJava(t, `
2549 java_library {
2550 name: "foo",
2551 srcs: ["aidl/foo/IFoo.aidl"],
2552 libs: ["bar"],
2553 }
2554
2555 java_import {
2556 name: "bar",
2557 jars: ["a.jar"],
2558 aidl: {
2559 export_include_dirs: ["aidl/bar"],
2560 },
2561 }
2562 `)
2563
2564 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2565 expectedAidlFlag := "-Iaidl/bar"
2566 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2567 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2568 }
2569}
Liz Kammerdd849a82020-06-12 16:38:45 -07002570
Jooyung Hane197d8b2021-01-05 10:33:16 +09002571func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2572 ctx, _ := testJava(t, `
2573 java_library {
2574 name: "foo",
2575 srcs: ["aidl/foo/IFoo.aidl"],
2576 aidl: { flags: ["-Werror"], },
2577 }
2578 `)
2579
2580 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2581 expectedAidlFlag := "-Werror"
2582 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2583 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2584 }
2585}
2586
Liz Kammerdd849a82020-06-12 16:38:45 -07002587func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002588 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002589 java_test_host {
2590 name: "foo",
2591 srcs: ["a.java"],
2592 data_native_bins: ["bin"]
2593 }
2594
2595 python_binary_host {
2596 name: "bin",
2597 srcs: ["bin.py"],
2598 }
2599 `)
2600
2601 buildOS := android.BuildOs.String()
2602
2603 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002604 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002605 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2606 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2607 if !reflect.DeepEqual(expected, actual) {
2608 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2609 }
2610}
Yuexi Ma627263f2021-03-04 13:47:56 -08002611
2612func TestDefaultInstallable(t *testing.T) {
2613 ctx, _ := testJava(t, `
2614 java_test_host {
2615 name: "foo"
2616 }
2617 `)
2618
2619 buildOS := android.BuildOs.String()
2620 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2621 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2622 module.properties.Installable)
2623}