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