blob: b6f639f98083b8295af1d94de263c3068343f12d [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Paul Duffin1a393322020-11-18 16:36:47 +000018 "fmt"
Colin Cross72bb3632017-07-13 16:23:21 -070019 "os"
20 "path/filepath"
Colin Crossc0806172019-06-14 18:51:47 -070021 "reflect"
Paul Duffindaaa3322020-05-26 18:13:57 +010022 "regexp"
Nan Zhang61eaedb2017-11-02 13:28:15 -070023 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070024 "strings"
25 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070026
Jeongik Cha28df2572019-11-11 10:46:36 +090027 "github.com/google/blueprint/proptools"
28
Colin Crossa4f08812018-10-02 22:03:40 -070029 "android/soong/android"
30 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080031 "android/soong/dexpreopt"
Colin Cross1661aff2021-03-12 17:56:51 -080032 "android/soong/genrule"
Liz Kammerdd849a82020-06-12 16:38:45 -070033 "android/soong/python"
Colin Cross72bb3632017-07-13 16:23:21 -070034)
35
Paul Duffin70d3bee2021-03-21 11:26:05 +000036// Legacy preparer used for running tests within the java package.
37//
38// This includes everything that was needed to run any test in the java package prior to the
39// introduction of the test fixtures. Tests that are being converted to use fixtures directly
40// rather than through the testJava...() methods should avoid using this and instead use the
41// various preparers directly, using android.GroupFixturePreparers(...) to group them when
42// necessary.
43//
44// deprecated
45var prepareForJavaTest = android.GroupFixturePreparers(
Paul Duffin95bdab42021-03-08 21:48:46 +000046 genrule.PrepareForTestWithGenRuleBuildComponents,
47 // Get the CC build components but not default modules.
48 cc.PrepareForTestWithCcBuildComponents,
49 // Include all the default java modules.
50 PrepareForTestWithJavaDefaultModules,
Paul Duffin42da69d2021-03-22 13:41:36 +000051 PrepareForTestWithOverlayBuildComponents,
Paul Duffin220ddd72021-03-17 23:54:30 +000052 python.PrepareForTestWithPythonBuildComponents,
Paul Duffin95bdab42021-03-08 21:48:46 +000053 android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
Paul Duffin95bdab42021-03-08 21:48:46 +000054 ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory)
55 }),
Paul Duffin95bdab42021-03-08 21:48:46 +000056 dexpreopt.PrepareForTestWithDexpreopt,
57)
58
Colin Cross72bb3632017-07-13 16:23:21 -070059func TestMain(m *testing.M) {
Paul Duffind6339af2021-03-22 18:05:04 +000060 os.Exit(m.Run())
Colin Cross527012a2017-11-30 22:56:16 -080061}
62
Paul Duffin95bdab42021-03-08 21:48:46 +000063// testJavaError is a legacy way of running tests of java modules that expect errors.
64//
65// See testJava for an explanation as to how to stop using this deprecated method.
66//
67// deprecated
Jeongik Cha2cc570d2019-10-29 15:44:45 +090068func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) {
Jeongik Cha538c0d02019-07-11 15:54:27 +090069 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +000070 result := android.GroupFixturePreparers(
71 prepareForJavaTest, dexpreopt.PrepareForTestWithDexpreopt).
Paul Duffin6bac49c2021-03-12 21:28:15 +000072 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
73 RunTestWithBp(t, bp)
74 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +090075}
76
Paul Duffin95bdab42021-03-08 21:48:46 +000077// testJavaErrorWithConfig is a legacy way of running tests of java modules that expect errors.
78//
79// See testJava for an explanation as to how to stop using this deprecated method.
80//
81// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080082func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) {
Jeongik Cha2cc570d2019-10-29 15:44:45 +090083 t.Helper()
Paul Duffin95bdab42021-03-08 21:48:46 +000084 // This must be done on the supplied config and not as part of the fixture because any changes to
85 // the fixture's config will be ignored when RunTestWithConfig replaces it.
Colin Cross98be1bb2019-12-13 20:41:13 -080086 pathCtx := android.PathContextForTesting(config)
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000087 dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
Paul Duffin71ae5942021-03-22 15:36:52 +000088 result := prepareForJavaTest.
Paul Duffin95bdab42021-03-08 21:48:46 +000089 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
90 RunTestWithConfig(t, config)
91 return result.TestContext, result.Config
Paul Duffinec0fe172021-02-25 15:34:13 +000092}
93
Paul Duffin71ae5942021-03-22 15:36:52 +000094// testJavaWithFS runs tests using the prepareForJavaTest
Paul Duffin95bdab42021-03-08 21:48:46 +000095//
96// See testJava for an explanation as to how to stop using this deprecated method.
97//
98// deprecated
99func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) {
Colin Cross238c1f32020-06-07 16:58:18 -0700100 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +0000101 result := android.GroupFixturePreparers(
102 prepareForJavaTest, fs.AddToFixture()).RunTestWithBp(t, bp)
Paul Duffin95bdab42021-03-08 21:48:46 +0000103 return result.TestContext, result.Config
Colin Cross238c1f32020-06-07 16:58:18 -0700104}
105
Paul Duffin71ae5942021-03-22 15:36:52 +0000106// testJava runs tests using the prepareForJavaTest
Paul Duffin95bdab42021-03-08 21:48:46 +0000107//
Paul Duffin71ae5942021-03-22 15:36:52 +0000108// Do not add any new usages of this, instead use the prepareForJavaTest directly as it makes it
Paul Duffin95bdab42021-03-08 21:48:46 +0000109// much easier to customize the test behavior.
110//
111// If it is necessary to customize the behavior of an existing test that uses this then please first
Paul Duffin71ae5942021-03-22 15:36:52 +0000112// convert the test to using prepareForJavaTest first and then in a following change add the
Paul Duffin95bdab42021-03-08 21:48:46 +0000113// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
114// that it did not change the test behavior unexpectedly.
115//
116// deprecated
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700117func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800118 t.Helper()
Paul Duffin71ae5942021-03-22 15:36:52 +0000119 result := prepareForJavaTest.RunTestWithBp(t, bp)
Paul Duffin95bdab42021-03-08 21:48:46 +0000120 return result.TestContext, result.Config
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900121}
122
Paul Duffin95bdab42021-03-08 21:48:46 +0000123// defaultModuleToPath constructs a path to the turbine generate jar for a default test module that
124// is defined in PrepareForIntegrationTestWithJava
125func defaultModuleToPath(name string) string {
Paul Duffin76101fa2021-03-13 09:55:25 +0000126 switch {
127 case name == `""`:
128 return name
129 case strings.HasSuffix(name, ".jar"):
130 return name
131 default:
Paul Duffin414ea5e2021-03-22 17:31:52 +0000132 return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar")
Paul Duffin76101fa2021-03-13 09:55:25 +0000133 }
Paul Duffin95bdab42021-03-08 21:48:46 +0000134}
135
Jeongik Chae403e9e2019-12-07 00:16:24 +0900136func TestJavaLinkType(t *testing.T) {
137 testJava(t, `
138 java_library {
139 name: "foo",
140 srcs: ["a.java"],
141 libs: ["bar"],
142 static_libs: ["baz"],
143 }
144
145 java_library {
146 name: "bar",
147 sdk_version: "current",
148 srcs: ["b.java"],
149 }
150
151 java_library {
152 name: "baz",
153 sdk_version: "system_current",
154 srcs: ["c.java"],
155 }
156 `)
157
Steven Moreland00298982020-11-17 21:44:36 +0000158 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900159 java_library {
160 name: "foo",
161 srcs: ["a.java"],
162 libs: ["bar"],
163 sdk_version: "current",
164 static_libs: ["baz"],
165 }
166
167 java_library {
168 name: "bar",
169 sdk_version: "current",
170 srcs: ["b.java"],
171 }
172
173 java_library {
174 name: "baz",
175 sdk_version: "system_current",
176 srcs: ["c.java"],
177 }
178 `)
179
180 testJava(t, `
181 java_library {
182 name: "foo",
183 srcs: ["a.java"],
184 libs: ["bar"],
185 sdk_version: "system_current",
186 static_libs: ["baz"],
187 }
188
189 java_library {
190 name: "bar",
191 sdk_version: "current",
192 srcs: ["b.java"],
193 }
194
195 java_library {
196 name: "baz",
197 sdk_version: "system_current",
198 srcs: ["c.java"],
199 }
200 `)
201
Steven Moreland00298982020-11-17 21:44:36 +0000202 testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", `
Jeongik Chae403e9e2019-12-07 00:16:24 +0900203 java_library {
204 name: "foo",
205 srcs: ["a.java"],
206 libs: ["bar"],
207 sdk_version: "system_current",
208 static_libs: ["baz"],
209 }
210
211 java_library {
212 name: "bar",
213 sdk_version: "current",
214 srcs: ["b.java"],
215 }
216
217 java_library {
218 name: "baz",
219 srcs: ["c.java"],
220 }
221 `)
222}
223
Colin Cross72bb3632017-07-13 16:23:21 -0700224func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700225 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700226 java_library {
227 name: "foo",
228 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700229 libs: ["bar"],
230 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700231 }
232
233 java_library {
234 name: "bar",
235 srcs: ["b.java"],
236 }
237
238 java_library {
239 name: "baz",
240 srcs: ["c.java"],
241 }
Colin Crossd5934c82017-10-02 13:55:26 -0700242 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700243
Paul Duffin414ea5e2021-03-22 17:31:52 +0000244 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop()
Nan Zhanged19fc32017-10-19 13:06:22 -0700245 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700246
247 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
248 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
249 }
250
Colin Cross1ee23172017-10-18 14:44:18 -0700251 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000252 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
253 bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700254
Paul Duffin22b77cd2021-03-12 19:15:01 +0000255 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700256
Paul Duffin22b77cd2021-03-12 19:15:01 +0000257 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700258
259 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
260 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700261 }
262}
263
Artur Satayev9cf46692019-11-26 18:08:34 +0000264func TestExportedPlugins(t *testing.T) {
265 type Result struct {
Colin Crossc9fe10f2020-11-19 18:06:03 -0800266 library string
267 processors string
268 disableTurbine bool
Artur Satayev9cf46692019-11-26 18:08:34 +0000269 }
270 var tests = []struct {
271 name string
272 extra string
273 results []Result
274 }{
275 {
276 name: "Exported plugin is not a direct plugin",
277 extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`,
278 results: []Result{{library: "exports", processors: "-proc:none"}},
279 },
280 {
281 name: "Exports plugin to dependee",
282 extra: `
283 java_library{name: "exports", exported_plugins: ["plugin"]}
284 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
285 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
286 `,
287 results: []Result{
288 {library: "foo", processors: "-processor com.android.TestPlugin"},
289 {library: "bar", processors: "-processor com.android.TestPlugin"},
290 },
291 },
292 {
293 name: "Exports plugin to android_library",
294 extra: `
295 java_library{name: "exports", exported_plugins: ["plugin"]}
296 android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
297 android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
298 `,
299 results: []Result{
300 {library: "foo", processors: "-processor com.android.TestPlugin"},
301 {library: "bar", processors: "-processor com.android.TestPlugin"},
302 },
303 },
304 {
305 name: "Exports plugin is not propagated via transitive deps",
306 extra: `
307 java_library{name: "exports", exported_plugins: ["plugin"]}
308 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
309 java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]}
310 `,
311 results: []Result{
312 {library: "foo", processors: "-processor com.android.TestPlugin"},
313 {library: "bar", processors: "-proc:none"},
314 },
315 },
316 {
317 name: "Exports plugin appends to plugins",
318 extra: `
319 java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"}
320 java_library{name: "exports", exported_plugins: ["plugin"]}
321 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]}
322 `,
323 results: []Result{
324 {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"},
325 },
326 },
Colin Crossc9fe10f2020-11-19 18:06:03 -0800327 {
328 name: "Exports plugin to with generates_api to dependee",
329 extra: `
330 java_library{name: "exports", exported_plugins: ["plugin_generates_api"]}
331 java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]}
332 java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]}
333 `,
334 results: []Result{
335 {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true},
336 {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true},
337 },
338 },
Artur Satayev9cf46692019-11-26 18:08:34 +0000339 }
340
341 for _, test := range tests {
342 t.Run(test.name, func(t *testing.T) {
343 ctx, _ := testJava(t, `
344 java_plugin {
345 name: "plugin",
346 processor_class: "com.android.TestPlugin",
347 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800348 java_plugin {
349 name: "plugin_generates_api",
350 generates_api: true,
351 processor_class: "com.android.TestPlugin",
352 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000353 `+test.extra)
354
355 for _, want := range test.results {
356 javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac")
357 if javac.Args["processor"] != want.processors {
358 t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"])
359 }
Colin Crossc9fe10f2020-11-19 18:06:03 -0800360 turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine")
361 disableTurbine := turbine.BuildParams.Rule == nil
362 if disableTurbine != want.disableTurbine {
363 t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine)
364 }
Artur Satayev9cf46692019-11-26 18:08:34 +0000365 }
366 })
367 }
368}
369
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900370func TestSdkVersionByPartition(t *testing.T) {
371 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900372 java_library {
373 name: "foo",
374 srcs: ["a.java"],
375 vendor: true,
376 }
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900377 `)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900378
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900379 testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900380 java_library {
381 name: "bar",
382 srcs: ["b.java"],
383 }
384 `)
385
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900386 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900387 bp := `
388 java_library {
389 name: "foo",
390 srcs: ["a.java"],
391 product_specific: true,
392 }
393 `
Colin Cross98be1bb2019-12-13 20:41:13 -0800394
Paul Duffin414ea5e2021-03-22 17:31:52 +0000395 errorHandler := android.FixtureExpectsNoErrors
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900396 if enforce {
Paul Duffin414ea5e2021-03-22 17:31:52 +0000397 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 +0900398 }
Paul Duffin414ea5e2021-03-22 17:31:52 +0000399
400 android.GroupFixturePreparers(
401 PrepareForTestWithJavaDefaultModules,
402 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
403 variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
404 }),
405 ).
406 ExtendWithErrorHandler(errorHandler).
407 RunTestWithBp(t, bp)
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900408 }
409}
410
Colin Crossd5934c82017-10-02 13:55:26 -0700411func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700412 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700413 java_library {
414 name: "foo",
415 srcs: ["a.java"],
416 target: {
417 android: {
418 srcs: ["b.java"],
419 },
420 },
421 }
422 `)
423
424 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
425 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
426 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
427 }
428}
429
Colin Cross6b4a32d2017-12-05 13:42:45 -0800430func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700431 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800432 java_library_host {
433 name: "foo",
434 srcs: ["a.java"],
435 }
436
437 java_binary_host {
438 name: "bar",
439 srcs: ["b.java"],
440 static_libs: ["foo"],
Colin Cross89226d92020-10-09 19:00:54 -0700441 jni_libs: ["libjni"],
442 }
443
444 cc_library_shared {
445 name: "libjni",
446 host_supported: true,
447 device_supported: false,
448 stl: "none",
Colin Cross6b4a32d2017-12-05 13:42:45 -0800449 }
450 `)
451
452 buildOS := android.BuildOs.String()
453
454 bar := ctx.ModuleForTests("bar", buildOS+"_common")
455 barJar := bar.Output("bar.jar").Output.String()
456 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
457 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
458
Colin Cross89226d92020-10-09 19:00:54 -0700459 libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared")
460 libjniSO := libjni.Rule("Cp").Output.String()
461
Colin Cross6b4a32d2017-12-05 13:42:45 -0800462 // Test that the install binary wrapper depends on the installed jar file
Colin Crossc179ea62020-10-09 10:54:15 -0700463 if g, w := barWrapperDeps, barJar; !android.InList(w, g) {
464 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800465 }
Colin Cross89226d92020-10-09 19:00:54 -0700466
467 // Test that the install binary wrapper depends on the installed JNI libraries
468 if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) {
469 t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g)
Colin Cross6b4a32d2017-12-05 13:42:45 -0800470 }
Alex Humesky2070e322020-06-09 20:23:08 -0400471}
Colin Cross6b4a32d2017-12-05 13:42:45 -0800472
Alex Humesky2070e322020-06-09 20:23:08 -0400473func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) {
474 bp := `
475 java_library {
476 name: "target_library",
477 srcs: ["a.java"],
478 }
479
480 java_binary_host {
481 name: "host_binary",
482 srcs: ["b.java"],
483 }
484 `
Alex Humesky2070e322020-06-09 20:23:08 -0400485
Paul Duffin414ea5e2021-03-22 17:31:52 +0000486 result := android.GroupFixturePreparers(
487 PrepareForTestWithJavaDefaultModules,
488 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
489 variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true)
490 }),
491 ).RunTestWithBp(t, bp)
Alex Humesky2070e322020-06-09 20:23:08 -0400492
Liz Kammer7941b302020-07-28 13:27:34 -0700493 // first, check that the -g flag is added to target modules
Paul Duffin414ea5e2021-03-22 17:31:52 +0000494 targetLibrary := result.ModuleForTests("target_library", "android_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400495 targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"]
496 if !strings.Contains(targetJavaFlags, "-g:source,lines") {
497 t.Errorf("target library javac flags %v should contain "+
498 "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags)
499 }
500
501 // check that -g is not overridden for host modules
502 buildOS := android.BuildOs.String()
Paul Duffin414ea5e2021-03-22 17:31:52 +0000503 hostBinary := result.ModuleForTests("host_binary", buildOS+"_common")
Alex Humesky2070e322020-06-09 20:23:08 -0400504 hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"]
505 if strings.Contains(hostJavaFlags, "-g:source,lines") {
506 t.Errorf("java_binary_host javac flags %v should not have "+
507 "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags)
508 }
Colin Cross6b4a32d2017-12-05 13:42:45 -0800509}
510
Colin Cross72bb3632017-07-13 16:23:21 -0700511func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700512 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700513 java_library {
514 name: "foo",
Paul Duffin91547182019-11-12 19:39:36 +0000515 srcs: ["a.java", ":stubs-source"],
Paul Duffinfcfd7912020-01-31 17:54:30 +0000516 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700517 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700518 }
519
Colin Cross74d73e22017-08-02 11:05:49 -0700520 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700521 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700522 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700523 }
524
Colin Cross74d73e22017-08-02 11:05:49 -0700525 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700526 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700527 jars: ["b.jar"],
Liz Kammerd6c31d22020-08-05 15:40:41 -0700528 sdk_version: "current",
529 compile_dex: true,
Colin Cross72bb3632017-07-13 16:23:21 -0700530 }
Colin Cross42be7612019-02-21 18:12:14 -0800531
532 dex_import {
533 name: "qux",
534 jars: ["b.jar"],
535 }
Colin Cross79c7c262019-04-17 11:11:46 -0700536
537 java_sdk_library_import {
Paul Duffin56d44902020-01-31 13:36:25 +0000538 name: "sdklib",
539 public: {
540 jars: ["c.jar"],
541 },
542 }
543
Paul Duffin91547182019-11-12 19:39:36 +0000544 prebuilt_stubs_sources {
545 name: "stubs-source",
Paul Duffin9b478b02019-12-10 13:41:51 +0000546 srcs: ["stubs/sources"],
Paul Duffin91547182019-11-12 19:39:36 +0000547 }
Paul Duffin1b82e6a2019-12-03 18:06:47 +0000548
549 java_test_import {
550 name: "test",
551 jars: ["a.jar"],
552 test_suites: ["cts"],
553 test_config: "AndroidTest.xml",
554 }
Colin Cross72bb3632017-07-13 16:23:21 -0700555 `)
556
Paul Duffin9b478b02019-12-10 13:41:51 +0000557 fooModule := ctx.ModuleForTests("foo", "android_common")
558 javac := fooModule.Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700559 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Liz Kammerd6c31d22020-08-05 15:40:41 -0700560 barModule := ctx.ModuleForTests("bar", "android_common")
561 barJar := barModule.Rule("combineJar").Output
562 bazModule := ctx.ModuleForTests("baz", "android_common")
563 bazJar := bazModule.Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700564 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700565
Paul Duffin9b478b02019-12-10 13:41:51 +0000566 fooLibrary := fooModule.Module().(*Library)
567 assertDeepEquals(t, "foo java sources incorrect",
568 []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings())
Paul Duffin91547182019-11-12 19:39:36 +0000569
Paul Duffin9b478b02019-12-10 13:41:51 +0000570 assertDeepEquals(t, "foo java source jars incorrect",
571 []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"},
572 android.NormalizePathsForTesting(fooLibrary.compiledSrcJars))
Paul Duffin91547182019-11-12 19:39:36 +0000573
Colin Cross37f6d792018-07-12 12:28:41 -0700574 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
575 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700576 }
577
Liz Kammerd6c31d22020-08-05 15:40:41 -0700578 barDexJar := barModule.Module().(*Import).DexJarBuildPath()
579 if barDexJar != nil {
580 t.Errorf("bar dex jar build path expected to be nil, got %q", barDexJar)
581 }
582
Colin Cross79c7c262019-04-17 11:11:46 -0700583 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
584 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
585 }
586
Colin Cross37f6d792018-07-12 12:28:41 -0700587 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
588 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700589 }
Colin Cross42be7612019-02-21 18:12:14 -0800590
Paul Duffin414ea5e2021-03-22 17:31:52 +0000591 bazDexJar := bazModule.Module().(*Import).DexJarBuildPath()
592 expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar"
593 android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar)
Liz Kammerd6c31d22020-08-05 15:40:41 -0700594
Colin Cross42be7612019-02-21 18:12:14 -0800595 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700596}
597
Paul Duffin9b478b02019-12-10 13:41:51 +0000598func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
599 if !reflect.DeepEqual(expected, actual) {
600 t.Errorf("%s: expected %q, found %q", message, expected, actual)
601 }
602}
603
Paul Duffin1a393322020-11-18 16:36:47 +0000604func TestPrebuiltStubsSources(t *testing.T) {
605 test := func(t *testing.T, sourcesPath string, expectedInputs []string) {
606 ctx, _ := testJavaWithFS(t, fmt.Sprintf(`
607prebuilt_stubs_sources {
608 name: "stubs-source",
609 srcs: ["%s"],
610}`, sourcesPath), map[string][]byte{
611 "stubs/sources/pkg/A.java": nil,
612 "stubs/sources/pkg/B.java": nil,
613 })
614
615 zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src")
616 if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) {
617 t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual)
618 }
619 }
620
621 t.Run("empty/missing directory", func(t *testing.T) {
622 test(t, "empty-directory", []string{})
623 })
624
625 t.Run("non-empty set of sources", func(t *testing.T) {
626 test(t, "stubs/sources", []string{
627 "stubs/sources/pkg/A.java",
628 "stubs/sources/pkg/B.java",
629 })
630 })
631}
632
Paul Duffin56d44902020-01-31 13:36:25 +0000633func TestJavaSdkLibraryImport(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000634 result := prepareForJavaTest.RunTestWithBp(t, `
Paul Duffin56d44902020-01-31 13:36:25 +0000635 java_library {
636 name: "foo",
637 srcs: ["a.java"],
638 libs: ["sdklib"],
639 sdk_version: "current",
640 }
641
642 java_library {
643 name: "foo.system",
644 srcs: ["a.java"],
645 libs: ["sdklib"],
646 sdk_version: "system_current",
647 }
648
649 java_library {
650 name: "foo.test",
651 srcs: ["a.java"],
652 libs: ["sdklib"],
653 sdk_version: "test_current",
654 }
655
656 java_sdk_library_import {
657 name: "sdklib",
658 public: {
659 jars: ["a.jar"],
660 },
661 system: {
662 jars: ["b.jar"],
663 },
664 test: {
665 jars: ["c.jar"],
Paul Duffin0f8faff2020-05-20 16:18:00 +0100666 stub_srcs: ["c.java"],
Paul Duffin56d44902020-01-31 13:36:25 +0000667 },
668 }
669 `)
670
671 for _, scope := range []string{"", ".system", ".test"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000672 fooModule := result.ModuleForTests("foo"+scope, "android_common")
Paul Duffin56d44902020-01-31 13:36:25 +0000673 javac := fooModule.Rule("javac")
674
Paul Duffin22b77cd2021-03-12 19:15:01 +0000675 sdklibStubsJar := result.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output
676 android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], sdklibStubsJar.String())
Paul Duffin56d44902020-01-31 13:36:25 +0000677 }
Paul Duffinca8d9a52020-06-26 22:20:25 +0100678
Paul Duffin22b77cd2021-03-12 19:15:01 +0000679 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100680 `prebuilt_sdklib.stubs`,
681 `prebuilt_sdklib.stubs.source.test`,
682 `prebuilt_sdklib.stubs.system`,
683 `prebuilt_sdklib.stubs.test`,
684 })
685}
686
687func TestJavaSdkLibraryImport_WithSource(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000688 result := android.GroupFixturePreparers(
689 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000690 PrepareForTestWithJavaSdkLibraryFiles,
691 FixtureWithLastReleaseApis("sdklib"),
692 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100693 java_sdk_library {
694 name: "sdklib",
695 srcs: ["a.java"],
696 sdk_version: "none",
697 system_modules: "none",
698 public: {
699 enabled: true,
700 },
701 }
702
703 java_sdk_library_import {
704 name: "sdklib",
705 public: {
706 jars: ["a.jar"],
707 },
708 }
709 `)
710
Paul Duffin22b77cd2021-03-12 19:15:01 +0000711 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100712 `dex2oatd`,
713 `prebuilt_sdklib`,
714 `sdklib.impl`,
715 `sdklib.stubs`,
716 `sdklib.stubs.source`,
717 `sdklib.xml`,
718 })
719
Paul Duffin22b77cd2021-03-12 19:15:01 +0000720 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffin44f1d842020-06-26 20:17:02 +0100721 `prebuilt_sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100722 `sdklib.impl`,
723 // This should be prebuilt_sdklib.stubs but is set to sdklib.stubs because the
724 // dependency is added after prebuilts may have been renamed and so has to use
725 // the renamed name.
Paul Duffinca8d9a52020-06-26 22:20:25 +0100726 `sdklib.xml`,
727 })
728}
729
730func TestJavaSdkLibraryImport_Preferred(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000731 result := android.GroupFixturePreparers(
732 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000733 PrepareForTestWithJavaSdkLibraryFiles,
734 FixtureWithLastReleaseApis("sdklib"),
735 ).RunTestWithBp(t, `
Paul Duffinca8d9a52020-06-26 22:20:25 +0100736 java_sdk_library {
737 name: "sdklib",
738 srcs: ["a.java"],
739 sdk_version: "none",
740 system_modules: "none",
741 public: {
742 enabled: true,
743 },
744 }
745
746 java_sdk_library_import {
747 name: "sdklib",
748 prefer: true,
749 public: {
750 jars: ["a.jar"],
751 },
752 }
753 `)
754
Paul Duffin22b77cd2021-03-12 19:15:01 +0000755 CheckModuleDependencies(t, result.TestContext, "sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100756 `dex2oatd`,
757 `prebuilt_sdklib`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100758 `sdklib.impl`,
Paul Duffin80342d72020-06-26 22:08:43 +0100759 `sdklib.stubs`,
Paul Duffinca8d9a52020-06-26 22:20:25 +0100760 `sdklib.stubs.source`,
761 `sdklib.xml`,
762 })
763
Paul Duffin22b77cd2021-03-12 19:15:01 +0000764 CheckModuleDependencies(t, result.TestContext, "prebuilt_sdklib", "android_common", []string{
Paul Duffinca8d9a52020-06-26 22:20:25 +0100765 `prebuilt_sdklib.stubs`,
766 `sdklib.impl`,
767 `sdklib.xml`,
768 })
Paul Duffin56d44902020-01-31 13:36:25 +0000769}
770
JaeMan Parkff715562020-10-19 17:25:58 +0900771func TestJavaSdkLibraryEnforce(t *testing.T) {
772 partitionToBpOption := func(partition string) string {
773 switch partition {
774 case "system":
775 return ""
776 case "vendor":
777 return "soc_specific: true,"
778 case "product":
779 return "product_specific: true,"
780 default:
781 panic("Invalid partition group name: " + partition)
782 }
783 }
784
785 type testConfigInfo struct {
786 libraryType string
787 fromPartition string
788 toPartition string
789 enforceVendorInterface bool
790 enforceProductInterface bool
791 enforceJavaSdkLibraryCheck bool
792 allowList []string
793 }
794
Paul Duffin05f72de2021-03-12 21:28:51 +0000795 createPreparer := func(info testConfigInfo) android.FixturePreparer {
JaeMan Parkff715562020-10-19 17:25:58 +0900796 bpFileTemplate := `
797 java_library {
798 name: "foo",
799 srcs: ["foo.java"],
800 libs: ["bar"],
801 sdk_version: "current",
802 %s
803 }
804
805 %s {
806 name: "bar",
807 srcs: ["bar.java"],
808 sdk_version: "current",
809 %s
810 }
811 `
812
813 bpFile := fmt.Sprintf(bpFileTemplate,
814 partitionToBpOption(info.fromPartition),
815 info.libraryType,
816 partitionToBpOption(info.toPartition))
817
Paul Duffin05f72de2021-03-12 21:28:51 +0000818 return android.GroupFixturePreparers(
Paul Duffin163043d2021-03-12 19:18:49 +0000819 PrepareForTestWithJavaSdkLibraryFiles,
820 FixtureWithLastReleaseApis("bar"),
Paul Duffin05f72de2021-03-12 21:28:51 +0000821 android.FixtureWithRootAndroidBp(bpFile),
822 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
823 variables.EnforceProductPartitionInterface = proptools.BoolPtr(info.enforceProductInterface)
824 if info.enforceVendorInterface {
825 variables.DeviceVndkVersion = proptools.StringPtr("current")
826 }
827 variables.EnforceInterPartitionJavaSdkLibrary = proptools.BoolPtr(info.enforceJavaSdkLibraryCheck)
828 variables.InterPartitionJavaLibraryAllowList = info.allowList
829 }),
830 )
JaeMan Parkff715562020-10-19 17:25:58 +0900831 }
832
Paul Duffinaa6caa72021-03-13 16:50:15 +0000833 runTest := func(t *testing.T, info testConfigInfo, expectedErrorPattern string) {
834 t.Run(fmt.Sprintf("%#v", info), func(t *testing.T) {
Paul Duffin05f72de2021-03-12 21:28:51 +0000835 errorHandler := android.FixtureExpectsNoErrors
836 if expectedErrorPattern != "" {
837 errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorPattern)
Paul Duffinaa6caa72021-03-13 16:50:15 +0000838 }
Paul Duffin71ae5942021-03-22 15:36:52 +0000839 prepareForJavaTest.ExtendWithErrorHandler(errorHandler).RunTest(t, createPreparer(info))
Paul Duffinaa6caa72021-03-13 16:50:15 +0000840 })
841 }
842
JaeMan Parkff715562020-10-19 17:25:58 +0900843 errorMessage := "is not allowed across the partitions"
844
Paul Duffinaa6caa72021-03-13 16:50:15 +0000845 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900846 libraryType: "java_library",
847 fromPartition: "product",
848 toPartition: "system",
849 enforceVendorInterface: true,
850 enforceProductInterface: true,
851 enforceJavaSdkLibraryCheck: false,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000852 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900853
Paul Duffinaa6caa72021-03-13 16:50:15 +0000854 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900855 libraryType: "java_library",
856 fromPartition: "product",
857 toPartition: "system",
858 enforceVendorInterface: true,
859 enforceProductInterface: false,
860 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000861 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900862
Paul Duffinaa6caa72021-03-13 16:50:15 +0000863 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900864 libraryType: "java_library",
865 fromPartition: "product",
866 toPartition: "system",
867 enforceVendorInterface: true,
868 enforceProductInterface: true,
869 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000870 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900871
Paul Duffinaa6caa72021-03-13 16:50:15 +0000872 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900873 libraryType: "java_library",
874 fromPartition: "vendor",
875 toPartition: "system",
876 enforceVendorInterface: true,
877 enforceProductInterface: true,
878 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000879 }, errorMessage)
JaeMan Parkff715562020-10-19 17:25:58 +0900880
Paul Duffinaa6caa72021-03-13 16:50:15 +0000881 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900882 libraryType: "java_library",
883 fromPartition: "vendor",
884 toPartition: "system",
885 enforceVendorInterface: true,
886 enforceProductInterface: true,
887 enforceJavaSdkLibraryCheck: true,
888 allowList: []string{"bar"},
Paul Duffinaa6caa72021-03-13 16:50:15 +0000889 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900890
Paul Duffinaa6caa72021-03-13 16:50:15 +0000891 runTest(t, testConfigInfo{
JaeMan Parkff715562020-10-19 17:25:58 +0900892 libraryType: "java_library",
893 fromPartition: "vendor",
JaeMan Park90e75352021-01-14 11:56:56 +0900894 toPartition: "product",
895 enforceVendorInterface: true,
896 enforceProductInterface: true,
897 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000898 }, errorMessage)
JaeMan Park90e75352021-01-14 11:56:56 +0900899
Paul Duffinaa6caa72021-03-13 16:50:15 +0000900 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900901 libraryType: "java_sdk_library",
902 fromPartition: "product",
JaeMan Parkff715562020-10-19 17:25:58 +0900903 toPartition: "system",
904 enforceVendorInterface: true,
905 enforceProductInterface: true,
906 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000907 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +0900908
Paul Duffinaa6caa72021-03-13 16:50:15 +0000909 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900910 libraryType: "java_sdk_library",
911 fromPartition: "vendor",
912 toPartition: "system",
913 enforceVendorInterface: true,
914 enforceProductInterface: true,
915 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000916 }, "")
JaeMan Park90e75352021-01-14 11:56:56 +0900917
Paul Duffinaa6caa72021-03-13 16:50:15 +0000918 runTest(t, testConfigInfo{
JaeMan Park90e75352021-01-14 11:56:56 +0900919 libraryType: "java_sdk_library",
920 fromPartition: "vendor",
921 toPartition: "product",
922 enforceVendorInterface: true,
923 enforceProductInterface: true,
924 enforceJavaSdkLibraryCheck: true,
Paul Duffinaa6caa72021-03-13 16:50:15 +0000925 }, "")
JaeMan Parkff715562020-10-19 17:25:58 +0900926}
927
Colin Cross89536d42017-07-07 14:35:50 -0700928func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700929 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700930 java_defaults {
931 name: "defaults",
932 srcs: ["a.java"],
933 libs: ["bar"],
934 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700935 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700936 }
937
938 java_library {
939 name: "foo",
940 defaults: ["defaults"],
941 }
942
943 java_library {
944 name: "bar",
945 srcs: ["b.java"],
946 }
947
948 java_library {
949 name: "baz",
950 srcs: ["c.java"],
951 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700952
953 android_test {
954 name: "atestOptimize",
955 defaults: ["defaults"],
956 optimize: {enabled: true},
957 }
958
959 android_test {
960 name: "atestNoOptimize",
961 defaults: ["defaults"],
962 }
963
964 android_test {
965 name: "atestDefault",
966 srcs: ["a.java"],
967 }
Colin Cross89536d42017-07-07 14:35:50 -0700968 `)
969
Paul Duffin414ea5e2021-03-22 17:31:52 +0000970 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac").RelativeToTop()
Nan Zhanged19fc32017-10-19 13:06:22 -0700971 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700972
973 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
974 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
975 }
976
Paul Duffin414ea5e2021-03-22 17:31:52 +0000977 barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhanged19fc32017-10-19 13:06:22 -0700978 if !strings.Contains(javac.Args["classpath"], barTurbine) {
979 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700980 }
981
Colin Cross1ee23172017-10-18 14:44:18 -0700982 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700983 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
984 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700985 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700986
987 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
988 if atestOptimize.Output == nil {
989 t.Errorf("atestOptimize should optimize APK")
990 }
991
992 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
993 if atestNoOptimize.Output == nil {
994 t.Errorf("atestNoOptimize should not optimize APK")
995 }
996
997 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
998 if atestDefault.Output == nil {
999 t.Errorf("atestDefault should optimize APK")
1000 }
Colin Cross89536d42017-07-07 14:35:50 -07001001}
1002
Colin Cross0f37af02017-09-27 17:42:05 -07001003func TestResources(t *testing.T) {
1004 var table = []struct {
1005 name string
1006 prop string
1007 extra string
1008 args string
1009 }{
1010 {
Colin Crossaf9c55b2017-10-03 14:50:08 -07001011 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -07001012 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -08001013 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001014 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001015 },
1016 {
1017 // Test that a module with java_resources includes the files
1018 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -07001019 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
1020 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001021 },
1022 {
1023 // Test that a module with a filegroup in java_resources includes the files with the
1024 // path prefix
1025 name: "resource filegroup",
1026 prop: `java_resources: [":foo-res"]`,
1027 extra: `
1028 filegroup {
1029 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -08001030 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -07001031 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -07001032 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -07001033 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -07001034 },
1035 {
Colin Cross0ead1d72018-04-10 13:07:42 -07001036 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
1037 name: "wildcard dirs",
1038 prop: `java_resource_dirs: ["java-res/*"]`,
1039 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
1040 },
1041 {
1042 // Test that a module exclude_java_resource_dirs excludes the files
1043 name: "wildcard dirs",
1044 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
1045 args: "-C java-res/a -f java-res/a/a",
1046 },
Colin Crosscedd4762018-09-13 11:26:19 -07001047 {
1048 // Test wildcards in java_resources
1049 name: "wildcard files",
1050 prop: `java_resources: ["java-res/**/*"]`,
1051 args: "-C . -f java-res/a/a -f java-res/b/b",
1052 },
1053 {
1054 // Test exclude_java_resources with java_resources
1055 name: "wildcard files with exclude",
1056 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
1057 args: "-C . -f java-res/a/a",
1058 },
1059 {
1060 // Test exclude_java_resources with java_resource_dirs
1061 name: "resource dirs with exclude files",
1062 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
1063 args: "-C java-res -f java-res/a/a",
1064 },
1065 {
1066 // Test exclude_java_resource_dirs with java_resource_dirs
1067 name: "resource dirs with exclude files",
1068 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
1069 args: "-C java-res -f java-res/a/a -f java-res/b/b",
1070 },
Colin Cross0f37af02017-09-27 17:42:05 -07001071 }
1072
1073 for _, test := range table {
1074 t.Run(test.name, func(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001075 ctx, _ := testJavaWithFS(t, `
Colin Cross0f37af02017-09-27 17:42:05 -07001076 java_library {
1077 name: "foo",
1078 srcs: [
1079 "a.java",
1080 "b.java",
1081 "c.java",
1082 ],
1083 `+test.prop+`,
1084 }
Colin Cross238c1f32020-06-07 16:58:18 -07001085 `+test.extra,
1086 map[string][]byte{
1087 "java-res/a/a": nil,
1088 "java-res/b/b": nil,
1089 "java-res2/a": nil,
1090 },
1091 )
Colin Cross0f37af02017-09-27 17:42:05 -07001092
Colin Cross331a1212018-08-15 20:40:52 -07001093 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -07001094 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -07001095
1096 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
1097 t.Errorf("foo combined jars %v does not contain %q",
1098 foo.Inputs.Strings(), fooRes.Output.String())
1099 }
1100
Colin Crossaf9c55b2017-10-03 14:50:08 -07001101 if fooRes.Args["jarArgs"] != test.args {
1102 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -07001103 fooRes.Args["jarArgs"], test.args)
1104 }
1105 })
1106 }
1107}
1108
Colin Cross0c4ce212019-05-03 15:28:19 -07001109func TestIncludeSrcs(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001110 ctx, _ := testJavaWithFS(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -07001111 java_library {
1112 name: "foo",
1113 srcs: [
1114 "a.java",
1115 "b.java",
1116 "c.java",
1117 ],
1118 include_srcs: true,
1119 }
1120
1121 java_library {
1122 name: "bar",
1123 srcs: [
1124 "a.java",
1125 "b.java",
1126 "c.java",
1127 ],
1128 java_resource_dirs: ["java-res"],
1129 include_srcs: true,
1130 }
Colin Cross238c1f32020-06-07 16:58:18 -07001131 `, map[string][]byte{
1132 "java-res/a/a": nil,
1133 "java-res/b/b": nil,
1134 "java-res2/a": nil,
1135 })
Colin Cross0c4ce212019-05-03 15:28:19 -07001136
1137 // Test a library with include_srcs: true
1138 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
1139 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
1140
1141 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
1142 t.Errorf("foo combined jars %v does not contain %q", w, g)
1143 }
1144
1145 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1146 t.Errorf("foo source jar args %q is not %q", w, g)
1147 }
1148
1149 // Test a library with include_srcs: true and resources
1150 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
1151 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
1152 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
1153 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
1154
1155 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1156 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1157 }
1158
1159 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
1160 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
1161 }
1162
1163 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
1164 t.Errorf("bar combined jars %v does not contain %q", w, g)
1165 }
1166
1167 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
1168 t.Errorf("bar source jar args %q is not %q", w, g)
1169 }
1170
1171 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
1172 t.Errorf("bar resource jar args %q is not %q", w, g)
1173 }
1174}
1175
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001176func TestJavaLint(t *testing.T) {
1177 ctx, _ := testJavaWithFS(t, `
1178 java_library {
1179 name: "foo",
1180 srcs: [
1181 "a.java",
1182 "b.java",
1183 "c.java",
1184 ],
1185 min_sdk_version: "29",
1186 sdk_version: "system_current",
1187 }
1188 `, map[string][]byte{
1189 "lint-baseline.xml": nil,
1190 })
1191
1192 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001193
Colin Cross1661aff2021-03-12 17:56:51 -08001194 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1195 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline lint-baseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001196 t.Error("did not pass --baseline flag")
1197 }
1198}
1199
1200func TestJavaLintWithoutBaseline(t *testing.T) {
1201 ctx, _ := testJavaWithFS(t, `
1202 java_library {
1203 name: "foo",
1204 srcs: [
1205 "a.java",
1206 "b.java",
1207 "c.java",
1208 ],
1209 min_sdk_version: "29",
1210 sdk_version: "system_current",
1211 }
1212 `, map[string][]byte{})
1213
1214 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001215
Colin Cross1661aff2021-03-12 17:56:51 -08001216 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1217 if strings.Contains(*sboxProto.Commands[0].Command, "--baseline") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001218 t.Error("passed --baseline flag for non existent file")
1219 }
1220}
1221
1222func TestJavaLintRequiresCustomLintFileToExist(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001223 config := TestConfig(t.TempDir(),
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001224 nil,
1225 `
1226 java_library {
1227 name: "foo",
1228 srcs: [
1229 ],
1230 min_sdk_version: "29",
1231 sdk_version: "system_current",
1232 lint: {
1233 baseline_filename: "mybaseline.xml",
1234 },
1235 }
1236 `, map[string][]byte{
1237 "build/soong/java/lint_defaults.txt": nil,
1238 "prebuilts/cmdline-tools/tools/bin/lint": nil,
1239 "prebuilts/cmdline-tools/tools/lib/lint-classpath.jar": nil,
1240 "framework/aidl": nil,
1241 "a.java": nil,
1242 "AndroidManifest.xml": nil,
1243 "build/make/target/product/security": nil,
1244 })
1245 config.TestAllowNonExistentPaths = false
1246 testJavaErrorWithConfig(t,
1247 "source path \"mybaseline.xml\" does not exist",
1248 config,
1249 )
1250}
1251
1252func TestJavaLintUsesCorrectBpConfig(t *testing.T) {
1253 ctx, _ := testJavaWithFS(t, `
1254 java_library {
1255 name: "foo",
1256 srcs: [
1257 "a.java",
1258 "b.java",
1259 "c.java",
1260 ],
1261 min_sdk_version: "29",
1262 sdk_version: "system_current",
1263 lint: {
1264 error_checks: ["SomeCheck"],
1265 baseline_filename: "mybaseline.xml",
1266 },
1267 }
1268 `, map[string][]byte{
1269 "mybaseline.xml": nil,
1270 })
1271
1272 foo := ctx.ModuleForTests("foo", "android_common")
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001273
Colin Cross1661aff2021-03-12 17:56:51 -08001274 sboxProto := android.RuleBuilderSboxProtoForTests(t, foo.Output("lint.sbox.textproto"))
1275 if !strings.Contains(*sboxProto.Commands[0].Command, "--baseline mybaseline.xml") {
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001276 t.Error("did not use the correct file for baseline")
1277 }
1278}
1279
Colin Cross54190b32017-10-09 15:34:10 -07001280func TestGeneratedSources(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001281 ctx, _ := testJavaWithFS(t, `
Colin Cross54190b32017-10-09 15:34:10 -07001282 java_library {
1283 name: "foo",
1284 srcs: [
1285 "a*.java",
1286 ":gen",
1287 "b*.java",
1288 ],
1289 }
1290
1291 genrule {
1292 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -08001293 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -07001294 out: ["gen.java"],
1295 }
Colin Cross238c1f32020-06-07 16:58:18 -07001296 `, map[string][]byte{
1297 "a.java": nil,
1298 "b.java": nil,
1299 })
Colin Cross54190b32017-10-09 15:34:10 -07001300
1301 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1302 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
1303
Colin Cross15e86d92017-10-20 15:07:08 -07001304 if filepath.Base(genrule.Output.String()) != "gen.java" {
1305 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -07001306 }
1307
1308 if len(javac.Inputs) != 3 ||
1309 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -07001310 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -07001311 javac.Inputs[2].String() != "b.java" {
1312 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
1313 }
1314}
1315
Nan Zhang61eaedb2017-11-02 13:28:15 -07001316func TestTurbine(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001317 result := android.GroupFixturePreparers(
1318 prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})).
Paul Duffin3d9f2682021-03-13 09:47:16 +00001319 RunTestWithBp(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001320 java_library {
1321 name: "foo",
1322 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +09001323 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001324 }
1325
1326 java_library {
1327 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -08001328 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -07001329 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +09001330 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -07001331 }
1332
1333 java_library {
1334 name: "baz",
1335 srcs: ["c.java"],
1336 libs: ["bar"],
1337 sdk_version: "14",
1338 }
1339 `)
1340
Paul Duffin414ea5e2021-03-22 17:31:52 +00001341 fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine").RelativeToTop()
1342 barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine").RelativeToTop()
1343 barJavac := result.ModuleForTests("bar", "android_common").Rule("javac").RelativeToTop()
1344 barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine").RelativeToTop()
1345 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac").RelativeToTop()
Nan Zhang61eaedb2017-11-02 13:28:15 -07001346
Paul Duffin414ea5e2021-03-22 17:31:52 +00001347 android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs)
Nan Zhang61eaedb2017-11-02 13:28:15 -07001348
Paul Duffin414ea5e2021-03-22 17:31:52 +00001349 fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1350 barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar")
Paul Duffin3d9f2682021-03-13 09:47:16 +00001351 android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["classpath"], fooHeaderJar)
1352 android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar)
Paul Duffin414ea5e2021-03-22 17:31:52 +00001353 android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs)
Paul Duffin3d9f2682021-03-13 09:47:16 +00001354 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001355}
1356
1357func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001358 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -07001359 java_library {
1360 name: "bar",
1361 srcs: ["a.java","b.java","c.java"],
1362 javac_shard_size: 1
1363 }
1364 `)
1365
Paul Duffin414ea5e2021-03-22 17:31:52 +00001366 barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -07001367 for i := 0; i < 3; i++ {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001368 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)).RelativeToTop()
Nan Zhang61eaedb2017-11-02 13:28:15 -07001369 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
1370 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
1371 }
1372 }
1373}
1374
Nan Zhang581fd212018-01-10 16:06:12 -08001375func TestDroiddoc(t *testing.T) {
Colin Cross238c1f32020-06-07 16:58:18 -07001376 ctx, _ := testJavaWithFS(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001377 droiddoc_exported_dir {
Dan Willemsencc090972018-02-26 14:33:31 -08001378 name: "droiddoc-templates-sdk",
1379 path: ".",
1380 }
Jiyong Park29074592019-07-07 16:27:47 +09001381 filegroup {
1382 name: "bar-doc-aidl-srcs",
1383 srcs: ["bar-doc/IBar.aidl"],
1384 path: "bar-doc",
1385 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001386 droidstubs {
1387 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001388 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001389 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001390 ],
Steve Kim3666c702020-09-01 17:58:01 +00001391 exclude_srcs: [
1392 "bar-doc/b.java"
1393 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001394 api_levels_annotations_dirs: [
1395 "droiddoc-templates-sdk",
1396 ],
1397 api_levels_annotations_enabled: true,
1398 }
1399 droiddoc {
1400 name: "bar-doc",
1401 srcs: [
1402 ":bar-stubs",
1403 "bar-doc/IFoo.aidl",
1404 ":bar-doc-aidl-srcs",
1405 ],
Dan Willemsencc090972018-02-26 14:33:31 -08001406 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -08001407 hdf: [
1408 "android.whichdoc offline",
1409 ],
1410 knowntags: [
1411 "bar-doc/known_oj_tags.txt",
1412 ],
1413 proofread_file: "libcore-proofread.txt",
1414 todo_file: "libcore-docs-todo.html",
Liz Kammer585cac22020-07-06 09:12:57 -07001415 flags: ["-offlinemode -title \"libcore\""],
Nan Zhang581fd212018-01-10 16:06:12 -08001416 }
Colin Cross238c1f32020-06-07 16:58:18 -07001417 `,
1418 map[string][]byte{
1419 "bar-doc/a.java": nil,
1420 "bar-doc/b.java": nil,
1421 })
Liz Kammere1ab2502020-09-10 15:29:25 +00001422 barStubs := ctx.ModuleForTests("bar-stubs", "android_common")
1423 barStubsOutputs, err := barStubs.Module().(*Droidstubs).OutputFiles("")
1424 if err != nil {
1425 t.Errorf("Unexpected error %q retrieving \"bar-stubs\" output file", err)
1426 }
1427 if len(barStubsOutputs) != 1 {
1428 t.Errorf("Expected one output from \"bar-stubs\" got %s", barStubsOutputs)
Liz Kammer1e2ee122020-07-30 15:07:22 -07001429 }
Nan Zhang581fd212018-01-10 16:06:12 -08001430
Liz Kammere1ab2502020-09-10 15:29:25 +00001431 barStubsOutput := barStubsOutputs[0]
1432 barDoc := ctx.ModuleForTests("bar-doc", "android_common")
Paul Duffin414ea5e2021-03-22 17:31:52 +00001433 javaDoc := barDoc.Rule("javadoc").RelativeToTop()
Liz Kammere1ab2502020-09-10 15:29:25 +00001434 if g, w := javaDoc.Implicits.Strings(), barStubsOutput.String(); !inList(w, g) {
1435 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
Jiyong Park29074592019-07-07 16:27:47 +09001436 }
1437
Paul Duffin414ea5e2021-03-22 17:31:52 +00001438 expected := "-sourcepath out/soong/.intermediates/bar-doc/android_common/srcjars "
Liz Kammere1ab2502020-09-10 15:29:25 +00001439 if !strings.Contains(javaDoc.RuleParams.Command, expected) {
1440 t.Errorf("bar-doc command does not contain flag %q, but should\n%q", expected, javaDoc.RuleParams.Command)
1441 }
1442
1443 aidl := barDoc.Rule("aidl")
1444 if g, w := javaDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) {
Colin Crossc0806172019-06-14 18:51:47 -07001445 t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g)
1446 }
1447
1448 if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) {
1449 t.Errorf("aidl inputs must be %q, but was %q", w, g)
Jiyong Park1e440682018-05-23 18:42:04 +09001450 }
Nan Zhang581fd212018-01-10 16:06:12 -08001451}
1452
Liz Kammer585cac22020-07-06 09:12:57 -07001453func TestDroiddocArgsAndFlagsCausesError(t *testing.T) {
1454 testJavaError(t, "flags is set. Cannot set args", `
1455 droiddoc_exported_dir {
1456 name: "droiddoc-templates-sdk",
1457 path: ".",
1458 }
1459 filegroup {
1460 name: "bar-doc-aidl-srcs",
1461 srcs: ["bar-doc/IBar.aidl"],
1462 path: "bar-doc",
1463 }
Liz Kammere1ab2502020-09-10 15:29:25 +00001464 droidstubs {
1465 name: "bar-stubs",
Liz Kammer3a55c912020-08-14 12:14:02 -07001466 srcs: [
Steve Kim3666c702020-09-01 17:58:01 +00001467 "bar-doc/a.java",
Liz Kammer3a55c912020-08-14 12:14:02 -07001468 ],
Steve Kim3666c702020-09-01 17:58:01 +00001469 exclude_srcs: [
1470 "bar-doc/b.java"
1471 ],
Liz Kammere1ab2502020-09-10 15:29:25 +00001472 api_levels_annotations_dirs: [
1473 "droiddoc-templates-sdk",
1474 ],
1475 api_levels_annotations_enabled: true,
1476 }
1477 droiddoc {
1478 name: "bar-doc",
1479 srcs: [
1480 ":bar-stubs",
1481 "bar-doc/IFoo.aidl",
1482 ":bar-doc-aidl-srcs",
1483 ],
Liz Kammer585cac22020-07-06 09:12:57 -07001484 custom_template: "droiddoc-templates-sdk",
1485 hdf: [
1486 "android.whichdoc offline",
1487 ],
1488 knowntags: [
1489 "bar-doc/known_oj_tags.txt",
1490 ],
1491 proofread_file: "libcore-proofread.txt",
1492 todo_file: "libcore-docs-todo.html",
1493 flags: ["-offlinemode -title \"libcore\""],
1494 args: "-offlinemode -title \"libcore\"",
1495 }
1496 `)
1497}
1498
Liz Kammer3d894b72020-08-04 09:55:13 -07001499func TestDroidstubs(t *testing.T) {
1500 ctx, _ := testJavaWithFS(t, `
1501 droiddoc_exported_dir {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001502 name: "droiddoc-templates-sdk",
1503 path: ".",
Liz Kammer3d894b72020-08-04 09:55:13 -07001504 }
1505
1506 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001507 name: "bar-stubs",
1508 srcs: ["bar-doc/a.java"],
1509 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1510 api_levels_annotations_enabled: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001511 }
1512
1513 droidstubs {
Anton Hansson52ac73d2020-10-26 09:57:40 +00001514 name: "bar-stubs-other",
1515 srcs: ["bar-doc/a.java"],
1516 high_mem: true,
1517 api_levels_annotations_dirs: ["droiddoc-templates-sdk"],
1518 api_levels_annotations_enabled: true,
1519 api_levels_jar_filename: "android.other.jar",
Liz Kammer3d894b72020-08-04 09:55:13 -07001520 }
1521 `,
1522 map[string][]byte{
1523 "bar-doc/a.java": nil,
1524 })
1525 testcases := []struct {
1526 moduleName string
1527 expectedJarFilename string
Anton Hansson52ac73d2020-10-26 09:57:40 +00001528 high_mem bool
Liz Kammer3d894b72020-08-04 09:55:13 -07001529 }{
1530 {
1531 moduleName: "bar-stubs",
1532 expectedJarFilename: "android.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001533 high_mem: false,
Liz Kammer3d894b72020-08-04 09:55:13 -07001534 },
1535 {
1536 moduleName: "bar-stubs-other",
1537 expectedJarFilename: "android.other.jar",
Anton Hansson52ac73d2020-10-26 09:57:40 +00001538 high_mem: true,
Liz Kammer3d894b72020-08-04 09:55:13 -07001539 },
1540 }
1541 for _, c := range testcases {
1542 m := ctx.ModuleForTests(c.moduleName, "android_common")
1543 metalava := m.Rule("metalava")
Anton Hansson52ac73d2020-10-26 09:57:40 +00001544 rp := metalava.RuleParams
Liz Kammer3d894b72020-08-04 09:55:13 -07001545 expected := "--android-jar-pattern ./%/public/" + c.expectedJarFilename
Anton Hansson52ac73d2020-10-26 09:57:40 +00001546 if actual := rp.Command; !strings.Contains(actual, expected) {
Liz Kammer3d894b72020-08-04 09:55:13 -07001547 t.Errorf("For %q, expected metalava argument %q, but was not found %q", c.moduleName, expected, actual)
1548 }
Anton Hansson52ac73d2020-10-26 09:57:40 +00001549
1550 if actual := rp.Pool != nil && strings.Contains(rp.Pool.String(), "highmem"); actual != c.high_mem {
1551 t.Errorf("Expected %q high_mem to be %v, was %v", c.moduleName, c.high_mem, actual)
1552 }
Liz Kammer3d894b72020-08-04 09:55:13 -07001553 }
1554}
1555
Paul Duffin83a2d962019-11-19 19:44:10 +00001556func TestDroidstubsWithSystemModules(t *testing.T) {
1557 ctx, _ := testJava(t, `
1558 droidstubs {
1559 name: "stubs-source-system-modules",
1560 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001561 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001562 ],
1563 sdk_version: "none",
1564 system_modules: "source-system-modules",
1565 }
1566
1567 java_library {
1568 name: "source-jar",
1569 srcs: [
1570 "a.java",
1571 ],
1572 }
1573
1574 java_system_modules {
1575 name: "source-system-modules",
1576 libs: ["source-jar"],
1577 }
1578
1579 droidstubs {
1580 name: "stubs-prebuilt-system-modules",
1581 srcs: [
Colin Cross238c1f32020-06-07 16:58:18 -07001582 "bar-doc/a.java",
Paul Duffin83a2d962019-11-19 19:44:10 +00001583 ],
1584 sdk_version: "none",
1585 system_modules: "prebuilt-system-modules",
1586 }
1587
1588 java_import {
1589 name: "prebuilt-jar",
1590 jars: ["a.jar"],
1591 }
1592
1593 java_system_modules_import {
1594 name: "prebuilt-system-modules",
1595 libs: ["prebuilt-jar"],
1596 }
1597 `)
1598
1599 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar")
1600
1601 checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar")
1602}
1603
1604func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) {
1605 metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava")
1606 var systemJars []string
1607 for _, i := range metalavaRule.Implicits {
1608 systemJars = append(systemJars, i.Base())
1609 }
Ramy Medhatc7965cd2020-04-30 03:08:37 -04001610 if len(systemJars) < 1 || systemJars[0] != systemJar {
Paul Duffin83a2d962019-11-19 19:44:10 +00001611 t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars)
1612 }
1613}
1614
Colin Cross54250902017-12-05 09:28:08 -08001615func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001616 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -08001617 java_library {
1618 name: "foo",
1619 srcs: ["a.java"],
1620 }
1621
1622 java_genrule {
1623 name: "jargen",
1624 tool_files: ["b.java"],
1625 cmd: "$(location b.java) $(in) $(out)",
1626 out: ["jargen.jar"],
1627 srcs: [":foo"],
1628 }
1629
1630 java_library {
1631 name: "bar",
1632 static_libs: ["jargen"],
1633 srcs: ["c.java"],
1634 }
1635
1636 java_library {
1637 name: "baz",
1638 libs: ["jargen"],
1639 srcs: ["c.java"],
1640 }
1641 `)
1642
1643 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
1644 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
1645 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
1646 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
1647 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
1648
Colin Cross3d680512020-11-13 16:23:53 -08001649 if g, w := jargen.Implicits.Strings(), foo.Output.String(); !android.InList(w, g) {
1650 t.Errorf("expected jargen inputs [%q], got %q", w, g)
Colin Cross54250902017-12-05 09:28:08 -08001651 }
1652
1653 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
1654 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
1655 }
1656
1657 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
1658 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
1659 }
1660
1661 if len(barCombined.Inputs) != 2 ||
1662 barCombined.Inputs[0].String() != bar.Output.String() ||
1663 barCombined.Inputs[1].String() != jargen.Output.String() {
1664 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
1665 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
1666 }
1667}
1668
Nan Zhang27e284d2018-02-09 21:03:53 +00001669func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001670 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +00001671 java_library {
1672 name: "foo",
1673 srcs: ["a.java", ":foo-srcs"],
1674 exclude_srcs: ["a.java", ":foo-excludes"],
1675 }
1676
1677 filegroup {
1678 name: "foo-srcs",
1679 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
1680 }
1681
1682 filegroup {
1683 name: "foo-excludes",
1684 srcs: ["java-fg/a.java", "java-fg/b.java"],
1685 }
1686 `)
1687
1688 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
1689
1690 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
1691 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
1692 }
1693}
Jiyong Parkc678ad32018-04-10 13:07:10 +09001694
Paul Duffin52d398a2019-06-11 12:31:14 +01001695func TestJavaLibrary(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001696 testJavaWithFS(t, "", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +01001697 "libcore/Android.bp": []byte(`
1698 java_library {
1699 name: "core",
1700 sdk_version: "none",
1701 system_modules: "none",
Paul Duffinaa55f742020-10-06 17:20:13 +01001702 }
1703
1704 filegroup {
1705 name: "core-jar",
1706 srcs: [":core{.jar}"],
1707 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001708 `),
Paul Duffinaa55f742020-10-06 17:20:13 +01001709 })
Paul Duffinaa55f742020-10-06 17:20:13 +01001710}
1711
1712func TestJavaImport(t *testing.T) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00001713 testJavaWithFS(t, "", map[string][]byte{
Paul Duffinaa55f742020-10-06 17:20:13 +01001714 "libcore/Android.bp": []byte(`
1715 java_import {
1716 name: "core",
1717 sdk_version: "none",
1718 }
1719
1720 filegroup {
1721 name: "core-jar",
1722 srcs: [":core{.jar}"],
1723 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00001724 `),
Paul Duffin52d398a2019-06-11 12:31:14 +01001725 })
Paul Duffin52d398a2019-06-11 12:31:14 +01001726}
1727
Jiyong Parkc678ad32018-04-10 13:07:10 +09001728func TestJavaSdkLibrary(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001729 result := android.GroupFixturePreparers(
1730 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001731 PrepareForTestWithJavaSdkLibraryFiles,
1732 FixtureWithPrebuiltApis(map[string][]string{
1733 "28": {"foo"},
1734 "29": {"foo"},
1735 "30": {"bar", "barney", "baz", "betty", "foo", "fred", "quuz", "wilma"},
1736 }),
1737 ).RunTestWithBp(t, `
Paul Duffin884363e2019-12-19 10:21:09 +00001738 droiddoc_exported_dir {
Jiyong Parkc678ad32018-04-10 13:07:10 +09001739 name: "droiddoc-templates-sdk",
1740 path: ".",
1741 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001742 java_sdk_library {
1743 name: "foo",
1744 srcs: ["a.java", "b.java"],
1745 api_packages: ["foo"],
1746 }
1747 java_sdk_library {
1748 name: "bar",
1749 srcs: ["a.java", "b.java"],
1750 api_packages: ["bar"],
1751 }
1752 java_library {
1753 name: "baz",
1754 srcs: ["c.java"],
Paul Duffin859fe962020-05-15 10:20:31 +01001755 libs: ["foo", "bar.stubs"],
Jiyong Parkc678ad32018-04-10 13:07:10 +09001756 sdk_version: "system_current",
1757 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001758 java_sdk_library {
1759 name: "barney",
1760 srcs: ["c.java"],
1761 api_only: true,
1762 }
1763 java_sdk_library {
1764 name: "betty",
1765 srcs: ["c.java"],
1766 shared_library: false,
1767 }
Paul Duffin859fe962020-05-15 10:20:31 +01001768 java_sdk_library_import {
1769 name: "quuz",
1770 public: {
1771 jars: ["c.jar"],
1772 },
1773 }
1774 java_sdk_library_import {
1775 name: "fred",
1776 public: {
1777 jars: ["b.jar"],
1778 },
1779 }
Paul Duffindfa131e2020-05-15 20:37:11 +01001780 java_sdk_library_import {
1781 name: "wilma",
1782 public: {
1783 jars: ["b.jar"],
1784 },
1785 shared_library: false,
1786 }
Jiyong Park1be96912018-05-28 18:02:19 +09001787 java_library {
1788 name: "qux",
1789 srcs: ["c.java"],
Paul Duffindfa131e2020-05-15 20:37:11 +01001790 libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"],
Jiyong Park1be96912018-05-28 18:02:19 +09001791 sdk_version: "system_current",
1792 }
Paul Duffin726d23c2020-01-22 16:30:37 +00001793 java_library {
1794 name: "baz-test",
1795 srcs: ["c.java"],
1796 libs: ["foo"],
1797 sdk_version: "test_current",
1798 }
Paul Duffina2db18f2020-01-22 17:11:15 +00001799 java_library {
1800 name: "baz-29",
1801 srcs: ["c.java"],
1802 libs: ["foo"],
1803 sdk_version: "system_29",
1804 }
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001805 java_library {
1806 name: "baz-module-30",
1807 srcs: ["c.java"],
1808 libs: ["foo"],
1809 sdk_version: "module_30",
1810 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001811 `)
1812
1813 // check the existence of the internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +00001814 result.ModuleForTests("foo", "android_common")
1815 result.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common")
1816 result.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common")
1817 result.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common")
1818 result.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common")
1819 result.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common")
1820 result.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common")
1821 result.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common")
1822 result.ModuleForTests("foo.api.public.28", "")
1823 result.ModuleForTests("foo.api.system.28", "")
1824 result.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001825
Paul Duffin22b77cd2021-03-12 19:15:01 +00001826 bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001827 // tests if baz is actually linked to the stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001828 android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.system.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001829 // ... and not to the impl lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001830 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +09001831 // test if baz is not linked to the system variant of foo
Paul Duffin22b77cd2021-03-12 19:15:01 +00001832 android.AssertStringDoesNotContain(t, "baz javac classpath", bazJavac.Args["classpath"], "foo.stubs.jar")
Jiyong Park1be96912018-05-28 18:02:19 +09001833
Paul Duffin22b77cd2021-03-12 19:15:01 +00001834 bazTestJavac := result.ModuleForTests("baz-test", "android_common").Rule("javac")
Paul Duffin726d23c2020-01-22 16:30:37 +00001835 // tests if baz-test is actually linked to the test stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001836 android.AssertStringDoesContain(t, "baz-test javac classpath", bazTestJavac.Args["classpath"], "foo.stubs.test.jar")
Paul Duffin726d23c2020-01-22 16:30:37 +00001837
Paul Duffin22b77cd2021-03-12 19:15:01 +00001838 baz29Javac := result.ModuleForTests("baz-29", "android_common").Rule("javac")
Paul Duffina2db18f2020-01-22 17:11:15 +00001839 // tests if baz-29 is actually linked to the system 29 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001840 android.AssertStringDoesContain(t, "baz-29 javac classpath", baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar")
Paul Duffina2db18f2020-01-22 17:11:15 +00001841
Paul Duffin22b77cd2021-03-12 19:15:01 +00001842 bazModule30Javac := result.ModuleForTests("baz-module-30", "android_common").Rule("javac")
Paul Duffinfb6ae5b2020-09-30 15:17:25 +01001843 // tests if "baz-module-30" is actually linked to the module 30 stubs lib
Paul Duffin22b77cd2021-03-12 19:15:01 +00001844 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 +01001845
Jiyong Park1be96912018-05-28 18:02:19 +09001846 // test if baz has exported SDK lib names foo and bar to qux
Paul Duffin22b77cd2021-03-12 19:15:01 +00001847 qux := result.ModuleForTests("qux", "android_common")
Jiyong Park1be96912018-05-28 18:02:19 +09001848 if quxLib, ok := qux.Module().(*Library); ok {
Ulya Trafimovichb23d28c2020-10-08 12:53:58 +01001849 sdkLibs := quxLib.ClassLoaderContexts().UsesLibs()
Paul Duffin22b77cd2021-03-12 19:15:01 +00001850 android.AssertDeepEquals(t, "qux exports", []string{"foo", "bar", "fred", "quuz"}, sdkLibs)
Jiyong Park1be96912018-05-28 18:02:19 +09001851 }
Jiyong Parkc678ad32018-04-10 13:07:10 +09001852}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02001853
Anton Hansson7f66efa2020-10-08 14:47:23 +01001854func TestJavaSdkLibrary_StubOrImplOnlyLibs(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001855 result := android.GroupFixturePreparers(
1856 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001857 PrepareForTestWithJavaSdkLibraryFiles,
1858 FixtureWithLastReleaseApis("sdklib"),
1859 ).RunTestWithBp(t, `
Anton Hansson7f66efa2020-10-08 14:47:23 +01001860 java_sdk_library {
Anton Hanssondff2c782020-12-21 17:10:01 +00001861 name: "sdklib",
Anton Hansson7f66efa2020-10-08 14:47:23 +01001862 srcs: ["a.java"],
1863 impl_only_libs: ["foo"],
1864 stub_only_libs: ["bar"],
1865 }
1866 java_library {
1867 name: "foo",
1868 srcs: ["a.java"],
1869 sdk_version: "current",
1870 }
1871 java_library {
1872 name: "bar",
1873 srcs: ["a.java"],
1874 sdk_version: "current",
1875 }
1876 `)
1877
Anton Hanssondff2c782020-12-21 17:10:01 +00001878 for _, implName := range []string{"sdklib", "sdklib.impl"} {
Paul Duffin22b77cd2021-03-12 19:15:01 +00001879 implJavacCp := result.ModuleForTests(implName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001880 if !strings.Contains(implJavacCp, "/foo.jar") || strings.Contains(implJavacCp, "/bar.jar") {
1881 t.Errorf("%v javac classpath %v does not contain foo and not bar", implName, implJavacCp)
1882 }
1883 }
Anton Hanssondff2c782020-12-21 17:10:01 +00001884 stubName := apiScopePublic.stubsLibraryModuleName("sdklib")
Paul Duffin22b77cd2021-03-12 19:15:01 +00001885 stubsJavacCp := result.ModuleForTests(stubName, "android_common").Rule("javac").Args["classpath"]
Anton Hansson7f66efa2020-10-08 14:47:23 +01001886 if strings.Contains(stubsJavacCp, "/foo.jar") || !strings.Contains(stubsJavacCp, "/bar.jar") {
1887 t.Errorf("stubs javac classpath %v does not contain bar and not foo", stubsJavacCp)
1888 }
1889}
1890
Paul Duffindaaa3322020-05-26 18:13:57 +01001891func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001892 result := android.GroupFixturePreparers(
1893 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001894 PrepareForTestWithJavaSdkLibraryFiles,
1895 FixtureWithLastReleaseApis("foo"),
1896 ).RunTestWithBp(t, `
Paul Duffindaaa3322020-05-26 18:13:57 +01001897 java_sdk_library {
1898 name: "foo",
1899 srcs: ["a.java"],
1900 api_only: true,
1901 public: {
1902 enabled: true,
1903 },
1904 }
1905
1906 java_library {
1907 name: "bar",
1908 srcs: ["b.java"],
1909 libs: ["foo"],
1910 }
1911 `)
1912
1913 // The bar library should depend on the stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00001914 barLibrary := result.ModuleForTests("bar", "android_common").Rule("javac")
Paul Duffindaaa3322020-05-26 18:13:57 +01001915 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
1916 t.Errorf("expected %q, found %#q", expected, actual)
1917 }
1918}
1919
Paul Duffin46dc45a2020-05-14 15:39:10 +01001920func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001921 android.GroupFixturePreparers(
1922 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001923 PrepareForTestWithJavaSdkLibraryFiles,
1924 FixtureWithLastReleaseApis("foo"),
1925 ).RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001926 java_sdk_library {
1927 name: "foo",
1928 srcs: ["a.java"],
1929 api_packages: ["foo"],
1930 public: {
1931 enabled: true,
1932 },
1933 }
1934
1935 java_library {
1936 name: "bar",
1937 srcs: ["b.java", ":foo{.public.stubs.source}"],
1938 }
1939 `)
1940}
1941
1942func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001943 android.GroupFixturePreparers(
1944 prepareForJavaTest,
1945 PrepareForTestWithJavaSdkLibraryFiles,
1946 FixtureWithLastReleaseApis("foo"),
1947 ).
Paul Duffin22b77cd2021-03-12 19:15:01 +00001948 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"foo" does not provide api scope system`)).
1949 RunTestWithBp(t, `
Paul Duffin46dc45a2020-05-14 15:39:10 +01001950 java_sdk_library {
1951 name: "foo",
1952 srcs: ["a.java"],
1953 api_packages: ["foo"],
1954 public: {
1955 enabled: true,
1956 },
1957 }
1958
1959 java_library {
1960 name: "bar",
1961 srcs: ["b.java", ":foo{.system.stubs.source}"],
1962 }
1963 `)
1964}
1965
Paul Duffinca8d9a52020-06-26 22:20:25 +01001966func TestJavaSdkLibrary_Deps(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00001967 result := android.GroupFixturePreparers(
1968 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00001969 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 Duffin71ae5942021-03-22 15:36:52 +00001993 prepareForJavaTest.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 Duffin71ae5942021-03-22 15:36:52 +00002026 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002027 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 Duffin71ae5942021-03-22 15:36:52 +00002041 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002042 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 Duffin71ae5942021-03-22 15:36:52 +00002055 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002056 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 Duffin71ae5942021-03-22 15:36:52 +00002070 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002071 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 Duffin71ae5942021-03-22 15:36:52 +00002090 android.GroupFixturePreparers(
2091 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002092 PrepareForTestWithJavaSdkLibraryFiles,
2093 FixtureWithLastReleaseApis("foo"),
2094 ).RunTestWithBp(t, `
Paul Duffin87a05a32020-05-12 11:50:28 +01002095 java_sdk_library {
2096 name: "foo",
2097 srcs: ["a.java", "b.java"],
2098 api_packages: ["foo"],
2099 system: {
2100 enabled: true,
2101 sdk_version: "module_current",
2102 },
2103 }
2104 `)
2105}
2106
Paul Duffin0c5bae52020-06-02 13:00:08 +01002107func TestJavaSdkLibrary_ModuleLib(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002108 android.GroupFixturePreparers(
2109 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002110 PrepareForTestWithJavaSdkLibraryFiles,
2111 FixtureWithLastReleaseApis("foo"),
2112 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002113 java_sdk_library {
2114 name: "foo",
2115 srcs: ["a.java", "b.java"],
2116 api_packages: ["foo"],
2117 system: {
2118 enabled: true,
2119 },
2120 module_lib: {
2121 enabled: true,
2122 },
2123 }
2124 `)
2125}
2126
2127func TestJavaSdkLibrary_SystemServer(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002128 android.GroupFixturePreparers(
2129 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002130 PrepareForTestWithJavaSdkLibraryFiles,
2131 FixtureWithLastReleaseApis("foo"),
2132 ).RunTestWithBp(t, `
Paul Duffin0c5bae52020-06-02 13:00:08 +01002133 java_sdk_library {
2134 name: "foo",
2135 srcs: ["a.java", "b.java"],
2136 api_packages: ["foo"],
2137 system: {
2138 enabled: true,
2139 },
2140 system_server: {
2141 enabled: true,
2142 },
2143 }
2144 `)
2145}
2146
Paul Duffin803a9562020-05-20 11:52:25 +01002147func TestJavaSdkLibrary_MissingScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002148 prepareForJavaTest.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002149 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`requires api scope module-lib from foo but it only has \[\] available`)).
2150 RunTestWithBp(t, `
2151 java_sdk_library {
2152 name: "foo",
2153 srcs: ["a.java"],
2154 public: {
2155 enabled: false,
2156 },
2157 }
Paul Duffin803a9562020-05-20 11:52:25 +01002158
Paul Duffin22b77cd2021-03-12 19:15:01 +00002159 java_library {
2160 name: "baz",
2161 srcs: ["a.java"],
2162 libs: ["foo"],
2163 sdk_version: "module_current",
2164 }
Paul Duffin803a9562020-05-20 11:52:25 +01002165 `)
2166}
2167
2168func TestJavaSdkLibrary_FallbackScope(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002169 android.GroupFixturePreparers(
2170 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002171 PrepareForTestWithJavaSdkLibraryFiles,
2172 FixtureWithLastReleaseApis("foo"),
2173 ).RunTestWithBp(t, `
Paul Duffin803a9562020-05-20 11:52:25 +01002174 java_sdk_library {
2175 name: "foo",
2176 srcs: ["a.java"],
2177 system: {
2178 enabled: true,
2179 },
2180 }
2181
2182 java_library {
2183 name: "baz",
2184 srcs: ["a.java"],
2185 libs: ["foo"],
2186 // foo does not have module-lib scope so it should fallback to system
2187 sdk_version: "module_current",
2188 }
2189 `)
2190}
2191
Jiyong Park932cdfe2020-05-28 00:19:53 +09002192func TestJavaSdkLibrary_DefaultToStubs(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +00002193 result := android.GroupFixturePreparers(
2194 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +00002195 PrepareForTestWithJavaSdkLibraryFiles,
2196 FixtureWithLastReleaseApis("foo"),
2197 ).RunTestWithBp(t, `
Jiyong Park932cdfe2020-05-28 00:19:53 +09002198 java_sdk_library {
2199 name: "foo",
2200 srcs: ["a.java"],
2201 system: {
2202 enabled: true,
2203 },
2204 default_to_stubs: true,
2205 }
2206
2207 java_library {
2208 name: "baz",
2209 srcs: ["a.java"],
2210 libs: ["foo"],
2211 // does not have sdk_version set, should fallback to module,
2212 // which will then fallback to system because the module scope
2213 // is not enabled.
2214 }
2215 `)
2216 // The baz library should depend on the system stubs jar.
Paul Duffin22b77cd2021-03-12 19:15:01 +00002217 bazLibrary := result.ModuleForTests("baz", "android_common").Rule("javac")
Jiyong Park932cdfe2020-05-28 00:19:53 +09002218 if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs.system\.jar$`, bazLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) {
2219 t.Errorf("expected %q, found %#q", expected, actual)
2220 }
2221}
2222
Zoran Jovanovic8736ce22018-08-21 17:10:29 +02002223var compilerFlagsTestCases = []struct {
2224 in string
2225 out bool
2226}{
2227 {
2228 in: "a",
2229 out: false,
2230 },
2231 {
2232 in: "-a",
2233 out: true,
2234 },
2235 {
2236 in: "-no-jdk",
2237 out: false,
2238 },
2239 {
2240 in: "-no-stdlib",
2241 out: false,
2242 },
2243 {
2244 in: "-kotlin-home",
2245 out: false,
2246 },
2247 {
2248 in: "-kotlin-home /some/path",
2249 out: false,
2250 },
2251 {
2252 in: "-include-runtime",
2253 out: false,
2254 },
2255 {
2256 in: "-Xintellij-plugin-root",
2257 out: false,
2258 },
2259}
2260
2261type mockContext struct {
2262 android.ModuleContext
2263 result bool
2264}
2265
2266func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2267 // CheckBadCompilerFlags calls this function when the flag should be rejected
2268 ctx.result = false
2269}
2270
2271func TestCompilerFlags(t *testing.T) {
2272 for _, testCase := range compilerFlagsTestCases {
2273 ctx := &mockContext{result: true}
2274 CheckKotlincFlags(ctx, []string{testCase.in})
2275 if ctx.result != testCase.out {
2276 t.Errorf("incorrect output:")
2277 t.Errorf(" input: %#v", testCase.in)
2278 t.Errorf(" expected: %#v", testCase.out)
2279 t.Errorf(" got: %#v", ctx.result)
2280 }
2281 }
2282}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002283
2284// TODO(jungjw): Consider making this more robust by ignoring path order.
2285func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
Paul Duffin414ea5e2021-03-22 17:31:52 +00002286 variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop()
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002287 flags := strings.Split(variables["javacFlags"], " ")
2288 got := ""
2289 for _, flag := range flags {
2290 keyEnd := strings.Index(flag, "=")
2291 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
2292 got = flag[keyEnd+1:]
2293 break
2294 }
2295 }
Paul Duffin414ea5e2021-03-22 17:31:52 +00002296 if expected != android.StringPathRelativeToTop(ctx.Config().BuildDir(), got) {
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002297 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
2298 }
2299}
2300
2301func TestPatchModule(t *testing.T) {
Pete Gillin0c2143e2019-05-02 15:32:11 +01002302 t.Run("Java language level 8", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002303 // Test with legacy javac -source 1.8 -target 1.8
Pete Gillinbdf5d712019-10-21 14:29:58 +01002304 bp := `
2305 java_library {
2306 name: "foo",
2307 srcs: ["a.java"],
2308 java_version: "1.8",
2309 }
2310
2311 java_library {
2312 name: "bar",
2313 srcs: ["b.java"],
2314 sdk_version: "none",
2315 system_modules: "none",
2316 patch_module: "java.base",
2317 java_version: "1.8",
2318 }
2319
2320 java_library {
2321 name: "baz",
2322 srcs: ["c.java"],
2323 patch_module: "java.base",
2324 java_version: "1.8",
2325 }
2326 `
2327 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002328
2329 checkPatchModuleFlag(t, ctx, "foo", "")
2330 checkPatchModuleFlag(t, ctx, "bar", "")
2331 checkPatchModuleFlag(t, ctx, "baz", "")
2332 })
2333
Pete Gillin0c2143e2019-05-02 15:32:11 +01002334 t.Run("Java language level 9", func(t *testing.T) {
Pete Gillin1b3370f2019-10-01 13:57:31 +01002335 // Test with default javac -source 9 -target 9
Pete Gillinbdf5d712019-10-21 14:29:58 +01002336 bp := `
2337 java_library {
2338 name: "foo",
2339 srcs: ["a.java"],
2340 }
2341
2342 java_library {
2343 name: "bar",
2344 srcs: ["b.java"],
2345 sdk_version: "none",
2346 system_modules: "none",
2347 patch_module: "java.base",
2348 }
2349
2350 java_library {
2351 name: "baz",
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002352 srcs: [
2353 "c.java",
2354 // Tests for b/150878007
2355 "dir/d.java",
2356 "dir2/e.java",
2357 "dir2/f.java",
2358 "nested/dir/g.java"
2359 ],
Pete Gillinbdf5d712019-10-21 14:29:58 +01002360 patch_module: "java.base",
2361 }
2362 `
Pete Gillin1b3370f2019-10-01 13:57:31 +01002363 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002364
2365 checkPatchModuleFlag(t, ctx, "foo", "")
Paul Duffin414ea5e2021-03-22 17:31:52 +00002366 expected := "java.base=.:out/soong"
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002367 checkPatchModuleFlag(t, ctx, "bar", expected)
Jingwen Chen5136a6e2020-10-30 01:01:35 -04002368 expected = "java.base=" + strings.Join([]string{
Paul Duffin414ea5e2021-03-22 17:31:52 +00002369 ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":")
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08002370 checkPatchModuleFlag(t, ctx, "baz", expected)
2371 })
2372}
Paul Duffina7b9f422020-01-10 17:12:18 +00002373
Paul Duffin83a2d962019-11-19 19:44:10 +00002374func TestJavaLibraryWithSystemModules(t *testing.T) {
2375 ctx, _ := testJava(t, `
2376 java_library {
2377 name: "lib-with-source-system-modules",
2378 srcs: [
2379 "a.java",
2380 ],
2381 sdk_version: "none",
2382 system_modules: "source-system-modules",
2383 }
2384
2385 java_library {
2386 name: "source-jar",
2387 srcs: [
2388 "a.java",
2389 ],
2390 }
2391
2392 java_system_modules {
2393 name: "source-system-modules",
2394 libs: ["source-jar"],
2395 }
2396
2397 java_library {
2398 name: "lib-with-prebuilt-system-modules",
2399 srcs: [
2400 "a.java",
2401 ],
2402 sdk_version: "none",
2403 system_modules: "prebuilt-system-modules",
2404 }
2405
2406 java_import {
2407 name: "prebuilt-jar",
2408 jars: ["a.jar"],
2409 }
2410
2411 java_system_modules_import {
2412 name: "prebuilt-system-modules",
2413 libs: ["prebuilt-jar"],
2414 }
2415 `)
2416
2417 checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar")
2418
2419 checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar")
2420}
2421
2422func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) {
2423 javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac")
2424 bootClasspath := javacRule.Args["bootClasspath"]
2425 if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) {
2426 t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath)
2427 }
2428}
Jiyong Park19604de2020-03-24 16:44:11 +09002429
2430func TestAidlExportIncludeDirsFromImports(t *testing.T) {
2431 ctx, _ := testJava(t, `
2432 java_library {
2433 name: "foo",
2434 srcs: ["aidl/foo/IFoo.aidl"],
2435 libs: ["bar"],
2436 }
2437
2438 java_import {
2439 name: "bar",
2440 jars: ["a.jar"],
2441 aidl: {
2442 export_include_dirs: ["aidl/bar"],
2443 },
2444 }
2445 `)
2446
2447 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2448 expectedAidlFlag := "-Iaidl/bar"
2449 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2450 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2451 }
2452}
Liz Kammerdd849a82020-06-12 16:38:45 -07002453
Jooyung Hane197d8b2021-01-05 10:33:16 +09002454func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) {
2455 ctx, _ := testJava(t, `
2456 java_library {
2457 name: "foo",
2458 srcs: ["aidl/foo/IFoo.aidl"],
2459 aidl: { flags: ["-Werror"], },
2460 }
2461 `)
2462
2463 aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command
2464 expectedAidlFlag := "-Werror"
2465 if !strings.Contains(aidlCommand, expectedAidlFlag) {
2466 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
2467 }
2468}
2469
Liz Kammerdd849a82020-06-12 16:38:45 -07002470func TestDataNativeBinaries(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -07002471 ctx, _ := testJava(t, `
Liz Kammerdd849a82020-06-12 16:38:45 -07002472 java_test_host {
2473 name: "foo",
2474 srcs: ["a.java"],
2475 data_native_bins: ["bin"]
2476 }
2477
2478 python_binary_host {
2479 name: "bin",
2480 srcs: ["bin.py"],
2481 }
2482 `)
2483
2484 buildOS := android.BuildOs.String()
2485
2486 test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
Colin Crossaa255532020-07-03 13:18:24 -07002487 entries := android.AndroidMkEntriesForTest(t, ctx, test)[0]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002488 expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64_PY3/bin:bin"}
Liz Kammerdd849a82020-06-12 16:38:45 -07002489 actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
Paul Duffin414ea5e2021-03-22 17:31:52 +00002490 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual)
Liz Kammerdd849a82020-06-12 16:38:45 -07002491}
Yuexi Ma627263f2021-03-04 13:47:56 -08002492
2493func TestDefaultInstallable(t *testing.T) {
2494 ctx, _ := testJava(t, `
2495 java_test_host {
2496 name: "foo"
2497 }
2498 `)
2499
2500 buildOS := android.BuildOs.String()
2501 module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost)
2502 assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true),
2503 module.properties.Installable)
2504}