blob: 85d2675d2ba57a465ac55be75100689a929c1790 [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",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070038 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",
Dennis Shen4e7773d2024-01-05 19:06:50 +000048 package: "com.example.package.bar",
Yu Liueae7b362023-11-16 17:05:47 -080049 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.EnsureListContainsSuffix(t, makeVar, "android_common/aconfig_merged.pb")
Joe Onorato6fe59eb2023-07-16 13:20:33 -070064}
65
66func TestAndroidMkJavaLibrary(t *testing.T) {
67 bp := `
68 java_library {
69 name: "my_module",
70 srcs: [
71 "src/foo.java",
72 ],
73 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -080074 "my_java_aconfig_library_foo",
75 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070076 ],
77 platform_apis: true,
78 }
79 `
80
81 runJavaAndroidMkTest(t, bp)
82}
83
84func TestAndroidMkAndroidApp(t *testing.T) {
85 bp := `
86 android_app {
87 name: "my_module",
88 srcs: [
89 "src/foo.java",
90 ],
91 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -080092 "my_java_aconfig_library_foo",
93 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -070094 ],
95 platform_apis: true,
96 }
97 `
98
99 runJavaAndroidMkTest(t, bp)
100}
101
102func TestAndroidMkBinary(t *testing.T) {
103 bp := `
104 java_binary {
105 name: "my_module",
106 srcs: [
107 "src/foo.java",
108 ],
109 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -0800110 "my_java_aconfig_library_foo",
111 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700112 ],
113 platform_apis: true,
114 main_class: "foo",
115 }
116 `
117
118 runJavaAndroidMkTest(t, bp)
119}
120
121func TestAndroidMkAndroidLibrary(t *testing.T) {
122 bp := `
123 android_library {
124 name: "my_module",
125 srcs: [
126 "src/foo.java",
127 ],
128 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -0800129 "my_java_aconfig_library_foo",
130 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700131 ],
132 platform_apis: true,
133 }
134 `
135
136 runJavaAndroidMkTest(t, bp)
137}
138
139func TestAndroidMkBinaryThatLinksAgainstAar(t *testing.T) {
140 // Tests AndroidLibrary's propagation of flags through JavaInfo
141 bp := `
142 android_library {
143 name: "some_library",
144 srcs: [
145 "src/foo.java",
146 ],
147 static_libs: [
Yu Liueae7b362023-11-16 17:05:47 -0800148 "my_java_aconfig_library_foo",
149 "my_java_aconfig_library_bar",
Joe Onorato6fe59eb2023-07-16 13:20:33 -0700150 ],
151 platform_apis: true,
152 }
153 java_binary {
154 name: "my_module",
155 srcs: [
156 "src/bar.java",
157 ],
158 static_libs: [
159 "some_library",
160 ],
161 platform_apis: true,
162 main_class: "foo",
163 }
164 `
165
166 runJavaAndroidMkTest(t, bp)
167}
Joe Onoratob7c294a2023-07-28 05:30:25 -0700168
169func testCodegenMode(t *testing.T, bpMode string, ruleMode string) {
170 result := android.GroupFixturePreparers(
171 PrepareForTestWithAconfigBuildComponents,
172 java.PrepareForTestWithJavaDefaultModules).
173 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
174 RunTestWithBp(t, fmt.Sprintf(`
175 aconfig_declarations {
176 name: "my_aconfig_declarations",
177 package: "com.example.package",
178 srcs: ["foo.aconfig"],
179 }
180
181 java_aconfig_library {
182 name: "my_java_aconfig_library",
183 aconfig_declarations: "my_aconfig_declarations",
184 %s
185 }
186 `, bpMode))
187
188 module := result.ModuleForTests("my_java_aconfig_library", "android_common")
189 rule := module.Rule("java_aconfig_library")
190 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
191}
192
Zi Wang275f6542023-11-09 14:59:31 -0800193func testCodegenModeWithError(t *testing.T, bpMode string, err string) {
194 android.GroupFixturePreparers(
195 PrepareForTestWithAconfigBuildComponents,
196 java.PrepareForTestWithJavaDefaultModules).
197 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
198 RunTestWithBp(t, fmt.Sprintf(`
199 aconfig_declarations {
200 name: "my_aconfig_declarations",
201 package: "com.example.package",
202 srcs: ["foo.aconfig"],
203 }
204
205 java_aconfig_library {
206 name: "my_java_aconfig_library",
207 aconfig_declarations: "my_aconfig_declarations",
208 %s
209 }
210 `, bpMode))
211}
212
Joe Onoratob7c294a2023-07-28 05:30:25 -0700213func TestDefaultProdMode(t *testing.T) {
214 testCodegenMode(t, "", "production")
215}
216
217func TestProdMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800218 testCodegenMode(t, "mode: `production`,", "production")
Joe Onoratob7c294a2023-07-28 05:30:25 -0700219}
220
221func TestTestMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800222 testCodegenMode(t, "mode: `test`,", "test")
223}
224
225func TestExportedMode(t *testing.T) {
226 testCodegenMode(t, "mode: `exported`,", "exported")
227}
228
Zhi Dou70e21242023-12-20 23:14:34 +0000229func TestForceReadOnlyMode(t *testing.T) {
230 testCodegenMode(t, "mode: `force-read-only`,", "force-read-only")
231}
232
Zi Wang275f6542023-11-09 14:59:31 -0800233func TestUnsupportedMode(t *testing.T) {
234 testCodegenModeWithError(t, "mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode")
235}