Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 1 | // 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 Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 15 | package codegen |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 18 | "fmt" |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 19 | "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 |
| 29 | func 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 Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 36 | name: "my_aconfig_declarations_foo", |
Dennis Shen | 4e7773d | 2024-01-05 19:06:50 +0000 | [diff] [blame] | 37 | package: "com.example.package.foo", |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame^] | 38 | container: "system", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 39 | srcs: ["foo.aconfig"], |
| 40 | } |
| 41 | |
| 42 | java_aconfig_library { |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 43 | name: "my_java_aconfig_library_foo", |
| 44 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 45 | } |
| 46 | |
| 47 | aconfig_declarations { |
| 48 | name: "my_aconfig_declarations_bar", |
Dennis Shen | 4e7773d | 2024-01-05 19:06:50 +0000 | [diff] [blame] | 49 | package: "com.example.package.bar", |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame^] | 50 | container: "system", |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 51 | srcs: ["bar.aconfig"], |
| 52 | } |
| 53 | |
| 54 | java_aconfig_library { |
| 55 | name: "my_java_aconfig_library_bar", |
| 56 | aconfig_declarations: "my_aconfig_declarations_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 57 | } |
| 58 | `) |
| 59 | |
| 60 | module := result.ModuleForTests("my_module", "android_common").Module() |
| 61 | |
| 62 | entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0] |
| 63 | |
| 64 | makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"] |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame^] | 65 | android.EnsureListContainsSuffix(t, makeVar, "android_common/system/aconfig_merged.pb") |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | func TestAndroidMkJavaLibrary(t *testing.T) { |
| 69 | bp := ` |
| 70 | java_library { |
| 71 | name: "my_module", |
| 72 | srcs: [ |
| 73 | "src/foo.java", |
| 74 | ], |
| 75 | static_libs: [ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 76 | "my_java_aconfig_library_foo", |
| 77 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 78 | ], |
| 79 | platform_apis: true, |
| 80 | } |
| 81 | ` |
| 82 | |
| 83 | runJavaAndroidMkTest(t, bp) |
| 84 | } |
| 85 | |
| 86 | func TestAndroidMkAndroidApp(t *testing.T) { |
| 87 | bp := ` |
| 88 | android_app { |
| 89 | name: "my_module", |
| 90 | srcs: [ |
| 91 | "src/foo.java", |
| 92 | ], |
| 93 | static_libs: [ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 94 | "my_java_aconfig_library_foo", |
| 95 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 96 | ], |
| 97 | platform_apis: true, |
| 98 | } |
| 99 | ` |
| 100 | |
| 101 | runJavaAndroidMkTest(t, bp) |
| 102 | } |
| 103 | |
| 104 | func TestAndroidMkBinary(t *testing.T) { |
| 105 | bp := ` |
| 106 | java_binary { |
| 107 | name: "my_module", |
| 108 | srcs: [ |
| 109 | "src/foo.java", |
| 110 | ], |
| 111 | static_libs: [ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 112 | "my_java_aconfig_library_foo", |
| 113 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 114 | ], |
| 115 | platform_apis: true, |
| 116 | main_class: "foo", |
| 117 | } |
| 118 | ` |
| 119 | |
| 120 | runJavaAndroidMkTest(t, bp) |
| 121 | } |
| 122 | |
| 123 | func TestAndroidMkAndroidLibrary(t *testing.T) { |
| 124 | bp := ` |
| 125 | android_library { |
| 126 | name: "my_module", |
| 127 | srcs: [ |
| 128 | "src/foo.java", |
| 129 | ], |
| 130 | static_libs: [ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 131 | "my_java_aconfig_library_foo", |
| 132 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 133 | ], |
| 134 | platform_apis: true, |
| 135 | } |
| 136 | ` |
| 137 | |
| 138 | runJavaAndroidMkTest(t, bp) |
| 139 | } |
| 140 | |
| 141 | func 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 Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 150 | "my_java_aconfig_library_foo", |
| 151 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 152 | ], |
| 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 Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 170 | |
| 171 | func 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", |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame^] | 180 | container: "com.android.foo", |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 181 | srcs: ["foo.aconfig"], |
Zi Wang | 0e5d16c | 2024-02-08 06:19:34 +0000 | [diff] [blame] | 182 | exportable: true, |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | java_aconfig_library { |
| 186 | name: "my_java_aconfig_library", |
| 187 | aconfig_declarations: "my_aconfig_declarations", |
| 188 | %s |
| 189 | } |
| 190 | `, bpMode)) |
| 191 | |
| 192 | module := result.ModuleForTests("my_java_aconfig_library", "android_common") |
| 193 | rule := module.Rule("java_aconfig_library") |
| 194 | android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode) |
| 195 | } |
| 196 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 197 | func testCodegenModeWithError(t *testing.T, bpMode string, err string) { |
| 198 | android.GroupFixturePreparers( |
| 199 | PrepareForTestWithAconfigBuildComponents, |
| 200 | java.PrepareForTestWithJavaDefaultModules). |
| 201 | ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)). |
| 202 | RunTestWithBp(t, fmt.Sprintf(` |
| 203 | aconfig_declarations { |
| 204 | name: "my_aconfig_declarations", |
| 205 | package: "com.example.package", |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame^] | 206 | container: "com.android.foo", |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 207 | srcs: ["foo.aconfig"], |
| 208 | } |
| 209 | |
| 210 | java_aconfig_library { |
| 211 | name: "my_java_aconfig_library", |
| 212 | aconfig_declarations: "my_aconfig_declarations", |
| 213 | %s |
| 214 | } |
| 215 | `, bpMode)) |
| 216 | } |
| 217 | |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 218 | func TestDefaultProdMode(t *testing.T) { |
| 219 | testCodegenMode(t, "", "production") |
| 220 | } |
| 221 | |
| 222 | func TestProdMode(t *testing.T) { |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 223 | testCodegenMode(t, "mode: `production`,", "production") |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | func TestTestMode(t *testing.T) { |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 227 | testCodegenMode(t, "mode: `test`,", "test") |
| 228 | } |
| 229 | |
| 230 | func TestExportedMode(t *testing.T) { |
| 231 | testCodegenMode(t, "mode: `exported`,", "exported") |
| 232 | } |
| 233 | |
Zhi Dou | 70e2124 | 2023-12-20 23:14:34 +0000 | [diff] [blame] | 234 | func TestForceReadOnlyMode(t *testing.T) { |
| 235 | testCodegenMode(t, "mode: `force-read-only`,", "force-read-only") |
| 236 | } |
| 237 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 238 | func TestUnsupportedMode(t *testing.T) { |
| 239 | testCodegenModeWithError(t, "mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode") |
| 240 | } |
Yu Liu | 315a53c | 2024-04-24 16:41:57 +0000 | [diff] [blame^] | 241 | |
| 242 | func TestMkEntriesMatchedContainer(t *testing.T) { |
| 243 | result := android.GroupFixturePreparers( |
| 244 | PrepareForTestWithAconfigBuildComponents, |
| 245 | java.PrepareForTestWithJavaDefaultModules). |
| 246 | ExtendWithErrorHandler(android.FixtureExpectsNoErrors). |
| 247 | RunTestWithBp(t, ` |
| 248 | aconfig_declarations { |
| 249 | name: "my_aconfig_declarations_foo", |
| 250 | package: "com.example.package.foo", |
| 251 | container: "system", |
| 252 | srcs: ["foo.aconfig"], |
| 253 | } |
| 254 | |
| 255 | java_aconfig_library { |
| 256 | name: "my_java_aconfig_library_foo", |
| 257 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 258 | } |
| 259 | |
| 260 | aconfig_declarations { |
| 261 | name: "my_aconfig_declarations_bar", |
| 262 | package: "com.example.package.bar", |
| 263 | container: "system_ext", |
| 264 | srcs: ["bar.aconfig"], |
| 265 | } |
| 266 | |
| 267 | java_aconfig_library { |
| 268 | name: "my_java_aconfig_library_bar", |
| 269 | aconfig_declarations: "my_aconfig_declarations_bar", |
| 270 | } |
| 271 | |
| 272 | java_library { |
| 273 | name: "my_module", |
| 274 | srcs: [ |
| 275 | "src/foo.java", |
| 276 | ], |
| 277 | static_libs: [ |
| 278 | "my_java_aconfig_library_foo", |
| 279 | "my_java_aconfig_library_bar", |
| 280 | ], |
| 281 | platform_apis: true, |
| 282 | } |
| 283 | `) |
| 284 | |
| 285 | module := result.ModuleForTests("my_module", "android_common").Module() |
| 286 | entry := android.AndroidMkEntriesForTest(t, result.TestContext, module)[0] |
| 287 | makeVar := entry.EntryMap["LOCAL_ACONFIG_FILES"] |
| 288 | android.EnsureListContainsSuffix(t, makeVar, "my_aconfig_declarations_foo/intermediate.pb") |
| 289 | } |