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