Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 18 | "fmt" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 19 | "path/filepath" |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 20 | "reflect" |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 21 | "regexp" |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 22 | "sort" |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 23 | "strings" |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 24 | "testing" |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint/proptools" |
| 27 | |
| 28 | "android/soong/android" |
| 29 | "android/soong/cc" |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | var ( |
| 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 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 46 | func testAppConfig(env map[string]string, bp string, fs map[string][]byte) android.Config { |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 47 | appFS := map[string][]byte{} |
| 48 | for k, v := range fs { |
| 49 | appFS[k] = v |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 52 | for _, file := range resourceFiles { |
| 53 | appFS[file] = nil |
| 54 | } |
| 55 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 56 | return testConfig(env, bp, appFS) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | func testApp(t *testing.T, bp string) *android.TestContext { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 60 | config := testAppConfig(nil, bp, nil) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 61 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 62 | ctx := testContext() |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 63 | |
| 64 | run(t, ctx, config) |
| 65 | |
| 66 | return ctx |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func TestApp(t *testing.T) { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 70 | 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 Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 75 | sdk_version: "current" |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 76 | } |
| 77 | `) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 78 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 79 | foo := ctx.ModuleForTests("foo", "android_common") |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 80 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 81 | var expectedLinkImplicits []string |
| 82 | |
| 83 | manifestFixer := foo.Output("manifest_fixer/AndroidManifest.xml") |
| 84 | expectedLinkImplicits = append(expectedLinkImplicits, manifestFixer.Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 85 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 86 | frameworkRes := ctx.ModuleForTests("framework-res", "android_common") |
| 87 | expectedLinkImplicits = append(expectedLinkImplicits, |
| 88 | frameworkRes.Output("package-res.apk").Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 89 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 90 | // 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 Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 96 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 97 | compiledResourceOutputs := compile.Outputs.Strings() |
| 98 | sort.Strings(compiledResourceOutputs) |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 99 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 100 | expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 101 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 102 | list := foo.Output("aapt2/res.list") |
| 103 | expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 104 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 105 | // 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 Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 114 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 115 | func 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 Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 121 | sdk_version: "current" |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 122 | }`) |
| 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 Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 135 | 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 Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 144 | func 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 | |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 178 | func TestAndroidAppLinkType(t *testing.T) { |
| 179 | testJava(t, ` |
| 180 | android_app { |
| 181 | name: "foo", |
| 182 | srcs: ["a.java"], |
| 183 | libs: ["bar"], |
| 184 | static_libs: ["baz"], |
| 185 | platform_apis: true, |
| 186 | } |
| 187 | |
| 188 | java_library { |
| 189 | name: "bar", |
| 190 | sdk_version: "current", |
| 191 | srcs: ["b.java"], |
| 192 | } |
| 193 | |
| 194 | android_library { |
| 195 | name: "baz", |
| 196 | sdk_version: "system_current", |
| 197 | srcs: ["c.java"], |
| 198 | } |
| 199 | `) |
| 200 | |
| 201 | testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", ` |
| 202 | android_app { |
| 203 | name: "foo", |
| 204 | srcs: ["a.java"], |
| 205 | libs: ["bar"], |
| 206 | sdk_version: "current", |
| 207 | static_libs: ["baz"], |
| 208 | } |
| 209 | |
| 210 | java_library { |
| 211 | name: "bar", |
| 212 | sdk_version: "current", |
| 213 | srcs: ["b.java"], |
| 214 | } |
| 215 | |
| 216 | android_library { |
| 217 | name: "baz", |
| 218 | sdk_version: "system_current", |
| 219 | srcs: ["c.java"], |
| 220 | } |
| 221 | `) |
| 222 | |
| 223 | testJava(t, ` |
| 224 | android_app { |
| 225 | name: "foo", |
| 226 | srcs: ["a.java"], |
| 227 | libs: ["bar"], |
| 228 | sdk_version: "system_current", |
| 229 | static_libs: ["baz"], |
| 230 | } |
| 231 | |
| 232 | java_library { |
| 233 | name: "bar", |
| 234 | sdk_version: "current", |
| 235 | srcs: ["b.java"], |
| 236 | } |
| 237 | |
| 238 | android_library { |
| 239 | name: "baz", |
| 240 | sdk_version: "system_current", |
| 241 | srcs: ["c.java"], |
| 242 | } |
| 243 | `) |
| 244 | |
| 245 | testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", ` |
| 246 | android_app { |
| 247 | name: "foo", |
| 248 | srcs: ["a.java"], |
| 249 | libs: ["bar"], |
| 250 | sdk_version: "system_current", |
| 251 | static_libs: ["baz"], |
| 252 | } |
| 253 | |
| 254 | java_library { |
| 255 | name: "bar", |
| 256 | sdk_version: "current", |
| 257 | srcs: ["b.java"], |
| 258 | } |
| 259 | |
| 260 | android_library { |
| 261 | name: "baz", |
| 262 | srcs: ["c.java"], |
| 263 | } |
| 264 | `) |
| 265 | } |
| 266 | |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 267 | func TestUpdatableApps(t *testing.T) { |
| 268 | testCases := []struct { |
| 269 | name string |
| 270 | bp string |
| 271 | expectedError string |
| 272 | }{ |
| 273 | { |
| 274 | name: "Stable public SDK", |
| 275 | bp: `android_app { |
| 276 | name: "foo", |
| 277 | srcs: ["a.java"], |
| 278 | sdk_version: "29", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 279 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 280 | updatable: true, |
| 281 | }`, |
| 282 | }, |
| 283 | { |
| 284 | name: "Stable system SDK", |
| 285 | bp: `android_app { |
| 286 | name: "foo", |
| 287 | srcs: ["a.java"], |
| 288 | sdk_version: "system_29", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 289 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 290 | updatable: true, |
| 291 | }`, |
| 292 | }, |
| 293 | { |
| 294 | name: "Current public SDK", |
| 295 | bp: `android_app { |
| 296 | name: "foo", |
| 297 | srcs: ["a.java"], |
| 298 | sdk_version: "current", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 299 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 300 | updatable: true, |
| 301 | }`, |
| 302 | }, |
| 303 | { |
| 304 | name: "Current system SDK", |
| 305 | bp: `android_app { |
| 306 | name: "foo", |
| 307 | srcs: ["a.java"], |
| 308 | sdk_version: "system_current", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 309 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 310 | updatable: true, |
| 311 | }`, |
| 312 | }, |
| 313 | { |
| 314 | name: "Current module SDK", |
| 315 | bp: `android_app { |
| 316 | name: "foo", |
| 317 | srcs: ["a.java"], |
| 318 | sdk_version: "module_current", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 319 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 320 | updatable: true, |
| 321 | }`, |
| 322 | }, |
| 323 | { |
| 324 | name: "Current core SDK", |
| 325 | bp: `android_app { |
| 326 | name: "foo", |
| 327 | srcs: ["a.java"], |
| 328 | sdk_version: "core_current", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 329 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 330 | updatable: true, |
| 331 | }`, |
| 332 | }, |
| 333 | { |
| 334 | name: "No Platform APIs", |
| 335 | bp: `android_app { |
| 336 | name: "foo", |
| 337 | srcs: ["a.java"], |
| 338 | platform_apis: true, |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 339 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 340 | updatable: true, |
| 341 | }`, |
| 342 | expectedError: "Updatable apps must use stable SDKs", |
| 343 | }, |
| 344 | { |
| 345 | name: "No Core Platform APIs", |
| 346 | bp: `android_app { |
| 347 | name: "foo", |
| 348 | srcs: ["a.java"], |
| 349 | sdk_version: "core_platform", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 350 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 351 | updatable: true, |
| 352 | }`, |
| 353 | expectedError: "Updatable apps must use stable SDKs", |
| 354 | }, |
| 355 | { |
| 356 | name: "No unspecified APIs", |
| 357 | bp: `android_app { |
| 358 | name: "foo", |
| 359 | srcs: ["a.java"], |
| 360 | updatable: true, |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 361 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 362 | }`, |
| 363 | expectedError: "Updatable apps must use stable SDK", |
| 364 | }, |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 365 | { |
| 366 | name: "Must specify min_sdk_version", |
| 367 | bp: `android_app { |
| 368 | name: "app_without_min_sdk_version", |
| 369 | srcs: ["a.java"], |
| 370 | sdk_version: "29", |
| 371 | updatable: true, |
| 372 | }`, |
| 373 | expectedError: "updatable apps must set min_sdk_version.", |
| 374 | }, |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | for _, test := range testCases { |
| 378 | t.Run(test.name, func(t *testing.T) { |
| 379 | if test.expectedError == "" { |
| 380 | testJava(t, test.bp) |
| 381 | } else { |
| 382 | testJavaError(t, test.expectedError, test.bp) |
| 383 | } |
| 384 | }) |
| 385 | } |
| 386 | } |
| 387 | |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 388 | func TestResourceDirs(t *testing.T) { |
| 389 | testCases := []struct { |
| 390 | name string |
| 391 | prop string |
| 392 | resources []string |
| 393 | }{ |
| 394 | { |
| 395 | name: "no resource_dirs", |
| 396 | prop: "", |
| 397 | resources: []string{"res/res/values/strings.xml"}, |
| 398 | }, |
| 399 | { |
| 400 | name: "resource_dirs", |
| 401 | prop: `resource_dirs: ["res"]`, |
| 402 | resources: []string{"res/res/values/strings.xml"}, |
| 403 | }, |
| 404 | { |
| 405 | name: "empty resource_dirs", |
| 406 | prop: `resource_dirs: []`, |
| 407 | resources: nil, |
| 408 | }, |
| 409 | } |
| 410 | |
| 411 | fs := map[string][]byte{ |
| 412 | "res/res/values/strings.xml": nil, |
| 413 | } |
| 414 | |
| 415 | bp := ` |
| 416 | android_app { |
| 417 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 418 | sdk_version: "current", |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 419 | %s |
| 420 | } |
| 421 | ` |
| 422 | |
| 423 | for _, testCase := range testCases { |
| 424 | t.Run(testCase.name, func(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 425 | config := testConfig(nil, fmt.Sprintf(bp, testCase.prop), fs) |
| 426 | ctx := testContext() |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 427 | run(t, ctx, config) |
| 428 | |
| 429 | module := ctx.ModuleForTests("foo", "android_common") |
| 430 | resourceList := module.MaybeOutput("aapt2/res.list") |
| 431 | |
| 432 | var resources []string |
| 433 | if resourceList.Rule != nil { |
| 434 | for _, compiledResource := range resourceList.Inputs.Strings() { |
| 435 | resources = append(resources, module.Output(compiledResource).Inputs.Strings()...) |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if !reflect.DeepEqual(resources, testCase.resources) { |
| 440 | t.Errorf("expected resource files %q, got %q", |
| 441 | testCase.resources, resources) |
| 442 | } |
| 443 | }) |
| 444 | } |
| 445 | } |
| 446 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 447 | func TestLibraryAssets(t *testing.T) { |
| 448 | bp := ` |
| 449 | android_app { |
| 450 | name: "foo", |
| 451 | sdk_version: "current", |
| 452 | static_libs: ["lib1", "lib2", "lib3"], |
| 453 | } |
| 454 | |
| 455 | android_library { |
| 456 | name: "lib1", |
| 457 | sdk_version: "current", |
| 458 | asset_dirs: ["assets_a"], |
| 459 | } |
| 460 | |
| 461 | android_library { |
| 462 | name: "lib2", |
| 463 | sdk_version: "current", |
| 464 | } |
| 465 | |
| 466 | android_library { |
| 467 | name: "lib3", |
| 468 | sdk_version: "current", |
| 469 | static_libs: ["lib4"], |
| 470 | } |
| 471 | |
| 472 | android_library { |
| 473 | name: "lib4", |
| 474 | sdk_version: "current", |
| 475 | asset_dirs: ["assets_b"], |
| 476 | } |
| 477 | ` |
| 478 | |
| 479 | testCases := []struct { |
| 480 | name string |
| 481 | assetFlag string |
| 482 | assetPackages []string |
| 483 | }{ |
| 484 | { |
| 485 | name: "foo", |
| 486 | // lib1 has its own asset. lib3 doesn't have any, but provides lib4's transitively. |
| 487 | assetPackages: []string{ |
| 488 | buildDir + "/.intermediates/foo/android_common/aapt2/package-res.apk", |
| 489 | buildDir + "/.intermediates/lib1/android_common/assets.zip", |
| 490 | buildDir + "/.intermediates/lib3/android_common/assets.zip", |
| 491 | }, |
| 492 | }, |
| 493 | { |
| 494 | name: "lib1", |
| 495 | assetFlag: "-A assets_a", |
| 496 | }, |
| 497 | { |
| 498 | name: "lib2", |
| 499 | }, |
| 500 | { |
| 501 | name: "lib3", |
| 502 | assetPackages: []string{ |
| 503 | buildDir + "/.intermediates/lib3/android_common/aapt2/package-res.apk", |
| 504 | buildDir + "/.intermediates/lib4/android_common/assets.zip", |
| 505 | }, |
| 506 | }, |
| 507 | { |
| 508 | name: "lib4", |
| 509 | assetFlag: "-A assets_b", |
| 510 | }, |
| 511 | } |
| 512 | ctx := testApp(t, bp) |
| 513 | |
| 514 | for _, test := range testCases { |
| 515 | t.Run(test.name, func(t *testing.T) { |
| 516 | m := ctx.ModuleForTests(test.name, "android_common") |
| 517 | |
| 518 | // Check asset flag in aapt2 link flags |
| 519 | var aapt2link android.TestingBuildParams |
| 520 | if len(test.assetPackages) > 0 { |
| 521 | aapt2link = m.Output("aapt2/package-res.apk") |
| 522 | } else { |
| 523 | aapt2link = m.Output("package-res.apk") |
| 524 | } |
| 525 | aapt2Flags := aapt2link.Args["flags"] |
| 526 | if test.assetFlag != "" { |
| 527 | if !strings.Contains(aapt2Flags, test.assetFlag) { |
| 528 | t.Errorf("Can't find asset flag %q in aapt2 link flags %q", test.assetFlag, aapt2Flags) |
| 529 | } |
| 530 | } else { |
| 531 | if strings.Contains(aapt2Flags, " -A ") { |
| 532 | t.Errorf("aapt2 link flags %q contain unexpected asset flag", aapt2Flags) |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | // Check asset merge rule. |
| 537 | if len(test.assetPackages) > 0 { |
| 538 | mergeAssets := m.Output("package-res.apk") |
| 539 | if !reflect.DeepEqual(test.assetPackages, mergeAssets.Inputs.Strings()) { |
| 540 | t.Errorf("Unexpected mergeAssets inputs: %v, expected: %v", |
| 541 | mergeAssets.Inputs.Strings(), test.assetPackages) |
| 542 | } |
| 543 | } |
| 544 | }) |
| 545 | } |
| 546 | } |
| 547 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 548 | func TestAndroidResources(t *testing.T) { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 549 | testCases := []struct { |
| 550 | name string |
| 551 | enforceRROTargets []string |
| 552 | enforceRROExcludedOverlays []string |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 553 | resourceFiles map[string][]string |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 554 | overlayFiles map[string][]string |
| 555 | rroDirs map[string][]string |
| 556 | }{ |
| 557 | { |
| 558 | name: "no RRO", |
| 559 | enforceRROTargets: nil, |
| 560 | enforceRROExcludedOverlays: nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 561 | resourceFiles: map[string][]string{ |
| 562 | "foo": nil, |
| 563 | "bar": {"bar/res/res/values/strings.xml"}, |
| 564 | "lib": nil, |
| 565 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 566 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 567 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 568 | "foo": { |
| 569 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 570 | buildDir + "/.intermediates/lib/android_common/package-res.apk", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 571 | buildDir + "/.intermediates/lib3/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 572 | "foo/res/res/values/strings.xml", |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 573 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 574 | "device/vendor/blah/overlay/foo/res/values/strings.xml", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 575 | "product/vendor/blah/overlay/foo/res/values/strings.xml", |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 576 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 577 | "bar": { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 578 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
| 579 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
| 580 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 581 | "lib": { |
| 582 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
| 583 | "lib/res/res/values/strings.xml", |
| 584 | "device/vendor/blah/overlay/lib/res/values/strings.xml", |
| 585 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 586 | }, |
| 587 | rroDirs: map[string][]string{ |
| 588 | "foo": nil, |
| 589 | "bar": nil, |
| 590 | }, |
| 591 | }, |
| 592 | { |
| 593 | name: "enforce RRO on foo", |
| 594 | enforceRROTargets: []string{"foo"}, |
| 595 | enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"}, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 596 | resourceFiles: map[string][]string{ |
| 597 | "foo": nil, |
| 598 | "bar": {"bar/res/res/values/strings.xml"}, |
| 599 | "lib": nil, |
| 600 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 601 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 602 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 603 | "foo": { |
| 604 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 605 | buildDir + "/.intermediates/lib/android_common/package-res.apk", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 606 | buildDir + "/.intermediates/lib3/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 607 | "foo/res/res/values/strings.xml", |
| 608 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 609 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 610 | "bar": { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 611 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
| 612 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
| 613 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 614 | "lib": { |
| 615 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
| 616 | "lib/res/res/values/strings.xml", |
| 617 | "device/vendor/blah/overlay/lib/res/values/strings.xml", |
| 618 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 619 | }, |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 620 | |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 621 | rroDirs: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 622 | "foo": { |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 623 | "device:device/vendor/blah/overlay/foo/res", |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 624 | // Enforce RRO on "foo" could imply RRO on static dependencies, but for now it doesn't. |
| 625 | // "device/vendor/blah/overlay/lib/res", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 626 | "product:product/vendor/blah/overlay/foo/res", |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 627 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 628 | "bar": nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 629 | "lib": nil, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 630 | }, |
| 631 | }, |
| 632 | { |
| 633 | name: "enforce RRO on all", |
| 634 | enforceRROTargets: []string{"*"}, |
| 635 | enforceRROExcludedOverlays: []string{ |
| 636 | // Excluding specific apps/res directories also allowed. |
| 637 | "device/vendor/blah/static_overlay/foo", |
| 638 | "device/vendor/blah/static_overlay/bar/res", |
| 639 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 640 | resourceFiles: map[string][]string{ |
| 641 | "foo": nil, |
| 642 | "bar": {"bar/res/res/values/strings.xml"}, |
| 643 | "lib": nil, |
| 644 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 645 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 646 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 647 | "foo": { |
| 648 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 649 | buildDir + "/.intermediates/lib/android_common/package-res.apk", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 650 | buildDir + "/.intermediates/lib3/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 651 | "foo/res/res/values/strings.xml", |
| 652 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 653 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 654 | "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"}, |
| 655 | "lib": { |
| 656 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
| 657 | "lib/res/res/values/strings.xml", |
| 658 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 659 | }, |
| 660 | rroDirs: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 661 | "foo": { |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 662 | "device:device/vendor/blah/overlay/foo/res", |
| 663 | "product:product/vendor/blah/overlay/foo/res", |
| 664 | // Lib dep comes after the direct deps |
| 665 | "device:device/vendor/blah/overlay/lib/res", |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 666 | }, |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 667 | "bar": {"device:device/vendor/blah/overlay/bar/res"}, |
| 668 | "lib": {"device:device/vendor/blah/overlay/lib/res"}, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 669 | }, |
| 670 | }, |
| 671 | } |
| 672 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 673 | deviceResourceOverlays := []string{ |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 674 | "device/vendor/blah/overlay", |
| 675 | "device/vendor/blah/overlay2", |
| 676 | "device/vendor/blah/static_overlay", |
| 677 | } |
| 678 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 679 | productResourceOverlays := []string{ |
| 680 | "product/vendor/blah/overlay", |
| 681 | } |
| 682 | |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 683 | fs := map[string][]byte{ |
| 684 | "foo/res/res/values/strings.xml": nil, |
| 685 | "bar/res/res/values/strings.xml": nil, |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 686 | "lib/res/res/values/strings.xml": nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 687 | "lib2/res/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 688 | "device/vendor/blah/overlay/foo/res/values/strings.xml": nil, |
| 689 | "device/vendor/blah/overlay/bar/res/values/strings.xml": nil, |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 690 | "device/vendor/blah/overlay/lib/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 691 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil, |
| 692 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil, |
| 693 | "device/vendor/blah/overlay2/res/values/strings.xml": nil, |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 694 | "product/vendor/blah/overlay/foo/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | bp := ` |
| 698 | android_app { |
| 699 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 700 | sdk_version: "current", |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 701 | resource_dirs: ["foo/res"], |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 702 | static_libs: ["lib", "lib3"], |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | android_app { |
| 706 | name: "bar", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 707 | sdk_version: "current", |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 708 | resource_dirs: ["bar/res"], |
| 709 | } |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 710 | |
| 711 | android_library { |
| 712 | name: "lib", |
Jeongik Cha | 75b83b0 | 2019-11-01 15:28:00 +0900 | [diff] [blame] | 713 | sdk_version: "current", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 714 | resource_dirs: ["lib/res"], |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 715 | static_libs: ["lib2"], |
| 716 | } |
| 717 | |
| 718 | android_library { |
| 719 | name: "lib2", |
Jeongik Cha | 75b83b0 | 2019-11-01 15:28:00 +0900 | [diff] [blame] | 720 | sdk_version: "current", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 721 | resource_dirs: ["lib2/res"], |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 722 | } |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 723 | |
| 724 | // This library has the same resources as lib (should not lead to dupe RROs) |
| 725 | android_library { |
| 726 | name: "lib3", |
Jeongik Cha | 75b83b0 | 2019-11-01 15:28:00 +0900 | [diff] [blame] | 727 | sdk_version: "current", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 728 | resource_dirs: ["lib/res"] |
| 729 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 730 | ` |
| 731 | |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 732 | for _, testCase := range testCases { |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 733 | t.Run(testCase.name, func(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 734 | config := testAppConfig(nil, bp, fs) |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 735 | config.TestProductVariables.DeviceResourceOverlays = deviceResourceOverlays |
| 736 | config.TestProductVariables.ProductResourceOverlays = productResourceOverlays |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 737 | if testCase.enforceRROTargets != nil { |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 738 | config.TestProductVariables.EnforceRROTargets = testCase.enforceRROTargets |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 739 | } |
| 740 | if testCase.enforceRROExcludedOverlays != nil { |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 741 | config.TestProductVariables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 744 | ctx := testContext() |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 745 | run(t, ctx, config) |
| 746 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 747 | resourceListToFiles := func(module android.TestingModule, list []string) (files []string) { |
| 748 | for _, o := range list { |
| 749 | res := module.MaybeOutput(o) |
| 750 | if res.Rule != nil { |
| 751 | // If the overlay is compiled as part of this module (i.e. a .arsc.flat file), |
| 752 | // verify the inputs to the .arsc.flat rule. |
| 753 | files = append(files, res.Inputs.Strings()...) |
| 754 | } else { |
| 755 | // Otherwise, verify the full path to the output of the other module |
| 756 | files = append(files, o) |
Anton Hansson | 94c93f3 | 2019-01-30 16:03:37 +0000 | [diff] [blame] | 757 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 758 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 759 | return files |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 760 | } |
| 761 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 762 | getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) { |
| 763 | module := ctx.ModuleForTests(moduleName, "android_common") |
| 764 | resourceList := module.MaybeOutput("aapt2/res.list") |
| 765 | if resourceList.Rule != nil { |
| 766 | resourceFiles = resourceListToFiles(module, resourceList.Inputs.Strings()) |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 767 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 768 | overlayList := module.MaybeOutput("aapt2/overlay.list") |
| 769 | if overlayList.Rule != nil { |
| 770 | overlayFiles = resourceListToFiles(module, overlayList.Inputs.Strings()) |
| 771 | } |
| 772 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 773 | for _, d := range module.Module().(AndroidLibraryDependency).ExportedRRODirs() { |
| 774 | var prefix string |
| 775 | if d.overlayType == device { |
| 776 | prefix = "device:" |
| 777 | } else if d.overlayType == product { |
| 778 | prefix = "product:" |
| 779 | } else { |
| 780 | t.Fatalf("Unexpected overlayType %d", d.overlayType) |
| 781 | } |
| 782 | rroDirs = append(rroDirs, prefix+d.path.String()) |
| 783 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 784 | |
| 785 | return resourceFiles, overlayFiles, rroDirs |
| 786 | } |
| 787 | |
| 788 | modules := []string{"foo", "bar", "lib", "lib2"} |
| 789 | for _, module := range modules { |
| 790 | resourceFiles, overlayFiles, rroDirs := getResources(module) |
| 791 | |
| 792 | if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) { |
| 793 | t.Errorf("expected %s resource files:\n %#v\n got:\n %#v", |
| 794 | module, testCase.resourceFiles[module], resourceFiles) |
| 795 | } |
| 796 | if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) { |
| 797 | t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v", |
| 798 | module, testCase.overlayFiles[module], overlayFiles) |
| 799 | } |
| 800 | if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) { |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 801 | t.Errorf("expected %s rroDirs: %#v\n got:\n %#v", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 802 | module, testCase.rroDirs[module], rroDirs) |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 803 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 804 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 805 | }) |
| 806 | } |
| 807 | } |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 808 | |
| 809 | func TestAppSdkVersion(t *testing.T) { |
| 810 | testCases := []struct { |
| 811 | name string |
| 812 | sdkVersion string |
| 813 | platformSdkInt int |
| 814 | platformSdkCodename string |
| 815 | platformSdkFinal bool |
| 816 | expectedMinSdkVersion string |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 817 | platformApis bool |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 818 | }{ |
| 819 | { |
| 820 | name: "current final SDK", |
| 821 | sdkVersion: "current", |
| 822 | platformSdkInt: 27, |
| 823 | platformSdkCodename: "REL", |
| 824 | platformSdkFinal: true, |
| 825 | expectedMinSdkVersion: "27", |
| 826 | }, |
| 827 | { |
| 828 | name: "current non-final SDK", |
| 829 | sdkVersion: "current", |
| 830 | platformSdkInt: 27, |
| 831 | platformSdkCodename: "OMR1", |
| 832 | platformSdkFinal: false, |
| 833 | expectedMinSdkVersion: "OMR1", |
| 834 | }, |
| 835 | { |
| 836 | name: "default final SDK", |
| 837 | sdkVersion: "", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 838 | platformApis: true, |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 839 | platformSdkInt: 27, |
| 840 | platformSdkCodename: "REL", |
| 841 | platformSdkFinal: true, |
| 842 | expectedMinSdkVersion: "27", |
| 843 | }, |
| 844 | { |
| 845 | name: "default non-final SDK", |
| 846 | sdkVersion: "", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 847 | platformApis: true, |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 848 | platformSdkInt: 27, |
| 849 | platformSdkCodename: "OMR1", |
| 850 | platformSdkFinal: false, |
| 851 | expectedMinSdkVersion: "OMR1", |
| 852 | }, |
| 853 | { |
| 854 | name: "14", |
| 855 | sdkVersion: "14", |
| 856 | expectedMinSdkVersion: "14", |
| 857 | }, |
| 858 | } |
| 859 | |
| 860 | for _, moduleType := range []string{"android_app", "android_library"} { |
| 861 | for _, test := range testCases { |
| 862 | t.Run(moduleType+" "+test.name, func(t *testing.T) { |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 863 | platformApiProp := "" |
| 864 | if test.platformApis { |
| 865 | platformApiProp = "platform_apis: true," |
| 866 | } |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 867 | bp := fmt.Sprintf(`%s { |
| 868 | name: "foo", |
| 869 | srcs: ["a.java"], |
| 870 | sdk_version: "%s", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 871 | %s |
| 872 | }`, moduleType, test.sdkVersion, platformApiProp) |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 873 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 874 | config := testAppConfig(nil, bp, nil) |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 875 | config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt |
| 876 | config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename |
| 877 | config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal |
| 878 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 879 | ctx := testContext() |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 880 | |
| 881 | run(t, ctx, config) |
| 882 | |
| 883 | foo := ctx.ModuleForTests("foo", "android_common") |
| 884 | link := foo.Output("package-res.apk") |
| 885 | linkFlags := strings.Split(link.Args["flags"], " ") |
| 886 | min := android.IndexList("--min-sdk-version", linkFlags) |
| 887 | target := android.IndexList("--target-sdk-version", linkFlags) |
| 888 | |
| 889 | if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 { |
| 890 | t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags) |
| 891 | } |
| 892 | |
| 893 | gotMinSdkVersion := linkFlags[min+1] |
| 894 | gotTargetSdkVersion := linkFlags[target+1] |
| 895 | |
| 896 | if gotMinSdkVersion != test.expectedMinSdkVersion { |
| 897 | t.Errorf("incorrect --min-sdk-version, expected %q got %q", |
| 898 | test.expectedMinSdkVersion, gotMinSdkVersion) |
| 899 | } |
| 900 | |
| 901 | if gotTargetSdkVersion != test.expectedMinSdkVersion { |
| 902 | t.Errorf("incorrect --target-sdk-version, expected %q got %q", |
| 903 | test.expectedMinSdkVersion, gotTargetSdkVersion) |
| 904 | } |
| 905 | }) |
| 906 | } |
| 907 | } |
| 908 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 909 | |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 910 | func TestJNIABI(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 911 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 912 | cc_library { |
| 913 | name: "libjni", |
| 914 | system_shared_libs: [], |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 915 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 916 | stl: "none", |
| 917 | } |
| 918 | |
| 919 | android_test { |
| 920 | name: "test", |
| 921 | sdk_version: "core_platform", |
| 922 | jni_libs: ["libjni"], |
| 923 | } |
| 924 | |
| 925 | android_test { |
| 926 | name: "test_first", |
| 927 | sdk_version: "core_platform", |
| 928 | compile_multilib: "first", |
| 929 | jni_libs: ["libjni"], |
| 930 | } |
| 931 | |
| 932 | android_test { |
| 933 | name: "test_both", |
| 934 | sdk_version: "core_platform", |
| 935 | compile_multilib: "both", |
| 936 | jni_libs: ["libjni"], |
| 937 | } |
| 938 | |
| 939 | android_test { |
| 940 | name: "test_32", |
| 941 | sdk_version: "core_platform", |
| 942 | compile_multilib: "32", |
| 943 | jni_libs: ["libjni"], |
| 944 | } |
| 945 | |
| 946 | android_test { |
| 947 | name: "test_64", |
| 948 | sdk_version: "core_platform", |
| 949 | compile_multilib: "64", |
| 950 | jni_libs: ["libjni"], |
| 951 | } |
| 952 | `) |
| 953 | |
| 954 | testCases := []struct { |
| 955 | name string |
| 956 | abis []string |
| 957 | }{ |
| 958 | {"test", []string{"arm64-v8a"}}, |
| 959 | {"test_first", []string{"arm64-v8a"}}, |
| 960 | {"test_both", []string{"arm64-v8a", "armeabi-v7a"}}, |
| 961 | {"test_32", []string{"armeabi-v7a"}}, |
| 962 | {"test_64", []string{"arm64-v8a"}}, |
| 963 | } |
| 964 | |
| 965 | for _, test := range testCases { |
| 966 | t.Run(test.name, func(t *testing.T) { |
| 967 | app := ctx.ModuleForTests(test.name, "android_common") |
| 968 | jniLibZip := app.Output("jnilibs.zip") |
| 969 | var abis []string |
| 970 | args := strings.Fields(jniLibZip.Args["jarArgs"]) |
| 971 | for i := 0; i < len(args); i++ { |
| 972 | if args[i] == "-P" { |
| 973 | abis = append(abis, filepath.Base(args[i+1])) |
| 974 | i++ |
| 975 | } |
| 976 | } |
| 977 | if !reflect.DeepEqual(abis, test.abis) { |
| 978 | t.Errorf("want abis %v, got %v", test.abis, abis) |
| 979 | } |
| 980 | }) |
| 981 | } |
| 982 | } |
| 983 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 984 | func TestAppSdkVersionByPartition(t *testing.T) { |
| 985 | testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", ` |
| 986 | android_app { |
| 987 | name: "foo", |
| 988 | srcs: ["a.java"], |
| 989 | vendor: true, |
| 990 | platform_apis: true, |
| 991 | } |
| 992 | `) |
| 993 | |
| 994 | testJava(t, ` |
| 995 | android_app { |
| 996 | name: "bar", |
| 997 | srcs: ["b.java"], |
| 998 | platform_apis: true, |
| 999 | } |
| 1000 | `) |
| 1001 | |
| 1002 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 1003 | bp := ` |
| 1004 | android_app { |
| 1005 | name: "foo", |
| 1006 | srcs: ["a.java"], |
| 1007 | product_specific: true, |
| 1008 | platform_apis: true, |
| 1009 | } |
| 1010 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1011 | |
| 1012 | config := testAppConfig(nil, bp, nil) |
| 1013 | config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 1014 | if enforce { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1015 | testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", config) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 1016 | } else { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1017 | testJavaWithConfig(t, config) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1022 | func TestJNIPackaging(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1023 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1024 | cc_library { |
| 1025 | name: "libjni", |
| 1026 | system_shared_libs: [], |
| 1027 | stl: "none", |
Colin Cross | 094cde4 | 2020-02-15 10:38:00 -0800 | [diff] [blame] | 1028 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | android_app { |
| 1032 | name: "app", |
| 1033 | jni_libs: ["libjni"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1034 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | android_app { |
| 1038 | name: "app_noembed", |
| 1039 | jni_libs: ["libjni"], |
| 1040 | use_embedded_native_libs: false, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1041 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | android_app { |
| 1045 | name: "app_embed", |
| 1046 | jni_libs: ["libjni"], |
| 1047 | use_embedded_native_libs: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1048 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | android_test { |
| 1052 | name: "test", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1053 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1054 | jni_libs: ["libjni"], |
| 1055 | } |
| 1056 | |
| 1057 | android_test { |
| 1058 | name: "test_noembed", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1059 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1060 | jni_libs: ["libjni"], |
| 1061 | use_embedded_native_libs: false, |
| 1062 | } |
| 1063 | |
| 1064 | android_test_helper_app { |
| 1065 | name: "test_helper", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1066 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1067 | jni_libs: ["libjni"], |
| 1068 | } |
| 1069 | |
| 1070 | android_test_helper_app { |
| 1071 | name: "test_helper_noembed", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1072 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1073 | jni_libs: ["libjni"], |
| 1074 | use_embedded_native_libs: false, |
| 1075 | } |
| 1076 | `) |
| 1077 | |
| 1078 | testCases := []struct { |
| 1079 | name string |
| 1080 | packaged bool |
| 1081 | compressed bool |
| 1082 | }{ |
| 1083 | {"app", false, false}, |
| 1084 | {"app_noembed", false, false}, |
| 1085 | {"app_embed", true, false}, |
| 1086 | {"test", true, false}, |
| 1087 | {"test_noembed", true, true}, |
| 1088 | {"test_helper", true, false}, |
| 1089 | {"test_helper_noembed", true, true}, |
| 1090 | } |
| 1091 | |
| 1092 | for _, test := range testCases { |
| 1093 | t.Run(test.name, func(t *testing.T) { |
| 1094 | app := ctx.ModuleForTests(test.name, "android_common") |
| 1095 | jniLibZip := app.MaybeOutput("jnilibs.zip") |
| 1096 | if g, w := (jniLibZip.Rule != nil), test.packaged; g != w { |
| 1097 | t.Errorf("expected jni packaged %v, got %v", w, g) |
| 1098 | } |
| 1099 | |
| 1100 | if jniLibZip.Rule != nil { |
| 1101 | if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w { |
| 1102 | t.Errorf("expected jni compressed %v, got %v", w, g) |
| 1103 | } |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1104 | |
| 1105 | if !strings.Contains(jniLibZip.Implicits[0].String(), "_sdk_") { |
| 1106 | t.Errorf("expected input %q to use sdk variant", jniLibZip.Implicits[0].String()) |
| 1107 | } |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1108 | } |
| 1109 | }) |
| 1110 | } |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1113 | func TestCertificates(t *testing.T) { |
| 1114 | testCases := []struct { |
| 1115 | name string |
| 1116 | bp string |
| 1117 | certificateOverride string |
| 1118 | expected string |
| 1119 | }{ |
| 1120 | { |
| 1121 | name: "default", |
| 1122 | bp: ` |
| 1123 | android_app { |
| 1124 | name: "foo", |
| 1125 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1126 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1127 | } |
| 1128 | `, |
| 1129 | certificateOverride: "", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 1130 | expected: "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1131 | }, |
| 1132 | { |
| 1133 | name: "module certificate property", |
| 1134 | bp: ` |
| 1135 | android_app { |
| 1136 | name: "foo", |
| 1137 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1138 | certificate: ":new_certificate", |
| 1139 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | android_app_certificate { |
| 1143 | name: "new_certificate", |
| 1144 | certificate: "cert/new_cert", |
| 1145 | } |
| 1146 | `, |
| 1147 | certificateOverride: "", |
| 1148 | expected: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 1149 | }, |
| 1150 | { |
| 1151 | name: "path certificate property", |
| 1152 | bp: ` |
| 1153 | android_app { |
| 1154 | name: "foo", |
| 1155 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1156 | certificate: "expiredkey", |
| 1157 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1158 | } |
| 1159 | `, |
| 1160 | certificateOverride: "", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 1161 | expected: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1162 | }, |
| 1163 | { |
| 1164 | name: "certificate overrides", |
| 1165 | bp: ` |
| 1166 | android_app { |
| 1167 | name: "foo", |
| 1168 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1169 | certificate: "expiredkey", |
| 1170 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | android_app_certificate { |
| 1174 | name: "new_certificate", |
| 1175 | certificate: "cert/new_cert", |
| 1176 | } |
| 1177 | `, |
| 1178 | certificateOverride: "foo:new_certificate", |
| 1179 | expected: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 1180 | }, |
| 1181 | } |
| 1182 | |
| 1183 | for _, test := range testCases { |
| 1184 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1185 | config := testAppConfig(nil, test.bp, nil) |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1186 | if test.certificateOverride != "" { |
| 1187 | config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride} |
| 1188 | } |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1189 | ctx := testContext() |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1190 | |
| 1191 | run(t, ctx, config) |
| 1192 | foo := ctx.ModuleForTests("foo", "android_common") |
| 1193 | |
| 1194 | signapk := foo.Output("foo.apk") |
| 1195 | signFlags := signapk.Args["certificates"] |
| 1196 | if test.expected != signFlags { |
| 1197 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags) |
| 1198 | } |
| 1199 | }) |
| 1200 | } |
| 1201 | } |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1202 | |
| 1203 | func TestPackageNameOverride(t *testing.T) { |
| 1204 | testCases := []struct { |
| 1205 | name string |
| 1206 | bp string |
| 1207 | packageNameOverride string |
| 1208 | expected []string |
| 1209 | }{ |
| 1210 | { |
| 1211 | name: "default", |
| 1212 | bp: ` |
| 1213 | android_app { |
| 1214 | name: "foo", |
| 1215 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1216 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1217 | } |
| 1218 | `, |
| 1219 | packageNameOverride: "", |
| 1220 | expected: []string{ |
| 1221 | buildDir + "/.intermediates/foo/android_common/foo.apk", |
| 1222 | buildDir + "/target/product/test_device/system/app/foo/foo.apk", |
| 1223 | }, |
| 1224 | }, |
| 1225 | { |
| 1226 | name: "overridden", |
| 1227 | bp: ` |
| 1228 | android_app { |
| 1229 | name: "foo", |
| 1230 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1231 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1232 | } |
| 1233 | `, |
| 1234 | packageNameOverride: "foo:bar", |
| 1235 | expected: []string{ |
| 1236 | // The package apk should be still be the original name for test dependencies. |
Jaewoong Jung | 5a49881 | 2019-11-07 14:14:38 -0800 | [diff] [blame] | 1237 | buildDir + "/.intermediates/foo/android_common/bar.apk", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1238 | buildDir + "/target/product/test_device/system/app/bar/bar.apk", |
| 1239 | }, |
| 1240 | }, |
| 1241 | } |
| 1242 | |
| 1243 | for _, test := range testCases { |
| 1244 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1245 | config := testAppConfig(nil, test.bp, nil) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1246 | if test.packageNameOverride != "" { |
| 1247 | config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride} |
| 1248 | } |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1249 | ctx := testContext() |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1250 | |
| 1251 | run(t, ctx, config) |
| 1252 | foo := ctx.ModuleForTests("foo", "android_common") |
| 1253 | |
| 1254 | outputs := foo.AllOutputs() |
| 1255 | outputMap := make(map[string]bool) |
| 1256 | for _, o := range outputs { |
| 1257 | outputMap[o] = true |
| 1258 | } |
| 1259 | for _, e := range test.expected { |
| 1260 | if _, exist := outputMap[e]; !exist { |
| 1261 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs) |
| 1262 | } |
| 1263 | } |
| 1264 | }) |
| 1265 | } |
| 1266 | } |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1267 | |
| 1268 | func TestInstrumentationTargetOverridden(t *testing.T) { |
| 1269 | bp := ` |
| 1270 | android_app { |
| 1271 | name: "foo", |
| 1272 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1273 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | android_test { |
| 1277 | name: "bar", |
| 1278 | instrumentation_for: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1279 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1280 | } |
| 1281 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1282 | config := testAppConfig(nil, bp, nil) |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1283 | config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"} |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1284 | ctx := testContext() |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1285 | |
| 1286 | run(t, ctx, config) |
| 1287 | |
| 1288 | bar := ctx.ModuleForTests("bar", "android_common") |
| 1289 | res := bar.Output("package-res.apk") |
| 1290 | aapt2Flags := res.Args["flags"] |
| 1291 | e := "--rename-instrumentation-target-package org.dandroid.bp" |
| 1292 | if !strings.Contains(aapt2Flags, e) { |
| 1293 | t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags) |
| 1294 | } |
| 1295 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1296 | |
| 1297 | func TestOverrideAndroidApp(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1298 | ctx, _ := testJava(t, ` |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1299 | android_app { |
| 1300 | name: "foo", |
| 1301 | srcs: ["a.java"], |
Jaewoong Jung | a641ee9 | 2019-03-27 11:17:14 -0700 | [diff] [blame] | 1302 | certificate: "expiredkey", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1303 | overrides: ["qux"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1304 | sdk_version: "current", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | override_android_app { |
| 1308 | name: "bar", |
| 1309 | base: "foo", |
| 1310 | certificate: ":new_certificate", |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1311 | logging_parent: "bah", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | android_app_certificate { |
| 1315 | name: "new_certificate", |
| 1316 | certificate: "cert/new_cert", |
| 1317 | } |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1318 | |
| 1319 | override_android_app { |
| 1320 | name: "baz", |
| 1321 | base: "foo", |
| 1322 | package_name: "org.dandroid.bp", |
| 1323 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1324 | `) |
| 1325 | |
| 1326 | expectedVariants := []struct { |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1327 | moduleName string |
| 1328 | variantName string |
| 1329 | apkName string |
| 1330 | apkPath string |
| 1331 | signFlag string |
| 1332 | overrides []string |
| 1333 | aaptFlag string |
| 1334 | logging_parent string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1335 | }{ |
| 1336 | { |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1337 | moduleName: "foo", |
| 1338 | variantName: "android_common", |
| 1339 | apkPath: "/target/product/test_device/system/app/foo/foo.apk", |
| 1340 | signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1341 | overrides: []string{"qux"}, |
| 1342 | aaptFlag: "", |
| 1343 | logging_parent: "", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1344 | }, |
| 1345 | { |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1346 | moduleName: "bar", |
| 1347 | variantName: "android_common_bar", |
| 1348 | apkPath: "/target/product/test_device/system/app/bar/bar.apk", |
| 1349 | signFlag: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 1350 | overrides: []string{"qux", "foo"}, |
| 1351 | aaptFlag: "", |
| 1352 | logging_parent: "bah", |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1353 | }, |
| 1354 | { |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1355 | moduleName: "baz", |
| 1356 | variantName: "android_common_baz", |
| 1357 | apkPath: "/target/product/test_device/system/app/baz/baz.apk", |
| 1358 | signFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1359 | overrides: []string{"qux", "foo"}, |
| 1360 | aaptFlag: "--rename-manifest-package org.dandroid.bp", |
| 1361 | logging_parent: "", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1362 | }, |
| 1363 | } |
| 1364 | for _, expected := range expectedVariants { |
| 1365 | variant := ctx.ModuleForTests("foo", expected.variantName) |
| 1366 | |
| 1367 | // Check the final apk name |
| 1368 | outputs := variant.AllOutputs() |
| 1369 | expectedApkPath := buildDir + expected.apkPath |
| 1370 | found := false |
| 1371 | for _, o := range outputs { |
| 1372 | if o == expectedApkPath { |
| 1373 | found = true |
| 1374 | break |
| 1375 | } |
| 1376 | } |
| 1377 | if !found { |
| 1378 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs) |
| 1379 | } |
| 1380 | |
| 1381 | // Check the certificate paths |
Jaewoong Jung | 5a49881 | 2019-11-07 14:14:38 -0800 | [diff] [blame] | 1382 | signapk := variant.Output(expected.moduleName + ".apk") |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1383 | signFlag := signapk.Args["certificates"] |
| 1384 | if expected.signFlag != signFlag { |
| 1385 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected.signFlag, signFlag) |
| 1386 | } |
| 1387 | |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1388 | // Check if the overrides field values are correctly aggregated. |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1389 | mod := variant.Module().(*AndroidApp) |
| 1390 | if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) { |
| 1391 | t.Errorf("Incorrect overrides property value, expected: %q, got: %q", |
| 1392 | expected.overrides, mod.appProperties.Overrides) |
| 1393 | } |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1394 | |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1395 | // Test Overridable property: Logging_parent |
| 1396 | logging_parent := mod.aapt.LoggingParent |
| 1397 | if expected.logging_parent != logging_parent { |
| 1398 | t.Errorf("Incorrect overrides property value for logging parent, expected: %q, got: %q", |
| 1399 | expected.logging_parent, logging_parent) |
| 1400 | } |
| 1401 | |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1402 | // Check the package renaming flag, if exists. |
| 1403 | res := variant.Output("package-res.apk") |
| 1404 | aapt2Flags := res.Args["flags"] |
| 1405 | if !strings.Contains(aapt2Flags, expected.aaptFlag) { |
| 1406 | t.Errorf("package renaming flag, %q is missing in aapt2 link flags, %q", expected.aaptFlag, aapt2Flags) |
| 1407 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1408 | } |
| 1409 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1410 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1411 | func TestOverrideAndroidAppDependency(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1412 | ctx, _ := testJava(t, ` |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1413 | android_app { |
| 1414 | name: "foo", |
| 1415 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1416 | sdk_version: "current", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | override_android_app { |
| 1420 | name: "bar", |
| 1421 | base: "foo", |
| 1422 | package_name: "org.dandroid.bp", |
| 1423 | } |
| 1424 | |
| 1425 | android_test { |
| 1426 | name: "baz", |
| 1427 | srcs: ["b.java"], |
| 1428 | instrumentation_for: "foo", |
| 1429 | } |
| 1430 | |
| 1431 | android_test { |
| 1432 | name: "qux", |
| 1433 | srcs: ["b.java"], |
| 1434 | instrumentation_for: "bar", |
| 1435 | } |
| 1436 | `) |
| 1437 | |
| 1438 | // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg. |
| 1439 | javac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1440 | fooTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 1441 | if !strings.Contains(javac.Args["classpath"], fooTurbine) { |
| 1442 | t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine) |
| 1443 | } |
| 1444 | |
| 1445 | // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg. |
| 1446 | javac = ctx.ModuleForTests("qux", "android_common").Rule("javac") |
| 1447 | barTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common_bar", "turbine-combined", "foo.jar") |
| 1448 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 1449 | t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
| 1450 | } |
| 1451 | } |
| 1452 | |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 1453 | func TestOverrideAndroidTest(t *testing.T) { |
| 1454 | ctx, _ := testJava(t, ` |
| 1455 | android_app { |
| 1456 | name: "foo", |
| 1457 | srcs: ["a.java"], |
| 1458 | package_name: "com.android.foo", |
| 1459 | sdk_version: "current", |
| 1460 | } |
| 1461 | |
| 1462 | override_android_app { |
| 1463 | name: "bar", |
| 1464 | base: "foo", |
| 1465 | package_name: "com.android.bar", |
| 1466 | } |
| 1467 | |
| 1468 | android_test { |
| 1469 | name: "foo_test", |
| 1470 | srcs: ["b.java"], |
| 1471 | instrumentation_for: "foo", |
| 1472 | } |
| 1473 | |
| 1474 | override_android_test { |
| 1475 | name: "bar_test", |
| 1476 | base: "foo_test", |
| 1477 | package_name: "com.android.bar.test", |
| 1478 | instrumentation_for: "bar", |
| 1479 | instrumentation_target_package: "com.android.bar", |
| 1480 | } |
| 1481 | `) |
| 1482 | |
| 1483 | expectedVariants := []struct { |
| 1484 | moduleName string |
| 1485 | variantName string |
| 1486 | apkPath string |
| 1487 | overrides []string |
| 1488 | targetVariant string |
| 1489 | packageFlag string |
| 1490 | targetPackageFlag string |
| 1491 | }{ |
| 1492 | { |
| 1493 | variantName: "android_common", |
Jaewoong Jung | 326a941 | 2019-11-21 10:41:00 -0800 | [diff] [blame] | 1494 | apkPath: "/target/product/test_device/testcases/foo_test/arm64/foo_test.apk", |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 1495 | overrides: nil, |
| 1496 | targetVariant: "android_common", |
| 1497 | packageFlag: "", |
| 1498 | targetPackageFlag: "", |
| 1499 | }, |
| 1500 | { |
| 1501 | variantName: "android_common_bar_test", |
Jaewoong Jung | 326a941 | 2019-11-21 10:41:00 -0800 | [diff] [blame] | 1502 | apkPath: "/target/product/test_device/testcases/bar_test/arm64/bar_test.apk", |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 1503 | overrides: []string{"foo_test"}, |
| 1504 | targetVariant: "android_common_bar", |
| 1505 | packageFlag: "com.android.bar.test", |
| 1506 | targetPackageFlag: "com.android.bar", |
| 1507 | }, |
| 1508 | } |
| 1509 | for _, expected := range expectedVariants { |
| 1510 | variant := ctx.ModuleForTests("foo_test", expected.variantName) |
| 1511 | |
| 1512 | // Check the final apk name |
| 1513 | outputs := variant.AllOutputs() |
| 1514 | expectedApkPath := buildDir + expected.apkPath |
| 1515 | found := false |
| 1516 | for _, o := range outputs { |
| 1517 | if o == expectedApkPath { |
| 1518 | found = true |
| 1519 | break |
| 1520 | } |
| 1521 | } |
| 1522 | if !found { |
| 1523 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs) |
| 1524 | } |
| 1525 | |
| 1526 | // Check if the overrides field values are correctly aggregated. |
| 1527 | mod := variant.Module().(*AndroidTest) |
| 1528 | if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) { |
| 1529 | t.Errorf("Incorrect overrides property value, expected: %q, got: %q", |
| 1530 | expected.overrides, mod.appProperties.Overrides) |
| 1531 | } |
| 1532 | |
| 1533 | // Check if javac classpath has the correct jar file path. This checks instrumentation_for overrides. |
| 1534 | javac := variant.Rule("javac") |
| 1535 | turbine := filepath.Join(buildDir, ".intermediates", "foo", expected.targetVariant, "turbine-combined", "foo.jar") |
| 1536 | if !strings.Contains(javac.Args["classpath"], turbine) { |
| 1537 | t.Errorf("classpath %q does not contain %q", javac.Args["classpath"], turbine) |
| 1538 | } |
| 1539 | |
| 1540 | // Check aapt2 flags. |
| 1541 | res := variant.Output("package-res.apk") |
| 1542 | aapt2Flags := res.Args["flags"] |
| 1543 | checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag) |
| 1544 | checkAapt2LinkFlag(t, aapt2Flags, "rename-instrumentation-target-package", expected.targetPackageFlag) |
| 1545 | } |
| 1546 | } |
| 1547 | |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 1548 | func TestAndroidTest_FixTestConfig(t *testing.T) { |
| 1549 | ctx, _ := testJava(t, ` |
| 1550 | android_app { |
| 1551 | name: "foo", |
| 1552 | srcs: ["a.java"], |
| 1553 | package_name: "com.android.foo", |
| 1554 | sdk_version: "current", |
| 1555 | } |
| 1556 | |
| 1557 | android_test { |
| 1558 | name: "foo_test", |
| 1559 | srcs: ["b.java"], |
| 1560 | instrumentation_for: "foo", |
| 1561 | } |
| 1562 | |
| 1563 | android_test { |
| 1564 | name: "bar_test", |
| 1565 | srcs: ["b.java"], |
| 1566 | package_name: "com.android.bar.test", |
| 1567 | instrumentation_for: "foo", |
| 1568 | } |
| 1569 | |
| 1570 | override_android_test { |
| 1571 | name: "baz_test", |
| 1572 | base: "foo_test", |
| 1573 | package_name: "com.android.baz.test", |
| 1574 | } |
| 1575 | `) |
| 1576 | |
| 1577 | testCases := []struct { |
| 1578 | moduleName string |
| 1579 | variantName string |
| 1580 | expectedFlags []string |
| 1581 | }{ |
| 1582 | { |
| 1583 | moduleName: "foo_test", |
| 1584 | variantName: "android_common", |
| 1585 | }, |
| 1586 | { |
| 1587 | moduleName: "bar_test", |
| 1588 | variantName: "android_common", |
| 1589 | expectedFlags: []string{ |
| 1590 | "--manifest " + buildDir + "/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml", |
| 1591 | "--package-name com.android.bar.test", |
| 1592 | }, |
| 1593 | }, |
| 1594 | { |
| 1595 | moduleName: "foo_test", |
| 1596 | variantName: "android_common_baz_test", |
| 1597 | expectedFlags: []string{ |
| 1598 | "--manifest " + buildDir + |
| 1599 | "/.intermediates/foo_test/android_common_baz_test/manifest_fixer/AndroidManifest.xml", |
| 1600 | "--package-name com.android.baz.test", |
| 1601 | "--test-file-name baz_test.apk", |
| 1602 | }, |
| 1603 | }, |
| 1604 | } |
| 1605 | |
| 1606 | for _, test := range testCases { |
| 1607 | variant := ctx.ModuleForTests(test.moduleName, test.variantName) |
| 1608 | params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml") |
| 1609 | |
| 1610 | if len(test.expectedFlags) > 0 { |
| 1611 | if params.Rule == nil { |
| 1612 | t.Errorf("test_config_fixer was expected to run, but didn't") |
| 1613 | } else { |
| 1614 | for _, flag := range test.expectedFlags { |
| 1615 | if !strings.Contains(params.RuleParams.Command, flag) { |
| 1616 | t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command) |
| 1617 | } |
| 1618 | } |
| 1619 | } |
| 1620 | } else { |
| 1621 | if params.Rule != nil { |
| 1622 | t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command) |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | } |
| 1627 | } |
| 1628 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1629 | func TestAndroidAppImport(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1630 | ctx, _ := testJava(t, ` |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1631 | android_app_import { |
| 1632 | name: "foo", |
| 1633 | apk: "prebuilts/apk/app.apk", |
| 1634 | certificate: "platform", |
| 1635 | dex_preopt: { |
| 1636 | enabled: true, |
| 1637 | }, |
| 1638 | } |
| 1639 | `) |
| 1640 | |
| 1641 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1642 | |
| 1643 | // Check dexpreopt outputs. |
| 1644 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil || |
| 1645 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil { |
| 1646 | t.Errorf("can't find dexpreopt outputs") |
| 1647 | } |
| 1648 | |
| 1649 | // Check cert signing flag. |
| 1650 | signedApk := variant.Output("signed/foo.apk") |
| 1651 | signingFlag := signedApk.Args["certificates"] |
| 1652 | expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8" |
| 1653 | if expected != signingFlag { |
| 1654 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag) |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | func TestAndroidAppImport_NoDexPreopt(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1659 | ctx, _ := testJava(t, ` |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1660 | android_app_import { |
| 1661 | name: "foo", |
| 1662 | apk: "prebuilts/apk/app.apk", |
| 1663 | certificate: "platform", |
| 1664 | dex_preopt: { |
| 1665 | enabled: false, |
| 1666 | }, |
| 1667 | } |
| 1668 | `) |
| 1669 | |
| 1670 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1671 | |
| 1672 | // Check dexpreopt outputs. They shouldn't exist. |
| 1673 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule != nil || |
| 1674 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule != nil { |
| 1675 | t.Errorf("dexpreopt shouldn't have run.") |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | func TestAndroidAppImport_Presigned(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1680 | ctx, _ := testJava(t, ` |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1681 | android_app_import { |
| 1682 | name: "foo", |
| 1683 | apk: "prebuilts/apk/app.apk", |
| 1684 | presigned: true, |
| 1685 | dex_preopt: { |
| 1686 | enabled: true, |
| 1687 | }, |
| 1688 | } |
| 1689 | `) |
| 1690 | |
| 1691 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1692 | |
| 1693 | // Check dexpreopt outputs. |
| 1694 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil || |
| 1695 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil { |
| 1696 | t.Errorf("can't find dexpreopt outputs") |
| 1697 | } |
Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 1698 | // Make sure signing was skipped and aligning was done. |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1699 | if variant.MaybeOutput("signed/foo.apk").Rule != nil { |
| 1700 | t.Errorf("signing rule shouldn't be included.") |
| 1701 | } |
| 1702 | if variant.MaybeOutput("zip-aligned/foo.apk").Rule == nil { |
| 1703 | t.Errorf("can't find aligning rule") |
| 1704 | } |
| 1705 | } |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1706 | |
Jaewoong Jung | 961d4fd | 2019-08-22 14:25:58 -0700 | [diff] [blame] | 1707 | func TestAndroidAppImport_DefaultDevCert(t *testing.T) { |
| 1708 | ctx, _ := testJava(t, ` |
| 1709 | android_app_import { |
| 1710 | name: "foo", |
| 1711 | apk: "prebuilts/apk/app.apk", |
| 1712 | default_dev_cert: true, |
| 1713 | dex_preopt: { |
| 1714 | enabled: true, |
| 1715 | }, |
| 1716 | } |
| 1717 | `) |
| 1718 | |
| 1719 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1720 | |
| 1721 | // Check dexpreopt outputs. |
| 1722 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil || |
| 1723 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil { |
| 1724 | t.Errorf("can't find dexpreopt outputs") |
| 1725 | } |
| 1726 | |
| 1727 | // Check cert signing flag. |
| 1728 | signedApk := variant.Output("signed/foo.apk") |
| 1729 | signingFlag := signedApk.Args["certificates"] |
| 1730 | expected := "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8" |
| 1731 | if expected != signingFlag { |
| 1732 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag) |
| 1733 | } |
| 1734 | } |
| 1735 | |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1736 | func TestAndroidAppImport_DpiVariants(t *testing.T) { |
| 1737 | bp := ` |
| 1738 | android_app_import { |
| 1739 | name: "foo", |
| 1740 | apk: "prebuilts/apk/app.apk", |
| 1741 | dpi_variants: { |
| 1742 | xhdpi: { |
| 1743 | apk: "prebuilts/apk/app_xhdpi.apk", |
| 1744 | }, |
| 1745 | xxhdpi: { |
| 1746 | apk: "prebuilts/apk/app_xxhdpi.apk", |
| 1747 | }, |
| 1748 | }, |
Jaewoong Jung | 961d4fd | 2019-08-22 14:25:58 -0700 | [diff] [blame] | 1749 | presigned: true, |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1750 | dex_preopt: { |
| 1751 | enabled: true, |
| 1752 | }, |
| 1753 | } |
| 1754 | ` |
| 1755 | testCases := []struct { |
| 1756 | name string |
| 1757 | aaptPreferredConfig *string |
| 1758 | aaptPrebuiltDPI []string |
| 1759 | expected string |
| 1760 | }{ |
| 1761 | { |
| 1762 | name: "no preferred", |
| 1763 | aaptPreferredConfig: nil, |
| 1764 | aaptPrebuiltDPI: []string{}, |
| 1765 | expected: "prebuilts/apk/app.apk", |
| 1766 | }, |
| 1767 | { |
| 1768 | name: "AAPTPreferredConfig matches", |
| 1769 | aaptPreferredConfig: proptools.StringPtr("xhdpi"), |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 1770 | aaptPrebuiltDPI: []string{"xxhdpi", "ldpi"}, |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1771 | expected: "prebuilts/apk/app_xhdpi.apk", |
| 1772 | }, |
| 1773 | { |
| 1774 | name: "AAPTPrebuiltDPI matches", |
| 1775 | aaptPreferredConfig: proptools.StringPtr("mdpi"), |
| 1776 | aaptPrebuiltDPI: []string{"xxhdpi", "xhdpi"}, |
| 1777 | expected: "prebuilts/apk/app_xxhdpi.apk", |
| 1778 | }, |
| 1779 | { |
| 1780 | name: "non-first AAPTPrebuiltDPI matches", |
| 1781 | aaptPreferredConfig: proptools.StringPtr("mdpi"), |
| 1782 | aaptPrebuiltDPI: []string{"ldpi", "xhdpi"}, |
| 1783 | expected: "prebuilts/apk/app_xhdpi.apk", |
| 1784 | }, |
| 1785 | { |
| 1786 | name: "no matches", |
| 1787 | aaptPreferredConfig: proptools.StringPtr("mdpi"), |
| 1788 | aaptPrebuiltDPI: []string{"ldpi", "xxxhdpi"}, |
| 1789 | expected: "prebuilts/apk/app.apk", |
| 1790 | }, |
| 1791 | } |
| 1792 | |
| 1793 | jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)") |
| 1794 | for _, test := range testCases { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1795 | config := testAppConfig(nil, bp, nil) |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1796 | config.TestProductVariables.AAPTPreferredConfig = test.aaptPreferredConfig |
| 1797 | config.TestProductVariables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1798 | ctx := testContext() |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1799 | |
| 1800 | run(t, ctx, config) |
| 1801 | |
| 1802 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1803 | jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command |
| 1804 | matches := jniRuleRe.FindStringSubmatch(jniRuleCommand) |
| 1805 | if len(matches) != 2 { |
| 1806 | t.Errorf("failed to extract the src apk path from %q", jniRuleCommand) |
| 1807 | } |
| 1808 | if test.expected != matches[1] { |
| 1809 | t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1]) |
| 1810 | } |
| 1811 | } |
| 1812 | } |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1813 | |
Jaewoong Jung | 8aae22e | 2019-07-17 10:21:49 -0700 | [diff] [blame] | 1814 | func TestAndroidAppImport_Filename(t *testing.T) { |
| 1815 | ctx, config := testJava(t, ` |
| 1816 | android_app_import { |
| 1817 | name: "foo", |
| 1818 | apk: "prebuilts/apk/app.apk", |
| 1819 | presigned: true, |
| 1820 | } |
| 1821 | |
| 1822 | android_app_import { |
| 1823 | name: "bar", |
| 1824 | apk: "prebuilts/apk/app.apk", |
| 1825 | presigned: true, |
| 1826 | filename: "bar_sample.apk" |
| 1827 | } |
| 1828 | `) |
| 1829 | |
| 1830 | testCases := []struct { |
| 1831 | name string |
| 1832 | expected string |
| 1833 | }{ |
| 1834 | { |
| 1835 | name: "foo", |
| 1836 | expected: "foo.apk", |
| 1837 | }, |
| 1838 | { |
| 1839 | name: "bar", |
| 1840 | expected: "bar_sample.apk", |
| 1841 | }, |
| 1842 | } |
| 1843 | |
| 1844 | for _, test := range testCases { |
| 1845 | variant := ctx.ModuleForTests(test.name, "android_common") |
| 1846 | if variant.MaybeOutput(test.expected).Rule == nil { |
| 1847 | t.Errorf("can't find output named %q - all outputs: %v", test.expected, variant.AllOutputs()) |
| 1848 | } |
| 1849 | |
| 1850 | a := variant.Module().(*AndroidAppImport) |
| 1851 | expectedValues := []string{test.expected} |
| 1852 | actualValues := android.AndroidMkEntriesForTest( |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 1853 | t, config, "", a)[0].EntryMap["LOCAL_INSTALLED_MODULE_STEM"] |
Jaewoong Jung | 8aae22e | 2019-07-17 10:21:49 -0700 | [diff] [blame] | 1854 | if !reflect.DeepEqual(actualValues, expectedValues) { |
| 1855 | t.Errorf("Incorrect LOCAL_INSTALLED_MODULE_STEM value '%s', expected '%s'", |
| 1856 | actualValues, expectedValues) |
| 1857 | } |
| 1858 | } |
| 1859 | } |
| 1860 | |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 1861 | func TestAndroidAppImport_ArchVariants(t *testing.T) { |
| 1862 | // The test config's target arch is ARM64. |
| 1863 | testCases := []struct { |
| 1864 | name string |
| 1865 | bp string |
| 1866 | expected string |
| 1867 | }{ |
| 1868 | { |
| 1869 | name: "matching arch", |
| 1870 | bp: ` |
| 1871 | android_app_import { |
| 1872 | name: "foo", |
| 1873 | apk: "prebuilts/apk/app.apk", |
| 1874 | arch: { |
| 1875 | arm64: { |
| 1876 | apk: "prebuilts/apk/app_arm64.apk", |
| 1877 | }, |
| 1878 | }, |
Jaewoong Jung | 961d4fd | 2019-08-22 14:25:58 -0700 | [diff] [blame] | 1879 | presigned: true, |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 1880 | dex_preopt: { |
| 1881 | enabled: true, |
| 1882 | }, |
| 1883 | } |
| 1884 | `, |
| 1885 | expected: "prebuilts/apk/app_arm64.apk", |
| 1886 | }, |
| 1887 | { |
| 1888 | name: "no matching arch", |
| 1889 | bp: ` |
| 1890 | android_app_import { |
| 1891 | name: "foo", |
| 1892 | apk: "prebuilts/apk/app.apk", |
| 1893 | arch: { |
| 1894 | arm: { |
| 1895 | apk: "prebuilts/apk/app_arm.apk", |
| 1896 | }, |
| 1897 | }, |
Jaewoong Jung | 961d4fd | 2019-08-22 14:25:58 -0700 | [diff] [blame] | 1898 | presigned: true, |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 1899 | dex_preopt: { |
| 1900 | enabled: true, |
| 1901 | }, |
| 1902 | } |
| 1903 | `, |
| 1904 | expected: "prebuilts/apk/app.apk", |
| 1905 | }, |
| 1906 | } |
| 1907 | |
| 1908 | jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)") |
| 1909 | for _, test := range testCases { |
| 1910 | ctx, _ := testJava(t, test.bp) |
| 1911 | |
| 1912 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1913 | jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command |
| 1914 | matches := jniRuleRe.FindStringSubmatch(jniRuleCommand) |
| 1915 | if len(matches) != 2 { |
| 1916 | t.Errorf("failed to extract the src apk path from %q", jniRuleCommand) |
| 1917 | } |
| 1918 | if test.expected != matches[1] { |
| 1919 | t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1]) |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | |
Jaewoong Jung | b28eb5f | 2019-08-27 15:01:50 -0700 | [diff] [blame] | 1924 | func TestAndroidTestImport(t *testing.T) { |
| 1925 | ctx, config := testJava(t, ` |
| 1926 | android_test_import { |
| 1927 | name: "foo", |
| 1928 | apk: "prebuilts/apk/app.apk", |
| 1929 | presigned: true, |
| 1930 | data: [ |
| 1931 | "testdata/data", |
| 1932 | ], |
| 1933 | } |
| 1934 | `) |
| 1935 | |
| 1936 | test := ctx.ModuleForTests("foo", "android_common").Module().(*AndroidTestImport) |
| 1937 | |
| 1938 | // Check android mks. |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 1939 | entries := android.AndroidMkEntriesForTest(t, config, "", test)[0] |
Jaewoong Jung | b28eb5f | 2019-08-27 15:01:50 -0700 | [diff] [blame] | 1940 | expected := []string{"tests"} |
| 1941 | actual := entries.EntryMap["LOCAL_MODULE_TAGS"] |
| 1942 | if !reflect.DeepEqual(expected, actual) { |
| 1943 | t.Errorf("Unexpected module tags - expected: %q, actual: %q", expected, actual) |
| 1944 | } |
| 1945 | expected = []string{"testdata/data:testdata/data"} |
| 1946 | actual = entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"] |
| 1947 | if !reflect.DeepEqual(expected, actual) { |
| 1948 | t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual) |
| 1949 | } |
| 1950 | } |
| 1951 | |
Jaewoong Jung | 7c5bd83 | 2020-01-13 09:55:39 -0800 | [diff] [blame] | 1952 | func TestAndroidTestImport_NoJinUncompressForPresigned(t *testing.T) { |
| 1953 | ctx, _ := testJava(t, ` |
| 1954 | android_test_import { |
| 1955 | name: "foo", |
| 1956 | apk: "prebuilts/apk/app.apk", |
| 1957 | certificate: "cert/new_cert", |
| 1958 | data: [ |
| 1959 | "testdata/data", |
| 1960 | ], |
| 1961 | } |
| 1962 | |
| 1963 | android_test_import { |
| 1964 | name: "foo_presigned", |
| 1965 | apk: "prebuilts/apk/app.apk", |
| 1966 | presigned: true, |
| 1967 | data: [ |
| 1968 | "testdata/data", |
| 1969 | ], |
| 1970 | } |
| 1971 | `) |
| 1972 | |
| 1973 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1974 | jniRule := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command |
| 1975 | if !strings.HasPrefix(jniRule, "if (zipinfo") { |
| 1976 | t.Errorf("Unexpected JNI uncompress rule command: " + jniRule) |
| 1977 | } |
| 1978 | |
| 1979 | variant = ctx.ModuleForTests("foo_presigned", "android_common") |
| 1980 | jniRule = variant.Output("jnis-uncompressed/foo_presigned.apk").BuildParams.Rule.String() |
| 1981 | if jniRule != android.Cp.String() { |
| 1982 | t.Errorf("Unexpected JNI uncompress rule: " + jniRule) |
| 1983 | } |
| 1984 | } |
| 1985 | |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1986 | func TestStl(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1987 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1988 | cc_library { |
| 1989 | name: "libjni", |
Peter Collingbourne | ad84f97 | 2019-12-17 16:46:18 -0800 | [diff] [blame] | 1990 | sdk_version: "current", |
| 1991 | stl: "c++_shared", |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | android_test { |
| 1995 | name: "stl", |
| 1996 | jni_libs: ["libjni"], |
| 1997 | compile_multilib: "both", |
| 1998 | sdk_version: "current", |
| 1999 | stl: "c++_shared", |
| 2000 | } |
| 2001 | |
| 2002 | android_test { |
| 2003 | name: "system", |
| 2004 | jni_libs: ["libjni"], |
| 2005 | compile_multilib: "both", |
| 2006 | sdk_version: "current", |
| 2007 | } |
| 2008 | `) |
| 2009 | |
| 2010 | testCases := []struct { |
| 2011 | name string |
| 2012 | jnis []string |
| 2013 | }{ |
| 2014 | {"stl", |
| 2015 | []string{ |
| 2016 | "libjni.so", |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 2017 | "libc++_shared.so", |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 2018 | }, |
| 2019 | }, |
| 2020 | {"system", |
| 2021 | []string{ |
| 2022 | "libjni.so", |
| 2023 | }, |
| 2024 | }, |
| 2025 | } |
| 2026 | |
| 2027 | for _, test := range testCases { |
| 2028 | t.Run(test.name, func(t *testing.T) { |
| 2029 | app := ctx.ModuleForTests(test.name, "android_common") |
| 2030 | jniLibZip := app.Output("jnilibs.zip") |
| 2031 | var jnis []string |
| 2032 | args := strings.Fields(jniLibZip.Args["jarArgs"]) |
| 2033 | for i := 0; i < len(args); i++ { |
| 2034 | if args[i] == "-f" { |
| 2035 | jnis = append(jnis, args[i+1]) |
| 2036 | i += 1 |
| 2037 | } |
| 2038 | } |
| 2039 | jnisJoined := strings.Join(jnis, " ") |
| 2040 | for _, jni := range test.jnis { |
| 2041 | if !strings.Contains(jnisJoined, jni) { |
| 2042 | t.Errorf("missing jni %q in %q", jni, jnis) |
| 2043 | } |
| 2044 | } |
| 2045 | }) |
| 2046 | } |
| 2047 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2048 | |
| 2049 | func TestUsesLibraries(t *testing.T) { |
| 2050 | bp := ` |
| 2051 | java_sdk_library { |
| 2052 | name: "foo", |
| 2053 | srcs: ["a.java"], |
| 2054 | api_packages: ["foo"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2055 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | java_sdk_library { |
| 2059 | name: "bar", |
| 2060 | srcs: ["a.java"], |
| 2061 | api_packages: ["bar"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2062 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | android_app { |
| 2066 | name: "app", |
| 2067 | srcs: ["a.java"], |
| 2068 | uses_libs: ["foo"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2069 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2070 | optional_uses_libs: [ |
| 2071 | "bar", |
| 2072 | "baz", |
| 2073 | ], |
| 2074 | } |
| 2075 | |
| 2076 | android_app_import { |
| 2077 | name: "prebuilt", |
| 2078 | apk: "prebuilts/apk/app.apk", |
| 2079 | certificate: "platform", |
| 2080 | uses_libs: ["foo"], |
| 2081 | optional_uses_libs: [ |
| 2082 | "bar", |
| 2083 | "baz", |
| 2084 | ], |
| 2085 | } |
| 2086 | ` |
| 2087 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 2088 | config := testAppConfig(nil, bp, nil) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2089 | config.TestProductVariables.MissingUsesLibraries = []string{"baz"} |
| 2090 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 2091 | ctx := testContext() |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2092 | |
| 2093 | run(t, ctx, config) |
| 2094 | |
| 2095 | app := ctx.ModuleForTests("app", "android_common") |
| 2096 | prebuilt := ctx.ModuleForTests("prebuilt", "android_common") |
| 2097 | |
| 2098 | // Test that all libraries are verified |
| 2099 | cmd := app.Rule("verify_uses_libraries").RuleParams.Command |
| 2100 | if w := "--uses-library foo"; !strings.Contains(cmd, w) { |
| 2101 | t.Errorf("wanted %q in %q", w, cmd) |
| 2102 | } |
| 2103 | |
| 2104 | if w := "--optional-uses-library bar --optional-uses-library baz"; !strings.Contains(cmd, w) { |
| 2105 | t.Errorf("wanted %q in %q", w, cmd) |
| 2106 | } |
| 2107 | |
| 2108 | cmd = prebuilt.Rule("verify_uses_libraries").RuleParams.Command |
| 2109 | |
| 2110 | if w := `uses_library_names="foo"`; !strings.Contains(cmd, w) { |
| 2111 | t.Errorf("wanted %q in %q", w, cmd) |
| 2112 | } |
| 2113 | |
| 2114 | if w := `optional_uses_library_names="bar baz"`; !strings.Contains(cmd, w) { |
| 2115 | t.Errorf("wanted %q in %q", w, cmd) |
| 2116 | } |
| 2117 | |
| 2118 | // Test that only present libraries are preopted |
| 2119 | cmd = app.Rule("dexpreopt").RuleParams.Command |
| 2120 | |
| 2121 | if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) { |
| 2122 | t.Errorf("wanted %q in %q", w, cmd) |
| 2123 | } |
| 2124 | |
| 2125 | cmd = prebuilt.Rule("dexpreopt").RuleParams.Command |
| 2126 | |
| 2127 | if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) { |
| 2128 | t.Errorf("wanted %q in %q", w, cmd) |
| 2129 | } |
| 2130 | } |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2131 | |
| 2132 | func TestCodelessApp(t *testing.T) { |
| 2133 | testCases := []struct { |
| 2134 | name string |
| 2135 | bp string |
| 2136 | noCode bool |
| 2137 | }{ |
| 2138 | { |
| 2139 | name: "normal", |
| 2140 | bp: ` |
| 2141 | android_app { |
| 2142 | name: "foo", |
| 2143 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2144 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2145 | } |
| 2146 | `, |
| 2147 | noCode: false, |
| 2148 | }, |
| 2149 | { |
| 2150 | name: "app without sources", |
| 2151 | bp: ` |
| 2152 | android_app { |
| 2153 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2154 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2155 | } |
| 2156 | `, |
| 2157 | noCode: true, |
| 2158 | }, |
| 2159 | { |
| 2160 | name: "app with libraries", |
| 2161 | bp: ` |
| 2162 | android_app { |
| 2163 | name: "foo", |
| 2164 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2165 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | java_library { |
| 2169 | name: "lib", |
| 2170 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2171 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2172 | } |
| 2173 | `, |
| 2174 | noCode: false, |
| 2175 | }, |
| 2176 | { |
| 2177 | name: "app with sourceless libraries", |
| 2178 | bp: ` |
| 2179 | android_app { |
| 2180 | name: "foo", |
| 2181 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2182 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | java_library { |
| 2186 | name: "lib", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2187 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2188 | } |
| 2189 | `, |
| 2190 | // TODO(jungjw): this should probably be true |
| 2191 | noCode: false, |
| 2192 | }, |
| 2193 | } |
| 2194 | |
| 2195 | for _, test := range testCases { |
| 2196 | t.Run(test.name, func(t *testing.T) { |
| 2197 | ctx := testApp(t, test.bp) |
| 2198 | |
| 2199 | foo := ctx.ModuleForTests("foo", "android_common") |
| 2200 | manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"] |
| 2201 | if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode { |
| 2202 | t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs) |
| 2203 | } |
| 2204 | }) |
| 2205 | } |
| 2206 | } |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2207 | |
| 2208 | func TestEmbedNotice(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 2209 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2210 | android_app { |
| 2211 | name: "foo", |
| 2212 | srcs: ["a.java"], |
| 2213 | static_libs: ["javalib"], |
| 2214 | jni_libs: ["libjni"], |
| 2215 | notice: "APP_NOTICE", |
| 2216 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2217 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | // No embed_notice flag |
| 2221 | android_app { |
| 2222 | name: "bar", |
| 2223 | srcs: ["a.java"], |
| 2224 | jni_libs: ["libjni"], |
| 2225 | notice: "APP_NOTICE", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2226 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | // No NOTICE files |
| 2230 | android_app { |
| 2231 | name: "baz", |
| 2232 | srcs: ["a.java"], |
| 2233 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2234 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | cc_library { |
| 2238 | name: "libjni", |
| 2239 | system_shared_libs: [], |
| 2240 | stl: "none", |
| 2241 | notice: "LIB_NOTICE", |
Colin Cross | 094cde4 | 2020-02-15 10:38:00 -0800 | [diff] [blame] | 2242 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | java_library { |
| 2246 | name: "javalib", |
| 2247 | srcs: [ |
| 2248 | ":gen", |
| 2249 | ], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2250 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2251 | } |
| 2252 | |
| 2253 | genrule { |
| 2254 | name: "gen", |
| 2255 | tools: ["gentool"], |
| 2256 | out: ["gen.java"], |
| 2257 | notice: "GENRULE_NOTICE", |
| 2258 | } |
| 2259 | |
| 2260 | java_binary_host { |
| 2261 | name: "gentool", |
| 2262 | srcs: ["b.java"], |
| 2263 | notice: "TOOL_NOTICE", |
| 2264 | } |
| 2265 | `) |
| 2266 | |
| 2267 | // foo has NOTICE files to process, and embed_notices is true. |
| 2268 | foo := ctx.ModuleForTests("foo", "android_common") |
| 2269 | // verify merge notices rule. |
| 2270 | mergeNotices := foo.Rule("mergeNoticesRule") |
| 2271 | noticeInputs := mergeNotices.Inputs.Strings() |
| 2272 | // TOOL_NOTICE should be excluded as it's a host module. |
| 2273 | if len(mergeNotices.Inputs) != 3 { |
| 2274 | t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs) |
| 2275 | } |
| 2276 | if !inList("APP_NOTICE", noticeInputs) { |
| 2277 | t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs) |
| 2278 | } |
| 2279 | if !inList("LIB_NOTICE", noticeInputs) { |
| 2280 | t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs) |
| 2281 | } |
| 2282 | if !inList("GENRULE_NOTICE", noticeInputs) { |
| 2283 | t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs) |
| 2284 | } |
| 2285 | // aapt2 flags should include -A <NOTICE dir> so that its contents are put in the APK's /assets. |
| 2286 | res := foo.Output("package-res.apk") |
| 2287 | aapt2Flags := res.Args["flags"] |
| 2288 | e := "-A " + buildDir + "/.intermediates/foo/android_common/NOTICE" |
| 2289 | if !strings.Contains(aapt2Flags, e) { |
| 2290 | t.Errorf("asset dir flag for NOTICE, %q is missing in aapt2 link flags, %q", e, aapt2Flags) |
| 2291 | } |
| 2292 | |
| 2293 | // bar has NOTICE files to process, but embed_notices is not set. |
| 2294 | bar := ctx.ModuleForTests("bar", "android_common") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 2295 | res = bar.Output("package-res.apk") |
| 2296 | aapt2Flags = res.Args["flags"] |
| 2297 | e = "-A " + buildDir + "/.intermediates/bar/android_common/NOTICE" |
| 2298 | if strings.Contains(aapt2Flags, e) { |
| 2299 | t.Errorf("bar shouldn't have the asset dir flag for NOTICE: %q", e) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | // baz's embed_notice is true, but it doesn't have any NOTICE files. |
| 2303 | baz := ctx.ModuleForTests("baz", "android_common") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 2304 | res = baz.Output("package-res.apk") |
| 2305 | aapt2Flags = res.Args["flags"] |
| 2306 | e = "-A " + buildDir + "/.intermediates/baz/android_common/NOTICE" |
| 2307 | if strings.Contains(aapt2Flags, e) { |
| 2308 | t.Errorf("baz shouldn't have the asset dir flag for NOTICE: %q", e) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2309 | } |
| 2310 | } |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2311 | |
| 2312 | func TestUncompressDex(t *testing.T) { |
| 2313 | testCases := []struct { |
| 2314 | name string |
| 2315 | bp string |
| 2316 | |
| 2317 | uncompressedPlatform bool |
| 2318 | uncompressedUnbundled bool |
| 2319 | }{ |
| 2320 | { |
| 2321 | name: "normal", |
| 2322 | bp: ` |
| 2323 | android_app { |
| 2324 | name: "foo", |
| 2325 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2326 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2327 | } |
| 2328 | `, |
| 2329 | uncompressedPlatform: true, |
| 2330 | uncompressedUnbundled: false, |
| 2331 | }, |
| 2332 | { |
| 2333 | name: "use_embedded_dex", |
| 2334 | bp: ` |
| 2335 | android_app { |
| 2336 | name: "foo", |
| 2337 | use_embedded_dex: true, |
| 2338 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2339 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2340 | } |
| 2341 | `, |
| 2342 | uncompressedPlatform: true, |
| 2343 | uncompressedUnbundled: true, |
| 2344 | }, |
| 2345 | { |
| 2346 | name: "privileged", |
| 2347 | bp: ` |
| 2348 | android_app { |
| 2349 | name: "foo", |
| 2350 | privileged: true, |
| 2351 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2352 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2353 | } |
| 2354 | `, |
| 2355 | uncompressedPlatform: true, |
| 2356 | uncompressedUnbundled: true, |
| 2357 | }, |
| 2358 | } |
| 2359 | |
| 2360 | test := func(t *testing.T, bp string, want bool, unbundled bool) { |
| 2361 | t.Helper() |
| 2362 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 2363 | config := testAppConfig(nil, bp, nil) |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2364 | if unbundled { |
| 2365 | config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true) |
| 2366 | } |
| 2367 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 2368 | ctx := testContext() |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2369 | |
| 2370 | run(t, ctx, config) |
| 2371 | |
| 2372 | foo := ctx.ModuleForTests("foo", "android_common") |
| 2373 | dex := foo.Rule("r8") |
| 2374 | uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0") |
| 2375 | aligned := foo.MaybeRule("zipalign").Rule != nil |
| 2376 | |
| 2377 | if uncompressedInDexJar != want { |
| 2378 | t.Errorf("want uncompressed in dex %v, got %v", want, uncompressedInDexJar) |
| 2379 | } |
| 2380 | |
| 2381 | if aligned != want { |
| 2382 | t.Errorf("want aligned %v, got %v", want, aligned) |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | for _, tt := range testCases { |
| 2387 | t.Run(tt.name, func(t *testing.T) { |
| 2388 | t.Run("platform", func(t *testing.T) { |
| 2389 | test(t, tt.bp, tt.uncompressedPlatform, false) |
| 2390 | }) |
| 2391 | t.Run("unbundled", func(t *testing.T) { |
| 2392 | test(t, tt.bp, tt.uncompressedUnbundled, true) |
| 2393 | }) |
| 2394 | }) |
| 2395 | } |
| 2396 | } |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2397 | |
| 2398 | func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) { |
| 2399 | if expectedValue != "" { |
| 2400 | expectedFlag := "--" + flagName + " " + expectedValue |
| 2401 | if !strings.Contains(aapt2Flags, expectedFlag) { |
| 2402 | t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags) |
| 2403 | } |
| 2404 | } else { |
| 2405 | unexpectedFlag := "--" + flagName |
| 2406 | if strings.Contains(aapt2Flags, unexpectedFlag) { |
| 2407 | t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags) |
| 2408 | } |
| 2409 | } |
| 2410 | } |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2411 | |
| 2412 | func TestRuntimeResourceOverlay(t *testing.T) { |
Jaewoong Jung | fe3c7f6 | 2020-04-09 16:15:30 -0700 | [diff] [blame] | 2413 | fs := map[string][]byte{ |
| 2414 | "baz/res/res/values/strings.xml": nil, |
| 2415 | "bar/res/res/values/strings.xml": nil, |
| 2416 | } |
| 2417 | bp := ` |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2418 | runtime_resource_overlay { |
| 2419 | name: "foo", |
| 2420 | certificate: "platform", |
| 2421 | product_specific: true, |
Jaewoong Jung | fe3c7f6 | 2020-04-09 16:15:30 -0700 | [diff] [blame] | 2422 | static_libs: ["bar"], |
| 2423 | resource_libs: ["baz"], |
Jaewoong Jung | f0f747c | 2020-01-24 10:30:02 -0800 | [diff] [blame] | 2424 | aaptflags: ["--keep-raw-values"], |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | runtime_resource_overlay { |
| 2428 | name: "foo_themed", |
| 2429 | certificate: "platform", |
| 2430 | product_specific: true, |
| 2431 | theme: "faza", |
Jaewoong Jung | ad0177b | 2020-04-24 15:22:40 -0700 | [diff] [blame] | 2432 | overrides: ["foo"], |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2433 | } |
Jaewoong Jung | fe3c7f6 | 2020-04-09 16:15:30 -0700 | [diff] [blame] | 2434 | |
| 2435 | android_library { |
| 2436 | name: "bar", |
| 2437 | resource_dirs: ["bar/res"], |
| 2438 | } |
| 2439 | |
| 2440 | android_app { |
| 2441 | name: "baz", |
| 2442 | sdk_version: "current", |
| 2443 | resource_dirs: ["baz/res"], |
| 2444 | } |
| 2445 | ` |
| 2446 | config := testAppConfig(nil, bp, fs) |
| 2447 | ctx := testContext() |
| 2448 | run(t, ctx, config) |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2449 | |
| 2450 | m := ctx.ModuleForTests("foo", "android_common") |
| 2451 | |
Jaewoong Jung | f0f747c | 2020-01-24 10:30:02 -0800 | [diff] [blame] | 2452 | // Check AAPT2 link flags. |
| 2453 | aapt2Flags := m.Output("package-res.apk").Args["flags"] |
| 2454 | expectedFlags := []string{"--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"} |
| 2455 | absentFlags := android.RemoveListFromList(expectedFlags, strings.Split(aapt2Flags, " ")) |
| 2456 | if len(absentFlags) > 0 { |
| 2457 | t.Errorf("expected values, %q are missing in aapt2 link flags, %q", absentFlags, aapt2Flags) |
| 2458 | } |
| 2459 | |
Jaewoong Jung | fe3c7f6 | 2020-04-09 16:15:30 -0700 | [diff] [blame] | 2460 | // Check overlay.list output for static_libs dependency. |
| 2461 | overlayList := m.Output("aapt2/overlay.list").Inputs.Strings() |
| 2462 | staticLibPackage := buildDir + "/.intermediates/bar/android_common/package-res.apk" |
| 2463 | if !inList(staticLibPackage, overlayList) { |
| 2464 | t.Errorf("Stactic lib res package %q missing in overlay list: %q", staticLibPackage, overlayList) |
| 2465 | } |
| 2466 | |
| 2467 | // Check AAPT2 link flags for resource_libs dependency. |
| 2468 | resourceLibFlag := "-I " + buildDir + "/.intermediates/baz/android_common/package-res.apk" |
| 2469 | if !strings.Contains(aapt2Flags, resourceLibFlag) { |
| 2470 | t.Errorf("Resource lib flag %q missing in aapt2 link flags: %q", resourceLibFlag, aapt2Flags) |
| 2471 | } |
| 2472 | |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2473 | // Check cert signing flag. |
| 2474 | signedApk := m.Output("signed/foo.apk") |
| 2475 | signingFlag := signedApk.Args["certificates"] |
| 2476 | expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8" |
| 2477 | if expected != signingFlag { |
| 2478 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag) |
| 2479 | } |
Jaewoong Jung | ad0177b | 2020-04-24 15:22:40 -0700 | [diff] [blame] | 2480 | androidMkEntries := android.AndroidMkEntriesForTest(t, config, "", m.Module())[0] |
| 2481 | path := androidMkEntries.EntryMap["LOCAL_CERTIFICATE"] |
Jaewoong Jung | 78ec5d8 | 2020-01-31 10:11:47 -0800 | [diff] [blame] | 2482 | expectedPath := []string{"build/make/target/product/security/platform.x509.pem"} |
| 2483 | if !reflect.DeepEqual(path, expectedPath) { |
| 2484 | t.Errorf("Unexpected LOCAL_CERTIFICATE value: %v, expected: %v", path, expectedPath) |
| 2485 | } |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2486 | |
| 2487 | // Check device location. |
Jaewoong Jung | ad0177b | 2020-04-24 15:22:40 -0700 | [diff] [blame] | 2488 | path = androidMkEntries.EntryMap["LOCAL_MODULE_PATH"] |
Jaewoong Jung | 78ec5d8 | 2020-01-31 10:11:47 -0800 | [diff] [blame] | 2489 | expectedPath = []string{"/tmp/target/product/test_device/product/overlay"} |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2490 | if !reflect.DeepEqual(path, expectedPath) { |
| 2491 | t.Errorf("Unexpected LOCAL_MODULE_PATH value: %v, expected: %v", path, expectedPath) |
| 2492 | } |
| 2493 | |
| 2494 | // A themed module has a different device location |
| 2495 | m = ctx.ModuleForTests("foo_themed", "android_common") |
Jaewoong Jung | ad0177b | 2020-04-24 15:22:40 -0700 | [diff] [blame] | 2496 | androidMkEntries = android.AndroidMkEntriesForTest(t, config, "", m.Module())[0] |
| 2497 | path = androidMkEntries.EntryMap["LOCAL_MODULE_PATH"] |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2498 | expectedPath = []string{"/tmp/target/product/test_device/product/overlay/faza"} |
| 2499 | if !reflect.DeepEqual(path, expectedPath) { |
| 2500 | t.Errorf("Unexpected LOCAL_MODULE_PATH value: %v, expected: %v", path, expectedPath) |
| 2501 | } |
Jaewoong Jung | ad0177b | 2020-04-24 15:22:40 -0700 | [diff] [blame] | 2502 | |
| 2503 | overrides := androidMkEntries.EntryMap["LOCAL_OVERRIDES_PACKAGES"] |
| 2504 | expectedOverrides := []string{"foo"} |
| 2505 | if !reflect.DeepEqual(overrides, expectedOverrides) { |
| 2506 | t.Errorf("Unexpected LOCAL_OVERRIDES_PACKAGES value: %v, expected: %v", overrides, expectedOverrides) |
| 2507 | } |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2508 | } |
Roshan Pius | 4df2bc7 | 2020-04-27 09:42:27 -0700 | [diff] [blame] | 2509 | |
| 2510 | func TestOverrideRuntimeResourceOverlay(t *testing.T) { |
| 2511 | ctx, _ := testJava(t, ` |
| 2512 | runtime_resource_overlay { |
| 2513 | name: "foo_overlay", |
| 2514 | certificate: "platform", |
| 2515 | product_specific: true, |
| 2516 | sdk_version: "current", |
| 2517 | } |
| 2518 | |
| 2519 | override_runtime_resource_overlay { |
| 2520 | name: "bar_overlay", |
| 2521 | base: "foo_overlay", |
| 2522 | package_name: "com.android.bar.overlay", |
| 2523 | target_package_name: "com.android.bar", |
| 2524 | } |
| 2525 | `) |
| 2526 | |
| 2527 | expectedVariants := []struct { |
| 2528 | moduleName string |
| 2529 | variantName string |
| 2530 | apkPath string |
| 2531 | overrides []string |
| 2532 | targetVariant string |
| 2533 | packageFlag string |
| 2534 | targetPackageFlag string |
| 2535 | }{ |
| 2536 | { |
| 2537 | variantName: "android_common", |
| 2538 | apkPath: "/target/product/test_device/product/overlay/foo_overlay.apk", |
| 2539 | overrides: nil, |
| 2540 | targetVariant: "android_common", |
| 2541 | packageFlag: "", |
| 2542 | targetPackageFlag: "", |
| 2543 | }, |
| 2544 | { |
| 2545 | variantName: "android_common_bar_overlay", |
| 2546 | apkPath: "/target/product/test_device/product/overlay/bar_overlay.apk", |
| 2547 | overrides: []string{"foo_overlay"}, |
| 2548 | targetVariant: "android_common_bar", |
| 2549 | packageFlag: "com.android.bar.overlay", |
| 2550 | targetPackageFlag: "com.android.bar", |
| 2551 | }, |
| 2552 | } |
| 2553 | for _, expected := range expectedVariants { |
| 2554 | variant := ctx.ModuleForTests("foo_overlay", expected.variantName) |
| 2555 | |
| 2556 | // Check the final apk name |
| 2557 | outputs := variant.AllOutputs() |
| 2558 | expectedApkPath := buildDir + expected.apkPath |
| 2559 | found := false |
| 2560 | for _, o := range outputs { |
| 2561 | if o == expectedApkPath { |
| 2562 | found = true |
| 2563 | break |
| 2564 | } |
| 2565 | } |
| 2566 | if !found { |
| 2567 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs) |
| 2568 | } |
| 2569 | |
| 2570 | // Check if the overrides field values are correctly aggregated. |
| 2571 | mod := variant.Module().(*RuntimeResourceOverlay) |
| 2572 | if !reflect.DeepEqual(expected.overrides, mod.properties.Overrides) { |
| 2573 | t.Errorf("Incorrect overrides property value, expected: %q, got: %q", |
| 2574 | expected.overrides, mod.properties.Overrides) |
| 2575 | } |
| 2576 | |
| 2577 | // Check aapt2 flags. |
| 2578 | res := variant.Output("package-res.apk") |
| 2579 | aapt2Flags := res.Args["flags"] |
| 2580 | checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag) |
| 2581 | checkAapt2LinkFlag(t, aapt2Flags, "rename-overlay-target-package", expected.targetPackageFlag) |
| 2582 | } |
| 2583 | } |
Jaewoong Jung | bf13546 | 2020-04-26 15:10:51 -0700 | [diff] [blame] | 2584 | |
| 2585 | func TestRuntimeResourceOverlay_JavaDefaults(t *testing.T) { |
| 2586 | ctx, config := testJava(t, ` |
| 2587 | java_defaults { |
| 2588 | name: "rro_defaults", |
| 2589 | theme: "default_theme", |
| 2590 | product_specific: true, |
| 2591 | aaptflags: ["--keep-raw-values"], |
| 2592 | } |
| 2593 | |
| 2594 | runtime_resource_overlay { |
| 2595 | name: "foo_with_defaults", |
| 2596 | defaults: ["rro_defaults"], |
| 2597 | } |
| 2598 | |
| 2599 | runtime_resource_overlay { |
| 2600 | name: "foo_barebones", |
| 2601 | } |
| 2602 | `) |
| 2603 | |
| 2604 | // |
| 2605 | // RRO module with defaults |
| 2606 | // |
| 2607 | m := ctx.ModuleForTests("foo_with_defaults", "android_common") |
| 2608 | |
| 2609 | // Check AAPT2 link flags. |
| 2610 | aapt2Flags := strings.Split(m.Output("package-res.apk").Args["flags"], " ") |
| 2611 | expectedFlags := []string{"--keep-raw-values", "--no-resource-deduping", "--no-resource-removal"} |
| 2612 | absentFlags := android.RemoveListFromList(expectedFlags, aapt2Flags) |
| 2613 | if len(absentFlags) > 0 { |
| 2614 | t.Errorf("expected values, %q are missing in aapt2 link flags, %q", absentFlags, aapt2Flags) |
| 2615 | } |
| 2616 | |
| 2617 | // Check device location. |
| 2618 | path := android.AndroidMkEntriesForTest(t, config, "", m.Module())[0].EntryMap["LOCAL_MODULE_PATH"] |
| 2619 | expectedPath := []string{"/tmp/target/product/test_device/product/overlay/default_theme"} |
| 2620 | if !reflect.DeepEqual(path, expectedPath) { |
| 2621 | t.Errorf("Unexpected LOCAL_MODULE_PATH value: %q, expected: %q", path, expectedPath) |
| 2622 | } |
| 2623 | |
| 2624 | // |
| 2625 | // RRO module without defaults |
| 2626 | // |
| 2627 | m = ctx.ModuleForTests("foo_barebones", "android_common") |
| 2628 | |
| 2629 | // Check AAPT2 link flags. |
| 2630 | aapt2Flags = strings.Split(m.Output("package-res.apk").Args["flags"], " ") |
| 2631 | unexpectedFlags := "--keep-raw-values" |
| 2632 | if inList(unexpectedFlags, aapt2Flags) { |
| 2633 | t.Errorf("unexpected value, %q is present in aapt2 link flags, %q", unexpectedFlags, aapt2Flags) |
| 2634 | } |
| 2635 | |
| 2636 | // Check device location. |
| 2637 | path = android.AndroidMkEntriesForTest(t, config, "", m.Module())[0].EntryMap["LOCAL_MODULE_PATH"] |
| 2638 | expectedPath = []string{"/tmp/target/product/test_device/system/overlay"} |
| 2639 | if !reflect.DeepEqual(path, expectedPath) { |
| 2640 | t.Errorf("Unexpected LOCAL_MODULE_PATH value: %v, expected: %v", path, expectedPath) |
| 2641 | } |
| 2642 | } |