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", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 37 | package: "com.example.package", |
| 38 | srcs: ["foo.aconfig"], |
| 39 | } |
| 40 | |
| 41 | java_aconfig_library { |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 42 | name: "my_java_aconfig_library_foo", |
| 43 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 44 | } |
| 45 | |
| 46 | aconfig_declarations { |
| 47 | name: "my_aconfig_declarations_bar", |
| 48 | package: "com.example.package", |
| 49 | srcs: ["bar.aconfig"], |
| 50 | } |
| 51 | |
| 52 | java_aconfig_library { |
| 53 | name: "my_java_aconfig_library_bar", |
| 54 | aconfig_declarations: "my_aconfig_declarations_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 55 | } |
| 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 Cross | d788b3e | 2023-11-28 13:14:56 -0800 | [diff] [blame^] | 63 | android.AssertIntEquals(t, "len(LOCAL_ACONFIG_FILES)", 1, len(makeVar)) |
| 64 | android.EnsureListContainsSuffix(t, makeVar, "android_common/aconfig_merged.pb") |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | func TestAndroidMkJavaLibrary(t *testing.T) { |
| 68 | bp := ` |
| 69 | java_library { |
| 70 | name: "my_module", |
| 71 | srcs: [ |
| 72 | "src/foo.java", |
| 73 | ], |
| 74 | static_libs: [ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 75 | "my_java_aconfig_library_foo", |
| 76 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 77 | ], |
| 78 | platform_apis: true, |
| 79 | } |
| 80 | ` |
| 81 | |
| 82 | runJavaAndroidMkTest(t, bp) |
| 83 | } |
| 84 | |
| 85 | func TestAndroidMkAndroidApp(t *testing.T) { |
| 86 | bp := ` |
| 87 | android_app { |
| 88 | name: "my_module", |
| 89 | srcs: [ |
| 90 | "src/foo.java", |
| 91 | ], |
| 92 | static_libs: [ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 93 | "my_java_aconfig_library_foo", |
| 94 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 95 | ], |
| 96 | platform_apis: true, |
| 97 | } |
| 98 | ` |
| 99 | |
| 100 | runJavaAndroidMkTest(t, bp) |
| 101 | } |
| 102 | |
| 103 | func TestAndroidMkBinary(t *testing.T) { |
| 104 | bp := ` |
| 105 | java_binary { |
| 106 | name: "my_module", |
| 107 | srcs: [ |
| 108 | "src/foo.java", |
| 109 | ], |
| 110 | static_libs: [ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 111 | "my_java_aconfig_library_foo", |
| 112 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 113 | ], |
| 114 | platform_apis: true, |
| 115 | main_class: "foo", |
| 116 | } |
| 117 | ` |
| 118 | |
| 119 | runJavaAndroidMkTest(t, bp) |
| 120 | } |
| 121 | |
| 122 | func TestAndroidMkAndroidLibrary(t *testing.T) { |
| 123 | bp := ` |
| 124 | android_library { |
| 125 | name: "my_module", |
| 126 | srcs: [ |
| 127 | "src/foo.java", |
| 128 | ], |
| 129 | static_libs: [ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 130 | "my_java_aconfig_library_foo", |
| 131 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 132 | ], |
| 133 | platform_apis: true, |
| 134 | } |
| 135 | ` |
| 136 | |
| 137 | runJavaAndroidMkTest(t, bp) |
| 138 | } |
| 139 | |
| 140 | func TestAndroidMkBinaryThatLinksAgainstAar(t *testing.T) { |
| 141 | // Tests AndroidLibrary's propagation of flags through JavaInfo |
| 142 | bp := ` |
| 143 | android_library { |
| 144 | name: "some_library", |
| 145 | srcs: [ |
| 146 | "src/foo.java", |
| 147 | ], |
| 148 | static_libs: [ |
Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 149 | "my_java_aconfig_library_foo", |
| 150 | "my_java_aconfig_library_bar", |
Joe Onorato | 6fe59eb | 2023-07-16 13:20:33 -0700 | [diff] [blame] | 151 | ], |
| 152 | platform_apis: true, |
| 153 | } |
| 154 | java_binary { |
| 155 | name: "my_module", |
| 156 | srcs: [ |
| 157 | "src/bar.java", |
| 158 | ], |
| 159 | static_libs: [ |
| 160 | "some_library", |
| 161 | ], |
| 162 | platform_apis: true, |
| 163 | main_class: "foo", |
| 164 | } |
| 165 | ` |
| 166 | |
| 167 | runJavaAndroidMkTest(t, bp) |
| 168 | } |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 169 | |
| 170 | func testCodegenMode(t *testing.T, bpMode string, ruleMode string) { |
| 171 | result := android.GroupFixturePreparers( |
| 172 | PrepareForTestWithAconfigBuildComponents, |
| 173 | java.PrepareForTestWithJavaDefaultModules). |
| 174 | ExtendWithErrorHandler(android.FixtureExpectsNoErrors). |
| 175 | RunTestWithBp(t, fmt.Sprintf(` |
| 176 | aconfig_declarations { |
| 177 | name: "my_aconfig_declarations", |
| 178 | package: "com.example.package", |
| 179 | srcs: ["foo.aconfig"], |
| 180 | } |
| 181 | |
| 182 | java_aconfig_library { |
| 183 | name: "my_java_aconfig_library", |
| 184 | aconfig_declarations: "my_aconfig_declarations", |
| 185 | %s |
| 186 | } |
| 187 | `, bpMode)) |
| 188 | |
| 189 | module := result.ModuleForTests("my_java_aconfig_library", "android_common") |
| 190 | rule := module.Rule("java_aconfig_library") |
| 191 | android.AssertStringEquals(t, "rule must contain test mode", rule.Args["mode"], ruleMode) |
| 192 | } |
| 193 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 194 | func testCodegenModeWithError(t *testing.T, bpMode string, err string) { |
| 195 | android.GroupFixturePreparers( |
| 196 | PrepareForTestWithAconfigBuildComponents, |
| 197 | java.PrepareForTestWithJavaDefaultModules). |
| 198 | ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(err)). |
| 199 | RunTestWithBp(t, fmt.Sprintf(` |
| 200 | aconfig_declarations { |
| 201 | name: "my_aconfig_declarations", |
| 202 | package: "com.example.package", |
| 203 | srcs: ["foo.aconfig"], |
| 204 | } |
| 205 | |
| 206 | java_aconfig_library { |
| 207 | name: "my_java_aconfig_library", |
| 208 | aconfig_declarations: "my_aconfig_declarations", |
| 209 | %s |
| 210 | } |
| 211 | `, bpMode)) |
| 212 | } |
| 213 | |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 214 | func TestDefaultProdMode(t *testing.T) { |
| 215 | testCodegenMode(t, "", "production") |
| 216 | } |
| 217 | |
| 218 | func TestProdMode(t *testing.T) { |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 219 | testCodegenMode(t, "mode: `production`,", "production") |
Joe Onorato | b7c294a | 2023-07-28 05:30:25 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | func TestTestMode(t *testing.T) { |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 223 | testCodegenMode(t, "mode: `test`,", "test") |
| 224 | } |
| 225 | |
| 226 | func TestExportedMode(t *testing.T) { |
| 227 | testCodegenMode(t, "mode: `exported`,", "exported") |
| 228 | } |
| 229 | |
| 230 | func TestUnsupportedMode(t *testing.T) { |
| 231 | testCodegenModeWithError(t, "mode: `unsupported`,", "mode: \"unsupported\" is not a supported mode") |
| 232 | } |