Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
| 20 | "path/filepath" |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 21 | "reflect" |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 22 | "strconv" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 23 | "strings" |
| 24 | "testing" |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 25 | |
Jeongik Cha | 28df257 | 2019-11-11 10:46:36 +0900 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" |
| 27 | |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 28 | "android/soong/android" |
| 29 | "android/soong/cc" |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 30 | "android/soong/dexpreopt" |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 31 | "android/soong/genrule" |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | var buildDir string |
| 35 | |
| 36 | func setUp() { |
| 37 | var err error |
| 38 | buildDir, err = ioutil.TempDir("", "soong_java_test") |
| 39 | if err != nil { |
| 40 | panic(err) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func tearDown() { |
| 45 | os.RemoveAll(buildDir) |
| 46 | } |
| 47 | |
| 48 | func TestMain(m *testing.M) { |
| 49 | run := func() int { |
| 50 | setUp() |
| 51 | defer tearDown() |
| 52 | |
| 53 | return m.Run() |
| 54 | } |
| 55 | |
| 56 | os.Exit(run()) |
| 57 | } |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 58 | |
| 59 | func testConfig(env map[string]string) android.Config { |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 60 | return TestConfig(buildDir, env) |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 63 | func testContext(bp string, fs map[string][]byte) *android.TestContext { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 64 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 65 | ctx := android.NewTestArchContext() |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 66 | ctx.RegisterModuleType("android_app", AndroidAppFactory) |
| 67 | ctx.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory) |
| 68 | ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory) |
| 69 | ctx.RegisterModuleType("android_library", AndroidLibraryFactory) |
| 70 | ctx.RegisterModuleType("android_test", AndroidTestFactory) |
| 71 | ctx.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory) |
| 72 | ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory) |
| 73 | ctx.RegisterModuleType("java_binary", BinaryFactory) |
| 74 | ctx.RegisterModuleType("java_binary_host", BinaryHostFactory) |
| 75 | ctx.RegisterModuleType("java_device_for_host", DeviceForHostFactory) |
| 76 | ctx.RegisterModuleType("java_host_for_device", HostForDeviceFactory) |
| 77 | ctx.RegisterModuleType("java_library", LibraryFactory) |
| 78 | ctx.RegisterModuleType("java_library_host", LibraryHostFactory) |
| 79 | ctx.RegisterModuleType("java_test", TestFactory) |
| 80 | ctx.RegisterModuleType("java_import", ImportFactory) |
| 81 | ctx.RegisterModuleType("java_import_host", ImportFactoryHost) |
| 82 | ctx.RegisterModuleType("java_defaults", defaultsFactory) |
| 83 | ctx.RegisterModuleType("java_system_modules", SystemModulesFactory) |
| 84 | ctx.RegisterModuleType("java_genrule", genRuleFactory) |
| 85 | ctx.RegisterModuleType("java_plugin", PluginFactory) |
| 86 | ctx.RegisterModuleType("dex_import", DexImportFactory) |
| 87 | ctx.RegisterModuleType("filegroup", android.FileGroupFactory) |
| 88 | ctx.RegisterModuleType("genrule", genrule.GenRuleFactory) |
| 89 | ctx.RegisterModuleType("droiddoc", DroiddocFactory) |
| 90 | ctx.RegisterModuleType("droiddoc_host", DroiddocHostFactory) |
| 91 | ctx.RegisterModuleType("droiddoc_template", ExportedDroiddocDirFactory) |
| 92 | ctx.RegisterModuleType("prebuilt_stubs_sources", PrebuiltStubsSourcesFactory) |
| 93 | ctx.RegisterModuleType("java_sdk_library", SdkLibraryFactory) |
| 94 | ctx.RegisterModuleType("java_sdk_library_import", sdkLibraryImportFactory) |
| 95 | ctx.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory) |
| 96 | ctx.RegisterModuleType("override_android_test", OverrideAndroidTestModuleFactory) |
| 97 | ctx.RegisterModuleType("prebuilt_apis", PrebuiltApisFactory) |
Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 98 | ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators) |
| 99 | ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators) |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 100 | ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators) |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 101 | ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) { |
Inseob Kim | c0907f1 | 2019-02-08 21:00:45 +0900 | [diff] [blame] | 102 | ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel() |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 103 | }) |
Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 104 | ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators) |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 105 | ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory)) |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 106 | ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory)) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 107 | |
| 108 | // Register module types and mutators from cc needed for JNI testing |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 109 | ctx.RegisterModuleType("cc_library", cc.LibraryFactory) |
| 110 | ctx.RegisterModuleType("cc_object", cc.ObjectFactory) |
| 111 | ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory) |
| 112 | ctx.RegisterModuleType("llndk_library", cc.LlndkLibraryFactory) |
| 113 | ctx.RegisterModuleType("ndk_prebuilt_shared_stl", cc.NdkPrebuiltSharedStlFactory) |
Colin Cross | a4f0881 | 2018-10-02 22:03:40 -0700 | [diff] [blame] | 114 | ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 115 | ctx.BottomUp("link", cc.LinkageMutator).Parallel() |
| 116 | ctx.BottomUp("begin", cc.BeginMutator).Parallel() |
| 117 | }) |
| 118 | |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 119 | bp += GatherRequiredDepsForTest() |
Colin Cross | 1369cdb | 2017-09-29 17:58:17 -0700 | [diff] [blame] | 120 | |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 121 | mockFS := map[string][]byte{ |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 122 | "Android.bp": []byte(bp), |
| 123 | "a.java": nil, |
| 124 | "b.java": nil, |
| 125 | "c.java": nil, |
| 126 | "b.kt": nil, |
| 127 | "a.jar": nil, |
| 128 | "b.jar": nil, |
Jaewoong Jung | 5b425e2 | 2019-06-17 17:40:56 -0700 | [diff] [blame] | 129 | "APP_NOTICE": nil, |
| 130 | "GENRULE_NOTICE": nil, |
| 131 | "LIB_NOTICE": nil, |
| 132 | "TOOL_NOTICE": nil, |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 133 | "java-res/a/a": nil, |
| 134 | "java-res/b/b": nil, |
| 135 | "java-res2/a": nil, |
| 136 | "java-fg/a.java": nil, |
| 137 | "java-fg/b.java": nil, |
| 138 | "java-fg/c.java": nil, |
| 139 | "api/current.txt": nil, |
| 140 | "api/removed.txt": nil, |
| 141 | "api/system-current.txt": nil, |
| 142 | "api/system-removed.txt": nil, |
| 143 | "api/test-current.txt": nil, |
| 144 | "api/test-removed.txt": nil, |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 145 | "framework/aidl/a.aidl": nil, |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 146 | |
Jaewoong Jung | 710756a | 2019-06-04 11:53:47 -0700 | [diff] [blame] | 147 | "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so": nil, |
| 148 | |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 149 | "prebuilts/sdk/14/public/android.jar": nil, |
| 150 | "prebuilts/sdk/14/public/framework.aidl": nil, |
| 151 | "prebuilts/sdk/14/system/android.jar": nil, |
Colin Cross | 98fd574 | 2019-01-09 23:04:25 -0800 | [diff] [blame] | 152 | "prebuilts/sdk/17/public/android.jar": nil, |
| 153 | "prebuilts/sdk/17/public/framework.aidl": nil, |
| 154 | "prebuilts/sdk/17/system/android.jar": nil, |
Colin Cross | 74362a4 | 2019-10-28 08:05:02 -0700 | [diff] [blame] | 155 | "prebuilts/sdk/29/public/android.jar": nil, |
| 156 | "prebuilts/sdk/29/public/framework.aidl": nil, |
| 157 | "prebuilts/sdk/29/system/android.jar": nil, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 158 | "prebuilts/sdk/current/core/android.jar": nil, |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 159 | "prebuilts/sdk/current/public/android.jar": nil, |
| 160 | "prebuilts/sdk/current/public/framework.aidl": nil, |
| 161 | "prebuilts/sdk/current/public/core.jar": nil, |
| 162 | "prebuilts/sdk/current/system/android.jar": nil, |
| 163 | "prebuilts/sdk/current/test/android.jar": nil, |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 164 | "prebuilts/sdk/28/public/api/foo.txt": nil, |
| 165 | "prebuilts/sdk/28/system/api/foo.txt": nil, |
| 166 | "prebuilts/sdk/28/test/api/foo.txt": nil, |
| 167 | "prebuilts/sdk/28/public/api/foo-removed.txt": nil, |
| 168 | "prebuilts/sdk/28/system/api/foo-removed.txt": nil, |
| 169 | "prebuilts/sdk/28/test/api/foo-removed.txt": nil, |
| 170 | "prebuilts/sdk/28/public/api/bar.txt": nil, |
| 171 | "prebuilts/sdk/28/system/api/bar.txt": nil, |
| 172 | "prebuilts/sdk/28/test/api/bar.txt": nil, |
| 173 | "prebuilts/sdk/28/public/api/bar-removed.txt": nil, |
| 174 | "prebuilts/sdk/28/system/api/bar-removed.txt": nil, |
| 175 | "prebuilts/sdk/28/test/api/bar-removed.txt": nil, |
Colin Cross | 86a60ae | 2018-05-29 14:44:55 -0700 | [diff] [blame] | 176 | "prebuilts/sdk/tools/core-lambda-stubs.jar": nil, |
Sundong Ahn | 27eecb9 | 2018-06-21 13:47:17 +0900 | [diff] [blame] | 177 | "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`), |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 178 | |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 179 | "prebuilts/apk/app.apk": nil, |
Jaewoong Jung | 1ce9ac6 | 2019-08-13 14:11:33 -0700 | [diff] [blame] | 180 | "prebuilts/apk/app_arm.apk": nil, |
| 181 | "prebuilts/apk/app_arm64.apk": nil, |
Jaewoong Jung | a5e5abc | 2019-04-26 14:31:50 -0700 | [diff] [blame] | 182 | "prebuilts/apk/app_xhdpi.apk": nil, |
| 183 | "prebuilts/apk/app_xxhdpi.apk": nil, |
Jaewoong Jung | ccbb393 | 2019-04-15 09:48:31 -0700 | [diff] [blame] | 184 | |
Colin Cross | 5ab4e6d | 2017-11-22 16:20:45 -0800 | [diff] [blame] | 185 | // For framework-res, which is an implicit dependency for framework |
Dan Willemsen | 412160e | 2019-04-09 21:36:26 -0700 | [diff] [blame] | 186 | "AndroidManifest.xml": nil, |
| 187 | "build/make/target/product/security/testkey": nil, |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 188 | |
| 189 | "build/soong/scripts/jar-wrapper.sh": nil, |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 190 | |
Colin Cross | 50ddcc4 | 2019-05-16 12:28:22 -0700 | [diff] [blame] | 191 | "build/make/core/verify_uses_libraries.sh": nil, |
| 192 | |
Colin Cross | 66dbc0b | 2017-12-28 12:23:20 -0800 | [diff] [blame] | 193 | "build/make/core/proguard.flags": nil, |
| 194 | "build/make/core/proguard_basic_keeps.flags": nil, |
| 195 | |
Colin Cross | 7fdd2b7 | 2018-01-02 18:14:25 -0800 | [diff] [blame] | 196 | "jdk8/jre/lib/jce.jar": nil, |
| 197 | "jdk8/jre/lib/rt.jar": nil, |
Nan Zhang | 61819ce | 2018-05-04 18:49:16 -0700 | [diff] [blame] | 198 | "jdk8/lib/tools.jar": nil, |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 199 | |
| 200 | "bar-doc/a.java": nil, |
| 201 | "bar-doc/b.java": nil, |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 202 | "bar-doc/IFoo.aidl": nil, |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 203 | "bar-doc/IBar.aidl": nil, |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 204 | "bar-doc/known_oj_tags.txt": nil, |
| 205 | "external/doclava/templates-sdk": nil, |
Jaewoong Jung | 2ad817c | 2019-01-18 14:27:16 -0800 | [diff] [blame] | 206 | |
| 207 | "cert/new_cert.x509.pem": nil, |
| 208 | "cert/new_cert.pk8": nil, |
Jaewoong Jung | b28eb5f | 2019-08-27 15:01:50 -0700 | [diff] [blame] | 209 | |
| 210 | "testdata/data": nil, |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 211 | |
| 212 | "stubs-sources/foo/Foo.java": nil, |
| 213 | "stubs/sources/foo/Foo.java": nil, |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | for k, v := range fs { |
| 217 | mockFS[k] = v |
| 218 | } |
| 219 | |
| 220 | ctx.MockFileSystem(mockFS) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 221 | |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 222 | return ctx |
| 223 | } |
| 224 | |
| 225 | func run(t *testing.T, ctx *android.TestContext, config android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 226 | t.Helper() |
Colin Cross | c28bb0b | 2019-02-25 14:20:47 -0800 | [diff] [blame] | 227 | |
| 228 | pathCtx := android.PathContextForTesting(config, nil) |
| 229 | setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
| 230 | |
| 231 | ctx.Register() |
Paul Duffin | baccf7e | 2019-06-11 11:58:30 +0100 | [diff] [blame] | 232 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 233 | android.FailIfErrored(t, errs) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 234 | _, errs = ctx.PrepareBuildActions(config) |
Logan Chien | 4203971 | 2018-03-12 16:29:17 +0800 | [diff] [blame] | 235 | android.FailIfErrored(t, errs) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 238 | 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] | 239 | t.Helper() |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 240 | return testJavaErrorWithConfig(t, pattern, bp, testConfig(nil)) |
| 241 | } |
| 242 | |
| 243 | func testJavaErrorWithConfig(t *testing.T, pattern string, bp string, config android.Config) (*android.TestContext, android.Config) { |
| 244 | t.Helper() |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 245 | ctx := testContext(bp, nil) |
| 246 | |
| 247 | pathCtx := android.PathContextForTesting(config, nil) |
| 248 | setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx)) |
| 249 | |
| 250 | ctx.Register() |
| 251 | _, errs := ctx.ParseBlueprintsFiles("Android.bp") |
| 252 | if len(errs) > 0 { |
| 253 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 254 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 255 | } |
| 256 | _, errs = ctx.PrepareBuildActions(config) |
| 257 | if len(errs) > 0 { |
| 258 | android.FailIfNoMatchingErrors(t, pattern, errs) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 259 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | t.Fatalf("missing expected error %q (0 errors are returned)", pattern) |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 263 | |
| 264 | return ctx, config |
Jeongik Cha | 538c0d0 | 2019-07-11 15:54:27 +0900 | [diff] [blame] | 265 | } |
| 266 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 267 | func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) { |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 268 | t.Helper() |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 269 | return testJavaWithConfig(t, bp, testConfig(nil)) |
| 270 | } |
| 271 | |
| 272 | func testJavaWithConfig(t *testing.T, bp string, config android.Config) (*android.TestContext, android.Config) { |
| 273 | t.Helper() |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 274 | ctx := testContext(bp, nil) |
Colin Cross | 527012a | 2017-11-30 22:56:16 -0800 | [diff] [blame] | 275 | run(t, ctx, config) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 276 | |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 277 | return ctx, config |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 280 | func moduleToPath(name string) string { |
| 281 | switch { |
| 282 | case name == `""`: |
| 283 | return name |
Colin Cross | fc3674a | 2017-09-18 17:41:52 -0700 | [diff] [blame] | 284 | case strings.HasSuffix(name, ".jar"): |
| 285 | return name |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 286 | default: |
| 287 | return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar") |
Colin Cross | 2acdae8 | 2017-09-15 19:44:24 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 291 | func TestSimple(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 292 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 293 | java_library { |
| 294 | name: "foo", |
| 295 | srcs: ["a.java"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 296 | libs: ["bar"], |
| 297 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | java_library { |
| 301 | name: "bar", |
| 302 | srcs: ["b.java"], |
| 303 | } |
| 304 | |
| 305 | java_library { |
| 306 | name: "baz", |
| 307 | srcs: ["c.java"], |
| 308 | } |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 309 | `) |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 310 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 311 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 312 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 313 | |
| 314 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 315 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 316 | } |
| 317 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 318 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 319 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 320 | bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar") |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 321 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 322 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 323 | 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] | 324 | } |
| 325 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 326 | if !strings.Contains(javac.Args["classpath"], bazTurbine) { |
| 327 | 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] | 328 | } |
| 329 | |
| 330 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 331 | 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] | 332 | } |
| 333 | } |
| 334 | |
Artur Satayev | 9cf4669 | 2019-11-26 18:08:34 +0000 | [diff] [blame^] | 335 | func TestExportedPlugins(t *testing.T) { |
| 336 | type Result struct { |
| 337 | library string |
| 338 | processors string |
| 339 | } |
| 340 | var tests = []struct { |
| 341 | name string |
| 342 | extra string |
| 343 | results []Result |
| 344 | }{ |
| 345 | { |
| 346 | name: "Exported plugin is not a direct plugin", |
| 347 | extra: `java_library { name: "exports", srcs: ["a.java"], exported_plugins: ["plugin"] }`, |
| 348 | results: []Result{{library: "exports", processors: "-proc:none"}}, |
| 349 | }, |
| 350 | { |
| 351 | name: "Exports plugin to dependee", |
| 352 | extra: ` |
| 353 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 354 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 355 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 356 | `, |
| 357 | results: []Result{ |
| 358 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 359 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 360 | }, |
| 361 | }, |
| 362 | { |
| 363 | name: "Exports plugin to android_library", |
| 364 | extra: ` |
| 365 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 366 | android_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 367 | android_library{name: "bar", srcs: ["a.java"], static_libs: ["exports"]} |
| 368 | `, |
| 369 | results: []Result{ |
| 370 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 371 | {library: "bar", processors: "-processor com.android.TestPlugin"}, |
| 372 | }, |
| 373 | }, |
| 374 | { |
| 375 | name: "Exports plugin is not propagated via transitive deps", |
| 376 | extra: ` |
| 377 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 378 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"]} |
| 379 | java_library{name: "bar", srcs: ["a.java"], static_libs: ["foo"]} |
| 380 | `, |
| 381 | results: []Result{ |
| 382 | {library: "foo", processors: "-processor com.android.TestPlugin"}, |
| 383 | {library: "bar", processors: "-proc:none"}, |
| 384 | }, |
| 385 | }, |
| 386 | { |
| 387 | name: "Exports plugin appends to plugins", |
| 388 | extra: ` |
| 389 | java_plugin{name: "plugin2", processor_class: "com.android.TestPlugin2"} |
| 390 | java_library{name: "exports", exported_plugins: ["plugin"]} |
| 391 | java_library{name: "foo", srcs: ["a.java"], libs: ["exports"], plugins: ["plugin2"]} |
| 392 | `, |
| 393 | results: []Result{ |
| 394 | {library: "foo", processors: "-processor com.android.TestPlugin,com.android.TestPlugin2"}, |
| 395 | }, |
| 396 | }, |
| 397 | } |
| 398 | |
| 399 | for _, test := range tests { |
| 400 | t.Run(test.name, func(t *testing.T) { |
| 401 | ctx, _ := testJava(t, ` |
| 402 | java_plugin { |
| 403 | name: "plugin", |
| 404 | processor_class: "com.android.TestPlugin", |
| 405 | } |
| 406 | `+test.extra) |
| 407 | |
| 408 | for _, want := range test.results { |
| 409 | javac := ctx.ModuleForTests(want.library, "android_common").Rule("javac") |
| 410 | if javac.Args["processor"] != want.processors { |
| 411 | t.Errorf("For library %v, expected %v, found %v", want.library, want.processors, javac.Args["processor"]) |
| 412 | } |
| 413 | } |
| 414 | }) |
| 415 | } |
| 416 | } |
| 417 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 418 | func TestSdkVersionByPartition(t *testing.T) { |
| 419 | 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] | 420 | java_library { |
| 421 | name: "foo", |
| 422 | srcs: ["a.java"], |
| 423 | vendor: true, |
| 424 | } |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 425 | `) |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 426 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 427 | testJava(t, ` |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 428 | java_library { |
| 429 | name: "bar", |
| 430 | srcs: ["b.java"], |
| 431 | } |
| 432 | `) |
| 433 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 434 | for _, enforce := range []bool{true, false} { |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 435 | |
Jeongik Cha | 2cc570d | 2019-10-29 15:44:45 +0900 | [diff] [blame] | 436 | config := testConfig(nil) |
| 437 | config.TestProductVariables.EnforceProductPartitionInterface = proptools.BoolPtr(enforce) |
| 438 | bp := ` |
| 439 | java_library { |
| 440 | name: "foo", |
| 441 | srcs: ["a.java"], |
| 442 | product_specific: true, |
| 443 | } |
| 444 | ` |
| 445 | if enforce { |
| 446 | testJavaErrorWithConfig(t, "sdk_version must have a value when the module is located at vendor or product", bp, config) |
| 447 | } else { |
| 448 | testJavaWithConfig(t, bp, config) |
| 449 | } |
Jeongik Cha | 6bd33c1 | 2019-06-25 16:26:18 +0900 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 453 | func TestArchSpecific(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 454 | ctx, _ := testJava(t, ` |
Colin Cross | d5934c8 | 2017-10-02 13:55:26 -0700 | [diff] [blame] | 455 | java_library { |
| 456 | name: "foo", |
| 457 | srcs: ["a.java"], |
| 458 | target: { |
| 459 | android: { |
| 460 | srcs: ["b.java"], |
| 461 | }, |
| 462 | }, |
| 463 | } |
| 464 | `) |
| 465 | |
| 466 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 467 | if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" { |
| 468 | t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs) |
| 469 | } |
| 470 | } |
| 471 | |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 472 | func TestBinary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 473 | ctx, _ := testJava(t, ` |
Colin Cross | 6b4a32d | 2017-12-05 13:42:45 -0800 | [diff] [blame] | 474 | java_library_host { |
| 475 | name: "foo", |
| 476 | srcs: ["a.java"], |
| 477 | } |
| 478 | |
| 479 | java_binary_host { |
| 480 | name: "bar", |
| 481 | srcs: ["b.java"], |
| 482 | static_libs: ["foo"], |
| 483 | } |
| 484 | `) |
| 485 | |
| 486 | buildOS := android.BuildOs.String() |
| 487 | |
| 488 | bar := ctx.ModuleForTests("bar", buildOS+"_common") |
| 489 | barJar := bar.Output("bar.jar").Output.String() |
| 490 | barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64") |
| 491 | barWrapperDeps := barWrapper.Output("bar").Implicits.Strings() |
| 492 | |
| 493 | // Test that the install binary wrapper depends on the installed jar file |
| 494 | if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar { |
| 495 | t.Errorf("expected binary wrapper implicits [%q], got %v", |
| 496 | barJar, barWrapperDeps) |
| 497 | } |
| 498 | |
| 499 | } |
| 500 | |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 501 | func TestPrebuilts(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 502 | ctx, _ := testJava(t, ` |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 503 | java_library { |
| 504 | name: "foo", |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 505 | srcs: ["a.java", ":stubs-source"], |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 506 | libs: ["bar", "sdklib"], |
Colin Cross | e8dc34a | 2017-07-19 11:22:16 -0700 | [diff] [blame] | 507 | static_libs: ["baz"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 510 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 511 | name: "bar", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 512 | jars: ["a.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 515 | java_import { |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 516 | name: "baz", |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 517 | jars: ["b.jar"], |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 518 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 519 | |
| 520 | dex_import { |
| 521 | name: "qux", |
| 522 | jars: ["b.jar"], |
| 523 | } |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 524 | |
| 525 | java_sdk_library_import { |
| 526 | name: "sdklib", |
| 527 | jars: ["b.jar"], |
| 528 | } |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 529 | |
| 530 | prebuilt_stubs_sources { |
| 531 | name: "stubs-source", |
| 532 | srcs: ["stubs/sources/**/*.java"], |
| 533 | } |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 534 | `) |
| 535 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 536 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 537 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 538 | barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output |
| 539 | bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 540 | sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 541 | |
Paul Duffin | 9154718 | 2019-11-12 19:39:36 +0000 | [diff] [blame] | 542 | inputs := []string{} |
| 543 | for _, p := range javac.BuildParams.Inputs { |
| 544 | inputs = append(inputs, p.String()) |
| 545 | } |
| 546 | |
| 547 | expected := []string{ |
| 548 | "a.java", |
| 549 | "stubs/sources/foo/Foo.java", |
| 550 | } |
| 551 | if !reflect.DeepEqual(expected, inputs) { |
| 552 | t.Errorf("foo inputs incorrect: expected %q, found %q", expected, inputs) |
| 553 | } |
| 554 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 555 | if !strings.Contains(javac.Args["classpath"], barJar.String()) { |
| 556 | 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] | 557 | } |
| 558 | |
Colin Cross | 79c7c26 | 2019-04-17 11:11:46 -0700 | [diff] [blame] | 559 | if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) { |
| 560 | t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String()) |
| 561 | } |
| 562 | |
Colin Cross | 37f6d79 | 2018-07-12 12:28:41 -0700 | [diff] [blame] | 563 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() { |
| 564 | 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] | 565 | } |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 566 | |
| 567 | ctx.ModuleForTests("qux", "android_common").Rule("Cp") |
Colin Cross | 72bb363 | 2017-07-13 16:23:21 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 570 | func TestDefaults(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 571 | ctx, _ := testJava(t, ` |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 572 | java_defaults { |
| 573 | name: "defaults", |
| 574 | srcs: ["a.java"], |
| 575 | libs: ["bar"], |
| 576 | static_libs: ["baz"], |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 577 | optimize: {enabled: false}, |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | java_library { |
| 581 | name: "foo", |
| 582 | defaults: ["defaults"], |
| 583 | } |
| 584 | |
| 585 | java_library { |
| 586 | name: "bar", |
| 587 | srcs: ["b.java"], |
| 588 | } |
| 589 | |
| 590 | java_library { |
| 591 | name: "baz", |
| 592 | srcs: ["c.java"], |
| 593 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 594 | |
| 595 | android_test { |
| 596 | name: "atestOptimize", |
| 597 | defaults: ["defaults"], |
| 598 | optimize: {enabled: true}, |
| 599 | } |
| 600 | |
| 601 | android_test { |
| 602 | name: "atestNoOptimize", |
| 603 | defaults: ["defaults"], |
| 604 | } |
| 605 | |
| 606 | android_test { |
| 607 | name: "atestDefault", |
| 608 | srcs: ["a.java"], |
| 609 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 610 | `) |
| 611 | |
Colin Cross | 4c428df | 2017-09-15 17:36:05 -0700 | [diff] [blame] | 612 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 613 | combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac") |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 614 | |
| 615 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" { |
| 616 | t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs) |
| 617 | } |
| 618 | |
Nan Zhang | ed19fc3 | 2017-10-19 13:06:22 -0700 | [diff] [blame] | 619 | barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 620 | if !strings.Contains(javac.Args["classpath"], barTurbine) { |
| 621 | 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] | 622 | } |
| 623 | |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 624 | baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String() |
Colin Cross | 0a6e007 | 2017-08-30 14:24:55 -0700 | [diff] [blame] | 625 | if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz { |
| 626 | 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] | 627 | } |
Sasha Smundak | 2057f82 | 2019-04-16 17:16:58 -0700 | [diff] [blame] | 628 | |
| 629 | atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8") |
| 630 | if atestOptimize.Output == nil { |
| 631 | t.Errorf("atestOptimize should optimize APK") |
| 632 | } |
| 633 | |
| 634 | atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8") |
| 635 | if atestNoOptimize.Output == nil { |
| 636 | t.Errorf("atestNoOptimize should not optimize APK") |
| 637 | } |
| 638 | |
| 639 | atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8") |
| 640 | if atestDefault.Output == nil { |
| 641 | t.Errorf("atestDefault should optimize APK") |
| 642 | } |
Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 645 | func TestResources(t *testing.T) { |
| 646 | var table = []struct { |
| 647 | name string |
| 648 | prop string |
| 649 | extra string |
| 650 | args string |
| 651 | }{ |
| 652 | { |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 653 | // Test that a module with java_resource_dirs includes the files |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 654 | name: "resource dirs", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 655 | prop: `java_resource_dirs: ["java-res"]`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 656 | 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] | 657 | }, |
| 658 | { |
| 659 | // Test that a module with java_resources includes the files |
| 660 | name: "resource files", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 661 | prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`, |
| 662 | args: "-C . -f java-res/a/a -f java-res/b/b", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 663 | }, |
| 664 | { |
| 665 | // Test that a module with a filegroup in java_resources includes the files with the |
| 666 | // path prefix |
| 667 | name: "resource filegroup", |
| 668 | prop: `java_resources: [":foo-res"]`, |
| 669 | extra: ` |
| 670 | filegroup { |
| 671 | name: "foo-res", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 672 | path: "java-res", |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 673 | srcs: ["java-res/a/a", "java-res/b/b"], |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 674 | }`, |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 675 | 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] | 676 | }, |
| 677 | { |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 678 | // Test that a module with wildcards in java_resource_dirs has the correct path prefixes |
| 679 | name: "wildcard dirs", |
| 680 | prop: `java_resource_dirs: ["java-res/*"]`, |
| 681 | args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b", |
| 682 | }, |
| 683 | { |
| 684 | // Test that a module exclude_java_resource_dirs excludes the files |
| 685 | name: "wildcard dirs", |
| 686 | prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`, |
| 687 | args: "-C java-res/a -f java-res/a/a", |
| 688 | }, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 689 | { |
| 690 | // Test wildcards in java_resources |
| 691 | name: "wildcard files", |
| 692 | prop: `java_resources: ["java-res/**/*"]`, |
| 693 | args: "-C . -f java-res/a/a -f java-res/b/b", |
| 694 | }, |
| 695 | { |
| 696 | // Test exclude_java_resources with java_resources |
| 697 | name: "wildcard files with exclude", |
| 698 | prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`, |
| 699 | args: "-C . -f java-res/a/a", |
| 700 | }, |
| 701 | { |
| 702 | // Test exclude_java_resources with java_resource_dirs |
| 703 | name: "resource dirs with exclude files", |
| 704 | prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`, |
| 705 | args: "-C java-res -f java-res/a/a", |
| 706 | }, |
| 707 | { |
| 708 | // Test exclude_java_resource_dirs with java_resource_dirs |
| 709 | name: "resource dirs with exclude files", |
| 710 | prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`, |
| 711 | args: "-C java-res -f java-res/a/a -f java-res/b/b", |
| 712 | }, |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | for _, test := range table { |
| 716 | t.Run(test.name, func(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 717 | ctx, _ := testJava(t, ` |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 718 | java_library { |
| 719 | name: "foo", |
| 720 | srcs: [ |
| 721 | "a.java", |
| 722 | "b.java", |
| 723 | "c.java", |
| 724 | ], |
| 725 | `+test.prop+`, |
| 726 | } |
| 727 | `+test.extra) |
| 728 | |
Colin Cross | 331a121 | 2018-08-15 20:40:52 -0700 | [diff] [blame] | 729 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
Colin Cross | 1ee2317 | 2017-10-18 14:44:18 -0700 | [diff] [blame] | 730 | fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar") |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 731 | |
| 732 | if !inList(fooRes.Output.String(), foo.Inputs.Strings()) { |
| 733 | t.Errorf("foo combined jars %v does not contain %q", |
| 734 | foo.Inputs.Strings(), fooRes.Output.String()) |
| 735 | } |
| 736 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 737 | if fooRes.Args["jarArgs"] != test.args { |
| 738 | t.Errorf("foo resource jar args %q is not %q", |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 739 | fooRes.Args["jarArgs"], test.args) |
| 740 | } |
| 741 | }) |
| 742 | } |
| 743 | } |
| 744 | |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 745 | func TestIncludeSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 746 | ctx, _ := testJava(t, ` |
Colin Cross | 0c4ce21 | 2019-05-03 15:28:19 -0700 | [diff] [blame] | 747 | java_library { |
| 748 | name: "foo", |
| 749 | srcs: [ |
| 750 | "a.java", |
| 751 | "b.java", |
| 752 | "c.java", |
| 753 | ], |
| 754 | include_srcs: true, |
| 755 | } |
| 756 | |
| 757 | java_library { |
| 758 | name: "bar", |
| 759 | srcs: [ |
| 760 | "a.java", |
| 761 | "b.java", |
| 762 | "c.java", |
| 763 | ], |
| 764 | java_resource_dirs: ["java-res"], |
| 765 | include_srcs: true, |
| 766 | } |
| 767 | `) |
| 768 | |
| 769 | // Test a library with include_srcs: true |
| 770 | foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar") |
| 771 | fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar") |
| 772 | |
| 773 | if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) { |
| 774 | t.Errorf("foo combined jars %v does not contain %q", w, g) |
| 775 | } |
| 776 | |
| 777 | if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 778 | t.Errorf("foo source jar args %q is not %q", w, g) |
| 779 | } |
| 780 | |
| 781 | // Test a library with include_srcs: true and resources |
| 782 | bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar") |
| 783 | barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar") |
| 784 | barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar") |
| 785 | barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar") |
| 786 | |
| 787 | if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 788 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 789 | } |
| 790 | |
| 791 | if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) { |
| 792 | t.Errorf("bar combined resource jars %v does not contain %q", w, g) |
| 793 | } |
| 794 | |
| 795 | if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) { |
| 796 | t.Errorf("bar combined jars %v does not contain %q", w, g) |
| 797 | } |
| 798 | |
| 799 | if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w { |
| 800 | t.Errorf("bar source jar args %q is not %q", w, g) |
| 801 | } |
| 802 | |
| 803 | if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w { |
| 804 | t.Errorf("bar resource jar args %q is not %q", w, g) |
| 805 | } |
| 806 | } |
| 807 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 808 | func TestGeneratedSources(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 809 | ctx, _ := testJava(t, ` |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 810 | java_library { |
| 811 | name: "foo", |
| 812 | srcs: [ |
| 813 | "a*.java", |
| 814 | ":gen", |
| 815 | "b*.java", |
| 816 | ], |
| 817 | } |
| 818 | |
| 819 | genrule { |
| 820 | name: "gen", |
Colin Cross | 824bee3 | 2017-11-22 17:27:51 -0800 | [diff] [blame] | 821 | tool_files: ["java-res/a"], |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 822 | out: ["gen.java"], |
| 823 | } |
| 824 | `) |
| 825 | |
| 826 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 827 | genrule := ctx.ModuleForTests("gen", "").Rule("generator") |
| 828 | |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 829 | if filepath.Base(genrule.Output.String()) != "gen.java" { |
| 830 | 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] | 831 | } |
| 832 | |
| 833 | if len(javac.Inputs) != 3 || |
| 834 | javac.Inputs[0].String() != "a.java" || |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 835 | javac.Inputs[1].String() != genrule.Output.String() || |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 836 | javac.Inputs[2].String() != "b.java" { |
| 837 | t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs) |
| 838 | } |
| 839 | } |
| 840 | |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 841 | func TestTurbine(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 842 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 843 | java_library { |
| 844 | name: "foo", |
| 845 | srcs: ["a.java"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 846 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | java_library { |
| 850 | name: "bar", |
Colin Cross | 9bc4343 | 2017-12-15 20:20:39 -0800 | [diff] [blame] | 851 | srcs: ["b.java"], |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 852 | static_libs: ["foo"], |
Jiyong Park | 2d49294 | 2018-03-05 17:44:10 +0900 | [diff] [blame] | 853 | sdk_version: "14", |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | java_library { |
| 857 | name: "baz", |
| 858 | srcs: ["c.java"], |
| 859 | libs: ["bar"], |
| 860 | sdk_version: "14", |
| 861 | } |
| 862 | `) |
| 863 | |
| 864 | fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine") |
| 865 | barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine") |
| 866 | barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac") |
| 867 | barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine") |
| 868 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 869 | |
| 870 | if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" { |
| 871 | t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs) |
| 872 | } |
| 873 | |
| 874 | fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar") |
| 875 | if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) { |
| 876 | t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar) |
| 877 | } |
| 878 | if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) { |
| 879 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar) |
| 880 | } |
| 881 | if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar { |
| 882 | t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar) |
| 883 | } |
Anton Hansson | f66efeb | 2018-04-11 13:57:30 +0100 | [diff] [blame] | 884 | if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") { |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 885 | 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] | 886 | "prebuilts/sdk/14/public/android.jar") |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
| 890 | func TestSharding(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 891 | ctx, _ := testJava(t, ` |
Nan Zhang | 61eaedb | 2017-11-02 13:28:15 -0700 | [diff] [blame] | 892 | java_library { |
| 893 | name: "bar", |
| 894 | srcs: ["a.java","b.java","c.java"], |
| 895 | javac_shard_size: 1 |
| 896 | } |
| 897 | `) |
| 898 | |
| 899 | barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar") |
| 900 | for i := 0; i < 3; i++ { |
| 901 | barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i)) |
| 902 | if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) { |
| 903 | t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar) |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 908 | func TestDroiddoc(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 909 | ctx, _ := testJava(t, ` |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 910 | droiddoc_template { |
| 911 | name: "droiddoc-templates-sdk", |
| 912 | path: ".", |
| 913 | } |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 914 | filegroup { |
| 915 | name: "bar-doc-aidl-srcs", |
| 916 | srcs: ["bar-doc/IBar.aidl"], |
| 917 | path: "bar-doc", |
| 918 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 919 | droiddoc { |
| 920 | name: "bar-doc", |
| 921 | srcs: [ |
| 922 | "bar-doc/*.java", |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 923 | "bar-doc/IFoo.aidl", |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 924 | ":bar-doc-aidl-srcs", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 925 | ], |
| 926 | exclude_srcs: [ |
| 927 | "bar-doc/b.java" |
| 928 | ], |
Dan Willemsen | cc09097 | 2018-02-26 14:33:31 -0800 | [diff] [blame] | 929 | custom_template: "droiddoc-templates-sdk", |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 930 | hdf: [ |
| 931 | "android.whichdoc offline", |
| 932 | ], |
| 933 | knowntags: [ |
| 934 | "bar-doc/known_oj_tags.txt", |
| 935 | ], |
| 936 | proofread_file: "libcore-proofread.txt", |
| 937 | todo_file: "libcore-docs-todo.html", |
| 938 | args: "-offlinemode -title \"libcore\"", |
| 939 | } |
| 940 | `) |
| 941 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 942 | barDoc := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc") |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 943 | var javaSrcs []string |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 944 | for _, i := range barDoc.Inputs { |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 945 | javaSrcs = append(javaSrcs, i.Base()) |
| 946 | } |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 947 | if len(javaSrcs) != 1 || javaSrcs[0] != "a.java" { |
| 948 | t.Errorf("inputs of bar-doc must be []string{\"a.java\"}, but was %#v.", javaSrcs) |
Jiyong Park | 2907459 | 2019-07-07 16:27:47 +0900 | [diff] [blame] | 949 | } |
| 950 | |
Colin Cross | c080617 | 2019-06-14 18:51:47 -0700 | [diff] [blame] | 951 | aidl := ctx.ModuleForTests("bar-doc", "android_common").Rule("aidl") |
| 952 | if g, w := barDoc.Implicits.Strings(), aidl.Output.String(); !inList(w, g) { |
| 953 | t.Errorf("implicits of bar-doc must contain %q, but was %q.", w, g) |
| 954 | } |
| 955 | |
| 956 | if g, w := aidl.Implicits.Strings(), []string{"bar-doc/IBar.aidl", "bar-doc/IFoo.aidl"}; !reflect.DeepEqual(w, g) { |
| 957 | t.Errorf("aidl inputs must be %q, but was %q", w, g) |
Jiyong Park | 1e44068 | 2018-05-23 18:42:04 +0900 | [diff] [blame] | 958 | } |
Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 961 | func TestJarGenrules(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 962 | ctx, _ := testJava(t, ` |
Colin Cross | 5425090 | 2017-12-05 09:28:08 -0800 | [diff] [blame] | 963 | java_library { |
| 964 | name: "foo", |
| 965 | srcs: ["a.java"], |
| 966 | } |
| 967 | |
| 968 | java_genrule { |
| 969 | name: "jargen", |
| 970 | tool_files: ["b.java"], |
| 971 | cmd: "$(location b.java) $(in) $(out)", |
| 972 | out: ["jargen.jar"], |
| 973 | srcs: [":foo"], |
| 974 | } |
| 975 | |
| 976 | java_library { |
| 977 | name: "bar", |
| 978 | static_libs: ["jargen"], |
| 979 | srcs: ["c.java"], |
| 980 | } |
| 981 | |
| 982 | java_library { |
| 983 | name: "baz", |
| 984 | libs: ["jargen"], |
| 985 | srcs: ["c.java"], |
| 986 | } |
| 987 | `) |
| 988 | |
| 989 | foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar") |
| 990 | jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar") |
| 991 | bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar") |
| 992 | baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar") |
| 993 | barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar") |
| 994 | |
| 995 | if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() { |
| 996 | t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings()) |
| 997 | } |
| 998 | |
| 999 | if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) { |
| 1000 | t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String()) |
| 1001 | } |
| 1002 | |
| 1003 | if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) { |
| 1004 | t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String()) |
| 1005 | } |
| 1006 | |
| 1007 | if len(barCombined.Inputs) != 2 || |
| 1008 | barCombined.Inputs[0].String() != bar.Output.String() || |
| 1009 | barCombined.Inputs[1].String() != jargen.Output.String() { |
| 1010 | t.Errorf("bar combined jar inputs %v is not [%q, %q]", |
| 1011 | barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String()) |
| 1012 | } |
| 1013 | } |
| 1014 | |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1015 | func TestExcludeFileGroupInSrcs(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1016 | ctx, _ := testJava(t, ` |
Nan Zhang | 27e284d | 2018-02-09 21:03:53 +0000 | [diff] [blame] | 1017 | java_library { |
| 1018 | name: "foo", |
| 1019 | srcs: ["a.java", ":foo-srcs"], |
| 1020 | exclude_srcs: ["a.java", ":foo-excludes"], |
| 1021 | } |
| 1022 | |
| 1023 | filegroup { |
| 1024 | name: "foo-srcs", |
| 1025 | srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"], |
| 1026 | } |
| 1027 | |
| 1028 | filegroup { |
| 1029 | name: "foo-excludes", |
| 1030 | srcs: ["java-fg/a.java", "java-fg/b.java"], |
| 1031 | } |
| 1032 | `) |
| 1033 | |
| 1034 | javac := ctx.ModuleForTests("foo", "android_common").Rule("javac") |
| 1035 | |
| 1036 | if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" { |
| 1037 | t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs) |
| 1038 | } |
| 1039 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1040 | |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1041 | func TestJavaLibrary(t *testing.T) { |
| 1042 | config := testConfig(nil) |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1043 | ctx := testContext("", map[string][]byte{ |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 1044 | "libcore/Android.bp": []byte(` |
| 1045 | java_library { |
| 1046 | name: "core", |
| 1047 | sdk_version: "none", |
| 1048 | system_modules: "none", |
| 1049 | }`), |
| 1050 | }) |
| 1051 | run(t, ctx, config) |
| 1052 | } |
| 1053 | |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1054 | func TestJavaSdkLibrary(t *testing.T) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 1055 | ctx, _ := testJava(t, ` |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1056 | droiddoc_template { |
| 1057 | name: "droiddoc-templates-sdk", |
| 1058 | path: ".", |
| 1059 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1060 | java_sdk_library { |
| 1061 | name: "foo", |
| 1062 | srcs: ["a.java", "b.java"], |
| 1063 | api_packages: ["foo"], |
| 1064 | } |
| 1065 | java_sdk_library { |
| 1066 | name: "bar", |
| 1067 | srcs: ["a.java", "b.java"], |
| 1068 | api_packages: ["bar"], |
| 1069 | } |
| 1070 | java_library { |
| 1071 | name: "baz", |
| 1072 | srcs: ["c.java"], |
| 1073 | libs: ["foo", "bar"], |
| 1074 | sdk_version: "system_current", |
| 1075 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1076 | java_library { |
| 1077 | name: "qux", |
| 1078 | srcs: ["c.java"], |
| 1079 | libs: ["baz"], |
| 1080 | sdk_version: "system_current", |
| 1081 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1082 | `) |
| 1083 | |
| 1084 | // check the existence of the internal modules |
| 1085 | ctx.ModuleForTests("foo", "android_common") |
| 1086 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common") |
| 1087 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1088 | ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1089 | ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common") |
| 1090 | ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1091 | ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common") |
Jaewoong Jung | b9a1151 | 2019-01-15 10:47:05 -0800 | [diff] [blame] | 1092 | ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a") |
Jiyong Park | 58c518b | 2018-05-12 22:29:12 +0900 | [diff] [blame] | 1093 | ctx.ModuleForTests("foo.api.public.28", "") |
| 1094 | ctx.ModuleForTests("foo.api.system.28", "") |
| 1095 | ctx.ModuleForTests("foo.api.test.28", "") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1096 | |
| 1097 | bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac") |
| 1098 | // tests if baz is actually linked to the stubs lib |
| 1099 | if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") { |
| 1100 | t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"], |
| 1101 | "foo.stubs.system.jar") |
| 1102 | } |
| 1103 | // ... and not to the impl lib |
Sundong Ahn | 054b19a | 2018-10-19 13:46:09 +0900 | [diff] [blame] | 1104 | if strings.Contains(bazJavac.Args["classpath"], "foo.jar") { |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1105 | 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] | 1106 | "foo.jar") |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1107 | } |
| 1108 | // test if baz is not linked to the system variant of foo |
| 1109 | if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") { |
| 1110 | t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"], |
| 1111 | "foo.stubs.jar") |
| 1112 | } |
Jiyong Park | 1be9691 | 2018-05-28 18:02:19 +0900 | [diff] [blame] | 1113 | |
| 1114 | // test if baz has exported SDK lib names foo and bar to qux |
| 1115 | qux := ctx.ModuleForTests("qux", "android_common") |
| 1116 | if quxLib, ok := qux.Module().(*Library); ok { |
| 1117 | sdkLibs := quxLib.ExportedSdkLibs() |
| 1118 | if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) { |
| 1119 | t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs) |
| 1120 | } |
| 1121 | } |
Jiyong Park | c678ad3 | 2018-04-10 13:07:10 +0900 | [diff] [blame] | 1122 | } |
Zoran Jovanovic | 8736ce2 | 2018-08-21 17:10:29 +0200 | [diff] [blame] | 1123 | |
| 1124 | var compilerFlagsTestCases = []struct { |
| 1125 | in string |
| 1126 | out bool |
| 1127 | }{ |
| 1128 | { |
| 1129 | in: "a", |
| 1130 | out: false, |
| 1131 | }, |
| 1132 | { |
| 1133 | in: "-a", |
| 1134 | out: true, |
| 1135 | }, |
| 1136 | { |
| 1137 | in: "-no-jdk", |
| 1138 | out: false, |
| 1139 | }, |
| 1140 | { |
| 1141 | in: "-no-stdlib", |
| 1142 | out: false, |
| 1143 | }, |
| 1144 | { |
| 1145 | in: "-kotlin-home", |
| 1146 | out: false, |
| 1147 | }, |
| 1148 | { |
| 1149 | in: "-kotlin-home /some/path", |
| 1150 | out: false, |
| 1151 | }, |
| 1152 | { |
| 1153 | in: "-include-runtime", |
| 1154 | out: false, |
| 1155 | }, |
| 1156 | { |
| 1157 | in: "-Xintellij-plugin-root", |
| 1158 | out: false, |
| 1159 | }, |
| 1160 | } |
| 1161 | |
| 1162 | type mockContext struct { |
| 1163 | android.ModuleContext |
| 1164 | result bool |
| 1165 | } |
| 1166 | |
| 1167 | func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) { |
| 1168 | // CheckBadCompilerFlags calls this function when the flag should be rejected |
| 1169 | ctx.result = false |
| 1170 | } |
| 1171 | |
| 1172 | func TestCompilerFlags(t *testing.T) { |
| 1173 | for _, testCase := range compilerFlagsTestCases { |
| 1174 | ctx := &mockContext{result: true} |
| 1175 | CheckKotlincFlags(ctx, []string{testCase.in}) |
| 1176 | if ctx.result != testCase.out { |
| 1177 | t.Errorf("incorrect output:") |
| 1178 | t.Errorf(" input: %#v", testCase.in) |
| 1179 | t.Errorf(" expected: %#v", testCase.out) |
| 1180 | t.Errorf(" got: %#v", ctx.result) |
| 1181 | } |
| 1182 | } |
| 1183 | } |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1184 | |
| 1185 | // TODO(jungjw): Consider making this more robust by ignoring path order. |
| 1186 | func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) { |
| 1187 | variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests() |
| 1188 | flags := strings.Split(variables["javacFlags"], " ") |
| 1189 | got := "" |
| 1190 | for _, flag := range flags { |
| 1191 | keyEnd := strings.Index(flag, "=") |
| 1192 | if keyEnd > -1 && flag[:keyEnd] == "--patch-module" { |
| 1193 | got = flag[keyEnd+1:] |
| 1194 | break |
| 1195 | } |
| 1196 | } |
| 1197 | if expected != got { |
| 1198 | t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got) |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | func TestPatchModule(t *testing.T) { |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1203 | t.Run("Java language level 8", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1204 | // Test with legacy javac -source 1.8 -target 1.8 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1205 | bp := ` |
| 1206 | java_library { |
| 1207 | name: "foo", |
| 1208 | srcs: ["a.java"], |
| 1209 | java_version: "1.8", |
| 1210 | } |
| 1211 | |
| 1212 | java_library { |
| 1213 | name: "bar", |
| 1214 | srcs: ["b.java"], |
| 1215 | sdk_version: "none", |
| 1216 | system_modules: "none", |
| 1217 | patch_module: "java.base", |
| 1218 | java_version: "1.8", |
| 1219 | } |
| 1220 | |
| 1221 | java_library { |
| 1222 | name: "baz", |
| 1223 | srcs: ["c.java"], |
| 1224 | patch_module: "java.base", |
| 1225 | java_version: "1.8", |
| 1226 | } |
| 1227 | ` |
| 1228 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1229 | |
| 1230 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1231 | checkPatchModuleFlag(t, ctx, "bar", "") |
| 1232 | checkPatchModuleFlag(t, ctx, "baz", "") |
| 1233 | }) |
| 1234 | |
Pete Gillin | 0c2143e | 2019-05-02 15:32:11 +0100 | [diff] [blame] | 1235 | t.Run("Java language level 9", func(t *testing.T) { |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1236 | // Test with default javac -source 9 -target 9 |
Pete Gillin | bdf5d71 | 2019-10-21 14:29:58 +0100 | [diff] [blame] | 1237 | bp := ` |
| 1238 | java_library { |
| 1239 | name: "foo", |
| 1240 | srcs: ["a.java"], |
| 1241 | } |
| 1242 | |
| 1243 | java_library { |
| 1244 | name: "bar", |
| 1245 | srcs: ["b.java"], |
| 1246 | sdk_version: "none", |
| 1247 | system_modules: "none", |
| 1248 | patch_module: "java.base", |
| 1249 | } |
| 1250 | |
| 1251 | java_library { |
| 1252 | name: "baz", |
| 1253 | srcs: ["c.java"], |
| 1254 | patch_module: "java.base", |
| 1255 | } |
| 1256 | ` |
Pete Gillin | 1b3370f | 2019-10-01 13:57:31 +0100 | [diff] [blame] | 1257 | ctx, _ := testJava(t, bp) |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1258 | |
| 1259 | checkPatchModuleFlag(t, ctx, "foo", "") |
| 1260 | expected := "java.base=.:" + buildDir |
| 1261 | checkPatchModuleFlag(t, ctx, "bar", expected) |
Insun Kang | 2343386 | 2019-01-24 20:46:28 +0900 | [diff] [blame] | 1262 | expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework"), moduleToPath("updatable_media_stubs")}, ":") |
Jaewoong Jung | 38e4fb2 | 2018-12-12 09:01:34 -0800 | [diff] [blame] | 1263 | checkPatchModuleFlag(t, ctx, "baz", expected) |
| 1264 | }) |
| 1265 | } |