blob: d1d26ba00d1877840e72e753379d688fd9791d2d [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 "os"
20 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070021 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010022 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070023 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070024 "strings"
25 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070026
Jeongik Cha28df2572019-11-11 10:46:36 +090027 "github.com/google/blueprint/proptools"
28
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/android"
30 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080031 "android/soong/dexpreopt"
Colin Cross1661aff2021-03-12 17:56:51 -080032 "android/soong/genrule"
Liz Kammerdd849a82020-06-12 16:38:45 -070033 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
Paul Duffin70d3bee2021-03-21 11:26:05 +000036// Legacy preparer used for running tests within the java package.
37//
38// This includes everything that was needed to run any test in the java package prior to the
39// introduction of the test fixtures. Tests that are being converted to use fixtures directly
40// rather than through the testJava...() methods should avoid using this and instead use the
41// various preparers directly, using android.GroupFixturePreparers(...) to group them when
42// necessary.
43//
44// deprecated
45var prepareForJavaTest = android.GroupFixturePreparers(
Paul Duffin95bdab42021-03-08 21:48:46 +000046 genrule.PrepareForTestWithGenRuleBuildComponents,
47 // Get the CC build components but not default modules.
48 cc.PrepareForTestWithCcBuildComponents,
49 // Include all the default java modules.
50 PrepareForTestWithJavaDefaultModules,
Paul Duffin42da69d2021-03-22 13:41:36 +000051 PrepareForTestWithOverlayBuildComponents,
Paul Duffin220ddd72021-03-17 23:54:30 +000052 python.PrepareForTestWithPythonBuildComponents,
Paul Duffin95bdab42021-03-08 21:48:46 +000053 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
Paul Duffin95bdab42021-03-08 21:48:46 +000054 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
55 }),
Paul Duffin9fc9f532021-03-23 15:41:11 +000056 PrepareForTestWithDexpreopt,
Paul Duffin95bdab42021-03-08 21:48:46 +000057)
58
Colin Cross72bb3632017-07-13 16:23:21 -070059func TestMain(m *testing.M) {
Paul Duffind6339af2021-03-22 18:05:04 +000060 os.Exit(m.Run())
Colin Cross527012a2017-11-30 22:56:16 -080061}
62
Paul Duffin95bdab42021-03-08 21:48:46 +000063// testJavaError is a legacy way of running tests of java modules that expect errors.
64//
65// See testJava for an explanation as to how to stop using this deprecated method.
66//
67// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +090068func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +090069 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +000070 result := android.GroupFixturePreparers(
Paul Duffin9fc9f532021-03-23 15:41:11 +000071 prepareForJavaTest, dexpreopt.PrepareForTestByEnablingDexpreopt).
Paul Duffin6bac49c2021-03-12 21:28:15 +000072 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
73 RunTestWithBp(t, bp)
74 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +090075}
76
Paul Duffin95bdab42021-03-08 21:48:46 +000077// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
78//
79// See testJava for an explanation as to how to stop using this deprecated method.
80//
81// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080082func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090083 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +000084 // This must be done on the supplied config and not as part of the fixture because any changes to
85 // the fixture's config will be ignored when RunTestWithConfig replaces it.
Colin Cross98be1bb2019-12-13 20:41:13 -080086 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000087 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Paul Duffin71ae5942021-03-22 15:36:52 +000088 result := prepareForJavaTest.
Paul Duffin95bdab42021-03-08 21:48:46 +000089 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
90 RunTestWithConfig(t, config)
91 return result.TestContext, result.Config
Paul Duffinec0fe172021-02-25 15:34:13 +000092}
93
Paul Duffin71ae5942021-03-22 15:36:52 +000094// testJavaWithFS runs tests using the prepareForJavaTest
Paul Duffin95bdab42021-03-08 21:48:46 +000095//
96// See testJava for an explanation as to how to stop using this deprecated method.
97//
98// deprecated
99func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -0700100 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +0000101 result := android.GroupFixturePreparers(
102 prepareForJavaTest, fs.AddToFixture()).RunTestWithBp(t, bp)
Paul Duffin95bdab42021-03-08 21:48:46 +0000103 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -0700104}
105
Paul Duffin71ae5942021-03-22 15:36:52 +0000106// testJava runs tests using the prepareForJavaTest
Paul Duffin95bdab42021-03-08 21:48:46 +0000107//
Paul Duffin71ae5942021-03-22 15:36:52 +0000108// Do not add any new usages of this, instead use the prepareForJavaTest directly as it makes it
Paul Duffin95bdab42021-03-08 21:48:46 +0000109// much easier to customize the test behavior.
110//
111// If it is necessary to customize the behavior of an existing test that uses this then please first
Paul Duffin71ae5942021-03-22 15:36:52 +0000112// convert the test to using prepareForJavaTest first and then in a following change add the
Paul Duffin95bdab42021-03-08 21:48:46 +0000113// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
114// that it did not change the test behavior unexpectedly.
115//
116// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700117func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800118 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +0000119 result := prepareForJavaTest.RunTestWithBp(t, bp)
Paul Duffin95bdab42021-03-08 21:48:46 +0000120 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900121}
122
Paul Duffin95bdab42021-03-08 21:48:46 +0000123// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
124// is defined in PrepareForIntegrationTestWithJava
125func defaultModuleToPath(name string) string {
Paul Duffin76101fa2021-03-13 09:55:25 +0000126 switch {
127 case name == `""`:
128 return name
129 case strings.HasSuffix(name, ".jar"):
130 return name
131 default:
Paul Duffin414ea5e2021-03-22 17:31:52 +0000132 return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
Paul Duffin76101fa2021-03-13 09:55:25 +0000133 }
Paul Duffin95bdab42021-03-08 21:48:46 +0000134}
135
Paul Duffin76e5c8a2021-03-20 14:19:46 +0000136// Test that the PrepareForTestWithJavaDefaultModules provides all the files that it uses by
137// running it in a fixture that requires all source files to exist.
138func TestPrepareForTestWithJavaDefaultModules(t *testing.T) {
139 android.GroupFixturePreparers(
140 PrepareForTestWithJavaDefaultModules,
141 android.PrepareForTestDisallowNonExistentPaths,
142 ).RunTest(t)
143}
144
Jeongik Chae403e9e2019-12-07 00:16:24 +0900145func TestJavaLinkType(t *testing.T) {
146 testJava(t, `
147 java_library {
148 name: "foo",
149 srcs: ["a.java"],
150 libs: ["bar"],
151 static_libs: ["baz"],
152 }
153
154 java_library {
155 name: "bar",
156 sdk_version: "current",
157 srcs: ["b.java"],
158 }
159
160 java_library {
161 name: "baz",
162 sdk_version: "system_current",
163 srcs: ["c.java"],
164 }
165 `)
166
Steven Moreland00298982020-11-17 21:44:36 +0000167 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900168 java_library {
169 name: "foo",
170 srcs: ["a.java"],
171 libs: ["bar"],
172 sdk_version: "current",
173 static_libs: ["baz"],
174 }
175
176 java_library {
177 name: "bar",
178 sdk_version: "current",
179 srcs: ["b.java"],
180 }
181
182 java_library {
183 name: "baz",
184 sdk_version: "system_current",
185 srcs: ["c.java"],
186 }
187 `)
188
189 testJava(t, `
190 java_library {
191 name: "foo",
192 srcs: ["a.java"],
193 libs: ["bar"],
194 sdk_version: "system_current",
195 static_libs: ["baz"],
196 }
197
198 java_library {
199 name: "bar",
200 sdk_version: "current",
201 srcs: ["b.java"],
202 }
203
204 java_library {
205 name: "baz",
206 sdk_version: "system_current",
207 srcs: ["c.java"],
208 }
209 `)
210
Steven Moreland00298982020-11-17 21:44:36 +0000211 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900212 java_library {
213 name: "foo",
214 srcs: ["a.java"],
215 libs: ["bar"],
216 sdk_version: "system_current",
217 static_libs: ["baz"],
218 }
219
220 java_library {
221 name: "bar",
222 sdk_version: "current",
223 srcs: ["b.java"],
224 }
225
226 java_library {
227 name: "baz",
228 srcs: ["c.java"],
229 }
230 `)
231}
232
Colin Cross72bb3632017-07-13 16:23:21 -0700233func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700234 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700235 java_library {
236 name: "foo",
237 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700238 libs: ["bar"],
239 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700240 }
241
242 java_library {
243 name: "bar",
244 srcs: ["b.java"],
245 }
246
247 java_library {
248 name: "baz",
249 srcs: ["c.java"],
250 }
Colin Crossd5934c82017-10-02 13:55:26 -0700251 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700252
Paul Duffina71a67a2021-03-29 00:42:57 +0100253 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700254 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700255
256 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
257 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
258 }
259
Colin Cross1ee23172017-10-18 14:44:18 -0700260 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000261 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
262 bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700263
Paul Duffin22b77cd2021-03-12 19:15:01 +0000264 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700265
Paul Duffin22b77cd2021-03-12 19:15:01 +0000266 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700267
268 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
269 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700270 }
271}
272
Artur Satayev9cf46692019-11-26 18:08:34 +0000273func TestExportedPlugins(t *testing.T) {
274 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800275 library string
276 processors string
277 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000278 }
279 var tests = []struct {
280 name string
281 extra string
282 results []Result
283 }{
284 {
285 name: "Exported plugin is not a direct plugin",
286 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
287 results: []Result{{library: "exports", processors: "-proc:none"}},
288 },
289 {
290 name: "Exports plugin to dependee",
291 extra: `
292 java_library{name: "exports", exported_plugins: ["plugin"]}
293 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
294 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
295 `,
296 results: []Result{
297 {library: "foo", processors: "-processor com.android.TestPlugin"},
298 {library: "bar", processors: "-processor com.android.TestPlugin"},
299 },
300 },
301 {
302 name: "Exports plugin to android_library",
303 extra: `
304 java_library{name: "exports", exported_plugins: ["plugin"]}
305 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
306 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
307 `,
308 results: []Result{
309 {library: "foo", processors: "-processor com.android.TestPlugin"},
310 {library: "bar", processors: "-processor com.android.TestPlugin"},
311 },
312 },
313 {
314 name: "Exports plugin is not propagated via transitive deps",
315 extra: `
316 java_library{name: "exports", exported_plugins: ["plugin"]}
317 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
318 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
319 `,
320 results: []Result{
321 {library: "foo", processors: "-processor com.android.TestPlugin"},
322 {library: "bar", processors: "-proc:none"},
323 },
324 },
325 {
326 name: "Exports plugin appends to plugins",
327 extra: `
328 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
329 java_library{name: "exports", exported_plugins: ["plugin"]}
330 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
331 `,
332 results: []Result{
333 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
334 },
335 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800336 {
337 name: "Exports plugin to with generates_api to dependee",
338 extra: `
339 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
340 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
341 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
342 `,
343 results: []Result{
344 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
345 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
346 },
347 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000348 }
349
350 for _, test := range tests {
351 t.Run(test.name, func(t *testing.T) {
352 ctx, _ := testJava(t, `
353 java_plugin {
354 name: "plugin",
355 processor_class: "com.android.TestPlugin",
356 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800357 java_plugin {
358 name: "plugin_generates_api",
359 generates_api: true,
360 processor_class: "com.android.TestPlugin",
361 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000362 `+test.extra)
363
364 for _, want := range test.results {
365 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
366 if javac.Args["processor"] != want.processors {
367 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
368 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800369 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
370 disableTurbine := turbine.BuildParams.Rule == nil
371 if disableTurbine != want.disableTurbine {
372 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
373 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000374 }
375 })
376 }
377}
378
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900379func TestSdkVersionByPartition(t *testing.T) {
380 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900381 java_library {
382 name: "foo",
383 srcs: ["a.java"],
384 vendor: true,
385 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900386 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900387
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900388 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900389 java_library {
390 name: "bar",
391 srcs: ["b.java"],
392 }
393 `)
394
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900395 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900396 bp := `
397 java_library {
398 name: "foo",
399 srcs: ["a.java"],
400 product_specific: true,
401 }
402 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800403
Paul Duffin414ea5e2021-03-22 17:31:52 +0000404 errorHandler := android.FixtureExpectsNoErrors
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900405 if enforce {
Paul Duffin414ea5e2021-03-22 17:31:52 +0000406 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern("sdk_version must have a value when the module is located at vendor or product")
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900407 }
Paul Duffin414ea5e2021-03-22 17:31:52 +0000408
409 android.GroupFixturePreparers(
410 PrepareForTestWithJavaDefaultModules,
411 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
412 variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
413 }),
414 ).
415 ExtendWithErrorHandler(errorHandler).
416 RunTestWithBp(t, bp)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900417 }
418}
419
Colin Crossd5934c82017-10-02 13:55:26 -0700420func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700421 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700422 java_library {
423 name: "foo",
424 srcs: ["a.java"],
425 target: {
426 android: {
427 srcs: ["b.java"],
428 },
429 },
430 }
431 `)
432
433 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
434 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
435 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
436 }
437}
438
Colin Cross6b4a32d2017-12-05 13:42:45 -0800439func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700440 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800441 java_library_host {
442 name: "foo",
443 srcs: ["a.java"],
444 }
445
446 java_binary_host {
447 name: "bar",
448 srcs: ["b.java"],
449 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700450 jni_libs: ["libjni"],
451 }
452
453 cc_library_shared {
454 name: "libjni",
455 host_supported: true,
456 device_supported: false,
457 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800458 }
459 `)
460
461 buildOS := android.BuildOs.String()
462
463 bar := ctx.ModuleForTests("bar", buildOS+"_common")
464 barJar := bar.Output("bar.jar").Output.String()
465 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
466 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
467
Colin Cross89226d92020-10-09 19:00:54 -0700468 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
469 libjniSO := libjni.Rule("Cp").Output.String()
470
Colin Cross6b4a32d2017-12-05 13:42:45 -0800471 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700472 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
473 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800474 }
Colin Cross89226d92020-10-09 19:00:54 -0700475
476 // Test that the install binary wrapper depends on the installed JNI libraries
477 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
478 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800479 }
Alex Humesky2070e322020-06-09 20:23:08 -0400480}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800481
Alex Humesky2070e322020-06-09 20:23:08 -0400482func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
483 bp := `
484 java_library {
485 name: "target_library",
486 srcs: ["a.java"],
487 }
488
489 java_binary_host {
490 name: "host_binary",
491 srcs: ["b.java"],
492 }
493 `
Alex Humesky2070e322020-06-09 20:23:08 -0400494
Paul Duffin414ea5e2021-03-22 17:31:52 +0000495 result := android.GroupFixturePreparers(
496 PrepareForTestWithJavaDefaultModules,
497 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
498 variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
499 }),
500 ).RunTestWithBp(t, bp)
Alex Humesky2070e322020-06-09 20:23:08 -0400501
Liz Kammer7941b302020-07-28 13:27:34 -0700502 // first, check that the -g flag is added to target modules
Paul Duffin414ea5e2021-03-22 17:31:52 +0000503 targetLibrary := result.ModuleForTests("target_library", "android_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400504 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
505 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
506 t.Errorf("target library javac flags %v should contain "+
507 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
508 }
509
510 // check that -g is not overridden for host modules
511 buildOS := android.BuildOs.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000512 hostBinary := result.ModuleForTests("host_binary", buildOS+"_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400513 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
514 if strings.Contains(hostJavaFlags, "-g:source,lines") {
515 t.Errorf("java_binary_host javac flags %v should not have "+
516 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
517 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800518}
519
Colin Cross72bb3632017-07-13 16:23:21 -0700520func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700521 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700522 java_library {
523 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000524 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000525 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700526 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700527 }
528
Colin Cross74d73e22017-08-02 11:05:49 -0700529 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700530 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700531 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700532 }
533
Colin Cross74d73e22017-08-02 11:05:49 -0700534 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700535 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700536 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700537 sdk_version: "current",
538 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700539 }
Colin Cross42be7612019-02-21 18:12:14 -0800540
541 dex_import {
542 name: "qux",
543 jars: ["b.jar"],
544 }
Colin Cross79c7c262019-04-17 11:11:46 -0700545
546 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000547 name: "sdklib",
548 public: {
549 jars: ["c.jar"],
550 },
551 }
552
Paul Duffin91547182019-11-12 19:39:36 +0000553 prebuilt_stubs_sources {
554 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000555 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000556 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000557
558 java_test_import {
559 name: "test",
560 jars: ["a.jar"],
561 test_suites: ["cts"],
562 test_config: "AndroidTest.xml",
563 }
Colin Cross72bb3632017-07-13 16:23:21 -0700564 `)
565
Paul Duffin9b478b02019-12-10 13:41:51 +0000566 fooModule := ctx.ModuleForTests("foo", "android_common")
567 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700568 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700569 barModule := ctx.ModuleForTests("bar", "android_common")
570 barJar := barModule.Rule("combineJar").Output
571 bazModule := ctx.ModuleForTests("baz", "android_common")
572 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700573 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700574
Paul Duffin9b478b02019-12-10 13:41:51 +0000575 fooLibrary := fooModule.Module().(*Library)
576 assertDeepEquals(t, "foo java sources incorrect",
577 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000578
Paul Duffin9b478b02019-12-10 13:41:51 +0000579 assertDeepEquals(t, "foo java source jars incorrect",
580 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
581 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000582
Colin Cross37f6d792018-07-12 12:28:41 -0700583 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
584 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700585 }
586
Liz Kammerd6c31d22020-08-05 15:40:41 -0700587 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
588 if barDexJar != nil {
589 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
590 }
591
Colin Cross79c7c262019-04-17 11:11:46 -0700592 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
593 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
594 }
595
Colin Cross37f6d792018-07-12 12:28:41 -0700596 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
597 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700598 }
Colin Cross42be7612019-02-21 18:12:14 -0800599
Paul Duffin414ea5e2021-03-22 17:31:52 +0000600 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath()
601 expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar"
602 android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar)
Liz Kammerd6c31d22020-08-05 15:40:41 -0700603
Colin Cross42be7612019-02-21 18:12:14 -0800604 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700605}
606
Paul Duffin9b478b02019-12-10 13:41:51 +0000607func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
608 if !reflect.DeepEqual(expected, actual) {
609 t.Errorf("%s: expected %q, found %q", message, expected, actual)
610 }
611}
612
Paul Duffin1a393322020-11-18 16:36:47 +0000613func TestPrebuiltStubsSources(t *testing.T) {
614 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
615 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
616prebuilt_stubs_sources {
617 name: "stubs-source",
618 srcs: ["%s"],
619}`, sourcesPath), map[string][]byte{
620 "stubs/sources/pkg/A.java": nil,
621 "stubs/sources/pkg/B.java": nil,
622 })
623
624 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
625 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
626 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
627 }
628 }
629
630 t.Run("empty/missing directory", func(t *testing.T) {
Colin Crossce3a51d2021-03-19 16:22:12 -0700631 test(t, "empty-directory", nil)
Paul Duffin1a393322020-11-18 16:36:47 +0000632 })
633
634 t.Run("non-empty set of sources", func(t *testing.T) {
635 test(t, "stubs/sources", []string{
636 "stubs/sources/pkg/A.java",
637 "stubs/sources/pkg/B.java",
638 })
639 })
640}
641
Paul Duffin56d44902020-01-31 13:36:25 +0000642func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000643 result := prepareForJavaTest.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000644 java_library {
645 name: "foo",
646 srcs: ["a.java"],
647 libs: ["sdklib"],
648 sdk_version: "current",
649 }
650
651 java_library {
652 name: "foo.system",
653 srcs: ["a.java"],
654 libs: ["sdklib"],
655 sdk_version: "system_current",
656 }
657
658 java_library {
659 name: "foo.test",
660 srcs: ["a.java"],
661 libs: ["sdklib"],
662 sdk_version: "test_current",
663 }
664
665 java_sdk_library_import {
666 name: "sdklib",
667 public: {
668 jars: ["a.jar"],
669 },
670 system: {
671 jars: ["b.jar"],
672 },
673 test: {
674 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100675 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000676 },
677 }
678 `)
679
680 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000681 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000682 javac := fooModule.Rule("javac")
683
Paul Duffin22b77cd2021-03-12 19:15:01 +0000684 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
685 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000686 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100687
Paul Duffin22b77cd2021-03-12 19:15:01 +0000688 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100689 `prebuilt_sdklib.stubs`,
690 `prebuilt_sdklib.stubs.source.test`,
691 `prebuilt_sdklib.stubs.system`,
692 `prebuilt_sdklib.stubs.test`,
693 })
694}
695
696func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000697 result := android.GroupFixturePreparers(
698 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000699 PrepareForTestWithJavaSdkLibraryFiles,
700 FixtureWithLastReleaseApis("sdklib"),
701 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100702 java_sdk_library {
703 name: "sdklib",
704 srcs: ["a.java"],
705 sdk_version: "none",
706 system_modules: "none",
707 public: {
708 enabled: true,
709 },
710 }
711
712 java_sdk_library_import {
713 name: "sdklib",
714 public: {
715 jars: ["a.jar"],
716 },
717 }
718 `)
719
Paul Duffin22b77cd2021-03-12 19:15:01 +0000720 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100721 `dex2oatd`,
722 `prebuilt_sdklib`,
723 `sdklib.impl`,
724 `sdklib.stubs`,
725 `sdklib.stubs.source`,
726 `sdklib.xml`,
727 })
728
Paul Duffin22b77cd2021-03-12 19:15:01 +0000729 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100730 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100731 `sdklib.impl`,
732 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
733 // dependency is added after prebuilts may have been renamed and so has to use
734 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100735 `sdklib.xml`,
736 })
737}
738
739func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000740 result := android.GroupFixturePreparers(
741 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000742 PrepareForTestWithJavaSdkLibraryFiles,
743 FixtureWithLastReleaseApis("sdklib"),
744 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100745 java_sdk_library {
746 name: "sdklib",
747 srcs: ["a.java"],
748 sdk_version: "none",
749 system_modules: "none",
750 public: {
751 enabled: true,
752 },
753 }
754
755 java_sdk_library_import {
756 name: "sdklib",
757 prefer: true,
758 public: {
759 jars: ["a.jar"],
760 },
761 }
762 `)
763
Paul Duffin22b77cd2021-03-12 19:15:01 +0000764 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100765 `dex2oatd`,
766 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100767 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100768 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100769 `sdklib.stubs.source`,
770 `sdklib.xml`,
771 })
772
Paul Duffin22b77cd2021-03-12 19:15:01 +0000773 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100774 `prebuilt_sdklib.stubs`,
775 `sdklib.impl`,
776 `sdklib.xml`,
777 })
Paul Duffin56d44902020-01-31 13:36:25 +0000778}
779
JaeMan Parkff715562020-10-19 17:25:58 +0900780func TestJavaSdkLibraryEnforce(t *testing.T) {
781 partitionToBpOption := func(partition string) string {
782 switch partition {
783 case "system":
784 return ""
785 case "vendor":
786 return "soc_specific: true,"
787 case "product":
788 return "product_specific: true,"
789 default:
790 panic("Invalid partition group name: " + partition)
791 }
792 }
793
794 type testConfigInfo struct {
795 libraryType string
796 fromPartition string
797 toPartition string
798 enforceVendorInterface bool
799 enforceProductInterface bool
800 enforceJavaSdkLibraryCheck bool
801 allowList []string
802 }
803
Paul Duffin05f72de2021-03-12 21:28:51 +0000804 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900805 bpFileTemplate := `
806 java_library {
807 name: "foo",
808 srcs: ["foo.java"],
809 libs: ["bar"],
810 sdk_version: "current",
811 %s
812 }
813
814 %s {
815 name: "bar",
816 srcs: ["bar.java"],
817 sdk_version: "current",
818 %s
819 }
820 `
821
822 bpFile := fmt.Sprintf(bpFileTemplate,
823 partitionToBpOption(info.fromPartition),
824 info.libraryType,
825 partitionToBpOption(info.toPartition))
826
Paul Duffin05f72de2021-03-12 21:28:51 +0000827 return android.GroupFixturePreparers(
Paul Duffin163043d2021-03-12 19:18:49 +0000828 PrepareForTestWithJavaSdkLibraryFiles,
829 FixtureWithLastReleaseApis("bar"),
Paul Duffin05f72de2021-03-12 21:28:51 +0000830 android.FixtureWithRootAndroidBp(bpFile),
831 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
832 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
833 if info.enforceVendorInterface {
834 variables.DeviceVndkVersion = proptools.StringPtr("current")
835 }
836 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
837 variables.InterPartitionJavaLibraryAllowList = info.allowList
838 }),
839 )
JaeMan Parkff715562020-10-19 17:25:58 +0900840 }
841
Paul Duffinaa6caa72021-03-13 16:50:15 +0000842 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
Paul Duffina35178b2021-03-24 14:54:12 +0000843 t.Run(fmt.Sprintf("%v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000844 errorHandler := android.FixtureExpectsNoErrors
845 if expectedErrorPattern != "" {
846 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000847 }
Paul Duffin71ae5942021-03-22 15:36:52 +0000848 prepareForJavaTest.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
Paul Duffinaa6caa72021-03-13 16:50:15 +0000849 })
850 }
851
JaeMan Parkff715562020-10-19 17:25:58 +0900852 errorMessage := "is not allowed across the partitions"
853
Paul Duffinaa6caa72021-03-13 16:50:15 +0000854 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900855 libraryType: "java_library",
856 fromPartition: "product",
857 toPartition: "system",
858 enforceVendorInterface: true,
859 enforceProductInterface: true,
860 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000861 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900862
Paul Duffinaa6caa72021-03-13 16:50:15 +0000863 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900864 libraryType: "java_library",
865 fromPartition: "product",
866 toPartition: "system",
867 enforceVendorInterface: true,
868 enforceProductInterface: false,
869 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000870 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900871
Paul Duffinaa6caa72021-03-13 16:50:15 +0000872 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900873 libraryType: "java_library",
874 fromPartition: "product",
875 toPartition: "system",
876 enforceVendorInterface: true,
877 enforceProductInterface: true,
878 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000879 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900880
Paul Duffinaa6caa72021-03-13 16:50:15 +0000881 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900882 libraryType: "java_library",
883 fromPartition: "vendor",
884 toPartition: "system",
885 enforceVendorInterface: true,
886 enforceProductInterface: true,
887 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000888 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900889
Paul Duffinaa6caa72021-03-13 16:50:15 +0000890 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900891 libraryType: "java_library",
892 fromPartition: "vendor",
893 toPartition: "system",
894 enforceVendorInterface: true,
895 enforceProductInterface: true,
896 enforceJavaSdkLibraryCheck: true,
897 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +0000898 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900899
Paul Duffinaa6caa72021-03-13 16:50:15 +0000900 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900901 libraryType: "java_library",
902 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900903 toPartition: "product",
904 enforceVendorInterface: true,
905 enforceProductInterface: true,
906 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000907 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +0900908
Paul Duffinaa6caa72021-03-13 16:50:15 +0000909 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900910 libraryType: "java_sdk_library",
911 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +0900912 toPartition: "system",
913 enforceVendorInterface: true,
914 enforceProductInterface: true,
915 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000916 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +0900917
Paul Duffinaa6caa72021-03-13 16:50:15 +0000918 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900919 libraryType: "java_sdk_library",
920 fromPartition: "vendor",
921 toPartition: "system",
922 enforceVendorInterface: true,
923 enforceProductInterface: true,
924 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000925 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +0900926
Paul Duffinaa6caa72021-03-13 16:50:15 +0000927 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900928 libraryType: "java_sdk_library",
929 fromPartition: "vendor",
930 toPartition: "product",
931 enforceVendorInterface: true,
932 enforceProductInterface: true,
933 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000934 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900935}
936
Colin Cross89536d42017-07-07 14:35:50 -0700937func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700938 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700939 java_defaults {
940 name: "defaults",
941 srcs: ["a.java"],
942 libs: ["bar"],
943 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700944 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700945 }
946
947 java_library {
948 name: "foo",
949 defaults: ["defaults"],
950 }
951
952 java_library {
953 name: "bar",
954 srcs: ["b.java"],
955 }
956
957 java_library {
958 name: "baz",
959 srcs: ["c.java"],
960 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700961
962 android_test {
963 name: "atestOptimize",
964 defaults: ["defaults"],
965 optimize: {enabled: true},
966 }
967
968 android_test {
969 name: "atestNoOptimize",
970 defaults: ["defaults"],
971 }
972
973 android_test {
974 name: "atestDefault",
975 srcs: ["a.java"],
976 }
Colin Cross89536d42017-07-07 14:35:50 -0700977 `)
978
Paul Duffina71a67a2021-03-29 00:42:57 +0100979 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700980 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700981
982 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
983 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
984 }
985
Paul Duffin414ea5e2021-03-22 17:31:52 +0000986 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700987 if !strings.Contains(javac.Args["classpath"], barTurbine) {
988 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700989 }
990
Colin Cross1ee23172017-10-18 14:44:18 -0700991 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700992 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
993 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700994 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700995
996 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
997 if atestOptimize.Output == nil {
998 t.Errorf("atestOptimize should optimize APK")
999 }
1000
1001 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1002 if atestNoOptimize.Output == nil {
1003 t.Errorf("atestNoOptimize should not optimize APK")
1004 }
1005
1006 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1007 if atestDefault.Output == nil {
1008 t.Errorf("atestDefault should optimize APK")
1009 }
Colin Cross89536d42017-07-07 14:35:50 -07001010}
1011
Colin Cross0f37af02017-09-27 17:42:05 -07001012func TestResources(t *testing.T) {
1013 var table = []struct {
1014 name string
1015 prop string
1016 extra string
1017 args string
1018 }{
1019 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001020 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001021 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001022 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001023 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001024 },
1025 {
1026 // Test that a module with java_resources includes the files
1027 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001028 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1029 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001030 },
1031 {
1032 // Test that a module with a filegroup in java_resources includes the files with the
1033 // path prefix
1034 name: "resource filegroup",
1035 prop: `java_resources: [":foo-res"]`,
1036 extra: `
1037 filegroup {
1038 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001039 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001040 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001041 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001042 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001043 },
1044 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001045 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1046 name: "wildcard dirs",
1047 prop: `java_resource_dirs: ["java-res/*"]`,
1048 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1049 },
1050 {
1051 // Test that a module exclude_java_resource_dirs excludes the files
1052 name: "wildcard dirs",
1053 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1054 args: "-C java-res/a -f java-res/a/a",
1055 },
Colin Crosscedd4762018-09-13 11:26:19 -07001056 {
1057 // Test wildcards in java_resources
1058 name: "wildcard files",
1059 prop: `java_resources: ["java-res/**/*"]`,
1060 args: "-C . -f java-res/a/a -f java-res/b/b",
1061 },
1062 {
1063 // Test exclude_java_resources with java_resources
1064 name: "wildcard files with exclude",
1065 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1066 args: "-C . -f java-res/a/a",
1067 },
1068 {
1069 // Test exclude_java_resources with java_resource_dirs
1070 name: "resource dirs with exclude files",
1071 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1072 args: "-C java-res -f java-res/a/a",
1073 },
1074 {
1075 // Test exclude_java_resource_dirs with java_resource_dirs
1076 name: "resource dirs with exclude files",
1077 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1078 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1079 },
Colin Cross0f37af02017-09-27 17:42:05 -07001080 }
1081
1082 for _, test := range table {
1083 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001084 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001085 java_library {
1086 name: "foo",
1087 srcs: [
1088 "a.java",
1089 "b.java",
1090 "c.java",
1091 ],
1092 `+test.prop+`,
1093 }
Colin Cross238c1f32020-06-07 16:58:18 -07001094 `+test.extra,
1095 map[string][]byte{
1096 "java-res/a/a": nil,
1097 "java-res/b/b": nil,
1098 "java-res2/a": nil,
1099 },
1100 )
Colin Cross0f37af02017-09-27 17:42:05 -07001101
Colin Cross331a1212018-08-15 20:40:52 -07001102 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001103 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001104
1105 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1106 t.Errorf("foo combined jars %v does not contain %q",
1107 foo.Inputs.Strings(), fooRes.Output.String())
1108 }
1109
Colin Crossaf9c55b2017-10-03 14:50:08 -07001110 if fooRes.Args["jarArgs"] != test.args {
1111 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001112 fooRes.Args["jarArgs"], test.args)
1113 }
1114 })
1115 }
1116}
1117
Colin Cross0c4ce212019-05-03 15:28:19 -07001118func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001119 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001120 java_library {
1121 name: "foo",
1122 srcs: [
1123 "a.java",
1124 "b.java",
1125 "c.java",
1126 ],
1127 include_srcs: true,
1128 }
1129
1130 java_library {
1131 name: "bar",
1132 srcs: [
1133 "a.java",
1134 "b.java",
1135 "c.java",
1136 ],
1137 java_resource_dirs: ["java-res"],
1138 include_srcs: true,
1139 }
Colin Cross238c1f32020-06-07 16:58:18 -07001140 `, map[string][]byte{
1141 "java-res/a/a": nil,
1142 "java-res/b/b": nil,
1143 "java-res2/a": nil,
1144 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001145
1146 // Test a library with include_srcs: true
1147 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1148 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1149
1150 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1151 t.Errorf("foo combined jars %v does not contain %q", w, g)
1152 }
1153
1154 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1155 t.Errorf("foo source jar args %q is not %q", w, g)
1156 }
1157
1158 // Test a library with include_srcs: true and resources
1159 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1160 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1161 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1162 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1163
1164 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1165 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1166 }
1167
1168 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1169 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1170 }
1171
1172 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1173 t.Errorf("bar combined jars %v does not contain %q", w, g)
1174 }
1175
1176 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1177 t.Errorf("bar source jar args %q is not %q", w, g)
1178 }
1179
1180 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1181 t.Errorf("bar resource jar args %q is not %q", w, g)
1182 }
1183}
1184
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001185func TestJavaLint(t *testing.T) {
1186 ctx, _ := testJavaWithFS(t, `
1187 java_library {
1188 name: "foo",
1189 srcs: [
1190 "a.java",
1191 "b.java",
1192 "c.java",
1193 ],
1194 min_sdk_version: "29",
1195 sdk_version: "system_current",
1196 }
1197 `, map[string][]byte{
1198 "lint-baseline.xml": nil,
1199 })
1200
1201 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001202
Colin Cross1661aff2021-03-12 17:56:51 -08001203 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1204 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001205 t.Error("did not pass --baseline flag")
1206 }
1207}
1208
1209func TestJavaLintWithoutBaseline(t *testing.T) {
1210 ctx, _ := testJavaWithFS(t, `
1211 java_library {
1212 name: "foo",
1213 srcs: [
1214 "a.java",
1215 "b.java",
1216 "c.java",
1217 ],
1218 min_sdk_version: "29",
1219 sdk_version: "system_current",
1220 }
1221 `, map[string][]byte{})
1222
1223 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001224
Colin Cross1661aff2021-03-12 17:56:51 -08001225 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1226 if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001227 t.Error("passed --baseline flag for non existent file")
1228 }
1229}
1230
1231func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
Paul Duffin76e5c8a2021-03-20 14:19:46 +00001232 android.GroupFixturePreparers(
1233 PrepareForTestWithJavaDefaultModules,
1234 android.PrepareForTestDisallowNonExistentPaths,
1235 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern([]string{`source path "mybaseline.xml" does not exist`})).
1236 RunTestWithBp(t, `
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 `)
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001248}
1249
1250func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1251 ctx, _ := testJavaWithFS(t, `
1252 java_library {
1253 name: "foo",
1254 srcs: [
1255 "a.java",
1256 "b.java",
1257 "c.java",
1258 ],
1259 min_sdk_version: "29",
1260 sdk_version: "system_current",
1261 lint: {
1262 error_checks: ["SomeCheck"],
1263 baseline_filename: "mybaseline.xml",
1264 },
1265 }
1266 `, map[string][]byte{
1267 "mybaseline.xml": nil,
1268 })
1269
1270 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001271
Colin Cross1661aff2021-03-12 17:56:51 -08001272 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1273 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001274 t.Error("did not use the correct file for baseline")
1275 }
1276}
1277
Colin Cross54190b32017-10-09 15:34:10 -07001278func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001279 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001280 java_library {
1281 name: "foo",
1282 srcs: [
1283 "a*.java",
1284 ":gen",
1285 "b*.java",
1286 ],
1287 }
1288
1289 genrule {
1290 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001291 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001292 out: ["gen.java"],
1293 }
Colin Cross238c1f32020-06-07 16:58:18 -07001294 `, map[string][]byte{
1295 "a.java": nil,
1296 "b.java": nil,
1297 })
Colin Cross54190b32017-10-09 15:34:10 -07001298
1299 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1300 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1301
Colin Cross15e86d92017-10-20 15:07:08 -07001302 if filepath.Base(genrule.Output.String()) != "gen.java" {
1303 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001304 }
1305
1306 if len(javac.Inputs) != 3 ||
1307 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001308 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001309 javac.Inputs[2].String() != "b.java" {
1310 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1311 }
1312}
1313
Nan Zhang61eaedb2017-11-02 13:28:15 -07001314func TestTurbine(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001315 result := android.GroupFixturePreparers(
1316 prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
Paul Duffin3d9f2682021-03-13 09:47:16 +00001317 RunTestWithBp(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001318 java_library {
1319 name: "foo",
1320 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001321 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001322 }
1323
1324 java_library {
1325 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001326 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001327 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001328 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001329 }
1330
1331 java_library {
1332 name: "baz",
1333 srcs: ["c.java"],
1334 libs: ["bar"],
1335 sdk_version: "14",
1336 }
1337 `)
1338
Paul Duffina71a67a2021-03-29 00:42:57 +01001339 fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine")
1340 barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine")
1341 barJavac := result.ModuleForTests("bar", "android_common").Rule("javac")
1342 barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine")
1343 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001344
Paul Duffin414ea5e2021-03-22 17:31:52 +00001345 android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001346
Paul Duffin414ea5e2021-03-22 17:31:52 +00001347 fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1348 barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar")
Paul Duffin3d9f2682021-03-13 09:47:16 +00001349 android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
1350 android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
Paul Duffin414ea5e2021-03-22 17:31:52 +00001351 android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs)
Paul Duffin3d9f2682021-03-13 09:47:16 +00001352 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001353}
1354
1355func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001356 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001357 java_library {
1358 name: "bar",
1359 srcs: ["a.java","b.java","c.java"],
1360 javac_shard_size: 1
1361 }
1362 `)
1363
Paul Duffin414ea5e2021-03-22 17:31:52 +00001364 barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001365 for i := 0; i < 3; i++ {
Paul Duffina71a67a2021-03-29 00:42:57 +01001366 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
Nan Zhang61eaedb2017-11-02 13:28:15 -07001367 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1368 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1369 }
1370 }
1371}
1372
Colin Cross54250902017-12-05 09:28:08 -08001373func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001374 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001375 java_library {
1376 name: "foo",
1377 srcs: ["a.java"],
1378 }
1379
1380 java_genrule {
1381 name: "jargen",
1382 tool_files: ["b.java"],
1383 cmd: "$(location b.java) $(in) $(out)",
1384 out: ["jargen.jar"],
1385 srcs: [":foo"],
1386 }
1387
1388 java_library {
1389 name: "bar",
1390 static_libs: ["jargen"],
1391 srcs: ["c.java"],
1392 }
1393
1394 java_library {
1395 name: "baz",
1396 libs: ["jargen"],
1397 srcs: ["c.java"],
1398 }
1399 `)
1400
1401 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1402 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1403 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1404 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1405 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1406
Colin Cross3d680512020-11-13 16:23:53 -08001407 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1408 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001409 }
1410
1411 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1412 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1413 }
1414
1415 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1416 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1417 }
1418
1419 if len(barCombined.Inputs) != 2 ||
1420 barCombined.Inputs[0].String() != bar.Output.String() ||
1421 barCombined.Inputs[1].String() != jargen.Output.String() {
1422 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1423 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1424 }
1425}
1426
Nan Zhang27e284d2018-02-09 21:03:53 +00001427func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001428 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001429 java_library {
1430 name: "foo",
1431 srcs: ["a.java", ":foo-srcs"],
1432 exclude_srcs: ["a.java", ":foo-excludes"],
1433 }
1434
1435 filegroup {
1436 name: "foo-srcs",
1437 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1438 }
1439
1440 filegroup {
1441 name: "foo-excludes",
1442 srcs: ["java-fg/a.java", "java-fg/b.java"],
1443 }
1444 `)
1445
1446 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1447
1448 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1449 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1450 }
1451}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001452
Paul Duffin52d398a2019-06-11 12:31:14 +01001453func TestJavaLibrary(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001454 testJavaWithFS(t, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001455 "libcore/Android.bp": []byte(`
1456 java_library {
1457 name: "core",
1458 sdk_version: "none",
1459 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001460 }
1461
1462 filegroup {
1463 name: "core-jar",
1464 srcs: [":core{.jar}"],
1465 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001466 `),
Paul Duffinaa55f742020-10-06 17:20:13 +01001467 })
Paul Duffinaa55f742020-10-06 17:20:13 +01001468}
1469
1470func TestJavaImport(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001471 testJavaWithFS(t, "", map[string][]byte{
Paul Duffinaa55f742020-10-06 17:20:13 +01001472 "libcore/Android.bp": []byte(`
1473 java_import {
1474 name: "core",
1475 sdk_version: "none",
1476 }
1477
1478 filegroup {
1479 name: "core-jar",
1480 srcs: [":core{.jar}"],
1481 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001482 `),
Paul Duffin52d398a2019-06-11 12:31:14 +01001483 })
Paul Duffin52d398a2019-06-11 12:31:14 +01001484}
1485
Jiyong Parkc678ad32018-04-10 13:07:10 +09001486func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001487 result := android.GroupFixturePreparers(
1488 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001489 PrepareForTestWithJavaSdkLibraryFiles,
1490 FixtureWithPrebuiltApis(map[string][]string{
1491 "28": {"foo"},
1492 "29": {"foo"},
1493 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1494 }),
1495 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001496 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001497 name: "droiddoc-templates-sdk",
1498 path: ".",
1499 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001500 java_sdk_library {
1501 name: "foo",
1502 srcs: ["a.java", "b.java"],
1503 api_packages: ["foo"],
1504 }
1505 java_sdk_library {
1506 name: "bar",
1507 srcs: ["a.java", "b.java"],
1508 api_packages: ["bar"],
1509 }
1510 java_library {
1511 name: "baz",
1512 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001513 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001514 sdk_version: "system_current",
1515 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001516 java_sdk_library {
1517 name: "barney",
1518 srcs: ["c.java"],
1519 api_only: true,
1520 }
1521 java_sdk_library {
1522 name: "betty",
1523 srcs: ["c.java"],
1524 shared_library: false,
1525 }
Paul Duffin859fe962020-05-15 10:20:31 +01001526 java_sdk_library_import {
1527 name: "quuz",
1528 public: {
1529 jars: ["c.jar"],
1530 },
1531 }
1532 java_sdk_library_import {
1533 name: "fred",
1534 public: {
1535 jars: ["b.jar"],
1536 },
1537 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001538 java_sdk_library_import {
1539 name: "wilma",
1540 public: {
1541 jars: ["b.jar"],
1542 },
1543 shared_library: false,
1544 }
Jiyong Park1be96912018-05-28 18:02:19 +09001545 java_library {
1546 name: "qux",
1547 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001548 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001549 sdk_version: "system_current",
1550 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001551 java_library {
1552 name: "baz-test",
1553 srcs: ["c.java"],
1554 libs: ["foo"],
1555 sdk_version: "test_current",
1556 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001557 java_library {
1558 name: "baz-29",
1559 srcs: ["c.java"],
1560 libs: ["foo"],
1561 sdk_version: "system_29",
1562 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001563 java_library {
1564 name: "baz-module-30",
1565 srcs: ["c.java"],
1566 libs: ["foo"],
1567 sdk_version: "module_30",
1568 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001569 `)
1570
1571 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001572 result.ModuleForTests("foo", "android_common")
1573 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1574 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1575 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1576 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1577 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1578 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1579 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1580 result.ModuleForTests("foo.api.public.28", "")
1581 result.ModuleForTests("foo.api.system.28", "")
1582 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001583
Paul Duffin22b77cd2021-03-12 19:15:01 +00001584 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001585 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001586 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001587 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001588 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001589 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001590 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001591
Paul Duffin22b77cd2021-03-12 19:15:01 +00001592 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001593 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001594 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001595
Paul Duffin22b77cd2021-03-12 19:15:01 +00001596 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001597 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001598 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001599
Paul Duffin22b77cd2021-03-12 19:15:01 +00001600 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001601 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001602 android.AssertStringDoesContain(t, "baz-module-30 javac classpath", bazModule30Javac.Args["classpath"], "prebuilts/sdk/30/module-lib/foo.jar")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001603
Jiyong Park1be96912018-05-28 18:02:19 +09001604 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001605 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001606 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001607 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001608 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001609 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001610}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001611
Anton Hansson7f66efa2020-10-08 14:47:23 +01001612func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001613 result := android.GroupFixturePreparers(
1614 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001615 PrepareForTestWithJavaSdkLibraryFiles,
1616 FixtureWithLastReleaseApis("sdklib"),
1617 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001618 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001619 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001620 srcs: ["a.java"],
1621 impl_only_libs: ["foo"],
1622 stub_only_libs: ["bar"],
1623 }
1624 java_library {
1625 name: "foo",
1626 srcs: ["a.java"],
1627 sdk_version: "current",
1628 }
1629 java_library {
1630 name: "bar",
1631 srcs: ["a.java"],
1632 sdk_version: "current",
1633 }
1634 `)
1635
Anton Hanssondff2c782020-12-21 17:10:01 +00001636 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001637 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001638 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1639 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1640 }
1641 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001642 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00001643 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001644 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1645 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1646 }
1647}
1648
Paul Duffindaaa3322020-05-26 18:13:57 +01001649func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001650 result := android.GroupFixturePreparers(
1651 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001652 PrepareForTestWithJavaSdkLibraryFiles,
1653 FixtureWithLastReleaseApis("foo"),
1654 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01001655 java_sdk_library {
1656 name: "foo",
1657 srcs: ["a.java"],
1658 api_only: true,
1659 public: {
1660 enabled: true,
1661 },
1662 }
1663
1664 java_library {
1665 name: "bar",
1666 srcs: ["b.java"],
1667 libs: ["foo"],
1668 }
1669 `)
1670
1671 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001672 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffine8366da2021-03-24 10:40:38 +00001673 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Paul Duffindaaa3322020-05-26 18:13:57 +01001674 t.Errorf("expected %q, found %#q", expected, actual)
1675 }
1676}
1677
Paul Duffin46dc45a2020-05-14 15:39:10 +01001678func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001679 android.GroupFixturePreparers(
1680 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001681 PrepareForTestWithJavaSdkLibraryFiles,
1682 FixtureWithLastReleaseApis("foo"),
1683 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001684 java_sdk_library {
1685 name: "foo",
1686 srcs: ["a.java"],
1687 api_packages: ["foo"],
1688 public: {
1689 enabled: true,
1690 },
1691 }
1692
1693 java_library {
1694 name: "bar",
1695 srcs: ["b.java", ":foo{.public.stubs.source}"],
1696 }
1697 `)
1698}
1699
1700func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001701 android.GroupFixturePreparers(
1702 prepareForJavaTest,
1703 PrepareForTestWithJavaSdkLibraryFiles,
1704 FixtureWithLastReleaseApis("foo"),
1705 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00001706 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
1707 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001708 java_sdk_library {
1709 name: "foo",
1710 srcs: ["a.java"],
1711 api_packages: ["foo"],
1712 public: {
1713 enabled: true,
1714 },
1715 }
1716
1717 java_library {
1718 name: "bar",
1719 srcs: ["b.java", ":foo{.system.stubs.source}"],
1720 }
1721 `)
1722}
1723
Paul Duffinca8d9a52020-06-26 22:20:25 +01001724func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001725 result := android.GroupFixturePreparers(
1726 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001727 PrepareForTestWithJavaSdkLibraryFiles,
1728 FixtureWithLastReleaseApis("sdklib"),
1729 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01001730 java_sdk_library {
1731 name: "sdklib",
1732 srcs: ["a.java"],
1733 sdk_version: "none",
1734 system_modules: "none",
1735 public: {
1736 enabled: true,
1737 },
1738 }
1739 `)
1740
Paul Duffin22b77cd2021-03-12 19:15:01 +00001741 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001742 `dex2oatd`,
1743 `sdklib.impl`,
1744 `sdklib.stubs`,
1745 `sdklib.stubs.source`,
1746 `sdklib.xml`,
1747 })
1748}
1749
Paul Duffin46dc45a2020-05-14 15:39:10 +01001750func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001751 prepareForJavaTest.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001752 java_sdk_library_import {
1753 name: "foo",
1754 public: {
1755 jars: ["a.jar"],
1756 stub_srcs: ["a.java"],
1757 current_api: "api/current.txt",
1758 removed_api: "api/removed.txt",
1759 },
1760 }
1761
1762 java_library {
1763 name: "bar",
1764 srcs: [":foo{.public.stubs.source}"],
1765 java_resources: [
1766 ":foo{.public.api.txt}",
1767 ":foo{.public.removed-api.txt}",
1768 ],
1769 }
1770 `)
1771}
1772
1773func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1774 bp := `
1775 java_sdk_library_import {
1776 name: "foo",
1777 public: {
1778 jars: ["a.jar"],
1779 },
1780 }
1781 `
1782
1783 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001784 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001785 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
1786 RunTestWithBp(t, bp+`
1787 java_library {
1788 name: "bar",
1789 srcs: [":foo{.public.stubs.source}"],
1790 java_resources: [
1791 ":foo{.public.api.txt}",
1792 ":foo{.public.removed-api.txt}",
1793 ],
1794 }
1795 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001796 })
1797
1798 t.Run("api.txt", func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001799 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001800 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
1801 RunTestWithBp(t, bp+`
1802 java_library {
1803 name: "bar",
1804 srcs: ["a.java"],
1805 java_resources: [
1806 ":foo{.public.api.txt}",
1807 ],
1808 }
1809 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001810 })
1811
1812 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001813 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001814 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
1815 RunTestWithBp(t, bp+`
1816 java_library {
1817 name: "bar",
1818 srcs: ["a.java"],
1819 java_resources: [
1820 ":foo{.public.removed-api.txt}",
1821 ],
1822 }
1823 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001824 })
1825}
1826
Paul Duffin3375e352020-04-28 10:44:03 +01001827func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001828 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001829 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
1830 RunTestWithBp(t, `
1831 java_sdk_library {
1832 name: "foo",
1833 srcs: ["a.java", "b.java"],
1834 api_packages: ["foo"],
1835 // Explicitly disable public to test the check that ensures the set of enabled
1836 // scopes is consistent.
1837 public: {
1838 enabled: false,
1839 },
1840 system: {
1841 enabled: true,
1842 },
1843 }
Paul Duffin3375e352020-04-28 10:44:03 +01001844 `)
1845}
1846
Paul Duffin87a05a32020-05-12 11:50:28 +01001847func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001848 android.GroupFixturePreparers(
1849 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001850 PrepareForTestWithJavaSdkLibraryFiles,
1851 FixtureWithLastReleaseApis("foo"),
1852 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01001853 java_sdk_library {
1854 name: "foo",
1855 srcs: ["a.java", "b.java"],
1856 api_packages: ["foo"],
1857 system: {
1858 enabled: true,
1859 sdk_version: "module_current",
1860 },
1861 }
1862 `)
1863}
1864
Paul Duffin0c5bae52020-06-02 13:00:08 +01001865func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001866 android.GroupFixturePreparers(
1867 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001868 PrepareForTestWithJavaSdkLibraryFiles,
1869 FixtureWithLastReleaseApis("foo"),
1870 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01001871 java_sdk_library {
1872 name: "foo",
1873 srcs: ["a.java", "b.java"],
1874 api_packages: ["foo"],
1875 system: {
1876 enabled: true,
1877 },
1878 module_lib: {
1879 enabled: true,
1880 },
1881 }
1882 `)
1883}
1884
1885func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001886 android.GroupFixturePreparers(
1887 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001888 PrepareForTestWithJavaSdkLibraryFiles,
1889 FixtureWithLastReleaseApis("foo"),
1890 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01001891 java_sdk_library {
1892 name: "foo",
1893 srcs: ["a.java", "b.java"],
1894 api_packages: ["foo"],
1895 system: {
1896 enabled: true,
1897 },
1898 system_server: {
1899 enabled: true,
1900 },
1901 }
1902 `)
1903}
1904
Paul Duffin803a9562020-05-20 11:52:25 +01001905func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001906 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001907 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
1908 RunTestWithBp(t, `
1909 java_sdk_library {
1910 name: "foo",
1911 srcs: ["a.java"],
1912 public: {
1913 enabled: false,
1914 },
1915 }
Paul Duffin803a9562020-05-20 11:52:25 +01001916
Paul Duffin22b77cd2021-03-12 19:15:01 +00001917 java_library {
1918 name: "baz",
1919 srcs: ["a.java"],
1920 libs: ["foo"],
1921 sdk_version: "module_current",
1922 }
Paul Duffin803a9562020-05-20 11:52:25 +01001923 `)
1924}
1925
1926func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001927 android.GroupFixturePreparers(
1928 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001929 PrepareForTestWithJavaSdkLibraryFiles,
1930 FixtureWithLastReleaseApis("foo"),
1931 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01001932 java_sdk_library {
1933 name: "foo",
1934 srcs: ["a.java"],
1935 system: {
1936 enabled: true,
1937 },
1938 }
1939
1940 java_library {
1941 name: "baz",
1942 srcs: ["a.java"],
1943 libs: ["foo"],
1944 // foo does not have module-lib scope so it should fallback to system
1945 sdk_version: "module_current",
1946 }
1947 `)
1948}
1949
Jiyong Park932cdfe2020-05-28 00:19:53 +09001950func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001951 result := android.GroupFixturePreparers(
1952 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001953 PrepareForTestWithJavaSdkLibraryFiles,
1954 FixtureWithLastReleaseApis("foo"),
1955 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09001956 java_sdk_library {
1957 name: "foo",
1958 srcs: ["a.java"],
1959 system: {
1960 enabled: true,
1961 },
1962 default_to_stubs: true,
1963 }
1964
1965 java_library {
1966 name: "baz",
1967 srcs: ["a.java"],
1968 libs: ["foo"],
1969 // does not have sdk_version set, should fallback to module,
1970 // which will then fallback to system because the module scope
1971 // is not enabled.
1972 }
1973 `)
1974 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001975 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Paul Duffine8366da2021-03-24 10:40:38 +00001976 if expected, actual := `^-classpath .*:out/soong/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
Jiyong Park932cdfe2020-05-28 00:19:53 +09001977 t.Errorf("expected %q, found %#q", expected, actual)
1978 }
1979}
1980
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001981var compilerFlagsTestCases = []struct {
1982 in string
1983 out bool
1984}{
1985 {
1986 in: "a",
1987 out: false,
1988 },
1989 {
1990 in: "-a",
1991 out: true,
1992 },
1993 {
1994 in: "-no-jdk",
1995 out: false,
1996 },
1997 {
1998 in: "-no-stdlib",
1999 out: false,
2000 },
2001 {
2002 in: "-kotlin-home",
2003 out: false,
2004 },
2005 {
2006 in: "-kotlin-home /some/path",
2007 out: false,
2008 },
2009 {
2010 in: "-include-runtime",
2011 out: false,
2012 },
2013 {
2014 in: "-Xintellij-plugin-root",
2015 out: false,
2016 },
2017}
2018
2019type mockContext struct {
2020 android.ModuleContext
2021 result bool
2022}
2023
2024func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2025 // CheckBadCompilerFlags calls this function when the flag should be rejected
2026 ctx.result = false
2027}
2028
2029func TestCompilerFlags(t *testing.T) {
2030 for _, testCase := range compilerFlagsTestCases {
2031 ctx := &mockContext{result: true}
2032 CheckKotlincFlags(ctx, []string{testCase.in})
2033 if ctx.result != testCase.out {
2034 t.Errorf("incorrect output:")
2035 t.Errorf(" input: %#v", testCase.in)
2036 t.Errorf(" expected: %#v", testCase.out)
2037 t.Errorf(" got: %#v", ctx.result)
2038 }
2039 }
2040}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002041
2042// TODO(jungjw): Consider making this more robust by ignoring path order.
2043func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00002044 variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop()
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002045 flags := strings.Split(variables["javacFlags"], " ")
2046 got := ""
2047 for _, flag := range flags {
2048 keyEnd := strings.Index(flag, "=")
2049 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2050 got = flag[keyEnd+1:]
2051 break
2052 }
2053 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00002054 if expected != android.StringPathRelativeToTop(ctx.Config().BuildDir(), got) {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002055 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2056 }
2057}
2058
2059func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002060 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002061 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002062 bp := `
2063 java_library {
2064 name: "foo",
2065 srcs: ["a.java"],
2066 java_version: "1.8",
2067 }
2068
2069 java_library {
2070 name: "bar",
2071 srcs: ["b.java"],
2072 sdk_version: "none",
2073 system_modules: "none",
2074 patch_module: "java.base",
2075 java_version: "1.8",
2076 }
2077
2078 java_library {
2079 name: "baz",
2080 srcs: ["c.java"],
2081 patch_module: "java.base",
2082 java_version: "1.8",
2083 }
2084 `
2085 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002086
2087 checkPatchModuleFlag(t, ctx, "foo", "")
2088 checkPatchModuleFlag(t, ctx, "bar", "")
2089 checkPatchModuleFlag(t, ctx, "baz", "")
2090 })
2091
Pete Gillin0c2143e2019-05-02 15:32:11 +01002092 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002093 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002094 bp := `
2095 java_library {
2096 name: "foo",
2097 srcs: ["a.java"],
2098 }
2099
2100 java_library {
2101 name: "bar",
2102 srcs: ["b.java"],
2103 sdk_version: "none",
2104 system_modules: "none",
2105 patch_module: "java.base",
2106 }
2107
2108 java_library {
2109 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002110 srcs: [
2111 "c.java",
2112 // Tests for b/150878007
2113 "dir/d.java",
2114 "dir2/e.java",
2115 "dir2/f.java",
2116 "nested/dir/g.java"
2117 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002118 patch_module: "java.base",
2119 }
2120 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002121 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002122
2123 checkPatchModuleFlag(t, ctx, "foo", "")
Paul Duffin414ea5e2021-03-22 17:31:52 +00002124 expected := "java.base=.:out/soong"
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002125 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002126 expected = "java.base=" + strings.Join([]string{
Paul Duffin414ea5e2021-03-22 17:31:52 +00002127 ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002128 checkPatchModuleFlag(t, ctx, "baz", expected)
2129 })
2130}
Paul Duffina7b9f422020-01-10 17:12:18 +00002131
Paul Duffin83a2d962019-11-19 19:44:10 +00002132func TestJavaLibraryWithSystemModules(t *testing.T) {
2133 ctx, _ := testJava(t, `
2134 java_library {
2135 name: "lib-with-source-system-modules",
2136 srcs: [
2137 "a.java",
2138 ],
2139 sdk_version: "none",
2140 system_modules: "source-system-modules",
2141 }
2142
2143 java_library {
2144 name: "source-jar",
2145 srcs: [
2146 "a.java",
2147 ],
2148 }
2149
2150 java_system_modules {
2151 name: "source-system-modules",
2152 libs: ["source-jar"],
2153 }
2154
2155 java_library {
2156 name: "lib-with-prebuilt-system-modules",
2157 srcs: [
2158 "a.java",
2159 ],
2160 sdk_version: "none",
2161 system_modules: "prebuilt-system-modules",
2162 }
2163
2164 java_import {
2165 name: "prebuilt-jar",
2166 jars: ["a.jar"],
2167 }
2168
2169 java_system_modules_import {
2170 name: "prebuilt-system-modules",
2171 libs: ["prebuilt-jar"],
2172 }
2173 `)
2174
2175 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2176
2177 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2178}
2179
2180func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2181 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2182 bootClasspath := javacRule.Args["bootClasspath"]
2183 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2184 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2185 }
2186}
Jiyong Park19604de2020-03-24 16:44:11 +09002187
2188func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2189 ctx, _ := testJava(t, `
2190 java_library {
2191 name: "foo",
2192 srcs: ["aidl/foo/IFoo.aidl"],
2193 libs: ["bar"],
2194 }
2195
2196 java_import {
2197 name: "bar",
2198 jars: ["a.jar"],
2199 aidl: {
2200 export_include_dirs: ["aidl/bar"],
2201 },
2202 }
2203 `)
2204
2205 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2206 expectedAidlFlag := "-Iaidl/bar"
2207 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2208 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2209 }
2210}
Liz Kammerdd849a82020-06-12 16:38:45 -07002211
Jooyung Hane197d8b2021-01-05 10:33:16 +09002212func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2213 ctx, _ := testJava(t, `
2214 java_library {
2215 name: "foo",
2216 srcs: ["aidl/foo/IFoo.aidl"],
2217 aidl: { flags: ["-Werror"], },
2218 }
2219 `)
2220
2221 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2222 expectedAidlFlag := "-Werror"
2223 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2224 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2225 }
2226}
2227
Liz Kammerdd849a82020-06-12 16:38:45 -07002228func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002229 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002230 java_test_host {
2231 name: "foo",
2232 srcs: ["a.java"],
2233 data_native_bins: ["bin"]
2234 }
2235
2236 python_binary_host {
2237 name: "bin",
2238 srcs: ["bin.py"],
2239 }
2240 `)
2241
2242 buildOS := android.BuildOs.String()
2243
2244 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002245 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002246 expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
Liz Kammerdd849a82020-06-12 16:38:45 -07002247 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002248 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual)
Liz Kammerdd849a82020-06-12 16:38:45 -07002249}
Yuexi Ma627263f2021-03-04 13:47:56 -08002250
2251func TestDefaultInstallable(t *testing.T) {
2252 ctx, _ := testJava(t, `
2253 java_test_host {
2254 name: "foo"
2255 }
2256 `)
2257
2258 buildOS := android.BuildOs.String()
2259 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2260 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2261 module.properties.Installable)
2262}