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 15package codegen 16 17import ( 18 "fmt" 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 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 { 36 name: "my_aconfig_declarations_foo", 37 package: "com.example.package.foo", 38 container: "system", 39 srcs: ["foo.aconfig"], 40 } 41 42 java_aconfig_library { 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", 49 package: "com.example.package.bar", 50 container: "system", 51 srcs: ["bar.aconfig"], 52 } 53 54 java_aconfig_library { 55 name: "my_java_aconfig_library_bar", 56 aconfig_declarations: "my_aconfig_declarations_bar", 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"] 65 android.EnsureListContainsSuffix(t, makeVar, "android_common/system/aconfig_merged.pb") 66} 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: [ 76 "my_java_aconfig_library_foo", 77 "my_java_aconfig_library_bar", 78 ], 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: [ 94 "my_java_aconfig_library_foo", 95 "my_java_aconfig_library_bar", 96 ], 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: [ 112 "my_java_aconfig_library_foo", 113 "my_java_aconfig_library_bar", 114 ], 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: [ 131 "my_java_aconfig_library_foo", 132 "my_java_aconfig_library_bar", 133 ], 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: [ 150 "my_java_aconfig_library_foo", 151 "my_java_aconfig_library_bar", 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} 170 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 container: "com.android.foo", 181 srcs: ["foo.aconfig"], 182 exportable: true, 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 197func 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", 206 container: "com.android.foo", 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 218func TestDefaultProdMode(t *testing.T) { 219 testCodegenMode(t, "", "production") 220} 221 222func TestProdMode(t *testing.T) { 223 testCodegenMode(t, "mode: `production`,", "production") 224} 225 226func TestTestMode(t *testing.T) { 227 testCodegenMode(t, "mode: `test`,", "test") 228} 229 230func TestExportedMode(t *testing.T) { 231 testCodegenMode(t, "mode: `exported`,", "exported") 232} 233 234func TestForceReadOnlyMode(t *testing.T) { 235 testCodegenMode(t, "mode: `force-read-only`,", "force-read-only") 236} 237 238func TestUnsupportedMode(t *testing.T) { 239 testCodegenModeWithError(t, "mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode") 240} 241 242func 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: "vendor", 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} 290