blob: ba08f58660a9f60c03b9629d45959345db287cae [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
Jeongik Cha2cc570d2019-10-29 15:44:45 +0900669func TestAppSdkVersionByPartition(t *testing.T) {
670 testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", `
671 android_app {
672 name: "foo",
673 srcs: ["a.java"],
674 vendor: true,
675 platform_apis: true,
676 }
677 `)
678
679 testJava(t, `
680 android_app {
681 name: "bar",
682 srcs: ["b.java"],
683 platform_apis: true,
684 }
685 `)
686
687 for _, enforce := range []bool{true, false} {
688
689 config := testConfig(nil)
690 config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce)
691 bp := `
692 android_app {
693 name: "foo",
694 srcs: ["a.java"],
695 product_specific: true,
696 platform_apis: true,
697 }
698 `
699 if enforce {
700 testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", bp, config)
701 } else {
702 testJavaWithConfig(t, bp, config)
703 }
704 }
705}
706
Paul Duffin50c217c2019-06-12 13:25:22 +0100707func TestJNIPackaging(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700708 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Paul Duffin50c217c2019-06-12 13:25:22 +0100709 cc_library {
710 name: "libjni",
711 system_shared_libs: [],
712 stl: "none",
713 }
714
715 android_app {
716 name: "app",
717 jni_libs: ["libjni"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900718 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +0100719 }
720
721 android_app {
722 name: "app_noembed",
723 jni_libs: ["libjni"],
724 use_embedded_native_libs: false,
Jeongik Cha538c0d02019-07-11 15:54:27 +0900725 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +0100726 }
727
728 android_app {
729 name: "app_embed",
730 jni_libs: ["libjni"],
731 use_embedded_native_libs: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +0900732 sdk_version: "current",
Paul Duffin50c217c2019-06-12 13:25:22 +0100733 }
734
735 android_test {
736 name: "test",
737 sdk_version: "core_platform",
738 jni_libs: ["libjni"],
739 }
740
741 android_test {
742 name: "test_noembed",
743 sdk_version: "core_platform",
744 jni_libs: ["libjni"],
745 use_embedded_native_libs: false,
746 }
747
748 android_test_helper_app {
749 name: "test_helper",
750 sdk_version: "core_platform",
751 jni_libs: ["libjni"],
752 }
753
754 android_test_helper_app {
755 name: "test_helper_noembed",
756 sdk_version: "core_platform",
757 jni_libs: ["libjni"],
758 use_embedded_native_libs: false,
759 }
760 `)
761
762 testCases := []struct {
763 name string
764 packaged bool
765 compressed bool
766 }{
767 {"app", false, false},
768 {"app_noembed", false, false},
769 {"app_embed", true, false},
770 {"test", true, false},
771 {"test_noembed", true, true},
772 {"test_helper", true, false},
773 {"test_helper_noembed", true, true},
774 }
775
776 for _, test := range testCases {
777 t.Run(test.name, func(t *testing.T) {
778 app := ctx.ModuleForTests(test.name, "android_common")
779 jniLibZip := app.MaybeOutput("jnilibs.zip")
780 if g, w := (jniLibZip.Rule != nil), test.packaged; g != w {
781 t.Errorf("expected jni packaged %v, got %v", w, g)
782 }
783
784 if jniLibZip.Rule != nil {
785 if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w {
786 t.Errorf("expected jni compressed %v, got %v", w, g)
787 }
788 }
789 })
790 }
Colin Cross47fa9d32019-03-26 10:51:39 -0700791}
792
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800793func TestCertificates(t *testing.T) {
794 testCases := []struct {
795 name string
796 bp string
797 certificateOverride string
798 expected string
799 }{
800 {
801 name: "default",
802 bp: `
803 android_app {
804 name: "foo",
805 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900806 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800807 }
808 `,
809 certificateOverride: "",
Dan Willemsen412160e2019-04-09 21:36:26 -0700810 expected: "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800811 },
812 {
813 name: "module certificate property",
814 bp: `
815 android_app {
816 name: "foo",
817 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900818 certificate: ":new_certificate",
819 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800820 }
821
822 android_app_certificate {
823 name: "new_certificate",
824 certificate: "cert/new_cert",
825 }
826 `,
827 certificateOverride: "",
828 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
829 },
830 {
831 name: "path certificate property",
832 bp: `
833 android_app {
834 name: "foo",
835 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900836 certificate: "expiredkey",
837 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800838 }
839 `,
840 certificateOverride: "",
Dan Willemsen412160e2019-04-09 21:36:26 -0700841 expected: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800842 },
843 {
844 name: "certificate overrides",
845 bp: `
846 android_app {
847 name: "foo",
848 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900849 certificate: "expiredkey",
850 sdk_version: "current",
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800851 }
852
853 android_app_certificate {
854 name: "new_certificate",
855 certificate: "cert/new_cert",
856 }
857 `,
858 certificateOverride: "foo:new_certificate",
859 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
860 },
861 }
862
863 for _, test := range testCases {
864 t.Run(test.name, func(t *testing.T) {
865 config := testConfig(nil)
866 if test.certificateOverride != "" {
867 config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride}
868 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700869 ctx := testAppContext(test.bp, nil)
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800870
871 run(t, ctx, config)
872 foo := ctx.ModuleForTests("foo", "android_common")
873
874 signapk := foo.Output("foo.apk")
875 signFlags := signapk.Args["certificates"]
876 if test.expected != signFlags {
877 t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags)
878 }
879 })
880 }
881}
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800882
883func TestPackageNameOverride(t *testing.T) {
884 testCases := []struct {
885 name string
886 bp string
887 packageNameOverride string
888 expected []string
889 }{
890 {
891 name: "default",
892 bp: `
893 android_app {
894 name: "foo",
895 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900896 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800897 }
898 `,
899 packageNameOverride: "",
900 expected: []string{
901 buildDir + "/.intermediates/foo/android_common/foo.apk",
902 buildDir + "/target/product/test_device/system/app/foo/foo.apk",
903 },
904 },
905 {
906 name: "overridden",
907 bp: `
908 android_app {
909 name: "foo",
910 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900911 sdk_version: "current",
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800912 }
913 `,
914 packageNameOverride: "foo:bar",
915 expected: []string{
916 // The package apk should be still be the original name for test dependencies.
917 buildDir + "/.intermediates/foo/android_common/foo.apk",
918 buildDir + "/target/product/test_device/system/app/bar/bar.apk",
919 },
920 },
921 }
922
923 for _, test := range testCases {
924 t.Run(test.name, func(t *testing.T) {
925 config := testConfig(nil)
926 if test.packageNameOverride != "" {
927 config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride}
928 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700929 ctx := testAppContext(test.bp, nil)
Jaewoong Jung9d22a912019-01-23 16:27:47 -0800930
931 run(t, ctx, config)
932 foo := ctx.ModuleForTests("foo", "android_common")
933
934 outputs := foo.AllOutputs()
935 outputMap := make(map[string]bool)
936 for _, o := range outputs {
937 outputMap[o] = true
938 }
939 for _, e := range test.expected {
940 if _, exist := outputMap[e]; !exist {
941 t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs)
942 }
943 }
944 })
945 }
946}
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800947
948func TestInstrumentationTargetOverridden(t *testing.T) {
949 bp := `
950 android_app {
951 name: "foo",
952 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900953 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800954 }
955
956 android_test {
957 name: "bar",
958 instrumentation_for: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +0900959 sdk_version: "current",
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800960 }
961 `
962 config := testConfig(nil)
963 config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"}
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700964 ctx := testAppContext(bp, nil)
Jaewoong Jung4102e5d2019-02-27 16:26:28 -0800965
966 run(t, ctx, config)
967
968 bar := ctx.ModuleForTests("bar", "android_common")
969 res := bar.Output("package-res.apk")
970 aapt2Flags := res.Args["flags"]
971 e := "--rename-instrumentation-target-package org.dandroid.bp"
972 if !strings.Contains(aapt2Flags, e) {
973 t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
974 }
975}
Jaewoong Jung525443a2019-02-28 15:35:54 -0800976
977func TestOverrideAndroidApp(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700978 ctx, _ := testJava(t, `
Jaewoong Jung525443a2019-02-28 15:35:54 -0800979 android_app {
980 name: "foo",
981 srcs: ["a.java"],
Jaewoong Junga641ee92019-03-27 11:17:14 -0700982 certificate: "expiredkey",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -0700983 overrides: ["qux"],
Jeongik Cha538c0d02019-07-11 15:54:27 +0900984 sdk_version: "current",
Jaewoong Jung525443a2019-02-28 15:35:54 -0800985 }
986
987 override_android_app {
988 name: "bar",
989 base: "foo",
990 certificate: ":new_certificate",
991 }
992
993 android_app_certificate {
994 name: "new_certificate",
995 certificate: "cert/new_cert",
996 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -0700997
998 override_android_app {
999 name: "baz",
1000 base: "foo",
1001 package_name: "org.dandroid.bp",
1002 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08001003 `)
1004
1005 expectedVariants := []struct {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001006 moduleName string
Jaewoong Jung525443a2019-02-28 15:35:54 -08001007 variantName string
1008 apkName string
1009 apkPath string
1010 signFlag string
1011 overrides []string
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001012 aaptFlag string
Jaewoong Jung525443a2019-02-28 15:35:54 -08001013 }{
1014 {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001015 moduleName: "foo",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001016 variantName: "android_common",
1017 apkPath: "/target/product/test_device/system/app/foo/foo.apk",
Dan Willemsen412160e2019-04-09 21:36:26 -07001018 signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001019 overrides: []string{"qux"},
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001020 aaptFlag: "",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001021 },
1022 {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001023 moduleName: "bar",
1024 variantName: "android_common_bar",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001025 apkPath: "/target/product/test_device/system/app/bar/bar.apk",
1026 signFlag: "cert/new_cert.x509.pem cert/new_cert.pk8",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001027 overrides: []string{"qux", "foo"},
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001028 aaptFlag: "",
1029 },
1030 {
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001031 moduleName: "baz",
1032 variantName: "android_common_baz",
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001033 apkPath: "/target/product/test_device/system/app/baz/baz.apk",
Dan Willemsen412160e2019-04-09 21:36:26 -07001034 signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001035 overrides: []string{"qux", "foo"},
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001036 aaptFlag: "--rename-manifest-package org.dandroid.bp",
Jaewoong Jung525443a2019-02-28 15:35:54 -08001037 },
1038 }
1039 for _, expected := range expectedVariants {
1040 variant := ctx.ModuleForTests("foo", expected.variantName)
1041
1042 // Check the final apk name
1043 outputs := variant.AllOutputs()
1044 expectedApkPath := buildDir + expected.apkPath
1045 found := false
1046 for _, o := range outputs {
1047 if o == expectedApkPath {
1048 found = true
1049 break
1050 }
1051 }
1052 if !found {
1053 t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs)
1054 }
1055
1056 // Check the certificate paths
1057 signapk := variant.Output("foo.apk")
1058 signFlag := signapk.Args["certificates"]
1059 if expected.signFlag != signFlag {
1060 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected.signFlag, signFlag)
1061 }
1062
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001063 // Check if the overrides field values are correctly aggregated.
Jaewoong Jung525443a2019-02-28 15:35:54 -08001064 mod := variant.Module().(*AndroidApp)
1065 if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) {
1066 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
1067 expected.overrides, mod.appProperties.Overrides)
1068 }
Jaewoong Jung6f373f62019-03-13 10:13:24 -07001069
1070 // Check the package renaming flag, if exists.
1071 res := variant.Output("package-res.apk")
1072 aapt2Flags := res.Args["flags"]
1073 if !strings.Contains(aapt2Flags, expected.aaptFlag) {
1074 t.Errorf("package renaming flag, %q is missing in aapt2 link flags, %q", expected.aaptFlag, aapt2Flags)
1075 }
Jaewoong Jung525443a2019-02-28 15:35:54 -08001076 }
1077}
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001078
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001079func TestOverrideAndroidAppDependency(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001080 ctx, _ := testJava(t, `
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001081 android_app {
1082 name: "foo",
1083 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001084 sdk_version: "current",
Jaewoong Jungb639a6a2019-05-10 15:16:29 -07001085 }
1086
1087 override_android_app {
1088 name: "bar",
1089 base: "foo",
1090 package_name: "org.dandroid.bp",
1091 }
1092
1093 android_test {
1094 name: "baz",
1095 srcs: ["b.java"],
1096 instrumentation_for: "foo",
1097 }
1098
1099 android_test {
1100 name: "qux",
1101 srcs: ["b.java"],
1102 instrumentation_for: "bar",
1103 }
1104 `)
1105
1106 // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg.
1107 javac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
1108 fooTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
1109 if !strings.Contains(javac.Args["classpath"], fooTurbine) {
1110 t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine)
1111 }
1112
1113 // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg.
1114 javac = ctx.ModuleForTests("qux", "android_common").Rule("javac")
1115 barTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common_bar", "turbine-combined", "foo.jar")
1116 if !strings.Contains(javac.Args["classpath"], barTurbine) {
1117 t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
1118 }
1119}
1120
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001121func TestAndroidAppImport(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001122 ctx, _ := testJava(t, `
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001123 android_app_import {
1124 name: "foo",
1125 apk: "prebuilts/apk/app.apk",
1126 certificate: "platform",
1127 dex_preopt: {
1128 enabled: true,
1129 },
1130 }
1131 `)
1132
1133 variant := ctx.ModuleForTests("foo", "android_common")
1134
1135 // Check dexpreopt outputs.
1136 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
1137 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
1138 t.Errorf("can't find dexpreopt outputs")
1139 }
1140
1141 // Check cert signing flag.
1142 signedApk := variant.Output("signed/foo.apk")
1143 signingFlag := signedApk.Args["certificates"]
1144 expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8"
1145 if expected != signingFlag {
1146 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
1147 }
1148}
1149
1150func TestAndroidAppImport_NoDexPreopt(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001151 ctx, _ := testJava(t, `
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001152 android_app_import {
1153 name: "foo",
1154 apk: "prebuilts/apk/app.apk",
1155 certificate: "platform",
1156 dex_preopt: {
1157 enabled: false,
1158 },
1159 }
1160 `)
1161
1162 variant := ctx.ModuleForTests("foo", "android_common")
1163
1164 // Check dexpreopt outputs. They shouldn't exist.
1165 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule != nil ||
1166 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule != nil {
1167 t.Errorf("dexpreopt shouldn't have run.")
1168 }
1169}
1170
1171func TestAndroidAppImport_Presigned(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001172 ctx, _ := testJava(t, `
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001173 android_app_import {
1174 name: "foo",
1175 apk: "prebuilts/apk/app.apk",
1176 presigned: true,
1177 dex_preopt: {
1178 enabled: true,
1179 },
1180 }
1181 `)
1182
1183 variant := ctx.ModuleForTests("foo", "android_common")
1184
1185 // Check dexpreopt outputs.
1186 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
1187 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
1188 t.Errorf("can't find dexpreopt outputs")
1189 }
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +01001190 // Make sure signing was skipped and aligning was done.
Jaewoong Jungccbb3932019-04-15 09:48:31 -07001191 if variant.MaybeOutput("signed/foo.apk").Rule != nil {
1192 t.Errorf("signing rule shouldn't be included.")
1193 }
1194 if variant.MaybeOutput("zip-aligned/foo.apk").Rule == nil {
1195 t.Errorf("can't find aligning rule")
1196 }
1197}
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001198
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001199func TestAndroidAppImport_DefaultDevCert(t *testing.T) {
1200 ctx, _ := testJava(t, `
1201 android_app_import {
1202 name: "foo",
1203 apk: "prebuilts/apk/app.apk",
1204 default_dev_cert: true,
1205 dex_preopt: {
1206 enabled: true,
1207 },
1208 }
1209 `)
1210
1211 variant := ctx.ModuleForTests("foo", "android_common")
1212
1213 // Check dexpreopt outputs.
1214 if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil ||
1215 variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil {
1216 t.Errorf("can't find dexpreopt outputs")
1217 }
1218
1219 // Check cert signing flag.
1220 signedApk := variant.Output("signed/foo.apk")
1221 signingFlag := signedApk.Args["certificates"]
1222 expected := "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8"
1223 if expected != signingFlag {
1224 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
1225 }
1226}
1227
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001228func TestAndroidAppImport_DpiVariants(t *testing.T) {
1229 bp := `
1230 android_app_import {
1231 name: "foo",
1232 apk: "prebuilts/apk/app.apk",
1233 dpi_variants: {
1234 xhdpi: {
1235 apk: "prebuilts/apk/app_xhdpi.apk",
1236 },
1237 xxhdpi: {
1238 apk: "prebuilts/apk/app_xxhdpi.apk",
1239 },
1240 },
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001241 presigned: true,
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001242 dex_preopt: {
1243 enabled: true,
1244 },
1245 }
1246 `
1247 testCases := []struct {
1248 name string
1249 aaptPreferredConfig *string
1250 aaptPrebuiltDPI []string
1251 expected string
1252 }{
1253 {
1254 name: "no preferred",
1255 aaptPreferredConfig: nil,
1256 aaptPrebuiltDPI: []string{},
1257 expected: "prebuilts/apk/app.apk",
1258 },
1259 {
1260 name: "AAPTPreferredConfig matches",
1261 aaptPreferredConfig: proptools.StringPtr("xhdpi"),
Jaewoong Jung3e18b192019-06-11 12:25:34 -07001262 aaptPrebuiltDPI: []string{"xxhdpi", "ldpi"},
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001263 expected: "prebuilts/apk/app_xhdpi.apk",
1264 },
1265 {
1266 name: "AAPTPrebuiltDPI matches",
1267 aaptPreferredConfig: proptools.StringPtr("mdpi"),
1268 aaptPrebuiltDPI: []string{"xxhdpi", "xhdpi"},
1269 expected: "prebuilts/apk/app_xxhdpi.apk",
1270 },
1271 {
1272 name: "non-first AAPTPrebuiltDPI matches",
1273 aaptPreferredConfig: proptools.StringPtr("mdpi"),
1274 aaptPrebuiltDPI: []string{"ldpi", "xhdpi"},
1275 expected: "prebuilts/apk/app_xhdpi.apk",
1276 },
1277 {
1278 name: "no matches",
1279 aaptPreferredConfig: proptools.StringPtr("mdpi"),
1280 aaptPrebuiltDPI: []string{"ldpi", "xxxhdpi"},
1281 expected: "prebuilts/apk/app.apk",
1282 },
1283 }
1284
1285 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
1286 for _, test := range testCases {
1287 config := testConfig(nil)
1288 config.TestProductVariables.AAPTPreferredConfig = test.aaptPreferredConfig
1289 config.TestProductVariables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001290 ctx := testAppContext(bp, nil)
Jaewoong Junga5e5abc2019-04-26 14:31:50 -07001291
1292 run(t, ctx, config)
1293
1294 variant := ctx.ModuleForTests("foo", "android_common")
1295 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
1296 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
1297 if len(matches) != 2 {
1298 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
1299 }
1300 if test.expected != matches[1] {
1301 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
1302 }
1303 }
1304}
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001305
Jaewoong Jung8aae22e2019-07-17 10:21:49 -07001306func TestAndroidAppImport_Filename(t *testing.T) {
1307 ctx, config := testJava(t, `
1308 android_app_import {
1309 name: "foo",
1310 apk: "prebuilts/apk/app.apk",
1311 presigned: true,
1312 }
1313
1314 android_app_import {
1315 name: "bar",
1316 apk: "prebuilts/apk/app.apk",
1317 presigned: true,
1318 filename: "bar_sample.apk"
1319 }
1320 `)
1321
1322 testCases := []struct {
1323 name string
1324 expected string
1325 }{
1326 {
1327 name: "foo",
1328 expected: "foo.apk",
1329 },
1330 {
1331 name: "bar",
1332 expected: "bar_sample.apk",
1333 },
1334 }
1335
1336 for _, test := range testCases {
1337 variant := ctx.ModuleForTests(test.name, "android_common")
1338 if variant.MaybeOutput(test.expected).Rule == nil {
1339 t.Errorf("can't find output named %q - all outputs: %v", test.expected, variant.AllOutputs())
1340 }
1341
1342 a := variant.Module().(*AndroidAppImport)
1343 expectedValues := []string{test.expected}
1344 actualValues := android.AndroidMkEntriesForTest(
1345 t, config, "", a).EntryMap["LOCAL_INSTALLED_MODULE_STEM"]
1346 if !reflect.DeepEqual(actualValues, expectedValues) {
1347 t.Errorf("Incorrect LOCAL_INSTALLED_MODULE_STEM value '%s', expected '%s'",
1348 actualValues, expectedValues)
1349 }
1350 }
1351}
1352
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001353func TestAndroidAppImport_ArchVariants(t *testing.T) {
1354 // The test config's target arch is ARM64.
1355 testCases := []struct {
1356 name string
1357 bp string
1358 expected string
1359 }{
1360 {
1361 name: "matching arch",
1362 bp: `
1363 android_app_import {
1364 name: "foo",
1365 apk: "prebuilts/apk/app.apk",
1366 arch: {
1367 arm64: {
1368 apk: "prebuilts/apk/app_arm64.apk",
1369 },
1370 },
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001371 presigned: true,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001372 dex_preopt: {
1373 enabled: true,
1374 },
1375 }
1376 `,
1377 expected: "prebuilts/apk/app_arm64.apk",
1378 },
1379 {
1380 name: "no matching arch",
1381 bp: `
1382 android_app_import {
1383 name: "foo",
1384 apk: "prebuilts/apk/app.apk",
1385 arch: {
1386 arm: {
1387 apk: "prebuilts/apk/app_arm.apk",
1388 },
1389 },
Jaewoong Jung961d4fd2019-08-22 14:25:58 -07001390 presigned: true,
Jaewoong Jung1ce9ac62019-08-13 14:11:33 -07001391 dex_preopt: {
1392 enabled: true,
1393 },
1394 }
1395 `,
1396 expected: "prebuilts/apk/app.apk",
1397 },
1398 }
1399
1400 jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)")
1401 for _, test := range testCases {
1402 ctx, _ := testJava(t, test.bp)
1403
1404 variant := ctx.ModuleForTests("foo", "android_common")
1405 jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command
1406 matches := jniRuleRe.FindStringSubmatch(jniRuleCommand)
1407 if len(matches) != 2 {
1408 t.Errorf("failed to extract the src apk path from %q", jniRuleCommand)
1409 }
1410 if test.expected != matches[1] {
1411 t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1])
1412 }
1413 }
1414}
1415
Jaewoong Jungb28eb5f2019-08-27 15:01:50 -07001416func TestAndroidTestImport(t *testing.T) {
1417 ctx, config := testJava(t, `
1418 android_test_import {
1419 name: "foo",
1420 apk: "prebuilts/apk/app.apk",
1421 presigned: true,
1422 data: [
1423 "testdata/data",
1424 ],
1425 }
1426 `)
1427
1428 test := ctx.ModuleForTests("foo", "android_common").Module().(*AndroidTestImport)
1429
1430 // Check android mks.
1431 entries := android.AndroidMkEntriesForTest(t, config, "", test)
1432 expected := []string{"tests"}
1433 actual := entries.EntryMap["LOCAL_MODULE_TAGS"]
1434 if !reflect.DeepEqual(expected, actual) {
1435 t.Errorf("Unexpected module tags - expected: %q, actual: %q", expected, actual)
1436 }
1437 expected = []string{"testdata/data:testdata/data"}
1438 actual = entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"]
1439 if !reflect.DeepEqual(expected, actual) {
1440 t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual)
1441 }
1442}
1443
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001444func TestStl(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001445 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001446 cc_library {
1447 name: "libjni",
1448 }
1449
1450 android_test {
1451 name: "stl",
1452 jni_libs: ["libjni"],
1453 compile_multilib: "both",
1454 sdk_version: "current",
1455 stl: "c++_shared",
1456 }
1457
1458 android_test {
1459 name: "system",
1460 jni_libs: ["libjni"],
1461 compile_multilib: "both",
1462 sdk_version: "current",
1463 }
Jaewoong Jung710756a2019-06-04 11:53:47 -07001464
1465 ndk_prebuilt_shared_stl {
1466 name: "ndk_libc++_shared",
1467 }
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001468 `)
1469
1470 testCases := []struct {
1471 name string
1472 jnis []string
1473 }{
1474 {"stl",
1475 []string{
1476 "libjni.so",
Jaewoong Jung710756a2019-06-04 11:53:47 -07001477 "libc++_shared.so",
Jaewoong Jungbc625cd2019-05-06 15:48:44 -07001478 },
1479 },
1480 {"system",
1481 []string{
1482 "libjni.so",
1483 },
1484 },
1485 }
1486
1487 for _, test := range testCases {
1488 t.Run(test.name, func(t *testing.T) {
1489 app := ctx.ModuleForTests(test.name, "android_common")
1490 jniLibZip := app.Output("jnilibs.zip")
1491 var jnis []string
1492 args := strings.Fields(jniLibZip.Args["jarArgs"])
1493 for i := 0; i < len(args); i++ {
1494 if args[i] == "-f" {
1495 jnis = append(jnis, args[i+1])
1496 i += 1
1497 }
1498 }
1499 jnisJoined := strings.Join(jnis, " ")
1500 for _, jni := range test.jnis {
1501 if !strings.Contains(jnisJoined, jni) {
1502 t.Errorf("missing jni %q in %q", jni, jnis)
1503 }
1504 }
1505 })
1506 }
1507}
Colin Cross50ddcc42019-05-16 12:28:22 -07001508
1509func TestUsesLibraries(t *testing.T) {
1510 bp := `
1511 java_sdk_library {
1512 name: "foo",
1513 srcs: ["a.java"],
1514 api_packages: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001515 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07001516 }
1517
1518 java_sdk_library {
1519 name: "bar",
1520 srcs: ["a.java"],
1521 api_packages: ["bar"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001522 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07001523 }
1524
1525 android_app {
1526 name: "app",
1527 srcs: ["a.java"],
1528 uses_libs: ["foo"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001529 sdk_version: "current",
Colin Cross50ddcc42019-05-16 12:28:22 -07001530 optional_uses_libs: [
1531 "bar",
1532 "baz",
1533 ],
1534 }
1535
1536 android_app_import {
1537 name: "prebuilt",
1538 apk: "prebuilts/apk/app.apk",
1539 certificate: "platform",
1540 uses_libs: ["foo"],
1541 optional_uses_libs: [
1542 "bar",
1543 "baz",
1544 ],
1545 }
1546 `
1547
1548 config := testConfig(nil)
1549 config.TestProductVariables.MissingUsesLibraries = []string{"baz"}
1550
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001551 ctx := testAppContext(bp, nil)
Colin Cross50ddcc42019-05-16 12:28:22 -07001552
1553 run(t, ctx, config)
1554
1555 app := ctx.ModuleForTests("app", "android_common")
1556 prebuilt := ctx.ModuleForTests("prebuilt", "android_common")
1557
1558 // Test that all libraries are verified
1559 cmd := app.Rule("verify_uses_libraries").RuleParams.Command
1560 if w := "--uses-library foo"; !strings.Contains(cmd, w) {
1561 t.Errorf("wanted %q in %q", w, cmd)
1562 }
1563
1564 if w := "--optional-uses-library bar --optional-uses-library baz"; !strings.Contains(cmd, w) {
1565 t.Errorf("wanted %q in %q", w, cmd)
1566 }
1567
1568 cmd = prebuilt.Rule("verify_uses_libraries").RuleParams.Command
1569
1570 if w := `uses_library_names="foo"`; !strings.Contains(cmd, w) {
1571 t.Errorf("wanted %q in %q", w, cmd)
1572 }
1573
1574 if w := `optional_uses_library_names="bar baz"`; !strings.Contains(cmd, w) {
1575 t.Errorf("wanted %q in %q", w, cmd)
1576 }
1577
1578 // Test that only present libraries are preopted
1579 cmd = app.Rule("dexpreopt").RuleParams.Command
1580
1581 if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) {
1582 t.Errorf("wanted %q in %q", w, cmd)
1583 }
1584
1585 cmd = prebuilt.Rule("dexpreopt").RuleParams.Command
1586
1587 if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) {
1588 t.Errorf("wanted %q in %q", w, cmd)
1589 }
1590}
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001591
1592func TestCodelessApp(t *testing.T) {
1593 testCases := []struct {
1594 name string
1595 bp string
1596 noCode bool
1597 }{
1598 {
1599 name: "normal",
1600 bp: `
1601 android_app {
1602 name: "foo",
1603 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001604 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001605 }
1606 `,
1607 noCode: false,
1608 },
1609 {
1610 name: "app without sources",
1611 bp: `
1612 android_app {
1613 name: "foo",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001614 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001615 }
1616 `,
1617 noCode: true,
1618 },
1619 {
1620 name: "app with libraries",
1621 bp: `
1622 android_app {
1623 name: "foo",
1624 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001625 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001626 }
1627
1628 java_library {
1629 name: "lib",
1630 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001631 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001632 }
1633 `,
1634 noCode: false,
1635 },
1636 {
1637 name: "app with sourceless libraries",
1638 bp: `
1639 android_app {
1640 name: "foo",
1641 static_libs: ["lib"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001642 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001643 }
1644
1645 java_library {
1646 name: "lib",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001647 sdk_version: "current",
Jaewoong Jungc27ab662019-05-30 15:51:14 -07001648 }
1649 `,
1650 // TODO(jungjw): this should probably be true
1651 noCode: false,
1652 },
1653 }
1654
1655 for _, test := range testCases {
1656 t.Run(test.name, func(t *testing.T) {
1657 ctx := testApp(t, test.bp)
1658
1659 foo := ctx.ModuleForTests("foo", "android_common")
1660 manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"]
1661 if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode {
1662 t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs)
1663 }
1664 })
1665 }
1666}
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001667
1668func TestEmbedNotice(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001669 ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+`
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001670 android_app {
1671 name: "foo",
1672 srcs: ["a.java"],
1673 static_libs: ["javalib"],
1674 jni_libs: ["libjni"],
1675 notice: "APP_NOTICE",
1676 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001677 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001678 }
1679
1680 // No embed_notice flag
1681 android_app {
1682 name: "bar",
1683 srcs: ["a.java"],
1684 jni_libs: ["libjni"],
1685 notice: "APP_NOTICE",
Jeongik Cha538c0d02019-07-11 15:54:27 +09001686 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001687 }
1688
1689 // No NOTICE files
1690 android_app {
1691 name: "baz",
1692 srcs: ["a.java"],
1693 embed_notices: true,
Jeongik Cha538c0d02019-07-11 15:54:27 +09001694 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001695 }
1696
1697 cc_library {
1698 name: "libjni",
1699 system_shared_libs: [],
1700 stl: "none",
1701 notice: "LIB_NOTICE",
1702 }
1703
1704 java_library {
1705 name: "javalib",
1706 srcs: [
1707 ":gen",
1708 ],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001709 sdk_version: "current",
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001710 }
1711
1712 genrule {
1713 name: "gen",
1714 tools: ["gentool"],
1715 out: ["gen.java"],
1716 notice: "GENRULE_NOTICE",
1717 }
1718
1719 java_binary_host {
1720 name: "gentool",
1721 srcs: ["b.java"],
1722 notice: "TOOL_NOTICE",
1723 }
1724 `)
1725
1726 // foo has NOTICE files to process, and embed_notices is true.
1727 foo := ctx.ModuleForTests("foo", "android_common")
1728 // verify merge notices rule.
1729 mergeNotices := foo.Rule("mergeNoticesRule")
1730 noticeInputs := mergeNotices.Inputs.Strings()
1731 // TOOL_NOTICE should be excluded as it's a host module.
1732 if len(mergeNotices.Inputs) != 3 {
1733 t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs)
1734 }
1735 if !inList("APP_NOTICE", noticeInputs) {
1736 t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs)
1737 }
1738 if !inList("LIB_NOTICE", noticeInputs) {
1739 t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs)
1740 }
1741 if !inList("GENRULE_NOTICE", noticeInputs) {
1742 t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs)
1743 }
1744 // aapt2 flags should include -A <NOTICE dir> so that its contents are put in the APK's /assets.
1745 res := foo.Output("package-res.apk")
1746 aapt2Flags := res.Args["flags"]
1747 e := "-A " + buildDir + "/.intermediates/foo/android_common/NOTICE"
1748 if !strings.Contains(aapt2Flags, e) {
1749 t.Errorf("asset dir flag for NOTICE, %q is missing in aapt2 link flags, %q", e, aapt2Flags)
1750 }
1751
1752 // bar has NOTICE files to process, but embed_notices is not set.
1753 bar := ctx.ModuleForTests("bar", "android_common")
Jaewoong Jung98772792019-07-01 17:15:13 -07001754 res = bar.Output("package-res.apk")
1755 aapt2Flags = res.Args["flags"]
1756 e = "-A " + buildDir + "/.intermediates/bar/android_common/NOTICE"
1757 if strings.Contains(aapt2Flags, e) {
1758 t.Errorf("bar shouldn't have the asset dir flag for NOTICE: %q", e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001759 }
1760
1761 // baz's embed_notice is true, but it doesn't have any NOTICE files.
1762 baz := ctx.ModuleForTests("baz", "android_common")
Jaewoong Jung98772792019-07-01 17:15:13 -07001763 res = baz.Output("package-res.apk")
1764 aapt2Flags = res.Args["flags"]
1765 e = "-A " + buildDir + "/.intermediates/baz/android_common/NOTICE"
1766 if strings.Contains(aapt2Flags, e) {
1767 t.Errorf("baz shouldn't have the asset dir flag for NOTICE: %q", e)
Jaewoong Jung5b425e22019-06-17 17:40:56 -07001768 }
1769}
Colin Cross53a87f52019-06-25 13:35:30 -07001770
1771func TestUncompressDex(t *testing.T) {
1772 testCases := []struct {
1773 name string
1774 bp string
1775
1776 uncompressedPlatform bool
1777 uncompressedUnbundled bool
1778 }{
1779 {
1780 name: "normal",
1781 bp: `
1782 android_app {
1783 name: "foo",
1784 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001785 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07001786 }
1787 `,
1788 uncompressedPlatform: true,
1789 uncompressedUnbundled: false,
1790 },
1791 {
1792 name: "use_embedded_dex",
1793 bp: `
1794 android_app {
1795 name: "foo",
1796 use_embedded_dex: true,
1797 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001798 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07001799 }
1800 `,
1801 uncompressedPlatform: true,
1802 uncompressedUnbundled: true,
1803 },
1804 {
1805 name: "privileged",
1806 bp: `
1807 android_app {
1808 name: "foo",
1809 privileged: true,
1810 srcs: ["a.java"],
Jeongik Cha538c0d02019-07-11 15:54:27 +09001811 sdk_version: "current",
Colin Cross53a87f52019-06-25 13:35:30 -07001812 }
1813 `,
1814 uncompressedPlatform: true,
1815 uncompressedUnbundled: true,
1816 },
1817 }
1818
1819 test := func(t *testing.T, bp string, want bool, unbundled bool) {
1820 t.Helper()
1821
1822 config := testConfig(nil)
1823 if unbundled {
1824 config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true)
1825 }
1826
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001827 ctx := testAppContext(bp, nil)
Colin Cross53a87f52019-06-25 13:35:30 -07001828
1829 run(t, ctx, config)
1830
1831 foo := ctx.ModuleForTests("foo", "android_common")
1832 dex := foo.Rule("r8")
1833 uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0")
1834 aligned := foo.MaybeRule("zipalign").Rule != nil
1835
1836 if uncompressedInDexJar != want {
1837 t.Errorf("want uncompressed in dex %v, got %v", want, uncompressedInDexJar)
1838 }
1839
1840 if aligned != want {
1841 t.Errorf("want aligned %v, got %v", want, aligned)
1842 }
1843 }
1844
1845 for _, tt := range testCases {
1846 t.Run(tt.name, func(t *testing.T) {
1847 t.Run("platform", func(t *testing.T) {
1848 test(t, tt.bp, tt.uncompressedPlatform, false)
1849 })
1850 t.Run("unbundled", func(t *testing.T) {
1851 test(t, tt.bp, tt.uncompressedUnbundled, true)
1852 })
1853 })
1854 }
1855}