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