blob: 924f76bdc0561f1c5b2624f6ebaeb8aa1c27f65b [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 {
Hans Boehme4b53422020-01-25 01:44:30 +000060 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()
Paul Duffinf9b1da02019-12-18 19:51:55 +000066 RegisterJavaBuildComponents(ctx)
67 RegisterAppBuildComponents(ctx)
68 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000069 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000070 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080071 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080072 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
73 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000074 RegisterDocsBuildComponents(ctx)
75 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000076 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070077 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffina48f7582019-12-19 11:25:19 +000078
79 RegisterPrebuiltApisBuildComponents(ctx)
80
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070081 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080082 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070083 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070084
85 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000086 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080087 ctx.RegisterModuleType("ndk_prebuilt_shared_stl", cc.NdkPrebuiltSharedStlFactory)
Colin Crossa4f08812018-10-02 22:03:40 -070088
Colin Cross527012a2017-11-30 22:56:16 -080089 return ctx
90}
91
92func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -080093 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -080094
Colin Cross98be1bb2019-12-13 20:41:13 -080095 pathCtx := android.PathContextForTesting(config)
Hans Boehm453bf092020-01-25 01:44:30 +000096 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -080097
Colin Cross98be1bb2019-12-13 20:41:13 -080098 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +010099 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800100 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700101 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800102 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800103}
104
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900105func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900106 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800107 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900108}
109
Colin Cross98be1bb2019-12-13 20:41:13 -0800110func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900111 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800112 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900113
Colin Cross98be1bb2019-12-13 20:41:13 -0800114 pathCtx := android.PathContextForTesting(config)
Hans Boehm453bf092020-01-25 01:44:30 +0000115 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900116
Colin Cross98be1bb2019-12-13 20:41:13 -0800117 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900118 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
119 if len(errs) > 0 {
120 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900121 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900122 }
123 _, errs = ctx.PrepareBuildActions(config)
124 if len(errs) > 0 {
125 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900126 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900127 }
128
129 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900130
131 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900132}
133
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700134func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800135 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800136 return testJavaWithConfig(t, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900137}
138
Colin Cross98be1bb2019-12-13 20:41:13 -0800139func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900140 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800141 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800142 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700143
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700144 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700145}
146
Colin Cross2acdae82017-09-15 19:44:24 -0700147func moduleToPath(name string) string {
148 switch {
149 case name == `""`:
150 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700151 case strings.HasSuffix(name, ".jar"):
152 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700153 default:
154 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700155 }
156}
157
Jeongik Chae403e9e2019-12-07 00:16:24 +0900158func TestJavaLinkType(t *testing.T) {
159 testJava(t, `
160 java_library {
161 name: "foo",
162 srcs: ["a.java"],
163 libs: ["bar"],
164 static_libs: ["baz"],
165 }
166
167 java_library {
168 name: "bar",
169 sdk_version: "current",
170 srcs: ["b.java"],
171 }
172
173 java_library {
174 name: "baz",
175 sdk_version: "system_current",
176 srcs: ["c.java"],
177 }
178 `)
179
180 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.", `
181 java_library {
182 name: "foo",
183 srcs: ["a.java"],
184 libs: ["bar"],
185 sdk_version: "current",
186 static_libs: ["baz"],
187 }
188
189 java_library {
190 name: "bar",
191 sdk_version: "current",
192 srcs: ["b.java"],
193 }
194
195 java_library {
196 name: "baz",
197 sdk_version: "system_current",
198 srcs: ["c.java"],
199 }
200 `)
201
202 testJava(t, `
203 java_library {
204 name: "foo",
205 srcs: ["a.java"],
206 libs: ["bar"],
207 sdk_version: "system_current",
208 static_libs: ["baz"],
209 }
210
211 java_library {
212 name: "bar",
213 sdk_version: "current",
214 srcs: ["b.java"],
215 }
216
217 java_library {
218 name: "baz",
219 sdk_version: "system_current",
220 srcs: ["c.java"],
221 }
222 `)
223
224 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.", `
225 java_library {
226 name: "foo",
227 srcs: ["a.java"],
228 libs: ["bar"],
229 sdk_version: "system_current",
230 static_libs: ["baz"],
231 }
232
233 java_library {
234 name: "bar",
235 sdk_version: "current",
236 srcs: ["b.java"],
237 }
238
239 java_library {
240 name: "baz",
241 srcs: ["c.java"],
242 }
243 `)
244}
245
Colin Cross72bb3632017-07-13 16:23:21 -0700246func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700247 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700248 java_library {
249 name: "foo",
250 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700251 libs: ["bar"],
252 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700253 }
254
255 java_library {
256 name: "bar",
257 srcs: ["b.java"],
258 }
259
260 java_library {
261 name: "baz",
262 srcs: ["c.java"],
263 }
Colin Crossd5934c82017-10-02 13:55:26 -0700264 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700265
Colin Cross4c428df2017-09-15 17:36:05 -0700266 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700267 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700268
269 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
270 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
271 }
272
Colin Cross1ee23172017-10-18 14:44:18 -0700273 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700274 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
275 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700276
Nan Zhanged19fc32017-10-19 13:06:22 -0700277 if !strings.Contains(javac.Args["classpath"], barTurbine) {
278 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700279 }
280
Nan Zhanged19fc32017-10-19 13:06:22 -0700281 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
282 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700283 }
284
285 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
286 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700287 }
288}
289
Artur Satayev9cf46692019-11-26 18:08:34 +0000290func TestExportedPlugins(t *testing.T) {
291 type Result struct {
292 library string
293 processors string
294 }
295 var tests = []struct {
296 name string
297 extra string
298 results []Result
299 }{
300 {
301 name: "Exported plugin is not a direct plugin",
302 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
303 results: []Result{{library: "exports", processors: "-proc:none"}},
304 },
305 {
306 name: "Exports plugin to dependee",
307 extra: `
308 java_library{name: "exports", exported_plugins: ["plugin"]}
309 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
310 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
311 `,
312 results: []Result{
313 {library: "foo", processors: "-processor com.android.TestPlugin"},
314 {library: "bar", processors: "-processor com.android.TestPlugin"},
315 },
316 },
317 {
318 name: "Exports plugin to android_library",
319 extra: `
320 java_library{name: "exports", exported_plugins: ["plugin"]}
321 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
322 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
323 `,
324 results: []Result{
325 {library: "foo", processors: "-processor com.android.TestPlugin"},
326 {library: "bar", processors: "-processor com.android.TestPlugin"},
327 },
328 },
329 {
330 name: "Exports plugin is not propagated via transitive deps",
331 extra: `
332 java_library{name: "exports", exported_plugins: ["plugin"]}
333 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
334 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
335 `,
336 results: []Result{
337 {library: "foo", processors: "-processor com.android.TestPlugin"},
338 {library: "bar", processors: "-proc:none"},
339 },
340 },
341 {
342 name: "Exports plugin appends to plugins",
343 extra: `
344 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
345 java_library{name: "exports", exported_plugins: ["plugin"]}
346 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
347 `,
348 results: []Result{
349 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
350 },
351 },
352 }
353
354 for _, test := range tests {
355 t.Run(test.name, func(t *testing.T) {
356 ctx, _ := testJava(t, `
357 java_plugin {
358 name: "plugin",
359 processor_class: "com.android.TestPlugin",
360 }
361 `+test.extra)
362
363 for _, want := range test.results {
364 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
365 if javac.Args["processor"] != want.processors {
366 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
367 }
368 }
369 })
370 }
371}
372
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900373func TestSdkVersionByPartition(t *testing.T) {
374 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900375 java_library {
376 name: "foo",
377 srcs: ["a.java"],
378 vendor: true,
379 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900380 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900381
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900382 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900383 java_library {
384 name: "bar",
385 srcs: ["b.java"],
386 }
387 `)
388
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900389 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900390 bp := `
391 java_library {
392 name: "foo",
393 srcs: ["a.java"],
394 product_specific: true,
395 }
396 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800397
398 config := testConfig(nil, bp, nil)
399 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900400 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800401 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 +0900402 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800403 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900404 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900405 }
406}
407
Colin Crossd5934c82017-10-02 13:55:26 -0700408func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700409 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700410 java_library {
411 name: "foo",
412 srcs: ["a.java"],
413 target: {
414 android: {
415 srcs: ["b.java"],
416 },
417 },
418 }
419 `)
420
421 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
422 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
423 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
424 }
425}
426
Colin Cross6b4a32d2017-12-05 13:42:45 -0800427func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700428 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800429 java_library_host {
430 name: "foo",
431 srcs: ["a.java"],
432 }
433
434 java_binary_host {
435 name: "bar",
436 srcs: ["b.java"],
437 static_libs: ["foo"],
438 }
439 `)
440
441 buildOS := android.BuildOs.String()
442
443 bar := ctx.ModuleForTests("bar", buildOS+"_common")
444 barJar := bar.Output("bar.jar").Output.String()
445 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
446 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
447
448 // Test that the install binary wrapper depends on the installed jar file
449 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
450 t.Errorf("expected binary wrapper implicits [%q], got %v",
451 barJar, barWrapperDeps)
452 }
453
454}
455
Colin Cross72bb3632017-07-13 16:23:21 -0700456func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700457 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700458 java_library {
459 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000460 srcs: ["a.java", ":stubs-source"],
Paul Duffin56d44902020-01-31 13:36:25 +0000461 libs: ["bar", "sdklib", "sdklib-legacy"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700462 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700463 }
464
Colin Cross74d73e22017-08-02 11:05:49 -0700465 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700466 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700467 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700468 }
469
Colin Cross74d73e22017-08-02 11:05:49 -0700470 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700471 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700472 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700473 }
Colin Cross42be7612019-02-21 18:12:14 -0800474
475 dex_import {
476 name: "qux",
477 jars: ["b.jar"],
478 }
Colin Cross79c7c262019-04-17 11:11:46 -0700479
480 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000481 name: "sdklib-legacy",
Colin Cross79c7c262019-04-17 11:11:46 -0700482 jars: ["b.jar"],
483 }
Paul Duffin91547182019-11-12 19:39:36 +0000484
Paul Duffin56d44902020-01-31 13:36:25 +0000485 java_sdk_library_import {
486 name: "sdklib",
487 public: {
488 jars: ["c.jar"],
489 },
490 }
491
Paul Duffin91547182019-11-12 19:39:36 +0000492 prebuilt_stubs_sources {
493 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000494 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000495 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000496
497 java_test_import {
498 name: "test",
499 jars: ["a.jar"],
500 test_suites: ["cts"],
501 test_config: "AndroidTest.xml",
502 }
Colin Cross72bb3632017-07-13 16:23:21 -0700503 `)
504
Paul Duffin9b478b02019-12-10 13:41:51 +0000505 fooModule := ctx.ModuleForTests("foo", "android_common")
506 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700507 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700508 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
509 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700510 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700511
Paul Duffin9b478b02019-12-10 13:41:51 +0000512 fooLibrary := fooModule.Module().(*Library)
513 assertDeepEquals(t, "foo java sources incorrect",
514 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000515
Paul Duffin9b478b02019-12-10 13:41:51 +0000516 assertDeepEquals(t, "foo java source jars incorrect",
517 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
518 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000519
Colin Cross37f6d792018-07-12 12:28:41 -0700520 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
521 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700522 }
523
Colin Cross79c7c262019-04-17 11:11:46 -0700524 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
525 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
526 }
527
Colin Cross37f6d792018-07-12 12:28:41 -0700528 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
529 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700530 }
Colin Cross42be7612019-02-21 18:12:14 -0800531
532 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700533}
534
Paul Duffin9b478b02019-12-10 13:41:51 +0000535func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
536 if !reflect.DeepEqual(expected, actual) {
537 t.Errorf("%s: expected %q, found %q", message, expected, actual)
538 }
539}
540
Paul Duffin56d44902020-01-31 13:36:25 +0000541func TestJavaSdkLibraryImport(t *testing.T) {
542 ctx, _ := testJava(t, `
543 java_library {
544 name: "foo",
545 srcs: ["a.java"],
546 libs: ["sdklib"],
547 sdk_version: "current",
548 }
549
550 java_library {
551 name: "foo.system",
552 srcs: ["a.java"],
553 libs: ["sdklib"],
554 sdk_version: "system_current",
555 }
556
557 java_library {
558 name: "foo.test",
559 srcs: ["a.java"],
560 libs: ["sdklib"],
561 sdk_version: "test_current",
562 }
563
564 java_sdk_library_import {
565 name: "sdklib",
566 public: {
567 jars: ["a.jar"],
568 },
569 system: {
570 jars: ["b.jar"],
571 },
572 test: {
573 jars: ["c.jar"],
574 },
575 }
576 `)
577
578 for _, scope := range []string{"", ".system", ".test"} {
579 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
580 javac := fooModule.Rule("javac")
581
582 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
583 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
584 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
585 }
586 }
587}
588
Colin Cross89536d42017-07-07 14:35:50 -0700589func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700590 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700591 java_defaults {
592 name: "defaults",
593 srcs: ["a.java"],
594 libs: ["bar"],
595 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700596 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700597 }
598
599 java_library {
600 name: "foo",
601 defaults: ["defaults"],
602 }
603
604 java_library {
605 name: "bar",
606 srcs: ["b.java"],
607 }
608
609 java_library {
610 name: "baz",
611 srcs: ["c.java"],
612 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700613
614 android_test {
615 name: "atestOptimize",
616 defaults: ["defaults"],
617 optimize: {enabled: true},
618 }
619
620 android_test {
621 name: "atestNoOptimize",
622 defaults: ["defaults"],
623 }
624
625 android_test {
626 name: "atestDefault",
627 srcs: ["a.java"],
628 }
Colin Cross89536d42017-07-07 14:35:50 -0700629 `)
630
Colin Cross4c428df2017-09-15 17:36:05 -0700631 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700632 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700633
634 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
635 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
636 }
637
Nan Zhanged19fc32017-10-19 13:06:22 -0700638 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
639 if !strings.Contains(javac.Args["classpath"], barTurbine) {
640 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700641 }
642
Colin Cross1ee23172017-10-18 14:44:18 -0700643 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700644 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
645 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700646 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700647
648 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
649 if atestOptimize.Output == nil {
650 t.Errorf("atestOptimize should optimize APK")
651 }
652
653 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
654 if atestNoOptimize.Output == nil {
655 t.Errorf("atestNoOptimize should not optimize APK")
656 }
657
658 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
659 if atestDefault.Output == nil {
660 t.Errorf("atestDefault should optimize APK")
661 }
Colin Cross89536d42017-07-07 14:35:50 -0700662}
663
Colin Cross0f37af02017-09-27 17:42:05 -0700664func TestResources(t *testing.T) {
665 var table = []struct {
666 name string
667 prop string
668 extra string
669 args string
670 }{
671 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700672 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700673 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800674 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700675 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700676 },
677 {
678 // Test that a module with java_resources includes the files
679 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700680 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
681 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700682 },
683 {
684 // Test that a module with a filegroup in java_resources includes the files with the
685 // path prefix
686 name: "resource filegroup",
687 prop: `java_resources: [":foo-res"]`,
688 extra: `
689 filegroup {
690 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800691 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700692 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700693 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700694 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700695 },
696 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700697 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
698 name: "wildcard dirs",
699 prop: `java_resource_dirs: ["java-res/*"]`,
700 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
701 },
702 {
703 // Test that a module exclude_java_resource_dirs excludes the files
704 name: "wildcard dirs",
705 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
706 args: "-C java-res/a -f java-res/a/a",
707 },
Colin Crosscedd4762018-09-13 11:26:19 -0700708 {
709 // Test wildcards in java_resources
710 name: "wildcard files",
711 prop: `java_resources: ["java-res/**/*"]`,
712 args: "-C . -f java-res/a/a -f java-res/b/b",
713 },
714 {
715 // Test exclude_java_resources with java_resources
716 name: "wildcard files with exclude",
717 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
718 args: "-C . -f java-res/a/a",
719 },
720 {
721 // Test exclude_java_resources with java_resource_dirs
722 name: "resource dirs with exclude files",
723 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
724 args: "-C java-res -f java-res/a/a",
725 },
726 {
727 // Test exclude_java_resource_dirs with java_resource_dirs
728 name: "resource dirs with exclude files",
729 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
730 args: "-C java-res -f java-res/a/a -f java-res/b/b",
731 },
Colin Cross0f37af02017-09-27 17:42:05 -0700732 }
733
734 for _, test := range table {
735 t.Run(test.name, func(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700736 ctx, _ := testJava(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700737 java_library {
738 name: "foo",
739 srcs: [
740 "a.java",
741 "b.java",
742 "c.java",
743 ],
744 `+test.prop+`,
745 }
746 `+test.extra)
747
Colin Cross331a1212018-08-15 20:40:52 -0700748 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700749 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700750
751 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
752 t.Errorf("foo combined jars %v does not contain %q",
753 foo.Inputs.Strings(), fooRes.Output.String())
754 }
755
Colin Crossaf9c55b2017-10-03 14:50:08 -0700756 if fooRes.Args["jarArgs"] != test.args {
757 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700758 fooRes.Args["jarArgs"], test.args)
759 }
760 })
761 }
762}
763
Colin Cross0c4ce212019-05-03 15:28:19 -0700764func TestIncludeSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700765 ctx, _ := testJava(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700766 java_library {
767 name: "foo",
768 srcs: [
769 "a.java",
770 "b.java",
771 "c.java",
772 ],
773 include_srcs: true,
774 }
775
776 java_library {
777 name: "bar",
778 srcs: [
779 "a.java",
780 "b.java",
781 "c.java",
782 ],
783 java_resource_dirs: ["java-res"],
784 include_srcs: true,
785 }
786 `)
787
788 // Test a library with include_srcs: true
789 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
790 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
791
792 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
793 t.Errorf("foo combined jars %v does not contain %q", w, g)
794 }
795
796 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
797 t.Errorf("foo source jar args %q is not %q", w, g)
798 }
799
800 // Test a library with include_srcs: true and resources
801 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
802 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
803 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
804 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
805
806 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
807 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
808 }
809
810 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
811 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
812 }
813
814 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
815 t.Errorf("bar combined jars %v does not contain %q", w, g)
816 }
817
818 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
819 t.Errorf("bar source jar args %q is not %q", w, g)
820 }
821
822 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
823 t.Errorf("bar resource jar args %q is not %q", w, g)
824 }
825}
826
Colin Cross54190b32017-10-09 15:34:10 -0700827func TestGeneratedSources(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700828 ctx, _ := testJava(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700829 java_library {
830 name: "foo",
831 srcs: [
832 "a*.java",
833 ":gen",
834 "b*.java",
835 ],
836 }
837
838 genrule {
839 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800840 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700841 out: ["gen.java"],
842 }
843 `)
844
845 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
846 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
847
Colin Cross15e86d92017-10-20 15:07:08 -0700848 if filepath.Base(genrule.Output.String()) != "gen.java" {
849 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700850 }
851
852 if len(javac.Inputs) != 3 ||
853 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700854 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700855 javac.Inputs[2].String() != "b.java" {
856 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
857 }
858}
859
Nan Zhang61eaedb2017-11-02 13:28:15 -0700860func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700861 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700862 java_library {
863 name: "foo",
864 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900865 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700866 }
867
868 java_library {
869 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800870 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700871 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900872 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700873 }
874
875 java_library {
876 name: "baz",
877 srcs: ["c.java"],
878 libs: ["bar"],
879 sdk_version: "14",
880 }
881 `)
882
883 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
884 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
885 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
886 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
887 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
888
889 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
890 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
891 }
892
893 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
894 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
895 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
896 }
897 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
898 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
899 }
900 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
901 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
902 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100903 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700904 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100905 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700906 }
907}
908
909func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700910 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700911 java_library {
912 name: "bar",
913 srcs: ["a.java","b.java","c.java"],
914 javac_shard_size: 1
915 }
916 `)
917
918 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
919 for i := 0; i < 3; i++ {
920 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
921 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
922 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
923 }
924 }
925}
926
Nan Zhang581fd212018-01-10 16:06:12 -0800927func TestDroiddoc(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700928 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +0000929 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -0800930 name: "droiddoc-templates-sdk",
931 path: ".",
932 }
Jiyong Park29074592019-07-07 16:27:47 +0900933 filegroup {
934 name: "bar-doc-aidl-srcs",
935 srcs: ["bar-doc/IBar.aidl"],
936 path: "bar-doc",
937 }
Nan Zhang581fd212018-01-10 16:06:12 -0800938 droiddoc {
939 name: "bar-doc",
940 srcs: [
941 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900942 "bar-doc/IFoo.aidl",
Jiyong Park29074592019-07-07 16:27:47 +0900943 ":bar-doc-aidl-srcs",
Nan Zhang581fd212018-01-10 16:06:12 -0800944 ],
945 exclude_srcs: [
946 "bar-doc/b.java"
947 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800948 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800949 hdf: [
950 "android.whichdoc offline",
951 ],
952 knowntags: [
953 "bar-doc/known_oj_tags.txt",
954 ],
955 proofread_file: "libcore-proofread.txt",
956 todo_file: "libcore-docs-todo.html",
957 args: "-offlinemode -title \"libcore\"",
958 }
959 `)
960
Colin Crossc0806172019-06-14 18:51:47 -0700961 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc")
Jiyong Park1e440682018-05-23 18:42:04 +0900962 var javaSrcs []string
Colin Crossc0806172019-06-14 18:51:47 -0700963 for _, i := range barDoc.Inputs {
Jiyong Park1e440682018-05-23 18:42:04 +0900964 javaSrcs = append(javaSrcs, i.Base())
965 }
Colin Crossc0806172019-06-14 18:51:47 -0700966 if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" {
967 t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs)
Jiyong Park29074592019-07-07 16:27:47 +0900968 }
969
Colin Crossc0806172019-06-14 18:51:47 -0700970 aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl")
971 if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
972 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
973 }
974
975 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
976 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +0900977 }
Nan Zhang581fd212018-01-10 16:06:12 -0800978}
979
Colin Cross54250902017-12-05 09:28:08 -0800980func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700981 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -0800982 java_library {
983 name: "foo",
984 srcs: ["a.java"],
985 }
986
987 java_genrule {
988 name: "jargen",
989 tool_files: ["b.java"],
990 cmd: "$(location b.java) $(in) $(out)",
991 out: ["jargen.jar"],
992 srcs: [":foo"],
993 }
994
995 java_library {
996 name: "bar",
997 static_libs: ["jargen"],
998 srcs: ["c.java"],
999 }
1000
1001 java_library {
1002 name: "baz",
1003 libs: ["jargen"],
1004 srcs: ["c.java"],
1005 }
1006 `)
1007
1008 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1009 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1010 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1011 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1012 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1013
1014 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1015 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1016 }
1017
1018 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1019 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1020 }
1021
1022 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1023 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1024 }
1025
1026 if len(barCombined.Inputs) != 2 ||
1027 barCombined.Inputs[0].String() != bar.Output.String() ||
1028 barCombined.Inputs[1].String() != jargen.Output.String() {
1029 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1030 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1031 }
1032}
1033
Nan Zhang27e284d2018-02-09 21:03:53 +00001034func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001035 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001036 java_library {
1037 name: "foo",
1038 srcs: ["a.java", ":foo-srcs"],
1039 exclude_srcs: ["a.java", ":foo-excludes"],
1040 }
1041
1042 filegroup {
1043 name: "foo-srcs",
1044 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1045 }
1046
1047 filegroup {
1048 name: "foo-excludes",
1049 srcs: ["java-fg/a.java", "java-fg/b.java"],
1050 }
1051 `)
1052
1053 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1054
1055 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1056 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1057 }
1058}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001059
Paul Duffin52d398a2019-06-11 12:31:14 +01001060func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001061 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001062 "libcore/Android.bp": []byte(`
1063 java_library {
1064 name: "core",
1065 sdk_version: "none",
1066 system_modules: "none",
1067 }`),
1068 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001069 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001070 run(t, ctx, config)
1071}
1072
Jiyong Parkc678ad32018-04-10 13:07:10 +09001073func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001074 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001075 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001076 name: "droiddoc-templates-sdk",
1077 path: ".",
1078 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001079 java_sdk_library {
1080 name: "foo",
1081 srcs: ["a.java", "b.java"],
1082 api_packages: ["foo"],
1083 }
1084 java_sdk_library {
1085 name: "bar",
1086 srcs: ["a.java", "b.java"],
1087 api_packages: ["bar"],
1088 }
1089 java_library {
1090 name: "baz",
1091 srcs: ["c.java"],
1092 libs: ["foo", "bar"],
1093 sdk_version: "system_current",
1094 }
Jiyong Park1be96912018-05-28 18:02:19 +09001095 java_library {
1096 name: "qux",
1097 srcs: ["c.java"],
1098 libs: ["baz"],
1099 sdk_version: "system_current",
1100 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001101 java_library {
1102 name: "baz-test",
1103 srcs: ["c.java"],
1104 libs: ["foo"],
1105 sdk_version: "test_current",
1106 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001107 `)
1108
1109 // check the existence of the internal modules
1110 ctx.ModuleForTests("foo", "android_common")
1111 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
1112 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001113 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001114 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
1115 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001116 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Colin Cross7113d202019-11-20 16:39:12 -08001117 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +09001118 ctx.ModuleForTests("foo.api.public.28", "")
1119 ctx.ModuleForTests("foo.api.system.28", "")
1120 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001121
1122 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1123 // tests if baz is actually linked to the stubs lib
1124 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1125 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1126 "foo.stubs.system.jar")
1127 }
1128 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001129 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001130 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001131 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001132 }
1133 // test if baz is not linked to the system variant of foo
1134 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1135 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1136 "foo.stubs.jar")
1137 }
Jiyong Park1be96912018-05-28 18:02:19 +09001138
Paul Duffin726d23c2020-01-22 16:30:37 +00001139 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1140 // tests if baz-test is actually linked to the test stubs lib
1141 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1142 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1143 "foo.stubs.test.jar")
1144 }
1145
Jiyong Park1be96912018-05-28 18:02:19 +09001146 // test if baz has exported SDK lib names foo and bar to qux
1147 qux := ctx.ModuleForTests("qux", "android_common")
1148 if quxLib, ok := qux.Module().(*Library); ok {
1149 sdkLibs := quxLib.ExportedSdkLibs()
1150 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
1151 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
1152 }
1153 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001154}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001155
1156var compilerFlagsTestCases = []struct {
1157 in string
1158 out bool
1159}{
1160 {
1161 in: "a",
1162 out: false,
1163 },
1164 {
1165 in: "-a",
1166 out: true,
1167 },
1168 {
1169 in: "-no-jdk",
1170 out: false,
1171 },
1172 {
1173 in: "-no-stdlib",
1174 out: false,
1175 },
1176 {
1177 in: "-kotlin-home",
1178 out: false,
1179 },
1180 {
1181 in: "-kotlin-home /some/path",
1182 out: false,
1183 },
1184 {
1185 in: "-include-runtime",
1186 out: false,
1187 },
1188 {
1189 in: "-Xintellij-plugin-root",
1190 out: false,
1191 },
1192}
1193
1194type mockContext struct {
1195 android.ModuleContext
1196 result bool
1197}
1198
1199func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1200 // CheckBadCompilerFlags calls this function when the flag should be rejected
1201 ctx.result = false
1202}
1203
1204func TestCompilerFlags(t *testing.T) {
1205 for _, testCase := range compilerFlagsTestCases {
1206 ctx := &mockContext{result: true}
1207 CheckKotlincFlags(ctx, []string{testCase.in})
1208 if ctx.result != testCase.out {
1209 t.Errorf("incorrect output:")
1210 t.Errorf(" input: %#v", testCase.in)
1211 t.Errorf(" expected: %#v", testCase.out)
1212 t.Errorf(" got: %#v", ctx.result)
1213 }
1214 }
1215}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001216
1217// TODO(jungjw): Consider making this more robust by ignoring path order.
1218func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1219 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1220 flags := strings.Split(variables["javacFlags"], " ")
1221 got := ""
1222 for _, flag := range flags {
1223 keyEnd := strings.Index(flag, "=")
1224 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1225 got = flag[keyEnd+1:]
1226 break
1227 }
1228 }
1229 if expected != got {
1230 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1231 }
1232}
1233
1234func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001235 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001236 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001237 bp := `
1238 java_library {
1239 name: "foo",
1240 srcs: ["a.java"],
1241 java_version: "1.8",
1242 }
1243
1244 java_library {
1245 name: "bar",
1246 srcs: ["b.java"],
1247 sdk_version: "none",
1248 system_modules: "none",
1249 patch_module: "java.base",
1250 java_version: "1.8",
1251 }
1252
1253 java_library {
1254 name: "baz",
1255 srcs: ["c.java"],
1256 patch_module: "java.base",
1257 java_version: "1.8",
1258 }
1259 `
1260 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001261
1262 checkPatchModuleFlag(t, ctx, "foo", "")
1263 checkPatchModuleFlag(t, ctx, "bar", "")
1264 checkPatchModuleFlag(t, ctx, "baz", "")
1265 })
1266
Pete Gillin0c2143e2019-05-02 15:32:11 +01001267 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001268 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001269 bp := `
1270 java_library {
1271 name: "foo",
1272 srcs: ["a.java"],
1273 }
1274
1275 java_library {
1276 name: "bar",
1277 srcs: ["b.java"],
1278 sdk_version: "none",
1279 system_modules: "none",
1280 patch_module: "java.base",
1281 }
1282
1283 java_library {
1284 name: "baz",
1285 srcs: ["c.java"],
1286 patch_module: "java.base",
1287 }
1288 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001289 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001290
1291 checkPatchModuleFlag(t, ctx, "foo", "")
1292 expected := "java.base=.:" + buildDir
1293 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09001294 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001295 checkPatchModuleFlag(t, ctx, "baz", expected)
1296 })
1297}
Paul Duffina7b9f422020-01-10 17:12:18 +00001298
1299func TestJavaSystemModules(t *testing.T) {
1300 ctx, _ := testJava(t, `
1301 java_system_modules {
1302 name: "system-modules",
1303 libs: ["system-module1", "system-module2"],
1304 }
1305 java_library {
1306 name: "system-module1",
1307 srcs: ["a.java"],
1308 sdk_version: "none",
1309 system_modules: "none",
1310 }
1311 java_library {
1312 name: "system-module2",
1313 srcs: ["b.java"],
1314 sdk_version: "none",
1315 system_modules: "none",
1316 }
1317 `)
1318
1319 // check the existence of the module
1320 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1321
1322 cmd := systemModules.Rule("jarsTosystemModules")
1323
1324 // make sure the command compiles against the supplied modules.
1325 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1326 if !strings.Contains(cmd.Args["classpath"], module) {
1327 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1328 module)
1329 }
1330 }
1331}
Paul Duffin90169ba2020-01-10 17:16:44 +00001332
1333func TestJavaSystemModulesImport(t *testing.T) {
1334 ctx, _ := testJava(t, `
1335 java_system_modules_import {
1336 name: "system-modules",
1337 libs: ["system-module1", "system-module2"],
1338 }
1339 java_import {
1340 name: "system-module1",
1341 jars: ["a.jar"],
1342 }
1343 java_import {
1344 name: "system-module2",
1345 jars: ["b.jar"],
1346 }
1347 `)
1348
1349 // check the existence of the module
1350 systemModules := ctx.ModuleForTests("system-modules", "android_common")
1351
1352 cmd := systemModules.Rule("jarsTosystemModules")
1353
1354 // make sure the command compiles against the supplied modules.
1355 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
1356 if !strings.Contains(cmd.Args["classpath"], module) {
1357 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
1358 module)
1359 }
1360 }
1361}