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