blob: 00ba5ba11f1bf5759eddfe9aa88f5bf4ad55d48d [file] [log] [blame]
Jaewoong Jungf9b44652020-12-21 12:29:12 -08001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
18 "reflect"
19 "strings"
20 "testing"
21
22 "android/soong/android"
Lukacs T. Berki7690c092021-02-26 14:27:36 +010023 "android/soong/shared"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080024)
25
26func TestRuntimeResourceOverlay(t *testing.T) {
Paul Duffin0342dc92021-03-22 17:31:52 +000027 fs := android.MockFS{
Jaewoong Jungf9b44652020-12-21 12:29:12 -080028 "baz/res/res/values/strings.xml": nil,
29 "bar/res/res/values/strings.xml": nil,
30 }
31 bp := `
32 runtime_resource_overlay {
33 name: "foo",
34 certificate: "platform",
35 lineage: "lineage.bin",
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040036 rotationMinSdkVersion: "32",
Jaewoong Jungf9b44652020-12-21 12:29:12 -080037 product_specific: true,
38 static_libs: ["bar"],
39 resource_libs: ["baz"],
40 aaptflags: ["--keep-raw-values"],
41 }
42
43 runtime_resource_overlay {
44 name: "foo_themed",
45 certificate: "platform",
46 product_specific: true,
47 theme: "faza",
48 overrides: ["foo"],
49 }
50
51 android_library {
52 name: "bar",
53 resource_dirs: ["bar/res"],
54 }
55
56 android_app {
57 name: "baz",
58 sdk_version: "current",
59 resource_dirs: ["baz/res"],
60 }
Paul Duffin0342dc92021-03-22 17:31:52 +000061 `
Jaewoong Jungf9b44652020-12-21 12:29:12 -080062
Paul Duffin0342dc92021-03-22 17:31:52 +000063 result := android.GroupFixturePreparers(
64 PrepareForTestWithJavaDefaultModules,
65 PrepareForTestWithOverlayBuildComponents,
Colin Crossc68db4b2021-11-11 18:59:15 -080066 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
Paul Duffin0342dc92021-03-22 17:31:52 +000067 fs.AddToFixture(),
68 ).RunTestWithBp(t, bp)
69
70 m := result.ModuleForTests("foo", "android_common")
Jaewoong Jungf9b44652020-12-21 12:29:12 -080071
72 // Check AAPT2 link flags.
Paul Duffina71a67a2021-03-29 00:42:57 +010073 aapt2Flags := m.Output("package-res.apk").Args["flags"]
Jaewoong Jungf9b44652020-12-21 12:29:12 -080074 expectedFlags := []string{"--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"}
75 absentFlags := android.RemoveListFromList(expectedFlags, strings.Split(aapt2Flags, " "))
76 if len(absentFlags) > 0 {
77 t.Errorf("expected values, %q are missing in aapt2 link flags, %q", absentFlags, aapt2Flags)
78 }
79
80 // Check overlay.list output for static_libs dependency.
Paul Duffin0342dc92021-03-22 17:31:52 +000081 overlayList := android.PathsRelativeToTop(m.Output("aapt2/overlay.list").Inputs)
82 staticLibPackage := "out/soong/.intermediates/bar/android_common/package-res.apk"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080083 if !inList(staticLibPackage, overlayList) {
84 t.Errorf("Stactic lib res package %q missing in overlay list: %q", staticLibPackage, overlayList)
85 }
86
87 // Check AAPT2 link flags for resource_libs dependency.
Paul Duffin0342dc92021-03-22 17:31:52 +000088 resourceLibFlag := "-I " + "out/soong/.intermediates/baz/android_common/package-res.apk"
Jaewoong Jungf9b44652020-12-21 12:29:12 -080089 if !strings.Contains(aapt2Flags, resourceLibFlag) {
90 t.Errorf("Resource lib flag %q missing in aapt2 link flags: %q", resourceLibFlag, aapt2Flags)
91 }
92
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040093 // Check cert signing flags.
Jaewoong Jungf9b44652020-12-21 12:29:12 -080094 signedApk := m.Output("signed/foo.apk")
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -040095 actualCertSigningFlags := signedApk.Args["flags"]
96 expectedCertSigningFlags := "--lineage lineage.bin --rotation-min-sdk-version 32"
97 if expectedCertSigningFlags != actualCertSigningFlags {
98 t.Errorf("Incorrect cert signing flags, expected: %q, got: %q", expectedCertSigningFlags, actualCertSigningFlags)
Jaewoong Jungf9b44652020-12-21 12:29:12 -080099 }
Rupert Shuttleworth8eab8692021-11-03 10:39:39 -0400100
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800101 signingFlag := signedApk.Args["certificates"]
102 expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8"
103 if expected != signingFlag {
104 t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
105 }
Paul Duffin0342dc92021-03-22 17:31:52 +0000106 androidMkEntries := android.AndroidMkEntriesForTest(t, result.TestContext, m.Module())[0]
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800107 path := androidMkEntries.EntryMap["LOCAL_CERTIFICATE"]
108 expectedPath := []string{"build/make/target/product/security/platform.x509.pem"}
109 if !reflect.DeepEqual(path, expectedPath) {
110 t.Errorf("Unexpected LOCAL_CERTIFICATE value: %v, expected: %v", path, expectedPath)
111 }
112
113 // Check device location.
114 path = androidMkEntries.EntryMap["LOCAL_MODULE_PATH"]
Paul Duffin0342dc92021-03-22 17:31:52 +0000115 expectedPath = []string{shared.JoinPath("out/target/product/test_device/product/overlay")}
116 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_MODULE_PATH", result.Config, expectedPath, path)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800117
118 // A themed module has a different device location
Paul Duffin0342dc92021-03-22 17:31:52 +0000119 m = result.ModuleForTests("foo_themed", "android_common")
120 androidMkEntries = android.AndroidMkEntriesForTest(t, result.TestContext, m.Module())[0]
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800121 path = androidMkEntries.EntryMap["LOCAL_MODULE_PATH"]
Paul Duffin0342dc92021-03-22 17:31:52 +0000122 expectedPath = []string{shared.JoinPath("out/target/product/test_device/product/overlay/faza")}
123 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_MODULE_PATH", result.Config, expectedPath, path)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800124
125 overrides := androidMkEntries.EntryMap["LOCAL_OVERRIDES_PACKAGES"]
126 expectedOverrides := []string{"foo"}
127 if !reflect.DeepEqual(overrides, expectedOverrides) {
128 t.Errorf("Unexpected LOCAL_OVERRIDES_PACKAGES value: %v, expected: %v", overrides, expectedOverrides)
129 }
130}
131
132func TestRuntimeResourceOverlay_JavaDefaults(t *testing.T) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800133 result := android.GroupFixturePreparers(
134 PrepareForTestWithJavaDefaultModules,
135 android.FixtureModifyConfig(android.SetKatiEnabledForTests),
136 ).RunTestWithBp(t, `
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800137 java_defaults {
138 name: "rro_defaults",
139 theme: "default_theme",
140 product_specific: true,
141 aaptflags: ["--keep-raw-values"],
142 }
143
144 runtime_resource_overlay {
145 name: "foo_with_defaults",
146 defaults: ["rro_defaults"],
147 }
148
149 runtime_resource_overlay {
150 name: "foo_barebones",
151 }
152 `)
153
154 //
155 // RRO module with defaults
156 //
Colin Crossc68db4b2021-11-11 18:59:15 -0800157 m := result.ModuleForTests("foo_with_defaults", "android_common")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800158
159 // Check AAPT2 link flags.
160 aapt2Flags := strings.Split(m.Output("package-res.apk").Args["flags"], " ")
161 expectedFlags := []string{"--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"}
162 absentFlags := android.RemoveListFromList(expectedFlags, aapt2Flags)
163 if len(absentFlags) > 0 {
164 t.Errorf("expected values, %q are missing in aapt2 link flags, %q", absentFlags, aapt2Flags)
165 }
166
167 // Check device location.
Colin Crossc68db4b2021-11-11 18:59:15 -0800168 path := android.AndroidMkEntriesForTest(t, result.TestContext, m.Module())[0].EntryMap["LOCAL_MODULE_PATH"]
Paul Duffin0342dc92021-03-22 17:31:52 +0000169 expectedPath := []string{shared.JoinPath("out/target/product/test_device/product/overlay/default_theme")}
Colin Crossc68db4b2021-11-11 18:59:15 -0800170 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_MODULE_PATH", result.Config, expectedPath, path)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800171
172 //
173 // RRO module without defaults
174 //
Colin Crossc68db4b2021-11-11 18:59:15 -0800175 m = result.ModuleForTests("foo_barebones", "android_common")
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800176
177 // Check AAPT2 link flags.
178 aapt2Flags = strings.Split(m.Output("package-res.apk").Args["flags"], " ")
179 unexpectedFlags := "--keep-raw-values"
180 if inList(unexpectedFlags, aapt2Flags) {
181 t.Errorf("unexpected value, %q is present in aapt2 link flags, %q", unexpectedFlags, aapt2Flags)
182 }
183
184 // Check device location.
Colin Crossc68db4b2021-11-11 18:59:15 -0800185 path = android.AndroidMkEntriesForTest(t, result.TestContext, m.Module())[0].EntryMap["LOCAL_MODULE_PATH"]
Spandan Das5d1b9292021-06-03 19:36:41 +0000186 expectedPath = []string{shared.JoinPath("out/target/product/test_device/product/overlay")}
Colin Crossc68db4b2021-11-11 18:59:15 -0800187 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_MODULE_PATH", result.Config, expectedPath, path)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800188}
189
190func TestOverrideRuntimeResourceOverlay(t *testing.T) {
191 ctx, _ := testJava(t, `
192 runtime_resource_overlay {
193 name: "foo_overlay",
194 certificate: "platform",
195 product_specific: true,
196 sdk_version: "current",
197 }
198
199 override_runtime_resource_overlay {
200 name: "bar_overlay",
201 base: "foo_overlay",
202 package_name: "com.android.bar.overlay",
203 target_package_name: "com.android.bar",
204 }
205 `)
206
207 expectedVariants := []struct {
208 moduleName string
209 variantName string
210 apkPath string
211 overrides []string
212 targetVariant string
213 packageFlag string
214 targetPackageFlag string
215 }{
216 {
217 variantName: "android_common",
Paul Duffin0342dc92021-03-22 17:31:52 +0000218 apkPath: "out/soong/target/product/test_device/product/overlay/foo_overlay.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800219 overrides: nil,
220 targetVariant: "android_common",
221 packageFlag: "",
222 targetPackageFlag: "",
223 },
224 {
225 variantName: "android_common_bar_overlay",
Paul Duffin0342dc92021-03-22 17:31:52 +0000226 apkPath: "out/soong/target/product/test_device/product/overlay/bar_overlay.apk",
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800227 overrides: []string{"foo_overlay"},
228 targetVariant: "android_common_bar",
229 packageFlag: "com.android.bar.overlay",
230 targetPackageFlag: "com.android.bar",
231 },
232 }
233 for _, expected := range expectedVariants {
234 variant := ctx.ModuleForTests("foo_overlay", expected.variantName)
235
236 // Check the final apk name
Paul Duffin0342dc92021-03-22 17:31:52 +0000237 variant.Output(expected.apkPath)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800238
239 // Check if the overrides field values are correctly aggregated.
240 mod := variant.Module().(*RuntimeResourceOverlay)
241 if !reflect.DeepEqual(expected.overrides, mod.properties.Overrides) {
242 t.Errorf("Incorrect overrides property value, expected: %q, got: %q",
243 expected.overrides, mod.properties.Overrides)
244 }
245
246 // Check aapt2 flags.
247 res := variant.Output("package-res.apk")
248 aapt2Flags := res.Args["flags"]
249 checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag)
250 checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", "")
251 checkAapt2LinkFlag(t, aapt2Flags, "rename-overlay-target-package", expected.targetPackageFlag)
252 }
253}
254
255func TestEnforceRRO_propagatesToDependencies(t *testing.T) {
256 testCases := []struct {
Jeongik Chacee5ba92021-02-19 12:11:51 +0900257 name string
258 enforceRROTargets []string
259 rroDirs map[string][]string
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800260 }{
261 {
Jeongik Chacee5ba92021-02-19 12:11:51 +0900262 name: "no RRO",
263 enforceRROTargets: nil,
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800264 rroDirs: map[string][]string{
265 "foo": nil,
266 "bar": nil,
267 },
268 },
269 {
Jeongik Chacee5ba92021-02-19 12:11:51 +0900270 name: "enforce RRO on all",
271 enforceRROTargets: []string{"*"},
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800272 rroDirs: map[string][]string{
273 "foo": {"product/vendor/blah/overlay/lib2/res"},
274 "bar": {"product/vendor/blah/overlay/lib2/res"},
275 },
276 },
277 {
Jeongik Chacee5ba92021-02-19 12:11:51 +0900278 name: "enforce RRO on foo",
279 enforceRROTargets: []string{"foo"},
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800280 rroDirs: map[string][]string{
281 "foo": {"product/vendor/blah/overlay/lib2/res"},
282 "bar": {"product/vendor/blah/overlay/lib2/res"},
283 },
284 },
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800285 }
286
287 productResourceOverlays := []string{
288 "product/vendor/blah/overlay",
289 }
290
Paul Duffin0342dc92021-03-22 17:31:52 +0000291 fs := android.MockFS{
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800292 "lib2/res/values/strings.xml": nil,
293 "product/vendor/blah/overlay/lib2/res/values/strings.xml": nil,
294 }
295
296 bp := `
297 android_app {
298 name: "foo",
299 sdk_version: "current",
300 resource_dirs: [],
301 static_libs: ["lib"],
302 }
303
304 android_app {
305 name: "bar",
306 sdk_version: "current",
307 resource_dirs: [],
308 static_libs: ["lib"],
309 }
310
311 android_library {
312 name: "lib",
313 sdk_version: "current",
314 resource_dirs: [],
315 static_libs: ["lib2"],
316 }
317
318 android_library {
319 name: "lib2",
320 sdk_version: "current",
321 resource_dirs: ["lib2/res"],
322 }
323 `
324
325 for _, testCase := range testCases {
326 t.Run(testCase.name, func(t *testing.T) {
Paul Duffin0342dc92021-03-22 17:31:52 +0000327 result := android.GroupFixturePreparers(
328 PrepareForTestWithJavaDefaultModules,
329 PrepareForTestWithOverlayBuildComponents,
330 fs.AddToFixture(),
331 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
332 variables.ProductResourceOverlays = productResourceOverlays
333 if testCase.enforceRROTargets != nil {
334 variables.EnforceRROTargets = testCase.enforceRROTargets
335 }
336 }),
337 ).RunTestWithBp(t, bp)
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800338
339 modules := []string{"foo", "bar"}
340 for _, moduleName := range modules {
Paul Duffin0342dc92021-03-22 17:31:52 +0000341 module := result.ModuleForTests(moduleName, "android_common")
342 mkEntries := android.AndroidMkEntriesForTest(t, result.TestContext, module.Module())[0]
Jaewoong Jungf9b44652020-12-21 12:29:12 -0800343 actualRRODirs := mkEntries.EntryMap["LOCAL_SOONG_PRODUCT_RRO_DIRS"]
344 if !reflect.DeepEqual(actualRRODirs, testCase.rroDirs[moduleName]) {
345 t.Errorf("exected %s LOCAL_SOONG_PRODUCT_RRO_DIRS entry: %v\ngot:%q",
346 moduleName, testCase.rroDirs[moduleName], actualRRODirs)
347 }
348 }
349 })
350 }
351}
Spandan Das5d1b9292021-06-03 19:36:41 +0000352
353func TestRuntimeResourceOverlayPartition(t *testing.T) {
354 bp := `
355 runtime_resource_overlay {
356 name: "device_specific",
357 device_specific: true,
358 }
359 runtime_resource_overlay {
360 name: "soc_specific",
361 soc_specific: true,
362 }
363 runtime_resource_overlay {
364 name: "system_ext_specific",
365 system_ext_specific: true,
366 }
367 runtime_resource_overlay {
368 name: "product_specific",
369 product_specific: true,
370 }
371 runtime_resource_overlay {
372 name: "default"
373 }
374 `
375 testCases := []struct {
376 name string
377 expectedPath string
378 }{
379 {
380 name: "device_specific",
381 expectedPath: "out/soong/target/product/test_device/odm/overlay",
382 },
383 {
384 name: "soc_specific",
385 expectedPath: "out/soong/target/product/test_device/vendor/overlay",
386 },
387 {
388 name: "system_ext_specific",
389 expectedPath: "out/soong/target/product/test_device/system_ext/overlay",
390 },
391 {
392 name: "product_specific",
393 expectedPath: "out/soong/target/product/test_device/product/overlay",
394 },
395 {
396 name: "default",
397 expectedPath: "out/soong/target/product/test_device/product/overlay",
398 },
399 }
400 for _, testCase := range testCases {
401 ctx, _ := testJava(t, bp)
402 mod := ctx.ModuleForTests(testCase.name, "android_common").Module().(*RuntimeResourceOverlay)
403 android.AssertPathRelativeToTopEquals(t, "Install dir is not correct for "+testCase.name, testCase.expectedPath, mod.installDir)
404 }
405}