blob: 215070e6febdd91eb3feefe67973fe0a92903e8c [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"
Paul Duffin859fe962020-05-15 10:20:31 +010023 "sort"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Jeongik Cha28df2572019-11-11 10:46:36 +090028 "github.com/google/blueprint/proptools"
29
Colin Crossa4f08812018-10-02 22:03:40 -070030 "android/soong/android"
31 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080032 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070033 "android/soong/genrule"
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)
Paul Duffin884363e2019-12-19 10:21:09 +000084 RegisterDocsBuildComponents(ctx)
85 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000086 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070087 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffina48f7582019-12-19 11:25:19 +000088
89 RegisterPrebuiltApisBuildComponents(ctx)
90
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070091 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080092 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070093 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070094
95 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000096 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -070097
Martin Stjernholmd90676f2020-01-11 00:37:30 +000098 dexpreopt.RegisterToolModulesForTest(ctx)
99
Colin Cross527012a2017-11-30 22:56:16 -0800100 return ctx
101}
102
103func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800104 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800105
Colin Cross98be1bb2019-12-13 20:41:13 -0800106 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000107 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800108
Colin Cross98be1bb2019-12-13 20:41:13 -0800109 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100110 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800111 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700112 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800113 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800114}
115
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900116func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900117 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800118 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900119}
120
Colin Cross98be1bb2019-12-13 20:41:13 -0800121func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900122 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800123 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900124
Colin Cross98be1bb2019-12-13 20:41:13 -0800125 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000126 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900127
Colin Cross98be1bb2019-12-13 20:41:13 -0800128 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900129 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
130 if len(errs) > 0 {
131 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900132 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900133 }
134 _, errs = ctx.PrepareBuildActions(config)
135 if len(errs) > 0 {
136 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900137 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900138 }
139
140 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900141
142 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900143}
144
Colin Cross238c1f32020-06-07 16:58:18 -0700145func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
146 t.Helper()
147 return testJavaWithConfig(t, testConfig(nil, bp, fs))
148}
149
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700150func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800151 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700152 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900153}
154
Colin Cross98be1bb2019-12-13 20:41:13 -0800155func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900156 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800157 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800158 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700159
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700160 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700161}
162
Colin Cross2acdae82017-09-15 19:44:24 -0700163func moduleToPath(name string) string {
164 switch {
165 case name == `""`:
166 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700167 case strings.HasSuffix(name, ".jar"):
168 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700169 default:
170 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700171 }
172}
173
Jeongik Chae403e9e2019-12-07 00:16:24 +0900174func TestJavaLinkType(t *testing.T) {
175 testJava(t, `
176 java_library {
177 name: "foo",
178 srcs: ["a.java"],
179 libs: ["bar"],
180 static_libs: ["baz"],
181 }
182
183 java_library {
184 name: "bar",
185 sdk_version: "current",
186 srcs: ["b.java"],
187 }
188
189 java_library {
190 name: "baz",
191 sdk_version: "system_current",
192 srcs: ["c.java"],
193 }
194 `)
195
196 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.", `
197 java_library {
198 name: "foo",
199 srcs: ["a.java"],
200 libs: ["bar"],
201 sdk_version: "current",
202 static_libs: ["baz"],
203 }
204
205 java_library {
206 name: "bar",
207 sdk_version: "current",
208 srcs: ["b.java"],
209 }
210
211 java_library {
212 name: "baz",
213 sdk_version: "system_current",
214 srcs: ["c.java"],
215 }
216 `)
217
218 testJava(t, `
219 java_library {
220 name: "foo",
221 srcs: ["a.java"],
222 libs: ["bar"],
223 sdk_version: "system_current",
224 static_libs: ["baz"],
225 }
226
227 java_library {
228 name: "bar",
229 sdk_version: "current",
230 srcs: ["b.java"],
231 }
232
233 java_library {
234 name: "baz",
235 sdk_version: "system_current",
236 srcs: ["c.java"],
237 }
238 `)
239
240 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.", `
241 java_library {
242 name: "foo",
243 srcs: ["a.java"],
244 libs: ["bar"],
245 sdk_version: "system_current",
246 static_libs: ["baz"],
247 }
248
249 java_library {
250 name: "bar",
251 sdk_version: "current",
252 srcs: ["b.java"],
253 }
254
255 java_library {
256 name: "baz",
257 srcs: ["c.java"],
258 }
259 `)
260}
261
Colin Cross72bb3632017-07-13 16:23:21 -0700262func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700263 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700264 java_library {
265 name: "foo",
266 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700267 libs: ["bar"],
268 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700269 }
270
271 java_library {
272 name: "bar",
273 srcs: ["b.java"],
274 }
275
276 java_library {
277 name: "baz",
278 srcs: ["c.java"],
279 }
Colin Crossd5934c82017-10-02 13:55:26 -0700280 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700281
Colin Cross4c428df2017-09-15 17:36:05 -0700282 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700283 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700284
285 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
286 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
287 }
288
Colin Cross1ee23172017-10-18 14:44:18 -0700289 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700290 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
291 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700292
Nan Zhanged19fc32017-10-19 13:06:22 -0700293 if !strings.Contains(javac.Args["classpath"], barTurbine) {
294 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700295 }
296
Nan Zhanged19fc32017-10-19 13:06:22 -0700297 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
298 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700299 }
300
301 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
302 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700303 }
304}
305
Artur Satayev9cf46692019-11-26 18:08:34 +0000306func TestExportedPlugins(t *testing.T) {
307 type Result struct {
308 library string
309 processors string
310 }
311 var tests = []struct {
312 name string
313 extra string
314 results []Result
315 }{
316 {
317 name: "Exported plugin is not a direct plugin",
318 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
319 results: []Result{{library: "exports", processors: "-proc:none"}},
320 },
321 {
322 name: "Exports plugin to dependee",
323 extra: `
324 java_library{name: "exports", exported_plugins: ["plugin"]}
325 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
326 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
327 `,
328 results: []Result{
329 {library: "foo", processors: "-processor com.android.TestPlugin"},
330 {library: "bar", processors: "-processor com.android.TestPlugin"},
331 },
332 },
333 {
334 name: "Exports plugin to android_library",
335 extra: `
336 java_library{name: "exports", exported_plugins: ["plugin"]}
337 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
338 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
339 `,
340 results: []Result{
341 {library: "foo", processors: "-processor com.android.TestPlugin"},
342 {library: "bar", processors: "-processor com.android.TestPlugin"},
343 },
344 },
345 {
346 name: "Exports plugin is not propagated via transitive deps",
347 extra: `
348 java_library{name: "exports", exported_plugins: ["plugin"]}
349 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
350 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
351 `,
352 results: []Result{
353 {library: "foo", processors: "-processor com.android.TestPlugin"},
354 {library: "bar", processors: "-proc:none"},
355 },
356 },
357 {
358 name: "Exports plugin appends to plugins",
359 extra: `
360 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
361 java_library{name: "exports", exported_plugins: ["plugin"]}
362 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
363 `,
364 results: []Result{
365 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
366 },
367 },
368 }
369
370 for _, test := range tests {
371 t.Run(test.name, func(t *testing.T) {
372 ctx, _ := testJava(t, `
373 java_plugin {
374 name: "plugin",
375 processor_class: "com.android.TestPlugin",
376 }
377 `+test.extra)
378
379 for _, want := range test.results {
380 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
381 if javac.Args["processor"] != want.processors {
382 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
383 }
384 }
385 })
386 }
387}
388
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900389func TestSdkVersionByPartition(t *testing.T) {
390 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900391 java_library {
392 name: "foo",
393 srcs: ["a.java"],
394 vendor: true,
395 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900396 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900397
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900398 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900399 java_library {
400 name: "bar",
401 srcs: ["b.java"],
402 }
403 `)
404
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900405 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900406 bp := `
407 java_library {
408 name: "foo",
409 srcs: ["a.java"],
410 product_specific: true,
411 }
412 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800413
414 config := testConfig(nil, bp, nil)
415 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900416 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800417 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 +0900418 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800419 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900420 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900421 }
422}
423
Colin Crossd5934c82017-10-02 13:55:26 -0700424func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700425 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700426 java_library {
427 name: "foo",
428 srcs: ["a.java"],
429 target: {
430 android: {
431 srcs: ["b.java"],
432 },
433 },
434 }
435 `)
436
437 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
438 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
439 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
440 }
441}
442
Colin Cross6b4a32d2017-12-05 13:42:45 -0800443func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700444 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800445 java_library_host {
446 name: "foo",
447 srcs: ["a.java"],
448 }
449
450 java_binary_host {
451 name: "bar",
452 srcs: ["b.java"],
453 static_libs: ["foo"],
454 }
455 `)
456
457 buildOS := android.BuildOs.String()
458
459 bar := ctx.ModuleForTests("bar", buildOS+"_common")
460 barJar := bar.Output("bar.jar").Output.String()
461 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
462 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
463
464 // Test that the install binary wrapper depends on the installed jar file
465 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
466 t.Errorf("expected binary wrapper implicits [%q], got %v",
467 barJar, barWrapperDeps)
468 }
469
470}
471
Colin Cross72bb3632017-07-13 16:23:21 -0700472func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700473 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700474 java_library {
475 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000476 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000477 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700478 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700479 }
480
Colin Cross74d73e22017-08-02 11:05:49 -0700481 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700482 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700483 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700484 }
485
Colin Cross74d73e22017-08-02 11:05:49 -0700486 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700487 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700488 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700489 }
Colin Cross42be7612019-02-21 18:12:14 -0800490
491 dex_import {
492 name: "qux",
493 jars: ["b.jar"],
494 }
Colin Cross79c7c262019-04-17 11:11:46 -0700495
496 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000497 name: "sdklib",
498 public: {
499 jars: ["c.jar"],
500 },
501 }
502
Paul Duffin91547182019-11-12 19:39:36 +0000503 prebuilt_stubs_sources {
504 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000505 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000506 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000507
508 java_test_import {
509 name: "test",
510 jars: ["a.jar"],
511 test_suites: ["cts"],
512 test_config: "AndroidTest.xml",
513 }
Colin Cross72bb3632017-07-13 16:23:21 -0700514 `)
515
Paul Duffin9b478b02019-12-10 13:41:51 +0000516 fooModule := ctx.ModuleForTests("foo", "android_common")
517 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700518 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700519 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
520 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700521 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700522
Paul Duffin9b478b02019-12-10 13:41:51 +0000523 fooLibrary := fooModule.Module().(*Library)
524 assertDeepEquals(t, "foo java sources incorrect",
525 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000526
Paul Duffin9b478b02019-12-10 13:41:51 +0000527 assertDeepEquals(t, "foo java source jars incorrect",
528 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
529 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000530
Colin Cross37f6d792018-07-12 12:28:41 -0700531 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
532 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700533 }
534
Colin Cross79c7c262019-04-17 11:11:46 -0700535 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
536 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
537 }
538
Colin Cross37f6d792018-07-12 12:28:41 -0700539 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
540 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700541 }
Colin Cross42be7612019-02-21 18:12:14 -0800542
543 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700544}
545
Paul Duffin9b478b02019-12-10 13:41:51 +0000546func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
547 if !reflect.DeepEqual(expected, actual) {
548 t.Errorf("%s: expected %q, found %q", message, expected, actual)
549 }
550}
551
Paul Duffin56d44902020-01-31 13:36:25 +0000552func TestJavaSdkLibraryImport(t *testing.T) {
553 ctx, _ := testJava(t, `
554 java_library {
555 name: "foo",
556 srcs: ["a.java"],
557 libs: ["sdklib"],
558 sdk_version: "current",
559 }
560
561 java_library {
562 name: "foo.system",
563 srcs: ["a.java"],
564 libs: ["sdklib"],
565 sdk_version: "system_current",
566 }
567
568 java_library {
569 name: "foo.test",
570 srcs: ["a.java"],
571 libs: ["sdklib"],
572 sdk_version: "test_current",
573 }
574
575 java_sdk_library_import {
576 name: "sdklib",
577 public: {
578 jars: ["a.jar"],
579 },
580 system: {
581 jars: ["b.jar"],
582 },
583 test: {
584 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100585 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000586 },
587 }
588 `)
589
590 for _, scope := range []string{"", ".system", ".test"} {
591 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
592 javac := fooModule.Rule("javac")
593
594 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
595 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
596 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
597 }
598 }
599}
600
Colin Cross89536d42017-07-07 14:35:50 -0700601func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700602 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700603 java_defaults {
604 name: "defaults",
605 srcs: ["a.java"],
606 libs: ["bar"],
607 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700608 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700609 }
610
611 java_library {
612 name: "foo",
613 defaults: ["defaults"],
614 }
615
616 java_library {
617 name: "bar",
618 srcs: ["b.java"],
619 }
620
621 java_library {
622 name: "baz",
623 srcs: ["c.java"],
624 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700625
626 android_test {
627 name: "atestOptimize",
628 defaults: ["defaults"],
629 optimize: {enabled: true},
630 }
631
632 android_test {
633 name: "atestNoOptimize",
634 defaults: ["defaults"],
635 }
636
637 android_test {
638 name: "atestDefault",
639 srcs: ["a.java"],
640 }
Colin Cross89536d42017-07-07 14:35:50 -0700641 `)
642
Colin Cross4c428df2017-09-15 17:36:05 -0700643 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700644 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700645
646 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
647 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
648 }
649
Nan Zhanged19fc32017-10-19 13:06:22 -0700650 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
651 if !strings.Contains(javac.Args["classpath"], barTurbine) {
652 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700653 }
654
Colin Cross1ee23172017-10-18 14:44:18 -0700655 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700656 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
657 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700658 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700659
660 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
661 if atestOptimize.Output == nil {
662 t.Errorf("atestOptimize should optimize APK")
663 }
664
665 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
666 if atestNoOptimize.Output == nil {
667 t.Errorf("atestNoOptimize should not optimize APK")
668 }
669
670 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
671 if atestDefault.Output == nil {
672 t.Errorf("atestDefault should optimize APK")
673 }
Colin Cross89536d42017-07-07 14:35:50 -0700674}
675
Colin Cross0f37af02017-09-27 17:42:05 -0700676func TestResources(t *testing.T) {
677 var table = []struct {
678 name string
679 prop string
680 extra string
681 args string
682 }{
683 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700684 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700685 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800686 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700687 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700688 },
689 {
690 // Test that a module with java_resources includes the files
691 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700692 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
693 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700694 },
695 {
696 // Test that a module with a filegroup in java_resources includes the files with the
697 // path prefix
698 name: "resource filegroup",
699 prop: `java_resources: [":foo-res"]`,
700 extra: `
701 filegroup {
702 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800703 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700704 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700705 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700706 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700707 },
708 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700709 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
710 name: "wildcard dirs",
711 prop: `java_resource_dirs: ["java-res/*"]`,
712 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
713 },
714 {
715 // Test that a module exclude_java_resource_dirs excludes the files
716 name: "wildcard dirs",
717 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
718 args: "-C java-res/a -f java-res/a/a",
719 },
Colin Crosscedd4762018-09-13 11:26:19 -0700720 {
721 // Test wildcards in java_resources
722 name: "wildcard files",
723 prop: `java_resources: ["java-res/**/*"]`,
724 args: "-C . -f java-res/a/a -f java-res/b/b",
725 },
726 {
727 // Test exclude_java_resources with java_resources
728 name: "wildcard files with exclude",
729 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
730 args: "-C . -f java-res/a/a",
731 },
732 {
733 // Test exclude_java_resources with java_resource_dirs
734 name: "resource dirs with exclude files",
735 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
736 args: "-C java-res -f java-res/a/a",
737 },
738 {
739 // Test exclude_java_resource_dirs with java_resource_dirs
740 name: "resource dirs with exclude files",
741 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
742 args: "-C java-res -f java-res/a/a -f java-res/b/b",
743 },
Colin Cross0f37af02017-09-27 17:42:05 -0700744 }
745
746 for _, test := range table {
747 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700748 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700749 java_library {
750 name: "foo",
751 srcs: [
752 "a.java",
753 "b.java",
754 "c.java",
755 ],
756 `+test.prop+`,
757 }
Colin Cross238c1f32020-06-07 16:58:18 -0700758 `+test.extra,
759 map[string][]byte{
760 "java-res/a/a": nil,
761 "java-res/b/b": nil,
762 "java-res2/a": nil,
763 },
764 )
Colin Cross0f37af02017-09-27 17:42:05 -0700765
Colin Cross331a1212018-08-15 20:40:52 -0700766 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700767 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700768
769 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
770 t.Errorf("foo combined jars %v does not contain %q",
771 foo.Inputs.Strings(), fooRes.Output.String())
772 }
773
Colin Crossaf9c55b2017-10-03 14:50:08 -0700774 if fooRes.Args["jarArgs"] != test.args {
775 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700776 fooRes.Args["jarArgs"], test.args)
777 }
778 })
779 }
780}
781
Colin Cross0c4ce212019-05-03 15:28:19 -0700782func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700783 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700784 java_library {
785 name: "foo",
786 srcs: [
787 "a.java",
788 "b.java",
789 "c.java",
790 ],
791 include_srcs: true,
792 }
793
794 java_library {
795 name: "bar",
796 srcs: [
797 "a.java",
798 "b.java",
799 "c.java",
800 ],
801 java_resource_dirs: ["java-res"],
802 include_srcs: true,
803 }
Colin Cross238c1f32020-06-07 16:58:18 -0700804 `, map[string][]byte{
805 "java-res/a/a": nil,
806 "java-res/b/b": nil,
807 "java-res2/a": nil,
808 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700809
810 // Test a library with include_srcs: true
811 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
812 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
813
814 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
815 t.Errorf("foo combined jars %v does not contain %q", w, g)
816 }
817
818 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
819 t.Errorf("foo source jar args %q is not %q", w, g)
820 }
821
822 // Test a library with include_srcs: true and resources
823 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
824 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
825 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
826 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
827
828 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
829 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
830 }
831
832 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
833 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
834 }
835
836 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
837 t.Errorf("bar combined jars %v does not contain %q", w, g)
838 }
839
840 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
841 t.Errorf("bar source jar args %q is not %q", w, g)
842 }
843
844 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
845 t.Errorf("bar resource jar args %q is not %q", w, g)
846 }
847}
848
Colin Cross54190b32017-10-09 15:34:10 -0700849func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700850 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700851 java_library {
852 name: "foo",
853 srcs: [
854 "a*.java",
855 ":gen",
856 "b*.java",
857 ],
858 }
859
860 genrule {
861 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800862 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700863 out: ["gen.java"],
864 }
Colin Cross238c1f32020-06-07 16:58:18 -0700865 `, map[string][]byte{
866 "a.java": nil,
867 "b.java": nil,
868 })
Colin Cross54190b32017-10-09 15:34:10 -0700869
870 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
871 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
872
Colin Cross15e86d92017-10-20 15:07:08 -0700873 if filepath.Base(genrule.Output.String()) != "gen.java" {
874 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700875 }
876
877 if len(javac.Inputs) != 3 ||
878 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700879 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700880 javac.Inputs[2].String() != "b.java" {
881 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
882 }
883}
884
Nan Zhang61eaedb2017-11-02 13:28:15 -0700885func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700886 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700887 java_library {
888 name: "foo",
889 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900890 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700891 }
892
893 java_library {
894 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800895 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700896 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900897 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700898 }
899
900 java_library {
901 name: "baz",
902 srcs: ["c.java"],
903 libs: ["bar"],
904 sdk_version: "14",
905 }
906 `)
907
908 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
909 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
910 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
911 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
912 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
913
914 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
915 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
916 }
917
918 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
919 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
920 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
921 }
922 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
923 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
924 }
925 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
926 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
927 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100928 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700929 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100930 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700931 }
932}
933
934func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700935 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700936 java_library {
937 name: "bar",
938 srcs: ["a.java","b.java","c.java"],
939 javac_shard_size: 1
940 }
941 `)
942
943 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
944 for i := 0; i < 3; i++ {
945 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
946 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
947 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
948 }
949 }
950}
951
Nan Zhang581fd212018-01-10 16:06:12 -0800952func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700953 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +0000954 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -0800955 name: "droiddoc-templates-sdk",
956 path: ".",
957 }
Jiyong Park29074592019-07-07 16:27:47 +0900958 filegroup {
959 name: "bar-doc-aidl-srcs",
960 srcs: ["bar-doc/IBar.aidl"],
961 path: "bar-doc",
962 }
Nan Zhang581fd212018-01-10 16:06:12 -0800963 droiddoc {
964 name: "bar-doc",
965 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -0700966 "bar-doc/a.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900967 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +0900968 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -0800969 ],
970 exclude_srcs: [
971 "bar-doc/b.java"
972 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800973 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800974 hdf: [
975 "android.whichdoc offline",
976 ],
977 knowntags: [
978 "bar-doc/known_oj_tags.txt",
979 ],
980 proofread_file: "libcore-proofread.txt",
981 todo_file: "libcore-docs-todo.html",
982 args: "-offlinemode -title \"libcore\"",
983 }
Colin Cross238c1f32020-06-07 16:58:18 -0700984 `,
985 map[string][]byte{
986 "bar-doc/a.java": nil,
987 "bar-doc/b.java": nil,
988 })
Nan Zhang581fd212018-01-10 16:06:12 -0800989
Colin Crossc0806172019-06-14 18:51:47 -0700990 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +0900991 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -0700992 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +0900993 javaSrcs = append(javaSrcs, i.Base())
994 }
Colin Crossc0806172019-06-14 18:51:47 -0700995 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
996 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +0900997 }
998
Colin Crossc0806172019-06-14 18:51:47 -0700999 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
1000 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
1001 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1002 }
1003
1004 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1005 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001006 }
Nan Zhang581fd212018-01-10 16:06:12 -08001007}
1008
Paul Duffin83a2d962019-11-19 19:44:10 +00001009func TestDroidstubsWithSystemModules(t *testing.T) {
1010 ctx, _ := testJava(t, `
1011 droidstubs {
1012 name: "stubs-source-system-modules",
1013 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001014 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001015 ],
1016 sdk_version: "none",
1017 system_modules: "source-system-modules",
1018 }
1019
1020 java_library {
1021 name: "source-jar",
1022 srcs: [
1023 "a.java",
1024 ],
1025 }
1026
1027 java_system_modules {
1028 name: "source-system-modules",
1029 libs: ["source-jar"],
1030 }
1031
1032 droidstubs {
1033 name: "stubs-prebuilt-system-modules",
1034 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001035 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001036 ],
1037 sdk_version: "none",
1038 system_modules: "prebuilt-system-modules",
1039 }
1040
1041 java_import {
1042 name: "prebuilt-jar",
1043 jars: ["a.jar"],
1044 }
1045
1046 java_system_modules_import {
1047 name: "prebuilt-system-modules",
1048 libs: ["prebuilt-jar"],
1049 }
1050 `)
1051
1052 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1053
1054 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1055}
1056
1057func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1058 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1059 var systemJars []string
1060 for _, i := range metalavaRule.Implicits {
1061 systemJars = append(systemJars, i.Base())
1062 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001063 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001064 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1065 }
1066}
1067
Colin Cross54250902017-12-05 09:28:08 -08001068func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001069 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001070 java_library {
1071 name: "foo",
1072 srcs: ["a.java"],
1073 }
1074
1075 java_genrule {
1076 name: "jargen",
1077 tool_files: ["b.java"],
1078 cmd: "$(location b.java) $(in) $(out)",
1079 out: ["jargen.jar"],
1080 srcs: [":foo"],
1081 }
1082
1083 java_library {
1084 name: "bar",
1085 static_libs: ["jargen"],
1086 srcs: ["c.java"],
1087 }
1088
1089 java_library {
1090 name: "baz",
1091 libs: ["jargen"],
1092 srcs: ["c.java"],
1093 }
1094 `)
1095
1096 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1097 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1098 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1099 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1100 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1101
1102 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1103 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1104 }
1105
1106 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1107 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1108 }
1109
1110 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1111 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1112 }
1113
1114 if len(barCombined.Inputs) != 2 ||
1115 barCombined.Inputs[0].String() != bar.Output.String() ||
1116 barCombined.Inputs[1].String() != jargen.Output.String() {
1117 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1118 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1119 }
1120}
1121
Nan Zhang27e284d2018-02-09 21:03:53 +00001122func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001123 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001124 java_library {
1125 name: "foo",
1126 srcs: ["a.java", ":foo-srcs"],
1127 exclude_srcs: ["a.java", ":foo-excludes"],
1128 }
1129
1130 filegroup {
1131 name: "foo-srcs",
1132 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1133 }
1134
1135 filegroup {
1136 name: "foo-excludes",
1137 srcs: ["java-fg/a.java", "java-fg/b.java"],
1138 }
1139 `)
1140
1141 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1142
1143 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1144 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1145 }
1146}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001147
Paul Duffin52d398a2019-06-11 12:31:14 +01001148func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001149 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001150 "libcore/Android.bp": []byte(`
1151 java_library {
1152 name: "core",
1153 sdk_version: "none",
1154 system_modules: "none",
1155 }`),
1156 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001157 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001158 run(t, ctx, config)
1159}
1160
Jiyong Parkc678ad32018-04-10 13:07:10 +09001161func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001162 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001163 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001164 name: "droiddoc-templates-sdk",
1165 path: ".",
1166 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001167 java_sdk_library {
1168 name: "foo",
1169 srcs: ["a.java", "b.java"],
1170 api_packages: ["foo"],
1171 }
1172 java_sdk_library {
1173 name: "bar",
1174 srcs: ["a.java", "b.java"],
1175 api_packages: ["bar"],
1176 }
1177 java_library {
1178 name: "baz",
1179 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001180 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001181 sdk_version: "system_current",
1182 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001183 java_sdk_library {
1184 name: "barney",
1185 srcs: ["c.java"],
1186 api_only: true,
1187 }
1188 java_sdk_library {
1189 name: "betty",
1190 srcs: ["c.java"],
1191 shared_library: false,
1192 }
Paul Duffin859fe962020-05-15 10:20:31 +01001193 java_sdk_library_import {
1194 name: "quuz",
1195 public: {
1196 jars: ["c.jar"],
1197 },
1198 }
1199 java_sdk_library_import {
1200 name: "fred",
1201 public: {
1202 jars: ["b.jar"],
1203 },
1204 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001205 java_sdk_library_import {
1206 name: "wilma",
1207 public: {
1208 jars: ["b.jar"],
1209 },
1210 shared_library: false,
1211 }
Jiyong Park1be96912018-05-28 18:02:19 +09001212 java_library {
1213 name: "qux",
1214 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001215 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001216 sdk_version: "system_current",
1217 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001218 java_library {
1219 name: "baz-test",
1220 srcs: ["c.java"],
1221 libs: ["foo"],
1222 sdk_version: "test_current",
1223 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001224 java_library {
1225 name: "baz-29",
1226 srcs: ["c.java"],
1227 libs: ["foo"],
1228 sdk_version: "system_29",
1229 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001230 `)
1231
1232 // check the existence of the internal modules
1233 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001234 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1235 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1236 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1237 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1238 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1239 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001240 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001241 ctx.ModuleForTests("foo.api.public.28", "")
1242 ctx.ModuleForTests("foo.api.system.28", "")
1243 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001244
1245 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1246 // tests if baz is actually linked to the stubs lib
1247 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1248 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1249 "foo.stubs.system.jar")
1250 }
1251 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001252 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001253 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001254 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001255 }
1256 // test if baz is not linked to the system variant of foo
1257 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1258 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1259 "foo.stubs.jar")
1260 }
Jiyong Park1be96912018-05-28 18:02:19 +09001261
Paul Duffin726d23c2020-01-22 16:30:37 +00001262 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1263 // tests if baz-test is actually linked to the test stubs lib
1264 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1265 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1266 "foo.stubs.test.jar")
1267 }
1268
Paul Duffina2db18f2020-01-22 17:11:15 +00001269 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1270 // tests if baz-29 is actually linked to the system 29 stubs lib
1271 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1272 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1273 "prebuilts/sdk/29/system/foo.jar")
1274 }
1275
Jiyong Park1be96912018-05-28 18:02:19 +09001276 // test if baz has exported SDK lib names foo and bar to qux
1277 qux := ctx.ModuleForTests("qux", "android_common")
1278 if quxLib, ok := qux.Module().(*Library); ok {
1279 sdkLibs := quxLib.ExportedSdkLibs()
Paul Duffin859fe962020-05-15 10:20:31 +01001280 sort.Strings(sdkLibs)
1281 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1282 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001283 }
1284 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001285}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001286
Paul Duffindaaa3322020-05-26 18:13:57 +01001287func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1288 ctx, _ := testJava(t, `
1289 java_sdk_library {
1290 name: "foo",
1291 srcs: ["a.java"],
1292 api_only: true,
1293 public: {
1294 enabled: true,
1295 },
1296 }
1297
1298 java_library {
1299 name: "bar",
1300 srcs: ["b.java"],
1301 libs: ["foo"],
1302 }
1303 `)
1304
1305 // The bar library should depend on the stubs jar.
1306 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1307 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1308 t.Errorf("expected %q, found %#q", expected, actual)
1309 }
1310}
1311
Paul Duffin46dc45a2020-05-14 15:39:10 +01001312func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1313 testJava(t, `
1314 java_sdk_library {
1315 name: "foo",
1316 srcs: ["a.java"],
1317 api_packages: ["foo"],
1318 public: {
1319 enabled: true,
1320 },
1321 }
1322
1323 java_library {
1324 name: "bar",
1325 srcs: ["b.java", ":foo{.public.stubs.source}"],
1326 }
1327 `)
1328}
1329
1330func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1331 testJavaError(t, `"foo" does not provide api scope system`, `
1332 java_sdk_library {
1333 name: "foo",
1334 srcs: ["a.java"],
1335 api_packages: ["foo"],
1336 public: {
1337 enabled: true,
1338 },
1339 }
1340
1341 java_library {
1342 name: "bar",
1343 srcs: ["b.java", ":foo{.system.stubs.source}"],
1344 }
1345 `)
1346}
1347
1348func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1349 testJava(t, `
1350 java_sdk_library_import {
1351 name: "foo",
1352 public: {
1353 jars: ["a.jar"],
1354 stub_srcs: ["a.java"],
1355 current_api: "api/current.txt",
1356 removed_api: "api/removed.txt",
1357 },
1358 }
1359
1360 java_library {
1361 name: "bar",
1362 srcs: [":foo{.public.stubs.source}"],
1363 java_resources: [
1364 ":foo{.public.api.txt}",
1365 ":foo{.public.removed-api.txt}",
1366 ],
1367 }
1368 `)
1369}
1370
1371func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1372 bp := `
1373 java_sdk_library_import {
1374 name: "foo",
1375 public: {
1376 jars: ["a.jar"],
1377 },
1378 }
1379 `
1380
1381 t.Run("stubs.source", func(t *testing.T) {
1382 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1383 java_library {
1384 name: "bar",
1385 srcs: [":foo{.public.stubs.source}"],
1386 java_resources: [
1387 ":foo{.public.api.txt}",
1388 ":foo{.public.removed-api.txt}",
1389 ],
1390 }
1391 `)
1392 })
1393
1394 t.Run("api.txt", func(t *testing.T) {
1395 testJavaError(t, `api.txt not available for api scope public`, bp+`
1396 java_library {
1397 name: "bar",
1398 srcs: ["a.java"],
1399 java_resources: [
1400 ":foo{.public.api.txt}",
1401 ],
1402 }
1403 `)
1404 })
1405
1406 t.Run("removed-api.txt", func(t *testing.T) {
1407 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1408 java_library {
1409 name: "bar",
1410 srcs: ["a.java"],
1411 java_resources: [
1412 ":foo{.public.removed-api.txt}",
1413 ],
1414 }
1415 `)
1416 })
1417}
1418
Paul Duffin3375e352020-04-28 10:44:03 +01001419func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1420 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1421 java_sdk_library {
1422 name: "foo",
1423 srcs: ["a.java", "b.java"],
1424 api_packages: ["foo"],
1425 // Explicitly disable public to test the check that ensures the set of enabled
1426 // scopes is consistent.
1427 public: {
1428 enabled: false,
1429 },
1430 system: {
1431 enabled: true,
1432 },
1433 }
1434 `)
1435}
1436
Paul Duffin87a05a32020-05-12 11:50:28 +01001437func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1438 testJava(t, `
1439 java_sdk_library {
1440 name: "foo",
1441 srcs: ["a.java", "b.java"],
1442 api_packages: ["foo"],
1443 system: {
1444 enabled: true,
1445 sdk_version: "module_current",
1446 },
1447 }
1448 `)
1449}
1450
Paul Duffin803a9562020-05-20 11:52:25 +01001451func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1452 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1453 java_sdk_library {
1454 name: "foo",
1455 srcs: ["a.java"],
1456 public: {
1457 enabled: false,
1458 },
1459 }
1460
1461 java_library {
1462 name: "baz",
1463 srcs: ["a.java"],
1464 libs: ["foo"],
1465 sdk_version: "module_current",
1466 }
1467 `)
1468}
1469
1470func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1471 testJava(t, `
1472 java_sdk_library {
1473 name: "foo",
1474 srcs: ["a.java"],
1475 system: {
1476 enabled: true,
1477 },
1478 }
1479
1480 java_library {
1481 name: "baz",
1482 srcs: ["a.java"],
1483 libs: ["foo"],
1484 // foo does not have module-lib scope so it should fallback to system
1485 sdk_version: "module_current",
1486 }
1487 `)
1488}
1489
Jiyong Park932cdfe2020-05-28 00:19:53 +09001490func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1491 ctx, _ := testJava(t, `
1492 java_sdk_library {
1493 name: "foo",
1494 srcs: ["a.java"],
1495 system: {
1496 enabled: true,
1497 },
1498 default_to_stubs: true,
1499 }
1500
1501 java_library {
1502 name: "baz",
1503 srcs: ["a.java"],
1504 libs: ["foo"],
1505 // does not have sdk_version set, should fallback to module,
1506 // which will then fallback to system because the module scope
1507 // is not enabled.
1508 }
1509 `)
1510 // The baz library should depend on the system stubs jar.
1511 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1512 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1513 t.Errorf("expected %q, found %#q", expected, actual)
1514 }
1515}
1516
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001517var compilerFlagsTestCases = []struct {
1518 in string
1519 out bool
1520}{
1521 {
1522 in: "a",
1523 out: false,
1524 },
1525 {
1526 in: "-a",
1527 out: true,
1528 },
1529 {
1530 in: "-no-jdk",
1531 out: false,
1532 },
1533 {
1534 in: "-no-stdlib",
1535 out: false,
1536 },
1537 {
1538 in: "-kotlin-home",
1539 out: false,
1540 },
1541 {
1542 in: "-kotlin-home /some/path",
1543 out: false,
1544 },
1545 {
1546 in: "-include-runtime",
1547 out: false,
1548 },
1549 {
1550 in: "-Xintellij-plugin-root",
1551 out: false,
1552 },
1553}
1554
1555type mockContext struct {
1556 android.ModuleContext
1557 result bool
1558}
1559
1560func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1561 // CheckBadCompilerFlags calls this function when the flag should be rejected
1562 ctx.result = false
1563}
1564
1565func TestCompilerFlags(t *testing.T) {
1566 for _, testCase := range compilerFlagsTestCases {
1567 ctx := &mockContext{result: true}
1568 CheckKotlincFlags(ctx, []string{testCase.in})
1569 if ctx.result != testCase.out {
1570 t.Errorf("incorrect output:")
1571 t.Errorf(" input: %#v", testCase.in)
1572 t.Errorf(" expected: %#v", testCase.out)
1573 t.Errorf(" got: %#v", ctx.result)
1574 }
1575 }
1576}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001577
1578// TODO(jungjw): Consider making this more robust by ignoring path order.
1579func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1580 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1581 flags := strings.Split(variables["javacFlags"], " ")
1582 got := ""
1583 for _, flag := range flags {
1584 keyEnd := strings.Index(flag, "=")
1585 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1586 got = flag[keyEnd+1:]
1587 break
1588 }
1589 }
1590 if expected != got {
1591 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1592 }
1593}
1594
1595func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001596 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001597 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001598 bp := `
1599 java_library {
1600 name: "foo",
1601 srcs: ["a.java"],
1602 java_version: "1.8",
1603 }
1604
1605 java_library {
1606 name: "bar",
1607 srcs: ["b.java"],
1608 sdk_version: "none",
1609 system_modules: "none",
1610 patch_module: "java.base",
1611 java_version: "1.8",
1612 }
1613
1614 java_library {
1615 name: "baz",
1616 srcs: ["c.java"],
1617 patch_module: "java.base",
1618 java_version: "1.8",
1619 }
1620 `
1621 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001622
1623 checkPatchModuleFlag(t, ctx, "foo", "")
1624 checkPatchModuleFlag(t, ctx, "bar", "")
1625 checkPatchModuleFlag(t, ctx, "baz", "")
1626 })
1627
Pete Gillin0c2143e2019-05-02 15:32:11 +01001628 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001629 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001630 bp := `
1631 java_library {
1632 name: "foo",
1633 srcs: ["a.java"],
1634 }
1635
1636 java_library {
1637 name: "bar",
1638 srcs: ["b.java"],
1639 sdk_version: "none",
1640 system_modules: "none",
1641 patch_module: "java.base",
1642 }
1643
1644 java_library {
1645 name: "baz",
1646 srcs: ["c.java"],
1647 patch_module: "java.base",
1648 }
1649 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001650 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001651
1652 checkPatchModuleFlag(t, ctx, "foo", "")
1653 expected := "java.base=.:" + buildDir
1654 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09001655 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001656 checkPatchModuleFlag(t, ctx, "baz", expected)
1657 })
1658}
Paul Duffina7b9f422020-01-10 17:12:18 +00001659
1660func TestJavaSystemModules(t *testing.T) {
1661 ctx, _ := testJava(t, `
1662 java_system_modules {
1663 name: "system-modules",
1664 libs: ["system-module1", "system-module2"],
1665 }
1666 java_library {
1667 name: "system-module1",
1668 srcs: ["a.java"],
1669 sdk_version: "none",
1670 system_modules: "none",
1671 }
1672 java_library {
1673 name: "system-module2",
1674 srcs: ["b.java"],
1675 sdk_version: "none",
1676 system_modules: "none",
1677 }
1678 `)
1679
1680 // check the existence of the module
1681 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1682
1683 cmd := systemModules.Rule("jarsTosystemModules")
1684
1685 // make sure the command compiles against the supplied modules.
1686 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1687 if !strings.Contains(cmd.Args["classpath"], module) {
1688 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1689 module)
1690 }
1691 }
1692}
Paul Duffin90169ba2020-01-10 17:16:44 +00001693
1694func TestJavaSystemModulesImport(t *testing.T) {
1695 ctx, _ := testJava(t, `
1696 java_system_modules_import {
1697 name: "system-modules",
1698 libs: ["system-module1", "system-module2"],
1699 }
1700 java_import {
1701 name: "system-module1",
1702 jars: ["a.jar"],
1703 }
1704 java_import {
1705 name: "system-module2",
1706 jars: ["b.jar"],
1707 }
1708 `)
1709
1710 // check the existence of the module
1711 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1712
1713 cmd := systemModules.Rule("jarsTosystemModules")
1714
1715 // make sure the command compiles against the supplied modules.
1716 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1717 if !strings.Contains(cmd.Args["classpath"], module) {
1718 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1719 module)
1720 }
1721 }
1722}
Paul Duffin83a2d962019-11-19 19:44:10 +00001723
1724func TestJavaLibraryWithSystemModules(t *testing.T) {
1725 ctx, _ := testJava(t, `
1726 java_library {
1727 name: "lib-with-source-system-modules",
1728 srcs: [
1729 "a.java",
1730 ],
1731 sdk_version: "none",
1732 system_modules: "source-system-modules",
1733 }
1734
1735 java_library {
1736 name: "source-jar",
1737 srcs: [
1738 "a.java",
1739 ],
1740 }
1741
1742 java_system_modules {
1743 name: "source-system-modules",
1744 libs: ["source-jar"],
1745 }
1746
1747 java_library {
1748 name: "lib-with-prebuilt-system-modules",
1749 srcs: [
1750 "a.java",
1751 ],
1752 sdk_version: "none",
1753 system_modules: "prebuilt-system-modules",
1754 }
1755
1756 java_import {
1757 name: "prebuilt-jar",
1758 jars: ["a.jar"],
1759 }
1760
1761 java_system_modules_import {
1762 name: "prebuilt-system-modules",
1763 libs: ["prebuilt-jar"],
1764 }
1765 `)
1766
1767 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
1768
1769 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
1770}
1771
1772func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
1773 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
1774 bootClasspath := javacRule.Args["bootClasspath"]
1775 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
1776 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
1777 }
1778}
Jiyong Park19604de2020-03-24 16:44:11 +09001779
1780func TestAidlExportIncludeDirsFromImports(t *testing.T) {
1781 ctx, _ := testJava(t, `
1782 java_library {
1783 name: "foo",
1784 srcs: ["aidl/foo/IFoo.aidl"],
1785 libs: ["bar"],
1786 }
1787
1788 java_import {
1789 name: "bar",
1790 jars: ["a.jar"],
1791 aidl: {
1792 export_include_dirs: ["aidl/bar"],
1793 },
1794 }
1795 `)
1796
1797 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
1798 expectedAidlFlag := "-Iaidl/bar"
1799 if !strings.Contains(aidlCommand, expectedAidlFlag) {
1800 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
1801 }
1802}