blob: a803672db0cd72b74c6f4a5f2cb5858b7d8cb4b8 [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
15package aconfig
16
17import (
Joe Onoratob7c294a2023-07-28 05:30:25 -070018 "fmt"
Joe Onorato6fe59eb2023-07-16 13:20:33 -070019 "strings"
20 "testing"
21
22 "android/soong/android"
23 "android/soong/java"
24)
25
26// Note: These tests cover the code in the java package. It'd be ideal of that code could
27// be in the aconfig package.
28
29// With the bp parameter that defines a my_module, make sure it has the LOCAL_ACONFIG_FILES entries
30func runJavaAndroidMkTest(t *testing.T, bp string) {
31 result := android.GroupFixturePreparers(
32 PrepareForTestWithAconfigBuildComponents,
33 java.PrepareForTestWithJavaDefaultModules).
34 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
35 RunTestWithBp(t, bp+`
36 aconfig_declarations {
37 name: "my_aconfig_declarations",
38 package: "com.example.package",
39 srcs: ["foo.aconfig"],
40 }
41
42 java_aconfig_library {
43 name: "my_java_aconfig_library",
44 aconfig_declarations: "my_aconfig_declarations",
45 }
46 `)
47
48 module := result.ModuleForTests("my_module", "android_common").Module()
49
50 entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0]
51
52 makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"]
53 android.AssertIntEquals(t, "len(LOCAL_ACONFIG_FILES)", 1, len(makeVar))
54 if !strings.HasSuffix(makeVar[0], "intermediate.pb") {
55 t.Errorf("LOCAL_ACONFIG_FILES should end with /intermediates.pb, instead it is: %s", makeVar[0])
56 }
57}
58
59func TestAndroidMkJavaLibrary(t *testing.T) {
60 bp := `
61 java_library {
62 name: "my_module",
63 srcs: [
64 "src/foo.java",
65 ],
66 static_libs: [
67 "my_java_aconfig_library",
68 ],
69 platform_apis: true,
70 }
71 `
72
73 runJavaAndroidMkTest(t, bp)
74}
75
76func TestAndroidMkAndroidApp(t *testing.T) {
77 bp := `
78 android_app {
79 name: "my_module",
80 srcs: [
81 "src/foo.java",
82 ],
83 static_libs: [
84 "my_java_aconfig_library",
85 ],
86 platform_apis: true,
87 }
88 `
89
90 runJavaAndroidMkTest(t, bp)
91}
92
93func TestAndroidMkBinary(t *testing.T) {
94 bp := `
95 java_binary {
96 name: "my_module",
97 srcs: [
98 "src/foo.java",
99 ],
100 static_libs: [
101 "my_java_aconfig_library",
102 ],
103 platform_apis: true,
104 main_class: "foo",
105 }
106 `
107
108 runJavaAndroidMkTest(t, bp)
109}
110
111func TestAndroidMkAndroidLibrary(t *testing.T) {
112 bp := `
113 android_library {
114 name: "my_module",
115 srcs: [
116 "src/foo.java",
117 ],
118 static_libs: [
119 "my_java_aconfig_library",
120 ],
121 platform_apis: true,
122 }
123 `
124
125 runJavaAndroidMkTest(t, bp)
126}
127
128func TestAndroidMkBinaryThatLinksAgainstAar(t *testing.T) {
129 // Tests AndroidLibrary's propagation of flags through JavaInfo
130 bp := `
131 android_library {
132 name: "some_library",
133 srcs: [
134 "src/foo.java",
135 ],
136 static_libs: [
137 "my_java_aconfig_library",
138 ],
139 platform_apis: true,
140 }
141 java_binary {
142 name: "my_module",
143 srcs: [
144 "src/bar.java",
145 ],
146 static_libs: [
147 "some_library",
148 ],
149 platform_apis: true,
150 main_class: "foo",
151 }
152 `
153
154 runJavaAndroidMkTest(t, bp)
155}
Joe Onoratob7c294a2023-07-28 05:30:25 -0700156
157func testCodegenMode(t *testing.T, bpMode string, ruleMode string) {
158 result := android.GroupFixturePreparers(
159 PrepareForTestWithAconfigBuildComponents,
160 java.PrepareForTestWithJavaDefaultModules).
161 ExtendWithErrorHandler(android.FixtureExpectsNoErrors).
162 RunTestWithBp(t, fmt.Sprintf(`
163 aconfig_declarations {
164 name: "my_aconfig_declarations",
165 package: "com.example.package",
166 srcs: ["foo.aconfig"],
167 }
168
169 java_aconfig_library {
170 name: "my_java_aconfig_library",
171 aconfig_declarations: "my_aconfig_declarations",
172 %s
173 }
174 `, bpMode))
175
176 module := result.ModuleForTests("my_java_aconfig_library", "android_common")
177 rule := module.Rule("java_aconfig_library")
178 android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode)
179}
180
Zi Wang275f6542023-11-09 14:59:31 -0800181func testCodegenModeWithError(t *testing.T, bpMode string, err string) {
182 android.GroupFixturePreparers(
183 PrepareForTestWithAconfigBuildComponents,
184 java.PrepareForTestWithJavaDefaultModules).
185 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)).
186 RunTestWithBp(t, fmt.Sprintf(`
187 aconfig_declarations {
188 name: "my_aconfig_declarations",
189 package: "com.example.package",
190 srcs: ["foo.aconfig"],
191 }
192
193 java_aconfig_library {
194 name: "my_java_aconfig_library",
195 aconfig_declarations: "my_aconfig_declarations",
196 %s
197 }
198 `, bpMode))
199}
200
Joe Onoratob7c294a2023-07-28 05:30:25 -0700201func TestDefaultProdMode(t *testing.T) {
202 testCodegenMode(t, "", "production")
203}
204
205func TestProdMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800206 testCodegenMode(t, "mode: `production`,", "production")
Joe Onoratob7c294a2023-07-28 05:30:25 -0700207}
208
209func TestTestMode(t *testing.T) {
Zi Wang275f6542023-11-09 14:59:31 -0800210 testCodegenMode(t, "mode: `test`,", "test")
211}
212
213func TestExportedMode(t *testing.T) {
214 testCodegenMode(t, "mode: `exported`,", "exported")
215}
216
217func TestUnsupportedMode(t *testing.T) {
218 testCodegenModeWithError(t, "mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode")
219}