blob: 1784fc3b0ed940fdadcc38680e8f5f918b3f9631 [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 (
18 "android/soong/android"
Colin Crossd09b0b62018-04-18 11:06:47 -070019 "fmt"
Colin Crossa4f08812018-10-02 22:03:40 -070020 "path/filepath"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080021 "reflect"
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"
25)
26
27var (
28 resourceFiles = []string{
29 "res/layout/layout.xml",
30 "res/values/strings.xml",
31 "res/values-en-rUS/strings.xml",
32 }
33
34 compiledResourceFiles = []string{
35 "aapt2/res/layout_layout.xml.flat",
36 "aapt2/res/values_strings.arsc.flat",
37 "aapt2/res/values-en-rUS_strings.arsc.flat",
38 }
39)
40
Colin Cross527012a2017-11-30 22:56:16 -080041func testAppContext(config android.Config, bp string, fs map[string][]byte) *android.TestContext {
42 appFS := map[string][]byte{}
43 for k, v := range fs {
44 appFS[k] = v
Colin Cross3bc7ffa2017-11-22 16:19:37 -080045 }
46
Colin Cross527012a2017-11-30 22:56:16 -080047 for _, file := range resourceFiles {
48 appFS[file] = nil
49 }
50
51 return testContext(config, bp, appFS)
52}
53
54func testApp(t *testing.T, bp string) *android.TestContext {
55 config := testConfig(nil)
56
57 ctx := testAppContext(config, bp, nil)
58
59 run(t, ctx, config)
60
61 return ctx
Colin Cross3bc7ffa2017-11-22 16:19:37 -080062}
63
64func TestApp(t *testing.T) {
Colin Crossa97c5d32018-03-28 14:58:31 -070065 for _, moduleType := range []string{"android_app", "android_library"} {
66 t.Run(moduleType, func(t *testing.T) {
67 ctx := testApp(t, moduleType+` {
68 name: "foo",
69 srcs: ["a.java"],
70 }
71 `)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080072
Colin Crossa97c5d32018-03-28 14:58:31 -070073 foo := ctx.ModuleForTests("foo", "android_common")
Colin Cross3bc7ffa2017-11-22 16:19:37 -080074
Colin Cross31656952018-05-24 16:11:20 -070075 var expectedLinkImplicits []string
76
77 manifestFixer := foo.Output("manifest_fixer/AndroidManifest.xml")
78 expectedLinkImplicits = append(expectedLinkImplicits, manifestFixer.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080079
Colin Crossa97c5d32018-03-28 14:58:31 -070080 frameworkRes := ctx.ModuleForTests("framework-res", "android_common")
81 expectedLinkImplicits = append(expectedLinkImplicits,
82 frameworkRes.Output("package-res.apk").Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080083
Colin Crossa97c5d32018-03-28 14:58:31 -070084 // Test the mapping from input files to compiled output file names
85 compile := foo.Output(compiledResourceFiles[0])
86 if !reflect.DeepEqual(resourceFiles, compile.Inputs.Strings()) {
87 t.Errorf("expected aapt2 compile inputs expected:\n %#v\n got:\n %#v",
88 resourceFiles, compile.Inputs.Strings())
89 }
Colin Crossb69301e2017-12-01 10:48:26 -080090
Colin Crossa97c5d32018-03-28 14:58:31 -070091 compiledResourceOutputs := compile.Outputs.Strings()
92 sort.Strings(compiledResourceOutputs)
Colin Crossb69301e2017-12-01 10:48:26 -080093
Colin Crossa97c5d32018-03-28 14:58:31 -070094 expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080095
Colin Crossa97c5d32018-03-28 14:58:31 -070096 list := foo.Output("aapt2/res.list")
97 expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080098
Colin Crossa97c5d32018-03-28 14:58:31 -070099 // Check that the link rule uses
100 res := ctx.ModuleForTests("foo", "android_common").Output("package-res.apk")
101 if !reflect.DeepEqual(expectedLinkImplicits, res.Implicits.Strings()) {
102 t.Errorf("expected aapt2 link implicits expected:\n %#v\n got:\n %#v",
103 expectedLinkImplicits, res.Implicits.Strings())
104 }
105 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800106 }
107}
Colin Cross890ff552017-11-30 20:13:19 -0800108
Colin Cross0ddae7f2019-02-07 15:30:01 -0800109func TestResourceDirs(t *testing.T) {
110 testCases := []struct {
111 name string
112 prop string
113 resources []string
114 }{
115 {
116 name: "no resource_dirs",
117 prop: "",
118 resources: []string{"res/res/values/strings.xml"},
119 },
120 {
121 name: "resource_dirs",
122 prop: `resource_dirs: ["res"]`,
123 resources: []string{"res/res/values/strings.xml"},
124 },
125 {
126 name: "empty resource_dirs",
127 prop: `resource_dirs: []`,
128 resources: nil,
129 },
130 }
131
132 fs := map[string][]byte{
133 "res/res/values/strings.xml": nil,
134 }
135
136 bp := `
137 android_app {
138 name: "foo",
139 %s
140 }
141 `
142
143 for _, testCase := range testCases {
144 t.Run(testCase.name, func(t *testing.T) {
145 config := testConfig(nil)
146 ctx := testContext(config, fmt.Sprintf(bp, testCase.prop), fs)
147 run(t, ctx, config)
148
149 module := ctx.ModuleForTests("foo", "android_common")
150 resourceList := module.MaybeOutput("aapt2/res.list")
151
152 var resources []string
153 if resourceList.Rule != nil {
154 for _, compiledResource := range resourceList.Inputs.Strings() {
155 resources = append(resources, module.Output(compiledResource).Inputs.Strings()...)
156 }
157 }
158
159 if !reflect.DeepEqual(resources, testCase.resources) {
160 t.Errorf("expected resource files %q, got %q",
161 testCase.resources, resources)
162 }
163 })
164 }
165}
166
Colin Crossbec85302019-02-13 13:15:46 -0800167func TestAndroidResources(t *testing.T) {
Colin Cross5c4791c2019-02-01 11:44:44 -0800168 testCases := []struct {
169 name string
170 enforceRROTargets []string
171 enforceRROExcludedOverlays []string
Colin Crossbec85302019-02-13 13:15:46 -0800172 resourceFiles map[string][]string
Colin Cross5c4791c2019-02-01 11:44:44 -0800173 overlayFiles map[string][]string
174 rroDirs map[string][]string
175 }{
176 {
177 name: "no RRO",
178 enforceRROTargets: nil,
179 enforceRROExcludedOverlays: nil,
Colin Crossbec85302019-02-13 13:15:46 -0800180 resourceFiles: map[string][]string{
181 "foo": nil,
182 "bar": {"bar/res/res/values/strings.xml"},
183 "lib": nil,
184 "lib2": {"lib2/res/res/values/strings.xml"},
185 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800186 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800187 "foo": {
188 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800189 buildDir + "/.intermediates/lib/android_common/package-res.apk",
190 "foo/res/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800191 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
192 "device/vendor/blah/overlay/foo/res/values/strings.xml",
193 },
Colin Crossbec85302019-02-13 13:15:46 -0800194 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800195 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
196 "device/vendor/blah/overlay/bar/res/values/strings.xml",
197 },
Colin Crossbec85302019-02-13 13:15:46 -0800198 "lib": {
199 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
200 "lib/res/res/values/strings.xml",
201 "device/vendor/blah/overlay/lib/res/values/strings.xml",
202 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800203 },
204 rroDirs: map[string][]string{
205 "foo": nil,
206 "bar": nil,
207 },
208 },
209 {
210 name: "enforce RRO on foo",
211 enforceRROTargets: []string{"foo"},
212 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Colin Crossbec85302019-02-13 13:15:46 -0800213 resourceFiles: map[string][]string{
214 "foo": nil,
215 "bar": {"bar/res/res/values/strings.xml"},
216 "lib": nil,
217 "lib2": {"lib2/res/res/values/strings.xml"},
218 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800219 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800220 "foo": {
221 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800222 buildDir + "/.intermediates/lib/android_common/package-res.apk",
223 "foo/res/res/values/strings.xml",
224 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
225 },
Colin Crossbec85302019-02-13 13:15:46 -0800226 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800227 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
228 "device/vendor/blah/overlay/bar/res/values/strings.xml",
229 },
Colin Crossbec85302019-02-13 13:15:46 -0800230 "lib": {
231 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
232 "lib/res/res/values/strings.xml",
233 "device/vendor/blah/overlay/lib/res/values/strings.xml",
234 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800235 },
Colin Crossc1c37552019-01-31 11:42:41 -0800236
Colin Cross5c4791c2019-02-01 11:44:44 -0800237 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800238 "foo": {
Colin Crossc1c37552019-01-31 11:42:41 -0800239 "device/vendor/blah/overlay/foo/res",
240 // Enforce RRO on "foo" could imply RRO on static dependencies, but for now it doesn't.
241 // "device/vendor/blah/overlay/lib/res",
242 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800243 "bar": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800244 "lib": nil,
Colin Cross5c4791c2019-02-01 11:44:44 -0800245 },
246 },
247 {
248 name: "enforce RRO on all",
249 enforceRROTargets: []string{"*"},
250 enforceRROExcludedOverlays: []string{
251 // Excluding specific apps/res directories also allowed.
252 "device/vendor/blah/static_overlay/foo",
253 "device/vendor/blah/static_overlay/bar/res",
254 },
Colin Crossbec85302019-02-13 13:15:46 -0800255 resourceFiles: map[string][]string{
256 "foo": nil,
257 "bar": {"bar/res/res/values/strings.xml"},
258 "lib": nil,
259 "lib2": {"lib2/res/res/values/strings.xml"},
260 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800261 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800262 "foo": {
263 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800264 buildDir + "/.intermediates/lib/android_common/package-res.apk",
265 "foo/res/res/values/strings.xml",
266 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
267 },
Colin Crossbec85302019-02-13 13:15:46 -0800268 "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"},
269 "lib": {
270 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
271 "lib/res/res/values/strings.xml",
272 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800273 },
274 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800275 "foo": {
Colin Crossc1c37552019-01-31 11:42:41 -0800276 "device/vendor/blah/overlay/foo/res",
277 "device/vendor/blah/overlay/lib/res",
278 },
Colin Crossbec85302019-02-13 13:15:46 -0800279 "bar": {"device/vendor/blah/overlay/bar/res"},
280 "lib": {"device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -0800281 },
282 },
283 }
284
Colin Cross890ff552017-11-30 20:13:19 -0800285 resourceOverlays := []string{
286 "device/vendor/blah/overlay",
287 "device/vendor/blah/overlay2",
288 "device/vendor/blah/static_overlay",
289 }
290
291 fs := map[string][]byte{
292 "foo/res/res/values/strings.xml": nil,
293 "bar/res/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800294 "lib/res/res/values/strings.xml": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800295 "lib2/res/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800296 "device/vendor/blah/overlay/foo/res/values/strings.xml": nil,
297 "device/vendor/blah/overlay/bar/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800298 "device/vendor/blah/overlay/lib/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800299 "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
300 "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
301 "device/vendor/blah/overlay2/res/values/strings.xml": nil,
302 }
303
304 bp := `
305 android_app {
306 name: "foo",
307 resource_dirs: ["foo/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -0800308 static_libs: ["lib"],
Colin Cross890ff552017-11-30 20:13:19 -0800309 }
310
311 android_app {
312 name: "bar",
313 resource_dirs: ["bar/res"],
314 }
Colin Cross6ed7dea2019-01-31 14:44:30 -0800315
316 android_library {
317 name: "lib",
318 resource_dirs: ["lib/res"],
Colin Crossbec85302019-02-13 13:15:46 -0800319 static_libs: ["lib2"],
320 }
321
322 android_library {
323 name: "lib2",
324 resource_dirs: ["lib2/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -0800325 }
Colin Cross890ff552017-11-30 20:13:19 -0800326 `
327
Colin Cross5c4791c2019-02-01 11:44:44 -0800328 for _, testCase := range testCases {
Colin Cross890ff552017-11-30 20:13:19 -0800329 t.Run(testCase.name, func(t *testing.T) {
330 config := testConfig(nil)
Colin Crossa74ca042019-01-31 14:31:51 -0800331 config.TestProductVariables.ResourceOverlays = resourceOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800332 if testCase.enforceRROTargets != nil {
Colin Crossa74ca042019-01-31 14:31:51 -0800333 config.TestProductVariables.EnforceRROTargets = testCase.enforceRROTargets
Colin Cross890ff552017-11-30 20:13:19 -0800334 }
335 if testCase.enforceRROExcludedOverlays != nil {
Colin Crossa74ca042019-01-31 14:31:51 -0800336 config.TestProductVariables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800337 }
338
339 ctx := testAppContext(config, bp, fs)
340 run(t, ctx, config)
341
Colin Crossbec85302019-02-13 13:15:46 -0800342 resourceListToFiles := func(module android.TestingModule, list []string) (files []string) {
343 for _, o := range list {
344 res := module.MaybeOutput(o)
345 if res.Rule != nil {
346 // If the overlay is compiled as part of this module (i.e. a .arsc.flat file),
347 // verify the inputs to the .arsc.flat rule.
348 files = append(files, res.Inputs.Strings()...)
349 } else {
350 // Otherwise, verify the full path to the output of the other module
351 files = append(files, o)
Anton Hansson94c93f32019-01-30 16:03:37 +0000352 }
Colin Cross890ff552017-11-30 20:13:19 -0800353 }
Colin Crossbec85302019-02-13 13:15:46 -0800354 return files
Colin Cross890ff552017-11-30 20:13:19 -0800355 }
356
Colin Crossbec85302019-02-13 13:15:46 -0800357 getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) {
358 module := ctx.ModuleForTests(moduleName, "android_common")
359 resourceList := module.MaybeOutput("aapt2/res.list")
360 if resourceList.Rule != nil {
361 resourceFiles = resourceListToFiles(module, resourceList.Inputs.Strings())
Anton Hansson0375a4f2019-01-24 14:39:19 +0000362 }
Colin Crossbec85302019-02-13 13:15:46 -0800363 overlayList := module.MaybeOutput("aapt2/overlay.list")
364 if overlayList.Rule != nil {
365 overlayFiles = resourceListToFiles(module, overlayList.Inputs.Strings())
366 }
367
368 rroDirs = module.Module().(AndroidLibraryDependency).ExportedRRODirs().Strings()
369
370 return resourceFiles, overlayFiles, rroDirs
371 }
372
373 modules := []string{"foo", "bar", "lib", "lib2"}
374 for _, module := range modules {
375 resourceFiles, overlayFiles, rroDirs := getResources(module)
376
377 if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) {
378 t.Errorf("expected %s resource files:\n %#v\n got:\n %#v",
379 module, testCase.resourceFiles[module], resourceFiles)
380 }
381 if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) {
382 t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v",
383 module, testCase.overlayFiles[module], overlayFiles)
384 }
385 if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) {
Anton Hansson0375a4f2019-01-24 14:39:19 +0000386 t.Errorf("expected %s rroDirs: %#v\n got:\n %#v",
Colin Crossbec85302019-02-13 13:15:46 -0800387 module, testCase.rroDirs[module], rroDirs)
Anton Hansson0375a4f2019-01-24 14:39:19 +0000388 }
Colin Cross890ff552017-11-30 20:13:19 -0800389 }
Colin Cross890ff552017-11-30 20:13:19 -0800390 })
391 }
392}
Colin Crossd09b0b62018-04-18 11:06:47 -0700393
394func TestAppSdkVersion(t *testing.T) {
395 testCases := []struct {
396 name string
397 sdkVersion string
398 platformSdkInt int
399 platformSdkCodename string
400 platformSdkFinal bool
401 expectedMinSdkVersion string
402 }{
403 {
404 name: "current final SDK",
405 sdkVersion: "current",
406 platformSdkInt: 27,
407 platformSdkCodename: "REL",
408 platformSdkFinal: true,
409 expectedMinSdkVersion: "27",
410 },
411 {
412 name: "current non-final SDK",
413 sdkVersion: "current",
414 platformSdkInt: 27,
415 platformSdkCodename: "OMR1",
416 platformSdkFinal: false,
417 expectedMinSdkVersion: "OMR1",
418 },
419 {
420 name: "default final SDK",
421 sdkVersion: "",
422 platformSdkInt: 27,
423 platformSdkCodename: "REL",
424 platformSdkFinal: true,
425 expectedMinSdkVersion: "27",
426 },
427 {
428 name: "default non-final SDK",
429 sdkVersion: "",
430 platformSdkInt: 27,
431 platformSdkCodename: "OMR1",
432 platformSdkFinal: false,
433 expectedMinSdkVersion: "OMR1",
434 },
435 {
436 name: "14",
437 sdkVersion: "14",
438 expectedMinSdkVersion: "14",
439 },
440 }
441
442 for _, moduleType := range []string{"android_app", "android_library"} {
443 for _, test := range testCases {
444 t.Run(moduleType+" "+test.name, func(t *testing.T) {
445 bp := fmt.Sprintf(`%s {
446 name: "foo",
447 srcs: ["a.java"],
448 sdk_version: "%s",
449 }`, moduleType, test.sdkVersion)
450
451 config := testConfig(nil)
452 config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt
453 config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename
454 config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal
455
456 ctx := testAppContext(config, bp, nil)
457
458 run(t, ctx, config)
459
460 foo := ctx.ModuleForTests("foo", "android_common")
461 link := foo.Output("package-res.apk")
462 linkFlags := strings.Split(link.Args["flags"], " ")
463 min := android.IndexList("--min-sdk-version", linkFlags)
464 target := android.IndexList("--target-sdk-version", linkFlags)
465
466 if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
467 t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
468 }
469
470 gotMinSdkVersion := linkFlags[min+1]
471 gotTargetSdkVersion := linkFlags[target+1]
472
473 if gotMinSdkVersion != test.expectedMinSdkVersion {
474 t.Errorf("incorrect --min-sdk-version, expected %q got %q",
475 test.expectedMinSdkVersion, gotMinSdkVersion)
476 }
477
478 if gotTargetSdkVersion != test.expectedMinSdkVersion {
479 t.Errorf("incorrect --target-sdk-version, expected %q got %q",
480 test.expectedMinSdkVersion, gotTargetSdkVersion)
481 }
482 })
483 }
484 }
485}
Colin Crossa4f08812018-10-02 22:03:40 -0700486
487func TestJNI(t *testing.T) {
488 ctx := testJava(t, `
489 toolchain_library {
490 name: "libcompiler_rt-extras",
491 src: "",
492 }
493
494 toolchain_library {
495 name: "libatomic",
496 src: "",
497 }
498
499 toolchain_library {
500 name: "libgcc",
501 src: "",
502 }
503
504 toolchain_library {
505 name: "libclang_rt.builtins-aarch64-android",
506 src: "",
507 }
508
509 toolchain_library {
510 name: "libclang_rt.builtins-arm-android",
511 src: "",
512 }
513
514 cc_object {
515 name: "crtbegin_so",
516 stl: "none",
517 }
518
519 cc_object {
520 name: "crtend_so",
521 stl: "none",
522 }
523
524 cc_library {
525 name: "libjni",
526 system_shared_libs: [],
527 stl: "none",
528 }
529
530 android_test {
531 name: "test",
532 no_framework_libs: true,
533 jni_libs: ["libjni"],
534 }
535
536 android_test {
537 name: "test_first",
538 no_framework_libs: true,
539 compile_multilib: "first",
540 jni_libs: ["libjni"],
541 }
542
543 android_test {
544 name: "test_both",
545 no_framework_libs: true,
546 compile_multilib: "both",
547 jni_libs: ["libjni"],
548 }
549
550 android_test {
551 name: "test_32",
552 no_framework_libs: true,
553 compile_multilib: "32",
554 jni_libs: ["libjni"],
555 }
556
557 android_test {
558 name: "test_64",
559 no_framework_libs: true,
560 compile_multilib: "64",
561 jni_libs: ["libjni"],
562 }
563 `)
564
565 // check the existence of the internal modules
566 ctx.ModuleForTests("test", "android_common")
567 ctx.ModuleForTests("test_first", "android_common")
568 ctx.ModuleForTests("test_both", "android_common")
569 ctx.ModuleForTests("test_32", "android_common")
570 ctx.ModuleForTests("test_64", "android_common")
571
572 testCases := []struct {
573 name string
574 abis []string
575 }{
576 {"test", []string{"arm64-v8a"}},
577 {"test_first", []string{"arm64-v8a"}},
578 {"test_both", []string{"arm64-v8a", "armeabi-v7a"}},
579 {"test_32", []string{"armeabi-v7a"}},
580 {"test_64", []string{"arm64-v8a"}},
581 }
582
583 for _, test := range testCases {
584 t.Run(test.name, func(t *testing.T) {
585 app := ctx.ModuleForTests(test.name, "android_common")
586 jniLibZip := app.Output("jnilibs.zip")
587 var abis []string
588 args := strings.Fields(jniLibZip.Args["jarArgs"])
589 for i := 0; i < len(args); i++ {
590 if args[i] == "-P" {
591 abis = append(abis, filepath.Base(args[i+1]))
592 i++
593 }
594 }
595 if !reflect.DeepEqual(abis, test.abis) {
596 t.Errorf("want abis %v, got %v", test.abis, abis)
597 }
598 })
599 }
600}
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800601
602func TestCertificates(t *testing.T) {
603 testCases := []struct {
604 name string
605 bp string
606 certificateOverride string
607 expected string
608 }{
609 {
610 name: "default",
611 bp: `
612 android_app {
613 name: "foo",
614 srcs: ["a.java"],
615 }
616 `,
617 certificateOverride: "",
618 expected: "build/target/product/security/testkey.x509.pem build/target/product/security/testkey.pk8",
619 },
620 {
621 name: "module certificate property",
622 bp: `
623 android_app {
624 name: "foo",
625 srcs: ["a.java"],
626 certificate: ":new_certificate"
627 }
628
629 android_app_certificate {
630 name: "new_certificate",
631 certificate: "cert/new_cert",
632 }
633 `,
634 certificateOverride: "",
635 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
636 },
637 {
638 name: "path certificate property",
639 bp: `
640 android_app {
641 name: "foo",
642 srcs: ["a.java"],
643 certificate: "expiredkey"
644 }
645 `,
646 certificateOverride: "",
647 expected: "build/target/product/security/expiredkey.x509.pem build/target/product/security/expiredkey.pk8",
648 },
649 {
650 name: "certificate overrides",
651 bp: `
652 android_app {
653 name: "foo",
654 srcs: ["a.java"],
655 certificate: "expiredkey"
656 }
657
658 android_app_certificate {
659 name: "new_certificate",
660 certificate: "cert/new_cert",
661 }
662 `,
663 certificateOverride: "foo:new_certificate",
664 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
665 },
666 }
667
668 for _, test := range testCases {
669 t.Run(test.name, func(t *testing.T) {
670 config := testConfig(nil)
671 if test.certificateOverride != "" {
672 config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride}
673 }
674 ctx := testAppContext(config, test.bp, nil)
675
676 run(t, ctx, config)
677 foo := ctx.ModuleForTests("foo", "android_common")
678
679 signapk := foo.Output("foo.apk")
680 signFlags := signapk.Args["certificates"]
681 if test.expected != signFlags {
682 t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags)
683 }
684 })
685 }
686}
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800687
688func TestPackageNameOverride(t *testing.T) {
689 testCases := []struct {
690 name string
691 bp string
692 packageNameOverride string
693 expected []string
694 }{
695 {
696 name: "default",
697 bp: `
698 android_app {
699 name: "foo",
700 srcs: ["a.java"],
701 }
702 `,
703 packageNameOverride: "",
704 expected: []string{
705 buildDir + "/.intermediates/foo/android_common/foo.apk",
706 buildDir + "/target/product/test_device/system/app/foo/foo.apk",
707 },
708 },
709 {
710 name: "overridden",
711 bp: `
712 android_app {
713 name: "foo",
714 srcs: ["a.java"],
715 }
716 `,
717 packageNameOverride: "foo:bar",
718 expected: []string{
719 // The package apk should be still be the original name for test dependencies.
720 buildDir + "/.intermediates/foo/android_common/foo.apk",
721 buildDir + "/target/product/test_device/system/app/bar/bar.apk",
722 },
723 },
724 }
725
726 for _, test := range testCases {
727 t.Run(test.name, func(t *testing.T) {
728 config := testConfig(nil)
729 if test.packageNameOverride != "" {
730 config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride}
731 }
732 ctx := testAppContext(config, test.bp, nil)
733
734 run(t, ctx, config)
735 foo := ctx.ModuleForTests("foo", "android_common")
736
737 outputs := foo.AllOutputs()
738 outputMap := make(map[string]bool)
739 for _, o := range outputs {
740 outputMap[o] = true
741 }
742 for _, e := range test.expected {
743 if _, exist := outputMap[e]; !exist {
744 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
745 }
746 }
747 })
748 }
749}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800750
751func TestInstrumentationTargetOverridden(t *testing.T) {
752 bp := `
753 android_app {
754 name: "foo",
755 srcs: ["a.java"],
756 }
757
758 android_test {
759 name: "bar",
760 instrumentation_for: "foo",
761 }
762 `
763 config := testConfig(nil)
764 config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
765 ctx := testAppContext(config, bp, nil)
766
767 run(t, ctx, config)
768
769 bar := ctx.ModuleForTests("bar", "android_common")
770 res := bar.Output("package-res.apk")
771 aapt2Flags := res.Args["flags"]
772 e := "--rename-instrumentation-target-package org.dandroid.bp"
773 if !strings.Contains(aapt2Flags, e) {
774 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
775 }
776}