blob: 998c76a3204c7993901ac9b4511565f353e25d5b [file] [log] [blame]
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001// 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 (
Colin Crossd09b0b62018-04-18 11:06:47 -070018 "fmt"
Colin Crossa4f08812018-10-02 22:03:40 -070019 "path/filepath"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080020 "reflect"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070021 "regexp"
Colin Crossb69301e2017-12-01 10:48:26 -080022 "sort"
Colin Crossd09b0b62018-04-18 11:06:47 -070023 "strings"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080024 "testing"
Jaewoong Junga5e5abc2019-04-26 14:31:50 -070025
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080030)
31
32var (
33 resourceFiles = []string{
34 "res/layout/layout.xml",
35 "res/values/strings.xml",
36 "res/values-en-rUS/strings.xml",
37 }
38
39 compiledResourceFiles = []string{
40 "aapt2/res/layout_layout.xml.flat",
41 "aapt2/res/values_strings.arsc.flat",
42 "aapt2/res/values-en-rUS_strings.arsc.flat",
43 }
44)
45
Colin Cross98be1bb2019-12-13 20:41:13 -080046func testAppConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Colin Cross527012a2017-11-30 22:56:16 -080047 appFS := map[string][]byte{}
48 for k, v := range fs {
49 appFS[k] = v
Colin Cross3bc7ffa2017-11-22 16:19:37 -080050 }
51
Colin Cross527012a2017-11-30 22:56:16 -080052 for _, file := range resourceFiles {
53 appFS[file] = nil
54 }
55
Colin Cross98be1bb2019-12-13 20:41:13 -080056 return testConfig(env, bp, appFS)
Colin Cross527012a2017-11-30 22:56:16 -080057}
58
59func testApp(t *testing.T, bp string) *android.TestContext {
Colin Cross98be1bb2019-12-13 20:41:13 -080060 config := testAppConfig(nil, bp, nil)
Colin Cross527012a2017-11-30 22:56:16 -080061
Colin Cross98be1bb2019-12-13 20:41:13 -080062 ctx := testContext()
Colin Cross527012a2017-11-30 22:56:16 -080063
64 run(t, ctx, config)
65
66 return ctx
Colin Cross3bc7ffa2017-11-22 16:19:37 -080067}
68
69func TestApp(t *testing.T) {
Colin Crossa97c5d32018-03-28 14:58:31 -070070 for _, moduleType := range []string{"android_app", "android_library"} {
71 t.Run(moduleType, func(t *testing.T) {
72 ctx := testApp(t, moduleType+` {
73 name: "foo",
74 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +090075 sdk_version: "current"
Colin Crossa97c5d32018-03-28 14:58:31 -070076 }
77 `)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080078
Colin Crossa97c5d32018-03-28 14:58:31 -070079 foo := ctx.ModuleForTests("foo", "android_common")
Colin Cross3bc7ffa2017-11-22 16:19:37 -080080
Colin Cross31656952018-05-24 16:11:20 -070081 var expectedLinkImplicits []string
82
83 manifestFixer := foo.Output("manifest_fixer/AndroidManifest.xml")
84 expectedLinkImplicits = append(expectedLinkImplicits, manifestFixer.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080085
Colin Crossa97c5d32018-03-28 14:58:31 -070086 frameworkRes := ctx.ModuleForTests("framework-res", "android_common")
87 expectedLinkImplicits = append(expectedLinkImplicits,
88 frameworkRes.Output("package-res.apk").Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080089
Colin Crossa97c5d32018-03-28 14:58:31 -070090 // Test the mapping from input files to compiled output file names
91 compile := foo.Output(compiledResourceFiles[0])
92 if !reflect.DeepEqual(resourceFiles, compile.Inputs.Strings()) {
93 t.Errorf("expected aapt2 compile inputs expected:\n %#v\n got:\n %#v",
94 resourceFiles, compile.Inputs.Strings())
95 }
Colin Crossb69301e2017-12-01 10:48:26 -080096
Colin Crossa97c5d32018-03-28 14:58:31 -070097 compiledResourceOutputs := compile.Outputs.Strings()
98 sort.Strings(compiledResourceOutputs)
Colin Crossb69301e2017-12-01 10:48:26 -080099
Colin Crossa97c5d32018-03-28 14:58:31 -0700100 expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800101
Colin Crossa97c5d32018-03-28 14:58:31 -0700102 list := foo.Output("aapt2/res.list")
103 expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800104
Colin Crossa97c5d32018-03-28 14:58:31 -0700105 // Check that the link rule uses
106 res := ctx.ModuleForTests("foo", "android_common").Output("package-res.apk")
107 if !reflect.DeepEqual(expectedLinkImplicits, res.Implicits.Strings()) {
108 t.Errorf("expected aapt2 link implicits expected:\n %#v\n got:\n %#v",
109 expectedLinkImplicits, res.Implicits.Strings())
110 }
111 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800112 }
113}
Colin Cross890ff552017-11-30 20:13:19 -0800114
Colin Crosse560c4a2019-03-19 16:03:11 -0700115func TestAppSplits(t *testing.T) {
116 ctx := testApp(t, `
117 android_app {
118 name: "foo",
119 srcs: ["a.java"],
120 package_splits: ["v4", "v7,hdpi"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900121 sdk_version: "current"
Colin Crosse560c4a2019-03-19 16:03:11 -0700122 }`)
123
124 foo := ctx.ModuleForTests("foo", "android_common")
125
126 expectedOutputs := []string{
127 filepath.Join(buildDir, ".intermediates/foo/android_common/foo.apk"),
128 filepath.Join(buildDir, ".intermediates/foo/android_common/foo_v4.apk"),
129 filepath.Join(buildDir, ".intermediates/foo/android_common/foo_v7_hdpi.apk"),
130 }
131 for _, expectedOutput := range expectedOutputs {
132 foo.Output(expectedOutput)
133 }
134
Colin Cross41955e82019-05-29 14:40:35 -0700135 outputFiles, err := foo.Module().(*AndroidApp).OutputFiles("")
136 if err != nil {
137 t.Fatal(err)
138 }
139 if g, w := outputFiles.Strings(), expectedOutputs; !reflect.DeepEqual(g, w) {
140 t.Errorf(`want OutputFiles("") = %q, got %q`, w, g)
Colin Crosse560c4a2019-03-19 16:03:11 -0700141 }
142}
143
Jeongik Cha538c0d02019-07-11 15:54:27 +0900144func TestPlatformAPIs(t *testing.T) {
145 testJava(t, `
146 android_app {
147 name: "foo",
148 srcs: ["a.java"],
149 platform_apis: true,
150 }
151 `)
152
153 testJava(t, `
154 android_app {
155 name: "foo",
156 srcs: ["a.java"],
157 sdk_version: "current",
158 }
159 `)
160
161 testJavaError(t, "platform_apis must be true when sdk_version is empty.", `
162 android_app {
163 name: "bar",
164 srcs: ["b.java"],
165 }
166 `)
167
168 testJavaError(t, "platform_apis must be false when sdk_version is not empty.", `
169 android_app {
170 name: "bar",
171 srcs: ["b.java"],
172 sdk_version: "system_current",
173 platform_apis: true,
174 }
175 `)
176}
177
Jeongik Chae403e9e2019-12-07 00:16:24 +0900178func TestAndroidAppLinkType(t *testing.T) {
179 testJava(t, `
180 android_app {
181 name: "foo",
182 srcs: ["a.java"],
183 libs: ["bar"],
184 static_libs: ["baz"],
185 platform_apis: true,
186 }
187
188 java_library {
189 name: "bar",
190 sdk_version: "current",
191 srcs: ["b.java"],
192 }
193
194 android_library {
195 name: "baz",
196 sdk_version: "system_current",
197 srcs: ["c.java"],
198 }
199 `)
200
201 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
202 android_app {
203 name: "foo",
204 srcs: ["a.java"],
205 libs: ["bar"],
206 sdk_version: "current",
207 static_libs: ["baz"],
208 }
209
210 java_library {
211 name: "bar",
212 sdk_version: "current",
213 srcs: ["b.java"],
214 }
215
216 android_library {
217 name: "baz",
218 sdk_version: "system_current",
219 srcs: ["c.java"],
220 }
221 `)
222
223 testJava(t, `
224 android_app {
225 name: "foo",
226 srcs: ["a.java"],
227 libs: ["bar"],
228 sdk_version: "system_current",
229 static_libs: ["baz"],
230 }
231
232 java_library {
233 name: "bar",
234 sdk_version: "current",
235 srcs: ["b.java"],
236 }
237
238 android_library {
239 name: "baz",
240 sdk_version: "system_current",
241 srcs: ["c.java"],
242 }
243 `)
244
245 testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", `
246 android_app {
247 name: "foo",
248 srcs: ["a.java"],
249 libs: ["bar"],
250 sdk_version: "system_current",
251 static_libs: ["baz"],
252 }
253
254 java_library {
255 name: "bar",
256 sdk_version: "current",
257 srcs: ["b.java"],
258 }
259
260 android_library {
261 name: "baz",
262 srcs: ["c.java"],
263 }
264 `)
265}
266
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100267func TestUpdatableApps(t *testing.T) {
268 testCases := []struct {
269 name string
270 bp string
271 expectedError string
272 }{
273 {
274 name: "Stable public SDK",
275 bp: `android_app {
276 name: "foo",
277 srcs: ["a.java"],
278 sdk_version: "29",
Artur Satayevf40fc852020-04-16 13:43:02 +0100279 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100280 updatable: true,
281 }`,
282 },
283 {
284 name: "Stable system SDK",
285 bp: `android_app {
286 name: "foo",
287 srcs: ["a.java"],
288 sdk_version: "system_29",
Artur Satayevf40fc852020-04-16 13:43:02 +0100289 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100290 updatable: true,
291 }`,
292 },
293 {
294 name: "Current public SDK",
295 bp: `android_app {
296 name: "foo",
297 srcs: ["a.java"],
298 sdk_version: "current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100299 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100300 updatable: true,
301 }`,
302 },
303 {
304 name: "Current system SDK",
305 bp: `android_app {
306 name: "foo",
307 srcs: ["a.java"],
308 sdk_version: "system_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100309 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100310 updatable: true,
311 }`,
312 },
313 {
314 name: "Current module SDK",
315 bp: `android_app {
316 name: "foo",
317 srcs: ["a.java"],
318 sdk_version: "module_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100319 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100320 updatable: true,
321 }`,
322 },
323 {
324 name: "Current core SDK",
325 bp: `android_app {
326 name: "foo",
327 srcs: ["a.java"],
328 sdk_version: "core_current",
Artur Satayevf40fc852020-04-16 13:43:02 +0100329 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100330 updatable: true,
331 }`,
332 },
333 {
334 name: "No Platform APIs",
335 bp: `android_app {
336 name: "foo",
337 srcs: ["a.java"],
338 platform_apis: true,
Artur Satayevf40fc852020-04-16 13:43:02 +0100339 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100340 updatable: true,
341 }`,
342 expectedError: "Updatable apps must use stable SDKs",
343 },
344 {
345 name: "No Core Platform APIs",
346 bp: `android_app {
347 name: "foo",
348 srcs: ["a.java"],
349 sdk_version: "core_platform",
Artur Satayevf40fc852020-04-16 13:43:02 +0100350 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100351 updatable: true,
352 }`,
353 expectedError: "Updatable apps must use stable SDKs",
354 },
355 {
356 name: "No unspecified APIs",
357 bp: `android_app {
358 name: "foo",
359 srcs: ["a.java"],
360 updatable: true,
Artur Satayevf40fc852020-04-16 13:43:02 +0100361 min_sdk_version: "29",
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100362 }`,
363 expectedError: "Updatable apps must use stable SDK",
364 },
Artur Satayevf40fc852020-04-16 13:43:02 +0100365 {
366 name: "Must specify min_sdk_version",
367 bp: `android_app {
368 name: "app_without_min_sdk_version",
369 srcs: ["a.java"],
370 sdk_version: "29",
371 updatable: true,
372 }`,
373 expectedError: "updatable apps must set min_sdk_version.",
374 },
Artur Satayev2db1c3f2020-04-08 19:09:30 +0100375 }
376
377 for _, test := range testCases {
378 t.Run(test.name, func(t *testing.T) {
379 if test.expectedError == "" {
380 testJava(t, test.bp)
381 } else {
382 testJavaError(t, test.expectedError, test.bp)
383 }
384 })
385 }
386}
387
Colin Cross0ddae7f2019-02-07 15:30:01 -0800388func TestResourceDirs(t *testing.T) {
389 testCases := []struct {
390 name string
391 prop string
392 resources []string
393 }{
394 {
395 name: "no resource_dirs",
396 prop: "",
397 resources: []string{"res/res/values/strings.xml"},
398 },
399 {
400 name: "resource_dirs",
401 prop: `resource_dirs: ["res"]`,
402 resources: []string{"res/res/values/strings.xml"},
403 },
404 {
405 name: "empty resource_dirs",
406 prop: `resource_dirs: []`,
407 resources: nil,
408 },
409 }
410
411 fs := map[string][]byte{
412 "res/res/values/strings.xml": nil,
413 }
414
415 bp := `
416 android_app {
417 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900418 sdk_version: "current",
Colin Cross0ddae7f2019-02-07 15:30:01 -0800419 %s
420 }
421 `
422
423 for _, testCase := range testCases {
424 t.Run(testCase.name, func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800425 config := testConfig(nil, fmt.Sprintf(bp, testCase.prop), fs)
426 ctx := testContext()
Colin Cross0ddae7f2019-02-07 15:30:01 -0800427 run(t, ctx, config)
428
429 module := ctx.ModuleForTests("foo", "android_common")
430 resourceList := module.MaybeOutput("aapt2/res.list")
431
432 var resources []string
433 if resourceList.Rule != nil {
434 for _, compiledResource := range resourceList.Inputs.Strings() {
435 resources = append(resources, module.Output(compiledResource).Inputs.Strings()...)
436 }
437 }
438
439 if !reflect.DeepEqual(resources, testCase.resources) {
440 t.Errorf("expected resource files %q, got %q",
441 testCase.resources, resources)
442 }
443 })
444 }
445}
446
Jaewoong Jung6431ca72020-01-15 14:15:10 -0800447func TestLibraryAssets(t *testing.T) {
448 bp := `
449 android_app {
450 name: "foo",
451 sdk_version: "current",
452 static_libs: ["lib1", "lib2", "lib3"],
453 }
454
455 android_library {
456 name: "lib1",
457 sdk_version: "current",
458 asset_dirs: ["assets_a"],
459 }
460
461 android_library {
462 name: "lib2",
463 sdk_version: "current",
464 }
465
466 android_library {
467 name: "lib3",
468 sdk_version: "current",
469 static_libs: ["lib4"],
470 }
471
472 android_library {
473 name: "lib4",
474 sdk_version: "current",
475 asset_dirs: ["assets_b"],
476 }
477 `
478
479 testCases := []struct {
480 name string
481 assetFlag string
482 assetPackages []string
483 }{
484 {
485 name: "foo",
486 // lib1 has its own asset. lib3 doesn't have any, but provides lib4's transitively.
487 assetPackages: []string{
488 buildDir + "/.intermediates/foo/android_common/aapt2/package-res.apk",
489 buildDir + "/.intermediates/lib1/android_common/assets.zip",
490 buildDir + "/.intermediates/lib3/android_common/assets.zip",
491 },
492 },
493 {
494 name: "lib1",
495 assetFlag: "-A assets_a",
496 },
497 {
498 name: "lib2",
499 },
500 {
501 name: "lib3",
502 assetPackages: []string{
503 buildDir + "/.intermediates/lib3/android_common/aapt2/package-res.apk",
504 buildDir + "/.intermediates/lib4/android_common/assets.zip",
505 },
506 },
507 {
508 name: "lib4",
509 assetFlag: "-A assets_b",
510 },
511 }
512 ctx := testApp(t, bp)
513
514 for _, test := range testCases {
515 t.Run(test.name, func(t *testing.T) {
516 m := ctx.ModuleForTests(test.name, "android_common")
517
518 // Check asset flag in aapt2 link flags
519 var aapt2link android.TestingBuildParams
520 if len(test.assetPackages) > 0 {
521 aapt2link = m.Output("aapt2/package-res.apk")
522 } else {
523 aapt2link = m.Output("package-res.apk")
524 }
525 aapt2Flags := aapt2link.Args["flags"]
526 if test.assetFlag != "" {
527 if !strings.Contains(aapt2Flags, test.assetFlag) {
528 t.Errorf("Can't find asset flag %q in aapt2 link flags %q", test.assetFlag, aapt2Flags)
529 }
530 } else {
531 if strings.Contains(aapt2Flags, " -A ") {
532 t.Errorf("aapt2 link flags %q contain unexpected asset flag", aapt2Flags)
533 }
534 }
535
536 // Check asset merge rule.
537 if len(test.assetPackages) > 0 {
538 mergeAssets := m.Output("package-res.apk")
539 if !reflect.DeepEqual(test.assetPackages, mergeAssets.Inputs.Strings()) {
540 t.Errorf("Unexpected mergeAssets inputs: %v, expected: %v",
541 mergeAssets.Inputs.Strings(), test.assetPackages)
542 }
543 }
544 })
545 }
546}
547
Colin Crossbec85302019-02-13 13:15:46 -0800548func TestAndroidResources(t *testing.T) {
Colin Cross5c4791c2019-02-01 11:44:44 -0800549 testCases := []struct {
550 name string
551 enforceRROTargets []string
552 enforceRROExcludedOverlays []string
Colin Crossbec85302019-02-13 13:15:46 -0800553 resourceFiles map[string][]string
Colin Cross5c4791c2019-02-01 11:44:44 -0800554 overlayFiles map[string][]string
555 rroDirs map[string][]string
556 }{
557 {
558 name: "no RRO",
559 enforceRROTargets: nil,
560 enforceRROExcludedOverlays: nil,
Colin Crossbec85302019-02-13 13:15:46 -0800561 resourceFiles: map[string][]string{
562 "foo": nil,
563 "bar": {"bar/res/res/values/strings.xml"},
564 "lib": nil,
565 "lib2": {"lib2/res/res/values/strings.xml"},
566 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800567 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800568 "foo": {
569 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800570 buildDir + "/.intermediates/lib/android_common/package-res.apk",
Anton Hansson53c88442019-03-18 15:53:16 +0000571 buildDir + "/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800572 "foo/res/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800573 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
574 "device/vendor/blah/overlay/foo/res/values/strings.xml",
Anton Hansson53c88442019-03-18 15:53:16 +0000575 "product/vendor/blah/overlay/foo/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800576 },
Colin Crossbec85302019-02-13 13:15:46 -0800577 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800578 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
579 "device/vendor/blah/overlay/bar/res/values/strings.xml",
580 },
Colin Crossbec85302019-02-13 13:15:46 -0800581 "lib": {
582 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
583 "lib/res/res/values/strings.xml",
584 "device/vendor/blah/overlay/lib/res/values/strings.xml",
585 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800586 },
587 rroDirs: map[string][]string{
588 "foo": nil,
589 "bar": nil,
590 },
591 },
592 {
593 name: "enforce RRO on foo",
594 enforceRROTargets: []string{"foo"},
595 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Colin Crossbec85302019-02-13 13:15:46 -0800596 resourceFiles: map[string][]string{
597 "foo": nil,
598 "bar": {"bar/res/res/values/strings.xml"},
599 "lib": nil,
600 "lib2": {"lib2/res/res/values/strings.xml"},
601 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800602 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800603 "foo": {
604 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800605 buildDir + "/.intermediates/lib/android_common/package-res.apk",
Anton Hansson53c88442019-03-18 15:53:16 +0000606 buildDir + "/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800607 "foo/res/res/values/strings.xml",
608 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
609 },
Colin Crossbec85302019-02-13 13:15:46 -0800610 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800611 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
612 "device/vendor/blah/overlay/bar/res/values/strings.xml",
613 },
Colin Crossbec85302019-02-13 13:15:46 -0800614 "lib": {
615 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
616 "lib/res/res/values/strings.xml",
617 "device/vendor/blah/overlay/lib/res/values/strings.xml",
618 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800619 },
Colin Crossc1c37552019-01-31 11:42:41 -0800620
Colin Cross5c4791c2019-02-01 11:44:44 -0800621 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800622 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +0000623 "device:device/vendor/blah/overlay/foo/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800624 // Enforce RRO on "foo" could imply RRO on static dependencies, but for now it doesn't.
625 // "device/vendor/blah/overlay/lib/res",
Anton Hansson53c88442019-03-18 15:53:16 +0000626 "product:product/vendor/blah/overlay/foo/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800627 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800628 "bar": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800629 "lib": nil,
Colin Cross5c4791c2019-02-01 11:44:44 -0800630 },
631 },
632 {
633 name: "enforce RRO on all",
634 enforceRROTargets: []string{"*"},
635 enforceRROExcludedOverlays: []string{
636 // Excluding specific apps/res directories also allowed.
637 "device/vendor/blah/static_overlay/foo",
638 "device/vendor/blah/static_overlay/bar/res",
639 },
Colin Crossbec85302019-02-13 13:15:46 -0800640 resourceFiles: map[string][]string{
641 "foo": nil,
642 "bar": {"bar/res/res/values/strings.xml"},
643 "lib": nil,
644 "lib2": {"lib2/res/res/values/strings.xml"},
645 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800646 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800647 "foo": {
648 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800649 buildDir + "/.intermediates/lib/android_common/package-res.apk",
Anton Hansson53c88442019-03-18 15:53:16 +0000650 buildDir + "/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800651 "foo/res/res/values/strings.xml",
652 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
653 },
Colin Crossbec85302019-02-13 13:15:46 -0800654 "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"},
655 "lib": {
656 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
657 "lib/res/res/values/strings.xml",
658 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800659 },
660 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800661 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +0000662 "device:device/vendor/blah/overlay/foo/res",
663 "product:product/vendor/blah/overlay/foo/res",
664 // Lib dep comes after the direct deps
665 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800666 },
Anton Hansson53c88442019-03-18 15:53:16 +0000667 "bar": {"device:device/vendor/blah/overlay/bar/res"},
668 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -0800669 },
670 },
671 }
672
Anton Hansson53c88442019-03-18 15:53:16 +0000673 deviceResourceOverlays := []string{
Colin Cross890ff552017-11-30 20:13:19 -0800674 "device/vendor/blah/overlay",
675 "device/vendor/blah/overlay2",
676 "device/vendor/blah/static_overlay",
677 }
678
Anton Hansson53c88442019-03-18 15:53:16 +0000679 productResourceOverlays := []string{
680 "product/vendor/blah/overlay",
681 }
682
Colin Cross890ff552017-11-30 20:13:19 -0800683 fs := map[string][]byte{
684 "foo/res/res/values/strings.xml": nil,
685 "bar/res/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800686 "lib/res/res/values/strings.xml": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800687 "lib2/res/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800688 "device/vendor/blah/overlay/foo/res/values/strings.xml": nil,
689 "device/vendor/blah/overlay/bar/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800690 "device/vendor/blah/overlay/lib/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800691 "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
692 "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
693 "device/vendor/blah/overlay2/res/values/strings.xml": nil,
Anton Hansson53c88442019-03-18 15:53:16 +0000694 "product/vendor/blah/overlay/foo/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800695 }
696
697 bp := `
698 android_app {
699 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900700 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -0800701 resource_dirs: ["foo/res"],
Anton Hansson53c88442019-03-18 15:53:16 +0000702 static_libs: ["lib", "lib3"],
Colin Cross890ff552017-11-30 20:13:19 -0800703 }
704
705 android_app {
706 name: "bar",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900707 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -0800708 resource_dirs: ["bar/res"],
709 }
Colin Cross6ed7dea2019-01-31 14:44:30 -0800710
711 android_library {
712 name: "lib",
Jeongik Cha75b83b02019-11-01 15:28:00 +0900713 sdk_version: "current",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800714 resource_dirs: ["lib/res"],
Colin Crossbec85302019-02-13 13:15:46 -0800715 static_libs: ["lib2"],
716 }
717
718 android_library {
719 name: "lib2",
Jeongik Cha75b83b02019-11-01 15:28:00 +0900720 sdk_version: "current",
Colin Crossbec85302019-02-13 13:15:46 -0800721 resource_dirs: ["lib2/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -0800722 }
Anton Hansson53c88442019-03-18 15:53:16 +0000723
724 // This library has the same resources as lib (should not lead to dupe RROs)
725 android_library {
726 name: "lib3",
Jeongik Cha75b83b02019-11-01 15:28:00 +0900727 sdk_version: "current",
Anton Hansson53c88442019-03-18 15:53:16 +0000728 resource_dirs: ["lib/res"]
729 }
Colin Cross890ff552017-11-30 20:13:19 -0800730 `
731
Colin Cross5c4791c2019-02-01 11:44:44 -0800732 for _, testCase := range testCases {
Colin Cross890ff552017-11-30 20:13:19 -0800733 t.Run(testCase.name, func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800734 config := testAppConfig(nil, bp, fs)
Anton Hansson53c88442019-03-18 15:53:16 +0000735 config.TestProductVariables.DeviceResourceOverlays = deviceResourceOverlays
736 config.TestProductVariables.ProductResourceOverlays = productResourceOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800737 if testCase.enforceRROTargets != nil {
Colin Crossa74ca042019-01-31 14:31:51 -0800738 config.TestProductVariables.EnforceRROTargets = testCase.enforceRROTargets
Colin Cross890ff552017-11-30 20:13:19 -0800739 }
740 if testCase.enforceRROExcludedOverlays != nil {
Colin Crossa74ca042019-01-31 14:31:51 -0800741 config.TestProductVariables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800742 }
743
Colin Cross98be1bb2019-12-13 20:41:13 -0800744 ctx := testContext()
Colin Cross890ff552017-11-30 20:13:19 -0800745 run(t, ctx, config)
746
Colin Crossbec85302019-02-13 13:15:46 -0800747 resourceListToFiles := func(module android.TestingModule, list []string) (files []string) {
748 for _, o := range list {
749 res := module.MaybeOutput(o)
750 if res.Rule != nil {
751 // If the overlay is compiled as part of this module (i.e. a .arsc.flat file),
752 // verify the inputs to the .arsc.flat rule.
753 files = append(files, res.Inputs.Strings()...)
754 } else {
755 // Otherwise, verify the full path to the output of the other module
756 files = append(files, o)
Anton Hansson94c93f32019-01-30 16:03:37 +0000757 }
Colin Cross890ff552017-11-30 20:13:19 -0800758 }
Colin Crossbec85302019-02-13 13:15:46 -0800759 return files
Colin Cross890ff552017-11-30 20:13:19 -0800760 }
761
Colin Crossbec85302019-02-13 13:15:46 -0800762 getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) {
763 module := ctx.ModuleForTests(moduleName, "android_common")
764 resourceList := module.MaybeOutput("aapt2/res.list")
765 if resourceList.Rule != nil {
766 resourceFiles = resourceListToFiles(module, resourceList.Inputs.Strings())
Anton Hansson0375a4f2019-01-24 14:39:19 +0000767 }
Colin Crossbec85302019-02-13 13:15:46 -0800768 overlayList := module.MaybeOutput("aapt2/overlay.list")
769 if overlayList.Rule != nil {
770 overlayFiles = resourceListToFiles(module, overlayList.Inputs.Strings())
771 }
772
Anton Hansson53c88442019-03-18 15:53:16 +0000773 for _, d := range module.Module().(AndroidLibraryDependency).ExportedRRODirs() {
774 var prefix string
775 if d.overlayType == device {
776 prefix = "device:"
777 } else if d.overlayType == product {
778 prefix = "product:"
779 } else {
780 t.Fatalf("Unexpected overlayType %d", d.overlayType)
781 }
782 rroDirs = append(rroDirs, prefix+d.path.String())
783 }
Colin Crossbec85302019-02-13 13:15:46 -0800784
785 return resourceFiles, overlayFiles, rroDirs
786 }
787
788 modules := []string{"foo", "bar", "lib", "lib2"}
789 for _, module := range modules {
790 resourceFiles, overlayFiles, rroDirs := getResources(module)
791
792 if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) {
793 t.Errorf("expected %s resource files:\n %#v\n got:\n %#v",
794 module, testCase.resourceFiles[module], resourceFiles)
795 }
796 if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) {
797 t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v",
798 module, testCase.overlayFiles[module], overlayFiles)
799 }
800 if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) {
Anton Hansson0375a4f2019-01-24 14:39:19 +0000801 t.Errorf("expected %s rroDirs: %#v\n got:\n %#v",
Colin Crossbec85302019-02-13 13:15:46 -0800802 module, testCase.rroDirs[module], rroDirs)
Anton Hansson0375a4f2019-01-24 14:39:19 +0000803 }
Colin Cross890ff552017-11-30 20:13:19 -0800804 }
Colin Cross890ff552017-11-30 20:13:19 -0800805 })
806 }
807}
Colin Crossd09b0b62018-04-18 11:06:47 -0700808
809func TestAppSdkVersion(t *testing.T) {
810 testCases := []struct {
811 name string
812 sdkVersion string
813 platformSdkInt int
814 platformSdkCodename string
815 platformSdkFinal bool
816 expectedMinSdkVersion string
Jeongik Cha538c0d02019-07-11 15:54:27 +0900817 platformApis bool
Colin Crossd09b0b62018-04-18 11:06:47 -0700818 }{
819 {
820 name: "current final SDK",
821 sdkVersion: "current",
822 platformSdkInt: 27,
823 platformSdkCodename: "REL",
824 platformSdkFinal: true,
825 expectedMinSdkVersion: "27",
826 },
827 {
828 name: "current non-final SDK",
829 sdkVersion: "current",
830 platformSdkInt: 27,
831 platformSdkCodename: "OMR1",
832 platformSdkFinal: false,
833 expectedMinSdkVersion: "OMR1",
834 },
835 {
836 name: "default final SDK",
837 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900838 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -0700839 platformSdkInt: 27,
840 platformSdkCodename: "REL",
841 platformSdkFinal: true,
842 expectedMinSdkVersion: "27",
843 },
844 {
845 name: "default non-final SDK",
846 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900847 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -0700848 platformSdkInt: 27,
849 platformSdkCodename: "OMR1",
850 platformSdkFinal: false,
851 expectedMinSdkVersion: "OMR1",
852 },
853 {
854 name: "14",
855 sdkVersion: "14",
856 expectedMinSdkVersion: "14",
857 },
858 }
859
860 for _, moduleType := range []string{"android_app", "android_library"} {
861 for _, test := range testCases {
862 t.Run(moduleType+" "+test.name, func(t *testing.T) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900863 platformApiProp := ""
864 if test.platformApis {
865 platformApiProp = "platform_apis: true,"
866 }
Colin Crossd09b0b62018-04-18 11:06:47 -0700867 bp := fmt.Sprintf(`%s {
868 name: "foo",
869 srcs: ["a.java"],
870 sdk_version: "%s",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900871 %s
872 }`, moduleType, test.sdkVersion, platformApiProp)
Colin Crossd09b0b62018-04-18 11:06:47 -0700873
Colin Cross98be1bb2019-12-13 20:41:13 -0800874 config := testAppConfig(nil, bp, nil)
Colin Crossd09b0b62018-04-18 11:06:47 -0700875 config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt
876 config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename
877 config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal
878
Colin Cross98be1bb2019-12-13 20:41:13 -0800879 ctx := testContext()
Colin Crossd09b0b62018-04-18 11:06:47 -0700880
881 run(t, ctx, config)
882
883 foo := ctx.ModuleForTests("foo", "android_common")
884 link := foo.Output("package-res.apk")
885 linkFlags := strings.Split(link.Args["flags"], " ")
886 min := android.IndexList("--min-sdk-version", linkFlags)
887 target := android.IndexList("--target-sdk-version", linkFlags)
888
889 if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
890 t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
891 }
892
893 gotMinSdkVersion := linkFlags[min+1]
894 gotTargetSdkVersion := linkFlags[target+1]
895
896 if gotMinSdkVersion != test.expectedMinSdkVersion {
897 t.Errorf("incorrect --min-sdk-version, expected %q got %q",
898 test.expectedMinSdkVersion, gotMinSdkVersion)
899 }
900
901 if gotTargetSdkVersion != test.expectedMinSdkVersion {
902 t.Errorf("incorrect --target-sdk-version, expected %q got %q",
903 test.expectedMinSdkVersion, gotTargetSdkVersion)
904 }
905 })
906 }
907 }
908}
Colin Crossa4f08812018-10-02 22:03:40 -0700909
Paul Duffin50c217c2019-06-12 13:25:22 +0100910func TestJNIABI(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700911 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +0100912 cc_library {
913 name: "libjni",
914 system_shared_libs: [],
Colin Crossc511bc52020-04-07 16:50:32 +0000915 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +0100916 stl: "none",
917 }
918
919 android_test {
920 name: "test",
921 sdk_version: "core_platform",
922 jni_libs: ["libjni"],
923 }
924
925 android_test {
926 name: "test_first",
927 sdk_version: "core_platform",
928 compile_multilib: "first",
929 jni_libs: ["libjni"],
930 }
931
932 android_test {
933 name: "test_both",
934 sdk_version: "core_platform",
935 compile_multilib: "both",
936 jni_libs: ["libjni"],
937 }
938
939 android_test {
940 name: "test_32",
941 sdk_version: "core_platform",
942 compile_multilib: "32",
943 jni_libs: ["libjni"],
944 }
945
946 android_test {
947 name: "test_64",
948 sdk_version: "core_platform",
949 compile_multilib: "64",
950 jni_libs: ["libjni"],
951 }
952 `)
953
954 testCases := []struct {
955 name string
956 abis []string
957 }{
958 {"test", []string{"arm64-v8a"}},
959 {"test_first", []string{"arm64-v8a"}},
960 {"test_both", []string{"arm64-v8a", "armeabi-v7a"}},
961 {"test_32", []string{"armeabi-v7a"}},
962 {"test_64", []string{"arm64-v8a"}},
963 }
964
965 for _, test := range testCases {
966 t.Run(test.name, func(t *testing.T) {
967 app := ctx.ModuleForTests(test.name, "android_common")
968 jniLibZip := app.Output("jnilibs.zip")
969 var abis []string
970 args := strings.Fields(jniLibZip.Args["jarArgs"])
971 for i := 0; i < len(args); i++ {
972 if args[i] == "-P" {
973 abis = append(abis, filepath.Base(args[i+1]))
974 i++
975 }
976 }
977 if !reflect.DeepEqual(abis, test.abis) {
978 t.Errorf("want abis %v, got %v", test.abis, abis)
979 }
980 })
981 }
982}
983
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900984func TestAppSdkVersionByPartition(t *testing.T) {
985 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
986 android_app {
987 name: "foo",
988 srcs: ["a.java"],
989 vendor: true,
990 platform_apis: true,
991 }
992 `)
993
994 testJava(t, `
995 android_app {
996 name: "bar",
997 srcs: ["b.java"],
998 platform_apis: true,
999 }
1000 `)
1001
1002 for _, enforce := range []bool{true, false} {
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001003 bp := `
1004 android_app {
1005 name: "foo",
1006 srcs: ["a.java"],
1007 product_specific: true,
1008 platform_apis: true,
1009 }
1010 `
Colin Cross98be1bb2019-12-13 20:41:13 -08001011
1012 config := testAppConfig(nil, bp, nil)
1013 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001014 if enforce {
Colin Cross98be1bb2019-12-13 20:41:13 -08001015 testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001016 } else {
Colin Cross98be1bb2019-12-13 20:41:13 -08001017 testJavaWithConfig(t, config)
Jeongik Cha2cc570d2019-10-29 15:44:45 +09001018 }
1019 }
1020}
1021
Paul Duffin50c217c2019-06-12 13:25:22 +01001022func TestJNIPackaging(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001023 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +01001024 cc_library {
1025 name: "libjni",
1026 system_shared_libs: [],
1027 stl: "none",
Colin Cross094cde42020-02-15 10:38:00 -08001028 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001029 }
1030
1031 android_app {
1032 name: "app",
1033 jni_libs: ["libjni"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001034 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001035 }
1036
1037 android_app {
1038 name: "app_noembed",
1039 jni_libs: ["libjni"],
1040 use_embedded_native_libs: false,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001041 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001042 }
1043
1044 android_app {
1045 name: "app_embed",
1046 jni_libs: ["libjni"],
1047 use_embedded_native_libs: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001048 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001049 }
1050
1051 android_test {
1052 name: "test",
Colin Crossc511bc52020-04-07 16:50:32 +00001053 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001054 jni_libs: ["libjni"],
1055 }
1056
1057 android_test {
1058 name: "test_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00001059 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001060 jni_libs: ["libjni"],
1061 use_embedded_native_libs: false,
1062 }
1063
1064 android_test_helper_app {
1065 name: "test_helper",
Colin Crossc511bc52020-04-07 16:50:32 +00001066 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001067 jni_libs: ["libjni"],
1068 }
1069
1070 android_test_helper_app {
1071 name: "test_helper_noembed",
Colin Crossc511bc52020-04-07 16:50:32 +00001072 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +01001073 jni_libs: ["libjni"],
1074 use_embedded_native_libs: false,
1075 }
1076 `)
1077
1078 testCases := []struct {
1079 name string
1080 packaged bool
1081 compressed bool
1082 }{
1083 {"app", false, false},
1084 {"app_noembed", false, false},
1085 {"app_embed", true, false},
1086 {"test", true, false},
1087 {"test_noembed", true, true},
1088 {"test_helper", true, false},
1089 {"test_helper_noembed", true, true},
1090 }
1091
1092 for _, test := range testCases {
1093 t.Run(test.name, func(t *testing.T) {
1094 app := ctx.ModuleForTests(test.name, "android_common")
1095 jniLibZip := app.MaybeOutput("jnilibs.zip")
1096 if g, w := (jniLibZip.Rule != nil), test.packaged; g != w {
1097 t.Errorf("expected jni packaged %v, got %v", w, g)
1098 }
1099
1100 if jniLibZip.Rule != nil {
1101 if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w {
1102 t.Errorf("expected jni compressed %v, got %v", w, g)
1103 }
Colin Crossc511bc52020-04-07 16:50:32 +00001104
1105 if !strings.Contains(jniLibZip.Implicits[0].String(), "_sdk_") {
1106 t.Errorf("expected input %q to use sdk variant", jniLibZip.Implicits[0].String())
1107 }
Paul Duffin50c217c2019-06-12 13:25:22 +01001108 }
1109 })
1110 }
Colin Cross47fa9d32019-03-26 10:51:39 -07001111}
1112
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001113func TestCertificates(t *testing.T) {
1114 testCases := []struct {
1115 name string
1116 bp string
1117 certificateOverride string
1118 expected string
1119 }{
1120 {
1121 name: "default",
1122 bp: `
1123 android_app {
1124 name: "foo",
1125 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001126 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001127 }
1128 `,
1129 certificateOverride: "",
Dan Willemsen412160e2019-04-09 21:36:26 -07001130 expected: "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001131 },
1132 {
1133 name: "module certificate property",
1134 bp: `
1135 android_app {
1136 name: "foo",
1137 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001138 certificate: ":new_certificate",
1139 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001140 }
1141
1142 android_app_certificate {
1143 name: "new_certificate",
1144 certificate: "cert/new_cert",
1145 }
1146 `,
1147 certificateOverride: "",
1148 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
1149 },
1150 {
1151 name: "path certificate property",
1152 bp: `
1153 android_app {
1154 name: "foo",
1155 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001156 certificate: "expiredkey",
1157 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001158 }
1159 `,
1160 certificateOverride: "",
Dan Willemsen412160e2019-04-09 21:36:26 -07001161 expected: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001162 },
1163 {
1164 name: "certificate overrides",
1165 bp: `
1166 android_app {
1167 name: "foo",
1168 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001169 certificate: "expiredkey",
1170 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001171 }
1172
1173 android_app_certificate {
1174 name: "new_certificate",
1175 certificate: "cert/new_cert",
1176 }
1177 `,
1178 certificateOverride: "foo:new_certificate",
1179 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
1180 },
1181 }
1182
1183 for _, test := range testCases {
1184 t.Run(test.name, func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001185 config := testAppConfig(nil, test.bp, nil)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001186 if test.certificateOverride != "" {
1187 config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride}
1188 }
Colin Cross98be1bb2019-12-13 20:41:13 -08001189 ctx := testContext()
Jaewoong Jung2ad817c2019-01-18 14:27:16 -08001190
1191 run(t, ctx, config)
1192 foo := ctx.ModuleForTests("foo", "android_common")
1193
1194 signapk := foo.Output("foo.apk")
1195 signFlags := signapk.Args["certificates"]
1196 if test.expected != signFlags {
1197 t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags)
1198 }
1199 })
1200 }
1201}
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001202
1203func TestPackageNameOverride(t *testing.T) {
1204 testCases := []struct {
1205 name string
1206 bp string
1207 packageNameOverride string
1208 expected []string
1209 }{
1210 {
1211 name: "default",
1212 bp: `
1213 android_app {
1214 name: "foo",
1215 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001216 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001217 }
1218 `,
1219 packageNameOverride: "",
1220 expected: []string{
1221 buildDir + "/.intermediates/foo/android_common/foo.apk",
1222 buildDir + "/target/product/test_device/system/app/foo/foo.apk",
1223 },
1224 },
1225 {
1226 name: "overridden",
1227 bp: `
1228 android_app {
1229 name: "foo",
1230 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001231 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001232 }
1233 `,
1234 packageNameOverride: "foo:bar",
1235 expected: []string{
1236 // The package apk should be still be the original name for test dependencies.
Jaewoong Jung5a498812019-11-07 14:14:38 -08001237 buildDir + "/.intermediates/foo/android_common/bar.apk",
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001238 buildDir + "/target/product/test_device/system/app/bar/bar.apk",
1239 },
1240 },
1241 }
1242
1243 for _, test := range testCases {
1244 t.Run(test.name, func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08001245 config := testAppConfig(nil, test.bp, nil)
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001246 if test.packageNameOverride != "" {
1247 config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride}
1248 }
Colin Cross98be1bb2019-12-13 20:41:13 -08001249 ctx := testContext()
Jaewoong Jung9d22a912019-01-23 16:27:47 -08001250
1251 run(t, ctx, config)
1252 foo := ctx.ModuleForTests("foo", "android_common")
1253
1254 outputs := foo.AllOutputs()
1255 outputMap := make(map[string]bool)
1256 for _, o := range outputs {
1257 outputMap[o] = true
1258 }
1259 for _, e := range test.expected {
1260 if _, exist := outputMap[e]; !exist {
1261 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
1262 }
1263 }
1264 })
1265 }
1266}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001267
1268func TestInstrumentationTargetOverridden(t *testing.T) {
1269 bp := `
1270 android_app {
1271 name: "foo",
1272 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001273 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001274 }
1275
1276 android_test {
1277 name: "bar",
1278 instrumentation_for: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001279 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001280 }
1281 `
Colin Cross98be1bb2019-12-13 20:41:13 -08001282 config := testAppConfig(nil, bp, nil)
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001283 config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
Colin Cross98be1bb2019-12-13 20:41:13 -08001284 ctx := testContext()
Jaewoong Jung4102e5d2019-02-27 16:26:28 -08001285
1286 run(t, ctx, config)
1287
1288 bar := ctx.ModuleForTests("bar", "android_common")
1289 res := bar.Output("package-res.apk")
1290 aapt2Flags := res.Args["flags"]
1291 e := "--rename-instrumentation-target-package org.dandroid.bp"
1292 if !strings.Contains(aapt2Flags, e) {
1293 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
1294 }
1295}
Jaewoong Jung525443a2019-02-28 15:35:54 -08001296
1297func TestOverrideAndroidApp(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001298 ctx, _ := testJava(t, `
Jaewoong Jung525443a2019-02-28 15:35:54 -08001299 android_app {
1300 name: "foo",
1301 srcs: ["a.java"],
Jaewoong Junga641ee92019-03-27 11:17:14 -07001302 certificate: "expiredkey",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001303 overrides: ["qux"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001304 sdk_version: "current",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001305 }
1306
1307 override_android_app {
1308 name: "bar",
1309 base: "foo",
1310 certificate: ":new_certificate",
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001311 logging_parent: "bah",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001312 }
1313
1314 android_app_certificate {
1315 name: "new_certificate",
1316 certificate: "cert/new_cert",
1317 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001318
1319 override_android_app {
1320 name: "baz",
1321 base: "foo",
1322 package_name: "org.dandroid.bp",
1323 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08001324 `)
1325
1326 expectedVariants := []struct {
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001327 moduleName string
1328 variantName string
1329 apkName string
1330 apkPath string
1331 signFlag string
1332 overrides []string
1333 aaptFlag string
1334 logging_parent string
Jaewoong Jung525443a2019-02-28 15:35:54 -08001335 }{
1336 {
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001337 moduleName: "foo",
1338 variantName: "android_common",
1339 apkPath: "/target/product/test_device/system/app/foo/foo.apk",
1340 signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1341 overrides: []string{"qux"},
1342 aaptFlag: "",
1343 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001344 },
1345 {
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001346 moduleName: "bar",
1347 variantName: "android_common_bar",
1348 apkPath: "/target/product/test_device/system/app/bar/bar.apk",
1349 signFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
1350 overrides: []string{"qux", "foo"},
1351 aaptFlag: "",
1352 logging_parent: "bah",
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001353 },
1354 {
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001355 moduleName: "baz",
1356 variantName: "android_common_baz",
1357 apkPath: "/target/product/test_device/system/app/baz/baz.apk",
1358 signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
1359 overrides: []string{"qux", "foo"},
1360 aaptFlag: "--rename-manifest-package org.dandroid.bp",
1361 logging_parent: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001362 },
1363 }
1364 for _, expected := range expectedVariants {
1365 variant := ctx.ModuleForTests("foo", expected.variantName)
1366
1367 // Check the final apk name
1368 outputs := variant.AllOutputs()
1369 expectedApkPath := buildDir + expected.apkPath
1370 found := false
1371 for _, o := range outputs {
1372 if o == expectedApkPath {
1373 found = true
1374 break
1375 }
1376 }
1377 if !found {
1378 t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs)
1379 }
1380
1381 // Check the certificate paths
Jaewoong Jung5a498812019-11-07 14:14:38 -08001382 signapk := variant.Output(expected.moduleName + ".apk")
Jaewoong Jung525443a2019-02-28 15:35:54 -08001383 signFlag := signapk.Args["certificates"]
1384 if expected.signFlag != signFlag {
1385 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected.signFlag, signFlag)
1386 }
1387
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001388 // Check if the overrides field values are correctly aggregated.
Jaewoong Jung525443a2019-02-28 15:35:54 -08001389 mod := variant.Module().(*AndroidApp)
1390 if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) {
1391 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
1392 expected.overrides, mod.appProperties.Overrides)
1393 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001394
Baligh Uddin5b16dfb2020-02-11 17:27:19 -08001395 // Test Overridable property: Logging_parent
1396 logging_parent := mod.aapt.LoggingParent
1397 if expected.logging_parent != logging_parent {
1398 t.Errorf("Incorrect overrides property value for logging parent, expected: %q, got: %q",
1399 expected.logging_parent, logging_parent)
1400 }
1401
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001402 // Check the package renaming flag, if exists.
1403 res := variant.Output("package-res.apk")
1404 aapt2Flags := res.Args["flags"]
1405 if !strings.Contains(aapt2Flags, expected.aaptFlag) {
1406 t.Errorf("package renaming flag, %q is missing in aapt2 link flags, %q", expected.aaptFlag, aapt2Flags)
1407 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08001408 }
1409}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001410
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001411func TestOverrideAndroidAppDependency(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001412 ctx, _ := testJava(t, `
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001413 android_app {
1414 name: "foo",
1415 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001416 sdk_version: "current",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001417 }
1418
1419 override_android_app {
1420 name: "bar",
1421 base: "foo",
1422 package_name: "org.dandroid.bp",
1423 }
1424
1425 android_test {
1426 name: "baz",
1427 srcs: ["b.java"],
1428 instrumentation_for: "foo",
1429 }
1430
1431 android_test {
1432 name: "qux",
1433 srcs: ["b.java"],
1434 instrumentation_for: "bar",
1435 }
1436 `)
1437
1438 // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg.
1439 javac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1440 fooTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1441 if !strings.Contains(javac.Args["classpath"], fooTurbine) {
1442 t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine)
1443 }
1444
1445 // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg.
1446 javac = ctx.ModuleForTests("qux", "android_common").Rule("javac")
1447 barTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common_bar", "turbine-combined", "foo.jar")
1448 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1449 t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
1450 }
1451}
1452
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001453func TestOverrideAndroidTest(t *testing.T) {
1454 ctx, _ := testJava(t, `
1455 android_app {
1456 name: "foo",
1457 srcs: ["a.java"],
1458 package_name: "com.android.foo",
1459 sdk_version: "current",
1460 }
1461
1462 override_android_app {
1463 name: "bar",
1464 base: "foo",
1465 package_name: "com.android.bar",
1466 }
1467
1468 android_test {
1469 name: "foo_test",
1470 srcs: ["b.java"],
1471 instrumentation_for: "foo",
1472 }
1473
1474 override_android_test {
1475 name: "bar_test",
1476 base: "foo_test",
1477 package_name: "com.android.bar.test",
1478 instrumentation_for: "bar",
1479 instrumentation_target_package: "com.android.bar",
1480 }
1481 `)
1482
1483 expectedVariants := []struct {
1484 moduleName string
1485 variantName string
1486 apkPath string
1487 overrides []string
1488 targetVariant string
1489 packageFlag string
1490 targetPackageFlag string
1491 }{
1492 {
1493 variantName: "android_common",
Jaewoong Jung326a9412019-11-21 10:41:00 -08001494 apkPath: "/target/product/test_device/testcases/foo_test/arm64/foo_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001495 overrides: nil,
1496 targetVariant: "android_common",
1497 packageFlag: "",
1498 targetPackageFlag: "",
1499 },
1500 {
1501 variantName: "android_common_bar_test",
Jaewoong Jung326a9412019-11-21 10:41:00 -08001502 apkPath: "/target/product/test_device/testcases/bar_test/arm64/bar_test.apk",
Jaewoong Jung26dedd32019-06-06 08:45:58 -07001503 overrides: []string{"foo_test"},
1504 targetVariant: "android_common_bar",
1505 packageFlag: "com.android.bar.test",
1506 targetPackageFlag: "com.android.bar",
1507 },
1508 }
1509 for _, expected := range expectedVariants {
1510 variant := ctx.ModuleForTests("foo_test", expected.variantName)
1511
1512 // Check the final apk name
1513 outputs := variant.AllOutputs()
1514 expectedApkPath := buildDir + expected.apkPath
1515 found := false
1516 for _, o := range outputs {
1517 if o == expectedApkPath {
1518 found = true
1519 break
1520 }
1521 }
1522 if !found {
1523 t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs)
1524 }
1525
1526 // Check if the overrides field values are correctly aggregated.
1527 mod := variant.Module().(*AndroidTest)
1528 if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) {
1529 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
1530 expected.overrides, mod.appProperties.Overrides)
1531 }
1532
1533 // Check if javac classpath has the correct jar file path. This checks instrumentation_for overrides.
1534 javac := variant.Rule("javac")
1535 turbine := filepath.Join(buildDir, ".intermediates", "foo", expected.targetVariant, "turbine-combined", "foo.jar")
1536 if !strings.Contains(javac.Args["classpath"], turbine) {
1537 t.Errorf("classpath %q does not contain %q", javac.Args["classpath"], turbine)
1538 }
1539
1540 // Check aapt2 flags.
1541 res := variant.Output("package-res.apk")
1542 aapt2Flags := res.Args["flags"]
1543 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
1544 checkAapt2LinkFlag(t, aapt2Flags, "rename-instrumentation-target-package", expected.targetPackageFlag)
1545 }
1546}
1547
Jaewoong Jung39982342020-01-14 10:27:18 -08001548func TestAndroidTest_FixTestConfig(t *testing.T) {
1549 ctx, _ := testJava(t, `
1550 android_app {
1551 name: "foo",
1552 srcs: ["a.java"],
1553 package_name: "com.android.foo",
1554 sdk_version: "current",
1555 }
1556
1557 android_test {
1558 name: "foo_test",
1559 srcs: ["b.java"],
1560 instrumentation_for: "foo",
1561 }
1562
1563 android_test {
1564 name: "bar_test",
1565 srcs: ["b.java"],
1566 package_name: "com.android.bar.test",
1567 instrumentation_for: "foo",
1568 }
1569
1570 override_android_test {
1571 name: "baz_test",
1572 base: "foo_test",
1573 package_name: "com.android.baz.test",
1574 }
1575 `)
1576
1577 testCases := []struct {
1578 moduleName string
1579 variantName string
1580 expectedFlags []string
1581 }{
1582 {
1583 moduleName: "foo_test",
1584 variantName: "android_common",
1585 },
1586 {
1587 moduleName: "bar_test",
1588 variantName: "android_common",
1589 expectedFlags: []string{
1590 "--manifest " + buildDir + "/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml",
1591 "--package-name com.android.bar.test",
1592 },
1593 },
1594 {
1595 moduleName: "foo_test",
1596 variantName: "android_common_baz_test",
1597 expectedFlags: []string{
1598 "--manifest " + buildDir +
1599 "/.intermediates/foo_test/android_common_baz_test/manifest_fixer/AndroidManifest.xml",
1600 "--package-name com.android.baz.test",
1601 "--test-file-name baz_test.apk",
1602 },
1603 },
1604 }
1605
1606 for _, test := range testCases {
1607 variant := ctx.ModuleForTests(test.moduleName, test.variantName)
1608 params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml")
1609
1610 if len(test.expectedFlags) > 0 {
1611 if params.Rule == nil {
1612 t.Errorf("test_config_fixer was expected to run, but didn't")
1613 } else {
1614 for _, flag := range test.expectedFlags {
1615 if !strings.Contains(params.RuleParams.Command, flag) {
1616 t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command)
1617 }
1618 }
1619 }
1620 } else {
1621 if params.Rule != nil {
1622 t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command)
1623 }
1624 }
1625
1626 }
1627}
1628
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001629func TestAndroidAppImport(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001630 ctx, _ := testJava(t, `
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001631 android_app_import {
1632 name: "foo",
1633 apk: "prebuilts/apk/app.apk",
1634 certificate: "platform",
1635 dex_preopt: {
1636 enabled: true,
1637 },
1638 }
1639 `)
1640
1641 variant := ctx.ModuleForTests("foo", "android_common")
1642
1643 // Check dexpreopt outputs.
1644 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
1645 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
1646 t.Errorf("can't find dexpreopt outputs")
1647 }
1648
1649 // Check cert signing flag.
1650 signedApk := variant.Output("signed/foo.apk")
1651 signingFlag := signedApk.Args["certificates"]
1652 expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8"
1653 if expected != signingFlag {
1654 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
1655 }
1656}
1657
1658func TestAndroidAppImport_NoDexPreopt(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001659 ctx, _ := testJava(t, `
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001660 android_app_import {
1661 name: "foo",
1662 apk: "prebuilts/apk/app.apk",
1663 certificate: "platform",
1664 dex_preopt: {
1665 enabled: false,
1666 },
1667 }
1668 `)
1669
1670 variant := ctx.ModuleForTests("foo", "android_common")
1671
1672 // Check dexpreopt outputs. They shouldn't exist.
1673 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule != nil ||
1674 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule != nil {
1675 t.Errorf("dexpreopt shouldn't have run.")
1676 }
1677}
1678
1679func TestAndroidAppImport_Presigned(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001680 ctx, _ := testJava(t, `
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001681 android_app_import {
1682 name: "foo",
1683 apk: "prebuilts/apk/app.apk",
1684 presigned: true,
1685 dex_preopt: {
1686 enabled: true,
1687 },
1688 }
1689 `)
1690
1691 variant := ctx.ModuleForTests("foo", "android_common")
1692
1693 // Check dexpreopt outputs.
1694 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
1695 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
1696 t.Errorf("can't find dexpreopt outputs")
1697 }
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +01001698 // Make sure signing was skipped and aligning was done.
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001699 if variant.MaybeOutput("signed/foo.apk").Rule != nil {
1700 t.Errorf("signing rule shouldn't be included.")
1701 }
1702 if variant.MaybeOutput("zip-aligned/foo.apk").Rule == nil {
1703 t.Errorf("can't find aligning rule")
1704 }
1705}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001706
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001707func TestAndroidAppImport_DefaultDevCert(t *testing.T) {
1708 ctx, _ := testJava(t, `
1709 android_app_import {
1710 name: "foo",
1711 apk: "prebuilts/apk/app.apk",
1712 default_dev_cert: true,
1713 dex_preopt: {
1714 enabled: true,
1715 },
1716 }
1717 `)
1718
1719 variant := ctx.ModuleForTests("foo", "android_common")
1720
1721 // Check dexpreopt outputs.
1722 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
1723 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
1724 t.Errorf("can't find dexpreopt outputs")
1725 }
1726
1727 // Check cert signing flag.
1728 signedApk := variant.Output("signed/foo.apk")
1729 signingFlag := signedApk.Args["certificates"]
1730 expected := "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8"
1731 if expected != signingFlag {
1732 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
1733 }
1734}
1735
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001736func TestAndroidAppImport_DpiVariants(t *testing.T) {
1737 bp := `
1738 android_app_import {
1739 name: "foo",
1740 apk: "prebuilts/apk/app.apk",
1741 dpi_variants: {
1742 xhdpi: {
1743 apk: "prebuilts/apk/app_xhdpi.apk",
1744 },
1745 xxhdpi: {
1746 apk: "prebuilts/apk/app_xxhdpi.apk",
1747 },
1748 },
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001749 presigned: true,
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001750 dex_preopt: {
1751 enabled: true,
1752 },
1753 }
1754 `
1755 testCases := []struct {
1756 name string
1757 aaptPreferredConfig *string
1758 aaptPrebuiltDPI []string
1759 expected string
1760 }{
1761 {
1762 name: "no preferred",
1763 aaptPreferredConfig: nil,
1764 aaptPrebuiltDPI: []string{},
1765 expected: "prebuilts/apk/app.apk",
1766 },
1767 {
1768 name: "AAPTPreferredConfig matches",
1769 aaptPreferredConfig: proptools.StringPtr("xhdpi"),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001770 aaptPrebuiltDPI: []string{"xxhdpi", "ldpi"},
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001771 expected: "prebuilts/apk/app_xhdpi.apk",
1772 },
1773 {
1774 name: "AAPTPrebuiltDPI matches",
1775 aaptPreferredConfig: proptools.StringPtr("mdpi"),
1776 aaptPrebuiltDPI: []string{"xxhdpi", "xhdpi"},
1777 expected: "prebuilts/apk/app_xxhdpi.apk",
1778 },
1779 {
1780 name: "non-first AAPTPrebuiltDPI matches",
1781 aaptPreferredConfig: proptools.StringPtr("mdpi"),
1782 aaptPrebuiltDPI: []string{"ldpi", "xhdpi"},
1783 expected: "prebuilts/apk/app_xhdpi.apk",
1784 },
1785 {
1786 name: "no matches",
1787 aaptPreferredConfig: proptools.StringPtr("mdpi"),
1788 aaptPrebuiltDPI: []string{"ldpi", "xxxhdpi"},
1789 expected: "prebuilts/apk/app.apk",
1790 },
1791 }
1792
1793 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
1794 for _, test := range testCases {
Colin Cross98be1bb2019-12-13 20:41:13 -08001795 config := testAppConfig(nil, bp, nil)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001796 config.TestProductVariables.AAPTPreferredConfig = test.aaptPreferredConfig
1797 config.TestProductVariables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI
Colin Cross98be1bb2019-12-13 20:41:13 -08001798 ctx := testContext()
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001799
1800 run(t, ctx, config)
1801
1802 variant := ctx.ModuleForTests("foo", "android_common")
1803 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
1804 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
1805 if len(matches) != 2 {
1806 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
1807 }
1808 if test.expected != matches[1] {
1809 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
1810 }
1811 }
1812}
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001813
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001814func TestAndroidAppImport_Filename(t *testing.T) {
1815 ctx, config := testJava(t, `
1816 android_app_import {
1817 name: "foo",
1818 apk: "prebuilts/apk/app.apk",
1819 presigned: true,
1820 }
1821
1822 android_app_import {
1823 name: "bar",
1824 apk: "prebuilts/apk/app.apk",
1825 presigned: true,
1826 filename: "bar_sample.apk"
1827 }
1828 `)
1829
1830 testCases := []struct {
1831 name string
1832 expected string
1833 }{
1834 {
1835 name: "foo",
1836 expected: "foo.apk",
1837 },
1838 {
1839 name: "bar",
1840 expected: "bar_sample.apk",
1841 },
1842 }
1843
1844 for _, test := range testCases {
1845 variant := ctx.ModuleForTests(test.name, "android_common")
1846 if variant.MaybeOutput(test.expected).Rule == nil {
1847 t.Errorf("can't find output named %q - all outputs: %v", test.expected, variant.AllOutputs())
1848 }
1849
1850 a := variant.Module().(*AndroidAppImport)
1851 expectedValues := []string{test.expected}
1852 actualValues := android.AndroidMkEntriesForTest(
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001853 t, config, "", a)[0].EntryMap["LOCAL_INSTALLED_MODULE_STEM"]
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001854 if !reflect.DeepEqual(actualValues, expectedValues) {
1855 t.Errorf("Incorrect LOCAL_INSTALLED_MODULE_STEM value '%s', expected '%s'",
1856 actualValues, expectedValues)
1857 }
1858 }
1859}
1860
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001861func TestAndroidAppImport_ArchVariants(t *testing.T) {
1862 // The test config's target arch is ARM64.
1863 testCases := []struct {
1864 name string
1865 bp string
1866 expected string
1867 }{
1868 {
1869 name: "matching arch",
1870 bp: `
1871 android_app_import {
1872 name: "foo",
1873 apk: "prebuilts/apk/app.apk",
1874 arch: {
1875 arm64: {
1876 apk: "prebuilts/apk/app_arm64.apk",
1877 },
1878 },
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001879 presigned: true,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001880 dex_preopt: {
1881 enabled: true,
1882 },
1883 }
1884 `,
1885 expected: "prebuilts/apk/app_arm64.apk",
1886 },
1887 {
1888 name: "no matching arch",
1889 bp: `
1890 android_app_import {
1891 name: "foo",
1892 apk: "prebuilts/apk/app.apk",
1893 arch: {
1894 arm: {
1895 apk: "prebuilts/apk/app_arm.apk",
1896 },
1897 },
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001898 presigned: true,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001899 dex_preopt: {
1900 enabled: true,
1901 },
1902 }
1903 `,
1904 expected: "prebuilts/apk/app.apk",
1905 },
1906 }
1907
1908 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
1909 for _, test := range testCases {
1910 ctx, _ := testJava(t, test.bp)
1911
1912 variant := ctx.ModuleForTests("foo", "android_common")
1913 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
1914 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
1915 if len(matches) != 2 {
1916 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
1917 }
1918 if test.expected != matches[1] {
1919 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
1920 }
1921 }
1922}
1923
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001924func TestAndroidTestImport(t *testing.T) {
1925 ctx, config := testJava(t, `
1926 android_test_import {
1927 name: "foo",
1928 apk: "prebuilts/apk/app.apk",
1929 presigned: true,
1930 data: [
1931 "testdata/data",
1932 ],
1933 }
1934 `)
1935
1936 test := ctx.ModuleForTests("foo", "android_common").Module().(*AndroidTestImport)
1937
1938 // Check android mks.
Jiyong Park0b0e1b92019-12-03 13:24:29 +09001939 entries := android.AndroidMkEntriesForTest(t, config, "", test)[0]
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001940 expected := []string{"tests"}
1941 actual := entries.EntryMap["LOCAL_MODULE_TAGS"]
1942 if !reflect.DeepEqual(expected, actual) {
1943 t.Errorf("Unexpected module tags - expected: %q, actual: %q", expected, actual)
1944 }
1945 expected = []string{"testdata/data:testdata/data"}
1946 actual = entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
1947 if !reflect.DeepEqual(expected, actual) {
1948 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
1949 }
1950}
1951
Jaewoong Jung7c5bd832020-01-13 09:55:39 -08001952func TestAndroidTestImport_NoJinUncompressForPresigned(t *testing.T) {
1953 ctx, _ := testJava(t, `
1954 android_test_import {
1955 name: "foo",
1956 apk: "prebuilts/apk/app.apk",
1957 certificate: "cert/new_cert",
1958 data: [
1959 "testdata/data",
1960 ],
1961 }
1962
1963 android_test_import {
1964 name: "foo_presigned",
1965 apk: "prebuilts/apk/app.apk",
1966 presigned: true,
1967 data: [
1968 "testdata/data",
1969 ],
1970 }
1971 `)
1972
1973 variant := ctx.ModuleForTests("foo", "android_common")
1974 jniRule := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
1975 if !strings.HasPrefix(jniRule, "if (zipinfo") {
1976 t.Errorf("Unexpected JNI uncompress rule command: " + jniRule)
1977 }
1978
1979 variant = ctx.ModuleForTests("foo_presigned", "android_common")
1980 jniRule = variant.Output("jnis-uncompressed/foo_presigned.apk").BuildParams.Rule.String()
1981 if jniRule != android.Cp.String() {
1982 t.Errorf("Unexpected JNI uncompress rule: " + jniRule)
1983 }
1984}
1985
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001986func TestStl(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001987 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001988 cc_library {
1989 name: "libjni",
Peter Collingbournead84f972019-12-17 16:46:18 -08001990 sdk_version: "current",
1991 stl: "c++_shared",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001992 }
1993
1994 android_test {
1995 name: "stl",
1996 jni_libs: ["libjni"],
1997 compile_multilib: "both",
1998 sdk_version: "current",
1999 stl: "c++_shared",
2000 }
2001
2002 android_test {
2003 name: "system",
2004 jni_libs: ["libjni"],
2005 compile_multilib: "both",
2006 sdk_version: "current",
2007 }
2008 `)
2009
2010 testCases := []struct {
2011 name string
2012 jnis []string
2013 }{
2014 {"stl",
2015 []string{
2016 "libjni.so",
Jaewoong Jung710756a2019-06-04 11:53:47 -07002017 "libc++_shared.so",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07002018 },
2019 },
2020 {"system",
2021 []string{
2022 "libjni.so",
2023 },
2024 },
2025 }
2026
2027 for _, test := range testCases {
2028 t.Run(test.name, func(t *testing.T) {
2029 app := ctx.ModuleForTests(test.name, "android_common")
2030 jniLibZip := app.Output("jnilibs.zip")
2031 var jnis []string
2032 args := strings.Fields(jniLibZip.Args["jarArgs"])
2033 for i := 0; i < len(args); i++ {
2034 if args[i] == "-f" {
2035 jnis = append(jnis, args[i+1])
2036 i += 1
2037 }
2038 }
2039 jnisJoined := strings.Join(jnis, " ")
2040 for _, jni := range test.jnis {
2041 if !strings.Contains(jnisJoined, jni) {
2042 t.Errorf("missing jni %q in %q", jni, jnis)
2043 }
2044 }
2045 })
2046 }
2047}
Colin Cross50ddcc42019-05-16 12:28:22 -07002048
2049func TestUsesLibraries(t *testing.T) {
2050 bp := `
2051 java_sdk_library {
2052 name: "foo",
2053 srcs: ["a.java"],
2054 api_packages: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002055 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002056 }
2057
2058 java_sdk_library {
2059 name: "bar",
2060 srcs: ["a.java"],
2061 api_packages: ["bar"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002062 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002063 }
2064
2065 android_app {
2066 name: "app",
2067 srcs: ["a.java"],
2068 uses_libs: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002069 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07002070 optional_uses_libs: [
2071 "bar",
2072 "baz",
2073 ],
2074 }
2075
2076 android_app_import {
2077 name: "prebuilt",
2078 apk: "prebuilts/apk/app.apk",
2079 certificate: "platform",
2080 uses_libs: ["foo"],
2081 optional_uses_libs: [
2082 "bar",
2083 "baz",
2084 ],
2085 }
2086 `
2087
Colin Cross98be1bb2019-12-13 20:41:13 -08002088 config := testAppConfig(nil, bp, nil)
Colin Cross50ddcc42019-05-16 12:28:22 -07002089 config.TestProductVariables.MissingUsesLibraries = []string{"baz"}
2090
Colin Cross98be1bb2019-12-13 20:41:13 -08002091 ctx := testContext()
Colin Cross50ddcc42019-05-16 12:28:22 -07002092
2093 run(t, ctx, config)
2094
2095 app := ctx.ModuleForTests("app", "android_common")
2096 prebuilt := ctx.ModuleForTests("prebuilt", "android_common")
2097
2098 // Test that all libraries are verified
2099 cmd := app.Rule("verify_uses_libraries").RuleParams.Command
2100 if w := "--uses-library foo"; !strings.Contains(cmd, w) {
2101 t.Errorf("wanted %q in %q", w, cmd)
2102 }
2103
2104 if w := "--optional-uses-library bar --optional-uses-library baz"; !strings.Contains(cmd, w) {
2105 t.Errorf("wanted %q in %q", w, cmd)
2106 }
2107
2108 cmd = prebuilt.Rule("verify_uses_libraries").RuleParams.Command
2109
2110 if w := `uses_library_names="foo"`; !strings.Contains(cmd, w) {
2111 t.Errorf("wanted %q in %q", w, cmd)
2112 }
2113
2114 if w := `optional_uses_library_names="bar baz"`; !strings.Contains(cmd, w) {
2115 t.Errorf("wanted %q in %q", w, cmd)
2116 }
2117
2118 // Test that only present libraries are preopted
2119 cmd = app.Rule("dexpreopt").RuleParams.Command
2120
2121 if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) {
2122 t.Errorf("wanted %q in %q", w, cmd)
2123 }
2124
2125 cmd = prebuilt.Rule("dexpreopt").RuleParams.Command
2126
2127 if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) {
2128 t.Errorf("wanted %q in %q", w, cmd)
2129 }
2130}
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002131
2132func TestCodelessApp(t *testing.T) {
2133 testCases := []struct {
2134 name string
2135 bp string
2136 noCode bool
2137 }{
2138 {
2139 name: "normal",
2140 bp: `
2141 android_app {
2142 name: "foo",
2143 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002144 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002145 }
2146 `,
2147 noCode: false,
2148 },
2149 {
2150 name: "app without sources",
2151 bp: `
2152 android_app {
2153 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002154 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002155 }
2156 `,
2157 noCode: true,
2158 },
2159 {
2160 name: "app with libraries",
2161 bp: `
2162 android_app {
2163 name: "foo",
2164 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002165 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002166 }
2167
2168 java_library {
2169 name: "lib",
2170 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002171 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002172 }
2173 `,
2174 noCode: false,
2175 },
2176 {
2177 name: "app with sourceless libraries",
2178 bp: `
2179 android_app {
2180 name: "foo",
2181 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002182 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002183 }
2184
2185 java_library {
2186 name: "lib",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002187 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07002188 }
2189 `,
2190 // TODO(jungjw): this should probably be true
2191 noCode: false,
2192 },
2193 }
2194
2195 for _, test := range testCases {
2196 t.Run(test.name, func(t *testing.T) {
2197 ctx := testApp(t, test.bp)
2198
2199 foo := ctx.ModuleForTests("foo", "android_common")
2200 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
2201 if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode {
2202 t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs)
2203 }
2204 })
2205 }
2206}
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002207
2208func TestEmbedNotice(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07002209 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002210 android_app {
2211 name: "foo",
2212 srcs: ["a.java"],
2213 static_libs: ["javalib"],
2214 jni_libs: ["libjni"],
2215 notice: "APP_NOTICE",
2216 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09002217 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002218 }
2219
2220 // No embed_notice flag
2221 android_app {
2222 name: "bar",
2223 srcs: ["a.java"],
2224 jni_libs: ["libjni"],
2225 notice: "APP_NOTICE",
Jeongik Cha538c0d02019-07-11 15:54:27 +09002226 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002227 }
2228
2229 // No NOTICE files
2230 android_app {
2231 name: "baz",
2232 srcs: ["a.java"],
2233 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09002234 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002235 }
2236
2237 cc_library {
2238 name: "libjni",
2239 system_shared_libs: [],
2240 stl: "none",
2241 notice: "LIB_NOTICE",
Colin Cross094cde42020-02-15 10:38:00 -08002242 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002243 }
2244
2245 java_library {
2246 name: "javalib",
2247 srcs: [
2248 ":gen",
2249 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002250 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002251 }
2252
2253 genrule {
2254 name: "gen",
2255 tools: ["gentool"],
2256 out: ["gen.java"],
2257 notice: "GENRULE_NOTICE",
2258 }
2259
2260 java_binary_host {
2261 name: "gentool",
2262 srcs: ["b.java"],
2263 notice: "TOOL_NOTICE",
2264 }
2265 `)
2266
2267 // foo has NOTICE files to process, and embed_notices is true.
2268 foo := ctx.ModuleForTests("foo", "android_common")
2269 // verify merge notices rule.
2270 mergeNotices := foo.Rule("mergeNoticesRule")
2271 noticeInputs := mergeNotices.Inputs.Strings()
2272 // TOOL_NOTICE should be excluded as it's a host module.
2273 if len(mergeNotices.Inputs) != 3 {
2274 t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs)
2275 }
2276 if !inList("APP_NOTICE", noticeInputs) {
2277 t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs)
2278 }
2279 if !inList("LIB_NOTICE", noticeInputs) {
2280 t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs)
2281 }
2282 if !inList("GENRULE_NOTICE", noticeInputs) {
2283 t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs)
2284 }
2285 // aapt2 flags should include -A <NOTICE dir> so that its contents are put in the APK's /assets.
2286 res := foo.Output("package-res.apk")
2287 aapt2Flags := res.Args["flags"]
2288 e := "-A " + buildDir + "/.intermediates/foo/android_common/NOTICE"
2289 if !strings.Contains(aapt2Flags, e) {
2290 t.Errorf("asset dir flag for NOTICE, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
2291 }
2292
2293 // bar has NOTICE files to process, but embed_notices is not set.
2294 bar := ctx.ModuleForTests("bar", "android_common")
Jaewoong Jung98772792019-07-01 17:15:13 -07002295 res = bar.Output("package-res.apk")
2296 aapt2Flags = res.Args["flags"]
2297 e = "-A " + buildDir + "/.intermediates/bar/android_common/NOTICE"
2298 if strings.Contains(aapt2Flags, e) {
2299 t.Errorf("bar shouldn't have the asset dir flag for NOTICE: %q", e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002300 }
2301
2302 // baz's embed_notice is true, but it doesn't have any NOTICE files.
2303 baz := ctx.ModuleForTests("baz", "android_common")
Jaewoong Jung98772792019-07-01 17:15:13 -07002304 res = baz.Output("package-res.apk")
2305 aapt2Flags = res.Args["flags"]
2306 e = "-A " + buildDir + "/.intermediates/baz/android_common/NOTICE"
2307 if strings.Contains(aapt2Flags, e) {
2308 t.Errorf("baz shouldn't have the asset dir flag for NOTICE: %q", e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07002309 }
2310}
Colin Cross53a87f52019-06-25 13:35:30 -07002311
2312func TestUncompressDex(t *testing.T) {
2313 testCases := []struct {
2314 name string
2315 bp string
2316
2317 uncompressedPlatform bool
2318 uncompressedUnbundled bool
2319 }{
2320 {
2321 name: "normal",
2322 bp: `
2323 android_app {
2324 name: "foo",
2325 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002326 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002327 }
2328 `,
2329 uncompressedPlatform: true,
2330 uncompressedUnbundled: false,
2331 },
2332 {
2333 name: "use_embedded_dex",
2334 bp: `
2335 android_app {
2336 name: "foo",
2337 use_embedded_dex: true,
2338 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002339 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002340 }
2341 `,
2342 uncompressedPlatform: true,
2343 uncompressedUnbundled: true,
2344 },
2345 {
2346 name: "privileged",
2347 bp: `
2348 android_app {
2349 name: "foo",
2350 privileged: true,
2351 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09002352 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07002353 }
2354 `,
2355 uncompressedPlatform: true,
2356 uncompressedUnbundled: true,
2357 },
2358 }
2359
2360 test := func(t *testing.T, bp string, want bool, unbundled bool) {
2361 t.Helper()
2362
Colin Cross98be1bb2019-12-13 20:41:13 -08002363 config := testAppConfig(nil, bp, nil)
Colin Cross53a87f52019-06-25 13:35:30 -07002364 if unbundled {
2365 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
2366 }
2367
Colin Cross98be1bb2019-12-13 20:41:13 -08002368 ctx := testContext()
Colin Cross53a87f52019-06-25 13:35:30 -07002369
2370 run(t, ctx, config)
2371
2372 foo := ctx.ModuleForTests("foo", "android_common")
2373 dex := foo.Rule("r8")
2374 uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0")
2375 aligned := foo.MaybeRule("zipalign").Rule != nil
2376
2377 if uncompressedInDexJar != want {
2378 t.Errorf("want uncompressed in dex %v, got %v", want, uncompressedInDexJar)
2379 }
2380
2381 if aligned != want {
2382 t.Errorf("want aligned %v, got %v", want, aligned)
2383 }
2384 }
2385
2386 for _, tt := range testCases {
2387 t.Run(tt.name, func(t *testing.T) {
2388 t.Run("platform", func(t *testing.T) {
2389 test(t, tt.bp, tt.uncompressedPlatform, false)
2390 })
2391 t.Run("unbundled", func(t *testing.T) {
2392 test(t, tt.bp, tt.uncompressedUnbundled, true)
2393 })
2394 })
2395 }
2396}
Jaewoong Jung26dedd32019-06-06 08:45:58 -07002397
2398func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) {
2399 if expectedValue != "" {
2400 expectedFlag := "--" + flagName + " " + expectedValue
2401 if !strings.Contains(aapt2Flags, expectedFlag) {
2402 t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags)
2403 }
2404 } else {
2405 unexpectedFlag := "--" + flagName
2406 if strings.Contains(aapt2Flags, unexpectedFlag) {
2407 t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags)
2408 }
2409 }
2410}
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002411
2412func TestRuntimeResourceOverlay(t *testing.T) {
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07002413 fs := map[string][]byte{
2414 "baz/res/res/values/strings.xml": nil,
2415 "bar/res/res/values/strings.xml": nil,
2416 }
2417 bp := `
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002418 runtime_resource_overlay {
2419 name: "foo",
2420 certificate: "platform",
2421 product_specific: true,
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07002422 static_libs: ["bar"],
2423 resource_libs: ["baz"],
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08002424 aaptflags: ["--keep-raw-values"],
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002425 }
2426
2427 runtime_resource_overlay {
2428 name: "foo_themed",
2429 certificate: "platform",
2430 product_specific: true,
2431 theme: "faza",
Jaewoong Jungad0177b2020-04-24 15:22:40 -07002432 overrides: ["foo"],
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002433 }
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07002434
2435 android_library {
2436 name: "bar",
2437 resource_dirs: ["bar/res"],
2438 }
2439
2440 android_app {
2441 name: "baz",
2442 sdk_version: "current",
2443 resource_dirs: ["baz/res"],
2444 }
2445 `
2446 config := testAppConfig(nil, bp, fs)
2447 ctx := testContext()
2448 run(t, ctx, config)
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002449
2450 m := ctx.ModuleForTests("foo", "android_common")
2451
Jaewoong Jungf0f747c2020-01-24 10:30:02 -08002452 // Check AAPT2 link flags.
2453 aapt2Flags := m.Output("package-res.apk").Args["flags"]
2454 expectedFlags := []string{"--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"}
2455 absentFlags := android.RemoveListFromList(expectedFlags, strings.Split(aapt2Flags, " "))
2456 if len(absentFlags) > 0 {
2457 t.Errorf("expected values, %q are missing in aapt2 link flags, %q", absentFlags, aapt2Flags)
2458 }
2459
Jaewoong Jungfe3c7f62020-04-09 16:15:30 -07002460 // Check overlay.list output for static_libs dependency.
2461 overlayList := m.Output("aapt2/overlay.list").Inputs.Strings()
2462 staticLibPackage := buildDir + "/.intermediates/bar/android_common/package-res.apk"
2463 if !inList(staticLibPackage, overlayList) {
2464 t.Errorf("Stactic lib res package %q missing in overlay list: %q", staticLibPackage, overlayList)
2465 }
2466
2467 // Check AAPT2 link flags for resource_libs dependency.
2468 resourceLibFlag := "-I " + buildDir + "/.intermediates/baz/android_common/package-res.apk"
2469 if !strings.Contains(aapt2Flags, resourceLibFlag) {
2470 t.Errorf("Resource lib flag %q missing in aapt2 link flags: %q", resourceLibFlag, aapt2Flags)
2471 }
2472
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002473 // Check cert signing flag.
2474 signedApk := m.Output("signed/foo.apk")
2475 signingFlag := signedApk.Args["certificates"]
2476 expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8"
2477 if expected != signingFlag {
2478 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
2479 }
Jaewoong Jungad0177b2020-04-24 15:22:40 -07002480 androidMkEntries := android.AndroidMkEntriesForTest(t, config, "", m.Module())[0]
2481 path := androidMkEntries.EntryMap["LOCAL_CERTIFICATE"]
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08002482 expectedPath := []string{"build/make/target/product/security/platform.x509.pem"}
2483 if !reflect.DeepEqual(path, expectedPath) {
2484 t.Errorf("Unexpected LOCAL_CERTIFICATE value: %v, expected: %v", path, expectedPath)
2485 }
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002486
2487 // Check device location.
Jaewoong Jungad0177b2020-04-24 15:22:40 -07002488 path = androidMkEntries.EntryMap["LOCAL_MODULE_PATH"]
Jaewoong Jung78ec5d82020-01-31 10:11:47 -08002489 expectedPath = []string{"/tmp/target/product/test_device/product/overlay"}
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002490 if !reflect.DeepEqual(path, expectedPath) {
2491 t.Errorf("Unexpected LOCAL_MODULE_PATH value: %v, expected: %v", path, expectedPath)
2492 }
2493
2494 // A themed module has a different device location
2495 m = ctx.ModuleForTests("foo_themed", "android_common")
Jaewoong Jungad0177b2020-04-24 15:22:40 -07002496 androidMkEntries = android.AndroidMkEntriesForTest(t, config, "", m.Module())[0]
2497 path = androidMkEntries.EntryMap["LOCAL_MODULE_PATH"]
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002498 expectedPath = []string{"/tmp/target/product/test_device/product/overlay/faza"}
2499 if !reflect.DeepEqual(path, expectedPath) {
2500 t.Errorf("Unexpected LOCAL_MODULE_PATH value: %v, expected: %v", path, expectedPath)
2501 }
Jaewoong Jungad0177b2020-04-24 15:22:40 -07002502
2503 overrides := androidMkEntries.EntryMap["LOCAL_OVERRIDES_PACKAGES"]
2504 expectedOverrides := []string{"foo"}
2505 if !reflect.DeepEqual(overrides, expectedOverrides) {
2506 t.Errorf("Unexpected LOCAL_OVERRIDES_PACKAGES value: %v, expected: %v", overrides, expectedOverrides)
2507 }
Jaewoong Jung9befb0c2020-01-18 10:33:43 -08002508}
Roshan Pius4df2bc72020-04-27 09:42:27 -07002509
2510func TestOverrideRuntimeResourceOverlay(t *testing.T) {
2511 ctx, _ := testJava(t, `
2512 runtime_resource_overlay {
2513 name: "foo_overlay",
2514 certificate: "platform",
2515 product_specific: true,
2516 sdk_version: "current",
2517 }
2518
2519 override_runtime_resource_overlay {
2520 name: "bar_overlay",
2521 base: "foo_overlay",
2522 package_name: "com.android.bar.overlay",
2523 target_package_name: "com.android.bar",
2524 }
2525 `)
2526
2527 expectedVariants := []struct {
2528 moduleName string
2529 variantName string
2530 apkPath string
2531 overrides []string
2532 targetVariant string
2533 packageFlag string
2534 targetPackageFlag string
2535 }{
2536 {
2537 variantName: "android_common",
2538 apkPath: "/target/product/test_device/product/overlay/foo_overlay.apk",
2539 overrides: nil,
2540 targetVariant: "android_common",
2541 packageFlag: "",
2542 targetPackageFlag: "",
2543 },
2544 {
2545 variantName: "android_common_bar_overlay",
2546 apkPath: "/target/product/test_device/product/overlay/bar_overlay.apk",
2547 overrides: []string{"foo_overlay"},
2548 targetVariant: "android_common_bar",
2549 packageFlag: "com.android.bar.overlay",
2550 targetPackageFlag: "com.android.bar",
2551 },
2552 }
2553 for _, expected := range expectedVariants {
2554 variant := ctx.ModuleForTests("foo_overlay", expected.variantName)
2555
2556 // Check the final apk name
2557 outputs := variant.AllOutputs()
2558 expectedApkPath := buildDir + expected.apkPath
2559 found := false
2560 for _, o := range outputs {
2561 if o == expectedApkPath {
2562 found = true
2563 break
2564 }
2565 }
2566 if !found {
2567 t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs)
2568 }
2569
2570 // Check if the overrides field values are correctly aggregated.
2571 mod := variant.Module().(*RuntimeResourceOverlay)
2572 if !reflect.DeepEqual(expected.overrides, mod.properties.Overrides) {
2573 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
2574 expected.overrides, mod.properties.Overrides)
2575 }
2576
2577 // Check aapt2 flags.
2578 res := variant.Output("package-res.apk")
2579 aapt2Flags := res.Args["flags"]
2580 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
2581 checkAapt2LinkFlag(t, aapt2Flags, "rename-overlay-target-package", expected.targetPackageFlag)
2582 }
2583}
Jaewoong Jungbf135462020-04-26 15:10:51 -07002584
2585func TestRuntimeResourceOverlay_JavaDefaults(t *testing.T) {
2586 ctx, config := testJava(t, `
2587 java_defaults {
2588 name: "rro_defaults",
2589 theme: "default_theme",
2590 product_specific: true,
2591 aaptflags: ["--keep-raw-values"],
2592 }
2593
2594 runtime_resource_overlay {
2595 name: "foo_with_defaults",
2596 defaults: ["rro_defaults"],
2597 }
2598
2599 runtime_resource_overlay {
2600 name: "foo_barebones",
2601 }
2602 `)
2603
2604 //
2605 // RRO module with defaults
2606 //
2607 m := ctx.ModuleForTests("foo_with_defaults", "android_common")
2608
2609 // Check AAPT2 link flags.
2610 aapt2Flags := strings.Split(m.Output("package-res.apk").Args["flags"], " ")
2611 expectedFlags := []string{"--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"}
2612 absentFlags := android.RemoveListFromList(expectedFlags, aapt2Flags)
2613 if len(absentFlags) > 0 {
2614 t.Errorf("expected values, %q are missing in aapt2 link flags, %q", absentFlags, aapt2Flags)
2615 }
2616
2617 // Check device location.
2618 path := android.AndroidMkEntriesForTest(t, config, "", m.Module())[0].EntryMap["LOCAL_MODULE_PATH"]
2619 expectedPath := []string{"/tmp/target/product/test_device/product/overlay/default_theme"}
2620 if !reflect.DeepEqual(path, expectedPath) {
2621 t.Errorf("Unexpected LOCAL_MODULE_PATH value: %q, expected: %q", path, expectedPath)
2622 }
2623
2624 //
2625 // RRO module without defaults
2626 //
2627 m = ctx.ModuleForTests("foo_barebones", "android_common")
2628
2629 // Check AAPT2 link flags.
2630 aapt2Flags = strings.Split(m.Output("package-res.apk").Args["flags"], " ")
2631 unexpectedFlags := "--keep-raw-values"
2632 if inList(unexpectedFlags, aapt2Flags) {
2633 t.Errorf("unexpected value, %q is present in aapt2 link flags, %q", unexpectedFlags, aapt2Flags)
2634 }
2635
2636 // Check device location.
2637 path = android.AndroidMkEntriesForTest(t, config, "", m.Module())[0].EntryMap["LOCAL_MODULE_PATH"]
2638 expectedPath = []string{"/tmp/target/product/test_device/system/overlay"}
2639 if !reflect.DeepEqual(path, expectedPath) {
2640 t.Errorf("Unexpected LOCAL_MODULE_PATH value: %v, expected: %v", path, expectedPath)
2641 }
2642}