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 ( |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 18 | "io/ioutil" |
| 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" |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 23 | "sort" |
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 | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 33 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | var buildDir string |
| 37 | |
| 38 | func setUp() { |
| 39 | var err error |
| 40 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func tearDown() { |
| 47 | os.RemoveAll(buildDir) |
| 48 | } |
| 49 | |
| 50 | func TestMain(m *testing.M) { |
| 51 | run := func() int { |
| 52 | setUp() |
| 53 | defer tearDown() |
| 54 | |
| 55 | return m.Run() |
| 56 | } |
| 57 | |
| 58 | os.Exit(run()) |
| 59 | } |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 60 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 61 | func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config { |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 62 | bp += dexpreopt.BpToolModulesForTest() |
| 63 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 64 | config := TestConfig(buildDir, env, bp, fs) |
| 65 | |
| 66 | // Set up the global Once cache used for dexpreopt.GlobalSoongConfig, so that |
| 67 | // it doesn't create a real one, which would fail. |
| 68 | _ = dexpreopt.GlobalSoongConfigForTests(config) |
| 69 | |
| 70 | return config |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 73 | func testContext() *android.TestContext { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 74 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 75 | ctx := android.NewTestArchContext() |
Paul Duffin | f9b1da0 | 2019-12-18 19:51:55 +0000 | [diff] [blame] | 76 | RegisterJavaBuildComponents(ctx) |
| 77 | RegisterAppBuildComponents(ctx) |
| 78 | RegisterAARBuildComponents(ctx) |
Paul Duffin | b0f8507 | 2019-12-19 10:28:56 +0000 | [diff] [blame] | 79 | RegisterGenRuleBuildComponents(ctx) |
Paul Duffin | 43dc1cc | 2019-12-19 11:18:54 +0000 | [diff] [blame] | 80 | RegisterSystemModulesBuildComponents(ctx) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 81 | ctx.RegisterModuleType("java_plugin", PluginFactory) |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 82 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
| 83 | ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 84 | RegisterDocsBuildComponents(ctx) |
| 85 | RegisterStubsBuildComponents(ctx) |
Paul Duffin | 43dc1cc | 2019-12-19 11:18:54 +0000 | [diff] [blame] | 86 | RegisterSdkLibraryBuildComponents(ctx) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 87 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Paul Duffin | a48f758 | 2019-12-19 11:25:19 +0000 | [diff] [blame] | 88 | |
| 89 | RegisterPrebuiltApisBuildComponents(ctx) |
| 90 | |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 91 | ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 92 | ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory)) |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 93 | ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory)) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 94 | |
| 95 | // Register module types and mutators from cc needed for JNI testing |
Paul Duffin | 77980a8 | 2019-12-19 16:01:36 +0000 | [diff] [blame] | 96 | cc.RegisterRequiredBuildComponentsForTest(ctx) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 97 | |
Martin Stjernholm | d90676f | 2020-01-11 00:37:30 +0000 | [diff] [blame] | 98 | dexpreopt.RegisterToolModulesForTest(ctx) |
| 99 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 100 | return ctx |
| 101 | } |
| 102 | |
| 103 | func run(t *testing.T, ctx *android.TestContext, config android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 104 | t.Helper() |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 105 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 106 | pathCtx := android.PathContextForTesting(config) |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 107 | dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 108 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 109 | ctx.Register(config) |
Paul Duffin | baccf7e | 2019-06-11 11:58:30 +0100 | [diff] [blame] | 110 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 111 | android.FailIfErrored(t, errs) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 112 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 113 | android.FailIfErrored(t, errs) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 116 | 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] | 117 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 118 | return testJavaErrorWithConfig(t, pattern, testConfig(nil, bp, nil)) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 121 | func testJavaErrorWithConfig(t *testing.T, pattern string, config android.Config) (*android.TestContext, android.Config) { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 122 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 123 | ctx := testContext() |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 124 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 125 | pathCtx := android.PathContextForTesting(config) |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 126 | dexpreopt.SetTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 127 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 128 | ctx.Register(config) |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 129 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 130 | if len(errs) > 0 { |
| 131 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 132 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 133 | } |
| 134 | _, errs = ctx.PrepareBuildActions(config) |
| 135 | if len(errs) > 0 { |
| 136 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 137 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 141 | |
| 142 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 145 | func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 146 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 147 | return testJavaWithConfig(t, testConfig(nil, bp, nil)) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 150 | func testJavaWithConfig(t *testing.T, config android.Config) (*android.TestContext, android.Config) { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 151 | t.Helper() |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 152 | ctx := testContext() |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 153 | run(t, ctx, config) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 154 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 155 | return ctx, config |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 158 | func moduleToPath(name string) string { |
| 159 | switch { |
| 160 | case name == `""`: |
| 161 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 162 | case strings.HasSuffix(name, ".jar"): |
| 163 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 164 | default: |
| 165 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Jeongik Cha | e403e9e | 2019-12-07 00:16:24 +0900 | [diff] [blame] | 169 | func TestJavaLinkType(t *testing.T) { |
| 170 | testJava(t, ` |
| 171 | java_library { |
| 172 | name: "foo", |
| 173 | srcs: ["a.java"], |
| 174 | libs: ["bar"], |
| 175 | static_libs: ["baz"], |
| 176 | } |
| 177 | |
| 178 | java_library { |
| 179 | name: "bar", |
| 180 | sdk_version: "current", |
| 181 | srcs: ["b.java"], |
| 182 | } |
| 183 | |
| 184 | java_library { |
| 185 | name: "baz", |
| 186 | sdk_version: "system_current", |
| 187 | srcs: ["c.java"], |
| 188 | } |
| 189 | `) |
| 190 | |
| 191 | testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", ` |
| 192 | java_library { |
| 193 | name: "foo", |
| 194 | srcs: ["a.java"], |
| 195 | libs: ["bar"], |
| 196 | sdk_version: "current", |
| 197 | static_libs: ["baz"], |
| 198 | } |
| 199 | |
| 200 | java_library { |
| 201 | name: "bar", |
| 202 | sdk_version: "current", |
| 203 | srcs: ["b.java"], |
| 204 | } |
| 205 | |
| 206 | java_library { |
| 207 | name: "baz", |
| 208 | sdk_version: "system_current", |
| 209 | srcs: ["c.java"], |
| 210 | } |
| 211 | `) |
| 212 | |
| 213 | testJava(t, ` |
| 214 | java_library { |
| 215 | name: "foo", |
| 216 | srcs: ["a.java"], |
| 217 | libs: ["bar"], |
| 218 | sdk_version: "system_current", |
| 219 | static_libs: ["baz"], |
| 220 | } |
| 221 | |
| 222 | java_library { |
| 223 | name: "bar", |
| 224 | sdk_version: "current", |
| 225 | srcs: ["b.java"], |
| 226 | } |
| 227 | |
| 228 | java_library { |
| 229 | name: "baz", |
| 230 | sdk_version: "system_current", |
| 231 | srcs: ["c.java"], |
| 232 | } |
| 233 | `) |
| 234 | |
| 235 | testJavaError(t, "Adjust sdk_version: property of the source or target module so that target module is built with the same or smaller API set than the source.", ` |
| 236 | java_library { |
| 237 | name: "foo", |
| 238 | srcs: ["a.java"], |
| 239 | libs: ["bar"], |
| 240 | sdk_version: "system_current", |
| 241 | static_libs: ["baz"], |
| 242 | } |
| 243 | |
| 244 | java_library { |
| 245 | name: "bar", |
| 246 | sdk_version: "current", |
| 247 | srcs: ["b.java"], |
| 248 | } |
| 249 | |
| 250 | java_library { |
| 251 | name: "baz", |
| 252 | srcs: ["c.java"], |
| 253 | } |
| 254 | `) |
| 255 | } |
| 256 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 257 | func TestSimple(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 258 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 259 | java_library { |
| 260 | name: "foo", |
| 261 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 262 | libs: ["bar"], |
| 263 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | java_library { |
| 267 | name: "bar", |
| 268 | srcs: ["b.java"], |
| 269 | } |
| 270 | |
| 271 | java_library { |
| 272 | name: "baz", |
| 273 | srcs: ["c.java"], |
| 274 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 275 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 276 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 277 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 278 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 279 | |
| 280 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 281 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 282 | } |
| 283 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 284 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 285 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 286 | bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 287 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 288 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 289 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 292 | if !strings.Contains(javac.Args["classpath"], bazTurbine) { |
| 293 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine) |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 297 | 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] | 298 | } |
| 299 | } |
| 300 | |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame] | 301 | func TestExportedPlugins(t *testing.T) { |
| 302 | type Result struct { |
| 303 | library string |
| 304 | processors string |
| 305 | } |
| 306 | var tests = []struct { |
| 307 | name string |
| 308 | extra string |
| 309 | results []Result |
| 310 | }{ |
| 311 | { |
| 312 | name: "Exported plugin is not a direct plugin", |
| 313 | extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`, |
| 314 | results: []Result{{library: "exports", processors: "-proc:none"}}, |
| 315 | }, |
| 316 | { |
| 317 | name: "Exports plugin to dependee", |
| 318 | extra: ` |
| 319 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 320 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 321 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 322 | `, |
| 323 | results: []Result{ |
| 324 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 325 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 326 | }, |
| 327 | }, |
| 328 | { |
| 329 | name: "Exports plugin to android_library", |
| 330 | extra: ` |
| 331 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 332 | android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 333 | android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 334 | `, |
| 335 | results: []Result{ |
| 336 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 337 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 338 | }, |
| 339 | }, |
| 340 | { |
| 341 | name: "Exports plugin is not propagated via transitive deps", |
| 342 | extra: ` |
| 343 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 344 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 345 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]} |
| 346 | `, |
| 347 | results: []Result{ |
| 348 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 349 | {library: "bar", processors: "-proc:none"}, |
| 350 | }, |
| 351 | }, |
| 352 | { |
| 353 | name: "Exports plugin appends to plugins", |
| 354 | extra: ` |
| 355 | java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"} |
| 356 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 357 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]} |
| 358 | `, |
| 359 | results: []Result{ |
| 360 | {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"}, |
| 361 | }, |
| 362 | }, |
| 363 | } |
| 364 | |
| 365 | for _, test := range tests { |
| 366 | t.Run(test.name, func(t *testing.T) { |
| 367 | ctx, _ := testJava(t, ` |
| 368 | java_plugin { |
| 369 | name: "plugin", |
| 370 | processor_class: "com.android.TestPlugin", |
| 371 | } |
| 372 | `+test.extra) |
| 373 | |
| 374 | for _, want := range test.results { |
| 375 | javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac") |
| 376 | if javac.Args["processor"] != want.processors { |
| 377 | t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"]) |
| 378 | } |
| 379 | } |
| 380 | }) |
| 381 | } |
| 382 | } |
| 383 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 384 | func TestSdkVersionByPartition(t *testing.T) { |
| 385 | 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] | 386 | java_library { |
| 387 | name: "foo", |
| 388 | srcs: ["a.java"], |
| 389 | vendor: true, |
| 390 | } |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 391 | `) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 392 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 393 | testJava(t, ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 394 | java_library { |
| 395 | name: "bar", |
| 396 | srcs: ["b.java"], |
| 397 | } |
| 398 | `) |
| 399 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 400 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 401 | bp := ` |
| 402 | java_library { |
| 403 | name: "foo", |
| 404 | srcs: ["a.java"], |
| 405 | product_specific: true, |
| 406 | } |
| 407 | ` |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 408 | |
| 409 | config := testConfig(nil, bp, nil) |
| 410 | config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 411 | if enforce { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 412 | testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", config) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 413 | } else { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 414 | testJavaWithConfig(t, config) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 415 | } |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 419 | func TestArchSpecific(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 420 | ctx, _ := testJava(t, ` |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 421 | java_library { |
| 422 | name: "foo", |
| 423 | srcs: ["a.java"], |
| 424 | target: { |
| 425 | android: { |
| 426 | srcs: ["b.java"], |
| 427 | }, |
| 428 | }, |
| 429 | } |
| 430 | `) |
| 431 | |
| 432 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 433 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 434 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 435 | } |
| 436 | } |
| 437 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 438 | func TestBinary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 439 | ctx, _ := testJava(t, ` |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 440 | java_library_host { |
| 441 | name: "foo", |
| 442 | srcs: ["a.java"], |
| 443 | } |
| 444 | |
| 445 | java_binary_host { |
| 446 | name: "bar", |
| 447 | srcs: ["b.java"], |
| 448 | static_libs: ["foo"], |
| 449 | } |
| 450 | `) |
| 451 | |
| 452 | buildOS := android.BuildOs.String() |
| 453 | |
| 454 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 455 | barJar := bar.Output("bar.jar").Output.String() |
| 456 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 457 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 458 | |
| 459 | // Test that the install binary wrapper depends on the installed jar file |
| 460 | if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar { |
| 461 | t.Errorf("expected binary wrapper implicits [%q], got %v", |
| 462 | barJar, barWrapperDeps) |
| 463 | } |
| 464 | |
| 465 | } |
| 466 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 467 | func TestPrebuilts(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 468 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 469 | java_library { |
| 470 | name: "foo", |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 471 | srcs: ["a.java", ":stubs-source"], |
Paul Duffin | fcfd791 | 2020-01-31 17:54:30 +0000 | [diff] [blame] | 472 | libs: ["bar", "sdklib"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 473 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 476 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 477 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 478 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 481 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 482 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 483 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 484 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 485 | |
| 486 | dex_import { |
| 487 | name: "qux", |
| 488 | jars: ["b.jar"], |
| 489 | } |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 490 | |
| 491 | java_sdk_library_import { |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 492 | name: "sdklib", |
| 493 | public: { |
| 494 | jars: ["c.jar"], |
| 495 | }, |
| 496 | } |
| 497 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 498 | prebuilt_stubs_sources { |
| 499 | name: "stubs-source", |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 500 | srcs: ["stubs/sources"], |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 501 | } |
Paul Duffin | 1b82e6a | 2019-12-03 18:06:47 +0000 | [diff] [blame] | 502 | |
| 503 | java_test_import { |
| 504 | name: "test", |
| 505 | jars: ["a.jar"], |
| 506 | test_suites: ["cts"], |
| 507 | test_config: "AndroidTest.xml", |
| 508 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 509 | `) |
| 510 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 511 | fooModule := ctx.ModuleForTests("foo", "android_common") |
| 512 | javac := fooModule.Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 513 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 514 | barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output |
| 515 | bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 516 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 517 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 518 | fooLibrary := fooModule.Module().(*Library) |
| 519 | assertDeepEquals(t, "foo java sources incorrect", |
| 520 | []string{"a.java"}, fooLibrary.compiledJavaSrcs.Strings()) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 521 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 522 | assertDeepEquals(t, "foo java source jars incorrect", |
| 523 | []string{".intermediates/stubs-source/android_common/stubs-source-stubs.srcjar"}, |
| 524 | android.NormalizePathsForTesting(fooLibrary.compiledSrcJars)) |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 525 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 526 | if !strings.Contains(javac.Args["classpath"], barJar.String()) { |
| 527 | 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] | 528 | } |
| 529 | |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 530 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 531 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 532 | } |
| 533 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 534 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() { |
| 535 | 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] | 536 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 537 | |
| 538 | ctx.ModuleForTests("qux", "android_common").Rule("Cp") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 541 | func assertDeepEquals(t *testing.T, message string, expected interface{}, actual interface{}) { |
| 542 | if !reflect.DeepEqual(expected, actual) { |
| 543 | t.Errorf("%s: expected %q, found %q", message, expected, actual) |
| 544 | } |
| 545 | } |
| 546 | |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 547 | func TestJavaSdkLibraryImport(t *testing.T) { |
| 548 | ctx, _ := testJava(t, ` |
| 549 | java_library { |
| 550 | name: "foo", |
| 551 | srcs: ["a.java"], |
| 552 | libs: ["sdklib"], |
| 553 | sdk_version: "current", |
| 554 | } |
| 555 | |
| 556 | java_library { |
| 557 | name: "foo.system", |
| 558 | srcs: ["a.java"], |
| 559 | libs: ["sdklib"], |
| 560 | sdk_version: "system_current", |
| 561 | } |
| 562 | |
| 563 | java_library { |
| 564 | name: "foo.test", |
| 565 | srcs: ["a.java"], |
| 566 | libs: ["sdklib"], |
| 567 | sdk_version: "test_current", |
| 568 | } |
| 569 | |
| 570 | java_sdk_library_import { |
| 571 | name: "sdklib", |
| 572 | public: { |
| 573 | jars: ["a.jar"], |
| 574 | }, |
| 575 | system: { |
| 576 | jars: ["b.jar"], |
| 577 | }, |
| 578 | test: { |
| 579 | jars: ["c.jar"], |
Paul Duffin | 0f8faff | 2020-05-20 16:18:00 +0100 | [diff] [blame] | 580 | stub_srcs: ["c.java"], |
Paul Duffin | 56d4490 | 2020-01-31 13:36:25 +0000 | [diff] [blame] | 581 | }, |
| 582 | } |
| 583 | `) |
| 584 | |
| 585 | for _, scope := range []string{"", ".system", ".test"} { |
| 586 | fooModule := ctx.ModuleForTests("foo"+scope, "android_common") |
| 587 | javac := fooModule.Rule("javac") |
| 588 | |
| 589 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs"+scope, "android_common").Rule("combineJar").Output |
| 590 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 591 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 596 | func TestDefaults(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 597 | ctx, _ := testJava(t, ` |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 598 | java_defaults { |
| 599 | name: "defaults", |
| 600 | srcs: ["a.java"], |
| 601 | libs: ["bar"], |
| 602 | static_libs: ["baz"], |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 603 | optimize: {enabled: false}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | java_library { |
| 607 | name: "foo", |
| 608 | defaults: ["defaults"], |
| 609 | } |
| 610 | |
| 611 | java_library { |
| 612 | name: "bar", |
| 613 | srcs: ["b.java"], |
| 614 | } |
| 615 | |
| 616 | java_library { |
| 617 | name: "baz", |
| 618 | srcs: ["c.java"], |
| 619 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 620 | |
| 621 | android_test { |
| 622 | name: "atestOptimize", |
| 623 | defaults: ["defaults"], |
| 624 | optimize: {enabled: true}, |
| 625 | } |
| 626 | |
| 627 | android_test { |
| 628 | name: "atestNoOptimize", |
| 629 | defaults: ["defaults"], |
| 630 | } |
| 631 | |
| 632 | android_test { |
| 633 | name: "atestDefault", |
| 634 | srcs: ["a.java"], |
| 635 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 636 | `) |
| 637 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 638 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 639 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 640 | |
| 641 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 642 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 643 | } |
| 644 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 645 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 646 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 647 | 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] | 648 | } |
| 649 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 650 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 651 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 652 | 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] | 653 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 654 | |
| 655 | atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8") |
| 656 | if atestOptimize.Output == nil { |
| 657 | t.Errorf("atestOptimize should optimize APK") |
| 658 | } |
| 659 | |
| 660 | atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8") |
| 661 | if atestNoOptimize.Output == nil { |
| 662 | t.Errorf("atestNoOptimize should not optimize APK") |
| 663 | } |
| 664 | |
| 665 | atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8") |
| 666 | if atestDefault.Output == nil { |
| 667 | t.Errorf("atestDefault should optimize APK") |
| 668 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 671 | func TestResources(t *testing.T) { |
| 672 | var table = []struct { |
| 673 | name string |
| 674 | prop string |
| 675 | extra string |
| 676 | args string |
| 677 | }{ |
| 678 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 679 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 680 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 681 | prop: `java_resource_dirs: ["java-res"]`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 682 | 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] | 683 | }, |
| 684 | { |
| 685 | // Test that a module with java_resources includes the files |
| 686 | name: "resource files", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 687 | prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`, |
| 688 | args: "-C . -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 689 | }, |
| 690 | { |
| 691 | // Test that a module with a filegroup in java_resources includes the files with the |
| 692 | // path prefix |
| 693 | name: "resource filegroup", |
| 694 | prop: `java_resources: [":foo-res"]`, |
| 695 | extra: ` |
| 696 | filegroup { |
| 697 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 698 | path: "java-res", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 699 | srcs: ["java-res/a/a", "java-res/b/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 700 | }`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 701 | 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] | 702 | }, |
| 703 | { |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 704 | // Test that a module with wildcards in java_resource_dirs has the correct path prefixes |
| 705 | name: "wildcard dirs", |
| 706 | prop: `java_resource_dirs: ["java-res/*"]`, |
| 707 | args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b", |
| 708 | }, |
| 709 | { |
| 710 | // Test that a module exclude_java_resource_dirs excludes the files |
| 711 | name: "wildcard dirs", |
| 712 | prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`, |
| 713 | args: "-C java-res/a -f java-res/a/a", |
| 714 | }, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 715 | { |
| 716 | // Test wildcards in java_resources |
| 717 | name: "wildcard files", |
| 718 | prop: `java_resources: ["java-res/**/*"]`, |
| 719 | args: "-C . -f java-res/a/a -f java-res/b/b", |
| 720 | }, |
| 721 | { |
| 722 | // Test exclude_java_resources with java_resources |
| 723 | name: "wildcard files with exclude", |
| 724 | prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`, |
| 725 | args: "-C . -f java-res/a/a", |
| 726 | }, |
| 727 | { |
| 728 | // Test exclude_java_resources with java_resource_dirs |
| 729 | name: "resource dirs with exclude files", |
| 730 | prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`, |
| 731 | args: "-C java-res -f java-res/a/a", |
| 732 | }, |
| 733 | { |
| 734 | // Test exclude_java_resource_dirs with java_resource_dirs |
| 735 | name: "resource dirs with exclude files", |
| 736 | prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`, |
| 737 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
| 738 | }, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | for _, test := range table { |
| 742 | t.Run(test.name, func(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 743 | ctx, _ := testJava(t, ` |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 744 | java_library { |
| 745 | name: "foo", |
| 746 | srcs: [ |
| 747 | "a.java", |
| 748 | "b.java", |
| 749 | "c.java", |
| 750 | ], |
| 751 | `+test.prop+`, |
| 752 | } |
| 753 | `+test.extra) |
| 754 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 755 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 756 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 757 | |
| 758 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 759 | t.Errorf("foo combined jars %v does not contain %q", |
| 760 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 761 | } |
| 762 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 763 | if fooRes.Args["jarArgs"] != test.args { |
| 764 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 765 | fooRes.Args["jarArgs"], test.args) |
| 766 | } |
| 767 | }) |
| 768 | } |
| 769 | } |
| 770 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 771 | func TestIncludeSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 772 | ctx, _ := testJava(t, ` |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 773 | java_library { |
| 774 | name: "foo", |
| 775 | srcs: [ |
| 776 | "a.java", |
| 777 | "b.java", |
| 778 | "c.java", |
| 779 | ], |
| 780 | include_srcs: true, |
| 781 | } |
| 782 | |
| 783 | java_library { |
| 784 | name: "bar", |
| 785 | srcs: [ |
| 786 | "a.java", |
| 787 | "b.java", |
| 788 | "c.java", |
| 789 | ], |
| 790 | java_resource_dirs: ["java-res"], |
| 791 | include_srcs: true, |
| 792 | } |
| 793 | `) |
| 794 | |
| 795 | // Test a library with include_srcs: true |
| 796 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
| 797 | fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar") |
| 798 | |
| 799 | if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) { |
| 800 | t.Errorf("foo combined jars %v does not contain %q", w, g) |
| 801 | } |
| 802 | |
| 803 | if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 804 | t.Errorf("foo source jar args %q is not %q", w, g) |
| 805 | } |
| 806 | |
| 807 | // Test a library with include_srcs: true and resources |
| 808 | bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar") |
| 809 | barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar") |
| 810 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
| 811 | barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar") |
| 812 | |
| 813 | if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 814 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 815 | } |
| 816 | |
| 817 | if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 818 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 819 | } |
| 820 | |
| 821 | if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) { |
| 822 | t.Errorf("bar combined jars %v does not contain %q", w, g) |
| 823 | } |
| 824 | |
| 825 | if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 826 | t.Errorf("bar source jar args %q is not %q", w, g) |
| 827 | } |
| 828 | |
| 829 | if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w { |
| 830 | t.Errorf("bar resource jar args %q is not %q", w, g) |
| 831 | } |
| 832 | } |
| 833 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 834 | func TestGeneratedSources(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 835 | ctx, _ := testJava(t, ` |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 836 | java_library { |
| 837 | name: "foo", |
| 838 | srcs: [ |
| 839 | "a*.java", |
| 840 | ":gen", |
| 841 | "b*.java", |
| 842 | ], |
| 843 | } |
| 844 | |
| 845 | genrule { |
| 846 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 847 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 848 | out: ["gen.java"], |
| 849 | } |
| 850 | `) |
| 851 | |
| 852 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 853 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 854 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 855 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 856 | 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] | 857 | } |
| 858 | |
| 859 | if len(javac.Inputs) != 3 || |
| 860 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 861 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 862 | javac.Inputs[2].String() != "b.java" { |
| 863 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 864 | } |
| 865 | } |
| 866 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 867 | func TestTurbine(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 868 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 869 | java_library { |
| 870 | name: "foo", |
| 871 | srcs: ["a.java"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 872 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | java_library { |
| 876 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 877 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 878 | static_libs: ["foo"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 879 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | java_library { |
| 883 | name: "baz", |
| 884 | srcs: ["c.java"], |
| 885 | libs: ["bar"], |
| 886 | sdk_version: "14", |
| 887 | } |
| 888 | `) |
| 889 | |
| 890 | fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine") |
| 891 | barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine") |
| 892 | barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 893 | barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine") |
| 894 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 895 | |
| 896 | if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" { |
| 897 | t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs) |
| 898 | } |
| 899 | |
| 900 | fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 901 | if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) { |
| 902 | t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar) |
| 903 | } |
| 904 | if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) { |
| 905 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar) |
| 906 | } |
| 907 | if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar { |
| 908 | t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar) |
| 909 | } |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 910 | if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") { |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 911 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 912 | "prebuilts/sdk/14/public/android.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | func TestSharding(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 917 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 918 | java_library { |
| 919 | name: "bar", |
| 920 | srcs: ["a.java","b.java","c.java"], |
| 921 | javac_shard_size: 1 |
| 922 | } |
| 923 | `) |
| 924 | |
| 925 | barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 926 | for i := 0; i < 3; i++ { |
| 927 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
| 928 | if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) { |
| 929 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar) |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 934 | func TestDroiddoc(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 935 | ctx, _ := testJava(t, ` |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 936 | droiddoc_exported_dir { |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 937 | name: "droiddoc-templates-sdk", |
| 938 | path: ".", |
| 939 | } |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 940 | filegroup { |
| 941 | name: "bar-doc-aidl-srcs", |
| 942 | srcs: ["bar-doc/IBar.aidl"], |
| 943 | path: "bar-doc", |
| 944 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 945 | droiddoc { |
| 946 | name: "bar-doc", |
| 947 | srcs: [ |
| 948 | "bar-doc/*.java", |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 949 | "bar-doc/IFoo.aidl", |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 950 | ":bar-doc-aidl-srcs", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 951 | ], |
| 952 | exclude_srcs: [ |
| 953 | "bar-doc/b.java" |
| 954 | ], |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 955 | custom_template: "droiddoc-templates-sdk", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 956 | hdf: [ |
| 957 | "android.whichdoc offline", |
| 958 | ], |
| 959 | knowntags: [ |
| 960 | "bar-doc/known_oj_tags.txt", |
| 961 | ], |
| 962 | proofread_file: "libcore-proofread.txt", |
| 963 | todo_file: "libcore-docs-todo.html", |
| 964 | args: "-offlinemode -title \"libcore\"", |
| 965 | } |
| 966 | `) |
| 967 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 968 | barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc") |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 969 | var javaSrcs []string |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 970 | for _, i := range barDoc.Inputs { |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 971 | javaSrcs = append(javaSrcs, i.Base()) |
| 972 | } |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 973 | if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" { |
| 974 | t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs) |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 975 | } |
| 976 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 977 | aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl") |
| 978 | if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) { |
| 979 | t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g) |
| 980 | } |
| 981 | |
| 982 | if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) { |
| 983 | t.Errorf("aidl inputs must be %q, but was %q", w, g) |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 984 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 985 | } |
| 986 | |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 987 | func TestDroidstubsWithSystemModules(t *testing.T) { |
| 988 | ctx, _ := testJava(t, ` |
| 989 | droidstubs { |
| 990 | name: "stubs-source-system-modules", |
| 991 | srcs: [ |
| 992 | "bar-doc/*.java", |
| 993 | ], |
| 994 | sdk_version: "none", |
| 995 | system_modules: "source-system-modules", |
| 996 | } |
| 997 | |
| 998 | java_library { |
| 999 | name: "source-jar", |
| 1000 | srcs: [ |
| 1001 | "a.java", |
| 1002 | ], |
| 1003 | } |
| 1004 | |
| 1005 | java_system_modules { |
| 1006 | name: "source-system-modules", |
| 1007 | libs: ["source-jar"], |
| 1008 | } |
| 1009 | |
| 1010 | droidstubs { |
| 1011 | name: "stubs-prebuilt-system-modules", |
| 1012 | srcs: [ |
| 1013 | "bar-doc/*.java", |
| 1014 | ], |
| 1015 | sdk_version: "none", |
| 1016 | system_modules: "prebuilt-system-modules", |
| 1017 | } |
| 1018 | |
| 1019 | java_import { |
| 1020 | name: "prebuilt-jar", |
| 1021 | jars: ["a.jar"], |
| 1022 | } |
| 1023 | |
| 1024 | java_system_modules_import { |
| 1025 | name: "prebuilt-system-modules", |
| 1026 | libs: ["prebuilt-jar"], |
| 1027 | } |
| 1028 | `) |
| 1029 | |
| 1030 | checkSystemModulesUseByDroidstubs(t, ctx, "stubs-source-system-modules", "source-jar.jar") |
| 1031 | |
| 1032 | checkSystemModulesUseByDroidstubs(t, ctx, "stubs-prebuilt-system-modules", "prebuilt-jar.jar") |
| 1033 | } |
| 1034 | |
| 1035 | func checkSystemModulesUseByDroidstubs(t *testing.T, ctx *android.TestContext, moduleName string, systemJar string) { |
| 1036 | metalavaRule := ctx.ModuleForTests(moduleName, "android_common").Rule("metalava") |
| 1037 | var systemJars []string |
| 1038 | for _, i := range metalavaRule.Implicits { |
| 1039 | systemJars = append(systemJars, i.Base()) |
| 1040 | } |
| 1041 | if len(systemJars) != 1 || systemJars[0] != systemJar { |
| 1042 | t.Errorf("inputs of %q must be []string{%q}, but was %#v.", moduleName, systemJar, systemJars) |
| 1043 | } |
| 1044 | } |
| 1045 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 1046 | func TestJarGenrules(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1047 | ctx, _ := testJava(t, ` |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 1048 | java_library { |
| 1049 | name: "foo", |
| 1050 | srcs: ["a.java"], |
| 1051 | } |
| 1052 | |
| 1053 | java_genrule { |
| 1054 | name: "jargen", |
| 1055 | tool_files: ["b.java"], |
| 1056 | cmd: "$(location b.java) $(in) $(out)", |
| 1057 | out: ["jargen.jar"], |
| 1058 | srcs: [":foo"], |
| 1059 | } |
| 1060 | |
| 1061 | java_library { |
| 1062 | name: "bar", |
| 1063 | static_libs: ["jargen"], |
| 1064 | srcs: ["c.java"], |
| 1065 | } |
| 1066 | |
| 1067 | java_library { |
| 1068 | name: "baz", |
| 1069 | libs: ["jargen"], |
| 1070 | srcs: ["c.java"], |
| 1071 | } |
| 1072 | `) |
| 1073 | |
| 1074 | foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar") |
| 1075 | jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar") |
| 1076 | bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar") |
| 1077 | baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar") |
| 1078 | barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") |
| 1079 | |
| 1080 | if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() { |
| 1081 | t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings()) |
| 1082 | } |
| 1083 | |
| 1084 | if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) { |
| 1085 | t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String()) |
| 1086 | } |
| 1087 | |
| 1088 | if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) { |
| 1089 | t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String()) |
| 1090 | } |
| 1091 | |
| 1092 | if len(barCombined.Inputs) != 2 || |
| 1093 | barCombined.Inputs[0].String() != bar.Output.String() || |
| 1094 | barCombined.Inputs[1].String() != jargen.Output.String() { |
| 1095 | t.Errorf("bar combined jar inputs %v is not [%q, %q]", |
| 1096 | barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String()) |
| 1097 | } |
| 1098 | } |
| 1099 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1100 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1101 | ctx, _ := testJava(t, ` |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1102 | java_library { |
| 1103 | name: "foo", |
| 1104 | srcs: ["a.java", ":foo-srcs"], |
| 1105 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 1106 | } |
| 1107 | |
| 1108 | filegroup { |
| 1109 | name: "foo-srcs", |
| 1110 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 1111 | } |
| 1112 | |
| 1113 | filegroup { |
| 1114 | name: "foo-excludes", |
| 1115 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 1116 | } |
| 1117 | `) |
| 1118 | |
| 1119 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1120 | |
| 1121 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 1122 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 1123 | } |
| 1124 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1125 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1126 | func TestJavaLibrary(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1127 | config := testConfig(nil, "", map[string][]byte{ |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1128 | "libcore/Android.bp": []byte(` |
| 1129 | java_library { |
| 1130 | name: "core", |
| 1131 | sdk_version: "none", |
| 1132 | system_modules: "none", |
| 1133 | }`), |
| 1134 | }) |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1135 | ctx := testContext() |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1136 | run(t, ctx, config) |
| 1137 | } |
| 1138 | |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1139 | func TestJavaSdkLibrary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1140 | ctx, _ := testJava(t, ` |
Paul Duffin | 884363e | 2019-12-19 10:21:09 +0000 | [diff] [blame] | 1141 | droiddoc_exported_dir { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1142 | name: "droiddoc-templates-sdk", |
| 1143 | path: ".", |
| 1144 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1145 | java_sdk_library { |
| 1146 | name: "foo", |
| 1147 | srcs: ["a.java", "b.java"], |
| 1148 | api_packages: ["foo"], |
| 1149 | } |
| 1150 | java_sdk_library { |
| 1151 | name: "bar", |
| 1152 | srcs: ["a.java", "b.java"], |
| 1153 | api_packages: ["bar"], |
| 1154 | } |
| 1155 | java_library { |
| 1156 | name: "baz", |
| 1157 | srcs: ["c.java"], |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1158 | libs: ["foo", "bar.stubs"], |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1159 | sdk_version: "system_current", |
| 1160 | } |
Paul Duffin | dfa131e | 2020-05-15 20:37:11 +0100 | [diff] [blame] | 1161 | java_sdk_library { |
| 1162 | name: "barney", |
| 1163 | srcs: ["c.java"], |
| 1164 | api_only: true, |
| 1165 | } |
| 1166 | java_sdk_library { |
| 1167 | name: "betty", |
| 1168 | srcs: ["c.java"], |
| 1169 | shared_library: false, |
| 1170 | } |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1171 | java_sdk_library_import { |
| 1172 | name: "quuz", |
| 1173 | public: { |
| 1174 | jars: ["c.jar"], |
| 1175 | }, |
| 1176 | } |
| 1177 | java_sdk_library_import { |
| 1178 | name: "fred", |
| 1179 | public: { |
| 1180 | jars: ["b.jar"], |
| 1181 | }, |
| 1182 | } |
Paul Duffin | dfa131e | 2020-05-15 20:37:11 +0100 | [diff] [blame] | 1183 | java_sdk_library_import { |
| 1184 | name: "wilma", |
| 1185 | public: { |
| 1186 | jars: ["b.jar"], |
| 1187 | }, |
| 1188 | shared_library: false, |
| 1189 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1190 | java_library { |
| 1191 | name: "qux", |
| 1192 | srcs: ["c.java"], |
Paul Duffin | dfa131e | 2020-05-15 20:37:11 +0100 | [diff] [blame] | 1193 | libs: ["baz", "fred", "quuz.stubs", "wilma", "barney", "betty"], |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1194 | sdk_version: "system_current", |
| 1195 | } |
Paul Duffin | 726d23c | 2020-01-22 16:30:37 +0000 | [diff] [blame] | 1196 | java_library { |
| 1197 | name: "baz-test", |
| 1198 | srcs: ["c.java"], |
| 1199 | libs: ["foo"], |
| 1200 | sdk_version: "test_current", |
| 1201 | } |
Paul Duffin | a2db18f | 2020-01-22 17:11:15 +0000 | [diff] [blame] | 1202 | java_library { |
| 1203 | name: "baz-29", |
| 1204 | srcs: ["c.java"], |
| 1205 | libs: ["foo"], |
| 1206 | sdk_version: "system_29", |
| 1207 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1208 | `) |
| 1209 | |
| 1210 | // check the existence of the internal modules |
| 1211 | ctx.ModuleForTests("foo", "android_common") |
Paul Duffin | dd9d074 | 2020-05-08 15:52:37 +0100 | [diff] [blame] | 1212 | ctx.ModuleForTests(apiScopePublic.stubsLibraryModuleName("foo"), "android_common") |
| 1213 | ctx.ModuleForTests(apiScopeSystem.stubsLibraryModuleName("foo"), "android_common") |
| 1214 | ctx.ModuleForTests(apiScopeTest.stubsLibraryModuleName("foo"), "android_common") |
| 1215 | ctx.ModuleForTests(apiScopePublic.stubsSourceModuleName("foo"), "android_common") |
| 1216 | ctx.ModuleForTests(apiScopeSystem.stubsSourceModuleName("foo"), "android_common") |
| 1217 | ctx.ModuleForTests(apiScopeTest.stubsSourceModuleName("foo"), "android_common") |
Jiyong Park | e383388 | 2020-02-17 17:28:10 +0900 | [diff] [blame] | 1218 | ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1219 | ctx.ModuleForTests("foo.api.public.28", "") |
| 1220 | ctx.ModuleForTests("foo.api.system.28", "") |
| 1221 | ctx.ModuleForTests("foo.api.test.28", "") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1222 | |
| 1223 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1224 | // tests if baz is actually linked to the stubs lib |
| 1225 | if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") { |
| 1226 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
| 1227 | "foo.stubs.system.jar") |
| 1228 | } |
| 1229 | // ... and not to the impl lib |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 1230 | if strings.Contains(bazJavac.Args["classpath"], "foo.jar") { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1231 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 1232 | "foo.jar") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1233 | } |
| 1234 | // test if baz is not linked to the system variant of foo |
| 1235 | if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") { |
| 1236 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
| 1237 | "foo.stubs.jar") |
| 1238 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1239 | |
Paul Duffin | 726d23c | 2020-01-22 16:30:37 +0000 | [diff] [blame] | 1240 | bazTestJavac := ctx.ModuleForTests("baz-test", "android_common").Rule("javac") |
| 1241 | // tests if baz-test is actually linked to the test stubs lib |
| 1242 | if !strings.Contains(bazTestJavac.Args["classpath"], "foo.stubs.test.jar") { |
| 1243 | t.Errorf("baz-test javac classpath %v does not contain %q", bazTestJavac.Args["classpath"], |
| 1244 | "foo.stubs.test.jar") |
| 1245 | } |
| 1246 | |
Paul Duffin | a2db18f | 2020-01-22 17:11:15 +0000 | [diff] [blame] | 1247 | baz29Javac := ctx.ModuleForTests("baz-29", "android_common").Rule("javac") |
| 1248 | // tests if baz-29 is actually linked to the system 29 stubs lib |
| 1249 | if !strings.Contains(baz29Javac.Args["classpath"], "prebuilts/sdk/29/system/foo.jar") { |
| 1250 | t.Errorf("baz-29 javac classpath %v does not contain %q", baz29Javac.Args["classpath"], |
| 1251 | "prebuilts/sdk/29/system/foo.jar") |
| 1252 | } |
| 1253 | |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1254 | // test if baz has exported SDK lib names foo and bar to qux |
| 1255 | qux := ctx.ModuleForTests("qux", "android_common") |
| 1256 | if quxLib, ok := qux.Module().(*Library); ok { |
| 1257 | sdkLibs := quxLib.ExportedSdkLibs() |
Paul Duffin | 859fe96 | 2020-05-15 10:20:31 +0100 | [diff] [blame] | 1258 | sort.Strings(sdkLibs) |
| 1259 | if w := []string{"bar", "foo", "fred", "quuz"}; !reflect.DeepEqual(w, sdkLibs) { |
| 1260 | t.Errorf("qux should export %q but exports %q", w, sdkLibs) |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1261 | } |
| 1262 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1263 | } |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1264 | |
Paul Duffin | daaa332 | 2020-05-26 18:13:57 +0100 | [diff] [blame^] | 1265 | func TestJavaSdkLibrary_DoNotAccessImplWhenItIsNotBuilt(t *testing.T) { |
| 1266 | ctx, _ := testJava(t, ` |
| 1267 | java_sdk_library { |
| 1268 | name: "foo", |
| 1269 | srcs: ["a.java"], |
| 1270 | api_only: true, |
| 1271 | public: { |
| 1272 | enabled: true, |
| 1273 | }, |
| 1274 | } |
| 1275 | |
| 1276 | java_library { |
| 1277 | name: "bar", |
| 1278 | srcs: ["b.java"], |
| 1279 | libs: ["foo"], |
| 1280 | } |
| 1281 | `) |
| 1282 | |
| 1283 | // The bar library should depend on the stubs jar. |
| 1284 | barLibrary := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 1285 | if expected, actual := `^-classpath .*:/[^:]*/turbine-combined/foo\.stubs\.jar$`, barLibrary.Args["classpath"]; !regexp.MustCompile(expected).MatchString(actual) { |
| 1286 | t.Errorf("expected %q, found %#q", expected, actual) |
| 1287 | } |
| 1288 | } |
| 1289 | |
Paul Duffin | 46dc45a | 2020-05-14 15:39:10 +0100 | [diff] [blame] | 1290 | func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) { |
| 1291 | testJava(t, ` |
| 1292 | java_sdk_library { |
| 1293 | name: "foo", |
| 1294 | srcs: ["a.java"], |
| 1295 | api_packages: ["foo"], |
| 1296 | public: { |
| 1297 | enabled: true, |
| 1298 | }, |
| 1299 | } |
| 1300 | |
| 1301 | java_library { |
| 1302 | name: "bar", |
| 1303 | srcs: ["b.java", ":foo{.public.stubs.source}"], |
| 1304 | } |
| 1305 | `) |
| 1306 | } |
| 1307 | |
| 1308 | func TestJavaSdkLibrary_AccessOutputFiles_MissingScope(t *testing.T) { |
| 1309 | testJavaError(t, `"foo" does not provide api scope system`, ` |
| 1310 | java_sdk_library { |
| 1311 | name: "foo", |
| 1312 | srcs: ["a.java"], |
| 1313 | api_packages: ["foo"], |
| 1314 | public: { |
| 1315 | enabled: true, |
| 1316 | }, |
| 1317 | } |
| 1318 | |
| 1319 | java_library { |
| 1320 | name: "bar", |
| 1321 | srcs: ["b.java", ":foo{.system.stubs.source}"], |
| 1322 | } |
| 1323 | `) |
| 1324 | } |
| 1325 | |
| 1326 | func TestJavaSdkLibraryImport_AccessOutputFiles(t *testing.T) { |
| 1327 | testJava(t, ` |
| 1328 | java_sdk_library_import { |
| 1329 | name: "foo", |
| 1330 | public: { |
| 1331 | jars: ["a.jar"], |
| 1332 | stub_srcs: ["a.java"], |
| 1333 | current_api: "api/current.txt", |
| 1334 | removed_api: "api/removed.txt", |
| 1335 | }, |
| 1336 | } |
| 1337 | |
| 1338 | java_library { |
| 1339 | name: "bar", |
| 1340 | srcs: [":foo{.public.stubs.source}"], |
| 1341 | java_resources: [ |
| 1342 | ":foo{.public.api.txt}", |
| 1343 | ":foo{.public.removed-api.txt}", |
| 1344 | ], |
| 1345 | } |
| 1346 | `) |
| 1347 | } |
| 1348 | |
| 1349 | func TestJavaSdkLibraryImport_AccessOutputFiles_Invalid(t *testing.T) { |
| 1350 | bp := ` |
| 1351 | java_sdk_library_import { |
| 1352 | name: "foo", |
| 1353 | public: { |
| 1354 | jars: ["a.jar"], |
| 1355 | }, |
| 1356 | } |
| 1357 | ` |
| 1358 | |
| 1359 | t.Run("stubs.source", func(t *testing.T) { |
| 1360 | testJavaError(t, `stubs.source not available for api scope public`, bp+` |
| 1361 | java_library { |
| 1362 | name: "bar", |
| 1363 | srcs: [":foo{.public.stubs.source}"], |
| 1364 | java_resources: [ |
| 1365 | ":foo{.public.api.txt}", |
| 1366 | ":foo{.public.removed-api.txt}", |
| 1367 | ], |
| 1368 | } |
| 1369 | `) |
| 1370 | }) |
| 1371 | |
| 1372 | t.Run("api.txt", func(t *testing.T) { |
| 1373 | testJavaError(t, `api.txt not available for api scope public`, bp+` |
| 1374 | java_library { |
| 1375 | name: "bar", |
| 1376 | srcs: ["a.java"], |
| 1377 | java_resources: [ |
| 1378 | ":foo{.public.api.txt}", |
| 1379 | ], |
| 1380 | } |
| 1381 | `) |
| 1382 | }) |
| 1383 | |
| 1384 | t.Run("removed-api.txt", func(t *testing.T) { |
| 1385 | testJavaError(t, `removed-api.txt not available for api scope public`, bp+` |
| 1386 | java_library { |
| 1387 | name: "bar", |
| 1388 | srcs: ["a.java"], |
| 1389 | java_resources: [ |
| 1390 | ":foo{.public.removed-api.txt}", |
| 1391 | ], |
| 1392 | } |
| 1393 | `) |
| 1394 | }) |
| 1395 | } |
| 1396 | |
Paul Duffin | 3375e35 | 2020-04-28 10:44:03 +0100 | [diff] [blame] | 1397 | func TestJavaSdkLibrary_InvalidScopes(t *testing.T) { |
| 1398 | testJavaError(t, `module "foo": enabled api scope "system" depends on disabled scope "public"`, ` |
| 1399 | java_sdk_library { |
| 1400 | name: "foo", |
| 1401 | srcs: ["a.java", "b.java"], |
| 1402 | api_packages: ["foo"], |
| 1403 | // Explicitly disable public to test the check that ensures the set of enabled |
| 1404 | // scopes is consistent. |
| 1405 | public: { |
| 1406 | enabled: false, |
| 1407 | }, |
| 1408 | system: { |
| 1409 | enabled: true, |
| 1410 | }, |
| 1411 | } |
| 1412 | `) |
| 1413 | } |
| 1414 | |
Paul Duffin | 87a05a3 | 2020-05-12 11:50:28 +0100 | [diff] [blame] | 1415 | func TestJavaSdkLibrary_SdkVersion_ForScope(t *testing.T) { |
| 1416 | testJava(t, ` |
| 1417 | java_sdk_library { |
| 1418 | name: "foo", |
| 1419 | srcs: ["a.java", "b.java"], |
| 1420 | api_packages: ["foo"], |
| 1421 | system: { |
| 1422 | enabled: true, |
| 1423 | sdk_version: "module_current", |
| 1424 | }, |
| 1425 | } |
| 1426 | `) |
| 1427 | } |
| 1428 | |
Paul Duffin | 803a956 | 2020-05-20 11:52:25 +0100 | [diff] [blame] | 1429 | func TestJavaSdkLibrary_MissingScope(t *testing.T) { |
| 1430 | testJavaError(t, `requires api scope module-lib from foo but it only has \[\] available`, ` |
| 1431 | java_sdk_library { |
| 1432 | name: "foo", |
| 1433 | srcs: ["a.java"], |
| 1434 | public: { |
| 1435 | enabled: false, |
| 1436 | }, |
| 1437 | } |
| 1438 | |
| 1439 | java_library { |
| 1440 | name: "baz", |
| 1441 | srcs: ["a.java"], |
| 1442 | libs: ["foo"], |
| 1443 | sdk_version: "module_current", |
| 1444 | } |
| 1445 | `) |
| 1446 | } |
| 1447 | |
| 1448 | func TestJavaSdkLibrary_FallbackScope(t *testing.T) { |
| 1449 | testJava(t, ` |
| 1450 | java_sdk_library { |
| 1451 | name: "foo", |
| 1452 | srcs: ["a.java"], |
| 1453 | system: { |
| 1454 | enabled: true, |
| 1455 | }, |
| 1456 | } |
| 1457 | |
| 1458 | java_library { |
| 1459 | name: "baz", |
| 1460 | srcs: ["a.java"], |
| 1461 | libs: ["foo"], |
| 1462 | // foo does not have module-lib scope so it should fallback to system |
| 1463 | sdk_version: "module_current", |
| 1464 | } |
| 1465 | `) |
| 1466 | } |
| 1467 | |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1468 | var compilerFlagsTestCases = []struct { |
| 1469 | in string |
| 1470 | out bool |
| 1471 | }{ |
| 1472 | { |
| 1473 | in: "a", |
| 1474 | out: false, |
| 1475 | }, |
| 1476 | { |
| 1477 | in: "-a", |
| 1478 | out: true, |
| 1479 | }, |
| 1480 | { |
| 1481 | in: "-no-jdk", |
| 1482 | out: false, |
| 1483 | }, |
| 1484 | { |
| 1485 | in: "-no-stdlib", |
| 1486 | out: false, |
| 1487 | }, |
| 1488 | { |
| 1489 | in: "-kotlin-home", |
| 1490 | out: false, |
| 1491 | }, |
| 1492 | { |
| 1493 | in: "-kotlin-home /some/path", |
| 1494 | out: false, |
| 1495 | }, |
| 1496 | { |
| 1497 | in: "-include-runtime", |
| 1498 | out: false, |
| 1499 | }, |
| 1500 | { |
| 1501 | in: "-Xintellij-plugin-root", |
| 1502 | out: false, |
| 1503 | }, |
| 1504 | } |
| 1505 | |
| 1506 | type mockContext struct { |
| 1507 | android.ModuleContext |
| 1508 | result bool |
| 1509 | } |
| 1510 | |
| 1511 | func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) { |
| 1512 | // CheckBadCompilerFlags calls this function when the flag should be rejected |
| 1513 | ctx.result = false |
| 1514 | } |
| 1515 | |
| 1516 | func TestCompilerFlags(t *testing.T) { |
| 1517 | for _, testCase := range compilerFlagsTestCases { |
| 1518 | ctx := &mockContext{result: true} |
| 1519 | CheckKotlincFlags(ctx, []string{testCase.in}) |
| 1520 | if ctx.result != testCase.out { |
| 1521 | t.Errorf("incorrect output:") |
| 1522 | t.Errorf(" input: %#v", testCase.in) |
| 1523 | t.Errorf(" expected: %#v", testCase.out) |
| 1524 | t.Errorf(" got: %#v", ctx.result) |
| 1525 | } |
| 1526 | } |
| 1527 | } |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1528 | |
| 1529 | // TODO(jungjw): Consider making this more robust by ignoring path order. |
| 1530 | func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) { |
| 1531 | variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests() |
| 1532 | flags := strings.Split(variables["javacFlags"], " ") |
| 1533 | got := "" |
| 1534 | for _, flag := range flags { |
| 1535 | keyEnd := strings.Index(flag, "=") |
| 1536 | if keyEnd > -1 && flag[:keyEnd] == "--patch-module" { |
| 1537 | got = flag[keyEnd+1:] |
| 1538 | break |
| 1539 | } |
| 1540 | } |
| 1541 | if expected != got { |
| 1542 | t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got) |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | func TestPatchModule(t *testing.T) { |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1547 | t.Run("Java language level 8", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1548 | // Test with legacy javac -source 1.8 -target 1.8 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1549 | bp := ` |
| 1550 | java_library { |
| 1551 | name: "foo", |
| 1552 | srcs: ["a.java"], |
| 1553 | java_version: "1.8", |
| 1554 | } |
| 1555 | |
| 1556 | java_library { |
| 1557 | name: "bar", |
| 1558 | srcs: ["b.java"], |
| 1559 | sdk_version: "none", |
| 1560 | system_modules: "none", |
| 1561 | patch_module: "java.base", |
| 1562 | java_version: "1.8", |
| 1563 | } |
| 1564 | |
| 1565 | java_library { |
| 1566 | name: "baz", |
| 1567 | srcs: ["c.java"], |
| 1568 | patch_module: "java.base", |
| 1569 | java_version: "1.8", |
| 1570 | } |
| 1571 | ` |
| 1572 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1573 | |
| 1574 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1575 | checkPatchModuleFlag(t, ctx, "bar", "") |
| 1576 | checkPatchModuleFlag(t, ctx, "baz", "") |
| 1577 | }) |
| 1578 | |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1579 | t.Run("Java language level 9", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1580 | // Test with default javac -source 9 -target 9 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1581 | bp := ` |
| 1582 | java_library { |
| 1583 | name: "foo", |
| 1584 | srcs: ["a.java"], |
| 1585 | } |
| 1586 | |
| 1587 | java_library { |
| 1588 | name: "bar", |
| 1589 | srcs: ["b.java"], |
| 1590 | sdk_version: "none", |
| 1591 | system_modules: "none", |
| 1592 | patch_module: "java.base", |
| 1593 | } |
| 1594 | |
| 1595 | java_library { |
| 1596 | name: "baz", |
| 1597 | srcs: ["c.java"], |
| 1598 | patch_module: "java.base", |
| 1599 | } |
| 1600 | ` |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1601 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1602 | |
| 1603 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1604 | expected := "java.base=.:" + buildDir |
| 1605 | checkPatchModuleFlag(t, ctx, "bar", expected) |
Jiyong Park | 231d056 | 2019-12-18 17:28:17 +0900 | [diff] [blame] | 1606 | expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":") |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1607 | checkPatchModuleFlag(t, ctx, "baz", expected) |
| 1608 | }) |
| 1609 | } |
Paul Duffin | a7b9f42 | 2020-01-10 17:12:18 +0000 | [diff] [blame] | 1610 | |
| 1611 | func TestJavaSystemModules(t *testing.T) { |
| 1612 | ctx, _ := testJava(t, ` |
| 1613 | java_system_modules { |
| 1614 | name: "system-modules", |
| 1615 | libs: ["system-module1", "system-module2"], |
| 1616 | } |
| 1617 | java_library { |
| 1618 | name: "system-module1", |
| 1619 | srcs: ["a.java"], |
| 1620 | sdk_version: "none", |
| 1621 | system_modules: "none", |
| 1622 | } |
| 1623 | java_library { |
| 1624 | name: "system-module2", |
| 1625 | srcs: ["b.java"], |
| 1626 | sdk_version: "none", |
| 1627 | system_modules: "none", |
| 1628 | } |
| 1629 | `) |
| 1630 | |
| 1631 | // check the existence of the module |
| 1632 | systemModules := ctx.ModuleForTests("system-modules", "android_common") |
| 1633 | |
| 1634 | cmd := systemModules.Rule("jarsTosystemModules") |
| 1635 | |
| 1636 | // make sure the command compiles against the supplied modules. |
| 1637 | for _, module := range []string{"system-module1.jar", "system-module2.jar"} { |
| 1638 | if !strings.Contains(cmd.Args["classpath"], module) { |
| 1639 | t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"], |
| 1640 | module) |
| 1641 | } |
| 1642 | } |
| 1643 | } |
Paul Duffin | 90169ba | 2020-01-10 17:16:44 +0000 | [diff] [blame] | 1644 | |
| 1645 | func TestJavaSystemModulesImport(t *testing.T) { |
| 1646 | ctx, _ := testJava(t, ` |
| 1647 | java_system_modules_import { |
| 1648 | name: "system-modules", |
| 1649 | libs: ["system-module1", "system-module2"], |
| 1650 | } |
| 1651 | java_import { |
| 1652 | name: "system-module1", |
| 1653 | jars: ["a.jar"], |
| 1654 | } |
| 1655 | java_import { |
| 1656 | name: "system-module2", |
| 1657 | jars: ["b.jar"], |
| 1658 | } |
| 1659 | `) |
| 1660 | |
| 1661 | // check the existence of the module |
| 1662 | systemModules := ctx.ModuleForTests("system-modules", "android_common") |
| 1663 | |
| 1664 | cmd := systemModules.Rule("jarsTosystemModules") |
| 1665 | |
| 1666 | // make sure the command compiles against the supplied modules. |
| 1667 | for _, module := range []string{"system-module1.jar", "system-module2.jar"} { |
| 1668 | if !strings.Contains(cmd.Args["classpath"], module) { |
| 1669 | t.Errorf("system modules classpath %v does not contain %q", cmd.Args["classpath"], |
| 1670 | module) |
| 1671 | } |
| 1672 | } |
| 1673 | } |
Paul Duffin | 83a2d96 | 2019-11-19 19:44:10 +0000 | [diff] [blame] | 1674 | |
| 1675 | func TestJavaLibraryWithSystemModules(t *testing.T) { |
| 1676 | ctx, _ := testJava(t, ` |
| 1677 | java_library { |
| 1678 | name: "lib-with-source-system-modules", |
| 1679 | srcs: [ |
| 1680 | "a.java", |
| 1681 | ], |
| 1682 | sdk_version: "none", |
| 1683 | system_modules: "source-system-modules", |
| 1684 | } |
| 1685 | |
| 1686 | java_library { |
| 1687 | name: "source-jar", |
| 1688 | srcs: [ |
| 1689 | "a.java", |
| 1690 | ], |
| 1691 | } |
| 1692 | |
| 1693 | java_system_modules { |
| 1694 | name: "source-system-modules", |
| 1695 | libs: ["source-jar"], |
| 1696 | } |
| 1697 | |
| 1698 | java_library { |
| 1699 | name: "lib-with-prebuilt-system-modules", |
| 1700 | srcs: [ |
| 1701 | "a.java", |
| 1702 | ], |
| 1703 | sdk_version: "none", |
| 1704 | system_modules: "prebuilt-system-modules", |
| 1705 | } |
| 1706 | |
| 1707 | java_import { |
| 1708 | name: "prebuilt-jar", |
| 1709 | jars: ["a.jar"], |
| 1710 | } |
| 1711 | |
| 1712 | java_system_modules_import { |
| 1713 | name: "prebuilt-system-modules", |
| 1714 | libs: ["prebuilt-jar"], |
| 1715 | } |
| 1716 | `) |
| 1717 | |
| 1718 | checkBootClasspathForSystemModule(t, ctx, "lib-with-source-system-modules", "/source-jar.jar") |
| 1719 | |
| 1720 | checkBootClasspathForSystemModule(t, ctx, "lib-with-prebuilt-system-modules", "/prebuilt-jar.jar") |
| 1721 | } |
| 1722 | |
| 1723 | func checkBootClasspathForSystemModule(t *testing.T, ctx *android.TestContext, moduleName string, expectedSuffix string) { |
| 1724 | javacRule := ctx.ModuleForTests(moduleName, "android_common").Rule("javac") |
| 1725 | bootClasspath := javacRule.Args["bootClasspath"] |
| 1726 | if strings.HasPrefix(bootClasspath, "--system ") && strings.HasSuffix(bootClasspath, expectedSuffix) { |
| 1727 | t.Errorf("bootclasspath of %q must start with --system and end with %q, but was %#v.", moduleName, expectedSuffix, bootClasspath) |
| 1728 | } |
| 1729 | } |
Jiyong Park | 19604de | 2020-03-24 16:44:11 +0900 | [diff] [blame] | 1730 | |
| 1731 | func TestAidlExportIncludeDirsFromImports(t *testing.T) { |
| 1732 | ctx, _ := testJava(t, ` |
| 1733 | java_library { |
| 1734 | name: "foo", |
| 1735 | srcs: ["aidl/foo/IFoo.aidl"], |
| 1736 | libs: ["bar"], |
| 1737 | } |
| 1738 | |
| 1739 | java_import { |
| 1740 | name: "bar", |
| 1741 | jars: ["a.jar"], |
| 1742 | aidl: { |
| 1743 | export_include_dirs: ["aidl/bar"], |
| 1744 | }, |
| 1745 | } |
| 1746 | `) |
| 1747 | |
| 1748 | aidlCommand := ctx.ModuleForTests("foo", "android_common").Rule("aidl").RuleParams.Command |
| 1749 | expectedAidlFlag := "-Iaidl/bar" |
| 1750 | if !strings.Contains(aidlCommand, expectedAidlFlag) { |
| 1751 | t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag) |
| 1752 | } |
| 1753 | } |