blob: a43e2a8c98aa2f5a992bb520d218a89e796c66ce [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
470 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
471 t.Errorf("expected binary wrapper implicits [%q], got %v",
472 barJar, barWrapperDeps)
473 }
Alex Humesky2070e322020-06-09 20:23:08 -0400474}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800475
Alex Humesky2070e322020-06-09 20:23:08 -0400476func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
477 bp := `
478 java_library {
479 name: "target_library",
480 srcs: ["a.java"],
481 }
482
483 java_binary_host {
484 name: "host_binary",
485 srcs: ["b.java"],
486 }
487 `
488 config := testConfig(nil, bp, nil)
489 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
490
491 ctx, _ := testJavaWithConfig(t, config)
492
Liz Kammer7941b302020-07-28 13:27:34 -0700493 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400494 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
495 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
496 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
497 t.Errorf("target library javac flags %v should contain "+
498 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
499 }
500
501 // check that -g is not overridden for host modules
502 buildOS := android.BuildOs.String()
503 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
504 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
505 if strings.Contains(hostJavaFlags, "-g:source,lines") {
506 t.Errorf("java_binary_host javac flags %v should not have "+
507 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
508 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800509}
510
Colin Cross72bb3632017-07-13 16:23:21 -0700511func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700512 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700513 java_library {
514 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000515 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000516 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700517 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700518 }
519
Colin Cross74d73e22017-08-02 11:05:49 -0700520 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700521 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700522 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700523 }
524
Colin Cross74d73e22017-08-02 11:05:49 -0700525 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700526 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700527 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700528 sdk_version: "current",
529 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700530 }
Colin Cross42be7612019-02-21 18:12:14 -0800531
532 dex_import {
533 name: "qux",
534 jars: ["b.jar"],
535 }
Colin Cross79c7c262019-04-17 11:11:46 -0700536
537 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000538 name: "sdklib",
539 public: {
540 jars: ["c.jar"],
541 },
542 }
543
Paul Duffin91547182019-11-12 19:39:36 +0000544 prebuilt_stubs_sources {
545 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000546 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000547 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000548
549 java_test_import {
550 name: "test",
551 jars: ["a.jar"],
552 test_suites: ["cts"],
553 test_config: "AndroidTest.xml",
554 }
Colin Cross72bb3632017-07-13 16:23:21 -0700555 `)
556
Paul Duffin9b478b02019-12-10 13:41:51 +0000557 fooModule := ctx.ModuleForTests("foo", "android_common")
558 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700559 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700560 barModule := ctx.ModuleForTests("bar", "android_common")
561 barJar := barModule.Rule("combineJar").Output
562 bazModule := ctx.ModuleForTests("baz", "android_common")
563 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700564 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700565
Paul Duffin9b478b02019-12-10 13:41:51 +0000566 fooLibrary := fooModule.Module().(*Library)
567 assertDeepEquals(t, "foo java sources incorrect",
568 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000569
Paul Duffin9b478b02019-12-10 13:41:51 +0000570 assertDeepEquals(t, "foo java source jars incorrect",
571 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
572 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000573
Colin Cross37f6d792018-07-12 12:28:41 -0700574 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
575 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700576 }
577
Liz Kammerd6c31d22020-08-05 15:40:41 -0700578 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
579 if barDexJar != nil {
580 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
581 }
582
Colin Cross79c7c262019-04-17 11:11:46 -0700583 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
Colin Cross37f6d792018-07-12 12:28:41 -0700587 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
588 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700589 }
Colin Cross42be7612019-02-21 18:12:14 -0800590
Liz Kammerd6c31d22020-08-05 15:40:41 -0700591 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
592 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
593 if bazDexJar != expectedDexJar {
594 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
595 }
596
Colin Cross42be7612019-02-21 18:12:14 -0800597 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700598}
599
Paul Duffin9b478b02019-12-10 13:41:51 +0000600func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
601 if !reflect.DeepEqual(expected, actual) {
602 t.Errorf("%s: expected %q, found %q", message, expected, actual)
603 }
604}
605
Paul Duffin56d44902020-01-31 13:36:25 +0000606func TestJavaSdkLibraryImport(t *testing.T) {
607 ctx, _ := testJava(t, `
608 java_library {
609 name: "foo",
610 srcs: ["a.java"],
611 libs: ["sdklib"],
612 sdk_version: "current",
613 }
614
615 java_library {
616 name: "foo.system",
617 srcs: ["a.java"],
618 libs: ["sdklib"],
619 sdk_version: "system_current",
620 }
621
622 java_library {
623 name: "foo.test",
624 srcs: ["a.java"],
625 libs: ["sdklib"],
626 sdk_version: "test_current",
627 }
628
629 java_sdk_library_import {
630 name: "sdklib",
631 public: {
632 jars: ["a.jar"],
633 },
634 system: {
635 jars: ["b.jar"],
636 },
637 test: {
638 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100639 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000640 },
641 }
642 `)
643
644 for _, scope := range []string{"", ".system", ".test"} {
645 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
646 javac := fooModule.Rule("javac")
647
648 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
649 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
650 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
651 }
652 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100653
Paul Duffincee7e662020-07-09 17:32:57 +0100654 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100655 `prebuilt_sdklib.stubs`,
656 `prebuilt_sdklib.stubs.source.test`,
657 `prebuilt_sdklib.stubs.system`,
658 `prebuilt_sdklib.stubs.test`,
659 })
660}
661
662func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
663 ctx, _ := testJava(t, `
664 java_sdk_library {
665 name: "sdklib",
666 srcs: ["a.java"],
667 sdk_version: "none",
668 system_modules: "none",
669 public: {
670 enabled: true,
671 },
672 }
673
674 java_sdk_library_import {
675 name: "sdklib",
676 public: {
677 jars: ["a.jar"],
678 },
679 }
680 `)
681
Paul Duffincee7e662020-07-09 17:32:57 +0100682 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100683 `dex2oatd`,
684 `prebuilt_sdklib`,
685 `sdklib.impl`,
686 `sdklib.stubs`,
687 `sdklib.stubs.source`,
688 `sdklib.xml`,
689 })
690
Paul Duffincee7e662020-07-09 17:32:57 +0100691 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100692 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100693 `sdklib.impl`,
694 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
695 // dependency is added after prebuilts may have been renamed and so has to use
696 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100697 `sdklib.xml`,
698 })
699}
700
701func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
702 ctx, _ := testJava(t, `
703 java_sdk_library {
704 name: "sdklib",
705 srcs: ["a.java"],
706 sdk_version: "none",
707 system_modules: "none",
708 public: {
709 enabled: true,
710 },
711 }
712
713 java_sdk_library_import {
714 name: "sdklib",
715 prefer: true,
716 public: {
717 jars: ["a.jar"],
718 },
719 }
720 `)
721
Paul Duffincee7e662020-07-09 17:32:57 +0100722 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100723 `dex2oatd`,
724 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100725 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100726 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100727 `sdklib.stubs.source`,
728 `sdklib.xml`,
729 })
730
Paul Duffincee7e662020-07-09 17:32:57 +0100731 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100732 `prebuilt_sdklib.stubs`,
733 `sdklib.impl`,
734 `sdklib.xml`,
735 })
Paul Duffin56d44902020-01-31 13:36:25 +0000736}
737
Colin Cross89536d42017-07-07 14:35:50 -0700738func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700739 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700740 java_defaults {
741 name: "defaults",
742 srcs: ["a.java"],
743 libs: ["bar"],
744 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700745 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700746 }
747
748 java_library {
749 name: "foo",
750 defaults: ["defaults"],
751 }
752
753 java_library {
754 name: "bar",
755 srcs: ["b.java"],
756 }
757
758 java_library {
759 name: "baz",
760 srcs: ["c.java"],
761 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700762
763 android_test {
764 name: "atestOptimize",
765 defaults: ["defaults"],
766 optimize: {enabled: true},
767 }
768
769 android_test {
770 name: "atestNoOptimize",
771 defaults: ["defaults"],
772 }
773
774 android_test {
775 name: "atestDefault",
776 srcs: ["a.java"],
777 }
Colin Cross89536d42017-07-07 14:35:50 -0700778 `)
779
Colin Cross4c428df2017-09-15 17:36:05 -0700780 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700781 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700782
783 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
784 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
785 }
786
Nan Zhanged19fc32017-10-19 13:06:22 -0700787 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
788 if !strings.Contains(javac.Args["classpath"], barTurbine) {
789 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700790 }
791
Colin Cross1ee23172017-10-18 14:44:18 -0700792 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700793 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
794 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700795 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700796
797 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
798 if atestOptimize.Output == nil {
799 t.Errorf("atestOptimize should optimize APK")
800 }
801
802 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
803 if atestNoOptimize.Output == nil {
804 t.Errorf("atestNoOptimize should not optimize APK")
805 }
806
807 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
808 if atestDefault.Output == nil {
809 t.Errorf("atestDefault should optimize APK")
810 }
Colin Cross89536d42017-07-07 14:35:50 -0700811}
812
Colin Cross0f37af02017-09-27 17:42:05 -0700813func TestResources(t *testing.T) {
814 var table = []struct {
815 name string
816 prop string
817 extra string
818 args string
819 }{
820 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700821 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700822 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800823 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700824 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700825 },
826 {
827 // Test that a module with java_resources includes the files
828 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700829 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
830 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700831 },
832 {
833 // Test that a module with a filegroup in java_resources includes the files with the
834 // path prefix
835 name: "resource filegroup",
836 prop: `java_resources: [":foo-res"]`,
837 extra: `
838 filegroup {
839 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800840 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700841 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700842 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700843 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700844 },
845 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700846 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
847 name: "wildcard dirs",
848 prop: `java_resource_dirs: ["java-res/*"]`,
849 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
850 },
851 {
852 // Test that a module exclude_java_resource_dirs excludes the files
853 name: "wildcard dirs",
854 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
855 args: "-C java-res/a -f java-res/a/a",
856 },
Colin Crosscedd4762018-09-13 11:26:19 -0700857 {
858 // Test wildcards in java_resources
859 name: "wildcard files",
860 prop: `java_resources: ["java-res/**/*"]`,
861 args: "-C . -f java-res/a/a -f java-res/b/b",
862 },
863 {
864 // Test exclude_java_resources with java_resources
865 name: "wildcard files with exclude",
866 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
867 args: "-C . -f java-res/a/a",
868 },
869 {
870 // Test exclude_java_resources with java_resource_dirs
871 name: "resource dirs with exclude files",
872 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
873 args: "-C java-res -f java-res/a/a",
874 },
875 {
876 // Test exclude_java_resource_dirs with java_resource_dirs
877 name: "resource dirs with exclude files",
878 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
879 args: "-C java-res -f java-res/a/a -f java-res/b/b",
880 },
Colin Cross0f37af02017-09-27 17:42:05 -0700881 }
882
883 for _, test := range table {
884 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700885 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700886 java_library {
887 name: "foo",
888 srcs: [
889 "a.java",
890 "b.java",
891 "c.java",
892 ],
893 `+test.prop+`,
894 }
Colin Cross238c1f32020-06-07 16:58:18 -0700895 `+test.extra,
896 map[string][]byte{
897 "java-res/a/a": nil,
898 "java-res/b/b": nil,
899 "java-res2/a": nil,
900 },
901 )
Colin Cross0f37af02017-09-27 17:42:05 -0700902
Colin Cross331a1212018-08-15 20:40:52 -0700903 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700904 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700905
906 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
907 t.Errorf("foo combined jars %v does not contain %q",
908 foo.Inputs.Strings(), fooRes.Output.String())
909 }
910
Colin Crossaf9c55b2017-10-03 14:50:08 -0700911 if fooRes.Args["jarArgs"] != test.args {
912 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700913 fooRes.Args["jarArgs"], test.args)
914 }
915 })
916 }
917}
918
Colin Cross0c4ce212019-05-03 15:28:19 -0700919func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700920 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700921 java_library {
922 name: "foo",
923 srcs: [
924 "a.java",
925 "b.java",
926 "c.java",
927 ],
928 include_srcs: true,
929 }
930
931 java_library {
932 name: "bar",
933 srcs: [
934 "a.java",
935 "b.java",
936 "c.java",
937 ],
938 java_resource_dirs: ["java-res"],
939 include_srcs: true,
940 }
Colin Cross238c1f32020-06-07 16:58:18 -0700941 `, map[string][]byte{
942 "java-res/a/a": nil,
943 "java-res/b/b": nil,
944 "java-res2/a": nil,
945 })
Colin Cross0c4ce212019-05-03 15:28:19 -0700946
947 // Test a library with include_srcs: true
948 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
949 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
950
951 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
952 t.Errorf("foo combined jars %v does not contain %q", w, g)
953 }
954
955 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
956 t.Errorf("foo source jar args %q is not %q", w, g)
957 }
958
959 // Test a library with include_srcs: true and resources
960 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
961 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
962 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
963 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
964
965 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
966 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
967 }
968
969 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
970 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
971 }
972
973 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
974 t.Errorf("bar combined jars %v does not contain %q", w, g)
975 }
976
977 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
978 t.Errorf("bar source jar args %q is not %q", w, g)
979 }
980
981 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
982 t.Errorf("bar resource jar args %q is not %q", w, g)
983 }
984}
985
Colin Cross54190b32017-10-09 15:34:10 -0700986func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -0700987 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700988 java_library {
989 name: "foo",
990 srcs: [
991 "a*.java",
992 ":gen",
993 "b*.java",
994 ],
995 }
996
997 genrule {
998 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800999 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001000 out: ["gen.java"],
1001 }
Colin Cross238c1f32020-06-07 16:58:18 -07001002 `, map[string][]byte{
1003 "a.java": nil,
1004 "b.java": nil,
1005 })
Colin Cross54190b32017-10-09 15:34:10 -07001006
1007 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1008 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1009
Colin Cross15e86d92017-10-20 15:07:08 -07001010 if filepath.Base(genrule.Output.String()) != "gen.java" {
1011 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001012 }
1013
1014 if len(javac.Inputs) != 3 ||
1015 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001016 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001017 javac.Inputs[2].String() != "b.java" {
1018 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1019 }
1020}
1021
Nan Zhang61eaedb2017-11-02 13:28:15 -07001022func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001023 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001024 java_library {
1025 name: "foo",
1026 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001027 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001028 }
1029
1030 java_library {
1031 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001032 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001033 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001034 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001035 }
1036
1037 java_library {
1038 name: "baz",
1039 srcs: ["c.java"],
1040 libs: ["bar"],
1041 sdk_version: "14",
1042 }
1043 `)
1044
1045 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1046 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1047 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1048 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1049 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1050
1051 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1052 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1053 }
1054
1055 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1056 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1057 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1058 }
1059 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1060 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1061 }
1062 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1063 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1064 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001065 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001066 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001067 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001068 }
1069}
1070
1071func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001072 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001073 java_library {
1074 name: "bar",
1075 srcs: ["a.java","b.java","c.java"],
1076 javac_shard_size: 1
1077 }
1078 `)
1079
1080 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1081 for i := 0; i < 3; i++ {
1082 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1083 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1084 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1085 }
1086 }
1087}
1088
Nan Zhang581fd212018-01-10 16:06:12 -08001089func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001090 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001091 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001092 name: "droiddoc-templates-sdk",
1093 path: ".",
1094 }
Jiyong Park29074592019-07-07 16:27:47 +09001095 filegroup {
1096 name: "bar-doc-aidl-srcs",
1097 srcs: ["bar-doc/IBar.aidl"],
1098 path: "bar-doc",
1099 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001100 droidstubs {
1101 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001102 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001103 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001104 ],
Steve Kim3666c702020-09-01 17:58:01 +00001105 exclude_srcs: [
1106 "bar-doc/b.java"
1107 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001108 api_levels_annotations_dirs: [
1109 "droiddoc-templates-sdk",
1110 ],
1111 api_levels_annotations_enabled: true,
1112 }
1113 droiddoc {
1114 name: "bar-doc",
1115 srcs: [
1116 ":bar-stubs",
1117 "bar-doc/IFoo.aidl",
1118 ":bar-doc-aidl-srcs",
1119 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001120 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001121 hdf: [
1122 "android.whichdoc offline",
1123 ],
1124 knowntags: [
1125 "bar-doc/known_oj_tags.txt",
1126 ],
1127 proofread_file: "libcore-proofread.txt",
1128 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001129 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001130 }
Colin Cross238c1f32020-06-07 16:58:18 -07001131 `,
1132 map[string][]byte{
1133 "bar-doc/a.java": nil,
1134 "bar-doc/b.java": nil,
1135 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001136 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1137 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1138 if err != nil {
1139 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1140 }
1141 if len(barStubsOutputs) != 1 {
1142 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001143 }
Nan Zhang581fd212018-01-10 16:06:12 -08001144
Liz Kammere1ab2502020-09-10 15:29:25 +00001145 barStubsOutput := barStubsOutputs[0]
1146 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1147 javaDoc := barDoc.Rule("javadoc")
1148 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1149 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001150 }
1151
Liz Kammere1ab2502020-09-10 15:29:25 +00001152 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1153 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1154 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1155 }
1156
1157 aidl := barDoc.Rule("aidl")
1158 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001159 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1160 }
1161
1162 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1163 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001164 }
Nan Zhang581fd212018-01-10 16:06:12 -08001165}
1166
Liz Kammer585cac22020-07-06 09:12:57 -07001167func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1168 testJavaError(t, "flags is set. Cannot set args", `
1169 droiddoc_exported_dir {
1170 name: "droiddoc-templates-sdk",
1171 path: ".",
1172 }
1173 filegroup {
1174 name: "bar-doc-aidl-srcs",
1175 srcs: ["bar-doc/IBar.aidl"],
1176 path: "bar-doc",
1177 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001178 droidstubs {
1179 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001180 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001181 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001182 ],
Steve Kim3666c702020-09-01 17:58:01 +00001183 exclude_srcs: [
1184 "bar-doc/b.java"
1185 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001186 api_levels_annotations_dirs: [
1187 "droiddoc-templates-sdk",
1188 ],
1189 api_levels_annotations_enabled: true,
1190 }
1191 droiddoc {
1192 name: "bar-doc",
1193 srcs: [
1194 ":bar-stubs",
1195 "bar-doc/IFoo.aidl",
1196 ":bar-doc-aidl-srcs",
1197 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001198 custom_template: "droiddoc-templates-sdk",
1199 hdf: [
1200 "android.whichdoc offline",
1201 ],
1202 knowntags: [
1203 "bar-doc/known_oj_tags.txt",
1204 ],
1205 proofread_file: "libcore-proofread.txt",
1206 todo_file: "libcore-docs-todo.html",
1207 flags: ["-offlinemode -title \"libcore\""],
1208 args: "-offlinemode -title \"libcore\"",
1209 }
1210 `)
1211}
1212
Liz Kammer3d894b72020-08-04 09:55:13 -07001213func TestDroidstubs(t *testing.T) {
1214 ctx, _ := testJavaWithFS(t, `
1215 droiddoc_exported_dir {
1216 name: "droiddoc-templates-sdk",
1217 path: ".",
1218 }
1219
1220 droidstubs {
1221 name: "bar-stubs",
1222 srcs: [
1223 "bar-doc/a.java",
1224 ],
1225 api_levels_annotations_dirs: [
1226 "droiddoc-templates-sdk",
1227 ],
1228 api_levels_annotations_enabled: true,
1229 }
1230
1231 droidstubs {
1232 name: "bar-stubs-other",
1233 srcs: [
1234 "bar-doc/a.java",
1235 ],
1236 api_levels_annotations_dirs: [
1237 "droiddoc-templates-sdk",
1238 ],
1239 api_levels_annotations_enabled: true,
1240 api_levels_jar_filename: "android.other.jar",
1241 }
1242 `,
1243 map[string][]byte{
1244 "bar-doc/a.java": nil,
1245 })
1246 testcases := []struct {
1247 moduleName string
1248 expectedJarFilename string
1249 }{
1250 {
1251 moduleName: "bar-stubs",
1252 expectedJarFilename: "android.jar",
1253 },
1254 {
1255 moduleName: "bar-stubs-other",
1256 expectedJarFilename: "android.other.jar",
1257 },
1258 }
1259 for _, c := range testcases {
1260 m := ctx.ModuleForTests(c.moduleName, "android_common")
1261 metalava := m.Rule("metalava")
1262 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
1263 if actual := metalava.RuleParams.Command; !strings.Contains(actual, expected) {
1264 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1265 }
1266 }
1267}
1268
Paul Duffin83a2d962019-11-19 19:44:10 +00001269func TestDroidstubsWithSystemModules(t *testing.T) {
1270 ctx, _ := testJava(t, `
1271 droidstubs {
1272 name: "stubs-source-system-modules",
1273 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001274 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001275 ],
1276 sdk_version: "none",
1277 system_modules: "source-system-modules",
1278 }
1279
1280 java_library {
1281 name: "source-jar",
1282 srcs: [
1283 "a.java",
1284 ],
1285 }
1286
1287 java_system_modules {
1288 name: "source-system-modules",
1289 libs: ["source-jar"],
1290 }
1291
1292 droidstubs {
1293 name: "stubs-prebuilt-system-modules",
1294 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001295 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001296 ],
1297 sdk_version: "none",
1298 system_modules: "prebuilt-system-modules",
1299 }
1300
1301 java_import {
1302 name: "prebuilt-jar",
1303 jars: ["a.jar"],
1304 }
1305
1306 java_system_modules_import {
1307 name: "prebuilt-system-modules",
1308 libs: ["prebuilt-jar"],
1309 }
1310 `)
1311
1312 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1313
1314 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1315}
1316
1317func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1318 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1319 var systemJars []string
1320 for _, i := range metalavaRule.Implicits {
1321 systemJars = append(systemJars, i.Base())
1322 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001323 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001324 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1325 }
1326}
1327
Colin Cross54250902017-12-05 09:28:08 -08001328func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001329 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001330 java_library {
1331 name: "foo",
1332 srcs: ["a.java"],
1333 }
1334
1335 java_genrule {
1336 name: "jargen",
1337 tool_files: ["b.java"],
1338 cmd: "$(location b.java) $(in) $(out)",
1339 out: ["jargen.jar"],
1340 srcs: [":foo"],
1341 }
1342
1343 java_library {
1344 name: "bar",
1345 static_libs: ["jargen"],
1346 srcs: ["c.java"],
1347 }
1348
1349 java_library {
1350 name: "baz",
1351 libs: ["jargen"],
1352 srcs: ["c.java"],
1353 }
1354 `)
1355
1356 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1357 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1358 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1359 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1360 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1361
1362 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
1363 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
1364 }
1365
1366 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1367 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1368 }
1369
1370 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1371 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1372 }
1373
1374 if len(barCombined.Inputs) != 2 ||
1375 barCombined.Inputs[0].String() != bar.Output.String() ||
1376 barCombined.Inputs[1].String() != jargen.Output.String() {
1377 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1378 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1379 }
1380}
1381
Nan Zhang27e284d2018-02-09 21:03:53 +00001382func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001383 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001384 java_library {
1385 name: "foo",
1386 srcs: ["a.java", ":foo-srcs"],
1387 exclude_srcs: ["a.java", ":foo-excludes"],
1388 }
1389
1390 filegroup {
1391 name: "foo-srcs",
1392 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1393 }
1394
1395 filegroup {
1396 name: "foo-excludes",
1397 srcs: ["java-fg/a.java", "java-fg/b.java"],
1398 }
1399 `)
1400
1401 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1402
1403 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1404 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1405 }
1406}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001407
Paul Duffin52d398a2019-06-11 12:31:14 +01001408func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001409 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001410 "libcore/Android.bp": []byte(`
1411 java_library {
1412 name: "core",
1413 sdk_version: "none",
1414 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001415 }
1416
1417 filegroup {
1418 name: "core-jar",
1419 srcs: [":core{.jar}"],
1420 }
1421`),
1422 })
1423 ctx := testContext()
1424 run(t, ctx, config)
1425}
1426
1427func TestJavaImport(t *testing.T) {
1428 config := testConfig(nil, "", map[string][]byte{
1429 "libcore/Android.bp": []byte(`
1430 java_import {
1431 name: "core",
1432 sdk_version: "none",
1433 }
1434
1435 filegroup {
1436 name: "core-jar",
1437 srcs: [":core{.jar}"],
1438 }
1439`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001440 })
Colin Cross98be1bb2019-12-13 20:41:13 -08001441 ctx := testContext()
Paul Duffin52d398a2019-06-11 12:31:14 +01001442 run(t, ctx, config)
1443}
1444
Jiyong Parkc678ad32018-04-10 13:07:10 +09001445func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001446 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001447 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001448 name: "droiddoc-templates-sdk",
1449 path: ".",
1450 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001451 java_sdk_library {
1452 name: "foo",
1453 srcs: ["a.java", "b.java"],
1454 api_packages: ["foo"],
1455 }
1456 java_sdk_library {
1457 name: "bar",
1458 srcs: ["a.java", "b.java"],
1459 api_packages: ["bar"],
1460 }
1461 java_library {
1462 name: "baz",
1463 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001464 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001465 sdk_version: "system_current",
1466 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001467 java_sdk_library {
1468 name: "barney",
1469 srcs: ["c.java"],
1470 api_only: true,
1471 }
1472 java_sdk_library {
1473 name: "betty",
1474 srcs: ["c.java"],
1475 shared_library: false,
1476 }
Paul Duffin859fe962020-05-15 10:20:31 +01001477 java_sdk_library_import {
1478 name: "quuz",
1479 public: {
1480 jars: ["c.jar"],
1481 },
1482 }
1483 java_sdk_library_import {
1484 name: "fred",
1485 public: {
1486 jars: ["b.jar"],
1487 },
1488 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001489 java_sdk_library_import {
1490 name: "wilma",
1491 public: {
1492 jars: ["b.jar"],
1493 },
1494 shared_library: false,
1495 }
Jiyong Park1be96912018-05-28 18:02:19 +09001496 java_library {
1497 name: "qux",
1498 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001499 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001500 sdk_version: "system_current",
1501 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001502 java_library {
1503 name: "baz-test",
1504 srcs: ["c.java"],
1505 libs: ["foo"],
1506 sdk_version: "test_current",
1507 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001508 java_library {
1509 name: "baz-29",
1510 srcs: ["c.java"],
1511 libs: ["foo"],
1512 sdk_version: "system_29",
1513 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001514 java_library {
1515 name: "baz-module-30",
1516 srcs: ["c.java"],
1517 libs: ["foo"],
1518 sdk_version: "module_30",
1519 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001520 `)
1521
1522 // check the existence of the internal modules
1523 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001524 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1525 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1526 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1527 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1528 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1529 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001530 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001531 ctx.ModuleForTests("foo.api.public.28", "")
1532 ctx.ModuleForTests("foo.api.system.28", "")
1533 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001534
1535 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1536 // tests if baz is actually linked to the stubs lib
1537 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1538 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1539 "foo.stubs.system.jar")
1540 }
1541 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001542 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001543 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001544 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001545 }
1546 // test if baz is not linked to the system variant of foo
1547 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1548 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1549 "foo.stubs.jar")
1550 }
Jiyong Park1be96912018-05-28 18:02:19 +09001551
Paul Duffin726d23c2020-01-22 16:30:37 +00001552 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1553 // tests if baz-test is actually linked to the test stubs lib
1554 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1555 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1556 "foo.stubs.test.jar")
1557 }
1558
Paul Duffina2db18f2020-01-22 17:11:15 +00001559 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1560 // tests if baz-29 is actually linked to the system 29 stubs lib
1561 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1562 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1563 "prebuilts/sdk/29/system/foo.jar")
1564 }
1565
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001566 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1567 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1568 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1569 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1570 "prebuilts/sdk/30/module-lib/foo.jar")
1571 }
1572
Jiyong Park1be96912018-05-28 18:02:19 +09001573 // test if baz has exported SDK lib names foo and bar to qux
1574 qux := ctx.ModuleForTests("qux", "android_common")
1575 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb8063c62020-08-20 11:33:12 +01001576 sdkLibs := android.SortedStringKeys(quxLib.ExportedSdkLibs())
Paul Duffin859fe962020-05-15 10:20:31 +01001577 if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
1578 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001579 }
1580 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001581}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001582
Paul Duffindaaa3322020-05-26 18:13:57 +01001583func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1584 ctx, _ := testJava(t, `
1585 java_sdk_library {
1586 name: "foo",
1587 srcs: ["a.java"],
1588 api_only: true,
1589 public: {
1590 enabled: true,
1591 },
1592 }
1593
1594 java_library {
1595 name: "bar",
1596 srcs: ["b.java"],
1597 libs: ["foo"],
1598 }
1599 `)
1600
1601 // The bar library should depend on the stubs jar.
1602 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1603 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1604 t.Errorf("expected %q, found %#q", expected, actual)
1605 }
1606}
1607
Paul Duffin46dc45a2020-05-14 15:39:10 +01001608func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1609 testJava(t, `
1610 java_sdk_library {
1611 name: "foo",
1612 srcs: ["a.java"],
1613 api_packages: ["foo"],
1614 public: {
1615 enabled: true,
1616 },
1617 }
1618
1619 java_library {
1620 name: "bar",
1621 srcs: ["b.java", ":foo{.public.stubs.source}"],
1622 }
1623 `)
1624}
1625
1626func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1627 testJavaError(t, `"foo" does not provide api scope system`, `
1628 java_sdk_library {
1629 name: "foo",
1630 srcs: ["a.java"],
1631 api_packages: ["foo"],
1632 public: {
1633 enabled: true,
1634 },
1635 }
1636
1637 java_library {
1638 name: "bar",
1639 srcs: ["b.java", ":foo{.system.stubs.source}"],
1640 }
1641 `)
1642}
1643
Paul Duffinca8d9a52020-06-26 22:20:25 +01001644func TestJavaSdkLibrary_Deps(t *testing.T) {
1645 ctx, _ := testJava(t, `
1646 java_sdk_library {
1647 name: "sdklib",
1648 srcs: ["a.java"],
1649 sdk_version: "none",
1650 system_modules: "none",
1651 public: {
1652 enabled: true,
1653 },
1654 }
1655 `)
1656
Paul Duffincee7e662020-07-09 17:32:57 +01001657 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001658 `dex2oatd`,
1659 `sdklib.impl`,
1660 `sdklib.stubs`,
1661 `sdklib.stubs.source`,
1662 `sdklib.xml`,
1663 })
1664}
1665
Paul Duffin46dc45a2020-05-14 15:39:10 +01001666func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
1667 testJava(t, `
1668 java_sdk_library_import {
1669 name: "foo",
1670 public: {
1671 jars: ["a.jar"],
1672 stub_srcs: ["a.java"],
1673 current_api: "api/current.txt",
1674 removed_api: "api/removed.txt",
1675 },
1676 }
1677
1678 java_library {
1679 name: "bar",
1680 srcs: [":foo{.public.stubs.source}"],
1681 java_resources: [
1682 ":foo{.public.api.txt}",
1683 ":foo{.public.removed-api.txt}",
1684 ],
1685 }
1686 `)
1687}
1688
1689func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1690 bp := `
1691 java_sdk_library_import {
1692 name: "foo",
1693 public: {
1694 jars: ["a.jar"],
1695 },
1696 }
1697 `
1698
1699 t.Run("stubs.source", func(t *testing.T) {
1700 testJavaError(t, `stubs.source not available for api scope public`, bp+`
1701 java_library {
1702 name: "bar",
1703 srcs: [":foo{.public.stubs.source}"],
1704 java_resources: [
1705 ":foo{.public.api.txt}",
1706 ":foo{.public.removed-api.txt}",
1707 ],
1708 }
1709 `)
1710 })
1711
1712 t.Run("api.txt", func(t *testing.T) {
1713 testJavaError(t, `api.txt not available for api scope public`, bp+`
1714 java_library {
1715 name: "bar",
1716 srcs: ["a.java"],
1717 java_resources: [
1718 ":foo{.public.api.txt}",
1719 ],
1720 }
1721 `)
1722 })
1723
1724 t.Run("removed-api.txt", func(t *testing.T) {
1725 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
1726 java_library {
1727 name: "bar",
1728 srcs: ["a.java"],
1729 java_resources: [
1730 ":foo{.public.removed-api.txt}",
1731 ],
1732 }
1733 `)
1734 })
1735}
1736
Paul Duffin3375e352020-04-28 10:44:03 +01001737func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
1738 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
1739 java_sdk_library {
1740 name: "foo",
1741 srcs: ["a.java", "b.java"],
1742 api_packages: ["foo"],
1743 // Explicitly disable public to test the check that ensures the set of enabled
1744 // scopes is consistent.
1745 public: {
1746 enabled: false,
1747 },
1748 system: {
1749 enabled: true,
1750 },
1751 }
1752 `)
1753}
1754
Paul Duffin87a05a32020-05-12 11:50:28 +01001755func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
1756 testJava(t, `
1757 java_sdk_library {
1758 name: "foo",
1759 srcs: ["a.java", "b.java"],
1760 api_packages: ["foo"],
1761 system: {
1762 enabled: true,
1763 sdk_version: "module_current",
1764 },
1765 }
1766 `)
1767}
1768
Paul Duffin0c5bae52020-06-02 13:00:08 +01001769func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
1770 testJava(t, `
1771 java_sdk_library {
1772 name: "foo",
1773 srcs: ["a.java", "b.java"],
1774 api_packages: ["foo"],
1775 system: {
1776 enabled: true,
1777 },
1778 module_lib: {
1779 enabled: true,
1780 },
1781 }
1782 `)
1783}
1784
1785func TestJavaSdkLibrary_SystemServer(t *testing.T) {
1786 testJava(t, `
1787 java_sdk_library {
1788 name: "foo",
1789 srcs: ["a.java", "b.java"],
1790 api_packages: ["foo"],
1791 system: {
1792 enabled: true,
1793 },
1794 system_server: {
1795 enabled: true,
1796 },
1797 }
1798 `)
1799}
1800
Paul Duffin803a9562020-05-20 11:52:25 +01001801func TestJavaSdkLibrary_MissingScope(t *testing.T) {
1802 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
1803 java_sdk_library {
1804 name: "foo",
1805 srcs: ["a.java"],
1806 public: {
1807 enabled: false,
1808 },
1809 }
1810
1811 java_library {
1812 name: "baz",
1813 srcs: ["a.java"],
1814 libs: ["foo"],
1815 sdk_version: "module_current",
1816 }
1817 `)
1818}
1819
1820func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
1821 testJava(t, `
1822 java_sdk_library {
1823 name: "foo",
1824 srcs: ["a.java"],
1825 system: {
1826 enabled: true,
1827 },
1828 }
1829
1830 java_library {
1831 name: "baz",
1832 srcs: ["a.java"],
1833 libs: ["foo"],
1834 // foo does not have module-lib scope so it should fallback to system
1835 sdk_version: "module_current",
1836 }
1837 `)
1838}
1839
Jiyong Park932cdfe2020-05-28 00:19:53 +09001840func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
1841 ctx, _ := testJava(t, `
1842 java_sdk_library {
1843 name: "foo",
1844 srcs: ["a.java"],
1845 system: {
1846 enabled: true,
1847 },
1848 default_to_stubs: true,
1849 }
1850
1851 java_library {
1852 name: "baz",
1853 srcs: ["a.java"],
1854 libs: ["foo"],
1855 // does not have sdk_version set, should fallback to module,
1856 // which will then fallback to system because the module scope
1857 // is not enabled.
1858 }
1859 `)
1860 // The baz library should depend on the system stubs jar.
1861 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1862 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1863 t.Errorf("expected %q, found %#q", expected, actual)
1864 }
1865}
1866
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001867var compilerFlagsTestCases = []struct {
1868 in string
1869 out bool
1870}{
1871 {
1872 in: "a",
1873 out: false,
1874 },
1875 {
1876 in: "-a",
1877 out: true,
1878 },
1879 {
1880 in: "-no-jdk",
1881 out: false,
1882 },
1883 {
1884 in: "-no-stdlib",
1885 out: false,
1886 },
1887 {
1888 in: "-kotlin-home",
1889 out: false,
1890 },
1891 {
1892 in: "-kotlin-home /some/path",
1893 out: false,
1894 },
1895 {
1896 in: "-include-runtime",
1897 out: false,
1898 },
1899 {
1900 in: "-Xintellij-plugin-root",
1901 out: false,
1902 },
1903}
1904
1905type mockContext struct {
1906 android.ModuleContext
1907 result bool
1908}
1909
1910func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1911 // CheckBadCompilerFlags calls this function when the flag should be rejected
1912 ctx.result = false
1913}
1914
1915func TestCompilerFlags(t *testing.T) {
1916 for _, testCase := range compilerFlagsTestCases {
1917 ctx := &mockContext{result: true}
1918 CheckKotlincFlags(ctx, []string{testCase.in})
1919 if ctx.result != testCase.out {
1920 t.Errorf("incorrect output:")
1921 t.Errorf(" input: %#v", testCase.in)
1922 t.Errorf(" expected: %#v", testCase.out)
1923 t.Errorf(" got: %#v", ctx.result)
1924 }
1925 }
1926}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001927
1928// TODO(jungjw): Consider making this more robust by ignoring path order.
1929func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1930 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1931 flags := strings.Split(variables["javacFlags"], " ")
1932 got := ""
1933 for _, flag := range flags {
1934 keyEnd := strings.Index(flag, "=")
1935 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1936 got = flag[keyEnd+1:]
1937 break
1938 }
1939 }
1940 if expected != got {
1941 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1942 }
1943}
1944
1945func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01001946 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001947 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01001948 bp := `
1949 java_library {
1950 name: "foo",
1951 srcs: ["a.java"],
1952 java_version: "1.8",
1953 }
1954
1955 java_library {
1956 name: "bar",
1957 srcs: ["b.java"],
1958 sdk_version: "none",
1959 system_modules: "none",
1960 patch_module: "java.base",
1961 java_version: "1.8",
1962 }
1963
1964 java_library {
1965 name: "baz",
1966 srcs: ["c.java"],
1967 patch_module: "java.base",
1968 java_version: "1.8",
1969 }
1970 `
1971 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001972
1973 checkPatchModuleFlag(t, ctx, "foo", "")
1974 checkPatchModuleFlag(t, ctx, "bar", "")
1975 checkPatchModuleFlag(t, ctx, "baz", "")
1976 })
1977
Pete Gillin0c2143e2019-05-02 15:32:11 +01001978 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01001979 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01001980 bp := `
1981 java_library {
1982 name: "foo",
1983 srcs: ["a.java"],
1984 }
1985
1986 java_library {
1987 name: "bar",
1988 srcs: ["b.java"],
1989 sdk_version: "none",
1990 system_modules: "none",
1991 patch_module: "java.base",
1992 }
1993
1994 java_library {
1995 name: "baz",
1996 srcs: ["c.java"],
1997 patch_module: "java.base",
1998 }
1999 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002000 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002001
2002 checkPatchModuleFlag(t, ctx, "foo", "")
2003 expected := "java.base=.:" + buildDir
2004 checkPatchModuleFlag(t, ctx, "bar", expected)
Jiyong Park231d0562019-12-18 17:28:17 +09002005 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002006 checkPatchModuleFlag(t, ctx, "baz", expected)
2007 })
2008}
Paul Duffina7b9f422020-01-10 17:12:18 +00002009
2010func TestJavaSystemModules(t *testing.T) {
2011 ctx, _ := testJava(t, `
2012 java_system_modules {
2013 name: "system-modules",
2014 libs: ["system-module1", "system-module2"],
2015 }
2016 java_library {
2017 name: "system-module1",
2018 srcs: ["a.java"],
2019 sdk_version: "none",
2020 system_modules: "none",
2021 }
2022 java_library {
2023 name: "system-module2",
2024 srcs: ["b.java"],
2025 sdk_version: "none",
2026 system_modules: "none",
2027 }
2028 `)
2029
2030 // check the existence of the module
2031 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2032
2033 cmd := systemModules.Rule("jarsTosystemModules")
2034
2035 // make sure the command compiles against the supplied modules.
2036 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2037 if !strings.Contains(cmd.Args["classpath"], module) {
2038 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2039 module)
2040 }
2041 }
2042}
Paul Duffin90169ba2020-01-10 17:16:44 +00002043
2044func TestJavaSystemModulesImport(t *testing.T) {
2045 ctx, _ := testJava(t, `
2046 java_system_modules_import {
2047 name: "system-modules",
2048 libs: ["system-module1", "system-module2"],
2049 }
2050 java_import {
2051 name: "system-module1",
2052 jars: ["a.jar"],
2053 }
2054 java_import {
2055 name: "system-module2",
2056 jars: ["b.jar"],
2057 }
2058 `)
2059
2060 // check the existence of the module
2061 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2062
2063 cmd := systemModules.Rule("jarsTosystemModules")
2064
2065 // make sure the command compiles against the supplied modules.
2066 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2067 if !strings.Contains(cmd.Args["classpath"], module) {
2068 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2069 module)
2070 }
2071 }
2072}
Paul Duffin83a2d962019-11-19 19:44:10 +00002073
2074func TestJavaLibraryWithSystemModules(t *testing.T) {
2075 ctx, _ := testJava(t, `
2076 java_library {
2077 name: "lib-with-source-system-modules",
2078 srcs: [
2079 "a.java",
2080 ],
2081 sdk_version: "none",
2082 system_modules: "source-system-modules",
2083 }
2084
2085 java_library {
2086 name: "source-jar",
2087 srcs: [
2088 "a.java",
2089 ],
2090 }
2091
2092 java_system_modules {
2093 name: "source-system-modules",
2094 libs: ["source-jar"],
2095 }
2096
2097 java_library {
2098 name: "lib-with-prebuilt-system-modules",
2099 srcs: [
2100 "a.java",
2101 ],
2102 sdk_version: "none",
2103 system_modules: "prebuilt-system-modules",
2104 }
2105
2106 java_import {
2107 name: "prebuilt-jar",
2108 jars: ["a.jar"],
2109 }
2110
2111 java_system_modules_import {
2112 name: "prebuilt-system-modules",
2113 libs: ["prebuilt-jar"],
2114 }
2115 `)
2116
2117 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2118
2119 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2120}
2121
2122func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2123 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2124 bootClasspath := javacRule.Args["bootClasspath"]
2125 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2126 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2127 }
2128}
Jiyong Park19604de2020-03-24 16:44:11 +09002129
2130func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2131 ctx, _ := testJava(t, `
2132 java_library {
2133 name: "foo",
2134 srcs: ["aidl/foo/IFoo.aidl"],
2135 libs: ["bar"],
2136 }
2137
2138 java_import {
2139 name: "bar",
2140 jars: ["a.jar"],
2141 aidl: {
2142 export_include_dirs: ["aidl/bar"],
2143 },
2144 }
2145 `)
2146
2147 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2148 expectedAidlFlag := "-Iaidl/bar"
2149 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2150 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2151 }
2152}
Liz Kammerdd849a82020-06-12 16:38:45 -07002153
2154func TestDataNativeBinaries(t *testing.T) {
2155 ctx, config := testJava(t, `
2156 java_test_host {
2157 name: "foo",
2158 srcs: ["a.java"],
2159 data_native_bins: ["bin"]
2160 }
2161
2162 python_binary_host {
2163 name: "bin",
2164 srcs: ["bin.py"],
2165 }
2166 `)
2167
2168 buildOS := android.BuildOs.String()
2169
2170 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2171 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
2172 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2173 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2174 if !reflect.DeepEqual(expected, actual) {
2175 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2176 }
2177}