blob: a727812b5d3a1e3e0ac3da52d97775e995023456 [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Colin Cross72bb3632017-07-13 16:23:21 -070018 "io/ioutil"
19 "os"
20 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070021 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010022 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070023 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070024 "strings"
25 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070026
Jeongik Cha28df2572019-11-11 10:46:36 +090027 "github.com/google/blueprint/proptools"
28
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/android"
30 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080031 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070032 "android/soong/genrule"
Liz Kammerdd849a82020-06-12 16:38:45 -070033 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
36var buildDir string
37
38func setUp() {
39 var err error
40 buildDir, err = ioutil.TempDir("", "soong_java_test")
41 if err != nil {
42 panic(err)
43 }
44}
45
46func tearDown() {
47 os.RemoveAll(buildDir)
48}
49
50func TestMain(m *testing.M) {
51 run := func() int {
52 setUp()
53 defer tearDown()
54
55 return m.Run()
56 }
57
58 os.Exit(run())
59}
Colin Cross527012a2017-11-30 22:56:16 -080060
Colin Cross98be1bb2019-12-13 20:41:13 -080061func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Martin Stjernholmd90676f2020-01-11 00:37:30 +000062 bp += dexpreopt.BpToolModulesForTest()
63
Martin Stjernholm75a48d82020-01-10 20:32:59 +000064 config := TestConfig(buildDir, env, bp, fs)
65
66 // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that
67 // it doesn't create a real one, which would fail.
68 _ = dexpreopt.GlobalSoongConfigForTests(config)
69
70 return config
Colin Cross1369cdb2017-09-29 17:58:17 -070071}
72
Colin Cross98be1bb2019-12-13 20:41:13 -080073func testContext() *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070074
Colin Cross4c428df2017-09-15 17:36:05 -070075 ctx := android.NewTestArchContext()
Paul Duffinf9b1da02019-12-18 19:51:55 +000076 RegisterJavaBuildComponents(ctx)
77 RegisterAppBuildComponents(ctx)
78 RegisterAARBuildComponents(ctx)
Paul Duffinb0f85072019-12-19 10:28:56 +000079 RegisterGenRuleBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000080 RegisterSystemModulesBuildComponents(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080081 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080082 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
83 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070084 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Paul Duffin884363e2019-12-19 10:21:09 +000085 RegisterDocsBuildComponents(ctx)
86 RegisterStubsBuildComponents(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000087 RegisterSdkLibraryBuildComponents(ctx)
Colin Cross89536d42017-07-07 14:35:50 -070088 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010089 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000090
91 RegisterPrebuiltApisBuildComponents(ctx)
92
Liz Kammerdd849a82020-06-12 16:38:45 -070093 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070094 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080095 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -070096 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070097
Paul Duffin021f4e52020-07-30 16:04:17 +010098 android.RegisterPrebuiltMutators(ctx)
99
Colin Crossa4f08812018-10-02 22:03:40 -0700100 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +0000101 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -0700102
Martin Stjernholmd90676f2020-01-11 00:37:30 +0000103 dexpreopt.RegisterToolModulesForTest(ctx)
104
Colin Cross527012a2017-11-30 22:56:16 -0800105 return ctx
106}
107
108func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800109 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800110
Colin Cross98be1bb2019-12-13 20:41:13 -0800111 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000112 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -0800113
Colin Cross98be1bb2019-12-13 20:41:13 -0800114 ctx.Register(config)
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100115 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800116 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700117 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800118 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800119}
120
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900121func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900122 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800123 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900124}
125
Colin Cross98be1bb2019-12-13 20:41:13 -0800126func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900127 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800128 ctx := testContext()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900129
Colin Cross98be1bb2019-12-13 20:41:13 -0800130 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000131 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900132
Colin Cross98be1bb2019-12-13 20:41:13 -0800133 ctx.Register(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900134 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
135 if len(errs) > 0 {
136 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900137 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900138 }
139 _, errs = ctx.PrepareBuildActions(config)
140 if len(errs) > 0 {
141 android.FailIfNoMatchingErrors(t, pattern, errs)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900142 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900143 }
144
145 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900146
147 return ctx, config
Jeongik Cha538c0d02019-07-11 15:54:27 +0900148}
149
Colin Cross238c1f32020-06-07 16:58:18 -0700150func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
151 t.Helper()
152 return testJavaWithConfig(t, testConfig(nil, bp, fs))
153}
154
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700155func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800156 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700157 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900158}
159
Colin Cross98be1bb2019-12-13 20:41:13 -0800160func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900161 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800162 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -0800163 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700164
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700165 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700166}
167
Colin Cross2acdae82017-09-15 19:44:24 -0700168func moduleToPath(name string) string {
169 switch {
170 case name == `""`:
171 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700172 case strings.HasSuffix(name, ".jar"):
173 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700174 default:
175 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700176 }
177}
178
Jeongik Chae403e9e2019-12-07 00:16:24 +0900179func TestJavaLinkType(t *testing.T) {
180 testJava(t, `
181 java_library {
182 name: "foo",
183 srcs: ["a.java"],
184 libs: ["bar"],
185 static_libs: ["baz"],
186 }
187
188 java_library {
189 name: "bar",
190 sdk_version: "current",
191 srcs: ["b.java"],
192 }
193
194 java_library {
195 name: "baz",
196 sdk_version: "system_current",
197 srcs: ["c.java"],
198 }
199 `)
200
201 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.", `
202 java_library {
203 name: "foo",
204 srcs: ["a.java"],
205 libs: ["bar"],
206 sdk_version: "current",
207 static_libs: ["baz"],
208 }
209
210 java_library {
211 name: "bar",
212 sdk_version: "current",
213 srcs: ["b.java"],
214 }
215
216 java_library {
217 name: "baz",
218 sdk_version: "system_current",
219 srcs: ["c.java"],
220 }
221 `)
222
223 testJava(t, `
224 java_library {
225 name: "foo",
226 srcs: ["a.java"],
227 libs: ["bar"],
228 sdk_version: "system_current",
229 static_libs: ["baz"],
230 }
231
232 java_library {
233 name: "bar",
234 sdk_version: "current",
235 srcs: ["b.java"],
236 }
237
238 java_library {
239 name: "baz",
240 sdk_version: "system_current",
241 srcs: ["c.java"],
242 }
243 `)
244
245 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.", `
246 java_library {
247 name: "foo",
248 srcs: ["a.java"],
249 libs: ["bar"],
250 sdk_version: "system_current",
251 static_libs: ["baz"],
252 }
253
254 java_library {
255 name: "bar",
256 sdk_version: "current",
257 srcs: ["b.java"],
258 }
259
260 java_library {
261 name: "baz",
262 srcs: ["c.java"],
263 }
264 `)
265}
266
Colin Cross72bb3632017-07-13 16:23:21 -0700267func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700268 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700269 java_library {
270 name: "foo",
271 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700272 libs: ["bar"],
273 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700274 }
275
276 java_library {
277 name: "bar",
278 srcs: ["b.java"],
279 }
280
281 java_library {
282 name: "baz",
283 srcs: ["c.java"],
284 }
Colin Crossd5934c82017-10-02 13:55:26 -0700285 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700286
Colin Cross4c428df2017-09-15 17:36:05 -0700287 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700288 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700289
290 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
291 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
292 }
293
Colin Cross1ee23172017-10-18 14:44:18 -0700294 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700295 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
296 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700297
Nan Zhanged19fc32017-10-19 13:06:22 -0700298 if !strings.Contains(javac.Args["classpath"], barTurbine) {
299 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700300 }
301
Nan Zhanged19fc32017-10-19 13:06:22 -0700302 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
303 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700304 }
305
306 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
307 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700308 }
309}
310
Artur Satayev9cf46692019-11-26 18:08:34 +0000311func TestExportedPlugins(t *testing.T) {
312 type Result struct {
313 library string
314 processors string
315 }
316 var tests = []struct {
317 name string
318 extra string
319 results []Result
320 }{
321 {
322 name: "Exported plugin is not a direct plugin",
323 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
324 results: []Result{{library: "exports", processors: "-proc:none"}},
325 },
326 {
327 name: "Exports plugin to dependee",
328 extra: `
329 java_library{name: "exports", exported_plugins: ["plugin"]}
330 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
331 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
332 `,
333 results: []Result{
334 {library: "foo", processors: "-processor com.android.TestPlugin"},
335 {library: "bar", processors: "-processor com.android.TestPlugin"},
336 },
337 },
338 {
339 name: "Exports plugin to android_library",
340 extra: `
341 java_library{name: "exports", exported_plugins: ["plugin"]}
342 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
343 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
344 `,
345 results: []Result{
346 {library: "foo", processors: "-processor com.android.TestPlugin"},
347 {library: "bar", processors: "-processor com.android.TestPlugin"},
348 },
349 },
350 {
351 name: "Exports plugin is not propagated via transitive deps",
352 extra: `
353 java_library{name: "exports", exported_plugins: ["plugin"]}
354 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
355 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
356 `,
357 results: []Result{
358 {library: "foo", processors: "-processor com.android.TestPlugin"},
359 {library: "bar", processors: "-proc:none"},
360 },
361 },
362 {
363 name: "Exports plugin appends to plugins",
364 extra: `
365 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
366 java_library{name: "exports", exported_plugins: ["plugin"]}
367 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
368 `,
369 results: []Result{
370 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
371 },
372 },
373 }
374
375 for _, test := range tests {
376 t.Run(test.name, func(t *testing.T) {
377 ctx, _ := testJava(t, `
378 java_plugin {
379 name: "plugin",
380 processor_class: "com.android.TestPlugin",
381 }
382 `+test.extra)
383
384 for _, want := range test.results {
385 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
386 if javac.Args["processor"] != want.processors {
387 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
388 }
389 }
390 })
391 }
392}
393
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900394func TestSdkVersionByPartition(t *testing.T) {
395 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900396 java_library {
397 name: "foo",
398 srcs: ["a.java"],
399 vendor: true,
400 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900401 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900402
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900403 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900404 java_library {
405 name: "bar",
406 srcs: ["b.java"],
407 }
408 `)
409
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900410 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900411 bp := `
412 java_library {
413 name: "foo",
414 srcs: ["a.java"],
415 product_specific: true,
416 }
417 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800418
419 config := testConfig(nil, bp, nil)
420 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900421 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800422 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 +0900423 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800424 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900425 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900426 }
427}
428
Colin Crossd5934c82017-10-02 13:55:26 -0700429func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700430 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700431 java_library {
432 name: "foo",
433 srcs: ["a.java"],
434 target: {
435 android: {
436 srcs: ["b.java"],
437 },
438 },
439 }
440 `)
441
442 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
443 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
444 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
445 }
446}
447
Colin Cross6b4a32d2017-12-05 13:42:45 -0800448func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700449 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800450 java_library_host {
451 name: "foo",
452 srcs: ["a.java"],
453 }
454
455 java_binary_host {
456 name: "bar",
457 srcs: ["b.java"],
458 static_libs: ["foo"],
459 }
460 `)
461
462 buildOS := android.BuildOs.String()
463
464 bar := ctx.ModuleForTests("bar", buildOS+"_common")
465 barJar := bar.Output("bar.jar").Output.String()
466 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
467 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
468
469 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700470 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
471 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800472 }
Alex Humesky2070e322020-06-09 20:23:08 -0400473}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800474
Alex Humesky2070e322020-06-09 20:23:08 -0400475func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
476 bp := `
477 java_library {
478 name: "target_library",
479 srcs: ["a.java"],
480 }
481
482 java_binary_host {
483 name: "host_binary",
484 srcs: ["b.java"],
485 }
486 `
487 config := testConfig(nil, bp, nil)
488 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
489
490 ctx, _ := testJavaWithConfig(t, config)
491
Liz Kammer7941b302020-07-28 13:27:34 -0700492 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400493 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
494 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
495 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
496 t.Errorf("target library javac flags %v should contain "+
497 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
498 }
499
500 // check that -g is not overridden for host modules
501 buildOS := android.BuildOs.String()
502 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
503 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
504 if strings.Contains(hostJavaFlags, "-g:source,lines") {
505 t.Errorf("java_binary_host javac flags %v should not have "+
506 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
507 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800508}
509
Colin Cross72bb3632017-07-13 16:23:21 -0700510func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700511 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700512 java_library {
513 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000514 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000515 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700516 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700517 }
518
Colin Cross74d73e22017-08-02 11:05:49 -0700519 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700520 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700521 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700522 }
523
Colin Cross74d73e22017-08-02 11:05:49 -0700524 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700525 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700526 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700527 sdk_version: "current",
528 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700529 }
Colin Cross42be7612019-02-21 18:12:14 -0800530
531 dex_import {
532 name: "qux",
533 jars: ["b.jar"],
534 }
Colin Cross79c7c262019-04-17 11:11:46 -0700535
536 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000537 name: "sdklib",
538 public: {
539 jars: ["c.jar"],
540 },
541 }
542
Paul Duffin91547182019-11-12 19:39:36 +0000543 prebuilt_stubs_sources {
544 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000545 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000546 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000547
548 java_test_import {
549 name: "test",
550 jars: ["a.jar"],
551 test_suites: ["cts"],
552 test_config: "AndroidTest.xml",
553 }
Colin Cross72bb3632017-07-13 16:23:21 -0700554 `)
555
Paul Duffin9b478b02019-12-10 13:41:51 +0000556 fooModule := ctx.ModuleForTests("foo", "android_common")
557 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700558 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700559 barModule := ctx.ModuleForTests("bar", "android_common")
560 barJar := barModule.Rule("combineJar").Output
561 bazModule := ctx.ModuleForTests("baz", "android_common")
562 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700563 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700564
Paul Duffin9b478b02019-12-10 13:41:51 +0000565 fooLibrary := fooModule.Module().(*Library)
566 assertDeepEquals(t, "foo java sources incorrect",
567 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000568
Paul Duffin9b478b02019-12-10 13:41:51 +0000569 assertDeepEquals(t, "foo java source jars incorrect",
570 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
571 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000572
Colin Cross37f6d792018-07-12 12:28:41 -0700573 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
574 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700575 }
576
Liz Kammerd6c31d22020-08-05 15:40:41 -0700577 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
578 if barDexJar != nil {
579 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
580 }
581
Colin Cross79c7c262019-04-17 11:11:46 -0700582 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
583 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
584 }
585
Colin Cross37f6d792018-07-12 12:28:41 -0700586 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
587 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700588 }
Colin Cross42be7612019-02-21 18:12:14 -0800589
Liz Kammerd6c31d22020-08-05 15:40:41 -0700590 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
591 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
592 if bazDexJar != expectedDexJar {
593 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
594 }
595
Colin Cross42be7612019-02-21 18:12:14 -0800596 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700597}
598
Paul Duffin9b478b02019-12-10 13:41:51 +0000599func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
600 if !reflect.DeepEqual(expected, actual) {
601 t.Errorf("%s: expected %q, found %q", message, expected, actual)
602 }
603}
604
Paul Duffin56d44902020-01-31 13:36:25 +0000605func TestJavaSdkLibraryImport(t *testing.T) {
606 ctx, _ := testJava(t, `
607 java_library {
608 name: "foo",
609 srcs: ["a.java"],
610 libs: ["sdklib"],
611 sdk_version: "current",
612 }
613
614 java_library {
615 name: "foo.system",
616 srcs: ["a.java"],
617 libs: ["sdklib"],
618 sdk_version: "system_current",
619 }
620
621 java_library {
622 name: "foo.test",
623 srcs: ["a.java"],
624 libs: ["sdklib"],
625 sdk_version: "test_current",
626 }
627
628 java_sdk_library_import {
629 name: "sdklib",
630 public: {
631 jars: ["a.jar"],
632 },
633 system: {
634 jars: ["b.jar"],
635 },
636 test: {
637 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100638 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000639 },
640 }
641 `)
642
643 for _, scope := range []string{"", ".system", ".test"} {
644 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
645 javac := fooModule.Rule("javac")
646
647 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
648 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
649 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
650 }
651 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100652
Paul Duffincee7e662020-07-09 17:32:57 +0100653 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100654 `prebuilt_sdklib.stubs`,
655 `prebuilt_sdklib.stubs.source.test`,
656 `prebuilt_sdklib.stubs.system`,
657 `prebuilt_sdklib.stubs.test`,
658 })
659}
660
661func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
662 ctx, _ := testJava(t, `
663 java_sdk_library {
664 name: "sdklib",
665 srcs: ["a.java"],
666 sdk_version: "none",
667 system_modules: "none",
668 public: {
669 enabled: true,
670 },
671 }
672
673 java_sdk_library_import {
674 name: "sdklib",
675 public: {
676 jars: ["a.jar"],
677 },
678 }
679 `)
680
Paul Duffincee7e662020-07-09 17:32:57 +0100681 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100682 `dex2oatd`,
683 `prebuilt_sdklib`,
684 `sdklib.impl`,
685 `sdklib.stubs`,
686 `sdklib.stubs.source`,
687 `sdklib.xml`,
688 })
689
Paul Duffincee7e662020-07-09 17:32:57 +0100690 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100691 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100692 `sdklib.impl`,
693 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
694 // dependency is added after prebuilts may have been renamed and so has to use
695 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100696 `sdklib.xml`,
697 })
698}
699
700func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
701 ctx, _ := testJava(t, `
702 java_sdk_library {
703 name: "sdklib",
704 srcs: ["a.java"],
705 sdk_version: "none",
706 system_modules: "none",
707 public: {
708 enabled: true,
709 },
710 }
711
712 java_sdk_library_import {
713 name: "sdklib",
714 prefer: true,
715 public: {
716 jars: ["a.jar"],
717 },
718 }
719 `)
720
Paul Duffincee7e662020-07-09 17:32:57 +0100721 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100722 `dex2oatd`,
723 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100724 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100725 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100726 `sdklib.stubs.source`,
727 `sdklib.xml`,
728 })
729
Paul Duffincee7e662020-07-09 17:32:57 +0100730 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100731 `prebuilt_sdklib.stubs`,
732 `sdklib.impl`,
733 `sdklib.xml`,
734 })
Paul Duffin56d44902020-01-31 13:36:25 +0000735}
736
Colin Cross89536d42017-07-07 14:35:50 -0700737func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700738 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700739 java_defaults {
740 name: "defaults",
741 srcs: ["a.java"],
742 libs: ["bar"],
743 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700744 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700745 }
746
747 java_library {
748 name: "foo",
749 defaults: ["defaults"],
750 }
751
752 java_library {
753 name: "bar",
754 srcs: ["b.java"],
755 }
756
757 java_library {
758 name: "baz",
759 srcs: ["c.java"],
760 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700761
762 android_test {
763 name: "atestOptimize",
764 defaults: ["defaults"],
765 optimize: {enabled: true},
766 }
767
768 android_test {
769 name: "atestNoOptimize",
770 defaults: ["defaults"],
771 }
772
773 android_test {
774 name: "atestDefault",
775 srcs: ["a.java"],
776 }
Colin Cross89536d42017-07-07 14:35:50 -0700777 `)
778
Colin Cross4c428df2017-09-15 17:36:05 -0700779 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700780 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700781
782 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
783 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
784 }
785
Nan Zhanged19fc32017-10-19 13:06:22 -0700786 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
787 if !strings.Contains(javac.Args["classpath"], barTurbine) {
788 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700789 }
790
Colin Cross1ee23172017-10-18 14:44:18 -0700791 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700792 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
793 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700794 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700795
796 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
797 if atestOptimize.Output == nil {
798 t.Errorf("atestOptimize should optimize APK")
799 }
800
801 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
802 if atestNoOptimize.Output == nil {
803 t.Errorf("atestNoOptimize should not optimize APK")
804 }
805
806 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
807 if atestDefault.Output == nil {
808 t.Errorf("atestDefault should optimize APK")
809 }
Colin Cross89536d42017-07-07 14:35:50 -0700810}
811
Colin Cross0f37af02017-09-27 17:42:05 -0700812func TestResources(t *testing.T) {
813 var table = []struct {
814 name string
815 prop string
816 extra string
817 args string
818 }{
819 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700820 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700821 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800822 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700823 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700824 },
825 {
826 // Test that a module with java_resources includes the files
827 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700828 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
829 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700830 },
831 {
832 // Test that a module with a filegroup in java_resources includes the files with the
833 // path prefix
834 name: "resource filegroup",
835 prop: `java_resources: [":foo-res"]`,
836 extra: `
837 filegroup {
838 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800839 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700840 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700841 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700842 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700843 },
844 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700845 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
846 name: "wildcard dirs",
847 prop: `java_resource_dirs: ["java-res/*"]`,
848 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
849 },
850 {
851 // Test that a module exclude_java_resource_dirs excludes the files
852 name: "wildcard dirs",
853 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
854 args: "-C java-res/a -f java-res/a/a",
855 },
Colin Crosscedd4762018-09-13 11:26:19 -0700856 {
857 // Test wildcards in java_resources
858 name: "wildcard files",
859 prop: `java_resources: ["java-res/**/*"]`,
860 args: "-C . -f java-res/a/a -f java-res/b/b",
861 },
862 {
863 // Test exclude_java_resources with java_resources
864 name: "wildcard files with exclude",
865 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
866 args: "-C . -f java-res/a/a",
867 },
868 {
869 // Test exclude_java_resources with java_resource_dirs
870 name: "resource dirs with exclude files",
871 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
872 args: "-C java-res -f java-res/a/a",
873 },
874 {
875 // Test exclude_java_resource_dirs with java_resource_dirs
876 name: "resource dirs with exclude files",
877 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
878 args: "-C java-res -f java-res/a/a -f java-res/b/b",
879 },
Colin Cross0f37af02017-09-27 17:42:05 -0700880 }
881
882 for _, test := range table {
883 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700884 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700885 java_library {
886 name: "foo",
887 srcs: [
888 "a.java",
889 "b.java",
890 "c.java",
891 ],
892 `+test.prop+`,
893 }
Colin Cross238c1f32020-06-07 16:58:18 -0700894 `+test.extra,
895 map[string][]byte{
896 "java-res/a/a": nil,
897 "java-res/b/b": nil,
898 "java-res2/a": nil,
899 },
900 )
Colin Cross0f37af02017-09-27 17:42:05 -0700901
Colin Cross331a1212018-08-15 20:40:52 -0700902 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700903 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700904
905 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
906 t.Errorf("foo combined jars %v does not contain %q",
907 foo.Inputs.Strings(), fooRes.Output.String())
908 }
909
Colin Crossaf9c55b2017-10-03 14:50:08 -0700910 if fooRes.Args["jarArgs"] != test.args {
911 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700912 fooRes.Args["jarArgs"], test.args)
913 }
914 })
915 }
916}
917
Colin Cross0c4ce212019-05-03 15:28:19 -0700918func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700919 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700920 java_library {
921 name: "foo",
922 srcs: [
923 "a.java",
924 "b.java",
925 "c.java",
926 ],
927 include_srcs: true,
928 }
929
930 java_library {
931 name: "bar",
932 srcs: [
933 "a.java",
934 "b.java",
935 "c.java",
936 ],
937 java_resource_dirs: ["java-res"],
938 include_srcs: true,
939 }
Colin Cross238c1f32020-06-07 16:58:18 -0700940 `, map[string][]byte{
941 "java-res/a/a": nil,
942 "java-res/b/b": nil,
943 "java-res2/a": nil,
944 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700945
946 // Test a library with include_srcs: true
947 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
948 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
949
950 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
951 t.Errorf("foo combined jars %v does not contain %q", w, g)
952 }
953
954 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
955 t.Errorf("foo source jar args %q is not %q", w, g)
956 }
957
958 // Test a library with include_srcs: true and resources
959 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
960 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
961 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
962 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
963
964 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
965 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
966 }
967
968 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
969 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
970 }
971
972 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
973 t.Errorf("bar combined jars %v does not contain %q", w, g)
974 }
975
976 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
977 t.Errorf("bar source jar args %q is not %q", w, g)
978 }
979
980 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
981 t.Errorf("bar resource jar args %q is not %q", w, g)
982 }
983}
984
Colin Cross54190b32017-10-09 15:34:10 -0700985func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700986 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700987 java_library {
988 name: "foo",
989 srcs: [
990 "a*.java",
991 ":gen",
992 "b*.java",
993 ],
994 }
995
996 genrule {
997 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800998 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700999 out: ["gen.java"],
1000 }
Colin Cross238c1f32020-06-07 16:58:18 -07001001 `, map[string][]byte{
1002 "a.java": nil,
1003 "b.java": nil,
1004 })
Colin Cross54190b32017-10-09 15:34:10 -07001005
1006 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1007 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1008
Colin Cross15e86d92017-10-20 15:07:08 -07001009 if filepath.Base(genrule.Output.String()) != "gen.java" {
1010 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001011 }
1012
1013 if len(javac.Inputs) != 3 ||
1014 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001015 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001016 javac.Inputs[2].String() != "b.java" {
1017 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1018 }
1019}
1020
Nan Zhang61eaedb2017-11-02 13:28:15 -07001021func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001022 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001023 java_library {
1024 name: "foo",
1025 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001026 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001027 }
1028
1029 java_library {
1030 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001031 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001032 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001033 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001034 }
1035
1036 java_library {
1037 name: "baz",
1038 srcs: ["c.java"],
1039 libs: ["bar"],
1040 sdk_version: "14",
1041 }
1042 `)
1043
1044 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1045 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1046 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1047 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1048 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1049
1050 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1051 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1052 }
1053
1054 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1055 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1056 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1057 }
1058 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1059 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1060 }
1061 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1062 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1063 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001064 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001065 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001066 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001067 }
1068}
1069
1070func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001071 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001072 java_library {
1073 name: "bar",
1074 srcs: ["a.java","b.java","c.java"],
1075 javac_shard_size: 1
1076 }
1077 `)
1078
1079 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1080 for i := 0; i < 3; i++ {
1081 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1082 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1083 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1084 }
1085 }
1086}
1087
Nan Zhang581fd212018-01-10 16:06:12 -08001088func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001089 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001090 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001091 name: "droiddoc-templates-sdk",
1092 path: ".",
1093 }
Jiyong Park29074592019-07-07 16:27:47 +09001094 filegroup {
1095 name: "bar-doc-aidl-srcs",
1096 srcs: ["bar-doc/IBar.aidl"],
1097 path: "bar-doc",
1098 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001099 droidstubs {
1100 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001101 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001102 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001103 ],
Steve Kim3666c702020-09-01 17:58:01 +00001104 exclude_srcs: [
1105 "bar-doc/b.java"
1106 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001107 api_levels_annotations_dirs: [
1108 "droiddoc-templates-sdk",
1109 ],
1110 api_levels_annotations_enabled: true,
1111 }
1112 droiddoc {
1113 name: "bar-doc",
1114 srcs: [
1115 ":bar-stubs",
1116 "bar-doc/IFoo.aidl",
1117 ":bar-doc-aidl-srcs",
1118 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001119 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001120 hdf: [
1121 "android.whichdoc offline",
1122 ],
1123 knowntags: [
1124 "bar-doc/known_oj_tags.txt",
1125 ],
1126 proofread_file: "libcore-proofread.txt",
1127 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001128 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001129 }
Colin Cross238c1f32020-06-07 16:58:18 -07001130 `,
1131 map[string][]byte{
1132 "bar-doc/a.java": nil,
1133 "bar-doc/b.java": nil,
1134 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001135 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1136 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1137 if err != nil {
1138 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1139 }
1140 if len(barStubsOutputs) != 1 {
1141 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001142 }
Nan Zhang581fd212018-01-10 16:06:12 -08001143
Liz Kammere1ab2502020-09-10 15:29:25 +00001144 barStubsOutput := barStubsOutputs[0]
1145 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1146 javaDoc := barDoc.Rule("javadoc")
1147 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1148 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001149 }
1150
Liz Kammere1ab2502020-09-10 15:29:25 +00001151 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1152 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1153 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1154 }
1155
1156 aidl := barDoc.Rule("aidl")
1157 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001158 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1159 }
1160
1161 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1162 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001163 }
Nan Zhang581fd212018-01-10 16:06:12 -08001164}
1165
Liz Kammer585cac22020-07-06 09:12:57 -07001166func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1167 testJavaError(t, "flags is set. Cannot set args", `
1168 droiddoc_exported_dir {
1169 name: "droiddoc-templates-sdk",
1170 path: ".",
1171 }
1172 filegroup {
1173 name: "bar-doc-aidl-srcs",
1174 srcs: ["bar-doc/IBar.aidl"],
1175 path: "bar-doc",
1176 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001177 droidstubs {
1178 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001179 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001180 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001181 ],
Steve Kim3666c702020-09-01 17:58:01 +00001182 exclude_srcs: [
1183 "bar-doc/b.java"
1184 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001185 api_levels_annotations_dirs: [
1186 "droiddoc-templates-sdk",
1187 ],
1188 api_levels_annotations_enabled: true,
1189 }
1190 droiddoc {
1191 name: "bar-doc",
1192 srcs: [
1193 ":bar-stubs",
1194 "bar-doc/IFoo.aidl",
1195 ":bar-doc-aidl-srcs",
1196 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001197 custom_template: "droiddoc-templates-sdk",
1198 hdf: [
1199 "android.whichdoc offline",
1200 ],
1201 knowntags: [
1202 "bar-doc/known_oj_tags.txt",
1203 ],
1204 proofread_file: "libcore-proofread.txt",
1205 todo_file: "libcore-docs-todo.html",
1206 flags: ["-offlinemode -title \"libcore\""],
1207 args: "-offlinemode -title \"libcore\"",
1208 }
1209 `)
1210}
1211
Liz Kammer3d894b72020-08-04 09:55:13 -07001212func TestDroidstubs(t *testing.T) {
1213 ctx, _ := testJavaWithFS(t, `
1214 droiddoc_exported_dir {
1215 name: "droiddoc-templates-sdk",
1216 path: ".",
1217 }
1218
1219 droidstubs {
1220 name: "bar-stubs",
1221 srcs: [
1222 "bar-doc/a.java",
1223 ],
1224 api_levels_annotations_dirs: [
1225 "droiddoc-templates-sdk",
1226 ],
1227 api_levels_annotations_enabled: true,
1228 }
1229
1230 droidstubs {
1231 name: "bar-stubs-other",
1232 srcs: [
1233 "bar-doc/a.java",
1234 ],
1235 api_levels_annotations_dirs: [
1236 "droiddoc-templates-sdk",
1237 ],
1238 api_levels_annotations_enabled: true,
1239 api_levels_jar_filename: "android.other.jar",
1240 }
1241 `,
1242 map[string][]byte{
1243 "bar-doc/a.java": nil,
1244 })
1245 testcases := []struct {
1246 moduleName string
1247 expectedJarFilename string
1248 }{
1249 {
1250 moduleName: "bar-stubs",
1251 expectedJarFilename: "android.jar",
1252 },
1253 {
1254 moduleName: "bar-stubs-other",
1255 expectedJarFilename: "android.other.jar",
1256 },
1257 }
1258 for _, c := range testcases {
1259 m := ctx.ModuleForTests(c.moduleName, "android_common")
1260 metalava := m.Rule("metalava")
1261 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
1262 if actual := metalava.RuleParams.Command; !strings.Contains(actual, expected) {
1263 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1264 }
1265 }
1266}
1267
Paul Duffin83a2d962019-11-19 19:44:10 +00001268func TestDroidstubsWithSystemModules(t *testing.T) {
1269 ctx, _ := testJava(t, `
1270 droidstubs {
1271 name: "stubs-source-system-modules",
1272 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001273 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001274 ],
1275 sdk_version: "none",
1276 system_modules: "source-system-modules",
1277 }
1278
1279 java_library {
1280 name: "source-jar",
1281 srcs: [
1282 "a.java",
1283 ],
1284 }
1285
1286 java_system_modules {
1287 name: "source-system-modules",
1288 libs: ["source-jar"],
1289 }
1290
1291 droidstubs {
1292 name: "stubs-prebuilt-system-modules",
1293 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001294 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001295 ],
1296 sdk_version: "none",
1297 system_modules: "prebuilt-system-modules",
1298 }
1299
1300 java_import {
1301 name: "prebuilt-jar",
1302 jars: ["a.jar"],
1303 }
1304
1305 java_system_modules_import {
1306 name: "prebuilt-system-modules",
1307 libs: ["prebuilt-jar"],
1308 }
1309 `)
1310
1311 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1312
1313 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1314}
1315
1316func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1317 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1318 var systemJars []string
1319 for _, i := range metalavaRule.Implicits {
1320 systemJars = append(systemJars, i.Base())
1321 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001322 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001323 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1324 }
1325}
1326
Colin Cross54250902017-12-05 09:28:08 -08001327func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001328 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001329 java_library {
1330 name: "foo",
1331 srcs: ["a.java"],
1332 }
1333
1334 java_genrule {
1335 name: "jargen",
1336 tool_files: ["b.java"],
1337 cmd: "$(location b.java) $(in) $(out)",
1338 out: ["jargen.jar"],
1339 srcs: [":foo"],
1340 }
1341
1342 java_library {
1343 name: "bar",
1344 static_libs: ["jargen"],
1345 srcs: ["c.java"],
1346 }
1347
1348 java_library {
1349 name: "baz",
1350 libs: ["jargen"],
1351 srcs: ["c.java"],
1352 }
1353 `)
1354
1355 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1356 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1357 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1358 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1359 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1360
1361 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1362 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1363 }
1364
1365 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1366 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1367 }
1368
1369 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1370 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1371 }
1372
1373 if len(barCombined.Inputs) != 2 ||
1374 barCombined.Inputs[0].String() != bar.Output.String() ||
1375 barCombined.Inputs[1].String() != jargen.Output.String() {
1376 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1377 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1378 }
1379}
1380
Nan Zhang27e284d2018-02-09 21:03:53 +00001381func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001382 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001383 java_library {
1384 name: "foo",
1385 srcs: ["a.java", ":foo-srcs"],
1386 exclude_srcs: ["a.java", ":foo-excludes"],
1387 }
1388
1389 filegroup {
1390 name: "foo-srcs",
1391 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1392 }
1393
1394 filegroup {
1395 name: "foo-excludes",
1396 srcs: ["java-fg/a.java", "java-fg/b.java"],
1397 }
1398 `)
1399
1400 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1401
1402 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1403 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1404 }
1405}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001406
Paul Duffin52d398a2019-06-11 12:31:14 +01001407func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001408 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001409 "libcore/Android.bp": []byte(`
1410 java_library {
1411 name: "core",
1412 sdk_version: "none",
1413 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001414 }
1415
1416 filegroup {
1417 name: "core-jar",
1418 srcs: [":core{.jar}"],
1419 }
1420`),
1421 })
1422 ctx := testContext()
1423 run(t, ctx, config)
1424}
1425
1426func TestJavaImport(t *testing.T) {
1427 config := testConfig(nil, "", map[string][]byte{
1428 "libcore/Android.bp": []byte(`
1429 java_import {
1430 name: "core",
1431 sdk_version: "none",
1432 }
1433
1434 filegroup {
1435 name: "core-jar",
1436 srcs: [":core{.jar}"],
1437 }
1438`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001439 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001440 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001441 run(t, ctx, config)
1442}
1443
Jiyong Parkc678ad32018-04-10 13:07:10 +09001444func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001445 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001446 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001447 name: "droiddoc-templates-sdk",
1448 path: ".",
1449 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001450 java_sdk_library {
1451 name: "foo",
1452 srcs: ["a.java", "b.java"],
1453 api_packages: ["foo"],
1454 }
1455 java_sdk_library {
1456 name: "bar",
1457 srcs: ["a.java", "b.java"],
1458 api_packages: ["bar"],
1459 }
1460 java_library {
1461 name: "baz",
1462 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001463 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001464 sdk_version: "system_current",
1465 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001466 java_sdk_library {
1467 name: "barney",
1468 srcs: ["c.java"],
1469 api_only: true,
1470 }
1471 java_sdk_library {
1472 name: "betty",
1473 srcs: ["c.java"],
1474 shared_library: false,
1475 }
Paul Duffin859fe962020-05-15 10:20:31 +01001476 java_sdk_library_import {
1477 name: "quuz",
1478 public: {
1479 jars: ["c.jar"],
1480 },
1481 }
1482 java_sdk_library_import {
1483 name: "fred",
1484 public: {
1485 jars: ["b.jar"],
1486 },
1487 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001488 java_sdk_library_import {
1489 name: "wilma",
1490 public: {
1491 jars: ["b.jar"],
1492 },
1493 shared_library: false,
1494 }
Jiyong Park1be96912018-05-28 18:02:19 +09001495 java_library {
1496 name: "qux",
1497 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001498 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001499 sdk_version: "system_current",
1500 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001501 java_library {
1502 name: "baz-test",
1503 srcs: ["c.java"],
1504 libs: ["foo"],
1505 sdk_version: "test_current",
1506 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001507 java_library {
1508 name: "baz-29",
1509 srcs: ["c.java"],
1510 libs: ["foo"],
1511 sdk_version: "system_29",
1512 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001513 java_library {
1514 name: "baz-module-30",
1515 srcs: ["c.java"],
1516 libs: ["foo"],
1517 sdk_version: "module_30",
1518 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001519 `)
1520
1521 // check the existence of the internal modules
1522 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001523 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1524 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1525 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1526 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1527 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1528 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001529 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001530 ctx.ModuleForTests("foo.api.public.28", "")
1531 ctx.ModuleForTests("foo.api.system.28", "")
1532 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001533
1534 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1535 // tests if baz is actually linked to the stubs lib
1536 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1537 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1538 "foo.stubs.system.jar")
1539 }
1540 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001541 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001542 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001543 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001544 }
1545 // test if baz is not linked to the system variant of foo
1546 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1547 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1548 "foo.stubs.jar")
1549 }
Jiyong Park1be96912018-05-28 18:02:19 +09001550
Paul Duffin726d23c2020-01-22 16:30:37 +00001551 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1552 // tests if baz-test is actually linked to the test stubs lib
1553 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1554 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1555 "foo.stubs.test.jar")
1556 }
1557
Paul Duffina2db18f2020-01-22 17:11:15 +00001558 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1559 // tests if baz-29 is actually linked to the system 29 stubs lib
1560 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1561 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1562 "prebuilts/sdk/29/system/foo.jar")
1563 }
1564
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001565 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1566 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1567 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1568 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1569 "prebuilts/sdk/30/module-lib/foo.jar")
1570 }
1571
Jiyong Park1be96912018-05-28 18:02:19 +09001572 // test if baz has exported SDK lib names foo and bar to qux
1573 qux := ctx.ModuleForTests("qux", "android_common")
1574 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb8063c62020-08-20 11:33:12 +01001575 sdkLibs := android.SortedStringKeys(quxLib.ExportedSdkLibs())
Paul Duffin859fe962020-05-15 10:20:31 +01001576 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1577 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001578 }
1579 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001580}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001581
Paul Duffindaaa3322020-05-26 18:13:57 +01001582func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1583 ctx, _ := testJava(t, `
1584 java_sdk_library {
1585 name: "foo",
1586 srcs: ["a.java"],
1587 api_only: true,
1588 public: {
1589 enabled: true,
1590 },
1591 }
1592
1593 java_library {
1594 name: "bar",
1595 srcs: ["b.java"],
1596 libs: ["foo"],
1597 }
1598 `)
1599
1600 // The bar library should depend on the stubs jar.
1601 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1602 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1603 t.Errorf("expected %q, found %#q", expected, actual)
1604 }
1605}
1606
Paul Duffin46dc45a2020-05-14 15:39:10 +01001607func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1608 testJava(t, `
1609 java_sdk_library {
1610 name: "foo",
1611 srcs: ["a.java"],
1612 api_packages: ["foo"],
1613 public: {
1614 enabled: true,
1615 },
1616 }
1617
1618 java_library {
1619 name: "bar",
1620 srcs: ["b.java", ":foo{.public.stubs.source}"],
1621 }
1622 `)
1623}
1624
1625func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1626 testJavaError(t, `"foo" does not provide api scope system`, `
1627 java_sdk_library {
1628 name: "foo",
1629 srcs: ["a.java"],
1630 api_packages: ["foo"],
1631 public: {
1632 enabled: true,
1633 },
1634 }
1635
1636 java_library {
1637 name: "bar",
1638 srcs: ["b.java", ":foo{.system.stubs.source}"],
1639 }
1640 `)
1641}
1642
Paul Duffinca8d9a52020-06-26 22:20:25 +01001643func TestJavaSdkLibrary_Deps(t *testing.T) {
1644 ctx, _ := testJava(t, `
1645 java_sdk_library {
1646 name: "sdklib",
1647 srcs: ["a.java"],
1648 sdk_version: "none",
1649 system_modules: "none",
1650 public: {
1651 enabled: true,
1652 },
1653 }
1654 `)
1655
Paul Duffincee7e662020-07-09 17:32:57 +01001656 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001657 `dex2oatd`,
1658 `sdklib.impl`,
1659 `sdklib.stubs`,
1660 `sdklib.stubs.source`,
1661 `sdklib.xml`,
1662 })
1663}
1664
Paul Duffin46dc45a2020-05-14 15:39:10 +01001665func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1666 testJava(t, `
1667 java_sdk_library_import {
1668 name: "foo",
1669 public: {
1670 jars: ["a.jar"],
1671 stub_srcs: ["a.java"],
1672 current_api: "api/current.txt",
1673 removed_api: "api/removed.txt",
1674 },
1675 }
1676
1677 java_library {
1678 name: "bar",
1679 srcs: [":foo{.public.stubs.source}"],
1680 java_resources: [
1681 ":foo{.public.api.txt}",
1682 ":foo{.public.removed-api.txt}",
1683 ],
1684 }
1685 `)
1686}
1687
1688func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1689 bp := `
1690 java_sdk_library_import {
1691 name: "foo",
1692 public: {
1693 jars: ["a.jar"],
1694 },
1695 }
1696 `
1697
1698 t.Run("stubs.source", func(t *testing.T) {
1699 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1700 java_library {
1701 name: "bar",
1702 srcs: [":foo{.public.stubs.source}"],
1703 java_resources: [
1704 ":foo{.public.api.txt}",
1705 ":foo{.public.removed-api.txt}",
1706 ],
1707 }
1708 `)
1709 })
1710
1711 t.Run("api.txt", func(t *testing.T) {
1712 testJavaError(t, `api.txt not available for api scope public`, bp+`
1713 java_library {
1714 name: "bar",
1715 srcs: ["a.java"],
1716 java_resources: [
1717 ":foo{.public.api.txt}",
1718 ],
1719 }
1720 `)
1721 })
1722
1723 t.Run("removed-api.txt", func(t *testing.T) {
1724 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1725 java_library {
1726 name: "bar",
1727 srcs: ["a.java"],
1728 java_resources: [
1729 ":foo{.public.removed-api.txt}",
1730 ],
1731 }
1732 `)
1733 })
1734}
1735
Paul Duffin3375e352020-04-28 10:44:03 +01001736func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1737 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1738 java_sdk_library {
1739 name: "foo",
1740 srcs: ["a.java", "b.java"],
1741 api_packages: ["foo"],
1742 // Explicitly disable public to test the check that ensures the set of enabled
1743 // scopes is consistent.
1744 public: {
1745 enabled: false,
1746 },
1747 system: {
1748 enabled: true,
1749 },
1750 }
1751 `)
1752}
1753
Paul Duffin87a05a32020-05-12 11:50:28 +01001754func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1755 testJava(t, `
1756 java_sdk_library {
1757 name: "foo",
1758 srcs: ["a.java", "b.java"],
1759 api_packages: ["foo"],
1760 system: {
1761 enabled: true,
1762 sdk_version: "module_current",
1763 },
1764 }
1765 `)
1766}
1767
Paul Duffin0c5bae52020-06-02 13:00:08 +01001768func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1769 testJava(t, `
1770 java_sdk_library {
1771 name: "foo",
1772 srcs: ["a.java", "b.java"],
1773 api_packages: ["foo"],
1774 system: {
1775 enabled: true,
1776 },
1777 module_lib: {
1778 enabled: true,
1779 },
1780 }
1781 `)
1782}
1783
1784func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1785 testJava(t, `
1786 java_sdk_library {
1787 name: "foo",
1788 srcs: ["a.java", "b.java"],
1789 api_packages: ["foo"],
1790 system: {
1791 enabled: true,
1792 },
1793 system_server: {
1794 enabled: true,
1795 },
1796 }
1797 `)
1798}
1799
Paul Duffin803a9562020-05-20 11:52:25 +01001800func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1801 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1802 java_sdk_library {
1803 name: "foo",
1804 srcs: ["a.java"],
1805 public: {
1806 enabled: false,
1807 },
1808 }
1809
1810 java_library {
1811 name: "baz",
1812 srcs: ["a.java"],
1813 libs: ["foo"],
1814 sdk_version: "module_current",
1815 }
1816 `)
1817}
1818
1819func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1820 testJava(t, `
1821 java_sdk_library {
1822 name: "foo",
1823 srcs: ["a.java"],
1824 system: {
1825 enabled: true,
1826 },
1827 }
1828
1829 java_library {
1830 name: "baz",
1831 srcs: ["a.java"],
1832 libs: ["foo"],
1833 // foo does not have module-lib scope so it should fallback to system
1834 sdk_version: "module_current",
1835 }
1836 `)
1837}
1838
Jiyong Park932cdfe2020-05-28 00:19:53 +09001839func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1840 ctx, _ := testJava(t, `
1841 java_sdk_library {
1842 name: "foo",
1843 srcs: ["a.java"],
1844 system: {
1845 enabled: true,
1846 },
1847 default_to_stubs: true,
1848 }
1849
1850 java_library {
1851 name: "baz",
1852 srcs: ["a.java"],
1853 libs: ["foo"],
1854 // does not have sdk_version set, should fallback to module,
1855 // which will then fallback to system because the module scope
1856 // is not enabled.
1857 }
1858 `)
1859 // The baz library should depend on the system stubs jar.
1860 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1861 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1862 t.Errorf("expected %q, found %#q", expected, actual)
1863 }
1864}
1865
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001866var compilerFlagsTestCases = []struct {
1867 in string
1868 out bool
1869}{
1870 {
1871 in: "a",
1872 out: false,
1873 },
1874 {
1875 in: "-a",
1876 out: true,
1877 },
1878 {
1879 in: "-no-jdk",
1880 out: false,
1881 },
1882 {
1883 in: "-no-stdlib",
1884 out: false,
1885 },
1886 {
1887 in: "-kotlin-home",
1888 out: false,
1889 },
1890 {
1891 in: "-kotlin-home /some/path",
1892 out: false,
1893 },
1894 {
1895 in: "-include-runtime",
1896 out: false,
1897 },
1898 {
1899 in: "-Xintellij-plugin-root",
1900 out: false,
1901 },
1902}
1903
1904type mockContext struct {
1905 android.ModuleContext
1906 result bool
1907}
1908
1909func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1910 // CheckBadCompilerFlags calls this function when the flag should be rejected
1911 ctx.result = false
1912}
1913
1914func TestCompilerFlags(t *testing.T) {
1915 for _, testCase := range compilerFlagsTestCases {
1916 ctx := &mockContext{result: true}
1917 CheckKotlincFlags(ctx, []string{testCase.in})
1918 if ctx.result != testCase.out {
1919 t.Errorf("incorrect output:")
1920 t.Errorf(" input: %#v", testCase.in)
1921 t.Errorf(" expected: %#v", testCase.out)
1922 t.Errorf(" got: %#v", ctx.result)
1923 }
1924 }
1925}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001926
1927// TODO(jungjw): Consider making this more robust by ignoring path order.
1928func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1929 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1930 flags := strings.Split(variables["javacFlags"], " ")
1931 got := ""
1932 for _, flag := range flags {
1933 keyEnd := strings.Index(flag, "=")
1934 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1935 got = flag[keyEnd+1:]
1936 break
1937 }
1938 }
1939 if expected != got {
1940 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1941 }
1942}
1943
1944func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001945 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001946 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001947 bp := `
1948 java_library {
1949 name: "foo",
1950 srcs: ["a.java"],
1951 java_version: "1.8",
1952 }
1953
1954 java_library {
1955 name: "bar",
1956 srcs: ["b.java"],
1957 sdk_version: "none",
1958 system_modules: "none",
1959 patch_module: "java.base",
1960 java_version: "1.8",
1961 }
1962
1963 java_library {
1964 name: "baz",
1965 srcs: ["c.java"],
1966 patch_module: "java.base",
1967 java_version: "1.8",
1968 }
1969 `
1970 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001971
1972 checkPatchModuleFlag(t, ctx, "foo", "")
1973 checkPatchModuleFlag(t, ctx, "bar", "")
1974 checkPatchModuleFlag(t, ctx, "baz", "")
1975 })
1976
Pete Gillin0c2143e2019-05-02 15:32:11 +01001977 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001978 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001979 bp := `
1980 java_library {
1981 name: "foo",
1982 srcs: ["a.java"],
1983 }
1984
1985 java_library {
1986 name: "bar",
1987 srcs: ["b.java"],
1988 sdk_version: "none",
1989 system_modules: "none",
1990 patch_module: "java.base",
1991 }
1992
1993 java_library {
1994 name: "baz",
1995 srcs: ["c.java"],
1996 patch_module: "java.base",
1997 }
1998 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01001999 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002000
2001 checkPatchModuleFlag(t, ctx, "foo", "")
2002 expected := "java.base=.:" + buildDir
2003 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09002004 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002005 checkPatchModuleFlag(t, ctx, "baz", expected)
2006 })
2007}
Paul Duffina7b9f422020-01-10 17:12:18 +00002008
2009func TestJavaSystemModules(t *testing.T) {
2010 ctx, _ := testJava(t, `
2011 java_system_modules {
2012 name: "system-modules",
2013 libs: ["system-module1", "system-module2"],
2014 }
2015 java_library {
2016 name: "system-module1",
2017 srcs: ["a.java"],
2018 sdk_version: "none",
2019 system_modules: "none",
2020 }
2021 java_library {
2022 name: "system-module2",
2023 srcs: ["b.java"],
2024 sdk_version: "none",
2025 system_modules: "none",
2026 }
2027 `)
2028
2029 // check the existence of the module
2030 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2031
2032 cmd := systemModules.Rule("jarsTosystemModules")
2033
2034 // make sure the command compiles against the supplied modules.
2035 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2036 if !strings.Contains(cmd.Args["classpath"], module) {
2037 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2038 module)
2039 }
2040 }
2041}
Paul Duffin90169ba2020-01-10 17:16:44 +00002042
2043func TestJavaSystemModulesImport(t *testing.T) {
2044 ctx, _ := testJava(t, `
2045 java_system_modules_import {
2046 name: "system-modules",
2047 libs: ["system-module1", "system-module2"],
2048 }
2049 java_import {
2050 name: "system-module1",
2051 jars: ["a.jar"],
2052 }
2053 java_import {
2054 name: "system-module2",
2055 jars: ["b.jar"],
2056 }
2057 `)
2058
2059 // check the existence of the module
2060 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2061
2062 cmd := systemModules.Rule("jarsTosystemModules")
2063
2064 // make sure the command compiles against the supplied modules.
2065 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2066 if !strings.Contains(cmd.Args["classpath"], module) {
2067 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2068 module)
2069 }
2070 }
2071}
Paul Duffin83a2d962019-11-19 19:44:10 +00002072
2073func TestJavaLibraryWithSystemModules(t *testing.T) {
2074 ctx, _ := testJava(t, `
2075 java_library {
2076 name: "lib-with-source-system-modules",
2077 srcs: [
2078 "a.java",
2079 ],
2080 sdk_version: "none",
2081 system_modules: "source-system-modules",
2082 }
2083
2084 java_library {
2085 name: "source-jar",
2086 srcs: [
2087 "a.java",
2088 ],
2089 }
2090
2091 java_system_modules {
2092 name: "source-system-modules",
2093 libs: ["source-jar"],
2094 }
2095
2096 java_library {
2097 name: "lib-with-prebuilt-system-modules",
2098 srcs: [
2099 "a.java",
2100 ],
2101 sdk_version: "none",
2102 system_modules: "prebuilt-system-modules",
2103 }
2104
2105 java_import {
2106 name: "prebuilt-jar",
2107 jars: ["a.jar"],
2108 }
2109
2110 java_system_modules_import {
2111 name: "prebuilt-system-modules",
2112 libs: ["prebuilt-jar"],
2113 }
2114 `)
2115
2116 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2117
2118 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2119}
2120
2121func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2122 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2123 bootClasspath := javacRule.Args["bootClasspath"]
2124 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2125 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2126 }
2127}
Jiyong Park19604de2020-03-24 16:44:11 +09002128
2129func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2130 ctx, _ := testJava(t, `
2131 java_library {
2132 name: "foo",
2133 srcs: ["aidl/foo/IFoo.aidl"],
2134 libs: ["bar"],
2135 }
2136
2137 java_import {
2138 name: "bar",
2139 jars: ["a.jar"],
2140 aidl: {
2141 export_include_dirs: ["aidl/bar"],
2142 },
2143 }
2144 `)
2145
2146 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2147 expectedAidlFlag := "-Iaidl/bar"
2148 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2149 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2150 }
2151}
Liz Kammerdd849a82020-06-12 16:38:45 -07002152
2153func TestDataNativeBinaries(t *testing.T) {
2154 ctx, config := testJava(t, `
2155 java_test_host {
2156 name: "foo",
2157 srcs: ["a.java"],
2158 data_native_bins: ["bin"]
2159 }
2160
2161 python_binary_host {
2162 name: "bin",
2163 srcs: ["bin.py"],
2164 }
2165 `)
2166
2167 buildOS := android.BuildOs.String()
2168
2169 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2170 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2171 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2172 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2173 if !reflect.DeepEqual(expected, actual) {
2174 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2175 }
2176}