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