blob: cbfdc2179727b5e4390b26bb25642ab960dcab5e [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"]
Yu Liueae7b362023-11-16 17:05:47 -080063 android.AssertIntEquals(t, "len(LOCAL_ACONFIG_FILES)", 2, len(makeVar))
64 android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb")
65 android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_bar/intermediate.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",
180 srcs: ["foo.aconfig"],
181 }
182
183 java_aconfig_library {
184 name: "my_java_aconfig_library",
185 aconfig_declarations: "my_aconfig_declarations",
186 %s
187 }
188 `, bpMode))
189
190 module := result.ModuleForTests("my_java_aconfig_library", "android_common")
191 rule := module.Rule("java_aconfig_library")
192 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
193}
194
Zi Wang275f6542023-11-09 14:59:31 -0800195func testCodegenModeWithError(t *testing.T, bpMode string, err string) {
196 android.GroupFixturePreparers(
197 PrepareForTestWithAconfigBuildComponents,
198 java.PrepareForTestWithJavaDefaultModules).
199 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
200 RunTestWithBp(t, fmt.Sprintf(`
201 aconfig_declarations {
202 name: "my_aconfig_declarations",
203 package: "com.example.package",
204 srcs: ["foo.aconfig"],
205 }
206
207 java_aconfig_library {
208 name: "my_java_aconfig_library",
209 aconfig_declarations: "my_aconfig_declarations",
210 %s
211 }
212 `, bpMode))
213}
214
Joe Onoratob7c294a2023-07-28 05:30:25 -0700215func TestDefaultProdMode(t *testing.T) {
216 testCodegenMode(t, "", "production")
217}
218
219func TestProdMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800220 testCodegenMode(t, "mode: `production`,", "production")
Joe Onoratob7c294a2023-07-28 05:30:25 -0700221}
222
223func TestTestMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800224 testCodegenMode(t, "mode: `test`,", "test")
225}
226
227func TestExportedMode(t *testing.T) {
228 testCodegenMode(t, "mode: `exported`,", "exported")
229}
230
231func TestUnsupportedMode(t *testing.T) {
232 testCodegenModeWithError(t, "mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode")
233}