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 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame^] | 669 | func TestAppSdkVersionByPartition(t *testing.T) { |
| 670 | testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", ` |
| 671 | android_app { |
| 672 | name: "foo", |
| 673 | srcs: ["a.java"], |
| 674 | vendor: true, |
| 675 | platform_apis: true, |
| 676 | } |
| 677 | `) |
| 678 | |
| 679 | testJava(t, ` |
| 680 | android_app { |
| 681 | name: "bar", |
| 682 | srcs: ["b.java"], |
| 683 | platform_apis: true, |
| 684 | } |
| 685 | `) |
| 686 | |
| 687 | for _, enforce := range []bool{true, false} { |
| 688 | |
| 689 | config := testConfig(nil) |
| 690 | config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
| 691 | bp := ` |
| 692 | android_app { |
| 693 | name: "foo", |
| 694 | srcs: ["a.java"], |
| 695 | product_specific: true, |
| 696 | platform_apis: true, |
| 697 | } |
| 698 | ` |
| 699 | if enforce { |
| 700 | testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", bp, config) |
| 701 | } else { |
| 702 | testJavaWithConfig(t, bp, config) |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 707 | func TestJNIPackaging(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 708 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 709 | cc_library { |
| 710 | name: "libjni", |
| 711 | system_shared_libs: [], |
| 712 | stl: "none", |
| 713 | } |
| 714 | |
| 715 | android_app { |
| 716 | name: "app", |
| 717 | jni_libs: ["libjni"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 718 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | android_app { |
| 722 | name: "app_noembed", |
| 723 | jni_libs: ["libjni"], |
| 724 | use_embedded_native_libs: false, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 725 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | android_app { |
| 729 | name: "app_embed", |
| 730 | jni_libs: ["libjni"], |
| 731 | use_embedded_native_libs: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 732 | sdk_version: "current", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | android_test { |
| 736 | name: "test", |
| 737 | sdk_version: "core_platform", |
| 738 | jni_libs: ["libjni"], |
| 739 | } |
| 740 | |
| 741 | android_test { |
| 742 | name: "test_noembed", |
| 743 | sdk_version: "core_platform", |
| 744 | jni_libs: ["libjni"], |
| 745 | use_embedded_native_libs: false, |
| 746 | } |
| 747 | |
| 748 | android_test_helper_app { |
| 749 | name: "test_helper", |
| 750 | sdk_version: "core_platform", |
| 751 | jni_libs: ["libjni"], |
| 752 | } |
| 753 | |
| 754 | android_test_helper_app { |
| 755 | name: "test_helper_noembed", |
| 756 | sdk_version: "core_platform", |
| 757 | jni_libs: ["libjni"], |
| 758 | use_embedded_native_libs: false, |
| 759 | } |
| 760 | `) |
| 761 | |
| 762 | testCases := []struct { |
| 763 | name string |
| 764 | packaged bool |
| 765 | compressed bool |
| 766 | }{ |
| 767 | {"app", false, false}, |
| 768 | {"app_noembed", false, false}, |
| 769 | {"app_embed", true, false}, |
| 770 | {"test", true, false}, |
| 771 | {"test_noembed", true, true}, |
| 772 | {"test_helper", true, false}, |
| 773 | {"test_helper_noembed", true, true}, |
| 774 | } |
| 775 | |
| 776 | for _, test := range testCases { |
| 777 | t.Run(test.name, func(t *testing.T) { |
| 778 | app := ctx.ModuleForTests(test.name, "android_common") |
| 779 | jniLibZip := app.MaybeOutput("jnilibs.zip") |
| 780 | if g, w := (jniLibZip.Rule != nil), test.packaged; g != w { |
| 781 | t.Errorf("expected jni packaged %v, got %v", w, g) |
| 782 | } |
| 783 | |
| 784 | if jniLibZip.Rule != nil { |
| 785 | if g, w := !strings.Contains(jniLibZip.Args["jarArgs"], "-L 0"), test.compressed; g != w { |
| 786 | t.Errorf("expected jni compressed %v, got %v", w, g) |
| 787 | } |
| 788 | } |
| 789 | }) |
| 790 | } |
Colin Cross | 47fa9d3 | 2019-03-26 10:51:39 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 793 | func TestCertificates(t *testing.T) { |
| 794 | testCases := []struct { |
| 795 | name string |
| 796 | bp string |
| 797 | certificateOverride string |
| 798 | expected string |
| 799 | }{ |
| 800 | { |
| 801 | name: "default", |
| 802 | bp: ` |
| 803 | android_app { |
| 804 | name: "foo", |
| 805 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 806 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 807 | } |
| 808 | `, |
| 809 | certificateOverride: "", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 810 | 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] | 811 | }, |
| 812 | { |
| 813 | name: "module certificate property", |
| 814 | bp: ` |
| 815 | android_app { |
| 816 | name: "foo", |
| 817 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 818 | certificate: ":new_certificate", |
| 819 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | android_app_certificate { |
| 823 | name: "new_certificate", |
| 824 | certificate: "cert/new_cert", |
| 825 | } |
| 826 | `, |
| 827 | certificateOverride: "", |
| 828 | expected: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 829 | }, |
| 830 | { |
| 831 | name: "path certificate property", |
| 832 | bp: ` |
| 833 | android_app { |
| 834 | name: "foo", |
| 835 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 836 | certificate: "expiredkey", |
| 837 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 838 | } |
| 839 | `, |
| 840 | certificateOverride: "", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 841 | 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] | 842 | }, |
| 843 | { |
| 844 | name: "certificate overrides", |
| 845 | bp: ` |
| 846 | android_app { |
| 847 | name: "foo", |
| 848 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 849 | certificate: "expiredkey", |
| 850 | sdk_version: "current", |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | android_app_certificate { |
| 854 | name: "new_certificate", |
| 855 | certificate: "cert/new_cert", |
| 856 | } |
| 857 | `, |
| 858 | certificateOverride: "foo:new_certificate", |
| 859 | expected: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 860 | }, |
| 861 | } |
| 862 | |
| 863 | for _, test := range testCases { |
| 864 | t.Run(test.name, func(t *testing.T) { |
| 865 | config := testConfig(nil) |
| 866 | if test.certificateOverride != "" { |
| 867 | config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride} |
| 868 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 869 | ctx := testAppContext(test.bp, nil) |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 870 | |
| 871 | run(t, ctx, config) |
| 872 | foo := ctx.ModuleForTests("foo", "android_common") |
| 873 | |
| 874 | signapk := foo.Output("foo.apk") |
| 875 | signFlags := signapk.Args["certificates"] |
| 876 | if test.expected != signFlags { |
| 877 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags) |
| 878 | } |
| 879 | }) |
| 880 | } |
| 881 | } |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 882 | |
| 883 | func TestPackageNameOverride(t *testing.T) { |
| 884 | testCases := []struct { |
| 885 | name string |
| 886 | bp string |
| 887 | packageNameOverride string |
| 888 | expected []string |
| 889 | }{ |
| 890 | { |
| 891 | name: "default", |
| 892 | bp: ` |
| 893 | android_app { |
| 894 | name: "foo", |
| 895 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 896 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 897 | } |
| 898 | `, |
| 899 | packageNameOverride: "", |
| 900 | expected: []string{ |
| 901 | buildDir + "/.intermediates/foo/android_common/foo.apk", |
| 902 | buildDir + "/target/product/test_device/system/app/foo/foo.apk", |
| 903 | }, |
| 904 | }, |
| 905 | { |
| 906 | name: "overridden", |
| 907 | bp: ` |
| 908 | android_app { |
| 909 | name: "foo", |
| 910 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 911 | sdk_version: "current", |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 912 | } |
| 913 | `, |
| 914 | packageNameOverride: "foo:bar", |
| 915 | expected: []string{ |
| 916 | // The package apk should be still be the original name for test dependencies. |
| 917 | buildDir + "/.intermediates/foo/android_common/foo.apk", |
| 918 | buildDir + "/target/product/test_device/system/app/bar/bar.apk", |
| 919 | }, |
| 920 | }, |
| 921 | } |
| 922 | |
| 923 | for _, test := range testCases { |
| 924 | t.Run(test.name, func(t *testing.T) { |
| 925 | config := testConfig(nil) |
| 926 | if test.packageNameOverride != "" { |
| 927 | config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride} |
| 928 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 929 | ctx := testAppContext(test.bp, nil) |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 930 | |
| 931 | run(t, ctx, config) |
| 932 | foo := ctx.ModuleForTests("foo", "android_common") |
| 933 | |
| 934 | outputs := foo.AllOutputs() |
| 935 | outputMap := make(map[string]bool) |
| 936 | for _, o := range outputs { |
| 937 | outputMap[o] = true |
| 938 | } |
| 939 | for _, e := range test.expected { |
| 940 | if _, exist := outputMap[e]; !exist { |
| 941 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs) |
| 942 | } |
| 943 | } |
| 944 | }) |
| 945 | } |
| 946 | } |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 947 | |
| 948 | func TestInstrumentationTargetOverridden(t *testing.T) { |
| 949 | bp := ` |
| 950 | android_app { |
| 951 | name: "foo", |
| 952 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 953 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | android_test { |
| 957 | name: "bar", |
| 958 | instrumentation_for: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 959 | sdk_version: "current", |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 960 | } |
| 961 | ` |
| 962 | config := testConfig(nil) |
| 963 | config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"} |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 964 | ctx := testAppContext(bp, nil) |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 965 | |
| 966 | run(t, ctx, config) |
| 967 | |
| 968 | bar := ctx.ModuleForTests("bar", "android_common") |
| 969 | res := bar.Output("package-res.apk") |
| 970 | aapt2Flags := res.Args["flags"] |
| 971 | e := "--rename-instrumentation-target-package org.dandroid.bp" |
| 972 | if !strings.Contains(aapt2Flags, e) { |
| 973 | t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags) |
| 974 | } |
| 975 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 976 | |
| 977 | func TestOverrideAndroidApp(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 978 | ctx, _ := testJava(t, ` |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 979 | android_app { |
| 980 | name: "foo", |
| 981 | srcs: ["a.java"], |
Jaewoong Jung | a641ee9 | 2019-03-27 11:17:14 -0700 | [diff] [blame] | 982 | certificate: "expiredkey", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 983 | overrides: ["qux"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 984 | sdk_version: "current", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | override_android_app { |
| 988 | name: "bar", |
| 989 | base: "foo", |
| 990 | certificate: ":new_certificate", |
| 991 | } |
| 992 | |
| 993 | android_app_certificate { |
| 994 | name: "new_certificate", |
| 995 | certificate: "cert/new_cert", |
| 996 | } |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 997 | |
| 998 | override_android_app { |
| 999 | name: "baz", |
| 1000 | base: "foo", |
| 1001 | package_name: "org.dandroid.bp", |
| 1002 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1003 | `) |
| 1004 | |
| 1005 | expectedVariants := []struct { |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1006 | moduleName string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1007 | variantName string |
| 1008 | apkName string |
| 1009 | apkPath string |
| 1010 | signFlag string |
| 1011 | overrides []string |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1012 | aaptFlag string |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1013 | }{ |
| 1014 | { |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1015 | moduleName: "foo", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1016 | variantName: "android_common", |
| 1017 | apkPath: "/target/product/test_device/system/app/foo/foo.apk", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 1018 | 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] | 1019 | overrides: []string{"qux"}, |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1020 | aaptFlag: "", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1021 | }, |
| 1022 | { |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1023 | moduleName: "bar", |
| 1024 | variantName: "android_common_bar", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1025 | apkPath: "/target/product/test_device/system/app/bar/bar.apk", |
| 1026 | signFlag: "cert/new_cert.x509.pem cert/new_cert.pk8", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1027 | overrides: []string{"qux", "foo"}, |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1028 | aaptFlag: "", |
| 1029 | }, |
| 1030 | { |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1031 | moduleName: "baz", |
| 1032 | variantName: "android_common_baz", |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1033 | apkPath: "/target/product/test_device/system/app/baz/baz.apk", |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 1034 | 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] | 1035 | overrides: []string{"qux", "foo"}, |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1036 | aaptFlag: "--rename-manifest-package org.dandroid.bp", |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1037 | }, |
| 1038 | } |
| 1039 | for _, expected := range expectedVariants { |
| 1040 | variant := ctx.ModuleForTests("foo", expected.variantName) |
| 1041 | |
| 1042 | // Check the final apk name |
| 1043 | outputs := variant.AllOutputs() |
| 1044 | expectedApkPath := buildDir + expected.apkPath |
| 1045 | found := false |
| 1046 | for _, o := range outputs { |
| 1047 | if o == expectedApkPath { |
| 1048 | found = true |
| 1049 | break |
| 1050 | } |
| 1051 | } |
| 1052 | if !found { |
| 1053 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", expectedApkPath, outputs) |
| 1054 | } |
| 1055 | |
| 1056 | // Check the certificate paths |
| 1057 | signapk := variant.Output("foo.apk") |
| 1058 | signFlag := signapk.Args["certificates"] |
| 1059 | if expected.signFlag != signFlag { |
| 1060 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected.signFlag, signFlag) |
| 1061 | } |
| 1062 | |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1063 | // Check if the overrides field values are correctly aggregated. |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1064 | mod := variant.Module().(*AndroidApp) |
| 1065 | if !reflect.DeepEqual(expected.overrides, mod.appProperties.Overrides) { |
| 1066 | t.Errorf("Incorrect overrides property value, expected: %q, got: %q", |
| 1067 | expected.overrides, mod.appProperties.Overrides) |
| 1068 | } |
Jaewoong Jung | 6f373f6 | 2019-03-13 10:13:24 -0700 | [diff] [blame] | 1069 | |
| 1070 | // Check the package renaming flag, if exists. |
| 1071 | res := variant.Output("package-res.apk") |
| 1072 | aapt2Flags := res.Args["flags"] |
| 1073 | if !strings.Contains(aapt2Flags, expected.aaptFlag) { |
| 1074 | t.Errorf("package renaming flag, %q is missing in aapt2 link flags, %q", expected.aaptFlag, aapt2Flags) |
| 1075 | } |
Jaewoong Jung | 525443a | 2019-02-28 15:35:54 -0800 | [diff] [blame] | 1076 | } |
| 1077 | } |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1078 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1079 | func TestOverrideAndroidAppDependency(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1080 | ctx, _ := testJava(t, ` |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1081 | android_app { |
| 1082 | name: "foo", |
| 1083 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1084 | sdk_version: "current", |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | override_android_app { |
| 1088 | name: "bar", |
| 1089 | base: "foo", |
| 1090 | package_name: "org.dandroid.bp", |
| 1091 | } |
| 1092 | |
| 1093 | android_test { |
| 1094 | name: "baz", |
| 1095 | srcs: ["b.java"], |
| 1096 | instrumentation_for: "foo", |
| 1097 | } |
| 1098 | |
| 1099 | android_test { |
| 1100 | name: "qux", |
| 1101 | srcs: ["b.java"], |
| 1102 | instrumentation_for: "bar", |
| 1103 | } |
| 1104 | `) |
| 1105 | |
| 1106 | // Verify baz, which depends on the overridden module foo, has the correct classpath javac arg. |
| 1107 | javac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1108 | fooTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 1109 | if !strings.Contains(javac.Args["classpath"], fooTurbine) { |
| 1110 | t.Errorf("baz classpath %v does not contain %q", javac.Args["classpath"], fooTurbine) |
| 1111 | } |
| 1112 | |
| 1113 | // Verify qux, which depends on the overriding module bar, has the correct classpath javac arg. |
| 1114 | javac = ctx.ModuleForTests("qux", "android_common").Rule("javac") |
| 1115 | barTurbine := filepath.Join(buildDir, ".intermediates", "foo", "android_common_bar", "turbine-combined", "foo.jar") |
| 1116 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 1117 | t.Errorf("qux classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
| 1118 | } |
| 1119 | } |
| 1120 | |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1121 | func TestAndroidAppImport(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1122 | ctx, _ := testJava(t, ` |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1123 | android_app_import { |
| 1124 | name: "foo", |
| 1125 | apk: "prebuilts/apk/app.apk", |
| 1126 | certificate: "platform", |
| 1127 | dex_preopt: { |
| 1128 | enabled: true, |
| 1129 | }, |
| 1130 | } |
| 1131 | `) |
| 1132 | |
| 1133 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1134 | |
| 1135 | // Check dexpreopt outputs. |
| 1136 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil || |
| 1137 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil { |
| 1138 | t.Errorf("can't find dexpreopt outputs") |
| 1139 | } |
| 1140 | |
| 1141 | // Check cert signing flag. |
| 1142 | signedApk := variant.Output("signed/foo.apk") |
| 1143 | signingFlag := signedApk.Args["certificates"] |
| 1144 | expected := "build/make/target/product/security/platform.x509.pem build/make/target/product/security/platform.pk8" |
| 1145 | if expected != signingFlag { |
| 1146 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag) |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | func TestAndroidAppImport_NoDexPreopt(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1151 | ctx, _ := testJava(t, ` |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1152 | android_app_import { |
| 1153 | name: "foo", |
| 1154 | apk: "prebuilts/apk/app.apk", |
| 1155 | certificate: "platform", |
| 1156 | dex_preopt: { |
| 1157 | enabled: false, |
| 1158 | }, |
| 1159 | } |
| 1160 | `) |
| 1161 | |
| 1162 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1163 | |
| 1164 | // Check dexpreopt outputs. They shouldn't exist. |
| 1165 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule != nil || |
| 1166 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule != nil { |
| 1167 | t.Errorf("dexpreopt shouldn't have run.") |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | func TestAndroidAppImport_Presigned(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1172 | ctx, _ := testJava(t, ` |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1173 | android_app_import { |
| 1174 | name: "foo", |
| 1175 | apk: "prebuilts/apk/app.apk", |
| 1176 | presigned: true, |
| 1177 | dex_preopt: { |
| 1178 | enabled: true, |
| 1179 | }, |
| 1180 | } |
| 1181 | `) |
| 1182 | |
| 1183 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1184 | |
| 1185 | // Check dexpreopt outputs. |
| 1186 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil || |
| 1187 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil { |
| 1188 | t.Errorf("can't find dexpreopt outputs") |
| 1189 | } |
Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame] | 1190 | // Make sure signing was skipped and aligning was done. |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 1191 | if variant.MaybeOutput("signed/foo.apk").Rule != nil { |
| 1192 | t.Errorf("signing rule shouldn't be included.") |
| 1193 | } |
| 1194 | if variant.MaybeOutput("zip-aligned/foo.apk").Rule == nil { |
| 1195 | t.Errorf("can't find aligning rule") |
| 1196 | } |
| 1197 | } |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1198 | |
Jaewoong Jung | 961d4fd | 2019-08-22 14:25:58 -0700 | [diff] [blame] | 1199 | func TestAndroidAppImport_DefaultDevCert(t *testing.T) { |
| 1200 | ctx, _ := testJava(t, ` |
| 1201 | android_app_import { |
| 1202 | name: "foo", |
| 1203 | apk: "prebuilts/apk/app.apk", |
| 1204 | default_dev_cert: true, |
| 1205 | dex_preopt: { |
| 1206 | enabled: true, |
| 1207 | }, |
| 1208 | } |
| 1209 | `) |
| 1210 | |
| 1211 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1212 | |
| 1213 | // Check dexpreopt outputs. |
| 1214 | if variant.MaybeOutput("dexpreopt/oat/arm64/package.vdex").Rule == nil || |
| 1215 | variant.MaybeOutput("dexpreopt/oat/arm64/package.odex").Rule == nil { |
| 1216 | t.Errorf("can't find dexpreopt outputs") |
| 1217 | } |
| 1218 | |
| 1219 | // Check cert signing flag. |
| 1220 | signedApk := variant.Output("signed/foo.apk") |
| 1221 | signingFlag := signedApk.Args["certificates"] |
| 1222 | expected := "build/make/target/product/security/testkey.x509.pem build/make/target/product/security/testkey.pk8" |
| 1223 | if expected != signingFlag { |
| 1224 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag) |
| 1225 | } |
| 1226 | } |
| 1227 | |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1228 | func TestAndroidAppImport_DpiVariants(t *testing.T) { |
| 1229 | bp := ` |
| 1230 | android_app_import { |
| 1231 | name: "foo", |
| 1232 | apk: "prebuilts/apk/app.apk", |
| 1233 | dpi_variants: { |
| 1234 | xhdpi: { |
| 1235 | apk: "prebuilts/apk/app_xhdpi.apk", |
| 1236 | }, |
| 1237 | xxhdpi: { |
| 1238 | apk: "prebuilts/apk/app_xxhdpi.apk", |
| 1239 | }, |
| 1240 | }, |
Jaewoong Jung | 961d4fd | 2019-08-22 14:25:58 -0700 | [diff] [blame] | 1241 | presigned: true, |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1242 | dex_preopt: { |
| 1243 | enabled: true, |
| 1244 | }, |
| 1245 | } |
| 1246 | ` |
| 1247 | testCases := []struct { |
| 1248 | name string |
| 1249 | aaptPreferredConfig *string |
| 1250 | aaptPrebuiltDPI []string |
| 1251 | expected string |
| 1252 | }{ |
| 1253 | { |
| 1254 | name: "no preferred", |
| 1255 | aaptPreferredConfig: nil, |
| 1256 | aaptPrebuiltDPI: []string{}, |
| 1257 | expected: "prebuilts/apk/app.apk", |
| 1258 | }, |
| 1259 | { |
| 1260 | name: "AAPTPreferredConfig matches", |
| 1261 | aaptPreferredConfig: proptools.StringPtr("xhdpi"), |
Jaewoong Jung | 3e18b19 | 2019-06-11 12:25:34 -0700 | [diff] [blame] | 1262 | aaptPrebuiltDPI: []string{"xxhdpi", "ldpi"}, |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1263 | expected: "prebuilts/apk/app_xhdpi.apk", |
| 1264 | }, |
| 1265 | { |
| 1266 | name: "AAPTPrebuiltDPI matches", |
| 1267 | aaptPreferredConfig: proptools.StringPtr("mdpi"), |
| 1268 | aaptPrebuiltDPI: []string{"xxhdpi", "xhdpi"}, |
| 1269 | expected: "prebuilts/apk/app_xxhdpi.apk", |
| 1270 | }, |
| 1271 | { |
| 1272 | name: "non-first AAPTPrebuiltDPI matches", |
| 1273 | aaptPreferredConfig: proptools.StringPtr("mdpi"), |
| 1274 | aaptPrebuiltDPI: []string{"ldpi", "xhdpi"}, |
| 1275 | expected: "prebuilts/apk/app_xhdpi.apk", |
| 1276 | }, |
| 1277 | { |
| 1278 | name: "no matches", |
| 1279 | aaptPreferredConfig: proptools.StringPtr("mdpi"), |
| 1280 | aaptPrebuiltDPI: []string{"ldpi", "xxxhdpi"}, |
| 1281 | expected: "prebuilts/apk/app.apk", |
| 1282 | }, |
| 1283 | } |
| 1284 | |
| 1285 | jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)") |
| 1286 | for _, test := range testCases { |
| 1287 | config := testConfig(nil) |
| 1288 | config.TestProductVariables.AAPTPreferredConfig = test.aaptPreferredConfig |
| 1289 | config.TestProductVariables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1290 | ctx := testAppContext(bp, nil) |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 1291 | |
| 1292 | run(t, ctx, config) |
| 1293 | |
| 1294 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1295 | jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command |
| 1296 | matches := jniRuleRe.FindStringSubmatch(jniRuleCommand) |
| 1297 | if len(matches) != 2 { |
| 1298 | t.Errorf("failed to extract the src apk path from %q", jniRuleCommand) |
| 1299 | } |
| 1300 | if test.expected != matches[1] { |
| 1301 | t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1]) |
| 1302 | } |
| 1303 | } |
| 1304 | } |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1305 | |
Jaewoong Jung | 8aae22e | 2019-07-17 10:21:49 -0700 | [diff] [blame] | 1306 | func TestAndroidAppImport_Filename(t *testing.T) { |
| 1307 | ctx, config := testJava(t, ` |
| 1308 | android_app_import { |
| 1309 | name: "foo", |
| 1310 | apk: "prebuilts/apk/app.apk", |
| 1311 | presigned: true, |
| 1312 | } |
| 1313 | |
| 1314 | android_app_import { |
| 1315 | name: "bar", |
| 1316 | apk: "prebuilts/apk/app.apk", |
| 1317 | presigned: true, |
| 1318 | filename: "bar_sample.apk" |
| 1319 | } |
| 1320 | `) |
| 1321 | |
| 1322 | testCases := []struct { |
| 1323 | name string |
| 1324 | expected string |
| 1325 | }{ |
| 1326 | { |
| 1327 | name: "foo", |
| 1328 | expected: "foo.apk", |
| 1329 | }, |
| 1330 | { |
| 1331 | name: "bar", |
| 1332 | expected: "bar_sample.apk", |
| 1333 | }, |
| 1334 | } |
| 1335 | |
| 1336 | for _, test := range testCases { |
| 1337 | variant := ctx.ModuleForTests(test.name, "android_common") |
| 1338 | if variant.MaybeOutput(test.expected).Rule == nil { |
| 1339 | t.Errorf("can't find output named %q - all outputs: %v", test.expected, variant.AllOutputs()) |
| 1340 | } |
| 1341 | |
| 1342 | a := variant.Module().(*AndroidAppImport) |
| 1343 | expectedValues := []string{test.expected} |
| 1344 | actualValues := android.AndroidMkEntriesForTest( |
| 1345 | t, config, "", a).EntryMap["LOCAL_INSTALLED_MODULE_STEM"] |
| 1346 | if !reflect.DeepEqual(actualValues, expectedValues) { |
| 1347 | t.Errorf("Incorrect LOCAL_INSTALLED_MODULE_STEM value '%s', expected '%s'", |
| 1348 | actualValues, expectedValues) |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 1353 | func TestAndroidAppImport_ArchVariants(t *testing.T) { |
| 1354 | // The test config's target arch is ARM64. |
| 1355 | testCases := []struct { |
| 1356 | name string |
| 1357 | bp string |
| 1358 | expected string |
| 1359 | }{ |
| 1360 | { |
| 1361 | name: "matching arch", |
| 1362 | bp: ` |
| 1363 | android_app_import { |
| 1364 | name: "foo", |
| 1365 | apk: "prebuilts/apk/app.apk", |
| 1366 | arch: { |
| 1367 | arm64: { |
| 1368 | apk: "prebuilts/apk/app_arm64.apk", |
| 1369 | }, |
| 1370 | }, |
Jaewoong Jung | 961d4fd | 2019-08-22 14:25:58 -0700 | [diff] [blame] | 1371 | presigned: true, |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 1372 | dex_preopt: { |
| 1373 | enabled: true, |
| 1374 | }, |
| 1375 | } |
| 1376 | `, |
| 1377 | expected: "prebuilts/apk/app_arm64.apk", |
| 1378 | }, |
| 1379 | { |
| 1380 | name: "no matching arch", |
| 1381 | bp: ` |
| 1382 | android_app_import { |
| 1383 | name: "foo", |
| 1384 | apk: "prebuilts/apk/app.apk", |
| 1385 | arch: { |
| 1386 | arm: { |
| 1387 | apk: "prebuilts/apk/app_arm.apk", |
| 1388 | }, |
| 1389 | }, |
Jaewoong Jung | 961d4fd | 2019-08-22 14:25:58 -0700 | [diff] [blame] | 1390 | presigned: true, |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 1391 | dex_preopt: { |
| 1392 | enabled: true, |
| 1393 | }, |
| 1394 | } |
| 1395 | `, |
| 1396 | expected: "prebuilts/apk/app.apk", |
| 1397 | }, |
| 1398 | } |
| 1399 | |
| 1400 | jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)") |
| 1401 | for _, test := range testCases { |
| 1402 | ctx, _ := testJava(t, test.bp) |
| 1403 | |
| 1404 | variant := ctx.ModuleForTests("foo", "android_common") |
| 1405 | jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command |
| 1406 | matches := jniRuleRe.FindStringSubmatch(jniRuleCommand) |
| 1407 | if len(matches) != 2 { |
| 1408 | t.Errorf("failed to extract the src apk path from %q", jniRuleCommand) |
| 1409 | } |
| 1410 | if test.expected != matches[1] { |
| 1411 | t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1]) |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | |
Jaewoong Jung | b28eb5f | 2019-08-27 15:01:50 -0700 | [diff] [blame] | 1416 | func TestAndroidTestImport(t *testing.T) { |
| 1417 | ctx, config := testJava(t, ` |
| 1418 | android_test_import { |
| 1419 | name: "foo", |
| 1420 | apk: "prebuilts/apk/app.apk", |
| 1421 | presigned: true, |
| 1422 | data: [ |
| 1423 | "testdata/data", |
| 1424 | ], |
| 1425 | } |
| 1426 | `) |
| 1427 | |
| 1428 | test := ctx.ModuleForTests("foo", "android_common").Module().(*AndroidTestImport) |
| 1429 | |
| 1430 | // Check android mks. |
| 1431 | entries := android.AndroidMkEntriesForTest(t, config, "", test) |
| 1432 | expected := []string{"tests"} |
| 1433 | actual := entries.EntryMap["LOCAL_MODULE_TAGS"] |
| 1434 | if !reflect.DeepEqual(expected, actual) { |
| 1435 | t.Errorf("Unexpected module tags - expected: %q, actual: %q", expected, actual) |
| 1436 | } |
| 1437 | expected = []string{"testdata/data:testdata/data"} |
| 1438 | actual = entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"] |
| 1439 | if !reflect.DeepEqual(expected, actual) { |
| 1440 | t.Errorf("Unexpected test data - expected: %q, actual: %q", expected, actual) |
| 1441 | } |
| 1442 | } |
| 1443 | |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1444 | func TestStl(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1445 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1446 | cc_library { |
| 1447 | name: "libjni", |
| 1448 | } |
| 1449 | |
| 1450 | android_test { |
| 1451 | name: "stl", |
| 1452 | jni_libs: ["libjni"], |
| 1453 | compile_multilib: "both", |
| 1454 | sdk_version: "current", |
| 1455 | stl: "c++_shared", |
| 1456 | } |
| 1457 | |
| 1458 | android_test { |
| 1459 | name: "system", |
| 1460 | jni_libs: ["libjni"], |
| 1461 | compile_multilib: "both", |
| 1462 | sdk_version: "current", |
| 1463 | } |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 1464 | |
| 1465 | ndk_prebuilt_shared_stl { |
| 1466 | name: "ndk_libc++_shared", |
| 1467 | } |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1468 | `) |
| 1469 | |
| 1470 | testCases := []struct { |
| 1471 | name string |
| 1472 | jnis []string |
| 1473 | }{ |
| 1474 | {"stl", |
| 1475 | []string{ |
| 1476 | "libjni.so", |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 1477 | "libc++_shared.so", |
Jaewoong Jung | bc625cd | 2019-05-06 15:48:44 -0700 | [diff] [blame] | 1478 | }, |
| 1479 | }, |
| 1480 | {"system", |
| 1481 | []string{ |
| 1482 | "libjni.so", |
| 1483 | }, |
| 1484 | }, |
| 1485 | } |
| 1486 | |
| 1487 | for _, test := range testCases { |
| 1488 | t.Run(test.name, func(t *testing.T) { |
| 1489 | app := ctx.ModuleForTests(test.name, "android_common") |
| 1490 | jniLibZip := app.Output("jnilibs.zip") |
| 1491 | var jnis []string |
| 1492 | args := strings.Fields(jniLibZip.Args["jarArgs"]) |
| 1493 | for i := 0; i < len(args); i++ { |
| 1494 | if args[i] == "-f" { |
| 1495 | jnis = append(jnis, args[i+1]) |
| 1496 | i += 1 |
| 1497 | } |
| 1498 | } |
| 1499 | jnisJoined := strings.Join(jnis, " ") |
| 1500 | for _, jni := range test.jnis { |
| 1501 | if !strings.Contains(jnisJoined, jni) { |
| 1502 | t.Errorf("missing jni %q in %q", jni, jnis) |
| 1503 | } |
| 1504 | } |
| 1505 | }) |
| 1506 | } |
| 1507 | } |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1508 | |
| 1509 | func TestUsesLibraries(t *testing.T) { |
| 1510 | bp := ` |
| 1511 | java_sdk_library { |
| 1512 | name: "foo", |
| 1513 | srcs: ["a.java"], |
| 1514 | api_packages: ["foo"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1515 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | java_sdk_library { |
| 1519 | name: "bar", |
| 1520 | srcs: ["a.java"], |
| 1521 | api_packages: ["bar"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1522 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | android_app { |
| 1526 | name: "app", |
| 1527 | srcs: ["a.java"], |
| 1528 | uses_libs: ["foo"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1529 | sdk_version: "current", |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1530 | optional_uses_libs: [ |
| 1531 | "bar", |
| 1532 | "baz", |
| 1533 | ], |
| 1534 | } |
| 1535 | |
| 1536 | android_app_import { |
| 1537 | name: "prebuilt", |
| 1538 | apk: "prebuilts/apk/app.apk", |
| 1539 | certificate: "platform", |
| 1540 | uses_libs: ["foo"], |
| 1541 | optional_uses_libs: [ |
| 1542 | "bar", |
| 1543 | "baz", |
| 1544 | ], |
| 1545 | } |
| 1546 | ` |
| 1547 | |
| 1548 | config := testConfig(nil) |
| 1549 | config.TestProductVariables.MissingUsesLibraries = []string{"baz"} |
| 1550 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1551 | ctx := testAppContext(bp, nil) |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 1552 | |
| 1553 | run(t, ctx, config) |
| 1554 | |
| 1555 | app := ctx.ModuleForTests("app", "android_common") |
| 1556 | prebuilt := ctx.ModuleForTests("prebuilt", "android_common") |
| 1557 | |
| 1558 | // Test that all libraries are verified |
| 1559 | cmd := app.Rule("verify_uses_libraries").RuleParams.Command |
| 1560 | if w := "--uses-library foo"; !strings.Contains(cmd, w) { |
| 1561 | t.Errorf("wanted %q in %q", w, cmd) |
| 1562 | } |
| 1563 | |
| 1564 | if w := "--optional-uses-library bar --optional-uses-library baz"; !strings.Contains(cmd, w) { |
| 1565 | t.Errorf("wanted %q in %q", w, cmd) |
| 1566 | } |
| 1567 | |
| 1568 | cmd = prebuilt.Rule("verify_uses_libraries").RuleParams.Command |
| 1569 | |
| 1570 | if w := `uses_library_names="foo"`; !strings.Contains(cmd, w) { |
| 1571 | t.Errorf("wanted %q in %q", w, cmd) |
| 1572 | } |
| 1573 | |
| 1574 | if w := `optional_uses_library_names="bar baz"`; !strings.Contains(cmd, w) { |
| 1575 | t.Errorf("wanted %q in %q", w, cmd) |
| 1576 | } |
| 1577 | |
| 1578 | // Test that only present libraries are preopted |
| 1579 | cmd = app.Rule("dexpreopt").RuleParams.Command |
| 1580 | |
| 1581 | if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) { |
| 1582 | t.Errorf("wanted %q in %q", w, cmd) |
| 1583 | } |
| 1584 | |
| 1585 | cmd = prebuilt.Rule("dexpreopt").RuleParams.Command |
| 1586 | |
| 1587 | if w := `dex_preopt_target_libraries="/system/framework/foo.jar /system/framework/bar.jar"`; !strings.Contains(cmd, w) { |
| 1588 | t.Errorf("wanted %q in %q", w, cmd) |
| 1589 | } |
| 1590 | } |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1591 | |
| 1592 | func TestCodelessApp(t *testing.T) { |
| 1593 | testCases := []struct { |
| 1594 | name string |
| 1595 | bp string |
| 1596 | noCode bool |
| 1597 | }{ |
| 1598 | { |
| 1599 | name: "normal", |
| 1600 | bp: ` |
| 1601 | android_app { |
| 1602 | name: "foo", |
| 1603 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1604 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1605 | } |
| 1606 | `, |
| 1607 | noCode: false, |
| 1608 | }, |
| 1609 | { |
| 1610 | name: "app without sources", |
| 1611 | bp: ` |
| 1612 | android_app { |
| 1613 | name: "foo", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1614 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1615 | } |
| 1616 | `, |
| 1617 | noCode: true, |
| 1618 | }, |
| 1619 | { |
| 1620 | name: "app with libraries", |
| 1621 | bp: ` |
| 1622 | android_app { |
| 1623 | name: "foo", |
| 1624 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1625 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | java_library { |
| 1629 | name: "lib", |
| 1630 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1631 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1632 | } |
| 1633 | `, |
| 1634 | noCode: false, |
| 1635 | }, |
| 1636 | { |
| 1637 | name: "app with sourceless libraries", |
| 1638 | bp: ` |
| 1639 | android_app { |
| 1640 | name: "foo", |
| 1641 | static_libs: ["lib"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1642 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | java_library { |
| 1646 | name: "lib", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1647 | sdk_version: "current", |
Jaewoong Jung | c27ab66 | 2019-05-30 15:51:14 -0700 | [diff] [blame] | 1648 | } |
| 1649 | `, |
| 1650 | // TODO(jungjw): this should probably be true |
| 1651 | noCode: false, |
| 1652 | }, |
| 1653 | } |
| 1654 | |
| 1655 | for _, test := range testCases { |
| 1656 | t.Run(test.name, func(t *testing.T) { |
| 1657 | ctx := testApp(t, test.bp) |
| 1658 | |
| 1659 | foo := ctx.ModuleForTests("foo", "android_common") |
| 1660 | manifestFixerArgs := foo.Output("manifest_fixer/AndroidManifest.xml").Args["args"] |
| 1661 | if strings.Contains(manifestFixerArgs, "--has-no-code") != test.noCode { |
| 1662 | t.Errorf("unexpected manifest_fixer args: %q", manifestFixerArgs) |
| 1663 | } |
| 1664 | }) |
| 1665 | } |
| 1666 | } |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1667 | |
| 1668 | func TestEmbedNotice(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1669 | ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1670 | android_app { |
| 1671 | name: "foo", |
| 1672 | srcs: ["a.java"], |
| 1673 | static_libs: ["javalib"], |
| 1674 | jni_libs: ["libjni"], |
| 1675 | notice: "APP_NOTICE", |
| 1676 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1677 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | // No embed_notice flag |
| 1681 | android_app { |
| 1682 | name: "bar", |
| 1683 | srcs: ["a.java"], |
| 1684 | jni_libs: ["libjni"], |
| 1685 | notice: "APP_NOTICE", |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1686 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | // No NOTICE files |
| 1690 | android_app { |
| 1691 | name: "baz", |
| 1692 | srcs: ["a.java"], |
| 1693 | embed_notices: true, |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1694 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | cc_library { |
| 1698 | name: "libjni", |
| 1699 | system_shared_libs: [], |
| 1700 | stl: "none", |
| 1701 | notice: "LIB_NOTICE", |
| 1702 | } |
| 1703 | |
| 1704 | java_library { |
| 1705 | name: "javalib", |
| 1706 | srcs: [ |
| 1707 | ":gen", |
| 1708 | ], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1709 | sdk_version: "current", |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | genrule { |
| 1713 | name: "gen", |
| 1714 | tools: ["gentool"], |
| 1715 | out: ["gen.java"], |
| 1716 | notice: "GENRULE_NOTICE", |
| 1717 | } |
| 1718 | |
| 1719 | java_binary_host { |
| 1720 | name: "gentool", |
| 1721 | srcs: ["b.java"], |
| 1722 | notice: "TOOL_NOTICE", |
| 1723 | } |
| 1724 | `) |
| 1725 | |
| 1726 | // foo has NOTICE files to process, and embed_notices is true. |
| 1727 | foo := ctx.ModuleForTests("foo", "android_common") |
| 1728 | // verify merge notices rule. |
| 1729 | mergeNotices := foo.Rule("mergeNoticesRule") |
| 1730 | noticeInputs := mergeNotices.Inputs.Strings() |
| 1731 | // TOOL_NOTICE should be excluded as it's a host module. |
| 1732 | if len(mergeNotices.Inputs) != 3 { |
| 1733 | t.Errorf("number of input notice files: expected = 3, actual = %q", noticeInputs) |
| 1734 | } |
| 1735 | if !inList("APP_NOTICE", noticeInputs) { |
| 1736 | t.Errorf("APP_NOTICE is missing from notice files, %q", noticeInputs) |
| 1737 | } |
| 1738 | if !inList("LIB_NOTICE", noticeInputs) { |
| 1739 | t.Errorf("LIB_NOTICE is missing from notice files, %q", noticeInputs) |
| 1740 | } |
| 1741 | if !inList("GENRULE_NOTICE", noticeInputs) { |
| 1742 | t.Errorf("GENRULE_NOTICE is missing from notice files, %q", noticeInputs) |
| 1743 | } |
| 1744 | // aapt2 flags should include -A <NOTICE dir> so that its contents are put in the APK's /assets. |
| 1745 | res := foo.Output("package-res.apk") |
| 1746 | aapt2Flags := res.Args["flags"] |
| 1747 | e := "-A " + buildDir + "/.intermediates/foo/android_common/NOTICE" |
| 1748 | if !strings.Contains(aapt2Flags, e) { |
| 1749 | t.Errorf("asset dir flag for NOTICE, %q is missing in aapt2 link flags, %q", e, aapt2Flags) |
| 1750 | } |
| 1751 | |
| 1752 | // bar has NOTICE files to process, but embed_notices is not set. |
| 1753 | bar := ctx.ModuleForTests("bar", "android_common") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 1754 | res = bar.Output("package-res.apk") |
| 1755 | aapt2Flags = res.Args["flags"] |
| 1756 | e = "-A " + buildDir + "/.intermediates/bar/android_common/NOTICE" |
| 1757 | if strings.Contains(aapt2Flags, e) { |
| 1758 | 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] | 1759 | } |
| 1760 | |
| 1761 | // baz's embed_notice is true, but it doesn't have any NOTICE files. |
| 1762 | baz := ctx.ModuleForTests("baz", "android_common") |
Jaewoong Jung | 9877279 | 2019-07-01 17:15:13 -0700 | [diff] [blame] | 1763 | res = baz.Output("package-res.apk") |
| 1764 | aapt2Flags = res.Args["flags"] |
| 1765 | e = "-A " + buildDir + "/.intermediates/baz/android_common/NOTICE" |
| 1766 | if strings.Contains(aapt2Flags, e) { |
| 1767 | 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] | 1768 | } |
| 1769 | } |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1770 | |
| 1771 | func TestUncompressDex(t *testing.T) { |
| 1772 | testCases := []struct { |
| 1773 | name string |
| 1774 | bp string |
| 1775 | |
| 1776 | uncompressedPlatform bool |
| 1777 | uncompressedUnbundled bool |
| 1778 | }{ |
| 1779 | { |
| 1780 | name: "normal", |
| 1781 | bp: ` |
| 1782 | android_app { |
| 1783 | name: "foo", |
| 1784 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1785 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1786 | } |
| 1787 | `, |
| 1788 | uncompressedPlatform: true, |
| 1789 | uncompressedUnbundled: false, |
| 1790 | }, |
| 1791 | { |
| 1792 | name: "use_embedded_dex", |
| 1793 | bp: ` |
| 1794 | android_app { |
| 1795 | name: "foo", |
| 1796 | use_embedded_dex: true, |
| 1797 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1798 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1799 | } |
| 1800 | `, |
| 1801 | uncompressedPlatform: true, |
| 1802 | uncompressedUnbundled: true, |
| 1803 | }, |
| 1804 | { |
| 1805 | name: "privileged", |
| 1806 | bp: ` |
| 1807 | android_app { |
| 1808 | name: "foo", |
| 1809 | privileged: true, |
| 1810 | srcs: ["a.java"], |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 1811 | sdk_version: "current", |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1812 | } |
| 1813 | `, |
| 1814 | uncompressedPlatform: true, |
| 1815 | uncompressedUnbundled: true, |
| 1816 | }, |
| 1817 | } |
| 1818 | |
| 1819 | test := func(t *testing.T, bp string, want bool, unbundled bool) { |
| 1820 | t.Helper() |
| 1821 | |
| 1822 | config := testConfig(nil) |
| 1823 | if unbundled { |
| 1824 | config.TestProductVariables.Unbundled_build = proptools.BoolPtr(true) |
| 1825 | } |
| 1826 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1827 | ctx := testAppContext(bp, nil) |
Colin Cross | 53a87f5 | 2019-06-25 13:35:30 -0700 | [diff] [blame] | 1828 | |
| 1829 | run(t, ctx, config) |
| 1830 | |
| 1831 | foo := ctx.ModuleForTests("foo", "android_common") |
| 1832 | dex := foo.Rule("r8") |
| 1833 | uncompressedInDexJar := strings.Contains(dex.Args["zipFlags"], "-L 0") |
| 1834 | aligned := foo.MaybeRule("zipalign").Rule != nil |
| 1835 | |
| 1836 | if uncompressedInDexJar != want { |
| 1837 | t.Errorf("want uncompressed in dex %v, got %v", want, uncompressedInDexJar) |
| 1838 | } |
| 1839 | |
| 1840 | if aligned != want { |
| 1841 | t.Errorf("want aligned %v, got %v", want, aligned) |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | for _, tt := range testCases { |
| 1846 | t.Run(tt.name, func(t *testing.T) { |
| 1847 | t.Run("platform", func(t *testing.T) { |
| 1848 | test(t, tt.bp, tt.uncompressedPlatform, false) |
| 1849 | }) |
| 1850 | t.Run("unbundled", func(t *testing.T) { |
| 1851 | test(t, tt.bp, tt.uncompressedUnbundled, true) |
| 1852 | }) |
| 1853 | }) |
| 1854 | } |
| 1855 | } |