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 ( |
| 18 | "android/soong/android" |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 19 | "fmt" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 20 | "path/filepath" |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 21 | "reflect" |
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" |
| 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | resourceFiles = []string{ |
| 29 | "res/layout/layout.xml", |
| 30 | "res/values/strings.xml", |
| 31 | "res/values-en-rUS/strings.xml", |
| 32 | } |
| 33 | |
| 34 | compiledResourceFiles = []string{ |
| 35 | "aapt2/res/layout_layout.xml.flat", |
| 36 | "aapt2/res/values_strings.arsc.flat", |
| 37 | "aapt2/res/values-en-rUS_strings.arsc.flat", |
| 38 | } |
| 39 | ) |
| 40 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 41 | func testAppContext(config android.Config, bp string, fs map[string][]byte) *android.TestContext { |
| 42 | appFS := map[string][]byte{} |
| 43 | for k, v := range fs { |
| 44 | appFS[k] = v |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 47 | for _, file := range resourceFiles { |
| 48 | appFS[file] = nil |
| 49 | } |
| 50 | |
| 51 | return testContext(config, bp, appFS) |
| 52 | } |
| 53 | |
| 54 | func testApp(t *testing.T, bp string) *android.TestContext { |
| 55 | config := testConfig(nil) |
| 56 | |
| 57 | ctx := testAppContext(config, bp, nil) |
| 58 | |
| 59 | run(t, ctx, config) |
| 60 | |
| 61 | return ctx |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | func TestApp(t *testing.T) { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 65 | for _, moduleType := range []string{"android_app", "android_library"} { |
| 66 | t.Run(moduleType, func(t *testing.T) { |
| 67 | ctx := testApp(t, moduleType+` { |
| 68 | name: "foo", |
| 69 | srcs: ["a.java"], |
| 70 | } |
| 71 | `) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 72 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 73 | foo := ctx.ModuleForTests("foo", "android_common") |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 74 | |
Colin Cross | 3165695 | 2018-05-24 16:11:20 -0700 | [diff] [blame] | 75 | var expectedLinkImplicits []string |
| 76 | |
| 77 | manifestFixer := foo.Output("manifest_fixer/AndroidManifest.xml") |
| 78 | expectedLinkImplicits = append(expectedLinkImplicits, manifestFixer.Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 79 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 80 | frameworkRes := ctx.ModuleForTests("framework-res", "android_common") |
| 81 | expectedLinkImplicits = append(expectedLinkImplicits, |
| 82 | frameworkRes.Output("package-res.apk").Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 83 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 84 | // Test the mapping from input files to compiled output file names |
| 85 | compile := foo.Output(compiledResourceFiles[0]) |
| 86 | if !reflect.DeepEqual(resourceFiles, compile.Inputs.Strings()) { |
| 87 | t.Errorf("expected aapt2 compile inputs expected:\n %#v\n got:\n %#v", |
| 88 | resourceFiles, compile.Inputs.Strings()) |
| 89 | } |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 90 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 91 | compiledResourceOutputs := compile.Outputs.Strings() |
| 92 | sort.Strings(compiledResourceOutputs) |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 93 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 94 | expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 95 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 96 | list := foo.Output("aapt2/res.list") |
| 97 | expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 98 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 99 | // Check that the link rule uses |
| 100 | res := ctx.ModuleForTests("foo", "android_common").Output("package-res.apk") |
| 101 | if !reflect.DeepEqual(expectedLinkImplicits, res.Implicits.Strings()) { |
| 102 | t.Errorf("expected aapt2 link implicits expected:\n %#v\n got:\n %#v", |
| 103 | expectedLinkImplicits, res.Implicits.Strings()) |
| 104 | } |
| 105 | }) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 108 | |
Colin Cross | e560c4a | 2019-03-19 16:03:11 -0700 | [diff] [blame^] | 109 | func TestAppSplits(t *testing.T) { |
| 110 | ctx := testApp(t, ` |
| 111 | android_app { |
| 112 | name: "foo", |
| 113 | srcs: ["a.java"], |
| 114 | package_splits: ["v4", "v7,hdpi"], |
| 115 | }`) |
| 116 | |
| 117 | foo := ctx.ModuleForTests("foo", "android_common") |
| 118 | |
| 119 | expectedOutputs := []string{ |
| 120 | filepath.Join(buildDir, ".intermediates/foo/android_common/foo.apk"), |
| 121 | filepath.Join(buildDir, ".intermediates/foo/android_common/foo_v4.apk"), |
| 122 | filepath.Join(buildDir, ".intermediates/foo/android_common/foo_v7_hdpi.apk"), |
| 123 | } |
| 124 | for _, expectedOutput := range expectedOutputs { |
| 125 | foo.Output(expectedOutput) |
| 126 | } |
| 127 | |
| 128 | if g, w := foo.Module().(*AndroidApp).Srcs().Strings(), expectedOutputs; !reflect.DeepEqual(g, w) { |
| 129 | t.Errorf("want Srcs() = %q, got %q", w, g) |
| 130 | } |
| 131 | } |
| 132 | |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 133 | func TestResourceDirs(t *testing.T) { |
| 134 | testCases := []struct { |
| 135 | name string |
| 136 | prop string |
| 137 | resources []string |
| 138 | }{ |
| 139 | { |
| 140 | name: "no resource_dirs", |
| 141 | prop: "", |
| 142 | resources: []string{"res/res/values/strings.xml"}, |
| 143 | }, |
| 144 | { |
| 145 | name: "resource_dirs", |
| 146 | prop: `resource_dirs: ["res"]`, |
| 147 | resources: []string{"res/res/values/strings.xml"}, |
| 148 | }, |
| 149 | { |
| 150 | name: "empty resource_dirs", |
| 151 | prop: `resource_dirs: []`, |
| 152 | resources: nil, |
| 153 | }, |
| 154 | } |
| 155 | |
| 156 | fs := map[string][]byte{ |
| 157 | "res/res/values/strings.xml": nil, |
| 158 | } |
| 159 | |
| 160 | bp := ` |
| 161 | android_app { |
| 162 | name: "foo", |
| 163 | %s |
| 164 | } |
| 165 | ` |
| 166 | |
| 167 | for _, testCase := range testCases { |
| 168 | t.Run(testCase.name, func(t *testing.T) { |
| 169 | config := testConfig(nil) |
| 170 | ctx := testContext(config, fmt.Sprintf(bp, testCase.prop), fs) |
| 171 | run(t, ctx, config) |
| 172 | |
| 173 | module := ctx.ModuleForTests("foo", "android_common") |
| 174 | resourceList := module.MaybeOutput("aapt2/res.list") |
| 175 | |
| 176 | var resources []string |
| 177 | if resourceList.Rule != nil { |
| 178 | for _, compiledResource := range resourceList.Inputs.Strings() { |
| 179 | resources = append(resources, module.Output(compiledResource).Inputs.Strings()...) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if !reflect.DeepEqual(resources, testCase.resources) { |
| 184 | t.Errorf("expected resource files %q, got %q", |
| 185 | testCase.resources, resources) |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | } |
| 190 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 191 | func TestAndroidResources(t *testing.T) { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 192 | testCases := []struct { |
| 193 | name string |
| 194 | enforceRROTargets []string |
| 195 | enforceRROExcludedOverlays []string |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 196 | resourceFiles map[string][]string |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 197 | overlayFiles map[string][]string |
| 198 | rroDirs map[string][]string |
| 199 | }{ |
| 200 | { |
| 201 | name: "no RRO", |
| 202 | enforceRROTargets: nil, |
| 203 | enforceRROExcludedOverlays: nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 204 | resourceFiles: map[string][]string{ |
| 205 | "foo": nil, |
| 206 | "bar": {"bar/res/res/values/strings.xml"}, |
| 207 | "lib": nil, |
| 208 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 209 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 210 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 211 | "foo": { |
| 212 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 213 | buildDir + "/.intermediates/lib/android_common/package-res.apk", |
| 214 | "foo/res/res/values/strings.xml", |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 215 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 216 | "device/vendor/blah/overlay/foo/res/values/strings.xml", |
| 217 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 218 | "bar": { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 219 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
| 220 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
| 221 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 222 | "lib": { |
| 223 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
| 224 | "lib/res/res/values/strings.xml", |
| 225 | "device/vendor/blah/overlay/lib/res/values/strings.xml", |
| 226 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 227 | }, |
| 228 | rroDirs: map[string][]string{ |
| 229 | "foo": nil, |
| 230 | "bar": nil, |
| 231 | }, |
| 232 | }, |
| 233 | { |
| 234 | name: "enforce RRO on foo", |
| 235 | enforceRROTargets: []string{"foo"}, |
| 236 | enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"}, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 237 | resourceFiles: map[string][]string{ |
| 238 | "foo": nil, |
| 239 | "bar": {"bar/res/res/values/strings.xml"}, |
| 240 | "lib": nil, |
| 241 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 242 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 243 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 244 | "foo": { |
| 245 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 246 | buildDir + "/.intermediates/lib/android_common/package-res.apk", |
| 247 | "foo/res/res/values/strings.xml", |
| 248 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 249 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 250 | "bar": { |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 251 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
| 252 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
| 253 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 254 | "lib": { |
| 255 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
| 256 | "lib/res/res/values/strings.xml", |
| 257 | "device/vendor/blah/overlay/lib/res/values/strings.xml", |
| 258 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 259 | }, |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 260 | |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 261 | rroDirs: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 262 | "foo": { |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 263 | "device/vendor/blah/overlay/foo/res", |
| 264 | // Enforce RRO on "foo" could imply RRO on static dependencies, but for now it doesn't. |
| 265 | // "device/vendor/blah/overlay/lib/res", |
| 266 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 267 | "bar": nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 268 | "lib": nil, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 269 | }, |
| 270 | }, |
| 271 | { |
| 272 | name: "enforce RRO on all", |
| 273 | enforceRROTargets: []string{"*"}, |
| 274 | enforceRROExcludedOverlays: []string{ |
| 275 | // Excluding specific apps/res directories also allowed. |
| 276 | "device/vendor/blah/static_overlay/foo", |
| 277 | "device/vendor/blah/static_overlay/bar/res", |
| 278 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 279 | resourceFiles: map[string][]string{ |
| 280 | "foo": nil, |
| 281 | "bar": {"bar/res/res/values/strings.xml"}, |
| 282 | "lib": nil, |
| 283 | "lib2": {"lib2/res/res/values/strings.xml"}, |
| 284 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 285 | overlayFiles: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 286 | "foo": { |
| 287 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 288 | buildDir + "/.intermediates/lib/android_common/package-res.apk", |
| 289 | "foo/res/res/values/strings.xml", |
| 290 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 291 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 292 | "bar": {"device/vendor/blah/static_overlay/bar/res/values/strings.xml"}, |
| 293 | "lib": { |
| 294 | buildDir + "/.intermediates/lib2/android_common/package-res.apk", |
| 295 | "lib/res/res/values/strings.xml", |
| 296 | }, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 297 | }, |
| 298 | rroDirs: map[string][]string{ |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 299 | "foo": { |
Colin Cross | c1c3755 | 2019-01-31 11:42:41 -0800 | [diff] [blame] | 300 | "device/vendor/blah/overlay/foo/res", |
| 301 | "device/vendor/blah/overlay/lib/res", |
| 302 | }, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 303 | "bar": {"device/vendor/blah/overlay/bar/res"}, |
| 304 | "lib": {"device/vendor/blah/overlay/lib/res"}, |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 305 | }, |
| 306 | }, |
| 307 | } |
| 308 | |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 309 | resourceOverlays := []string{ |
| 310 | "device/vendor/blah/overlay", |
| 311 | "device/vendor/blah/overlay2", |
| 312 | "device/vendor/blah/static_overlay", |
| 313 | } |
| 314 | |
| 315 | fs := map[string][]byte{ |
| 316 | "foo/res/res/values/strings.xml": nil, |
| 317 | "bar/res/res/values/strings.xml": nil, |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 318 | "lib/res/res/values/strings.xml": nil, |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 319 | "lib2/res/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 320 | "device/vendor/blah/overlay/foo/res/values/strings.xml": nil, |
| 321 | "device/vendor/blah/overlay/bar/res/values/strings.xml": nil, |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 322 | "device/vendor/blah/overlay/lib/res/values/strings.xml": nil, |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 323 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil, |
| 324 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil, |
| 325 | "device/vendor/blah/overlay2/res/values/strings.xml": nil, |
| 326 | } |
| 327 | |
| 328 | bp := ` |
| 329 | android_app { |
| 330 | name: "foo", |
| 331 | resource_dirs: ["foo/res"], |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 332 | static_libs: ["lib"], |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | android_app { |
| 336 | name: "bar", |
| 337 | resource_dirs: ["bar/res"], |
| 338 | } |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 339 | |
| 340 | android_library { |
| 341 | name: "lib", |
| 342 | resource_dirs: ["lib/res"], |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 343 | static_libs: ["lib2"], |
| 344 | } |
| 345 | |
| 346 | android_library { |
| 347 | name: "lib2", |
| 348 | resource_dirs: ["lib2/res"], |
Colin Cross | 6ed7dea | 2019-01-31 14:44:30 -0800 | [diff] [blame] | 349 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 350 | ` |
| 351 | |
Colin Cross | 5c4791c | 2019-02-01 11:44:44 -0800 | [diff] [blame] | 352 | for _, testCase := range testCases { |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 353 | t.Run(testCase.name, func(t *testing.T) { |
| 354 | config := testConfig(nil) |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 355 | config.TestProductVariables.ResourceOverlays = resourceOverlays |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 356 | if testCase.enforceRROTargets != nil { |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 357 | config.TestProductVariables.EnforceRROTargets = testCase.enforceRROTargets |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 358 | } |
| 359 | if testCase.enforceRROExcludedOverlays != nil { |
Colin Cross | a74ca04 | 2019-01-31 14:31:51 -0800 | [diff] [blame] | 360 | config.TestProductVariables.EnforceRROExcludedOverlays = testCase.enforceRROExcludedOverlays |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | ctx := testAppContext(config, bp, fs) |
| 364 | run(t, ctx, config) |
| 365 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 366 | resourceListToFiles := func(module android.TestingModule, list []string) (files []string) { |
| 367 | for _, o := range list { |
| 368 | res := module.MaybeOutput(o) |
| 369 | if res.Rule != nil { |
| 370 | // If the overlay is compiled as part of this module (i.e. a .arsc.flat file), |
| 371 | // verify the inputs to the .arsc.flat rule. |
| 372 | files = append(files, res.Inputs.Strings()...) |
| 373 | } else { |
| 374 | // Otherwise, verify the full path to the output of the other module |
| 375 | files = append(files, o) |
Anton Hansson | 94c93f3 | 2019-01-30 16:03:37 +0000 | [diff] [blame] | 376 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 377 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 378 | return files |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 381 | getResources := func(moduleName string) (resourceFiles, overlayFiles, rroDirs []string) { |
| 382 | module := ctx.ModuleForTests(moduleName, "android_common") |
| 383 | resourceList := module.MaybeOutput("aapt2/res.list") |
| 384 | if resourceList.Rule != nil { |
| 385 | resourceFiles = resourceListToFiles(module, resourceList.Inputs.Strings()) |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 386 | } |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 387 | overlayList := module.MaybeOutput("aapt2/overlay.list") |
| 388 | if overlayList.Rule != nil { |
| 389 | overlayFiles = resourceListToFiles(module, overlayList.Inputs.Strings()) |
| 390 | } |
| 391 | |
| 392 | rroDirs = module.Module().(AndroidLibraryDependency).ExportedRRODirs().Strings() |
| 393 | |
| 394 | return resourceFiles, overlayFiles, rroDirs |
| 395 | } |
| 396 | |
| 397 | modules := []string{"foo", "bar", "lib", "lib2"} |
| 398 | for _, module := range modules { |
| 399 | resourceFiles, overlayFiles, rroDirs := getResources(module) |
| 400 | |
| 401 | if !reflect.DeepEqual(resourceFiles, testCase.resourceFiles[module]) { |
| 402 | t.Errorf("expected %s resource files:\n %#v\n got:\n %#v", |
| 403 | module, testCase.resourceFiles[module], resourceFiles) |
| 404 | } |
| 405 | if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[module]) { |
| 406 | t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v", |
| 407 | module, testCase.overlayFiles[module], overlayFiles) |
| 408 | } |
| 409 | if !reflect.DeepEqual(rroDirs, testCase.rroDirs[module]) { |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 410 | t.Errorf("expected %s rroDirs: %#v\n got:\n %#v", |
Colin Cross | bec8530 | 2019-02-13 13:15:46 -0800 | [diff] [blame] | 411 | module, testCase.rroDirs[module], rroDirs) |
Anton Hansson | 0375a4f | 2019-01-24 14:39:19 +0000 | [diff] [blame] | 412 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 413 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 414 | }) |
| 415 | } |
| 416 | } |
Colin Cross | d09b0b6 | 2018-04-18 11:06:47 -0700 | [diff] [blame] | 417 | |
| 418 | func TestAppSdkVersion(t *testing.T) { |
| 419 | testCases := []struct { |
| 420 | name string |
| 421 | sdkVersion string |
| 422 | platformSdkInt int |
| 423 | platformSdkCodename string |
| 424 | platformSdkFinal bool |
| 425 | expectedMinSdkVersion string |
| 426 | }{ |
| 427 | { |
| 428 | name: "current final SDK", |
| 429 | sdkVersion: "current", |
| 430 | platformSdkInt: 27, |
| 431 | platformSdkCodename: "REL", |
| 432 | platformSdkFinal: true, |
| 433 | expectedMinSdkVersion: "27", |
| 434 | }, |
| 435 | { |
| 436 | name: "current non-final SDK", |
| 437 | sdkVersion: "current", |
| 438 | platformSdkInt: 27, |
| 439 | platformSdkCodename: "OMR1", |
| 440 | platformSdkFinal: false, |
| 441 | expectedMinSdkVersion: "OMR1", |
| 442 | }, |
| 443 | { |
| 444 | name: "default final SDK", |
| 445 | sdkVersion: "", |
| 446 | platformSdkInt: 27, |
| 447 | platformSdkCodename: "REL", |
| 448 | platformSdkFinal: true, |
| 449 | expectedMinSdkVersion: "27", |
| 450 | }, |
| 451 | { |
| 452 | name: "default non-final SDK", |
| 453 | sdkVersion: "", |
| 454 | platformSdkInt: 27, |
| 455 | platformSdkCodename: "OMR1", |
| 456 | platformSdkFinal: false, |
| 457 | expectedMinSdkVersion: "OMR1", |
| 458 | }, |
| 459 | { |
| 460 | name: "14", |
| 461 | sdkVersion: "14", |
| 462 | expectedMinSdkVersion: "14", |
| 463 | }, |
| 464 | } |
| 465 | |
| 466 | for _, moduleType := range []string{"android_app", "android_library"} { |
| 467 | for _, test := range testCases { |
| 468 | t.Run(moduleType+" "+test.name, func(t *testing.T) { |
| 469 | bp := fmt.Sprintf(`%s { |
| 470 | name: "foo", |
| 471 | srcs: ["a.java"], |
| 472 | sdk_version: "%s", |
| 473 | }`, moduleType, test.sdkVersion) |
| 474 | |
| 475 | config := testConfig(nil) |
| 476 | config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt |
| 477 | config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename |
| 478 | config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal |
| 479 | |
| 480 | ctx := testAppContext(config, bp, nil) |
| 481 | |
| 482 | run(t, ctx, config) |
| 483 | |
| 484 | foo := ctx.ModuleForTests("foo", "android_common") |
| 485 | link := foo.Output("package-res.apk") |
| 486 | linkFlags := strings.Split(link.Args["flags"], " ") |
| 487 | min := android.IndexList("--min-sdk-version", linkFlags) |
| 488 | target := android.IndexList("--target-sdk-version", linkFlags) |
| 489 | |
| 490 | if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 { |
| 491 | t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags) |
| 492 | } |
| 493 | |
| 494 | gotMinSdkVersion := linkFlags[min+1] |
| 495 | gotTargetSdkVersion := linkFlags[target+1] |
| 496 | |
| 497 | if gotMinSdkVersion != test.expectedMinSdkVersion { |
| 498 | t.Errorf("incorrect --min-sdk-version, expected %q got %q", |
| 499 | test.expectedMinSdkVersion, gotMinSdkVersion) |
| 500 | } |
| 501 | |
| 502 | if gotTargetSdkVersion != test.expectedMinSdkVersion { |
| 503 | t.Errorf("incorrect --target-sdk-version, expected %q got %q", |
| 504 | test.expectedMinSdkVersion, gotTargetSdkVersion) |
| 505 | } |
| 506 | }) |
| 507 | } |
| 508 | } |
| 509 | } |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 510 | |
| 511 | func TestJNI(t *testing.T) { |
| 512 | ctx := testJava(t, ` |
| 513 | toolchain_library { |
| 514 | name: "libcompiler_rt-extras", |
| 515 | src: "", |
| 516 | } |
| 517 | |
| 518 | toolchain_library { |
| 519 | name: "libatomic", |
| 520 | src: "", |
| 521 | } |
| 522 | |
| 523 | toolchain_library { |
| 524 | name: "libgcc", |
| 525 | src: "", |
| 526 | } |
| 527 | |
| 528 | toolchain_library { |
| 529 | name: "libclang_rt.builtins-aarch64-android", |
| 530 | src: "", |
| 531 | } |
| 532 | |
| 533 | toolchain_library { |
| 534 | name: "libclang_rt.builtins-arm-android", |
| 535 | src: "", |
| 536 | } |
| 537 | |
| 538 | cc_object { |
| 539 | name: "crtbegin_so", |
| 540 | stl: "none", |
| 541 | } |
| 542 | |
| 543 | cc_object { |
| 544 | name: "crtend_so", |
| 545 | stl: "none", |
| 546 | } |
| 547 | |
| 548 | cc_library { |
| 549 | name: "libjni", |
| 550 | system_shared_libs: [], |
| 551 | stl: "none", |
| 552 | } |
| 553 | |
| 554 | android_test { |
| 555 | name: "test", |
| 556 | no_framework_libs: true, |
| 557 | jni_libs: ["libjni"], |
| 558 | } |
| 559 | |
| 560 | android_test { |
| 561 | name: "test_first", |
| 562 | no_framework_libs: true, |
| 563 | compile_multilib: "first", |
| 564 | jni_libs: ["libjni"], |
| 565 | } |
| 566 | |
| 567 | android_test { |
| 568 | name: "test_both", |
| 569 | no_framework_libs: true, |
| 570 | compile_multilib: "both", |
| 571 | jni_libs: ["libjni"], |
| 572 | } |
| 573 | |
| 574 | android_test { |
| 575 | name: "test_32", |
| 576 | no_framework_libs: true, |
| 577 | compile_multilib: "32", |
| 578 | jni_libs: ["libjni"], |
| 579 | } |
| 580 | |
| 581 | android_test { |
| 582 | name: "test_64", |
| 583 | no_framework_libs: true, |
| 584 | compile_multilib: "64", |
| 585 | jni_libs: ["libjni"], |
| 586 | } |
| 587 | `) |
| 588 | |
| 589 | // check the existence of the internal modules |
| 590 | ctx.ModuleForTests("test", "android_common") |
| 591 | ctx.ModuleForTests("test_first", "android_common") |
| 592 | ctx.ModuleForTests("test_both", "android_common") |
| 593 | ctx.ModuleForTests("test_32", "android_common") |
| 594 | ctx.ModuleForTests("test_64", "android_common") |
| 595 | |
| 596 | testCases := []struct { |
| 597 | name string |
| 598 | abis []string |
| 599 | }{ |
| 600 | {"test", []string{"arm64-v8a"}}, |
| 601 | {"test_first", []string{"arm64-v8a"}}, |
| 602 | {"test_both", []string{"arm64-v8a", "armeabi-v7a"}}, |
| 603 | {"test_32", []string{"armeabi-v7a"}}, |
| 604 | {"test_64", []string{"arm64-v8a"}}, |
| 605 | } |
| 606 | |
| 607 | for _, test := range testCases { |
| 608 | t.Run(test.name, func(t *testing.T) { |
| 609 | app := ctx.ModuleForTests(test.name, "android_common") |
| 610 | jniLibZip := app.Output("jnilibs.zip") |
| 611 | var abis []string |
| 612 | args := strings.Fields(jniLibZip.Args["jarArgs"]) |
| 613 | for i := 0; i < len(args); i++ { |
| 614 | if args[i] == "-P" { |
| 615 | abis = append(abis, filepath.Base(args[i+1])) |
| 616 | i++ |
| 617 | } |
| 618 | } |
| 619 | if !reflect.DeepEqual(abis, test.abis) { |
| 620 | t.Errorf("want abis %v, got %v", test.abis, abis) |
| 621 | } |
| 622 | }) |
| 623 | } |
| 624 | } |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 625 | |
| 626 | func TestCertificates(t *testing.T) { |
| 627 | testCases := []struct { |
| 628 | name string |
| 629 | bp string |
| 630 | certificateOverride string |
| 631 | expected string |
| 632 | }{ |
| 633 | { |
| 634 | name: "default", |
| 635 | bp: ` |
| 636 | android_app { |
| 637 | name: "foo", |
| 638 | srcs: ["a.java"], |
| 639 | } |
| 640 | `, |
| 641 | certificateOverride: "", |
| 642 | expected: "build/target/product/security/testkey.x509.pem build/target/product/security/testkey.pk8", |
| 643 | }, |
| 644 | { |
| 645 | name: "module certificate property", |
| 646 | bp: ` |
| 647 | android_app { |
| 648 | name: "foo", |
| 649 | srcs: ["a.java"], |
| 650 | certificate: ":new_certificate" |
| 651 | } |
| 652 | |
| 653 | android_app_certificate { |
| 654 | name: "new_certificate", |
| 655 | certificate: "cert/new_cert", |
| 656 | } |
| 657 | `, |
| 658 | certificateOverride: "", |
| 659 | expected: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 660 | }, |
| 661 | { |
| 662 | name: "path certificate property", |
| 663 | bp: ` |
| 664 | android_app { |
| 665 | name: "foo", |
| 666 | srcs: ["a.java"], |
| 667 | certificate: "expiredkey" |
| 668 | } |
| 669 | `, |
| 670 | certificateOverride: "", |
| 671 | expected: "build/target/product/security/expiredkey.x509.pem build/target/product/security/expiredkey.pk8", |
| 672 | }, |
| 673 | { |
| 674 | name: "certificate overrides", |
| 675 | bp: ` |
| 676 | android_app { |
| 677 | name: "foo", |
| 678 | srcs: ["a.java"], |
| 679 | certificate: "expiredkey" |
| 680 | } |
| 681 | |
| 682 | android_app_certificate { |
| 683 | name: "new_certificate", |
| 684 | certificate: "cert/new_cert", |
| 685 | } |
| 686 | `, |
| 687 | certificateOverride: "foo:new_certificate", |
| 688 | expected: "cert/new_cert.x509.pem cert/new_cert.pk8", |
| 689 | }, |
| 690 | } |
| 691 | |
| 692 | for _, test := range testCases { |
| 693 | t.Run(test.name, func(t *testing.T) { |
| 694 | config := testConfig(nil) |
| 695 | if test.certificateOverride != "" { |
| 696 | config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride} |
| 697 | } |
| 698 | ctx := testAppContext(config, test.bp, nil) |
| 699 | |
| 700 | run(t, ctx, config) |
| 701 | foo := ctx.ModuleForTests("foo", "android_common") |
| 702 | |
| 703 | signapk := foo.Output("foo.apk") |
| 704 | signFlags := signapk.Args["certificates"] |
| 705 | if test.expected != signFlags { |
| 706 | t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags) |
| 707 | } |
| 708 | }) |
| 709 | } |
| 710 | } |
Jaewoong Jung | 9d22a91 | 2019-01-23 16:27:47 -0800 | [diff] [blame] | 711 | |
| 712 | func TestPackageNameOverride(t *testing.T) { |
| 713 | testCases := []struct { |
| 714 | name string |
| 715 | bp string |
| 716 | packageNameOverride string |
| 717 | expected []string |
| 718 | }{ |
| 719 | { |
| 720 | name: "default", |
| 721 | bp: ` |
| 722 | android_app { |
| 723 | name: "foo", |
| 724 | srcs: ["a.java"], |
| 725 | } |
| 726 | `, |
| 727 | packageNameOverride: "", |
| 728 | expected: []string{ |
| 729 | buildDir + "/.intermediates/foo/android_common/foo.apk", |
| 730 | buildDir + "/target/product/test_device/system/app/foo/foo.apk", |
| 731 | }, |
| 732 | }, |
| 733 | { |
| 734 | name: "overridden", |
| 735 | bp: ` |
| 736 | android_app { |
| 737 | name: "foo", |
| 738 | srcs: ["a.java"], |
| 739 | } |
| 740 | `, |
| 741 | packageNameOverride: "foo:bar", |
| 742 | expected: []string{ |
| 743 | // The package apk should be still be the original name for test dependencies. |
| 744 | buildDir + "/.intermediates/foo/android_common/foo.apk", |
| 745 | buildDir + "/target/product/test_device/system/app/bar/bar.apk", |
| 746 | }, |
| 747 | }, |
| 748 | } |
| 749 | |
| 750 | for _, test := range testCases { |
| 751 | t.Run(test.name, func(t *testing.T) { |
| 752 | config := testConfig(nil) |
| 753 | if test.packageNameOverride != "" { |
| 754 | config.TestProductVariables.PackageNameOverrides = []string{test.packageNameOverride} |
| 755 | } |
| 756 | ctx := testAppContext(config, test.bp, nil) |
| 757 | |
| 758 | run(t, ctx, config) |
| 759 | foo := ctx.ModuleForTests("foo", "android_common") |
| 760 | |
| 761 | outputs := foo.AllOutputs() |
| 762 | outputMap := make(map[string]bool) |
| 763 | for _, o := range outputs { |
| 764 | outputMap[o] = true |
| 765 | } |
| 766 | for _, e := range test.expected { |
| 767 | if _, exist := outputMap[e]; !exist { |
| 768 | t.Errorf("Can't find %q in output files.\nAll outputs:%v", e, outputs) |
| 769 | } |
| 770 | } |
| 771 | }) |
| 772 | } |
| 773 | } |
Jaewoong Jung | 4102e5d | 2019-02-27 16:26:28 -0800 | [diff] [blame] | 774 | |
| 775 | func TestInstrumentationTargetOverridden(t *testing.T) { |
| 776 | bp := ` |
| 777 | android_app { |
| 778 | name: "foo", |
| 779 | srcs: ["a.java"], |
| 780 | } |
| 781 | |
| 782 | android_test { |
| 783 | name: "bar", |
| 784 | instrumentation_for: "foo", |
| 785 | } |
| 786 | ` |
| 787 | config := testConfig(nil) |
| 788 | config.TestProductVariables.ManifestPackageNameOverrides = []string{"foo:org.dandroid.bp"} |
| 789 | ctx := testAppContext(config, bp, nil) |
| 790 | |
| 791 | run(t, ctx, config) |
| 792 | |
| 793 | bar := ctx.ModuleForTests("bar", "android_common") |
| 794 | res := bar.Output("package-res.apk") |
| 795 | aapt2Flags := res.Args["flags"] |
| 796 | e := "--rename-instrumentation-target-package org.dandroid.bp" |
| 797 | if !strings.Contains(aapt2Flags, e) { |
| 798 | t.Errorf("target package renaming flag, %q is missing in aapt2 link flags, %q", e, aapt2Flags) |
| 799 | } |
| 800 | } |