blob: 2cc66169b81f98ffc2cad0d9339826ac51820cd1 [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) {
Paul Duffin3d9f2682021-03-13 09:47:16 +00001432 result := javaFixtureFactory.
1433 Extend(FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
1434 RunTestWithBp(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001435 java_library {
1436 name: "foo",
1437 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001438 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001439 }
1440
1441 java_library {
1442 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001443 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001444 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001445 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001446 }
1447
1448 java_library {
1449 name: "baz",
1450 srcs: ["c.java"],
1451 libs: ["bar"],
1452 sdk_version: "14",
1453 }
1454 `)
1455
Paul Duffin3d9f2682021-03-13 09:47:16 +00001456 fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine")
1457 barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine")
1458 barJavac := result.ModuleForTests("bar", "android_common").Rule("javac")
1459 barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine")
1460 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001461
Paul Duffin3d9f2682021-03-13 09:47:16 +00001462 android.AssertArrayString(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs.Strings())
Nan Zhang61eaedb2017-11-02 13:28:15 -07001463
1464 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
Paul Duffin3d9f2682021-03-13 09:47:16 +00001465 barTurbineJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine", "bar.jar")
1466 android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
1467 android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
1468 android.AssertArrayString(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs.Strings())
1469 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001470}
1471
1472func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001473 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001474 java_library {
1475 name: "bar",
1476 srcs: ["a.java","b.java","c.java"],
1477 javac_shard_size: 1
1478 }
1479 `)
1480
1481 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1482 for i := 0; i < 3; i++ {
1483 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1484 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1485 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1486 }
1487 }
1488}
1489
Nan Zhang581fd212018-01-10 16:06:12 -08001490func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001491 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001492 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001493 name: "droiddoc-templates-sdk",
1494 path: ".",
1495 }
Jiyong Park29074592019-07-07 16:27:47 +09001496 filegroup {
1497 name: "bar-doc-aidl-srcs",
1498 srcs: ["bar-doc/IBar.aidl"],
1499 path: "bar-doc",
1500 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001501 droidstubs {
1502 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001503 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001504 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001505 ],
Steve Kim3666c702020-09-01 17:58:01 +00001506 exclude_srcs: [
1507 "bar-doc/b.java"
1508 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001509 api_levels_annotations_dirs: [
1510 "droiddoc-templates-sdk",
1511 ],
1512 api_levels_annotations_enabled: true,
1513 }
1514 droiddoc {
1515 name: "bar-doc",
1516 srcs: [
1517 ":bar-stubs",
1518 "bar-doc/IFoo.aidl",
1519 ":bar-doc-aidl-srcs",
1520 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001521 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001522 hdf: [
1523 "android.whichdoc offline",
1524 ],
1525 knowntags: [
1526 "bar-doc/known_oj_tags.txt",
1527 ],
1528 proofread_file: "libcore-proofread.txt",
1529 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001530 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001531 }
Colin Cross238c1f32020-06-07 16:58:18 -07001532 `,
1533 map[string][]byte{
1534 "bar-doc/a.java": nil,
1535 "bar-doc/b.java": nil,
1536 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001537 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1538 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1539 if err != nil {
1540 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1541 }
1542 if len(barStubsOutputs) != 1 {
1543 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001544 }
Nan Zhang581fd212018-01-10 16:06:12 -08001545
Liz Kammere1ab2502020-09-10 15:29:25 +00001546 barStubsOutput := barStubsOutputs[0]
1547 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1548 javaDoc := barDoc.Rule("javadoc")
1549 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1550 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001551 }
1552
Liz Kammere1ab2502020-09-10 15:29:25 +00001553 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1554 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1555 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1556 }
1557
1558 aidl := barDoc.Rule("aidl")
1559 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001560 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1561 }
1562
1563 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1564 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001565 }
Nan Zhang581fd212018-01-10 16:06:12 -08001566}
1567
Liz Kammer585cac22020-07-06 09:12:57 -07001568func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1569 testJavaError(t, "flags is set. Cannot set args", `
1570 droiddoc_exported_dir {
1571 name: "droiddoc-templates-sdk",
1572 path: ".",
1573 }
1574 filegroup {
1575 name: "bar-doc-aidl-srcs",
1576 srcs: ["bar-doc/IBar.aidl"],
1577 path: "bar-doc",
1578 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001579 droidstubs {
1580 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001581 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001582 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001583 ],
Steve Kim3666c702020-09-01 17:58:01 +00001584 exclude_srcs: [
1585 "bar-doc/b.java"
1586 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001587 api_levels_annotations_dirs: [
1588 "droiddoc-templates-sdk",
1589 ],
1590 api_levels_annotations_enabled: true,
1591 }
1592 droiddoc {
1593 name: "bar-doc",
1594 srcs: [
1595 ":bar-stubs",
1596 "bar-doc/IFoo.aidl",
1597 ":bar-doc-aidl-srcs",
1598 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001599 custom_template: "droiddoc-templates-sdk",
1600 hdf: [
1601 "android.whichdoc offline",
1602 ],
1603 knowntags: [
1604 "bar-doc/known_oj_tags.txt",
1605 ],
1606 proofread_file: "libcore-proofread.txt",
1607 todo_file: "libcore-docs-todo.html",
1608 flags: ["-offlinemode -title \"libcore\""],
1609 args: "-offlinemode -title \"libcore\"",
1610 }
1611 `)
1612}
1613
Liz Kammer3d894b72020-08-04 09:55:13 -07001614func TestDroidstubs(t *testing.T) {
1615 ctx, _ := testJavaWithFS(t, `
1616 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001617 name: "droiddoc-templates-sdk",
1618 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001619 }
1620
1621 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001622 name: "bar-stubs",
1623 srcs: ["bar-doc/a.java"],
1624 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1625 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001626 }
1627
1628 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001629 name: "bar-stubs-other",
1630 srcs: ["bar-doc/a.java"],
1631 high_mem: true,
1632 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1633 api_levels_annotations_enabled: true,
1634 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001635 }
1636 `,
1637 map[string][]byte{
1638 "bar-doc/a.java": nil,
1639 })
1640 testcases := []struct {
1641 moduleName string
1642 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001643 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001644 }{
1645 {
1646 moduleName: "bar-stubs",
1647 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001648 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001649 },
1650 {
1651 moduleName: "bar-stubs-other",
1652 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001653 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001654 },
1655 }
1656 for _, c := range testcases {
1657 m := ctx.ModuleForTests(c.moduleName, "android_common")
1658 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001659 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001660 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001661 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001662 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1663 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001664
1665 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1666 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1667 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001668 }
1669}
1670
Paul Duffin83a2d962019-11-19 19:44:10 +00001671func TestDroidstubsWithSystemModules(t *testing.T) {
1672 ctx, _ := testJava(t, `
1673 droidstubs {
1674 name: "stubs-source-system-modules",
1675 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001676 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001677 ],
1678 sdk_version: "none",
1679 system_modules: "source-system-modules",
1680 }
1681
1682 java_library {
1683 name: "source-jar",
1684 srcs: [
1685 "a.java",
1686 ],
1687 }
1688
1689 java_system_modules {
1690 name: "source-system-modules",
1691 libs: ["source-jar"],
1692 }
1693
1694 droidstubs {
1695 name: "stubs-prebuilt-system-modules",
1696 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001697 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001698 ],
1699 sdk_version: "none",
1700 system_modules: "prebuilt-system-modules",
1701 }
1702
1703 java_import {
1704 name: "prebuilt-jar",
1705 jars: ["a.jar"],
1706 }
1707
1708 java_system_modules_import {
1709 name: "prebuilt-system-modules",
1710 libs: ["prebuilt-jar"],
1711 }
1712 `)
1713
1714 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1715
1716 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1717}
1718
1719func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1720 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1721 var systemJars []string
1722 for _, i := range metalavaRule.Implicits {
1723 systemJars = append(systemJars, i.Base())
1724 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001725 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001726 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1727 }
1728}
1729
Colin Cross54250902017-12-05 09:28:08 -08001730func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001731 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001732 java_library {
1733 name: "foo",
1734 srcs: ["a.java"],
1735 }
1736
1737 java_genrule {
1738 name: "jargen",
1739 tool_files: ["b.java"],
1740 cmd: "$(location b.java) $(in) $(out)",
1741 out: ["jargen.jar"],
1742 srcs: [":foo"],
1743 }
1744
1745 java_library {
1746 name: "bar",
1747 static_libs: ["jargen"],
1748 srcs: ["c.java"],
1749 }
1750
1751 java_library {
1752 name: "baz",
1753 libs: ["jargen"],
1754 srcs: ["c.java"],
1755 }
1756 `)
1757
1758 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1759 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1760 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1761 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1762 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1763
Colin Cross3d680512020-11-13 16:23:53 -08001764 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1765 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001766 }
1767
1768 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1769 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1770 }
1771
1772 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1773 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1774 }
1775
1776 if len(barCombined.Inputs) != 2 ||
1777 barCombined.Inputs[0].String() != bar.Output.String() ||
1778 barCombined.Inputs[1].String() != jargen.Output.String() {
1779 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1780 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1781 }
1782}
1783
Nan Zhang27e284d2018-02-09 21:03:53 +00001784func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001785 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001786 java_library {
1787 name: "foo",
1788 srcs: ["a.java", ":foo-srcs"],
1789 exclude_srcs: ["a.java", ":foo-excludes"],
1790 }
1791
1792 filegroup {
1793 name: "foo-srcs",
1794 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1795 }
1796
1797 filegroup {
1798 name: "foo-excludes",
1799 srcs: ["java-fg/a.java", "java-fg/b.java"],
1800 }
1801 `)
1802
1803 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1804
1805 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1806 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1807 }
1808}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001809
Paul Duffin52d398a2019-06-11 12:31:14 +01001810func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001811 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001812 "libcore/Android.bp": []byte(`
1813 java_library {
1814 name: "core",
1815 sdk_version: "none",
1816 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001817 }
1818
1819 filegroup {
1820 name: "core-jar",
1821 srcs: [":core{.jar}"],
1822 }
1823`),
1824 })
Colin Crossae8600b2020-10-29 17:09:13 -07001825 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001826 run(t, ctx, config)
1827}
1828
1829func TestJavaImport(t *testing.T) {
1830 config := testConfig(nil, "", map[string][]byte{
1831 "libcore/Android.bp": []byte(`
1832 java_import {
1833 name: "core",
1834 sdk_version: "none",
1835 }
1836
1837 filegroup {
1838 name: "core-jar",
1839 srcs: [":core{.jar}"],
1840 }
1841`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001842 })
Colin Crossae8600b2020-10-29 17:09:13 -07001843 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001844 run(t, ctx, config)
1845}
1846
Jiyong Parkc678ad32018-04-10 13:07:10 +09001847func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001848 result := javaFixtureFactory.Extend(
1849 PrepareForTestWithJavaSdkLibraryFiles,
1850 FixtureWithPrebuiltApis(map[string][]string{
1851 "28": {"foo"},
1852 "29": {"foo"},
1853 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1854 }),
1855 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001856 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001857 name: "droiddoc-templates-sdk",
1858 path: ".",
1859 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001860 java_sdk_library {
1861 name: "foo",
1862 srcs: ["a.java", "b.java"],
1863 api_packages: ["foo"],
1864 }
1865 java_sdk_library {
1866 name: "bar",
1867 srcs: ["a.java", "b.java"],
1868 api_packages: ["bar"],
1869 }
1870 java_library {
1871 name: "baz",
1872 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001873 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001874 sdk_version: "system_current",
1875 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001876 java_sdk_library {
1877 name: "barney",
1878 srcs: ["c.java"],
1879 api_only: true,
1880 }
1881 java_sdk_library {
1882 name: "betty",
1883 srcs: ["c.java"],
1884 shared_library: false,
1885 }
Paul Duffin859fe962020-05-15 10:20:31 +01001886 java_sdk_library_import {
1887 name: "quuz",
1888 public: {
1889 jars: ["c.jar"],
1890 },
1891 }
1892 java_sdk_library_import {
1893 name: "fred",
1894 public: {
1895 jars: ["b.jar"],
1896 },
1897 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001898 java_sdk_library_import {
1899 name: "wilma",
1900 public: {
1901 jars: ["b.jar"],
1902 },
1903 shared_library: false,
1904 }
Jiyong Park1be96912018-05-28 18:02:19 +09001905 java_library {
1906 name: "qux",
1907 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001908 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001909 sdk_version: "system_current",
1910 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001911 java_library {
1912 name: "baz-test",
1913 srcs: ["c.java"],
1914 libs: ["foo"],
1915 sdk_version: "test_current",
1916 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001917 java_library {
1918 name: "baz-29",
1919 srcs: ["c.java"],
1920 libs: ["foo"],
1921 sdk_version: "system_29",
1922 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001923 java_library {
1924 name: "baz-module-30",
1925 srcs: ["c.java"],
1926 libs: ["foo"],
1927 sdk_version: "module_30",
1928 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001929 `)
1930
1931 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001932 result.ModuleForTests("foo", "android_common")
1933 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1934 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1935 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1936 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1937 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1938 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1939 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1940 result.ModuleForTests("foo.api.public.28", "")
1941 result.ModuleForTests("foo.api.system.28", "")
1942 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001943
Paul Duffin22b77cd2021-03-12 19:15:01 +00001944 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001945 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001946 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001947 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001948 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001949 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001950 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001951
Paul Duffin22b77cd2021-03-12 19:15:01 +00001952 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001953 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001954 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001955
Paul Duffin22b77cd2021-03-12 19:15:01 +00001956 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001957 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001958 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001959
Paul Duffin22b77cd2021-03-12 19:15:01 +00001960 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001961 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001962 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 +01001963
Jiyong Park1be96912018-05-28 18:02:19 +09001964 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001965 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001966 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001967 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001968 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001969 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001970}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001971
Anton Hansson7f66efa2020-10-08 14:47:23 +01001972func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001973 result := javaFixtureFactory.Extend(
1974 PrepareForTestWithJavaSdkLibraryFiles,
1975 FixtureWithLastReleaseApis("sdklib"),
1976 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001977 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001978 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001979 srcs: ["a.java"],
1980 impl_only_libs: ["foo"],
1981 stub_only_libs: ["bar"],
1982 }
1983 java_library {
1984 name: "foo",
1985 srcs: ["a.java"],
1986 sdk_version: "current",
1987 }
1988 java_library {
1989 name: "bar",
1990 srcs: ["a.java"],
1991 sdk_version: "current",
1992 }
1993 `)
1994
Anton Hanssondff2c782020-12-21 17:10:01 +00001995 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001996 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001997 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1998 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1999 }
2000 }
Anton Hanssondff2c782020-12-21 17:10:01 +00002001 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00002002 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01002003 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
2004 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
2005 }
2006}
2007
Paul Duffindaaa3322020-05-26 18:13:57 +01002008func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002009 result := javaFixtureFactory.Extend(
2010 PrepareForTestWithJavaSdkLibraryFiles,
2011 FixtureWithLastReleaseApis("foo"),
2012 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01002013 java_sdk_library {
2014 name: "foo",
2015 srcs: ["a.java"],
2016 api_only: true,
2017 public: {
2018 enabled: true,
2019 },
2020 }
2021
2022 java_library {
2023 name: "bar",
2024 srcs: ["b.java"],
2025 libs: ["foo"],
2026 }
2027 `)
2028
2029 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002030 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01002031 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2032 t.Errorf("expected %q, found %#q", expected, actual)
2033 }
2034}
2035
Paul Duffin46dc45a2020-05-14 15:39:10 +01002036func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002037 javaFixtureFactory.Extend(
2038 PrepareForTestWithJavaSdkLibraryFiles,
2039 FixtureWithLastReleaseApis("foo"),
2040 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002041 java_sdk_library {
2042 name: "foo",
2043 srcs: ["a.java"],
2044 api_packages: ["foo"],
2045 public: {
2046 enabled: true,
2047 },
2048 }
2049
2050 java_library {
2051 name: "bar",
2052 srcs: ["b.java", ":foo{.public.stubs.source}"],
2053 }
2054 `)
2055}
2056
2057func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002058 javaFixtureFactory.
Paul Duffin163043d2021-03-12 19:18:49 +00002059 Extend(
2060 PrepareForTestWithJavaSdkLibraryFiles,
2061 FixtureWithLastReleaseApis("foo"),
2062 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00002063 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
2064 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002065 java_sdk_library {
2066 name: "foo",
2067 srcs: ["a.java"],
2068 api_packages: ["foo"],
2069 public: {
2070 enabled: true,
2071 },
2072 }
2073
2074 java_library {
2075 name: "bar",
2076 srcs: ["b.java", ":foo{.system.stubs.source}"],
2077 }
2078 `)
2079}
2080
Paul Duffinca8d9a52020-06-26 22:20:25 +01002081func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002082 result := javaFixtureFactory.Extend(
2083 PrepareForTestWithJavaSdkLibraryFiles,
2084 FixtureWithLastReleaseApis("sdklib"),
2085 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01002086 java_sdk_library {
2087 name: "sdklib",
2088 srcs: ["a.java"],
2089 sdk_version: "none",
2090 system_modules: "none",
2091 public: {
2092 enabled: true,
2093 },
2094 }
2095 `)
2096
Paul Duffin22b77cd2021-03-12 19:15:01 +00002097 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01002098 `dex2oatd`,
2099 `sdklib.impl`,
2100 `sdklib.stubs`,
2101 `sdklib.stubs.source`,
2102 `sdklib.xml`,
2103 })
2104}
2105
Paul Duffin46dc45a2020-05-14 15:39:10 +01002106func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002107 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01002108 java_sdk_library_import {
2109 name: "foo",
2110 public: {
2111 jars: ["a.jar"],
2112 stub_srcs: ["a.java"],
2113 current_api: "api/current.txt",
2114 removed_api: "api/removed.txt",
2115 },
2116 }
2117
2118 java_library {
2119 name: "bar",
2120 srcs: [":foo{.public.stubs.source}"],
2121 java_resources: [
2122 ":foo{.public.api.txt}",
2123 ":foo{.public.removed-api.txt}",
2124 ],
2125 }
2126 `)
2127}
2128
2129func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2130 bp := `
2131 java_sdk_library_import {
2132 name: "foo",
2133 public: {
2134 jars: ["a.jar"],
2135 },
2136 }
2137 `
2138
2139 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002140 javaFixtureFactory.
2141 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
2142 RunTestWithBp(t, bp+`
2143 java_library {
2144 name: "bar",
2145 srcs: [":foo{.public.stubs.source}"],
2146 java_resources: [
2147 ":foo{.public.api.txt}",
2148 ":foo{.public.removed-api.txt}",
2149 ],
2150 }
2151 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002152 })
2153
2154 t.Run("api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002155 javaFixtureFactory.
2156 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
2157 RunTestWithBp(t, bp+`
2158 java_library {
2159 name: "bar",
2160 srcs: ["a.java"],
2161 java_resources: [
2162 ":foo{.public.api.txt}",
2163 ],
2164 }
2165 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002166 })
2167
2168 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002169 javaFixtureFactory.
2170 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
2171 RunTestWithBp(t, bp+`
2172 java_library {
2173 name: "bar",
2174 srcs: ["a.java"],
2175 java_resources: [
2176 ":foo{.public.removed-api.txt}",
2177 ],
2178 }
2179 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002180 })
2181}
2182
Paul Duffin3375e352020-04-28 10:44:03 +01002183func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002184 javaFixtureFactory.
2185 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
2186 RunTestWithBp(t, `
2187 java_sdk_library {
2188 name: "foo",
2189 srcs: ["a.java", "b.java"],
2190 api_packages: ["foo"],
2191 // Explicitly disable public to test the check that ensures the set of enabled
2192 // scopes is consistent.
2193 public: {
2194 enabled: false,
2195 },
2196 system: {
2197 enabled: true,
2198 },
2199 }
Paul Duffin3375e352020-04-28 10:44:03 +01002200 `)
2201}
2202
Paul Duffin87a05a32020-05-12 11:50:28 +01002203func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002204 javaFixtureFactory.Extend(
2205 PrepareForTestWithJavaSdkLibraryFiles,
2206 FixtureWithLastReleaseApis("foo"),
2207 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002208 java_sdk_library {
2209 name: "foo",
2210 srcs: ["a.java", "b.java"],
2211 api_packages: ["foo"],
2212 system: {
2213 enabled: true,
2214 sdk_version: "module_current",
2215 },
2216 }
2217 `)
2218}
2219
Paul Duffin0c5bae52020-06-02 13:00:08 +01002220func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002221 javaFixtureFactory.Extend(
2222 PrepareForTestWithJavaSdkLibraryFiles,
2223 FixtureWithLastReleaseApis("foo"),
2224 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002225 java_sdk_library {
2226 name: "foo",
2227 srcs: ["a.java", "b.java"],
2228 api_packages: ["foo"],
2229 system: {
2230 enabled: true,
2231 },
2232 module_lib: {
2233 enabled: true,
2234 },
2235 }
2236 `)
2237}
2238
2239func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002240 javaFixtureFactory.Extend(
2241 PrepareForTestWithJavaSdkLibraryFiles,
2242 FixtureWithLastReleaseApis("foo"),
2243 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002244 java_sdk_library {
2245 name: "foo",
2246 srcs: ["a.java", "b.java"],
2247 api_packages: ["foo"],
2248 system: {
2249 enabled: true,
2250 },
2251 system_server: {
2252 enabled: true,
2253 },
2254 }
2255 `)
2256}
2257
Paul Duffin803a9562020-05-20 11:52:25 +01002258func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002259 javaFixtureFactory.
2260 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2261 RunTestWithBp(t, `
2262 java_sdk_library {
2263 name: "foo",
2264 srcs: ["a.java"],
2265 public: {
2266 enabled: false,
2267 },
2268 }
Paul Duffin803a9562020-05-20 11:52:25 +01002269
Paul Duffin22b77cd2021-03-12 19:15:01 +00002270 java_library {
2271 name: "baz",
2272 srcs: ["a.java"],
2273 libs: ["foo"],
2274 sdk_version: "module_current",
2275 }
Paul Duffin803a9562020-05-20 11:52:25 +01002276 `)
2277}
2278
2279func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002280 javaFixtureFactory.Extend(
2281 PrepareForTestWithJavaSdkLibraryFiles,
2282 FixtureWithLastReleaseApis("foo"),
2283 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002284 java_sdk_library {
2285 name: "foo",
2286 srcs: ["a.java"],
2287 system: {
2288 enabled: true,
2289 },
2290 }
2291
2292 java_library {
2293 name: "baz",
2294 srcs: ["a.java"],
2295 libs: ["foo"],
2296 // foo does not have module-lib scope so it should fallback to system
2297 sdk_version: "module_current",
2298 }
2299 `)
2300}
2301
Jiyong Park932cdfe2020-05-28 00:19:53 +09002302func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002303 result := javaFixtureFactory.Extend(
2304 PrepareForTestWithJavaSdkLibraryFiles,
2305 FixtureWithLastReleaseApis("foo"),
2306 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002307 java_sdk_library {
2308 name: "foo",
2309 srcs: ["a.java"],
2310 system: {
2311 enabled: true,
2312 },
2313 default_to_stubs: true,
2314 }
2315
2316 java_library {
2317 name: "baz",
2318 srcs: ["a.java"],
2319 libs: ["foo"],
2320 // does not have sdk_version set, should fallback to module,
2321 // which will then fallback to system because the module scope
2322 // is not enabled.
2323 }
2324 `)
2325 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002326 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002327 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2328 t.Errorf("expected %q, found %#q", expected, actual)
2329 }
2330}
2331
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002332var compilerFlagsTestCases = []struct {
2333 in string
2334 out bool
2335}{
2336 {
2337 in: "a",
2338 out: false,
2339 },
2340 {
2341 in: "-a",
2342 out: true,
2343 },
2344 {
2345 in: "-no-jdk",
2346 out: false,
2347 },
2348 {
2349 in: "-no-stdlib",
2350 out: false,
2351 },
2352 {
2353 in: "-kotlin-home",
2354 out: false,
2355 },
2356 {
2357 in: "-kotlin-home /some/path",
2358 out: false,
2359 },
2360 {
2361 in: "-include-runtime",
2362 out: false,
2363 },
2364 {
2365 in: "-Xintellij-plugin-root",
2366 out: false,
2367 },
2368}
2369
2370type mockContext struct {
2371 android.ModuleContext
2372 result bool
2373}
2374
2375func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2376 // CheckBadCompilerFlags calls this function when the flag should be rejected
2377 ctx.result = false
2378}
2379
2380func TestCompilerFlags(t *testing.T) {
2381 for _, testCase := range compilerFlagsTestCases {
2382 ctx := &mockContext{result: true}
2383 CheckKotlincFlags(ctx, []string{testCase.in})
2384 if ctx.result != testCase.out {
2385 t.Errorf("incorrect output:")
2386 t.Errorf(" input: %#v", testCase.in)
2387 t.Errorf(" expected: %#v", testCase.out)
2388 t.Errorf(" got: %#v", ctx.result)
2389 }
2390 }
2391}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002392
2393// TODO(jungjw): Consider making this more robust by ignoring path order.
2394func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2395 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2396 flags := strings.Split(variables["javacFlags"], " ")
2397 got := ""
2398 for _, flag := range flags {
2399 keyEnd := strings.Index(flag, "=")
2400 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2401 got = flag[keyEnd+1:]
2402 break
2403 }
2404 }
2405 if expected != got {
2406 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2407 }
2408}
2409
2410func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002411 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002412 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002413 bp := `
2414 java_library {
2415 name: "foo",
2416 srcs: ["a.java"],
2417 java_version: "1.8",
2418 }
2419
2420 java_library {
2421 name: "bar",
2422 srcs: ["b.java"],
2423 sdk_version: "none",
2424 system_modules: "none",
2425 patch_module: "java.base",
2426 java_version: "1.8",
2427 }
2428
2429 java_library {
2430 name: "baz",
2431 srcs: ["c.java"],
2432 patch_module: "java.base",
2433 java_version: "1.8",
2434 }
2435 `
2436 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002437
2438 checkPatchModuleFlag(t, ctx, "foo", "")
2439 checkPatchModuleFlag(t, ctx, "bar", "")
2440 checkPatchModuleFlag(t, ctx, "baz", "")
2441 })
2442
Pete Gillin0c2143e2019-05-02 15:32:11 +01002443 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002444 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002445 bp := `
2446 java_library {
2447 name: "foo",
2448 srcs: ["a.java"],
2449 }
2450
2451 java_library {
2452 name: "bar",
2453 srcs: ["b.java"],
2454 sdk_version: "none",
2455 system_modules: "none",
2456 patch_module: "java.base",
2457 }
2458
2459 java_library {
2460 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002461 srcs: [
2462 "c.java",
2463 // Tests for b/150878007
2464 "dir/d.java",
2465 "dir2/e.java",
2466 "dir2/f.java",
2467 "nested/dir/g.java"
2468 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002469 patch_module: "java.base",
2470 }
2471 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002472 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002473
2474 checkPatchModuleFlag(t, ctx, "foo", "")
2475 expected := "java.base=.:" + buildDir
2476 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002477 expected = "java.base=" + strings.Join([]string{
Paul Duffin95bdab42021-03-08 21:48:46 +00002478 ".", buildDir, "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002479 checkPatchModuleFlag(t, ctx, "baz", expected)
2480 })
2481}
Paul Duffina7b9f422020-01-10 17:12:18 +00002482
Paul Duffin83a2d962019-11-19 19:44:10 +00002483func TestJavaLibraryWithSystemModules(t *testing.T) {
2484 ctx, _ := testJava(t, `
2485 java_library {
2486 name: "lib-with-source-system-modules",
2487 srcs: [
2488 "a.java",
2489 ],
2490 sdk_version: "none",
2491 system_modules: "source-system-modules",
2492 }
2493
2494 java_library {
2495 name: "source-jar",
2496 srcs: [
2497 "a.java",
2498 ],
2499 }
2500
2501 java_system_modules {
2502 name: "source-system-modules",
2503 libs: ["source-jar"],
2504 }
2505
2506 java_library {
2507 name: "lib-with-prebuilt-system-modules",
2508 srcs: [
2509 "a.java",
2510 ],
2511 sdk_version: "none",
2512 system_modules: "prebuilt-system-modules",
2513 }
2514
2515 java_import {
2516 name: "prebuilt-jar",
2517 jars: ["a.jar"],
2518 }
2519
2520 java_system_modules_import {
2521 name: "prebuilt-system-modules",
2522 libs: ["prebuilt-jar"],
2523 }
2524 `)
2525
2526 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2527
2528 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2529}
2530
2531func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2532 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2533 bootClasspath := javacRule.Args["bootClasspath"]
2534 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2535 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2536 }
2537}
Jiyong Park19604de2020-03-24 16:44:11 +09002538
2539func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2540 ctx, _ := testJava(t, `
2541 java_library {
2542 name: "foo",
2543 srcs: ["aidl/foo/IFoo.aidl"],
2544 libs: ["bar"],
2545 }
2546
2547 java_import {
2548 name: "bar",
2549 jars: ["a.jar"],
2550 aidl: {
2551 export_include_dirs: ["aidl/bar"],
2552 },
2553 }
2554 `)
2555
2556 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2557 expectedAidlFlag := "-Iaidl/bar"
2558 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2559 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2560 }
2561}
Liz Kammerdd849a82020-06-12 16:38:45 -07002562
Jooyung Hane197d8b2021-01-05 10:33:16 +09002563func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2564 ctx, _ := testJava(t, `
2565 java_library {
2566 name: "foo",
2567 srcs: ["aidl/foo/IFoo.aidl"],
2568 aidl: { flags: ["-Werror"], },
2569 }
2570 `)
2571
2572 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2573 expectedAidlFlag := "-Werror"
2574 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2575 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2576 }
2577}
2578
Liz Kammerdd849a82020-06-12 16:38:45 -07002579func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002580 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002581 java_test_host {
2582 name: "foo",
2583 srcs: ["a.java"],
2584 data_native_bins: ["bin"]
2585 }
2586
2587 python_binary_host {
2588 name: "bin",
2589 srcs: ["bin.py"],
2590 }
2591 `)
2592
2593 buildOS := android.BuildOs.String()
2594
2595 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002596 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002597 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2598 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2599 if !reflect.DeepEqual(expected, actual) {
2600 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2601 }
2602}
Yuexi Ma627263f2021-03-04 13:47:56 -08002603
2604func TestDefaultInstallable(t *testing.T) {
2605 ctx, _ := testJava(t, `
2606 java_test_host {
2607 name: "foo"
2608 }
2609 `)
2610
2611 buildOS := android.BuildOs.String()
2612 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2613 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2614 module.properties.Installable)
2615}