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 | |
| 147 | testJavaError(t, "platform_apis must be true when sdk_version is empty.", ` |
| 148 | android_app { |
| 149 | name: "bar", |
| 150 | srcs: ["b.java"], |
| 151 | } |
| 152 | `) |
| 153 | |
| 154 | testJavaError(t, "platform_apis must be false when sdk_version is not empty.", ` |
| 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 { |
| 1488 | name string |
| 1489 | bp string |
| 1490 | certificateOverride string |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1491 | expectedLineage 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 | `, |
| 1503 | certificateOverride: "", |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1504 | expectedLineage: "", |
| 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 | `, |
| 1522 | certificateOverride: "", |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1523 | expectedLineage: "", |
| 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 | `, |
| 1536 | certificateOverride: "", |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1537 | expectedLineage: "", |
| 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 | `, |
| 1555 | certificateOverride: "foo:new_certificate", |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1556 | expectedLineage: "", |
| 1557 | expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 1558 | }, |
| 1559 | { |
| 1560 | name: "certificate lineage", |
| 1561 | bp: ` |
| 1562 | android_app { |
| 1563 | name: "foo", |
| 1564 | srcs: ["a.java"], |
| 1565 | certificate: ":new_certificate", |
| 1566 | lineage: "lineage.bin", |
| 1567 | sdk_version: "current", |
| 1568 | } |
| 1569 | |
| 1570 | android_app_certificate { |
| 1571 | name: "new_certificate", |
| 1572 | certificate: "cert/new_cert", |
| 1573 | } |
| 1574 | `, |
| 1575 | certificateOverride: "", |
| 1576 | expectedLineage: "--lineage lineage.bin", |
| 1577 | expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1578 | }, |
Jaewoong Jung | 1c1b6e6 | 2021-03-09 15:02:31 -0800 | [diff] [blame] | 1579 | { |
| 1580 | name: "lineage from filegroup", |
| 1581 | bp: ` |
| 1582 | android_app { |
| 1583 | name: "foo", |
| 1584 | srcs: ["a.java"], |
| 1585 | certificate: ":new_certificate", |
| 1586 | lineage: ":lineage_bin", |
| 1587 | sdk_version: "current", |
| 1588 | } |
| 1589 | |
| 1590 | android_app_certificate { |
| 1591 | name: "new_certificate", |
| 1592 | certificate: "cert/new_cert", |
| 1593 | } |
| 1594 | |
| 1595 | filegroup { |
| 1596 | name: "lineage_bin", |
| 1597 | srcs: ["lineage.bin"], |
| 1598 | } |
| 1599 | `, |
| 1600 | certificateOverride: "", |
| 1601 | expectedLineage: "--lineage lineage.bin", |
| 1602 | expectedCertificate: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 1603 | }, |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | for _, test := range testCases { |
| 1607 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1608 | result := android.GroupFixturePreparers( |
| 1609 | PrepareForTestWithJavaDefaultModules, |
| 1610 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 1611 | if test.certificateOverride != "" { |
| 1612 | variables.CertificateOverrides = []string{test.certificateOverride} |
| 1613 | } |
| 1614 | }), |
| 1615 | ).RunTestWithBp(t, test.bp) |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1616 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1617 | foo := result.ModuleForTests("foo", "android_common") |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1618 | |
| 1619 | signapk := foo.Output("foo.apk") |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1620 | signCertificateFlags := signapk.Args["certificates"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1621 | android.AssertStringEquals(t, "certificates flags", test.expectedCertificate, signCertificateFlags) |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1622 | |
| 1623 | signFlags := signapk.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1624 | android.AssertStringEquals(t, "signing flags", test.expectedLineage, signFlags) |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 1625 | }) |
| 1626 | } |
| 1627 | } |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1628 | |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 1629 | func TestRequestV4SigningFlag(t *testing.T) { |
| 1630 | testCases := []struct { |
| 1631 | name string |
| 1632 | bp string |
| 1633 | expected string |
| 1634 | }{ |
| 1635 | { |
| 1636 | name: "default", |
| 1637 | bp: ` |
| 1638 | android_app { |
| 1639 | name: "foo", |
| 1640 | srcs: ["a.java"], |
| 1641 | sdk_version: "current", |
| 1642 | } |
| 1643 | `, |
| 1644 | expected: "", |
| 1645 | }, |
| 1646 | { |
| 1647 | name: "default", |
| 1648 | bp: ` |
| 1649 | android_app { |
| 1650 | name: "foo", |
| 1651 | srcs: ["a.java"], |
| 1652 | sdk_version: "current", |
| 1653 | v4_signature: false, |
| 1654 | } |
| 1655 | `, |
| 1656 | expected: "", |
| 1657 | }, |
| 1658 | { |
| 1659 | name: "module certificate property", |
| 1660 | bp: ` |
| 1661 | android_app { |
| 1662 | name: "foo", |
| 1663 | srcs: ["a.java"], |
| 1664 | sdk_version: "current", |
| 1665 | v4_signature: true, |
| 1666 | } |
| 1667 | `, |
| 1668 | expected: "--enable-v4", |
| 1669 | }, |
| 1670 | } |
| 1671 | |
| 1672 | for _, test := range testCases { |
| 1673 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1674 | result := android.GroupFixturePreparers( |
| 1675 | PrepareForTestWithJavaDefaultModules, |
| 1676 | ).RunTestWithBp(t, test.bp) |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 1677 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1678 | foo := result.ModuleForTests("foo", "android_common") |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 1679 | |
| 1680 | signapk := foo.Output("foo.apk") |
| 1681 | signFlags := signapk.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1682 | android.AssertStringEquals(t, "signing flags", test.expected, signFlags) |
Songchun Fan | 688de9a | 2020-03-24 20:32:24 -0700 | [diff] [blame] | 1683 | }) |
| 1684 | } |
| 1685 | } |
| 1686 | |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1687 | func TestPackageNameOverride(t *testing.T) { |
| 1688 | testCases := []struct { |
| 1689 | name string |
| 1690 | bp string |
| 1691 | packageNameOverride string |
| 1692 | expected []string |
| 1693 | }{ |
| 1694 | { |
| 1695 | name: "default", |
| 1696 | bp: ` |
| 1697 | android_app { |
| 1698 | name: "foo", |
| 1699 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1700 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1701 | } |
| 1702 | `, |
| 1703 | packageNameOverride: "", |
| 1704 | expected: []string{ |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1705 | "out/soong/.intermediates/foo/android_common/foo.apk", |
| 1706 | "out/soong/target/product/test_device/system/app/foo/foo.apk", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1707 | }, |
| 1708 | }, |
| 1709 | { |
| 1710 | name: "overridden", |
| 1711 | bp: ` |
| 1712 | android_app { |
| 1713 | name: "foo", |
| 1714 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1715 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1716 | } |
| 1717 | `, |
| 1718 | packageNameOverride: "foo:bar", |
| 1719 | expected: []string{ |
| 1720 | // 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] | 1721 | "out/soong/.intermediates/foo/android_common/bar.apk", |
| 1722 | "out/soong/target/product/test_device/system/app/bar/bar.apk", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1723 | }, |
| 1724 | }, |
| 1725 | } |
| 1726 | |
| 1727 | for _, test := range testCases { |
| 1728 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1729 | result := android.GroupFixturePreparers( |
| 1730 | PrepareForTestWithJavaDefaultModules, |
| 1731 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 1732 | if test.packageNameOverride != "" { |
| 1733 | variables.PackageNameOverrides = []string{test.packageNameOverride} |
| 1734 | } |
| 1735 | }), |
| 1736 | ).RunTestWithBp(t, test.bp) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1737 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1738 | foo := result.ModuleForTests("foo", "android_common") |
| 1739 | |
Lukacs T. Berki | fbaf725 | 2021-08-19 09:36:42 +0200 | [diff] [blame^] | 1740 | outSoongDir := result.Config.SoongOutDir() |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1741 | |
| 1742 | outputs := foo.AllOutputs() |
| 1743 | outputMap := make(map[string]bool) |
| 1744 | for _, o := range outputs { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1745 | outputMap[android.StringPathRelativeToTop(outSoongDir, o)] = true |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 1746 | } |
| 1747 | for _, e := range test.expected { |
| 1748 | if _, exist := outputMap[e]; !exist { |
| 1749 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs) |
| 1750 | } |
| 1751 | } |
| 1752 | }) |
| 1753 | } |
| 1754 | } |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1755 | |
| 1756 | func TestInstrumentationTargetOverridden(t *testing.T) { |
| 1757 | bp := ` |
| 1758 | android_app { |
| 1759 | name: "foo", |
| 1760 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1761 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | android_test { |
| 1765 | name: "bar", |
| 1766 | instrumentation_for: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1767 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1768 | } |
| 1769 | ` |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1770 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1771 | result := android.GroupFixturePreparers( |
| 1772 | PrepareForTestWithJavaDefaultModules, |
| 1773 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 1774 | variables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"} |
| 1775 | }), |
| 1776 | ).RunTestWithBp(t, bp) |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1777 | |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1778 | bar := result.ModuleForTests("bar", "android_common") |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 1779 | res := bar.Output("package-res.apk") |
| 1780 | aapt2Flags := res.Args["flags"] |
| 1781 | e := "--rename-instrumentation-target-package org.dandroid.bp" |
| 1782 | if !strings.Contains(aapt2Flags, e) { |
| 1783 | t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags) |
| 1784 | } |
| 1785 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1786 | |
| 1787 | func TestOverrideAndroidApp(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1788 | result := PrepareForTestWithJavaDefaultModules.RunTestWithBp( |
| 1789 | t, ` |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1790 | android_app { |
| 1791 | name: "foo", |
| 1792 | srcs: ["a.java"], |
Jaewoong Jung | a641ee9 | 2019-03-27 11:17:14 -0700 | [diff] [blame] | 1793 | certificate: "expiredkey", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1794 | overrides: ["qux"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1795 | sdk_version: "current", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | override_android_app { |
| 1799 | name: "bar", |
| 1800 | base: "foo", |
| 1801 | certificate: ":new_certificate", |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1802 | lineage: "lineage.bin", |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1803 | logging_parent: "bah", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | android_app_certificate { |
| 1807 | name: "new_certificate", |
| 1808 | certificate: "cert/new_cert", |
| 1809 | } |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1810 | |
| 1811 | override_android_app { |
| 1812 | name: "baz", |
| 1813 | base: "foo", |
| 1814 | package_name: "org.dandroid.bp", |
| 1815 | } |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1816 | |
| 1817 | override_android_app { |
| 1818 | name: "baz_no_rename_resources", |
| 1819 | base: "foo", |
| 1820 | package_name: "org.dandroid.bp", |
| 1821 | rename_resources_package: false, |
| 1822 | } |
| 1823 | |
| 1824 | android_app { |
| 1825 | name: "foo_no_rename_resources", |
| 1826 | srcs: ["a.java"], |
| 1827 | certificate: "expiredkey", |
| 1828 | overrides: ["qux"], |
| 1829 | rename_resources_package: false, |
| 1830 | sdk_version: "current", |
| 1831 | } |
| 1832 | |
| 1833 | override_android_app { |
| 1834 | name: "baz_base_no_rename_resources", |
| 1835 | base: "foo_no_rename_resources", |
| 1836 | package_name: "org.dandroid.bp", |
| 1837 | } |
| 1838 | |
| 1839 | override_android_app { |
| 1840 | name: "baz_override_base_rename_resources", |
| 1841 | base: "foo_no_rename_resources", |
| 1842 | package_name: "org.dandroid.bp", |
| 1843 | rename_resources_package: true, |
| 1844 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1845 | `) |
| 1846 | |
| 1847 | expectedVariants := []struct { |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1848 | name string |
| 1849 | moduleName string |
| 1850 | variantName string |
| 1851 | apkName string |
| 1852 | apkPath string |
| 1853 | certFlag string |
| 1854 | lineageFlag string |
| 1855 | overrides []string |
| 1856 | packageFlag string |
| 1857 | renameResources bool |
| 1858 | logging_parent string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1859 | }{ |
| 1860 | { |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1861 | name: "foo", |
| 1862 | moduleName: "foo", |
| 1863 | variantName: "android_common", |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1864 | apkPath: "out/soong/target/product/test_device/system/app/foo/foo.apk", |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1865 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1866 | lineageFlag: "", |
| 1867 | overrides: []string{"qux"}, |
| 1868 | packageFlag: "", |
| 1869 | renameResources: false, |
| 1870 | logging_parent: "", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1871 | }, |
| 1872 | { |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1873 | name: "foo", |
| 1874 | moduleName: "bar", |
| 1875 | variantName: "android_common_bar", |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1876 | apkPath: "out/soong/target/product/test_device/system/app/bar/bar.apk", |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1877 | certFlag: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 1878 | lineageFlag: "--lineage lineage.bin", |
| 1879 | overrides: []string{"qux", "foo"}, |
| 1880 | packageFlag: "", |
| 1881 | renameResources: false, |
| 1882 | logging_parent: "bah", |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1883 | }, |
| 1884 | { |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1885 | name: "foo", |
| 1886 | moduleName: "baz", |
| 1887 | variantName: "android_common_baz", |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1888 | apkPath: "out/soong/target/product/test_device/system/app/baz/baz.apk", |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1889 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1890 | lineageFlag: "", |
| 1891 | overrides: []string{"qux", "foo"}, |
| 1892 | packageFlag: "org.dandroid.bp", |
| 1893 | renameResources: true, |
| 1894 | logging_parent: "", |
| 1895 | }, |
| 1896 | { |
| 1897 | name: "foo", |
| 1898 | moduleName: "baz_no_rename_resources", |
| 1899 | variantName: "android_common_baz_no_rename_resources", |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1900 | apkPath: "out/soong/target/product/test_device/system/app/baz_no_rename_resources/baz_no_rename_resources.apk", |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1901 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1902 | lineageFlag: "", |
| 1903 | overrides: []string{"qux", "foo"}, |
| 1904 | packageFlag: "org.dandroid.bp", |
| 1905 | renameResources: false, |
| 1906 | logging_parent: "", |
| 1907 | }, |
| 1908 | { |
| 1909 | name: "foo_no_rename_resources", |
| 1910 | moduleName: "baz_base_no_rename_resources", |
| 1911 | variantName: "android_common_baz_base_no_rename_resources", |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1912 | apkPath: "out/soong/target/product/test_device/system/app/baz_base_no_rename_resources/baz_base_no_rename_resources.apk", |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1913 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1914 | lineageFlag: "", |
| 1915 | overrides: []string{"qux", "foo_no_rename_resources"}, |
| 1916 | packageFlag: "org.dandroid.bp", |
| 1917 | renameResources: false, |
| 1918 | logging_parent: "", |
| 1919 | }, |
| 1920 | { |
| 1921 | name: "foo_no_rename_resources", |
| 1922 | moduleName: "baz_override_base_rename_resources", |
| 1923 | variantName: "android_common_baz_override_base_rename_resources", |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1924 | apkPath: "out/soong/target/product/test_device/system/app/baz_override_base_rename_resources/baz_override_base_rename_resources.apk", |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1925 | certFlag: "build/make/target/product/security/expiredkey.x509.pem build/make/target/product/security/expiredkey.pk8", |
| 1926 | lineageFlag: "", |
| 1927 | overrides: []string{"qux", "foo_no_rename_resources"}, |
| 1928 | packageFlag: "org.dandroid.bp", |
| 1929 | renameResources: true, |
| 1930 | logging_parent: "", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1931 | }, |
| 1932 | } |
| 1933 | for _, expected := range expectedVariants { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1934 | variant := result.ModuleForTests(expected.name, expected.variantName) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1935 | |
| 1936 | // Check the final apk name |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1937 | variant.Output(expected.apkPath) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1938 | |
| 1939 | // Check the certificate paths |
Jaewoong Jung | 5a49881 | 2019-11-07 14:14:38 -0800 | [diff] [blame] | 1940 | signapk := variant.Output(expected.moduleName + ".apk") |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1941 | certFlag := signapk.Args["certificates"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1942 | android.AssertStringEquals(t, "certificates flags", expected.certFlag, certFlag) |
Liz Kammer | e2b27f4 | 2020-05-07 13:24:05 -0700 | [diff] [blame] | 1943 | |
| 1944 | // Check the lineage flags |
| 1945 | lineageFlag := signapk.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1946 | android.AssertStringEquals(t, "signing flags", expected.lineageFlag, lineageFlag) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1947 | |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1948 | // Check if the overrides field values are correctly aggregated. |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1949 | mod := variant.Module().(*AndroidApp) |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1950 | android.AssertDeepEquals(t, "overrides property", expected.overrides, mod.appProperties.Overrides) |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1951 | |
Baligh Uddin | 5b16dfb | 2020-02-11 17:27:19 -0800 | [diff] [blame] | 1952 | // Test Overridable property: Logging_parent |
| 1953 | logging_parent := mod.aapt.LoggingParent |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1954 | 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] | 1955 | |
Liz Kammer | 1d5983b | 2020-05-19 19:15:37 +0000 | [diff] [blame] | 1956 | // Check the package renaming flag, if exists. |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1957 | res := variant.Output("package-res.apk") |
| 1958 | aapt2Flags := res.Args["flags"] |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1959 | checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag) |
| 1960 | expectedPackage := expected.packageFlag |
| 1961 | if !expected.renameResources { |
| 1962 | expectedPackage = "" |
Liz Kammer | 1d5983b | 2020-05-19 19:15:37 +0000 | [diff] [blame] | 1963 | } |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 1964 | checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expectedPackage) |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1965 | } |
| 1966 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1967 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1968 | func TestOverrideAndroidAppDependency(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1969 | ctx, _ := testJava(t, ` |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1970 | android_app { |
| 1971 | name: "foo", |
| 1972 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1973 | sdk_version: "current", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | override_android_app { |
| 1977 | name: "bar", |
| 1978 | base: "foo", |
| 1979 | package_name: "org.dandroid.bp", |
| 1980 | } |
| 1981 | |
| 1982 | android_test { |
| 1983 | name: "baz", |
| 1984 | srcs: ["b.java"], |
| 1985 | instrumentation_for: "foo", |
| 1986 | } |
| 1987 | |
| 1988 | android_test { |
| 1989 | name: "qux", |
| 1990 | srcs: ["b.java"], |
| 1991 | instrumentation_for: "bar", |
| 1992 | } |
| 1993 | `) |
| 1994 | |
| 1995 | // 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] | 1996 | javac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1997 | fooTurbine := "out/soong/.intermediates/foo/android_common/turbine-combined/foo.jar" |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1998 | if !strings.Contains(javac.Args["classpath"], fooTurbine) { |
| 1999 | t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine) |
| 2000 | } |
| 2001 | |
| 2002 | // 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] | 2003 | javac = ctx.ModuleForTests("qux", "android_common").Rule("javac") |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2004 | barTurbine := "out/soong/.intermediates/foo/android_common_bar/turbine-combined/foo.jar" |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 2005 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 2006 | t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
| 2007 | } |
| 2008 | } |
| 2009 | |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2010 | func TestOverrideAndroidTest(t *testing.T) { |
| 2011 | ctx, _ := testJava(t, ` |
| 2012 | android_app { |
| 2013 | name: "foo", |
| 2014 | srcs: ["a.java"], |
| 2015 | package_name: "com.android.foo", |
| 2016 | sdk_version: "current", |
| 2017 | } |
| 2018 | |
| 2019 | override_android_app { |
| 2020 | name: "bar", |
| 2021 | base: "foo", |
| 2022 | package_name: "com.android.bar", |
| 2023 | } |
| 2024 | |
| 2025 | android_test { |
| 2026 | name: "foo_test", |
| 2027 | srcs: ["b.java"], |
| 2028 | instrumentation_for: "foo", |
| 2029 | } |
| 2030 | |
| 2031 | override_android_test { |
| 2032 | name: "bar_test", |
| 2033 | base: "foo_test", |
| 2034 | package_name: "com.android.bar.test", |
| 2035 | instrumentation_for: "bar", |
| 2036 | instrumentation_target_package: "com.android.bar", |
| 2037 | } |
| 2038 | `) |
| 2039 | |
| 2040 | expectedVariants := []struct { |
| 2041 | moduleName string |
| 2042 | variantName string |
| 2043 | apkPath string |
| 2044 | overrides []string |
| 2045 | targetVariant string |
| 2046 | packageFlag string |
| 2047 | targetPackageFlag string |
| 2048 | }{ |
| 2049 | { |
| 2050 | variantName: "android_common", |
Jaewoong Jung | 326a941 | 2019-11-21 10:41:00 -0800 | [diff] [blame] | 2051 | apkPath: "/target/product/test_device/testcases/foo_test/arm64/foo_test.apk", |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2052 | overrides: nil, |
| 2053 | targetVariant: "android_common", |
| 2054 | packageFlag: "", |
| 2055 | targetPackageFlag: "", |
| 2056 | }, |
| 2057 | { |
| 2058 | variantName: "android_common_bar_test", |
Jaewoong Jung | 326a941 | 2019-11-21 10:41:00 -0800 | [diff] [blame] | 2059 | apkPath: "/target/product/test_device/testcases/bar_test/arm64/bar_test.apk", |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2060 | overrides: []string{"foo_test"}, |
| 2061 | targetVariant: "android_common_bar", |
| 2062 | packageFlag: "com.android.bar.test", |
| 2063 | targetPackageFlag: "com.android.bar", |
| 2064 | }, |
| 2065 | } |
| 2066 | for _, expected := range expectedVariants { |
| 2067 | variant := ctx.ModuleForTests("foo_test", expected.variantName) |
| 2068 | |
| 2069 | // Check the final apk name |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2070 | variant.Output("out/soong" + expected.apkPath) |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2071 | |
| 2072 | // Check if the overrides field values are correctly aggregated. |
| 2073 | mod := variant.Module().(*AndroidTest) |
| 2074 | if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) { |
| 2075 | t.Errorf("Incorrect overrides property value, expected: %q, got: %q", |
| 2076 | expected.overrides, mod.appProperties.Overrides) |
| 2077 | } |
| 2078 | |
| 2079 | // 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] | 2080 | javac := variant.Rule("javac") |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2081 | 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] | 2082 | if !strings.Contains(javac.Args["classpath"], turbine) { |
| 2083 | t.Errorf("classpath %q does not contain %q", javac.Args["classpath"], turbine) |
| 2084 | } |
| 2085 | |
| 2086 | // Check aapt2 flags. |
| 2087 | res := variant.Output("package-res.apk") |
| 2088 | aapt2Flags := res.Args["flags"] |
| 2089 | checkAapt2LinkFlag(t, aapt2Flags, "rename-manifest-package", expected.packageFlag) |
Liz Kammer | 9f9fd02 | 2020-06-18 19:44:06 +0000 | [diff] [blame] | 2090 | checkAapt2LinkFlag(t, aapt2Flags, "rename-resources-package", expected.packageFlag) |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2091 | checkAapt2LinkFlag(t, aapt2Flags, "rename-instrumentation-target-package", expected.targetPackageFlag) |
| 2092 | } |
| 2093 | } |
| 2094 | |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 2095 | func TestAndroidTest_FixTestConfig(t *testing.T) { |
| 2096 | ctx, _ := testJava(t, ` |
| 2097 | android_app { |
| 2098 | name: "foo", |
| 2099 | srcs: ["a.java"], |
| 2100 | package_name: "com.android.foo", |
| 2101 | sdk_version: "current", |
| 2102 | } |
| 2103 | |
| 2104 | android_test { |
| 2105 | name: "foo_test", |
| 2106 | srcs: ["b.java"], |
| 2107 | instrumentation_for: "foo", |
| 2108 | } |
| 2109 | |
| 2110 | android_test { |
| 2111 | name: "bar_test", |
| 2112 | srcs: ["b.java"], |
| 2113 | package_name: "com.android.bar.test", |
| 2114 | instrumentation_for: "foo", |
| 2115 | } |
| 2116 | |
| 2117 | override_android_test { |
| 2118 | name: "baz_test", |
| 2119 | base: "foo_test", |
| 2120 | package_name: "com.android.baz.test", |
| 2121 | } |
| 2122 | `) |
| 2123 | |
| 2124 | testCases := []struct { |
| 2125 | moduleName string |
| 2126 | variantName string |
| 2127 | expectedFlags []string |
| 2128 | }{ |
| 2129 | { |
| 2130 | moduleName: "foo_test", |
| 2131 | variantName: "android_common", |
| 2132 | }, |
| 2133 | { |
| 2134 | moduleName: "bar_test", |
| 2135 | variantName: "android_common", |
| 2136 | expectedFlags: []string{ |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2137 | "--manifest out/soong/.intermediates/bar_test/android_common/manifest_fixer/AndroidManifest.xml", |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 2138 | "--package-name com.android.bar.test", |
| 2139 | }, |
| 2140 | }, |
| 2141 | { |
| 2142 | moduleName: "foo_test", |
| 2143 | variantName: "android_common_baz_test", |
| 2144 | expectedFlags: []string{ |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2145 | "--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] | 2146 | "--package-name com.android.baz.test", |
| 2147 | "--test-file-name baz_test.apk", |
| 2148 | }, |
| 2149 | }, |
| 2150 | } |
| 2151 | |
| 2152 | for _, test := range testCases { |
| 2153 | variant := ctx.ModuleForTests(test.moduleName, test.variantName) |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 2154 | params := variant.MaybeOutput("test_config_fixer/AndroidTest.xml") |
Jaewoong Jung | 3998234 | 2020-01-14 10:27:18 -0800 | [diff] [blame] | 2155 | |
| 2156 | if len(test.expectedFlags) > 0 { |
| 2157 | if params.Rule == nil { |
| 2158 | t.Errorf("test_config_fixer was expected to run, but didn't") |
| 2159 | } else { |
| 2160 | for _, flag := range test.expectedFlags { |
| 2161 | if !strings.Contains(params.RuleParams.Command, flag) { |
| 2162 | t.Errorf("Flag %q was not found in command: %q", flag, params.RuleParams.Command) |
| 2163 | } |
| 2164 | } |
| 2165 | } |
| 2166 | } else { |
| 2167 | if params.Rule != nil { |
| 2168 | t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command) |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | } |
| 2173 | } |
| 2174 | |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 2175 | func TestStl(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 2176 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 2177 | cc_library { |
| 2178 | name: "libjni", |
Peter Collingbourne | ad84f97 | 2019-12-17 16:46:18 -0800 | [diff] [blame] | 2179 | sdk_version: "current", |
| 2180 | stl: "c++_shared", |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | android_test { |
| 2184 | name: "stl", |
| 2185 | jni_libs: ["libjni"], |
| 2186 | compile_multilib: "both", |
| 2187 | sdk_version: "current", |
| 2188 | stl: "c++_shared", |
| 2189 | } |
| 2190 | |
| 2191 | android_test { |
| 2192 | name: "system", |
| 2193 | jni_libs: ["libjni"], |
| 2194 | compile_multilib: "both", |
| 2195 | sdk_version: "current", |
| 2196 | } |
| 2197 | `) |
| 2198 | |
| 2199 | testCases := []struct { |
| 2200 | name string |
| 2201 | jnis []string |
| 2202 | }{ |
| 2203 | {"stl", |
| 2204 | []string{ |
| 2205 | "libjni.so", |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 2206 | "libc++_shared.so", |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 2207 | }, |
| 2208 | }, |
| 2209 | {"system", |
| 2210 | []string{ |
| 2211 | "libjni.so", |
| 2212 | }, |
| 2213 | }, |
| 2214 | } |
| 2215 | |
| 2216 | for _, test := range testCases { |
| 2217 | t.Run(test.name, func(t *testing.T) { |
| 2218 | app := ctx.ModuleForTests(test.name, "android_common") |
| 2219 | jniLibZip := app.Output("jnilibs.zip") |
| 2220 | var jnis []string |
| 2221 | args := strings.Fields(jniLibZip.Args["jarArgs"]) |
| 2222 | for i := 0; i < len(args); i++ { |
| 2223 | if args[i] == "-f" { |
| 2224 | jnis = append(jnis, args[i+1]) |
| 2225 | i += 1 |
| 2226 | } |
| 2227 | } |
| 2228 | jnisJoined := strings.Join(jnis, " ") |
| 2229 | for _, jni := range test.jnis { |
| 2230 | if !strings.Contains(jnisJoined, jni) { |
| 2231 | t.Errorf("missing jni %q in %q", jni, jnis) |
| 2232 | } |
| 2233 | } |
| 2234 | }) |
| 2235 | } |
| 2236 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2237 | |
| 2238 | func TestUsesLibraries(t *testing.T) { |
| 2239 | bp := ` |
| 2240 | java_sdk_library { |
| 2241 | name: "foo", |
| 2242 | srcs: ["a.java"], |
| 2243 | api_packages: ["foo"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2244 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | java_sdk_library { |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 2248 | name: "qux", |
| 2249 | srcs: ["a.java"], |
| 2250 | api_packages: ["qux"], |
| 2251 | sdk_version: "current", |
| 2252 | } |
| 2253 | |
| 2254 | java_sdk_library { |
| 2255 | name: "quuz", |
| 2256 | srcs: ["a.java"], |
| 2257 | api_packages: ["quuz"], |
| 2258 | sdk_version: "current", |
| 2259 | } |
| 2260 | |
| 2261 | java_sdk_library { |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2262 | name: "fred", |
| 2263 | srcs: ["a.java"], |
| 2264 | api_packages: ["fred"], |
| 2265 | sdk_version: "current", |
| 2266 | } |
| 2267 | |
| 2268 | java_sdk_library { |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2269 | name: "bar", |
| 2270 | srcs: ["a.java"], |
| 2271 | api_packages: ["bar"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2272 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2273 | } |
| 2274 | |
Ulya Trafimovich | 4b6d4c1 | 2020-08-19 14:58:01 +0100 | [diff] [blame] | 2275 | java_sdk_library { |
| 2276 | name: "runtime-library", |
| 2277 | srcs: ["a.java"], |
| 2278 | sdk_version: "current", |
| 2279 | } |
| 2280 | |
| 2281 | java_library { |
| 2282 | name: "static-runtime-helper", |
| 2283 | srcs: ["a.java"], |
| 2284 | libs: ["runtime-library"], |
| 2285 | sdk_version: "current", |
| 2286 | } |
| 2287 | |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2288 | java_library { |
| 2289 | name: "runtime-required-x", |
| 2290 | srcs: ["a.java"], |
| 2291 | installable: true, |
| 2292 | sdk_version: "current", |
| 2293 | } |
| 2294 | |
| 2295 | java_library { |
| 2296 | name: "runtime-optional-x", |
| 2297 | srcs: ["a.java"], |
| 2298 | installable: true, |
| 2299 | sdk_version: "current", |
| 2300 | } |
| 2301 | |
| 2302 | android_library { |
| 2303 | name: "static-x", |
| 2304 | uses_libs: ["runtime-required-x"], |
| 2305 | optional_uses_libs: ["runtime-optional-x"], |
| 2306 | sdk_version: "current", |
| 2307 | } |
| 2308 | |
| 2309 | java_library { |
| 2310 | name: "runtime-required-y", |
| 2311 | srcs: ["a.java"], |
| 2312 | installable: true, |
| 2313 | sdk_version: "current", |
| 2314 | } |
| 2315 | |
| 2316 | java_library { |
| 2317 | name: "runtime-optional-y", |
| 2318 | srcs: ["a.java"], |
| 2319 | installable: true, |
| 2320 | sdk_version: "current", |
| 2321 | } |
| 2322 | |
| 2323 | java_library { |
| 2324 | name: "static-y", |
| 2325 | srcs: ["a.java"], |
| 2326 | uses_libs: ["runtime-required-y"], |
| 2327 | optional_uses_libs: ["runtime-optional-y"], |
| 2328 | sdk_version: "current", |
| 2329 | } |
| 2330 | |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2331 | // A library that has to use "provides_uses_lib", because: |
| 2332 | // - it is not an SDK library |
| 2333 | // - its library name is different from its module name |
| 2334 | java_library { |
| 2335 | name: "non-sdk-lib", |
| 2336 | provides_uses_lib: "com.non.sdk.lib", |
| 2337 | installable: true, |
| 2338 | srcs: ["a.java"], |
| 2339 | } |
| 2340 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2341 | android_app { |
| 2342 | name: "app", |
| 2343 | srcs: ["a.java"], |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2344 | libs: [ |
| 2345 | "qux", |
| 2346 | "quuz.stubs" |
| 2347 | ], |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2348 | static_libs: [ |
| 2349 | "static-runtime-helper", |
| 2350 | // statically linked component libraries should not pull their SDK libraries, |
| 2351 | // so "fred" should not be added to class loader context |
| 2352 | "fred.stubs", |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2353 | "static-x", |
| 2354 | "static-y", |
Ulya Trafimovich | 65b0319 | 2020-12-03 16:50:22 +0000 | [diff] [blame] | 2355 | ], |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2356 | uses_libs: [ |
| 2357 | "foo", |
| 2358 | "non-sdk-lib" |
| 2359 | ], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2360 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2361 | optional_uses_libs: [ |
| 2362 | "bar", |
| 2363 | "baz", |
| 2364 | ], |
| 2365 | } |
| 2366 | |
| 2367 | android_app_import { |
| 2368 | name: "prebuilt", |
| 2369 | apk: "prebuilts/apk/app.apk", |
| 2370 | certificate: "platform", |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2371 | uses_libs: [ |
| 2372 | "foo", |
| 2373 | "non-sdk-lib", |
| 2374 | "android.test.runner" |
| 2375 | ], |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2376 | optional_uses_libs: [ |
| 2377 | "bar", |
| 2378 | "baz", |
| 2379 | ], |
| 2380 | } |
| 2381 | ` |
| 2382 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 2383 | result := android.GroupFixturePreparers( |
| 2384 | prepareForJavaTest, |
Paul Duffin | 2645a29 | 2021-03-13 02:36:00 +0000 | [diff] [blame] | 2385 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2386 | FixtureWithLastReleaseApis("runtime-library", "foo", "quuz", "qux", "bar", "fred"), |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2387 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 2388 | variables.MissingUsesLibraries = []string{"baz"} |
| 2389 | }), |
| 2390 | ).RunTestWithBp(t, bp) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2391 | |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2392 | app := result.ModuleForTests("app", "android_common") |
| 2393 | prebuilt := result.ModuleForTests("prebuilt", "android_common") |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2394 | |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 2395 | // 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] | 2396 | // This should not include explicit `uses_libs`/`optional_uses_libs` entries. |
| 2397 | actualManifestFixerArgs := app.Output("manifest_fixer/AndroidManifest.xml").Args["args"] |
| 2398 | expectManifestFixerArgs := `--extract-native-libs=true ` + |
| 2399 | `--uses-library qux ` + |
| 2400 | `--uses-library quuz ` + |
| 2401 | `--uses-library foo ` + // TODO(b/132357300): "foo" should not be passed to manifest_fixer |
Ulya Trafimovich | eea486a | 2021-02-26 11:38:21 +0000 | [diff] [blame] | 2402 | `--uses-library com.non.sdk.lib ` + // TODO(b/132357300): "com.non.sdk.lib" should not be passed to manifest_fixer |
Ulya Trafimovich | fc0f6e3 | 2021-08-12 16:16:11 +0100 | [diff] [blame] | 2403 | `--uses-library runtime-library ` + |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2404 | `--uses-library runtime-required-x ` + // TODO(b/132357300): "runtime-required-x" should not be passed to manifest_fixer |
| 2405 | `--uses-library runtime-required-y ` + // TODO(b/132357300): "runtime-required-y" should not be passed to manifest_fixer |
| 2406 | `--optional-uses-library bar ` + // TODO(b/132357300): "bar" should not be passed to manifest_fixer |
| 2407 | `--optional-uses-library runtime-optional-x ` + // TODO(b/132357300): "runtime-optional-x" should not be passed to manifest_fixer |
| 2408 | `--optional-uses-library runtime-optional-y` // TODO(b/132357300): "runtime-optional-y" should not be passed to manifest_fixer |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2409 | android.AssertStringEquals(t, "manifest_fixer args", expectManifestFixerArgs, actualManifestFixerArgs) |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 2410 | |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2411 | // Test that all libraries are verified (library order matters). |
| 2412 | verifyCmd := app.Rule("verify_uses_libraries").RuleParams.Command |
| 2413 | verifyArgs := `--uses-library foo ` + |
Ulya Trafimovich | eea486a | 2021-02-26 11:38:21 +0000 | [diff] [blame] | 2414 | `--uses-library com.non.sdk.lib ` + |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2415 | `--uses-library qux ` + |
| 2416 | `--uses-library quuz ` + |
| 2417 | `--uses-library runtime-library ` + |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2418 | `--uses-library runtime-required-x ` + |
| 2419 | `--uses-library runtime-required-y ` + |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2420 | `--optional-uses-library bar ` + |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2421 | `--optional-uses-library baz ` + |
| 2422 | `--optional-uses-library runtime-optional-x ` + |
| 2423 | `--optional-uses-library runtime-optional-y ` |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2424 | android.AssertStringDoesContain(t, "verify cmd args", verifyCmd, verifyArgs) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2425 | |
Ulya Trafimovich | 2eaf5c5 | 2021-02-26 12:05:11 +0000 | [diff] [blame] | 2426 | // Test that all libraries are verified for an APK (library order matters). |
| 2427 | verifyApkCmd := prebuilt.Rule("verify_uses_libraries").RuleParams.Command |
Ulya Trafimovich | 0aba252 | 2021-03-03 16:38:37 +0000 | [diff] [blame] | 2428 | verifyApkArgs := `--uses-library foo ` + |
| 2429 | `--uses-library com.non.sdk.lib ` + |
| 2430 | `--uses-library android.test.runner ` + |
| 2431 | `--optional-uses-library bar ` + |
| 2432 | `--optional-uses-library baz ` |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2433 | android.AssertStringDoesContain(t, "verify apk cmd args", verifyApkCmd, verifyApkArgs) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2434 | |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 2435 | // 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] | 2436 | cmd := app.Rule("dexpreopt").RuleParams.Command |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 2437 | w := `--target-context-for-sdk any ` + |
Ulya Trafimovich | 8130c48 | 2020-10-07 15:17:13 +0100 | [diff] [blame] | 2438 | `PCL[/system/framework/qux.jar]#` + |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 2439 | `PCL[/system/framework/quuz.jar]#` + |
| 2440 | `PCL[/system/framework/foo.jar]#` + |
Ulya Trafimovich | 861a896 | 2021-02-26 14:49:07 +0000 | [diff] [blame] | 2441 | `PCL[/system/framework/non-sdk-lib.jar]#` + |
Ulya Trafimovich | 8cbc5d2 | 2020-11-03 15:15:46 +0000 | [diff] [blame] | 2442 | `PCL[/system/framework/bar.jar]#` + |
Ulya Trafimovich | e443287 | 2021-08-18 16:57:11 +0100 | [diff] [blame] | 2443 | `PCL[/system/framework/runtime-library.jar]#` + |
| 2444 | `PCL[/system/framework/runtime-required-x.jar]#` + |
| 2445 | `PCL[/system/framework/runtime-optional-x.jar]#` + |
| 2446 | `PCL[/system/framework/runtime-required-y.jar]#` + |
| 2447 | `PCL[/system/framework/runtime-optional-y.jar] ` |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2448 | android.AssertStringDoesContain(t, "dexpreopt app cmd args", cmd, w) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2449 | |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2450 | // Test conditional context for target SDK version 28. |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2451 | android.AssertStringDoesContain(t, "dexpreopt app cmd 28", cmd, |
| 2452 | `--target-context-for-sdk 28`+ |
| 2453 | ` PCL[/system/framework/org.apache.http.legacy.jar] `) |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2454 | |
| 2455 | // Test conditional context for target SDK version 29. |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2456 | android.AssertStringDoesContain(t, "dexpreopt app cmd 29", cmd, |
| 2457 | `--target-context-for-sdk 29`+ |
| 2458 | ` PCL[/system/framework/android.hidl.manager-V1.0-java.jar]`+ |
| 2459 | `#PCL[/system/framework/android.hidl.base-V1.0-java.jar] `) |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2460 | |
| 2461 | // Test conditional context for target SDK version 30. |
Ulya Trafimovich | 46b3d5b | 2020-10-21 13:20:55 +0100 | [diff] [blame] | 2462 | // "android.test.mock" is absent because "android.test.runner" is not used. |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2463 | android.AssertStringDoesContain(t, "dexpreopt app cmd 30", cmd, |
| 2464 | `--target-context-for-sdk 30`+ |
| 2465 | ` PCL[/system/framework/android.test.base.jar] `) |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2466 | |
Ulya Trafimovich | fc24ad3 | 2020-08-19 16:32:54 +0100 | [diff] [blame] | 2467 | cmd = prebuilt.Rule("dexpreopt").RuleParams.Command |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2468 | android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd", cmd, |
| 2469 | `--target-context-for-sdk any`+ |
| 2470 | ` PCL[/system/framework/foo.jar]`+ |
| 2471 | `#PCL[/system/framework/non-sdk-lib.jar]`+ |
| 2472 | `#PCL[/system/framework/android.test.runner.jar]`+ |
| 2473 | `#PCL[/system/framework/bar.jar] `) |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 2474 | |
| 2475 | // Test conditional context for target SDK version 30. |
Ulya Trafimovich | 46b3d5b | 2020-10-21 13:20:55 +0100 | [diff] [blame] | 2476 | // "android.test.mock" is present because "android.test.runner" is used. |
Paul Duffin | d234b41 | 2021-03-12 23:04:46 +0000 | [diff] [blame] | 2477 | android.AssertStringDoesContain(t, "dexpreopt prebuilt cmd 30", cmd, |
| 2478 | `--target-context-for-sdk 30`+ |
| 2479 | ` PCL[/system/framework/android.test.base.jar]`+ |
| 2480 | `#PCL[/system/framework/android.test.mock.jar] `) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 2481 | } |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2482 | |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 2483 | func TestDexpreoptBcp(t *testing.T) { |
| 2484 | bp := ` |
| 2485 | java_sdk_library { |
| 2486 | name: "foo", |
| 2487 | srcs: ["a.java"], |
| 2488 | api_packages: ["foo"], |
| 2489 | sdk_version: "current", |
| 2490 | } |
| 2491 | |
| 2492 | java_sdk_library { |
| 2493 | name: "bar", |
| 2494 | srcs: ["a.java"], |
| 2495 | api_packages: ["bar"], |
| 2496 | permitted_packages: ["bar"], |
| 2497 | sdk_version: "current", |
| 2498 | } |
| 2499 | |
| 2500 | android_app { |
| 2501 | name: "app", |
| 2502 | srcs: ["a.java"], |
| 2503 | sdk_version: "current", |
| 2504 | } |
| 2505 | ` |
| 2506 | |
| 2507 | testCases := []struct { |
| 2508 | name string |
| 2509 | with bool |
| 2510 | expect string |
| 2511 | }{ |
| 2512 | { |
| 2513 | name: "with updatable bcp", |
| 2514 | with: true, |
| 2515 | expect: "/system/framework/foo.jar:/system/framework/bar.jar", |
| 2516 | }, |
| 2517 | { |
| 2518 | name: "without updatable bcp", |
| 2519 | with: false, |
| 2520 | expect: "/system/framework/foo.jar", |
| 2521 | }, |
| 2522 | } |
| 2523 | |
| 2524 | for _, test := range testCases { |
| 2525 | t.Run(test.name, func(t *testing.T) { |
| 2526 | result := android.GroupFixturePreparers( |
| 2527 | prepareForJavaTest, |
| 2528 | PrepareForTestWithJavaSdkLibraryFiles, |
| 2529 | FixtureWithLastReleaseApis("runtime-library", "foo", "bar"), |
| 2530 | dexpreopt.FixtureSetBootJars("platform:foo"), |
satayev | d604b21 | 2021-07-21 14:23:52 +0100 | [diff] [blame] | 2531 | dexpreopt.FixtureSetApexBootJars("platform:bar"), |
Ulya Trafimovich | 9023b02 | 2021-03-22 16:02:28 +0000 | [diff] [blame] | 2532 | dexpreopt.FixtureSetPreoptWithUpdatableBcp(test.with), |
| 2533 | ).RunTestWithBp(t, bp) |
| 2534 | |
| 2535 | app := result.ModuleForTests("app", "android_common") |
| 2536 | cmd := app.Rule("dexpreopt").RuleParams.Command |
| 2537 | bcp := " -Xbootclasspath-locations:" + test.expect + " " // space at the end matters |
| 2538 | android.AssertStringDoesContain(t, "dexpreopt app bcp", cmd, bcp) |
| 2539 | }) |
| 2540 | } |
| 2541 | } |
| 2542 | |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2543 | func TestCodelessApp(t *testing.T) { |
| 2544 | testCases := []struct { |
| 2545 | name string |
| 2546 | bp string |
| 2547 | noCode bool |
| 2548 | }{ |
| 2549 | { |
| 2550 | name: "normal", |
| 2551 | bp: ` |
| 2552 | android_app { |
| 2553 | name: "foo", |
| 2554 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2555 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2556 | } |
| 2557 | `, |
| 2558 | noCode: false, |
| 2559 | }, |
| 2560 | { |
| 2561 | name: "app without sources", |
| 2562 | bp: ` |
| 2563 | android_app { |
| 2564 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2565 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2566 | } |
| 2567 | `, |
| 2568 | noCode: true, |
| 2569 | }, |
| 2570 | { |
| 2571 | name: "app with libraries", |
| 2572 | bp: ` |
| 2573 | android_app { |
| 2574 | name: "foo", |
| 2575 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2576 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | java_library { |
| 2580 | name: "lib", |
| 2581 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2582 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2583 | } |
| 2584 | `, |
| 2585 | noCode: false, |
| 2586 | }, |
| 2587 | { |
| 2588 | name: "app with sourceless libraries", |
| 2589 | bp: ` |
| 2590 | android_app { |
| 2591 | name: "foo", |
| 2592 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2593 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | java_library { |
| 2597 | name: "lib", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2598 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 2599 | } |
| 2600 | `, |
| 2601 | // TODO(jungjw): this should probably be true |
| 2602 | noCode: false, |
| 2603 | }, |
| 2604 | } |
| 2605 | |
| 2606 | for _, test := range testCases { |
| 2607 | t.Run(test.name, func(t *testing.T) { |
| 2608 | ctx := testApp(t, test.bp) |
| 2609 | |
| 2610 | foo := ctx.ModuleForTests("foo", "android_common") |
| 2611 | manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"] |
| 2612 | if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode { |
| 2613 | t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs) |
| 2614 | } |
| 2615 | }) |
| 2616 | } |
| 2617 | } |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2618 | |
| 2619 | func TestEmbedNotice(t *testing.T) { |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2620 | result := android.GroupFixturePreparers( |
| 2621 | PrepareForTestWithJavaDefaultModules, |
| 2622 | cc.PrepareForTestWithCcDefaultModules, |
| 2623 | genrule.PrepareForTestWithGenRuleBuildComponents, |
| 2624 | android.MockFS{ |
| 2625 | "APP_NOTICE": nil, |
| 2626 | "GENRULE_NOTICE": nil, |
| 2627 | "LIB_NOTICE": nil, |
| 2628 | "TOOL_NOTICE": nil, |
| 2629 | }.AddToFixture(), |
| 2630 | ).RunTestWithBp(t, ` |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2631 | android_app { |
| 2632 | name: "foo", |
| 2633 | srcs: ["a.java"], |
| 2634 | static_libs: ["javalib"], |
| 2635 | jni_libs: ["libjni"], |
| 2636 | notice: "APP_NOTICE", |
| 2637 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2638 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | // No embed_notice flag |
| 2642 | android_app { |
| 2643 | name: "bar", |
| 2644 | srcs: ["a.java"], |
| 2645 | jni_libs: ["libjni"], |
| 2646 | notice: "APP_NOTICE", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2647 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | // No NOTICE files |
| 2651 | android_app { |
| 2652 | name: "baz", |
| 2653 | srcs: ["a.java"], |
| 2654 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2655 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | cc_library { |
| 2659 | name: "libjni", |
| 2660 | system_shared_libs: [], |
| 2661 | stl: "none", |
| 2662 | notice: "LIB_NOTICE", |
Colin Cross | 094cde4 | 2020-02-15 10:38:00 -0800 | [diff] [blame] | 2663 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | java_library { |
| 2667 | name: "javalib", |
| 2668 | srcs: [ |
| 2669 | ":gen", |
| 2670 | ], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2671 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2672 | } |
| 2673 | |
| 2674 | genrule { |
| 2675 | name: "gen", |
| 2676 | tools: ["gentool"], |
| 2677 | out: ["gen.java"], |
| 2678 | notice: "GENRULE_NOTICE", |
| 2679 | } |
| 2680 | |
| 2681 | java_binary_host { |
| 2682 | name: "gentool", |
| 2683 | srcs: ["b.java"], |
| 2684 | notice: "TOOL_NOTICE", |
| 2685 | } |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2686 | `) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2687 | |
| 2688 | // foo has NOTICE files to process, and embed_notices is true. |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2689 | foo := result.ModuleForTests("foo", "android_common") |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2690 | // verify merge notices rule. |
| 2691 | mergeNotices := foo.Rule("mergeNoticesRule") |
| 2692 | noticeInputs := mergeNotices.Inputs.Strings() |
| 2693 | // TOOL_NOTICE should be excluded as it's a host module. |
| 2694 | if len(mergeNotices.Inputs) != 3 { |
| 2695 | t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs) |
| 2696 | } |
| 2697 | if !inList("APP_NOTICE", noticeInputs) { |
| 2698 | t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs) |
| 2699 | } |
| 2700 | if !inList("LIB_NOTICE", noticeInputs) { |
| 2701 | t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs) |
| 2702 | } |
| 2703 | if !inList("GENRULE_NOTICE", noticeInputs) { |
| 2704 | t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs) |
| 2705 | } |
| 2706 | // 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] | 2707 | res := foo.Output("package-res.apk") |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2708 | aapt2Flags := res.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2709 | e := "-A out/soong/.intermediates/foo/android_common/NOTICE" |
| 2710 | android.AssertStringDoesContain(t, "expected.apkPath", aapt2Flags, e) |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 2711 | |
| 2712 | // bar has NOTICE files to process, but embed_notices is not set. |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2713 | bar := result.ModuleForTests("bar", "android_common") |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 2714 | res = bar.Output("package-res.apk") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 2715 | aapt2Flags = res.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2716 | e = "-A out/soong/.intermediates/bar/android_common/NOTICE" |
| 2717 | 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] | 2718 | |
| 2719 | // 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] | 2720 | baz := result.ModuleForTests("baz", "android_common") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 2721 | res = baz.Output("package-res.apk") |
| 2722 | aapt2Flags = res.Args["flags"] |
Paul Duffin | fb0fe9f | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 2723 | e = "-A out/soong/.intermediates/baz/android_common/NOTICE" |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 2724 | if strings.Contains(aapt2Flags, e) { |
| 2725 | 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] | 2726 | } |
| 2727 | } |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2728 | |
| 2729 | func TestUncompressDex(t *testing.T) { |
| 2730 | testCases := []struct { |
| 2731 | name string |
| 2732 | bp string |
| 2733 | |
| 2734 | uncompressedPlatform bool |
| 2735 | uncompressedUnbundled bool |
| 2736 | }{ |
| 2737 | { |
| 2738 | name: "normal", |
| 2739 | bp: ` |
| 2740 | android_app { |
| 2741 | name: "foo", |
| 2742 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2743 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2744 | } |
| 2745 | `, |
| 2746 | uncompressedPlatform: true, |
| 2747 | uncompressedUnbundled: false, |
| 2748 | }, |
| 2749 | { |
| 2750 | name: "use_embedded_dex", |
| 2751 | bp: ` |
| 2752 | android_app { |
| 2753 | name: "foo", |
| 2754 | use_embedded_dex: true, |
| 2755 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2756 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2757 | } |
| 2758 | `, |
| 2759 | uncompressedPlatform: true, |
| 2760 | uncompressedUnbundled: true, |
| 2761 | }, |
| 2762 | { |
| 2763 | name: "privileged", |
| 2764 | bp: ` |
| 2765 | android_app { |
| 2766 | name: "foo", |
| 2767 | privileged: true, |
| 2768 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 2769 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2770 | } |
| 2771 | `, |
| 2772 | uncompressedPlatform: true, |
| 2773 | uncompressedUnbundled: true, |
| 2774 | }, |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 2775 | { |
| 2776 | name: "normal_uncompress_dex_true", |
| 2777 | bp: ` |
| 2778 | android_app { |
| 2779 | name: "foo", |
| 2780 | srcs: ["a.java"], |
| 2781 | sdk_version: "current", |
| 2782 | uncompress_dex: true, |
| 2783 | } |
| 2784 | `, |
| 2785 | uncompressedPlatform: true, |
| 2786 | uncompressedUnbundled: true, |
| 2787 | }, |
| 2788 | { |
| 2789 | name: "normal_uncompress_dex_false", |
| 2790 | bp: ` |
| 2791 | android_app { |
| 2792 | name: "foo", |
| 2793 | srcs: ["a.java"], |
| 2794 | sdk_version: "current", |
| 2795 | uncompress_dex: false, |
| 2796 | } |
| 2797 | `, |
| 2798 | uncompressedPlatform: false, |
| 2799 | uncompressedUnbundled: false, |
| 2800 | }, |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | test := func(t *testing.T, bp string, want bool, unbundled bool) { |
| 2804 | t.Helper() |
| 2805 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 2806 | result := android.GroupFixturePreparers( |
| 2807 | prepareForJavaTest, |
Paul Duffin | 2645a29 | 2021-03-13 02:36:00 +0000 | [diff] [blame] | 2808 | PrepareForTestWithPrebuiltsOfCurrentApi, |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 2809 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 2810 | if unbundled { |
| 2811 | variables.Unbundled_build = proptools.BoolPtr(true) |
| 2812 | variables.Always_use_prebuilt_sdks = proptools.BoolPtr(true) |
| 2813 | } |
| 2814 | }), |
| 2815 | ).RunTestWithBp(t, bp) |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2816 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 2817 | foo := result.ModuleForTests("foo", "android_common") |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2818 | dex := foo.Rule("r8") |
| 2819 | uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0") |
| 2820 | aligned := foo.MaybeRule("zipalign").Rule != nil |
| 2821 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 2822 | android.AssertBoolEquals(t, "uncompressed in dex", want, uncompressedInDexJar) |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2823 | |
Paul Duffin | cdb88a9 | 2021-03-14 00:36:50 +0000 | [diff] [blame] | 2824 | android.AssertBoolEquals(t, "aligne", want, aligned) |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | for _, tt := range testCases { |
| 2828 | t.Run(tt.name, func(t *testing.T) { |
| 2829 | t.Run("platform", func(t *testing.T) { |
| 2830 | test(t, tt.bp, tt.uncompressedPlatform, false) |
| 2831 | }) |
| 2832 | t.Run("unbundled", func(t *testing.T) { |
| 2833 | test(t, tt.bp, tt.uncompressedUnbundled, true) |
| 2834 | }) |
| 2835 | }) |
| 2836 | } |
| 2837 | } |
Jaewoong Jung | 26dedd3 | 2019-06-06 08:45:58 -0700 | [diff] [blame] | 2838 | |
| 2839 | func checkAapt2LinkFlag(t *testing.T, aapt2Flags, flagName, expectedValue string) { |
| 2840 | if expectedValue != "" { |
| 2841 | expectedFlag := "--" + flagName + " " + expectedValue |
| 2842 | if !strings.Contains(aapt2Flags, expectedFlag) { |
| 2843 | t.Errorf("%q is missing in aapt2 link flags, %q", expectedFlag, aapt2Flags) |
| 2844 | } |
| 2845 | } else { |
| 2846 | unexpectedFlag := "--" + flagName |
| 2847 | if strings.Contains(aapt2Flags, unexpectedFlag) { |
| 2848 | t.Errorf("unexpected flag, %q is found in aapt2 link flags, %q", unexpectedFlag, aapt2Flags) |
| 2849 | } |
| 2850 | } |
| 2851 | } |
Jaewoong Jung | 9befb0c | 2020-01-18 10:33:43 -0800 | [diff] [blame] | 2852 | |
Cole Faust | 9a63131 | 2020-10-22 21:05:24 +0000 | [diff] [blame] | 2853 | func TestExportedProguardFlagFiles(t *testing.T) { |
| 2854 | ctx, _ := testJava(t, ` |
| 2855 | android_app { |
| 2856 | name: "foo", |
| 2857 | sdk_version: "current", |
| 2858 | static_libs: ["lib1"], |
| 2859 | } |
| 2860 | |
| 2861 | android_library { |
| 2862 | name: "lib1", |
| 2863 | sdk_version: "current", |
| 2864 | optimize: { |
| 2865 | proguard_flags_files: ["lib1proguard.cfg"], |
| 2866 | } |
| 2867 | } |
| 2868 | `) |
| 2869 | |
| 2870 | m := ctx.ModuleForTests("foo", "android_common") |
| 2871 | hasLib1Proguard := false |
| 2872 | for _, s := range m.Rule("java.r8").Implicits.Strings() { |
| 2873 | if s == "lib1proguard.cfg" { |
| 2874 | hasLib1Proguard = true |
| 2875 | break |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | if !hasLib1Proguard { |
| 2880 | t.Errorf("App does not use library proguard config") |
| 2881 | } |
| 2882 | } |