blob: 2523abcbc941e09d5c62413ac45fc3419dfbdcb9 [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",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070037 package: "com.example.package",
38 srcs: ["foo.aconfig"],
39 }
40
41 java_aconfig_library {
Yu Liueae7b362023-11-16 17:05:47 -080042 name: "my_java_aconfig_library_foo",
43 aconfig_declarations: "my_aconfig_declarations_foo",
44 }
45
46 aconfig_declarations {
47 name: "my_aconfig_declarations_bar",
48 package: "com.example.package",
49 srcs: ["bar.aconfig"],
50 }
51
52 java_aconfig_library {
53 name: "my_java_aconfig_library_bar",
54 aconfig_declarations: "my_aconfig_declarations_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070055 }
56 `)
57
58 module := result.ModuleForTests("my_module", "android_common").Module()
59
60 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
61
62 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
Colin Crossd788b3e2023-11-28 13:14:56 -080063 android.AssertIntEquals(t, "len(LOCAL_ACONFIG_FILES)", 1, len(makeVar))
64 android.EnsureListContainsSuffix(t, makeVar, "android_common/aconfig_merged.pb")
Joe Onorato6fe59eb2023-07-16 13:20:33 -070065}
66
67func TestAndroidMkJavaLibrary(t *testing.T) {
68 bp := `
69 java_library {
70 name: "my_module",
71 srcs: [
72 "src/foo.java",
73 ],
74 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -080075 "my_java_aconfig_library_foo",
76 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070077 ],
78 platform_apis: true,
79 }
80 `
81
82 runJavaAndroidMkTest(t, bp)
83}
84
85func TestAndroidMkAndroidApp(t *testing.T) {
86 bp := `
87 android_app {
88 name: "my_module",
89 srcs: [
90 "src/foo.java",
91 ],
92 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -080093 "my_java_aconfig_library_foo",
94 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070095 ],
96 platform_apis: true,
97 }
98 `
99
100 runJavaAndroidMkTest(t, bp)
101}
102
103func TestAndroidMkBinary(t *testing.T) {
104 bp := `
105 java_binary {
106 name: "my_module",
107 srcs: [
108 "src/foo.java",
109 ],
110 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -0800111 "my_java_aconfig_library_foo",
112 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700113 ],
114 platform_apis: true,
115 main_class: "foo",
116 }
117 `
118
119 runJavaAndroidMkTest(t, bp)
120}
121
122func TestAndroidMkAndroidLibrary(t *testing.T) {
123 bp := `
124 android_library {
125 name: "my_module",
126 srcs: [
127 "src/foo.java",
128 ],
129 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -0800130 "my_java_aconfig_library_foo",
131 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700132 ],
133 platform_apis: true,
134 }
135 `
136
137 runJavaAndroidMkTest(t, bp)
138}
139
140func TestAndroidMkBinaryThatLinksAgainstAar(t *testing.T) {
141 // Tests AndroidLibrary's propagation of flags through JavaInfo
142 bp := `
143 android_library {
144 name: "some_library",
145 srcs: [
146 "src/foo.java",
147 ],
148 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -0800149 "my_java_aconfig_library_foo",
150 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700151 ],
152 platform_apis: true,
153 }
154 java_binary {
155 name: "my_module",
156 srcs: [
157 "src/bar.java",
158 ],
159 static_libs: [
160 "some_library",
161 ],
162 platform_apis: true,
163 main_class: "foo",
164 }
165 `
166
167 runJavaAndroidMkTest(t, bp)
168}
Joe Onoratob7c294a2023-07-28 05:30:25 -0700169
170func testCodegenMode(t *testing.T, bpMode string, ruleMode string) {
171 result := android.GroupFixturePreparers(
172 PrepareForTestWithAconfigBuildComponents,
173 java.PrepareForTestWithJavaDefaultModules).
174 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
175 RunTestWithBp(t, fmt.Sprintf(`
176 aconfig_declarations {
177 name: "my_aconfig_declarations",
178 package: "com.example.package",
179 srcs: ["foo.aconfig"],
180 }
181
182 java_aconfig_library {
183 name: "my_java_aconfig_library",
184 aconfig_declarations: "my_aconfig_declarations",
185 %s
186 }
187 `, bpMode))
188
189 module := result.ModuleForTests("my_java_aconfig_library", "android_common")
190 rule := module.Rule("java_aconfig_library")
191 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
192}
193
Zi Wang275f6542023-11-09 14:59:31 -0800194func testCodegenModeWithError(t *testing.T, bpMode string, err string) {
195 android.GroupFixturePreparers(
196 PrepareForTestWithAconfigBuildComponents,
197 java.PrepareForTestWithJavaDefaultModules).
198 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
199 RunTestWithBp(t, fmt.Sprintf(`
200 aconfig_declarations {
201 name: "my_aconfig_declarations",
202 package: "com.example.package",
203 srcs: ["foo.aconfig"],
204 }
205
206 java_aconfig_library {
207 name: "my_java_aconfig_library",
208 aconfig_declarations: "my_aconfig_declarations",
209 %s
210 }
211 `, bpMode))
212}
213
Joe Onoratob7c294a2023-07-28 05:30:25 -0700214func TestDefaultProdMode(t *testing.T) {
215 testCodegenMode(t, "", "production")
216}
217
218func TestProdMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800219 testCodegenMode(t, "mode: `production`,", "production")
Joe Onoratob7c294a2023-07-28 05:30:25 -0700220}
221
222func TestTestMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800223 testCodegenMode(t, "mode: `test`,", "test")
224}
225
226func TestExportedMode(t *testing.T) {
227 testCodegenMode(t, "mode: `exported`,", "exported")
228}
229
230func TestUnsupportedMode(t *testing.T) {
231 testCodegenModeWithError(t, "mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode")
232}