blob: a907c8263daeac66137ab9e527a95afa2189c2d7 [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 factory to use to create fixtures for tests in this package.
37//
38// deprecated: See prepareForJavaTest
39var javaFixtureFactory = android.NewFixtureFactory(
Paul Duffind6339af2021-03-22 18:05:04 +000040 nil,
Paul Duffin70d3bee2021-03-21 11:26:05 +000041 prepareForJavaTest,
42)
Paul Duffinafeee222021-03-17 00:36:35 +000043
Paul Duffin70d3bee2021-03-21 11:26:05 +000044// Legacy preparer used for running tests within the java package.
45//
46// This includes everything that was needed to run any test in the java package prior to the
47// introduction of the test fixtures. Tests that are being converted to use fixtures directly
48// rather than through the testJava...() methods should avoid using this and instead use the
49// various preparers directly, using android.GroupFixturePreparers(...) to group them when
50// necessary.
51//
52// deprecated
53var prepareForJavaTest = android.GroupFixturePreparers(
Paul Duffin95bdab42021-03-08 21:48:46 +000054 genrule.PrepareForTestWithGenRuleBuildComponents,
55 // Get the CC build components but not default modules.
56 cc.PrepareForTestWithCcBuildComponents,
57 // Include all the default java modules.
58 PrepareForTestWithJavaDefaultModules,
Paul Duffin42da69d2021-03-22 13:41:36 +000059 PrepareForTestWithOverlayBuildComponents,
Paul Duffin220ddd72021-03-17 23:54:30 +000060 python.PrepareForTestWithPythonBuildComponents,
Paul Duffin95bdab42021-03-08 21:48:46 +000061 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
Paul Duffin95bdab42021-03-08 21:48:46 +000062 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
63 }),
Paul Duffin95bdab42021-03-08 21:48:46 +000064 dexpreopt.PrepareForTestWithDexpreopt,
65)
66
Colin Cross72bb3632017-07-13 16:23:21 -070067func TestMain(m *testing.M) {
Paul Duffind6339af2021-03-22 18:05:04 +000068 os.Exit(m.Run())
Colin Cross527012a2017-11-30 22:56:16 -080069}
70
Paul Duffin95bdab42021-03-08 21:48:46 +000071// testJavaError is a legacy way of running tests of java modules that expect errors.
72//
73// See testJava for an explanation as to how to stop using this deprecated method.
74//
75// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +090076func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +090077 t.Helper()
Paul Duffin6bac49c2021-03-12 21:28:15 +000078 result := javaFixtureFactory.
79 Extend(dexpreopt.PrepareForTestWithDexpreopt).
80 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
81 RunTestWithBp(t, bp)
82 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +090083}
84
Paul Duffin95bdab42021-03-08 21:48:46 +000085// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
86//
87// See testJava for an explanation as to how to stop using this deprecated method.
88//
89// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080090func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090091 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +000092 // This must be done on the supplied config and not as part of the fixture because any changes to
93 // the fixture's config will be ignored when RunTestWithConfig replaces it.
Colin Cross98be1bb2019-12-13 20:41:13 -080094 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000095 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Paul Duffin95bdab42021-03-08 21:48:46 +000096 result := javaFixtureFactory.
97 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
98 RunTestWithConfig(t, config)
99 return result.TestContext, result.Config
Paul Duffinec0fe172021-02-25 15:34:13 +0000100}
101
Paul Duffin95bdab42021-03-08 21:48:46 +0000102// testJavaWithFS runs tests using the javaFixtureFactory
103//
104// See testJava for an explanation as to how to stop using this deprecated method.
105//
106// deprecated
107func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -0700108 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000109 result := javaFixtureFactory.Extend(fs.AddToFixture()).RunTestWithBp(t, bp)
110 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -0700111}
112
Paul Duffin95bdab42021-03-08 21:48:46 +0000113// testJava runs tests using the javaFixtureFactory
114//
115// Do not add any new usages of this, instead use the javaFixtureFactory directly as it makes it
116// much easier to customize the test behavior.
117//
118// If it is necessary to customize the behavior of an existing test that uses this then please first
119// convert the test to using javaFixtureFactory first and then in a following change add the
120// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
121// that it did not change the test behavior unexpectedly.
122//
123// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700124func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800125 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +0000126 result := javaFixtureFactory.RunTestWithBp(t, bp)
127 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900128}
129
Paul Duffin95bdab42021-03-08 21:48:46 +0000130// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
131// is defined in PrepareForIntegrationTestWithJava
132func defaultModuleToPath(name string) string {
Paul Duffin76101fa2021-03-13 09:55:25 +0000133 switch {
134 case name == `""`:
135 return name
136 case strings.HasSuffix(name, ".jar"):
137 return name
138 default:
Paul Duffin414ea5e2021-03-22 17:31:52 +0000139 return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
Paul Duffin76101fa2021-03-13 09:55:25 +0000140 }
Paul Duffin95bdab42021-03-08 21:48:46 +0000141}
142
Jeongik Chae403e9e2019-12-07 00:16:24 +0900143func TestJavaLinkType(t *testing.T) {
144 testJava(t, `
145 java_library {
146 name: "foo",
147 srcs: ["a.java"],
148 libs: ["bar"],
149 static_libs: ["baz"],
150 }
151
152 java_library {
153 name: "bar",
154 sdk_version: "current",
155 srcs: ["b.java"],
156 }
157
158 java_library {
159 name: "baz",
160 sdk_version: "system_current",
161 srcs: ["c.java"],
162 }
163 `)
164
Steven Moreland00298982020-11-17 21:44:36 +0000165 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900166 java_library {
167 name: "foo",
168 srcs: ["a.java"],
169 libs: ["bar"],
170 sdk_version: "current",
171 static_libs: ["baz"],
172 }
173
174 java_library {
175 name: "bar",
176 sdk_version: "current",
177 srcs: ["b.java"],
178 }
179
180 java_library {
181 name: "baz",
182 sdk_version: "system_current",
183 srcs: ["c.java"],
184 }
185 `)
186
187 testJava(t, `
188 java_library {
189 name: "foo",
190 srcs: ["a.java"],
191 libs: ["bar"],
192 sdk_version: "system_current",
193 static_libs: ["baz"],
194 }
195
196 java_library {
197 name: "bar",
198 sdk_version: "current",
199 srcs: ["b.java"],
200 }
201
202 java_library {
203 name: "baz",
204 sdk_version: "system_current",
205 srcs: ["c.java"],
206 }
207 `)
208
Steven Moreland00298982020-11-17 21:44:36 +0000209 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900210 java_library {
211 name: "foo",
212 srcs: ["a.java"],
213 libs: ["bar"],
214 sdk_version: "system_current",
215 static_libs: ["baz"],
216 }
217
218 java_library {
219 name: "bar",
220 sdk_version: "current",
221 srcs: ["b.java"],
222 }
223
224 java_library {
225 name: "baz",
226 srcs: ["c.java"],
227 }
228 `)
229}
230
Colin Cross72bb3632017-07-13 16:23:21 -0700231func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700232 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700233 java_library {
234 name: "foo",
235 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700236 libs: ["bar"],
237 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700238 }
239
240 java_library {
241 name: "bar",
242 srcs: ["b.java"],
243 }
244
245 java_library {
246 name: "baz",
247 srcs: ["c.java"],
248 }
Colin Crossd5934c82017-10-02 13:55:26 -0700249 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700250
Paul Duffin414ea5e2021-03-22 17:31:52 +0000251 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop()
Nan Zhanged19fc32017-10-19 13:06:22 -0700252 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700253
254 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
255 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
256 }
257
Colin Cross1ee23172017-10-18 14:44:18 -0700258 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000259 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
260 bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700261
Paul Duffin22b77cd2021-03-12 19:15:01 +0000262 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700263
Paul Duffin22b77cd2021-03-12 19:15:01 +0000264 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700265
266 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
267 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700268 }
269}
270
Artur Satayev9cf46692019-11-26 18:08:34 +0000271func TestExportedPlugins(t *testing.T) {
272 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800273 library string
274 processors string
275 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000276 }
277 var tests = []struct {
278 name string
279 extra string
280 results []Result
281 }{
282 {
283 name: "Exported plugin is not a direct plugin",
284 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
285 results: []Result{{library: "exports", processors: "-proc:none"}},
286 },
287 {
288 name: "Exports plugin to dependee",
289 extra: `
290 java_library{name: "exports", exported_plugins: ["plugin"]}
291 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
292 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
293 `,
294 results: []Result{
295 {library: "foo", processors: "-processor com.android.TestPlugin"},
296 {library: "bar", processors: "-processor com.android.TestPlugin"},
297 },
298 },
299 {
300 name: "Exports plugin to android_library",
301 extra: `
302 java_library{name: "exports", exported_plugins: ["plugin"]}
303 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
304 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
305 `,
306 results: []Result{
307 {library: "foo", processors: "-processor com.android.TestPlugin"},
308 {library: "bar", processors: "-processor com.android.TestPlugin"},
309 },
310 },
311 {
312 name: "Exports plugin is not propagated via transitive deps",
313 extra: `
314 java_library{name: "exports", exported_plugins: ["plugin"]}
315 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
316 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
317 `,
318 results: []Result{
319 {library: "foo", processors: "-processor com.android.TestPlugin"},
320 {library: "bar", processors: "-proc:none"},
321 },
322 },
323 {
324 name: "Exports plugin appends to plugins",
325 extra: `
326 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
327 java_library{name: "exports", exported_plugins: ["plugin"]}
328 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
329 `,
330 results: []Result{
331 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
332 },
333 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800334 {
335 name: "Exports plugin to with generates_api to dependee",
336 extra: `
337 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
338 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
339 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
340 `,
341 results: []Result{
342 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
343 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
344 },
345 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000346 }
347
348 for _, test := range tests {
349 t.Run(test.name, func(t *testing.T) {
350 ctx, _ := testJava(t, `
351 java_plugin {
352 name: "plugin",
353 processor_class: "com.android.TestPlugin",
354 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800355 java_plugin {
356 name: "plugin_generates_api",
357 generates_api: true,
358 processor_class: "com.android.TestPlugin",
359 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000360 `+test.extra)
361
362 for _, want := range test.results {
363 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
364 if javac.Args["processor"] != want.processors {
365 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
366 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800367 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
368 disableTurbine := turbine.BuildParams.Rule == nil
369 if disableTurbine != want.disableTurbine {
370 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
371 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000372 }
373 })
374 }
375}
376
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900377func TestSdkVersionByPartition(t *testing.T) {
378 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900379 java_library {
380 name: "foo",
381 srcs: ["a.java"],
382 vendor: true,
383 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900384 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900385
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900386 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900387 java_library {
388 name: "bar",
389 srcs: ["b.java"],
390 }
391 `)
392
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900393 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900394 bp := `
395 java_library {
396 name: "foo",
397 srcs: ["a.java"],
398 product_specific: true,
399 }
400 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800401
Paul Duffin414ea5e2021-03-22 17:31:52 +0000402 errorHandler := android.FixtureExpectsNoErrors
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900403 if enforce {
Paul Duffin414ea5e2021-03-22 17:31:52 +0000404 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 +0900405 }
Paul Duffin414ea5e2021-03-22 17:31:52 +0000406
407 android.GroupFixturePreparers(
408 PrepareForTestWithJavaDefaultModules,
409 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
410 variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
411 }),
412 ).
413 ExtendWithErrorHandler(errorHandler).
414 RunTestWithBp(t, bp)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900415 }
416}
417
Colin Crossd5934c82017-10-02 13:55:26 -0700418func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700419 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700420 java_library {
421 name: "foo",
422 srcs: ["a.java"],
423 target: {
424 android: {
425 srcs: ["b.java"],
426 },
427 },
428 }
429 `)
430
431 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
432 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
433 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
434 }
435}
436
Colin Cross6b4a32d2017-12-05 13:42:45 -0800437func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700438 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800439 java_library_host {
440 name: "foo",
441 srcs: ["a.java"],
442 }
443
444 java_binary_host {
445 name: "bar",
446 srcs: ["b.java"],
447 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700448 jni_libs: ["libjni"],
449 }
450
451 cc_library_shared {
452 name: "libjni",
453 host_supported: true,
454 device_supported: false,
455 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800456 }
457 `)
458
459 buildOS := android.BuildOs.String()
460
461 bar := ctx.ModuleForTests("bar", buildOS+"_common")
462 barJar := bar.Output("bar.jar").Output.String()
463 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
464 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
465
Colin Cross89226d92020-10-09 19:00:54 -0700466 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
467 libjniSO := libjni.Rule("Cp").Output.String()
468
Colin Cross6b4a32d2017-12-05 13:42:45 -0800469 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700470 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
471 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800472 }
Colin Cross89226d92020-10-09 19:00:54 -0700473
474 // Test that the install binary wrapper depends on the installed JNI libraries
475 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
476 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800477 }
Alex Humesky2070e322020-06-09 20:23:08 -0400478}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800479
Alex Humesky2070e322020-06-09 20:23:08 -0400480func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
481 bp := `
482 java_library {
483 name: "target_library",
484 srcs: ["a.java"],
485 }
486
487 java_binary_host {
488 name: "host_binary",
489 srcs: ["b.java"],
490 }
491 `
Alex Humesky2070e322020-06-09 20:23:08 -0400492
Paul Duffin414ea5e2021-03-22 17:31:52 +0000493 result := android.GroupFixturePreparers(
494 PrepareForTestWithJavaDefaultModules,
495 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
496 variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
497 }),
498 ).RunTestWithBp(t, bp)
Alex Humesky2070e322020-06-09 20:23:08 -0400499
Liz Kammer7941b302020-07-28 13:27:34 -0700500 // first, check that the -g flag is added to target modules
Paul Duffin414ea5e2021-03-22 17:31:52 +0000501 targetLibrary := result.ModuleForTests("target_library", "android_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400502 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
503 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
504 t.Errorf("target library javac flags %v should contain "+
505 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
506 }
507
508 // check that -g is not overridden for host modules
509 buildOS := android.BuildOs.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000510 hostBinary := result.ModuleForTests("host_binary", buildOS+"_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400511 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
512 if strings.Contains(hostJavaFlags, "-g:source,lines") {
513 t.Errorf("java_binary_host javac flags %v should not have "+
514 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
515 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800516}
517
Colin Cross72bb3632017-07-13 16:23:21 -0700518func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700519 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700520 java_library {
521 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000522 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000523 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700524 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700525 }
526
Colin Cross74d73e22017-08-02 11:05:49 -0700527 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700528 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700529 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700530 }
531
Colin Cross74d73e22017-08-02 11:05:49 -0700532 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700533 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700534 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700535 sdk_version: "current",
536 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700537 }
Colin Cross42be7612019-02-21 18:12:14 -0800538
539 dex_import {
540 name: "qux",
541 jars: ["b.jar"],
542 }
Colin Cross79c7c262019-04-17 11:11:46 -0700543
544 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000545 name: "sdklib",
546 public: {
547 jars: ["c.jar"],
548 },
549 }
550
Paul Duffin91547182019-11-12 19:39:36 +0000551 prebuilt_stubs_sources {
552 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000553 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000554 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000555
556 java_test_import {
557 name: "test",
558 jars: ["a.jar"],
559 test_suites: ["cts"],
560 test_config: "AndroidTest.xml",
561 }
Colin Cross72bb3632017-07-13 16:23:21 -0700562 `)
563
Paul Duffin9b478b02019-12-10 13:41:51 +0000564 fooModule := ctx.ModuleForTests("foo", "android_common")
565 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700566 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700567 barModule := ctx.ModuleForTests("bar", "android_common")
568 barJar := barModule.Rule("combineJar").Output
569 bazModule := ctx.ModuleForTests("baz", "android_common")
570 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700571 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700572
Paul Duffin9b478b02019-12-10 13:41:51 +0000573 fooLibrary := fooModule.Module().(*Library)
574 assertDeepEquals(t, "foo java sources incorrect",
575 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000576
Paul Duffin9b478b02019-12-10 13:41:51 +0000577 assertDeepEquals(t, "foo java source jars incorrect",
578 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
579 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000580
Colin Cross37f6d792018-07-12 12:28:41 -0700581 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
582 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700583 }
584
Liz Kammerd6c31d22020-08-05 15:40:41 -0700585 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
586 if barDexJar != nil {
587 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
588 }
589
Colin Cross79c7c262019-04-17 11:11:46 -0700590 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
591 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
592 }
593
Colin Cross37f6d792018-07-12 12:28:41 -0700594 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
595 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700596 }
Colin Cross42be7612019-02-21 18:12:14 -0800597
Paul Duffin414ea5e2021-03-22 17:31:52 +0000598 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath()
599 expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar"
600 android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar)
Liz Kammerd6c31d22020-08-05 15:40:41 -0700601
Colin Cross42be7612019-02-21 18:12:14 -0800602 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700603}
604
Paul Duffin9b478b02019-12-10 13:41:51 +0000605func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
606 if !reflect.DeepEqual(expected, actual) {
607 t.Errorf("%s: expected %q, found %q", message, expected, actual)
608 }
609}
610
Paul Duffin1a393322020-11-18 16:36:47 +0000611func TestPrebuiltStubsSources(t *testing.T) {
612 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
613 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
614prebuilt_stubs_sources {
615 name: "stubs-source",
616 srcs: ["%s"],
617}`, sourcesPath), map[string][]byte{
618 "stubs/sources/pkg/A.java": nil,
619 "stubs/sources/pkg/B.java": nil,
620 })
621
622 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
623 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
624 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
625 }
626 }
627
628 t.Run("empty/missing directory", func(t *testing.T) {
629 test(t, "empty-directory", []string{})
630 })
631
632 t.Run("non-empty set of sources", func(t *testing.T) {
633 test(t, "stubs/sources", []string{
634 "stubs/sources/pkg/A.java",
635 "stubs/sources/pkg/B.java",
636 })
637 })
638}
639
Paul Duffin56d44902020-01-31 13:36:25 +0000640func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000641 result := javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000642 java_library {
643 name: "foo",
644 srcs: ["a.java"],
645 libs: ["sdklib"],
646 sdk_version: "current",
647 }
648
649 java_library {
650 name: "foo.system",
651 srcs: ["a.java"],
652 libs: ["sdklib"],
653 sdk_version: "system_current",
654 }
655
656 java_library {
657 name: "foo.test",
658 srcs: ["a.java"],
659 libs: ["sdklib"],
660 sdk_version: "test_current",
661 }
662
663 java_sdk_library_import {
664 name: "sdklib",
665 public: {
666 jars: ["a.jar"],
667 },
668 system: {
669 jars: ["b.jar"],
670 },
671 test: {
672 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100673 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000674 },
675 }
676 `)
677
678 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000679 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000680 javac := fooModule.Rule("javac")
681
Paul Duffin22b77cd2021-03-12 19:15:01 +0000682 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
683 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000684 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100685
Paul Duffin22b77cd2021-03-12 19:15:01 +0000686 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100687 `prebuilt_sdklib.stubs`,
688 `prebuilt_sdklib.stubs.source.test`,
689 `prebuilt_sdklib.stubs.system`,
690 `prebuilt_sdklib.stubs.test`,
691 })
692}
693
694func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000695 result := javaFixtureFactory.Extend(
696 PrepareForTestWithJavaSdkLibraryFiles,
697 FixtureWithLastReleaseApis("sdklib"),
698 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100699 java_sdk_library {
700 name: "sdklib",
701 srcs: ["a.java"],
702 sdk_version: "none",
703 system_modules: "none",
704 public: {
705 enabled: true,
706 },
707 }
708
709 java_sdk_library_import {
710 name: "sdklib",
711 public: {
712 jars: ["a.jar"],
713 },
714 }
715 `)
716
Paul Duffin22b77cd2021-03-12 19:15:01 +0000717 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100718 `dex2oatd`,
719 `prebuilt_sdklib`,
720 `sdklib.impl`,
721 `sdklib.stubs`,
722 `sdklib.stubs.source`,
723 `sdklib.xml`,
724 })
725
Paul Duffin22b77cd2021-03-12 19:15:01 +0000726 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100727 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100728 `sdklib.impl`,
729 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
730 // dependency is added after prebuilts may have been renamed and so has to use
731 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100732 `sdklib.xml`,
733 })
734}
735
736func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +0000737 result := javaFixtureFactory.Extend(
738 PrepareForTestWithJavaSdkLibraryFiles,
739 FixtureWithLastReleaseApis("sdklib"),
740 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100741 java_sdk_library {
742 name: "sdklib",
743 srcs: ["a.java"],
744 sdk_version: "none",
745 system_modules: "none",
746 public: {
747 enabled: true,
748 },
749 }
750
751 java_sdk_library_import {
752 name: "sdklib",
753 prefer: true,
754 public: {
755 jars: ["a.jar"],
756 },
757 }
758 `)
759
Paul Duffin22b77cd2021-03-12 19:15:01 +0000760 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100761 `dex2oatd`,
762 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100763 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100764 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100765 `sdklib.stubs.source`,
766 `sdklib.xml`,
767 })
768
Paul Duffin22b77cd2021-03-12 19:15:01 +0000769 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100770 `prebuilt_sdklib.stubs`,
771 `sdklib.impl`,
772 `sdklib.xml`,
773 })
Paul Duffin56d44902020-01-31 13:36:25 +0000774}
775
JaeMan Parkff715562020-10-19 17:25:58 +0900776func TestJavaSdkLibraryEnforce(t *testing.T) {
777 partitionToBpOption := func(partition string) string {
778 switch partition {
779 case "system":
780 return ""
781 case "vendor":
782 return "soc_specific: true,"
783 case "product":
784 return "product_specific: true,"
785 default:
786 panic("Invalid partition group name: " + partition)
787 }
788 }
789
790 type testConfigInfo struct {
791 libraryType string
792 fromPartition string
793 toPartition string
794 enforceVendorInterface bool
795 enforceProductInterface bool
796 enforceJavaSdkLibraryCheck bool
797 allowList []string
798 }
799
Paul Duffin05f72de2021-03-12 21:28:51 +0000800 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900801 bpFileTemplate := `
802 java_library {
803 name: "foo",
804 srcs: ["foo.java"],
805 libs: ["bar"],
806 sdk_version: "current",
807 %s
808 }
809
810 %s {
811 name: "bar",
812 srcs: ["bar.java"],
813 sdk_version: "current",
814 %s
815 }
816 `
817
818 bpFile := fmt.Sprintf(bpFileTemplate,
819 partitionToBpOption(info.fromPartition),
820 info.libraryType,
821 partitionToBpOption(info.toPartition))
822
Paul Duffin05f72de2021-03-12 21:28:51 +0000823 return android.GroupFixturePreparers(
Paul Duffin163043d2021-03-12 19:18:49 +0000824 PrepareForTestWithJavaSdkLibraryFiles,
825 FixtureWithLastReleaseApis("bar"),
Paul Duffin05f72de2021-03-12 21:28:51 +0000826 android.FixtureWithRootAndroidBp(bpFile),
827 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
828 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
829 if info.enforceVendorInterface {
830 variables.DeviceVndkVersion = proptools.StringPtr("current")
831 }
832 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
833 variables.InterPartitionJavaLibraryAllowList = info.allowList
834 }),
835 )
JaeMan Parkff715562020-10-19 17:25:58 +0900836 }
837
Paul Duffinaa6caa72021-03-13 16:50:15 +0000838 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
839 t.Run(fmt.Sprintf("%#v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000840 errorHandler := android.FixtureExpectsNoErrors
841 if expectedErrorPattern != "" {
842 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000843 }
Paul Duffin05f72de2021-03-12 21:28:51 +0000844 javaFixtureFactory.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
Paul Duffinaa6caa72021-03-13 16:50:15 +0000845 })
846 }
847
JaeMan Parkff715562020-10-19 17:25:58 +0900848 errorMessage := "is not allowed across the partitions"
849
Paul Duffinaa6caa72021-03-13 16:50:15 +0000850 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900851 libraryType: "java_library",
852 fromPartition: "product",
853 toPartition: "system",
854 enforceVendorInterface: true,
855 enforceProductInterface: true,
856 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000857 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900858
Paul Duffinaa6caa72021-03-13 16:50:15 +0000859 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900860 libraryType: "java_library",
861 fromPartition: "product",
862 toPartition: "system",
863 enforceVendorInterface: true,
864 enforceProductInterface: false,
865 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000866 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900867
Paul Duffinaa6caa72021-03-13 16:50:15 +0000868 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900869 libraryType: "java_library",
870 fromPartition: "product",
871 toPartition: "system",
872 enforceVendorInterface: true,
873 enforceProductInterface: true,
874 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000875 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900876
Paul Duffinaa6caa72021-03-13 16:50:15 +0000877 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900878 libraryType: "java_library",
879 fromPartition: "vendor",
880 toPartition: "system",
881 enforceVendorInterface: true,
882 enforceProductInterface: true,
883 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000884 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900885
Paul Duffinaa6caa72021-03-13 16:50:15 +0000886 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900887 libraryType: "java_library",
888 fromPartition: "vendor",
889 toPartition: "system",
890 enforceVendorInterface: true,
891 enforceProductInterface: true,
892 enforceJavaSdkLibraryCheck: true,
893 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +0000894 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900895
Paul Duffinaa6caa72021-03-13 16:50:15 +0000896 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900897 libraryType: "java_library",
898 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900899 toPartition: "product",
900 enforceVendorInterface: true,
901 enforceProductInterface: true,
902 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000903 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +0900904
Paul Duffinaa6caa72021-03-13 16:50:15 +0000905 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900906 libraryType: "java_sdk_library",
907 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +0900908 toPartition: "system",
909 enforceVendorInterface: true,
910 enforceProductInterface: true,
911 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000912 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +0900913
Paul Duffinaa6caa72021-03-13 16:50:15 +0000914 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900915 libraryType: "java_sdk_library",
916 fromPartition: "vendor",
917 toPartition: "system",
918 enforceVendorInterface: true,
919 enforceProductInterface: true,
920 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000921 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +0900922
Paul Duffinaa6caa72021-03-13 16:50:15 +0000923 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900924 libraryType: "java_sdk_library",
925 fromPartition: "vendor",
926 toPartition: "product",
927 enforceVendorInterface: true,
928 enforceProductInterface: true,
929 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000930 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900931}
932
Colin Cross89536d42017-07-07 14:35:50 -0700933func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700934 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700935 java_defaults {
936 name: "defaults",
937 srcs: ["a.java"],
938 libs: ["bar"],
939 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700940 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700941 }
942
943 java_library {
944 name: "foo",
945 defaults: ["defaults"],
946 }
947
948 java_library {
949 name: "bar",
950 srcs: ["b.java"],
951 }
952
953 java_library {
954 name: "baz",
955 srcs: ["c.java"],
956 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700957
958 android_test {
959 name: "atestOptimize",
960 defaults: ["defaults"],
961 optimize: {enabled: true},
962 }
963
964 android_test {
965 name: "atestNoOptimize",
966 defaults: ["defaults"],
967 }
968
969 android_test {
970 name: "atestDefault",
971 srcs: ["a.java"],
972 }
Colin Cross89536d42017-07-07 14:35:50 -0700973 `)
974
Paul Duffin414ea5e2021-03-22 17:31:52 +0000975 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop()
Nan Zhanged19fc32017-10-19 13:06:22 -0700976 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700977
978 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
979 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
980 }
981
Paul Duffin414ea5e2021-03-22 17:31:52 +0000982 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700983 if !strings.Contains(javac.Args["classpath"], barTurbine) {
984 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700985 }
986
Colin Cross1ee23172017-10-18 14:44:18 -0700987 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700988 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
989 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700990 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700991
992 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
993 if atestOptimize.Output == nil {
994 t.Errorf("atestOptimize should optimize APK")
995 }
996
997 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
998 if atestNoOptimize.Output == nil {
999 t.Errorf("atestNoOptimize should not optimize APK")
1000 }
1001
1002 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
1003 if atestDefault.Output == nil {
1004 t.Errorf("atestDefault should optimize APK")
1005 }
Colin Cross89536d42017-07-07 14:35:50 -07001006}
1007
Colin Cross0f37af02017-09-27 17:42:05 -07001008func TestResources(t *testing.T) {
1009 var table = []struct {
1010 name string
1011 prop string
1012 extra string
1013 args string
1014 }{
1015 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001016 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001017 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001018 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001019 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001020 },
1021 {
1022 // Test that a module with java_resources includes the files
1023 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001024 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1025 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001026 },
1027 {
1028 // Test that a module with a filegroup in java_resources includes the files with the
1029 // path prefix
1030 name: "resource filegroup",
1031 prop: `java_resources: [":foo-res"]`,
1032 extra: `
1033 filegroup {
1034 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001035 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001036 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001037 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001038 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001039 },
1040 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001041 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1042 name: "wildcard dirs",
1043 prop: `java_resource_dirs: ["java-res/*"]`,
1044 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1045 },
1046 {
1047 // Test that a module exclude_java_resource_dirs excludes the files
1048 name: "wildcard dirs",
1049 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1050 args: "-C java-res/a -f java-res/a/a",
1051 },
Colin Crosscedd4762018-09-13 11:26:19 -07001052 {
1053 // Test wildcards in java_resources
1054 name: "wildcard files",
1055 prop: `java_resources: ["java-res/**/*"]`,
1056 args: "-C . -f java-res/a/a -f java-res/b/b",
1057 },
1058 {
1059 // Test exclude_java_resources with java_resources
1060 name: "wildcard files with exclude",
1061 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1062 args: "-C . -f java-res/a/a",
1063 },
1064 {
1065 // Test exclude_java_resources with java_resource_dirs
1066 name: "resource dirs with exclude files",
1067 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1068 args: "-C java-res -f java-res/a/a",
1069 },
1070 {
1071 // Test exclude_java_resource_dirs with java_resource_dirs
1072 name: "resource dirs with exclude files",
1073 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1074 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1075 },
Colin Cross0f37af02017-09-27 17:42:05 -07001076 }
1077
1078 for _, test := range table {
1079 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001080 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001081 java_library {
1082 name: "foo",
1083 srcs: [
1084 "a.java",
1085 "b.java",
1086 "c.java",
1087 ],
1088 `+test.prop+`,
1089 }
Colin Cross238c1f32020-06-07 16:58:18 -07001090 `+test.extra,
1091 map[string][]byte{
1092 "java-res/a/a": nil,
1093 "java-res/b/b": nil,
1094 "java-res2/a": nil,
1095 },
1096 )
Colin Cross0f37af02017-09-27 17:42:05 -07001097
Colin Cross331a1212018-08-15 20:40:52 -07001098 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001099 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001100
1101 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1102 t.Errorf("foo combined jars %v does not contain %q",
1103 foo.Inputs.Strings(), fooRes.Output.String())
1104 }
1105
Colin Crossaf9c55b2017-10-03 14:50:08 -07001106 if fooRes.Args["jarArgs"] != test.args {
1107 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001108 fooRes.Args["jarArgs"], test.args)
1109 }
1110 })
1111 }
1112}
1113
Colin Cross0c4ce212019-05-03 15:28:19 -07001114func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001115 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001116 java_library {
1117 name: "foo",
1118 srcs: [
1119 "a.java",
1120 "b.java",
1121 "c.java",
1122 ],
1123 include_srcs: true,
1124 }
1125
1126 java_library {
1127 name: "bar",
1128 srcs: [
1129 "a.java",
1130 "b.java",
1131 "c.java",
1132 ],
1133 java_resource_dirs: ["java-res"],
1134 include_srcs: true,
1135 }
Colin Cross238c1f32020-06-07 16:58:18 -07001136 `, map[string][]byte{
1137 "java-res/a/a": nil,
1138 "java-res/b/b": nil,
1139 "java-res2/a": nil,
1140 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001141
1142 // Test a library with include_srcs: true
1143 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1144 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1145
1146 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1147 t.Errorf("foo combined jars %v does not contain %q", w, g)
1148 }
1149
1150 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1151 t.Errorf("foo source jar args %q is not %q", w, g)
1152 }
1153
1154 // Test a library with include_srcs: true and resources
1155 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1156 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1157 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1158 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1159
1160 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1161 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1162 }
1163
1164 if g, w := barRes.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 := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1169 t.Errorf("bar combined jars %v does not contain %q", w, g)
1170 }
1171
1172 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1173 t.Errorf("bar source jar args %q is not %q", w, g)
1174 }
1175
1176 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1177 t.Errorf("bar resource jar args %q is not %q", w, g)
1178 }
1179}
1180
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001181func TestJavaLint(t *testing.T) {
1182 ctx, _ := testJavaWithFS(t, `
1183 java_library {
1184 name: "foo",
1185 srcs: [
1186 "a.java",
1187 "b.java",
1188 "c.java",
1189 ],
1190 min_sdk_version: "29",
1191 sdk_version: "system_current",
1192 }
1193 `, map[string][]byte{
1194 "lint-baseline.xml": nil,
1195 })
1196
1197 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001198
Colin Cross1661aff2021-03-12 17:56:51 -08001199 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1200 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001201 t.Error("did not pass --baseline flag")
1202 }
1203}
1204
1205func TestJavaLintWithoutBaseline(t *testing.T) {
1206 ctx, _ := testJavaWithFS(t, `
1207 java_library {
1208 name: "foo",
1209 srcs: [
1210 "a.java",
1211 "b.java",
1212 "c.java",
1213 ],
1214 min_sdk_version: "29",
1215 sdk_version: "system_current",
1216 }
1217 `, map[string][]byte{})
1218
1219 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001220
Colin Cross1661aff2021-03-12 17:56:51 -08001221 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1222 if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001223 t.Error("passed --baseline flag for non existent file")
1224 }
1225}
1226
1227func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001228 config := TestConfig(t.TempDir(),
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001229 nil,
1230 `
1231 java_library {
1232 name: "foo",
1233 srcs: [
1234 ],
1235 min_sdk_version: "29",
1236 sdk_version: "system_current",
1237 lint: {
1238 baseline_filename: "mybaseline.xml",
1239 },
1240 }
1241 `, map[string][]byte{
1242 "build/soong/java/lint_defaults.txt": nil,
1243 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1244 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1245 "framework/aidl": nil,
1246 "a.java": nil,
1247 "AndroidManifest.xml": nil,
1248 "build/make/target/product/security": nil,
1249 })
1250 config.TestAllowNonExistentPaths = false
1251 testJavaErrorWithConfig(t,
1252 "source path \"mybaseline.xml\" does not exist",
1253 config,
1254 )
1255}
1256
1257func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1258 ctx, _ := testJavaWithFS(t, `
1259 java_library {
1260 name: "foo",
1261 srcs: [
1262 "a.java",
1263 "b.java",
1264 "c.java",
1265 ],
1266 min_sdk_version: "29",
1267 sdk_version: "system_current",
1268 lint: {
1269 error_checks: ["SomeCheck"],
1270 baseline_filename: "mybaseline.xml",
1271 },
1272 }
1273 `, map[string][]byte{
1274 "mybaseline.xml": nil,
1275 })
1276
1277 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001278
Colin Cross1661aff2021-03-12 17:56:51 -08001279 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1280 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001281 t.Error("did not use the correct file for baseline")
1282 }
1283}
1284
Colin Cross54190b32017-10-09 15:34:10 -07001285func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001286 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001287 java_library {
1288 name: "foo",
1289 srcs: [
1290 "a*.java",
1291 ":gen",
1292 "b*.java",
1293 ],
1294 }
1295
1296 genrule {
1297 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001298 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001299 out: ["gen.java"],
1300 }
Colin Cross238c1f32020-06-07 16:58:18 -07001301 `, map[string][]byte{
1302 "a.java": nil,
1303 "b.java": nil,
1304 })
Colin Cross54190b32017-10-09 15:34:10 -07001305
1306 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1307 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1308
Colin Cross15e86d92017-10-20 15:07:08 -07001309 if filepath.Base(genrule.Output.String()) != "gen.java" {
1310 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001311 }
1312
1313 if len(javac.Inputs) != 3 ||
1314 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001315 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001316 javac.Inputs[2].String() != "b.java" {
1317 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1318 }
1319}
1320
Nan Zhang61eaedb2017-11-02 13:28:15 -07001321func TestTurbine(t *testing.T) {
Paul Duffin3d9f2682021-03-13 09:47:16 +00001322 result := javaFixtureFactory.
1323 Extend(FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
1324 RunTestWithBp(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001325 java_library {
1326 name: "foo",
1327 srcs: ["a.java"],
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: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001333 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001334 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001335 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001336 }
1337
1338 java_library {
1339 name: "baz",
1340 srcs: ["c.java"],
1341 libs: ["bar"],
1342 sdk_version: "14",
1343 }
1344 `)
1345
Paul Duffin414ea5e2021-03-22 17:31:52 +00001346 fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine").RelativeToTop()
1347 barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine").RelativeToTop()
1348 barJavac := result.ModuleForTests("bar", "android_common").Rule("javac").RelativeToTop()
1349 barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine").RelativeToTop()
1350 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac").RelativeToTop()
Nan Zhang61eaedb2017-11-02 13:28:15 -07001351
Paul Duffin414ea5e2021-03-22 17:31:52 +00001352 android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001353
Paul Duffin414ea5e2021-03-22 17:31:52 +00001354 fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1355 barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar")
Paul Duffin3d9f2682021-03-13 09:47:16 +00001356 android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
1357 android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
Paul Duffin414ea5e2021-03-22 17:31:52 +00001358 android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs)
Paul Duffin3d9f2682021-03-13 09:47:16 +00001359 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001360}
1361
1362func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001363 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001364 java_library {
1365 name: "bar",
1366 srcs: ["a.java","b.java","c.java"],
1367 javac_shard_size: 1
1368 }
1369 `)
1370
Paul Duffin414ea5e2021-03-22 17:31:52 +00001371 barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001372 for i := 0; i < 3; i++ {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001373 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)).RelativeToTop()
Nan Zhang61eaedb2017-11-02 13:28:15 -07001374 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1375 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1376 }
1377 }
1378}
1379
Nan Zhang581fd212018-01-10 16:06:12 -08001380func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001381 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001382 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001383 name: "droiddoc-templates-sdk",
1384 path: ".",
1385 }
Jiyong Park29074592019-07-07 16:27:47 +09001386 filegroup {
1387 name: "bar-doc-aidl-srcs",
1388 srcs: ["bar-doc/IBar.aidl"],
1389 path: "bar-doc",
1390 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001391 droidstubs {
1392 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001393 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001394 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001395 ],
Steve Kim3666c702020-09-01 17:58:01 +00001396 exclude_srcs: [
1397 "bar-doc/b.java"
1398 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001399 api_levels_annotations_dirs: [
1400 "droiddoc-templates-sdk",
1401 ],
1402 api_levels_annotations_enabled: true,
1403 }
1404 droiddoc {
1405 name: "bar-doc",
1406 srcs: [
1407 ":bar-stubs",
1408 "bar-doc/IFoo.aidl",
1409 ":bar-doc-aidl-srcs",
1410 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001411 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001412 hdf: [
1413 "android.whichdoc offline",
1414 ],
1415 knowntags: [
1416 "bar-doc/known_oj_tags.txt",
1417 ],
1418 proofread_file: "libcore-proofread.txt",
1419 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001420 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001421 }
Colin Cross238c1f32020-06-07 16:58:18 -07001422 `,
1423 map[string][]byte{
1424 "bar-doc/a.java": nil,
1425 "bar-doc/b.java": nil,
1426 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001427 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1428 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1429 if err != nil {
1430 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1431 }
1432 if len(barStubsOutputs) != 1 {
1433 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001434 }
Nan Zhang581fd212018-01-10 16:06:12 -08001435
Liz Kammere1ab2502020-09-10 15:29:25 +00001436 barStubsOutput := barStubsOutputs[0]
1437 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
Paul Duffin414ea5e2021-03-22 17:31:52 +00001438 javaDoc := barDoc.Rule("javadoc").RelativeToTop()
Liz Kammere1ab2502020-09-10 15:29:25 +00001439 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1440 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001441 }
1442
Paul Duffin414ea5e2021-03-22 17:31:52 +00001443 expected := "-sourcepath out/soong/.intermediates/bar-doc/android_common/srcjars "
Liz Kammere1ab2502020-09-10 15:29:25 +00001444 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1445 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1446 }
1447
1448 aidl := barDoc.Rule("aidl")
1449 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001450 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1451 }
1452
1453 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1454 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001455 }
Nan Zhang581fd212018-01-10 16:06:12 -08001456}
1457
Liz Kammer585cac22020-07-06 09:12:57 -07001458func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1459 testJavaError(t, "flags is set. Cannot set args", `
1460 droiddoc_exported_dir {
1461 name: "droiddoc-templates-sdk",
1462 path: ".",
1463 }
1464 filegroup {
1465 name: "bar-doc-aidl-srcs",
1466 srcs: ["bar-doc/IBar.aidl"],
1467 path: "bar-doc",
1468 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001469 droidstubs {
1470 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001471 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001472 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001473 ],
Steve Kim3666c702020-09-01 17:58:01 +00001474 exclude_srcs: [
1475 "bar-doc/b.java"
1476 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001477 api_levels_annotations_dirs: [
1478 "droiddoc-templates-sdk",
1479 ],
1480 api_levels_annotations_enabled: true,
1481 }
1482 droiddoc {
1483 name: "bar-doc",
1484 srcs: [
1485 ":bar-stubs",
1486 "bar-doc/IFoo.aidl",
1487 ":bar-doc-aidl-srcs",
1488 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001489 custom_template: "droiddoc-templates-sdk",
1490 hdf: [
1491 "android.whichdoc offline",
1492 ],
1493 knowntags: [
1494 "bar-doc/known_oj_tags.txt",
1495 ],
1496 proofread_file: "libcore-proofread.txt",
1497 todo_file: "libcore-docs-todo.html",
1498 flags: ["-offlinemode -title \"libcore\""],
1499 args: "-offlinemode -title \"libcore\"",
1500 }
1501 `)
1502}
1503
Liz Kammer3d894b72020-08-04 09:55:13 -07001504func TestDroidstubs(t *testing.T) {
1505 ctx, _ := testJavaWithFS(t, `
1506 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001507 name: "droiddoc-templates-sdk",
1508 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001509 }
1510
1511 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001512 name: "bar-stubs",
1513 srcs: ["bar-doc/a.java"],
1514 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1515 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001516 }
1517
1518 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001519 name: "bar-stubs-other",
1520 srcs: ["bar-doc/a.java"],
1521 high_mem: true,
1522 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1523 api_levels_annotations_enabled: true,
1524 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001525 }
1526 `,
1527 map[string][]byte{
1528 "bar-doc/a.java": nil,
1529 })
1530 testcases := []struct {
1531 moduleName string
1532 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001533 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001534 }{
1535 {
1536 moduleName: "bar-stubs",
1537 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001538 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001539 },
1540 {
1541 moduleName: "bar-stubs-other",
1542 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001543 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001544 },
1545 }
1546 for _, c := range testcases {
1547 m := ctx.ModuleForTests(c.moduleName, "android_common")
1548 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001549 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001550 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001551 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001552 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1553 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001554
1555 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1556 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1557 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001558 }
1559}
1560
Paul Duffin83a2d962019-11-19 19:44:10 +00001561func TestDroidstubsWithSystemModules(t *testing.T) {
1562 ctx, _ := testJava(t, `
1563 droidstubs {
1564 name: "stubs-source-system-modules",
1565 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001566 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001567 ],
1568 sdk_version: "none",
1569 system_modules: "source-system-modules",
1570 }
1571
1572 java_library {
1573 name: "source-jar",
1574 srcs: [
1575 "a.java",
1576 ],
1577 }
1578
1579 java_system_modules {
1580 name: "source-system-modules",
1581 libs: ["source-jar"],
1582 }
1583
1584 droidstubs {
1585 name: "stubs-prebuilt-system-modules",
1586 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001587 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001588 ],
1589 sdk_version: "none",
1590 system_modules: "prebuilt-system-modules",
1591 }
1592
1593 java_import {
1594 name: "prebuilt-jar",
1595 jars: ["a.jar"],
1596 }
1597
1598 java_system_modules_import {
1599 name: "prebuilt-system-modules",
1600 libs: ["prebuilt-jar"],
1601 }
1602 `)
1603
1604 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1605
1606 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1607}
1608
1609func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1610 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1611 var systemJars []string
1612 for _, i := range metalavaRule.Implicits {
1613 systemJars = append(systemJars, i.Base())
1614 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001615 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001616 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1617 }
1618}
1619
Colin Cross54250902017-12-05 09:28:08 -08001620func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001621 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001622 java_library {
1623 name: "foo",
1624 srcs: ["a.java"],
1625 }
1626
1627 java_genrule {
1628 name: "jargen",
1629 tool_files: ["b.java"],
1630 cmd: "$(location b.java) $(in) $(out)",
1631 out: ["jargen.jar"],
1632 srcs: [":foo"],
1633 }
1634
1635 java_library {
1636 name: "bar",
1637 static_libs: ["jargen"],
1638 srcs: ["c.java"],
1639 }
1640
1641 java_library {
1642 name: "baz",
1643 libs: ["jargen"],
1644 srcs: ["c.java"],
1645 }
1646 `)
1647
1648 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1649 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1650 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1651 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1652 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1653
Colin Cross3d680512020-11-13 16:23:53 -08001654 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1655 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001656 }
1657
1658 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1659 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1660 }
1661
1662 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1663 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1664 }
1665
1666 if len(barCombined.Inputs) != 2 ||
1667 barCombined.Inputs[0].String() != bar.Output.String() ||
1668 barCombined.Inputs[1].String() != jargen.Output.String() {
1669 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1670 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1671 }
1672}
1673
Nan Zhang27e284d2018-02-09 21:03:53 +00001674func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001675 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001676 java_library {
1677 name: "foo",
1678 srcs: ["a.java", ":foo-srcs"],
1679 exclude_srcs: ["a.java", ":foo-excludes"],
1680 }
1681
1682 filegroup {
1683 name: "foo-srcs",
1684 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1685 }
1686
1687 filegroup {
1688 name: "foo-excludes",
1689 srcs: ["java-fg/a.java", "java-fg/b.java"],
1690 }
1691 `)
1692
1693 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1694
1695 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1696 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1697 }
1698}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001699
Paul Duffin52d398a2019-06-11 12:31:14 +01001700func TestJavaLibrary(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001701 testJavaWithFS(t, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001702 "libcore/Android.bp": []byte(`
1703 java_library {
1704 name: "core",
1705 sdk_version: "none",
1706 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001707 }
1708
1709 filegroup {
1710 name: "core-jar",
1711 srcs: [":core{.jar}"],
1712 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001713 `),
Paul Duffinaa55f742020-10-06 17:20:13 +01001714 })
Paul Duffinaa55f742020-10-06 17:20:13 +01001715}
1716
1717func TestJavaImport(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001718 testJavaWithFS(t, "", map[string][]byte{
Paul Duffinaa55f742020-10-06 17:20:13 +01001719 "libcore/Android.bp": []byte(`
1720 java_import {
1721 name: "core",
1722 sdk_version: "none",
1723 }
1724
1725 filegroup {
1726 name: "core-jar",
1727 srcs: [":core{.jar}"],
1728 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001729 `),
Paul Duffin52d398a2019-06-11 12:31:14 +01001730 })
Paul Duffin52d398a2019-06-11 12:31:14 +01001731}
1732
Jiyong Parkc678ad32018-04-10 13:07:10 +09001733func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001734 result := javaFixtureFactory.Extend(
1735 PrepareForTestWithJavaSdkLibraryFiles,
1736 FixtureWithPrebuiltApis(map[string][]string{
1737 "28": {"foo"},
1738 "29": {"foo"},
1739 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1740 }),
1741 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001742 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001743 name: "droiddoc-templates-sdk",
1744 path: ".",
1745 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001746 java_sdk_library {
1747 name: "foo",
1748 srcs: ["a.java", "b.java"],
1749 api_packages: ["foo"],
1750 }
1751 java_sdk_library {
1752 name: "bar",
1753 srcs: ["a.java", "b.java"],
1754 api_packages: ["bar"],
1755 }
1756 java_library {
1757 name: "baz",
1758 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001759 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001760 sdk_version: "system_current",
1761 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001762 java_sdk_library {
1763 name: "barney",
1764 srcs: ["c.java"],
1765 api_only: true,
1766 }
1767 java_sdk_library {
1768 name: "betty",
1769 srcs: ["c.java"],
1770 shared_library: false,
1771 }
Paul Duffin859fe962020-05-15 10:20:31 +01001772 java_sdk_library_import {
1773 name: "quuz",
1774 public: {
1775 jars: ["c.jar"],
1776 },
1777 }
1778 java_sdk_library_import {
1779 name: "fred",
1780 public: {
1781 jars: ["b.jar"],
1782 },
1783 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001784 java_sdk_library_import {
1785 name: "wilma",
1786 public: {
1787 jars: ["b.jar"],
1788 },
1789 shared_library: false,
1790 }
Jiyong Park1be96912018-05-28 18:02:19 +09001791 java_library {
1792 name: "qux",
1793 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001794 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001795 sdk_version: "system_current",
1796 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001797 java_library {
1798 name: "baz-test",
1799 srcs: ["c.java"],
1800 libs: ["foo"],
1801 sdk_version: "test_current",
1802 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001803 java_library {
1804 name: "baz-29",
1805 srcs: ["c.java"],
1806 libs: ["foo"],
1807 sdk_version: "system_29",
1808 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001809 java_library {
1810 name: "baz-module-30",
1811 srcs: ["c.java"],
1812 libs: ["foo"],
1813 sdk_version: "module_30",
1814 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001815 `)
1816
1817 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001818 result.ModuleForTests("foo", "android_common")
1819 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1820 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1821 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1822 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1823 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1824 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1825 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1826 result.ModuleForTests("foo.api.public.28", "")
1827 result.ModuleForTests("foo.api.system.28", "")
1828 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001829
Paul Duffin22b77cd2021-03-12 19:15:01 +00001830 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001831 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001832 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001833 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001834 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001835 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001836 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001837
Paul Duffin22b77cd2021-03-12 19:15:01 +00001838 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001839 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001840 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001841
Paul Duffin22b77cd2021-03-12 19:15:01 +00001842 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001843 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001844 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001845
Paul Duffin22b77cd2021-03-12 19:15:01 +00001846 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001847 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001848 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 +01001849
Jiyong Park1be96912018-05-28 18:02:19 +09001850 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001851 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001852 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001853 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001854 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001855 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001856}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001857
Anton Hansson7f66efa2020-10-08 14:47:23 +01001858func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001859 result := javaFixtureFactory.Extend(
1860 PrepareForTestWithJavaSdkLibraryFiles,
1861 FixtureWithLastReleaseApis("sdklib"),
1862 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001863 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001864 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001865 srcs: ["a.java"],
1866 impl_only_libs: ["foo"],
1867 stub_only_libs: ["bar"],
1868 }
1869 java_library {
1870 name: "foo",
1871 srcs: ["a.java"],
1872 sdk_version: "current",
1873 }
1874 java_library {
1875 name: "bar",
1876 srcs: ["a.java"],
1877 sdk_version: "current",
1878 }
1879 `)
1880
Anton Hanssondff2c782020-12-21 17:10:01 +00001881 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001882 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001883 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1884 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1885 }
1886 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001887 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00001888 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001889 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1890 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1891 }
1892}
1893
Paul Duffindaaa3322020-05-26 18:13:57 +01001894func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001895 result := javaFixtureFactory.Extend(
1896 PrepareForTestWithJavaSdkLibraryFiles,
1897 FixtureWithLastReleaseApis("foo"),
1898 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01001899 java_sdk_library {
1900 name: "foo",
1901 srcs: ["a.java"],
1902 api_only: true,
1903 public: {
1904 enabled: true,
1905 },
1906 }
1907
1908 java_library {
1909 name: "bar",
1910 srcs: ["b.java"],
1911 libs: ["foo"],
1912 }
1913 `)
1914
1915 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001916 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01001917 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1918 t.Errorf("expected %q, found %#q", expected, actual)
1919 }
1920}
1921
Paul Duffin46dc45a2020-05-14 15:39:10 +01001922func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001923 javaFixtureFactory.Extend(
1924 PrepareForTestWithJavaSdkLibraryFiles,
1925 FixtureWithLastReleaseApis("foo"),
1926 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001927 java_sdk_library {
1928 name: "foo",
1929 srcs: ["a.java"],
1930 api_packages: ["foo"],
1931 public: {
1932 enabled: true,
1933 },
1934 }
1935
1936 java_library {
1937 name: "bar",
1938 srcs: ["b.java", ":foo{.public.stubs.source}"],
1939 }
1940 `)
1941}
1942
1943func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001944 javaFixtureFactory.
Paul Duffin163043d2021-03-12 19:18:49 +00001945 Extend(
1946 PrepareForTestWithJavaSdkLibraryFiles,
1947 FixtureWithLastReleaseApis("foo"),
1948 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00001949 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
1950 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001951 java_sdk_library {
1952 name: "foo",
1953 srcs: ["a.java"],
1954 api_packages: ["foo"],
1955 public: {
1956 enabled: true,
1957 },
1958 }
1959
1960 java_library {
1961 name: "bar",
1962 srcs: ["b.java", ":foo{.system.stubs.source}"],
1963 }
1964 `)
1965}
1966
Paul Duffinca8d9a52020-06-26 22:20:25 +01001967func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00001968 result := javaFixtureFactory.Extend(
1969 PrepareForTestWithJavaSdkLibraryFiles,
1970 FixtureWithLastReleaseApis("sdklib"),
1971 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +01001972 java_sdk_library {
1973 name: "sdklib",
1974 srcs: ["a.java"],
1975 sdk_version: "none",
1976 system_modules: "none",
1977 public: {
1978 enabled: true,
1979 },
1980 }
1981 `)
1982
Paul Duffin22b77cd2021-03-12 19:15:01 +00001983 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +01001984 `dex2oatd`,
1985 `sdklib.impl`,
1986 `sdklib.stubs`,
1987 `sdklib.stubs.source`,
1988 `sdklib.xml`,
1989 })
1990}
1991
Paul Duffin46dc45a2020-05-14 15:39:10 +01001992func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001993 javaFixtureFactory.RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001994 java_sdk_library_import {
1995 name: "foo",
1996 public: {
1997 jars: ["a.jar"],
1998 stub_srcs: ["a.java"],
1999 current_api: "api/current.txt",
2000 removed_api: "api/removed.txt",
2001 },
2002 }
2003
2004 java_library {
2005 name: "bar",
2006 srcs: [":foo{.public.stubs.source}"],
2007 java_resources: [
2008 ":foo{.public.api.txt}",
2009 ":foo{.public.removed-api.txt}",
2010 ],
2011 }
2012 `)
2013}
2014
2015func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) {
2016 bp := `
2017 java_sdk_library_import {
2018 name: "foo",
2019 public: {
2020 jars: ["a.jar"],
2021 },
2022 }
2023 `
2024
2025 t.Run("stubs.source", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002026 javaFixtureFactory.
2027 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`stubs.source not available for api scope public`)).
2028 RunTestWithBp(t, bp+`
2029 java_library {
2030 name: "bar",
2031 srcs: [":foo{.public.stubs.source}"],
2032 java_resources: [
2033 ":foo{.public.api.txt}",
2034 ":foo{.public.removed-api.txt}",
2035 ],
2036 }
2037 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002038 })
2039
2040 t.Run("api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002041 javaFixtureFactory.
2042 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`api.txt not available for api scope public`)).
2043 RunTestWithBp(t, bp+`
2044 java_library {
2045 name: "bar",
2046 srcs: ["a.java"],
2047 java_resources: [
2048 ":foo{.public.api.txt}",
2049 ],
2050 }
2051 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002052 })
2053
2054 t.Run("removed-api.txt", func(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002055 javaFixtureFactory.
2056 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`removed-api.txt not available for api scope public`)).
2057 RunTestWithBp(t, bp+`
2058 java_library {
2059 name: "bar",
2060 srcs: ["a.java"],
2061 java_resources: [
2062 ":foo{.public.removed-api.txt}",
2063 ],
2064 }
2065 `)
Paul Duffin46dc45a2020-05-14 15:39:10 +01002066 })
2067}
2068
Paul Duffin3375e352020-04-28 10:44:03 +01002069func TestJavaSdkLibrary_InvalidScopes(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002070 javaFixtureFactory.
2071 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "foo": enabled api scope "system" depends on disabled scope "public"`)).
2072 RunTestWithBp(t, `
2073 java_sdk_library {
2074 name: "foo",
2075 srcs: ["a.java", "b.java"],
2076 api_packages: ["foo"],
2077 // Explicitly disable public to test the check that ensures the set of enabled
2078 // scopes is consistent.
2079 public: {
2080 enabled: false,
2081 },
2082 system: {
2083 enabled: true,
2084 },
2085 }
Paul Duffin3375e352020-04-28 10:44:03 +01002086 `)
2087}
2088
Paul Duffin87a05a32020-05-12 11:50:28 +01002089func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002090 javaFixtureFactory.Extend(
2091 PrepareForTestWithJavaSdkLibraryFiles,
2092 FixtureWithLastReleaseApis("foo"),
2093 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002094 java_sdk_library {
2095 name: "foo",
2096 srcs: ["a.java", "b.java"],
2097 api_packages: ["foo"],
2098 system: {
2099 enabled: true,
2100 sdk_version: "module_current",
2101 },
2102 }
2103 `)
2104}
2105
Paul Duffin0c5bae52020-06-02 13:00:08 +01002106func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002107 javaFixtureFactory.Extend(
2108 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 Duffin163043d2021-03-12 19:18:49 +00002126 javaFixtureFactory.Extend(
2127 PrepareForTestWithJavaSdkLibraryFiles,
2128 FixtureWithLastReleaseApis("foo"),
2129 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002130 java_sdk_library {
2131 name: "foo",
2132 srcs: ["a.java", "b.java"],
2133 api_packages: ["foo"],
2134 system: {
2135 enabled: true,
2136 },
2137 system_server: {
2138 enabled: true,
2139 },
2140 }
2141 `)
2142}
2143
Paul Duffin803a9562020-05-20 11:52:25 +01002144func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin22b77cd2021-03-12 19:15:01 +00002145 javaFixtureFactory.
2146 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2147 RunTestWithBp(t, `
2148 java_sdk_library {
2149 name: "foo",
2150 srcs: ["a.java"],
2151 public: {
2152 enabled: false,
2153 },
2154 }
Paul Duffin803a9562020-05-20 11:52:25 +01002155
Paul Duffin22b77cd2021-03-12 19:15:01 +00002156 java_library {
2157 name: "baz",
2158 srcs: ["a.java"],
2159 libs: ["foo"],
2160 sdk_version: "module_current",
2161 }
Paul Duffin803a9562020-05-20 11:52:25 +01002162 `)
2163}
2164
2165func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002166 javaFixtureFactory.Extend(
2167 PrepareForTestWithJavaSdkLibraryFiles,
2168 FixtureWithLastReleaseApis("foo"),
2169 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002170 java_sdk_library {
2171 name: "foo",
2172 srcs: ["a.java"],
2173 system: {
2174 enabled: true,
2175 },
2176 }
2177
2178 java_library {
2179 name: "baz",
2180 srcs: ["a.java"],
2181 libs: ["foo"],
2182 // foo does not have module-lib scope so it should fallback to system
2183 sdk_version: "module_current",
2184 }
2185 `)
2186}
2187
Jiyong Park932cdfe2020-05-28 00:19:53 +09002188func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin163043d2021-03-12 19:18:49 +00002189 result := javaFixtureFactory.Extend(
2190 PrepareForTestWithJavaSdkLibraryFiles,
2191 FixtureWithLastReleaseApis("foo"),
2192 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002193 java_sdk_library {
2194 name: "foo",
2195 srcs: ["a.java"],
2196 system: {
2197 enabled: true,
2198 },
2199 default_to_stubs: true,
2200 }
2201
2202 java_library {
2203 name: "baz",
2204 srcs: ["a.java"],
2205 libs: ["foo"],
2206 // does not have sdk_version set, should fallback to module,
2207 // which will then fallback to system because the module scope
2208 // is not enabled.
2209 }
2210 `)
2211 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002212 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002213 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2214 t.Errorf("expected %q, found %#q", expected, actual)
2215 }
2216}
2217
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002218var compilerFlagsTestCases = []struct {
2219 in string
2220 out bool
2221}{
2222 {
2223 in: "a",
2224 out: false,
2225 },
2226 {
2227 in: "-a",
2228 out: true,
2229 },
2230 {
2231 in: "-no-jdk",
2232 out: false,
2233 },
2234 {
2235 in: "-no-stdlib",
2236 out: false,
2237 },
2238 {
2239 in: "-kotlin-home",
2240 out: false,
2241 },
2242 {
2243 in: "-kotlin-home /some/path",
2244 out: false,
2245 },
2246 {
2247 in: "-include-runtime",
2248 out: false,
2249 },
2250 {
2251 in: "-Xintellij-plugin-root",
2252 out: false,
2253 },
2254}
2255
2256type mockContext struct {
2257 android.ModuleContext
2258 result bool
2259}
2260
2261func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2262 // CheckBadCompilerFlags calls this function when the flag should be rejected
2263 ctx.result = false
2264}
2265
2266func TestCompilerFlags(t *testing.T) {
2267 for _, testCase := range compilerFlagsTestCases {
2268 ctx := &mockContext{result: true}
2269 CheckKotlincFlags(ctx, []string{testCase.in})
2270 if ctx.result != testCase.out {
2271 t.Errorf("incorrect output:")
2272 t.Errorf(" input: %#v", testCase.in)
2273 t.Errorf(" expected: %#v", testCase.out)
2274 t.Errorf(" got: %#v", ctx.result)
2275 }
2276 }
2277}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002278
2279// TODO(jungjw): Consider making this more robust by ignoring path order.
2280func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00002281 variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop()
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002282 flags := strings.Split(variables["javacFlags"], " ")
2283 got := ""
2284 for _, flag := range flags {
2285 keyEnd := strings.Index(flag, "=")
2286 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2287 got = flag[keyEnd+1:]
2288 break
2289 }
2290 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00002291 if expected != android.StringPathRelativeToTop(ctx.Config().BuildDir(), got) {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002292 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2293 }
2294}
2295
2296func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002297 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002298 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002299 bp := `
2300 java_library {
2301 name: "foo",
2302 srcs: ["a.java"],
2303 java_version: "1.8",
2304 }
2305
2306 java_library {
2307 name: "bar",
2308 srcs: ["b.java"],
2309 sdk_version: "none",
2310 system_modules: "none",
2311 patch_module: "java.base",
2312 java_version: "1.8",
2313 }
2314
2315 java_library {
2316 name: "baz",
2317 srcs: ["c.java"],
2318 patch_module: "java.base",
2319 java_version: "1.8",
2320 }
2321 `
2322 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002323
2324 checkPatchModuleFlag(t, ctx, "foo", "")
2325 checkPatchModuleFlag(t, ctx, "bar", "")
2326 checkPatchModuleFlag(t, ctx, "baz", "")
2327 })
2328
Pete Gillin0c2143e2019-05-02 15:32:11 +01002329 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002330 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002331 bp := `
2332 java_library {
2333 name: "foo",
2334 srcs: ["a.java"],
2335 }
2336
2337 java_library {
2338 name: "bar",
2339 srcs: ["b.java"],
2340 sdk_version: "none",
2341 system_modules: "none",
2342 patch_module: "java.base",
2343 }
2344
2345 java_library {
2346 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002347 srcs: [
2348 "c.java",
2349 // Tests for b/150878007
2350 "dir/d.java",
2351 "dir2/e.java",
2352 "dir2/f.java",
2353 "nested/dir/g.java"
2354 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002355 patch_module: "java.base",
2356 }
2357 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002358 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002359
2360 checkPatchModuleFlag(t, ctx, "foo", "")
Paul Duffin414ea5e2021-03-22 17:31:52 +00002361 expected := "java.base=.:out/soong"
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002362 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002363 expected = "java.base=" + strings.Join([]string{
Paul Duffin414ea5e2021-03-22 17:31:52 +00002364 ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002365 checkPatchModuleFlag(t, ctx, "baz", expected)
2366 })
2367}
Paul Duffina7b9f422020-01-10 17:12:18 +00002368
Paul Duffin83a2d962019-11-19 19:44:10 +00002369func TestJavaLibraryWithSystemModules(t *testing.T) {
2370 ctx, _ := testJava(t, `
2371 java_library {
2372 name: "lib-with-source-system-modules",
2373 srcs: [
2374 "a.java",
2375 ],
2376 sdk_version: "none",
2377 system_modules: "source-system-modules",
2378 }
2379
2380 java_library {
2381 name: "source-jar",
2382 srcs: [
2383 "a.java",
2384 ],
2385 }
2386
2387 java_system_modules {
2388 name: "source-system-modules",
2389 libs: ["source-jar"],
2390 }
2391
2392 java_library {
2393 name: "lib-with-prebuilt-system-modules",
2394 srcs: [
2395 "a.java",
2396 ],
2397 sdk_version: "none",
2398 system_modules: "prebuilt-system-modules",
2399 }
2400
2401 java_import {
2402 name: "prebuilt-jar",
2403 jars: ["a.jar"],
2404 }
2405
2406 java_system_modules_import {
2407 name: "prebuilt-system-modules",
2408 libs: ["prebuilt-jar"],
2409 }
2410 `)
2411
2412 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2413
2414 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2415}
2416
2417func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2418 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2419 bootClasspath := javacRule.Args["bootClasspath"]
2420 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2421 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2422 }
2423}
Jiyong Park19604de2020-03-24 16:44:11 +09002424
2425func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2426 ctx, _ := testJava(t, `
2427 java_library {
2428 name: "foo",
2429 srcs: ["aidl/foo/IFoo.aidl"],
2430 libs: ["bar"],
2431 }
2432
2433 java_import {
2434 name: "bar",
2435 jars: ["a.jar"],
2436 aidl: {
2437 export_include_dirs: ["aidl/bar"],
2438 },
2439 }
2440 `)
2441
2442 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2443 expectedAidlFlag := "-Iaidl/bar"
2444 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2445 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2446 }
2447}
Liz Kammerdd849a82020-06-12 16:38:45 -07002448
Jooyung Hane197d8b2021-01-05 10:33:16 +09002449func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2450 ctx, _ := testJava(t, `
2451 java_library {
2452 name: "foo",
2453 srcs: ["aidl/foo/IFoo.aidl"],
2454 aidl: { flags: ["-Werror"], },
2455 }
2456 `)
2457
2458 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2459 expectedAidlFlag := "-Werror"
2460 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2461 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2462 }
2463}
2464
Liz Kammerdd849a82020-06-12 16:38:45 -07002465func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002466 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002467 java_test_host {
2468 name: "foo",
2469 srcs: ["a.java"],
2470 data_native_bins: ["bin"]
2471 }
2472
2473 python_binary_host {
2474 name: "bin",
2475 srcs: ["bin.py"],
2476 }
2477 `)
2478
2479 buildOS := android.BuildOs.String()
2480
2481 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002482 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002483 expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
Liz Kammerdd849a82020-06-12 16:38:45 -07002484 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002485 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual)
Liz Kammerdd849a82020-06-12 16:38:45 -07002486}
Yuexi Ma627263f2021-03-04 13:47:56 -08002487
2488func TestDefaultInstallable(t *testing.T) {
2489 ctx, _ := testJava(t, `
2490 java_test_host {
2491 name: "foo"
2492 }
2493 `)
2494
2495 buildOS := android.BuildOs.String()
2496 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2497 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2498 module.properties.Installable)
2499}