blob: e7ea4ef54eccce66dc4939ef89cf3f3b38c499b1 [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"
Colin Crossf8d9c492021-01-26 11:01:43 -080023 "runtime"
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"
Colin Cross1661aff2021-03-12 17:56:51 -080033 "android/soong/genrule"
Liz Kammerdd849a82020-06-12 16:38:45 -070034 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070035)
36
Paul Duffin70d3bee2021-03-21 11:26:05 +000037// Legacy preparer used for running tests within the java package.
38//
39// This includes everything that was needed to run any test in the java package prior to the
40// introduction of the test fixtures. Tests that are being converted to use fixtures directly
41// rather than through the testJava...() methods should avoid using this and instead use the
42// various preparers directly, using android.GroupFixturePreparers(...) to group them when
43// necessary.
44//
45// deprecated
46var prepareForJavaTest = android.GroupFixturePreparers(
Paul Duffin95bdab42021-03-08 21:48:46 +000047 genrule.PrepareForTestWithGenRuleBuildComponents,
48 // Get the CC build components but not default modules.
49 cc.PrepareForTestWithCcBuildComponents,
50 // Include all the default java modules.
51 PrepareForTestWithJavaDefaultModules,
Paul Duffin42da69d2021-03-22 13:41:36 +000052 PrepareForTestWithOverlayBuildComponents,
Paul Duffin220ddd72021-03-17 23:54:30 +000053 python.PrepareForTestWithPythonBuildComponents,
Paul Duffin95bdab42021-03-08 21:48:46 +000054 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
Paul Duffin95bdab42021-03-08 21:48:46 +000055 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
56 }),
Paul Duffin9fc9f532021-03-23 15:41:11 +000057 PrepareForTestWithDexpreopt,
Paul Duffin95bdab42021-03-08 21:48:46 +000058)
59
Colin Cross72bb3632017-07-13 16:23:21 -070060func TestMain(m *testing.M) {
Paul Duffind6339af2021-03-22 18:05:04 +000061 os.Exit(m.Run())
Colin Cross527012a2017-11-30 22:56:16 -080062}
63
Paul Duffin95bdab42021-03-08 21:48:46 +000064// testJavaError is a legacy way of running tests of java modules that expect errors.
65//
66// See testJava for an explanation as to how to stop using this deprecated method.
67//
68// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +090069func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +090070 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +000071 result := android.GroupFixturePreparers(
Paul Duffin9fc9f532021-03-23 15:41:11 +000072 prepareForJavaTest, dexpreopt.PrepareForTestByEnablingDexpreopt).
Paul Duffin6bac49c2021-03-12 21:28:15 +000073 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
74 RunTestWithBp(t, bp)
75 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +090076}
77
Paul Duffin71ae5942021-03-22 15:36:52 +000078// testJavaWithFS runs tests using the prepareForJavaTest
Paul Duffin95bdab42021-03-08 21:48:46 +000079//
80// See testJava for an explanation as to how to stop using this deprecated method.
81//
82// deprecated
83func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -070084 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +000085 result := android.GroupFixturePreparers(
86 prepareForJavaTest, fs.AddToFixture()).RunTestWithBp(t, bp)
Paul Duffin95bdab42021-03-08 21:48:46 +000087 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -070088}
89
Paul Duffin71ae5942021-03-22 15:36:52 +000090// testJava runs tests using the prepareForJavaTest
Paul Duffin95bdab42021-03-08 21:48:46 +000091//
Paul Duffin71ae5942021-03-22 15:36:52 +000092// Do not add any new usages of this, instead use the prepareForJavaTest directly as it makes it
Paul Duffin95bdab42021-03-08 21:48:46 +000093// much easier to customize the test behavior.
94//
95// 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 +000096// convert the test to using prepareForJavaTest first and then in a following change add the
Paul Duffin95bdab42021-03-08 21:48:46 +000097// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
98// that it did not change the test behavior unexpectedly.
99//
100// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700101func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800102 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +0000103 result := prepareForJavaTest.RunTestWithBp(t, bp)
Paul Duffin95bdab42021-03-08 21:48:46 +0000104 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900105}
106
Paul Duffin95bdab42021-03-08 21:48:46 +0000107// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
108// is defined in PrepareForIntegrationTestWithJava
109func defaultModuleToPath(name string) string {
Paul Duffin76101fa2021-03-13 09:55:25 +0000110 switch {
111 case name == `""`:
112 return name
113 case strings.HasSuffix(name, ".jar"):
114 return name
115 default:
Paul Duffin414ea5e2021-03-22 17:31:52 +0000116 return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
Paul Duffin76101fa2021-03-13 09:55:25 +0000117 }
Paul Duffin95bdab42021-03-08 21:48:46 +0000118}
119
Paul Duffin76e5c8a2021-03-20 14:19:46 +0000120// Test that the PrepareForTestWithJavaDefaultModules provides all the files that it uses by
121// running it in a fixture that requires all source files to exist.
122func TestPrepareForTestWithJavaDefaultModules(t *testing.T) {
123 android.GroupFixturePreparers(
124 PrepareForTestWithJavaDefaultModules,
125 android.PrepareForTestDisallowNonExistentPaths,
126 ).RunTest(t)
127}
128
Jeongik Chae403e9e2019-12-07 00:16:24 +0900129func TestJavaLinkType(t *testing.T) {
130 testJava(t, `
131 java_library {
132 name: "foo",
133 srcs: ["a.java"],
134 libs: ["bar"],
135 static_libs: ["baz"],
136 }
137
138 java_library {
139 name: "bar",
140 sdk_version: "current",
141 srcs: ["b.java"],
142 }
143
144 java_library {
145 name: "baz",
146 sdk_version: "system_current",
147 srcs: ["c.java"],
148 }
149 `)
150
Steven Moreland00298982020-11-17 21:44:36 +0000151 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900152 java_library {
153 name: "foo",
154 srcs: ["a.java"],
155 libs: ["bar"],
156 sdk_version: "current",
157 static_libs: ["baz"],
158 }
159
160 java_library {
161 name: "bar",
162 sdk_version: "current",
163 srcs: ["b.java"],
164 }
165
166 java_library {
167 name: "baz",
168 sdk_version: "system_current",
169 srcs: ["c.java"],
170 }
171 `)
172
173 testJava(t, `
174 java_library {
175 name: "foo",
176 srcs: ["a.java"],
177 libs: ["bar"],
178 sdk_version: "system_current",
179 static_libs: ["baz"],
180 }
181
182 java_library {
183 name: "bar",
184 sdk_version: "current",
185 srcs: ["b.java"],
186 }
187
188 java_library {
189 name: "baz",
190 sdk_version: "system_current",
191 srcs: ["c.java"],
192 }
193 `)
194
Steven Moreland00298982020-11-17 21:44:36 +0000195 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900196 java_library {
197 name: "foo",
198 srcs: ["a.java"],
199 libs: ["bar"],
200 sdk_version: "system_current",
201 static_libs: ["baz"],
202 }
203
204 java_library {
205 name: "bar",
206 sdk_version: "current",
207 srcs: ["b.java"],
208 }
209
210 java_library {
211 name: "baz",
212 srcs: ["c.java"],
213 }
214 `)
215}
216
Colin Cross72bb3632017-07-13 16:23:21 -0700217func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700218 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700219 java_library {
220 name: "foo",
221 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700222 libs: ["bar"],
223 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700224 }
225
226 java_library {
227 name: "bar",
228 srcs: ["b.java"],
229 }
230
231 java_library {
232 name: "baz",
233 srcs: ["c.java"],
234 }
Colin Crossd5934c82017-10-02 13:55:26 -0700235 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700236
Paul Duffina71a67a2021-03-29 00:42:57 +0100237 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700238 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700239
240 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
241 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
242 }
243
Colin Cross1ee23172017-10-18 14:44:18 -0700244 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000245 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
246 bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700247
Paul Duffin22b77cd2021-03-12 19:15:01 +0000248 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700249
Paul Duffin22b77cd2021-03-12 19:15:01 +0000250 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700251
252 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
253 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700254 }
255}
256
Artur Satayev9cf46692019-11-26 18:08:34 +0000257func TestExportedPlugins(t *testing.T) {
258 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800259 library string
260 processors string
261 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000262 }
263 var tests = []struct {
264 name string
265 extra string
266 results []Result
267 }{
268 {
269 name: "Exported plugin is not a direct plugin",
270 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
271 results: []Result{{library: "exports", processors: "-proc:none"}},
272 },
273 {
274 name: "Exports plugin to dependee",
275 extra: `
276 java_library{name: "exports", exported_plugins: ["plugin"]}
277 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
278 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
279 `,
280 results: []Result{
281 {library: "foo", processors: "-processor com.android.TestPlugin"},
282 {library: "bar", processors: "-processor com.android.TestPlugin"},
283 },
284 },
285 {
286 name: "Exports plugin to android_library",
287 extra: `
288 java_library{name: "exports", exported_plugins: ["plugin"]}
289 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
290 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
291 `,
292 results: []Result{
293 {library: "foo", processors: "-processor com.android.TestPlugin"},
294 {library: "bar", processors: "-processor com.android.TestPlugin"},
295 },
296 },
297 {
298 name: "Exports plugin is not propagated via transitive deps",
299 extra: `
300 java_library{name: "exports", exported_plugins: ["plugin"]}
301 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
302 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
303 `,
304 results: []Result{
305 {library: "foo", processors: "-processor com.android.TestPlugin"},
306 {library: "bar", processors: "-proc:none"},
307 },
308 },
309 {
310 name: "Exports plugin appends to plugins",
311 extra: `
312 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
313 java_library{name: "exports", exported_plugins: ["plugin"]}
314 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
315 `,
316 results: []Result{
317 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
318 },
319 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800320 {
321 name: "Exports plugin to with generates_api to dependee",
322 extra: `
323 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
324 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
325 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
326 `,
327 results: []Result{
328 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
329 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
330 },
331 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000332 }
333
334 for _, test := range tests {
335 t.Run(test.name, func(t *testing.T) {
336 ctx, _ := testJava(t, `
337 java_plugin {
338 name: "plugin",
339 processor_class: "com.android.TestPlugin",
340 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800341 java_plugin {
342 name: "plugin_generates_api",
343 generates_api: true,
344 processor_class: "com.android.TestPlugin",
345 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000346 `+test.extra)
347
348 for _, want := range test.results {
349 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
350 if javac.Args["processor"] != want.processors {
351 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
352 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800353 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
354 disableTurbine := turbine.BuildParams.Rule == nil
355 if disableTurbine != want.disableTurbine {
356 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
357 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000358 }
359 })
360 }
361}
362
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900363func TestSdkVersionByPartition(t *testing.T) {
364 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900365 java_library {
366 name: "foo",
367 srcs: ["a.java"],
368 vendor: true,
369 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900370 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900371
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900372 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900373 java_library {
374 name: "bar",
375 srcs: ["b.java"],
376 }
377 `)
378
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900379 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900380 bp := `
381 java_library {
382 name: "foo",
383 srcs: ["a.java"],
384 product_specific: true,
385 }
386 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800387
Paul Duffin414ea5e2021-03-22 17:31:52 +0000388 errorHandler := android.FixtureExpectsNoErrors
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900389 if enforce {
Paul Duffin414ea5e2021-03-22 17:31:52 +0000390 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 +0900391 }
Paul Duffin414ea5e2021-03-22 17:31:52 +0000392
393 android.GroupFixturePreparers(
394 PrepareForTestWithJavaDefaultModules,
395 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
396 variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
397 }),
398 ).
399 ExtendWithErrorHandler(errorHandler).
400 RunTestWithBp(t, bp)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900401 }
402}
403
Colin Crossd5934c82017-10-02 13:55:26 -0700404func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700405 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700406 java_library {
407 name: "foo",
408 srcs: ["a.java"],
409 target: {
410 android: {
411 srcs: ["b.java"],
412 },
413 },
414 }
415 `)
416
417 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
418 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
419 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
420 }
421}
422
Colin Cross6b4a32d2017-12-05 13:42:45 -0800423func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700424 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800425 java_library_host {
426 name: "foo",
427 srcs: ["a.java"],
428 }
429
430 java_binary_host {
431 name: "bar",
432 srcs: ["b.java"],
433 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700434 jni_libs: ["libjni"],
435 }
436
437 cc_library_shared {
438 name: "libjni",
439 host_supported: true,
440 device_supported: false,
441 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800442 }
443 `)
444
445 buildOS := android.BuildOs.String()
446
447 bar := ctx.ModuleForTests("bar", buildOS+"_common")
448 barJar := bar.Output("bar.jar").Output.String()
449 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
450 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
451
Colin Cross89226d92020-10-09 19:00:54 -0700452 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
453 libjniSO := libjni.Rule("Cp").Output.String()
454
Colin Cross6b4a32d2017-12-05 13:42:45 -0800455 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700456 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
457 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800458 }
Colin Cross89226d92020-10-09 19:00:54 -0700459
460 // Test that the install binary wrapper depends on the installed JNI libraries
461 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
462 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800463 }
Alex Humesky2070e322020-06-09 20:23:08 -0400464}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800465
Colin Crossf8d9c492021-01-26 11:01:43 -0800466func TestTest(t *testing.T) {
467 ctx, _ := testJava(t, `
468 java_test_host {
469 name: "foo",
470 srcs: ["a.java"],
471 jni_libs: ["libjni"],
472 }
473
474 cc_library_shared {
475 name: "libjni",
476 host_supported: true,
477 device_supported: false,
478 stl: "none",
479 }
480 `)
481
482 buildOS := android.BuildOs.String()
483
484 foo := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
485
486 expected := "lib64/libjni.so"
487 if runtime.GOOS == "darwin" {
488 expected = "lib64/libjni.dylib"
489 }
490
491 fooTestData := foo.data
492 if len(fooTestData) != 1 || fooTestData[0].Rel() != expected {
493 t.Errorf(`expected foo test data relative path [%q], got %q`,
494 expected, fooTestData.Strings())
495 }
496}
497
Alex Humesky2070e322020-06-09 20:23:08 -0400498func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
499 bp := `
500 java_library {
501 name: "target_library",
502 srcs: ["a.java"],
503 }
504
505 java_binary_host {
506 name: "host_binary",
507 srcs: ["b.java"],
508 }
509 `
Alex Humesky2070e322020-06-09 20:23:08 -0400510
Paul Duffin414ea5e2021-03-22 17:31:52 +0000511 result := android.GroupFixturePreparers(
512 PrepareForTestWithJavaDefaultModules,
513 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
514 variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
515 }),
516 ).RunTestWithBp(t, bp)
Alex Humesky2070e322020-06-09 20:23:08 -0400517
Liz Kammer7941b302020-07-28 13:27:34 -0700518 // first, check that the -g flag is added to target modules
Paul Duffin414ea5e2021-03-22 17:31:52 +0000519 targetLibrary := result.ModuleForTests("target_library", "android_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400520 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
521 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
522 t.Errorf("target library javac flags %v should contain "+
523 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
524 }
525
526 // check that -g is not overridden for host modules
527 buildOS := android.BuildOs.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000528 hostBinary := result.ModuleForTests("host_binary", buildOS+"_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400529 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
530 if strings.Contains(hostJavaFlags, "-g:source,lines") {
531 t.Errorf("java_binary_host javac flags %v should not have "+
532 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
533 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800534}
535
Colin Cross72bb3632017-07-13 16:23:21 -0700536func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700537 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700538 java_library {
539 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000540 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000541 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700542 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700543 }
544
Colin Cross74d73e22017-08-02 11:05:49 -0700545 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700546 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700547 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700548 }
549
Colin Cross74d73e22017-08-02 11:05:49 -0700550 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700551 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700552 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700553 sdk_version: "current",
554 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700555 }
Colin Cross42be7612019-02-21 18:12:14 -0800556
557 dex_import {
558 name: "qux",
559 jars: ["b.jar"],
560 }
Colin Cross79c7c262019-04-17 11:11:46 -0700561
562 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000563 name: "sdklib",
564 public: {
565 jars: ["c.jar"],
566 },
567 }
568
Paul Duffin91547182019-11-12 19:39:36 +0000569 prebuilt_stubs_sources {
570 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000571 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000572 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000573
574 java_test_import {
575 name: "test",
576 jars: ["a.jar"],
577 test_suites: ["cts"],
578 test_config: "AndroidTest.xml",
579 }
Colin Cross72bb3632017-07-13 16:23:21 -0700580 `)
581
Paul Duffin9b478b02019-12-10 13:41:51 +0000582 fooModule := ctx.ModuleForTests("foo", "android_common")
583 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700584 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700585 barModule := ctx.ModuleForTests("bar", "android_common")
586 barJar := barModule.Rule("combineJar").Output
587 bazModule := ctx.ModuleForTests("baz", "android_common")
588 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700589 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700590
Paul Duffin9b478b02019-12-10 13:41:51 +0000591 fooLibrary := fooModule.Module().(*Library)
592 assertDeepEquals(t, "foo java sources incorrect",
593 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000594
Paul Duffin9b478b02019-12-10 13:41:51 +0000595 assertDeepEquals(t, "foo java source jars incorrect",
596 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
597 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000598
Colin Cross37f6d792018-07-12 12:28:41 -0700599 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
600 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700601 }
602
Liz Kammerd6c31d22020-08-05 15:40:41 -0700603 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
604 if barDexJar != nil {
605 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
606 }
607
Colin Cross79c7c262019-04-17 11:11:46 -0700608 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
609 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
610 }
611
Colin Cross37f6d792018-07-12 12:28:41 -0700612 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
613 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700614 }
Colin Cross42be7612019-02-21 18:12:14 -0800615
Paul Duffin414ea5e2021-03-22 17:31:52 +0000616 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath()
617 expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar"
618 android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar)
Liz Kammerd6c31d22020-08-05 15:40:41 -0700619
Colin Cross42be7612019-02-21 18:12:14 -0800620 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700621}
622
Paul Duffin9b478b02019-12-10 13:41:51 +0000623func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
624 if !reflect.DeepEqual(expected, actual) {
625 t.Errorf("%s: expected %q, found %q", message, expected, actual)
626 }
627}
628
Paul Duffin1a393322020-11-18 16:36:47 +0000629func TestPrebuiltStubsSources(t *testing.T) {
630 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
631 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
632prebuilt_stubs_sources {
633 name: "stubs-source",
634 srcs: ["%s"],
635}`, sourcesPath), map[string][]byte{
636 "stubs/sources/pkg/A.java": nil,
637 "stubs/sources/pkg/B.java": nil,
638 })
639
640 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
641 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
642 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
643 }
644 }
645
646 t.Run("empty/missing directory", func(t *testing.T) {
Colin Crossce3a51d2021-03-19 16:22:12 -0700647 test(t, "empty-directory", nil)
Paul Duffin1a393322020-11-18 16:36:47 +0000648 })
649
650 t.Run("non-empty set of sources", func(t *testing.T) {
651 test(t, "stubs/sources", []string{
652 "stubs/sources/pkg/A.java",
653 "stubs/sources/pkg/B.java",
654 })
655 })
656}
657
Paul Duffin56d44902020-01-31 13:36:25 +0000658func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000659 result := prepareForJavaTest.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000660 java_library {
661 name: "foo",
662 srcs: ["a.java"],
663 libs: ["sdklib"],
664 sdk_version: "current",
665 }
666
667 java_library {
668 name: "foo.system",
669 srcs: ["a.java"],
670 libs: ["sdklib"],
671 sdk_version: "system_current",
672 }
673
674 java_library {
675 name: "foo.test",
676 srcs: ["a.java"],
677 libs: ["sdklib"],
678 sdk_version: "test_current",
679 }
680
681 java_sdk_library_import {
682 name: "sdklib",
683 public: {
684 jars: ["a.jar"],
685 },
686 system: {
687 jars: ["b.jar"],
688 },
689 test: {
690 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100691 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000692 },
693 }
694 `)
695
696 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000697 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000698 javac := fooModule.Rule("javac")
699
Paul Duffin22b77cd2021-03-12 19:15:01 +0000700 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
701 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000702 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100703
Paul Duffin22b77cd2021-03-12 19:15:01 +0000704 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100705 `prebuilt_sdklib.stubs`,
706 `prebuilt_sdklib.stubs.source.test`,
707 `prebuilt_sdklib.stubs.system`,
708 `prebuilt_sdklib.stubs.test`,
709 })
710}
711
712func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000713 result := android.GroupFixturePreparers(
714 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000715 PrepareForTestWithJavaSdkLibraryFiles,
716 FixtureWithLastReleaseApis("sdklib"),
717 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100718 java_sdk_library {
719 name: "sdklib",
720 srcs: ["a.java"],
721 sdk_version: "none",
722 system_modules: "none",
723 public: {
724 enabled: true,
725 },
726 }
727
728 java_sdk_library_import {
729 name: "sdklib",
730 public: {
731 jars: ["a.jar"],
732 },
733 }
734 `)
735
Paul Duffin22b77cd2021-03-12 19:15:01 +0000736 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100737 `dex2oatd`,
738 `prebuilt_sdklib`,
739 `sdklib.impl`,
740 `sdklib.stubs`,
741 `sdklib.stubs.source`,
742 `sdklib.xml`,
743 })
744
Paul Duffin22b77cd2021-03-12 19:15:01 +0000745 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100746 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100747 `sdklib.impl`,
748 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
749 // dependency is added after prebuilts may have been renamed and so has to use
750 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100751 `sdklib.xml`,
752 })
753}
754
755func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000756 result := android.GroupFixturePreparers(
757 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000758 PrepareForTestWithJavaSdkLibraryFiles,
759 FixtureWithLastReleaseApis("sdklib"),
760 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100761 java_sdk_library {
762 name: "sdklib",
763 srcs: ["a.java"],
764 sdk_version: "none",
765 system_modules: "none",
766 public: {
767 enabled: true,
768 },
769 }
770
771 java_sdk_library_import {
772 name: "sdklib",
773 prefer: true,
774 public: {
775 jars: ["a.jar"],
776 },
777 }
778 `)
779
Paul Duffin22b77cd2021-03-12 19:15:01 +0000780 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100781 `dex2oatd`,
782 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100783 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100784 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100785 `sdklib.stubs.source`,
786 `sdklib.xml`,
787 })
788
Paul Duffin22b77cd2021-03-12 19:15:01 +0000789 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100790 `prebuilt_sdklib.stubs`,
791 `sdklib.impl`,
792 `sdklib.xml`,
793 })
Paul Duffin56d44902020-01-31 13:36:25 +0000794}
795
JaeMan Parkff715562020-10-19 17:25:58 +0900796func TestJavaSdkLibraryEnforce(t *testing.T) {
797 partitionToBpOption := func(partition string) string {
798 switch partition {
799 case "system":
800 return ""
801 case "vendor":
802 return "soc_specific: true,"
803 case "product":
804 return "product_specific: true,"
805 default:
806 panic("Invalid partition group name: " + partition)
807 }
808 }
809
810 type testConfigInfo struct {
811 libraryType string
812 fromPartition string
813 toPartition string
814 enforceVendorInterface bool
815 enforceProductInterface bool
816 enforceJavaSdkLibraryCheck bool
817 allowList []string
818 }
819
Paul Duffin05f72de2021-03-12 21:28:51 +0000820 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900821 bpFileTemplate := `
822 java_library {
823 name: "foo",
824 srcs: ["foo.java"],
825 libs: ["bar"],
826 sdk_version: "current",
827 %s
828 }
829
830 %s {
831 name: "bar",
832 srcs: ["bar.java"],
833 sdk_version: "current",
834 %s
835 }
836 `
837
838 bpFile := fmt.Sprintf(bpFileTemplate,
839 partitionToBpOption(info.fromPartition),
840 info.libraryType,
841 partitionToBpOption(info.toPartition))
842
Paul Duffin05f72de2021-03-12 21:28:51 +0000843 return android.GroupFixturePreparers(
Paul Duffin163043d2021-03-12 19:18:49 +0000844 PrepareForTestWithJavaSdkLibraryFiles,
845 FixtureWithLastReleaseApis("bar"),
Paul Duffin05f72de2021-03-12 21:28:51 +0000846 android.FixtureWithRootAndroidBp(bpFile),
847 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
848 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
849 if info.enforceVendorInterface {
850 variables.DeviceVndkVersion = proptools.StringPtr("current")
851 }
852 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
853 variables.InterPartitionJavaLibraryAllowList = info.allowList
854 }),
855 )
JaeMan Parkff715562020-10-19 17:25:58 +0900856 }
857
Paul Duffinaa6caa72021-03-13 16:50:15 +0000858 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
Paul Duffina35178b2021-03-24 14:54:12 +0000859 t.Run(fmt.Sprintf("%v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000860 errorHandler := android.FixtureExpectsNoErrors
861 if expectedErrorPattern != "" {
862 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000863 }
Paul Duffin55e740e2021-03-29 02:06:19 +0100864 android.GroupFixturePreparers(
865 prepareForJavaTest,
866 createPreparer(info),
867 ).
868 ExtendWithErrorHandler(errorHandler).
869 RunTest(t)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000870 })
871 }
872
JaeMan Parkff715562020-10-19 17:25:58 +0900873 errorMessage := "is not allowed across the partitions"
874
Paul Duffinaa6caa72021-03-13 16:50:15 +0000875 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900876 libraryType: "java_library",
877 fromPartition: "product",
878 toPartition: "system",
879 enforceVendorInterface: true,
880 enforceProductInterface: true,
881 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000882 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900883
Paul Duffinaa6caa72021-03-13 16:50:15 +0000884 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900885 libraryType: "java_library",
886 fromPartition: "product",
887 toPartition: "system",
888 enforceVendorInterface: true,
889 enforceProductInterface: false,
890 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000891 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900892
Paul Duffinaa6caa72021-03-13 16:50:15 +0000893 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900894 libraryType: "java_library",
895 fromPartition: "product",
896 toPartition: "system",
897 enforceVendorInterface: true,
898 enforceProductInterface: true,
899 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000900 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900901
Paul Duffinaa6caa72021-03-13 16:50:15 +0000902 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900903 libraryType: "java_library",
904 fromPartition: "vendor",
905 toPartition: "system",
906 enforceVendorInterface: true,
907 enforceProductInterface: true,
908 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000909 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900910
Paul Duffinaa6caa72021-03-13 16:50:15 +0000911 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900912 libraryType: "java_library",
913 fromPartition: "vendor",
914 toPartition: "system",
915 enforceVendorInterface: true,
916 enforceProductInterface: true,
917 enforceJavaSdkLibraryCheck: true,
918 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +0000919 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900920
Paul Duffinaa6caa72021-03-13 16:50:15 +0000921 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900922 libraryType: "java_library",
923 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900924 toPartition: "product",
925 enforceVendorInterface: true,
926 enforceProductInterface: true,
927 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000928 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +0900929
Paul Duffinaa6caa72021-03-13 16:50:15 +0000930 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900931 libraryType: "java_sdk_library",
932 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +0900933 toPartition: "system",
934 enforceVendorInterface: true,
935 enforceProductInterface: true,
936 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000937 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +0900938
Paul Duffinaa6caa72021-03-13 16:50:15 +0000939 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900940 libraryType: "java_sdk_library",
941 fromPartition: "vendor",
942 toPartition: "system",
943 enforceVendorInterface: true,
944 enforceProductInterface: true,
945 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000946 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +0900947
Paul Duffinaa6caa72021-03-13 16:50:15 +0000948 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900949 libraryType: "java_sdk_library",
950 fromPartition: "vendor",
951 toPartition: "product",
952 enforceVendorInterface: true,
953 enforceProductInterface: true,
954 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000955 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900956}
957
Colin Cross89536d42017-07-07 14:35:50 -0700958func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700959 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700960 java_defaults {
961 name: "defaults",
962 srcs: ["a.java"],
963 libs: ["bar"],
964 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700965 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700966 }
967
968 java_library {
969 name: "foo",
970 defaults: ["defaults"],
971 }
972
973 java_library {
974 name: "bar",
975 srcs: ["b.java"],
976 }
977
978 java_library {
979 name: "baz",
980 srcs: ["c.java"],
981 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700982
983 android_test {
984 name: "atestOptimize",
985 defaults: ["defaults"],
986 optimize: {enabled: true},
987 }
988
989 android_test {
990 name: "atestNoOptimize",
991 defaults: ["defaults"],
992 }
993
994 android_test {
995 name: "atestDefault",
996 srcs: ["a.java"],
997 }
Colin Cross89536d42017-07-07 14:35:50 -0700998 `)
999
Paul Duffina71a67a2021-03-29 00:42:57 +01001000 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -07001001 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -07001002
1003 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
1004 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
1005 }
1006
Paul Duffin414ea5e2021-03-22 17:31:52 +00001007 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -07001008 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1009 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -07001010 }
1011
Colin Cross1ee23172017-10-18 14:44:18 -07001012 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -07001013 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
1014 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -07001015 }
Sasha Smundak2057f822019-04-16 17:16:58 -07001016
1017 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
1018 if atestOptimize.Output == nil {
1019 t.Errorf("atestOptimize should optimize APK")
1020 }
1021
1022 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
1023 if atestNoOptimize.Output == nil {
1024 t.Errorf("atestNoOptimize should not optimize APK")
1025 }
1026
1027 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1028 if atestDefault.Output == nil {
1029 t.Errorf("atestDefault should optimize APK")
1030 }
Colin Cross89536d42017-07-07 14:35:50 -07001031}
1032
Colin Cross0f37af02017-09-27 17:42:05 -07001033func TestResources(t *testing.T) {
1034 var table = []struct {
1035 name string
1036 prop string
1037 extra string
1038 args string
1039 }{
1040 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001041 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001042 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001043 prop: `java_resource_dirs: ["java-res"]`,
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 {
1047 // Test that a module with java_resources includes the files
1048 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001049 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1050 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001051 },
1052 {
1053 // Test that a module with a filegroup in java_resources includes the files with the
1054 // path prefix
1055 name: "resource filegroup",
1056 prop: `java_resources: [":foo-res"]`,
1057 extra: `
1058 filegroup {
1059 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001060 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001061 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001062 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001063 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001064 },
1065 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001066 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1067 name: "wildcard dirs",
1068 prop: `java_resource_dirs: ["java-res/*"]`,
1069 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1070 },
1071 {
1072 // Test that a module exclude_java_resource_dirs excludes the files
1073 name: "wildcard dirs",
1074 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1075 args: "-C java-res/a -f java-res/a/a",
1076 },
Colin Crosscedd4762018-09-13 11:26:19 -07001077 {
1078 // Test wildcards in java_resources
1079 name: "wildcard files",
1080 prop: `java_resources: ["java-res/**/*"]`,
1081 args: "-C . -f java-res/a/a -f java-res/b/b",
1082 },
1083 {
1084 // Test exclude_java_resources with java_resources
1085 name: "wildcard files with exclude",
1086 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1087 args: "-C . -f java-res/a/a",
1088 },
1089 {
1090 // Test exclude_java_resources with java_resource_dirs
1091 name: "resource dirs with exclude files",
1092 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1093 args: "-C java-res -f java-res/a/a",
1094 },
1095 {
1096 // Test exclude_java_resource_dirs with java_resource_dirs
1097 name: "resource dirs with exclude files",
1098 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1099 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1100 },
Colin Cross0f37af02017-09-27 17:42:05 -07001101 }
1102
1103 for _, test := range table {
1104 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001105 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001106 java_library {
1107 name: "foo",
1108 srcs: [
1109 "a.java",
1110 "b.java",
1111 "c.java",
1112 ],
1113 `+test.prop+`,
1114 }
Colin Cross238c1f32020-06-07 16:58:18 -07001115 `+test.extra,
1116 map[string][]byte{
1117 "java-res/a/a": nil,
1118 "java-res/b/b": nil,
1119 "java-res2/a": nil,
1120 },
1121 )
Colin Cross0f37af02017-09-27 17:42:05 -07001122
Colin Cross331a1212018-08-15 20:40:52 -07001123 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001124 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001125
1126 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1127 t.Errorf("foo combined jars %v does not contain %q",
1128 foo.Inputs.Strings(), fooRes.Output.String())
1129 }
1130
Colin Crossaf9c55b2017-10-03 14:50:08 -07001131 if fooRes.Args["jarArgs"] != test.args {
1132 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001133 fooRes.Args["jarArgs"], test.args)
1134 }
1135 })
1136 }
1137}
1138
Colin Cross0c4ce212019-05-03 15:28:19 -07001139func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001140 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001141 java_library {
1142 name: "foo",
1143 srcs: [
1144 "a.java",
1145 "b.java",
1146 "c.java",
1147 ],
1148 include_srcs: true,
1149 }
1150
1151 java_library {
1152 name: "bar",
1153 srcs: [
1154 "a.java",
1155 "b.java",
1156 "c.java",
1157 ],
1158 java_resource_dirs: ["java-res"],
1159 include_srcs: true,
1160 }
Colin Cross238c1f32020-06-07 16:58:18 -07001161 `, map[string][]byte{
1162 "java-res/a/a": nil,
1163 "java-res/b/b": nil,
1164 "java-res2/a": nil,
1165 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001166
1167 // Test a library with include_srcs: true
1168 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1169 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1170
1171 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1172 t.Errorf("foo combined jars %v does not contain %q", w, g)
1173 }
1174
1175 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1176 t.Errorf("foo source jar args %q is not %q", w, g)
1177 }
1178
1179 // Test a library with include_srcs: true and resources
1180 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1181 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1182 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1183 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1184
1185 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1186 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1187 }
1188
1189 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1190 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1191 }
1192
1193 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1194 t.Errorf("bar combined jars %v does not contain %q", w, g)
1195 }
1196
1197 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1198 t.Errorf("bar source jar args %q is not %q", w, g)
1199 }
1200
1201 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1202 t.Errorf("bar resource jar args %q is not %q", w, g)
1203 }
1204}
1205
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001206func TestJavaLint(t *testing.T) {
1207 ctx, _ := testJavaWithFS(t, `
1208 java_library {
1209 name: "foo",
1210 srcs: [
1211 "a.java",
1212 "b.java",
1213 "c.java",
1214 ],
1215 min_sdk_version: "29",
1216 sdk_version: "system_current",
1217 }
1218 `, map[string][]byte{
1219 "lint-baseline.xml": nil,
1220 })
1221
1222 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001223
Colin Cross1661aff2021-03-12 17:56:51 -08001224 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1225 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001226 t.Error("did not pass --baseline flag")
1227 }
1228}
1229
1230func TestJavaLintWithoutBaseline(t *testing.T) {
1231 ctx, _ := testJavaWithFS(t, `
1232 java_library {
1233 name: "foo",
1234 srcs: [
1235 "a.java",
1236 "b.java",
1237 "c.java",
1238 ],
1239 min_sdk_version: "29",
1240 sdk_version: "system_current",
1241 }
1242 `, map[string][]byte{})
1243
1244 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001245
Colin Cross1661aff2021-03-12 17:56:51 -08001246 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1247 if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001248 t.Error("passed --baseline flag for non existent file")
1249 }
1250}
1251
1252func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
Paul Duffin76e5c8a2021-03-20 14:19:46 +00001253 android.GroupFixturePreparers(
1254 PrepareForTestWithJavaDefaultModules,
1255 android.PrepareForTestDisallowNonExistentPaths,
1256 ).ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern([]string{`source path "mybaseline.xml" does not exist`})).
1257 RunTestWithBp(t, `
1258 java_library {
1259 name: "foo",
1260 srcs: [
1261 ],
1262 min_sdk_version: "29",
1263 sdk_version: "system_current",
1264 lint: {
1265 baseline_filename: "mybaseline.xml",
1266 },
1267 }
1268 `)
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001269}
1270
1271func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1272 ctx, _ := testJavaWithFS(t, `
1273 java_library {
1274 name: "foo",
1275 srcs: [
1276 "a.java",
1277 "b.java",
1278 "c.java",
1279 ],
1280 min_sdk_version: "29",
1281 sdk_version: "system_current",
1282 lint: {
1283 error_checks: ["SomeCheck"],
1284 baseline_filename: "mybaseline.xml",
1285 },
1286 }
1287 `, map[string][]byte{
1288 "mybaseline.xml": nil,
1289 })
1290
1291 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001292
Colin Cross1661aff2021-03-12 17:56:51 -08001293 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1294 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001295 t.Error("did not use the correct file for baseline")
1296 }
Pedro Loureirof4a88b12021-02-25 16:23:22 +00001297
1298 if !strings.Contains(*sboxProto.Commands[0].Command, "--error_check NewApi") {
1299 t.Error("should check NewApi errors")
1300 }
1301
1302 if !strings.Contains(*sboxProto.Commands[0].Command, "--error_check SomeCheck") {
1303 t.Error("should combine NewApi errors with SomeCheck errors")
1304 }
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001305}
1306
Colin Cross54190b32017-10-09 15:34:10 -07001307func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001308 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001309 java_library {
1310 name: "foo",
1311 srcs: [
1312 "a*.java",
1313 ":gen",
1314 "b*.java",
1315 ],
1316 }
1317
1318 genrule {
1319 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001320 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001321 out: ["gen.java"],
1322 }
Colin Cross238c1f32020-06-07 16:58:18 -07001323 `, map[string][]byte{
1324 "a.java": nil,
1325 "b.java": nil,
1326 })
Colin Cross54190b32017-10-09 15:34:10 -07001327
1328 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1329 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1330
Colin Cross15e86d92017-10-20 15:07:08 -07001331 if filepath.Base(genrule.Output.String()) != "gen.java" {
1332 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001333 }
1334
1335 if len(javac.Inputs) != 3 ||
1336 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001337 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001338 javac.Inputs[2].String() != "b.java" {
1339 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1340 }
1341}
1342
Nan Zhang61eaedb2017-11-02 13:28:15 -07001343func TestTurbine(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001344 result := android.GroupFixturePreparers(
1345 prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
Paul Duffin3d9f2682021-03-13 09:47:16 +00001346 RunTestWithBp(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001347 java_library {
1348 name: "foo",
1349 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001350 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001351 }
1352
1353 java_library {
1354 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001355 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001356 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001357 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001358 }
1359
1360 java_library {
1361 name: "baz",
1362 srcs: ["c.java"],
1363 libs: ["bar"],
1364 sdk_version: "14",
1365 }
1366 `)
1367
Paul Duffina71a67a2021-03-29 00:42:57 +01001368 fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine")
1369 barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine")
1370 barJavac := result.ModuleForTests("bar", "android_common").Rule("javac")
1371 barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine")
1372 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001373
Paul Duffin414ea5e2021-03-22 17:31:52 +00001374 android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001375
Paul Duffin414ea5e2021-03-22 17:31:52 +00001376 fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1377 barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar")
Paul Duffin3d9f2682021-03-13 09:47:16 +00001378 android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
1379 android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
Paul Duffin414ea5e2021-03-22 17:31:52 +00001380 android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs)
Paul Duffin3d9f2682021-03-13 09:47:16 +00001381 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001382}
1383
1384func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001385 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001386 java_library {
1387 name: "bar",
1388 srcs: ["a.java","b.java","c.java"],
1389 javac_shard_size: 1
1390 }
1391 `)
1392
Paul Duffin414ea5e2021-03-22 17:31:52 +00001393 barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001394 for i := 0; i < 3; i++ {
Paul Duffina71a67a2021-03-29 00:42:57 +01001395 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
Nan Zhang61eaedb2017-11-02 13:28:15 -07001396 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1397 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1398 }
1399 }
1400}
1401
Colin Cross54250902017-12-05 09:28:08 -08001402func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001403 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001404 java_library {
1405 name: "foo",
1406 srcs: ["a.java"],
1407 }
1408
1409 java_genrule {
1410 name: "jargen",
1411 tool_files: ["b.java"],
1412 cmd: "$(location b.java) $(in) $(out)",
1413 out: ["jargen.jar"],
1414 srcs: [":foo"],
1415 }
1416
1417 java_library {
1418 name: "bar",
1419 static_libs: ["jargen"],
1420 srcs: ["c.java"],
1421 }
1422
1423 java_library {
1424 name: "baz",
1425 libs: ["jargen"],
1426 srcs: ["c.java"],
1427 }
1428 `)
1429
1430 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1431 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1432 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1433 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1434 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1435
Colin Cross3d680512020-11-13 16:23:53 -08001436 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1437 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001438 }
1439
1440 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1441 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1442 }
1443
1444 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1445 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1446 }
1447
1448 if len(barCombined.Inputs) != 2 ||
1449 barCombined.Inputs[0].String() != bar.Output.String() ||
1450 barCombined.Inputs[1].String() != jargen.Output.String() {
1451 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1452 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1453 }
1454}
1455
Nan Zhang27e284d2018-02-09 21:03:53 +00001456func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001457 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001458 java_library {
1459 name: "foo",
1460 srcs: ["a.java", ":foo-srcs"],
1461 exclude_srcs: ["a.java", ":foo-excludes"],
1462 }
1463
1464 filegroup {
1465 name: "foo-srcs",
1466 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1467 }
1468
1469 filegroup {
1470 name: "foo-excludes",
1471 srcs: ["java-fg/a.java", "java-fg/b.java"],
1472 }
1473 `)
1474
1475 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1476
1477 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1478 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1479 }
1480}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001481
Paul Duffin52d398a2019-06-11 12:31:14 +01001482func TestJavaLibrary(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001483 testJavaWithFS(t, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001484 "libcore/Android.bp": []byte(`
1485 java_library {
1486 name: "core",
1487 sdk_version: "none",
1488 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001489 }
1490
1491 filegroup {
1492 name: "core-jar",
1493 srcs: [":core{.jar}"],
1494 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001495 `),
Paul Duffinaa55f742020-10-06 17:20:13 +01001496 })
Paul Duffinaa55f742020-10-06 17:20:13 +01001497}
1498
1499func TestJavaImport(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001500 testJavaWithFS(t, "", map[string][]byte{
Paul Duffinaa55f742020-10-06 17:20:13 +01001501 "libcore/Android.bp": []byte(`
1502 java_import {
1503 name: "core",
1504 sdk_version: "none",
1505 }
1506
1507 filegroup {
1508 name: "core-jar",
1509 srcs: [":core{.jar}"],
1510 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001511 `),
Paul Duffin52d398a2019-06-11 12:31:14 +01001512 })
Paul Duffin52d398a2019-06-11 12:31:14 +01001513}
1514
Jiyong Parkc678ad32018-04-10 13:07:10 +09001515func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001516 result := android.GroupFixturePreparers(
1517 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001518 PrepareForTestWithJavaSdkLibraryFiles,
1519 FixtureWithPrebuiltApis(map[string][]string{
1520 "28": {"foo"},
1521 "29": {"foo"},
1522 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1523 }),
1524 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001525 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001526 name: "droiddoc-templates-sdk",
1527 path: ".",
1528 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001529 java_sdk_library {
1530 name: "foo",
1531 srcs: ["a.java", "b.java"],
1532 api_packages: ["foo"],
1533 }
1534 java_sdk_library {
1535 name: "bar",
1536 srcs: ["a.java", "b.java"],
1537 api_packages: ["bar"],
1538 }
1539 java_library {
1540 name: "baz",
1541 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001542 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001543 sdk_version: "system_current",
1544 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001545 java_sdk_library {
1546 name: "barney",
1547 srcs: ["c.java"],
1548 api_only: true,
1549 }
1550 java_sdk_library {
1551 name: "betty",
1552 srcs: ["c.java"],
1553 shared_library: false,
1554 }
Paul Duffin859fe962020-05-15 10:20:31 +01001555 java_sdk_library_import {
1556 name: "quuz",
1557 public: {
1558 jars: ["c.jar"],
1559 },
1560 }
1561 java_sdk_library_import {
1562 name: "fred",
1563 public: {
1564 jars: ["b.jar"],
1565 },
1566 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001567 java_sdk_library_import {
1568 name: "wilma",
1569 public: {
1570 jars: ["b.jar"],
1571 },
1572 shared_library: false,
1573 }
Jiyong Park1be96912018-05-28 18:02:19 +09001574 java_library {
1575 name: "qux",
1576 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001577 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001578 sdk_version: "system_current",
1579 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001580 java_library {
1581 name: "baz-test",
1582 srcs: ["c.java"],
1583 libs: ["foo"],
1584 sdk_version: "test_current",
1585 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001586 java_library {
1587 name: "baz-29",
1588 srcs: ["c.java"],
1589 libs: ["foo"],
1590 sdk_version: "system_29",
1591 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001592 java_library {
1593 name: "baz-module-30",
1594 srcs: ["c.java"],
1595 libs: ["foo"],
1596 sdk_version: "module_30",
1597 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001598 `)
1599
1600 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001601 result.ModuleForTests("foo", "android_common")
1602 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1603 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1604 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1605 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1606 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1607 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1608 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1609 result.ModuleForTests("foo.api.public.28", "")
1610 result.ModuleForTests("foo.api.system.28", "")
1611 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001612
Paul Duffin22b77cd2021-03-12 19:15:01 +00001613 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001614 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001615 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001616 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001617 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001618 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001619 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001620
Paul Duffin22b77cd2021-03-12 19:15:01 +00001621 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001622 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001623 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001624
Paul Duffin22b77cd2021-03-12 19:15:01 +00001625 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001626 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001627 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001628
Paul Duffin22b77cd2021-03-12 19:15:01 +00001629 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001630 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001631 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 +01001632
Jiyong Park1be96912018-05-28 18:02:19 +09001633 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001634 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001635 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001636 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001637 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001638 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001639}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001640
Anton Hansson7f66efa2020-10-08 14:47:23 +01001641func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001642 result := android.GroupFixturePreparers(
1643 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001644 PrepareForTestWithJavaSdkLibraryFiles,
1645 FixtureWithLastReleaseApis("sdklib"),
1646 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001647 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001648 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001649 srcs: ["a.java"],
1650 impl_only_libs: ["foo"],
1651 stub_only_libs: ["bar"],
1652 }
1653 java_library {
1654 name: "foo",
1655 srcs: ["a.java"],
1656 sdk_version: "current",
1657 }
1658 java_library {
1659 name: "bar",
1660 srcs: ["a.java"],
1661 sdk_version: "current",
1662 }
1663 `)
1664
Anton Hanssondff2c782020-12-21 17:10:01 +00001665 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001666 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001667 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1668 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1669 }
1670 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001671 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00001672 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001673 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1674 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1675 }
1676}
1677
Paul Duffindaaa3322020-05-26 18:13:57 +01001678func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001679 result := android.GroupFixturePreparers(
1680 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001681 PrepareForTestWithJavaSdkLibraryFiles,
1682 FixtureWithLastReleaseApis("foo"),
1683 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01001684 java_sdk_library {
1685 name: "foo",
1686 srcs: ["a.java"],
1687 api_only: true,
1688 public: {
1689 enabled: true,
1690 },
1691 }
1692
1693 java_library {
1694 name: "bar",
1695 srcs: ["b.java"],
1696 libs: ["foo"],
1697 }
1698 `)
1699
1700 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001701 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffine8366da2021-03-24 10:40:38 +00001702 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 +01001703 t.Errorf("expected %q, found %#q", expected, actual)
1704 }
1705}
1706
Paul Duffin46dc45a2020-05-14 15:39:10 +01001707func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001708 android.GroupFixturePreparers(
1709 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001710 PrepareForTestWithJavaSdkLibraryFiles,
1711 FixtureWithLastReleaseApis("foo"),
1712 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001713 java_sdk_library {
1714 name: "foo",
1715 srcs: ["a.java"],
1716 api_packages: ["foo"],
1717 public: {
1718 enabled: true,
1719 },
1720 }
1721
1722 java_library {
1723 name: "bar",
1724 srcs: ["b.java", ":foo{.public.stubs.source}"],
1725 }
1726 `)
1727}
1728
1729func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001730 android.GroupFixturePreparers(
1731 prepareForJavaTest,
1732 PrepareForTestWithJavaSdkLibraryFiles,
1733 FixtureWithLastReleaseApis("foo"),
1734 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00001735 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
1736 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001737 java_sdk_library {
1738 name: "foo",
1739 srcs: ["a.java"],
1740 api_packages: ["foo"],
1741 public: {
1742 enabled: true,
1743 },
1744 }
1745
1746 java_library {
1747 name: "bar",
1748 srcs: ["b.java", ":foo{.system.stubs.source}"],
1749 }
1750 `)
1751}
1752
Paul Duffinca8d9a52020-06-26 22:20:25 +01001753func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001754 result := android.GroupFixturePreparers(
1755 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001756 PrepareForTestWithJavaSdkLibraryFiles,
1757 FixtureWithLastReleaseApis("sdklib"),
1758 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01001759 java_sdk_library {
1760 name: "sdklib",
1761 srcs: ["a.java"],
1762 sdk_version: "none",
1763 system_modules: "none",
1764 public: {
1765 enabled: true,
1766 },
1767 }
1768 `)
1769
Paul Duffin22b77cd2021-03-12 19:15:01 +00001770 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001771 `dex2oatd`,
1772 `sdklib.impl`,
1773 `sdklib.stubs`,
1774 `sdklib.stubs.source`,
1775 `sdklib.xml`,
1776 })
1777}
1778
Paul Duffin46dc45a2020-05-14 15:39:10 +01001779func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001780 prepareForJavaTest.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001781 java_sdk_library_import {
1782 name: "foo",
1783 public: {
1784 jars: ["a.jar"],
1785 stub_srcs: ["a.java"],
1786 current_api: "api/current.txt",
1787 removed_api: "api/removed.txt",
1788 },
1789 }
1790
1791 java_library {
1792 name: "bar",
1793 srcs: [":foo{.public.stubs.source}"],
1794 java_resources: [
1795 ":foo{.public.api.txt}",
1796 ":foo{.public.removed-api.txt}",
1797 ],
1798 }
1799 `)
1800}
1801
1802func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
1803 bp := `
1804 java_sdk_library_import {
1805 name: "foo",
1806 public: {
1807 jars: ["a.jar"],
1808 },
1809 }
1810 `
1811
1812 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001813 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001814 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
1815 RunTestWithBp(t, bp+`
1816 java_library {
1817 name: "bar",
1818 srcs: [":foo{.public.stubs.source}"],
1819 java_resources: [
1820 ":foo{.public.api.txt}",
1821 ":foo{.public.removed-api.txt}",
1822 ],
1823 }
1824 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001825 })
1826
1827 t.Run("api.txt", func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001828 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001829 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
1830 RunTestWithBp(t, bp+`
1831 java_library {
1832 name: "bar",
1833 srcs: ["a.java"],
1834 java_resources: [
1835 ":foo{.public.api.txt}",
1836 ],
1837 }
1838 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001839 })
1840
1841 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001842 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001843 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
1844 RunTestWithBp(t, bp+`
1845 java_library {
1846 name: "bar",
1847 srcs: ["a.java"],
1848 java_resources: [
1849 ":foo{.public.removed-api.txt}",
1850 ],
1851 }
1852 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01001853 })
1854}
1855
Paul Duffin3375e352020-04-28 10:44:03 +01001856func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001857 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001858 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
1859 RunTestWithBp(t, `
1860 java_sdk_library {
1861 name: "foo",
1862 srcs: ["a.java", "b.java"],
1863 api_packages: ["foo"],
1864 // Explicitly disable public to test the check that ensures the set of enabled
1865 // scopes is consistent.
1866 public: {
1867 enabled: false,
1868 },
1869 system: {
1870 enabled: true,
1871 },
1872 }
Paul Duffin3375e352020-04-28 10:44:03 +01001873 `)
1874}
1875
Paul Duffin87a05a32020-05-12 11:50:28 +01001876func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001877 android.GroupFixturePreparers(
1878 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001879 PrepareForTestWithJavaSdkLibraryFiles,
1880 FixtureWithLastReleaseApis("foo"),
1881 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01001882 java_sdk_library {
1883 name: "foo",
1884 srcs: ["a.java", "b.java"],
1885 api_packages: ["foo"],
1886 system: {
1887 enabled: true,
1888 sdk_version: "module_current",
1889 },
1890 }
1891 `)
1892}
1893
Paul Duffin0c5bae52020-06-02 13:00:08 +01001894func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001895 android.GroupFixturePreparers(
1896 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001897 PrepareForTestWithJavaSdkLibraryFiles,
1898 FixtureWithLastReleaseApis("foo"),
1899 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01001900 java_sdk_library {
1901 name: "foo",
1902 srcs: ["a.java", "b.java"],
1903 api_packages: ["foo"],
1904 system: {
1905 enabled: true,
1906 },
1907 module_lib: {
1908 enabled: true,
1909 },
1910 }
1911 `)
1912}
1913
1914func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001915 android.GroupFixturePreparers(
1916 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001917 PrepareForTestWithJavaSdkLibraryFiles,
1918 FixtureWithLastReleaseApis("foo"),
1919 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01001920 java_sdk_library {
1921 name: "foo",
1922 srcs: ["a.java", "b.java"],
1923 api_packages: ["foo"],
1924 system: {
1925 enabled: true,
1926 },
1927 system_server: {
1928 enabled: true,
1929 },
1930 }
1931 `)
1932}
1933
Paul Duffin803a9562020-05-20 11:52:25 +01001934func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001935 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001936 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
1937 RunTestWithBp(t, `
1938 java_sdk_library {
1939 name: "foo",
1940 srcs: ["a.java"],
1941 public: {
1942 enabled: false,
1943 },
1944 }
Paul Duffin803a9562020-05-20 11:52:25 +01001945
Paul Duffin22b77cd2021-03-12 19:15:01 +00001946 java_library {
1947 name: "baz",
1948 srcs: ["a.java"],
1949 libs: ["foo"],
1950 sdk_version: "module_current",
1951 }
Paul Duffin803a9562020-05-20 11:52:25 +01001952 `)
1953}
1954
1955func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001956 android.GroupFixturePreparers(
1957 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001958 PrepareForTestWithJavaSdkLibraryFiles,
1959 FixtureWithLastReleaseApis("foo"),
1960 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01001961 java_sdk_library {
1962 name: "foo",
1963 srcs: ["a.java"],
1964 system: {
1965 enabled: true,
1966 },
1967 }
1968
1969 java_library {
1970 name: "baz",
1971 srcs: ["a.java"],
1972 libs: ["foo"],
1973 // foo does not have module-lib scope so it should fallback to system
1974 sdk_version: "module_current",
1975 }
1976 `)
1977}
1978
Jiyong Park932cdfe2020-05-28 00:19:53 +09001979func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001980 result := android.GroupFixturePreparers(
1981 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001982 PrepareForTestWithJavaSdkLibraryFiles,
1983 FixtureWithLastReleaseApis("foo"),
1984 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09001985 java_sdk_library {
1986 name: "foo",
1987 srcs: ["a.java"],
1988 system: {
1989 enabled: true,
1990 },
1991 default_to_stubs: true,
1992 }
1993
1994 java_library {
1995 name: "baz",
1996 srcs: ["a.java"],
1997 libs: ["foo"],
1998 // does not have sdk_version set, should fallback to module,
1999 // which will then fallback to system because the module scope
2000 // is not enabled.
2001 }
2002 `)
2003 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002004 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Paul Duffine8366da2021-03-24 10:40:38 +00002005 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 +09002006 t.Errorf("expected %q, found %#q", expected, actual)
2007 }
2008}
2009
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002010var compilerFlagsTestCases = []struct {
2011 in string
2012 out bool
2013}{
2014 {
2015 in: "a",
2016 out: false,
2017 },
2018 {
2019 in: "-a",
2020 out: true,
2021 },
2022 {
2023 in: "-no-jdk",
2024 out: false,
2025 },
2026 {
2027 in: "-no-stdlib",
2028 out: false,
2029 },
2030 {
2031 in: "-kotlin-home",
2032 out: false,
2033 },
2034 {
2035 in: "-kotlin-home /some/path",
2036 out: false,
2037 },
2038 {
2039 in: "-include-runtime",
2040 out: false,
2041 },
2042 {
2043 in: "-Xintellij-plugin-root",
2044 out: false,
2045 },
2046}
2047
2048type mockContext struct {
2049 android.ModuleContext
2050 result bool
2051}
2052
2053func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2054 // CheckBadCompilerFlags calls this function when the flag should be rejected
2055 ctx.result = false
2056}
2057
2058func TestCompilerFlags(t *testing.T) {
2059 for _, testCase := range compilerFlagsTestCases {
2060 ctx := &mockContext{result: true}
2061 CheckKotlincFlags(ctx, []string{testCase.in})
2062 if ctx.result != testCase.out {
2063 t.Errorf("incorrect output:")
2064 t.Errorf(" input: %#v", testCase.in)
2065 t.Errorf(" expected: %#v", testCase.out)
2066 t.Errorf(" got: %#v", ctx.result)
2067 }
2068 }
2069}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002070
2071// TODO(jungjw): Consider making this more robust by ignoring path order.
2072func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00002073 variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop()
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002074 flags := strings.Split(variables["javacFlags"], " ")
2075 got := ""
2076 for _, flag := range flags {
2077 keyEnd := strings.Index(flag, "=")
2078 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2079 got = flag[keyEnd+1:]
2080 break
2081 }
2082 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00002083 if expected != android.StringPathRelativeToTop(ctx.Config().BuildDir(), got) {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002084 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2085 }
2086}
2087
2088func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002089 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002090 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002091 bp := `
2092 java_library {
2093 name: "foo",
2094 srcs: ["a.java"],
2095 java_version: "1.8",
2096 }
2097
2098 java_library {
2099 name: "bar",
2100 srcs: ["b.java"],
2101 sdk_version: "none",
2102 system_modules: "none",
2103 patch_module: "java.base",
2104 java_version: "1.8",
2105 }
2106
2107 java_library {
2108 name: "baz",
2109 srcs: ["c.java"],
2110 patch_module: "java.base",
2111 java_version: "1.8",
2112 }
2113 `
2114 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002115
2116 checkPatchModuleFlag(t, ctx, "foo", "")
2117 checkPatchModuleFlag(t, ctx, "bar", "")
2118 checkPatchModuleFlag(t, ctx, "baz", "")
2119 })
2120
Pete Gillin0c2143e2019-05-02 15:32:11 +01002121 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002122 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002123 bp := `
2124 java_library {
2125 name: "foo",
2126 srcs: ["a.java"],
2127 }
2128
2129 java_library {
2130 name: "bar",
2131 srcs: ["b.java"],
2132 sdk_version: "none",
2133 system_modules: "none",
2134 patch_module: "java.base",
2135 }
2136
2137 java_library {
2138 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002139 srcs: [
2140 "c.java",
2141 // Tests for b/150878007
2142 "dir/d.java",
2143 "dir2/e.java",
2144 "dir2/f.java",
2145 "nested/dir/g.java"
2146 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002147 patch_module: "java.base",
2148 }
2149 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002150 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002151
2152 checkPatchModuleFlag(t, ctx, "foo", "")
Paul Duffin414ea5e2021-03-22 17:31:52 +00002153 expected := "java.base=.:out/soong"
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002154 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002155 expected = "java.base=" + strings.Join([]string{
Paul Duffin414ea5e2021-03-22 17:31:52 +00002156 ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002157 checkPatchModuleFlag(t, ctx, "baz", expected)
2158 })
2159}
Paul Duffina7b9f422020-01-10 17:12:18 +00002160
Paul Duffin83a2d962019-11-19 19:44:10 +00002161func TestJavaLibraryWithSystemModules(t *testing.T) {
2162 ctx, _ := testJava(t, `
2163 java_library {
2164 name: "lib-with-source-system-modules",
2165 srcs: [
2166 "a.java",
2167 ],
2168 sdk_version: "none",
2169 system_modules: "source-system-modules",
2170 }
2171
2172 java_library {
2173 name: "source-jar",
2174 srcs: [
2175 "a.java",
2176 ],
2177 }
2178
2179 java_system_modules {
2180 name: "source-system-modules",
2181 libs: ["source-jar"],
2182 }
2183
2184 java_library {
2185 name: "lib-with-prebuilt-system-modules",
2186 srcs: [
2187 "a.java",
2188 ],
2189 sdk_version: "none",
2190 system_modules: "prebuilt-system-modules",
2191 }
2192
2193 java_import {
2194 name: "prebuilt-jar",
2195 jars: ["a.jar"],
2196 }
2197
2198 java_system_modules_import {
2199 name: "prebuilt-system-modules",
2200 libs: ["prebuilt-jar"],
2201 }
2202 `)
2203
2204 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2205
2206 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2207}
2208
2209func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2210 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2211 bootClasspath := javacRule.Args["bootClasspath"]
2212 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2213 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2214 }
2215}
Jiyong Park19604de2020-03-24 16:44:11 +09002216
2217func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2218 ctx, _ := testJava(t, `
2219 java_library {
2220 name: "foo",
2221 srcs: ["aidl/foo/IFoo.aidl"],
2222 libs: ["bar"],
2223 }
2224
2225 java_import {
2226 name: "bar",
2227 jars: ["a.jar"],
2228 aidl: {
2229 export_include_dirs: ["aidl/bar"],
2230 },
2231 }
2232 `)
2233
2234 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2235 expectedAidlFlag := "-Iaidl/bar"
2236 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2237 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2238 }
2239}
Liz Kammerdd849a82020-06-12 16:38:45 -07002240
Jooyung Hane197d8b2021-01-05 10:33:16 +09002241func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2242 ctx, _ := testJava(t, `
2243 java_library {
2244 name: "foo",
2245 srcs: ["aidl/foo/IFoo.aidl"],
2246 aidl: { flags: ["-Werror"], },
2247 }
2248 `)
2249
2250 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2251 expectedAidlFlag := "-Werror"
2252 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2253 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2254 }
2255}
2256
Liz Kammerdd849a82020-06-12 16:38:45 -07002257func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002258 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002259 java_test_host {
2260 name: "foo",
2261 srcs: ["a.java"],
2262 data_native_bins: ["bin"]
2263 }
2264
2265 python_binary_host {
2266 name: "bin",
2267 srcs: ["bin.py"],
2268 }
2269 `)
2270
2271 buildOS := android.BuildOs.String()
2272
2273 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002274 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002275 expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
Liz Kammerdd849a82020-06-12 16:38:45 -07002276 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002277 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual)
Liz Kammerdd849a82020-06-12 16:38:45 -07002278}
Yuexi Ma627263f2021-03-04 13:47:56 -08002279
2280func TestDefaultInstallable(t *testing.T) {
2281 ctx, _ := testJava(t, `
2282 java_test_host {
2283 name: "foo"
2284 }
2285 `)
2286
2287 buildOS := android.BuildOs.String()
2288 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2289 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2290 module.properties.Installable)
2291}