blob: 50b1c34ce270eb9e34a0490ca33f804be448362a [file] [log] [blame]
Colin Cross72bb3632017-07-13 16:23:21 -07001// 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
15package java
16
17import (
Colin Cross72bb3632017-07-13 16:23:21 -070018 "io/ioutil"
19 "os"
20 "path/filepath"
Nan Zhang61eaedb2017-11-02 13:28:15 -070021 "strconv"
Colin Cross72bb3632017-07-13 16:23:21 -070022 "strings"
23 "testing"
Colin Cross86a60ae2018-05-29 14:44:55 -070024
Colin Crossa4f08812018-10-02 22:03:40 -070025 "android/soong/android"
26 "android/soong/cc"
Colin Crossc28bb0b2019-02-25 14:20:47 -080027 "android/soong/dexpreopt"
Colin Crossa4f08812018-10-02 22:03:40 -070028 "android/soong/genrule"
Colin Cross72bb3632017-07-13 16:23:21 -070029)
30
31var buildDir string
32
33func setUp() {
34 var err error
35 buildDir, err = ioutil.TempDir("", "soong_java_test")
36 if err != nil {
37 panic(err)
38 }
39}
40
41func tearDown() {
42 os.RemoveAll(buildDir)
43}
44
45func 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 Cross527012a2017-11-30 22:56:16 -080055
56func testConfig(env map[string]string) android.Config {
Colin Crosse4759b92019-02-15 10:37:39 -080057 return TestConfig(buildDir, env)
Colin Cross1369cdb2017-09-29 17:58:17 -070058}
59
Colin Cross527012a2017-11-30 22:56:16 -080060func testContext(config android.Config, bp string,
61 fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070062
Colin Cross4c428df2017-09-15 17:36:05 -070063 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070064 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Jaewoong Jung2ad817c2019-01-18 14:27:16 -080065 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(AndroidAppCertificateFactory))
Jaewoong Jungccbb3932019-04-15 09:48:31 -070066 ctx.RegisterModuleType("android_app_import", android.ModuleFactoryAdaptor(AndroidAppImportFactory))
Colin Crossa97c5d32018-03-28 14:58:31 -070067 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070068 ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory))
Colin Cross638149e2019-01-05 22:12:12 -080069 ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory))
70 ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080071 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080072 ctx.RegisterModuleType("java_device_for_host", android.ModuleFactoryAdaptor(DeviceForHostFactory))
73 ctx.RegisterModuleType("java_host_for_device", android.ModuleFactoryAdaptor(HostForDeviceFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070074 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070075 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross638149e2019-01-05 22:12:12 -080076 ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070077 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080078 ctx.RegisterModuleType("java_import_host", android.ModuleFactoryAdaptor(ImportFactoryHost))
Colin Cross89536d42017-07-07 14:35:50 -070079 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070080 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080081 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Crossbe9cdb82019-01-21 21:37:16 -080082 ctx.RegisterModuleType("java_plugin", android.ModuleFactoryAdaptor(PluginFactory))
Colin Cross42be7612019-02-21 18:12:14 -080083 ctx.RegisterModuleType("dex_import", android.ModuleFactoryAdaptor(DexImportFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070084 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070085 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080086 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
87 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070088 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090089 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(SdkLibraryFactory))
Colin Cross79c7c262019-04-17 11:11:46 -070090 ctx.RegisterModuleType("java_sdk_library_import", android.ModuleFactoryAdaptor(sdkLibraryImportFactory))
Jaewoong Jung525443a2019-02-28 15:35:54 -080091 ctx.RegisterModuleType("override_android_app", android.ModuleFactoryAdaptor(OverrideAndroidAppModuleFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090092 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(PrebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070093 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
94 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070095 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090096 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +090097 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +090098 })
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070099 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800100 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -0700101 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700102
103 // Register module types and mutators from cc needed for JNI testing
104 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
105 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
106 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Colin Cross47fa9d32019-03-26 10:51:39 -0700107 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700108 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
109 ctx.BottomUp("link", cc.LinkageMutator).Parallel()
110 ctx.BottomUp("begin", cc.BeginMutator).Parallel()
111 })
112
Colin Crosse4759b92019-02-15 10:37:39 -0800113 bp += GatherRequiredDepsForTest()
Colin Cross1369cdb2017-09-29 17:58:17 -0700114
Colin Cross824bee32017-11-22 17:27:51 -0800115 mockFS := map[string][]byte{
Jiyong Park58c518b2018-05-12 22:29:12 +0900116 "Android.bp": []byte(bp),
117 "a.java": nil,
118 "b.java": nil,
119 "c.java": nil,
120 "b.kt": nil,
121 "a.jar": nil,
122 "b.jar": nil,
123 "java-res/a/a": nil,
124 "java-res/b/b": nil,
125 "java-res2/a": nil,
126 "java-fg/a.java": nil,
127 "java-fg/b.java": nil,
128 "java-fg/c.java": nil,
129 "api/current.txt": nil,
130 "api/removed.txt": nil,
131 "api/system-current.txt": nil,
132 "api/system-removed.txt": nil,
133 "api/test-current.txt": nil,
134 "api/test-removed.txt": nil,
Colin Cross3047fa22019-04-18 10:56:44 -0700135 "framework/aidl/a.aidl": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700136
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100137 "prebuilts/sdk/14/public/android.jar": nil,
138 "prebuilts/sdk/14/public/framework.aidl": nil,
139 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross98fd5742019-01-09 23:04:25 -0800140 "prebuilts/sdk/17/public/android.jar": nil,
141 "prebuilts/sdk/17/public/framework.aidl": nil,
142 "prebuilts/sdk/17/system/android.jar": nil,
Colin Crossff0daf42019-04-02 16:10:56 -0700143 "prebuilts/sdk/25/public/android.jar": nil,
144 "prebuilts/sdk/25/public/framework.aidl": nil,
145 "prebuilts/sdk/25/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700146 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100147 "prebuilts/sdk/current/public/android.jar": nil,
148 "prebuilts/sdk/current/public/framework.aidl": nil,
149 "prebuilts/sdk/current/public/core.jar": nil,
150 "prebuilts/sdk/current/system/android.jar": nil,
151 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900152 "prebuilts/sdk/28/public/api/foo.txt": nil,
153 "prebuilts/sdk/28/system/api/foo.txt": nil,
154 "prebuilts/sdk/28/test/api/foo.txt": nil,
155 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
156 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
157 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
158 "prebuilts/sdk/28/public/api/bar.txt": nil,
159 "prebuilts/sdk/28/system/api/bar.txt": nil,
160 "prebuilts/sdk/28/test/api/bar.txt": nil,
161 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
162 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
163 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700164 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900165 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800166
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700167 "prebuilts/apk/app.apk": nil,
168 "prebuilts/apk/app_xhdpi.apk": nil,
169 "prebuilts/apk/app_xxhdpi.apk": nil,
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700170
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800171 // For framework-res, which is an implicit dependency for framework
Dan Willemsen412160e2019-04-09 21:36:26 -0700172 "AndroidManifest.xml": nil,
173 "build/make/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800174
175 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800176
Colin Cross66dbc0b2017-12-28 12:23:20 -0800177 "build/make/core/proguard.flags": nil,
178 "build/make/core/proguard_basic_keeps.flags": nil,
179
Colin Cross7fdd2b72018-01-02 18:14:25 -0800180 "jdk8/jre/lib/jce.jar": nil,
181 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700182 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800183
184 "bar-doc/a.java": nil,
185 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900186 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800187 "bar-doc/known_oj_tags.txt": nil,
188 "external/doclava/templates-sdk": nil,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800189
190 "cert/new_cert.x509.pem": nil,
191 "cert/new_cert.pk8": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800192 }
193
194 for k, v := range fs {
195 mockFS[k] = v
196 }
197
198 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700199
Colin Cross527012a2017-11-30 22:56:16 -0800200 return ctx
201}
202
203func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800204 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800205
206 pathCtx := android.PathContextForTesting(config, nil)
207 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
208
209 ctx.Register()
Jiyong Park58c518b2018-05-12 22:29:12 +0900210 _, errs := ctx.ParseFileList(".", []string{"Android.bp", "prebuilts/sdk/Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +0800211 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700212 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800213 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800214}
215
216func testJava(t *testing.T, bp string) *android.TestContext {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800217 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800218 config := testConfig(nil)
219 ctx := testContext(config, bp, nil)
220 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700221
222 return ctx
223}
224
Colin Cross2acdae82017-09-15 19:44:24 -0700225func moduleToPath(name string) string {
226 switch {
227 case name == `""`:
228 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700229 case strings.HasSuffix(name, ".jar"):
230 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700231 default:
232 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700233 }
234}
235
Colin Cross72bb3632017-07-13 16:23:21 -0700236func TestSimple(t *testing.T) {
237 ctx := testJava(t, `
238 java_library {
239 name: "foo",
240 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700241 libs: ["bar"],
242 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700243 }
244
245 java_library {
246 name: "bar",
247 srcs: ["b.java"],
248 }
249
250 java_library {
251 name: "baz",
252 srcs: ["c.java"],
253 }
Colin Crossd5934c82017-10-02 13:55:26 -0700254 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700255
Colin Cross4c428df2017-09-15 17:36:05 -0700256 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700257 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700258
259 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
260 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
261 }
262
Colin Cross1ee23172017-10-18 14:44:18 -0700263 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700264 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
265 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700266
Nan Zhanged19fc32017-10-19 13:06:22 -0700267 if !strings.Contains(javac.Args["classpath"], barTurbine) {
268 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700269 }
270
Nan Zhanged19fc32017-10-19 13:06:22 -0700271 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
272 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700273 }
274
275 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
276 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700277 }
278}
279
Colin Crossd5934c82017-10-02 13:55:26 -0700280func TestArchSpecific(t *testing.T) {
281 ctx := testJava(t, `
282 java_library {
283 name: "foo",
284 srcs: ["a.java"],
285 target: {
286 android: {
287 srcs: ["b.java"],
288 },
289 },
290 }
291 `)
292
293 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
294 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
295 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
296 }
297}
298
Colin Cross6b4a32d2017-12-05 13:42:45 -0800299func TestBinary(t *testing.T) {
300 ctx := testJava(t, `
301 java_library_host {
302 name: "foo",
303 srcs: ["a.java"],
304 }
305
306 java_binary_host {
307 name: "bar",
308 srcs: ["b.java"],
309 static_libs: ["foo"],
310 }
311 `)
312
313 buildOS := android.BuildOs.String()
314
315 bar := ctx.ModuleForTests("bar", buildOS+"_common")
316 barJar := bar.Output("bar.jar").Output.String()
317 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
318 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
319
320 // Test that the install binary wrapper depends on the installed jar file
321 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
322 t.Errorf("expected binary wrapper implicits [%q], got %v",
323 barJar, barWrapperDeps)
324 }
325
326}
327
Colin Cross72bb3632017-07-13 16:23:21 -0700328func TestPrebuilts(t *testing.T) {
329 ctx := testJava(t, `
330 java_library {
331 name: "foo",
332 srcs: ["a.java"],
Colin Cross79c7c262019-04-17 11:11:46 -0700333 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700334 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700335 }
336
Colin Cross74d73e22017-08-02 11:05:49 -0700337 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700338 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700339 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700340 }
341
Colin Cross74d73e22017-08-02 11:05:49 -0700342 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700343 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700344 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700345 }
Colin Cross42be7612019-02-21 18:12:14 -0800346
347 dex_import {
348 name: "qux",
349 jars: ["b.jar"],
350 }
Colin Cross79c7c262019-04-17 11:11:46 -0700351
352 java_sdk_library_import {
353 name: "sdklib",
354 jars: ["b.jar"],
355 }
Colin Cross72bb3632017-07-13 16:23:21 -0700356 `)
357
Colin Cross4c428df2017-09-15 17:36:05 -0700358 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700359 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700360 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
361 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700362 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700363
Colin Cross37f6d792018-07-12 12:28:41 -0700364 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
365 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700366 }
367
Colin Cross79c7c262019-04-17 11:11:46 -0700368 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
369 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
370 }
371
Colin Cross37f6d792018-07-12 12:28:41 -0700372 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
373 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700374 }
Colin Cross42be7612019-02-21 18:12:14 -0800375
376 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700377}
378
Colin Cross89536d42017-07-07 14:35:50 -0700379func TestDefaults(t *testing.T) {
380 ctx := testJava(t, `
381 java_defaults {
382 name: "defaults",
383 srcs: ["a.java"],
384 libs: ["bar"],
385 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700386 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700387 }
388
389 java_library {
390 name: "foo",
391 defaults: ["defaults"],
392 }
393
394 java_library {
395 name: "bar",
396 srcs: ["b.java"],
397 }
398
399 java_library {
400 name: "baz",
401 srcs: ["c.java"],
402 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700403
404 android_test {
405 name: "atestOptimize",
406 defaults: ["defaults"],
407 optimize: {enabled: true},
408 }
409
410 android_test {
411 name: "atestNoOptimize",
412 defaults: ["defaults"],
413 }
414
415 android_test {
416 name: "atestDefault",
417 srcs: ["a.java"],
418 }
Colin Cross89536d42017-07-07 14:35:50 -0700419 `)
420
Colin Cross4c428df2017-09-15 17:36:05 -0700421 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700422 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700423
424 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
425 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
426 }
427
Nan Zhanged19fc32017-10-19 13:06:22 -0700428 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
429 if !strings.Contains(javac.Args["classpath"], barTurbine) {
430 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700431 }
432
Colin Cross1ee23172017-10-18 14:44:18 -0700433 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700434 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
435 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700436 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700437
438 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
439 if atestOptimize.Output == nil {
440 t.Errorf("atestOptimize should optimize APK")
441 }
442
443 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
444 if atestNoOptimize.Output == nil {
445 t.Errorf("atestNoOptimize should not optimize APK")
446 }
447
448 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
449 if atestDefault.Output == nil {
450 t.Errorf("atestDefault should optimize APK")
451 }
Colin Cross89536d42017-07-07 14:35:50 -0700452}
453
Colin Cross0f37af02017-09-27 17:42:05 -0700454func TestResources(t *testing.T) {
455 var table = []struct {
456 name string
457 prop string
458 extra string
459 args string
460 }{
461 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700462 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700463 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800464 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700465 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700466 },
467 {
468 // Test that a module with java_resources includes the files
469 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700470 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
471 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700472 },
473 {
474 // Test that a module with a filegroup in java_resources includes the files with the
475 // path prefix
476 name: "resource filegroup",
477 prop: `java_resources: [":foo-res"]`,
478 extra: `
479 filegroup {
480 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800481 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700482 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700483 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700484 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700485 },
486 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700487 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
488 name: "wildcard dirs",
489 prop: `java_resource_dirs: ["java-res/*"]`,
490 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
491 },
492 {
493 // Test that a module exclude_java_resource_dirs excludes the files
494 name: "wildcard dirs",
495 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
496 args: "-C java-res/a -f java-res/a/a",
497 },
Colin Crosscedd4762018-09-13 11:26:19 -0700498 {
499 // Test wildcards in java_resources
500 name: "wildcard files",
501 prop: `java_resources: ["java-res/**/*"]`,
502 args: "-C . -f java-res/a/a -f java-res/b/b",
503 },
504 {
505 // Test exclude_java_resources with java_resources
506 name: "wildcard files with exclude",
507 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
508 args: "-C . -f java-res/a/a",
509 },
510 {
511 // Test exclude_java_resources with java_resource_dirs
512 name: "resource dirs with exclude files",
513 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
514 args: "-C java-res -f java-res/a/a",
515 },
516 {
517 // Test exclude_java_resource_dirs with java_resource_dirs
518 name: "resource dirs with exclude files",
519 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
520 args: "-C java-res -f java-res/a/a -f java-res/b/b",
521 },
Colin Cross0f37af02017-09-27 17:42:05 -0700522 }
523
524 for _, test := range table {
525 t.Run(test.name, func(t *testing.T) {
526 ctx := testJava(t, `
527 java_library {
528 name: "foo",
529 srcs: [
530 "a.java",
531 "b.java",
532 "c.java",
533 ],
534 `+test.prop+`,
535 }
536 `+test.extra)
537
Colin Cross331a1212018-08-15 20:40:52 -0700538 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700539 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700540
541 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
542 t.Errorf("foo combined jars %v does not contain %q",
543 foo.Inputs.Strings(), fooRes.Output.String())
544 }
545
Colin Crossaf9c55b2017-10-03 14:50:08 -0700546 if fooRes.Args["jarArgs"] != test.args {
547 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700548 fooRes.Args["jarArgs"], test.args)
549 }
550 })
551 }
552}
553
Colin Cross0c4ce212019-05-03 15:28:19 -0700554func TestIncludeSrcs(t *testing.T) {
555 ctx := testJava(t, `
556 java_library {
557 name: "foo",
558 srcs: [
559 "a.java",
560 "b.java",
561 "c.java",
562 ],
563 include_srcs: true,
564 }
565
566 java_library {
567 name: "bar",
568 srcs: [
569 "a.java",
570 "b.java",
571 "c.java",
572 ],
573 java_resource_dirs: ["java-res"],
574 include_srcs: true,
575 }
576 `)
577
578 // Test a library with include_srcs: true
579 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
580 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
581
582 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
583 t.Errorf("foo combined jars %v does not contain %q", w, g)
584 }
585
586 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
587 t.Errorf("foo source jar args %q is not %q", w, g)
588 }
589
590 // Test a library with include_srcs: true and resources
591 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
592 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
593 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
594 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
595
596 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
597 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
598 }
599
600 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
601 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
602 }
603
604 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
605 t.Errorf("bar combined jars %v does not contain %q", w, g)
606 }
607
608 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
609 t.Errorf("bar source jar args %q is not %q", w, g)
610 }
611
612 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
613 t.Errorf("bar resource jar args %q is not %q", w, g)
614 }
615}
616
Colin Cross54190b32017-10-09 15:34:10 -0700617func TestGeneratedSources(t *testing.T) {
618 ctx := testJava(t, `
619 java_library {
620 name: "foo",
621 srcs: [
622 "a*.java",
623 ":gen",
624 "b*.java",
625 ],
626 }
627
628 genrule {
629 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800630 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700631 out: ["gen.java"],
632 }
633 `)
634
635 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
636 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
637
Colin Cross15e86d92017-10-20 15:07:08 -0700638 if filepath.Base(genrule.Output.String()) != "gen.java" {
639 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700640 }
641
642 if len(javac.Inputs) != 3 ||
643 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700644 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700645 javac.Inputs[2].String() != "b.java" {
646 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
647 }
648}
649
Nan Zhang61eaedb2017-11-02 13:28:15 -0700650func TestTurbine(t *testing.T) {
651 ctx := testJava(t, `
652 java_library {
653 name: "foo",
654 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900655 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700656 }
657
658 java_library {
659 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800660 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700661 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900662 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700663 }
664
665 java_library {
666 name: "baz",
667 srcs: ["c.java"],
668 libs: ["bar"],
669 sdk_version: "14",
670 }
671 `)
672
673 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
674 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
675 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
676 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
677 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
678
679 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
680 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
681 }
682
683 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
684 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
685 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
686 }
687 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
688 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
689 }
690 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
691 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
692 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100693 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700694 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100695 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700696 }
697}
698
699func TestSharding(t *testing.T) {
700 ctx := testJava(t, `
701 java_library {
702 name: "bar",
703 srcs: ["a.java","b.java","c.java"],
704 javac_shard_size: 1
705 }
706 `)
707
708 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
709 for i := 0; i < 3; i++ {
710 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
711 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
712 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
713 }
714 }
715}
716
Nan Zhang581fd212018-01-10 16:06:12 -0800717func TestDroiddoc(t *testing.T) {
718 ctx := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800719 droiddoc_template {
720 name: "droiddoc-templates-sdk",
721 path: ".",
722 }
Nan Zhang581fd212018-01-10 16:06:12 -0800723 droiddoc {
724 name: "bar-doc",
725 srcs: [
726 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900727 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800728 ],
729 exclude_srcs: [
730 "bar-doc/b.java"
731 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800732 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800733 hdf: [
734 "android.whichdoc offline",
735 ],
736 knowntags: [
737 "bar-doc/known_oj_tags.txt",
738 ],
739 proofread_file: "libcore-proofread.txt",
740 todo_file: "libcore-docs-todo.html",
741 args: "-offlinemode -title \"libcore\"",
742 }
743 `)
744
Nan Zhang3bb14202018-02-28 10:14:25 -0800745 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
746 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800747 if stubsJar != barDoc.Output.String() {
748 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
749 }
Jiyong Park1e440682018-05-23 18:42:04 +0900750 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
751 var javaSrcs []string
752 for _, i := range inputs {
753 javaSrcs = append(javaSrcs, i.Base())
754 }
755 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
756 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
757 }
Nan Zhang581fd212018-01-10 16:06:12 -0800758}
759
Colin Cross54250902017-12-05 09:28:08 -0800760func TestJarGenrules(t *testing.T) {
761 ctx := testJava(t, `
762 java_library {
763 name: "foo",
764 srcs: ["a.java"],
765 }
766
767 java_genrule {
768 name: "jargen",
769 tool_files: ["b.java"],
770 cmd: "$(location b.java) $(in) $(out)",
771 out: ["jargen.jar"],
772 srcs: [":foo"],
773 }
774
775 java_library {
776 name: "bar",
777 static_libs: ["jargen"],
778 srcs: ["c.java"],
779 }
780
781 java_library {
782 name: "baz",
783 libs: ["jargen"],
784 srcs: ["c.java"],
785 }
786 `)
787
788 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
789 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
790 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
791 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
792 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
793
794 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
795 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
796 }
797
798 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
799 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
800 }
801
802 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
803 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
804 }
805
806 if len(barCombined.Inputs) != 2 ||
807 barCombined.Inputs[0].String() != bar.Output.String() ||
808 barCombined.Inputs[1].String() != jargen.Output.String() {
809 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
810 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
811 }
812}
813
Nan Zhang27e284d2018-02-09 21:03:53 +0000814func TestExcludeFileGroupInSrcs(t *testing.T) {
815 ctx := testJava(t, `
816 java_library {
817 name: "foo",
818 srcs: ["a.java", ":foo-srcs"],
819 exclude_srcs: ["a.java", ":foo-excludes"],
820 }
821
822 filegroup {
823 name: "foo-srcs",
824 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
825 }
826
827 filegroup {
828 name: "foo-excludes",
829 srcs: ["java-fg/a.java", "java-fg/b.java"],
830 }
831 `)
832
833 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
834
835 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
836 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
837 }
838}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900839
840func TestJavaSdkLibrary(t *testing.T) {
841 ctx := testJava(t, `
842 droiddoc_template {
843 name: "droiddoc-templates-sdk",
844 path: ".",
845 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900846 java_sdk_library {
847 name: "foo",
848 srcs: ["a.java", "b.java"],
849 api_packages: ["foo"],
850 }
851 java_sdk_library {
852 name: "bar",
853 srcs: ["a.java", "b.java"],
854 api_packages: ["bar"],
855 }
856 java_library {
857 name: "baz",
858 srcs: ["c.java"],
859 libs: ["foo", "bar"],
860 sdk_version: "system_current",
861 }
Jiyong Park1be96912018-05-28 18:02:19 +0900862 java_library {
863 name: "qux",
864 srcs: ["c.java"],
865 libs: ["baz"],
866 sdk_version: "system_current",
867 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900868 `)
869
870 // check the existence of the internal modules
871 ctx.ModuleForTests("foo", "android_common")
872 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
873 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900874 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900875 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
876 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900877 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800878 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +0900879 ctx.ModuleForTests("foo.api.public.28", "")
880 ctx.ModuleForTests("foo.api.system.28", "")
881 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900882
883 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
884 // tests if baz is actually linked to the stubs lib
885 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
886 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
887 "foo.stubs.system.jar")
888 }
889 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +0900890 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900891 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +0900892 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900893 }
894 // test if baz is not linked to the system variant of foo
895 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
896 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
897 "foo.stubs.jar")
898 }
Jiyong Park1be96912018-05-28 18:02:19 +0900899
900 // test if baz has exported SDK lib names foo and bar to qux
901 qux := ctx.ModuleForTests("qux", "android_common")
902 if quxLib, ok := qux.Module().(*Library); ok {
903 sdkLibs := quxLib.ExportedSdkLibs()
904 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
905 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
906 }
907 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900908}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200909
910var compilerFlagsTestCases = []struct {
911 in string
912 out bool
913}{
914 {
915 in: "a",
916 out: false,
917 },
918 {
919 in: "-a",
920 out: true,
921 },
922 {
923 in: "-no-jdk",
924 out: false,
925 },
926 {
927 in: "-no-stdlib",
928 out: false,
929 },
930 {
931 in: "-kotlin-home",
932 out: false,
933 },
934 {
935 in: "-kotlin-home /some/path",
936 out: false,
937 },
938 {
939 in: "-include-runtime",
940 out: false,
941 },
942 {
943 in: "-Xintellij-plugin-root",
944 out: false,
945 },
946}
947
948type mockContext struct {
949 android.ModuleContext
950 result bool
951}
952
953func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
954 // CheckBadCompilerFlags calls this function when the flag should be rejected
955 ctx.result = false
956}
957
958func TestCompilerFlags(t *testing.T) {
959 for _, testCase := range compilerFlagsTestCases {
960 ctx := &mockContext{result: true}
961 CheckKotlincFlags(ctx, []string{testCase.in})
962 if ctx.result != testCase.out {
963 t.Errorf("incorrect output:")
964 t.Errorf(" input: %#v", testCase.in)
965 t.Errorf(" expected: %#v", testCase.out)
966 t.Errorf(" got: %#v", ctx.result)
967 }
968 }
969}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -0800970
971// TODO(jungjw): Consider making this more robust by ignoring path order.
972func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
973 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
974 flags := strings.Split(variables["javacFlags"], " ")
975 got := ""
976 for _, flag := range flags {
977 keyEnd := strings.Index(flag, "=")
978 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
979 got = flag[keyEnd+1:]
980 break
981 }
982 }
983 if expected != got {
984 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
985 }
986}
987
988func TestPatchModule(t *testing.T) {
989 bp := `
990 java_library {
991 name: "foo",
992 srcs: ["a.java"],
993 }
994
995 java_library {
996 name: "bar",
997 srcs: ["b.java"],
998 no_standard_libs: true,
999 system_modules: "none",
1000 patch_module: "java.base",
1001 }
1002
1003 java_library {
1004 name: "baz",
1005 srcs: ["c.java"],
1006 patch_module: "java.base",
1007 }
1008 `
1009
Pete Gillin0c2143e2019-05-02 15:32:11 +01001010 t.Run("Java language level 8", func(t *testing.T) {
1011 // Test default javac -source 1.8 -target 1.8
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001012 ctx := testJava(t, bp)
1013
1014 checkPatchModuleFlag(t, ctx, "foo", "")
1015 checkPatchModuleFlag(t, ctx, "bar", "")
1016 checkPatchModuleFlag(t, ctx, "baz", "")
1017 })
1018
Pete Gillin0c2143e2019-05-02 15:32:11 +01001019 t.Run("Java language level 9", func(t *testing.T) {
1020 // Test again with javac -source 9 -target 9
1021 config := testConfig(map[string]string{"EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9": "true"})
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001022 ctx := testContext(config, bp, nil)
1023 run(t, ctx, config)
1024
1025 checkPatchModuleFlag(t, ctx, "foo", "")
1026 expected := "java.base=.:" + buildDir
1027 checkPatchModuleFlag(t, ctx, "bar", expected)
1028 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
1029 checkPatchModuleFlag(t, ctx, "baz", expected)
1030 })
1031}