blob: 3942ecdb7d3e48e1770093fae479232c706ee381 [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 Crosse560c4a2019-03-19 16:03:11 -0700109func TestAppSplits(t *testing.T) {
110 ctx := testApp(t, `
111 android_app {
112 name: "foo",
113 srcs: ["a.java"],
114 package_splits: ["v4", "v7,hdpi"],
115 }`)
116
117 foo := ctx.ModuleForTests("foo", "android_common")
118
119 expectedOutputs := []string{
120 filepath.Join(buildDir, ".intermediates/foo/android_common/foo.apk"),
121 filepath.Join(buildDir, ".intermediates/foo/android_common/foo_v4.apk"),
122 filepath.Join(buildDir, ".intermediates/foo/android_common/foo_v7_hdpi.apk"),
123 }
124 for _, expectedOutput := range expectedOutputs {
125 foo.Output(expectedOutput)
126 }
127
128 if g, w := foo.Module().(*AndroidApp).Srcs().Strings(), expectedOutputs; !reflect.DeepEqual(g, w) {
129 t.Errorf("want Srcs() = %q, got %q", w, g)
130 }
131}
132
Colin Cross0ddae7f2019-02-07 15:30:01 -0800133func TestResourceDirs(t *testing.T) {
134 testCases := []struct {
135 name string
136 prop string
137 resources []string
138 }{
139 {
140 name: "no resource_dirs",
141 prop: "",
142 resources: []string{"res/res/values/strings.xml"},
143 },
144 {
145 name: "resource_dirs",
146 prop: `resource_dirs: ["res"]`,
147 resources: []string{"res/res/values/strings.xml"},
148 },
149 {
150 name: "empty resource_dirs",
151 prop: `resource_dirs: []`,
152 resources: nil,
153 },
154 }
155
156 fs := map[string][]byte{
157 "res/res/values/strings.xml": nil,
158 }
159
160 bp := `
161 android_app {
162 name: "foo",
163 %s
164 }
165 `
166
167 for _, testCase := range testCases {
168 t.Run(testCase.name, func(t *testing.T) {
169 config := testConfig(nil)
170 ctx := testContext(config, fmt.Sprintf(bp, testCase.prop), fs)
171 run(t, ctx, config)
172
173 module := ctx.ModuleForTests("foo", "android_common")
174 resourceList := module.MaybeOutput("aapt2/res.list")
175
176 var resources []string
177 if resourceList.Rule != nil {
178 for _, compiledResource := range resourceList.Inputs.Strings() {
179 resources = append(resources, module.Output(compiledResource).Inputs.Strings()...)
180 }
181 }
182
183 if !reflect.DeepEqual(resources, testCase.resources) {
184 t.Errorf("expected resource files %q, got %q",
185 testCase.resources, resources)
186 }
187 })
188 }
189}
190
Colin Crossbec85302019-02-13 13:15:46 -0800191func TestAndroidResources(t *testing.T) {
Colin Cross5c4791c2019-02-01 11:44:44 -0800192 testCases := []struct {
193 name string
194 enforceRROTargets []string
195 enforceRROExcludedOverlays []string
Colin Crossbec85302019-02-13 13:15:46 -0800196 resourceFiles map[string][]string
Colin Cross5c4791c2019-02-01 11:44:44 -0800197 overlayFiles map[string][]string
198 rroDirs map[string][]string
199 }{
200 {
201 name: "no RRO",
202 enforceRROTargets: nil,
203 enforceRROExcludedOverlays: nil,
Colin Crossbec85302019-02-13 13:15:46 -0800204 resourceFiles: map[string][]string{
205 "foo": nil,
206 "bar": {"bar/res/res/values/strings.xml"},
207 "lib": nil,
208 "lib2": {"lib2/res/res/values/strings.xml"},
209 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800210 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800211 "foo": {
212 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800213 buildDir + "/.intermediates/lib/android_common/package-res.apk",
214 "foo/res/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800215 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
216 "device/vendor/blah/overlay/foo/res/values/strings.xml",
217 },
Colin Crossbec85302019-02-13 13:15:46 -0800218 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800219 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
220 "device/vendor/blah/overlay/bar/res/values/strings.xml",
221 },
Colin Crossbec85302019-02-13 13:15:46 -0800222 "lib": {
223 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
224 "lib/res/res/values/strings.xml",
225 "device/vendor/blah/overlay/lib/res/values/strings.xml",
226 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800227 },
228 rroDirs: map[string][]string{
229 "foo": nil,
230 "bar": nil,
231 },
232 },
233 {
234 name: "enforce RRO on foo",
235 enforceRROTargets: []string{"foo"},
236 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Colin Crossbec85302019-02-13 13:15:46 -0800237 resourceFiles: map[string][]string{
238 "foo": nil,
239 "bar": {"bar/res/res/values/strings.xml"},
240 "lib": nil,
241 "lib2": {"lib2/res/res/values/strings.xml"},
242 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800243 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800244 "foo": {
245 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800246 buildDir + "/.intermediates/lib/android_common/package-res.apk",
247 "foo/res/res/values/strings.xml",
248 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
249 },
Colin Crossbec85302019-02-13 13:15:46 -0800250 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800251 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
252 "device/vendor/blah/overlay/bar/res/values/strings.xml",
253 },
Colin Crossbec85302019-02-13 13:15:46 -0800254 "lib": {
255 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
256 "lib/res/res/values/strings.xml",
257 "device/vendor/blah/overlay/lib/res/values/strings.xml",
258 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800259 },
Colin Crossc1c37552019-01-31 11:42:41 -0800260
Colin Cross5c4791c2019-02-01 11:44:44 -0800261 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800262 "foo": {
Colin Crossc1c37552019-01-31 11:42:41 -0800263 "device/vendor/blah/overlay/foo/res",
264 // Enforce RRO on "foo" could imply RRO on static dependencies, but for now it doesn't.
265 // "device/vendor/blah/overlay/lib/res",
266 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800267 "bar": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800268 "lib": nil,
Colin Cross5c4791c2019-02-01 11:44:44 -0800269 },
270 },
271 {
272 name: "enforce RRO on all",
273 enforceRROTargets: []string{"*"},
274 enforceRROExcludedOverlays: []string{
275 // Excluding specific apps/res directories also allowed.
276 "device/vendor/blah/static_overlay/foo",
277 "device/vendor/blah/static_overlay/bar/res",
278 },
Colin Crossbec85302019-02-13 13:15:46 -0800279 resourceFiles: map[string][]string{
280 "foo": nil,
281 "bar": {"bar/res/res/values/strings.xml"},
282 "lib": nil,
283 "lib2": {"lib2/res/res/values/strings.xml"},
284 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800285 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800286 "foo": {
287 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800288 buildDir + "/.intermediates/lib/android_common/package-res.apk",
289 "foo/res/res/values/strings.xml",
290 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
291 },
Colin Crossbec85302019-02-13 13:15:46 -0800292 "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"},
293 "lib": {
294 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
295 "lib/res/res/values/strings.xml",
296 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800297 },
298 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800299 "foo": {
Colin Crossc1c37552019-01-31 11:42:41 -0800300 "device/vendor/blah/overlay/foo/res",
301 "device/vendor/blah/overlay/lib/res",
302 },
Colin Crossbec85302019-02-13 13:15:46 -0800303 "bar": {"device/vendor/blah/overlay/bar/res"},
304 "lib": {"device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -0800305 },
306 },
307 }
308
Colin Cross890ff552017-11-30 20:13:19 -0800309 resourceOverlays := []string{
310 "device/vendor/blah/overlay",
311 "device/vendor/blah/overlay2",
312 "device/vendor/blah/static_overlay",
313 }
314
315 fs := map[string][]byte{
316 "foo/res/res/values/strings.xml": nil,
317 "bar/res/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800318 "lib/res/res/values/strings.xml": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800319 "lib2/res/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800320 "device/vendor/blah/overlay/foo/res/values/strings.xml": nil,
321 "device/vendor/blah/overlay/bar/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800322 "device/vendor/blah/overlay/lib/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800323 "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
324 "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
325 "device/vendor/blah/overlay2/res/values/strings.xml": nil,
326 }
327
328 bp := `
329 android_app {
330 name: "foo",
331 resource_dirs: ["foo/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -0800332 static_libs: ["lib"],
Colin Cross890ff552017-11-30 20:13:19 -0800333 }
334
335 android_app {
336 name: "bar",
337 resource_dirs: ["bar/res"],
338 }
Colin Cross6ed7dea2019-01-31 14:44:30 -0800339
340 android_library {
341 name: "lib",
342 resource_dirs: ["lib/res"],
Colin Crossbec85302019-02-13 13:15:46 -0800343 static_libs: ["lib2"],
344 }
345
346 android_library {
347 name: "lib2",
348 resource_dirs: ["lib2/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -0800349 }
Colin Cross890ff552017-11-30 20:13:19 -0800350 `
351
Colin Cross5c4791c2019-02-01 11:44:44 -0800352 for _, testCase := range testCases {
Colin Cross890ff552017-11-30 20:13:19 -0800353 t.Run(testCase.name, func(t *testing.T) {
354 config := testConfig(nil)
Colin Crossa74ca042019-01-31 14:31:51 -0800355 config.TestProductVariables.ResourceOverlays = resourceOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800356 if testCase.enforceRROTargets != nil {
Colin Crossa74ca042019-01-31 14:31:51 -0800357 config.TestProductVariables.EnforceRROTargets = testCase.enforceRROTargets
Colin Cross890ff552017-11-30 20:13:19 -0800358 }
359 if testCase.enforceRROExcludedOverlays != nil {
Colin Crossa74ca042019-01-31 14:31:51 -0800360 config.TestProductVariables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800361 }
362
363 ctx := testAppContext(config, bp, fs)
364 run(t, ctx, config)
365
Colin Crossbec85302019-02-13 13:15:46 -0800366 resourceListToFiles := func(module android.TestingModule, list []string) (files []string) {
367 for _, o := range list {
368 res := module.MaybeOutput(o)
369 if res.Rule != nil {
370 // If the overlay is compiled as part of this module (i.e. a .arsc.flat file),
371 // verify the inputs to the .arsc.flat rule.
372 files = append(files, res.Inputs.Strings()...)
373 } else {
374 // Otherwise, verify the full path to the output of the other module
375 files = append(files, o)
Anton Hansson94c93f32019-01-30 16:03:37 +0000376 }
Colin Cross890ff552017-11-30 20:13:19 -0800377 }
Colin Crossbec85302019-02-13 13:15:46 -0800378 return files
Colin Cross890ff552017-11-30 20:13:19 -0800379 }
380
Colin Crossbec85302019-02-13 13:15:46 -0800381 getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) {
382 module := ctx.ModuleForTests(moduleName, "android_common")
383 resourceList := module.MaybeOutput("aapt2/res.list")
384 if resourceList.Rule != nil {
385 resourceFiles = resourceListToFiles(module, resourceList.Inputs.Strings())
Anton Hansson0375a4f2019-01-24 14:39:19 +0000386 }
Colin Crossbec85302019-02-13 13:15:46 -0800387 overlayList := module.MaybeOutput("aapt2/overlay.list")
388 if overlayList.Rule != nil {
389 overlayFiles = resourceListToFiles(module, overlayList.Inputs.Strings())
390 }
391
392 rroDirs = module.Module().(AndroidLibraryDependency).ExportedRRODirs().Strings()
393
394 return resourceFiles, overlayFiles, rroDirs
395 }
396
397 modules := []string{"foo", "bar", "lib", "lib2"}
398 for _, module := range modules {
399 resourceFiles, overlayFiles, rroDirs := getResources(module)
400
401 if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) {
402 t.Errorf("expected %s resource files:\n %#v\n got:\n %#v",
403 module, testCase.resourceFiles[module], resourceFiles)
404 }
405 if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) {
406 t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v",
407 module, testCase.overlayFiles[module], overlayFiles)
408 }
409 if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) {
Anton Hansson0375a4f2019-01-24 14:39:19 +0000410 t.Errorf("expected %s rroDirs: %#v\n got:\n %#v",
Colin Crossbec85302019-02-13 13:15:46 -0800411 module, testCase.rroDirs[module], rroDirs)
Anton Hansson0375a4f2019-01-24 14:39:19 +0000412 }
Colin Cross890ff552017-11-30 20:13:19 -0800413 }
Colin Cross890ff552017-11-30 20:13:19 -0800414 })
415 }
416}
Colin Crossd09b0b62018-04-18 11:06:47 -0700417
418func TestAppSdkVersion(t *testing.T) {
419 testCases := []struct {
420 name string
421 sdkVersion string
422 platformSdkInt int
423 platformSdkCodename string
424 platformSdkFinal bool
425 expectedMinSdkVersion string
426 }{
427 {
428 name: "current final SDK",
429 sdkVersion: "current",
430 platformSdkInt: 27,
431 platformSdkCodename: "REL",
432 platformSdkFinal: true,
433 expectedMinSdkVersion: "27",
434 },
435 {
436 name: "current non-final SDK",
437 sdkVersion: "current",
438 platformSdkInt: 27,
439 platformSdkCodename: "OMR1",
440 platformSdkFinal: false,
441 expectedMinSdkVersion: "OMR1",
442 },
443 {
444 name: "default final SDK",
445 sdkVersion: "",
446 platformSdkInt: 27,
447 platformSdkCodename: "REL",
448 platformSdkFinal: true,
449 expectedMinSdkVersion: "27",
450 },
451 {
452 name: "default non-final SDK",
453 sdkVersion: "",
454 platformSdkInt: 27,
455 platformSdkCodename: "OMR1",
456 platformSdkFinal: false,
457 expectedMinSdkVersion: "OMR1",
458 },
459 {
460 name: "14",
461 sdkVersion: "14",
462 expectedMinSdkVersion: "14",
463 },
464 }
465
466 for _, moduleType := range []string{"android_app", "android_library"} {
467 for _, test := range testCases {
468 t.Run(moduleType+" "+test.name, func(t *testing.T) {
469 bp := fmt.Sprintf(`%s {
470 name: "foo",
471 srcs: ["a.java"],
472 sdk_version: "%s",
473 }`, moduleType, test.sdkVersion)
474
475 config := testConfig(nil)
476 config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt
477 config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename
478 config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal
479
480 ctx := testAppContext(config, bp, nil)
481
482 run(t, ctx, config)
483
484 foo := ctx.ModuleForTests("foo", "android_common")
485 link := foo.Output("package-res.apk")
486 linkFlags := strings.Split(link.Args["flags"], " ")
487 min := android.IndexList("--min-sdk-version", linkFlags)
488 target := android.IndexList("--target-sdk-version", linkFlags)
489
490 if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
491 t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
492 }
493
494 gotMinSdkVersion := linkFlags[min+1]
495 gotTargetSdkVersion := linkFlags[target+1]
496
497 if gotMinSdkVersion != test.expectedMinSdkVersion {
498 t.Errorf("incorrect --min-sdk-version, expected %q got %q",
499 test.expectedMinSdkVersion, gotMinSdkVersion)
500 }
501
502 if gotTargetSdkVersion != test.expectedMinSdkVersion {
503 t.Errorf("incorrect --target-sdk-version, expected %q got %q",
504 test.expectedMinSdkVersion, gotTargetSdkVersion)
505 }
506 })
507 }
508 }
509}
Colin Crossa4f08812018-10-02 22:03:40 -0700510
511func TestJNI(t *testing.T) {
512 ctx := testJava(t, `
513 toolchain_library {
514 name: "libcompiler_rt-extras",
515 src: "",
516 }
517
518 toolchain_library {
519 name: "libatomic",
520 src: "",
521 }
522
523 toolchain_library {
524 name: "libgcc",
525 src: "",
526 }
527
528 toolchain_library {
529 name: "libclang_rt.builtins-aarch64-android",
530 src: "",
531 }
532
533 toolchain_library {
534 name: "libclang_rt.builtins-arm-android",
535 src: "",
536 }
537
538 cc_object {
539 name: "crtbegin_so",
540 stl: "none",
541 }
542
543 cc_object {
544 name: "crtend_so",
545 stl: "none",
546 }
547
548 cc_library {
549 name: "libjni",
550 system_shared_libs: [],
551 stl: "none",
552 }
553
554 android_test {
555 name: "test",
556 no_framework_libs: true,
557 jni_libs: ["libjni"],
558 }
559
560 android_test {
561 name: "test_first",
562 no_framework_libs: true,
563 compile_multilib: "first",
564 jni_libs: ["libjni"],
565 }
566
567 android_test {
568 name: "test_both",
569 no_framework_libs: true,
570 compile_multilib: "both",
571 jni_libs: ["libjni"],
572 }
573
574 android_test {
575 name: "test_32",
576 no_framework_libs: true,
577 compile_multilib: "32",
578 jni_libs: ["libjni"],
579 }
580
581 android_test {
582 name: "test_64",
583 no_framework_libs: true,
584 compile_multilib: "64",
585 jni_libs: ["libjni"],
586 }
587 `)
588
589 // check the existence of the internal modules
590 ctx.ModuleForTests("test", "android_common")
591 ctx.ModuleForTests("test_first", "android_common")
592 ctx.ModuleForTests("test_both", "android_common")
593 ctx.ModuleForTests("test_32", "android_common")
594 ctx.ModuleForTests("test_64", "android_common")
595
596 testCases := []struct {
597 name string
598 abis []string
599 }{
600 {"test", []string{"arm64-v8a"}},
601 {"test_first", []string{"arm64-v8a"}},
602 {"test_both", []string{"arm64-v8a", "armeabi-v7a"}},
603 {"test_32", []string{"armeabi-v7a"}},
604 {"test_64", []string{"arm64-v8a"}},
605 }
606
607 for _, test := range testCases {
608 t.Run(test.name, func(t *testing.T) {
609 app := ctx.ModuleForTests(test.name, "android_common")
610 jniLibZip := app.Output("jnilibs.zip")
611 var abis []string
612 args := strings.Fields(jniLibZip.Args["jarArgs"])
613 for i := 0; i < len(args); i++ {
614 if args[i] == "-P" {
615 abis = append(abis, filepath.Base(args[i+1]))
616 i++
617 }
618 }
619 if !reflect.DeepEqual(abis, test.abis) {
620 t.Errorf("want abis %v, got %v", test.abis, abis)
621 }
622 })
623 }
624}
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800625
626func TestCertificates(t *testing.T) {
627 testCases := []struct {
628 name string
629 bp string
630 certificateOverride string
631 expected string
632 }{
633 {
634 name: "default",
635 bp: `
636 android_app {
637 name: "foo",
638 srcs: ["a.java"],
639 }
640 `,
641 certificateOverride: "",
642 expected: "build/target/product/security/testkey.x509.pem build/target/product/security/testkey.pk8",
643 },
644 {
645 name: "module certificate property",
646 bp: `
647 android_app {
648 name: "foo",
649 srcs: ["a.java"],
650 certificate: ":new_certificate"
651 }
652
653 android_app_certificate {
654 name: "new_certificate",
655 certificate: "cert/new_cert",
656 }
657 `,
658 certificateOverride: "",
659 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
660 },
661 {
662 name: "path certificate property",
663 bp: `
664 android_app {
665 name: "foo",
666 srcs: ["a.java"],
667 certificate: "expiredkey"
668 }
669 `,
670 certificateOverride: "",
671 expected: "build/target/product/security/expiredkey.x509.pem build/target/product/security/expiredkey.pk8",
672 },
673 {
674 name: "certificate overrides",
675 bp: `
676 android_app {
677 name: "foo",
678 srcs: ["a.java"],
679 certificate: "expiredkey"
680 }
681
682 android_app_certificate {
683 name: "new_certificate",
684 certificate: "cert/new_cert",
685 }
686 `,
687 certificateOverride: "foo:new_certificate",
688 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
689 },
690 }
691
692 for _, test := range testCases {
693 t.Run(test.name, func(t *testing.T) {
694 config := testConfig(nil)
695 if test.certificateOverride != "" {
696 config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride}
697 }
698 ctx := testAppContext(config, test.bp, nil)
699
700 run(t, ctx, config)
701 foo := ctx.ModuleForTests("foo", "android_common")
702
703 signapk := foo.Output("foo.apk")
704 signFlags := signapk.Args["certificates"]
705 if test.expected != signFlags {
706 t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags)
707 }
708 })
709 }
710}
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800711
712func TestPackageNameOverride(t *testing.T) {
713 testCases := []struct {
714 name string
715 bp string
716 packageNameOverride string
717 expected []string
718 }{
719 {
720 name: "default",
721 bp: `
722 android_app {
723 name: "foo",
724 srcs: ["a.java"],
725 }
726 `,
727 packageNameOverride: "",
728 expected: []string{
729 buildDir + "/.intermediates/foo/android_common/foo.apk",
730 buildDir + "/target/product/test_device/system/app/foo/foo.apk",
731 },
732 },
733 {
734 name: "overridden",
735 bp: `
736 android_app {
737 name: "foo",
738 srcs: ["a.java"],
739 }
740 `,
741 packageNameOverride: "foo:bar",
742 expected: []string{
743 // The package apk should be still be the original name for test dependencies.
744 buildDir + "/.intermediates/foo/android_common/foo.apk",
745 buildDir + "/target/product/test_device/system/app/bar/bar.apk",
746 },
747 },
748 }
749
750 for _, test := range testCases {
751 t.Run(test.name, func(t *testing.T) {
752 config := testConfig(nil)
753 if test.packageNameOverride != "" {
754 config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride}
755 }
756 ctx := testAppContext(config, test.bp, nil)
757
758 run(t, ctx, config)
759 foo := ctx.ModuleForTests("foo", "android_common")
760
761 outputs := foo.AllOutputs()
762 outputMap := make(map[string]bool)
763 for _, o := range outputs {
764 outputMap[o] = true
765 }
766 for _, e := range test.expected {
767 if _, exist := outputMap[e]; !exist {
768 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
769 }
770 }
771 })
772 }
773}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800774
775func TestInstrumentationTargetOverridden(t *testing.T) {
776 bp := `
777 android_app {
778 name: "foo",
779 srcs: ["a.java"],
780 }
781
782 android_test {
783 name: "bar",
784 instrumentation_for: "foo",
785 }
786 `
787 config := testConfig(nil)
788 config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
789 ctx := testAppContext(config, bp, nil)
790
791 run(t, ctx, config)
792
793 bar := ctx.ModuleForTests("bar", "android_common")
794 res := bar.Output("package-res.apk")
795 aapt2Flags := res.Args["flags"]
796 e := "--rename-instrumentation-target-package org.dandroid.bp"
797 if !strings.Contains(aapt2Flags, e) {
798 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
799 }
800}