blob: 5a130448fae0a3403ececa2d389cd372602389b9 [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 Duffin414ea5e2021-03-22 17:31:52 +0000253 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop()
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 Duffin414ea5e2021-03-22 17:31:52 +0000979 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop()
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 Duffin414ea5e2021-03-22 17:31:52 +00001339 fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine").RelativeToTop()
1340 barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine").RelativeToTop()
1341 barJavac := result.ModuleForTests("bar", "android_common").Rule("javac").RelativeToTop()
1342 barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine").RelativeToTop()
1343 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac").RelativeToTop()
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 Duffin414ea5e2021-03-22 17:31:52 +00001366 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)).RelativeToTop()
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
Nan Zhang581fd212018-01-10 16:06:12 -08001373func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001374 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001375 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001376 name: "droiddoc-templates-sdk",
1377 path: ".",
1378 }
Jiyong Park29074592019-07-07 16:27:47 +09001379 filegroup {
1380 name: "bar-doc-aidl-srcs",
1381 srcs: ["bar-doc/IBar.aidl"],
1382 path: "bar-doc",
1383 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001384 droidstubs {
1385 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001386 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001387 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001388 ],
Steve Kim3666c702020-09-01 17:58:01 +00001389 exclude_srcs: [
1390 "bar-doc/b.java"
1391 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001392 api_levels_annotations_dirs: [
1393 "droiddoc-templates-sdk",
1394 ],
1395 api_levels_annotations_enabled: true,
1396 }
1397 droiddoc {
1398 name: "bar-doc",
1399 srcs: [
1400 ":bar-stubs",
1401 "bar-doc/IFoo.aidl",
1402 ":bar-doc-aidl-srcs",
1403 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001404 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001405 hdf: [
1406 "android.whichdoc offline",
1407 ],
1408 knowntags: [
1409 "bar-doc/known_oj_tags.txt",
1410 ],
1411 proofread_file: "libcore-proofread.txt",
1412 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001413 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001414 }
Colin Cross238c1f32020-06-07 16:58:18 -07001415 `,
1416 map[string][]byte{
1417 "bar-doc/a.java": nil,
1418 "bar-doc/b.java": nil,
1419 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001420 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1421 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1422 if err != nil {
1423 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1424 }
1425 if len(barStubsOutputs) != 1 {
1426 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001427 }
Nan Zhang581fd212018-01-10 16:06:12 -08001428
Liz Kammere1ab2502020-09-10 15:29:25 +00001429 barStubsOutput := barStubsOutputs[0]
1430 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
Paul Duffin414ea5e2021-03-22 17:31:52 +00001431 javaDoc := barDoc.Rule("javadoc").RelativeToTop()
Paul Duffin4dd6cdb2021-03-24 10:34:35 +00001432 if g, w := android.PathsRelativeToTop(javaDoc.Implicits), android.PathRelativeToTop(barStubsOutput); !inList(w, g) {
Liz Kammere1ab2502020-09-10 15:29:25 +00001433 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001434 }
1435
Paul Duffin414ea5e2021-03-22 17:31:52 +00001436 expected := "-sourcepath out/soong/.intermediates/bar-doc/android_common/srcjars "
Liz Kammere1ab2502020-09-10 15:29:25 +00001437 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1438 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1439 }
1440
1441 aidl := barDoc.Rule("aidl")
Paul Duffin4dd6cdb2021-03-24 10:34:35 +00001442 if g, w := android.PathsRelativeToTop(javaDoc.Implicits), android.PathRelativeToTop(aidl.Output); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001443 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1444 }
1445
1446 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1447 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001448 }
Nan Zhang581fd212018-01-10 16:06:12 -08001449}
1450
Liz Kammer585cac22020-07-06 09:12:57 -07001451func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1452 testJavaError(t, "flags is set. Cannot set args", `
1453 droiddoc_exported_dir {
1454 name: "droiddoc-templates-sdk",
1455 path: ".",
1456 }
1457 filegroup {
1458 name: "bar-doc-aidl-srcs",
1459 srcs: ["bar-doc/IBar.aidl"],
1460 path: "bar-doc",
1461 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001462 droidstubs {
1463 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001464 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001465 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001466 ],
Steve Kim3666c702020-09-01 17:58:01 +00001467 exclude_srcs: [
1468 "bar-doc/b.java"
1469 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001470 api_levels_annotations_dirs: [
1471 "droiddoc-templates-sdk",
1472 ],
1473 api_levels_annotations_enabled: true,
1474 }
1475 droiddoc {
1476 name: "bar-doc",
1477 srcs: [
1478 ":bar-stubs",
1479 "bar-doc/IFoo.aidl",
1480 ":bar-doc-aidl-srcs",
1481 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001482 custom_template: "droiddoc-templates-sdk",
1483 hdf: [
1484 "android.whichdoc offline",
1485 ],
1486 knowntags: [
1487 "bar-doc/known_oj_tags.txt",
1488 ],
1489 proofread_file: "libcore-proofread.txt",
1490 todo_file: "libcore-docs-todo.html",
1491 flags: ["-offlinemode -title \"libcore\""],
1492 args: "-offlinemode -title \"libcore\"",
1493 }
1494 `)
1495}
1496
Liz Kammer3d894b72020-08-04 09:55:13 -07001497func TestDroidstubs(t *testing.T) {
1498 ctx, _ := testJavaWithFS(t, `
1499 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001500 name: "droiddoc-templates-sdk",
1501 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001502 }
1503
1504 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001505 name: "bar-stubs",
1506 srcs: ["bar-doc/a.java"],
1507 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1508 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001509 }
1510
1511 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001512 name: "bar-stubs-other",
1513 srcs: ["bar-doc/a.java"],
1514 high_mem: true,
1515 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1516 api_levels_annotations_enabled: true,
1517 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001518 }
1519 `,
1520 map[string][]byte{
1521 "bar-doc/a.java": nil,
1522 })
1523 testcases := []struct {
1524 moduleName string
1525 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001526 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001527 }{
1528 {
1529 moduleName: "bar-stubs",
1530 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001531 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001532 },
1533 {
1534 moduleName: "bar-stubs-other",
1535 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001536 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001537 },
1538 }
1539 for _, c := range testcases {
1540 m := ctx.ModuleForTests(c.moduleName, "android_common")
1541 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001542 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001543 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001544 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001545 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1546 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001547
1548 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1549 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1550 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001551 }
1552}
1553
Paul Duffin83a2d962019-11-19 19:44:10 +00001554func TestDroidstubsWithSystemModules(t *testing.T) {
1555 ctx, _ := testJava(t, `
1556 droidstubs {
1557 name: "stubs-source-system-modules",
1558 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001559 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001560 ],
1561 sdk_version: "none",
1562 system_modules: "source-system-modules",
1563 }
1564
1565 java_library {
1566 name: "source-jar",
1567 srcs: [
1568 "a.java",
1569 ],
1570 }
1571
1572 java_system_modules {
1573 name: "source-system-modules",
1574 libs: ["source-jar"],
1575 }
1576
1577 droidstubs {
1578 name: "stubs-prebuilt-system-modules",
1579 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001580 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001581 ],
1582 sdk_version: "none",
1583 system_modules: "prebuilt-system-modules",
1584 }
1585
1586 java_import {
1587 name: "prebuilt-jar",
1588 jars: ["a.jar"],
1589 }
1590
1591 java_system_modules_import {
1592 name: "prebuilt-system-modules",
1593 libs: ["prebuilt-jar"],
1594 }
1595 `)
1596
1597 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1598
1599 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1600}
1601
1602func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1603 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1604 var systemJars []string
1605 for _, i := range metalavaRule.Implicits {
1606 systemJars = append(systemJars, i.Base())
1607 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001608 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001609 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1610 }
1611}
1612
Colin Cross54250902017-12-05 09:28:08 -08001613func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001614 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001615 java_library {
1616 name: "foo",
1617 srcs: ["a.java"],
1618 }
1619
1620 java_genrule {
1621 name: "jargen",
1622 tool_files: ["b.java"],
1623 cmd: "$(location b.java) $(in) $(out)",
1624 out: ["jargen.jar"],
1625 srcs: [":foo"],
1626 }
1627
1628 java_library {
1629 name: "bar",
1630 static_libs: ["jargen"],
1631 srcs: ["c.java"],
1632 }
1633
1634 java_library {
1635 name: "baz",
1636 libs: ["jargen"],
1637 srcs: ["c.java"],
1638 }
1639 `)
1640
1641 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1642 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1643 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1644 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1645 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1646
Colin Cross3d680512020-11-13 16:23:53 -08001647 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1648 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001649 }
1650
1651 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1652 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1653 }
1654
1655 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1656 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1657 }
1658
1659 if len(barCombined.Inputs) != 2 ||
1660 barCombined.Inputs[0].String() != bar.Output.String() ||
1661 barCombined.Inputs[1].String() != jargen.Output.String() {
1662 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1663 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1664 }
1665}
1666
Nan Zhang27e284d2018-02-09 21:03:53 +00001667func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001668 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001669 java_library {
1670 name: "foo",
1671 srcs: ["a.java", ":foo-srcs"],
1672 exclude_srcs: ["a.java", ":foo-excludes"],
1673 }
1674
1675 filegroup {
1676 name: "foo-srcs",
1677 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1678 }
1679
1680 filegroup {
1681 name: "foo-excludes",
1682 srcs: ["java-fg/a.java", "java-fg/b.java"],
1683 }
1684 `)
1685
1686 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1687
1688 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1689 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1690 }
1691}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001692
Paul Duffin52d398a2019-06-11 12:31:14 +01001693func TestJavaLibrary(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001694 testJavaWithFS(t, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001695 "libcore/Android.bp": []byte(`
1696 java_library {
1697 name: "core",
1698 sdk_version: "none",
1699 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001700 }
1701
1702 filegroup {
1703 name: "core-jar",
1704 srcs: [":core{.jar}"],
1705 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001706 `),
Paul Duffinaa55f742020-10-06 17:20:13 +01001707 })
Paul Duffinaa55f742020-10-06 17:20:13 +01001708}
1709
1710func TestJavaImport(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001711 testJavaWithFS(t, "", map[string][]byte{
Paul Duffinaa55f742020-10-06 17:20:13 +01001712 "libcore/Android.bp": []byte(`
1713 java_import {
1714 name: "core",
1715 sdk_version: "none",
1716 }
1717
1718 filegroup {
1719 name: "core-jar",
1720 srcs: [":core{.jar}"],
1721 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001722 `),
Paul Duffin52d398a2019-06-11 12:31:14 +01001723 })
Paul Duffin52d398a2019-06-11 12:31:14 +01001724}
1725
Jiyong Parkc678ad32018-04-10 13:07:10 +09001726func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001727 result := android.GroupFixturePreparers(
1728 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001729 PrepareForTestWithJavaSdkLibraryFiles,
1730 FixtureWithPrebuiltApis(map[string][]string{
1731 "28": {"foo"},
1732 "29": {"foo"},
1733 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1734 }),
1735 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001736 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001737 name: "droiddoc-templates-sdk",
1738 path: ".",
1739 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001740 java_sdk_library {
1741 name: "foo",
1742 srcs: ["a.java", "b.java"],
1743 api_packages: ["foo"],
1744 }
1745 java_sdk_library {
1746 name: "bar",
1747 srcs: ["a.java", "b.java"],
1748 api_packages: ["bar"],
1749 }
1750 java_library {
1751 name: "baz",
1752 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001753 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001754 sdk_version: "system_current",
1755 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001756 java_sdk_library {
1757 name: "barney",
1758 srcs: ["c.java"],
1759 api_only: true,
1760 }
1761 java_sdk_library {
1762 name: "betty",
1763 srcs: ["c.java"],
1764 shared_library: false,
1765 }
Paul Duffin859fe962020-05-15 10:20:31 +01001766 java_sdk_library_import {
1767 name: "quuz",
1768 public: {
1769 jars: ["c.jar"],
1770 },
1771 }
1772 java_sdk_library_import {
1773 name: "fred",
1774 public: {
1775 jars: ["b.jar"],
1776 },
1777 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001778 java_sdk_library_import {
1779 name: "wilma",
1780 public: {
1781 jars: ["b.jar"],
1782 },
1783 shared_library: false,
1784 }
Jiyong Park1be96912018-05-28 18:02:19 +09001785 java_library {
1786 name: "qux",
1787 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001788 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001789 sdk_version: "system_current",
1790 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001791 java_library {
1792 name: "baz-test",
1793 srcs: ["c.java"],
1794 libs: ["foo"],
1795 sdk_version: "test_current",
1796 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001797 java_library {
1798 name: "baz-29",
1799 srcs: ["c.java"],
1800 libs: ["foo"],
1801 sdk_version: "system_29",
1802 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001803 java_library {
1804 name: "baz-module-30",
1805 srcs: ["c.java"],
1806 libs: ["foo"],
1807 sdk_version: "module_30",
1808 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001809 `)
1810
1811 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001812 result.ModuleForTests("foo", "android_common")
1813 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1814 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1815 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1816 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1817 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1818 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1819 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1820 result.ModuleForTests("foo.api.public.28", "")
1821 result.ModuleForTests("foo.api.system.28", "")
1822 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001823
Paul Duffin22b77cd2021-03-12 19:15:01 +00001824 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001825 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001826 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001827 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001828 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001829 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001830 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001831
Paul Duffin22b77cd2021-03-12 19:15:01 +00001832 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001833 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001834 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001835
Paul Duffin22b77cd2021-03-12 19:15:01 +00001836 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001837 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001838 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001839
Paul Duffin22b77cd2021-03-12 19:15:01 +00001840 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001841 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001842 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 +01001843
Jiyong Park1be96912018-05-28 18:02:19 +09001844 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001845 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001846 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001847 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001848 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001849 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001850}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001851
Anton Hansson7f66efa2020-10-08 14:47:23 +01001852func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001853 result := android.GroupFixturePreparers(
1854 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001855 PrepareForTestWithJavaSdkLibraryFiles,
1856 FixtureWithLastReleaseApis("sdklib"),
1857 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001858 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001859 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001860 srcs: ["a.java"],
1861 impl_only_libs: ["foo"],
1862 stub_only_libs: ["bar"],
1863 }
1864 java_library {
1865 name: "foo",
1866 srcs: ["a.java"],
1867 sdk_version: "current",
1868 }
1869 java_library {
1870 name: "bar",
1871 srcs: ["a.java"],
1872 sdk_version: "current",
1873 }
1874 `)
1875
Anton Hanssondff2c782020-12-21 17:10:01 +00001876 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001877 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001878 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1879 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1880 }
1881 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001882 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00001883 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001884 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1885 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1886 }
1887}
1888
Paul Duffindaaa3322020-05-26 18:13:57 +01001889func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001890 result := android.GroupFixturePreparers(
1891 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001892 PrepareForTestWithJavaSdkLibraryFiles,
1893 FixtureWithLastReleaseApis("foo"),
1894 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01001895 java_sdk_library {
1896 name: "foo",
1897 srcs: ["a.java"],
1898 api_only: true,
1899 public: {
1900 enabled: true,
1901 },
1902 }
1903
1904 java_library {
1905 name: "bar",
1906 srcs: ["b.java"],
1907 libs: ["foo"],
1908 }
1909 `)
1910
1911 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001912 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01001913 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1914 t.Errorf("expected %q, found %#q", expected, actual)
1915 }
1916}
1917
Paul Duffin46dc45a2020-05-14 15:39:10 +01001918func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001919 android.GroupFixturePreparers(
1920 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001921 PrepareForTestWithJavaSdkLibraryFiles,
1922 FixtureWithLastReleaseApis("foo"),
1923 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001924 java_sdk_library {
1925 name: "foo",
1926 srcs: ["a.java"],
1927 api_packages: ["foo"],
1928 public: {
1929 enabled: true,
1930 },
1931 }
1932
1933 java_library {
1934 name: "bar",
1935 srcs: ["b.java", ":foo{.public.stubs.source}"],
1936 }
1937 `)
1938}
1939
1940func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001941 android.GroupFixturePreparers(
1942 prepareForJavaTest,
1943 PrepareForTestWithJavaSdkLibraryFiles,
1944 FixtureWithLastReleaseApis("foo"),
1945 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00001946 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
1947 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001948 java_sdk_library {
1949 name: "foo",
1950 srcs: ["a.java"],
1951 api_packages: ["foo"],
1952 public: {
1953 enabled: true,
1954 },
1955 }
1956
1957 java_library {
1958 name: "bar",
1959 srcs: ["b.java", ":foo{.system.stubs.source}"],
1960 }
1961 `)
1962}
1963
Paul Duffinca8d9a52020-06-26 22:20:25 +01001964func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001965 result := android.GroupFixturePreparers(
1966 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001967 PrepareForTestWithJavaSdkLibraryFiles,
1968 FixtureWithLastReleaseApis("sdklib"),
1969 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01001970 java_sdk_library {
1971 name: "sdklib",
1972 srcs: ["a.java"],
1973 sdk_version: "none",
1974 system_modules: "none",
1975 public: {
1976 enabled: true,
1977 },
1978 }
1979 `)
1980
Paul Duffin22b77cd2021-03-12 19:15:01 +00001981 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001982 `dex2oatd`,
1983 `sdklib.impl`,
1984 `sdklib.stubs`,
1985 `sdklib.stubs.source`,
1986 `sdklib.xml`,
1987 })
1988}
1989
Paul Duffin46dc45a2020-05-14 15:39:10 +01001990func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001991 prepareForJavaTest.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001992 java_sdk_library_import {
1993 name: "foo",
1994 public: {
1995 jars: ["a.jar"],
1996 stub_srcs: ["a.java"],
1997 current_api: "api/current.txt",
1998 removed_api: "api/removed.txt",
1999 },
2000 }
2001
2002 java_library {
2003 name: "bar",
2004 srcs: [":foo{.public.stubs.source}"],
2005 java_resources: [
2006 ":foo{.public.api.txt}",
2007 ":foo{.public.removed-api.txt}",
2008 ],
2009 }
2010 `)
2011}
2012
2013func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2014 bp := `
2015 java_sdk_library_import {
2016 name: "foo",
2017 public: {
2018 jars: ["a.jar"],
2019 },
2020 }
2021 `
2022
2023 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002024 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002025 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
2026 RunTestWithBp(t, bp+`
2027 java_library {
2028 name: "bar",
2029 srcs: [":foo{.public.stubs.source}"],
2030 java_resources: [
2031 ":foo{.public.api.txt}",
2032 ":foo{.public.removed-api.txt}",
2033 ],
2034 }
2035 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002036 })
2037
2038 t.Run("api.txt", func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002039 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002040 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
2041 RunTestWithBp(t, bp+`
2042 java_library {
2043 name: "bar",
2044 srcs: ["a.java"],
2045 java_resources: [
2046 ":foo{.public.api.txt}",
2047 ],
2048 }
2049 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002050 })
2051
2052 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002053 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002054 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
2055 RunTestWithBp(t, bp+`
2056 java_library {
2057 name: "bar",
2058 srcs: ["a.java"],
2059 java_resources: [
2060 ":foo{.public.removed-api.txt}",
2061 ],
2062 }
2063 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002064 })
2065}
2066
Paul Duffin3375e352020-04-28 10:44:03 +01002067func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002068 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002069 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
2070 RunTestWithBp(t, `
2071 java_sdk_library {
2072 name: "foo",
2073 srcs: ["a.java", "b.java"],
2074 api_packages: ["foo"],
2075 // Explicitly disable public to test the check that ensures the set of enabled
2076 // scopes is consistent.
2077 public: {
2078 enabled: false,
2079 },
2080 system: {
2081 enabled: true,
2082 },
2083 }
Paul Duffin3375e352020-04-28 10:44:03 +01002084 `)
2085}
2086
Paul Duffin87a05a32020-05-12 11:50:28 +01002087func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002088 android.GroupFixturePreparers(
2089 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002090 PrepareForTestWithJavaSdkLibraryFiles,
2091 FixtureWithLastReleaseApis("foo"),
2092 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002093 java_sdk_library {
2094 name: "foo",
2095 srcs: ["a.java", "b.java"],
2096 api_packages: ["foo"],
2097 system: {
2098 enabled: true,
2099 sdk_version: "module_current",
2100 },
2101 }
2102 `)
2103}
2104
Paul Duffin0c5bae52020-06-02 13:00:08 +01002105func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002106 android.GroupFixturePreparers(
2107 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002108 PrepareForTestWithJavaSdkLibraryFiles,
2109 FixtureWithLastReleaseApis("foo"),
2110 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002111 java_sdk_library {
2112 name: "foo",
2113 srcs: ["a.java", "b.java"],
2114 api_packages: ["foo"],
2115 system: {
2116 enabled: true,
2117 },
2118 module_lib: {
2119 enabled: true,
2120 },
2121 }
2122 `)
2123}
2124
2125func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002126 android.GroupFixturePreparers(
2127 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002128 PrepareForTestWithJavaSdkLibraryFiles,
2129 FixtureWithLastReleaseApis("foo"),
2130 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002131 java_sdk_library {
2132 name: "foo",
2133 srcs: ["a.java", "b.java"],
2134 api_packages: ["foo"],
2135 system: {
2136 enabled: true,
2137 },
2138 system_server: {
2139 enabled: true,
2140 },
2141 }
2142 `)
2143}
2144
Paul Duffin803a9562020-05-20 11:52:25 +01002145func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002146 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002147 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2148 RunTestWithBp(t, `
2149 java_sdk_library {
2150 name: "foo",
2151 srcs: ["a.java"],
2152 public: {
2153 enabled: false,
2154 },
2155 }
Paul Duffin803a9562020-05-20 11:52:25 +01002156
Paul Duffin22b77cd2021-03-12 19:15:01 +00002157 java_library {
2158 name: "baz",
2159 srcs: ["a.java"],
2160 libs: ["foo"],
2161 sdk_version: "module_current",
2162 }
Paul Duffin803a9562020-05-20 11:52:25 +01002163 `)
2164}
2165
2166func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002167 android.GroupFixturePreparers(
2168 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002169 PrepareForTestWithJavaSdkLibraryFiles,
2170 FixtureWithLastReleaseApis("foo"),
2171 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002172 java_sdk_library {
2173 name: "foo",
2174 srcs: ["a.java"],
2175 system: {
2176 enabled: true,
2177 },
2178 }
2179
2180 java_library {
2181 name: "baz",
2182 srcs: ["a.java"],
2183 libs: ["foo"],
2184 // foo does not have module-lib scope so it should fallback to system
2185 sdk_version: "module_current",
2186 }
2187 `)
2188}
2189
Jiyong Park932cdfe2020-05-28 00:19:53 +09002190func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002191 result := android.GroupFixturePreparers(
2192 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002193 PrepareForTestWithJavaSdkLibraryFiles,
2194 FixtureWithLastReleaseApis("foo"),
2195 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002196 java_sdk_library {
2197 name: "foo",
2198 srcs: ["a.java"],
2199 system: {
2200 enabled: true,
2201 },
2202 default_to_stubs: true,
2203 }
2204
2205 java_library {
2206 name: "baz",
2207 srcs: ["a.java"],
2208 libs: ["foo"],
2209 // does not have sdk_version set, should fallback to module,
2210 // which will then fallback to system because the module scope
2211 // is not enabled.
2212 }
2213 `)
2214 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002215 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002216 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2217 t.Errorf("expected %q, found %#q", expected, actual)
2218 }
2219}
2220
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002221var compilerFlagsTestCases = []struct {
2222 in string
2223 out bool
2224}{
2225 {
2226 in: "a",
2227 out: false,
2228 },
2229 {
2230 in: "-a",
2231 out: true,
2232 },
2233 {
2234 in: "-no-jdk",
2235 out: false,
2236 },
2237 {
2238 in: "-no-stdlib",
2239 out: false,
2240 },
2241 {
2242 in: "-kotlin-home",
2243 out: false,
2244 },
2245 {
2246 in: "-kotlin-home /some/path",
2247 out: false,
2248 },
2249 {
2250 in: "-include-runtime",
2251 out: false,
2252 },
2253 {
2254 in: "-Xintellij-plugin-root",
2255 out: false,
2256 },
2257}
2258
2259type mockContext struct {
2260 android.ModuleContext
2261 result bool
2262}
2263
2264func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2265 // CheckBadCompilerFlags calls this function when the flag should be rejected
2266 ctx.result = false
2267}
2268
2269func TestCompilerFlags(t *testing.T) {
2270 for _, testCase := range compilerFlagsTestCases {
2271 ctx := &mockContext{result: true}
2272 CheckKotlincFlags(ctx, []string{testCase.in})
2273 if ctx.result != testCase.out {
2274 t.Errorf("incorrect output:")
2275 t.Errorf(" input: %#v", testCase.in)
2276 t.Errorf(" expected: %#v", testCase.out)
2277 t.Errorf(" got: %#v", ctx.result)
2278 }
2279 }
2280}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002281
2282// TODO(jungjw): Consider making this more robust by ignoring path order.
2283func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00002284 variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop()
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002285 flags := strings.Split(variables["javacFlags"], " ")
2286 got := ""
2287 for _, flag := range flags {
2288 keyEnd := strings.Index(flag, "=")
2289 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2290 got = flag[keyEnd+1:]
2291 break
2292 }
2293 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00002294 if expected != android.StringPathRelativeToTop(ctx.Config().BuildDir(), got) {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002295 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2296 }
2297}
2298
2299func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002300 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002301 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002302 bp := `
2303 java_library {
2304 name: "foo",
2305 srcs: ["a.java"],
2306 java_version: "1.8",
2307 }
2308
2309 java_library {
2310 name: "bar",
2311 srcs: ["b.java"],
2312 sdk_version: "none",
2313 system_modules: "none",
2314 patch_module: "java.base",
2315 java_version: "1.8",
2316 }
2317
2318 java_library {
2319 name: "baz",
2320 srcs: ["c.java"],
2321 patch_module: "java.base",
2322 java_version: "1.8",
2323 }
2324 `
2325 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002326
2327 checkPatchModuleFlag(t, ctx, "foo", "")
2328 checkPatchModuleFlag(t, ctx, "bar", "")
2329 checkPatchModuleFlag(t, ctx, "baz", "")
2330 })
2331
Pete Gillin0c2143e2019-05-02 15:32:11 +01002332 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002333 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002334 bp := `
2335 java_library {
2336 name: "foo",
2337 srcs: ["a.java"],
2338 }
2339
2340 java_library {
2341 name: "bar",
2342 srcs: ["b.java"],
2343 sdk_version: "none",
2344 system_modules: "none",
2345 patch_module: "java.base",
2346 }
2347
2348 java_library {
2349 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002350 srcs: [
2351 "c.java",
2352 // Tests for b/150878007
2353 "dir/d.java",
2354 "dir2/e.java",
2355 "dir2/f.java",
2356 "nested/dir/g.java"
2357 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002358 patch_module: "java.base",
2359 }
2360 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002361 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002362
2363 checkPatchModuleFlag(t, ctx, "foo", "")
Paul Duffin414ea5e2021-03-22 17:31:52 +00002364 expected := "java.base=.:out/soong"
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002365 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002366 expected = "java.base=" + strings.Join([]string{
Paul Duffin414ea5e2021-03-22 17:31:52 +00002367 ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002368 checkPatchModuleFlag(t, ctx, "baz", expected)
2369 })
2370}
Paul Duffina7b9f422020-01-10 17:12:18 +00002371
Paul Duffin83a2d962019-11-19 19:44:10 +00002372func TestJavaLibraryWithSystemModules(t *testing.T) {
2373 ctx, _ := testJava(t, `
2374 java_library {
2375 name: "lib-with-source-system-modules",
2376 srcs: [
2377 "a.java",
2378 ],
2379 sdk_version: "none",
2380 system_modules: "source-system-modules",
2381 }
2382
2383 java_library {
2384 name: "source-jar",
2385 srcs: [
2386 "a.java",
2387 ],
2388 }
2389
2390 java_system_modules {
2391 name: "source-system-modules",
2392 libs: ["source-jar"],
2393 }
2394
2395 java_library {
2396 name: "lib-with-prebuilt-system-modules",
2397 srcs: [
2398 "a.java",
2399 ],
2400 sdk_version: "none",
2401 system_modules: "prebuilt-system-modules",
2402 }
2403
2404 java_import {
2405 name: "prebuilt-jar",
2406 jars: ["a.jar"],
2407 }
2408
2409 java_system_modules_import {
2410 name: "prebuilt-system-modules",
2411 libs: ["prebuilt-jar"],
2412 }
2413 `)
2414
2415 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2416
2417 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2418}
2419
2420func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2421 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2422 bootClasspath := javacRule.Args["bootClasspath"]
2423 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2424 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2425 }
2426}
Jiyong Park19604de2020-03-24 16:44:11 +09002427
2428func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2429 ctx, _ := testJava(t, `
2430 java_library {
2431 name: "foo",
2432 srcs: ["aidl/foo/IFoo.aidl"],
2433 libs: ["bar"],
2434 }
2435
2436 java_import {
2437 name: "bar",
2438 jars: ["a.jar"],
2439 aidl: {
2440 export_include_dirs: ["aidl/bar"],
2441 },
2442 }
2443 `)
2444
2445 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2446 expectedAidlFlag := "-Iaidl/bar"
2447 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2448 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2449 }
2450}
Liz Kammerdd849a82020-06-12 16:38:45 -07002451
Jooyung Hane197d8b2021-01-05 10:33:16 +09002452func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2453 ctx, _ := testJava(t, `
2454 java_library {
2455 name: "foo",
2456 srcs: ["aidl/foo/IFoo.aidl"],
2457 aidl: { flags: ["-Werror"], },
2458 }
2459 `)
2460
2461 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2462 expectedAidlFlag := "-Werror"
2463 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2464 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2465 }
2466}
2467
Liz Kammerdd849a82020-06-12 16:38:45 -07002468func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002469 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002470 java_test_host {
2471 name: "foo",
2472 srcs: ["a.java"],
2473 data_native_bins: ["bin"]
2474 }
2475
2476 python_binary_host {
2477 name: "bin",
2478 srcs: ["bin.py"],
2479 }
2480 `)
2481
2482 buildOS := android.BuildOs.String()
2483
2484 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002485 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002486 expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
Liz Kammerdd849a82020-06-12 16:38:45 -07002487 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002488 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual)
Liz Kammerdd849a82020-06-12 16:38:45 -07002489}
Yuexi Ma627263f2021-03-04 13:47:56 -08002490
2491func TestDefaultInstallable(t *testing.T) {
2492 ctx, _ := testJava(t, `
2493 java_test_host {
2494 name: "foo"
2495 }
2496 `)
2497
2498 buildOS := android.BuildOs.String()
2499 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2500 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2501 module.properties.Installable)
2502}