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