blob: 49838c7c70edcd80ee0b436c0bb4bfed770b9178 [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"
Nan Zhang61eaedb2017-11-02 13:28:15 -070022 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070023 "strings"
24 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070025
Jeongik Cha28df2572019-11-11 10:46:36 +090026 "github.com/google/blueprint/proptools"
27
Colin Crossa4f08812018-10-02 22:03:40 -070028 "android/soong/android"
29 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080030 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070031 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070032)
33
34var buildDir string
35
36func setUp() {
37 var err error
38 buildDir, err = ioutil.TempDir("", "soong_java_test")
39 if err != nil {
40 panic(err)
41 }
42}
43
44func tearDown() {
45 os.RemoveAll(buildDir)
46}
47
48func TestMain(m *testing.M) {
49 run := func() int {
50 setUp()
51 defer tearDown()
52
53 return m.Run()
54 }
55
56 os.Exit(run())
57}
Colin Cross527012a2017-11-30 22:56:16 -080058
Colin Cross98be1bb2019-12-13 20:41:13 -080059func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
60 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -070061}
62
Colin Cross98be1bb2019-12-13 20:41:13 -080063func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070064
Colin Cross4c428df2017-09-15 17:36:05 -070065 ctx := android.NewTestArchContext()
Colin Cross4b49b762019-11-22 15:25:03 -080066 ctx.RegisterModuleType("android_app", AndroidAppFactory)
67 ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
68 ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory)
69 ctx.RegisterModuleType("android_library", AndroidLibraryFactory)
70 ctx.RegisterModuleType("android_test", AndroidTestFactory)
71 ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
72 ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory)
73 ctx.RegisterModuleType("java_binary", BinaryFactory)
74 ctx.RegisterModuleType("java_binary_host", BinaryHostFactory)
75 ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory)
76 ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory)
77 ctx.RegisterModuleType("java_library", LibraryFactory)
78 ctx.RegisterModuleType("java_library_host", LibraryHostFactory)
79 ctx.RegisterModuleType("java_test", TestFactory)
80 ctx.RegisterModuleType("java_import", ImportFactory)
81 ctx.RegisterModuleType("java_import_host", ImportFactoryHost)
Paul Duffin47357662019-12-05 14:07:14 +000082 ctx.RegisterModuleType("java_defaults", DefaultsFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080083 ctx.RegisterModuleType("java_system_modules", SystemModulesFactory)
84 ctx.RegisterModuleType("java_genrule", genRuleFactory)
85 ctx.RegisterModuleType("java_plugin", PluginFactory)
86 ctx.RegisterModuleType("dex_import", DexImportFactory)
87 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
88 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
89 ctx.RegisterModuleType("droiddoc", DroiddocFactory)
90 ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory)
91 ctx.RegisterModuleType("droiddoc_template", ExportedDroiddocDirFactory)
92 ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory)
93 ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory)
94 ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory)
95 ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
96 ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory)
97 ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory)
Colin Cross5ea9bcc2017-07-27 15:41:32 -070098 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
99 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -0700100 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +0900101 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +0900102 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +0900103 })
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700104 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800105 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -0700106 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700107
108 // Register module types and mutators from cc needed for JNI testing
Colin Cross4b49b762019-11-22 15:25:03 -0800109 ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
110 ctx.RegisterModuleType("cc_object", cc.ObjectFactory)
111 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
112 ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory)
113 ctx.RegisterModuleType("ndk_prebuilt_shared_stl", cc.NdkPrebuiltSharedStlFactory)
Colin Crossa4f08812018-10-02 22:03:40 -0700114 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
115 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
116 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
117 })
118
Colin Cross527012a2017-11-30 22:56:16 -0800119 return ctx
120}
121
122func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800123 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800124
Colin Cross98be1bb2019-12-13 20:41:13 -0800125 pathCtx := android.PathContextForTesting(config)
Colin Crossc28bb0b2019-02-25 14:20:47 -0800126 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
127
Colin Cross98be1bb2019-12-13 20:41:13 -0800128 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100129 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800130 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700131 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800132 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800133}
134
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900135func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900136 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800137 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900138}
139
Colin Cross98be1bb2019-12-13 20:41:13 -0800140func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900141 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800142 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900143
Colin Cross98be1bb2019-12-13 20:41:13 -0800144 pathCtx := android.PathContextForTesting(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900145 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
146
Colin Cross98be1bb2019-12-13 20:41:13 -0800147 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900148 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
149 if len(errs) > 0 {
150 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900151 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900152 }
153 _, errs = ctx.PrepareBuildActions(config)
154 if len(errs) > 0 {
155 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900156 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900157 }
158
159 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900160
161 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900162}
163
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700164func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800165 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800166 return testJavaWithConfig(t, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900167}
168
Colin Cross98be1bb2019-12-13 20:41:13 -0800169func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900170 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800171 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800172 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700173
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700174 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700175}
176
Colin Cross2acdae82017-09-15 19:44:24 -0700177func moduleToPath(name string) string {
178 switch {
179 case name == `""`:
180 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700181 case strings.HasSuffix(name, ".jar"):
182 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700183 default:
184 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700185 }
186}
187
Jeongik Chae403e9e2019-12-07 00:16:24 +0900188func TestJavaLinkType(t *testing.T) {
189 testJava(t, `
190 java_library {
191 name: "foo",
192 srcs: ["a.java"],
193 libs: ["bar"],
194 static_libs: ["baz"],
195 }
196
197 java_library {
198 name: "bar",
199 sdk_version: "current",
200 srcs: ["b.java"],
201 }
202
203 java_library {
204 name: "baz",
205 sdk_version: "system_current",
206 srcs: ["c.java"],
207 }
208 `)
209
210 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.", `
211 java_library {
212 name: "foo",
213 srcs: ["a.java"],
214 libs: ["bar"],
215 sdk_version: "current",
216 static_libs: ["baz"],
217 }
218
219 java_library {
220 name: "bar",
221 sdk_version: "current",
222 srcs: ["b.java"],
223 }
224
225 java_library {
226 name: "baz",
227 sdk_version: "system_current",
228 srcs: ["c.java"],
229 }
230 `)
231
232 testJava(t, `
233 java_library {
234 name: "foo",
235 srcs: ["a.java"],
236 libs: ["bar"],
237 sdk_version: "system_current",
238 static_libs: ["baz"],
239 }
240
241 java_library {
242 name: "bar",
243 sdk_version: "current",
244 srcs: ["b.java"],
245 }
246
247 java_library {
248 name: "baz",
249 sdk_version: "system_current",
250 srcs: ["c.java"],
251 }
252 `)
253
254 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.", `
255 java_library {
256 name: "foo",
257 srcs: ["a.java"],
258 libs: ["bar"],
259 sdk_version: "system_current",
260 static_libs: ["baz"],
261 }
262
263 java_library {
264 name: "bar",
265 sdk_version: "current",
266 srcs: ["b.java"],
267 }
268
269 java_library {
270 name: "baz",
271 srcs: ["c.java"],
272 }
273 `)
274}
275
Colin Cross72bb3632017-07-13 16:23:21 -0700276func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700277 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700278 java_library {
279 name: "foo",
280 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700281 libs: ["bar"],
282 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700283 }
284
285 java_library {
286 name: "bar",
287 srcs: ["b.java"],
288 }
289
290 java_library {
291 name: "baz",
292 srcs: ["c.java"],
293 }
Colin Crossd5934c82017-10-02 13:55:26 -0700294 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700295
Colin Cross4c428df2017-09-15 17:36:05 -0700296 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700297 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700298
299 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
300 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
301 }
302
Colin Cross1ee23172017-10-18 14:44:18 -0700303 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700304 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
305 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700306
Nan Zhanged19fc32017-10-19 13:06:22 -0700307 if !strings.Contains(javac.Args["classpath"], barTurbine) {
308 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700309 }
310
Nan Zhanged19fc32017-10-19 13:06:22 -0700311 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
312 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700313 }
314
315 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
316 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700317 }
318}
319
Artur Satayev9cf46692019-11-26 18:08:34 +0000320func TestExportedPlugins(t *testing.T) {
321 type Result struct {
322 library string
323 processors string
324 }
325 var tests = []struct {
326 name string
327 extra string
328 results []Result
329 }{
330 {
331 name: "Exported plugin is not a direct plugin",
332 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
333 results: []Result{{library: "exports", processors: "-proc:none"}},
334 },
335 {
336 name: "Exports plugin to dependee",
337 extra: `
338 java_library{name: "exports", exported_plugins: ["plugin"]}
339 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
340 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
341 `,
342 results: []Result{
343 {library: "foo", processors: "-processor com.android.TestPlugin"},
344 {library: "bar", processors: "-processor com.android.TestPlugin"},
345 },
346 },
347 {
348 name: "Exports plugin to android_library",
349 extra: `
350 java_library{name: "exports", exported_plugins: ["plugin"]}
351 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
352 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
353 `,
354 results: []Result{
355 {library: "foo", processors: "-processor com.android.TestPlugin"},
356 {library: "bar", processors: "-processor com.android.TestPlugin"},
357 },
358 },
359 {
360 name: "Exports plugin is not propagated via transitive deps",
361 extra: `
362 java_library{name: "exports", exported_plugins: ["plugin"]}
363 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
364 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
365 `,
366 results: []Result{
367 {library: "foo", processors: "-processor com.android.TestPlugin"},
368 {library: "bar", processors: "-proc:none"},
369 },
370 },
371 {
372 name: "Exports plugin appends to plugins",
373 extra: `
374 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
375 java_library{name: "exports", exported_plugins: ["plugin"]}
376 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
377 `,
378 results: []Result{
379 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
380 },
381 },
382 }
383
384 for _, test := range tests {
385 t.Run(test.name, func(t *testing.T) {
386 ctx, _ := testJava(t, `
387 java_plugin {
388 name: "plugin",
389 processor_class: "com.android.TestPlugin",
390 }
391 `+test.extra)
392
393 for _, want := range test.results {
394 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
395 if javac.Args["processor"] != want.processors {
396 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
397 }
398 }
399 })
400 }
401}
402
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900403func TestSdkVersionByPartition(t *testing.T) {
404 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900405 java_library {
406 name: "foo",
407 srcs: ["a.java"],
408 vendor: true,
409 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900410 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900411
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900412 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900413 java_library {
414 name: "bar",
415 srcs: ["b.java"],
416 }
417 `)
418
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900419 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900420 bp := `
421 java_library {
422 name: "foo",
423 srcs: ["a.java"],
424 product_specific: true,
425 }
426 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800427
428 config := testConfig(nil, bp, nil)
429 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900430 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800431 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 +0900432 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800433 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900434 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900435 }
436}
437
Colin Crossd5934c82017-10-02 13:55:26 -0700438func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700439 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700440 java_library {
441 name: "foo",
442 srcs: ["a.java"],
443 target: {
444 android: {
445 srcs: ["b.java"],
446 },
447 },
448 }
449 `)
450
451 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
452 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
453 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
454 }
455}
456
Colin Cross6b4a32d2017-12-05 13:42:45 -0800457func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700458 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800459 java_library_host {
460 name: "foo",
461 srcs: ["a.java"],
462 }
463
464 java_binary_host {
465 name: "bar",
466 srcs: ["b.java"],
467 static_libs: ["foo"],
468 }
469 `)
470
471 buildOS := android.BuildOs.String()
472
473 bar := ctx.ModuleForTests("bar", buildOS+"_common")
474 barJar := bar.Output("bar.jar").Output.String()
475 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
476 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
477
478 // Test that the install binary wrapper depends on the installed jar file
479 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
480 t.Errorf("expected binary wrapper implicits [%q], got %v",
481 barJar, barWrapperDeps)
482 }
483
484}
485
Colin Cross72bb3632017-07-13 16:23:21 -0700486func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700487 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700488 java_library {
489 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000490 srcs: ["a.java", ":stubs-source"],
Colin Cross79c7c262019-04-17 11:11:46 -0700491 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700492 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700493 }
494
Colin Cross74d73e22017-08-02 11:05:49 -0700495 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700496 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700497 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700498 }
499
Colin Cross74d73e22017-08-02 11:05:49 -0700500 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700501 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700502 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700503 }
Colin Cross42be7612019-02-21 18:12:14 -0800504
505 dex_import {
506 name: "qux",
507 jars: ["b.jar"],
508 }
Colin Cross79c7c262019-04-17 11:11:46 -0700509
510 java_sdk_library_import {
511 name: "sdklib",
512 jars: ["b.jar"],
513 }
Paul Duffin91547182019-11-12 19:39:36 +0000514
515 prebuilt_stubs_sources {
516 name: "stubs-source",
517 srcs: ["stubs/sources/**/*.java"],
518 }
Colin Cross72bb3632017-07-13 16:23:21 -0700519 `)
520
Colin Cross4c428df2017-09-15 17:36:05 -0700521 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700522 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700523 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
524 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700525 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700526
Paul Duffin91547182019-11-12 19:39:36 +0000527 inputs := []string{}
528 for _, p := range javac.BuildParams.Inputs {
529 inputs = append(inputs, p.String())
530 }
531
532 expected := []string{
533 "a.java",
534 "stubs/sources/foo/Foo.java",
535 }
536 if !reflect.DeepEqual(expected, inputs) {
537 t.Errorf("foo inputs incorrect: expected %q, found %q", expected, inputs)
538 }
539
Colin Cross37f6d792018-07-12 12:28:41 -0700540 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
541 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700542 }
543
Colin Cross79c7c262019-04-17 11:11:46 -0700544 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
545 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
546 }
547
Colin Cross37f6d792018-07-12 12:28:41 -0700548 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
549 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700550 }
Colin Cross42be7612019-02-21 18:12:14 -0800551
552 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700553}
554
Colin Cross89536d42017-07-07 14:35:50 -0700555func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700556 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700557 java_defaults {
558 name: "defaults",
559 srcs: ["a.java"],
560 libs: ["bar"],
561 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700562 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700563 }
564
565 java_library {
566 name: "foo",
567 defaults: ["defaults"],
568 }
569
570 java_library {
571 name: "bar",
572 srcs: ["b.java"],
573 }
574
575 java_library {
576 name: "baz",
577 srcs: ["c.java"],
578 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700579
580 android_test {
581 name: "atestOptimize",
582 defaults: ["defaults"],
583 optimize: {enabled: true},
584 }
585
586 android_test {
587 name: "atestNoOptimize",
588 defaults: ["defaults"],
589 }
590
591 android_test {
592 name: "atestDefault",
593 srcs: ["a.java"],
594 }
Colin Cross89536d42017-07-07 14:35:50 -0700595 `)
596
Colin Cross4c428df2017-09-15 17:36:05 -0700597 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700598 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700599
600 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
601 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
602 }
603
Nan Zhanged19fc32017-10-19 13:06:22 -0700604 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
605 if !strings.Contains(javac.Args["classpath"], barTurbine) {
606 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700607 }
608
Colin Cross1ee23172017-10-18 14:44:18 -0700609 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700610 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
611 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700612 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700613
614 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
615 if atestOptimize.Output == nil {
616 t.Errorf("atestOptimize should optimize APK")
617 }
618
619 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
620 if atestNoOptimize.Output == nil {
621 t.Errorf("atestNoOptimize should not optimize APK")
622 }
623
624 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
625 if atestDefault.Output == nil {
626 t.Errorf("atestDefault should optimize APK")
627 }
Colin Cross89536d42017-07-07 14:35:50 -0700628}
629
Colin Cross0f37af02017-09-27 17:42:05 -0700630func TestResources(t *testing.T) {
631 var table = []struct {
632 name string
633 prop string
634 extra string
635 args string
636 }{
637 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700638 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700639 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800640 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700641 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700642 },
643 {
644 // Test that a module with java_resources includes the files
645 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700646 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
647 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700648 },
649 {
650 // Test that a module with a filegroup in java_resources includes the files with the
651 // path prefix
652 name: "resource filegroup",
653 prop: `java_resources: [":foo-res"]`,
654 extra: `
655 filegroup {
656 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800657 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700658 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700659 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700660 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700661 },
662 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700663 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
664 name: "wildcard dirs",
665 prop: `java_resource_dirs: ["java-res/*"]`,
666 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
667 },
668 {
669 // Test that a module exclude_java_resource_dirs excludes the files
670 name: "wildcard dirs",
671 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
672 args: "-C java-res/a -f java-res/a/a",
673 },
Colin Crosscedd4762018-09-13 11:26:19 -0700674 {
675 // Test wildcards in java_resources
676 name: "wildcard files",
677 prop: `java_resources: ["java-res/**/*"]`,
678 args: "-C . -f java-res/a/a -f java-res/b/b",
679 },
680 {
681 // Test exclude_java_resources with java_resources
682 name: "wildcard files with exclude",
683 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
684 args: "-C . -f java-res/a/a",
685 },
686 {
687 // Test exclude_java_resources with java_resource_dirs
688 name: "resource dirs with exclude files",
689 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
690 args: "-C java-res -f java-res/a/a",
691 },
692 {
693 // Test exclude_java_resource_dirs with java_resource_dirs
694 name: "resource dirs with exclude files",
695 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
696 args: "-C java-res -f java-res/a/a -f java-res/b/b",
697 },
Colin Cross0f37af02017-09-27 17:42:05 -0700698 }
699
700 for _, test := range table {
701 t.Run(test.name, func(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700702 ctx, _ := testJava(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700703 java_library {
704 name: "foo",
705 srcs: [
706 "a.java",
707 "b.java",
708 "c.java",
709 ],
710 `+test.prop+`,
711 }
712 `+test.extra)
713
Colin Cross331a1212018-08-15 20:40:52 -0700714 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700715 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700716
717 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
718 t.Errorf("foo combined jars %v does not contain %q",
719 foo.Inputs.Strings(), fooRes.Output.String())
720 }
721
Colin Crossaf9c55b2017-10-03 14:50:08 -0700722 if fooRes.Args["jarArgs"] != test.args {
723 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700724 fooRes.Args["jarArgs"], test.args)
725 }
726 })
727 }
728}
729
Colin Cross0c4ce212019-05-03 15:28:19 -0700730func TestIncludeSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700731 ctx, _ := testJava(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700732 java_library {
733 name: "foo",
734 srcs: [
735 "a.java",
736 "b.java",
737 "c.java",
738 ],
739 include_srcs: true,
740 }
741
742 java_library {
743 name: "bar",
744 srcs: [
745 "a.java",
746 "b.java",
747 "c.java",
748 ],
749 java_resource_dirs: ["java-res"],
750 include_srcs: true,
751 }
752 `)
753
754 // Test a library with include_srcs: true
755 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
756 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
757
758 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
759 t.Errorf("foo combined jars %v does not contain %q", w, g)
760 }
761
762 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
763 t.Errorf("foo source jar args %q is not %q", w, g)
764 }
765
766 // Test a library with include_srcs: true and resources
767 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
768 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
769 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
770 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
771
772 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
773 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
774 }
775
776 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
777 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
778 }
779
780 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
781 t.Errorf("bar combined jars %v does not contain %q", w, g)
782 }
783
784 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
785 t.Errorf("bar source jar args %q is not %q", w, g)
786 }
787
788 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
789 t.Errorf("bar resource jar args %q is not %q", w, g)
790 }
791}
792
Colin Cross54190b32017-10-09 15:34:10 -0700793func TestGeneratedSources(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700794 ctx, _ := testJava(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700795 java_library {
796 name: "foo",
797 srcs: [
798 "a*.java",
799 ":gen",
800 "b*.java",
801 ],
802 }
803
804 genrule {
805 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800806 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700807 out: ["gen.java"],
808 }
809 `)
810
811 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
812 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
813
Colin Cross15e86d92017-10-20 15:07:08 -0700814 if filepath.Base(genrule.Output.String()) != "gen.java" {
815 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700816 }
817
818 if len(javac.Inputs) != 3 ||
819 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700820 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700821 javac.Inputs[2].String() != "b.java" {
822 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
823 }
824}
825
Nan Zhang61eaedb2017-11-02 13:28:15 -0700826func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700827 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700828 java_library {
829 name: "foo",
830 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900831 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700832 }
833
834 java_library {
835 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800836 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700837 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900838 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700839 }
840
841 java_library {
842 name: "baz",
843 srcs: ["c.java"],
844 libs: ["bar"],
845 sdk_version: "14",
846 }
847 `)
848
849 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
850 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
851 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
852 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
853 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
854
855 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
856 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
857 }
858
859 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
860 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
861 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
862 }
863 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
864 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
865 }
866 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
867 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
868 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100869 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700870 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100871 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700872 }
873}
874
875func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700876 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700877 java_library {
878 name: "bar",
879 srcs: ["a.java","b.java","c.java"],
880 javac_shard_size: 1
881 }
882 `)
883
884 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
885 for i := 0; i < 3; i++ {
886 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
887 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
888 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
889 }
890 }
891}
892
Nan Zhang581fd212018-01-10 16:06:12 -0800893func TestDroiddoc(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700894 ctx, _ := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800895 droiddoc_template {
896 name: "droiddoc-templates-sdk",
897 path: ".",
898 }
Jiyong Park29074592019-07-07 16:27:47 +0900899 filegroup {
900 name: "bar-doc-aidl-srcs",
901 srcs: ["bar-doc/IBar.aidl"],
902 path: "bar-doc",
903 }
Nan Zhang581fd212018-01-10 16:06:12 -0800904 droiddoc {
905 name: "bar-doc",
906 srcs: [
907 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900908 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +0900909 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -0800910 ],
911 exclude_srcs: [
912 "bar-doc/b.java"
913 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800914 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800915 hdf: [
916 "android.whichdoc offline",
917 ],
918 knowntags: [
919 "bar-doc/known_oj_tags.txt",
920 ],
921 proofread_file: "libcore-proofread.txt",
922 todo_file: "libcore-docs-todo.html",
923 args: "-offlinemode -title \"libcore\"",
924 }
925 `)
926
Colin Crossc0806172019-06-14 18:51:47 -0700927 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +0900928 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -0700929 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +0900930 javaSrcs = append(javaSrcs, i.Base())
931 }
Colin Crossc0806172019-06-14 18:51:47 -0700932 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
933 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +0900934 }
935
Colin Crossc0806172019-06-14 18:51:47 -0700936 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
937 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
938 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
939 }
940
941 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
942 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +0900943 }
Nan Zhang581fd212018-01-10 16:06:12 -0800944}
945
Colin Cross54250902017-12-05 09:28:08 -0800946func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700947 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -0800948 java_library {
949 name: "foo",
950 srcs: ["a.java"],
951 }
952
953 java_genrule {
954 name: "jargen",
955 tool_files: ["b.java"],
956 cmd: "$(location b.java) $(in) $(out)",
957 out: ["jargen.jar"],
958 srcs: [":foo"],
959 }
960
961 java_library {
962 name: "bar",
963 static_libs: ["jargen"],
964 srcs: ["c.java"],
965 }
966
967 java_library {
968 name: "baz",
969 libs: ["jargen"],
970 srcs: ["c.java"],
971 }
972 `)
973
974 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
975 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
976 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
977 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
978 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
979
980 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
981 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
982 }
983
984 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
985 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
986 }
987
988 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
989 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
990 }
991
992 if len(barCombined.Inputs) != 2 ||
993 barCombined.Inputs[0].String() != bar.Output.String() ||
994 barCombined.Inputs[1].String() != jargen.Output.String() {
995 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
996 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
997 }
998}
999
Nan Zhang27e284d2018-02-09 21:03:53 +00001000func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001001 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001002 java_library {
1003 name: "foo",
1004 srcs: ["a.java", ":foo-srcs"],
1005 exclude_srcs: ["a.java", ":foo-excludes"],
1006 }
1007
1008 filegroup {
1009 name: "foo-srcs",
1010 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1011 }
1012
1013 filegroup {
1014 name: "foo-excludes",
1015 srcs: ["java-fg/a.java", "java-fg/b.java"],
1016 }
1017 `)
1018
1019 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1020
1021 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1022 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1023 }
1024}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001025
Paul Duffin52d398a2019-06-11 12:31:14 +01001026func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001027 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001028 "libcore/Android.bp": []byte(`
1029 java_library {
1030 name: "core",
1031 sdk_version: "none",
1032 system_modules: "none",
1033 }`),
1034 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001035 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001036 run(t, ctx, config)
1037}
1038
Jiyong Parkc678ad32018-04-10 13:07:10 +09001039func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001040 ctx, _ := testJava(t, `
Jiyong Parkc678ad32018-04-10 13:07:10 +09001041 droiddoc_template {
1042 name: "droiddoc-templates-sdk",
1043 path: ".",
1044 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001045 java_sdk_library {
1046 name: "foo",
1047 srcs: ["a.java", "b.java"],
1048 api_packages: ["foo"],
1049 }
1050 java_sdk_library {
1051 name: "bar",
1052 srcs: ["a.java", "b.java"],
1053 api_packages: ["bar"],
1054 }
1055 java_library {
1056 name: "baz",
1057 srcs: ["c.java"],
1058 libs: ["foo", "bar"],
1059 sdk_version: "system_current",
1060 }
Jiyong Park1be96912018-05-28 18:02:19 +09001061 java_library {
1062 name: "qux",
1063 srcs: ["c.java"],
1064 libs: ["baz"],
1065 sdk_version: "system_current",
1066 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001067 `)
1068
1069 // check the existence of the internal modules
1070 ctx.ModuleForTests("foo", "android_common")
1071 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
1072 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001073 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001074 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
1075 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001076 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Colin Cross7113d202019-11-20 16:39:12 -08001077 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +09001078 ctx.ModuleForTests("foo.api.public.28", "")
1079 ctx.ModuleForTests("foo.api.system.28", "")
1080 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001081
1082 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1083 // tests if baz is actually linked to the stubs lib
1084 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1085 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1086 "foo.stubs.system.jar")
1087 }
1088 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001089 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001090 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001091 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001092 }
1093 // test if baz is not linked to the system variant of foo
1094 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1095 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1096 "foo.stubs.jar")
1097 }
Jiyong Park1be96912018-05-28 18:02:19 +09001098
1099 // test if baz has exported SDK lib names foo and bar to qux
1100 qux := ctx.ModuleForTests("qux", "android_common")
1101 if quxLib, ok := qux.Module().(*Library); ok {
1102 sdkLibs := quxLib.ExportedSdkLibs()
1103 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
1104 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
1105 }
1106 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001107}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001108
1109var compilerFlagsTestCases = []struct {
1110 in string
1111 out bool
1112}{
1113 {
1114 in: "a",
1115 out: false,
1116 },
1117 {
1118 in: "-a",
1119 out: true,
1120 },
1121 {
1122 in: "-no-jdk",
1123 out: false,
1124 },
1125 {
1126 in: "-no-stdlib",
1127 out: false,
1128 },
1129 {
1130 in: "-kotlin-home",
1131 out: false,
1132 },
1133 {
1134 in: "-kotlin-home /some/path",
1135 out: false,
1136 },
1137 {
1138 in: "-include-runtime",
1139 out: false,
1140 },
1141 {
1142 in: "-Xintellij-plugin-root",
1143 out: false,
1144 },
1145}
1146
1147type mockContext struct {
1148 android.ModuleContext
1149 result bool
1150}
1151
1152func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1153 // CheckBadCompilerFlags calls this function when the flag should be rejected
1154 ctx.result = false
1155}
1156
1157func TestCompilerFlags(t *testing.T) {
1158 for _, testCase := range compilerFlagsTestCases {
1159 ctx := &mockContext{result: true}
1160 CheckKotlincFlags(ctx, []string{testCase.in})
1161 if ctx.result != testCase.out {
1162 t.Errorf("incorrect output:")
1163 t.Errorf(" input: %#v", testCase.in)
1164 t.Errorf(" expected: %#v", testCase.out)
1165 t.Errorf(" got: %#v", ctx.result)
1166 }
1167 }
1168}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001169
1170// TODO(jungjw): Consider making this more robust by ignoring path order.
1171func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1172 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1173 flags := strings.Split(variables["javacFlags"], " ")
1174 got := ""
1175 for _, flag := range flags {
1176 keyEnd := strings.Index(flag, "=")
1177 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1178 got = flag[keyEnd+1:]
1179 break
1180 }
1181 }
1182 if expected != got {
1183 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1184 }
1185}
1186
1187func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001188 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001189 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001190 bp := `
1191 java_library {
1192 name: "foo",
1193 srcs: ["a.java"],
1194 java_version: "1.8",
1195 }
1196
1197 java_library {
1198 name: "bar",
1199 srcs: ["b.java"],
1200 sdk_version: "none",
1201 system_modules: "none",
1202 patch_module: "java.base",
1203 java_version: "1.8",
1204 }
1205
1206 java_library {
1207 name: "baz",
1208 srcs: ["c.java"],
1209 patch_module: "java.base",
1210 java_version: "1.8",
1211 }
1212 `
1213 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001214
1215 checkPatchModuleFlag(t, ctx, "foo", "")
1216 checkPatchModuleFlag(t, ctx, "bar", "")
1217 checkPatchModuleFlag(t, ctx, "baz", "")
1218 })
1219
Pete Gillin0c2143e2019-05-02 15:32:11 +01001220 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001221 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001222 bp := `
1223 java_library {
1224 name: "foo",
1225 srcs: ["a.java"],
1226 }
1227
1228 java_library {
1229 name: "bar",
1230 srcs: ["b.java"],
1231 sdk_version: "none",
1232 system_modules: "none",
1233 patch_module: "java.base",
1234 }
1235
1236 java_library {
1237 name: "baz",
1238 srcs: ["c.java"],
1239 patch_module: "java.base",
1240 }
1241 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001242 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001243
1244 checkPatchModuleFlag(t, ctx, "foo", "")
1245 expected := "java.base=.:" + buildDir
1246 checkPatchModuleFlag(t, ctx, "bar", expected)
Insun Kang23433862019-01-24 20:46:28 +09001247 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework"), moduleToPath("updatable_media_stubs")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001248 checkPatchModuleFlag(t, ctx, "baz", expected)
1249 })
1250}