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 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 46 | func testAppContext(bp string, fs map[string][]byte) *android.TestContext { |
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 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 56 | return testContext(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 { |
| 60 | config := testConfig(nil) |
| 61 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 62 | ctx := testAppContext(bp, nil) |
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 | |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 178 | func TestResourceDirs(t *testing.T) { |
| 179 | testCases := []struct { |
| 180 | name string |
| 181 | prop string |
| 182 | resources []string |
| 183 | }{ |
| 184 | { |
| 185 | name: "no resource_dirs", |
| 186 | prop: "", |
| 187 | resources: []string{"res/res/values/strings.xml"}, |
| 188 | }, |
| 189 | { |
| 190 | name: "resource_dirs", |
| 191 | prop: `resource_dirs: ["res"]`, |
| 192 | resources: []string{"res/res/values/strings.xml"}, |
| 193 | }, |
| 194 | { |
| 195 | name: "empty resource_dirs", |
| 196 | prop: `resource_dirs: []`, |
| 197 | resources: nil, |
| 198 | }, |
| 199 | } |
| 200 | |
| 201 | fs := map[string][]byte{ |
| 202 | "res/res/values/strings.xml": nil, |
| 203 | } |
| 204 | |
| 205 | bp := ` |
| 206 | android_app { |
| 207 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 208 | sdk_version: "current", |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 209 | %s |
| 210 | } |
| 211 | ` |
| 212 | |
| 213 | for _, testCase := range testCases { |
| 214 | t.Run(testCase.name, func(t *testing.T) { |
| 215 | config := testConfig(nil) |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 216 | ctx := testContext(fmt.Sprintf(bp, testCase.prop), fs) |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 217 | run(t, ctx, config) |
| 218 | |
| 219 | module := ctx.ModuleForTests("foo", "android_common") |
| 220 | resourceList := module.MaybeOutput("aapt2/res.list") |
| 221 | |
| 222 | var resources []string |
| 223 | if resourceList.Rule != nil { |
| 224 | for _, compiledResource := range resourceList.Inputs.Strings() { |
| 225 | resources = append(resources, module.Output(compiledResource).Inputs.Strings()...) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if !reflect.DeepEqual(resources, testCase.resources) { |
| 230 | t.Errorf("expected resource files %q, got %q", |
| 231 | testCase.resources, resources) |
| 232 | } |
| 233 | }) |
| 234 | } |
| 235 | } |
| 236 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 237 | func TestAndroidResources(t *testing.T) { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 238 | testCases := []struct { |
| 239 | name string |
| 240 | enforceRROTargets []string |
| 241 | enforceRROExcludedOverlays []string |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 242 | resourceFiles map[string][]string |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 243 | overlayFiles map[string][]string |
| 244 | rroDirs map[string][]string |
| 245 | }{ |
| 246 | { |
| 247 | name: "no RRO", |
| 248 | enforceRROTargets: nil, |
| 249 | enforceRROExcludedOverlays: nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 250 | resourceFiles: map[string][]string{ |
| 251 | "foo": nil, |
| 252 | "bar": {"bar/res/res/values/strings.xml"}, |
| 253 | "lib": nil, |
| 254 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 255 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 256 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 257 | "foo": { |
| 258 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 259 | buildDir + "/.intermediates/lib/android_common/package-res.apk", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 260 | buildDir + "/.intermediates/lib3/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 261 | "foo/res/res/values/strings.xml", |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 262 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 263 | "device/vendor/blah/overlay/foo/res/values/strings.xml", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 264 | "product/vendor/blah/overlay/foo/res/values/strings.xml", |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 265 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 266 | "bar": { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 267 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
| 268 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
| 269 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 270 | "lib": { |
| 271 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
| 272 | "lib/res/res/values/strings.xml", |
| 273 | "device/vendor/blah/overlay/lib/res/values/strings.xml", |
| 274 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 275 | }, |
| 276 | rroDirs: map[string][]string{ |
| 277 | "foo": nil, |
| 278 | "bar": nil, |
| 279 | }, |
| 280 | }, |
| 281 | { |
| 282 | name: "enforce RRO on foo", |
| 283 | enforceRROTargets: []string{"foo"}, |
| 284 | enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"}, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 285 | resourceFiles: map[string][]string{ |
| 286 | "foo": nil, |
| 287 | "bar": {"bar/res/res/values/strings.xml"}, |
| 288 | "lib": nil, |
| 289 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 290 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 291 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 292 | "foo": { |
| 293 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 294 | buildDir + "/.intermediates/lib/android_common/package-res.apk", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 295 | buildDir + "/.intermediates/lib3/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 296 | "foo/res/res/values/strings.xml", |
| 297 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 298 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 299 | "bar": { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 300 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
| 301 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
| 302 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 303 | "lib": { |
| 304 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
| 305 | "lib/res/res/values/strings.xml", |
| 306 | "device/vendor/blah/overlay/lib/res/values/strings.xml", |
| 307 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 308 | }, |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 309 | |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 310 | rroDirs: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 311 | "foo": { |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 312 | "device:device/vendor/blah/overlay/foo/res", |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 313 | // Enforce RRO on "foo" could imply RRO on static dependencies, but for now it doesn't. |
| 314 | // "device/vendor/blah/overlay/lib/res", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 315 | "product:product/vendor/blah/overlay/foo/res", |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 316 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 317 | "bar": nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 318 | "lib": nil, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 319 | }, |
| 320 | }, |
| 321 | { |
| 322 | name: "enforce RRO on all", |
| 323 | enforceRROTargets: []string{"*"}, |
| 324 | enforceRROExcludedOverlays: []string{ |
| 325 | // Excluding specific apps/res directories also allowed. |
| 326 | "device/vendor/blah/static_overlay/foo", |
| 327 | "device/vendor/blah/static_overlay/bar/res", |
| 328 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 329 | resourceFiles: map[string][]string{ |
| 330 | "foo": nil, |
| 331 | "bar": {"bar/res/res/values/strings.xml"}, |
| 332 | "lib": nil, |
| 333 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 334 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 335 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 336 | "foo": { |
| 337 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 338 | buildDir + "/.intermediates/lib/android_common/package-res.apk", |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 339 | buildDir + "/.intermediates/lib3/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 340 | "foo/res/res/values/strings.xml", |
| 341 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 342 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 343 | "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"}, |
| 344 | "lib": { |
| 345 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
| 346 | "lib/res/res/values/strings.xml", |
| 347 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 348 | }, |
| 349 | rroDirs: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 350 | "foo": { |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 351 | "device:device/vendor/blah/overlay/foo/res", |
| 352 | "product:product/vendor/blah/overlay/foo/res", |
| 353 | // Lib dep comes after the direct deps |
| 354 | "device:device/vendor/blah/overlay/lib/res", |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 355 | }, |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 356 | "bar": {"device:device/vendor/blah/overlay/bar/res"}, |
| 357 | "lib": {"device:device/vendor/blah/overlay/lib/res"}, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 358 | }, |
| 359 | }, |
| 360 | } |
| 361 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 362 | deviceResourceOverlays := []string{ |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 363 | "device/vendor/blah/overlay", |
| 364 | "device/vendor/blah/overlay2", |
| 365 | "device/vendor/blah/static_overlay", |
| 366 | } |
| 367 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 368 | productResourceOverlays := []string{ |
| 369 | "product/vendor/blah/overlay", |
| 370 | } |
| 371 | |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 372 | fs := map[string][]byte{ |
| 373 | "foo/res/res/values/strings.xml": nil, |
| 374 | "bar/res/res/values/strings.xml": nil, |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 375 | "lib/res/res/values/strings.xml": nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 376 | "lib2/res/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 377 | "device/vendor/blah/overlay/foo/res/values/strings.xml": nil, |
| 378 | "device/vendor/blah/overlay/bar/res/values/strings.xml": nil, |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 379 | "device/vendor/blah/overlay/lib/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 380 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil, |
| 381 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil, |
| 382 | "device/vendor/blah/overlay2/res/values/strings.xml": nil, |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 383 | "product/vendor/blah/overlay/foo/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | bp := ` |
| 387 | android_app { |
| 388 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 389 | sdk_version: "current", |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 390 | resource_dirs: ["foo/res"], |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 391 | static_libs: ["lib", "lib3"], |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | android_app { |
| 395 | name: "bar", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 396 | sdk_version: "current", |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 397 | resource_dirs: ["bar/res"], |
| 398 | } |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 399 | |
| 400 | android_library { |
| 401 | name: "lib", |
| 402 | resource_dirs: ["lib/res"], |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 403 | static_libs: ["lib2"], |
| 404 | } |
| 405 | |
| 406 | android_library { |
| 407 | name: "lib2", |
| 408 | resource_dirs: ["lib2/res"], |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 409 | } |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 410 | |
| 411 | // This library has the same resources as lib (should not lead to dupe RROs) |
| 412 | android_library { |
| 413 | name: "lib3", |
| 414 | resource_dirs: ["lib/res"] |
| 415 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 416 | ` |
| 417 | |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 418 | for _, testCase := range testCases { |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 419 | t.Run(testCase.name, func(t *testing.T) { |
| 420 | config := testConfig(nil) |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 421 | config.TestProductVariables.DeviceResourceOverlays = deviceResourceOverlays |
| 422 | config.TestProductVariables.ProductResourceOverlays = productResourceOverlays |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 423 | if testCase.enforceRROTargets != nil { |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 424 | config.TestProductVariables.EnforceRROTargets = testCase.enforceRROTargets |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 425 | } |
| 426 | if testCase.enforceRROExcludedOverlays != nil { |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 427 | config.TestProductVariables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 430 | ctx := testAppContext(bp, fs) |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 431 | run(t, ctx, config) |
| 432 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 433 | resourceListToFiles := func(module android.TestingModule, list []string) (files []string) { |
| 434 | for _, o := range list { |
| 435 | res := module.MaybeOutput(o) |
| 436 | if res.Rule != nil { |
| 437 | // If the overlay is compiled as part of this module (i.e. a .arsc.flat file), |
| 438 | // verify the inputs to the .arsc.flat rule. |
| 439 | files = append(files, res.Inputs.Strings()...) |
| 440 | } else { |
| 441 | // Otherwise, verify the full path to the output of the other module |
| 442 | files = append(files, o) |
Anton Hansson | 94c93f3 | 2019-01-30 16:03:37 +0000 | [diff] [blame] | 443 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 444 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 445 | return files |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 448 | getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) { |
| 449 | module := ctx.ModuleForTests(moduleName, "android_common") |
| 450 | resourceList := module.MaybeOutput("aapt2/res.list") |
| 451 | if resourceList.Rule != nil { |
| 452 | resourceFiles = resourceListToFiles(module, resourceList.Inputs.Strings()) |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 453 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 454 | overlayList := module.MaybeOutput("aapt2/overlay.list") |
| 455 | if overlayList.Rule != nil { |
| 456 | overlayFiles = resourceListToFiles(module, overlayList.Inputs.Strings()) |
| 457 | } |
| 458 | |
Anton Hansson | 53c8844 | 2019-03-18 15:53:16 +0000 | [diff] [blame] | 459 | for _, d := range module.Module().(AndroidLibraryDependency).ExportedRRODirs() { |
| 460 | var prefix string |
| 461 | if d.overlayType == device { |
| 462 | prefix = "device:" |
| 463 | } else if d.overlayType == product { |
| 464 | prefix = "product:" |
| 465 | } else { |
| 466 | t.Fatalf("Unexpected overlayType %d", d.overlayType) |
| 467 | } |
| 468 | rroDirs = append(rroDirs, prefix+d.path.String()) |
| 469 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 470 | |
| 471 | return resourceFiles, overlayFiles, rroDirs |
| 472 | } |
| 473 | |
| 474 | modules := []string{"foo", "bar", "lib", "lib2"} |
| 475 | for _, module := range modules { |
| 476 | resourceFiles, overlayFiles, rroDirs := getResources(module) |
| 477 | |
| 478 | if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) { |
| 479 | t.Errorf("expected %s resource files:\n %#v\n got:\n %#v", |
| 480 | module, testCase.resourceFiles[module], resourceFiles) |
| 481 | } |
| 482 | if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) { |
| 483 | t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v", |
| 484 | module, testCase.overlayFiles[module], overlayFiles) |
| 485 | } |
| 486 | if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) { |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 487 | t.Errorf("expected %s rroDirs: %#v\n got:\n %#v", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 488 | module, testCase.rroDirs[module], rroDirs) |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 489 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 490 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 491 | }) |
| 492 | } |
| 493 | } |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 494 | |
| 495 | func TestAppSdkVersion(t *testing.T) { |
| 496 | testCases := []struct { |
| 497 | name string |
| 498 | sdkVersion string |
| 499 | platformSdkInt int |
| 500 | platformSdkCodename string |
| 501 | platformSdkFinal bool |
| 502 | expectedMinSdkVersion string |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 503 | platformApis bool |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 504 | }{ |
| 505 | { |
| 506 | name: "current final SDK", |
| 507 | sdkVersion: "current", |
| 508 | platformSdkInt: 27, |
| 509 | platformSdkCodename: "REL", |
| 510 | platformSdkFinal: true, |
| 511 | expectedMinSdkVersion: "27", |
| 512 | }, |
| 513 | { |
| 514 | name: "current non-final SDK", |
| 515 | sdkVersion: "current", |
| 516 | platformSdkInt: 27, |
| 517 | platformSdkCodename: "OMR1", |
| 518 | platformSdkFinal: false, |
| 519 | expectedMinSdkVersion: "OMR1", |
| 520 | }, |
| 521 | { |
| 522 | name: "default final SDK", |
| 523 | sdkVersion: "", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 524 | platformApis: true, |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 525 | platformSdkInt: 27, |
| 526 | platformSdkCodename: "REL", |
| 527 | platformSdkFinal: true, |
| 528 | expectedMinSdkVersion: "27", |
| 529 | }, |
| 530 | { |
| 531 | name: "default non-final SDK", |
| 532 | sdkVersion: "", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 533 | platformApis: true, |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 534 | platformSdkInt: 27, |
| 535 | platformSdkCodename: "OMR1", |
| 536 | platformSdkFinal: false, |
| 537 | expectedMinSdkVersion: "OMR1", |
| 538 | }, |
| 539 | { |
| 540 | name: "14", |
| 541 | sdkVersion: "14", |
| 542 | expectedMinSdkVersion: "14", |
| 543 | }, |
| 544 | } |
| 545 | |
| 546 | for _, moduleType := range []string{"android_app", "android_library"} { |
| 547 | for _, test := range testCases { |
| 548 | t.Run(moduleType+" "+test.name, func(t *testing.T) { |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 549 | platformApiProp := "" |
| 550 | if test.platformApis { |
| 551 | platformApiProp = "platform_apis: true," |
| 552 | } |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 553 | bp := fmt.Sprintf(`%s { |
| 554 | name: "foo", |
| 555 | srcs: ["a.java"], |
| 556 | sdk_version: "%s", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 557 | %s |
| 558 | }`, moduleType, test.sdkVersion, platformApiProp) |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 559 | |
| 560 | config := testConfig(nil) |
| 561 | config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt |
| 562 | config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename |
| 563 | config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal |
| 564 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 565 | ctx := testAppContext(bp, nil) |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 566 | |
| 567 | run(t, ctx, config) |
| 568 | |
| 569 | foo := ctx.ModuleForTests("foo", "android_common") |
| 570 | link := foo.Output("package-res.apk") |
| 571 | linkFlags := strings.Split(link.Args["flags"], " ") |
| 572 | min := android.IndexList("--min-sdk-version", linkFlags) |
| 573 | target := android.IndexList("--target-sdk-version", linkFlags) |
| 574 | |
| 575 | if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 { |
| 576 | t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags) |
| 577 | } |
| 578 | |
| 579 | gotMinSdkVersion := linkFlags[min+1] |
| 580 | gotTargetSdkVersion := linkFlags[target+1] |
| 581 | |
| 582 | if gotMinSdkVersion != test.expectedMinSdkVersion { |
| 583 | t.Errorf("incorrect --min-sdk-version, expected %q got %q", |
| 584 | test.expectedMinSdkVersion, gotMinSdkVersion) |
| 585 | } |
| 586 | |
| 587 | if gotTargetSdkVersion != test.expectedMinSdkVersion { |
| 588 | t.Errorf("incorrect --target-sdk-version, expected %q got %q", |
| 589 | test.expectedMinSdkVersion, gotTargetSdkVersion) |
| 590 | } |
| 591 | }) |
| 592 | } |
| 593 | } |
| 594 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 595 | |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 596 | func TestJNIABI(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 597 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 598 | cc_library { |
| 599 | name: "libjni", |
| 600 | system_shared_libs: [], |
| 601 | stl: "none", |
| 602 | } |
| 603 | |
| 604 | android_test { |
| 605 | name: "test", |
| 606 | sdk_version: "core_platform", |
| 607 | jni_libs: ["libjni"], |
| 608 | } |
| 609 | |
| 610 | android_test { |
| 611 | name: "test_first", |
| 612 | sdk_version: "core_platform", |
| 613 | compile_multilib: "first", |
| 614 | jni_libs: ["libjni"], |
| 615 | } |
| 616 | |
| 617 | android_test { |
| 618 | name: "test_both", |
| 619 | sdk_version: "core_platform", |
| 620 | compile_multilib: "both", |
| 621 | jni_libs: ["libjni"], |
| 622 | } |
| 623 | |
| 624 | android_test { |
| 625 | name: "test_32", |
| 626 | sdk_version: "core_platform", |
| 627 | compile_multilib: "32", |
| 628 | jni_libs: ["libjni"], |
| 629 | } |
| 630 | |
| 631 | android_test { |
| 632 | name: "test_64", |
| 633 | sdk_version: "core_platform", |
| 634 | compile_multilib: "64", |
| 635 | jni_libs: ["libjni"], |
| 636 | } |
| 637 | `) |
| 638 | |
| 639 | testCases := []struct { |
| 640 | name string |
| 641 | abis []string |
| 642 | }{ |
| 643 | {"test", []string{"arm64-v8a"}}, |
| 644 | {"test_first", []string{"arm64-v8a"}}, |
| 645 | {"test_both", []string{"arm64-v8a", "armeabi-v7a"}}, |
| 646 | {"test_32", []string{"armeabi-v7a"}}, |
| 647 | {"test_64", []string{"arm64-v8a"}}, |
| 648 | } |
| 649 | |
| 650 | for _, test := range testCases { |
| 651 | t.Run(test.name, func(t *testing.T) { |
| 652 | app := ctx.ModuleForTests(test.name, "android_common") |
| 653 | jniLibZip := app.Output("jnilibs.zip") |
| 654 | var abis []string |
| 655 | args := strings.Fields(jniLibZip.Args["jarArgs"]) |
| 656 | for i := 0; i < len(args); i++ { |
| 657 | if args[i] == "-P" { |
| 658 | abis = append(abis, filepath.Base(args[i+1])) |
| 659 | i++ |
| 660 | } |
| 661 | } |
| 662 | if !reflect.DeepEqual(abis, test.abis) { |
| 663 | t.Errorf("want abis %v, got %v", test.abis, abis) |
| 664 | } |
| 665 | }) |
| 666 | } |
| 667 | } |
| 668 | |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 669 | func TestJNIPackaging(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 670 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 671 | cc_library { |
| 672 | name: "libjni", |
| 673 | system_shared_libs: [], |
| 674 | stl: "none", |
| 675 | } |
| 676 | |
| 677 | android_app { |
| 678 | name: "app", |
| 679 | jni_libs: ["libjni"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 680 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | android_app { |
| 684 | name: "app_noembed", |
| 685 | jni_libs: ["libjni"], |
| 686 | use_embedded_native_libs: false, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 687 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | android_app { |
| 691 | name: "app_embed", |
| 692 | jni_libs: ["libjni"], |
| 693 | use_embedded_native_libs: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 694 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | android_test { |
| 698 | name: "test", |
| 699 | sdk_version: "core_platform", |
| 700 | jni_libs: ["libjni"], |
| 701 | } |
| 702 | |
| 703 | android_test { |
| 704 | name: "test_noembed", |
| 705 | sdk_version: "core_platform", |
| 706 | jni_libs: ["libjni"], |
| 707 | use_embedded_native_libs: false, |
| 708 | } |
| 709 | |
| 710 | android_test_helper_app { |
| 711 | name: "test_helper", |
| 712 | sdk_version: "core_platform", |
| 713 | jni_libs: ["libjni"], |
| 714 | } |
| 715 | |
| 716 | android_test_helper_app { |
| 717 | name: "test_helper_noembed", |
| 718 | sdk_version: "core_platform", |
| 719 | jni_libs: ["libjni"], |
| 720 | use_embedded_native_libs: false, |
| 721 | } |
| 722 | `) |
| 723 | |
| 724 | testCases := []struct { |
| 725 | name string |
| 726 | packaged bool |
| 727 | compressed bool |
| 728 | }{ |
| 729 | {"app", false, false}, |
| 730 | {"app_noembed", false, false}, |
| 731 | {"app_embed", true, false}, |
| 732 | {"test", true, false}, |
| 733 | {"test_noembed", true, true}, |
| 734 | {"test_helper", true, false}, |
| 735 | {"test_helper_noembed", true, true}, |
| 736 | } |
| 737 | |
| 738 | for _, test := range testCases { |
| 739 | t.Run(test.name, func(t *testing.T) { |
| 740 | app := ctx.ModuleForTests(test.name, "android_common") |
| 741 | jniLibZip := app.MaybeOutput("jnilibs.zip") |
| 742 | if g, w := (jniLibZip.Rule != nil), test.packaged; g != w { |
| 743 | t.Errorf("expected jni packaged %v, got %v", w, g) |
| 744 | } |
| 745 | |
| 746 | if jniLibZip.Rule != nil { |
| 747 | if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w { |
| 748 | t.Errorf("expected jni compressed %v, got %v", w, g) |
| 749 | } |
| 750 | } |
| 751 | }) |
| 752 | } |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 755 | func TestCertificates(t *testing.T) { |
| 756 | testCases := []struct { |
| 757 | name string |
| 758 | bp string |
| 759 | certificateOverride string |
| 760 | expected string |
| 761 | }{ |
| 762 | { |
| 763 | name: "default", |
| 764 | bp: ` |
| 765 | android_app { |
| 766 | name: "foo", |
| 767 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 768 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 769 | } |
| 770 | `, |
| 771 | certificateOverride: "", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 772 | 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] | 773 | }, |
| 774 | { |
| 775 | name: "module certificate property", |
| 776 | bp: ` |
| 777 | android_app { |
| 778 | name: "foo", |
| 779 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 780 | certificate: ":new_certificate", |
| 781 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | android_app_certificate { |
| 785 | name: "new_certificate", |
| 786 | certificate: "cert/new_cert", |
| 787 | } |
| 788 | `, |
| 789 | certificateOverride: "", |
| 790 | expected: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 791 | }, |
| 792 | { |
| 793 | name: "path certificate property", |
| 794 | bp: ` |
| 795 | android_app { |
| 796 | name: "foo", |
| 797 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 798 | certificate: "expiredkey", |
| 799 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 800 | } |
| 801 | `, |
| 802 | certificateOverride: "", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 803 | 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] | 804 | }, |
| 805 | { |
| 806 | name: "certificate overrides", |
| 807 | bp: ` |
| 808 | android_app { |
| 809 | name: "foo", |
| 810 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 811 | certificate: "expiredkey", |
| 812 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | android_app_certificate { |
| 816 | name: "new_certificate", |
| 817 | certificate: "cert/new_cert", |
| 818 | } |
| 819 | `, |
| 820 | certificateOverride: "foo:new_certificate", |
| 821 | expected: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 822 | }, |
| 823 | } |
| 824 | |
| 825 | for _, test := range testCases { |
| 826 | t.Run(test.name, func(t *testing.T) { |
| 827 | config := testConfig(nil) |
| 828 | if test.certificateOverride != "" { |
| 829 | config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride} |
| 830 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 831 | ctx := testAppContext(test.bp, nil) |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 832 | |
| 833 | run(t, ctx, config) |
| 834 | foo := ctx.ModuleForTests("foo", "android_common") |
| 835 | |
| 836 | signapk := foo.Output("foo.apk") |
| 837 | signFlags := signapk.Args["certificates"] |
| 838 | if test.expected != signFlags { |
| 839 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags) |
| 840 | } |
| 841 | }) |
| 842 | } |
| 843 | } |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 844 | |
| 845 | func TestPackageNameOverride(t *testing.T) { |
| 846 | testCases := []struct { |
| 847 | name string |
| 848 | bp string |
| 849 | packageNameOverride string |
| 850 | expected []string |
| 851 | }{ |
| 852 | { |
| 853 | name: "default", |
| 854 | bp: ` |
| 855 | android_app { |
| 856 | name: "foo", |
| 857 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 858 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 859 | } |
| 860 | `, |
| 861 | packageNameOverride: "", |
| 862 | expected: []string{ |
| 863 | buildDir + "/.intermediates/foo/android_common/foo.apk", |
| 864 | buildDir + "/target/product/test_device/system/app/foo/foo.apk", |
| 865 | }, |
| 866 | }, |
| 867 | { |
| 868 | name: "overridden", |
| 869 | bp: ` |
| 870 | android_app { |
| 871 | name: "foo", |
| 872 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 873 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 874 | } |
| 875 | `, |
| 876 | packageNameOverride: "foo:bar", |
| 877 | expected: []string{ |
| 878 | // The package apk should be still be the original name for test dependencies. |
| 879 | buildDir + "/.intermediates/foo/android_common/foo.apk", |
| 880 | buildDir + "/target/product/test_device/system/app/bar/bar.apk", |
| 881 | }, |
| 882 | }, |
| 883 | } |
| 884 | |
| 885 | for _, test := range testCases { |
| 886 | t.Run(test.name, func(t *testing.T) { |
| 887 | config := testConfig(nil) |
| 888 | if test.packageNameOverride != "" { |
| 889 | config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride} |
| 890 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 891 | ctx := testAppContext(test.bp, nil) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 892 | |
| 893 | run(t, ctx, config) |
| 894 | foo := ctx.ModuleForTests("foo", "android_common") |
| 895 | |
| 896 | outputs := foo.AllOutputs() |
| 897 | outputMap := make(map[string]bool) |
| 898 | for _, o := range outputs { |
| 899 | outputMap[o] = true |
| 900 | } |
| 901 | for _, e := range test.expected { |
| 902 | if _, exist := outputMap[e]; !exist { |
| 903 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs) |
| 904 | } |
| 905 | } |
| 906 | }) |
| 907 | } |
| 908 | } |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 909 | |
| 910 | func TestInstrumentationTargetOverridden(t *testing.T) { |
| 911 | bp := ` |
| 912 | android_app { |
| 913 | name: "foo", |
| 914 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 915 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | android_test { |
| 919 | name: "bar", |
| 920 | instrumentation_for: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 921 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 922 | } |
| 923 | ` |
| 924 | config := testConfig(nil) |
| 925 | config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"} |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 926 | ctx := testAppContext(bp, nil) |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 927 | |
| 928 | run(t, ctx, config) |
| 929 | |
| 930 | bar := ctx.ModuleForTests("bar", "android_common") |
| 931 | res := bar.Output("package-res.apk") |
| 932 | aapt2Flags := res.Args["flags"] |
| 933 | e := "--rename-instrumentation-target-package org.dandroid.bp" |
| 934 | if !strings.Contains(aapt2Flags, e) { |
| 935 | t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags) |
| 936 | } |
| 937 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 938 | |
| 939 | func TestOverrideAndroidApp(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 940 | ctx, _ := testJava(t, ` |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 941 | android_app { |
| 942 | name: "foo", |
| 943 | srcs: ["a.java"], |
Jaewoong Jung | a641ee9 | 2019-03-27 11:17:14 -0700 | [diff] [blame] | 944 | certificate: "expiredkey", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 945 | overrides: ["qux"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 946 | sdk_version: "current", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | override_android_app { |
| 950 | name: "bar", |
| 951 | base: "foo", |
| 952 | certificate: ":new_certificate", |
| 953 | } |
| 954 | |
| 955 | android_app_certificate { |
| 956 | name: "new_certificate", |
| 957 | certificate: "cert/new_cert", |
| 958 | } |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 959 | |
| 960 | override_android_app { |
| 961 | name: "baz", |
| 962 | base: "foo", |
| 963 | package_name: "org.dandroid.bp", |
| 964 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 965 | `) |
| 966 | |
| 967 | expectedVariants := []struct { |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 968 | moduleName string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 969 | variantName string |
| 970 | apkName string |
| 971 | apkPath string |
| 972 | signFlag string |
| 973 | overrides []string |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 974 | aaptFlag string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 975 | }{ |
| 976 | { |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 977 | moduleName: "foo", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 978 | variantName: "android_common", |
| 979 | apkPath: "/target/product/test_device/system/app/foo/foo.apk", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 980 | 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] | 981 | overrides: []string{"qux"}, |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 982 | aaptFlag: "", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 983 | }, |
| 984 | { |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 985 | moduleName: "bar", |
| 986 | variantName: "android_common_bar", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 987 | apkPath: "/target/product/test_device/system/app/bar/bar.apk", |
| 988 | signFlag: "cert/new_cert.x509.pem cert/new_cert.pk8", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 989 | overrides: []string{"qux", "foo"}, |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 990 | aaptFlag: "", |
| 991 | }, |
| 992 | { |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 993 | moduleName: "baz", |
| 994 | variantName: "android_common_baz", |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 995 | apkPath: "/target/product/test_device/system/app/baz/baz.apk", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 996 | 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] | 997 | overrides: []string{"qux", "foo"}, |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 998 | aaptFlag: "--rename-manifest-package org.dandroid.bp", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 999 | }, |
| 1000 | } |
| 1001 | for _, expected := range expectedVariants { |
| 1002 | variant := ctx.ModuleForTests("foo", expected.variantName) |
| 1003 | |
| 1004 | // Check the final apk name |
| 1005 | outputs := variant.AllOutputs() |
| 1006 | expectedApkPath := buildDir + expected.apkPath |
| 1007 | found := false |
| 1008 | for _, o := range outputs { |
| 1009 | if o == expectedApkPath { |
| 1010 | found = true |
| 1011 | break |
| 1012 | } |
| 1013 | } |
| 1014 | if !found { |
| 1015 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs) |
| 1016 | } |
| 1017 | |
| 1018 | // Check the certificate paths |
| 1019 | signapk := variant.Output("foo.apk") |
| 1020 | signFlag := signapk.Args["certificates"] |
| 1021 | if expected.signFlag != signFlag { |
| 1022 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected.signFlag, signFlag) |
| 1023 | } |
| 1024 | |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1025 | // Check if the overrides field values are correctly aggregated. |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1026 | mod := variant.Module().(*AndroidApp) |
| 1027 | if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) { |
| 1028 | t.Errorf("Incorrect overrides property value, expected: %q, got: %q", |
| 1029 | expected.overrides, mod.appProperties.Overrides) |
| 1030 | } |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1031 | |
| 1032 | // Check the package renaming flag, if exists. |
| 1033 | res := variant.Output("package-res.apk") |
| 1034 | aapt2Flags := res.Args["flags"] |
| 1035 | if !strings.Contains(aapt2Flags, expected.aaptFlag) { |
| 1036 | t.Errorf("package renaming flag, %q is missing in aapt2 link flags, %q", expected.aaptFlag, aapt2Flags) |
| 1037 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1038 | } |
| 1039 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1040 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1041 | func TestOverrideAndroidAppDependency(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1042 | ctx, _ := testJava(t, ` |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1043 | android_app { |
| 1044 | name: "foo", |
| 1045 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1046 | sdk_version: "current", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | override_android_app { |
| 1050 | name: "bar", |
| 1051 | base: "foo", |
| 1052 | package_name: "org.dandroid.bp", |
| 1053 | } |
| 1054 | |
| 1055 | android_test { |
| 1056 | name: "baz", |
| 1057 | srcs: ["b.java"], |
| 1058 | instrumentation_for: "foo", |
| 1059 | } |
| 1060 | |
| 1061 | android_test { |
| 1062 | name: "qux", |
| 1063 | srcs: ["b.java"], |
| 1064 | instrumentation_for: "bar", |
| 1065 | } |
| 1066 | `) |
| 1067 | |
| 1068 | // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg. |
| 1069 | javac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1070 | fooTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 1071 | if !strings.Contains(javac.Args["classpath"], fooTurbine) { |
| 1072 | t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine) |
| 1073 | } |
| 1074 | |
| 1075 | // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg. |
| 1076 | javac = ctx.ModuleForTests("qux", "android_common").Rule("javac") |
| 1077 | barTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common_bar", "turbine-combined", "foo.jar") |
| 1078 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 1079 | t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
| 1080 | } |
| 1081 | } |
| 1082 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1083 | func TestAndroidAppImport(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1084 | ctx, _ := testJava(t, ` |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1085 | android_app_import { |
| 1086 | name: "foo", |
| 1087 | apk: "prebuilts/apk/app.apk", |
| 1088 | certificate: "platform", |
| 1089 | dex_preopt: { |
| 1090 | enabled: true, |
| 1091 | }, |
| 1092 | } |
| 1093 | `) |
| 1094 | |
| 1095 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1096 | |
| 1097 | // Check dexpreopt outputs. |
| 1098 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil || |
| 1099 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil { |
| 1100 | t.Errorf("can't find dexpreopt outputs") |
| 1101 | } |
| 1102 | |
| 1103 | // Check cert signing flag. |
| 1104 | signedApk := variant.Output("signed/foo.apk") |
| 1105 | signingFlag := signedApk.Args["certificates"] |
| 1106 | expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8" |
| 1107 | if expected != signingFlag { |
| 1108 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag) |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | func TestAndroidAppImport_NoDexPreopt(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1113 | ctx, _ := testJava(t, ` |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1114 | android_app_import { |
| 1115 | name: "foo", |
| 1116 | apk: "prebuilts/apk/app.apk", |
| 1117 | certificate: "platform", |
| 1118 | dex_preopt: { |
| 1119 | enabled: false, |
| 1120 | }, |
| 1121 | } |
| 1122 | `) |
| 1123 | |
| 1124 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1125 | |
| 1126 | // Check dexpreopt outputs. They shouldn't exist. |
| 1127 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule != nil || |
| 1128 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule != nil { |
| 1129 | t.Errorf("dexpreopt shouldn't have run.") |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | func TestAndroidAppImport_Presigned(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1134 | ctx, _ := testJava(t, ` |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1135 | android_app_import { |
| 1136 | name: "foo", |
| 1137 | apk: "prebuilts/apk/app.apk", |
| 1138 | presigned: true, |
| 1139 | dex_preopt: { |
| 1140 | enabled: true, |
| 1141 | }, |
| 1142 | } |
| 1143 | `) |
| 1144 | |
| 1145 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1146 | |
| 1147 | // Check dexpreopt outputs. |
| 1148 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil || |
| 1149 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil { |
| 1150 | t.Errorf("can't find dexpreopt outputs") |
| 1151 | } |
| 1152 | // Make sure stripping wasn't done. |
| 1153 | stripRule := variant.Output("dexpreopt/foo.apk") |
| 1154 | if !strings.HasPrefix(stripRule.RuleParams.Command, "cp -f") { |
| 1155 | t.Errorf("unexpected, non-skipping strip command: %q", stripRule.RuleParams.Command) |
| 1156 | } |
| 1157 | |
| 1158 | // Make sure signing was skipped and aligning was done instead. |
| 1159 | if variant.MaybeOutput("signed/foo.apk").Rule != nil { |
| 1160 | t.Errorf("signing rule shouldn't be included.") |
| 1161 | } |
| 1162 | if variant.MaybeOutput("zip-aligned/foo.apk").Rule == nil { |
| 1163 | t.Errorf("can't find aligning rule") |
| 1164 | } |
| 1165 | } |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1166 | |
| 1167 | func TestAndroidAppImport_DpiVariants(t *testing.T) { |
| 1168 | bp := ` |
| 1169 | android_app_import { |
| 1170 | name: "foo", |
| 1171 | apk: "prebuilts/apk/app.apk", |
| 1172 | dpi_variants: { |
| 1173 | xhdpi: { |
| 1174 | apk: "prebuilts/apk/app_xhdpi.apk", |
| 1175 | }, |
| 1176 | xxhdpi: { |
| 1177 | apk: "prebuilts/apk/app_xxhdpi.apk", |
| 1178 | }, |
| 1179 | }, |
| 1180 | certificate: "PRESIGNED", |
| 1181 | dex_preopt: { |
| 1182 | enabled: true, |
| 1183 | }, |
| 1184 | } |
| 1185 | ` |
| 1186 | testCases := []struct { |
| 1187 | name string |
| 1188 | aaptPreferredConfig *string |
| 1189 | aaptPrebuiltDPI []string |
| 1190 | expected string |
| 1191 | }{ |
| 1192 | { |
| 1193 | name: "no preferred", |
| 1194 | aaptPreferredConfig: nil, |
| 1195 | aaptPrebuiltDPI: []string{}, |
| 1196 | expected: "prebuilts/apk/app.apk", |
| 1197 | }, |
| 1198 | { |
| 1199 | name: "AAPTPreferredConfig matches", |
| 1200 | aaptPreferredConfig: proptools.StringPtr("xhdpi"), |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 1201 | aaptPrebuiltDPI: []string{"xxhdpi", "ldpi"}, |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1202 | expected: "prebuilts/apk/app_xhdpi.apk", |
| 1203 | }, |
| 1204 | { |
| 1205 | name: "AAPTPrebuiltDPI matches", |
| 1206 | aaptPreferredConfig: proptools.StringPtr("mdpi"), |
| 1207 | aaptPrebuiltDPI: []string{"xxhdpi", "xhdpi"}, |
| 1208 | expected: "prebuilts/apk/app_xxhdpi.apk", |
| 1209 | }, |
| 1210 | { |
| 1211 | name: "non-first AAPTPrebuiltDPI matches", |
| 1212 | aaptPreferredConfig: proptools.StringPtr("mdpi"), |
| 1213 | aaptPrebuiltDPI: []string{"ldpi", "xhdpi"}, |
| 1214 | expected: "prebuilts/apk/app_xhdpi.apk", |
| 1215 | }, |
| 1216 | { |
| 1217 | name: "no matches", |
| 1218 | aaptPreferredConfig: proptools.StringPtr("mdpi"), |
| 1219 | aaptPrebuiltDPI: []string{"ldpi", "xxxhdpi"}, |
| 1220 | expected: "prebuilts/apk/app.apk", |
| 1221 | }, |
| 1222 | } |
| 1223 | |
| 1224 | jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)") |
| 1225 | for _, test := range testCases { |
| 1226 | config := testConfig(nil) |
| 1227 | config.TestProductVariables.AAPTPreferredConfig = test.aaptPreferredConfig |
| 1228 | config.TestProductVariables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1229 | ctx := testAppContext(bp, nil) |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1230 | |
| 1231 | run(t, ctx, config) |
| 1232 | |
| 1233 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1234 | jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command |
| 1235 | matches := jniRuleRe.FindStringSubmatch(jniRuleCommand) |
| 1236 | if len(matches) != 2 { |
| 1237 | t.Errorf("failed to extract the src apk path from %q", jniRuleCommand) |
| 1238 | } |
| 1239 | if test.expected != matches[1] { |
| 1240 | t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1]) |
| 1241 | } |
| 1242 | } |
| 1243 | } |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1244 | |
Jaewoong Jung | 8aae22e | 2019-07-17 10:21:49 -0700 | [diff] [blame] | 1245 | func TestAndroidAppImport_Filename(t *testing.T) { |
| 1246 | ctx, config := testJava(t, ` |
| 1247 | android_app_import { |
| 1248 | name: "foo", |
| 1249 | apk: "prebuilts/apk/app.apk", |
| 1250 | presigned: true, |
| 1251 | } |
| 1252 | |
| 1253 | android_app_import { |
| 1254 | name: "bar", |
| 1255 | apk: "prebuilts/apk/app.apk", |
| 1256 | presigned: true, |
| 1257 | filename: "bar_sample.apk" |
| 1258 | } |
| 1259 | `) |
| 1260 | |
| 1261 | testCases := []struct { |
| 1262 | name string |
| 1263 | expected string |
| 1264 | }{ |
| 1265 | { |
| 1266 | name: "foo", |
| 1267 | expected: "foo.apk", |
| 1268 | }, |
| 1269 | { |
| 1270 | name: "bar", |
| 1271 | expected: "bar_sample.apk", |
| 1272 | }, |
| 1273 | } |
| 1274 | |
| 1275 | for _, test := range testCases { |
| 1276 | variant := ctx.ModuleForTests(test.name, "android_common") |
| 1277 | if variant.MaybeOutput(test.expected).Rule == nil { |
| 1278 | t.Errorf("can't find output named %q - all outputs: %v", test.expected, variant.AllOutputs()) |
| 1279 | } |
| 1280 | |
| 1281 | a := variant.Module().(*AndroidAppImport) |
| 1282 | expectedValues := []string{test.expected} |
| 1283 | actualValues := android.AndroidMkEntriesForTest( |
| 1284 | t, config, "", a).EntryMap["LOCAL_INSTALLED_MODULE_STEM"] |
| 1285 | if !reflect.DeepEqual(actualValues, expectedValues) { |
| 1286 | t.Errorf("Incorrect LOCAL_INSTALLED_MODULE_STEM value '%s', expected '%s'", |
| 1287 | actualValues, expectedValues) |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame^] | 1292 | func TestAndroidAppImport_ArchVariants(t *testing.T) { |
| 1293 | // The test config's target arch is ARM64. |
| 1294 | testCases := []struct { |
| 1295 | name string |
| 1296 | bp string |
| 1297 | expected string |
| 1298 | }{ |
| 1299 | { |
| 1300 | name: "matching arch", |
| 1301 | bp: ` |
| 1302 | android_app_import { |
| 1303 | name: "foo", |
| 1304 | apk: "prebuilts/apk/app.apk", |
| 1305 | arch: { |
| 1306 | arm64: { |
| 1307 | apk: "prebuilts/apk/app_arm64.apk", |
| 1308 | }, |
| 1309 | }, |
| 1310 | certificate: "PRESIGNED", |
| 1311 | dex_preopt: { |
| 1312 | enabled: true, |
| 1313 | }, |
| 1314 | } |
| 1315 | `, |
| 1316 | expected: "prebuilts/apk/app_arm64.apk", |
| 1317 | }, |
| 1318 | { |
| 1319 | name: "no matching arch", |
| 1320 | bp: ` |
| 1321 | android_app_import { |
| 1322 | name: "foo", |
| 1323 | apk: "prebuilts/apk/app.apk", |
| 1324 | arch: { |
| 1325 | arm: { |
| 1326 | apk: "prebuilts/apk/app_arm.apk", |
| 1327 | }, |
| 1328 | }, |
| 1329 | certificate: "PRESIGNED", |
| 1330 | dex_preopt: { |
| 1331 | enabled: true, |
| 1332 | }, |
| 1333 | } |
| 1334 | `, |
| 1335 | expected: "prebuilts/apk/app.apk", |
| 1336 | }, |
| 1337 | } |
| 1338 | |
| 1339 | jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)") |
| 1340 | for _, test := range testCases { |
| 1341 | ctx, _ := testJava(t, test.bp) |
| 1342 | |
| 1343 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1344 | jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command |
| 1345 | matches := jniRuleRe.FindStringSubmatch(jniRuleCommand) |
| 1346 | if len(matches) != 2 { |
| 1347 | t.Errorf("failed to extract the src apk path from %q", jniRuleCommand) |
| 1348 | } |
| 1349 | if test.expected != matches[1] { |
| 1350 | t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1]) |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1355 | func TestStl(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1356 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1357 | cc_library { |
| 1358 | name: "libjni", |
| 1359 | } |
| 1360 | |
| 1361 | android_test { |
| 1362 | name: "stl", |
| 1363 | jni_libs: ["libjni"], |
| 1364 | compile_multilib: "both", |
| 1365 | sdk_version: "current", |
| 1366 | stl: "c++_shared", |
| 1367 | } |
| 1368 | |
| 1369 | android_test { |
| 1370 | name: "system", |
| 1371 | jni_libs: ["libjni"], |
| 1372 | compile_multilib: "both", |
| 1373 | sdk_version: "current", |
| 1374 | } |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 1375 | |
| 1376 | ndk_prebuilt_shared_stl { |
| 1377 | name: "ndk_libc++_shared", |
| 1378 | } |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1379 | `) |
| 1380 | |
| 1381 | testCases := []struct { |
| 1382 | name string |
| 1383 | jnis []string |
| 1384 | }{ |
| 1385 | {"stl", |
| 1386 | []string{ |
| 1387 | "libjni.so", |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 1388 | "libc++_shared.so", |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1389 | }, |
| 1390 | }, |
| 1391 | {"system", |
| 1392 | []string{ |
| 1393 | "libjni.so", |
| 1394 | }, |
| 1395 | }, |
| 1396 | } |
| 1397 | |
| 1398 | for _, test := range testCases { |
| 1399 | t.Run(test.name, func(t *testing.T) { |
| 1400 | app := ctx.ModuleForTests(test.name, "android_common") |
| 1401 | jniLibZip := app.Output("jnilibs.zip") |
| 1402 | var jnis []string |
| 1403 | args := strings.Fields(jniLibZip.Args["jarArgs"]) |
| 1404 | for i := 0; i < len(args); i++ { |
| 1405 | if args[i] == "-f" { |
| 1406 | jnis = append(jnis, args[i+1]) |
| 1407 | i += 1 |
| 1408 | } |
| 1409 | } |
| 1410 | jnisJoined := strings.Join(jnis, " ") |
| 1411 | for _, jni := range test.jnis { |
| 1412 | if !strings.Contains(jnisJoined, jni) { |
| 1413 | t.Errorf("missing jni %q in %q", jni, jnis) |
| 1414 | } |
| 1415 | } |
| 1416 | }) |
| 1417 | } |
| 1418 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1419 | |
| 1420 | func TestUsesLibraries(t *testing.T) { |
| 1421 | bp := ` |
| 1422 | java_sdk_library { |
| 1423 | name: "foo", |
| 1424 | srcs: ["a.java"], |
| 1425 | api_packages: ["foo"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1426 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | java_sdk_library { |
| 1430 | name: "bar", |
| 1431 | srcs: ["a.java"], |
| 1432 | api_packages: ["bar"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1433 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | android_app { |
| 1437 | name: "app", |
| 1438 | srcs: ["a.java"], |
| 1439 | uses_libs: ["foo"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1440 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1441 | optional_uses_libs: [ |
| 1442 | "bar", |
| 1443 | "baz", |
| 1444 | ], |
| 1445 | } |
| 1446 | |
| 1447 | android_app_import { |
| 1448 | name: "prebuilt", |
| 1449 | apk: "prebuilts/apk/app.apk", |
| 1450 | certificate: "platform", |
| 1451 | uses_libs: ["foo"], |
| 1452 | optional_uses_libs: [ |
| 1453 | "bar", |
| 1454 | "baz", |
| 1455 | ], |
| 1456 | } |
| 1457 | ` |
| 1458 | |
| 1459 | config := testConfig(nil) |
| 1460 | config.TestProductVariables.MissingUsesLibraries = []string{"baz"} |
| 1461 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1462 | ctx := testAppContext(bp, nil) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1463 | |
| 1464 | run(t, ctx, config) |
| 1465 | |
| 1466 | app := ctx.ModuleForTests("app", "android_common") |
| 1467 | prebuilt := ctx.ModuleForTests("prebuilt", "android_common") |
| 1468 | |
| 1469 | // Test that all libraries are verified |
| 1470 | cmd := app.Rule("verify_uses_libraries").RuleParams.Command |
| 1471 | if w := "--uses-library foo"; !strings.Contains(cmd, w) { |
| 1472 | t.Errorf("wanted %q in %q", w, cmd) |
| 1473 | } |
| 1474 | |
| 1475 | if w := "--optional-uses-library bar --optional-uses-library baz"; !strings.Contains(cmd, w) { |
| 1476 | t.Errorf("wanted %q in %q", w, cmd) |
| 1477 | } |
| 1478 | |
| 1479 | cmd = prebuilt.Rule("verify_uses_libraries").RuleParams.Command |
| 1480 | |
| 1481 | if w := `uses_library_names="foo"`; !strings.Contains(cmd, w) { |
| 1482 | t.Errorf("wanted %q in %q", w, cmd) |
| 1483 | } |
| 1484 | |
| 1485 | if w := `optional_uses_library_names="bar baz"`; !strings.Contains(cmd, w) { |
| 1486 | t.Errorf("wanted %q in %q", w, cmd) |
| 1487 | } |
| 1488 | |
| 1489 | // Test that only present libraries are preopted |
| 1490 | cmd = app.Rule("dexpreopt").RuleParams.Command |
| 1491 | |
| 1492 | if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) { |
| 1493 | t.Errorf("wanted %q in %q", w, cmd) |
| 1494 | } |
| 1495 | |
| 1496 | cmd = prebuilt.Rule("dexpreopt").RuleParams.Command |
| 1497 | |
| 1498 | if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) { |
| 1499 | t.Errorf("wanted %q in %q", w, cmd) |
| 1500 | } |
| 1501 | } |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1502 | |
| 1503 | func TestCodelessApp(t *testing.T) { |
| 1504 | testCases := []struct { |
| 1505 | name string |
| 1506 | bp string |
| 1507 | noCode bool |
| 1508 | }{ |
| 1509 | { |
| 1510 | name: "normal", |
| 1511 | bp: ` |
| 1512 | android_app { |
| 1513 | name: "foo", |
| 1514 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1515 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1516 | } |
| 1517 | `, |
| 1518 | noCode: false, |
| 1519 | }, |
| 1520 | { |
| 1521 | name: "app without sources", |
| 1522 | bp: ` |
| 1523 | android_app { |
| 1524 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1525 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1526 | } |
| 1527 | `, |
| 1528 | noCode: true, |
| 1529 | }, |
| 1530 | { |
| 1531 | name: "app with libraries", |
| 1532 | bp: ` |
| 1533 | android_app { |
| 1534 | name: "foo", |
| 1535 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1536 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | java_library { |
| 1540 | name: "lib", |
| 1541 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1542 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1543 | } |
| 1544 | `, |
| 1545 | noCode: false, |
| 1546 | }, |
| 1547 | { |
| 1548 | name: "app with sourceless libraries", |
| 1549 | bp: ` |
| 1550 | android_app { |
| 1551 | name: "foo", |
| 1552 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1553 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | java_library { |
| 1557 | name: "lib", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1558 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1559 | } |
| 1560 | `, |
| 1561 | // TODO(jungjw): this should probably be true |
| 1562 | noCode: false, |
| 1563 | }, |
| 1564 | } |
| 1565 | |
| 1566 | for _, test := range testCases { |
| 1567 | t.Run(test.name, func(t *testing.T) { |
| 1568 | ctx := testApp(t, test.bp) |
| 1569 | |
| 1570 | foo := ctx.ModuleForTests("foo", "android_common") |
| 1571 | manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"] |
| 1572 | if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode { |
| 1573 | t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs) |
| 1574 | } |
| 1575 | }) |
| 1576 | } |
| 1577 | } |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1578 | |
| 1579 | func TestEmbedNotice(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1580 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1581 | android_app { |
| 1582 | name: "foo", |
| 1583 | srcs: ["a.java"], |
| 1584 | static_libs: ["javalib"], |
| 1585 | jni_libs: ["libjni"], |
| 1586 | notice: "APP_NOTICE", |
| 1587 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1588 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | // No embed_notice flag |
| 1592 | android_app { |
| 1593 | name: "bar", |
| 1594 | srcs: ["a.java"], |
| 1595 | jni_libs: ["libjni"], |
| 1596 | notice: "APP_NOTICE", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1597 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | // No NOTICE files |
| 1601 | android_app { |
| 1602 | name: "baz", |
| 1603 | srcs: ["a.java"], |
| 1604 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1605 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | cc_library { |
| 1609 | name: "libjni", |
| 1610 | system_shared_libs: [], |
| 1611 | stl: "none", |
| 1612 | notice: "LIB_NOTICE", |
| 1613 | } |
| 1614 | |
| 1615 | java_library { |
| 1616 | name: "javalib", |
| 1617 | srcs: [ |
| 1618 | ":gen", |
| 1619 | ], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1620 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | genrule { |
| 1624 | name: "gen", |
| 1625 | tools: ["gentool"], |
| 1626 | out: ["gen.java"], |
| 1627 | notice: "GENRULE_NOTICE", |
| 1628 | } |
| 1629 | |
| 1630 | java_binary_host { |
| 1631 | name: "gentool", |
| 1632 | srcs: ["b.java"], |
| 1633 | notice: "TOOL_NOTICE", |
| 1634 | } |
| 1635 | `) |
| 1636 | |
| 1637 | // foo has NOTICE files to process, and embed_notices is true. |
| 1638 | foo := ctx.ModuleForTests("foo", "android_common") |
| 1639 | // verify merge notices rule. |
| 1640 | mergeNotices := foo.Rule("mergeNoticesRule") |
| 1641 | noticeInputs := mergeNotices.Inputs.Strings() |
| 1642 | // TOOL_NOTICE should be excluded as it's a host module. |
| 1643 | if len(mergeNotices.Inputs) != 3 { |
| 1644 | t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs) |
| 1645 | } |
| 1646 | if !inList("APP_NOTICE", noticeInputs) { |
| 1647 | t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs) |
| 1648 | } |
| 1649 | if !inList("LIB_NOTICE", noticeInputs) { |
| 1650 | t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs) |
| 1651 | } |
| 1652 | if !inList("GENRULE_NOTICE", noticeInputs) { |
| 1653 | t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs) |
| 1654 | } |
| 1655 | // aapt2 flags should include -A <NOTICE dir> so that its contents are put in the APK's /assets. |
| 1656 | res := foo.Output("package-res.apk") |
| 1657 | aapt2Flags := res.Args["flags"] |
| 1658 | e := "-A " + buildDir + "/.intermediates/foo/android_common/NOTICE" |
| 1659 | if !strings.Contains(aapt2Flags, e) { |
| 1660 | t.Errorf("asset dir flag for NOTICE, %q is missing in aapt2 link flags, %q", e, aapt2Flags) |
| 1661 | } |
| 1662 | |
| 1663 | // bar has NOTICE files to process, but embed_notices is not set. |
| 1664 | bar := ctx.ModuleForTests("bar", "android_common") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 1665 | res = bar.Output("package-res.apk") |
| 1666 | aapt2Flags = res.Args["flags"] |
| 1667 | e = "-A " + buildDir + "/.intermediates/bar/android_common/NOTICE" |
| 1668 | if strings.Contains(aapt2Flags, e) { |
| 1669 | 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] | 1670 | } |
| 1671 | |
| 1672 | // baz's embed_notice is true, but it doesn't have any NOTICE files. |
| 1673 | baz := ctx.ModuleForTests("baz", "android_common") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 1674 | res = baz.Output("package-res.apk") |
| 1675 | aapt2Flags = res.Args["flags"] |
| 1676 | e = "-A " + buildDir + "/.intermediates/baz/android_common/NOTICE" |
| 1677 | if strings.Contains(aapt2Flags, e) { |
| 1678 | 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] | 1679 | } |
| 1680 | } |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1681 | |
| 1682 | func TestUncompressDex(t *testing.T) { |
| 1683 | testCases := []struct { |
| 1684 | name string |
| 1685 | bp string |
| 1686 | |
| 1687 | uncompressedPlatform bool |
| 1688 | uncompressedUnbundled bool |
| 1689 | }{ |
| 1690 | { |
| 1691 | name: "normal", |
| 1692 | bp: ` |
| 1693 | android_app { |
| 1694 | name: "foo", |
| 1695 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1696 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1697 | } |
| 1698 | `, |
| 1699 | uncompressedPlatform: true, |
| 1700 | uncompressedUnbundled: false, |
| 1701 | }, |
| 1702 | { |
| 1703 | name: "use_embedded_dex", |
| 1704 | bp: ` |
| 1705 | android_app { |
| 1706 | name: "foo", |
| 1707 | use_embedded_dex: true, |
| 1708 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1709 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1710 | } |
| 1711 | `, |
| 1712 | uncompressedPlatform: true, |
| 1713 | uncompressedUnbundled: true, |
| 1714 | }, |
| 1715 | { |
| 1716 | name: "privileged", |
| 1717 | bp: ` |
| 1718 | android_app { |
| 1719 | name: "foo", |
| 1720 | privileged: true, |
| 1721 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1722 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1723 | } |
| 1724 | `, |
| 1725 | uncompressedPlatform: true, |
| 1726 | uncompressedUnbundled: true, |
| 1727 | }, |
| 1728 | } |
| 1729 | |
| 1730 | test := func(t *testing.T, bp string, want bool, unbundled bool) { |
| 1731 | t.Helper() |
| 1732 | |
| 1733 | config := testConfig(nil) |
| 1734 | if unbundled { |
| 1735 | config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true) |
| 1736 | } |
| 1737 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1738 | ctx := testAppContext(bp, nil) |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1739 | |
| 1740 | run(t, ctx, config) |
| 1741 | |
| 1742 | foo := ctx.ModuleForTests("foo", "android_common") |
| 1743 | dex := foo.Rule("r8") |
| 1744 | uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0") |
| 1745 | aligned := foo.MaybeRule("zipalign").Rule != nil |
| 1746 | |
| 1747 | if uncompressedInDexJar != want { |
| 1748 | t.Errorf("want uncompressed in dex %v, got %v", want, uncompressedInDexJar) |
| 1749 | } |
| 1750 | |
| 1751 | if aligned != want { |
| 1752 | t.Errorf("want aligned %v, got %v", want, aligned) |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | for _, tt := range testCases { |
| 1757 | t.Run(tt.name, func(t *testing.T) { |
| 1758 | t.Run("platform", func(t *testing.T) { |
| 1759 | test(t, tt.bp, tt.uncompressedPlatform, false) |
| 1760 | }) |
| 1761 | t.Run("unbundled", func(t *testing.T) { |
| 1762 | test(t, tt.bp, tt.uncompressedUnbundled, true) |
| 1763 | }) |
| 1764 | }) |
| 1765 | } |
| 1766 | } |