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