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