blob: bb51ebc3d709030a28a336641a64f0b3f04e66b8 [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 (
Paul Duffin1a393322020-11-18 16:36:47 +000018 "fmt"
Colin Cross72bb3632017-07-13 16:23:21 -070019 "io/ioutil"
20 "os"
21 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070022 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010023 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070024 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070025 "strings"
26 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070027
Jeongik Cha28df2572019-11-11 10:46:36 +090028 "github.com/google/blueprint/proptools"
29
Colin Crossa4f08812018-10-02 22:03:40 -070030 "android/soong/android"
31 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080032 "android/soong/dexpreopt"
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 {
Paul Duffin9f045242021-01-21 15:05:11 +000062 return TestConfig(buildDir, env, bp, fs)
Colin Cross1369cdb2017-09-29 17:58:17 -070063}
64
Colin Crossae8600b2020-10-29 17:09:13 -070065func testContext(config android.Config) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070066
Colin Crossae8600b2020-10-29 17:09:13 -070067 ctx := android.NewTestArchContext(config)
Paul Duffinc059c8c2021-01-20 17:13:52 +000068 RegisterRequiredBuildComponentsForTest(ctx)
Colin Cross4b49b762019-11-22 15:25:03 -080069 ctx.RegisterModuleType("java_plugin", PluginFactory)
Colin Cross4b49b762019-11-22 15:25:03 -080070 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Liz Kammerdd849a82020-06-12 16:38:45 -070071 ctx.RegisterModuleType("python_binary_host", python.PythonBinaryHostFactory)
Colin Cross89536d42017-07-07 14:35:50 -070072 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Paul Duffin44f1d842020-06-26 20:17:02 +010073 ctx.PreArchMutators(android.RegisterComponentsMutator)
Paul Duffina48f7582019-12-19 11:25:19 +000074
Liz Kammerdd849a82020-06-12 16:38:45 -070075 ctx.PreDepsMutators(python.RegisterPythonPreDepsMutators)
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070076 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Paul Duffineafc16b2021-02-24 01:43:18 +000077 ctx.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
78 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
Colin Crossa4f08812018-10-02 22:03:40 -070079
Paul Duffin021f4e52020-07-30 16:04:17 +010080 android.RegisterPrebuiltMutators(ctx)
81
Colin Crossa4f08812018-10-02 22:03:40 -070082 // Register module types and mutators from cc needed for JNI testing
Paul Duffin77980a82019-12-19 16:01:36 +000083 cc.RegisterRequiredBuildComponentsForTest(ctx)
Colin Crossa4f08812018-10-02 22:03:40 -070084
Jaewoong Jungc779cd42020-10-06 18:56:10 -070085 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
86 ctx.TopDown("propagate_rro_enforcement", propagateRROEnforcementMutator).Parallel()
87 })
88
Colin Cross527012a2017-11-30 22:56:16 -080089 return ctx
90}
91
92func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -080093 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -080094
Colin Cross98be1bb2019-12-13 20:41:13 -080095 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000096 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Colin Crossc28bb0b2019-02-25 14:20:47 -080097
Colin Crossae8600b2020-10-29 17:09:13 -070098 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +010099 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800100 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700101 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800102 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800103}
104
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900105func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900106 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800107 return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil))
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900108}
109
Colin Cross98be1bb2019-12-13 20:41:13 -0800110func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900111 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700112 ctx := testContext(config)
Jeongik Cha538c0d02019-07-11 15:54:27 +0900113
Colin Cross98be1bb2019-12-13 20:41:13 -0800114 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000115 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Jeongik Cha538c0d02019-07-11 15:54:27 +0900116
Paul Duffinec0fe172021-02-25 15:34:13 +0000117 runWithErrors(t, ctx, config, pattern)
118
119 return ctx, config
120}
121
122func runWithErrors(t *testing.T, ctx *android.TestContext, config android.Config, pattern string) {
Colin Crossae8600b2020-10-29 17:09:13 -0700123 ctx.Register()
Jeongik Cha538c0d02019-07-11 15:54:27 +0900124 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
125 if len(errs) > 0 {
126 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000127 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900128 }
129 _, errs = ctx.PrepareBuildActions(config)
130 if len(errs) > 0 {
131 android.FailIfNoMatchingErrors(t, pattern, errs)
Paul Duffinec0fe172021-02-25 15:34:13 +0000132 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900133 }
134
135 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
Paul Duffinec0fe172021-02-25 15:34:13 +0000136 return
Jeongik Cha538c0d02019-07-11 15:54:27 +0900137}
138
Colin Cross238c1f32020-06-07 16:58:18 -0700139func testJavaWithFS(t *testing.T, bp string, fs map[string][]byte) (*android.TestContext, android.Config) {
140 t.Helper()
141 return testJavaWithConfig(t, testConfig(nil, bp, fs))
142}
143
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700144func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800145 t.Helper()
Colin Cross238c1f32020-06-07 16:58:18 -0700146 return testJavaWithFS(t, bp, nil)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900147}
148
Colin Cross98be1bb2019-12-13 20:41:13 -0800149func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900150 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -0700151 ctx := testContext(config)
Colin Cross527012a2017-11-30 22:56:16 -0800152 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700153
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700154 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700155}
156
Colin Cross2acdae82017-09-15 19:44:24 -0700157func moduleToPath(name string) string {
158 switch {
159 case name == `""`:
160 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700161 case strings.HasSuffix(name, ".jar"):
162 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700163 default:
164 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700165 }
166}
167
Jeongik Chae403e9e2019-12-07 00:16:24 +0900168func TestJavaLinkType(t *testing.T) {
169 testJava(t, `
170 java_library {
171 name: "foo",
172 srcs: ["a.java"],
173 libs: ["bar"],
174 static_libs: ["baz"],
175 }
176
177 java_library {
178 name: "bar",
179 sdk_version: "current",
180 srcs: ["b.java"],
181 }
182
183 java_library {
184 name: "baz",
185 sdk_version: "system_current",
186 srcs: ["c.java"],
187 }
188 `)
189
Steven Moreland00298982020-11-17 21:44:36 +0000190 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900191 java_library {
192 name: "foo",
193 srcs: ["a.java"],
194 libs: ["bar"],
195 sdk_version: "current",
196 static_libs: ["baz"],
197 }
198
199 java_library {
200 name: "bar",
201 sdk_version: "current",
202 srcs: ["b.java"],
203 }
204
205 java_library {
206 name: "baz",
207 sdk_version: "system_current",
208 srcs: ["c.java"],
209 }
210 `)
211
212 testJava(t, `
213 java_library {
214 name: "foo",
215 srcs: ["a.java"],
216 libs: ["bar"],
217 sdk_version: "system_current",
218 static_libs: ["baz"],
219 }
220
221 java_library {
222 name: "bar",
223 sdk_version: "current",
224 srcs: ["b.java"],
225 }
226
227 java_library {
228 name: "baz",
229 sdk_version: "system_current",
230 srcs: ["c.java"],
231 }
232 `)
233
Steven Moreland00298982020-11-17 21:44:36 +0000234 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900235 java_library {
236 name: "foo",
237 srcs: ["a.java"],
238 libs: ["bar"],
239 sdk_version: "system_current",
240 static_libs: ["baz"],
241 }
242
243 java_library {
244 name: "bar",
245 sdk_version: "current",
246 srcs: ["b.java"],
247 }
248
249 java_library {
250 name: "baz",
251 srcs: ["c.java"],
252 }
253 `)
254}
255
Colin Cross72bb3632017-07-13 16:23:21 -0700256func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700257 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700258 java_library {
259 name: "foo",
260 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700261 libs: ["bar"],
262 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700263 }
264
265 java_library {
266 name: "bar",
267 srcs: ["b.java"],
268 }
269
270 java_library {
271 name: "baz",
272 srcs: ["c.java"],
273 }
Colin Crossd5934c82017-10-02 13:55:26 -0700274 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700275
Colin Cross4c428df2017-09-15 17:36:05 -0700276 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700277 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700278
279 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
280 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
281 }
282
Colin Cross1ee23172017-10-18 14:44:18 -0700283 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700284 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
285 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700286
Nan Zhanged19fc32017-10-19 13:06:22 -0700287 if !strings.Contains(javac.Args["classpath"], barTurbine) {
288 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700289 }
290
Nan Zhanged19fc32017-10-19 13:06:22 -0700291 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
292 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700293 }
294
295 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
296 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700297 }
298}
299
Artur Satayev9cf46692019-11-26 18:08:34 +0000300func TestExportedPlugins(t *testing.T) {
301 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800302 library string
303 processors string
304 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000305 }
306 var tests = []struct {
307 name string
308 extra string
309 results []Result
310 }{
311 {
312 name: "Exported plugin is not a direct plugin",
313 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
314 results: []Result{{library: "exports", processors: "-proc:none"}},
315 },
316 {
317 name: "Exports plugin to dependee",
318 extra: `
319 java_library{name: "exports", exported_plugins: ["plugin"]}
320 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
321 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
322 `,
323 results: []Result{
324 {library: "foo", processors: "-processor com.android.TestPlugin"},
325 {library: "bar", processors: "-processor com.android.TestPlugin"},
326 },
327 },
328 {
329 name: "Exports plugin to android_library",
330 extra: `
331 java_library{name: "exports", exported_plugins: ["plugin"]}
332 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
333 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
334 `,
335 results: []Result{
336 {library: "foo", processors: "-processor com.android.TestPlugin"},
337 {library: "bar", processors: "-processor com.android.TestPlugin"},
338 },
339 },
340 {
341 name: "Exports plugin is not propagated via transitive deps",
342 extra: `
343 java_library{name: "exports", exported_plugins: ["plugin"]}
344 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
345 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
346 `,
347 results: []Result{
348 {library: "foo", processors: "-processor com.android.TestPlugin"},
349 {library: "bar", processors: "-proc:none"},
350 },
351 },
352 {
353 name: "Exports plugin appends to plugins",
354 extra: `
355 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
356 java_library{name: "exports", exported_plugins: ["plugin"]}
357 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
358 `,
359 results: []Result{
360 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
361 },
362 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800363 {
364 name: "Exports plugin to with generates_api to dependee",
365 extra: `
366 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
367 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
368 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
369 `,
370 results: []Result{
371 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
372 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
373 },
374 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000375 }
376
377 for _, test := range tests {
378 t.Run(test.name, func(t *testing.T) {
379 ctx, _ := testJava(t, `
380 java_plugin {
381 name: "plugin",
382 processor_class: "com.android.TestPlugin",
383 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800384 java_plugin {
385 name: "plugin_generates_api",
386 generates_api: true,
387 processor_class: "com.android.TestPlugin",
388 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000389 `+test.extra)
390
391 for _, want := range test.results {
392 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
393 if javac.Args["processor"] != want.processors {
394 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
395 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800396 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
397 disableTurbine := turbine.BuildParams.Rule == nil
398 if disableTurbine != want.disableTurbine {
399 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
400 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000401 }
402 })
403 }
404}
405
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900406func TestSdkVersionByPartition(t *testing.T) {
407 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900408 java_library {
409 name: "foo",
410 srcs: ["a.java"],
411 vendor: true,
412 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900413 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900414
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900415 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900416 java_library {
417 name: "bar",
418 srcs: ["b.java"],
419 }
420 `)
421
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900422 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900423 bp := `
424 java_library {
425 name: "foo",
426 srcs: ["a.java"],
427 product_specific: true,
428 }
429 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800430
431 config := testConfig(nil, bp, nil)
432 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900433 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -0800434 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 +0900435 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -0800436 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900437 }
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900438 }
439}
440
Colin Crossd5934c82017-10-02 13:55:26 -0700441func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700442 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700443 java_library {
444 name: "foo",
445 srcs: ["a.java"],
446 target: {
447 android: {
448 srcs: ["b.java"],
449 },
450 },
451 }
452 `)
453
454 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
455 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
456 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
457 }
458}
459
Colin Cross6b4a32d2017-12-05 13:42:45 -0800460func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700461 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800462 java_library_host {
463 name: "foo",
464 srcs: ["a.java"],
465 }
466
467 java_binary_host {
468 name: "bar",
469 srcs: ["b.java"],
470 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700471 jni_libs: ["libjni"],
472 }
473
474 cc_library_shared {
475 name: "libjni",
476 host_supported: true,
477 device_supported: false,
478 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800479 }
480 `)
481
482 buildOS := android.BuildOs.String()
483
484 bar := ctx.ModuleForTests("bar", buildOS+"_common")
485 barJar := bar.Output("bar.jar").Output.String()
486 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
487 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
488
Colin Cross89226d92020-10-09 19:00:54 -0700489 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
490 libjniSO := libjni.Rule("Cp").Output.String()
491
Colin Cross6b4a32d2017-12-05 13:42:45 -0800492 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700493 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
494 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800495 }
Colin Cross89226d92020-10-09 19:00:54 -0700496
497 // Test that the install binary wrapper depends on the installed JNI libraries
498 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
499 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800500 }
Alex Humesky2070e322020-06-09 20:23:08 -0400501}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800502
Alex Humesky2070e322020-06-09 20:23:08 -0400503func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
504 bp := `
505 java_library {
506 name: "target_library",
507 srcs: ["a.java"],
508 }
509
510 java_binary_host {
511 name: "host_binary",
512 srcs: ["b.java"],
513 }
514 `
515 config := testConfig(nil, bp, nil)
516 config.TestProductVariables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
517
518 ctx, _ := testJavaWithConfig(t, config)
519
Liz Kammer7941b302020-07-28 13:27:34 -0700520 // first, check that the -g flag is added to target modules
Alex Humesky2070e322020-06-09 20:23:08 -0400521 targetLibrary := ctx.ModuleForTests("target_library", "android_common")
522 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
523 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
524 t.Errorf("target library javac flags %v should contain "+
525 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
526 }
527
528 // check that -g is not overridden for host modules
529 buildOS := android.BuildOs.String()
530 hostBinary := ctx.ModuleForTests("host_binary", buildOS+"_common")
531 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
532 if strings.Contains(hostJavaFlags, "-g:source,lines") {
533 t.Errorf("java_binary_host javac flags %v should not have "+
534 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
535 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800536}
537
Colin Cross72bb3632017-07-13 16:23:21 -0700538func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700539 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700540 java_library {
541 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000542 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000543 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700544 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700545 }
546
Colin Cross74d73e22017-08-02 11:05:49 -0700547 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700548 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700549 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700550 }
551
Colin Cross74d73e22017-08-02 11:05:49 -0700552 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700553 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700554 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700555 sdk_version: "current",
556 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700557 }
Colin Cross42be7612019-02-21 18:12:14 -0800558
559 dex_import {
560 name: "qux",
561 jars: ["b.jar"],
562 }
Colin Cross79c7c262019-04-17 11:11:46 -0700563
564 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000565 name: "sdklib",
566 public: {
567 jars: ["c.jar"],
568 },
569 }
570
Paul Duffin91547182019-11-12 19:39:36 +0000571 prebuilt_stubs_sources {
572 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000573 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000574 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000575
576 java_test_import {
577 name: "test",
578 jars: ["a.jar"],
579 test_suites: ["cts"],
580 test_config: "AndroidTest.xml",
581 }
Colin Cross72bb3632017-07-13 16:23:21 -0700582 `)
583
Paul Duffin9b478b02019-12-10 13:41:51 +0000584 fooModule := ctx.ModuleForTests("foo", "android_common")
585 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700586 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700587 barModule := ctx.ModuleForTests("bar", "android_common")
588 barJar := barModule.Rule("combineJar").Output
589 bazModule := ctx.ModuleForTests("baz", "android_common")
590 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700591 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700592
Paul Duffin9b478b02019-12-10 13:41:51 +0000593 fooLibrary := fooModule.Module().(*Library)
594 assertDeepEquals(t, "foo java sources incorrect",
595 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000596
Paul Duffin9b478b02019-12-10 13:41:51 +0000597 assertDeepEquals(t, "foo java source jars incorrect",
598 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
599 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000600
Colin Cross37f6d792018-07-12 12:28:41 -0700601 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
602 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700603 }
604
Liz Kammerd6c31d22020-08-05 15:40:41 -0700605 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
606 if barDexJar != nil {
607 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
608 }
609
Colin Cross79c7c262019-04-17 11:11:46 -0700610 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
611 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
612 }
613
Colin Cross37f6d792018-07-12 12:28:41 -0700614 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
615 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700616 }
Colin Cross42be7612019-02-21 18:12:14 -0800617
Liz Kammerd6c31d22020-08-05 15:40:41 -0700618 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().String()
619 expectedDexJar := buildDir + "/.intermediates/baz/android_common/dex/baz.jar"
620 if bazDexJar != expectedDexJar {
621 t.Errorf("baz dex jar build path expected %q, got %q", expectedDexJar, bazDexJar)
622 }
623
Colin Cross42be7612019-02-21 18:12:14 -0800624 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700625}
626
Paul Duffin9b478b02019-12-10 13:41:51 +0000627func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
628 if !reflect.DeepEqual(expected, actual) {
629 t.Errorf("%s: expected %q, found %q", message, expected, actual)
630 }
631}
632
Paul Duffin1a393322020-11-18 16:36:47 +0000633func TestPrebuiltStubsSources(t *testing.T) {
634 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
635 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
636prebuilt_stubs_sources {
637 name: "stubs-source",
638 srcs: ["%s"],
639}`, sourcesPath), map[string][]byte{
640 "stubs/sources/pkg/A.java": nil,
641 "stubs/sources/pkg/B.java": nil,
642 })
643
644 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
645 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
646 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
647 }
648 }
649
650 t.Run("empty/missing directory", func(t *testing.T) {
651 test(t, "empty-directory", []string{})
652 })
653
654 t.Run("non-empty set of sources", func(t *testing.T) {
655 test(t, "stubs/sources", []string{
656 "stubs/sources/pkg/A.java",
657 "stubs/sources/pkg/B.java",
658 })
659 })
660}
661
Paul Duffin56d44902020-01-31 13:36:25 +0000662func TestJavaSdkLibraryImport(t *testing.T) {
663 ctx, _ := testJava(t, `
664 java_library {
665 name: "foo",
666 srcs: ["a.java"],
667 libs: ["sdklib"],
668 sdk_version: "current",
669 }
670
671 java_library {
672 name: "foo.system",
673 srcs: ["a.java"],
674 libs: ["sdklib"],
675 sdk_version: "system_current",
676 }
677
678 java_library {
679 name: "foo.test",
680 srcs: ["a.java"],
681 libs: ["sdklib"],
682 sdk_version: "test_current",
683 }
684
685 java_sdk_library_import {
686 name: "sdklib",
687 public: {
688 jars: ["a.jar"],
689 },
690 system: {
691 jars: ["b.jar"],
692 },
693 test: {
694 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100695 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000696 },
697 }
698 `)
699
700 for _, scope := range []string{"", ".system", ".test"} {
701 fooModule := ctx.ModuleForTests("foo"+scope, "android_common")
702 javac := fooModule.Rule("javac")
703
704 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
705 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
706 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
707 }
708 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100709
Paul Duffincee7e662020-07-09 17:32:57 +0100710 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100711 `prebuilt_sdklib.stubs`,
712 `prebuilt_sdklib.stubs.source.test`,
713 `prebuilt_sdklib.stubs.system`,
714 `prebuilt_sdklib.stubs.test`,
715 })
716}
717
718func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
719 ctx, _ := testJava(t, `
720 java_sdk_library {
721 name: "sdklib",
722 srcs: ["a.java"],
723 sdk_version: "none",
724 system_modules: "none",
725 public: {
726 enabled: true,
727 },
728 }
729
730 java_sdk_library_import {
731 name: "sdklib",
732 public: {
733 jars: ["a.jar"],
734 },
735 }
736 `)
737
Paul Duffincee7e662020-07-09 17:32:57 +0100738 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100739 `dex2oatd`,
740 `prebuilt_sdklib`,
741 `sdklib.impl`,
742 `sdklib.stubs`,
743 `sdklib.stubs.source`,
744 `sdklib.xml`,
745 })
746
Paul Duffincee7e662020-07-09 17:32:57 +0100747 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100748 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100749 `sdklib.impl`,
750 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
751 // dependency is added after prebuilts may have been renamed and so has to use
752 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100753 `sdklib.xml`,
754 })
755}
756
757func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
758 ctx, _ := testJava(t, `
759 java_sdk_library {
760 name: "sdklib",
761 srcs: ["a.java"],
762 sdk_version: "none",
763 system_modules: "none",
764 public: {
765 enabled: true,
766 },
767 }
768
769 java_sdk_library_import {
770 name: "sdklib",
771 prefer: true,
772 public: {
773 jars: ["a.jar"],
774 },
775 }
776 `)
777
Paul Duffincee7e662020-07-09 17:32:57 +0100778 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100779 `dex2oatd`,
780 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100781 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100782 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100783 `sdklib.stubs.source`,
784 `sdklib.xml`,
785 })
786
Paul Duffincee7e662020-07-09 17:32:57 +0100787 CheckModuleDependencies(t, ctx, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100788 `prebuilt_sdklib.stubs`,
789 `sdklib.impl`,
790 `sdklib.xml`,
791 })
Paul Duffin56d44902020-01-31 13:36:25 +0000792}
793
JaeMan Parkff715562020-10-19 17:25:58 +0900794func TestJavaSdkLibraryEnforce(t *testing.T) {
795 partitionToBpOption := func(partition string) string {
796 switch partition {
797 case "system":
798 return ""
799 case "vendor":
800 return "soc_specific: true,"
801 case "product":
802 return "product_specific: true,"
803 default:
804 panic("Invalid partition group name: " + partition)
805 }
806 }
807
808 type testConfigInfo struct {
809 libraryType string
810 fromPartition string
811 toPartition string
812 enforceVendorInterface bool
813 enforceProductInterface bool
814 enforceJavaSdkLibraryCheck bool
815 allowList []string
816 }
817
818 createTestConfig := func(info testConfigInfo) android.Config {
819 bpFileTemplate := `
820 java_library {
821 name: "foo",
822 srcs: ["foo.java"],
823 libs: ["bar"],
824 sdk_version: "current",
825 %s
826 }
827
828 %s {
829 name: "bar",
830 srcs: ["bar.java"],
831 sdk_version: "current",
832 %s
833 }
834 `
835
836 bpFile := fmt.Sprintf(bpFileTemplate,
837 partitionToBpOption(info.fromPartition),
838 info.libraryType,
839 partitionToBpOption(info.toPartition))
840
841 config := testConfig(nil, bpFile, nil)
842 configVariables := config.TestProductVariables
843
844 configVariables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
845 if info.enforceVendorInterface {
846 configVariables.DeviceVndkVersion = proptools.StringPtr("current")
847 }
848 configVariables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
849 configVariables.InterPartitionJavaLibraryAllowList = info.allowList
850
851 return config
852 }
853
JaeMan Parkff715562020-10-19 17:25:58 +0900854 errorMessage := "is not allowed across the partitions"
855
JaeMan Park90e75352021-01-14 11:56:56 +0900856 testJavaWithConfig(t, createTestConfig(testConfigInfo{
857 libraryType: "java_library",
858 fromPartition: "product",
859 toPartition: "system",
860 enforceVendorInterface: true,
861 enforceProductInterface: true,
862 enforceJavaSdkLibraryCheck: false,
863 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900864
JaeMan Park90e75352021-01-14 11:56:56 +0900865 testJavaWithConfig(t, createTestConfig(testConfigInfo{
866 libraryType: "java_library",
867 fromPartition: "product",
868 toPartition: "system",
869 enforceVendorInterface: true,
870 enforceProductInterface: false,
871 enforceJavaSdkLibraryCheck: true,
872 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900873
JaeMan Park90e75352021-01-14 11:56:56 +0900874 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
875 libraryType: "java_library",
876 fromPartition: "product",
877 toPartition: "system",
878 enforceVendorInterface: true,
879 enforceProductInterface: true,
880 enforceJavaSdkLibraryCheck: true,
881 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900882
JaeMan Park90e75352021-01-14 11:56:56 +0900883 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
884 libraryType: "java_library",
885 fromPartition: "vendor",
886 toPartition: "system",
887 enforceVendorInterface: true,
888 enforceProductInterface: true,
889 enforceJavaSdkLibraryCheck: true,
890 }))
JaeMan Parkff715562020-10-19 17:25:58 +0900891
892 testJavaWithConfig(t, createTestConfig(testConfigInfo{
893 libraryType: "java_library",
894 fromPartition: "vendor",
895 toPartition: "system",
896 enforceVendorInterface: true,
897 enforceProductInterface: true,
898 enforceJavaSdkLibraryCheck: true,
899 allowList: []string{"bar"},
900 }))
901
902 testJavaErrorWithConfig(t, errorMessage, createTestConfig(testConfigInfo{
903 libraryType: "java_library",
904 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900905 toPartition: "product",
906 enforceVendorInterface: true,
907 enforceProductInterface: true,
908 enforceJavaSdkLibraryCheck: true,
909 }))
910
911 testJavaWithConfig(t, createTestConfig(testConfigInfo{
912 libraryType: "java_sdk_library",
913 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +0900914 toPartition: "system",
915 enforceVendorInterface: true,
916 enforceProductInterface: true,
917 enforceJavaSdkLibraryCheck: true,
JaeMan Park90e75352021-01-14 11:56:56 +0900918 }))
919
920 testJavaWithConfig(t, createTestConfig(testConfigInfo{
921 libraryType: "java_sdk_library",
922 fromPartition: "vendor",
923 toPartition: "system",
924 enforceVendorInterface: true,
925 enforceProductInterface: true,
926 enforceJavaSdkLibraryCheck: true,
927 }))
928
929 testJavaWithConfig(t, createTestConfig(testConfigInfo{
930 libraryType: "java_sdk_library",
931 fromPartition: "vendor",
932 toPartition: "product",
933 enforceVendorInterface: true,
934 enforceProductInterface: true,
935 enforceJavaSdkLibraryCheck: true,
JaeMan Parkff715562020-10-19 17:25:58 +0900936 }))
937}
938
Colin Cross89536d42017-07-07 14:35:50 -0700939func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700940 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700941 java_defaults {
942 name: "defaults",
943 srcs: ["a.java"],
944 libs: ["bar"],
945 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700946 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700947 }
948
949 java_library {
950 name: "foo",
951 defaults: ["defaults"],
952 }
953
954 java_library {
955 name: "bar",
956 srcs: ["b.java"],
957 }
958
959 java_library {
960 name: "baz",
961 srcs: ["c.java"],
962 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700963
964 android_test {
965 name: "atestOptimize",
966 defaults: ["defaults"],
967 optimize: {enabled: true},
968 }
969
970 android_test {
971 name: "atestNoOptimize",
972 defaults: ["defaults"],
973 }
974
975 android_test {
976 name: "atestDefault",
977 srcs: ["a.java"],
978 }
Colin Cross89536d42017-07-07 14:35:50 -0700979 `)
980
Colin Cross4c428df2017-09-15 17:36:05 -0700981 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700982 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700983
984 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
985 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
986 }
987
Nan Zhanged19fc32017-10-19 13:06:22 -0700988 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
989 if !strings.Contains(javac.Args["classpath"], barTurbine) {
990 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700991 }
992
Colin Cross1ee23172017-10-18 14:44:18 -0700993 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700994 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
995 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700996 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700997
998 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
999 if atestOptimize.Output == nil {
1000 t.Errorf("atestOptimize should optimize APK")
1001 }
1002
1003 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1004 if atestNoOptimize.Output == nil {
1005 t.Errorf("atestNoOptimize should not optimize APK")
1006 }
1007
1008 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1009 if atestDefault.Output == nil {
1010 t.Errorf("atestDefault should optimize APK")
1011 }
Colin Cross89536d42017-07-07 14:35:50 -07001012}
1013
Colin Cross0f37af02017-09-27 17:42:05 -07001014func TestResources(t *testing.T) {
1015 var table = []struct {
1016 name string
1017 prop string
1018 extra string
1019 args string
1020 }{
1021 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001022 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001023 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001024 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001025 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001026 },
1027 {
1028 // Test that a module with java_resources includes the files
1029 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001030 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1031 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001032 },
1033 {
1034 // Test that a module with a filegroup in java_resources includes the files with the
1035 // path prefix
1036 name: "resource filegroup",
1037 prop: `java_resources: [":foo-res"]`,
1038 extra: `
1039 filegroup {
1040 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001041 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001042 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001043 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001044 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001045 },
1046 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001047 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1048 name: "wildcard dirs",
1049 prop: `java_resource_dirs: ["java-res/*"]`,
1050 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1051 },
1052 {
1053 // Test that a module exclude_java_resource_dirs excludes the files
1054 name: "wildcard dirs",
1055 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1056 args: "-C java-res/a -f java-res/a/a",
1057 },
Colin Crosscedd4762018-09-13 11:26:19 -07001058 {
1059 // Test wildcards in java_resources
1060 name: "wildcard files",
1061 prop: `java_resources: ["java-res/**/*"]`,
1062 args: "-C . -f java-res/a/a -f java-res/b/b",
1063 },
1064 {
1065 // Test exclude_java_resources with java_resources
1066 name: "wildcard files with exclude",
1067 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1068 args: "-C . -f java-res/a/a",
1069 },
1070 {
1071 // Test exclude_java_resources with java_resource_dirs
1072 name: "resource dirs with exclude files",
1073 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1074 args: "-C java-res -f java-res/a/a",
1075 },
1076 {
1077 // Test exclude_java_resource_dirs with java_resource_dirs
1078 name: "resource dirs with exclude files",
1079 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1080 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1081 },
Colin Cross0f37af02017-09-27 17:42:05 -07001082 }
1083
1084 for _, test := range table {
1085 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001086 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001087 java_library {
1088 name: "foo",
1089 srcs: [
1090 "a.java",
1091 "b.java",
1092 "c.java",
1093 ],
1094 `+test.prop+`,
1095 }
Colin Cross238c1f32020-06-07 16:58:18 -07001096 `+test.extra,
1097 map[string][]byte{
1098 "java-res/a/a": nil,
1099 "java-res/b/b": nil,
1100 "java-res2/a": nil,
1101 },
1102 )
Colin Cross0f37af02017-09-27 17:42:05 -07001103
Colin Cross331a1212018-08-15 20:40:52 -07001104 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001105 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001106
1107 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1108 t.Errorf("foo combined jars %v does not contain %q",
1109 foo.Inputs.Strings(), fooRes.Output.String())
1110 }
1111
Colin Crossaf9c55b2017-10-03 14:50:08 -07001112 if fooRes.Args["jarArgs"] != test.args {
1113 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001114 fooRes.Args["jarArgs"], test.args)
1115 }
1116 })
1117 }
1118}
1119
Colin Cross0c4ce212019-05-03 15:28:19 -07001120func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001121 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001122 java_library {
1123 name: "foo",
1124 srcs: [
1125 "a.java",
1126 "b.java",
1127 "c.java",
1128 ],
1129 include_srcs: true,
1130 }
1131
1132 java_library {
1133 name: "bar",
1134 srcs: [
1135 "a.java",
1136 "b.java",
1137 "c.java",
1138 ],
1139 java_resource_dirs: ["java-res"],
1140 include_srcs: true,
1141 }
Colin Cross238c1f32020-06-07 16:58:18 -07001142 `, map[string][]byte{
1143 "java-res/a/a": nil,
1144 "java-res/b/b": nil,
1145 "java-res2/a": nil,
1146 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001147
1148 // Test a library with include_srcs: true
1149 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1150 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1151
1152 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1153 t.Errorf("foo combined jars %v does not contain %q", w, g)
1154 }
1155
1156 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1157 t.Errorf("foo source jar args %q is not %q", w, g)
1158 }
1159
1160 // Test a library with include_srcs: true and resources
1161 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1162 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1163 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1164 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1165
1166 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1167 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1168 }
1169
1170 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1171 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1172 }
1173
1174 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1175 t.Errorf("bar combined jars %v does not contain %q", w, g)
1176 }
1177
1178 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1179 t.Errorf("bar source jar args %q is not %q", w, g)
1180 }
1181
1182 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1183 t.Errorf("bar resource jar args %q is not %q", w, g)
1184 }
1185}
1186
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001187func TestJavaLint(t *testing.T) {
1188 ctx, _ := testJavaWithFS(t, `
1189 java_library {
1190 name: "foo",
1191 srcs: [
1192 "a.java",
1193 "b.java",
1194 "c.java",
1195 ],
1196 min_sdk_version: "29",
1197 sdk_version: "system_current",
1198 }
1199 `, map[string][]byte{
1200 "lint-baseline.xml": nil,
1201 })
1202
1203 foo := ctx.ModuleForTests("foo", "android_common")
1204 rule := foo.Rule("lint")
1205
1206 if !strings.Contains(rule.RuleParams.Command, "--baseline lint-baseline.xml") {
1207 t.Error("did not pass --baseline flag")
1208 }
1209}
1210
1211func TestJavaLintWithoutBaseline(t *testing.T) {
1212 ctx, _ := testJavaWithFS(t, `
1213 java_library {
1214 name: "foo",
1215 srcs: [
1216 "a.java",
1217 "b.java",
1218 "c.java",
1219 ],
1220 min_sdk_version: "29",
1221 sdk_version: "system_current",
1222 }
1223 `, map[string][]byte{})
1224
1225 foo := ctx.ModuleForTests("foo", "android_common")
1226 rule := foo.Rule("lint")
1227
1228 if strings.Contains(rule.RuleParams.Command, "--baseline") {
1229 t.Error("passed --baseline flag for non existent file")
1230 }
1231}
1232
1233func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
1234 config := testConfig(
1235 nil,
1236 `
1237 java_library {
1238 name: "foo",
1239 srcs: [
1240 ],
1241 min_sdk_version: "29",
1242 sdk_version: "system_current",
1243 lint: {
1244 baseline_filename: "mybaseline.xml",
1245 },
1246 }
1247 `, map[string][]byte{
1248 "build/soong/java/lint_defaults.txt": nil,
1249 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1250 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1251 "framework/aidl": nil,
1252 "a.java": nil,
1253 "AndroidManifest.xml": nil,
1254 "build/make/target/product/security": nil,
1255 })
1256 config.TestAllowNonExistentPaths = false
1257 testJavaErrorWithConfig(t,
1258 "source path \"mybaseline.xml\" does not exist",
1259 config,
1260 )
1261}
1262
1263func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1264 ctx, _ := testJavaWithFS(t, `
1265 java_library {
1266 name: "foo",
1267 srcs: [
1268 "a.java",
1269 "b.java",
1270 "c.java",
1271 ],
1272 min_sdk_version: "29",
1273 sdk_version: "system_current",
1274 lint: {
1275 error_checks: ["SomeCheck"],
1276 baseline_filename: "mybaseline.xml",
1277 },
1278 }
1279 `, map[string][]byte{
1280 "mybaseline.xml": nil,
1281 })
1282
1283 foo := ctx.ModuleForTests("foo", "android_common")
1284 rule := foo.Rule("lint")
1285
1286 if !strings.Contains(rule.RuleParams.Command, "--baseline mybaseline.xml") {
1287 t.Error("did not use the correct file for baseline")
1288 }
1289}
1290
Colin Cross54190b32017-10-09 15:34:10 -07001291func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001292 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001293 java_library {
1294 name: "foo",
1295 srcs: [
1296 "a*.java",
1297 ":gen",
1298 "b*.java",
1299 ],
1300 }
1301
1302 genrule {
1303 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001304 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001305 out: ["gen.java"],
1306 }
Colin Cross238c1f32020-06-07 16:58:18 -07001307 `, map[string][]byte{
1308 "a.java": nil,
1309 "b.java": nil,
1310 })
Colin Cross54190b32017-10-09 15:34:10 -07001311
1312 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1313 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1314
Colin Cross15e86d92017-10-20 15:07:08 -07001315 if filepath.Base(genrule.Output.String()) != "gen.java" {
1316 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001317 }
1318
1319 if len(javac.Inputs) != 3 ||
1320 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001321 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001322 javac.Inputs[2].String() != "b.java" {
1323 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1324 }
1325}
1326
Nan Zhang61eaedb2017-11-02 13:28:15 -07001327func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001328 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001329 java_library {
1330 name: "foo",
1331 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001332 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001333 }
1334
1335 java_library {
1336 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001337 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001338 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001339 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001340 }
1341
1342 java_library {
1343 name: "baz",
1344 srcs: ["c.java"],
1345 libs: ["bar"],
1346 sdk_version: "14",
1347 }
1348 `)
1349
1350 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
1351 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
1352 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1353 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
1354 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1355
1356 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
1357 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
1358 }
1359
1360 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1361 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
1362 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
1363 }
1364 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
1365 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
1366 }
1367 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
1368 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
1369 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001370 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -07001371 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +01001372 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001373 }
1374}
1375
1376func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001377 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001378 java_library {
1379 name: "bar",
1380 srcs: ["a.java","b.java","c.java"],
1381 javac_shard_size: 1
1382 }
1383 `)
1384
1385 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
1386 for i := 0; i < 3; i++ {
1387 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
1388 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1389 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1390 }
1391 }
1392}
1393
Nan Zhang581fd212018-01-10 16:06:12 -08001394func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001395 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001396 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001397 name: "droiddoc-templates-sdk",
1398 path: ".",
1399 }
Jiyong Park29074592019-07-07 16:27:47 +09001400 filegroup {
1401 name: "bar-doc-aidl-srcs",
1402 srcs: ["bar-doc/IBar.aidl"],
1403 path: "bar-doc",
1404 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001405 droidstubs {
1406 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001407 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001408 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001409 ],
Steve Kim3666c702020-09-01 17:58:01 +00001410 exclude_srcs: [
1411 "bar-doc/b.java"
1412 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001413 api_levels_annotations_dirs: [
1414 "droiddoc-templates-sdk",
1415 ],
1416 api_levels_annotations_enabled: true,
1417 }
1418 droiddoc {
1419 name: "bar-doc",
1420 srcs: [
1421 ":bar-stubs",
1422 "bar-doc/IFoo.aidl",
1423 ":bar-doc-aidl-srcs",
1424 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001425 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001426 hdf: [
1427 "android.whichdoc offline",
1428 ],
1429 knowntags: [
1430 "bar-doc/known_oj_tags.txt",
1431 ],
1432 proofread_file: "libcore-proofread.txt",
1433 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001434 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001435 }
Colin Cross238c1f32020-06-07 16:58:18 -07001436 `,
1437 map[string][]byte{
1438 "bar-doc/a.java": nil,
1439 "bar-doc/b.java": nil,
1440 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001441 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1442 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1443 if err != nil {
1444 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1445 }
1446 if len(barStubsOutputs) != 1 {
1447 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001448 }
Nan Zhang581fd212018-01-10 16:06:12 -08001449
Liz Kammere1ab2502020-09-10 15:29:25 +00001450 barStubsOutput := barStubsOutputs[0]
1451 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
1452 javaDoc := barDoc.Rule("javadoc")
1453 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1454 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001455 }
1456
Liz Kammere1ab2502020-09-10 15:29:25 +00001457 expected := "-sourcepath " + buildDir + "/.intermediates/bar-doc/android_common/srcjars "
1458 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1459 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1460 }
1461
1462 aidl := barDoc.Rule("aidl")
1463 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001464 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1465 }
1466
1467 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1468 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001469 }
Nan Zhang581fd212018-01-10 16:06:12 -08001470}
1471
Liz Kammer585cac22020-07-06 09:12:57 -07001472func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1473 testJavaError(t, "flags is set. Cannot set args", `
1474 droiddoc_exported_dir {
1475 name: "droiddoc-templates-sdk",
1476 path: ".",
1477 }
1478 filegroup {
1479 name: "bar-doc-aidl-srcs",
1480 srcs: ["bar-doc/IBar.aidl"],
1481 path: "bar-doc",
1482 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001483 droidstubs {
1484 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001485 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001486 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001487 ],
Steve Kim3666c702020-09-01 17:58:01 +00001488 exclude_srcs: [
1489 "bar-doc/b.java"
1490 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001491 api_levels_annotations_dirs: [
1492 "droiddoc-templates-sdk",
1493 ],
1494 api_levels_annotations_enabled: true,
1495 }
1496 droiddoc {
1497 name: "bar-doc",
1498 srcs: [
1499 ":bar-stubs",
1500 "bar-doc/IFoo.aidl",
1501 ":bar-doc-aidl-srcs",
1502 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001503 custom_template: "droiddoc-templates-sdk",
1504 hdf: [
1505 "android.whichdoc offline",
1506 ],
1507 knowntags: [
1508 "bar-doc/known_oj_tags.txt",
1509 ],
1510 proofread_file: "libcore-proofread.txt",
1511 todo_file: "libcore-docs-todo.html",
1512 flags: ["-offlinemode -title \"libcore\""],
1513 args: "-offlinemode -title \"libcore\"",
1514 }
1515 `)
1516}
1517
Liz Kammer3d894b72020-08-04 09:55:13 -07001518func TestDroidstubs(t *testing.T) {
1519 ctx, _ := testJavaWithFS(t, `
1520 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001521 name: "droiddoc-templates-sdk",
1522 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001523 }
1524
1525 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001526 name: "bar-stubs",
1527 srcs: ["bar-doc/a.java"],
1528 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1529 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001530 }
1531
1532 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001533 name: "bar-stubs-other",
1534 srcs: ["bar-doc/a.java"],
1535 high_mem: true,
1536 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1537 api_levels_annotations_enabled: true,
1538 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001539 }
1540 `,
1541 map[string][]byte{
1542 "bar-doc/a.java": nil,
1543 })
1544 testcases := []struct {
1545 moduleName string
1546 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001547 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001548 }{
1549 {
1550 moduleName: "bar-stubs",
1551 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001552 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001553 },
1554 {
1555 moduleName: "bar-stubs-other",
1556 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001557 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001558 },
1559 }
1560 for _, c := range testcases {
1561 m := ctx.ModuleForTests(c.moduleName, "android_common")
1562 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001563 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001564 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001565 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001566 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1567 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001568
1569 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1570 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1571 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001572 }
1573}
1574
Paul Duffin83a2d962019-11-19 19:44:10 +00001575func TestDroidstubsWithSystemModules(t *testing.T) {
1576 ctx, _ := testJava(t, `
1577 droidstubs {
1578 name: "stubs-source-system-modules",
1579 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001580 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001581 ],
1582 sdk_version: "none",
1583 system_modules: "source-system-modules",
1584 }
1585
1586 java_library {
1587 name: "source-jar",
1588 srcs: [
1589 "a.java",
1590 ],
1591 }
1592
1593 java_system_modules {
1594 name: "source-system-modules",
1595 libs: ["source-jar"],
1596 }
1597
1598 droidstubs {
1599 name: "stubs-prebuilt-system-modules",
1600 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001601 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001602 ],
1603 sdk_version: "none",
1604 system_modules: "prebuilt-system-modules",
1605 }
1606
1607 java_import {
1608 name: "prebuilt-jar",
1609 jars: ["a.jar"],
1610 }
1611
1612 java_system_modules_import {
1613 name: "prebuilt-system-modules",
1614 libs: ["prebuilt-jar"],
1615 }
1616 `)
1617
1618 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1619
1620 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1621}
1622
1623func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1624 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1625 var systemJars []string
1626 for _, i := range metalavaRule.Implicits {
1627 systemJars = append(systemJars, i.Base())
1628 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001629 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001630 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1631 }
1632}
1633
Colin Cross54250902017-12-05 09:28:08 -08001634func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001635 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001636 java_library {
1637 name: "foo",
1638 srcs: ["a.java"],
1639 }
1640
1641 java_genrule {
1642 name: "jargen",
1643 tool_files: ["b.java"],
1644 cmd: "$(location b.java) $(in) $(out)",
1645 out: ["jargen.jar"],
1646 srcs: [":foo"],
1647 }
1648
1649 java_library {
1650 name: "bar",
1651 static_libs: ["jargen"],
1652 srcs: ["c.java"],
1653 }
1654
1655 java_library {
1656 name: "baz",
1657 libs: ["jargen"],
1658 srcs: ["c.java"],
1659 }
1660 `)
1661
1662 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1663 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1664 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1665 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1666 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1667
Colin Cross3d680512020-11-13 16:23:53 -08001668 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1669 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001670 }
1671
1672 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1673 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1674 }
1675
1676 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1677 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1678 }
1679
1680 if len(barCombined.Inputs) != 2 ||
1681 barCombined.Inputs[0].String() != bar.Output.String() ||
1682 barCombined.Inputs[1].String() != jargen.Output.String() {
1683 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1684 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1685 }
1686}
1687
Nan Zhang27e284d2018-02-09 21:03:53 +00001688func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001689 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001690 java_library {
1691 name: "foo",
1692 srcs: ["a.java", ":foo-srcs"],
1693 exclude_srcs: ["a.java", ":foo-excludes"],
1694 }
1695
1696 filegroup {
1697 name: "foo-srcs",
1698 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1699 }
1700
1701 filegroup {
1702 name: "foo-excludes",
1703 srcs: ["java-fg/a.java", "java-fg/b.java"],
1704 }
1705 `)
1706
1707 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1708
1709 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1710 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1711 }
1712}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001713
Paul Duffin52d398a2019-06-11 12:31:14 +01001714func TestJavaLibrary(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001715 config := testConfig(nil, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001716 "libcore/Android.bp": []byte(`
1717 java_library {
1718 name: "core",
1719 sdk_version: "none",
1720 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001721 }
1722
1723 filegroup {
1724 name: "core-jar",
1725 srcs: [":core{.jar}"],
1726 }
1727`),
1728 })
Colin Crossae8600b2020-10-29 17:09:13 -07001729 ctx := testContext(config)
Paul Duffinaa55f742020-10-06 17:20:13 +01001730 run(t, ctx, config)
1731}
1732
1733func TestJavaImport(t *testing.T) {
1734 config := testConfig(nil, "", map[string][]byte{
1735 "libcore/Android.bp": []byte(`
1736 java_import {
1737 name: "core",
1738 sdk_version: "none",
1739 }
1740
1741 filegroup {
1742 name: "core-jar",
1743 srcs: [":core{.jar}"],
1744 }
1745`),
Paul Duffin52d398a2019-06-11 12:31:14 +01001746 })
Colin Crossae8600b2020-10-29 17:09:13 -07001747 ctx := testContext(config)
Paul Duffin52d398a2019-06-11 12:31:14 +01001748 run(t, ctx, config)
1749}
1750
Jiyong Parkc678ad32018-04-10 13:07:10 +09001751func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001752 ctx, _ := testJava(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001753 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001754 name: "droiddoc-templates-sdk",
1755 path: ".",
1756 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001757 java_sdk_library {
1758 name: "foo",
1759 srcs: ["a.java", "b.java"],
1760 api_packages: ["foo"],
1761 }
1762 java_sdk_library {
1763 name: "bar",
1764 srcs: ["a.java", "b.java"],
1765 api_packages: ["bar"],
1766 }
1767 java_library {
1768 name: "baz",
1769 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001770 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001771 sdk_version: "system_current",
1772 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001773 java_sdk_library {
1774 name: "barney",
1775 srcs: ["c.java"],
1776 api_only: true,
1777 }
1778 java_sdk_library {
1779 name: "betty",
1780 srcs: ["c.java"],
1781 shared_library: false,
1782 }
Paul Duffin859fe962020-05-15 10:20:31 +01001783 java_sdk_library_import {
1784 name: "quuz",
1785 public: {
1786 jars: ["c.jar"],
1787 },
1788 }
1789 java_sdk_library_import {
1790 name: "fred",
1791 public: {
1792 jars: ["b.jar"],
1793 },
1794 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001795 java_sdk_library_import {
1796 name: "wilma",
1797 public: {
1798 jars: ["b.jar"],
1799 },
1800 shared_library: false,
1801 }
Jiyong Park1be96912018-05-28 18:02:19 +09001802 java_library {
1803 name: "qux",
1804 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001805 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001806 sdk_version: "system_current",
1807 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001808 java_library {
1809 name: "baz-test",
1810 srcs: ["c.java"],
1811 libs: ["foo"],
1812 sdk_version: "test_current",
1813 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001814 java_library {
1815 name: "baz-29",
1816 srcs: ["c.java"],
1817 libs: ["foo"],
1818 sdk_version: "system_29",
1819 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001820 java_library {
1821 name: "baz-module-30",
1822 srcs: ["c.java"],
1823 libs: ["foo"],
1824 sdk_version: "module_30",
1825 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001826 `)
1827
1828 // check the existence of the internal modules
1829 ctx.ModuleForTests("foo", "android_common")
Paul Duffindd9d0742020-05-08 15:52:37 +01001830 ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1831 ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1832 ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1833 ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1834 ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1835 ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
Jiyong Parke3833882020-02-17 17:28:10 +09001836 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +09001837 ctx.ModuleForTests("foo.api.public.28", "")
1838 ctx.ModuleForTests("foo.api.system.28", "")
1839 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001840
1841 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1842 // tests if baz is actually linked to the stubs lib
1843 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
1844 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
1845 "foo.stubs.system.jar")
1846 }
1847 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +09001848 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001849 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +09001850 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001851 }
1852 // test if baz is not linked to the system variant of foo
1853 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
1854 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
1855 "foo.stubs.jar")
1856 }
Jiyong Park1be96912018-05-28 18:02:19 +09001857
Paul Duffin726d23c2020-01-22 16:30:37 +00001858 bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac")
1859 // tests if baz-test is actually linked to the test stubs lib
1860 if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") {
1861 t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"],
1862 "foo.stubs.test.jar")
1863 }
1864
Paul Duffina2db18f2020-01-22 17:11:15 +00001865 baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac")
1866 // tests if baz-29 is actually linked to the system 29 stubs lib
1867 if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") {
1868 t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"],
1869 "prebuilts/sdk/29/system/foo.jar")
1870 }
1871
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001872 bazModule30Javac := ctx.ModuleForTests("baz-module-30", "android_common").Rule("javac")
1873 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
1874 if !strings.Contains(bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar") {
1875 t.Errorf("baz-module-30 javac classpath %v does not contain %q", bazModule30Javac.Args["classpath"],
1876 "prebuilts/sdk/30/module-lib/foo.jar")
1877 }
1878
Jiyong Park1be96912018-05-28 18:02:19 +09001879 // test if baz has exported SDK lib names foo and bar to qux
1880 qux := ctx.ModuleForTests("qux", "android_common")
1881 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001882 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +00001883 if w := []string{"foo", "bar", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) {
Paul Duffin859fe962020-05-15 10:20:31 +01001884 t.Errorf("qux should export %q but exports %q", w, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001885 }
1886 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001887}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001888
Anton Hansson7f66efa2020-10-08 14:47:23 +01001889func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
1890 ctx, _ := testJava(t, `
1891 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001892 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001893 srcs: ["a.java"],
1894 impl_only_libs: ["foo"],
1895 stub_only_libs: ["bar"],
1896 }
1897 java_library {
1898 name: "foo",
1899 srcs: ["a.java"],
1900 sdk_version: "current",
1901 }
1902 java_library {
1903 name: "bar",
1904 srcs: ["a.java"],
1905 sdk_version: "current",
1906 }
1907 `)
1908
Anton Hanssondff2c782020-12-21 17:10:01 +00001909 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Anton Hansson7f66efa2020-10-08 14:47:23 +01001910 implJavacCp := ctx.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
1911 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1912 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1913 }
1914 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001915 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Anton Hansson7f66efa2020-10-08 14:47:23 +01001916 stubsJavacCp := ctx.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
1917 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1918 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1919 }
1920}
1921
Paul Duffindaaa3322020-05-26 18:13:57 +01001922func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
1923 ctx, _ := testJava(t, `
1924 java_sdk_library {
1925 name: "foo",
1926 srcs: ["a.java"],
1927 api_only: true,
1928 public: {
1929 enabled: true,
1930 },
1931 }
1932
1933 java_library {
1934 name: "bar",
1935 srcs: ["b.java"],
1936 libs: ["foo"],
1937 }
1938 `)
1939
1940 // The bar library should depend on the stubs jar.
1941 barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac")
1942 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1943 t.Errorf("expected %q, found %#q", expected, actual)
1944 }
1945}
1946
Paul Duffin46dc45a2020-05-14 15:39:10 +01001947func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
1948 testJava(t, `
1949 java_sdk_library {
1950 name: "foo",
1951 srcs: ["a.java"],
1952 api_packages: ["foo"],
1953 public: {
1954 enabled: true,
1955 },
1956 }
1957
1958 java_library {
1959 name: "bar",
1960 srcs: ["b.java", ":foo{.public.stubs.source}"],
1961 }
1962 `)
1963}
1964
1965func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
1966 testJavaError(t, `"foo" does not provide api scope system`, `
1967 java_sdk_library {
1968 name: "foo",
1969 srcs: ["a.java"],
1970 api_packages: ["foo"],
1971 public: {
1972 enabled: true,
1973 },
1974 }
1975
1976 java_library {
1977 name: "bar",
1978 srcs: ["b.java", ":foo{.system.stubs.source}"],
1979 }
1980 `)
1981}
1982
Paul Duffinca8d9a52020-06-26 22:20:25 +01001983func TestJavaSdkLibrary_Deps(t *testing.T) {
1984 ctx, _ := testJava(t, `
1985 java_sdk_library {
1986 name: "sdklib",
1987 srcs: ["a.java"],
1988 sdk_version: "none",
1989 system_modules: "none",
1990 public: {
1991 enabled: true,
1992 },
1993 }
1994 `)
1995
Paul Duffincee7e662020-07-09 17:32:57 +01001996 CheckModuleDependencies(t, ctx, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001997 `dex2oatd`,
1998 `sdklib.impl`,
1999 `sdklib.stubs`,
2000 `sdklib.stubs.source`,
2001 `sdklib.xml`,
2002 })
2003}
2004
Paul Duffin46dc45a2020-05-14 15:39:10 +01002005func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
2006 testJava(t, `
2007 java_sdk_library_import {
2008 name: "foo",
2009 public: {
2010 jars: ["a.jar"],
2011 stub_srcs: ["a.java"],
2012 current_api: "api/current.txt",
2013 removed_api: "api/removed.txt",
2014 },
2015 }
2016
2017 java_library {
2018 name: "bar",
2019 srcs: [":foo{.public.stubs.source}"],
2020 java_resources: [
2021 ":foo{.public.api.txt}",
2022 ":foo{.public.removed-api.txt}",
2023 ],
2024 }
2025 `)
2026}
2027
2028func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2029 bp := `
2030 java_sdk_library_import {
2031 name: "foo",
2032 public: {
2033 jars: ["a.jar"],
2034 },
2035 }
2036 `
2037
2038 t.Run("stubs.source", func(t *testing.T) {
2039 testJavaError(t, `stubs.source not available for api scope public`, bp+`
2040 java_library {
2041 name: "bar",
2042 srcs: [":foo{.public.stubs.source}"],
2043 java_resources: [
2044 ":foo{.public.api.txt}",
2045 ":foo{.public.removed-api.txt}",
2046 ],
2047 }
2048 `)
2049 })
2050
2051 t.Run("api.txt", func(t *testing.T) {
2052 testJavaError(t, `api.txt not available for api scope public`, bp+`
2053 java_library {
2054 name: "bar",
2055 srcs: ["a.java"],
2056 java_resources: [
2057 ":foo{.public.api.txt}",
2058 ],
2059 }
2060 `)
2061 })
2062
2063 t.Run("removed-api.txt", func(t *testing.T) {
2064 testJavaError(t, `removed-api.txt not available for api scope public`, bp+`
2065 java_library {
2066 name: "bar",
2067 srcs: ["a.java"],
2068 java_resources: [
2069 ":foo{.public.removed-api.txt}",
2070 ],
2071 }
2072 `)
2073 })
2074}
2075
Paul Duffin3375e352020-04-28 10:44:03 +01002076func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
2077 testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, `
2078 java_sdk_library {
2079 name: "foo",
2080 srcs: ["a.java", "b.java"],
2081 api_packages: ["foo"],
2082 // Explicitly disable public to test the check that ensures the set of enabled
2083 // scopes is consistent.
2084 public: {
2085 enabled: false,
2086 },
2087 system: {
2088 enabled: true,
2089 },
2090 }
2091 `)
2092}
2093
Paul Duffin87a05a32020-05-12 11:50:28 +01002094func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
2095 testJava(t, `
2096 java_sdk_library {
2097 name: "foo",
2098 srcs: ["a.java", "b.java"],
2099 api_packages: ["foo"],
2100 system: {
2101 enabled: true,
2102 sdk_version: "module_current",
2103 },
2104 }
2105 `)
2106}
2107
Paul Duffin0c5bae52020-06-02 13:00:08 +01002108func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
2109 testJava(t, `
2110 java_sdk_library {
2111 name: "foo",
2112 srcs: ["a.java", "b.java"],
2113 api_packages: ["foo"],
2114 system: {
2115 enabled: true,
2116 },
2117 module_lib: {
2118 enabled: true,
2119 },
2120 }
2121 `)
2122}
2123
2124func TestJavaSdkLibrary_SystemServer(t *testing.T) {
2125 testJava(t, `
2126 java_sdk_library {
2127 name: "foo",
2128 srcs: ["a.java", "b.java"],
2129 api_packages: ["foo"],
2130 system: {
2131 enabled: true,
2132 },
2133 system_server: {
2134 enabled: true,
2135 },
2136 }
2137 `)
2138}
2139
Paul Duffin803a9562020-05-20 11:52:25 +01002140func TestJavaSdkLibrary_MissingScope(t *testing.T) {
2141 testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, `
2142 java_sdk_library {
2143 name: "foo",
2144 srcs: ["a.java"],
2145 public: {
2146 enabled: false,
2147 },
2148 }
2149
2150 java_library {
2151 name: "baz",
2152 srcs: ["a.java"],
2153 libs: ["foo"],
2154 sdk_version: "module_current",
2155 }
2156 `)
2157}
2158
2159func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
2160 testJava(t, `
2161 java_sdk_library {
2162 name: "foo",
2163 srcs: ["a.java"],
2164 system: {
2165 enabled: true,
2166 },
2167 }
2168
2169 java_library {
2170 name: "baz",
2171 srcs: ["a.java"],
2172 libs: ["foo"],
2173 // foo does not have module-lib scope so it should fallback to system
2174 sdk_version: "module_current",
2175 }
2176 `)
2177}
2178
Jiyong Park932cdfe2020-05-28 00:19:53 +09002179func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
2180 ctx, _ := testJava(t, `
2181 java_sdk_library {
2182 name: "foo",
2183 srcs: ["a.java"],
2184 system: {
2185 enabled: true,
2186 },
2187 default_to_stubs: true,
2188 }
2189
2190 java_library {
2191 name: "baz",
2192 srcs: ["a.java"],
2193 libs: ["foo"],
2194 // does not have sdk_version set, should fallback to module,
2195 // which will then fallback to system because the module scope
2196 // is not enabled.
2197 }
2198 `)
2199 // The baz library should depend on the system stubs jar.
2200 bazLibrary := ctx.ModuleForTests("baz", "android_common").Rule("javac")
2201 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2202 t.Errorf("expected %q, found %#q", expected, actual)
2203 }
2204}
2205
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002206var compilerFlagsTestCases = []struct {
2207 in string
2208 out bool
2209}{
2210 {
2211 in: "a",
2212 out: false,
2213 },
2214 {
2215 in: "-a",
2216 out: true,
2217 },
2218 {
2219 in: "-no-jdk",
2220 out: false,
2221 },
2222 {
2223 in: "-no-stdlib",
2224 out: false,
2225 },
2226 {
2227 in: "-kotlin-home",
2228 out: false,
2229 },
2230 {
2231 in: "-kotlin-home /some/path",
2232 out: false,
2233 },
2234 {
2235 in: "-include-runtime",
2236 out: false,
2237 },
2238 {
2239 in: "-Xintellij-plugin-root",
2240 out: false,
2241 },
2242}
2243
2244type mockContext struct {
2245 android.ModuleContext
2246 result bool
2247}
2248
2249func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2250 // CheckBadCompilerFlags calls this function when the flag should be rejected
2251 ctx.result = false
2252}
2253
2254func TestCompilerFlags(t *testing.T) {
2255 for _, testCase := range compilerFlagsTestCases {
2256 ctx := &mockContext{result: true}
2257 CheckKotlincFlags(ctx, []string{testCase.in})
2258 if ctx.result != testCase.out {
2259 t.Errorf("incorrect output:")
2260 t.Errorf(" input: %#v", testCase.in)
2261 t.Errorf(" expected: %#v", testCase.out)
2262 t.Errorf(" got: %#v", ctx.result)
2263 }
2264 }
2265}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002266
2267// TODO(jungjw): Consider making this more robust by ignoring path order.
2268func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
2269 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
2270 flags := strings.Split(variables["javacFlags"], " ")
2271 got := ""
2272 for _, flag := range flags {
2273 keyEnd := strings.Index(flag, "=")
2274 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2275 got = flag[keyEnd+1:]
2276 break
2277 }
2278 }
2279 if expected != got {
2280 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2281 }
2282}
2283
2284func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002285 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002286 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002287 bp := `
2288 java_library {
2289 name: "foo",
2290 srcs: ["a.java"],
2291 java_version: "1.8",
2292 }
2293
2294 java_library {
2295 name: "bar",
2296 srcs: ["b.java"],
2297 sdk_version: "none",
2298 system_modules: "none",
2299 patch_module: "java.base",
2300 java_version: "1.8",
2301 }
2302
2303 java_library {
2304 name: "baz",
2305 srcs: ["c.java"],
2306 patch_module: "java.base",
2307 java_version: "1.8",
2308 }
2309 `
2310 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002311
2312 checkPatchModuleFlag(t, ctx, "foo", "")
2313 checkPatchModuleFlag(t, ctx, "bar", "")
2314 checkPatchModuleFlag(t, ctx, "baz", "")
2315 })
2316
Pete Gillin0c2143e2019-05-02 15:32:11 +01002317 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002318 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002319 bp := `
2320 java_library {
2321 name: "foo",
2322 srcs: ["a.java"],
2323 }
2324
2325 java_library {
2326 name: "bar",
2327 srcs: ["b.java"],
2328 sdk_version: "none",
2329 system_modules: "none",
2330 patch_module: "java.base",
2331 }
2332
2333 java_library {
2334 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002335 srcs: [
2336 "c.java",
2337 // Tests for b/150878007
2338 "dir/d.java",
2339 "dir2/e.java",
2340 "dir2/f.java",
2341 "nested/dir/g.java"
2342 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002343 patch_module: "java.base",
2344 }
2345 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002346 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002347
2348 checkPatchModuleFlag(t, ctx, "foo", "")
2349 expected := "java.base=.:" + buildDir
2350 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002351 expected = "java.base=" + strings.Join([]string{
2352 ".", buildDir, "dir", "dir2", "nested", moduleToPath("ext"), moduleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002353 checkPatchModuleFlag(t, ctx, "baz", expected)
2354 })
2355}
Paul Duffina7b9f422020-01-10 17:12:18 +00002356
2357func TestJavaSystemModules(t *testing.T) {
2358 ctx, _ := testJava(t, `
2359 java_system_modules {
2360 name: "system-modules",
2361 libs: ["system-module1", "system-module2"],
2362 }
2363 java_library {
2364 name: "system-module1",
2365 srcs: ["a.java"],
2366 sdk_version: "none",
2367 system_modules: "none",
2368 }
2369 java_library {
2370 name: "system-module2",
2371 srcs: ["b.java"],
2372 sdk_version: "none",
2373 system_modules: "none",
2374 }
2375 `)
2376
2377 // check the existence of the module
2378 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2379
2380 cmd := systemModules.Rule("jarsTosystemModules")
2381
2382 // make sure the command compiles against the supplied modules.
2383 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2384 if !strings.Contains(cmd.Args["classpath"], module) {
2385 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2386 module)
2387 }
2388 }
2389}
Paul Duffin90169ba2020-01-10 17:16:44 +00002390
2391func TestJavaSystemModulesImport(t *testing.T) {
2392 ctx, _ := testJava(t, `
2393 java_system_modules_import {
2394 name: "system-modules",
2395 libs: ["system-module1", "system-module2"],
2396 }
2397 java_import {
2398 name: "system-module1",
2399 jars: ["a.jar"],
2400 }
2401 java_import {
2402 name: "system-module2",
2403 jars: ["b.jar"],
2404 }
2405 `)
2406
2407 // check the existence of the module
2408 systemModules := ctx.ModuleForTests("system-modules", "android_common")
2409
2410 cmd := systemModules.Rule("jarsTosystemModules")
2411
2412 // make sure the command compiles against the supplied modules.
2413 for _, module := range []string{"system-module1.jar", "system-module2.jar"} {
2414 if !strings.Contains(cmd.Args["classpath"], module) {
2415 t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"],
2416 module)
2417 }
2418 }
2419}
Paul Duffin83a2d962019-11-19 19:44:10 +00002420
2421func TestJavaLibraryWithSystemModules(t *testing.T) {
2422 ctx, _ := testJava(t, `
2423 java_library {
2424 name: "lib-with-source-system-modules",
2425 srcs: [
2426 "a.java",
2427 ],
2428 sdk_version: "none",
2429 system_modules: "source-system-modules",
2430 }
2431
2432 java_library {
2433 name: "source-jar",
2434 srcs: [
2435 "a.java",
2436 ],
2437 }
2438
2439 java_system_modules {
2440 name: "source-system-modules",
2441 libs: ["source-jar"],
2442 }
2443
2444 java_library {
2445 name: "lib-with-prebuilt-system-modules",
2446 srcs: [
2447 "a.java",
2448 ],
2449 sdk_version: "none",
2450 system_modules: "prebuilt-system-modules",
2451 }
2452
2453 java_import {
2454 name: "prebuilt-jar",
2455 jars: ["a.jar"],
2456 }
2457
2458 java_system_modules_import {
2459 name: "prebuilt-system-modules",
2460 libs: ["prebuilt-jar"],
2461 }
2462 `)
2463
2464 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2465
2466 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2467}
2468
2469func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2470 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2471 bootClasspath := javacRule.Args["bootClasspath"]
2472 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2473 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2474 }
2475}
Jiyong Park19604de2020-03-24 16:44:11 +09002476
2477func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2478 ctx, _ := testJava(t, `
2479 java_library {
2480 name: "foo",
2481 srcs: ["aidl/foo/IFoo.aidl"],
2482 libs: ["bar"],
2483 }
2484
2485 java_import {
2486 name: "bar",
2487 jars: ["a.jar"],
2488 aidl: {
2489 export_include_dirs: ["aidl/bar"],
2490 },
2491 }
2492 `)
2493
2494 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2495 expectedAidlFlag := "-Iaidl/bar"
2496 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2497 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2498 }
2499}
Liz Kammerdd849a82020-06-12 16:38:45 -07002500
Jooyung Hane197d8b2021-01-05 10:33:16 +09002501func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2502 ctx, _ := testJava(t, `
2503 java_library {
2504 name: "foo",
2505 srcs: ["aidl/foo/IFoo.aidl"],
2506 aidl: { flags: ["-Werror"], },
2507 }
2508 `)
2509
2510 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2511 expectedAidlFlag := "-Werror"
2512 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2513 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2514 }
2515}
2516
Liz Kammerdd849a82020-06-12 16:38:45 -07002517func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002518 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002519 java_test_host {
2520 name: "foo",
2521 srcs: ["a.java"],
2522 data_native_bins: ["bin"]
2523 }
2524
2525 python_binary_host {
2526 name: "bin",
2527 srcs: ["bin.py"],
2528 }
2529 `)
2530
2531 buildOS := android.BuildOs.String()
2532
2533 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002534 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Liz Kammerdd849a82020-06-12 16:38:45 -07002535 expected := []string{buildDir + "/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
2536 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
2537 if !reflect.DeepEqual(expected, actual) {
2538 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
2539 }
2540}