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