Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Paul Duffin | 1a39332 | 2020-11-18 16:36:47 +0000 | [diff] [blame] | 18 | "fmt" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 19 | "os" |
| 20 | "path/filepath" |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 21 | "reflect" |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 22 | "runtime" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 23 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 24 | "strings" |
| 25 | "testing" |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 26 | |
Jeongik Cha | 28df257 | 2019-11-11 10:46:36 +0900 | [diff] [blame] | 27 | "github.com/google/blueprint/proptools" |
| 28 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 29 | "android/soong/android" |
| 30 | "android/soong/cc" |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 31 | "android/soong/dexpreopt" |
Colin Cross | 1661aff | 2021-03-12 17:56:51 -0800 | [diff] [blame] | 32 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
Paul Duffin | 70d3bee | 2021-03-21 11:26:05 +0000 | [diff] [blame] | 35 | // Legacy preparer used for running tests within the java package. |
| 36 | // |
| 37 | // This includes everything that was needed to run any test in the java package prior to the |
| 38 | // introduction of the test fixtures. Tests that are being converted to use fixtures directly |
| 39 | // rather than through the testJava...() methods should avoid using this and instead use the |
| 40 | // various preparers directly, using android.GroupFixturePreparers(...) to group them when |
| 41 | // necessary. |
| 42 | // |
| 43 | // deprecated |
| 44 | var prepareForJavaTest = android.GroupFixturePreparers( |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 45 | genrule.PrepareForTestWithGenRuleBuildComponents, |
| 46 | // Get the CC build components but not default modules. |
| 47 | cc.PrepareForTestWithCcBuildComponents, |
| 48 | // Include all the default java modules. |
Jiakai Zhang | b95998b | 2023-05-11 16:39:27 +0100 | [diff] [blame] | 49 | PrepareForTestWithDexpreopt, |
Paul Duffin | 42da69d | 2021-03-22 13:41:36 +0000 | [diff] [blame] | 50 | PrepareForTestWithOverlayBuildComponents, |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 51 | android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 52 | ctx.RegisterPreSingletonType("sdk_versions", sdkPreSingletonFactory) |
| 53 | }), |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 54 | ) |
| 55 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 56 | func TestMain(m *testing.M) { |
Paul Duffin | d6339af | 2021-03-22 18:05:04 +0000 | [diff] [blame] | 57 | os.Exit(m.Run()) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 60 | // testJavaError is a legacy way of running tests of java modules that expect errors. |
| 61 | // |
| 62 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 63 | // |
| 64 | // deprecated |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 65 | func testJavaError(t *testing.T, pattern string, bp string) (*android.TestContext, android.Config) { |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 66 | t.Helper() |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 67 | result := android.GroupFixturePreparers( |
Paul Duffin | 9fc9f53 | 2021-03-23 15:41:11 +0000 | [diff] [blame] | 68 | prepareForJavaTest, dexpreopt.PrepareForTestByEnablingDexpreopt). |
Paul Duffin | 6bac49c | 2021-03-12 21:28:15 +0000 | [diff] [blame] | 69 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)). |
| 70 | RunTestWithBp(t, bp) |
| 71 | return result.TestContext, result.Config |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 72 | } |
| 73 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 74 | // testJavaWithFS runs tests using the prepareForJavaTest |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 75 | // |
| 76 | // See testJava for an explanation as to how to stop using this deprecated method. |
| 77 | // |
| 78 | // deprecated |
| 79 | func testJavaWithFS(t *testing.T, bp string, fs android.MockFS) (*android.TestContext, android.Config) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 80 | t.Helper() |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 81 | result := android.GroupFixturePreparers( |
| 82 | prepareForJavaTest, fs.AddToFixture()).RunTestWithBp(t, bp) |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 83 | return result.TestContext, result.Config |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 86 | // testJava runs tests using the prepareForJavaTest |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 87 | // |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 88 | // Do not add any new usages of this, instead use the prepareForJavaTest directly as it makes it |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 89 | // much easier to customize the test behavior. |
| 90 | // |
| 91 | // If it is necessary to customize the behavior of an existing test that uses this then please first |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 92 | // convert the test to using prepareForJavaTest first and then in a following change add the |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 93 | // appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify |
| 94 | // that it did not change the test behavior unexpectedly. |
| 95 | // |
| 96 | // deprecated |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 97 | func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 98 | t.Helper() |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 99 | result := prepareForJavaTest.RunTestWithBp(t, bp) |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 100 | return result.TestContext, result.Config |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 101 | } |
| 102 | |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 103 | // defaultModuleToPath constructs a path to the turbine generate jar for a default test module that |
| 104 | // is defined in PrepareForIntegrationTestWithJava |
| 105 | func defaultModuleToPath(name string) string { |
Paul Duffin | 76101fa | 2021-03-13 09:55:25 +0000 | [diff] [blame] | 106 | switch { |
| 107 | case name == `""`: |
| 108 | return name |
| 109 | case strings.HasSuffix(name, ".jar"): |
| 110 | return name |
| 111 | default: |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 112 | return filepath.Join("out", "soong", ".intermediates", defaultJavaDir, name, "android_common", "turbine-combined", name+".jar") |
Paul Duffin | 76101fa | 2021-03-13 09:55:25 +0000 | [diff] [blame] | 113 | } |
Paul Duffin | 95bdab4 | 2021-03-08 21:48:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Paul Duffin | 76e5c8a | 2021-03-20 14:19:46 +0000 | [diff] [blame] | 116 | // Test that the PrepareForTestWithJavaDefaultModules provides all the files that it uses by |
| 117 | // running it in a fixture that requires all source files to exist. |
| 118 | func TestPrepareForTestWithJavaDefaultModules(t *testing.T) { |
| 119 | android.GroupFixturePreparers( |
| 120 | PrepareForTestWithJavaDefaultModules, |
| 121 | android.PrepareForTestDisallowNonExistentPaths, |
| 122 | ).RunTest(t) |
| 123 | } |
| 124 | |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 125 | func TestJavaLinkType(t *testing.T) { |
| 126 | testJava(t, ` |
| 127 | java_library { |
| 128 | name: "foo", |
| 129 | srcs: ["a.java"], |
| 130 | libs: ["bar"], |
| 131 | static_libs: ["baz"], |
| 132 | } |
| 133 | |
| 134 | java_library { |
| 135 | name: "bar", |
| 136 | sdk_version: "current", |
| 137 | srcs: ["b.java"], |
| 138 | } |
| 139 | |
| 140 | java_library { |
| 141 | name: "baz", |
| 142 | sdk_version: "system_current", |
| 143 | srcs: ["c.java"], |
| 144 | } |
| 145 | `) |
| 146 | |
Steven Moreland | 0029898 | 2020-11-17 21:44:36 +0000 | [diff] [blame] | 147 | testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", ` |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 148 | java_library { |
| 149 | name: "foo", |
| 150 | srcs: ["a.java"], |
| 151 | libs: ["bar"], |
| 152 | sdk_version: "current", |
| 153 | static_libs: ["baz"], |
| 154 | } |
| 155 | |
| 156 | java_library { |
| 157 | name: "bar", |
| 158 | sdk_version: "current", |
| 159 | srcs: ["b.java"], |
| 160 | } |
| 161 | |
| 162 | java_library { |
| 163 | name: "baz", |
| 164 | sdk_version: "system_current", |
| 165 | srcs: ["c.java"], |
| 166 | } |
| 167 | `) |
| 168 | |
| 169 | testJava(t, ` |
| 170 | java_library { |
| 171 | name: "foo", |
| 172 | srcs: ["a.java"], |
| 173 | libs: ["bar"], |
| 174 | sdk_version: "system_current", |
| 175 | static_libs: ["baz"], |
| 176 | } |
| 177 | |
| 178 | java_library { |
| 179 | name: "bar", |
| 180 | sdk_version: "current", |
| 181 | srcs: ["b.java"], |
| 182 | } |
| 183 | |
| 184 | java_library { |
| 185 | name: "baz", |
| 186 | sdk_version: "system_current", |
| 187 | srcs: ["c.java"], |
| 188 | } |
| 189 | `) |
| 190 | |
Steven Moreland | 0029898 | 2020-11-17 21:44:36 +0000 | [diff] [blame] | 191 | testJavaError(t, "consider adjusting sdk_version: OR platform_apis:", ` |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 192 | java_library { |
| 193 | name: "foo", |
| 194 | srcs: ["a.java"], |
| 195 | libs: ["bar"], |
| 196 | sdk_version: "system_current", |
| 197 | static_libs: ["baz"], |
| 198 | } |
| 199 | |
| 200 | java_library { |
| 201 | name: "bar", |
| 202 | sdk_version: "current", |
| 203 | srcs: ["b.java"], |
| 204 | } |
| 205 | |
| 206 | java_library { |
| 207 | name: "baz", |
| 208 | srcs: ["c.java"], |
| 209 | } |
| 210 | `) |
| 211 | } |
| 212 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 213 | func TestSimple(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 214 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 215 | java_library { |
| 216 | name: "foo", |
| 217 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 218 | libs: ["bar"], |
| 219 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | java_library { |
| 223 | name: "bar", |
| 224 | srcs: ["b.java"], |
| 225 | } |
| 226 | |
| 227 | java_library { |
| 228 | name: "baz", |
| 229 | srcs: ["c.java"], |
| 230 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 231 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 232 | |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 233 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 234 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 235 | |
| 236 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 237 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 238 | } |
| 239 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 240 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 241 | barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 242 | bazTurbine := filepath.Join("out", "soong", ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 243 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 244 | android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], barTurbine) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 245 | |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 246 | android.AssertStringDoesContain(t, "foo classpath", javac.Args["classpath"], bazTurbine) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 247 | |
| 248 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 249 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 253 | func TestExportedPlugins(t *testing.T) { |
| 254 | type Result struct { |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 255 | library string |
| 256 | processors string |
| 257 | disableTurbine bool |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 258 | } |
| 259 | var tests = []struct { |
| 260 | name string |
| 261 | extra string |
| 262 | results []Result |
| 263 | }{ |
| 264 | { |
| 265 | name: "Exported plugin is not a direct plugin", |
| 266 | extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`, |
| 267 | results: []Result{{library: "exports", processors: "-proc:none"}}, |
| 268 | }, |
| 269 | { |
| 270 | name: "Exports plugin to dependee", |
| 271 | extra: ` |
| 272 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 273 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 274 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 275 | `, |
| 276 | results: []Result{ |
| 277 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 278 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 279 | }, |
| 280 | }, |
| 281 | { |
| 282 | name: "Exports plugin to android_library", |
| 283 | extra: ` |
| 284 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 285 | android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 286 | android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 287 | `, |
| 288 | results: []Result{ |
| 289 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 290 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 291 | }, |
| 292 | }, |
| 293 | { |
| 294 | name: "Exports plugin is not propagated via transitive deps", |
| 295 | extra: ` |
| 296 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 297 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 298 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]} |
| 299 | `, |
| 300 | results: []Result{ |
| 301 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 302 | {library: "bar", processors: "-proc:none"}, |
| 303 | }, |
| 304 | }, |
| 305 | { |
| 306 | name: "Exports plugin appends to plugins", |
| 307 | extra: ` |
| 308 | java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"} |
| 309 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 310 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]} |
| 311 | `, |
| 312 | results: []Result{ |
| 313 | {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"}, |
| 314 | }, |
| 315 | }, |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 316 | { |
| 317 | name: "Exports plugin to with generates_api to dependee", |
| 318 | extra: ` |
| 319 | java_library{name: "exports", exported_plugins: ["plugin_generates_api"]} |
| 320 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 321 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 322 | `, |
| 323 | results: []Result{ |
| 324 | {library: "foo", processors: "-processor com.android.TestPlugin", disableTurbine: true}, |
| 325 | {library: "bar", processors: "-processor com.android.TestPlugin", disableTurbine: true}, |
| 326 | }, |
| 327 | }, |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | for _, test := range tests { |
| 331 | t.Run(test.name, func(t *testing.T) { |
| 332 | ctx, _ := testJava(t, ` |
| 333 | java_plugin { |
| 334 | name: "plugin", |
| 335 | processor_class: "com.android.TestPlugin", |
| 336 | } |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 337 | java_plugin { |
| 338 | name: "plugin_generates_api", |
| 339 | generates_api: true, |
| 340 | processor_class: "com.android.TestPlugin", |
| 341 | } |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 342 | `+test.extra) |
| 343 | |
| 344 | for _, want := range test.results { |
| 345 | javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac") |
| 346 | if javac.Args["processor"] != want.processors { |
| 347 | t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"]) |
| 348 | } |
Colin Cross | c9fe10f | 2020-11-19 18:06:03 -0800 | [diff] [blame] | 349 | turbine := ctx.ModuleForTests(want.library, "android_common").MaybeRule("turbine") |
| 350 | disableTurbine := turbine.BuildParams.Rule == nil |
| 351 | if disableTurbine != want.disableTurbine { |
| 352 | t.Errorf("For library %v, expected disableTurbine %v, found %v", want.library, want.disableTurbine, disableTurbine) |
| 353 | } |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 354 | } |
| 355 | }) |
| 356 | } |
| 357 | } |
| 358 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 359 | func TestSdkVersionByPartition(t *testing.T) { |
| 360 | testJavaError(t, "sdk_version must have a value when the module is located at vendor or product", ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 361 | java_library { |
| 362 | name: "foo", |
| 363 | srcs: ["a.java"], |
| 364 | vendor: true, |
| 365 | } |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 366 | `) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 367 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 368 | testJava(t, ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 369 | java_library { |
| 370 | name: "bar", |
| 371 | srcs: ["b.java"], |
| 372 | } |
| 373 | `) |
| 374 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 375 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 376 | bp := ` |
| 377 | java_library { |
| 378 | name: "foo", |
| 379 | srcs: ["a.java"], |
| 380 | product_specific: true, |
| 381 | } |
| 382 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 383 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 384 | errorHandler := android.FixtureExpectsNoErrors |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 385 | if enforce { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 386 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern("sdk_version must have a value when the module is located at vendor or product") |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 387 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 388 | |
| 389 | android.GroupFixturePreparers( |
| 390 | PrepareForTestWithJavaDefaultModules, |
| 391 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 392 | variables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
| 393 | }), |
| 394 | ). |
| 395 | ExtendWithErrorHandler(errorHandler). |
| 396 | RunTestWithBp(t, bp) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 400 | func TestArchSpecific(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 401 | ctx, _ := testJava(t, ` |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 402 | java_library { |
| 403 | name: "foo", |
| 404 | srcs: ["a.java"], |
| 405 | target: { |
| 406 | android: { |
| 407 | srcs: ["b.java"], |
| 408 | }, |
| 409 | }, |
| 410 | } |
| 411 | `) |
| 412 | |
| 413 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 414 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 415 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 416 | } |
| 417 | } |
| 418 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 419 | func TestBinary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 420 | ctx, _ := testJava(t, ` |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 421 | java_library_host { |
| 422 | name: "foo", |
| 423 | srcs: ["a.java"], |
| 424 | } |
| 425 | |
| 426 | java_binary_host { |
| 427 | name: "bar", |
| 428 | srcs: ["b.java"], |
| 429 | static_libs: ["foo"], |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 430 | jni_libs: ["libjni"], |
| 431 | } |
| 432 | |
| 433 | cc_library_shared { |
| 434 | name: "libjni", |
| 435 | host_supported: true, |
| 436 | device_supported: false, |
| 437 | stl: "none", |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 438 | } |
| 439 | `) |
| 440 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 441 | buildOS := ctx.Config().BuildOS.String() |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 442 | |
| 443 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 444 | barJar := bar.Output("bar.jar").Output.String() |
| 445 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 446 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 447 | |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 448 | libjni := ctx.ModuleForTests("libjni", buildOS+"_x86_64_shared") |
| 449 | libjniSO := libjni.Rule("Cp").Output.String() |
| 450 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 451 | // Test that the install binary wrapper depends on the installed jar file |
Colin Cross | c179ea6 | 2020-10-09 10:54:15 -0700 | [diff] [blame] | 452 | if g, w := barWrapperDeps, barJar; !android.InList(w, g) { |
| 453 | t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 454 | } |
Colin Cross | 89226d9 | 2020-10-09 19:00:54 -0700 | [diff] [blame] | 455 | |
| 456 | // Test that the install binary wrapper depends on the installed JNI libraries |
| 457 | if g, w := barWrapperDeps, libjniSO; !android.InList(w, g) { |
| 458 | t.Errorf("expected binary wrapper implicits to contain %q, got %q", w, g) |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 459 | } |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 460 | } |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 461 | |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 462 | func TestTest(t *testing.T) { |
| 463 | ctx, _ := testJava(t, ` |
| 464 | java_test_host { |
| 465 | name: "foo", |
| 466 | srcs: ["a.java"], |
| 467 | jni_libs: ["libjni"], |
| 468 | } |
| 469 | |
| 470 | cc_library_shared { |
| 471 | name: "libjni", |
| 472 | host_supported: true, |
| 473 | device_supported: false, |
| 474 | stl: "none", |
| 475 | } |
| 476 | `) |
| 477 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 478 | buildOS := ctx.Config().BuildOS.String() |
Colin Cross | f8d9c49 | 2021-01-26 11:01:43 -0800 | [diff] [blame] | 479 | |
| 480 | foo := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost) |
| 481 | |
| 482 | expected := "lib64/libjni.so" |
| 483 | if runtime.GOOS == "darwin" { |
| 484 | expected = "lib64/libjni.dylib" |
| 485 | } |
| 486 | |
| 487 | fooTestData := foo.data |
| 488 | if len(fooTestData) != 1 || fooTestData[0].Rel() != expected { |
| 489 | t.Errorf(`expected foo test data relative path [%q], got %q`, |
| 490 | expected, fooTestData.Strings()) |
| 491 | } |
| 492 | } |
| 493 | |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 494 | func TestHostBinaryNoJavaDebugInfoOverride(t *testing.T) { |
| 495 | bp := ` |
| 496 | java_library { |
| 497 | name: "target_library", |
| 498 | srcs: ["a.java"], |
| 499 | } |
| 500 | |
| 501 | java_binary_host { |
| 502 | name: "host_binary", |
| 503 | srcs: ["b.java"], |
| 504 | } |
| 505 | ` |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 506 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 507 | result := android.GroupFixturePreparers( |
| 508 | PrepareForTestWithJavaDefaultModules, |
| 509 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 510 | variables.MinimizeJavaDebugInfo = proptools.BoolPtr(true) |
| 511 | }), |
| 512 | ).RunTestWithBp(t, bp) |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 513 | |
Liz Kammer | 7941b30 | 2020-07-28 13:27:34 -0700 | [diff] [blame] | 514 | // first, check that the -g flag is added to target modules |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 515 | targetLibrary := result.ModuleForTests("target_library", "android_common") |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 516 | targetJavaFlags := targetLibrary.Module().VariablesForTests()["javacFlags"] |
| 517 | if !strings.Contains(targetJavaFlags, "-g:source,lines") { |
| 518 | t.Errorf("target library javac flags %v should contain "+ |
| 519 | "-g:source,lines override with MinimizeJavaDebugInfo", targetJavaFlags) |
| 520 | } |
| 521 | |
| 522 | // check that -g is not overridden for host modules |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 523 | buildOS := result.Config.BuildOS.String() |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 524 | hostBinary := result.ModuleForTests("host_binary", buildOS+"_common") |
Alex Humesky | 2070e32 | 2020-06-09 20:23:08 -0400 | [diff] [blame] | 525 | hostJavaFlags := hostBinary.Module().VariablesForTests()["javacFlags"] |
| 526 | if strings.Contains(hostJavaFlags, "-g:source,lines") { |
| 527 | t.Errorf("java_binary_host javac flags %v should not have "+ |
| 528 | "-g:source,lines override with MinimizeJavaDebugInfo", hostJavaFlags) |
| 529 | } |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 532 | func TestPrebuilts(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 533 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 534 | java_library { |
| 535 | name: "foo", |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 536 | srcs: ["a.java", ":stubs-source"], |
Paul Duffin | fcfd791 | 2020-01-31 17:54:30 +0000 | [diff] [blame] | 537 | libs: ["bar", "sdklib"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 538 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 541 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 542 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 543 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 546 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 547 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 548 | jars: ["b.jar"], |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 549 | sdk_version: "current", |
| 550 | compile_dex: true, |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 551 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 552 | |
| 553 | dex_import { |
| 554 | name: "qux", |
| 555 | jars: ["b.jar"], |
| 556 | } |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 557 | |
| 558 | java_sdk_library_import { |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 559 | name: "sdklib", |
| 560 | public: { |
| 561 | jars: ["c.jar"], |
| 562 | }, |
| 563 | } |
| 564 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 565 | prebuilt_stubs_sources { |
| 566 | name: "stubs-source", |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 567 | srcs: ["stubs/sources"], |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 568 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 569 | |
| 570 | java_test_import { |
| 571 | name: "test", |
| 572 | jars: ["a.jar"], |
| 573 | test_suites: ["cts"], |
| 574 | test_config: "AndroidTest.xml", |
| 575 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 576 | `) |
| 577 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 578 | fooModule := ctx.ModuleForTests("foo", "android_common") |
| 579 | javac := fooModule.Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 580 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 581 | barModule := ctx.ModuleForTests("bar", "android_common") |
| 582 | barJar := barModule.Rule("combineJar").Output |
| 583 | bazModule := ctx.ModuleForTests("baz", "android_common") |
| 584 | bazJar := bazModule.Rule("combineJar").Output |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 585 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 586 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 587 | fooLibrary := fooModule.Module().(*Library) |
Chaohui Wang | dcbe33c | 2022-10-11 11:13:30 +0800 | [diff] [blame] | 588 | assertDeepEquals(t, "foo unique sources incorrect", |
| 589 | []string{"a.java"}, fooLibrary.uniqueSrcFiles.Strings()) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 590 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 591 | assertDeepEquals(t, "foo java source jars incorrect", |
| 592 | []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"}, |
| 593 | android.NormalizePathsForTesting(fooLibrary.compiledSrcJars)) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 594 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 595 | if !strings.Contains(javac.Args["classpath"], barJar.String()) { |
| 596 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 599 | barDexJar := barModule.Module().(*Import).DexJarBuildPath() |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 600 | if barDexJar.IsSet() { |
| 601 | t.Errorf("bar dex jar build path expected to be set, got %s", barDexJar) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 604 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 605 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 606 | } |
| 607 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 608 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() { |
| 609 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String()) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 610 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 611 | |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 612 | bazDexJar := bazModule.Module().(*Import).DexJarBuildPath().Path() |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 613 | expectedDexJar := "out/soong/.intermediates/baz/android_common/dex/baz.jar" |
| 614 | android.AssertPathRelativeToTopEquals(t, "baz dex jar build path", expectedDexJar, bazDexJar) |
Liz Kammer | d6c31d2 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 615 | |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 616 | ctx.ModuleForTests("qux", "android_common").Rule("Cp") |
Wei Li | 598f92d | 2023-01-04 17:12:24 -0800 | [diff] [blame] | 617 | |
| 618 | entries := android.AndroidMkEntriesForTest(t, ctx, fooModule.Module())[0] |
| 619 | android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "java_library", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0]) |
| 620 | entries = android.AndroidMkEntriesForTest(t, ctx, barModule.Module())[0] |
| 621 | android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "java_import", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0]) |
| 622 | entries = android.AndroidMkEntriesForTest(t, ctx, ctx.ModuleForTests("sdklib", "android_common").Module())[0] |
| 623 | android.AssertStringEquals(t, "unexpected LOCAL_SOONG_MODULE_TYPE", "java_sdk_library_import", entries.EntryMap["LOCAL_SOONG_MODULE_TYPE"][0]) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 626 | func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) { |
| 627 | if !reflect.DeepEqual(expected, actual) { |
| 628 | t.Errorf("%s: expected %q, found %q", message, expected, actual) |
| 629 | } |
| 630 | } |
| 631 | |
Paul Duffin | 1a39332 | 2020-11-18 16:36:47 +0000 | [diff] [blame] | 632 | func TestPrebuiltStubsSources(t *testing.T) { |
| 633 | test := func(t *testing.T, sourcesPath string, expectedInputs []string) { |
| 634 | ctx, _ := testJavaWithFS(t, fmt.Sprintf(` |
| 635 | prebuilt_stubs_sources { |
| 636 | name: "stubs-source", |
| 637 | srcs: ["%s"], |
| 638 | }`, sourcesPath), map[string][]byte{ |
| 639 | "stubs/sources/pkg/A.java": nil, |
| 640 | "stubs/sources/pkg/B.java": nil, |
| 641 | }) |
| 642 | |
| 643 | zipSrc := ctx.ModuleForTests("stubs-source", "android_common").Rule("zip_src") |
| 644 | if expected, actual := expectedInputs, zipSrc.Inputs.Strings(); !reflect.DeepEqual(expected, actual) { |
| 645 | t.Errorf("mismatch of inputs to soong_zip: expected %q, actual %q", expected, actual) |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | t.Run("empty/missing directory", func(t *testing.T) { |
Colin Cross | ce3a51d | 2021-03-19 16:22:12 -0700 | [diff] [blame] | 650 | test(t, "empty-directory", nil) |
Paul Duffin | 1a39332 | 2020-11-18 16:36:47 +0000 | [diff] [blame] | 651 | }) |
| 652 | |
| 653 | t.Run("non-empty set of sources", func(t *testing.T) { |
| 654 | test(t, "stubs/sources", []string{ |
| 655 | "stubs/sources/pkg/A.java", |
| 656 | "stubs/sources/pkg/B.java", |
| 657 | }) |
| 658 | }) |
| 659 | } |
| 660 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 661 | func TestDefaults(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 662 | ctx, _ := testJava(t, ` |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 663 | java_defaults { |
| 664 | name: "defaults", |
| 665 | srcs: ["a.java"], |
| 666 | libs: ["bar"], |
| 667 | static_libs: ["baz"], |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 668 | optimize: {enabled: false}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | java_library { |
| 672 | name: "foo", |
| 673 | defaults: ["defaults"], |
| 674 | } |
| 675 | |
| 676 | java_library { |
| 677 | name: "bar", |
| 678 | srcs: ["b.java"], |
| 679 | } |
| 680 | |
| 681 | java_library { |
| 682 | name: "baz", |
| 683 | srcs: ["c.java"], |
| 684 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 685 | |
| 686 | android_test { |
| 687 | name: "atestOptimize", |
| 688 | defaults: ["defaults"], |
| 689 | optimize: {enabled: true}, |
| 690 | } |
| 691 | |
| 692 | android_test { |
| 693 | name: "atestNoOptimize", |
| 694 | defaults: ["defaults"], |
| 695 | } |
| 696 | |
| 697 | android_test { |
| 698 | name: "atestDefault", |
| 699 | srcs: ["a.java"], |
| 700 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 701 | `) |
| 702 | |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 703 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 704 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 705 | |
| 706 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 707 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 708 | } |
| 709 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 710 | barTurbine := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 711 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 712 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 715 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 716 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 717 | t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 718 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 719 | |
| 720 | atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8") |
| 721 | if atestOptimize.Output == nil { |
| 722 | t.Errorf("atestOptimize should optimize APK") |
| 723 | } |
| 724 | |
| 725 | atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8") |
| 726 | if atestNoOptimize.Output == nil { |
| 727 | t.Errorf("atestNoOptimize should not optimize APK") |
| 728 | } |
| 729 | |
Jared Duke | 63a3da9 | 2022-06-02 19:11:14 +0000 | [diff] [blame] | 730 | atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("d8") |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 731 | if atestDefault.Output == nil { |
Jared Duke | 63a3da9 | 2022-06-02 19:11:14 +0000 | [diff] [blame] | 732 | t.Errorf("atestDefault should not optimize APK") |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 733 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 736 | func TestResources(t *testing.T) { |
| 737 | var table = []struct { |
| 738 | name string |
| 739 | prop string |
| 740 | extra string |
| 741 | args string |
| 742 | }{ |
| 743 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 744 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 745 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 746 | prop: `java_resource_dirs: ["java-res"]`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 747 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 748 | }, |
| 749 | { |
| 750 | // Test that a module with java_resources includes the files |
| 751 | name: "resource files", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 752 | prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`, |
| 753 | args: "-C . -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 754 | }, |
| 755 | { |
| 756 | // Test that a module with a filegroup in java_resources includes the files with the |
| 757 | // path prefix |
| 758 | name: "resource filegroup", |
| 759 | prop: `java_resources: [":foo-res"]`, |
| 760 | extra: ` |
| 761 | filegroup { |
| 762 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 763 | path: "java-res", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 764 | srcs: ["java-res/a/a", "java-res/b/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 765 | }`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 766 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 767 | }, |
| 768 | { |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 769 | // Test that a module with wildcards in java_resource_dirs has the correct path prefixes |
| 770 | name: "wildcard dirs", |
| 771 | prop: `java_resource_dirs: ["java-res/*"]`, |
| 772 | args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b", |
| 773 | }, |
| 774 | { |
| 775 | // Test that a module exclude_java_resource_dirs excludes the files |
| 776 | name: "wildcard dirs", |
| 777 | prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`, |
| 778 | args: "-C java-res/a -f java-res/a/a", |
| 779 | }, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 780 | { |
| 781 | // Test wildcards in java_resources |
| 782 | name: "wildcard files", |
| 783 | prop: `java_resources: ["java-res/**/*"]`, |
| 784 | args: "-C . -f java-res/a/a -f java-res/b/b", |
| 785 | }, |
| 786 | { |
| 787 | // Test exclude_java_resources with java_resources |
| 788 | name: "wildcard files with exclude", |
| 789 | prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`, |
| 790 | args: "-C . -f java-res/a/a", |
| 791 | }, |
| 792 | { |
| 793 | // Test exclude_java_resources with java_resource_dirs |
| 794 | name: "resource dirs with exclude files", |
| 795 | prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`, |
| 796 | args: "-C java-res -f java-res/a/a", |
| 797 | }, |
| 798 | { |
| 799 | // Test exclude_java_resource_dirs with java_resource_dirs |
| 800 | name: "resource dirs with exclude files", |
| 801 | prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`, |
| 802 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
| 803 | }, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | for _, test := range table { |
| 807 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 808 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 809 | java_library { |
| 810 | name: "foo", |
| 811 | srcs: [ |
| 812 | "a.java", |
| 813 | "b.java", |
| 814 | "c.java", |
| 815 | ], |
| 816 | `+test.prop+`, |
| 817 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 818 | `+test.extra, |
| 819 | map[string][]byte{ |
| 820 | "java-res/a/a": nil, |
| 821 | "java-res/b/b": nil, |
| 822 | "java-res2/a": nil, |
| 823 | }, |
| 824 | ) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 825 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 826 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 827 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 828 | |
| 829 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 830 | t.Errorf("foo combined jars %v does not contain %q", |
| 831 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 832 | } |
| 833 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 834 | if fooRes.Args["jarArgs"] != test.args { |
| 835 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 836 | fooRes.Args["jarArgs"], test.args) |
| 837 | } |
| 838 | }) |
| 839 | } |
| 840 | } |
| 841 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 842 | func TestIncludeSrcs(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 843 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 844 | java_library { |
| 845 | name: "foo", |
| 846 | srcs: [ |
| 847 | "a.java", |
| 848 | "b.java", |
| 849 | "c.java", |
| 850 | ], |
| 851 | include_srcs: true, |
| 852 | } |
| 853 | |
| 854 | java_library { |
| 855 | name: "bar", |
| 856 | srcs: [ |
| 857 | "a.java", |
| 858 | "b.java", |
| 859 | "c.java", |
| 860 | ], |
| 861 | java_resource_dirs: ["java-res"], |
| 862 | include_srcs: true, |
| 863 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 864 | `, map[string][]byte{ |
| 865 | "java-res/a/a": nil, |
| 866 | "java-res/b/b": nil, |
| 867 | "java-res2/a": nil, |
| 868 | }) |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 869 | |
| 870 | // Test a library with include_srcs: true |
| 871 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
| 872 | fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar") |
| 873 | |
| 874 | if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) { |
| 875 | t.Errorf("foo combined jars %v does not contain %q", w, g) |
| 876 | } |
| 877 | |
| 878 | if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 879 | t.Errorf("foo source jar args %q is not %q", w, g) |
| 880 | } |
| 881 | |
| 882 | // Test a library with include_srcs: true and resources |
| 883 | bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar") |
| 884 | barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar") |
| 885 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
| 886 | barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar") |
| 887 | |
| 888 | if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 889 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 890 | } |
| 891 | |
| 892 | if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 893 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 894 | } |
| 895 | |
| 896 | if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) { |
| 897 | t.Errorf("bar combined jars %v does not contain %q", w, g) |
| 898 | } |
| 899 | |
| 900 | if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 901 | t.Errorf("bar source jar args %q is not %q", w, g) |
| 902 | } |
| 903 | |
| 904 | if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w { |
| 905 | t.Errorf("bar resource jar args %q is not %q", w, g) |
| 906 | } |
| 907 | } |
| 908 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 909 | func TestGeneratedSources(t *testing.T) { |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 910 | ctx, _ := testJavaWithFS(t, ` |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 911 | java_library { |
| 912 | name: "foo", |
| 913 | srcs: [ |
| 914 | "a*.java", |
| 915 | ":gen", |
| 916 | "b*.java", |
| 917 | ], |
| 918 | } |
| 919 | |
| 920 | genrule { |
| 921 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 922 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 923 | out: ["gen.java"], |
| 924 | } |
Colin Cross | 238c1f3 | 2020-06-07 16:58:18 -0700 | [diff] [blame] | 925 | `, map[string][]byte{ |
| 926 | "a.java": nil, |
| 927 | "b.java": nil, |
| 928 | }) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 929 | |
| 930 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 931 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 932 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 933 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 934 | t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String()) |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | if len(javac.Inputs) != 3 || |
| 938 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 939 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 940 | javac.Inputs[2].String() != "b.java" { |
| 941 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 942 | } |
| 943 | } |
| 944 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 945 | func TestTurbine(t *testing.T) { |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 946 | result := android.GroupFixturePreparers( |
| 947 | prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})). |
Paul Duffin | 3d9f268 | 2021-03-13 09:47:16 +0000 | [diff] [blame] | 948 | RunTestWithBp(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 949 | java_library { |
| 950 | name: "foo", |
| 951 | srcs: ["a.java"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 952 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | java_library { |
| 956 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 957 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 958 | static_libs: ["foo"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 959 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | java_library { |
| 963 | name: "baz", |
| 964 | srcs: ["c.java"], |
| 965 | libs: ["bar"], |
| 966 | sdk_version: "14", |
| 967 | } |
| 968 | `) |
| 969 | |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 970 | fooTurbine := result.ModuleForTests("foo", "android_common").Rule("turbine") |
| 971 | barTurbine := result.ModuleForTests("bar", "android_common").Rule("turbine") |
| 972 | barJavac := result.ModuleForTests("bar", "android_common").Rule("javac") |
| 973 | barTurbineCombined := result.ModuleForTests("bar", "android_common").Description("for turbine") |
| 974 | bazJavac := result.ModuleForTests("baz", "android_common").Rule("javac") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 975 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 976 | android.AssertPathsRelativeToTopEquals(t, "foo inputs", []string{"a.java"}, fooTurbine.Inputs) |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 977 | |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 978 | fooHeaderJar := filepath.Join("out", "soong", ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 979 | barTurbineJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar") |
Colin Cross | f61766e | 2022-03-16 18:06:48 -0700 | [diff] [blame] | 980 | android.AssertStringDoesContain(t, "bar turbine classpath", barTurbine.Args["turbineFlags"], fooHeaderJar) |
Paul Duffin | 3d9f268 | 2021-03-13 09:47:16 +0000 | [diff] [blame] | 981 | android.AssertStringDoesContain(t, "bar javac classpath", barJavac.Args["classpath"], fooHeaderJar) |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 982 | android.AssertPathsRelativeToTopEquals(t, "bar turbine combineJar", []string{barTurbineJar, fooHeaderJar}, barTurbineCombined.Inputs) |
Paul Duffin | 3d9f268 | 2021-03-13 09:47:16 +0000 | [diff] [blame] | 983 | android.AssertStringDoesContain(t, "baz javac classpath", bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | func TestSharding(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 987 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 988 | java_library { |
| 989 | name: "bar", |
| 990 | srcs: ["a.java","b.java","c.java"], |
| 991 | javac_shard_size: 1 |
| 992 | } |
| 993 | `) |
| 994 | |
Colin Cross | 3d56ed5 | 2021-11-18 22:23:12 -0800 | [diff] [blame] | 995 | barHeaderJar := filepath.Join("out", "soong", ".intermediates", "bar", "android_common", "turbine", "bar.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 996 | for i := 0; i < 3; i++ { |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 997 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
Colin Cross | 3d56ed5 | 2021-11-18 22:23:12 -0800 | [diff] [blame] | 998 | if !strings.HasPrefix(barJavac.Args["classpath"], "-classpath "+barHeaderJar+":") { |
| 999 | t.Errorf("bar javac classpath %v does start with %q", barJavac.Args["classpath"], barHeaderJar) |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1004 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1005 | ctx, _ := testJava(t, ` |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1006 | java_library { |
| 1007 | name: "foo", |
| 1008 | srcs: ["a.java", ":foo-srcs"], |
| 1009 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 1010 | } |
| 1011 | |
| 1012 | filegroup { |
| 1013 | name: "foo-srcs", |
| 1014 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 1015 | } |
| 1016 | |
| 1017 | filegroup { |
| 1018 | name: "foo-excludes", |
| 1019 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 1020 | } |
| 1021 | `) |
| 1022 | |
| 1023 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1024 | |
| 1025 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 1026 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 1027 | } |
| 1028 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1029 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1030 | func TestJavaLibrary(t *testing.T) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1031 | testJavaWithFS(t, "", map[string][]byte{ |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1032 | "libcore/Android.bp": []byte(` |
| 1033 | java_library { |
| 1034 | name: "core", |
| 1035 | sdk_version: "none", |
| 1036 | system_modules: "none", |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | filegroup { |
| 1040 | name: "core-jar", |
| 1041 | srcs: [":core{.jar}"], |
| 1042 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1043 | `), |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1044 | }) |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | func TestJavaImport(t *testing.T) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1048 | testJavaWithFS(t, "", map[string][]byte{ |
Paul Duffin | aa55f74 | 2020-10-06 17:20:13 +0100 | [diff] [blame] | 1049 | "libcore/Android.bp": []byte(` |
| 1050 | java_import { |
| 1051 | name: "core", |
| 1052 | sdk_version: "none", |
| 1053 | } |
| 1054 | |
| 1055 | filegroup { |
| 1056 | name: "core-jar", |
| 1057 | srcs: [":core{.jar}"], |
| 1058 | } |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1059 | `), |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1060 | }) |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1061 | } |
| 1062 | |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1063 | var compilerFlagsTestCases = []struct { |
| 1064 | in string |
| 1065 | out bool |
| 1066 | }{ |
| 1067 | { |
| 1068 | in: "a", |
| 1069 | out: false, |
| 1070 | }, |
| 1071 | { |
| 1072 | in: "-a", |
| 1073 | out: true, |
| 1074 | }, |
| 1075 | { |
| 1076 | in: "-no-jdk", |
| 1077 | out: false, |
| 1078 | }, |
| 1079 | { |
| 1080 | in: "-no-stdlib", |
| 1081 | out: false, |
| 1082 | }, |
| 1083 | { |
| 1084 | in: "-kotlin-home", |
| 1085 | out: false, |
| 1086 | }, |
| 1087 | { |
| 1088 | in: "-kotlin-home /some/path", |
| 1089 | out: false, |
| 1090 | }, |
| 1091 | { |
| 1092 | in: "-include-runtime", |
| 1093 | out: false, |
| 1094 | }, |
| 1095 | { |
| 1096 | in: "-Xintellij-plugin-root", |
| 1097 | out: false, |
| 1098 | }, |
| 1099 | } |
| 1100 | |
| 1101 | type mockContext struct { |
| 1102 | android.ModuleContext |
| 1103 | result bool |
| 1104 | } |
| 1105 | |
| 1106 | func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) { |
| 1107 | // CheckBadCompilerFlags calls this function when the flag should be rejected |
| 1108 | ctx.result = false |
| 1109 | } |
| 1110 | |
| 1111 | func TestCompilerFlags(t *testing.T) { |
| 1112 | for _, testCase := range compilerFlagsTestCases { |
| 1113 | ctx := &mockContext{result: true} |
| 1114 | CheckKotlincFlags(ctx, []string{testCase.in}) |
| 1115 | if ctx.result != testCase.out { |
| 1116 | t.Errorf("incorrect output:") |
| 1117 | t.Errorf(" input: %#v", testCase.in) |
| 1118 | t.Errorf(" expected: %#v", testCase.out) |
| 1119 | t.Errorf(" got: %#v", ctx.result) |
| 1120 | } |
| 1121 | } |
| 1122 | } |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1123 | |
| 1124 | // TODO(jungjw): Consider making this more robust by ignoring path order. |
| 1125 | func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) { |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1126 | variables := ctx.ModuleForTests(moduleName, "android_common").VariablesForTestsRelativeToTop() |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1127 | flags := strings.Split(variables["javacFlags"], " ") |
| 1128 | got := "" |
| 1129 | for _, flag := range flags { |
| 1130 | keyEnd := strings.Index(flag, "=") |
| 1131 | if keyEnd > -1 && flag[:keyEnd] == "--patch-module" { |
| 1132 | got = flag[keyEnd+1:] |
| 1133 | break |
| 1134 | } |
| 1135 | } |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1136 | if expected != android.StringPathRelativeToTop(ctx.Config().SoongOutDir(), got) { |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1137 | t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got) |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | func TestPatchModule(t *testing.T) { |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1142 | t.Run("Java language level 8", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1143 | // Test with legacy javac -source 1.8 -target 1.8 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1144 | bp := ` |
| 1145 | java_library { |
| 1146 | name: "foo", |
| 1147 | srcs: ["a.java"], |
| 1148 | java_version: "1.8", |
| 1149 | } |
| 1150 | |
| 1151 | java_library { |
| 1152 | name: "bar", |
| 1153 | srcs: ["b.java"], |
| 1154 | sdk_version: "none", |
| 1155 | system_modules: "none", |
| 1156 | patch_module: "java.base", |
| 1157 | java_version: "1.8", |
| 1158 | } |
| 1159 | |
| 1160 | java_library { |
| 1161 | name: "baz", |
| 1162 | srcs: ["c.java"], |
| 1163 | patch_module: "java.base", |
| 1164 | java_version: "1.8", |
| 1165 | } |
| 1166 | ` |
| 1167 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1168 | |
| 1169 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1170 | checkPatchModuleFlag(t, ctx, "bar", "") |
| 1171 | checkPatchModuleFlag(t, ctx, "baz", "") |
| 1172 | }) |
| 1173 | |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1174 | t.Run("Java language level 9", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1175 | // Test with default javac -source 9 -target 9 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1176 | bp := ` |
| 1177 | java_library { |
| 1178 | name: "foo", |
| 1179 | srcs: ["a.java"], |
| 1180 | } |
| 1181 | |
| 1182 | java_library { |
| 1183 | name: "bar", |
| 1184 | srcs: ["b.java"], |
| 1185 | sdk_version: "none", |
| 1186 | system_modules: "none", |
| 1187 | patch_module: "java.base", |
| 1188 | } |
| 1189 | |
| 1190 | java_library { |
| 1191 | name: "baz", |
Jingwen Chen | 5136a6e | 2020-10-30 01:01:35 -0400 | [diff] [blame] | 1192 | srcs: [ |
| 1193 | "c.java", |
| 1194 | // Tests for b/150878007 |
| 1195 | "dir/d.java", |
| 1196 | "dir2/e.java", |
| 1197 | "dir2/f.java", |
| 1198 | "nested/dir/g.java" |
| 1199 | ], |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1200 | patch_module: "java.base", |
| 1201 | } |
| 1202 | ` |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1203 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1204 | |
| 1205 | checkPatchModuleFlag(t, ctx, "foo", "") |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1206 | expected := "java.base=.:out/soong" |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1207 | checkPatchModuleFlag(t, ctx, "bar", expected) |
Jingwen Chen | 5136a6e | 2020-10-30 01:01:35 -0400 | [diff] [blame] | 1208 | expected = "java.base=" + strings.Join([]string{ |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1209 | ".", "out/soong", "dir", "dir2", "nested", defaultModuleToPath("ext"), defaultModuleToPath("framework")}, ":") |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1210 | checkPatchModuleFlag(t, ctx, "baz", expected) |
| 1211 | }) |
| 1212 | } |
Paul Duffin | a7b9f42 | 2020-01-10 17:12:18 +0000 | [diff] [blame] | 1213 | |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1214 | func TestJavaLibraryWithSystemModules(t *testing.T) { |
| 1215 | ctx, _ := testJava(t, ` |
| 1216 | java_library { |
| 1217 | name: "lib-with-source-system-modules", |
| 1218 | srcs: [ |
| 1219 | "a.java", |
| 1220 | ], |
| 1221 | sdk_version: "none", |
| 1222 | system_modules: "source-system-modules", |
| 1223 | } |
| 1224 | |
| 1225 | java_library { |
| 1226 | name: "source-jar", |
| 1227 | srcs: [ |
| 1228 | "a.java", |
| 1229 | ], |
| 1230 | } |
| 1231 | |
| 1232 | java_system_modules { |
| 1233 | name: "source-system-modules", |
| 1234 | libs: ["source-jar"], |
| 1235 | } |
| 1236 | |
| 1237 | java_library { |
| 1238 | name: "lib-with-prebuilt-system-modules", |
| 1239 | srcs: [ |
| 1240 | "a.java", |
| 1241 | ], |
| 1242 | sdk_version: "none", |
| 1243 | system_modules: "prebuilt-system-modules", |
| 1244 | } |
| 1245 | |
| 1246 | java_import { |
| 1247 | name: "prebuilt-jar", |
| 1248 | jars: ["a.jar"], |
| 1249 | } |
| 1250 | |
| 1251 | java_system_modules_import { |
| 1252 | name: "prebuilt-system-modules", |
| 1253 | libs: ["prebuilt-jar"], |
| 1254 | } |
| 1255 | `) |
| 1256 | |
| 1257 | checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar") |
| 1258 | |
| 1259 | checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar") |
| 1260 | } |
| 1261 | |
| 1262 | func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) { |
| 1263 | javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac") |
| 1264 | bootClasspath := javacRule.Args["bootClasspath"] |
| 1265 | if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) { |
| 1266 | t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath) |
| 1267 | } |
| 1268 | } |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 1269 | |
| 1270 | func TestAidlExportIncludeDirsFromImports(t *testing.T) { |
| 1271 | ctx, _ := testJava(t, ` |
| 1272 | java_library { |
| 1273 | name: "foo", |
| 1274 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1275 | libs: ["bar"], |
| 1276 | } |
| 1277 | |
| 1278 | java_import { |
| 1279 | name: "bar", |
| 1280 | jars: ["a.jar"], |
| 1281 | aidl: { |
| 1282 | export_include_dirs: ["aidl/bar"], |
| 1283 | }, |
| 1284 | } |
| 1285 | `) |
| 1286 | |
| 1287 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1288 | expectedAidlFlag := "-Iaidl/bar" |
| 1289 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1290 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1291 | } |
| 1292 | } |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1293 | |
Vinh Tran | 16fe8e1 | 2022-08-16 16:45:44 -0400 | [diff] [blame] | 1294 | func TestAidlIncludeDirFromConvertedFileGroupWithPathPropInMixedBuilds(t *testing.T) { |
Liz Kammer | 748209c | 2022-10-24 10:43:27 -0400 | [diff] [blame] | 1295 | // TODO(b/247782695), TODO(b/242847534) Fix mixed builds for filegroups |
| 1296 | t.Skip("Re-enable once filegroups are corrected for mixed builds") |
Vinh Tran | 16fe8e1 | 2022-08-16 16:45:44 -0400 | [diff] [blame] | 1297 | bp := ` |
| 1298 | filegroup { |
| 1299 | name: "foo_aidl", |
| 1300 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1301 | path: "aidl/foo", |
| 1302 | bazel_module: { label: "//:foo_aidl" }, |
| 1303 | } |
| 1304 | java_library { |
| 1305 | name: "foo", |
| 1306 | srcs: [":foo_aidl"], |
| 1307 | } |
| 1308 | ` |
| 1309 | |
| 1310 | outBaseDir := "out/bazel/output" |
| 1311 | result := android.GroupFixturePreparers( |
| 1312 | prepareForJavaTest, |
| 1313 | android.PrepareForTestWithFilegroup, |
| 1314 | android.FixtureModifyConfig(func(config android.Config) { |
| 1315 | config.BazelContext = android.MockBazelContext{ |
| 1316 | OutputBaseDir: outBaseDir, |
| 1317 | LabelToOutputFiles: map[string][]string{ |
| 1318 | "//:foo_aidl": []string{"aidl/foo/IFoo.aidl"}, |
| 1319 | }, |
| 1320 | } |
| 1321 | }), |
| 1322 | ).RunTestWithBp(t, bp) |
| 1323 | |
| 1324 | aidlCommand := result.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1325 | expectedAidlFlag := "-I" + outBaseDir + "/execroot/__main__/aidl/foo" |
| 1326 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1327 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1328 | } |
| 1329 | } |
| 1330 | |
Jooyung Han | e197d8b | 2021-01-05 10:33:16 +0900 | [diff] [blame] | 1331 | func TestAidlFlagsArePassedToTheAidlCompiler(t *testing.T) { |
| 1332 | ctx, _ := testJava(t, ` |
| 1333 | java_library { |
| 1334 | name: "foo", |
| 1335 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1336 | aidl: { flags: ["-Werror"], }, |
| 1337 | } |
| 1338 | `) |
| 1339 | |
| 1340 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1341 | expectedAidlFlag := "-Werror" |
| 1342 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1343 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1344 | } |
| 1345 | } |
| 1346 | |
Jooyung Han | 07f70c0 | 2021-11-06 07:08:45 +0900 | [diff] [blame] | 1347 | func TestAidlFlagsWithMinSdkVersion(t *testing.T) { |
| 1348 | fixture := android.GroupFixturePreparers( |
| 1349 | prepareForJavaTest, FixtureWithPrebuiltApis(map[string][]string{"14": {"foo"}})) |
| 1350 | |
| 1351 | for _, tc := range []struct { |
| 1352 | name string |
| 1353 | sdkVersion string |
| 1354 | expected string |
| 1355 | }{ |
| 1356 | {"default is current", "", "current"}, |
| 1357 | {"use sdk_version", `sdk_version: "14"`, "14"}, |
| 1358 | {"system_current", `sdk_version: "system_current"`, "current"}, |
| 1359 | } { |
| 1360 | t.Run(tc.name, func(t *testing.T) { |
| 1361 | ctx := fixture.RunTestWithBp(t, ` |
| 1362 | java_library { |
| 1363 | name: "foo", |
| 1364 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1365 | `+tc.sdkVersion+` |
| 1366 | } |
| 1367 | `) |
| 1368 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1369 | expectedAidlFlag := "--min_sdk_version=" + tc.expected |
| 1370 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1371 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1372 | } |
| 1373 | }) |
| 1374 | } |
| 1375 | } |
| 1376 | |
Spandan Das | 757b666 | 2022-11-17 04:29:59 +0000 | [diff] [blame] | 1377 | func TestAidlFlagsMinSdkVersionDroidstubs(t *testing.T) { |
| 1378 | bpTemplate := ` |
| 1379 | droidstubs { |
| 1380 | name: "foo-stubs", |
| 1381 | srcs: ["foo.aidl"], |
| 1382 | %s |
| 1383 | system_modules: "none", |
| 1384 | } |
| 1385 | ` |
| 1386 | testCases := []struct { |
| 1387 | desc string |
| 1388 | sdkVersionBp string |
| 1389 | minSdkVersionExpected string |
| 1390 | }{ |
| 1391 | { |
| 1392 | desc: "sdk_version not set, module compiles against private platform APIs", |
| 1393 | sdkVersionBp: ``, |
| 1394 | minSdkVersionExpected: "10000", |
| 1395 | }, |
| 1396 | { |
| 1397 | desc: "sdk_version set to none, module does not build against an SDK", |
| 1398 | sdkVersionBp: `sdk_version: "none",`, |
| 1399 | minSdkVersionExpected: "10000", |
| 1400 | }, |
| 1401 | } |
| 1402 | for _, tc := range testCases { |
| 1403 | ctx := prepareForJavaTest.RunTestWithBp(t, fmt.Sprintf(bpTemplate, tc.sdkVersionBp)) |
| 1404 | aidlCmd := ctx.ModuleForTests("foo-stubs", "android_common").Rule("aidl").RuleParams.Command |
| 1405 | expected := "--min_sdk_version=" + tc.minSdkVersionExpected |
| 1406 | android.AssertStringDoesContain(t, "aidl command conatins incorrect min_sdk_version for testCse: "+tc.desc, aidlCmd, expected) |
| 1407 | } |
| 1408 | } |
| 1409 | |
Thiébaud Weksteen | de8417c | 2022-02-10 15:41:46 +1100 | [diff] [blame] | 1410 | func TestAidlEnforcePermissions(t *testing.T) { |
| 1411 | ctx, _ := testJava(t, ` |
| 1412 | java_library { |
| 1413 | name: "foo", |
| 1414 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1415 | aidl: { enforce_permissions: true }, |
| 1416 | } |
| 1417 | `) |
| 1418 | |
| 1419 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1420 | expectedAidlFlag := "-Wmissing-permission-annotation -Werror" |
| 1421 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1422 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | func TestAidlEnforcePermissionsException(t *testing.T) { |
| 1427 | ctx, _ := testJava(t, ` |
| 1428 | java_library { |
| 1429 | name: "foo", |
| 1430 | srcs: ["aidl/foo/IFoo.aidl", "aidl/foo/IFoo2.aidl"], |
| 1431 | aidl: { enforce_permissions: true, enforce_permissions_exceptions: ["aidl/foo/IFoo2.aidl"] }, |
| 1432 | } |
| 1433 | `) |
| 1434 | |
| 1435 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1436 | expectedAidlFlag := "$$FLAGS -Wmissing-permission-annotation -Werror aidl/foo/IFoo.aidl" |
| 1437 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1438 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1439 | } |
| 1440 | expectedAidlFlag = "$$FLAGS aidl/foo/IFoo2.aidl" |
| 1441 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1442 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1443 | } |
| 1444 | } |
| 1445 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1446 | func TestDataNativeBinaries(t *testing.T) { |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 1447 | ctx := android.GroupFixturePreparers( |
| 1448 | prepareForJavaTest, |
| 1449 | android.PrepareForTestWithAllowMissingDependencies).RunTestWithBp(t, ` |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1450 | java_test_host { |
| 1451 | name: "foo", |
| 1452 | srcs: ["a.java"], |
| 1453 | data_native_bins: ["bin"] |
| 1454 | } |
| 1455 | |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 1456 | cc_binary_host { |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1457 | name: "bin", |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 1458 | srcs: ["bin.cpp"], |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1459 | } |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 1460 | `).TestContext |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1461 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 1462 | buildOS := ctx.Config().BuildOS.String() |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1463 | |
| 1464 | test := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost) |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 1465 | entries := android.AndroidMkEntriesForTest(t, ctx, test)[0] |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 1466 | expected := []string{"out/soong/.intermediates/bin/" + buildOS + "_x86_64/bin:bin"} |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1467 | actual := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"] |
Paul Duffin | 414ea5e | 2021-03-22 17:31:52 +0000 | [diff] [blame] | 1468 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_COMPATIBILITY_SUPPORT_FILES", ctx.Config(), expected, actual) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 1469 | } |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1470 | |
| 1471 | func TestDefaultInstallable(t *testing.T) { |
| 1472 | ctx, _ := testJava(t, ` |
| 1473 | java_test_host { |
| 1474 | name: "foo" |
| 1475 | } |
| 1476 | `) |
| 1477 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 1478 | buildOS := ctx.Config().BuildOS.String() |
Yuexi Ma | 627263f | 2021-03-04 13:47:56 -0800 | [diff] [blame] | 1479 | module := ctx.ModuleForTests("foo", buildOS+"_common").Module().(*TestHost) |
| 1480 | assertDeepEquals(t, "Default installable value should be true.", proptools.BoolPtr(true), |
| 1481 | module.properties.Installable) |
| 1482 | } |
Cole Faust | 75fffb1 | 2021-06-13 15:23:16 -0700 | [diff] [blame] | 1483 | |
| 1484 | func TestErrorproneEnabled(t *testing.T) { |
| 1485 | ctx, _ := testJava(t, ` |
| 1486 | java_library { |
| 1487 | name: "foo", |
| 1488 | srcs: ["a.java"], |
| 1489 | errorprone: { |
| 1490 | enabled: true, |
| 1491 | }, |
| 1492 | } |
| 1493 | `) |
| 1494 | |
| 1495 | javac := ctx.ModuleForTests("foo", "android_common").Description("javac") |
| 1496 | |
| 1497 | // Test that the errorprone plugins are passed to javac |
| 1498 | expectedSubstring := "-Xplugin:ErrorProne" |
| 1499 | if !strings.Contains(javac.Args["javacFlags"], expectedSubstring) { |
Cole Faust | 2b1536e | 2021-06-18 12:25:54 -0700 | [diff] [blame] | 1500 | t.Errorf("expected javacFlags to contain %q, got %q", expectedSubstring, javac.Args["javacFlags"]) |
Cole Faust | 75fffb1 | 2021-06-13 15:23:16 -0700 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | // Modules with errorprone { enabled: true } will include errorprone checks |
| 1504 | // in the main javac build rule. Only when RUN_ERROR_PRONE is true will |
| 1505 | // the explicit errorprone build rule be created. |
| 1506 | errorprone := ctx.ModuleForTests("foo", "android_common").MaybeDescription("errorprone") |
| 1507 | if errorprone.RuleParams.Description != "" { |
| 1508 | t.Errorf("expected errorprone build rule to not exist, but it did") |
| 1509 | } |
| 1510 | } |
Cole Faust | 2b1536e | 2021-06-18 12:25:54 -0700 | [diff] [blame] | 1511 | |
| 1512 | func TestErrorproneDisabled(t *testing.T) { |
| 1513 | bp := ` |
| 1514 | java_library { |
| 1515 | name: "foo", |
| 1516 | srcs: ["a.java"], |
| 1517 | errorprone: { |
| 1518 | enabled: false, |
| 1519 | }, |
| 1520 | } |
| 1521 | ` |
| 1522 | ctx := android.GroupFixturePreparers( |
| 1523 | PrepareForTestWithJavaDefaultModules, |
| 1524 | android.FixtureMergeEnv(map[string]string{ |
| 1525 | "RUN_ERROR_PRONE": "true", |
| 1526 | }), |
| 1527 | ).RunTestWithBp(t, bp) |
| 1528 | |
| 1529 | javac := ctx.ModuleForTests("foo", "android_common").Description("javac") |
| 1530 | |
| 1531 | // Test that the errorprone plugins are not passed to javac, like they would |
| 1532 | // be if enabled was true. |
| 1533 | expectedSubstring := "-Xplugin:ErrorProne" |
| 1534 | if strings.Contains(javac.Args["javacFlags"], expectedSubstring) { |
| 1535 | t.Errorf("expected javacFlags to not contain %q, got %q", expectedSubstring, javac.Args["javacFlags"]) |
| 1536 | } |
| 1537 | |
| 1538 | // Check that no errorprone build rule is created, like there would be |
| 1539 | // if enabled was unset and RUN_ERROR_PRONE was true. |
| 1540 | errorprone := ctx.ModuleForTests("foo", "android_common").MaybeDescription("errorprone") |
| 1541 | if errorprone.RuleParams.Description != "" { |
| 1542 | t.Errorf("expected errorprone build rule to not exist, but it did") |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | func TestErrorproneEnabledOnlyByEnvironmentVariable(t *testing.T) { |
| 1547 | bp := ` |
| 1548 | java_library { |
| 1549 | name: "foo", |
| 1550 | srcs: ["a.java"], |
| 1551 | } |
| 1552 | ` |
| 1553 | ctx := android.GroupFixturePreparers( |
| 1554 | PrepareForTestWithJavaDefaultModules, |
| 1555 | android.FixtureMergeEnv(map[string]string{ |
| 1556 | "RUN_ERROR_PRONE": "true", |
| 1557 | }), |
| 1558 | ).RunTestWithBp(t, bp) |
| 1559 | |
| 1560 | javac := ctx.ModuleForTests("foo", "android_common").Description("javac") |
| 1561 | errorprone := ctx.ModuleForTests("foo", "android_common").Description("errorprone") |
| 1562 | |
| 1563 | // Check that the errorprone plugins are not passed to javac, because they |
| 1564 | // will instead be passed to the separate errorprone compilation |
| 1565 | expectedSubstring := "-Xplugin:ErrorProne" |
| 1566 | if strings.Contains(javac.Args["javacFlags"], expectedSubstring) { |
| 1567 | t.Errorf("expected javacFlags to not contain %q, got %q", expectedSubstring, javac.Args["javacFlags"]) |
| 1568 | } |
| 1569 | |
| 1570 | // Check that the errorprone plugin is enabled |
| 1571 | if !strings.Contains(errorprone.Args["javacFlags"], expectedSubstring) { |
| 1572 | t.Errorf("expected errorprone to contain %q, got %q", expectedSubstring, javac.Args["javacFlags"]) |
| 1573 | } |
| 1574 | } |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1575 | |
| 1576 | func TestDataDeviceBinsBuildsDeviceBinary(t *testing.T) { |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1577 | testCases := []struct { |
| 1578 | dataDeviceBinType string |
| 1579 | depCompileMultilib string |
| 1580 | variants []string |
| 1581 | expectedError string |
| 1582 | }{ |
| 1583 | { |
| 1584 | dataDeviceBinType: "first", |
| 1585 | depCompileMultilib: "first", |
| 1586 | variants: []string{"android_arm64_armv8-a"}, |
| 1587 | }, |
| 1588 | { |
| 1589 | dataDeviceBinType: "first", |
| 1590 | depCompileMultilib: "both", |
| 1591 | variants: []string{"android_arm64_armv8-a"}, |
| 1592 | }, |
| 1593 | { |
| 1594 | // this is true because our testing framework is set up with |
| 1595 | // Targets ~ [<64bit target>, <32bit target>], where 64bit is "first" |
| 1596 | dataDeviceBinType: "first", |
| 1597 | depCompileMultilib: "32", |
| 1598 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1599 | }, |
| 1600 | { |
| 1601 | dataDeviceBinType: "first", |
| 1602 | depCompileMultilib: "64", |
| 1603 | variants: []string{"android_arm64_armv8-a"}, |
| 1604 | }, |
| 1605 | { |
| 1606 | dataDeviceBinType: "both", |
| 1607 | depCompileMultilib: "both", |
| 1608 | variants: []string{ |
| 1609 | "android_arm_armv7-a-neon", |
| 1610 | "android_arm64_armv8-a", |
| 1611 | }, |
| 1612 | }, |
| 1613 | { |
| 1614 | dataDeviceBinType: "both", |
| 1615 | depCompileMultilib: "32", |
| 1616 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1617 | }, |
| 1618 | { |
| 1619 | dataDeviceBinType: "both", |
| 1620 | depCompileMultilib: "64", |
| 1621 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1622 | }, |
| 1623 | { |
| 1624 | dataDeviceBinType: "both", |
| 1625 | depCompileMultilib: "first", |
| 1626 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1627 | }, |
| 1628 | { |
| 1629 | dataDeviceBinType: "32", |
| 1630 | depCompileMultilib: "32", |
| 1631 | variants: []string{"android_arm_armv7-a-neon"}, |
| 1632 | }, |
| 1633 | { |
| 1634 | dataDeviceBinType: "32", |
| 1635 | depCompileMultilib: "first", |
| 1636 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1637 | }, |
| 1638 | { |
| 1639 | dataDeviceBinType: "32", |
| 1640 | depCompileMultilib: "both", |
| 1641 | variants: []string{"android_arm_armv7-a-neon"}, |
| 1642 | }, |
| 1643 | { |
| 1644 | dataDeviceBinType: "32", |
| 1645 | depCompileMultilib: "64", |
| 1646 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1647 | }, |
| 1648 | { |
| 1649 | dataDeviceBinType: "64", |
| 1650 | depCompileMultilib: "64", |
| 1651 | variants: []string{"android_arm64_armv8-a"}, |
| 1652 | }, |
| 1653 | { |
| 1654 | dataDeviceBinType: "64", |
| 1655 | depCompileMultilib: "both", |
| 1656 | variants: []string{"android_arm64_armv8-a"}, |
| 1657 | }, |
| 1658 | { |
| 1659 | dataDeviceBinType: "64", |
| 1660 | depCompileMultilib: "first", |
| 1661 | variants: []string{"android_arm64_armv8-a"}, |
| 1662 | }, |
| 1663 | { |
| 1664 | dataDeviceBinType: "64", |
| 1665 | depCompileMultilib: "32", |
| 1666 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1667 | }, |
| 1668 | { |
| 1669 | dataDeviceBinType: "prefer32", |
| 1670 | depCompileMultilib: "32", |
| 1671 | variants: []string{"android_arm_armv7-a-neon"}, |
| 1672 | }, |
| 1673 | { |
| 1674 | dataDeviceBinType: "prefer32", |
| 1675 | depCompileMultilib: "both", |
| 1676 | variants: []string{"android_arm_armv7-a-neon"}, |
| 1677 | }, |
| 1678 | { |
| 1679 | dataDeviceBinType: "prefer32", |
| 1680 | depCompileMultilib: "first", |
| 1681 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1682 | }, |
| 1683 | { |
| 1684 | dataDeviceBinType: "prefer32", |
| 1685 | depCompileMultilib: "64", |
| 1686 | expectedError: `Android.bp:2:3: dependency "bar" of "foo" missing variant`, |
| 1687 | }, |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1688 | } |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1689 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1690 | bpTemplate := ` |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1691 | java_test_host { |
| 1692 | name: "foo", |
| 1693 | srcs: ["test.java"], |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1694 | data_device_bins_%s: ["bar"], |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | cc_binary { |
| 1698 | name: "bar", |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1699 | compile_multilib: "%s", |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1700 | } |
| 1701 | ` |
| 1702 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1703 | for _, tc := range testCases { |
| 1704 | bp := fmt.Sprintf(bpTemplate, tc.dataDeviceBinType, tc.depCompileMultilib) |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1705 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1706 | errorHandler := android.FixtureExpectsNoErrors |
| 1707 | if tc.expectedError != "" { |
| 1708 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(tc.expectedError) |
| 1709 | } |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1710 | |
Sam Delmerico | cc271e2 | 2022-06-01 15:45:02 +0000 | [diff] [blame] | 1711 | testName := fmt.Sprintf(`data_device_bins_%s with compile_multilib:"%s"`, tc.dataDeviceBinType, tc.depCompileMultilib) |
| 1712 | t.Run(testName, func(t *testing.T) { |
| 1713 | ctx := android.GroupFixturePreparers(PrepareForIntegrationTestWithJava). |
| 1714 | ExtendWithErrorHandler(errorHandler). |
| 1715 | RunTestWithBp(t, bp) |
| 1716 | if tc.expectedError != "" { |
| 1717 | return |
| 1718 | } |
| 1719 | |
| 1720 | buildOS := ctx.Config.BuildOS.String() |
| 1721 | fooVariant := ctx.ModuleForTests("foo", buildOS+"_common") |
| 1722 | fooMod := fooVariant.Module().(*TestHost) |
| 1723 | entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, fooMod)[0] |
| 1724 | |
| 1725 | expectedAutogenConfig := `<option name="push-file" key="bar" value="/data/local/tests/unrestricted/foo/bar" />` |
| 1726 | autogen := fooVariant.Rule("autogen") |
| 1727 | if !strings.Contains(autogen.Args["extraConfigs"], expectedAutogenConfig) { |
| 1728 | t.Errorf("foo extraConfigs %v does not contain %q", autogen.Args["extraConfigs"], expectedAutogenConfig) |
| 1729 | } |
| 1730 | |
| 1731 | expectedData := []string{} |
| 1732 | for _, variant := range tc.variants { |
| 1733 | barVariant := ctx.ModuleForTests("bar", variant) |
| 1734 | relocated := barVariant.Output("bar") |
| 1735 | expectedInput := fmt.Sprintf("out/soong/.intermediates/bar/%s/unstripped/bar", variant) |
| 1736 | android.AssertPathRelativeToTopEquals(t, "relocation input", expectedInput, relocated.Input) |
| 1737 | |
| 1738 | expectedData = append(expectedData, fmt.Sprintf("out/soong/.intermediates/bar/%s/bar:bar", variant)) |
| 1739 | } |
| 1740 | |
| 1741 | actualData := entries.EntryMap["LOCAL_COMPATIBILITY_SUPPORT_FILES"] |
| 1742 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_TEST_DATA", ctx.Config, expectedData, actualData) |
| 1743 | }) |
Sam Delmerico | b3342ce | 2022-01-20 21:10:28 +0000 | [diff] [blame] | 1744 | } |
| 1745 | } |
Sam Delmerico | 277795c | 2022-02-25 17:04:37 +0000 | [diff] [blame] | 1746 | |
| 1747 | func TestImportMixedBuild(t *testing.T) { |
| 1748 | bp := ` |
| 1749 | java_import { |
| 1750 | name: "baz", |
| 1751 | jars: [ |
| 1752 | "test1.jar", |
| 1753 | "test2.jar", |
| 1754 | ], |
| 1755 | bazel_module: { label: "//foo/bar:baz" }, |
| 1756 | } |
| 1757 | ` |
| 1758 | |
| 1759 | ctx := android.GroupFixturePreparers( |
| 1760 | prepareForJavaTest, |
| 1761 | android.FixtureModifyConfig(func(config android.Config) { |
| 1762 | config.BazelContext = android.MockBazelContext{ |
| 1763 | OutputBaseDir: "outputbase", |
| 1764 | LabelToOutputFiles: map[string][]string{ |
| 1765 | "//foo/bar:baz": []string{"test1.jar", "test2.jar"}, |
| 1766 | }, |
| 1767 | } |
| 1768 | }), |
| 1769 | ).RunTestWithBp(t, bp) |
| 1770 | |
| 1771 | bazMod := ctx.ModuleForTests("baz", "android_common").Module() |
| 1772 | producer := bazMod.(android.OutputFileProducer) |
| 1773 | expectedOutputFiles := []string{".intermediates/baz/android_common/bazelCombined/baz.jar"} |
| 1774 | |
| 1775 | outputFiles, err := producer.OutputFiles("") |
| 1776 | if err != nil { |
| 1777 | t.Errorf("Unexpected error getting java_import outputfiles %s", err) |
| 1778 | } |
| 1779 | actualOutputFiles := android.NormalizePathsForTesting(outputFiles) |
| 1780 | android.AssertDeepEquals(t, "Output files are produced", expectedOutputFiles, actualOutputFiles) |
| 1781 | |
| 1782 | javaInfoProvider := ctx.ModuleProvider(bazMod, JavaInfoProvider) |
| 1783 | javaInfo, ok := javaInfoProvider.(JavaInfo) |
| 1784 | if !ok { |
| 1785 | t.Error("could not get JavaInfo from java_import module") |
| 1786 | } |
| 1787 | android.AssertDeepEquals(t, "Header JARs are produced", expectedOutputFiles, android.NormalizePathsForTesting(javaInfo.HeaderJars)) |
| 1788 | android.AssertDeepEquals(t, "Implementation/Resources JARs are produced", expectedOutputFiles, android.NormalizePathsForTesting(javaInfo.ImplementationAndResourcesJars)) |
| 1789 | android.AssertDeepEquals(t, "Implementation JARs are produced", expectedOutputFiles, android.NormalizePathsForTesting(javaInfo.ImplementationJars)) |
| 1790 | } |
Sam Delmerico | 2351eac | 2022-05-24 17:10:02 +0000 | [diff] [blame] | 1791 | |
| 1792 | func TestGenAidlIncludeFlagsForMixedBuilds(t *testing.T) { |
| 1793 | bazelOutputBaseDir := filepath.Join("out", "bazel") |
| 1794 | result := android.GroupFixturePreparers( |
| 1795 | PrepareForIntegrationTestWithJava, |
| 1796 | android.FixtureModifyConfig(func(config android.Config) { |
| 1797 | config.BazelContext = android.MockBazelContext{ |
| 1798 | OutputBaseDir: bazelOutputBaseDir, |
| 1799 | } |
| 1800 | }), |
| 1801 | ).RunTest(t) |
| 1802 | |
| 1803 | ctx := &android.TestPathContext{TestResult: result} |
| 1804 | |
| 1805 | srcDirectory := filepath.Join("frameworks", "base") |
| 1806 | srcDirectoryAlreadyIncluded := filepath.Join("frameworks", "base", "core", "java") |
| 1807 | bazelSrcDirectory := android.PathForBazelOut(ctx, srcDirectory) |
| 1808 | bazelSrcDirectoryAlreadyIncluded := android.PathForBazelOut(ctx, srcDirectoryAlreadyIncluded) |
| 1809 | srcs := android.Paths{ |
| 1810 | android.PathForTestingWithRel(bazelSrcDirectory.String(), "bazelAidl.aidl"), |
| 1811 | android.PathForTestingWithRel(bazelSrcDirectory.String(), "bazelAidl2.aidl"), |
| 1812 | android.PathForTestingWithRel(bazelSrcDirectoryAlreadyIncluded.String(), "bazelAidlExclude.aidl"), |
| 1813 | android.PathForTestingWithRel(bazelSrcDirectoryAlreadyIncluded.String(), "bazelAidl2Exclude.aidl"), |
| 1814 | } |
| 1815 | dirsAlreadyIncluded := android.Paths{ |
| 1816 | android.PathForTesting(srcDirectoryAlreadyIncluded), |
| 1817 | } |
| 1818 | |
| 1819 | expectedFlags := " -Iout/bazel/execroot/__main__/frameworks/base" |
| 1820 | flags := genAidlIncludeFlags(ctx, srcs, dirsAlreadyIncluded) |
| 1821 | if flags != expectedFlags { |
| 1822 | t.Errorf("expected flags to be %q; was %q", expectedFlags, flags) |
| 1823 | } |
| 1824 | } |
Zi Wang | ca65b40 | 2022-10-10 13:45:06 -0700 | [diff] [blame] | 1825 | |
| 1826 | func TestDeviceBinaryWrapperGeneration(t *testing.T) { |
| 1827 | // Scenario 1: java_binary has main_class property in its bp |
| 1828 | ctx, _ := testJava(t, ` |
| 1829 | java_binary { |
| 1830 | name: "foo", |
| 1831 | srcs: ["foo.java"], |
| 1832 | main_class: "foo.bar.jb", |
| 1833 | } |
| 1834 | `) |
| 1835 | wrapperPath := fmt.Sprint(ctx.ModuleForTests("foo", "android_arm64_armv8-a").AllOutputs()) |
| 1836 | if !strings.Contains(wrapperPath, "foo.sh") { |
| 1837 | t.Errorf("wrapper file foo.sh is not generated") |
| 1838 | } |
| 1839 | |
| 1840 | // Scenario 2: java_binary has neither wrapper nor main_class, its build |
| 1841 | // is expected to be failed. |
| 1842 | testJavaError(t, "main_class property is required for device binary if no default wrapper is assigned", ` |
| 1843 | java_binary { |
| 1844 | name: "foo", |
| 1845 | srcs: ["foo.java"], |
| 1846 | }`) |
| 1847 | } |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1848 | |
Jihoon Kang | 3198f3c | 2023-01-26 08:08:52 +0000 | [diff] [blame] | 1849 | func TestJavaApiContributionEmptyApiFile(t *testing.T) { |
| 1850 | testJavaError(t, |
| 1851 | "Error: foo has an empty api file.", |
| 1852 | `java_api_contribution { |
| 1853 | name: "foo", |
| 1854 | } |
| 1855 | java_api_library { |
| 1856 | name: "bar", |
| 1857 | api_surface: "public", |
| 1858 | api_contributions: ["foo"], |
| 1859 | } |
| 1860 | `) |
| 1861 | } |
| 1862 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1863 | func TestJavaApiLibraryAndProviderLink(t *testing.T) { |
| 1864 | provider_bp_a := ` |
| 1865 | java_api_contribution { |
| 1866 | name: "foo1", |
| 1867 | api_file: "foo1.txt", |
| 1868 | } |
| 1869 | ` |
| 1870 | provider_bp_b := `java_api_contribution { |
| 1871 | name: "foo2", |
| 1872 | api_file: "foo2.txt", |
| 1873 | } |
| 1874 | ` |
| 1875 | ctx, _ := testJavaWithFS(t, ` |
| 1876 | java_api_library { |
| 1877 | name: "bar1", |
| 1878 | api_surface: "public", |
Jihoon Kang | 60d4a09 | 2022-11-17 23:47:43 +0000 | [diff] [blame] | 1879 | api_contributions: ["foo1"], |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | java_api_library { |
| 1883 | name: "bar2", |
| 1884 | api_surface: "system", |
Jihoon Kang | 60d4a09 | 2022-11-17 23:47:43 +0000 | [diff] [blame] | 1885 | api_contributions: ["foo1", "foo2"], |
Spandan Das | c082eb8 | 2022-12-01 21:43:06 +0000 | [diff] [blame] | 1886 | api_files: ["api1/current.txt", "api2/current.txt"] |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1887 | } |
| 1888 | `, |
| 1889 | map[string][]byte{ |
| 1890 | "a/Android.bp": []byte(provider_bp_a), |
| 1891 | "b/Android.bp": []byte(provider_bp_b), |
| 1892 | }) |
| 1893 | |
| 1894 | testcases := []struct { |
| 1895 | moduleName string |
| 1896 | sourceTextFileDirs []string |
| 1897 | }{ |
| 1898 | { |
| 1899 | moduleName: "bar1", |
| 1900 | sourceTextFileDirs: []string{"a/foo1.txt"}, |
| 1901 | }, |
| 1902 | { |
| 1903 | moduleName: "bar2", |
Spandan Das | c082eb8 | 2022-12-01 21:43:06 +0000 | [diff] [blame] | 1904 | sourceTextFileDirs: []string{"a/foo1.txt", "b/foo2.txt", "api1/current.txt", "api2/current.txt"}, |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 1905 | }, |
| 1906 | } |
| 1907 | for _, c := range testcases { |
| 1908 | m := ctx.ModuleForTests(c.moduleName, "android_common") |
| 1909 | manifest := m.Output("metalava.sbox.textproto") |
| 1910 | sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest) |
| 1911 | manifestCommand := sboxProto.Commands[0].GetCommand() |
| 1912 | sourceFilesFlag := "--source-files " + strings.Join(c.sourceTextFileDirs, " ") |
| 1913 | android.AssertStringDoesContain(t, "source text files not present", manifestCommand, sourceFilesFlag) |
| 1914 | } |
| 1915 | } |
| 1916 | |
Jihoon Kang | 1c51f50 | 2023-01-09 23:42:40 +0000 | [diff] [blame] | 1917 | func TestJavaApiLibraryAndDefaultsLink(t *testing.T) { |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1918 | provider_bp_a := ` |
| 1919 | java_api_contribution { |
Jihoon Kang | 1c51f50 | 2023-01-09 23:42:40 +0000 | [diff] [blame] | 1920 | name: "foo1", |
| 1921 | api_file: "foo1.txt", |
| 1922 | } |
| 1923 | ` |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1924 | provider_bp_b := ` |
| 1925 | java_api_contribution { |
Jihoon Kang | 1c51f50 | 2023-01-09 23:42:40 +0000 | [diff] [blame] | 1926 | name: "foo2", |
| 1927 | api_file: "foo2.txt", |
| 1928 | } |
| 1929 | ` |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1930 | provider_bp_c := ` |
| 1931 | java_api_contribution { |
Jihoon Kang | 1c51f50 | 2023-01-09 23:42:40 +0000 | [diff] [blame] | 1932 | name: "foo3", |
| 1933 | api_file: "foo3.txt", |
| 1934 | } |
| 1935 | ` |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 1936 | provider_bp_d := ` |
| 1937 | java_api_contribution { |
Jihoon Kang | 1c51f50 | 2023-01-09 23:42:40 +0000 | [diff] [blame] | 1938 | name: "foo4", |
| 1939 | api_file: "foo4.txt", |
| 1940 | } |
| 1941 | ` |
| 1942 | ctx, _ := testJavaWithFS(t, ` |
| 1943 | java_defaults { |
| 1944 | name: "baz1", |
| 1945 | api_surface: "public", |
| 1946 | api_contributions: ["foo1", "foo2"], |
| 1947 | } |
| 1948 | |
| 1949 | java_defaults { |
| 1950 | name: "baz2", |
| 1951 | api_surface: "system", |
| 1952 | api_contributions: ["foo3"], |
| 1953 | } |
| 1954 | |
| 1955 | java_api_library { |
| 1956 | name: "bar1", |
| 1957 | api_surface: "public", |
| 1958 | api_contributions: ["foo1"], |
| 1959 | } |
| 1960 | |
| 1961 | java_api_library { |
| 1962 | name: "bar2", |
| 1963 | api_surface: "public", |
| 1964 | defaults:["baz1"], |
| 1965 | } |
| 1966 | |
| 1967 | java_api_library { |
| 1968 | name: "bar3", |
| 1969 | api_surface: "system", |
| 1970 | defaults:["baz1", "baz2"], |
| 1971 | api_contributions: ["foo4"], |
| 1972 | api_files: ["api1/current.txt", "api2/current.txt"] |
| 1973 | } |
| 1974 | `, |
| 1975 | map[string][]byte{ |
| 1976 | "a/Android.bp": []byte(provider_bp_a), |
| 1977 | "b/Android.bp": []byte(provider_bp_b), |
| 1978 | "c/Android.bp": []byte(provider_bp_c), |
| 1979 | "d/Android.bp": []byte(provider_bp_d), |
| 1980 | }) |
| 1981 | |
| 1982 | testcases := []struct { |
| 1983 | moduleName string |
| 1984 | sourceTextFileDirs []string |
| 1985 | }{ |
| 1986 | { |
| 1987 | moduleName: "bar1", |
| 1988 | sourceTextFileDirs: []string{"a/foo1.txt"}, |
| 1989 | }, |
| 1990 | { |
| 1991 | moduleName: "bar2", |
| 1992 | sourceTextFileDirs: []string{"a/foo1.txt", "b/foo2.txt"}, |
| 1993 | }, |
| 1994 | { |
| 1995 | moduleName: "bar3", |
| 1996 | sourceTextFileDirs: []string{"c/foo3.txt", "a/foo1.txt", "b/foo2.txt", "d/foo4.txt", "api1/current.txt", "api2/current.txt"}, |
| 1997 | }, |
| 1998 | } |
| 1999 | for _, c := range testcases { |
| 2000 | m := ctx.ModuleForTests(c.moduleName, "android_common") |
| 2001 | manifest := m.Output("metalava.sbox.textproto") |
| 2002 | sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest) |
| 2003 | manifestCommand := sboxProto.Commands[0].GetCommand() |
| 2004 | sourceFilesFlag := "--source-files " + strings.Join(c.sourceTextFileDirs, " ") |
| 2005 | android.AssertStringDoesContain(t, "source text files not present", manifestCommand, sourceFilesFlag) |
| 2006 | } |
| 2007 | } |
| 2008 | |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 2009 | func TestJavaApiLibraryJarGeneration(t *testing.T) { |
| 2010 | provider_bp_a := ` |
| 2011 | java_api_contribution { |
| 2012 | name: "foo1", |
| 2013 | api_file: "foo1.txt", |
| 2014 | } |
| 2015 | ` |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 2016 | provider_bp_b := ` |
| 2017 | java_api_contribution { |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 2018 | name: "foo2", |
| 2019 | api_file: "foo2.txt", |
| 2020 | } |
| 2021 | ` |
| 2022 | ctx, _ := testJavaWithFS(t, ` |
| 2023 | java_api_library { |
| 2024 | name: "bar1", |
| 2025 | api_surface: "public", |
Jihoon Kang | 60d4a09 | 2022-11-17 23:47:43 +0000 | [diff] [blame] | 2026 | api_contributions: ["foo1"], |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | java_api_library { |
| 2030 | name: "bar2", |
| 2031 | api_surface: "system", |
Jihoon Kang | 60d4a09 | 2022-11-17 23:47:43 +0000 | [diff] [blame] | 2032 | api_contributions: ["foo1", "foo2"], |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 2033 | } |
| 2034 | `, |
| 2035 | map[string][]byte{ |
| 2036 | "a/Android.bp": []byte(provider_bp_a), |
| 2037 | "b/Android.bp": []byte(provider_bp_b), |
| 2038 | }) |
| 2039 | |
| 2040 | testcases := []struct { |
| 2041 | moduleName string |
| 2042 | outputJarName string |
| 2043 | }{ |
| 2044 | { |
| 2045 | moduleName: "bar1", |
Jihoon Kang | 25857f5 | 2023-03-01 00:27:32 +0000 | [diff] [blame] | 2046 | outputJarName: "bar1/bar1.jar", |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 2047 | }, |
| 2048 | { |
| 2049 | moduleName: "bar2", |
Jihoon Kang | 25857f5 | 2023-03-01 00:27:32 +0000 | [diff] [blame] | 2050 | outputJarName: "bar2/bar2.jar", |
Jihoon Kang | 0ac87c2 | 2022-11-15 19:06:14 +0000 | [diff] [blame] | 2051 | }, |
| 2052 | } |
| 2053 | for _, c := range testcases { |
| 2054 | m := ctx.ModuleForTests(c.moduleName, "android_common") |
| 2055 | outputs := fmt.Sprint(m.AllOutputs()) |
| 2056 | if !strings.Contains(outputs, c.outputJarName) { |
| 2057 | t.Errorf("Module output does not contain expected jar %s", c.outputJarName) |
| 2058 | } |
| 2059 | } |
| 2060 | } |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 2061 | |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 2062 | func TestJavaApiLibraryLibsLink(t *testing.T) { |
| 2063 | provider_bp_a := ` |
| 2064 | java_api_contribution { |
| 2065 | name: "foo1", |
| 2066 | api_file: "foo1.txt", |
| 2067 | } |
| 2068 | ` |
| 2069 | provider_bp_b := ` |
| 2070 | java_api_contribution { |
| 2071 | name: "foo2", |
| 2072 | api_file: "foo2.txt", |
| 2073 | } |
| 2074 | ` |
| 2075 | lib_bp_a := ` |
| 2076 | java_library { |
| 2077 | name: "lib1", |
| 2078 | srcs: ["Lib.java"], |
| 2079 | } |
| 2080 | ` |
| 2081 | lib_bp_b := ` |
| 2082 | java_library { |
| 2083 | name: "lib2", |
| 2084 | srcs: ["Lib.java"], |
| 2085 | } |
| 2086 | ` |
| 2087 | |
| 2088 | ctx, _ := testJavaWithFS(t, ` |
| 2089 | java_api_library { |
| 2090 | name: "bar1", |
| 2091 | api_surface: "public", |
| 2092 | api_contributions: ["foo1"], |
| 2093 | libs: ["lib1"], |
| 2094 | } |
| 2095 | |
| 2096 | java_api_library { |
| 2097 | name: "bar2", |
| 2098 | api_surface: "system", |
| 2099 | api_contributions: ["foo1", "foo2"], |
| 2100 | libs: ["lib1", "lib2", "bar1"], |
| 2101 | } |
| 2102 | `, |
| 2103 | map[string][]byte{ |
| 2104 | "a/Android.bp": []byte(provider_bp_a), |
| 2105 | "b/Android.bp": []byte(provider_bp_b), |
| 2106 | "c/Android.bp": []byte(lib_bp_a), |
| 2107 | "c/Lib.java": {}, |
| 2108 | "d/Android.bp": []byte(lib_bp_b), |
| 2109 | "d/Lib.java": {}, |
| 2110 | }) |
| 2111 | |
| 2112 | testcases := []struct { |
| 2113 | moduleName string |
| 2114 | classPathJarNames []string |
| 2115 | }{ |
| 2116 | { |
| 2117 | moduleName: "bar1", |
| 2118 | classPathJarNames: []string{"lib1.jar"}, |
| 2119 | }, |
| 2120 | { |
| 2121 | moduleName: "bar2", |
Jihoon Kang | 25857f5 | 2023-03-01 00:27:32 +0000 | [diff] [blame] | 2122 | classPathJarNames: []string{"lib1.jar", "lib2.jar", "bar1/bar1.jar"}, |
Jihoon Kang | 362aa9d | 2023-01-20 19:44:07 +0000 | [diff] [blame] | 2123 | }, |
| 2124 | } |
| 2125 | for _, c := range testcases { |
| 2126 | m := ctx.ModuleForTests(c.moduleName, "android_common") |
| 2127 | javacRules := m.Rule("javac") |
| 2128 | classPathArgs := javacRules.Args["classpath"] |
| 2129 | for _, jarName := range c.classPathJarNames { |
| 2130 | if !strings.Contains(classPathArgs, jarName) { |
| 2131 | t.Errorf("Module output does not contain expected jar %s", jarName) |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | } |
| 2136 | |
Jihoon Kang | e30fff0 | 2023-02-14 20:18:20 +0000 | [diff] [blame] | 2137 | func TestJavaApiLibraryStaticLibsLink(t *testing.T) { |
| 2138 | provider_bp_a := ` |
| 2139 | java_api_contribution { |
| 2140 | name: "foo1", |
| 2141 | api_file: "foo1.txt", |
| 2142 | } |
| 2143 | ` |
| 2144 | provider_bp_b := ` |
| 2145 | java_api_contribution { |
| 2146 | name: "foo2", |
| 2147 | api_file: "foo2.txt", |
| 2148 | } |
| 2149 | ` |
| 2150 | lib_bp_a := ` |
| 2151 | java_library { |
| 2152 | name: "lib1", |
| 2153 | srcs: ["Lib.java"], |
| 2154 | } |
| 2155 | ` |
| 2156 | lib_bp_b := ` |
| 2157 | java_library { |
| 2158 | name: "lib2", |
| 2159 | srcs: ["Lib.java"], |
| 2160 | } |
| 2161 | ` |
| 2162 | |
| 2163 | ctx, _ := testJavaWithFS(t, ` |
| 2164 | java_api_library { |
| 2165 | name: "bar1", |
| 2166 | api_surface: "public", |
| 2167 | api_contributions: ["foo1"], |
| 2168 | static_libs: ["lib1"], |
| 2169 | } |
| 2170 | |
| 2171 | java_api_library { |
| 2172 | name: "bar2", |
| 2173 | api_surface: "system", |
| 2174 | api_contributions: ["foo1", "foo2"], |
| 2175 | static_libs: ["lib1", "lib2", "bar1"], |
| 2176 | } |
| 2177 | `, |
| 2178 | map[string][]byte{ |
| 2179 | "a/Android.bp": []byte(provider_bp_a), |
| 2180 | "b/Android.bp": []byte(provider_bp_b), |
| 2181 | "c/Android.bp": []byte(lib_bp_a), |
| 2182 | "c/Lib.java": {}, |
| 2183 | "d/Android.bp": []byte(lib_bp_b), |
| 2184 | "d/Lib.java": {}, |
| 2185 | }) |
| 2186 | |
| 2187 | testcases := []struct { |
| 2188 | moduleName string |
| 2189 | staticLibJarNames []string |
| 2190 | }{ |
| 2191 | { |
| 2192 | moduleName: "bar1", |
| 2193 | staticLibJarNames: []string{"lib1.jar"}, |
| 2194 | }, |
| 2195 | { |
| 2196 | moduleName: "bar2", |
Jihoon Kang | 25857f5 | 2023-03-01 00:27:32 +0000 | [diff] [blame] | 2197 | staticLibJarNames: []string{"lib1.jar", "lib2.jar", "bar1/bar1.jar"}, |
Jihoon Kang | e30fff0 | 2023-02-14 20:18:20 +0000 | [diff] [blame] | 2198 | }, |
| 2199 | } |
| 2200 | for _, c := range testcases { |
| 2201 | m := ctx.ModuleForTests(c.moduleName, "android_common") |
| 2202 | mergeZipsCommand := m.Rule("merge_zips").RuleParams.Command |
| 2203 | for _, jarName := range c.staticLibJarNames { |
| 2204 | if !strings.Contains(mergeZipsCommand, jarName) { |
| 2205 | t.Errorf("merge_zips command does not contain expected jar %s", jarName) |
| 2206 | } |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | |
Jihoon Kang | 01e522c | 2023-03-14 01:09:34 +0000 | [diff] [blame] | 2211 | func TestJavaApiLibraryDepApiSrcs(t *testing.T) { |
| 2212 | provider_bp_a := ` |
| 2213 | java_api_contribution { |
| 2214 | name: "foo1", |
| 2215 | api_file: "foo1.txt", |
| 2216 | } |
| 2217 | ` |
| 2218 | provider_bp_b := ` |
| 2219 | java_api_contribution { |
| 2220 | name: "foo2", |
| 2221 | api_file: "foo2.txt", |
| 2222 | } |
| 2223 | ` |
| 2224 | lib_bp_a := ` |
| 2225 | java_api_library { |
| 2226 | name: "lib1", |
| 2227 | api_surface: "public", |
| 2228 | api_contributions: ["foo1", "foo2"], |
| 2229 | } |
| 2230 | ` |
| 2231 | |
| 2232 | ctx, _ := testJavaWithFS(t, ` |
| 2233 | java_api_library { |
| 2234 | name: "bar1", |
| 2235 | api_surface: "public", |
| 2236 | api_contributions: ["foo1"], |
| 2237 | dep_api_srcs: "lib1", |
| 2238 | } |
| 2239 | `, |
| 2240 | map[string][]byte{ |
| 2241 | "a/Android.bp": []byte(provider_bp_a), |
| 2242 | "b/Android.bp": []byte(provider_bp_b), |
| 2243 | "c/Android.bp": []byte(lib_bp_a), |
| 2244 | }) |
| 2245 | |
| 2246 | m := ctx.ModuleForTests("bar1", "android_common") |
| 2247 | manifest := m.Output("metalava.sbox.textproto") |
| 2248 | sboxProto := android.RuleBuilderSboxProtoForTests(t, manifest) |
| 2249 | manifestCommand := sboxProto.Commands[0].GetCommand() |
| 2250 | |
| 2251 | android.AssertStringDoesContain(t, "Command expected to contain module srcjar file", manifestCommand, "bar1-stubs.srcjar") |
| 2252 | android.AssertStringDoesContain(t, "Command expected to contain output files list text file flag", manifestCommand, "--out __SBOX_SANDBOX_DIR__/out/sources.txt") |
| 2253 | } |
| 2254 | |
Jihoon Kang | 795319f | 2023-05-17 00:23:38 +0000 | [diff] [blame] | 2255 | func TestJavaApiLibraryFilegroupInput(t *testing.T) { |
| 2256 | ctx, _ := testJavaWithFS(t, ` |
| 2257 | filegroup { |
| 2258 | name: "default_current.txt", |
| 2259 | srcs: ["current.txt"], |
| 2260 | } |
| 2261 | |
| 2262 | java_api_library { |
| 2263 | name: "foo", |
| 2264 | api_files: [":default_current.txt"], |
| 2265 | } |
| 2266 | `, |
| 2267 | map[string][]byte{ |
| 2268 | "current.txt": nil, |
| 2269 | }) |
| 2270 | |
| 2271 | m := ctx.ModuleForTests("foo", "android_common") |
| 2272 | outputs := fmt.Sprint(m.AllOutputs()) |
| 2273 | if !strings.Contains(outputs, "foo/foo.jar") { |
| 2274 | t.Errorf("Module output does not contain expected jar %s", "foo/foo.jar") |
| 2275 | } |
| 2276 | } |
| 2277 | |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 2278 | func TestTradefedOptions(t *testing.T) { |
| 2279 | result := PrepareForTestWithJavaBuildComponents.RunTestWithBp(t, ` |
| 2280 | java_test_host { |
| 2281 | name: "foo", |
| 2282 | test_options: { |
| 2283 | tradefed_options: [ |
| 2284 | { |
| 2285 | name: "exclude-path", |
| 2286 | value: "org/apache" |
| 2287 | } |
| 2288 | ] |
| 2289 | } |
| 2290 | } |
| 2291 | `) |
| 2292 | |
| 2293 | buildOS := result.Config.BuildOS.String() |
| 2294 | args := result.ModuleForTests("foo", buildOS+"_common"). |
| 2295 | Output("out/soong/.intermediates/foo/" + buildOS + "_common/foo.config").Args |
| 2296 | expected := proptools.NinjaAndShellEscape("<option name=\"exclude-path\" value=\"org/apache\" />") |
| 2297 | if args["extraConfigs"] != expected { |
| 2298 | t.Errorf("Expected args[\"extraConfigs\"] to equal %q, was %q", expected, args["extraConfigs"]) |
| 2299 | } |
| 2300 | } |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 2301 | |
| 2302 | func TestTestRunnerOptions(t *testing.T) { |
| 2303 | result := PrepareForTestWithJavaBuildComponents.RunTestWithBp(t, ` |
| 2304 | java_test_host { |
| 2305 | name: "foo", |
| 2306 | test_options: { |
| 2307 | test_runner_options: [ |
| 2308 | { |
| 2309 | name: "test-timeout", |
| 2310 | value: "10m" |
| 2311 | } |
| 2312 | ] |
| 2313 | } |
| 2314 | } |
| 2315 | `) |
| 2316 | |
| 2317 | buildOS := result.Config.BuildOS.String() |
| 2318 | args := result.ModuleForTests("foo", buildOS+"_common"). |
| 2319 | Output("out/soong/.intermediates/foo/" + buildOS + "_common/foo.config").Args |
| 2320 | expected := proptools.NinjaAndShellEscape("<option name=\"test-timeout\" value=\"10m\" />\\n ") |
| 2321 | if args["extraTestRunnerConfigs"] != expected { |
| 2322 | t.Errorf("Expected args[\"extraTestRunnerConfigs\"] to equal %q, was %q", expected, args["extraTestRunnerConfigs"]) |
| 2323 | } |
| 2324 | } |
Jihoon Kang | 381c2fa | 2023-06-01 22:17:32 +0000 | [diff] [blame] | 2325 | |
| 2326 | func TestJavaExcludeStaticLib(t *testing.T) { |
| 2327 | ctx, _ := testJava(t, ` |
| 2328 | java_library { |
| 2329 | name: "bar", |
| 2330 | } |
| 2331 | java_library { |
| 2332 | name: "foo", |
| 2333 | } |
| 2334 | java_library { |
| 2335 | name: "baz", |
| 2336 | static_libs: [ |
| 2337 | "foo", |
| 2338 | "bar", |
| 2339 | ], |
| 2340 | exclude_static_libs: [ |
| 2341 | "bar", |
| 2342 | ], |
| 2343 | } |
| 2344 | `) |
| 2345 | |
| 2346 | // "bar" not included as dependency of "baz" |
| 2347 | CheckModuleDependencies(t, ctx, "baz", "android_common", []string{ |
| 2348 | `core-lambda-stubs`, |
| 2349 | `ext`, |
| 2350 | `foo`, |
| 2351 | `framework`, |
| 2352 | `stable-core-platform-api-stubs-system-modules`, |
| 2353 | `stable.core.platform.api.stubs`, |
| 2354 | }) |
| 2355 | } |