blob: 564211c2ceac53fc7823443cb958f0ad21816855 [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
Jaewoong Jungf9a04432019-07-17 11:15:09 -070046func testAppContext(bp string, fs map[string][]byte) *android.TestContext {
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
Jaewoong Jungf9a04432019-07-17 11:15:09 -070056 return testContext(bp, appFS)
Colin Cross527012a2017-11-30 22:56:16 -080057}
58
59func testApp(t *testing.T, bp string) *android.TestContext {
60 config := testConfig(nil)
61
Jaewoong Jungf9a04432019-07-17 11:15:09 -070062 ctx := testAppContext(bp, nil)
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
Colin Cross0ddae7f2019-02-07 15:30:01 -0800178func TestResourceDirs(t *testing.T) {
179 testCases := []struct {
180 name string
181 prop string
182 resources []string
183 }{
184 {
185 name: "no resource_dirs",
186 prop: "",
187 resources: []string{"res/res/values/strings.xml"},
188 },
189 {
190 name: "resource_dirs",
191 prop: `resource_dirs: ["res"]`,
192 resources: []string{"res/res/values/strings.xml"},
193 },
194 {
195 name: "empty resource_dirs",
196 prop: `resource_dirs: []`,
197 resources: nil,
198 },
199 }
200
201 fs := map[string][]byte{
202 "res/res/values/strings.xml": nil,
203 }
204
205 bp := `
206 android_app {
207 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900208 sdk_version: "current",
Colin Cross0ddae7f2019-02-07 15:30:01 -0800209 %s
210 }
211 `
212
213 for _, testCase := range testCases {
214 t.Run(testCase.name, func(t *testing.T) {
215 config := testConfig(nil)
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700216 ctx := testContext(fmt.Sprintf(bp, testCase.prop), fs)
Colin Cross0ddae7f2019-02-07 15:30:01 -0800217 run(t, ctx, config)
218
219 module := ctx.ModuleForTests("foo", "android_common")
220 resourceList := module.MaybeOutput("aapt2/res.list")
221
222 var resources []string
223 if resourceList.Rule != nil {
224 for _, compiledResource := range resourceList.Inputs.Strings() {
225 resources = append(resources, module.Output(compiledResource).Inputs.Strings()...)
226 }
227 }
228
229 if !reflect.DeepEqual(resources, testCase.resources) {
230 t.Errorf("expected resource files %q, got %q",
231 testCase.resources, resources)
232 }
233 })
234 }
235}
236
Colin Crossbec85302019-02-13 13:15:46 -0800237func TestAndroidResources(t *testing.T) {
Colin Cross5c4791c2019-02-01 11:44:44 -0800238 testCases := []struct {
239 name string
240 enforceRROTargets []string
241 enforceRROExcludedOverlays []string
Colin Crossbec85302019-02-13 13:15:46 -0800242 resourceFiles map[string][]string
Colin Cross5c4791c2019-02-01 11:44:44 -0800243 overlayFiles map[string][]string
244 rroDirs map[string][]string
245 }{
246 {
247 name: "no RRO",
248 enforceRROTargets: nil,
249 enforceRROExcludedOverlays: nil,
Colin Crossbec85302019-02-13 13:15:46 -0800250 resourceFiles: map[string][]string{
251 "foo": nil,
252 "bar": {"bar/res/res/values/strings.xml"},
253 "lib": nil,
254 "lib2": {"lib2/res/res/values/strings.xml"},
255 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800256 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800257 "foo": {
258 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800259 buildDir + "/.intermediates/lib/android_common/package-res.apk",
Anton Hansson53c88442019-03-18 15:53:16 +0000260 buildDir + "/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800261 "foo/res/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800262 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
263 "device/vendor/blah/overlay/foo/res/values/strings.xml",
Anton Hansson53c88442019-03-18 15:53:16 +0000264 "product/vendor/blah/overlay/foo/res/values/strings.xml",
Colin Cross5c4791c2019-02-01 11:44:44 -0800265 },
Colin Crossbec85302019-02-13 13:15:46 -0800266 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800267 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
268 "device/vendor/blah/overlay/bar/res/values/strings.xml",
269 },
Colin Crossbec85302019-02-13 13:15:46 -0800270 "lib": {
271 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
272 "lib/res/res/values/strings.xml",
273 "device/vendor/blah/overlay/lib/res/values/strings.xml",
274 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800275 },
276 rroDirs: map[string][]string{
277 "foo": nil,
278 "bar": nil,
279 },
280 },
281 {
282 name: "enforce RRO on foo",
283 enforceRROTargets: []string{"foo"},
284 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Colin Crossbec85302019-02-13 13:15:46 -0800285 resourceFiles: map[string][]string{
286 "foo": nil,
287 "bar": {"bar/res/res/values/strings.xml"},
288 "lib": nil,
289 "lib2": {"lib2/res/res/values/strings.xml"},
290 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800291 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800292 "foo": {
293 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800294 buildDir + "/.intermediates/lib/android_common/package-res.apk",
Anton Hansson53c88442019-03-18 15:53:16 +0000295 buildDir + "/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800296 "foo/res/res/values/strings.xml",
297 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
298 },
Colin Crossbec85302019-02-13 13:15:46 -0800299 "bar": {
Colin Cross5c4791c2019-02-01 11:44:44 -0800300 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
301 "device/vendor/blah/overlay/bar/res/values/strings.xml",
302 },
Colin Crossbec85302019-02-13 13:15:46 -0800303 "lib": {
304 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
305 "lib/res/res/values/strings.xml",
306 "device/vendor/blah/overlay/lib/res/values/strings.xml",
307 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800308 },
Colin Crossc1c37552019-01-31 11:42:41 -0800309
Colin Cross5c4791c2019-02-01 11:44:44 -0800310 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800311 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +0000312 "device:device/vendor/blah/overlay/foo/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800313 // Enforce RRO on "foo" could imply RRO on static dependencies, but for now it doesn't.
314 // "device/vendor/blah/overlay/lib/res",
Anton Hansson53c88442019-03-18 15:53:16 +0000315 "product:product/vendor/blah/overlay/foo/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800316 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800317 "bar": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800318 "lib": nil,
Colin Cross5c4791c2019-02-01 11:44:44 -0800319 },
320 },
321 {
322 name: "enforce RRO on all",
323 enforceRROTargets: []string{"*"},
324 enforceRROExcludedOverlays: []string{
325 // Excluding specific apps/res directories also allowed.
326 "device/vendor/blah/static_overlay/foo",
327 "device/vendor/blah/static_overlay/bar/res",
328 },
Colin Crossbec85302019-02-13 13:15:46 -0800329 resourceFiles: map[string][]string{
330 "foo": nil,
331 "bar": {"bar/res/res/values/strings.xml"},
332 "lib": nil,
333 "lib2": {"lib2/res/res/values/strings.xml"},
334 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800335 overlayFiles: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800336 "foo": {
337 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800338 buildDir + "/.intermediates/lib/android_common/package-res.apk",
Anton Hansson53c88442019-03-18 15:53:16 +0000339 buildDir + "/.intermediates/lib3/android_common/package-res.apk",
Colin Cross6ed7dea2019-01-31 14:44:30 -0800340 "foo/res/res/values/strings.xml",
341 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
342 },
Colin Crossbec85302019-02-13 13:15:46 -0800343 "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"},
344 "lib": {
345 buildDir + "/.intermediates/lib2/android_common/package-res.apk",
346 "lib/res/res/values/strings.xml",
347 },
Colin Cross5c4791c2019-02-01 11:44:44 -0800348 },
349 rroDirs: map[string][]string{
Colin Crossbec85302019-02-13 13:15:46 -0800350 "foo": {
Anton Hansson53c88442019-03-18 15:53:16 +0000351 "device:device/vendor/blah/overlay/foo/res",
352 "product:product/vendor/blah/overlay/foo/res",
353 // Lib dep comes after the direct deps
354 "device:device/vendor/blah/overlay/lib/res",
Colin Crossc1c37552019-01-31 11:42:41 -0800355 },
Anton Hansson53c88442019-03-18 15:53:16 +0000356 "bar": {"device:device/vendor/blah/overlay/bar/res"},
357 "lib": {"device:device/vendor/blah/overlay/lib/res"},
Colin Cross5c4791c2019-02-01 11:44:44 -0800358 },
359 },
360 }
361
Anton Hansson53c88442019-03-18 15:53:16 +0000362 deviceResourceOverlays := []string{
Colin Cross890ff552017-11-30 20:13:19 -0800363 "device/vendor/blah/overlay",
364 "device/vendor/blah/overlay2",
365 "device/vendor/blah/static_overlay",
366 }
367
Anton Hansson53c88442019-03-18 15:53:16 +0000368 productResourceOverlays := []string{
369 "product/vendor/blah/overlay",
370 }
371
Colin Cross890ff552017-11-30 20:13:19 -0800372 fs := map[string][]byte{
373 "foo/res/res/values/strings.xml": nil,
374 "bar/res/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800375 "lib/res/res/values/strings.xml": nil,
Colin Crossbec85302019-02-13 13:15:46 -0800376 "lib2/res/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800377 "device/vendor/blah/overlay/foo/res/values/strings.xml": nil,
378 "device/vendor/blah/overlay/bar/res/values/strings.xml": nil,
Colin Cross6ed7dea2019-01-31 14:44:30 -0800379 "device/vendor/blah/overlay/lib/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800380 "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
381 "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
382 "device/vendor/blah/overlay2/res/values/strings.xml": nil,
Anton Hansson53c88442019-03-18 15:53:16 +0000383 "product/vendor/blah/overlay/foo/res/values/strings.xml": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800384 }
385
386 bp := `
387 android_app {
388 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900389 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -0800390 resource_dirs: ["foo/res"],
Anton Hansson53c88442019-03-18 15:53:16 +0000391 static_libs: ["lib", "lib3"],
Colin Cross890ff552017-11-30 20:13:19 -0800392 }
393
394 android_app {
395 name: "bar",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900396 sdk_version: "current",
Colin Cross890ff552017-11-30 20:13:19 -0800397 resource_dirs: ["bar/res"],
398 }
Colin Cross6ed7dea2019-01-31 14:44:30 -0800399
400 android_library {
401 name: "lib",
402 resource_dirs: ["lib/res"],
Colin Crossbec85302019-02-13 13:15:46 -0800403 static_libs: ["lib2"],
404 }
405
406 android_library {
407 name: "lib2",
408 resource_dirs: ["lib2/res"],
Colin Cross6ed7dea2019-01-31 14:44:30 -0800409 }
Anton Hansson53c88442019-03-18 15:53:16 +0000410
411 // This library has the same resources as lib (should not lead to dupe RROs)
412 android_library {
413 name: "lib3",
414 resource_dirs: ["lib/res"]
415 }
Colin Cross890ff552017-11-30 20:13:19 -0800416 `
417
Colin Cross5c4791c2019-02-01 11:44:44 -0800418 for _, testCase := range testCases {
Colin Cross890ff552017-11-30 20:13:19 -0800419 t.Run(testCase.name, func(t *testing.T) {
420 config := testConfig(nil)
Anton Hansson53c88442019-03-18 15:53:16 +0000421 config.TestProductVariables.DeviceResourceOverlays = deviceResourceOverlays
422 config.TestProductVariables.ProductResourceOverlays = productResourceOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800423 if testCase.enforceRROTargets != nil {
Colin Crossa74ca042019-01-31 14:31:51 -0800424 config.TestProductVariables.EnforceRROTargets = testCase.enforceRROTargets
Colin Cross890ff552017-11-30 20:13:19 -0800425 }
426 if testCase.enforceRROExcludedOverlays != nil {
Colin Crossa74ca042019-01-31 14:31:51 -0800427 config.TestProductVariables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800428 }
429
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700430 ctx := testAppContext(bp, fs)
Colin Cross890ff552017-11-30 20:13:19 -0800431 run(t, ctx, config)
432
Colin Crossbec85302019-02-13 13:15:46 -0800433 resourceListToFiles := func(module android.TestingModule, list []string) (files []string) {
434 for _, o := range list {
435 res := module.MaybeOutput(o)
436 if res.Rule != nil {
437 // If the overlay is compiled as part of this module (i.e. a .arsc.flat file),
438 // verify the inputs to the .arsc.flat rule.
439 files = append(files, res.Inputs.Strings()...)
440 } else {
441 // Otherwise, verify the full path to the output of the other module
442 files = append(files, o)
Anton Hansson94c93f32019-01-30 16:03:37 +0000443 }
Colin Cross890ff552017-11-30 20:13:19 -0800444 }
Colin Crossbec85302019-02-13 13:15:46 -0800445 return files
Colin Cross890ff552017-11-30 20:13:19 -0800446 }
447
Colin Crossbec85302019-02-13 13:15:46 -0800448 getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) {
449 module := ctx.ModuleForTests(moduleName, "android_common")
450 resourceList := module.MaybeOutput("aapt2/res.list")
451 if resourceList.Rule != nil {
452 resourceFiles = resourceListToFiles(module, resourceList.Inputs.Strings())
Anton Hansson0375a4f2019-01-24 14:39:19 +0000453 }
Colin Crossbec85302019-02-13 13:15:46 -0800454 overlayList := module.MaybeOutput("aapt2/overlay.list")
455 if overlayList.Rule != nil {
456 overlayFiles = resourceListToFiles(module, overlayList.Inputs.Strings())
457 }
458
Anton Hansson53c88442019-03-18 15:53:16 +0000459 for _, d := range module.Module().(AndroidLibraryDependency).ExportedRRODirs() {
460 var prefix string
461 if d.overlayType == device {
462 prefix = "device:"
463 } else if d.overlayType == product {
464 prefix = "product:"
465 } else {
466 t.Fatalf("Unexpected overlayType %d", d.overlayType)
467 }
468 rroDirs = append(rroDirs, prefix+d.path.String())
469 }
Colin Crossbec85302019-02-13 13:15:46 -0800470
471 return resourceFiles, overlayFiles, rroDirs
472 }
473
474 modules := []string{"foo", "bar", "lib", "lib2"}
475 for _, module := range modules {
476 resourceFiles, overlayFiles, rroDirs := getResources(module)
477
478 if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) {
479 t.Errorf("expected %s resource files:\n %#v\n got:\n %#v",
480 module, testCase.resourceFiles[module], resourceFiles)
481 }
482 if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) {
483 t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v",
484 module, testCase.overlayFiles[module], overlayFiles)
485 }
486 if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) {
Anton Hansson0375a4f2019-01-24 14:39:19 +0000487 t.Errorf("expected %s rroDirs: %#v\n got:\n %#v",
Colin Crossbec85302019-02-13 13:15:46 -0800488 module, testCase.rroDirs[module], rroDirs)
Anton Hansson0375a4f2019-01-24 14:39:19 +0000489 }
Colin Cross890ff552017-11-30 20:13:19 -0800490 }
Colin Cross890ff552017-11-30 20:13:19 -0800491 })
492 }
493}
Colin Crossd09b0b62018-04-18 11:06:47 -0700494
495func TestAppSdkVersion(t *testing.T) {
496 testCases := []struct {
497 name string
498 sdkVersion string
499 platformSdkInt int
500 platformSdkCodename string
501 platformSdkFinal bool
502 expectedMinSdkVersion string
Jeongik Cha538c0d02019-07-11 15:54:27 +0900503 platformApis bool
Colin Crossd09b0b62018-04-18 11:06:47 -0700504 }{
505 {
506 name: "current final SDK",
507 sdkVersion: "current",
508 platformSdkInt: 27,
509 platformSdkCodename: "REL",
510 platformSdkFinal: true,
511 expectedMinSdkVersion: "27",
512 },
513 {
514 name: "current non-final SDK",
515 sdkVersion: "current",
516 platformSdkInt: 27,
517 platformSdkCodename: "OMR1",
518 platformSdkFinal: false,
519 expectedMinSdkVersion: "OMR1",
520 },
521 {
522 name: "default final SDK",
523 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900524 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -0700525 platformSdkInt: 27,
526 platformSdkCodename: "REL",
527 platformSdkFinal: true,
528 expectedMinSdkVersion: "27",
529 },
530 {
531 name: "default non-final SDK",
532 sdkVersion: "",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900533 platformApis: true,
Colin Crossd09b0b62018-04-18 11:06:47 -0700534 platformSdkInt: 27,
535 platformSdkCodename: "OMR1",
536 platformSdkFinal: false,
537 expectedMinSdkVersion: "OMR1",
538 },
539 {
540 name: "14",
541 sdkVersion: "14",
542 expectedMinSdkVersion: "14",
543 },
544 }
545
546 for _, moduleType := range []string{"android_app", "android_library"} {
547 for _, test := range testCases {
548 t.Run(moduleType+" "+test.name, func(t *testing.T) {
Jeongik Cha538c0d02019-07-11 15:54:27 +0900549 platformApiProp := ""
550 if test.platformApis {
551 platformApiProp = "platform_apis: true,"
552 }
Colin Crossd09b0b62018-04-18 11:06:47 -0700553 bp := fmt.Sprintf(`%s {
554 name: "foo",
555 srcs: ["a.java"],
556 sdk_version: "%s",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900557 %s
558 }`, moduleType, test.sdkVersion, platformApiProp)
Colin Crossd09b0b62018-04-18 11:06:47 -0700559
560 config := testConfig(nil)
561 config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt
562 config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename
563 config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal
564
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700565 ctx := testAppContext(bp, nil)
Colin Crossd09b0b62018-04-18 11:06:47 -0700566
567 run(t, ctx, config)
568
569 foo := ctx.ModuleForTests("foo", "android_common")
570 link := foo.Output("package-res.apk")
571 linkFlags := strings.Split(link.Args["flags"], " ")
572 min := android.IndexList("--min-sdk-version", linkFlags)
573 target := android.IndexList("--target-sdk-version", linkFlags)
574
575 if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
576 t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
577 }
578
579 gotMinSdkVersion := linkFlags[min+1]
580 gotTargetSdkVersion := linkFlags[target+1]
581
582 if gotMinSdkVersion != test.expectedMinSdkVersion {
583 t.Errorf("incorrect --min-sdk-version, expected %q got %q",
584 test.expectedMinSdkVersion, gotMinSdkVersion)
585 }
586
587 if gotTargetSdkVersion != test.expectedMinSdkVersion {
588 t.Errorf("incorrect --target-sdk-version, expected %q got %q",
589 test.expectedMinSdkVersion, gotTargetSdkVersion)
590 }
591 })
592 }
593 }
594}
Colin Crossa4f08812018-10-02 22:03:40 -0700595
Paul Duffin50c217c2019-06-12 13:25:22 +0100596func TestJNIABI(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700597 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +0100598 cc_library {
599 name: "libjni",
600 system_shared_libs: [],
601 stl: "none",
602 }
603
604 android_test {
605 name: "test",
606 sdk_version: "core_platform",
607 jni_libs: ["libjni"],
608 }
609
610 android_test {
611 name: "test_first",
612 sdk_version: "core_platform",
613 compile_multilib: "first",
614 jni_libs: ["libjni"],
615 }
616
617 android_test {
618 name: "test_both",
619 sdk_version: "core_platform",
620 compile_multilib: "both",
621 jni_libs: ["libjni"],
622 }
623
624 android_test {
625 name: "test_32",
626 sdk_version: "core_platform",
627 compile_multilib: "32",
628 jni_libs: ["libjni"],
629 }
630
631 android_test {
632 name: "test_64",
633 sdk_version: "core_platform",
634 compile_multilib: "64",
635 jni_libs: ["libjni"],
636 }
637 `)
638
639 testCases := []struct {
640 name string
641 abis []string
642 }{
643 {"test", []string{"arm64-v8a"}},
644 {"test_first", []string{"arm64-v8a"}},
645 {"test_both", []string{"arm64-v8a", "armeabi-v7a"}},
646 {"test_32", []string{"armeabi-v7a"}},
647 {"test_64", []string{"arm64-v8a"}},
648 }
649
650 for _, test := range testCases {
651 t.Run(test.name, func(t *testing.T) {
652 app := ctx.ModuleForTests(test.name, "android_common")
653 jniLibZip := app.Output("jnilibs.zip")
654 var abis []string
655 args := strings.Fields(jniLibZip.Args["jarArgs"])
656 for i := 0; i < len(args); i++ {
657 if args[i] == "-P" {
658 abis = append(abis, filepath.Base(args[i+1]))
659 i++
660 }
661 }
662 if !reflect.DeepEqual(abis, test.abis) {
663 t.Errorf("want abis %v, got %v", test.abis, abis)
664 }
665 })
666 }
667}
668
Paul Duffin50c217c2019-06-12 13:25:22 +0100669func TestJNIPackaging(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700670 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +0100671 cc_library {
672 name: "libjni",
673 system_shared_libs: [],
674 stl: "none",
675 }
676
677 android_app {
678 name: "app",
679 jni_libs: ["libjni"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900680 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +0100681 }
682
683 android_app {
684 name: "app_noembed",
685 jni_libs: ["libjni"],
686 use_embedded_native_libs: false,
Jeongik Cha538c0d02019-07-11 15:54:27 +0900687 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +0100688 }
689
690 android_app {
691 name: "app_embed",
692 jni_libs: ["libjni"],
693 use_embedded_native_libs: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +0900694 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +0100695 }
696
697 android_test {
698 name: "test",
699 sdk_version: "core_platform",
700 jni_libs: ["libjni"],
701 }
702
703 android_test {
704 name: "test_noembed",
705 sdk_version: "core_platform",
706 jni_libs: ["libjni"],
707 use_embedded_native_libs: false,
708 }
709
710 android_test_helper_app {
711 name: "test_helper",
712 sdk_version: "core_platform",
713 jni_libs: ["libjni"],
714 }
715
716 android_test_helper_app {
717 name: "test_helper_noembed",
718 sdk_version: "core_platform",
719 jni_libs: ["libjni"],
720 use_embedded_native_libs: false,
721 }
722 `)
723
724 testCases := []struct {
725 name string
726 packaged bool
727 compressed bool
728 }{
729 {"app", false, false},
730 {"app_noembed", false, false},
731 {"app_embed", true, false},
732 {"test", true, false},
733 {"test_noembed", true, true},
734 {"test_helper", true, false},
735 {"test_helper_noembed", true, true},
736 }
737
738 for _, test := range testCases {
739 t.Run(test.name, func(t *testing.T) {
740 app := ctx.ModuleForTests(test.name, "android_common")
741 jniLibZip := app.MaybeOutput("jnilibs.zip")
742 if g, w := (jniLibZip.Rule != nil), test.packaged; g != w {
743 t.Errorf("expected jni packaged %v, got %v", w, g)
744 }
745
746 if jniLibZip.Rule != nil {
747 if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w {
748 t.Errorf("expected jni compressed %v, got %v", w, g)
749 }
750 }
751 })
752 }
Colin Cross47fa9d32019-03-26 10:51:39 -0700753}
754
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800755func TestCertificates(t *testing.T) {
756 testCases := []struct {
757 name string
758 bp string
759 certificateOverride string
760 expected string
761 }{
762 {
763 name: "default",
764 bp: `
765 android_app {
766 name: "foo",
767 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900768 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800769 }
770 `,
771 certificateOverride: "",
Dan Willemsen412160e2019-04-09 21:36:26 -0700772 expected: "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800773 },
774 {
775 name: "module certificate property",
776 bp: `
777 android_app {
778 name: "foo",
779 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900780 certificate: ":new_certificate",
781 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800782 }
783
784 android_app_certificate {
785 name: "new_certificate",
786 certificate: "cert/new_cert",
787 }
788 `,
789 certificateOverride: "",
790 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
791 },
792 {
793 name: "path certificate property",
794 bp: `
795 android_app {
796 name: "foo",
797 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900798 certificate: "expiredkey",
799 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800800 }
801 `,
802 certificateOverride: "",
Dan Willemsen412160e2019-04-09 21:36:26 -0700803 expected: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800804 },
805 {
806 name: "certificate overrides",
807 bp: `
808 android_app {
809 name: "foo",
810 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900811 certificate: "expiredkey",
812 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800813 }
814
815 android_app_certificate {
816 name: "new_certificate",
817 certificate: "cert/new_cert",
818 }
819 `,
820 certificateOverride: "foo:new_certificate",
821 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
822 },
823 }
824
825 for _, test := range testCases {
826 t.Run(test.name, func(t *testing.T) {
827 config := testConfig(nil)
828 if test.certificateOverride != "" {
829 config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride}
830 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700831 ctx := testAppContext(test.bp, nil)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800832
833 run(t, ctx, config)
834 foo := ctx.ModuleForTests("foo", "android_common")
835
836 signapk := foo.Output("foo.apk")
837 signFlags := signapk.Args["certificates"]
838 if test.expected != signFlags {
839 t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags)
840 }
841 })
842 }
843}
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800844
845func TestPackageNameOverride(t *testing.T) {
846 testCases := []struct {
847 name string
848 bp string
849 packageNameOverride string
850 expected []string
851 }{
852 {
853 name: "default",
854 bp: `
855 android_app {
856 name: "foo",
857 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900858 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800859 }
860 `,
861 packageNameOverride: "",
862 expected: []string{
863 buildDir + "/.intermediates/foo/android_common/foo.apk",
864 buildDir + "/target/product/test_device/system/app/foo/foo.apk",
865 },
866 },
867 {
868 name: "overridden",
869 bp: `
870 android_app {
871 name: "foo",
872 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900873 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800874 }
875 `,
876 packageNameOverride: "foo:bar",
877 expected: []string{
878 // The package apk should be still be the original name for test dependencies.
879 buildDir + "/.intermediates/foo/android_common/foo.apk",
880 buildDir + "/target/product/test_device/system/app/bar/bar.apk",
881 },
882 },
883 }
884
885 for _, test := range testCases {
886 t.Run(test.name, func(t *testing.T) {
887 config := testConfig(nil)
888 if test.packageNameOverride != "" {
889 config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride}
890 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700891 ctx := testAppContext(test.bp, nil)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800892
893 run(t, ctx, config)
894 foo := ctx.ModuleForTests("foo", "android_common")
895
896 outputs := foo.AllOutputs()
897 outputMap := make(map[string]bool)
898 for _, o := range outputs {
899 outputMap[o] = true
900 }
901 for _, e := range test.expected {
902 if _, exist := outputMap[e]; !exist {
903 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
904 }
905 }
906 })
907 }
908}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800909
910func TestInstrumentationTargetOverridden(t *testing.T) {
911 bp := `
912 android_app {
913 name: "foo",
914 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900915 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800916 }
917
918 android_test {
919 name: "bar",
920 instrumentation_for: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900921 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800922 }
923 `
924 config := testConfig(nil)
925 config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700926 ctx := testAppContext(bp, nil)
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800927
928 run(t, ctx, config)
929
930 bar := ctx.ModuleForTests("bar", "android_common")
931 res := bar.Output("package-res.apk")
932 aapt2Flags := res.Args["flags"]
933 e := "--rename-instrumentation-target-package org.dandroid.bp"
934 if !strings.Contains(aapt2Flags, e) {
935 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
936 }
937}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800938
939func TestOverrideAndroidApp(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700940 ctx, _ := testJava(t, `
Jaewoong Jung525443a2019-02-28 15:35:54 -0800941 android_app {
942 name: "foo",
943 srcs: ["a.java"],
Jaewoong Junga641ee92019-03-27 11:17:14 -0700944 certificate: "expiredkey",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700945 overrides: ["qux"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900946 sdk_version: "current",
Jaewoong Jung525443a2019-02-28 15:35:54 -0800947 }
948
949 override_android_app {
950 name: "bar",
951 base: "foo",
952 certificate: ":new_certificate",
953 }
954
955 android_app_certificate {
956 name: "new_certificate",
957 certificate: "cert/new_cert",
958 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700959
960 override_android_app {
961 name: "baz",
962 base: "foo",
963 package_name: "org.dandroid.bp",
964 }
Jaewoong Jung525443a2019-02-28 15:35:54 -0800965 `)
966
967 expectedVariants := []struct {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700968 moduleName string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800969 variantName string
970 apkName string
971 apkPath string
972 signFlag string
973 overrides []string
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700974 aaptFlag string
Jaewoong Jung525443a2019-02-28 15:35:54 -0800975 }{
976 {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700977 moduleName: "foo",
Jaewoong Jung525443a2019-02-28 15:35:54 -0800978 variantName: "android_common",
979 apkPath: "/target/product/test_device/system/app/foo/foo.apk",
Dan Willemsen412160e2019-04-09 21:36:26 -0700980 signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700981 overrides: []string{"qux"},
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700982 aaptFlag: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -0800983 },
984 {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700985 moduleName: "bar",
986 variantName: "android_common_bar",
Jaewoong Jung525443a2019-02-28 15:35:54 -0800987 apkPath: "/target/product/test_device/system/app/bar/bar.apk",
988 signFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700989 overrides: []string{"qux", "foo"},
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700990 aaptFlag: "",
991 },
992 {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700993 moduleName: "baz",
994 variantName: "android_common_baz",
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700995 apkPath: "/target/product/test_device/system/app/baz/baz.apk",
Dan Willemsen412160e2019-04-09 21:36:26 -0700996 signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700997 overrides: []string{"qux", "foo"},
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700998 aaptFlag: "--rename-manifest-package org.dandroid.bp",
Jaewoong Jung525443a2019-02-28 15:35:54 -0800999 },
1000 }
1001 for _, expected := range expectedVariants {
1002 variant := ctx.ModuleForTests("foo", expected.variantName)
1003
1004 // Check the final apk name
1005 outputs := variant.AllOutputs()
1006 expectedApkPath := buildDir + expected.apkPath
1007 found := false
1008 for _, o := range outputs {
1009 if o == expectedApkPath {
1010 found = true
1011 break
1012 }
1013 }
1014 if !found {
1015 t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs)
1016 }
1017
1018 // Check the certificate paths
1019 signapk := variant.Output("foo.apk")
1020 signFlag := signapk.Args["certificates"]
1021 if expected.signFlag != signFlag {
1022 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected.signFlag, signFlag)
1023 }
1024
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001025 // Check if the overrides field values are correctly aggregated.
Jaewoong Jung525443a2019-02-28 15:35:54 -08001026 mod := variant.Module().(*AndroidApp)
1027 if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) {
1028 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
1029 expected.overrides, mod.appProperties.Overrides)
1030 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001031
1032 // Check the package renaming flag, if exists.
1033 res := variant.Output("package-res.apk")
1034 aapt2Flags := res.Args["flags"]
1035 if !strings.Contains(aapt2Flags, expected.aaptFlag) {
1036 t.Errorf("package renaming flag, %q is missing in aapt2 link flags, %q", expected.aaptFlag, aapt2Flags)
1037 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08001038 }
1039}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001040
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001041func TestOverrideAndroidAppDependency(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001042 ctx, _ := testJava(t, `
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001043 android_app {
1044 name: "foo",
1045 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001046 sdk_version: "current",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001047 }
1048
1049 override_android_app {
1050 name: "bar",
1051 base: "foo",
1052 package_name: "org.dandroid.bp",
1053 }
1054
1055 android_test {
1056 name: "baz",
1057 srcs: ["b.java"],
1058 instrumentation_for: "foo",
1059 }
1060
1061 android_test {
1062 name: "qux",
1063 srcs: ["b.java"],
1064 instrumentation_for: "bar",
1065 }
1066 `)
1067
1068 // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg.
1069 javac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1070 fooTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1071 if !strings.Contains(javac.Args["classpath"], fooTurbine) {
1072 t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine)
1073 }
1074
1075 // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg.
1076 javac = ctx.ModuleForTests("qux", "android_common").Rule("javac")
1077 barTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common_bar", "turbine-combined", "foo.jar")
1078 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1079 t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
1080 }
1081}
1082
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001083func TestAndroidAppImport(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001084 ctx, _ := testJava(t, `
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001085 android_app_import {
1086 name: "foo",
1087 apk: "prebuilts/apk/app.apk",
1088 certificate: "platform",
1089 dex_preopt: {
1090 enabled: true,
1091 },
1092 }
1093 `)
1094
1095 variant := ctx.ModuleForTests("foo", "android_common")
1096
1097 // Check dexpreopt outputs.
1098 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
1099 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
1100 t.Errorf("can't find dexpreopt outputs")
1101 }
1102
1103 // Check cert signing flag.
1104 signedApk := variant.Output("signed/foo.apk")
1105 signingFlag := signedApk.Args["certificates"]
1106 expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8"
1107 if expected != signingFlag {
1108 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
1109 }
1110}
1111
1112func TestAndroidAppImport_NoDexPreopt(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001113 ctx, _ := testJava(t, `
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001114 android_app_import {
1115 name: "foo",
1116 apk: "prebuilts/apk/app.apk",
1117 certificate: "platform",
1118 dex_preopt: {
1119 enabled: false,
1120 },
1121 }
1122 `)
1123
1124 variant := ctx.ModuleForTests("foo", "android_common")
1125
1126 // Check dexpreopt outputs. They shouldn't exist.
1127 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule != nil ||
1128 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule != nil {
1129 t.Errorf("dexpreopt shouldn't have run.")
1130 }
1131}
1132
1133func TestAndroidAppImport_Presigned(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001134 ctx, _ := testJava(t, `
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001135 android_app_import {
1136 name: "foo",
1137 apk: "prebuilts/apk/app.apk",
1138 presigned: true,
1139 dex_preopt: {
1140 enabled: true,
1141 },
1142 }
1143 `)
1144
1145 variant := ctx.ModuleForTests("foo", "android_common")
1146
1147 // Check dexpreopt outputs.
1148 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
1149 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
1150 t.Errorf("can't find dexpreopt outputs")
1151 }
1152 // Make sure stripping wasn't done.
1153 stripRule := variant.Output("dexpreopt/foo.apk")
1154 if !strings.HasPrefix(stripRule.RuleParams.Command, "cp -f") {
1155 t.Errorf("unexpected, non-skipping strip command: %q", stripRule.RuleParams.Command)
1156 }
1157
1158 // Make sure signing was skipped and aligning was done instead.
1159 if variant.MaybeOutput("signed/foo.apk").Rule != nil {
1160 t.Errorf("signing rule shouldn't be included.")
1161 }
1162 if variant.MaybeOutput("zip-aligned/foo.apk").Rule == nil {
1163 t.Errorf("can't find aligning rule")
1164 }
1165}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001166
1167func TestAndroidAppImport_DpiVariants(t *testing.T) {
1168 bp := `
1169 android_app_import {
1170 name: "foo",
1171 apk: "prebuilts/apk/app.apk",
1172 dpi_variants: {
1173 xhdpi: {
1174 apk: "prebuilts/apk/app_xhdpi.apk",
1175 },
1176 xxhdpi: {
1177 apk: "prebuilts/apk/app_xxhdpi.apk",
1178 },
1179 },
1180 certificate: "PRESIGNED",
1181 dex_preopt: {
1182 enabled: true,
1183 },
1184 }
1185 `
1186 testCases := []struct {
1187 name string
1188 aaptPreferredConfig *string
1189 aaptPrebuiltDPI []string
1190 expected string
1191 }{
1192 {
1193 name: "no preferred",
1194 aaptPreferredConfig: nil,
1195 aaptPrebuiltDPI: []string{},
1196 expected: "prebuilts/apk/app.apk",
1197 },
1198 {
1199 name: "AAPTPreferredConfig matches",
1200 aaptPreferredConfig: proptools.StringPtr("xhdpi"),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001201 aaptPrebuiltDPI: []string{"xxhdpi", "ldpi"},
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001202 expected: "prebuilts/apk/app_xhdpi.apk",
1203 },
1204 {
1205 name: "AAPTPrebuiltDPI matches",
1206 aaptPreferredConfig: proptools.StringPtr("mdpi"),
1207 aaptPrebuiltDPI: []string{"xxhdpi", "xhdpi"},
1208 expected: "prebuilts/apk/app_xxhdpi.apk",
1209 },
1210 {
1211 name: "non-first AAPTPrebuiltDPI matches",
1212 aaptPreferredConfig: proptools.StringPtr("mdpi"),
1213 aaptPrebuiltDPI: []string{"ldpi", "xhdpi"},
1214 expected: "prebuilts/apk/app_xhdpi.apk",
1215 },
1216 {
1217 name: "no matches",
1218 aaptPreferredConfig: proptools.StringPtr("mdpi"),
1219 aaptPrebuiltDPI: []string{"ldpi", "xxxhdpi"},
1220 expected: "prebuilts/apk/app.apk",
1221 },
1222 }
1223
1224 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
1225 for _, test := range testCases {
1226 config := testConfig(nil)
1227 config.TestProductVariables.AAPTPreferredConfig = test.aaptPreferredConfig
1228 config.TestProductVariables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001229 ctx := testAppContext(bp, nil)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001230
1231 run(t, ctx, config)
1232
1233 variant := ctx.ModuleForTests("foo", "android_common")
1234 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
1235 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
1236 if len(matches) != 2 {
1237 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
1238 }
1239 if test.expected != matches[1] {
1240 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
1241 }
1242 }
1243}
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001244
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001245func TestAndroidAppImport_Filename(t *testing.T) {
1246 ctx, config := testJava(t, `
1247 android_app_import {
1248 name: "foo",
1249 apk: "prebuilts/apk/app.apk",
1250 presigned: true,
1251 }
1252
1253 android_app_import {
1254 name: "bar",
1255 apk: "prebuilts/apk/app.apk",
1256 presigned: true,
1257 filename: "bar_sample.apk"
1258 }
1259 `)
1260
1261 testCases := []struct {
1262 name string
1263 expected string
1264 }{
1265 {
1266 name: "foo",
1267 expected: "foo.apk",
1268 },
1269 {
1270 name: "bar",
1271 expected: "bar_sample.apk",
1272 },
1273 }
1274
1275 for _, test := range testCases {
1276 variant := ctx.ModuleForTests(test.name, "android_common")
1277 if variant.MaybeOutput(test.expected).Rule == nil {
1278 t.Errorf("can't find output named %q - all outputs: %v", test.expected, variant.AllOutputs())
1279 }
1280
1281 a := variant.Module().(*AndroidAppImport)
1282 expectedValues := []string{test.expected}
1283 actualValues := android.AndroidMkEntriesForTest(
1284 t, config, "", a).EntryMap["LOCAL_INSTALLED_MODULE_STEM"]
1285 if !reflect.DeepEqual(actualValues, expectedValues) {
1286 t.Errorf("Incorrect LOCAL_INSTALLED_MODULE_STEM value '%s', expected '%s'",
1287 actualValues, expectedValues)
1288 }
1289 }
1290}
1291
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001292func TestAndroidAppImport_ArchVariants(t *testing.T) {
1293 // The test config's target arch is ARM64.
1294 testCases := []struct {
1295 name string
1296 bp string
1297 expected string
1298 }{
1299 {
1300 name: "matching arch",
1301 bp: `
1302 android_app_import {
1303 name: "foo",
1304 apk: "prebuilts/apk/app.apk",
1305 arch: {
1306 arm64: {
1307 apk: "prebuilts/apk/app_arm64.apk",
1308 },
1309 },
1310 certificate: "PRESIGNED",
1311 dex_preopt: {
1312 enabled: true,
1313 },
1314 }
1315 `,
1316 expected: "prebuilts/apk/app_arm64.apk",
1317 },
1318 {
1319 name: "no matching arch",
1320 bp: `
1321 android_app_import {
1322 name: "foo",
1323 apk: "prebuilts/apk/app.apk",
1324 arch: {
1325 arm: {
1326 apk: "prebuilts/apk/app_arm.apk",
1327 },
1328 },
1329 certificate: "PRESIGNED",
1330 dex_preopt: {
1331 enabled: true,
1332 },
1333 }
1334 `,
1335 expected: "prebuilts/apk/app.apk",
1336 },
1337 }
1338
1339 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
1340 for _, test := range testCases {
1341 ctx, _ := testJava(t, test.bp)
1342
1343 variant := ctx.ModuleForTests("foo", "android_common")
1344 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
1345 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
1346 if len(matches) != 2 {
1347 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
1348 }
1349 if test.expected != matches[1] {
1350 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
1351 }
1352 }
1353}
1354
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001355func TestStl(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001356 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001357 cc_library {
1358 name: "libjni",
1359 }
1360
1361 android_test {
1362 name: "stl",
1363 jni_libs: ["libjni"],
1364 compile_multilib: "both",
1365 sdk_version: "current",
1366 stl: "c++_shared",
1367 }
1368
1369 android_test {
1370 name: "system",
1371 jni_libs: ["libjni"],
1372 compile_multilib: "both",
1373 sdk_version: "current",
1374 }
Jaewoong Jung710756a2019-06-04 11:53:47 -07001375
1376 ndk_prebuilt_shared_stl {
1377 name: "ndk_libc++_shared",
1378 }
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001379 `)
1380
1381 testCases := []struct {
1382 name string
1383 jnis []string
1384 }{
1385 {"stl",
1386 []string{
1387 "libjni.so",
Jaewoong Jung710756a2019-06-04 11:53:47 -07001388 "libc++_shared.so",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001389 },
1390 },
1391 {"system",
1392 []string{
1393 "libjni.so",
1394 },
1395 },
1396 }
1397
1398 for _, test := range testCases {
1399 t.Run(test.name, func(t *testing.T) {
1400 app := ctx.ModuleForTests(test.name, "android_common")
1401 jniLibZip := app.Output("jnilibs.zip")
1402 var jnis []string
1403 args := strings.Fields(jniLibZip.Args["jarArgs"])
1404 for i := 0; i < len(args); i++ {
1405 if args[i] == "-f" {
1406 jnis = append(jnis, args[i+1])
1407 i += 1
1408 }
1409 }
1410 jnisJoined := strings.Join(jnis, " ")
1411 for _, jni := range test.jnis {
1412 if !strings.Contains(jnisJoined, jni) {
1413 t.Errorf("missing jni %q in %q", jni, jnis)
1414 }
1415 }
1416 })
1417 }
1418}
Colin Cross50ddcc42019-05-16 12:28:22 -07001419
1420func TestUsesLibraries(t *testing.T) {
1421 bp := `
1422 java_sdk_library {
1423 name: "foo",
1424 srcs: ["a.java"],
1425 api_packages: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001426 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07001427 }
1428
1429 java_sdk_library {
1430 name: "bar",
1431 srcs: ["a.java"],
1432 api_packages: ["bar"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001433 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07001434 }
1435
1436 android_app {
1437 name: "app",
1438 srcs: ["a.java"],
1439 uses_libs: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001440 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07001441 optional_uses_libs: [
1442 "bar",
1443 "baz",
1444 ],
1445 }
1446
1447 android_app_import {
1448 name: "prebuilt",
1449 apk: "prebuilts/apk/app.apk",
1450 certificate: "platform",
1451 uses_libs: ["foo"],
1452 optional_uses_libs: [
1453 "bar",
1454 "baz",
1455 ],
1456 }
1457 `
1458
1459 config := testConfig(nil)
1460 config.TestProductVariables.MissingUsesLibraries = []string{"baz"}
1461
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001462 ctx := testAppContext(bp, nil)
Colin Cross50ddcc42019-05-16 12:28:22 -07001463
1464 run(t, ctx, config)
1465
1466 app := ctx.ModuleForTests("app", "android_common")
1467 prebuilt := ctx.ModuleForTests("prebuilt", "android_common")
1468
1469 // Test that all libraries are verified
1470 cmd := app.Rule("verify_uses_libraries").RuleParams.Command
1471 if w := "--uses-library foo"; !strings.Contains(cmd, w) {
1472 t.Errorf("wanted %q in %q", w, cmd)
1473 }
1474
1475 if w := "--optional-uses-library bar --optional-uses-library baz"; !strings.Contains(cmd, w) {
1476 t.Errorf("wanted %q in %q", w, cmd)
1477 }
1478
1479 cmd = prebuilt.Rule("verify_uses_libraries").RuleParams.Command
1480
1481 if w := `uses_library_names="foo"`; !strings.Contains(cmd, w) {
1482 t.Errorf("wanted %q in %q", w, cmd)
1483 }
1484
1485 if w := `optional_uses_library_names="bar baz"`; !strings.Contains(cmd, w) {
1486 t.Errorf("wanted %q in %q", w, cmd)
1487 }
1488
1489 // Test that only present libraries are preopted
1490 cmd = app.Rule("dexpreopt").RuleParams.Command
1491
1492 if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) {
1493 t.Errorf("wanted %q in %q", w, cmd)
1494 }
1495
1496 cmd = prebuilt.Rule("dexpreopt").RuleParams.Command
1497
1498 if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) {
1499 t.Errorf("wanted %q in %q", w, cmd)
1500 }
1501}
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001502
1503func TestCodelessApp(t *testing.T) {
1504 testCases := []struct {
1505 name string
1506 bp string
1507 noCode bool
1508 }{
1509 {
1510 name: "normal",
1511 bp: `
1512 android_app {
1513 name: "foo",
1514 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001515 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001516 }
1517 `,
1518 noCode: false,
1519 },
1520 {
1521 name: "app without sources",
1522 bp: `
1523 android_app {
1524 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001525 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001526 }
1527 `,
1528 noCode: true,
1529 },
1530 {
1531 name: "app with libraries",
1532 bp: `
1533 android_app {
1534 name: "foo",
1535 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001536 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001537 }
1538
1539 java_library {
1540 name: "lib",
1541 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001542 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001543 }
1544 `,
1545 noCode: false,
1546 },
1547 {
1548 name: "app with sourceless libraries",
1549 bp: `
1550 android_app {
1551 name: "foo",
1552 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001553 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001554 }
1555
1556 java_library {
1557 name: "lib",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001558 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001559 }
1560 `,
1561 // TODO(jungjw): this should probably be true
1562 noCode: false,
1563 },
1564 }
1565
1566 for _, test := range testCases {
1567 t.Run(test.name, func(t *testing.T) {
1568 ctx := testApp(t, test.bp)
1569
1570 foo := ctx.ModuleForTests("foo", "android_common")
1571 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
1572 if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode {
1573 t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs)
1574 }
1575 })
1576 }
1577}
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001578
1579func TestEmbedNotice(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001580 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001581 android_app {
1582 name: "foo",
1583 srcs: ["a.java"],
1584 static_libs: ["javalib"],
1585 jni_libs: ["libjni"],
1586 notice: "APP_NOTICE",
1587 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001588 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001589 }
1590
1591 // No embed_notice flag
1592 android_app {
1593 name: "bar",
1594 srcs: ["a.java"],
1595 jni_libs: ["libjni"],
1596 notice: "APP_NOTICE",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001597 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001598 }
1599
1600 // No NOTICE files
1601 android_app {
1602 name: "baz",
1603 srcs: ["a.java"],
1604 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001605 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001606 }
1607
1608 cc_library {
1609 name: "libjni",
1610 system_shared_libs: [],
1611 stl: "none",
1612 notice: "LIB_NOTICE",
1613 }
1614
1615 java_library {
1616 name: "javalib",
1617 srcs: [
1618 ":gen",
1619 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001620 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001621 }
1622
1623 genrule {
1624 name: "gen",
1625 tools: ["gentool"],
1626 out: ["gen.java"],
1627 notice: "GENRULE_NOTICE",
1628 }
1629
1630 java_binary_host {
1631 name: "gentool",
1632 srcs: ["b.java"],
1633 notice: "TOOL_NOTICE",
1634 }
1635 `)
1636
1637 // foo has NOTICE files to process, and embed_notices is true.
1638 foo := ctx.ModuleForTests("foo", "android_common")
1639 // verify merge notices rule.
1640 mergeNotices := foo.Rule("mergeNoticesRule")
1641 noticeInputs := mergeNotices.Inputs.Strings()
1642 // TOOL_NOTICE should be excluded as it's a host module.
1643 if len(mergeNotices.Inputs) != 3 {
1644 t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs)
1645 }
1646 if !inList("APP_NOTICE", noticeInputs) {
1647 t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs)
1648 }
1649 if !inList("LIB_NOTICE", noticeInputs) {
1650 t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs)
1651 }
1652 if !inList("GENRULE_NOTICE", noticeInputs) {
1653 t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs)
1654 }
1655 // aapt2 flags should include -A <NOTICE dir> so that its contents are put in the APK's /assets.
1656 res := foo.Output("package-res.apk")
1657 aapt2Flags := res.Args["flags"]
1658 e := "-A " + buildDir + "/.intermediates/foo/android_common/NOTICE"
1659 if !strings.Contains(aapt2Flags, e) {
1660 t.Errorf("asset dir flag for NOTICE, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
1661 }
1662
1663 // bar has NOTICE files to process, but embed_notices is not set.
1664 bar := ctx.ModuleForTests("bar", "android_common")
Jaewoong Jung98772792019-07-01 17:15:13 -07001665 res = bar.Output("package-res.apk")
1666 aapt2Flags = res.Args["flags"]
1667 e = "-A " + buildDir + "/.intermediates/bar/android_common/NOTICE"
1668 if strings.Contains(aapt2Flags, e) {
1669 t.Errorf("bar shouldn't have the asset dir flag for NOTICE: %q", e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001670 }
1671
1672 // baz's embed_notice is true, but it doesn't have any NOTICE files.
1673 baz := ctx.ModuleForTests("baz", "android_common")
Jaewoong Jung98772792019-07-01 17:15:13 -07001674 res = baz.Output("package-res.apk")
1675 aapt2Flags = res.Args["flags"]
1676 e = "-A " + buildDir + "/.intermediates/baz/android_common/NOTICE"
1677 if strings.Contains(aapt2Flags, e) {
1678 t.Errorf("baz shouldn't have the asset dir flag for NOTICE: %q", e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001679 }
1680}
Colin Cross53a87f52019-06-25 13:35:30 -07001681
1682func TestUncompressDex(t *testing.T) {
1683 testCases := []struct {
1684 name string
1685 bp string
1686
1687 uncompressedPlatform bool
1688 uncompressedUnbundled bool
1689 }{
1690 {
1691 name: "normal",
1692 bp: `
1693 android_app {
1694 name: "foo",
1695 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001696 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07001697 }
1698 `,
1699 uncompressedPlatform: true,
1700 uncompressedUnbundled: false,
1701 },
1702 {
1703 name: "use_embedded_dex",
1704 bp: `
1705 android_app {
1706 name: "foo",
1707 use_embedded_dex: true,
1708 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001709 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07001710 }
1711 `,
1712 uncompressedPlatform: true,
1713 uncompressedUnbundled: true,
1714 },
1715 {
1716 name: "privileged",
1717 bp: `
1718 android_app {
1719 name: "foo",
1720 privileged: true,
1721 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001722 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07001723 }
1724 `,
1725 uncompressedPlatform: true,
1726 uncompressedUnbundled: true,
1727 },
1728 }
1729
1730 test := func(t *testing.T, bp string, want bool, unbundled bool) {
1731 t.Helper()
1732
1733 config := testConfig(nil)
1734 if unbundled {
1735 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
1736 }
1737
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001738 ctx := testAppContext(bp, nil)
Colin Cross53a87f52019-06-25 13:35:30 -07001739
1740 run(t, ctx, config)
1741
1742 foo := ctx.ModuleForTests("foo", "android_common")
1743 dex := foo.Rule("r8")
1744 uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0")
1745 aligned := foo.MaybeRule("zipalign").Rule != nil
1746
1747 if uncompressedInDexJar != want {
1748 t.Errorf("want uncompressed in dex %v, got %v", want, uncompressedInDexJar)
1749 }
1750
1751 if aligned != want {
1752 t.Errorf("want aligned %v, got %v", want, aligned)
1753 }
1754 }
1755
1756 for _, tt := range testCases {
1757 t.Run(tt.name, func(t *testing.T) {
1758 t.Run("platform", func(t *testing.T) {
1759 test(t, tt.bp, tt.uncompressedPlatform, false)
1760 })
1761 t.Run("unbundled", func(t *testing.T) {
1762 test(t, tt.bp, tt.uncompressedUnbundled, true)
1763 })
1764 })
1765 }
1766}