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" |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 21 | "sort" |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 22 | "strings" |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 23 | "testing" |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 24 | |
| 25 | "github.com/google/blueprint/proptools" |
| 26 | |
| 27 | "android/soong/android" |
| 28 | "android/soong/cc" |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 29 | "android/soong/dexpreopt" |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 30 | "android/soong/genrule" |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 33 | // testApp runs tests using the prepareForJavaTest |
Paul Duffin | 0ed42d3 | 2021-03-13 02:19:32 +0000 | [diff] [blame] | 34 | // |
| 35 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 36 | // |
| 37 | // deprecated |
| 38 | func testApp(t *testing.T, bp string) *android.TestContext { |
| 39 | t.Helper() |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 40 | result := prepareForJavaTest.RunTestWithBp(t, bp) |
Paul Duffin | 0ed42d3 | 2021-03-13 02:19:32 +0000 | [diff] [blame] | 41 | return result.TestContext |
| 42 | } |
| 43 | |
| 44 | func TestApp(t *testing.T) { |
| 45 | resourceFiles := []string{ |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 46 | "res/layout/layout.xml", |
| 47 | "res/values/strings.xml", |
| 48 | "res/values-en-rUS/strings.xml", |
| 49 | } |
| 50 | |
Paul Duffin | 0ed42d3 | 2021-03-13 02:19:32 +0000 | [diff] [blame] | 51 | compiledResourceFiles := []string{ |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 52 | "aapt2/res/layout_layout.xml.flat", |
| 53 | "aapt2/res/values_strings.arsc.flat", |
| 54 | "aapt2/res/values-en-rUS_strings.arsc.flat", |
| 55 | } |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 56 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 57 | for _, moduleType := range []string{"android_app", "android_library"} { |
| 58 | t.Run(moduleType, func(t *testing.T) { |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 59 | result := android.GroupFixturePreparers( |
| 60 | prepareForJavaTest, |
Paul Duffin | 0ed42d3 | 2021-03-13 02:19:32 +0000 | [diff] [blame] | 61 | android.FixtureModifyMockFS(func(fs android.MockFS) { |
| 62 | for _, file := range resourceFiles { |
| 63 | fs[file] = nil |
| 64 | } |
| 65 | }), |
| 66 | ).RunTestWithBp(t, moduleType+` { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 67 | name: "foo", |
| 68 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 69 | sdk_version: "current" |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 70 | } |
| 71 | `) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 72 | |
Paul Duffin | 0ed42d3 | 2021-03-13 02:19:32 +0000 | [diff] [blame] | 73 | foo := result.ModuleForTests("foo", "android_common") |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 74 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 75 | var expectedLinkImplicits []string |
| 76 | |
| 77 | manifestFixer := foo.Output("manifest_fixer/AndroidManifest.xml") |
| 78 | expectedLinkImplicits = append(expectedLinkImplicits, manifestFixer.Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 79 | |
Paul Duffin | 0ed42d3 | 2021-03-13 02:19:32 +0000 | [diff] [blame] | 80 | frameworkRes := result.ModuleForTests("framework-res", "android_common") |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 81 | expectedLinkImplicits = append(expectedLinkImplicits, |
| 82 | frameworkRes.Output("package-res.apk").Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 83 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 84 | // Test the mapping from input files to compiled output file names |
| 85 | compile := foo.Output(compiledResourceFiles[0]) |
Paul Duffin | 0ed42d3 | 2021-03-13 02:19:32 +0000 | [diff] [blame] | 86 | android.AssertDeepEquals(t, "aapt2 compile inputs", resourceFiles, compile.Inputs.Strings()) |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 87 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 88 | compiledResourceOutputs := compile.Outputs.Strings() |
| 89 | sort.Strings(compiledResourceOutputs) |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 90 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 91 | expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 92 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 93 | list := foo.Output("aapt2/res.list") |
| 94 | expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 95 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 96 | // Check that the link rule uses |
Paul Duffin | 0ed42d3 | 2021-03-13 02:19:32 +0000 | [diff] [blame] | 97 | res := result.ModuleForTests("foo", "android_common").Output("package-res.apk") |
| 98 | android.AssertDeepEquals(t, "aapt2 link implicits", expectedLinkImplicits, res.Implicits.Strings()) |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 99 | }) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 102 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 103 | func TestAppSplits(t *testing.T) { |
| 104 | ctx := testApp(t, ` |
| 105 | android_app { |
| 106 | name: "foo", |
| 107 | srcs: ["a.java"], |
| 108 | package_splits: ["v4", "v7,hdpi"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 109 | sdk_version: "current" |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 110 | }`) |
| 111 | |
| 112 | foo := ctx.ModuleForTests("foo", "android_common") |
| 113 | |
| 114 | expectedOutputs := []string{ |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 115 | "out/soong/.intermediates/foo/android_common/foo.apk", |
| 116 | "out/soong/.intermediates/foo/android_common/foo_v4.apk", |
| 117 | "out/soong/.intermediates/foo/android_common/foo_v7_hdpi.apk", |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 118 | } |
| 119 | for _, expectedOutput := range expectedOutputs { |
| 120 | foo.Output(expectedOutput) |
| 121 | } |
| 122 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 123 | outputFiles, err := foo.Module().(*AndroidApp).OutputFiles("") |
| 124 | if err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 127 | android.AssertPathsRelativeToTopEquals(t, `OutputFiles("")`, expectedOutputs, outputFiles) |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 130 | func TestPlatformAPIs(t *testing.T) { |
| 131 | testJava(t, ` |
| 132 | android_app { |
| 133 | name: "foo", |
| 134 | srcs: ["a.java"], |
| 135 | platform_apis: true, |
| 136 | } |
| 137 | `) |
| 138 | |
| 139 | testJava(t, ` |
| 140 | android_app { |
| 141 | name: "foo", |
| 142 | srcs: ["a.java"], |
| 143 | sdk_version: "current", |
| 144 | } |
| 145 | `) |
| 146 | |
Spandan Das | 6099934 | 2021-11-16 04:15:33 +0000 | [diff] [blame] | 147 | testJavaError(t, "This module has conflicting settings. sdk_version is empty, which means that this module is build against platform APIs. However platform_apis is not set to true", ` |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 148 | android_app { |
| 149 | name: "bar", |
| 150 | srcs: ["b.java"], |
| 151 | } |
| 152 | `) |
| 153 | |
Spandan Das | 6099934 | 2021-11-16 04:15:33 +0000 | [diff] [blame] | 154 | testJavaError(t, "This module has conflicting settings. sdk_version is not empty, which means this module cannot use platform APIs. However platform_apis is set to true.", ` |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 155 | android_app { |
| 156 | name: "bar", |
| 157 | srcs: ["b.java"], |
| 158 | sdk_version: "system_current", |
| 159 | platform_apis: true, |
| 160 | } |
| 161 | `) |
| 162 | } |
| 163 | |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 164 | func TestAndroidAppLinkType(t *testing.T) { |
| 165 | testJava(t, ` |
| 166 | android_app { |
| 167 | name: "foo", |
| 168 | srcs: ["a.java"], |
| 169 | libs: ["bar"], |
| 170 | static_libs: ["baz"], |
| 171 | platform_apis: true, |
| 172 | } |
| 173 | |
| 174 | java_library { |
| 175 | name: "bar", |
| 176 | sdk_version: "current", |
| 177 | srcs: ["b.java"], |
| 178 | } |
| 179 | |
| 180 | android_library { |
| 181 | name: "baz", |
| 182 | sdk_version: "system_current", |
| 183 | srcs: ["c.java"], |
| 184 | } |
| 185 | `) |
| 186 | |
Steven Moreland | 0029898 | 2020-11-17 21:44:36 +0000 | [diff] [blame] | 187 | testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", ` |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 188 | android_app { |
| 189 | name: "foo", |
| 190 | srcs: ["a.java"], |
| 191 | libs: ["bar"], |
| 192 | sdk_version: "current", |
| 193 | static_libs: ["baz"], |
| 194 | } |
| 195 | |
| 196 | java_library { |
| 197 | name: "bar", |
| 198 | sdk_version: "current", |
| 199 | srcs: ["b.java"], |
| 200 | } |
| 201 | |
| 202 | android_library { |
| 203 | name: "baz", |
| 204 | sdk_version: "system_current", |
| 205 | srcs: ["c.java"], |
| 206 | } |
| 207 | `) |
| 208 | |
| 209 | testJava(t, ` |
| 210 | android_app { |
| 211 | name: "foo", |
| 212 | srcs: ["a.java"], |
| 213 | libs: ["bar"], |
| 214 | sdk_version: "system_current", |
| 215 | static_libs: ["baz"], |
| 216 | } |
| 217 | |
| 218 | java_library { |
| 219 | name: "bar", |
| 220 | sdk_version: "current", |
| 221 | srcs: ["b.java"], |
| 222 | } |
| 223 | |
| 224 | android_library { |
| 225 | name: "baz", |
| 226 | sdk_version: "system_current", |
| 227 | srcs: ["c.java"], |
| 228 | } |
| 229 | `) |
| 230 | |
Steven Moreland | 0029898 | 2020-11-17 21:44:36 +0000 | [diff] [blame] | 231 | testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", ` |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 232 | android_app { |
| 233 | name: "foo", |
| 234 | srcs: ["a.java"], |
| 235 | libs: ["bar"], |
| 236 | sdk_version: "system_current", |
| 237 | static_libs: ["baz"], |
| 238 | } |
| 239 | |
| 240 | java_library { |
| 241 | name: "bar", |
| 242 | sdk_version: "current", |
| 243 | srcs: ["b.java"], |
| 244 | } |
| 245 | |
| 246 | android_library { |
| 247 | name: "baz", |
| 248 | srcs: ["c.java"], |
| 249 | } |
| 250 | `) |
| 251 | } |
| 252 | |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 253 | func TestUpdatableApps(t *testing.T) { |
| 254 | testCases := []struct { |
| 255 | name string |
| 256 | bp string |
| 257 | expectedError string |
| 258 | }{ |
| 259 | { |
| 260 | name: "Stable public SDK", |
| 261 | bp: `android_app { |
| 262 | name: "foo", |
| 263 | srcs: ["a.java"], |
| 264 | sdk_version: "29", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 265 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 266 | updatable: true, |
| 267 | }`, |
| 268 | }, |
| 269 | { |
| 270 | name: "Stable system SDK", |
| 271 | bp: `android_app { |
| 272 | name: "foo", |
| 273 | srcs: ["a.java"], |
| 274 | sdk_version: "system_29", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 275 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 276 | updatable: true, |
| 277 | }`, |
| 278 | }, |
| 279 | { |
| 280 | name: "Current public SDK", |
| 281 | bp: `android_app { |
| 282 | name: "foo", |
| 283 | srcs: ["a.java"], |
| 284 | sdk_version: "current", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 285 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 286 | updatable: true, |
| 287 | }`, |
| 288 | }, |
| 289 | { |
| 290 | name: "Current system SDK", |
| 291 | bp: `android_app { |
| 292 | name: "foo", |
| 293 | srcs: ["a.java"], |
| 294 | sdk_version: "system_current", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 295 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 296 | updatable: true, |
| 297 | }`, |
| 298 | }, |
| 299 | { |
| 300 | name: "Current module SDK", |
| 301 | bp: `android_app { |
| 302 | name: "foo", |
| 303 | srcs: ["a.java"], |
| 304 | sdk_version: "module_current", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 305 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 306 | updatable: true, |
| 307 | }`, |
| 308 | }, |
| 309 | { |
| 310 | name: "Current core SDK", |
| 311 | bp: `android_app { |
| 312 | name: "foo", |
| 313 | srcs: ["a.java"], |
| 314 | sdk_version: "core_current", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 315 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 316 | updatable: true, |
| 317 | }`, |
| 318 | }, |
| 319 | { |
| 320 | name: "No Platform APIs", |
| 321 | bp: `android_app { |
| 322 | name: "foo", |
| 323 | srcs: ["a.java"], |
| 324 | platform_apis: true, |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 325 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 326 | updatable: true, |
| 327 | }`, |
| 328 | expectedError: "Updatable apps must use stable SDKs", |
| 329 | }, |
| 330 | { |
| 331 | name: "No Core Platform APIs", |
| 332 | bp: `android_app { |
| 333 | name: "foo", |
| 334 | srcs: ["a.java"], |
| 335 | sdk_version: "core_platform", |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 336 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 337 | updatable: true, |
| 338 | }`, |
| 339 | expectedError: "Updatable apps must use stable SDKs", |
| 340 | }, |
| 341 | { |
| 342 | name: "No unspecified APIs", |
| 343 | bp: `android_app { |
| 344 | name: "foo", |
| 345 | srcs: ["a.java"], |
| 346 | updatable: true, |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 347 | min_sdk_version: "29", |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 348 | }`, |
| 349 | expectedError: "Updatable apps must use stable SDK", |
| 350 | }, |
Artur Satayev | f40fc85 | 2020-04-16 13:43:02 +0100 | [diff] [blame] | 351 | { |
| 352 | name: "Must specify min_sdk_version", |
| 353 | bp: `android_app { |
| 354 | name: "app_without_min_sdk_version", |
| 355 | srcs: ["a.java"], |
| 356 | sdk_version: "29", |
| 357 | updatable: true, |
| 358 | }`, |
| 359 | expectedError: "updatable apps must set min_sdk_version.", |
| 360 | }, |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | for _, test := range testCases { |
| 364 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 365 | errorHandler := android.FixtureExpectsNoErrors |
| 366 | if test.expectedError != "" { |
| 367 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError) |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 368 | } |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 369 | android.GroupFixturePreparers( |
| 370 | prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{ |
Paul Duffin | 2645a29 | 2021-03-13 02:36:00 +0000 | [diff] [blame] | 371 | "29": {"foo"}, |
| 372 | })). |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 373 | ExtendWithErrorHandler(errorHandler).RunTestWithBp(t, test.bp) |
Artur Satayev | 2db1c3f | 2020-04-08 19:09:30 +0100 | [diff] [blame] | 374 | }) |
| 375 | } |
| 376 | } |
| 377 | |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 378 | func TestUpdatableApps_TransitiveDepsShouldSetMinSdkVersion(t *testing.T) { |
| 379 | testJavaError(t, `module "bar".*: should support min_sdk_version\(29\)`, cc.GatherRequiredDepsForTest(android.Android)+` |
| 380 | android_app { |
| 381 | name: "foo", |
| 382 | srcs: ["a.java"], |
| 383 | updatable: true, |
| 384 | sdk_version: "current", |
| 385 | min_sdk_version: "29", |
| 386 | static_libs: ["bar"], |
| 387 | } |
| 388 | |
| 389 | java_library { |
| 390 | name: "bar", |
| 391 | sdk_version: "current", |
| 392 | } |
| 393 | `) |
| 394 | } |
| 395 | |
Jooyung Han | bbc3fb7 | 2020-04-29 14:01:06 +0900 | [diff] [blame] | 396 | func TestUpdatableApps_JniLibsShouldShouldSupportMinSdkVersion(t *testing.T) { |
| 397 | testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
| 398 | android_app { |
| 399 | name: "foo", |
| 400 | srcs: ["a.java"], |
| 401 | updatable: true, |
| 402 | sdk_version: "current", |
| 403 | min_sdk_version: "current", |
| 404 | jni_libs: ["libjni"], |
| 405 | } |
| 406 | |
| 407 | cc_library { |
| 408 | name: "libjni", |
| 409 | stl: "none", |
| 410 | system_shared_libs: [], |
| 411 | sdk_version: "current", |
| 412 | } |
| 413 | `) |
| 414 | } |
| 415 | |
| 416 | func TestUpdatableApps_JniLibShouldBeBuiltAgainstMinSdkVersion(t *testing.T) { |
| 417 | bp := cc.GatherRequiredDepsForTest(android.Android) + ` |
| 418 | android_app { |
| 419 | name: "foo", |
| 420 | srcs: ["a.java"], |
| 421 | updatable: true, |
| 422 | sdk_version: "current", |
| 423 | min_sdk_version: "29", |
| 424 | jni_libs: ["libjni"], |
| 425 | } |
| 426 | |
| 427 | cc_library { |
| 428 | name: "libjni", |
| 429 | stl: "none", |
| 430 | system_shared_libs: [], |
| 431 | sdk_version: "29", |
| 432 | } |
Jooyung Han | bbc3fb7 | 2020-04-29 14:01:06 +0900 | [diff] [blame] | 433 | ` |
| 434 | fs := map[string][]byte{ |
| 435 | "prebuilts/ndk/current/platforms/android-29/arch-arm64/usr/lib/crtbegin_so.o": nil, |
| 436 | "prebuilts/ndk/current/platforms/android-29/arch-arm64/usr/lib/crtend_so.o": nil, |
| 437 | "prebuilts/ndk/current/platforms/android-29/arch-arm/usr/lib/crtbegin_so.o": nil, |
| 438 | "prebuilts/ndk/current/platforms/android-29/arch-arm/usr/lib/crtend_so.o": nil, |
| 439 | } |
| 440 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 441 | ctx, _ := testJavaWithFS(t, bp, fs) |
Jooyung Han | bbc3fb7 | 2020-04-29 14:01:06 +0900 | [diff] [blame] | 442 | |
| 443 | inputs := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_sdk_shared").Description("link").Implicits |
| 444 | var crtbeginFound, crtendFound bool |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 445 | expectedCrtBegin := ctx.ModuleForTests("crtbegin_so", |
| 446 | "android_arm64_armv8-a_sdk_29").Rule("partialLd").Output |
| 447 | expectedCrtEnd := ctx.ModuleForTests("crtend_so", |
| 448 | "android_arm64_armv8-a_sdk_29").Rule("partialLd").Output |
| 449 | implicits := []string{} |
Jooyung Han | bbc3fb7 | 2020-04-29 14:01:06 +0900 | [diff] [blame] | 450 | for _, input := range inputs { |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 451 | implicits = append(implicits, input.String()) |
| 452 | if strings.HasSuffix(input.String(), expectedCrtBegin.String()) { |
Jooyung Han | bbc3fb7 | 2020-04-29 14:01:06 +0900 | [diff] [blame] | 453 | crtbeginFound = true |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 454 | } else if strings.HasSuffix(input.String(), expectedCrtEnd.String()) { |
Jooyung Han | bbc3fb7 | 2020-04-29 14:01:06 +0900 | [diff] [blame] | 455 | crtendFound = true |
| 456 | } |
| 457 | } |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 458 | if !crtbeginFound { |
| 459 | t.Error(fmt.Sprintf( |
| 460 | "expected implicit with suffix %q, have the following implicits:\n%s", |
| 461 | expectedCrtBegin, strings.Join(implicits, "\n"))) |
| 462 | } |
| 463 | if !crtendFound { |
| 464 | t.Error(fmt.Sprintf( |
| 465 | "expected implicit with suffix %q, have the following implicits:\n%s", |
| 466 | expectedCrtEnd, strings.Join(implicits, "\n"))) |
Jooyung Han | bbc3fb7 | 2020-04-29 14:01:06 +0900 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
| 470 | func TestUpdatableApps_ErrorIfJniLibDoesntSupportMinSdkVersion(t *testing.T) { |
| 471 | bp := cc.GatherRequiredDepsForTest(android.Android) + ` |
| 472 | android_app { |
| 473 | name: "foo", |
| 474 | srcs: ["a.java"], |
| 475 | updatable: true, |
| 476 | sdk_version: "current", |
| 477 | min_sdk_version: "29", // this APK should support 29 |
| 478 | jni_libs: ["libjni"], |
| 479 | } |
| 480 | |
| 481 | cc_library { |
| 482 | name: "libjni", |
| 483 | stl: "none", |
| 484 | sdk_version: "current", |
| 485 | } |
| 486 | ` |
| 487 | testJavaError(t, `"libjni" .*: sdk_version\(current\) is higher than min_sdk_version\(29\)`, bp) |
| 488 | } |
| 489 | |
| 490 | func TestUpdatableApps_ErrorIfDepSdkVersionIsHigher(t *testing.T) { |
| 491 | bp := cc.GatherRequiredDepsForTest(android.Android) + ` |
| 492 | android_app { |
| 493 | name: "foo", |
| 494 | srcs: ["a.java"], |
| 495 | updatable: true, |
| 496 | sdk_version: "current", |
| 497 | min_sdk_version: "29", // this APK should support 29 |
| 498 | jni_libs: ["libjni"], |
| 499 | } |
| 500 | |
| 501 | cc_library { |
| 502 | name: "libjni", |
| 503 | stl: "none", |
| 504 | shared_libs: ["libbar"], |
| 505 | system_shared_libs: [], |
| 506 | sdk_version: "27", |
| 507 | } |
| 508 | |
| 509 | cc_library { |
| 510 | name: "libbar", |
| 511 | stl: "none", |
| 512 | system_shared_libs: [], |
| 513 | sdk_version: "current", |
| 514 | } |
| 515 | ` |
| 516 | testJavaError(t, `"libjni" .*: links "libbar" built against newer API version "current"`, bp) |
| 517 | } |
| 518 | |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 519 | func TestResourceDirs(t *testing.T) { |
| 520 | testCases := []struct { |
| 521 | name string |
| 522 | prop string |
| 523 | resources []string |
| 524 | }{ |
| 525 | { |
| 526 | name: "no resource_dirs", |
| 527 | prop: "", |
| 528 | resources: []string{"res/res/values/strings.xml"}, |
| 529 | }, |
| 530 | { |
| 531 | name: "resource_dirs", |
| 532 | prop: `resource_dirs: ["res"]`, |
| 533 | resources: []string{"res/res/values/strings.xml"}, |
| 534 | }, |
| 535 | { |
| 536 | name: "empty resource_dirs", |
| 537 | prop: `resource_dirs: []`, |
| 538 | resources: nil, |
| 539 | }, |
| 540 | } |
| 541 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 542 | fs := android.MockFS{ |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 543 | "res/res/values/strings.xml": nil, |
| 544 | } |
| 545 | |
| 546 | bp := ` |
| 547 | android_app { |
| 548 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 549 | sdk_version: "current", |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 550 | %s |
| 551 | } |
| 552 | ` |
| 553 | |
| 554 | for _, testCase := range testCases { |
| 555 | t.Run(testCase.name, func(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 556 | result := android.GroupFixturePreparers( |
| 557 | PrepareForTestWithJavaDefaultModules, |
| 558 | PrepareForTestWithOverlayBuildComponents, |
| 559 | fs.AddToFixture(), |
| 560 | ).RunTestWithBp(t, fmt.Sprintf(bp, testCase.prop)) |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 561 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 562 | module := result.ModuleForTests("foo", "android_common") |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 563 | resourceList := module.MaybeOutput("aapt2/res.list") |
| 564 | |
| 565 | var resources []string |
| 566 | if resourceList.Rule != nil { |
| 567 | for _, compiledResource := range resourceList.Inputs.Strings() { |
| 568 | resources = append(resources, module.Output(compiledResource).Inputs.Strings()...) |
| 569 | } |
| 570 | } |
| 571 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 572 | android.AssertDeepEquals(t, "resource files", testCase.resources, resources) |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 573 | }) |
| 574 | } |
| 575 | } |
| 576 | |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 577 | func TestLibraryAssets(t *testing.T) { |
| 578 | bp := ` |
| 579 | android_app { |
| 580 | name: "foo", |
| 581 | sdk_version: "current", |
| 582 | static_libs: ["lib1", "lib2", "lib3"], |
| 583 | } |
| 584 | |
| 585 | android_library { |
| 586 | name: "lib1", |
| 587 | sdk_version: "current", |
| 588 | asset_dirs: ["assets_a"], |
| 589 | } |
| 590 | |
| 591 | android_library { |
| 592 | name: "lib2", |
| 593 | sdk_version: "current", |
| 594 | } |
| 595 | |
| 596 | android_library { |
| 597 | name: "lib3", |
| 598 | sdk_version: "current", |
| 599 | static_libs: ["lib4"], |
| 600 | } |
| 601 | |
| 602 | android_library { |
| 603 | name: "lib4", |
| 604 | sdk_version: "current", |
| 605 | asset_dirs: ["assets_b"], |
| 606 | } |
| 607 | ` |
| 608 | |
| 609 | testCases := []struct { |
| 610 | name string |
| 611 | assetFlag string |
| 612 | assetPackages []string |
| 613 | }{ |
| 614 | { |
| 615 | name: "foo", |
| 616 | // lib1 has its own asset. lib3 doesn't have any, but provides lib4's transitively. |
| 617 | assetPackages: []string{ |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 618 | "out/soong/.intermediates/foo/android_common/aapt2/package-res.apk", |
| 619 | "out/soong/.intermediates/lib1/android_common/assets.zip", |
| 620 | "out/soong/.intermediates/lib3/android_common/assets.zip", |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 621 | }, |
| 622 | }, |
| 623 | { |
| 624 | name: "lib1", |
| 625 | assetFlag: "-A assets_a", |
| 626 | }, |
| 627 | { |
| 628 | name: "lib2", |
| 629 | }, |
| 630 | { |
| 631 | name: "lib3", |
| 632 | assetPackages: []string{ |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 633 | "out/soong/.intermediates/lib3/android_common/aapt2/package-res.apk", |
| 634 | "out/soong/.intermediates/lib4/android_common/assets.zip", |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 635 | }, |
| 636 | }, |
| 637 | { |
| 638 | name: "lib4", |
| 639 | assetFlag: "-A assets_b", |
| 640 | }, |
| 641 | } |
| 642 | ctx := testApp(t, bp) |
| 643 | |
| 644 | for _, test := range testCases { |
| 645 | t.Run(test.name, func(t *testing.T) { |
| 646 | m := ctx.ModuleForTests(test.name, "android_common") |
| 647 | |
| 648 | // Check asset flag in aapt2 link flags |
| 649 | var aapt2link android.TestingBuildParams |
| 650 | if len(test.assetPackages) > 0 { |
| 651 | aapt2link = m.Output("aapt2/package-res.apk") |
| 652 | } else { |
| 653 | aapt2link = m.Output("package-res.apk") |
| 654 | } |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 655 | aapt2link = aapt2link |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 656 | aapt2Flags := aapt2link.Args["flags"] |
| 657 | if test.assetFlag != "" { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 658 | android.AssertStringDoesContain(t, "asset flag", aapt2Flags, test.assetFlag) |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 659 | } else { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 660 | android.AssertStringDoesNotContain(t, "aapt2 link flags", aapt2Flags, " -A ") |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | // Check asset merge rule. |
| 664 | if len(test.assetPackages) > 0 { |
| 665 | mergeAssets := m.Output("package-res.apk") |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 666 | android.AssertPathsRelativeToTopEquals(t, "mergeAssets inputs", test.assetPackages, mergeAssets.Inputs) |
Jaewoong Jung | 6431ca7 | 2020-01-15 14:15:10 -0800 | [diff] [blame] | 667 | } |
| 668 | }) |
| 669 | } |
| 670 | } |
| 671 | |
Colin Cross | b014f07 | 2021-02-26 14:54:36 -0800 | [diff] [blame] | 672 | func TestAppJavaResources(t *testing.T) { |
| 673 | bp := ` |
| 674 | android_app { |
| 675 | name: "foo", |
| 676 | sdk_version: "current", |
| 677 | java_resources: ["resources/a"], |
| 678 | srcs: ["a.java"], |
| 679 | } |
| 680 | |
| 681 | android_app { |
| 682 | name: "bar", |
| 683 | sdk_version: "current", |
| 684 | java_resources: ["resources/a"], |
| 685 | } |
| 686 | ` |
| 687 | |
| 688 | ctx := testApp(t, bp) |
| 689 | |
| 690 | foo := ctx.ModuleForTests("foo", "android_common") |
| 691 | fooResources := foo.Output("res/foo.jar") |
| 692 | fooDexJar := foo.Output("dex-withres/foo.jar") |
| 693 | fooDexJarAligned := foo.Output("dex-withres-aligned/foo.jar") |
| 694 | fooApk := foo.Rule("combineApk") |
| 695 | |
| 696 | if g, w := fooDexJar.Inputs.Strings(), fooResources.Output.String(); !android.InList(w, g) { |
| 697 | t.Errorf("expected resource jar %q in foo dex jar inputs %q", w, g) |
| 698 | } |
| 699 | |
| 700 | if g, w := fooDexJarAligned.Input.String(), fooDexJar.Output.String(); g != w { |
| 701 | t.Errorf("expected dex jar %q in foo aligned dex jar inputs %q", w, g) |
| 702 | } |
| 703 | |
| 704 | if g, w := fooApk.Inputs.Strings(), fooDexJarAligned.Output.String(); !android.InList(w, g) { |
| 705 | t.Errorf("expected aligned dex jar %q in foo apk inputs %q", w, g) |
| 706 | } |
| 707 | |
| 708 | bar := ctx.ModuleForTests("bar", "android_common") |
| 709 | barResources := bar.Output("res/bar.jar") |
| 710 | barApk := bar.Rule("combineApk") |
| 711 | |
| 712 | if g, w := barApk.Inputs.Strings(), barResources.Output.String(); !android.InList(w, g) { |
| 713 | t.Errorf("expected resources jar %q in bar apk inputs %q", w, g) |
| 714 | } |
| 715 | } |
| 716 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 717 | func TestAndroidResources(t *testing.T) { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 718 | testCases := []struct { |
| 719 | name string |
| 720 | enforceRROTargets []string |
| 721 | enforceRROExcludedOverlays []string |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 722 | resourceFiles map[string][]string |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 723 | overlayFiles map[string][]string |
| 724 | rroDirs map[string][]string |
| 725 | }{ |
| 726 | { |
| 727 | name: "no RRO", |
| 728 | enforceRROTargets: nil, |
| 729 | enforceRROExcludedOverlays: nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 730 | resourceFiles: map[string][]string{ |
| 731 | "foo": nil, |
| 732 | "bar": {"bar/res/res/values/strings.xml"}, |
| 733 | "lib": nil, |
| 734 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 735 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 736 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 737 | "foo": { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 738 | "out/soong/.intermediates/lib2/android_common/package-res.apk", |
| 739 | "out/soong/.intermediates/lib/android_common/package-res.apk", |
| 740 | "out/soong/.intermediates/lib3/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 741 | "foo/res/res/values/strings.xml", |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 742 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 743 | "device/vendor/blah/overlay/foo/res/values/strings.xml", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 744 | "product/vendor/blah/overlay/foo/res/values/strings.xml", |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 745 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 746 | "bar": { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 747 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
| 748 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
| 749 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 750 | "lib": { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 751 | "out/soong/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 752 | "lib/res/res/values/strings.xml", |
| 753 | "device/vendor/blah/overlay/lib/res/values/strings.xml", |
| 754 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 755 | }, |
| 756 | rroDirs: map[string][]string{ |
| 757 | "foo": nil, |
| 758 | "bar": nil, |
| 759 | }, |
| 760 | }, |
| 761 | { |
| 762 | name: "enforce RRO on foo", |
| 763 | enforceRROTargets: []string{"foo"}, |
| 764 | enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"}, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 765 | resourceFiles: map[string][]string{ |
| 766 | "foo": nil, |
| 767 | "bar": {"bar/res/res/values/strings.xml"}, |
| 768 | "lib": nil, |
| 769 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 770 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 771 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 772 | "foo": { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 773 | "out/soong/.intermediates/lib2/android_common/package-res.apk", |
| 774 | "out/soong/.intermediates/lib/android_common/package-res.apk", |
| 775 | "out/soong/.intermediates/lib3/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 776 | "foo/res/res/values/strings.xml", |
| 777 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 778 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 779 | "bar": { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 780 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
| 781 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
| 782 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 783 | "lib": { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 784 | "out/soong/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 785 | "lib/res/res/values/strings.xml", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 786 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 787 | }, |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 788 | |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 789 | rroDirs: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 790 | "foo": { |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 791 | "device:device/vendor/blah/overlay/foo/res", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 792 | "product:product/vendor/blah/overlay/foo/res", |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 793 | "device:device/vendor/blah/overlay/lib/res", |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 794 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 795 | "bar": nil, |
Jaewoong Jung | c779cd4 | 2020-10-06 18:56:10 -0700 | [diff] [blame] | 796 | "lib": {"device:device/vendor/blah/overlay/lib/res"}, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 797 | }, |
| 798 | }, |
| 799 | { |
| 800 | name: "enforce RRO on all", |
| 801 | enforceRROTargets: []string{"*"}, |
| 802 | enforceRROExcludedOverlays: []string{ |
| 803 | // Excluding specific apps/res directories also allowed. |
| 804 | "device/vendor/blah/static_overlay/foo", |
| 805 | "device/vendor/blah/static_overlay/bar/res", |
| 806 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 807 | resourceFiles: map[string][]string{ |
| 808 | "foo": nil, |
| 809 | "bar": {"bar/res/res/values/strings.xml"}, |
| 810 | "lib": nil, |
| 811 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 812 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 813 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 814 | "foo": { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 815 | "out/soong/.intermediates/lib2/android_common/package-res.apk", |
| 816 | "out/soong/.intermediates/lib/android_common/package-res.apk", |
| 817 | "out/soong/.intermediates/lib3/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 818 | "foo/res/res/values/strings.xml", |
| 819 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 820 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 821 | "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"}, |
| 822 | "lib": { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 823 | "out/soong/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 824 | "lib/res/res/values/strings.xml", |
| 825 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 826 | }, |
| 827 | rroDirs: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 828 | "foo": { |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 829 | "device:device/vendor/blah/overlay/foo/res", |
| 830 | "product:product/vendor/blah/overlay/foo/res", |
| 831 | // Lib dep comes after the direct deps |
| 832 | "device:device/vendor/blah/overlay/lib/res", |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 833 | }, |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 834 | "bar": {"device:device/vendor/blah/overlay/bar/res"}, |
| 835 | "lib": {"device:device/vendor/blah/overlay/lib/res"}, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 836 | }, |
| 837 | }, |
| 838 | } |
| 839 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 840 | deviceResourceOverlays := []string{ |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 841 | "device/vendor/blah/overlay", |
| 842 | "device/vendor/blah/overlay2", |
| 843 | "device/vendor/blah/static_overlay", |
| 844 | } |
| 845 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 846 | productResourceOverlays := []string{ |
| 847 | "product/vendor/blah/overlay", |
| 848 | } |
| 849 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 850 | fs := android.MockFS{ |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 851 | "foo/res/res/values/strings.xml": nil, |
| 852 | "bar/res/res/values/strings.xml": nil, |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 853 | "lib/res/res/values/strings.xml": nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 854 | "lib2/res/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 855 | "device/vendor/blah/overlay/foo/res/values/strings.xml": nil, |
| 856 | "device/vendor/blah/overlay/bar/res/values/strings.xml": nil, |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 857 | "device/vendor/blah/overlay/lib/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 858 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil, |
| 859 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil, |
| 860 | "device/vendor/blah/overlay2/res/values/strings.xml": nil, |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 861 | "product/vendor/blah/overlay/foo/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | bp := ` |
| 865 | android_app { |
| 866 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 867 | sdk_version: "current", |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 868 | resource_dirs: ["foo/res"], |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 869 | static_libs: ["lib", "lib3"], |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | android_app { |
| 873 | name: "bar", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 874 | sdk_version: "current", |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 875 | resource_dirs: ["bar/res"], |
| 876 | } |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 877 | |
| 878 | android_library { |
| 879 | name: "lib", |
Jeongik Cha | 75b83b0 | 2019-11-01 15:28:00 +0900 | [diff] [blame] | 880 | sdk_version: "current", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 881 | resource_dirs: ["lib/res"], |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 882 | static_libs: ["lib2"], |
| 883 | } |
| 884 | |
| 885 | android_library { |
| 886 | name: "lib2", |
Jeongik Cha | 75b83b0 | 2019-11-01 15:28:00 +0900 | [diff] [blame] | 887 | sdk_version: "current", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 888 | resource_dirs: ["lib2/res"], |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 889 | } |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 890 | |
| 891 | // This library has the same resources as lib (should not lead to dupe RROs) |
| 892 | android_library { |
| 893 | name: "lib3", |
Jeongik Cha | 75b83b0 | 2019-11-01 15:28:00 +0900 | [diff] [blame] | 894 | sdk_version: "current", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 895 | resource_dirs: ["lib/res"] |
| 896 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 897 | ` |
| 898 | |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 899 | for _, testCase := range testCases { |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 900 | t.Run(testCase.name, func(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 901 | result := android.GroupFixturePreparers( |
| 902 | PrepareForTestWithJavaDefaultModules, |
| 903 | PrepareForTestWithOverlayBuildComponents, |
| 904 | fs.AddToFixture(), |
| 905 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 906 | variables.DeviceResourceOverlays = deviceResourceOverlays |
| 907 | variables.ProductResourceOverlays = productResourceOverlays |
| 908 | if testCase.enforceRROTargets != nil { |
| 909 | variables.EnforceRROTargets = testCase.enforceRROTargets |
| 910 | } |
| 911 | if testCase.enforceRROExcludedOverlays != nil { |
| 912 | variables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays |
| 913 | } |
| 914 | }), |
| 915 | ).RunTestWithBp(t, bp) |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 916 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 917 | resourceListToFiles := func(module android.TestingModule, list []string) (files []string) { |
| 918 | for _, o := range list { |
| 919 | res := module.MaybeOutput(o) |
| 920 | if res.Rule != nil { |
| 921 | // If the overlay is compiled as part of this module (i.e. a .arsc.flat file), |
| 922 | // verify the inputs to the .arsc.flat rule. |
| 923 | files = append(files, res.Inputs.Strings()...) |
| 924 | } else { |
| 925 | // Otherwise, verify the full path to the output of the other module |
| 926 | files = append(files, o) |
Anton Hansson | 94c93f3 | 2019-01-30 16:03:37 +0000 | [diff] [blame] | 927 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 928 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 929 | return files |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 930 | } |
| 931 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 932 | getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 933 | module := result.ModuleForTests(moduleName, "android_common") |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 934 | resourceList := module.MaybeOutput("aapt2/res.list") |
| 935 | if resourceList.Rule != nil { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 936 | resourceFiles = resourceListToFiles(module, android.PathsRelativeToTop(resourceList.Inputs)) |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 937 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 938 | overlayList := module.MaybeOutput("aapt2/overlay.list") |
| 939 | if overlayList.Rule != nil { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 940 | overlayFiles = resourceListToFiles(module, android.PathsRelativeToTop(overlayList.Inputs)) |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 941 | } |
| 942 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 943 | for _, d := range module.Module().(AndroidLibraryDependency).ExportedRRODirs() { |
| 944 | var prefix string |
| 945 | if d.overlayType == device { |
| 946 | prefix = "device:" |
| 947 | } else if d.overlayType == product { |
| 948 | prefix = "product:" |
| 949 | } else { |
| 950 | t.Fatalf("Unexpected overlayType %d", d.overlayType) |
| 951 | } |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 952 | rroDirs = append(rroDirs, prefix+android.PathRelativeToTop(d.path)) |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 953 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 954 | |
| 955 | return resourceFiles, overlayFiles, rroDirs |
| 956 | } |
| 957 | |
| 958 | modules := []string{"foo", "bar", "lib", "lib2"} |
| 959 | for _, module := range modules { |
| 960 | resourceFiles, overlayFiles, rroDirs := getResources(module) |
| 961 | |
| 962 | if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) { |
| 963 | t.Errorf("expected %s resource files:\n %#v\n got:\n %#v", |
| 964 | module, testCase.resourceFiles[module], resourceFiles) |
| 965 | } |
| 966 | if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) { |
| 967 | t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v", |
| 968 | module, testCase.overlayFiles[module], overlayFiles) |
| 969 | } |
| 970 | if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) { |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 971 | t.Errorf("expected %s rroDirs: %#v\n got:\n %#v", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 972 | module, testCase.rroDirs[module], rroDirs) |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 973 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 974 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 975 | }) |
| 976 | } |
| 977 | } |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 978 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 979 | func checkSdkVersion(t *testing.T, result *android.TestResult, expectedSdkVersion string) { |
| 980 | foo := result.ModuleForTests("foo", "android_common") |
Jeongik Cha | 219141c | 2020-08-06 23:00:37 +0900 | [diff] [blame] | 981 | link := foo.Output("package-res.apk") |
| 982 | linkFlags := strings.Split(link.Args["flags"], " ") |
| 983 | min := android.IndexList("--min-sdk-version", linkFlags) |
| 984 | target := android.IndexList("--target-sdk-version", linkFlags) |
| 985 | |
| 986 | if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 { |
| 987 | t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags) |
| 988 | } |
| 989 | |
| 990 | gotMinSdkVersion := linkFlags[min+1] |
| 991 | gotTargetSdkVersion := linkFlags[target+1] |
| 992 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 993 | android.AssertStringEquals(t, "incorrect --min-sdk-version", expectedSdkVersion, gotMinSdkVersion) |
Jeongik Cha | 219141c | 2020-08-06 23:00:37 +0900 | [diff] [blame] | 994 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 995 | android.AssertStringEquals(t, "incorrect --target-sdk-version", expectedSdkVersion, gotTargetSdkVersion) |
Jeongik Cha | 219141c | 2020-08-06 23:00:37 +0900 | [diff] [blame] | 996 | } |
| 997 | |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 998 | func TestAppSdkVersion(t *testing.T) { |
| 999 | testCases := []struct { |
| 1000 | name string |
| 1001 | sdkVersion string |
| 1002 | platformSdkInt int |
| 1003 | platformSdkCodename string |
| 1004 | platformSdkFinal bool |
| 1005 | expectedMinSdkVersion string |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1006 | platformApis bool |
Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 1007 | activeCodenames []string |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1008 | }{ |
| 1009 | { |
| 1010 | name: "current final SDK", |
| 1011 | sdkVersion: "current", |
| 1012 | platformSdkInt: 27, |
| 1013 | platformSdkCodename: "REL", |
| 1014 | platformSdkFinal: true, |
| 1015 | expectedMinSdkVersion: "27", |
| 1016 | }, |
| 1017 | { |
| 1018 | name: "current non-final SDK", |
| 1019 | sdkVersion: "current", |
| 1020 | platformSdkInt: 27, |
| 1021 | platformSdkCodename: "OMR1", |
| 1022 | platformSdkFinal: false, |
| 1023 | expectedMinSdkVersion: "OMR1", |
Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 1024 | activeCodenames: []string{"OMR1"}, |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1025 | }, |
| 1026 | { |
| 1027 | name: "default final SDK", |
| 1028 | sdkVersion: "", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1029 | platformApis: true, |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1030 | platformSdkInt: 27, |
| 1031 | platformSdkCodename: "REL", |
| 1032 | platformSdkFinal: true, |
| 1033 | expectedMinSdkVersion: "27", |
| 1034 | }, |
| 1035 | { |
| 1036 | name: "default non-final SDK", |
| 1037 | sdkVersion: "", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1038 | platformApis: true, |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1039 | platformSdkInt: 27, |
| 1040 | platformSdkCodename: "OMR1", |
| 1041 | platformSdkFinal: false, |
| 1042 | expectedMinSdkVersion: "OMR1", |
Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 1043 | activeCodenames: []string{"OMR1"}, |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1044 | }, |
| 1045 | { |
| 1046 | name: "14", |
| 1047 | sdkVersion: "14", |
| 1048 | expectedMinSdkVersion: "14", |
Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 1049 | platformSdkCodename: "S", |
| 1050 | activeCodenames: []string{"S"}, |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1051 | }, |
| 1052 | } |
| 1053 | |
| 1054 | for _, moduleType := range []string{"android_app", "android_library"} { |
| 1055 | for _, test := range testCases { |
| 1056 | t.Run(moduleType+" "+test.name, func(t *testing.T) { |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1057 | platformApiProp := "" |
| 1058 | if test.platformApis { |
| 1059 | platformApiProp = "platform_apis: true," |
| 1060 | } |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1061 | bp := fmt.Sprintf(`%s { |
| 1062 | name: "foo", |
| 1063 | srcs: ["a.java"], |
| 1064 | sdk_version: "%s", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1065 | %s |
| 1066 | }`, moduleType, test.sdkVersion, platformApiProp) |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1067 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 1068 | result := android.GroupFixturePreparers( |
| 1069 | prepareForJavaTest, |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 1070 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 1071 | variables.Platform_sdk_version = &test.platformSdkInt |
| 1072 | variables.Platform_sdk_codename = &test.platformSdkCodename |
| 1073 | variables.Platform_version_active_codenames = test.activeCodenames |
| 1074 | variables.Platform_sdk_final = &test.platformSdkFinal |
| 1075 | }), |
Paul Duffin | 2645a29 | 2021-03-13 02:36:00 +0000 | [diff] [blame] | 1076 | FixtureWithPrebuiltApis(map[string][]string{ |
| 1077 | "14": {"foo"}, |
| 1078 | }), |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 1079 | ).RunTestWithBp(t, bp) |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1080 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 1081 | checkSdkVersion(t, result, test.expectedMinSdkVersion) |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 1082 | }) |
| 1083 | } |
| 1084 | } |
| 1085 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 1086 | |
Jeongik Cha | 219141c | 2020-08-06 23:00:37 +0900 | [diff] [blame] | 1087 | func TestVendorAppSdkVersion(t *testing.T) { |
| 1088 | testCases := []struct { |
| 1089 | name string |
| 1090 | sdkVersion string |
| 1091 | platformSdkInt int |
| 1092 | platformSdkCodename string |
| 1093 | platformSdkFinal bool |
| 1094 | deviceCurrentApiLevelForVendorModules string |
| 1095 | expectedMinSdkVersion string |
| 1096 | }{ |
| 1097 | { |
| 1098 | name: "current final SDK", |
| 1099 | sdkVersion: "current", |
| 1100 | platformSdkInt: 29, |
| 1101 | platformSdkCodename: "REL", |
| 1102 | platformSdkFinal: true, |
| 1103 | deviceCurrentApiLevelForVendorModules: "29", |
| 1104 | expectedMinSdkVersion: "29", |
| 1105 | }, |
| 1106 | { |
| 1107 | name: "current final SDK", |
| 1108 | sdkVersion: "current", |
| 1109 | platformSdkInt: 29, |
| 1110 | platformSdkCodename: "REL", |
| 1111 | platformSdkFinal: true, |
| 1112 | deviceCurrentApiLevelForVendorModules: "28", |
| 1113 | expectedMinSdkVersion: "28", |
| 1114 | }, |
| 1115 | { |
| 1116 | name: "current final SDK", |
| 1117 | sdkVersion: "current", |
| 1118 | platformSdkInt: 29, |
| 1119 | platformSdkCodename: "Q", |
| 1120 | platformSdkFinal: false, |
Jeongik Cha | 219141c | 2020-08-06 23:00:37 +0900 | [diff] [blame] | 1121 | deviceCurrentApiLevelForVendorModules: "28", |
| 1122 | expectedMinSdkVersion: "28", |
| 1123 | }, |
| 1124 | } |
| 1125 | |
| 1126 | for _, moduleType := range []string{"android_app", "android_library"} { |
| 1127 | for _, sdkKind := range []string{"", "system_"} { |
| 1128 | for _, test := range testCases { |
| 1129 | t.Run(moduleType+" "+test.name, func(t *testing.T) { |
| 1130 | bp := fmt.Sprintf(`%s { |
| 1131 | name: "foo", |
| 1132 | srcs: ["a.java"], |
| 1133 | sdk_version: "%s%s", |
| 1134 | vendor: true, |
| 1135 | }`, moduleType, sdkKind, test.sdkVersion) |
| 1136 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 1137 | result := android.GroupFixturePreparers( |
| 1138 | prepareForJavaTest, |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 1139 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 1140 | variables.Platform_sdk_version = &test.platformSdkInt |
| 1141 | variables.Platform_sdk_codename = &test.platformSdkCodename |
| 1142 | variables.Platform_sdk_final = &test.platformSdkFinal |
| 1143 | variables.DeviceCurrentApiLevelForVendorModules = &test.deviceCurrentApiLevelForVendorModules |
| 1144 | variables.DeviceSystemSdkVersions = []string{"28", "29"} |
| 1145 | }), |
Paul Duffin | 2645a29 | 2021-03-13 02:36:00 +0000 | [diff] [blame] | 1146 | FixtureWithPrebuiltApis(map[string][]string{ |
| 1147 | "28": {"foo"}, |
| 1148 | "29": {"foo"}, |
| 1149 | "current": {"foo"}, |
| 1150 | }), |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 1151 | ).RunTestWithBp(t, bp) |
| 1152 | |
| 1153 | checkSdkVersion(t, result, test.expectedMinSdkVersion) |
Jeongik Cha | 219141c | 2020-08-06 23:00:37 +0900 | [diff] [blame] | 1154 | }) |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1160 | func TestJNIABI(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1161 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1162 | cc_library { |
| 1163 | name: "libjni", |
| 1164 | system_shared_libs: [], |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1165 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1166 | stl: "none", |
| 1167 | } |
| 1168 | |
| 1169 | android_test { |
| 1170 | name: "test", |
| 1171 | sdk_version: "core_platform", |
| 1172 | jni_libs: ["libjni"], |
| 1173 | } |
| 1174 | |
| 1175 | android_test { |
| 1176 | name: "test_first", |
| 1177 | sdk_version: "core_platform", |
| 1178 | compile_multilib: "first", |
| 1179 | jni_libs: ["libjni"], |
| 1180 | } |
| 1181 | |
| 1182 | android_test { |
| 1183 | name: "test_both", |
| 1184 | sdk_version: "core_platform", |
| 1185 | compile_multilib: "both", |
| 1186 | jni_libs: ["libjni"], |
| 1187 | } |
| 1188 | |
| 1189 | android_test { |
| 1190 | name: "test_32", |
| 1191 | sdk_version: "core_platform", |
| 1192 | compile_multilib: "32", |
| 1193 | jni_libs: ["libjni"], |
| 1194 | } |
| 1195 | |
| 1196 | android_test { |
| 1197 | name: "test_64", |
| 1198 | sdk_version: "core_platform", |
| 1199 | compile_multilib: "64", |
| 1200 | jni_libs: ["libjni"], |
| 1201 | } |
| 1202 | `) |
| 1203 | |
| 1204 | testCases := []struct { |
| 1205 | name string |
| 1206 | abis []string |
| 1207 | }{ |
| 1208 | {"test", []string{"arm64-v8a"}}, |
| 1209 | {"test_first", []string{"arm64-v8a"}}, |
| 1210 | {"test_both", []string{"arm64-v8a", "armeabi-v7a"}}, |
| 1211 | {"test_32", []string{"armeabi-v7a"}}, |
| 1212 | {"test_64", []string{"arm64-v8a"}}, |
| 1213 | } |
| 1214 | |
| 1215 | for _, test := range testCases { |
| 1216 | t.Run(test.name, func(t *testing.T) { |
| 1217 | app := ctx.ModuleForTests(test.name, "android_common") |
| 1218 | jniLibZip := app.Output("jnilibs.zip") |
| 1219 | var abis []string |
| 1220 | args := strings.Fields(jniLibZip.Args["jarArgs"]) |
| 1221 | for i := 0; i < len(args); i++ { |
| 1222 | if args[i] == "-P" { |
| 1223 | abis = append(abis, filepath.Base(args[i+1])) |
| 1224 | i++ |
| 1225 | } |
| 1226 | } |
| 1227 | if !reflect.DeepEqual(abis, test.abis) { |
| 1228 | t.Errorf("want abis %v, got %v", test.abis, abis) |
| 1229 | } |
| 1230 | }) |
| 1231 | } |
| 1232 | } |
| 1233 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 1234 | func TestAppSdkVersionByPartition(t *testing.T) { |
| 1235 | testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", ` |
| 1236 | android_app { |
| 1237 | name: "foo", |
| 1238 | srcs: ["a.java"], |
| 1239 | vendor: true, |
| 1240 | platform_apis: true, |
| 1241 | } |
| 1242 | `) |
| 1243 | |
| 1244 | testJava(t, ` |
| 1245 | android_app { |
| 1246 | name: "bar", |
| 1247 | srcs: ["b.java"], |
| 1248 | platform_apis: true, |
| 1249 | } |
| 1250 | `) |
| 1251 | |
| 1252 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 1253 | bp := ` |
| 1254 | android_app { |
| 1255 | name: "foo", |
| 1256 | srcs: ["a.java"], |
| 1257 | product_specific: true, |
| 1258 | platform_apis: true, |
| 1259 | } |
| 1260 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1261 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1262 | errorHandler := android.FixtureExpectsNoErrors |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 1263 | if enforce { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1264 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern("sdk_version must have a value when the module is located at vendor or product") |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 1265 | } |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1266 | |
| 1267 | android.GroupFixturePreparers( |
| 1268 | PrepareForTestWithJavaDefaultModules, |
| 1269 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 1270 | variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
| 1271 | }), |
| 1272 | ). |
| 1273 | ExtendWithErrorHandler(errorHandler). |
| 1274 | RunTestWithBp(t, bp) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1278 | func TestJNIPackaging(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1279 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1280 | cc_library { |
| 1281 | name: "libjni", |
| 1282 | system_shared_libs: [], |
| 1283 | stl: "none", |
Colin Cross | 094cde4 | 2020-02-15 10:38:00 -0800 | [diff] [blame] | 1284 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | android_app { |
| 1288 | name: "app", |
| 1289 | jni_libs: ["libjni"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1290 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | android_app { |
| 1294 | name: "app_noembed", |
| 1295 | jni_libs: ["libjni"], |
| 1296 | use_embedded_native_libs: false, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1297 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | android_app { |
| 1301 | name: "app_embed", |
| 1302 | jni_libs: ["libjni"], |
| 1303 | use_embedded_native_libs: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1304 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | android_test { |
| 1308 | name: "test", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1309 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1310 | jni_libs: ["libjni"], |
| 1311 | } |
| 1312 | |
| 1313 | android_test { |
| 1314 | name: "test_noembed", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1315 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1316 | jni_libs: ["libjni"], |
| 1317 | use_embedded_native_libs: false, |
| 1318 | } |
| 1319 | |
| 1320 | android_test_helper_app { |
| 1321 | name: "test_helper", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1322 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1323 | jni_libs: ["libjni"], |
| 1324 | } |
| 1325 | |
| 1326 | android_test_helper_app { |
| 1327 | name: "test_helper_noembed", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1328 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1329 | jni_libs: ["libjni"], |
| 1330 | use_embedded_native_libs: false, |
| 1331 | } |
| 1332 | `) |
| 1333 | |
| 1334 | testCases := []struct { |
| 1335 | name string |
| 1336 | packaged bool |
| 1337 | compressed bool |
| 1338 | }{ |
| 1339 | {"app", false, false}, |
| 1340 | {"app_noembed", false, false}, |
| 1341 | {"app_embed", true, false}, |
| 1342 | {"test", true, false}, |
| 1343 | {"test_noembed", true, true}, |
| 1344 | {"test_helper", true, false}, |
| 1345 | {"test_helper_noembed", true, true}, |
| 1346 | } |
| 1347 | |
| 1348 | for _, test := range testCases { |
| 1349 | t.Run(test.name, func(t *testing.T) { |
| 1350 | app := ctx.ModuleForTests(test.name, "android_common") |
| 1351 | jniLibZip := app.MaybeOutput("jnilibs.zip") |
| 1352 | if g, w := (jniLibZip.Rule != nil), test.packaged; g != w { |
| 1353 | t.Errorf("expected jni packaged %v, got %v", w, g) |
| 1354 | } |
| 1355 | |
| 1356 | if jniLibZip.Rule != nil { |
| 1357 | if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w { |
| 1358 | t.Errorf("expected jni compressed %v, got %v", w, g) |
| 1359 | } |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 1360 | |
| 1361 | if !strings.Contains(jniLibZip.Implicits[0].String(), "_sdk_") { |
| 1362 | t.Errorf("expected input %q to use sdk variant", jniLibZip.Implicits[0].String()) |
| 1363 | } |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 1364 | } |
| 1365 | }) |
| 1366 | } |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Colin Cross | 3c00770 | 2020-05-08 11:20:24 -0700 | [diff] [blame] | 1369 | func TestJNISDK(t *testing.T) { |
| 1370 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
| 1371 | cc_library { |
| 1372 | name: "libjni", |
| 1373 | system_shared_libs: [], |
| 1374 | stl: "none", |
| 1375 | sdk_version: "current", |
| 1376 | } |
| 1377 | |
| 1378 | android_test { |
| 1379 | name: "app_platform", |
| 1380 | jni_libs: ["libjni"], |
| 1381 | platform_apis: true, |
| 1382 | } |
| 1383 | |
| 1384 | android_test { |
| 1385 | name: "app_sdk", |
| 1386 | jni_libs: ["libjni"], |
| 1387 | sdk_version: "current", |
| 1388 | } |
| 1389 | |
| 1390 | android_test { |
| 1391 | name: "app_force_platform", |
| 1392 | jni_libs: ["libjni"], |
| 1393 | sdk_version: "current", |
| 1394 | jni_uses_platform_apis: true, |
| 1395 | } |
| 1396 | |
| 1397 | android_test { |
| 1398 | name: "app_force_sdk", |
| 1399 | jni_libs: ["libjni"], |
| 1400 | platform_apis: true, |
| 1401 | jni_uses_sdk_apis: true, |
| 1402 | } |
Colin Cross | c2d2405 | 2020-05-13 11:05:02 -0700 | [diff] [blame] | 1403 | |
| 1404 | cc_library { |
| 1405 | name: "libvendorjni", |
| 1406 | system_shared_libs: [], |
| 1407 | stl: "none", |
| 1408 | vendor: true, |
| 1409 | } |
| 1410 | |
| 1411 | android_test { |
| 1412 | name: "app_vendor", |
| 1413 | jni_libs: ["libvendorjni"], |
| 1414 | sdk_version: "current", |
| 1415 | vendor: true, |
| 1416 | } |
Colin Cross | 3c00770 | 2020-05-08 11:20:24 -0700 | [diff] [blame] | 1417 | `) |
| 1418 | |
| 1419 | testCases := []struct { |
Colin Cross | c2d2405 | 2020-05-13 11:05:02 -0700 | [diff] [blame] | 1420 | name string |
| 1421 | sdkJNI bool |
| 1422 | vendorJNI bool |
Colin Cross | 3c00770 | 2020-05-08 11:20:24 -0700 | [diff] [blame] | 1423 | }{ |
Colin Cross | c2d2405 | 2020-05-13 11:05:02 -0700 | [diff] [blame] | 1424 | {name: "app_platform"}, |
| 1425 | {name: "app_sdk", sdkJNI: true}, |
| 1426 | {name: "app_force_platform"}, |
| 1427 | {name: "app_force_sdk", sdkJNI: true}, |
| 1428 | {name: "app_vendor", vendorJNI: true}, |
Colin Cross | 3c00770 | 2020-05-08 11:20:24 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Colin Cross | c2d2405 | 2020-05-13 11:05:02 -0700 | [diff] [blame] | 1431 | platformJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_shared"). |
| 1432 | Output("libjni.so").Output.String() |
| 1433 | sdkJNI := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_sdk_shared"). |
| 1434 | Output("libjni.so").Output.String() |
| 1435 | vendorJNI := ctx.ModuleForTests("libvendorjni", "android_arm64_armv8-a_shared"). |
| 1436 | Output("libvendorjni.so").Output.String() |
| 1437 | |
Colin Cross | 3c00770 | 2020-05-08 11:20:24 -0700 | [diff] [blame] | 1438 | for _, test := range testCases { |
| 1439 | t.Run(test.name, func(t *testing.T) { |
| 1440 | app := ctx.ModuleForTests(test.name, "android_common") |
Colin Cross | 3c00770 | 2020-05-08 11:20:24 -0700 | [diff] [blame] | 1441 | |
| 1442 | jniLibZip := app.MaybeOutput("jnilibs.zip") |
| 1443 | if len(jniLibZip.Implicits) != 1 { |
| 1444 | t.Fatalf("expected exactly one jni library, got %q", jniLibZip.Implicits.Strings()) |
| 1445 | } |
| 1446 | gotJNI := jniLibZip.Implicits[0].String() |
| 1447 | |
| 1448 | if test.sdkJNI { |
| 1449 | if gotJNI != sdkJNI { |
| 1450 | t.Errorf("expected SDK JNI library %q, got %q", sdkJNI, gotJNI) |
| 1451 | } |
Colin Cross | c2d2405 | 2020-05-13 11:05:02 -0700 | [diff] [blame] | 1452 | } else if test.vendorJNI { |
| 1453 | if gotJNI != vendorJNI { |
| 1454 | t.Errorf("expected platform JNI library %q, got %q", vendorJNI, gotJNI) |
| 1455 | } |
Colin Cross | 3c00770 | 2020-05-08 11:20:24 -0700 | [diff] [blame] | 1456 | } else { |
| 1457 | if gotJNI != platformJNI { |
| 1458 | t.Errorf("expected platform JNI library %q, got %q", platformJNI, gotJNI) |
| 1459 | } |
| 1460 | } |
| 1461 | }) |
| 1462 | } |
| 1463 | |
| 1464 | t.Run("jni_uses_platform_apis_error", func(t *testing.T) { |
| 1465 | testJavaError(t, `jni_uses_platform_apis: can only be set for modules that set sdk_version`, ` |
| 1466 | android_test { |
| 1467 | name: "app_platform", |
| 1468 | platform_apis: true, |
| 1469 | jni_uses_platform_apis: true, |
| 1470 | } |
| 1471 | `) |
| 1472 | }) |
| 1473 | |
| 1474 | t.Run("jni_uses_sdk_apis_error", func(t *testing.T) { |
| 1475 | testJavaError(t, `jni_uses_sdk_apis: can only be set for modules that do not set sdk_version`, ` |
| 1476 | android_test { |
| 1477 | name: "app_sdk", |
| 1478 | sdk_version: "current", |
| 1479 | jni_uses_sdk_apis: true, |
| 1480 | } |
| 1481 | `) |
| 1482 | }) |
| 1483 | |
| 1484 | } |
| 1485 | |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1486 | func TestCertificates(t *testing.T) { |
| 1487 | testCases := []struct { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1488 | name string |
| 1489 | bp string |
| 1490 | certificateOverride string |
| 1491 | expectedCertSigningFlags string |
| 1492 | expectedCertificate string |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1493 | }{ |
| 1494 | { |
| 1495 | name: "default", |
| 1496 | bp: ` |
| 1497 | android_app { |
| 1498 | name: "foo", |
| 1499 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1500 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1501 | } |
| 1502 | `, |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1503 | certificateOverride: "", |
| 1504 | expectedCertSigningFlags: "", |
| 1505 | expectedCertificate: "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] | 1506 | }, |
| 1507 | { |
| 1508 | name: "module certificate property", |
| 1509 | bp: ` |
| 1510 | android_app { |
| 1511 | name: "foo", |
| 1512 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1513 | certificate: ":new_certificate", |
| 1514 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | android_app_certificate { |
| 1518 | name: "new_certificate", |
Colin Cross | 3c00770 | 2020-05-08 11:20:24 -0700 | [diff] [blame] | 1519 | certificate: "cert/new_cert", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1520 | } |
| 1521 | `, |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1522 | certificateOverride: "", |
| 1523 | expectedCertSigningFlags: "", |
| 1524 | expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1525 | }, |
| 1526 | { |
| 1527 | name: "path certificate property", |
| 1528 | bp: ` |
| 1529 | android_app { |
| 1530 | name: "foo", |
| 1531 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1532 | certificate: "expiredkey", |
| 1533 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1534 | } |
| 1535 | `, |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1536 | certificateOverride: "", |
| 1537 | expectedCertSigningFlags: "", |
| 1538 | expectedCertificate: "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] | 1539 | }, |
| 1540 | { |
| 1541 | name: "certificate overrides", |
| 1542 | bp: ` |
| 1543 | android_app { |
| 1544 | name: "foo", |
| 1545 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1546 | certificate: "expiredkey", |
| 1547 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | android_app_certificate { |
| 1551 | name: "new_certificate", |
Colin Cross | 3c00770 | 2020-05-08 11:20:24 -0700 | [diff] [blame] | 1552 | certificate: "cert/new_cert", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1553 | } |
| 1554 | `, |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1555 | certificateOverride: "foo:new_certificate", |
| 1556 | expectedCertSigningFlags: "", |
| 1557 | expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8", |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1558 | }, |
| 1559 | { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1560 | name: "certificate signing flags", |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1561 | bp: ` |
| 1562 | android_app { |
| 1563 | name: "foo", |
| 1564 | srcs: ["a.java"], |
| 1565 | certificate: ":new_certificate", |
| 1566 | lineage: "lineage.bin", |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1567 | rotationMinSdkVersion: "32", |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1568 | sdk_version: "current", |
| 1569 | } |
| 1570 | |
| 1571 | android_app_certificate { |
| 1572 | name: "new_certificate", |
| 1573 | certificate: "cert/new_cert", |
| 1574 | } |
| 1575 | `, |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1576 | certificateOverride: "", |
| 1577 | expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32", |
| 1578 | expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1579 | }, |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 1580 | { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1581 | name: "cert signing flags from filegroup", |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 1582 | bp: ` |
| 1583 | android_app { |
| 1584 | name: "foo", |
| 1585 | srcs: ["a.java"], |
| 1586 | certificate: ":new_certificate", |
| 1587 | lineage: ":lineage_bin", |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1588 | rotationMinSdkVersion: "32", |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 1589 | sdk_version: "current", |
| 1590 | } |
| 1591 | |
| 1592 | android_app_certificate { |
| 1593 | name: "new_certificate", |
| 1594 | certificate: "cert/new_cert", |
| 1595 | } |
| 1596 | |
| 1597 | filegroup { |
| 1598 | name: "lineage_bin", |
| 1599 | srcs: ["lineage.bin"], |
| 1600 | } |
| 1601 | `, |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1602 | certificateOverride: "", |
| 1603 | expectedCertSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32", |
| 1604 | expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8", |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 1605 | }, |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | for _, test := range testCases { |
| 1609 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1610 | result := android.GroupFixturePreparers( |
| 1611 | PrepareForTestWithJavaDefaultModules, |
| 1612 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 1613 | if test.certificateOverride != "" { |
| 1614 | variables.CertificateOverrides = []string{test.certificateOverride} |
| 1615 | } |
| 1616 | }), |
| 1617 | ).RunTestWithBp(t, test.bp) |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1618 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1619 | foo := result.ModuleForTests("foo", "android_common") |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1620 | |
| 1621 | signapk := foo.Output("foo.apk") |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1622 | signCertificateFlags := signapk.Args["certificates"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1623 | android.AssertStringEquals(t, "certificates flags", test.expectedCertificate, signCertificateFlags) |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1624 | |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1625 | certSigningFlags := signapk.Args["flags"] |
| 1626 | android.AssertStringEquals(t, "cert signing flags", test.expectedCertSigningFlags, certSigningFlags) |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1627 | }) |
| 1628 | } |
| 1629 | } |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1630 | |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 1631 | func TestRequestV4SigningFlag(t *testing.T) { |
| 1632 | testCases := []struct { |
| 1633 | name string |
| 1634 | bp string |
| 1635 | expected string |
| 1636 | }{ |
| 1637 | { |
| 1638 | name: "default", |
| 1639 | bp: ` |
| 1640 | android_app { |
| 1641 | name: "foo", |
| 1642 | srcs: ["a.java"], |
| 1643 | sdk_version: "current", |
| 1644 | } |
| 1645 | `, |
| 1646 | expected: "", |
| 1647 | }, |
| 1648 | { |
| 1649 | name: "default", |
| 1650 | bp: ` |
| 1651 | android_app { |
| 1652 | name: "foo", |
| 1653 | srcs: ["a.java"], |
| 1654 | sdk_version: "current", |
| 1655 | v4_signature: false, |
| 1656 | } |
| 1657 | `, |
| 1658 | expected: "", |
| 1659 | }, |
| 1660 | { |
| 1661 | name: "module certificate property", |
| 1662 | bp: ` |
| 1663 | android_app { |
| 1664 | name: "foo", |
| 1665 | srcs: ["a.java"], |
| 1666 | sdk_version: "current", |
| 1667 | v4_signature: true, |
| 1668 | } |
| 1669 | `, |
| 1670 | expected: "--enable-v4", |
| 1671 | }, |
| 1672 | } |
| 1673 | |
| 1674 | for _, test := range testCases { |
| 1675 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1676 | result := android.GroupFixturePreparers( |
| 1677 | PrepareForTestWithJavaDefaultModules, |
| 1678 | ).RunTestWithBp(t, test.bp) |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 1679 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1680 | foo := result.ModuleForTests("foo", "android_common") |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 1681 | |
| 1682 | signapk := foo.Output("foo.apk") |
| 1683 | signFlags := signapk.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1684 | android.AssertStringEquals(t, "signing flags", test.expected, signFlags) |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 1685 | }) |
| 1686 | } |
| 1687 | } |
| 1688 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1689 | func TestPackageNameOverride(t *testing.T) { |
| 1690 | testCases := []struct { |
| 1691 | name string |
| 1692 | bp string |
| 1693 | packageNameOverride string |
| 1694 | expected []string |
| 1695 | }{ |
| 1696 | { |
| 1697 | name: "default", |
| 1698 | bp: ` |
| 1699 | android_app { |
| 1700 | name: "foo", |
| 1701 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1702 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1703 | } |
| 1704 | `, |
| 1705 | packageNameOverride: "", |
| 1706 | expected: []string{ |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1707 | "out/soong/.intermediates/foo/android_common/foo.apk", |
| 1708 | "out/soong/target/product/test_device/system/app/foo/foo.apk", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1709 | }, |
| 1710 | }, |
| 1711 | { |
Jooyung Han | 29e2f6d | 2022-01-08 12:13:59 +0900 | [diff] [blame] | 1712 | name: "overridden via PRODUCT_PACKAGE_NAME_OVERRIDES", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1713 | bp: ` |
| 1714 | android_app { |
| 1715 | name: "foo", |
| 1716 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1717 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1718 | } |
| 1719 | `, |
| 1720 | packageNameOverride: "foo:bar", |
| 1721 | expected: []string{ |
| 1722 | // The package apk should be still be the original name for test dependencies. |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1723 | "out/soong/.intermediates/foo/android_common/bar.apk", |
| 1724 | "out/soong/target/product/test_device/system/app/bar/bar.apk", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1725 | }, |
| 1726 | }, |
Jooyung Han | 29e2f6d | 2022-01-08 12:13:59 +0900 | [diff] [blame] | 1727 | { |
| 1728 | name: "overridden via stem", |
| 1729 | bp: ` |
| 1730 | android_app { |
| 1731 | name: "foo", |
| 1732 | srcs: ["a.java"], |
| 1733 | sdk_version: "current", |
| 1734 | stem: "bar", |
| 1735 | } |
| 1736 | `, |
| 1737 | packageNameOverride: "", |
| 1738 | expected: []string{ |
| 1739 | "out/soong/.intermediates/foo/android_common/bar.apk", |
| 1740 | "out/soong/target/product/test_device/system/app/bar/bar.apk", |
| 1741 | }, |
| 1742 | }, |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | for _, test := range testCases { |
| 1746 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1747 | result := android.GroupFixturePreparers( |
| 1748 | PrepareForTestWithJavaDefaultModules, |
| 1749 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 1750 | if test.packageNameOverride != "" { |
| 1751 | variables.PackageNameOverrides = []string{test.packageNameOverride} |
| 1752 | } |
| 1753 | }), |
| 1754 | ).RunTestWithBp(t, test.bp) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1755 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1756 | foo := result.ModuleForTests("foo", "android_common") |
| 1757 | |
Lukacs T. Berki | fbaf725 | 2021-08-19 09:36:42 +0200 | [diff] [blame] | 1758 | outSoongDir := result.Config.SoongOutDir() |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1759 | |
| 1760 | outputs := foo.AllOutputs() |
| 1761 | outputMap := make(map[string]bool) |
| 1762 | for _, o := range outputs { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1763 | outputMap[android.StringPathRelativeToTop(outSoongDir, o)] = true |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1764 | } |
| 1765 | for _, e := range test.expected { |
| 1766 | if _, exist := outputMap[e]; !exist { |
| 1767 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs) |
| 1768 | } |
| 1769 | } |
| 1770 | }) |
| 1771 | } |
| 1772 | } |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1773 | |
| 1774 | func TestInstrumentationTargetOverridden(t *testing.T) { |
| 1775 | bp := ` |
| 1776 | android_app { |
| 1777 | name: "foo", |
| 1778 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1779 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | android_test { |
| 1783 | name: "bar", |
| 1784 | instrumentation_for: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1785 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1786 | } |
| 1787 | ` |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1788 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1789 | result := android.GroupFixturePreparers( |
| 1790 | PrepareForTestWithJavaDefaultModules, |
| 1791 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 1792 | variables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"} |
| 1793 | }), |
| 1794 | ).RunTestWithBp(t, bp) |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1795 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1796 | bar := result.ModuleForTests("bar", "android_common") |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1797 | res := bar.Output("package-res.apk") |
| 1798 | aapt2Flags := res.Args["flags"] |
| 1799 | e := "--rename-instrumentation-target-package org.dandroid.bp" |
| 1800 | if !strings.Contains(aapt2Flags, e) { |
| 1801 | t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags) |
| 1802 | } |
| 1803 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1804 | |
| 1805 | func TestOverrideAndroidApp(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1806 | result := PrepareForTestWithJavaDefaultModules.RunTestWithBp( |
| 1807 | t, ` |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1808 | android_app { |
| 1809 | name: "foo", |
| 1810 | srcs: ["a.java"], |
Jaewoong Jung | a641ee9 | 2019-03-27 11:17:14 -0700 | [diff] [blame] | 1811 | certificate: "expiredkey", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1812 | overrides: ["qux"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1813 | sdk_version: "current", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | override_android_app { |
| 1817 | name: "bar", |
| 1818 | base: "foo", |
| 1819 | certificate: ":new_certificate", |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1820 | lineage: "lineage.bin", |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1821 | rotationMinSdkVersion: "32", |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1822 | logging_parent: "bah", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | android_app_certificate { |
| 1826 | name: "new_certificate", |
| 1827 | certificate: "cert/new_cert", |
| 1828 | } |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1829 | |
| 1830 | override_android_app { |
| 1831 | name: "baz", |
| 1832 | base: "foo", |
| 1833 | package_name: "org.dandroid.bp", |
| 1834 | } |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1835 | |
| 1836 | override_android_app { |
| 1837 | name: "baz_no_rename_resources", |
| 1838 | base: "foo", |
| 1839 | package_name: "org.dandroid.bp", |
| 1840 | rename_resources_package: false, |
| 1841 | } |
| 1842 | |
| 1843 | android_app { |
| 1844 | name: "foo_no_rename_resources", |
| 1845 | srcs: ["a.java"], |
| 1846 | certificate: "expiredkey", |
| 1847 | overrides: ["qux"], |
| 1848 | rename_resources_package: false, |
| 1849 | sdk_version: "current", |
| 1850 | } |
| 1851 | |
| 1852 | override_android_app { |
| 1853 | name: "baz_base_no_rename_resources", |
| 1854 | base: "foo_no_rename_resources", |
| 1855 | package_name: "org.dandroid.bp", |
| 1856 | } |
| 1857 | |
| 1858 | override_android_app { |
| 1859 | name: "baz_override_base_rename_resources", |
| 1860 | base: "foo_no_rename_resources", |
| 1861 | package_name: "org.dandroid.bp", |
| 1862 | rename_resources_package: true, |
| 1863 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1864 | `) |
| 1865 | |
| 1866 | expectedVariants := []struct { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1867 | name string |
| 1868 | moduleName string |
| 1869 | variantName string |
| 1870 | apkName string |
| 1871 | apkPath string |
| 1872 | certFlag string |
| 1873 | certSigningFlags string |
| 1874 | overrides []string |
| 1875 | packageFlag string |
| 1876 | renameResources bool |
| 1877 | logging_parent string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1878 | }{ |
| 1879 | { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1880 | name: "foo", |
| 1881 | moduleName: "foo", |
| 1882 | variantName: "android_common", |
| 1883 | apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk", |
| 1884 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1885 | certSigningFlags: "", |
| 1886 | overrides: []string{"qux"}, |
| 1887 | packageFlag: "", |
| 1888 | renameResources: false, |
| 1889 | logging_parent: "", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1890 | }, |
| 1891 | { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1892 | name: "foo", |
| 1893 | moduleName: "bar", |
| 1894 | variantName: "android_common_bar", |
| 1895 | apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk", |
| 1896 | certFlag: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 1897 | certSigningFlags: "--lineage lineage.bin --rotation-min-sdk-version 32", |
| 1898 | overrides: []string{"qux", "foo"}, |
| 1899 | packageFlag: "", |
| 1900 | renameResources: false, |
| 1901 | logging_parent: "bah", |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1902 | }, |
| 1903 | { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1904 | name: "foo", |
| 1905 | moduleName: "baz", |
| 1906 | variantName: "android_common_baz", |
| 1907 | apkPath: "out/soong/target/product/test_device/system/app/baz/baz.apk", |
| 1908 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1909 | certSigningFlags: "", |
| 1910 | overrides: []string{"qux", "foo"}, |
| 1911 | packageFlag: "org.dandroid.bp", |
| 1912 | renameResources: true, |
| 1913 | logging_parent: "", |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1914 | }, |
| 1915 | { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1916 | name: "foo", |
| 1917 | moduleName: "baz_no_rename_resources", |
| 1918 | variantName: "android_common_baz_no_rename_resources", |
| 1919 | apkPath: "out/soong/target/product/test_device/system/app/baz_no_rename_resources/baz_no_rename_resources.apk", |
| 1920 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1921 | certSigningFlags: "", |
| 1922 | overrides: []string{"qux", "foo"}, |
| 1923 | packageFlag: "org.dandroid.bp", |
| 1924 | renameResources: false, |
| 1925 | logging_parent: "", |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1926 | }, |
| 1927 | { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1928 | name: "foo_no_rename_resources", |
| 1929 | moduleName: "baz_base_no_rename_resources", |
| 1930 | variantName: "android_common_baz_base_no_rename_resources", |
| 1931 | apkPath: "out/soong/target/product/test_device/system/app/baz_base_no_rename_resources/baz_base_no_rename_resources.apk", |
| 1932 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1933 | certSigningFlags: "", |
| 1934 | overrides: []string{"qux", "foo_no_rename_resources"}, |
| 1935 | packageFlag: "org.dandroid.bp", |
| 1936 | renameResources: false, |
| 1937 | logging_parent: "", |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1938 | }, |
| 1939 | { |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1940 | name: "foo_no_rename_resources", |
| 1941 | moduleName: "baz_override_base_rename_resources", |
| 1942 | variantName: "android_common_baz_override_base_rename_resources", |
| 1943 | apkPath: "out/soong/target/product/test_device/system/app/baz_override_base_rename_resources/baz_override_base_rename_resources.apk", |
| 1944 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1945 | certSigningFlags: "", |
| 1946 | overrides: []string{"qux", "foo_no_rename_resources"}, |
| 1947 | packageFlag: "org.dandroid.bp", |
| 1948 | renameResources: true, |
| 1949 | logging_parent: "", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1950 | }, |
| 1951 | } |
| 1952 | for _, expected := range expectedVariants { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1953 | variant := result.ModuleForTests(expected.name, expected.variantName) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1954 | |
| 1955 | // Check the final apk name |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1956 | variant.Output(expected.apkPath) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1957 | |
| 1958 | // Check the certificate paths |
Jaewoong Jung | 5a49881 | 2019-11-07 14:14:38 -0800 | [diff] [blame] | 1959 | signapk := variant.Output(expected.moduleName + ".apk") |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1960 | certFlag := signapk.Args["certificates"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1961 | android.AssertStringEquals(t, "certificates flags", expected.certFlag, certFlag) |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1962 | |
Rupert Shuttleworth | 8eab869 | 2021-11-03 10:39:39 -0400 | [diff] [blame] | 1963 | // Check the cert signing flags |
| 1964 | certSigningFlags := signapk.Args["flags"] |
| 1965 | android.AssertStringEquals(t, "cert signing flags", expected.certSigningFlags, certSigningFlags) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1966 | |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1967 | // Check if the overrides field values are correctly aggregated. |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1968 | mod := variant.Module().(*AndroidApp) |
zhidou | a2ce78f | 2022-02-17 02:33:12 +0000 | [diff] [blame^] | 1969 | android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides) |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1970 | |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1971 | // Test Overridable property: Logging_parent |
| 1972 | logging_parent := mod.aapt.LoggingParent |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1973 | android.AssertStringEquals(t, "overrides property value for logging parent", expected.logging_parent, logging_parent) |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1974 | |
Liz Kammer | 1d5983b | 2020-05-19 19:15:37 +0000 | [diff] [blame] | 1975 | // Check the package renaming flag, if exists. |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1976 | res := variant.Output("package-res.apk") |
| 1977 | aapt2Flags := res.Args["flags"] |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1978 | checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag) |
| 1979 | expectedPackage := expected.packageFlag |
| 1980 | if !expected.renameResources { |
| 1981 | expectedPackage = "" |
Liz Kammer | 1d5983b | 2020-05-19 19:15:37 +0000 | [diff] [blame] | 1982 | } |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1983 | checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expectedPackage) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1984 | } |
| 1985 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1986 | |
zhidou | a2ce78f | 2022-02-17 02:33:12 +0000 | [diff] [blame^] | 1987 | func TestOverrideAndroidAppOverrides(t *testing.T) { |
| 1988 | ctx, _ := testJava( |
| 1989 | t, ` |
| 1990 | android_app { |
| 1991 | name: "foo", |
| 1992 | srcs: ["a.java"], |
| 1993 | sdk_version: "current", |
| 1994 | overrides: ["qux"] |
| 1995 | } |
| 1996 | |
| 1997 | android_app { |
| 1998 | name: "bar", |
| 1999 | srcs: ["b.java"], |
| 2000 | sdk_version: "current", |
| 2001 | overrides: ["foo"] |
| 2002 | } |
| 2003 | |
| 2004 | override_android_app { |
| 2005 | name: "foo_override", |
| 2006 | base: "foo", |
| 2007 | overrides: ["bar"] |
| 2008 | } |
| 2009 | `) |
| 2010 | |
| 2011 | expectedVariants := []struct { |
| 2012 | name string |
| 2013 | moduleName string |
| 2014 | variantName string |
| 2015 | overrides []string |
| 2016 | }{ |
| 2017 | { |
| 2018 | name: "foo", |
| 2019 | moduleName: "foo", |
| 2020 | variantName: "android_common", |
| 2021 | overrides: []string{"qux"}, |
| 2022 | }, |
| 2023 | { |
| 2024 | name: "bar", |
| 2025 | moduleName: "bar", |
| 2026 | variantName: "android_common", |
| 2027 | overrides: []string{"foo"}, |
| 2028 | }, |
| 2029 | { |
| 2030 | name: "foo", |
| 2031 | moduleName: "foo_override", |
| 2032 | variantName: "android_common_foo_override", |
| 2033 | overrides: []string{"bar", "foo"}, |
| 2034 | }, |
| 2035 | } |
| 2036 | for _, expected := range expectedVariants { |
| 2037 | variant := ctx.ModuleForTests(expected.name, expected.variantName) |
| 2038 | |
| 2039 | // Check if the overrides field values are correctly aggregated. |
| 2040 | mod := variant.Module().(*AndroidApp) |
| 2041 | android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.overridableAppProperties.Overrides) |
| 2042 | } |
| 2043 | } |
| 2044 | |
Jooyung Han | 01d80d8 | 2022-01-08 12:16:32 +0900 | [diff] [blame] | 2045 | func TestOverrideAndroidAppStem(t *testing.T) { |
| 2046 | ctx, _ := testJava(t, ` |
| 2047 | android_app { |
| 2048 | name: "foo", |
| 2049 | srcs: ["a.java"], |
| 2050 | sdk_version: "current", |
| 2051 | } |
| 2052 | override_android_app { |
| 2053 | name: "bar", |
| 2054 | base: "foo", |
| 2055 | } |
| 2056 | override_android_app { |
| 2057 | name: "baz", |
| 2058 | base: "foo", |
| 2059 | stem: "baz_stem", |
| 2060 | } |
| 2061 | android_app { |
| 2062 | name: "foo2", |
| 2063 | srcs: ["a.java"], |
| 2064 | sdk_version: "current", |
| 2065 | stem: "foo2_stem", |
| 2066 | } |
| 2067 | override_android_app { |
| 2068 | name: "bar2", |
| 2069 | base: "foo2", |
| 2070 | } |
| 2071 | override_android_app { |
| 2072 | name: "baz2", |
| 2073 | base: "foo2", |
| 2074 | stem: "baz2_stem", |
| 2075 | } |
| 2076 | `) |
| 2077 | for _, expected := range []struct { |
| 2078 | moduleName string |
| 2079 | variantName string |
| 2080 | apkPath string |
| 2081 | }{ |
| 2082 | { |
| 2083 | moduleName: "foo", |
| 2084 | variantName: "android_common", |
| 2085 | apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk", |
| 2086 | }, |
| 2087 | { |
| 2088 | moduleName: "foo", |
| 2089 | variantName: "android_common_bar", |
| 2090 | apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk", |
| 2091 | }, |
| 2092 | { |
| 2093 | moduleName: "foo", |
| 2094 | variantName: "android_common_baz", |
| 2095 | apkPath: "out/soong/target/product/test_device/system/app/baz_stem/baz_stem.apk", |
| 2096 | }, |
| 2097 | { |
| 2098 | moduleName: "foo2", |
| 2099 | variantName: "android_common", |
| 2100 | apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk", |
| 2101 | }, |
| 2102 | { |
| 2103 | moduleName: "foo2", |
| 2104 | variantName: "android_common_bar2", |
| 2105 | // Note that this may cause the duplicate output error. |
| 2106 | apkPath: "out/soong/target/product/test_device/system/app/foo2_stem/foo2_stem.apk", |
| 2107 | }, |
| 2108 | { |
| 2109 | moduleName: "foo2", |
| 2110 | variantName: "android_common_baz2", |
| 2111 | apkPath: "out/soong/target/product/test_device/system/app/baz2_stem/baz2_stem.apk", |
| 2112 | }, |
| 2113 | } { |
| 2114 | variant := ctx.ModuleForTests(expected.moduleName, expected.variantName) |
| 2115 | variant.Output(expected.apkPath) |
| 2116 | } |
| 2117 | } |
| 2118 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 2119 | func TestOverrideAndroidAppDependency(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 2120 | ctx, _ := testJava(t, ` |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 2121 | android_app { |
| 2122 | name: "foo", |
| 2123 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2124 | sdk_version: "current", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | override_android_app { |
| 2128 | name: "bar", |
| 2129 | base: "foo", |
| 2130 | package_name: "org.dandroid.bp", |
| 2131 | } |
| 2132 | |
| 2133 | android_test { |
| 2134 | name: "baz", |
| 2135 | srcs: ["b.java"], |
| 2136 | instrumentation_for: "foo", |
| 2137 | } |
| 2138 | |
| 2139 | android_test { |
| 2140 | name: "qux", |
| 2141 | srcs: ["b.java"], |
| 2142 | instrumentation_for: "bar", |
| 2143 | } |
| 2144 | `) |
| 2145 | |
| 2146 | // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg. |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 2147 | javac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2148 | fooTurbine := "out/soong/.intermediates/foo/android_common/turbine-combined/foo.jar" |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 2149 | if !strings.Contains(javac.Args["classpath"], fooTurbine) { |
| 2150 | t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine) |
| 2151 | } |
| 2152 | |
| 2153 | // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg. |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 2154 | javac = ctx.ModuleForTests("qux", "android_common").Rule("javac") |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2155 | barTurbine := "out/soong/.intermediates/foo/android_common_bar/turbine-combined/foo.jar" |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 2156 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 2157 | t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
| 2158 | } |
| 2159 | } |
| 2160 | |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2161 | func TestOverrideAndroidTest(t *testing.T) { |
| 2162 | ctx, _ := testJava(t, ` |
| 2163 | android_app { |
| 2164 | name: "foo", |
| 2165 | srcs: ["a.java"], |
| 2166 | package_name: "com.android.foo", |
| 2167 | sdk_version: "current", |
| 2168 | } |
| 2169 | |
| 2170 | override_android_app { |
| 2171 | name: "bar", |
| 2172 | base: "foo", |
| 2173 | package_name: "com.android.bar", |
| 2174 | } |
| 2175 | |
| 2176 | android_test { |
| 2177 | name: "foo_test", |
| 2178 | srcs: ["b.java"], |
| 2179 | instrumentation_for: "foo", |
| 2180 | } |
| 2181 | |
| 2182 | override_android_test { |
| 2183 | name: "bar_test", |
| 2184 | base: "foo_test", |
| 2185 | package_name: "com.android.bar.test", |
| 2186 | instrumentation_for: "bar", |
| 2187 | instrumentation_target_package: "com.android.bar", |
| 2188 | } |
| 2189 | `) |
| 2190 | |
| 2191 | expectedVariants := []struct { |
| 2192 | moduleName string |
| 2193 | variantName string |
| 2194 | apkPath string |
| 2195 | overrides []string |
| 2196 | targetVariant string |
| 2197 | packageFlag string |
| 2198 | targetPackageFlag string |
| 2199 | }{ |
| 2200 | { |
| 2201 | variantName: "android_common", |
Jaewoong Jung | 326a941 | 2019-11-21 10:41:00 -0800 | [diff] [blame] | 2202 | apkPath: "/target/product/test_device/testcases/foo_test/arm64/foo_test.apk", |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2203 | overrides: nil, |
| 2204 | targetVariant: "android_common", |
| 2205 | packageFlag: "", |
| 2206 | targetPackageFlag: "", |
| 2207 | }, |
| 2208 | { |
| 2209 | variantName: "android_common_bar_test", |
Jaewoong Jung | 326a941 | 2019-11-21 10:41:00 -0800 | [diff] [blame] | 2210 | apkPath: "/target/product/test_device/testcases/bar_test/arm64/bar_test.apk", |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2211 | overrides: []string{"foo_test"}, |
| 2212 | targetVariant: "android_common_bar", |
| 2213 | packageFlag: "com.android.bar.test", |
| 2214 | targetPackageFlag: "com.android.bar", |
| 2215 | }, |
| 2216 | } |
| 2217 | for _, expected := range expectedVariants { |
| 2218 | variant := ctx.ModuleForTests("foo_test", expected.variantName) |
| 2219 | |
| 2220 | // Check the final apk name |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2221 | variant.Output("out/soong" + expected.apkPath) |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2222 | |
| 2223 | // Check if the overrides field values are correctly aggregated. |
| 2224 | mod := variant.Module().(*AndroidTest) |
zhidou | a2ce78f | 2022-02-17 02:33:12 +0000 | [diff] [blame^] | 2225 | if !reflect.DeepEqual(expected.overrides, mod.overridableAppProperties.Overrides) { |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2226 | t.Errorf("Incorrect overrides property value, expected: %q, got: %q", |
zhidou | a2ce78f | 2022-02-17 02:33:12 +0000 | [diff] [blame^] | 2227 | expected.overrides, mod.overridableAppProperties.Overrides) |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | // Check if javac classpath has the correct jar file path. This checks instrumentation_for overrides. |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 2231 | javac := variant.Rule("javac") |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2232 | turbine := filepath.Join("out", "soong", ".intermediates", "foo", expected.targetVariant, "turbine-combined", "foo.jar") |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2233 | if !strings.Contains(javac.Args["classpath"], turbine) { |
| 2234 | t.Errorf("classpath %q does not contain %q", javac.Args["classpath"], turbine) |
| 2235 | } |
| 2236 | |
| 2237 | // Check aapt2 flags. |
| 2238 | res := variant.Output("package-res.apk") |
| 2239 | aapt2Flags := res.Args["flags"] |
| 2240 | checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag) |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 2241 | checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expected.packageFlag) |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2242 | checkAapt2LinkFlag(t, aapt2Flags, "rename-instrumentation-target-package", expected.targetPackageFlag) |
| 2243 | } |
| 2244 | } |
| 2245 | |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 2246 | func TestAndroidTest_FixTestConfig(t *testing.T) { |
| 2247 | ctx, _ := testJava(t, ` |
| 2248 | android_app { |
| 2249 | name: "foo", |
| 2250 | srcs: ["a.java"], |
| 2251 | package_name: "com.android.foo", |
| 2252 | sdk_version: "current", |
| 2253 | } |
| 2254 | |
| 2255 | android_test { |
| 2256 | name: "foo_test", |
| 2257 | srcs: ["b.java"], |
| 2258 | instrumentation_for: "foo", |
| 2259 | } |
| 2260 | |
| 2261 | android_test { |
| 2262 | name: "bar_test", |
| 2263 | srcs: ["b.java"], |
| 2264 | package_name: "com.android.bar.test", |
| 2265 | instrumentation_for: "foo", |
| 2266 | } |
| 2267 | |
| 2268 | override_android_test { |
| 2269 | name: "baz_test", |
| 2270 | base: "foo_test", |
| 2271 | package_name: "com.android.baz.test", |
| 2272 | } |
| 2273 | `) |
| 2274 | |
| 2275 | testCases := []struct { |
| 2276 | moduleName string |
| 2277 | variantName string |
| 2278 | expectedFlags []string |
| 2279 | }{ |
| 2280 | { |
| 2281 | moduleName: "foo_test", |
| 2282 | variantName: "android_common", |
| 2283 | }, |
| 2284 | { |
| 2285 | moduleName: "bar_test", |
| 2286 | variantName: "android_common", |
| 2287 | expectedFlags: []string{ |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2288 | "--manifest out/soong/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml", |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 2289 | "--package-name com.android.bar.test", |
| 2290 | }, |
| 2291 | }, |
| 2292 | { |
| 2293 | moduleName: "foo_test", |
| 2294 | variantName: "android_common_baz_test", |
| 2295 | expectedFlags: []string{ |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2296 | "--manifest out/soong/.intermediates/foo_test/android_common_baz_test/manifest_fixer/AndroidManifest.xml", |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 2297 | "--package-name com.android.baz.test", |
| 2298 | "--test-file-name baz_test.apk", |
| 2299 | }, |
| 2300 | }, |
| 2301 | } |
| 2302 | |
| 2303 | for _, test := range testCases { |
| 2304 | variant := ctx.ModuleForTests(test.moduleName, test.variantName) |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 2305 | params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml") |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 2306 | |
| 2307 | if len(test.expectedFlags) > 0 { |
| 2308 | if params.Rule == nil { |
| 2309 | t.Errorf("test_config_fixer was expected to run, but didn't") |
| 2310 | } else { |
| 2311 | for _, flag := range test.expectedFlags { |
| 2312 | if !strings.Contains(params.RuleParams.Command, flag) { |
| 2313 | t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command) |
| 2314 | } |
| 2315 | } |
| 2316 | } |
| 2317 | } else { |
| 2318 | if params.Rule != nil { |
| 2319 | t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command) |
| 2320 | } |
| 2321 | } |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 2322 | } |
| 2323 | } |
| 2324 | |
Paul Duffin | 53a70a4 | 2022-01-11 14:35:55 +0000 | [diff] [blame] | 2325 | func TestInstrumentationTargetPrebuilt(t *testing.T) { |
| 2326 | bp := ` |
| 2327 | android_app_import { |
| 2328 | name: "foo", |
| 2329 | apk: "foo.apk", |
| 2330 | presigned: true, |
| 2331 | } |
| 2332 | |
| 2333 | android_test { |
| 2334 | name: "bar", |
| 2335 | srcs: ["a.java"], |
| 2336 | instrumentation_for: "foo", |
| 2337 | sdk_version: "current", |
| 2338 | } |
| 2339 | ` |
| 2340 | |
| 2341 | android.GroupFixturePreparers( |
| 2342 | PrepareForTestWithJavaDefaultModules, |
| 2343 | ).ExtendWithErrorHandler( |
| 2344 | android.FixtureExpectsAtLeastOneErrorMatchingPattern( |
| 2345 | "instrumentation_for: dependency \"foo\" of type \"android_app_import\" does not provide JavaInfo so is unsuitable for use with this property")). |
| 2346 | RunTestWithBp(t, bp) |
| 2347 | } |
| 2348 | |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 2349 | func TestStl(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 2350 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 2351 | cc_library { |
| 2352 | name: "libjni", |
Peter Collingbourne | ad84f97 | 2019-12-17 16:46:18 -0800 | [diff] [blame] | 2353 | sdk_version: "current", |
| 2354 | stl: "c++_shared", |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 2355 | } |
| 2356 | |
| 2357 | android_test { |
| 2358 | name: "stl", |
| 2359 | jni_libs: ["libjni"], |
| 2360 | compile_multilib: "both", |
| 2361 | sdk_version: "current", |
| 2362 | stl: "c++_shared", |
| 2363 | } |
| 2364 | |
| 2365 | android_test { |
| 2366 | name: "system", |
| 2367 | jni_libs: ["libjni"], |
| 2368 | compile_multilib: "both", |
| 2369 | sdk_version: "current", |
| 2370 | } |
| 2371 | `) |
| 2372 | |
| 2373 | testCases := []struct { |
| 2374 | name string |
| 2375 | jnis []string |
| 2376 | }{ |
| 2377 | {"stl", |
| 2378 | []string{ |
| 2379 | "libjni.so", |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 2380 | "libc++_shared.so", |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 2381 | }, |
| 2382 | }, |
| 2383 | {"system", |
| 2384 | []string{ |
| 2385 | "libjni.so", |
| 2386 | }, |
| 2387 | }, |
| 2388 | } |
| 2389 | |
| 2390 | for _, test := range testCases { |
| 2391 | t.Run(test.name, func(t *testing.T) { |
| 2392 | app := ctx.ModuleForTests(test.name, "android_common") |
| 2393 | jniLibZip := app.Output("jnilibs.zip") |
| 2394 | var jnis []string |
| 2395 | args := strings.Fields(jniLibZip.Args["jarArgs"]) |
| 2396 | for i := 0; i < len(args); i++ { |
| 2397 | if args[i] == "-f" { |
| 2398 | jnis = append(jnis, args[i+1]) |
| 2399 | i += 1 |
| 2400 | } |
| 2401 | } |
| 2402 | jnisJoined := strings.Join(jnis, " ") |
| 2403 | for _, jni := range test.jnis { |
| 2404 | if !strings.Contains(jnisJoined, jni) { |
| 2405 | t.Errorf("missing jni %q in %q", jni, jnis) |
| 2406 | } |
| 2407 | } |
| 2408 | }) |
| 2409 | } |
| 2410 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2411 | |
| 2412 | func TestUsesLibraries(t *testing.T) { |
| 2413 | bp := ` |
| 2414 | java_sdk_library { |
| 2415 | name: "foo", |
| 2416 | srcs: ["a.java"], |
| 2417 | api_packages: ["foo"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2418 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | java_sdk_library { |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 2422 | name: "qux", |
| 2423 | srcs: ["a.java"], |
| 2424 | api_packages: ["qux"], |
| 2425 | sdk_version: "current", |
| 2426 | } |
| 2427 | |
| 2428 | java_sdk_library { |
| 2429 | name: "quuz", |
| 2430 | srcs: ["a.java"], |
| 2431 | api_packages: ["quuz"], |
| 2432 | sdk_version: "current", |
| 2433 | } |
| 2434 | |
| 2435 | java_sdk_library { |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2436 | name: "fred", |
| 2437 | srcs: ["a.java"], |
| 2438 | api_packages: ["fred"], |
| 2439 | sdk_version: "current", |
| 2440 | } |
| 2441 | |
| 2442 | java_sdk_library { |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2443 | name: "bar", |
| 2444 | srcs: ["a.java"], |
| 2445 | api_packages: ["bar"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2446 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2447 | } |
| 2448 | |
Ulya Trafimovich | 4b6d4c1 | 2020-08-19 14:58:01 +0100 | [diff] [blame] | 2449 | java_sdk_library { |
| 2450 | name: "runtime-library", |
| 2451 | srcs: ["a.java"], |
| 2452 | sdk_version: "current", |
| 2453 | } |
| 2454 | |
| 2455 | java_library { |
| 2456 | name: "static-runtime-helper", |
| 2457 | srcs: ["a.java"], |
| 2458 | libs: ["runtime-library"], |
| 2459 | sdk_version: "current", |
| 2460 | } |
| 2461 | |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2462 | java_library { |
| 2463 | name: "runtime-required-x", |
| 2464 | srcs: ["a.java"], |
| 2465 | installable: true, |
| 2466 | sdk_version: "current", |
| 2467 | } |
| 2468 | |
| 2469 | java_library { |
| 2470 | name: "runtime-optional-x", |
| 2471 | srcs: ["a.java"], |
| 2472 | installable: true, |
| 2473 | sdk_version: "current", |
| 2474 | } |
| 2475 | |
| 2476 | android_library { |
| 2477 | name: "static-x", |
| 2478 | uses_libs: ["runtime-required-x"], |
| 2479 | optional_uses_libs: ["runtime-optional-x"], |
| 2480 | sdk_version: "current", |
| 2481 | } |
| 2482 | |
| 2483 | java_library { |
| 2484 | name: "runtime-required-y", |
| 2485 | srcs: ["a.java"], |
| 2486 | installable: true, |
| 2487 | sdk_version: "current", |
| 2488 | } |
| 2489 | |
| 2490 | java_library { |
| 2491 | name: "runtime-optional-y", |
| 2492 | srcs: ["a.java"], |
| 2493 | installable: true, |
| 2494 | sdk_version: "current", |
| 2495 | } |
| 2496 | |
| 2497 | java_library { |
| 2498 | name: "static-y", |
| 2499 | srcs: ["a.java"], |
| 2500 | uses_libs: ["runtime-required-y"], |
| 2501 | optional_uses_libs: ["runtime-optional-y"], |
| 2502 | sdk_version: "current", |
| 2503 | } |
| 2504 | |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2505 | // A library that has to use "provides_uses_lib", because: |
| 2506 | // - it is not an SDK library |
| 2507 | // - its library name is different from its module name |
| 2508 | java_library { |
| 2509 | name: "non-sdk-lib", |
| 2510 | provides_uses_lib: "com.non.sdk.lib", |
| 2511 | installable: true, |
| 2512 | srcs: ["a.java"], |
| 2513 | } |
| 2514 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2515 | android_app { |
| 2516 | name: "app", |
| 2517 | srcs: ["a.java"], |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2518 | libs: [ |
| 2519 | "qux", |
| 2520 | "quuz.stubs" |
| 2521 | ], |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2522 | static_libs: [ |
| 2523 | "static-runtime-helper", |
| 2524 | // statically linked component libraries should not pull their SDK libraries, |
| 2525 | // so "fred" should not be added to class loader context |
| 2526 | "fred.stubs", |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2527 | "static-x", |
| 2528 | "static-y", |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2529 | ], |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2530 | uses_libs: [ |
| 2531 | "foo", |
| 2532 | "non-sdk-lib" |
| 2533 | ], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2534 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2535 | optional_uses_libs: [ |
| 2536 | "bar", |
| 2537 | "baz", |
| 2538 | ], |
| 2539 | } |
| 2540 | |
| 2541 | android_app_import { |
| 2542 | name: "prebuilt", |
| 2543 | apk: "prebuilts/apk/app.apk", |
| 2544 | certificate: "platform", |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2545 | uses_libs: [ |
| 2546 | "foo", |
| 2547 | "non-sdk-lib", |
| 2548 | "android.test.runner" |
| 2549 | ], |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2550 | optional_uses_libs: [ |
| 2551 | "bar", |
| 2552 | "baz", |
| 2553 | ], |
| 2554 | } |
| 2555 | ` |
| 2556 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 2557 | result := android.GroupFixturePreparers( |
| 2558 | prepareForJavaTest, |
Paul Duffin | 2645a29 | 2021-03-13 02:36:00 +0000 | [diff] [blame] | 2559 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2560 | FixtureWithLastReleaseApis("runtime-library", "foo", "quuz", "qux", "bar", "fred"), |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2561 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 2562 | variables.MissingUsesLibraries = []string{"baz"} |
| 2563 | }), |
| 2564 | ).RunTestWithBp(t, bp) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2565 | |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2566 | app := result.ModuleForTests("app", "android_common") |
| 2567 | prebuilt := result.ModuleForTests("prebuilt", "android_common") |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2568 | |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 2569 | // Test that implicit dependencies on java_sdk_library instances are passed to the manifest. |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2570 | // This should not include explicit `uses_libs`/`optional_uses_libs` entries. |
| 2571 | actualManifestFixerArgs := app.Output("manifest_fixer/AndroidManifest.xml").Args["args"] |
| 2572 | expectManifestFixerArgs := `--extract-native-libs=true ` + |
| 2573 | `--uses-library qux ` + |
| 2574 | `--uses-library quuz ` + |
Ulya Trafimovich | 0b1c70e | 2021-08-20 15:39:12 +0100 | [diff] [blame] | 2575 | `--uses-library runtime-library` |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 2576 | android.AssertStringDoesContain(t, "manifest_fixer args", actualManifestFixerArgs, expectManifestFixerArgs) |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 2577 | |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2578 | // Test that all libraries are verified (library order matters). |
| 2579 | verifyCmd := app.Rule("verify_uses_libraries").RuleParams.Command |
| 2580 | verifyArgs := `--uses-library foo ` + |
Ulya Trafimovich | eea486a | 2021-02-26 11:38:21 +0000 | [diff] [blame] | 2581 | `--uses-library com.non.sdk.lib ` + |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2582 | `--uses-library qux ` + |
| 2583 | `--uses-library quuz ` + |
| 2584 | `--uses-library runtime-library ` + |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2585 | `--uses-library runtime-required-x ` + |
| 2586 | `--uses-library runtime-required-y ` + |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2587 | `--optional-uses-library bar ` + |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2588 | `--optional-uses-library baz ` + |
| 2589 | `--optional-uses-library runtime-optional-x ` + |
| 2590 | `--optional-uses-library runtime-optional-y ` |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2591 | android.AssertStringDoesContain(t, "verify cmd args", verifyCmd, verifyArgs) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2592 | |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2593 | // Test that all libraries are verified for an APK (library order matters). |
| 2594 | verifyApkCmd := prebuilt.Rule("verify_uses_libraries").RuleParams.Command |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 2595 | verifyApkArgs := `--uses-library foo ` + |
| 2596 | `--uses-library com.non.sdk.lib ` + |
| 2597 | `--uses-library android.test.runner ` + |
| 2598 | `--optional-uses-library bar ` + |
| 2599 | `--optional-uses-library baz ` |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2600 | android.AssertStringDoesContain(t, "verify apk cmd args", verifyApkCmd, verifyApkArgs) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2601 | |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 2602 | // Test that all present libraries are preopted, including implicit SDK dependencies, possibly stubs |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2603 | cmd := app.Rule("dexpreopt").RuleParams.Command |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 2604 | w := `--target-context-for-sdk any ` + |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 2605 | `PCL[/system/framework/qux.jar]#` + |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 2606 | `PCL[/system/framework/quuz.jar]#` + |
| 2607 | `PCL[/system/framework/foo.jar]#` + |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2608 | `PCL[/system/framework/non-sdk-lib.jar]#` + |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 2609 | `PCL[/system/framework/bar.jar]#` + |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2610 | `PCL[/system/framework/runtime-library.jar]#` + |
| 2611 | `PCL[/system/framework/runtime-required-x.jar]#` + |
| 2612 | `PCL[/system/framework/runtime-optional-x.jar]#` + |
| 2613 | `PCL[/system/framework/runtime-required-y.jar]#` + |
| 2614 | `PCL[/system/framework/runtime-optional-y.jar] ` |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2615 | android.AssertStringDoesContain(t, "dexpreopt app cmd args", cmd, w) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2616 | |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2617 | // Test conditional context for target SDK version 28. |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2618 | android.AssertStringDoesContain(t, "dexpreopt app cmd 28", cmd, |
| 2619 | `--target-context-for-sdk 28`+ |
| 2620 | ` PCL[/system/framework/org.apache.http.legacy.jar] `) |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2621 | |
| 2622 | // Test conditional context for target SDK version 29. |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2623 | android.AssertStringDoesContain(t, "dexpreopt app cmd 29", cmd, |
| 2624 | `--target-context-for-sdk 29`+ |
| 2625 | ` PCL[/system/framework/android.hidl.manager-V1.0-java.jar]`+ |
| 2626 | `#PCL[/system/framework/android.hidl.base-V1.0-java.jar] `) |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2627 | |
| 2628 | // Test conditional context for target SDK version 30. |
Ulya Trafimovich | 46b3d5b | 2020-10-21 13:20:55 +0100 | [diff] [blame] | 2629 | // "android.test.mock" is absent because "android.test.runner" is not used. |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2630 | android.AssertStringDoesContain(t, "dexpreopt app cmd 30", cmd, |
| 2631 | `--target-context-for-sdk 30`+ |
| 2632 | ` PCL[/system/framework/android.test.base.jar] `) |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2633 | |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 2634 | cmd = prebuilt.Rule("dexpreopt").RuleParams.Command |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2635 | android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd", cmd, |
| 2636 | `--target-context-for-sdk any`+ |
| 2637 | ` PCL[/system/framework/foo.jar]`+ |
| 2638 | `#PCL[/system/framework/non-sdk-lib.jar]`+ |
| 2639 | `#PCL[/system/framework/android.test.runner.jar]`+ |
| 2640 | `#PCL[/system/framework/bar.jar] `) |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2641 | |
| 2642 | // Test conditional context for target SDK version 30. |
Ulya Trafimovich | 46b3d5b | 2020-10-21 13:20:55 +0100 | [diff] [blame] | 2643 | // "android.test.mock" is present because "android.test.runner" is used. |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2644 | android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd 30", cmd, |
| 2645 | `--target-context-for-sdk 30`+ |
| 2646 | ` PCL[/system/framework/android.test.base.jar]`+ |
| 2647 | `#PCL[/system/framework/android.test.mock.jar] `) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2648 | } |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2649 | |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 2650 | func TestDexpreoptBcp(t *testing.T) { |
| 2651 | bp := ` |
| 2652 | java_sdk_library { |
| 2653 | name: "foo", |
| 2654 | srcs: ["a.java"], |
| 2655 | api_packages: ["foo"], |
| 2656 | sdk_version: "current", |
| 2657 | } |
| 2658 | |
| 2659 | java_sdk_library { |
| 2660 | name: "bar", |
| 2661 | srcs: ["a.java"], |
| 2662 | api_packages: ["bar"], |
| 2663 | permitted_packages: ["bar"], |
| 2664 | sdk_version: "current", |
| 2665 | } |
| 2666 | |
| 2667 | android_app { |
| 2668 | name: "app", |
| 2669 | srcs: ["a.java"], |
| 2670 | sdk_version: "current", |
| 2671 | } |
| 2672 | ` |
| 2673 | |
| 2674 | testCases := []struct { |
| 2675 | name string |
| 2676 | with bool |
| 2677 | expect string |
| 2678 | }{ |
| 2679 | { |
| 2680 | name: "with updatable bcp", |
| 2681 | with: true, |
| 2682 | expect: "/system/framework/foo.jar:/system/framework/bar.jar", |
| 2683 | }, |
| 2684 | { |
| 2685 | name: "without updatable bcp", |
| 2686 | with: false, |
| 2687 | expect: "/system/framework/foo.jar", |
| 2688 | }, |
| 2689 | } |
| 2690 | |
| 2691 | for _, test := range testCases { |
| 2692 | t.Run(test.name, func(t *testing.T) { |
| 2693 | result := android.GroupFixturePreparers( |
| 2694 | prepareForJavaTest, |
| 2695 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2696 | FixtureWithLastReleaseApis("runtime-library", "foo", "bar"), |
| 2697 | dexpreopt.FixtureSetBootJars("platform:foo"), |
satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 2698 | dexpreopt.FixtureSetApexBootJars("platform:bar"), |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 2699 | dexpreopt.FixtureSetPreoptWithUpdatableBcp(test.with), |
| 2700 | ).RunTestWithBp(t, bp) |
| 2701 | |
| 2702 | app := result.ModuleForTests("app", "android_common") |
| 2703 | cmd := app.Rule("dexpreopt").RuleParams.Command |
| 2704 | bcp := " -Xbootclasspath-locations:" + test.expect + " " // space at the end matters |
| 2705 | android.AssertStringDoesContain(t, "dexpreopt app bcp", cmd, bcp) |
| 2706 | }) |
| 2707 | } |
| 2708 | } |
| 2709 | |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2710 | func TestCodelessApp(t *testing.T) { |
| 2711 | testCases := []struct { |
| 2712 | name string |
| 2713 | bp string |
| 2714 | noCode bool |
| 2715 | }{ |
| 2716 | { |
| 2717 | name: "normal", |
| 2718 | bp: ` |
| 2719 | android_app { |
| 2720 | name: "foo", |
| 2721 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2722 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2723 | } |
| 2724 | `, |
| 2725 | noCode: false, |
| 2726 | }, |
| 2727 | { |
| 2728 | name: "app without sources", |
| 2729 | bp: ` |
| 2730 | android_app { |
| 2731 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2732 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2733 | } |
| 2734 | `, |
| 2735 | noCode: true, |
| 2736 | }, |
| 2737 | { |
| 2738 | name: "app with libraries", |
| 2739 | bp: ` |
| 2740 | android_app { |
| 2741 | name: "foo", |
| 2742 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2743 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | java_library { |
| 2747 | name: "lib", |
| 2748 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2749 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2750 | } |
| 2751 | `, |
| 2752 | noCode: false, |
| 2753 | }, |
| 2754 | { |
| 2755 | name: "app with sourceless libraries", |
| 2756 | bp: ` |
| 2757 | android_app { |
| 2758 | name: "foo", |
| 2759 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2760 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | java_library { |
| 2764 | name: "lib", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2765 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2766 | } |
| 2767 | `, |
| 2768 | // TODO(jungjw): this should probably be true |
| 2769 | noCode: false, |
| 2770 | }, |
| 2771 | } |
| 2772 | |
| 2773 | for _, test := range testCases { |
| 2774 | t.Run(test.name, func(t *testing.T) { |
| 2775 | ctx := testApp(t, test.bp) |
| 2776 | |
| 2777 | foo := ctx.ModuleForTests("foo", "android_common") |
| 2778 | manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"] |
| 2779 | if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode { |
| 2780 | t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs) |
| 2781 | } |
| 2782 | }) |
| 2783 | } |
| 2784 | } |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2785 | |
| 2786 | func TestEmbedNotice(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2787 | result := android.GroupFixturePreparers( |
| 2788 | PrepareForTestWithJavaDefaultModules, |
| 2789 | cc.PrepareForTestWithCcDefaultModules, |
| 2790 | genrule.PrepareForTestWithGenRuleBuildComponents, |
| 2791 | android.MockFS{ |
| 2792 | "APP_NOTICE": nil, |
| 2793 | "GENRULE_NOTICE": nil, |
| 2794 | "LIB_NOTICE": nil, |
| 2795 | "TOOL_NOTICE": nil, |
| 2796 | }.AddToFixture(), |
| 2797 | ).RunTestWithBp(t, ` |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2798 | android_app { |
| 2799 | name: "foo", |
| 2800 | srcs: ["a.java"], |
| 2801 | static_libs: ["javalib"], |
| 2802 | jni_libs: ["libjni"], |
| 2803 | notice: "APP_NOTICE", |
| 2804 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2805 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | // No embed_notice flag |
| 2809 | android_app { |
| 2810 | name: "bar", |
| 2811 | srcs: ["a.java"], |
| 2812 | jni_libs: ["libjni"], |
| 2813 | notice: "APP_NOTICE", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2814 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | // No NOTICE files |
| 2818 | android_app { |
| 2819 | name: "baz", |
| 2820 | srcs: ["a.java"], |
| 2821 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2822 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | cc_library { |
| 2826 | name: "libjni", |
| 2827 | system_shared_libs: [], |
| 2828 | stl: "none", |
| 2829 | notice: "LIB_NOTICE", |
Colin Cross | 094cde4 | 2020-02-15 10:38:00 -0800 | [diff] [blame] | 2830 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | java_library { |
| 2834 | name: "javalib", |
| 2835 | srcs: [ |
| 2836 | ":gen", |
| 2837 | ], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2838 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | genrule { |
| 2842 | name: "gen", |
| 2843 | tools: ["gentool"], |
| 2844 | out: ["gen.java"], |
| 2845 | notice: "GENRULE_NOTICE", |
| 2846 | } |
| 2847 | |
| 2848 | java_binary_host { |
| 2849 | name: "gentool", |
| 2850 | srcs: ["b.java"], |
| 2851 | notice: "TOOL_NOTICE", |
| 2852 | } |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2853 | `) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2854 | |
| 2855 | // foo has NOTICE files to process, and embed_notices is true. |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2856 | foo := result.ModuleForTests("foo", "android_common") |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2857 | // verify merge notices rule. |
| 2858 | mergeNotices := foo.Rule("mergeNoticesRule") |
| 2859 | noticeInputs := mergeNotices.Inputs.Strings() |
| 2860 | // TOOL_NOTICE should be excluded as it's a host module. |
| 2861 | if len(mergeNotices.Inputs) != 3 { |
| 2862 | t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs) |
| 2863 | } |
| 2864 | if !inList("APP_NOTICE", noticeInputs) { |
| 2865 | t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs) |
| 2866 | } |
| 2867 | if !inList("LIB_NOTICE", noticeInputs) { |
| 2868 | t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs) |
| 2869 | } |
| 2870 | if !inList("GENRULE_NOTICE", noticeInputs) { |
| 2871 | t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs) |
| 2872 | } |
| 2873 | // aapt2 flags should include -A <NOTICE dir> so that its contents are put in the APK's /assets. |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 2874 | res := foo.Output("package-res.apk") |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2875 | aapt2Flags := res.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2876 | e := "-A out/soong/.intermediates/foo/android_common/NOTICE" |
| 2877 | android.AssertStringDoesContain(t, "expected.apkPath", aapt2Flags, e) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2878 | |
| 2879 | // bar has NOTICE files to process, but embed_notices is not set. |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2880 | bar := result.ModuleForTests("bar", "android_common") |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 2881 | res = bar.Output("package-res.apk") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 2882 | aapt2Flags = res.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2883 | e = "-A out/soong/.intermediates/bar/android_common/NOTICE" |
| 2884 | android.AssertStringDoesNotContain(t, "bar shouldn't have the asset dir flag for NOTICE", aapt2Flags, e) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2885 | |
| 2886 | // baz's embed_notice is true, but it doesn't have any NOTICE files. |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2887 | baz := result.ModuleForTests("baz", "android_common") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 2888 | res = baz.Output("package-res.apk") |
| 2889 | aapt2Flags = res.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2890 | e = "-A out/soong/.intermediates/baz/android_common/NOTICE" |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 2891 | if strings.Contains(aapt2Flags, e) { |
| 2892 | 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] | 2893 | } |
| 2894 | } |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2895 | |
| 2896 | func TestUncompressDex(t *testing.T) { |
| 2897 | testCases := []struct { |
| 2898 | name string |
| 2899 | bp string |
| 2900 | |
| 2901 | uncompressedPlatform bool |
| 2902 | uncompressedUnbundled bool |
| 2903 | }{ |
| 2904 | { |
| 2905 | name: "normal", |
| 2906 | bp: ` |
| 2907 | android_app { |
| 2908 | name: "foo", |
| 2909 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2910 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2911 | } |
| 2912 | `, |
| 2913 | uncompressedPlatform: true, |
| 2914 | uncompressedUnbundled: false, |
| 2915 | }, |
| 2916 | { |
| 2917 | name: "use_embedded_dex", |
| 2918 | bp: ` |
| 2919 | android_app { |
| 2920 | name: "foo", |
| 2921 | use_embedded_dex: true, |
| 2922 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2923 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2924 | } |
| 2925 | `, |
| 2926 | uncompressedPlatform: true, |
| 2927 | uncompressedUnbundled: true, |
| 2928 | }, |
| 2929 | { |
| 2930 | name: "privileged", |
| 2931 | bp: ` |
| 2932 | android_app { |
| 2933 | name: "foo", |
| 2934 | privileged: true, |
| 2935 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2936 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2937 | } |
| 2938 | `, |
| 2939 | uncompressedPlatform: true, |
| 2940 | uncompressedUnbundled: true, |
| 2941 | }, |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 2942 | { |
| 2943 | name: "normal_uncompress_dex_true", |
| 2944 | bp: ` |
| 2945 | android_app { |
| 2946 | name: "foo", |
| 2947 | srcs: ["a.java"], |
| 2948 | sdk_version: "current", |
| 2949 | uncompress_dex: true, |
| 2950 | } |
| 2951 | `, |
| 2952 | uncompressedPlatform: true, |
| 2953 | uncompressedUnbundled: true, |
| 2954 | }, |
| 2955 | { |
| 2956 | name: "normal_uncompress_dex_false", |
| 2957 | bp: ` |
| 2958 | android_app { |
| 2959 | name: "foo", |
| 2960 | srcs: ["a.java"], |
| 2961 | sdk_version: "current", |
| 2962 | uncompress_dex: false, |
| 2963 | } |
| 2964 | `, |
| 2965 | uncompressedPlatform: false, |
| 2966 | uncompressedUnbundled: false, |
| 2967 | }, |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | test := func(t *testing.T, bp string, want bool, unbundled bool) { |
| 2971 | t.Helper() |
| 2972 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 2973 | result := android.GroupFixturePreparers( |
| 2974 | prepareForJavaTest, |
Paul Duffin | 2645a29 | 2021-03-13 02:36:00 +0000 | [diff] [blame] | 2975 | PrepareForTestWithPrebuiltsOfCurrentApi, |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 2976 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 2977 | if unbundled { |
| 2978 | variables.Unbundled_build = proptools.BoolPtr(true) |
| 2979 | variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true) |
| 2980 | } |
| 2981 | }), |
| 2982 | ).RunTestWithBp(t, bp) |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2983 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 2984 | foo := result.ModuleForTests("foo", "android_common") |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2985 | dex := foo.Rule("r8") |
| 2986 | uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0") |
| 2987 | aligned := foo.MaybeRule("zipalign").Rule != nil |
| 2988 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 2989 | android.AssertBoolEquals(t, "uncompressed in dex", want, uncompressedInDexJar) |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2990 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 2991 | android.AssertBoolEquals(t, "aligne", want, aligned) |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | for _, tt := range testCases { |
| 2995 | t.Run(tt.name, func(t *testing.T) { |
| 2996 | t.Run("platform", func(t *testing.T) { |
| 2997 | test(t, tt.bp, tt.uncompressedPlatform, false) |
| 2998 | }) |
| 2999 | t.Run("unbundled", func(t *testing.T) { |
| 3000 | test(t, tt.bp, tt.uncompressedUnbundled, true) |
| 3001 | }) |
| 3002 | }) |
| 3003 | } |
| 3004 | } |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 3005 | |
| 3006 | func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) { |
| 3007 | if expectedValue != "" { |
| 3008 | expectedFlag := "--" + flagName + " " + expectedValue |
| 3009 | if !strings.Contains(aapt2Flags, expectedFlag) { |
| 3010 | t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags) |
| 3011 | } |
| 3012 | } else { |
| 3013 | unexpectedFlag := "--" + flagName |
| 3014 | if strings.Contains(aapt2Flags, unexpectedFlag) { |
| 3015 | t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags) |
| 3016 | } |
| 3017 | } |
| 3018 | } |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 3019 | |
Cole Faust | 9a63131 | 2020-10-22 21:05:24 +0000 | [diff] [blame] | 3020 | func TestExportedProguardFlagFiles(t *testing.T) { |
| 3021 | ctx, _ := testJava(t, ` |
| 3022 | android_app { |
| 3023 | name: "foo", |
| 3024 | sdk_version: "current", |
| 3025 | static_libs: ["lib1"], |
| 3026 | } |
| 3027 | |
| 3028 | android_library { |
| 3029 | name: "lib1", |
| 3030 | sdk_version: "current", |
| 3031 | optimize: { |
| 3032 | proguard_flags_files: ["lib1proguard.cfg"], |
| 3033 | } |
| 3034 | } |
| 3035 | `) |
| 3036 | |
| 3037 | m := ctx.ModuleForTests("foo", "android_common") |
| 3038 | hasLib1Proguard := false |
| 3039 | for _, s := range m.Rule("java.r8").Implicits.Strings() { |
| 3040 | if s == "lib1proguard.cfg" { |
| 3041 | hasLib1Proguard = true |
| 3042 | break |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | if !hasLib1Proguard { |
| 3047 | t.Errorf("App does not use library proguard config") |
| 3048 | } |
| 3049 | } |
Spandan Das | c8cb0c3 | 2021-12-08 17:00:38 +0000 | [diff] [blame] | 3050 | |
| 3051 | func TestTargetSdkVersionManifestFixer(t *testing.T) { |
| 3052 | platform_sdk_codename := "Tiramisu" |
| 3053 | testCases := []struct { |
| 3054 | name string |
| 3055 | targetSdkVersionInBp string |
| 3056 | targetSdkVersionExpected string |
| 3057 | unbundledBuild bool |
| 3058 | }{ |
| 3059 | { |
| 3060 | name: "Non-Unbundled build: Android.bp has targetSdkVersion", |
| 3061 | targetSdkVersionInBp: "30", |
| 3062 | targetSdkVersionExpected: "30", |
| 3063 | unbundledBuild: false, |
| 3064 | }, |
| 3065 | { |
| 3066 | name: "Unbundled build: Android.bp has targetSdkVersion", |
| 3067 | targetSdkVersionInBp: "30", |
| 3068 | targetSdkVersionExpected: "30", |
| 3069 | unbundledBuild: true, |
| 3070 | }, |
| 3071 | { |
| 3072 | name: "Non-Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename", |
| 3073 | targetSdkVersionInBp: platform_sdk_codename, |
| 3074 | targetSdkVersionExpected: platform_sdk_codename, |
| 3075 | unbundledBuild: false, |
| 3076 | }, |
| 3077 | { |
| 3078 | name: "Unbundled build: Android.bp has targetSdkVersion equal to platform_sdk_codename", |
| 3079 | targetSdkVersionInBp: platform_sdk_codename, |
| 3080 | targetSdkVersionExpected: "10000", |
| 3081 | unbundledBuild: true, |
| 3082 | }, |
| 3083 | |
| 3084 | { |
| 3085 | name: "Non-Unbundled build: Android.bp has no targetSdkVersion", |
| 3086 | targetSdkVersionExpected: platform_sdk_codename, |
| 3087 | unbundledBuild: false, |
| 3088 | }, |
| 3089 | { |
| 3090 | name: "Unbundled build: Android.bp has no targetSdkVersion", |
| 3091 | targetSdkVersionExpected: "10000", |
| 3092 | unbundledBuild: true, |
| 3093 | }, |
| 3094 | } |
| 3095 | for _, testCase := range testCases { |
| 3096 | bp := fmt.Sprintf(` |
| 3097 | android_app { |
| 3098 | name: "foo", |
| 3099 | sdk_version: "current", |
| 3100 | target_sdk_version: "%v", |
| 3101 | } |
| 3102 | `, testCase.targetSdkVersionInBp) |
| 3103 | fixture := android.GroupFixturePreparers( |
| 3104 | prepareForJavaTest, |
| 3105 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 3106 | // explicitly set platform_sdk_codename to make the test deterministic |
| 3107 | variables.Platform_sdk_codename = &platform_sdk_codename |
| 3108 | variables.Platform_version_active_codenames = []string{platform_sdk_codename} |
| 3109 | // create a non-empty list if unbundledBuild==true |
| 3110 | if testCase.unbundledBuild { |
| 3111 | variables.Unbundled_build_apps = []string{"apex_a", "apex_b"} |
| 3112 | } |
| 3113 | }), |
| 3114 | ) |
| 3115 | |
| 3116 | result := fixture.RunTestWithBp(t, bp) |
| 3117 | foo := result.ModuleForTests("foo", "android_common") |
| 3118 | |
Gurpreet Singh | 75d65f3 | 2022-01-24 17:44:05 +0000 | [diff] [blame] | 3119 | manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"] |
| 3120 | android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected) |
Spandan Das | c8cb0c3 | 2021-12-08 17:00:38 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |