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