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" |
| 19 | "reflect" |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 20 | "sort" |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | resourceFiles = []string{ |
| 26 | "res/layout/layout.xml", |
| 27 | "res/values/strings.xml", |
| 28 | "res/values-en-rUS/strings.xml", |
| 29 | } |
| 30 | |
| 31 | compiledResourceFiles = []string{ |
| 32 | "aapt2/res/layout_layout.xml.flat", |
| 33 | "aapt2/res/values_strings.arsc.flat", |
| 34 | "aapt2/res/values-en-rUS_strings.arsc.flat", |
| 35 | } |
| 36 | ) |
| 37 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 38 | func testAppContext(config android.Config, bp string, fs map[string][]byte) *android.TestContext { |
| 39 | appFS := map[string][]byte{} |
| 40 | for k, v := range fs { |
| 41 | appFS[k] = v |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 44 | for _, file := range resourceFiles { |
| 45 | appFS[file] = nil |
| 46 | } |
| 47 | |
| 48 | return testContext(config, bp, appFS) |
| 49 | } |
| 50 | |
| 51 | func testApp(t *testing.T, bp string) *android.TestContext { |
| 52 | config := testConfig(nil) |
| 53 | |
| 54 | ctx := testAppContext(config, bp, nil) |
| 55 | |
| 56 | run(t, ctx, config) |
| 57 | |
| 58 | return ctx |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func TestApp(t *testing.T) { |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 62 | for _, moduleType := range []string{"android_app", "android_library"} { |
| 63 | t.Run(moduleType, func(t *testing.T) { |
| 64 | ctx := testApp(t, moduleType+` { |
| 65 | name: "foo", |
| 66 | srcs: ["a.java"], |
| 67 | } |
| 68 | `) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 69 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 70 | foo := ctx.ModuleForTests("foo", "android_common") |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 71 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 72 | expectedLinkImplicits := []string{"AndroidManifest.xml"} |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 73 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 74 | frameworkRes := ctx.ModuleForTests("framework-res", "android_common") |
| 75 | expectedLinkImplicits = append(expectedLinkImplicits, |
| 76 | frameworkRes.Output("package-res.apk").Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 77 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 78 | // Test the mapping from input files to compiled output file names |
| 79 | compile := foo.Output(compiledResourceFiles[0]) |
| 80 | if !reflect.DeepEqual(resourceFiles, compile.Inputs.Strings()) { |
| 81 | t.Errorf("expected aapt2 compile inputs expected:\n %#v\n got:\n %#v", |
| 82 | resourceFiles, compile.Inputs.Strings()) |
| 83 | } |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 84 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 85 | compiledResourceOutputs := compile.Outputs.Strings() |
| 86 | sort.Strings(compiledResourceOutputs) |
Colin Cross | b69301e | 2017-12-01 10:48:26 -0800 | [diff] [blame] | 87 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 88 | expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 89 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 90 | list := foo.Output("aapt2/res.list") |
| 91 | expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String()) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 92 | |
Colin Cross | a97c5d3 | 2018-03-28 14:58:31 -0700 | [diff] [blame] | 93 | // Check that the link rule uses |
| 94 | res := ctx.ModuleForTests("foo", "android_common").Output("package-res.apk") |
| 95 | if !reflect.DeepEqual(expectedLinkImplicits, res.Implicits.Strings()) { |
| 96 | t.Errorf("expected aapt2 link implicits expected:\n %#v\n got:\n %#v", |
| 97 | expectedLinkImplicits, res.Implicits.Strings()) |
| 98 | } |
| 99 | }) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 102 | |
| 103 | var testEnforceRROTests = []struct { |
| 104 | name string |
| 105 | enforceRROTargets []string |
| 106 | enforceRROExcludedOverlays []string |
| 107 | fooOverlayFiles []string |
| 108 | fooRRODirs []string |
| 109 | barOverlayFiles []string |
| 110 | barRRODirs []string |
| 111 | }{ |
| 112 | { |
| 113 | name: "no RRO", |
| 114 | enforceRROTargets: nil, |
| 115 | enforceRROExcludedOverlays: nil, |
| 116 | fooOverlayFiles: []string{ |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 117 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
Colin Cross | 68a7023 | 2018-01-04 14:50:13 -0800 | [diff] [blame] | 118 | "device/vendor/blah/overlay/foo/res/values/strings.xml", |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 119 | }, |
| 120 | fooRRODirs: nil, |
| 121 | barOverlayFiles: []string{ |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 122 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
Colin Cross | 68a7023 | 2018-01-04 14:50:13 -0800 | [diff] [blame] | 123 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 124 | }, |
| 125 | barRRODirs: nil, |
| 126 | }, |
| 127 | { |
| 128 | name: "enforce RRO on foo", |
| 129 | enforceRROTargets: []string{"foo"}, |
| 130 | enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"}, |
| 131 | fooOverlayFiles: []string{ |
| 132 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 133 | }, |
| 134 | fooRRODirs: []string{ |
| 135 | "device/vendor/blah/overlay/foo/res", |
| 136 | }, |
| 137 | barOverlayFiles: []string{ |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 138 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
Colin Cross | 68a7023 | 2018-01-04 14:50:13 -0800 | [diff] [blame] | 139 | "device/vendor/blah/overlay/bar/res/values/strings.xml", |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 140 | }, |
| 141 | barRRODirs: nil, |
| 142 | }, |
| 143 | { |
| 144 | name: "enforce RRO on all", |
| 145 | enforceRROTargets: []string{"*"}, |
| 146 | enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"}, |
| 147 | fooOverlayFiles: []string{ |
| 148 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml", |
| 149 | }, |
| 150 | fooRRODirs: []string{ |
| 151 | "device/vendor/blah/overlay/foo/res", |
| 152 | }, |
| 153 | barOverlayFiles: []string{ |
| 154 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml", |
| 155 | }, |
| 156 | barRRODirs: []string{ |
| 157 | "device/vendor/blah/overlay/bar/res", |
| 158 | }, |
| 159 | }, |
| 160 | } |
| 161 | |
| 162 | func TestEnforceRRO(t *testing.T) { |
| 163 | resourceOverlays := []string{ |
| 164 | "device/vendor/blah/overlay", |
| 165 | "device/vendor/blah/overlay2", |
| 166 | "device/vendor/blah/static_overlay", |
| 167 | } |
| 168 | |
| 169 | fs := map[string][]byte{ |
| 170 | "foo/res/res/values/strings.xml": nil, |
| 171 | "bar/res/res/values/strings.xml": nil, |
| 172 | "device/vendor/blah/overlay/foo/res/values/strings.xml": nil, |
| 173 | "device/vendor/blah/overlay/bar/res/values/strings.xml": nil, |
| 174 | "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil, |
| 175 | "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil, |
| 176 | "device/vendor/blah/overlay2/res/values/strings.xml": nil, |
| 177 | } |
| 178 | |
| 179 | bp := ` |
| 180 | android_app { |
| 181 | name: "foo", |
| 182 | resource_dirs: ["foo/res"], |
| 183 | } |
| 184 | |
| 185 | android_app { |
| 186 | name: "bar", |
| 187 | resource_dirs: ["bar/res"], |
| 188 | } |
| 189 | ` |
| 190 | |
| 191 | for _, testCase := range testEnforceRROTests { |
| 192 | t.Run(testCase.name, func(t *testing.T) { |
| 193 | config := testConfig(nil) |
Dan Willemsen | 674dc7f | 2018-03-12 18:06:05 -0700 | [diff] [blame] | 194 | config.TestProductVariables.ResourceOverlays = &resourceOverlays |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 195 | if testCase.enforceRROTargets != nil { |
Dan Willemsen | 674dc7f | 2018-03-12 18:06:05 -0700 | [diff] [blame] | 196 | config.TestProductVariables.EnforceRROTargets = &testCase.enforceRROTargets |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 197 | } |
| 198 | if testCase.enforceRROExcludedOverlays != nil { |
Dan Willemsen | 674dc7f | 2018-03-12 18:06:05 -0700 | [diff] [blame] | 199 | config.TestProductVariables.EnforceRROExcludedOverlays = &testCase.enforceRROExcludedOverlays |
Colin Cross | 890ff55 | 2017-11-30 20:13:19 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | ctx := testAppContext(config, bp, fs) |
| 203 | run(t, ctx, config) |
| 204 | |
| 205 | getOverlays := func(moduleName string) ([]string, []string) { |
| 206 | module := ctx.ModuleForTests(moduleName, "android_common") |
| 207 | overlayCompiledPaths := module.Output("aapt2/overlay.list").Inputs.Strings() |
| 208 | |
| 209 | var overlayFiles []string |
| 210 | for _, o := range overlayCompiledPaths { |
| 211 | overlayFiles = append(overlayFiles, module.Output(o).Inputs.Strings()...) |
| 212 | } |
| 213 | |
| 214 | rroDirs := module.Module().(*AndroidApp).rroDirs.Strings() |
| 215 | |
| 216 | return overlayFiles, rroDirs |
| 217 | } |
| 218 | |
| 219 | fooOverlayFiles, fooRRODirs := getOverlays("foo") |
| 220 | barOverlayFiles, barRRODirs := getOverlays("bar") |
| 221 | |
| 222 | if !reflect.DeepEqual(fooOverlayFiles, testCase.fooOverlayFiles) { |
| 223 | t.Errorf("expected foo overlay files:\n %#v\n got:\n %#v", |
| 224 | testCase.fooOverlayFiles, fooOverlayFiles) |
| 225 | } |
| 226 | if !reflect.DeepEqual(fooRRODirs, testCase.fooRRODirs) { |
| 227 | t.Errorf("expected foo rroDirs: %#v\n got:\n %#v", |
| 228 | testCase.fooRRODirs, fooRRODirs) |
| 229 | } |
| 230 | |
| 231 | if !reflect.DeepEqual(barOverlayFiles, testCase.barOverlayFiles) { |
| 232 | t.Errorf("expected bar overlay files:\n %#v\n got:\n %#v", |
| 233 | testCase.barOverlayFiles, barOverlayFiles) |
| 234 | } |
| 235 | if !reflect.DeepEqual(barRRODirs, testCase.barRRODirs) { |
| 236 | t.Errorf("expected bar rroDirs: %#v\n got:\n %#v", |
| 237 | testCase.barRRODirs, barRRODirs) |
| 238 | } |
| 239 | |
| 240 | }) |
| 241 | } |
| 242 | } |