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