blob: 7a61771387bbf2119c7eb21182e71412cd9bc83e [file] [log] [blame]
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001// 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
15package java
16
17import (
18 "android/soong/android"
19 "reflect"
Colin Crossb69301e2017-12-01 10:48:26 -080020 "sort"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080021 "testing"
22)
23
24var (
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 Cross527012a2017-11-30 22:56:16 -080038func 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 Cross3bc7ffa2017-11-22 16:19:37 -080042 }
43
Colin Cross527012a2017-11-30 22:56:16 -080044 for _, file := range resourceFiles {
45 appFS[file] = nil
46 }
47
48 return testContext(config, bp, appFS)
49}
50
51func 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 Cross3bc7ffa2017-11-22 16:19:37 -080059}
60
61func TestApp(t *testing.T) {
Colin Crossa97c5d32018-03-28 14:58:31 -070062 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 Cross3bc7ffa2017-11-22 16:19:37 -080069
Colin Crossa97c5d32018-03-28 14:58:31 -070070 foo := ctx.ModuleForTests("foo", "android_common")
Colin Cross3bc7ffa2017-11-22 16:19:37 -080071
Colin Crossa97c5d32018-03-28 14:58:31 -070072 expectedLinkImplicits := []string{"AndroidManifest.xml"}
Colin Cross3bc7ffa2017-11-22 16:19:37 -080073
Colin Crossa97c5d32018-03-28 14:58:31 -070074 frameworkRes := ctx.ModuleForTests("framework-res", "android_common")
75 expectedLinkImplicits = append(expectedLinkImplicits,
76 frameworkRes.Output("package-res.apk").Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080077
Colin Crossa97c5d32018-03-28 14:58:31 -070078 // 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 Crossb69301e2017-12-01 10:48:26 -080084
Colin Crossa97c5d32018-03-28 14:58:31 -070085 compiledResourceOutputs := compile.Outputs.Strings()
86 sort.Strings(compiledResourceOutputs)
Colin Crossb69301e2017-12-01 10:48:26 -080087
Colin Crossa97c5d32018-03-28 14:58:31 -070088 expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080089
Colin Crossa97c5d32018-03-28 14:58:31 -070090 list := foo.Output("aapt2/res.list")
91 expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080092
Colin Crossa97c5d32018-03-28 14:58:31 -070093 // 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 Cross3bc7ffa2017-11-22 16:19:37 -0800100 }
101}
Colin Cross890ff552017-11-30 20:13:19 -0800102
103var 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 Cross890ff552017-11-30 20:13:19 -0800117 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
Colin Cross68a70232018-01-04 14:50:13 -0800118 "device/vendor/blah/overlay/foo/res/values/strings.xml",
Colin Cross890ff552017-11-30 20:13:19 -0800119 },
120 fooRRODirs: nil,
121 barOverlayFiles: []string{
Colin Cross890ff552017-11-30 20:13:19 -0800122 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
Colin Cross68a70232018-01-04 14:50:13 -0800123 "device/vendor/blah/overlay/bar/res/values/strings.xml",
Colin Cross890ff552017-11-30 20:13:19 -0800124 },
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 Cross890ff552017-11-30 20:13:19 -0800138 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
Colin Cross68a70232018-01-04 14:50:13 -0800139 "device/vendor/blah/overlay/bar/res/values/strings.xml",
Colin Cross890ff552017-11-30 20:13:19 -0800140 },
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
162func 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 Willemsen674dc7f2018-03-12 18:06:05 -0700194 config.TestProductVariables.ResourceOverlays = &resourceOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800195 if testCase.enforceRROTargets != nil {
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700196 config.TestProductVariables.EnforceRROTargets = &testCase.enforceRROTargets
Colin Cross890ff552017-11-30 20:13:19 -0800197 }
198 if testCase.enforceRROExcludedOverlays != nil {
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700199 config.TestProductVariables.EnforceRROExcludedOverlays = &testCase.enforceRROExcludedOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800200 }
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}