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