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