blob: 6c0a908569e2d0319116033c59b1af0a8a09daa2 [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 (
Colin Cross72bb3632017-07-13 16:23:21 -070018 "io/ioutil"
19 "os"
20 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070021 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010022 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070023 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070024 "strings"
25 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070026
Jeongik Cha28df2572019-11-11 10:46:36 +090027 "github.com/google/blueprint/proptools"
28
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/android"
30 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080031 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070032 "android/soong/genrule"
Liz Kammerdd849a82020-06-12 16:38:45 -070033 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
36var buildDir string
37
38func setUp() {
39 var err error
40 buildDir, err = ioutil.TempDir("", "soong_java_test")
41 if err != nil {
42 panic(err)
43 }
44}
45
46func tearDown() {
47 os.RemoveAll(buildDir)
48}
49
50func TestMain(m *testing.M) {
51 run := func() int {
52 setUp()
53 defer tearDown()
54
55 return m.Run()
56 }
57
58 os.Exit(run())
59}
Colin Cross527012a2017-11-30 22:56:16 -080060
Colin Cross98be1bb2019-12-13 20:41:13 -080061func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000062 bp += dexpreopt.BpToolModulesForTest()
63
Martin Stjernholm75a48d82020-01-10 20:32:59 +000064 config := TestConfig(buildDir, env, bp, fs)
65
66 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
67 // it doesn't create a real one, which would fail.
68 _ = dexpreopt.GlobalSoongConfigForTests(config)
69
70 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070071}
72
Colin Cross98be1bb2019-12-13 20:41:13 -080073func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070074
Colin Cross4c428df2017-09-15 17:36:05 -070075 ctx := android.NewTestArchContext()
Paul Duffinf9b1da02019-12-18 19:51:55 +000076 RegisterJavaBuildComponents(ctx)
77 RegisterAppBuildComponents(ctx)
78 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000079 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000080 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080081 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080082 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
83 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070084 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000085 RegisterDocsBuildComponents(ctx)
86 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000087 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070088 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010089 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000090
91 RegisterPrebuiltApisBuildComponents(ctx)
92
Liz Kammerdd849a82020-06-12 16:38:45 -070093 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070094 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080095 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070096 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070097
Paul Duffin021f4e52020-07-30 16:04:17 +010098 android.RegisterPrebuiltMutators(ctx)
99
Colin Crossa4f08812018-10-02 22:03:40 -0700100 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000101 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700102
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000103 dexpreopt.RegisterToolModulesForTest(ctx)
104
Jaewoong Jungc779cd42020-10-06 18:56:10 -0700105 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
106 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
107 })
108
Colin Cross527012a2017-11-30 22:56:16 -0800109 return ctx
110}
111
112func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800113 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800114
Colin Cross98be1bb2019-12-13 20:41:13 -0800115 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000116 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800117
Colin Cross98be1bb2019-12-13 20:41:13 -0800118 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100119 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800120 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700121 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800122 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800123}
124
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900125func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900126 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800127 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900128}
129
Colin Cross98be1bb2019-12-13 20:41:13 -0800130func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900131 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800132 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900133
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))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900136
Colin Cross98be1bb2019-12-13 20:41:13 -0800137 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900138 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
139 if len(errs) > 0 {
140 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900141 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900142 }
143 _, errs = ctx.PrepareBuildActions(config)
144 if len(errs) > 0 {
145 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900146 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900147 }
148
149 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900150
151 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900152}
153
Colin Cross238c1f32020-06-07 16:58:18 -0700154func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
155 t.Helper()
156 return testJavaWithConfig(t, testConfig(nil, bp, fs))
157}
158
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700159func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800160 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700161 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900162}
163
Colin Cross98be1bb2019-12-13 20:41:13 -0800164func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900165 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800166 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800167 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700168
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700169 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700170}
171
Colin Cross2acdae82017-09-15 19:44:24 -0700172func moduleToPath(name string) string {
173 switch {
174 case name == `""`:
175 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700176 case strings.HasSuffix(name, ".jar"):
177 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700178 default:
179 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700180 }
181}
182
Jeongik Chae403e9e2019-12-07 00:16:24 +0900183func TestJavaLinkType(t *testing.T) {
184 testJava(t, `
185 java_library {
186 name: "foo",
187 srcs: ["a.java"],
188 libs: ["bar"],
189 static_libs: ["baz"],
190 }
191
192 java_library {
193 name: "bar",
194 sdk_version: "current",
195 srcs: ["b.java"],
196 }
197
198 java_library {
199 name: "baz",
200 sdk_version: "system_current",
201 srcs: ["c.java"],
202 }
203 `)
204
205 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
206 java_library {
207 name: "foo",
208 srcs: ["a.java"],
209 libs: ["bar"],
210 sdk_version: "current",
211 static_libs: ["baz"],
212 }
213
214 java_library {
215 name: "bar",
216 sdk_version: "current",
217 srcs: ["b.java"],
218 }
219
220 java_library {
221 name: "baz",
222 sdk_version: "system_current",
223 srcs: ["c.java"],
224 }
225 `)
226
227 testJava(t, `
228 java_library {
229 name: "foo",
230 srcs: ["a.java"],
231 libs: ["bar"],
232 sdk_version: "system_current",
233 static_libs: ["baz"],
234 }
235
236 java_library {
237 name: "bar",
238 sdk_version: "current",
239 srcs: ["b.java"],
240 }
241
242 java_library {
243 name: "baz",
244 sdk_version: "system_current",
245 srcs: ["c.java"],
246 }
247 `)
248
249 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
250 java_library {
251 name: "foo",
252 srcs: ["a.java"],
253 libs: ["bar"],
254 sdk_version: "system_current",
255 static_libs: ["baz"],
256 }
257
258 java_library {
259 name: "bar",
260 sdk_version: "current",
261 srcs: ["b.java"],
262 }
263
264 java_library {
265 name: "baz",
266 srcs: ["c.java"],
267 }
268 `)
269}
270
Colin Cross72bb3632017-07-13 16:23:21 -0700271func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700272 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700273 java_library {
274 name: "foo",
275 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700276 libs: ["bar"],
277 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700278 }
279
280 java_library {
281 name: "bar",
282 srcs: ["b.java"],
283 }
284
285 java_library {
286 name: "baz",
287 srcs: ["c.java"],
288 }
Colin Crossd5934c82017-10-02 13:55:26 -0700289 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700290
Colin Cross4c428df2017-09-15 17:36:05 -0700291 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700292 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700293
294 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
295 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
296 }
297
Colin Cross1ee23172017-10-18 14:44:18 -0700298 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700299 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
300 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700301
Nan Zhanged19fc32017-10-19 13:06:22 -0700302 if !strings.Contains(javac.Args["classpath"], barTurbine) {
303 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700304 }
305
Nan Zhanged19fc32017-10-19 13:06:22 -0700306 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
307 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700308 }
309
310 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
311 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700312 }
313}
314
Artur Satayev9cf46692019-11-26 18:08:34 +0000315func TestExportedPlugins(t *testing.T) {
316 type Result struct {
317 library string
318 processors string
319 }
320 var tests = []struct {
321 name string
322 extra string
323 results []Result
324 }{
325 {
326 name: "Exported plugin is not a direct plugin",
327 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
328 results: []Result{{library: "exports", processors: "-proc:none"}},
329 },
330 {
331 name: "Exports plugin to dependee",
332 extra: `
333 java_library{name: "exports", exported_plugins: ["plugin"]}
334 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
335 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
336 `,
337 results: []Result{
338 {library: "foo", processors: "-processor com.android.TestPlugin"},
339 {library: "bar", processors: "-processor com.android.TestPlugin"},
340 },
341 },
342 {
343 name: "Exports plugin to android_library",
344 extra: `
345 java_library{name: "exports", exported_plugins: ["plugin"]}
346 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
347 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
348 `,
349 results: []Result{
350 {library: "foo", processors: "-processor com.android.TestPlugin"},
351 {library: "bar", processors: "-processor com.android.TestPlugin"},
352 },
353 },
354 {
355 name: "Exports plugin is not propagated via transitive deps",
356 extra: `
357 java_library{name: "exports", exported_plugins: ["plugin"]}
358 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
359 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
360 `,
361 results: []Result{
362 {library: "foo", processors: "-processor com.android.TestPlugin"},
363 {library: "bar", processors: "-proc:none"},
364 },
365 },
366 {
367 name: "Exports plugin appends to plugins",
368 extra: `
369 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
370 java_library{name: "exports", exported_plugins: ["plugin"]}
371 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
372 `,
373 results: []Result{
374 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
375 },
376 },
377 }
378
379 for _, test := range tests {
380 t.Run(test.name, func(t *testing.T) {
381 ctx, _ := testJava(t, `
382 java_plugin {
383 name: "plugin",
384 processor_class: "com.android.TestPlugin",
385 }
386 `+test.extra)
387
388 for _, want := range test.results {
389 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
390 if javac.Args["processor"] != want.processors {
391 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
392 }
393 }
394 })
395 }
396}
397
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900398func TestSdkVersionByPartition(t *testing.T) {
399 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900400 java_library {
401 name: "foo",
402 srcs: ["a.java"],
403 vendor: true,
404 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900405 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900406
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900407 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900408 java_library {
409 name: "bar",
410 srcs: ["b.java"],
411 }
412 `)
413
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900414 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900415 bp := `
416 java_library {
417 name: "foo",
418 srcs: ["a.java"],
419 product_specific: true,
420 }
421 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800422
423 config := testConfig(nil, bp, nil)
424 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900425 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800426 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 +0900427 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800428 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900429 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900430 }
431}
432
Colin Crossd5934c82017-10-02 13:55:26 -0700433func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700434 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700435 java_library {
436 name: "foo",
437 srcs: ["a.java"],
438 target: {
439 android: {
440 srcs: ["b.java"],
441 },
442 },
443 }
444 `)
445
446 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
447 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
448 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
449 }
450}
451
Colin Cross6b4a32d2017-12-05 13:42:45 -0800452func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700453 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800454 java_library_host {
455 name: "foo",
456 srcs: ["a.java"],
457 }
458
459 java_binary_host {
460 name: "bar",
461 srcs: ["b.java"],
462 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700463 jni_libs: ["libjni"],
464 }
465
466 cc_library_shared {
467 name: "libjni",
468 host_supported: true,
469 device_supported: false,
470 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800471 }
472 `)
473
474 buildOS := android.BuildOs.String()
475
476 bar := ctx.ModuleForTests("bar", buildOS+"_common")
477 barJar := bar.Output("bar.jar").Output.String()
478 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
479 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
480
Colin Cross89226d92020-10-09 19:00:54 -0700481 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
482 libjniSO := libjni.Rule("Cp").Output.String()
483
Colin Cross6b4a32d2017-12-05 13:42:45 -0800484 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700485 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
486 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800487 }
Colin Cross89226d92020-10-09 19:00:54 -0700488
489 // Test that the install binary wrapper depends on the installed JNI libraries
490 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
491 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800492 }
Alex Humesky2070e322020-06-09 20:23:08 -0400493}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800494
Alex Humesky2070e322020-06-09 20:23:08 -0400495func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
496 bp := `
497 java_library {
498 name: "target_library",
499 srcs: ["a.java"],
500 }
501
502 java_binary_host {
503 name: "host_binary",
504 srcs: ["b.java"],
505 }
506 `
507 config := testConfig(nil, bp, nil)
508 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
509
510 ctx, _ := testJavaWithConfig(t, config)
511
Liz Kammer7941b302020-07-28 13:27:34 -0700512 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400513 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
514 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
515 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
516 t.Errorf("target library javac flags %v should contain "+
517 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
518 }
519
520 // check that -g is not overridden for host modules
521 buildOS := android.BuildOs.String()
522 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
523 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
524 if strings.Contains(hostJavaFlags, "-g:source,lines") {
525 t.Errorf("java_binary_host javac flags %v should not have "+
526 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
527 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800528}
529
Colin Cross72bb3632017-07-13 16:23:21 -0700530func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700531 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700532 java_library {
533 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000534 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000535 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700536 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700537 }
538
Colin Cross74d73e22017-08-02 11:05:49 -0700539 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700540 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700541 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700542 }
543
Colin Cross74d73e22017-08-02 11:05:49 -0700544 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700545 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700546 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700547 sdk_version: "current",
548 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700549 }
Colin Cross42be7612019-02-21 18:12:14 -0800550
551 dex_import {
552 name: "qux",
553 jars: ["b.jar"],
554 }
Colin Cross79c7c262019-04-17 11:11:46 -0700555
556 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000557 name: "sdklib",
558 public: {
559 jars: ["c.jar"],
560 },
561 }
562
Paul Duffin91547182019-11-12 19:39:36 +0000563 prebuilt_stubs_sources {
564 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000565 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000566 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000567
568 java_test_import {
569 name: "test",
570 jars: ["a.jar"],
571 test_suites: ["cts"],
572 test_config: "AndroidTest.xml",
573 }
Colin Cross72bb3632017-07-13 16:23:21 -0700574 `)
575
Paul Duffin9b478b02019-12-10 13:41:51 +0000576 fooModule := ctx.ModuleForTests("foo", "android_common")
577 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700578 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700579 barModule := ctx.ModuleForTests("bar", "android_common")
580 barJar := barModule.Rule("combineJar").Output
581 bazModule := ctx.ModuleForTests("baz", "android_common")
582 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700583 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700584
Paul Duffin9b478b02019-12-10 13:41:51 +0000585 fooLibrary := fooModule.Module().(*Library)
586 assertDeepEquals(t, "foo java sources incorrect",
587 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000588
Paul Duffin9b478b02019-12-10 13:41:51 +0000589 assertDeepEquals(t, "foo java source jars incorrect",
590 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
591 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000592
Colin Cross37f6d792018-07-12 12:28:41 -0700593 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
594 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700595 }
596
Liz Kammerd6c31d22020-08-05 15:40:41 -0700597 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
598 if barDexJar != nil {
599 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
600 }
601
Colin Cross79c7c262019-04-17 11:11:46 -0700602 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
603 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
604 }
605
Colin Cross37f6d792018-07-12 12:28:41 -0700606 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
607 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700608 }
Colin Cross42be7612019-02-21 18:12:14 -0800609
Liz Kammerd6c31d22020-08-05 15:40:41 -0700610 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
611 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
612 if bazDexJar != expectedDexJar {
613 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
614 }
615
Colin Cross42be7612019-02-21 18:12:14 -0800616 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700617}
618
Paul Duffin9b478b02019-12-10 13:41:51 +0000619func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
620 if !reflect.DeepEqual(expected, actual) {
621 t.Errorf("%s: expected %q, found %q", message, expected, actual)
622 }
623}
624
Paul Duffin56d44902020-01-31 13:36:25 +0000625func TestJavaSdkLibraryImport(t *testing.T) {
626 ctx, _ := testJava(t, `
627 java_library {
628 name: "foo",
629 srcs: ["a.java"],
630 libs: ["sdklib"],
631 sdk_version: "current",
632 }
633
634 java_library {
635 name: "foo.system",
636 srcs: ["a.java"],
637 libs: ["sdklib"],
638 sdk_version: "system_current",
639 }
640
641 java_library {
642 name: "foo.test",
643 srcs: ["a.java"],
644 libs: ["sdklib"],
645 sdk_version: "test_current",
646 }
647
648 java_sdk_library_import {
649 name: "sdklib",
650 public: {
651 jars: ["a.jar"],
652 },
653 system: {
654 jars: ["b.jar"],
655 },
656 test: {
657 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100658 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000659 },
660 }
661 `)
662
663 for _, scope := range []string{"", ".system", ".test"} {
664 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
665 javac := fooModule.Rule("javac")
666
667 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
668 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
669 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
670 }
671 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100672
Paul Duffincee7e662020-07-09 17:32:57 +0100673 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100674 `prebuilt_sdklib.stubs`,
675 `prebuilt_sdklib.stubs.source.test`,
676 `prebuilt_sdklib.stubs.system`,
677 `prebuilt_sdklib.stubs.test`,
678 })
679}
680
681func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
682 ctx, _ := testJava(t, `
683 java_sdk_library {
684 name: "sdklib",
685 srcs: ["a.java"],
686 sdk_version: "none",
687 system_modules: "none",
688 public: {
689 enabled: true,
690 },
691 }
692
693 java_sdk_library_import {
694 name: "sdklib",
695 public: {
696 jars: ["a.jar"],
697 },
698 }
699 `)
700
Paul Duffincee7e662020-07-09 17:32:57 +0100701 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100702 `dex2oatd`,
703 `prebuilt_sdklib`,
704 `sdklib.impl`,
705 `sdklib.stubs`,
706 `sdklib.stubs.source`,
707 `sdklib.xml`,
708 })
709
Paul Duffincee7e662020-07-09 17:32:57 +0100710 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100711 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100712 `sdklib.impl`,
713 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
714 // dependency is added after prebuilts may have been renamed and so has to use
715 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100716 `sdklib.xml`,
717 })
718}
719
720func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
721 ctx, _ := testJava(t, `
722 java_sdk_library {
723 name: "sdklib",
724 srcs: ["a.java"],
725 sdk_version: "none",
726 system_modules: "none",
727 public: {
728 enabled: true,
729 },
730 }
731
732 java_sdk_library_import {
733 name: "sdklib",
734 prefer: true,
735 public: {
736 jars: ["a.jar"],
737 },
738 }
739 `)
740
Paul Duffincee7e662020-07-09 17:32:57 +0100741 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100742 `dex2oatd`,
743 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100744 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100745 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100746 `sdklib.stubs.source`,
747 `sdklib.xml`,
748 })
749
Paul Duffincee7e662020-07-09 17:32:57 +0100750 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100751 `prebuilt_sdklib.stubs`,
752 `sdklib.impl`,
753 `sdklib.xml`,
754 })
Paul Duffin56d44902020-01-31 13:36:25 +0000755}
756
Colin Cross89536d42017-07-07 14:35:50 -0700757func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700758 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700759 java_defaults {
760 name: "defaults",
761 srcs: ["a.java"],
762 libs: ["bar"],
763 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700764 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700765 }
766
767 java_library {
768 name: "foo",
769 defaults: ["defaults"],
770 }
771
772 java_library {
773 name: "bar",
774 srcs: ["b.java"],
775 }
776
777 java_library {
778 name: "baz",
779 srcs: ["c.java"],
780 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700781
782 android_test {
783 name: "atestOptimize",
784 defaults: ["defaults"],
785 optimize: {enabled: true},
786 }
787
788 android_test {
789 name: "atestNoOptimize",
790 defaults: ["defaults"],
791 }
792
793 android_test {
794 name: "atestDefault",
795 srcs: ["a.java"],
796 }
Colin Cross89536d42017-07-07 14:35:50 -0700797 `)
798
Colin Cross4c428df2017-09-15 17:36:05 -0700799 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700800 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700801
802 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
803 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
804 }
805
Nan Zhanged19fc32017-10-19 13:06:22 -0700806 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
807 if !strings.Contains(javac.Args["classpath"], barTurbine) {
808 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700809 }
810
Colin Cross1ee23172017-10-18 14:44:18 -0700811 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700812 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
813 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700814 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700815
816 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
817 if atestOptimize.Output == nil {
818 t.Errorf("atestOptimize should optimize APK")
819 }
820
821 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
822 if atestNoOptimize.Output == nil {
823 t.Errorf("atestNoOptimize should not optimize APK")
824 }
825
826 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
827 if atestDefault.Output == nil {
828 t.Errorf("atestDefault should optimize APK")
829 }
Colin Cross89536d42017-07-07 14:35:50 -0700830}
831
Colin Cross0f37af02017-09-27 17:42:05 -0700832func TestResources(t *testing.T) {
833 var table = []struct {
834 name string
835 prop string
836 extra string
837 args string
838 }{
839 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700840 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700841 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800842 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700843 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700844 },
845 {
846 // Test that a module with java_resources includes the files
847 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700848 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
849 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700850 },
851 {
852 // Test that a module with a filegroup in java_resources includes the files with the
853 // path prefix
854 name: "resource filegroup",
855 prop: `java_resources: [":foo-res"]`,
856 extra: `
857 filegroup {
858 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800859 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700860 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700861 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700862 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700863 },
864 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700865 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
866 name: "wildcard dirs",
867 prop: `java_resource_dirs: ["java-res/*"]`,
868 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
869 },
870 {
871 // Test that a module exclude_java_resource_dirs excludes the files
872 name: "wildcard dirs",
873 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
874 args: "-C java-res/a -f java-res/a/a",
875 },
Colin Crosscedd4762018-09-13 11:26:19 -0700876 {
877 // Test wildcards in java_resources
878 name: "wildcard files",
879 prop: `java_resources: ["java-res/**/*"]`,
880 args: "-C . -f java-res/a/a -f java-res/b/b",
881 },
882 {
883 // Test exclude_java_resources with java_resources
884 name: "wildcard files with exclude",
885 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
886 args: "-C . -f java-res/a/a",
887 },
888 {
889 // Test exclude_java_resources with java_resource_dirs
890 name: "resource dirs with exclude files",
891 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
892 args: "-C java-res -f java-res/a/a",
893 },
894 {
895 // Test exclude_java_resource_dirs with java_resource_dirs
896 name: "resource dirs with exclude files",
897 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
898 args: "-C java-res -f java-res/a/a -f java-res/b/b",
899 },
Colin Cross0f37af02017-09-27 17:42:05 -0700900 }
901
902 for _, test := range table {
903 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700904 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700905 java_library {
906 name: "foo",
907 srcs: [
908 "a.java",
909 "b.java",
910 "c.java",
911 ],
912 `+test.prop+`,
913 }
Colin Cross238c1f32020-06-07 16:58:18 -0700914 `+test.extra,
915 map[string][]byte{
916 "java-res/a/a": nil,
917 "java-res/b/b": nil,
918 "java-res2/a": nil,
919 },
920 )
Colin Cross0f37af02017-09-27 17:42:05 -0700921
Colin Cross331a1212018-08-15 20:40:52 -0700922 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700923 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700924
925 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
926 t.Errorf("foo combined jars %v does not contain %q",
927 foo.Inputs.Strings(), fooRes.Output.String())
928 }
929
Colin Crossaf9c55b2017-10-03 14:50:08 -0700930 if fooRes.Args["jarArgs"] != test.args {
931 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700932 fooRes.Args["jarArgs"], test.args)
933 }
934 })
935 }
936}
937
Colin Cross0c4ce212019-05-03 15:28:19 -0700938func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700939 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700940 java_library {
941 name: "foo",
942 srcs: [
943 "a.java",
944 "b.java",
945 "c.java",
946 ],
947 include_srcs: true,
948 }
949
950 java_library {
951 name: "bar",
952 srcs: [
953 "a.java",
954 "b.java",
955 "c.java",
956 ],
957 java_resource_dirs: ["java-res"],
958 include_srcs: true,
959 }
Colin Cross238c1f32020-06-07 16:58:18 -0700960 `, map[string][]byte{
961 "java-res/a/a": nil,
962 "java-res/b/b": nil,
963 "java-res2/a": nil,
964 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700965
966 // Test a library with include_srcs: true
967 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
968 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
969
970 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
971 t.Errorf("foo combined jars %v does not contain %q", w, g)
972 }
973
974 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
975 t.Errorf("foo source jar args %q is not %q", w, g)
976 }
977
978 // Test a library with include_srcs: true and resources
979 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
980 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
981 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
982 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
983
984 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
985 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
986 }
987
988 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
989 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
990 }
991
992 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
993 t.Errorf("bar combined jars %v does not contain %q", w, g)
994 }
995
996 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
997 t.Errorf("bar source jar args %q is not %q", w, g)
998 }
999
1000 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1001 t.Errorf("bar resource jar args %q is not %q", w, g)
1002 }
1003}
1004
Colin Cross54190b32017-10-09 15:34:10 -07001005func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001006 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001007 java_library {
1008 name: "foo",
1009 srcs: [
1010 "a*.java",
1011 ":gen",
1012 "b*.java",
1013 ],
1014 }
1015
1016 genrule {
1017 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001018 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001019 out: ["gen.java"],
1020 }
Colin Cross238c1f32020-06-07 16:58:18 -07001021 `, map[string][]byte{
1022 "a.java": nil,
1023 "b.java": nil,
1024 })
Colin Cross54190b32017-10-09 15:34:10 -07001025
1026 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1027 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1028
Colin Cross15e86d92017-10-20 15:07:08 -07001029 if filepath.Base(genrule.Output.String()) != "gen.java" {
1030 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001031 }
1032
1033 if len(javac.Inputs) != 3 ||
1034 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001035 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001036 javac.Inputs[2].String() != "b.java" {
1037 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1038 }
1039}
1040
Nan Zhang61eaedb2017-11-02 13:28:15 -07001041func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001042 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001043 java_library {
1044 name: "foo",
1045 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001046 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001047 }
1048
1049 java_library {
1050 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001051 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001052 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001053 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001054 }
1055
1056 java_library {
1057 name: "baz",
1058 srcs: ["c.java"],
1059 libs: ["bar"],
1060 sdk_version: "14",
1061 }
1062 `)
1063
1064 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1065 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1066 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1067 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1068 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1069
1070 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1071 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1072 }
1073
1074 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1075 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1076 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1077 }
1078 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1079 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1080 }
1081 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1082 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1083 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001084 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001085 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001086 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001087 }
1088}
1089
1090func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001091 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001092 java_library {
1093 name: "bar",
1094 srcs: ["a.java","b.java","c.java"],
1095 javac_shard_size: 1
1096 }
1097 `)
1098
1099 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1100 for i := 0; i < 3; i++ {
1101 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1102 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1103 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1104 }
1105 }
1106}
1107
Nan Zhang581fd212018-01-10 16:06:12 -08001108func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001109 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001110 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001111 name: "droiddoc-templates-sdk",
1112 path: ".",
1113 }
Jiyong Park29074592019-07-07 16:27:47 +09001114 filegroup {
1115 name: "bar-doc-aidl-srcs",
1116 srcs: ["bar-doc/IBar.aidl"],
1117 path: "bar-doc",
1118 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001119 droidstubs {
1120 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001121 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001122 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001123 ],
Steve Kim3666c702020-09-01 17:58:01 +00001124 exclude_srcs: [
1125 "bar-doc/b.java"
1126 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001127 api_levels_annotations_dirs: [
1128 "droiddoc-templates-sdk",
1129 ],
1130 api_levels_annotations_enabled: true,
1131 }
1132 droiddoc {
1133 name: "bar-doc",
1134 srcs: [
1135 ":bar-stubs",
1136 "bar-doc/IFoo.aidl",
1137 ":bar-doc-aidl-srcs",
1138 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001139 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001140 hdf: [
1141 "android.whichdoc offline",
1142 ],
1143 knowntags: [
1144 "bar-doc/known_oj_tags.txt",
1145 ],
1146 proofread_file: "libcore-proofread.txt",
1147 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001148 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001149 }
Colin Cross238c1f32020-06-07 16:58:18 -07001150 `,
1151 map[string][]byte{
1152 "bar-doc/a.java": nil,
1153 "bar-doc/b.java": nil,
1154 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001155 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1156 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1157 if err != nil {
1158 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1159 }
1160 if len(barStubsOutputs) != 1 {
1161 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001162 }
Nan Zhang581fd212018-01-10 16:06:12 -08001163
Liz Kammere1ab2502020-09-10 15:29:25 +00001164 barStubsOutput := barStubsOutputs[0]
1165 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1166 javaDoc := barDoc.Rule("javadoc")
1167 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1168 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001169 }
1170
Liz Kammere1ab2502020-09-10 15:29:25 +00001171 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1172 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1173 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1174 }
1175
1176 aidl := barDoc.Rule("aidl")
1177 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001178 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1179 }
1180
1181 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1182 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001183 }
Nan Zhang581fd212018-01-10 16:06:12 -08001184}
1185
Liz Kammer585cac22020-07-06 09:12:57 -07001186func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1187 testJavaError(t, "flags is set. Cannot set args", `
1188 droiddoc_exported_dir {
1189 name: "droiddoc-templates-sdk",
1190 path: ".",
1191 }
1192 filegroup {
1193 name: "bar-doc-aidl-srcs",
1194 srcs: ["bar-doc/IBar.aidl"],
1195 path: "bar-doc",
1196 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001197 droidstubs {
1198 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001199 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001200 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001201 ],
Steve Kim3666c702020-09-01 17:58:01 +00001202 exclude_srcs: [
1203 "bar-doc/b.java"
1204 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001205 api_levels_annotations_dirs: [
1206 "droiddoc-templates-sdk",
1207 ],
1208 api_levels_annotations_enabled: true,
1209 }
1210 droiddoc {
1211 name: "bar-doc",
1212 srcs: [
1213 ":bar-stubs",
1214 "bar-doc/IFoo.aidl",
1215 ":bar-doc-aidl-srcs",
1216 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001217 custom_template: "droiddoc-templates-sdk",
1218 hdf: [
1219 "android.whichdoc offline",
1220 ],
1221 knowntags: [
1222 "bar-doc/known_oj_tags.txt",
1223 ],
1224 proofread_file: "libcore-proofread.txt",
1225 todo_file: "libcore-docs-todo.html",
1226 flags: ["-offlinemode -title \"libcore\""],
1227 args: "-offlinemode -title \"libcore\"",
1228 }
1229 `)
1230}
1231
Liz Kammer3d894b72020-08-04 09:55:13 -07001232func TestDroidstubs(t *testing.T) {
1233 ctx, _ := testJavaWithFS(t, `
1234 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001235 name: "droiddoc-templates-sdk",
1236 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001237 }
1238
1239 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001240 name: "bar-stubs",
1241 srcs: ["bar-doc/a.java"],
1242 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1243 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001244 }
1245
1246 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001247 name: "bar-stubs-other",
1248 srcs: ["bar-doc/a.java"],
1249 high_mem: true,
1250 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1251 api_levels_annotations_enabled: true,
1252 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001253 }
1254 `,
1255 map[string][]byte{
1256 "bar-doc/a.java": nil,
1257 })
1258 testcases := []struct {
1259 moduleName string
1260 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001261 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001262 }{
1263 {
1264 moduleName: "bar-stubs",
1265 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001266 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001267 },
1268 {
1269 moduleName: "bar-stubs-other",
1270 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001271 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001272 },
1273 }
1274 for _, c := range testcases {
1275 m := ctx.ModuleForTests(c.moduleName, "android_common")
1276 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001277 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001278 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001279 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001280 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1281 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001282
1283 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1284 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1285 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001286 }
1287}
1288
Paul Duffin83a2d962019-11-19 19:44:10 +00001289func TestDroidstubsWithSystemModules(t *testing.T) {
1290 ctx, _ := testJava(t, `
1291 droidstubs {
1292 name: "stubs-source-system-modules",
1293 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001294 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001295 ],
1296 sdk_version: "none",
1297 system_modules: "source-system-modules",
1298 }
1299
1300 java_library {
1301 name: "source-jar",
1302 srcs: [
1303 "a.java",
1304 ],
1305 }
1306
1307 java_system_modules {
1308 name: "source-system-modules",
1309 libs: ["source-jar"],
1310 }
1311
1312 droidstubs {
1313 name: "stubs-prebuilt-system-modules",
1314 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001315 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001316 ],
1317 sdk_version: "none",
1318 system_modules: "prebuilt-system-modules",
1319 }
1320
1321 java_import {
1322 name: "prebuilt-jar",
1323 jars: ["a.jar"],
1324 }
1325
1326 java_system_modules_import {
1327 name: "prebuilt-system-modules",
1328 libs: ["prebuilt-jar"],
1329 }
1330 `)
1331
1332 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1333
1334 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1335}
1336
1337func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1338 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1339 var systemJars []string
1340 for _, i := range metalavaRule.Implicits {
1341 systemJars = append(systemJars, i.Base())
1342 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001343 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001344 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1345 }
1346}
1347
Colin Cross54250902017-12-05 09:28:08 -08001348func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001349 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001350 java_library {
1351 name: "foo",
1352 srcs: ["a.java"],
1353 }
1354
1355 java_genrule {
1356 name: "jargen",
1357 tool_files: ["b.java"],
1358 cmd: "$(location b.java) $(in) $(out)",
1359 out: ["jargen.jar"],
1360 srcs: [":foo"],
1361 }
1362
1363 java_library {
1364 name: "bar",
1365 static_libs: ["jargen"],
1366 srcs: ["c.java"],
1367 }
1368
1369 java_library {
1370 name: "baz",
1371 libs: ["jargen"],
1372 srcs: ["c.java"],
1373 }
1374 `)
1375
1376 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1377 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1378 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1379 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1380 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1381
1382 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1383 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1384 }
1385
1386 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1387 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1388 }
1389
1390 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1391 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1392 }
1393
1394 if len(barCombined.Inputs) != 2 ||
1395 barCombined.Inputs[0].String() != bar.Output.String() ||
1396 barCombined.Inputs[1].String() != jargen.Output.String() {
1397 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1398 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1399 }
1400}
1401
Nan Zhang27e284d2018-02-09 21:03:53 +00001402func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001403 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001404 java_library {
1405 name: "foo",
1406 srcs: ["a.java", ":foo-srcs"],
1407 exclude_srcs: ["a.java", ":foo-excludes"],
1408 }
1409
1410 filegroup {
1411 name: "foo-srcs",
1412 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1413 }
1414
1415 filegroup {
1416 name: "foo-excludes",
1417 srcs: ["java-fg/a.java", "java-fg/b.java"],
1418 }
1419 `)
1420
1421 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1422
1423 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1424 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1425 }
1426}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001427
Paul Duffin52d398a2019-06-11 12:31:14 +01001428func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001429 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001430 "libcore/Android.bp": []byte(`
1431 java_library {
1432 name: "core",
1433 sdk_version: "none",
1434 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001435 }
1436
1437 filegroup {
1438 name: "core-jar",
1439 srcs: [":core{.jar}"],
1440 }
1441`),
1442 })
1443 ctx := testContext()
1444 run(t, ctx, config)
1445}
1446
1447func TestJavaImport(t *testing.T) {
1448 config := testConfig(nil, "", map[string][]byte{
1449 "libcore/Android.bp": []byte(`
1450 java_import {
1451 name: "core",
1452 sdk_version: "none",
1453 }
1454
1455 filegroup {
1456 name: "core-jar",
1457 srcs: [":core{.jar}"],
1458 }
1459`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001460 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001461 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001462 run(t, ctx, config)
1463}
1464
Jiyong Parkc678ad32018-04-10 13:07:10 +09001465func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001466 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001467 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001468 name: "droiddoc-templates-sdk",
1469 path: ".",
1470 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001471 java_sdk_library {
1472 name: "foo",
1473 srcs: ["a.java", "b.java"],
1474 api_packages: ["foo"],
1475 }
1476 java_sdk_library {
1477 name: "bar",
1478 srcs: ["a.java", "b.java"],
1479 api_packages: ["bar"],
1480 }
1481 java_library {
1482 name: "baz",
1483 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001484 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001485 sdk_version: "system_current",
1486 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001487 java_sdk_library {
1488 name: "barney",
1489 srcs: ["c.java"],
1490 api_only: true,
1491 }
1492 java_sdk_library {
1493 name: "betty",
1494 srcs: ["c.java"],
1495 shared_library: false,
1496 }
Paul Duffin859fe962020-05-15 10:20:31 +01001497 java_sdk_library_import {
1498 name: "quuz",
1499 public: {
1500 jars: ["c.jar"],
1501 },
1502 }
1503 java_sdk_library_import {
1504 name: "fred",
1505 public: {
1506 jars: ["b.jar"],
1507 },
1508 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001509 java_sdk_library_import {
1510 name: "wilma",
1511 public: {
1512 jars: ["b.jar"],
1513 },
1514 shared_library: false,
1515 }
Jiyong Park1be96912018-05-28 18:02:19 +09001516 java_library {
1517 name: "qux",
1518 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001519 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001520 sdk_version: "system_current",
1521 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001522 java_library {
1523 name: "baz-test",
1524 srcs: ["c.java"],
1525 libs: ["foo"],
1526 sdk_version: "test_current",
1527 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001528 java_library {
1529 name: "baz-29",
1530 srcs: ["c.java"],
1531 libs: ["foo"],
1532 sdk_version: "system_29",
1533 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001534 java_library {
1535 name: "baz-module-30",
1536 srcs: ["c.java"],
1537 libs: ["foo"],
1538 sdk_version: "module_30",
1539 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001540 `)
1541
1542 // check the existence of the internal modules
1543 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001544 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1545 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1546 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1547 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1548 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1549 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001550 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001551 ctx.ModuleForTests("foo.api.public.28", "")
1552 ctx.ModuleForTests("foo.api.system.28", "")
1553 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001554
1555 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1556 // tests if baz is actually linked to the stubs lib
1557 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1558 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1559 "foo.stubs.system.jar")
1560 }
1561 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001562 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001563 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001564 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001565 }
1566 // test if baz is not linked to the system variant of foo
1567 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1568 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1569 "foo.stubs.jar")
1570 }
Jiyong Park1be96912018-05-28 18:02:19 +09001571
Paul Duffin726d23c2020-01-22 16:30:37 +00001572 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1573 // tests if baz-test is actually linked to the test stubs lib
1574 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1575 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1576 "foo.stubs.test.jar")
1577 }
1578
Paul Duffina2db18f2020-01-22 17:11:15 +00001579 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1580 // tests if baz-29 is actually linked to the system 29 stubs lib
1581 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1582 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1583 "prebuilts/sdk/29/system/foo.jar")
1584 }
1585
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001586 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1587 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1588 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1589 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1590 "prebuilts/sdk/30/module-lib/foo.jar")
1591 }
1592
Jiyong Park1be96912018-05-28 18:02:19 +09001593 // test if baz has exported SDK lib names foo and bar to qux
1594 qux := ctx.ModuleForTests("qux", "android_common")
1595 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb8063c62020-08-20 11:33:12 +01001596 sdkLibs := android.SortedStringKeys(quxLib.ExportedSdkLibs())
Paul Duffin859fe962020-05-15 10:20:31 +01001597 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1598 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001599 }
1600 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001601}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001602
Anton Hansson7f66efa2020-10-08 14:47:23 +01001603func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1604 ctx, _ := testJava(t, `
1605 java_sdk_library {
1606 name: "sdk_lib",
1607 srcs: ["a.java"],
1608 impl_only_libs: ["foo"],
1609 stub_only_libs: ["bar"],
1610 }
1611 java_library {
1612 name: "foo",
1613 srcs: ["a.java"],
1614 sdk_version: "current",
1615 }
1616 java_library {
1617 name: "bar",
1618 srcs: ["a.java"],
1619 sdk_version: "current",
1620 }
1621 `)
1622
1623 for _, implName := range []string{"sdk_lib", "sdk_lib.impl"} {
1624 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1625 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1626 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1627 }
1628 }
1629 stubName := apiScopePublic.stubsLibraryModuleName("sdk_lib")
1630 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1631 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1632 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1633 }
1634}
1635
Paul Duffindaaa3322020-05-26 18:13:57 +01001636func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1637 ctx, _ := testJava(t, `
1638 java_sdk_library {
1639 name: "foo",
1640 srcs: ["a.java"],
1641 api_only: true,
1642 public: {
1643 enabled: true,
1644 },
1645 }
1646
1647 java_library {
1648 name: "bar",
1649 srcs: ["b.java"],
1650 libs: ["foo"],
1651 }
1652 `)
1653
1654 // The bar library should depend on the stubs jar.
1655 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1656 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1657 t.Errorf("expected %q, found %#q", expected, actual)
1658 }
1659}
1660
Paul Duffin46dc45a2020-05-14 15:39:10 +01001661func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1662 testJava(t, `
1663 java_sdk_library {
1664 name: "foo",
1665 srcs: ["a.java"],
1666 api_packages: ["foo"],
1667 public: {
1668 enabled: true,
1669 },
1670 }
1671
1672 java_library {
1673 name: "bar",
1674 srcs: ["b.java", ":foo{.public.stubs.source}"],
1675 }
1676 `)
1677}
1678
1679func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1680 testJavaError(t, `"foo" does not provide api scope system`, `
1681 java_sdk_library {
1682 name: "foo",
1683 srcs: ["a.java"],
1684 api_packages: ["foo"],
1685 public: {
1686 enabled: true,
1687 },
1688 }
1689
1690 java_library {
1691 name: "bar",
1692 srcs: ["b.java", ":foo{.system.stubs.source}"],
1693 }
1694 `)
1695}
1696
Paul Duffinca8d9a52020-06-26 22:20:25 +01001697func TestJavaSdkLibrary_Deps(t *testing.T) {
1698 ctx, _ := testJava(t, `
1699 java_sdk_library {
1700 name: "sdklib",
1701 srcs: ["a.java"],
1702 sdk_version: "none",
1703 system_modules: "none",
1704 public: {
1705 enabled: true,
1706 },
1707 }
1708 `)
1709
Paul Duffincee7e662020-07-09 17:32:57 +01001710 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001711 `dex2oatd`,
1712 `sdklib.impl`,
1713 `sdklib.stubs`,
1714 `sdklib.stubs.source`,
1715 `sdklib.xml`,
1716 })
1717}
1718
Paul Duffin46dc45a2020-05-14 15:39:10 +01001719func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1720 testJava(t, `
1721 java_sdk_library_import {
1722 name: "foo",
1723 public: {
1724 jars: ["a.jar"],
1725 stub_srcs: ["a.java"],
1726 current_api: "api/current.txt",
1727 removed_api: "api/removed.txt",
1728 },
1729 }
1730
1731 java_library {
1732 name: "bar",
1733 srcs: [":foo{.public.stubs.source}"],
1734 java_resources: [
1735 ":foo{.public.api.txt}",
1736 ":foo{.public.removed-api.txt}",
1737 ],
1738 }
1739 `)
1740}
1741
1742func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1743 bp := `
1744 java_sdk_library_import {
1745 name: "foo",
1746 public: {
1747 jars: ["a.jar"],
1748 },
1749 }
1750 `
1751
1752 t.Run("stubs.source", func(t *testing.T) {
1753 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1754 java_library {
1755 name: "bar",
1756 srcs: [":foo{.public.stubs.source}"],
1757 java_resources: [
1758 ":foo{.public.api.txt}",
1759 ":foo{.public.removed-api.txt}",
1760 ],
1761 }
1762 `)
1763 })
1764
1765 t.Run("api.txt", func(t *testing.T) {
1766 testJavaError(t, `api.txt not available for api scope public`, bp+`
1767 java_library {
1768 name: "bar",
1769 srcs: ["a.java"],
1770 java_resources: [
1771 ":foo{.public.api.txt}",
1772 ],
1773 }
1774 `)
1775 })
1776
1777 t.Run("removed-api.txt", func(t *testing.T) {
1778 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1779 java_library {
1780 name: "bar",
1781 srcs: ["a.java"],
1782 java_resources: [
1783 ":foo{.public.removed-api.txt}",
1784 ],
1785 }
1786 `)
1787 })
1788}
1789
Paul Duffin3375e352020-04-28 10:44:03 +01001790func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1791 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1792 java_sdk_library {
1793 name: "foo",
1794 srcs: ["a.java", "b.java"],
1795 api_packages: ["foo"],
1796 // Explicitly disable public to test the check that ensures the set of enabled
1797 // scopes is consistent.
1798 public: {
1799 enabled: false,
1800 },
1801 system: {
1802 enabled: true,
1803 },
1804 }
1805 `)
1806}
1807
Paul Duffin87a05a32020-05-12 11:50:28 +01001808func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1809 testJava(t, `
1810 java_sdk_library {
1811 name: "foo",
1812 srcs: ["a.java", "b.java"],
1813 api_packages: ["foo"],
1814 system: {
1815 enabled: true,
1816 sdk_version: "module_current",
1817 },
1818 }
1819 `)
1820}
1821
Paul Duffin0c5bae52020-06-02 13:00:08 +01001822func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1823 testJava(t, `
1824 java_sdk_library {
1825 name: "foo",
1826 srcs: ["a.java", "b.java"],
1827 api_packages: ["foo"],
1828 system: {
1829 enabled: true,
1830 },
1831 module_lib: {
1832 enabled: true,
1833 },
1834 }
1835 `)
1836}
1837
1838func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1839 testJava(t, `
1840 java_sdk_library {
1841 name: "foo",
1842 srcs: ["a.java", "b.java"],
1843 api_packages: ["foo"],
1844 system: {
1845 enabled: true,
1846 },
1847 system_server: {
1848 enabled: true,
1849 },
1850 }
1851 `)
1852}
1853
Paul Duffin803a9562020-05-20 11:52:25 +01001854func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1855 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1856 java_sdk_library {
1857 name: "foo",
1858 srcs: ["a.java"],
1859 public: {
1860 enabled: false,
1861 },
1862 }
1863
1864 java_library {
1865 name: "baz",
1866 srcs: ["a.java"],
1867 libs: ["foo"],
1868 sdk_version: "module_current",
1869 }
1870 `)
1871}
1872
1873func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1874 testJava(t, `
1875 java_sdk_library {
1876 name: "foo",
1877 srcs: ["a.java"],
1878 system: {
1879 enabled: true,
1880 },
1881 }
1882
1883 java_library {
1884 name: "baz",
1885 srcs: ["a.java"],
1886 libs: ["foo"],
1887 // foo does not have module-lib scope so it should fallback to system
1888 sdk_version: "module_current",
1889 }
1890 `)
1891}
1892
Jiyong Park932cdfe2020-05-28 00:19:53 +09001893func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1894 ctx, _ := testJava(t, `
1895 java_sdk_library {
1896 name: "foo",
1897 srcs: ["a.java"],
1898 system: {
1899 enabled: true,
1900 },
1901 default_to_stubs: true,
1902 }
1903
1904 java_library {
1905 name: "baz",
1906 srcs: ["a.java"],
1907 libs: ["foo"],
1908 // does not have sdk_version set, should fallback to module,
1909 // which will then fallback to system because the module scope
1910 // is not enabled.
1911 }
1912 `)
1913 // The baz library should depend on the system stubs jar.
1914 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1915 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1916 t.Errorf("expected %q, found %#q", expected, actual)
1917 }
1918}
1919
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001920var compilerFlagsTestCases = []struct {
1921 in string
1922 out bool
1923}{
1924 {
1925 in: "a",
1926 out: false,
1927 },
1928 {
1929 in: "-a",
1930 out: true,
1931 },
1932 {
1933 in: "-no-jdk",
1934 out: false,
1935 },
1936 {
1937 in: "-no-stdlib",
1938 out: false,
1939 },
1940 {
1941 in: "-kotlin-home",
1942 out: false,
1943 },
1944 {
1945 in: "-kotlin-home /some/path",
1946 out: false,
1947 },
1948 {
1949 in: "-include-runtime",
1950 out: false,
1951 },
1952 {
1953 in: "-Xintellij-plugin-root",
1954 out: false,
1955 },
1956}
1957
1958type mockContext struct {
1959 android.ModuleContext
1960 result bool
1961}
1962
1963func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1964 // CheckBadCompilerFlags calls this function when the flag should be rejected
1965 ctx.result = false
1966}
1967
1968func TestCompilerFlags(t *testing.T) {
1969 for _, testCase := range compilerFlagsTestCases {
1970 ctx := &mockContext{result: true}
1971 CheckKotlincFlags(ctx, []string{testCase.in})
1972 if ctx.result != testCase.out {
1973 t.Errorf("incorrect output:")
1974 t.Errorf(" input: %#v", testCase.in)
1975 t.Errorf(" expected: %#v", testCase.out)
1976 t.Errorf(" got: %#v", ctx.result)
1977 }
1978 }
1979}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001980
1981// TODO(jungjw): Consider making this more robust by ignoring path order.
1982func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1983 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1984 flags := strings.Split(variables["javacFlags"], " ")
1985 got := ""
1986 for _, flag := range flags {
1987 keyEnd := strings.Index(flag, "=")
1988 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1989 got = flag[keyEnd+1:]
1990 break
1991 }
1992 }
1993 if expected != got {
1994 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1995 }
1996}
1997
1998func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001999 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002000 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002001 bp := `
2002 java_library {
2003 name: "foo",
2004 srcs: ["a.java"],
2005 java_version: "1.8",
2006 }
2007
2008 java_library {
2009 name: "bar",
2010 srcs: ["b.java"],
2011 sdk_version: "none",
2012 system_modules: "none",
2013 patch_module: "java.base",
2014 java_version: "1.8",
2015 }
2016
2017 java_library {
2018 name: "baz",
2019 srcs: ["c.java"],
2020 patch_module: "java.base",
2021 java_version: "1.8",
2022 }
2023 `
2024 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002025
2026 checkPatchModuleFlag(t, ctx, "foo", "")
2027 checkPatchModuleFlag(t, ctx, "bar", "")
2028 checkPatchModuleFlag(t, ctx, "baz", "")
2029 })
2030
Pete Gillin0c2143e2019-05-02 15:32:11 +01002031 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002032 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002033 bp := `
2034 java_library {
2035 name: "foo",
2036 srcs: ["a.java"],
2037 }
2038
2039 java_library {
2040 name: "bar",
2041 srcs: ["b.java"],
2042 sdk_version: "none",
2043 system_modules: "none",
2044 patch_module: "java.base",
2045 }
2046
2047 java_library {
2048 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002049 srcs: [
2050 "c.java",
2051 // Tests for b/150878007
2052 "dir/d.java",
2053 "dir2/e.java",
2054 "dir2/f.java",
2055 "nested/dir/g.java"
2056 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002057 patch_module: "java.base",
2058 }
2059 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002060 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002061
2062 checkPatchModuleFlag(t, ctx, "foo", "")
2063 expected := "java.base=.:" + buildDir
2064 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002065 expected = "java.base=" + strings.Join([]string{
2066 ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002067 checkPatchModuleFlag(t, ctx, "baz", expected)
2068 })
2069}
Paul Duffina7b9f422020-01-10 17:12:18 +00002070
2071func TestJavaSystemModules(t *testing.T) {
2072 ctx, _ := testJava(t, `
2073 java_system_modules {
2074 name: "system-modules",
2075 libs: ["system-module1", "system-module2"],
2076 }
2077 java_library {
2078 name: "system-module1",
2079 srcs: ["a.java"],
2080 sdk_version: "none",
2081 system_modules: "none",
2082 }
2083 java_library {
2084 name: "system-module2",
2085 srcs: ["b.java"],
2086 sdk_version: "none",
2087 system_modules: "none",
2088 }
2089 `)
2090
2091 // check the existence of the module
2092 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2093
2094 cmd := systemModules.Rule("jarsTosystemModules")
2095
2096 // make sure the command compiles against the supplied modules.
2097 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2098 if !strings.Contains(cmd.Args["classpath"], module) {
2099 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2100 module)
2101 }
2102 }
2103}
Paul Duffin90169ba2020-01-10 17:16:44 +00002104
2105func TestJavaSystemModulesImport(t *testing.T) {
2106 ctx, _ := testJava(t, `
2107 java_system_modules_import {
2108 name: "system-modules",
2109 libs: ["system-module1", "system-module2"],
2110 }
2111 java_import {
2112 name: "system-module1",
2113 jars: ["a.jar"],
2114 }
2115 java_import {
2116 name: "system-module2",
2117 jars: ["b.jar"],
2118 }
2119 `)
2120
2121 // check the existence of the module
2122 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2123
2124 cmd := systemModules.Rule("jarsTosystemModules")
2125
2126 // make sure the command compiles against the supplied modules.
2127 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2128 if !strings.Contains(cmd.Args["classpath"], module) {
2129 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2130 module)
2131 }
2132 }
2133}
Paul Duffin83a2d962019-11-19 19:44:10 +00002134
2135func TestJavaLibraryWithSystemModules(t *testing.T) {
2136 ctx, _ := testJava(t, `
2137 java_library {
2138 name: "lib-with-source-system-modules",
2139 srcs: [
2140 "a.java",
2141 ],
2142 sdk_version: "none",
2143 system_modules: "source-system-modules",
2144 }
2145
2146 java_library {
2147 name: "source-jar",
2148 srcs: [
2149 "a.java",
2150 ],
2151 }
2152
2153 java_system_modules {
2154 name: "source-system-modules",
2155 libs: ["source-jar"],
2156 }
2157
2158 java_library {
2159 name: "lib-with-prebuilt-system-modules",
2160 srcs: [
2161 "a.java",
2162 ],
2163 sdk_version: "none",
2164 system_modules: "prebuilt-system-modules",
2165 }
2166
2167 java_import {
2168 name: "prebuilt-jar",
2169 jars: ["a.jar"],
2170 }
2171
2172 java_system_modules_import {
2173 name: "prebuilt-system-modules",
2174 libs: ["prebuilt-jar"],
2175 }
2176 `)
2177
2178 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2179
2180 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2181}
2182
2183func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2184 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2185 bootClasspath := javacRule.Args["bootClasspath"]
2186 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2187 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2188 }
2189}
Jiyong Park19604de2020-03-24 16:44:11 +09002190
2191func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2192 ctx, _ := testJava(t, `
2193 java_library {
2194 name: "foo",
2195 srcs: ["aidl/foo/IFoo.aidl"],
2196 libs: ["bar"],
2197 }
2198
2199 java_import {
2200 name: "bar",
2201 jars: ["a.jar"],
2202 aidl: {
2203 export_include_dirs: ["aidl/bar"],
2204 },
2205 }
2206 `)
2207
2208 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2209 expectedAidlFlag := "-Iaidl/bar"
2210 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2211 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2212 }
2213}
Liz Kammerdd849a82020-06-12 16:38:45 -07002214
2215func TestDataNativeBinaries(t *testing.T) {
2216 ctx, config := testJava(t, `
2217 java_test_host {
2218 name: "foo",
2219 srcs: ["a.java"],
2220 data_native_bins: ["bin"]
2221 }
2222
2223 python_binary_host {
2224 name: "bin",
2225 srcs: ["bin.py"],
2226 }
2227 `)
2228
2229 buildOS := android.BuildOs.String()
2230
2231 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2232 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2233 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2234 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2235 if !reflect.DeepEqual(expected, actual) {
2236 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2237 }
2238}