blob: 87b54a47f46035a158ca3adc43bb21d9a9fd43b0 [file] [log] [blame]
Joe Onorato6fe59eb2023-07-16 13:20:33 -07001// Copyright 2023 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
Yu Liueae7b362023-11-16 17:05:47 -080015package codegen
Joe Onorato6fe59eb2023-07-16 13:20:33 -070016
17import (
Joe Onoratob7c294a2023-07-28 05:30:25 -070018 "fmt"
Joe Onorato6fe59eb2023-07-16 13:20:33 -070019 "testing"
20
21 "android/soong/android"
22 "android/soong/java"
23)
24
25// Note: These tests cover the code in the java package. It'd be ideal of that code could
26// be in the aconfig package.
27
28// With the bp parameter that defines a my_module, make sure it has the LOCAL_ACONFIG_FILES entries
29func runJavaAndroidMkTest(t *testing.T, bp string) {
30 result := android.GroupFixturePreparers(
31 PrepareForTestWithAconfigBuildComponents,
32 java.PrepareForTestWithJavaDefaultModules).
33 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
34 RunTestWithBp(t, bp+`
35 aconfig_declarations {
Yu Liueae7b362023-11-16 17:05:47 -080036 name: "my_aconfig_declarations_foo",
Dennis Shen4e7773d2024-01-05 19:06:50 +000037 package: "com.example.package.foo",
Yu Liu315a53c2024-04-24 16:41:57 +000038 container: "system",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070039 srcs: ["foo.aconfig"],
40 }
41
42 java_aconfig_library {
Yu Liueae7b362023-11-16 17:05:47 -080043 name: "my_java_aconfig_library_foo",
44 aconfig_declarations: "my_aconfig_declarations_foo",
45 }
46
47 aconfig_declarations {
48 name: "my_aconfig_declarations_bar",
Dennis Shen4e7773d2024-01-05 19:06:50 +000049 package: "com.example.package.bar",
Yu Liu315a53c2024-04-24 16:41:57 +000050 container: "system",
Yu Liueae7b362023-11-16 17:05:47 -080051 srcs: ["bar.aconfig"],
52 }
53
54 java_aconfig_library {
55 name: "my_java_aconfig_library_bar",
56 aconfig_declarations: "my_aconfig_declarations_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070057 }
58 `)
59
60 module := result.ModuleForTests("my_module", "android_common").Module()
61
62 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
63
64 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
Yu Liu315a53c2024-04-24 16:41:57 +000065 android.EnsureListContainsSuffix(t, makeVar, "android_common/system/aconfig_merged.pb")
Joe Onorato6fe59eb2023-07-16 13:20:33 -070066}
67
68func TestAndroidMkJavaLibrary(t *testing.T) {
69 bp := `
70 java_library {
71 name: "my_module",
72 srcs: [
73 "src/foo.java",
74 ],
75 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -080076 "my_java_aconfig_library_foo",
77 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070078 ],
79 platform_apis: true,
80 }
81 `
82
83 runJavaAndroidMkTest(t, bp)
84}
85
86func TestAndroidMkAndroidApp(t *testing.T) {
87 bp := `
88 android_app {
89 name: "my_module",
90 srcs: [
91 "src/foo.java",
92 ],
93 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -080094 "my_java_aconfig_library_foo",
95 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070096 ],
97 platform_apis: true,
98 }
99 `
100
101 runJavaAndroidMkTest(t, bp)
102}
103
104func TestAndroidMkBinary(t *testing.T) {
105 bp := `
106 java_binary {
107 name: "my_module",
108 srcs: [
109 "src/foo.java",
110 ],
111 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -0800112 "my_java_aconfig_library_foo",
113 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700114 ],
115 platform_apis: true,
116 main_class: "foo",
117 }
118 `
119
120 runJavaAndroidMkTest(t, bp)
121}
122
123func TestAndroidMkAndroidLibrary(t *testing.T) {
124 bp := `
125 android_library {
126 name: "my_module",
127 srcs: [
128 "src/foo.java",
129 ],
130 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -0800131 "my_java_aconfig_library_foo",
132 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700133 ],
134 platform_apis: true,
135 }
136 `
137
138 runJavaAndroidMkTest(t, bp)
139}
140
141func TestAndroidMkBinaryThatLinksAgainstAar(t *testing.T) {
142 // Tests AndroidLibrary's propagation of flags through JavaInfo
143 bp := `
144 android_library {
145 name: "some_library",
146 srcs: [
147 "src/foo.java",
148 ],
149 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -0800150 "my_java_aconfig_library_foo",
151 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700152 ],
153 platform_apis: true,
154 }
155 java_binary {
156 name: "my_module",
157 srcs: [
158 "src/bar.java",
159 ],
160 static_libs: [
161 "some_library",
162 ],
163 platform_apis: true,
164 main_class: "foo",
165 }
166 `
167
168 runJavaAndroidMkTest(t, bp)
169}
Joe Onoratob7c294a2023-07-28 05:30:25 -0700170
171func testCodegenMode(t *testing.T, bpMode string, ruleMode string) {
172 result := android.GroupFixturePreparers(
173 PrepareForTestWithAconfigBuildComponents,
174 java.PrepareForTestWithJavaDefaultModules).
175 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
176 RunTestWithBp(t, fmt.Sprintf(`
177 aconfig_declarations {
178 name: "my_aconfig_declarations",
179 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +0000180 container: "com.android.foo",
Joe Onoratob7c294a2023-07-28 05:30:25 -0700181 srcs: ["foo.aconfig"],
Zi Wang0e5d16c2024-02-08 06:19:34 +0000182 exportable: true,
Joe Onoratob7c294a2023-07-28 05:30:25 -0700183 }
184
185 java_aconfig_library {
186 name: "my_java_aconfig_library",
187 aconfig_declarations: "my_aconfig_declarations",
188 %s
189 }
190 `, bpMode))
191
192 module := result.ModuleForTests("my_java_aconfig_library", "android_common")
193 rule := module.Rule("java_aconfig_library")
194 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
195}
196
Zi Wang275f6542023-11-09 14:59:31 -0800197func testCodegenModeWithError(t *testing.T, bpMode string, err string) {
198 android.GroupFixturePreparers(
199 PrepareForTestWithAconfigBuildComponents,
200 java.PrepareForTestWithJavaDefaultModules).
201 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
202 RunTestWithBp(t, fmt.Sprintf(`
203 aconfig_declarations {
204 name: "my_aconfig_declarations",
205 package: "com.example.package",
Yu Liu315a53c2024-04-24 16:41:57 +0000206 container: "com.android.foo",
Zi Wang275f6542023-11-09 14:59:31 -0800207 srcs: ["foo.aconfig"],
208 }
209
210 java_aconfig_library {
211 name: "my_java_aconfig_library",
212 aconfig_declarations: "my_aconfig_declarations",
213 %s
214 }
215 `, bpMode))
216}
217
Joe Onoratob7c294a2023-07-28 05:30:25 -0700218func TestDefaultProdMode(t *testing.T) {
219 testCodegenMode(t, "", "production")
220}
221
222func TestProdMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800223 testCodegenMode(t, "mode: `production`,", "production")
Joe Onoratob7c294a2023-07-28 05:30:25 -0700224}
225
226func TestTestMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800227 testCodegenMode(t, "mode: `test`,", "test")
228}
229
230func TestExportedMode(t *testing.T) {
231 testCodegenMode(t, "mode: `exported`,", "exported")
232}
233
Zhi Dou70e21242023-12-20 23:14:34 +0000234func TestForceReadOnlyMode(t *testing.T) {
235 testCodegenMode(t, "mode: `force-read-only`,", "force-read-only")
236}
237
Zi Wang275f6542023-11-09 14:59:31 -0800238func TestUnsupportedMode(t *testing.T) {
239 testCodegenModeWithError(t, "mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode")
240}
Yu Liu315a53c2024-04-24 16:41:57 +0000241
242func TestMkEntriesMatchedContainer(t *testing.T) {
243 result := android.GroupFixturePreparers(
244 PrepareForTestWithAconfigBuildComponents,
245 java.PrepareForTestWithJavaDefaultModules).
246 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
247 RunTestWithBp(t, `
248 aconfig_declarations {
249 name: "my_aconfig_declarations_foo",
250 package: "com.example.package.foo",
251 container: "system",
252 srcs: ["foo.aconfig"],
253 }
254
255 java_aconfig_library {
256 name: "my_java_aconfig_library_foo",
257 aconfig_declarations: "my_aconfig_declarations_foo",
258 }
259
260 aconfig_declarations {
261 name: "my_aconfig_declarations_bar",
262 package: "com.example.package.bar",
263 container: "system_ext",
264 srcs: ["bar.aconfig"],
265 }
266
267 java_aconfig_library {
268 name: "my_java_aconfig_library_bar",
269 aconfig_declarations: "my_aconfig_declarations_bar",
270 }
271
272 java_library {
273 name: "my_module",
274 srcs: [
275 "src/foo.java",
276 ],
277 static_libs: [
278 "my_java_aconfig_library_foo",
279 "my_java_aconfig_library_bar",
280 ],
281 platform_apis: true,
282 }
283 `)
284
285 module := result.ModuleForTests("my_module", "android_common").Module()
286 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
287 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
288 android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
289}