blob: e89cd316fcf82ed97f9e354829fba90c3b4c05c1 [file] [log] [blame]
Joe Onorato175073c2023-06-01 14:42:59 -07001// Copyright 2018 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
Joe Onorato981c9262023-06-21 15:16:23 -070015package aconfig
Joe Onorato175073c2023-06-01 14:42:59 -070016
17import (
LaMont Jones21d04d92024-06-11 11:28:54 -070018 "slices"
Joe Onorato175073c2023-06-01 14:42:59 -070019 "strings"
20 "testing"
21
22 "android/soong/android"
23)
24
Joe Onorato981c9262023-06-21 15:16:23 -070025func TestAconfigDeclarations(t *testing.T) {
Joe Onorato175073c2023-06-01 14:42:59 -070026 bp := `
Joe Onorato981c9262023-06-21 15:16:23 -070027 aconfig_declarations {
Joe Onorato175073c2023-06-01 14:42:59 -070028 name: "module_name",
Joe Onorato81b25ed2023-06-21 13:49:37 -070029 package: "com.example.package",
Yu Liueae7b362023-11-16 17:05:47 -080030 container: "com.android.foo",
Zi Wang0e5d16c2024-02-08 06:19:34 +000031 exportable: true,
Zhi Dou8a35a6f2023-07-19 18:04:24 +000032 srcs: [
33 "foo.aconfig",
34 "bar.aconfig",
35 ],
Joe Onorato175073c2023-06-01 14:42:59 -070036 }
37 `
38 result := runTest(t, android.FixtureExpectsNoErrors, bp)
39
Joe Onorato981c9262023-06-21 15:16:23 -070040 module := result.ModuleForTests("module_name", "").Module().(*DeclarationsModule)
Joe Onorato175073c2023-06-01 14:42:59 -070041
42 // Check that the provider has the right contents
Yu Liu663e4502024-08-12 18:23:59 +000043 depData, _ := android.OtherModuleProvider(result, module, android.AconfigDeclarationsProviderKey)
Joe Onorato81b25ed2023-06-21 13:49:37 -070044 android.AssertStringEquals(t, "package", depData.Package, "com.example.package")
Yu Liueae7b362023-11-16 17:05:47 -080045 android.AssertStringEquals(t, "container", depData.Container, "com.android.foo")
Zi Wang0e5d16c2024-02-08 06:19:34 +000046 android.AssertBoolEquals(t, "exportable", depData.Exportable, true)
Jihoon Kangcca3e0c2023-11-29 19:35:29 +000047 if !strings.HasSuffix(depData.IntermediateCacheOutputPath.String(), "/intermediate.pb") {
48 t.Errorf("Missing intermediates proto path in provider: %s", depData.IntermediateCacheOutputPath.String())
49 }
50 if !strings.HasSuffix(depData.IntermediateDumpOutputPath.String(), "/intermediate.txt") {
51 t.Errorf("Missing intermediates text path in provider: %s", depData.IntermediateDumpOutputPath.String())
Joe Onorato175073c2023-06-01 14:42:59 -070052 }
53}
Zi Wang0e5d16c2024-02-08 06:19:34 +000054
55func TestAconfigDeclarationsWithExportableUnset(t *testing.T) {
56 bp := `
57 aconfig_declarations {
58 name: "module_name",
59 package: "com.example.package",
60 container: "com.android.foo",
61 srcs: [
62 "foo.aconfig",
63 "bar.aconfig",
64 ],
65 }
66 `
67 result := runTest(t, android.FixtureExpectsNoErrors, bp)
68
69 module := result.ModuleForTests("module_name", "").Module().(*DeclarationsModule)
Yu Liu663e4502024-08-12 18:23:59 +000070 depData, _ := android.OtherModuleProvider(result, module, android.AconfigDeclarationsProviderKey)
Zi Wang0e5d16c2024-02-08 06:19:34 +000071 android.AssertBoolEquals(t, "exportable", depData.Exportable, false)
72}
Yu Liueeff2222024-03-19 23:07:34 +000073
74func TestAconfigDeclarationsWithContainer(t *testing.T) {
75 bp := `
76 aconfig_declarations {
77 name: "module_name",
78 package: "com.example.package",
79 container: "com.android.foo",
80 srcs: [
81 "foo.aconfig",
82 ],
83 }
84 `
85 result := runTest(t, android.FixtureExpectsNoErrors, bp)
86
87 module := result.ModuleForTests("module_name", "")
88 rule := module.Rule("aconfig")
89 android.AssertStringEquals(t, "rule must contain container", rule.Args["container"], "--container com.android.foo")
90}
91
Yu Liu315a53c2024-04-24 16:41:57 +000092func TestMandatoryProperties(t *testing.T) {
93 testCases := []struct {
94 name string
95 expectedError string
96 bp string
97 }{
98 {
99 name: "Srcs missing from aconfig_declarations",
100 bp: `
101 aconfig_declarations {
102 name: "my_aconfig_declarations_foo",
103 package: "com.example.package",
104 container: "otherapex",
105 }`,
106 expectedError: `missing source files`,
107 },
108 {
109 name: "Package missing from aconfig_declarations",
110 bp: `
111 aconfig_declarations {
112 name: "my_aconfig_declarations_foo",
113 container: "otherapex",
114 srcs: ["foo.aconfig"],
115 }`,
116 expectedError: `missing package property`,
117 },
118 {
119 name: "Container missing from aconfig_declarations",
120 bp: `
121 aconfig_declarations {
122 name: "my_aconfig_declarations_foo",
123 package: "com.example.package",
124 srcs: ["foo.aconfig"],
125 }`,
126 expectedError: `missing container property`,
127 },
128 }
129 for _, test := range testCases {
130 t.Run(test.name, func(t *testing.T) {
131 errorHandler := android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError)
132 android.GroupFixturePreparers(PrepareForTestWithAconfigBuildComponents).
133 ExtendWithErrorHandler(errorHandler).
134 RunTestWithBp(t, test.bp)
135 })
136 }
Yu Liueeff2222024-03-19 23:07:34 +0000137}
LaMont Jones21d04d92024-06-11 11:28:54 -0700138
139func TestAssembleFileName(t *testing.T) {
140 testCases := []struct {
141 name string
142 releaseConfig string
143 path string
144 expectedValue string
145 }{
146 {
147 name: "active release config",
148 path: "file.path",
149 expectedValue: "file.path",
150 },
151 {
152 name: "release config FOO",
153 releaseConfig: "FOO",
154 path: "file.path",
155 expectedValue: "file-FOO.path",
156 },
157 }
158 for _, test := range testCases {
159 actualValue := assembleFileName(test.releaseConfig, test.path)
160 if actualValue != test.expectedValue {
161 t.Errorf("Expected %q found %q", test.expectedValue, actualValue)
162 }
163 }
164}
165
166func TestGenerateAndroidBuildActions(t *testing.T) {
167 testCases := []struct {
168 name string
169 buildFlags map[string]string
170 bp string
171 errorHandler android.FixtureErrorHandler
172 }{
173 {
174 name: "generate extra",
175 buildFlags: map[string]string{
176 "RELEASE_ACONFIG_EXTRA_RELEASE_CONFIGS": "config2",
177 "RELEASE_ACONFIG_VALUE_SETS": "aconfig_value_set-config1",
178 "RELEASE_ACONFIG_VALUE_SETS_config2": "aconfig_value_set-config2",
179 },
180 bp: `
181 aconfig_declarations {
182 name: "module_name",
183 package: "com.example.package",
184 container: "com.android.foo",
185 srcs: [
186 "foo.aconfig",
187 "bar.aconfig",
188 ],
189 }
190 aconfig_value_set {
191 name: "aconfig_value_set-config1",
192 values: []
193 }
194 aconfig_value_set {
195 name: "aconfig_value_set-config2",
196 values: []
197 }
198 `,
199 },
200 }
201 for _, test := range testCases {
202 fixture := PrepareForTest(t, addBuildFlagsForTest(test.buildFlags))
203 if test.errorHandler != nil {
204 fixture = fixture.ExtendWithErrorHandler(test.errorHandler)
205 }
206 result := fixture.RunTestWithBp(t, test.bp)
207 module := result.ModuleForTests("module_name", "").Module().(*DeclarationsModule)
Yu Liu663e4502024-08-12 18:23:59 +0000208 depData, _ := android.OtherModuleProvider(result, module, android.AconfigReleaseDeclarationsProviderKey)
LaMont Jones21d04d92024-06-11 11:28:54 -0700209 expectedKeys := []string{""}
210 for _, rc := range strings.Split(test.buildFlags["RELEASE_ACONFIG_EXTRA_RELEASE_CONFIGS"], " ") {
211 expectedKeys = append(expectedKeys, rc)
212 }
213 slices.Sort(expectedKeys)
214 actualKeys := []string{}
215 for rc := range depData {
216 actualKeys = append(actualKeys, rc)
217 }
218 slices.Sort(actualKeys)
219 android.AssertStringEquals(t, "provider keys", strings.Join(expectedKeys, " "), strings.Join(actualKeys, " "))
220 for _, rc := range actualKeys {
221 if !strings.HasSuffix(depData[rc].IntermediateCacheOutputPath.String(), assembleFileName(rc, "/intermediate.pb")) {
222 t.Errorf("Incorrect intermediates proto path in provider for release config %s: %s", rc, depData[rc].IntermediateCacheOutputPath.String())
223 }
224 if !strings.HasSuffix(depData[rc].IntermediateDumpOutputPath.String(), assembleFileName(rc, "/intermediate.txt")) {
225 t.Errorf("Incorrect intermediates text path in provider for release config %s: %s", rc, depData[rc].IntermediateDumpOutputPath.String())
226 }
227 }
228 }
229}