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