blob: 5942afe00cc2ef00ccd37f2e0487d1598a5718a1 [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
Jaewoong Jungf9a04432019-07-17 11:15:09 -070060func testContext(bp string, fs map[string][]byte) *android.TestContext {
Colin Cross72bb3632017-07-13 16:23:21 -070061
Colin Cross4c428df2017-09-15 17:36:05 -070062 ctx := android.NewTestArchContext()
Colin Cross72bb3632017-07-13 16:23:21 -070063 ctx.RegisterModuleType("android_app", android.ModuleFactoryAdaptor(AndroidAppFactory))
Jaewoong Jung2ad817c2019-01-18 14:27:16 -080064 ctx.RegisterModuleType("android_app_certificate", android.ModuleFactoryAdaptor(AndroidAppCertificateFactory))
Jaewoong Jungccbb3932019-04-15 09:48:31 -070065 ctx.RegisterModuleType("android_app_import", android.ModuleFactoryAdaptor(AndroidAppImportFactory))
Colin Crossa97c5d32018-03-28 14:58:31 -070066 ctx.RegisterModuleType("android_library", android.ModuleFactoryAdaptor(AndroidLibraryFactory))
Colin Crossa4f08812018-10-02 22:03:40 -070067 ctx.RegisterModuleType("android_test", android.ModuleFactoryAdaptor(AndroidTestFactory))
Colin Cross638149e2019-01-05 22:12:12 -080068 ctx.RegisterModuleType("android_test_helper_app", android.ModuleFactoryAdaptor(AndroidTestHelperAppFactory))
69 ctx.RegisterModuleType("java_binary", android.ModuleFactoryAdaptor(BinaryFactory))
Colin Cross6b4a32d2017-12-05 13:42:45 -080070 ctx.RegisterModuleType("java_binary_host", android.ModuleFactoryAdaptor(BinaryHostFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080071 ctx.RegisterModuleType("java_device_for_host", android.ModuleFactoryAdaptor(DeviceForHostFactory))
72 ctx.RegisterModuleType("java_host_for_device", android.ModuleFactoryAdaptor(HostForDeviceFactory))
Colin Cross9ae1b922018-06-26 17:59:05 -070073 ctx.RegisterModuleType("java_library", android.ModuleFactoryAdaptor(LibraryFactory))
Colin Cross2acdae82017-09-15 19:44:24 -070074 ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
Colin Cross638149e2019-01-05 22:12:12 -080075 ctx.RegisterModuleType("java_test", android.ModuleFactoryAdaptor(TestFactory))
Colin Cross74d73e22017-08-02 11:05:49 -070076 ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
Colin Cross3d7c9822019-03-01 13:46:24 -080077 ctx.RegisterModuleType("java_import_host", android.ModuleFactoryAdaptor(ImportFactoryHost))
Colin Cross89536d42017-07-07 14:35:50 -070078 ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
Colin Cross1369cdb2017-09-29 17:58:17 -070079 ctx.RegisterModuleType("java_system_modules", android.ModuleFactoryAdaptor(SystemModulesFactory))
Colin Cross54250902017-12-05 09:28:08 -080080 ctx.RegisterModuleType("java_genrule", android.ModuleFactoryAdaptor(genRuleFactory))
Colin Crossbe9cdb82019-01-21 21:37:16 -080081 ctx.RegisterModuleType("java_plugin", android.ModuleFactoryAdaptor(PluginFactory))
Colin Cross42be7612019-02-21 18:12:14 -080082 ctx.RegisterModuleType("dex_import", android.ModuleFactoryAdaptor(DexImportFactory))
Pirama Arumuga Nainar955dc492018-04-17 14:58:42 -070083 ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(android.FileGroupFactory))
Colin Cross54190b32017-10-09 15:34:10 -070084 ctx.RegisterModuleType("genrule", android.ModuleFactoryAdaptor(genrule.GenRuleFactory))
Nan Zhang581fd212018-01-10 16:06:12 -080085 ctx.RegisterModuleType("droiddoc", android.ModuleFactoryAdaptor(DroiddocFactory))
86 ctx.RegisterModuleType("droiddoc_host", android.ModuleFactoryAdaptor(DroiddocHostFactory))
Nan Zhangf4936b02018-08-01 15:00:28 -070087 ctx.RegisterModuleType("droiddoc_template", android.ModuleFactoryAdaptor(ExportedDroiddocDirFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090088 ctx.RegisterModuleType("java_sdk_library", android.ModuleFactoryAdaptor(SdkLibraryFactory))
Colin Cross79c7c262019-04-17 11:11:46 -070089 ctx.RegisterModuleType("java_sdk_library_import", android.ModuleFactoryAdaptor(sdkLibraryImportFactory))
Jaewoong Jung525443a2019-02-28 15:35:54 -080090 ctx.RegisterModuleType("override_android_app", android.ModuleFactoryAdaptor(OverrideAndroidAppModuleFactory))
Inseob Kimc0907f12019-02-08 21:00:45 +090091 ctx.RegisterModuleType("prebuilt_apis", android.ModuleFactoryAdaptor(PrebuiltApisFactory))
Colin Cross5ea9bcc2017-07-27 15:41:32 -070092 ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
93 ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
Colin Cross89536d42017-07-07 14:35:50 -070094 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Jiyong Parkc678ad32018-04-10 13:07:10 +090095 ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
Inseob Kimc0907f12019-02-08 21:00:45 +090096 ctx.TopDown("prebuilt_apis", PrebuiltApisMutator).Parallel()
Jiyong Parkc678ad32018-04-10 13:07:10 +090097 })
Jaewoong Jungb639a6a2019-05-10 15:16:29 -070098 ctx.PostDepsMutators(android.RegisterOverridePostDepsMutators)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080099 ctx.RegisterPreSingletonType("overlay", android.SingletonFactoryAdaptor(OverlaySingletonFactory))
Colin Cross3047fa22019-04-18 10:56:44 -0700100 ctx.RegisterPreSingletonType("sdk_versions", android.SingletonFactoryAdaptor(sdkPreSingletonFactory))
Colin Crossa4f08812018-10-02 22:03:40 -0700101
102 // Register module types and mutators from cc needed for JNI testing
103 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
104 ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
105 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(cc.ToolchainLibraryFactory))
Colin Cross47fa9d32019-03-26 10:51:39 -0700106 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
Jaewoong Jung710756a2019-06-04 11:53:47 -0700107 ctx.RegisterModuleType("ndk_prebuilt_shared_stl", android.ModuleFactoryAdaptor(cc.NdkPrebuiltSharedStlFactory))
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,
Jaewoong Jung5b425e22019-06-17 17:40:56 -0700123 "APP_NOTICE": nil,
124 "GENRULE_NOTICE": nil,
125 "LIB_NOTICE": nil,
126 "TOOL_NOTICE": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900127 "java-res/a/a": nil,
128 "java-res/b/b": nil,
129 "java-res2/a": nil,
130 "java-fg/a.java": nil,
131 "java-fg/b.java": nil,
132 "java-fg/c.java": nil,
133 "api/current.txt": nil,
134 "api/removed.txt": nil,
135 "api/system-current.txt": nil,
136 "api/system-removed.txt": nil,
137 "api/test-current.txt": nil,
138 "api/test-removed.txt": nil,
Colin Cross3047fa22019-04-18 10:56:44 -0700139 "framework/aidl/a.aidl": nil,
Colin Cross0532fb02017-10-02 16:57:40 -0700140
Jaewoong Jung710756a2019-06-04 11:53:47 -0700141 "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so": nil,
142
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100143 "prebuilts/sdk/14/public/android.jar": nil,
144 "prebuilts/sdk/14/public/framework.aidl": nil,
145 "prebuilts/sdk/14/system/android.jar": nil,
Colin Cross98fd5742019-01-09 23:04:25 -0800146 "prebuilts/sdk/17/public/android.jar": nil,
147 "prebuilts/sdk/17/public/framework.aidl": nil,
148 "prebuilts/sdk/17/system/android.jar": nil,
Colin Crossff0daf42019-04-02 16:10:56 -0700149 "prebuilts/sdk/25/public/android.jar": nil,
150 "prebuilts/sdk/25/public/framework.aidl": nil,
151 "prebuilts/sdk/25/system/android.jar": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700152 "prebuilts/sdk/current/core/android.jar": nil,
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100153 "prebuilts/sdk/current/public/android.jar": nil,
154 "prebuilts/sdk/current/public/framework.aidl": nil,
155 "prebuilts/sdk/current/public/core.jar": nil,
156 "prebuilts/sdk/current/system/android.jar": nil,
157 "prebuilts/sdk/current/test/android.jar": nil,
Jiyong Park58c518b2018-05-12 22:29:12 +0900158 "prebuilts/sdk/28/public/api/foo.txt": nil,
159 "prebuilts/sdk/28/system/api/foo.txt": nil,
160 "prebuilts/sdk/28/test/api/foo.txt": nil,
161 "prebuilts/sdk/28/public/api/foo-removed.txt": nil,
162 "prebuilts/sdk/28/system/api/foo-removed.txt": nil,
163 "prebuilts/sdk/28/test/api/foo-removed.txt": nil,
164 "prebuilts/sdk/28/public/api/bar.txt": nil,
165 "prebuilts/sdk/28/system/api/bar.txt": nil,
166 "prebuilts/sdk/28/test/api/bar.txt": nil,
167 "prebuilts/sdk/28/public/api/bar-removed.txt": nil,
168 "prebuilts/sdk/28/system/api/bar-removed.txt": nil,
169 "prebuilts/sdk/28/test/api/bar-removed.txt": nil,
Colin Cross86a60ae2018-05-29 14:44:55 -0700170 "prebuilts/sdk/tools/core-lambda-stubs.jar": nil,
Sundong Ahn27eecb92018-06-21 13:47:17 +0900171 "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "current"],}`),
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800172
Jaewoong Junga5e5abc2019-04-26 14:31:50 -0700173 "prebuilts/apk/app.apk": nil,
174 "prebuilts/apk/app_xhdpi.apk": nil,
175 "prebuilts/apk/app_xxhdpi.apk": nil,
Jaewoong Jungccbb3932019-04-15 09:48:31 -0700176
Colin Cross5ab4e6d2017-11-22 16:20:45 -0800177 // For framework-res, which is an implicit dependency for framework
Dan Willemsen412160e2019-04-09 21:36:26 -0700178 "AndroidManifest.xml": nil,
179 "build/make/target/product/security/testkey": nil,
Colin Cross6b4a32d2017-12-05 13:42:45 -0800180
181 "build/soong/scripts/jar-wrapper.sh": nil,
Colin Cross7fdd2b72018-01-02 18:14:25 -0800182
Colin Cross50ddcc42019-05-16 12:28:22 -0700183 "build/make/core/verify_uses_libraries.sh": nil,
184
Colin Cross66dbc0b2017-12-28 12:23:20 -0800185 "build/make/core/proguard.flags": nil,
186 "build/make/core/proguard_basic_keeps.flags": nil,
187
Colin Cross7fdd2b72018-01-02 18:14:25 -0800188 "jdk8/jre/lib/jce.jar": nil,
189 "jdk8/jre/lib/rt.jar": nil,
Nan Zhang61819ce2018-05-04 18:49:16 -0700190 "jdk8/lib/tools.jar": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800191
192 "bar-doc/a.java": nil,
193 "bar-doc/b.java": nil,
Jiyong Park1e440682018-05-23 18:42:04 +0900194 "bar-doc/IFoo.aidl": nil,
Nan Zhang581fd212018-01-10 16:06:12 -0800195 "bar-doc/known_oj_tags.txt": nil,
196 "external/doclava/templates-sdk": nil,
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800197
198 "cert/new_cert.x509.pem": nil,
199 "cert/new_cert.pk8": nil,
Colin Cross824bee32017-11-22 17:27:51 -0800200 }
201
202 for k, v := range fs {
203 mockFS[k] = v
204 }
205
206 ctx.MockFileSystem(mockFS)
Colin Cross72bb3632017-07-13 16:23:21 -0700207
Colin Cross527012a2017-11-30 22:56:16 -0800208 return ctx
209}
210
211func run(t *testing.T, ctx *android.TestContext, config android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800212 t.Helper()
Colin Crossc28bb0b2019-02-25 14:20:47 -0800213
214 pathCtx := android.PathContextForTesting(config, nil)
215 setDexpreoptTestGlobalConfig(config, dexpreopt.GlobalConfigForTests(pathCtx))
216
217 ctx.Register()
Paul Duffinbaccf7e2019-06-11 11:58:30 +0100218 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
Logan Chien42039712018-03-12 16:29:17 +0800219 android.FailIfErrored(t, errs)
Colin Cross72bb3632017-07-13 16:23:21 -0700220 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +0800221 android.FailIfErrored(t, errs)
Colin Cross527012a2017-11-30 22:56:16 -0800222}
223
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700224func testJava(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross6b4a32d2017-12-05 13:42:45 -0800225 t.Helper()
Colin Cross527012a2017-11-30 22:56:16 -0800226 config := testConfig(nil)
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700227 ctx := testContext(bp, nil)
Colin Cross527012a2017-11-30 22:56:16 -0800228 run(t, ctx, config)
Colin Cross72bb3632017-07-13 16:23:21 -0700229
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700230 return ctx, config
Colin Cross72bb3632017-07-13 16:23:21 -0700231}
232
Colin Cross2acdae82017-09-15 19:44:24 -0700233func moduleToPath(name string) string {
234 switch {
235 case name == `""`:
236 return name
Colin Crossfc3674a2017-09-18 17:41:52 -0700237 case strings.HasSuffix(name, ".jar"):
238 return name
Nan Zhanged19fc32017-10-19 13:06:22 -0700239 default:
240 return filepath.Join(buildDir, ".intermediates", name, "android_common", "turbine-combined", name+".jar")
Colin Cross2acdae82017-09-15 19:44:24 -0700241 }
242}
243
Colin Cross72bb3632017-07-13 16:23:21 -0700244func TestSimple(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700245 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700246 java_library {
247 name: "foo",
248 srcs: ["a.java"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700249 libs: ["bar"],
250 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700251 }
252
253 java_library {
254 name: "bar",
255 srcs: ["b.java"],
256 }
257
258 java_library {
259 name: "baz",
260 srcs: ["c.java"],
261 }
Colin Crossd5934c82017-10-02 13:55:26 -0700262 `)
Colin Cross72bb3632017-07-13 16:23:21 -0700263
Colin Cross4c428df2017-09-15 17:36:05 -0700264 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700265 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross72bb3632017-07-13 16:23:21 -0700266
267 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
268 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
269 }
270
Colin Cross1ee23172017-10-18 14:44:18 -0700271 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Nan Zhanged19fc32017-10-19 13:06:22 -0700272 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
273 bazTurbine := filepath.Join(buildDir, ".intermediates", "baz", "android_common", "turbine-combined", "baz.jar")
Colin Cross0a6e0072017-08-30 14:24:55 -0700274
Nan Zhanged19fc32017-10-19 13:06:22 -0700275 if !strings.Contains(javac.Args["classpath"], barTurbine) {
276 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross72bb3632017-07-13 16:23:21 -0700277 }
278
Nan Zhanged19fc32017-10-19 13:06:22 -0700279 if !strings.Contains(javac.Args["classpath"], bazTurbine) {
280 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bazTurbine)
Colin Cross0a6e0072017-08-30 14:24:55 -0700281 }
282
283 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
284 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross72bb3632017-07-13 16:23:21 -0700285 }
286}
287
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900288func TestSdkVersion(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700289 ctx, _ := testJava(t, `
Jeongik Cha6bd33c12019-06-25 16:26:18 +0900290 java_library {
291 name: "foo",
292 srcs: ["a.java"],
293 vendor: true,
294 }
295
296 java_library {
297 name: "bar",
298 srcs: ["b.java"],
299 }
300 `)
301
302 foo := ctx.ModuleForTests("foo", "android_common").Module().(*Library)
303 bar := ctx.ModuleForTests("bar", "android_common").Module().(*Library)
304
305 if foo.sdkVersion() != "system_current" {
306 t.Errorf("If sdk version of vendor module is empty, it must change to system_current.")
307 }
308
309 if bar.sdkVersion() != "" {
310 t.Errorf("If sdk version of non-vendor module is empty, it keeps empty.")
311 }
312}
313
Colin Crossd5934c82017-10-02 13:55:26 -0700314func TestArchSpecific(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700315 ctx, _ := testJava(t, `
Colin Crossd5934c82017-10-02 13:55:26 -0700316 java_library {
317 name: "foo",
318 srcs: ["a.java"],
319 target: {
320 android: {
321 srcs: ["b.java"],
322 },
323 },
324 }
325 `)
326
327 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
328 if len(javac.Inputs) != 2 || javac.Inputs[0].String() != "a.java" || javac.Inputs[1].String() != "b.java" {
329 t.Errorf(`foo inputs %v != ["a.java", "b.java"]`, javac.Inputs)
330 }
331}
332
Colin Cross6b4a32d2017-12-05 13:42:45 -0800333func TestBinary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700334 ctx, _ := testJava(t, `
Colin Cross6b4a32d2017-12-05 13:42:45 -0800335 java_library_host {
336 name: "foo",
337 srcs: ["a.java"],
338 }
339
340 java_binary_host {
341 name: "bar",
342 srcs: ["b.java"],
343 static_libs: ["foo"],
344 }
345 `)
346
347 buildOS := android.BuildOs.String()
348
349 bar := ctx.ModuleForTests("bar", buildOS+"_common")
350 barJar := bar.Output("bar.jar").Output.String()
351 barWrapper := ctx.ModuleForTests("bar", buildOS+"_x86_64")
352 barWrapperDeps := barWrapper.Output("bar").Implicits.Strings()
353
354 // Test that the install binary wrapper depends on the installed jar file
355 if len(barWrapperDeps) != 1 || barWrapperDeps[0] != barJar {
356 t.Errorf("expected binary wrapper implicits [%q], got %v",
357 barJar, barWrapperDeps)
358 }
359
360}
361
Colin Cross72bb3632017-07-13 16:23:21 -0700362func TestPrebuilts(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700363 ctx, _ := testJava(t, `
Colin Cross72bb3632017-07-13 16:23:21 -0700364 java_library {
365 name: "foo",
366 srcs: ["a.java"],
Colin Cross79c7c262019-04-17 11:11:46 -0700367 libs: ["bar", "sdklib"],
Colin Crosse8dc34a2017-07-19 11:22:16 -0700368 static_libs: ["baz"],
Colin Cross72bb3632017-07-13 16:23:21 -0700369 }
370
Colin Cross74d73e22017-08-02 11:05:49 -0700371 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700372 name: "bar",
Colin Cross74d73e22017-08-02 11:05:49 -0700373 jars: ["a.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700374 }
375
Colin Cross74d73e22017-08-02 11:05:49 -0700376 java_import {
Colin Cross72bb3632017-07-13 16:23:21 -0700377 name: "baz",
Colin Cross74d73e22017-08-02 11:05:49 -0700378 jars: ["b.jar"],
Colin Cross72bb3632017-07-13 16:23:21 -0700379 }
Colin Cross42be7612019-02-21 18:12:14 -0800380
381 dex_import {
382 name: "qux",
383 jars: ["b.jar"],
384 }
Colin Cross79c7c262019-04-17 11:11:46 -0700385
386 java_sdk_library_import {
387 name: "sdklib",
388 jars: ["b.jar"],
389 }
Colin Cross72bb3632017-07-13 16:23:21 -0700390 `)
391
Colin Cross4c428df2017-09-15 17:36:05 -0700392 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700393 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross37f6d792018-07-12 12:28:41 -0700394 barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
395 bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
Colin Cross79c7c262019-04-17 11:11:46 -0700396 sdklibStubsJar := ctx.ModuleForTests("sdklib.stubs", "android_common").Rule("combineJar").Output
Colin Cross72bb3632017-07-13 16:23:21 -0700397
Colin Cross37f6d792018-07-12 12:28:41 -0700398 if !strings.Contains(javac.Args["classpath"], barJar.String()) {
399 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700400 }
401
Colin Cross79c7c262019-04-17 11:11:46 -0700402 if !strings.Contains(javac.Args["classpath"], sdklibStubsJar.String()) {
403 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], sdklibStubsJar.String())
404 }
405
Colin Cross37f6d792018-07-12 12:28:41 -0700406 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
407 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
Colin Cross72bb3632017-07-13 16:23:21 -0700408 }
Colin Cross42be7612019-02-21 18:12:14 -0800409
410 ctx.ModuleForTests("qux", "android_common").Rule("Cp")
Colin Cross72bb3632017-07-13 16:23:21 -0700411}
412
Colin Cross89536d42017-07-07 14:35:50 -0700413func TestDefaults(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700414 ctx, _ := testJava(t, `
Colin Cross89536d42017-07-07 14:35:50 -0700415 java_defaults {
416 name: "defaults",
417 srcs: ["a.java"],
418 libs: ["bar"],
419 static_libs: ["baz"],
Sasha Smundak2057f822019-04-16 17:16:58 -0700420 optimize: {enabled: false},
Colin Cross89536d42017-07-07 14:35:50 -0700421 }
422
423 java_library {
424 name: "foo",
425 defaults: ["defaults"],
426 }
427
428 java_library {
429 name: "bar",
430 srcs: ["b.java"],
431 }
432
433 java_library {
434 name: "baz",
435 srcs: ["c.java"],
436 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700437
438 android_test {
439 name: "atestOptimize",
440 defaults: ["defaults"],
441 optimize: {enabled: true},
442 }
443
444 android_test {
445 name: "atestNoOptimize",
446 defaults: ["defaults"],
447 }
448
449 android_test {
450 name: "atestDefault",
451 srcs: ["a.java"],
452 }
Colin Cross89536d42017-07-07 14:35:50 -0700453 `)
454
Colin Cross4c428df2017-09-15 17:36:05 -0700455 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
Nan Zhanged19fc32017-10-19 13:06:22 -0700456 combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
Colin Cross89536d42017-07-07 14:35:50 -0700457
458 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "a.java" {
459 t.Errorf(`foo inputs %v != ["a.java"]`, javac.Inputs)
460 }
461
Nan Zhanged19fc32017-10-19 13:06:22 -0700462 barTurbine := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
463 if !strings.Contains(javac.Args["classpath"], barTurbine) {
464 t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barTurbine)
Colin Cross89536d42017-07-07 14:35:50 -0700465 }
466
Colin Cross1ee23172017-10-18 14:44:18 -0700467 baz := ctx.ModuleForTests("baz", "android_common").Rule("javac").Output.String()
Colin Cross0a6e0072017-08-30 14:24:55 -0700468 if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != baz {
469 t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, baz)
Colin Cross89536d42017-07-07 14:35:50 -0700470 }
Sasha Smundak2057f822019-04-16 17:16:58 -0700471
472 atestOptimize := ctx.ModuleForTests("atestOptimize", "android_common").MaybeRule("r8")
473 if atestOptimize.Output == nil {
474 t.Errorf("atestOptimize should optimize APK")
475 }
476
477 atestNoOptimize := ctx.ModuleForTests("atestNoOptimize", "android_common").MaybeRule("d8")
478 if atestNoOptimize.Output == nil {
479 t.Errorf("atestNoOptimize should not optimize APK")
480 }
481
482 atestDefault := ctx.ModuleForTests("atestDefault", "android_common").MaybeRule("r8")
483 if atestDefault.Output == nil {
484 t.Errorf("atestDefault should optimize APK")
485 }
Colin Cross89536d42017-07-07 14:35:50 -0700486}
487
Colin Cross0f37af02017-09-27 17:42:05 -0700488func TestResources(t *testing.T) {
489 var table = []struct {
490 name string
491 prop string
492 extra string
493 args string
494 }{
495 {
Colin Crossaf9c55b2017-10-03 14:50:08 -0700496 // Test that a module with java_resource_dirs includes the files
Colin Cross0f37af02017-09-27 17:42:05 -0700497 name: "resource dirs",
Colin Cross824bee32017-11-22 17:27:51 -0800498 prop: `java_resource_dirs: ["java-res"]`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700499 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700500 },
501 {
502 // Test that a module with java_resources includes the files
503 name: "resource files",
Colin Cross0ead1d72018-04-10 13:07:42 -0700504 prop: `java_resources: ["java-res/a/a", "java-res/b/b"]`,
505 args: "-C . -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700506 },
507 {
508 // Test that a module with a filegroup in java_resources includes the files with the
509 // path prefix
510 name: "resource filegroup",
511 prop: `java_resources: [":foo-res"]`,
512 extra: `
513 filegroup {
514 name: "foo-res",
Colin Cross824bee32017-11-22 17:27:51 -0800515 path: "java-res",
Colin Cross0ead1d72018-04-10 13:07:42 -0700516 srcs: ["java-res/a/a", "java-res/b/b"],
Colin Cross0f37af02017-09-27 17:42:05 -0700517 }`,
Colin Cross0ead1d72018-04-10 13:07:42 -0700518 args: "-C java-res -f java-res/a/a -f java-res/b/b",
Colin Cross0f37af02017-09-27 17:42:05 -0700519 },
520 {
Colin Cross0ead1d72018-04-10 13:07:42 -0700521 // Test that a module with wildcards in java_resource_dirs has the correct path prefixes
522 name: "wildcard dirs",
523 prop: `java_resource_dirs: ["java-res/*"]`,
524 args: "-C java-res/a -f java-res/a/a -C java-res/b -f java-res/b/b",
525 },
526 {
527 // Test that a module exclude_java_resource_dirs excludes the files
528 name: "wildcard dirs",
529 prop: `java_resource_dirs: ["java-res/*"], exclude_java_resource_dirs: ["java-res/b"]`,
530 args: "-C java-res/a -f java-res/a/a",
531 },
Colin Crosscedd4762018-09-13 11:26:19 -0700532 {
533 // Test wildcards in java_resources
534 name: "wildcard files",
535 prop: `java_resources: ["java-res/**/*"]`,
536 args: "-C . -f java-res/a/a -f java-res/b/b",
537 },
538 {
539 // Test exclude_java_resources with java_resources
540 name: "wildcard files with exclude",
541 prop: `java_resources: ["java-res/**/*"], exclude_java_resources: ["java-res/b/*"]`,
542 args: "-C . -f java-res/a/a",
543 },
544 {
545 // Test exclude_java_resources with java_resource_dirs
546 name: "resource dirs with exclude files",
547 prop: `java_resource_dirs: ["java-res"], exclude_java_resources: ["java-res/b/b"]`,
548 args: "-C java-res -f java-res/a/a",
549 },
550 {
551 // Test exclude_java_resource_dirs with java_resource_dirs
552 name: "resource dirs with exclude files",
553 prop: `java_resource_dirs: ["java-res", "java-res2"], exclude_java_resource_dirs: ["java-res2"]`,
554 args: "-C java-res -f java-res/a/a -f java-res/b/b",
555 },
Colin Cross0f37af02017-09-27 17:42:05 -0700556 }
557
558 for _, test := range table {
559 t.Run(test.name, func(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700560 ctx, _ := testJava(t, `
Colin Cross0f37af02017-09-27 17:42:05 -0700561 java_library {
562 name: "foo",
563 srcs: [
564 "a.java",
565 "b.java",
566 "c.java",
567 ],
568 `+test.prop+`,
569 }
570 `+test.extra)
571
Colin Cross331a1212018-08-15 20:40:52 -0700572 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
Colin Cross1ee23172017-10-18 14:44:18 -0700573 fooRes := ctx.ModuleForTests("foo", "android_common").Output("res/foo.jar")
Colin Cross0f37af02017-09-27 17:42:05 -0700574
575 if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
576 t.Errorf("foo combined jars %v does not contain %q",
577 foo.Inputs.Strings(), fooRes.Output.String())
578 }
579
Colin Crossaf9c55b2017-10-03 14:50:08 -0700580 if fooRes.Args["jarArgs"] != test.args {
581 t.Errorf("foo resource jar args %q is not %q",
Colin Cross0f37af02017-09-27 17:42:05 -0700582 fooRes.Args["jarArgs"], test.args)
583 }
584 })
585 }
586}
587
Colin Cross0c4ce212019-05-03 15:28:19 -0700588func TestIncludeSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700589 ctx, _ := testJava(t, `
Colin Cross0c4ce212019-05-03 15:28:19 -0700590 java_library {
591 name: "foo",
592 srcs: [
593 "a.java",
594 "b.java",
595 "c.java",
596 ],
597 include_srcs: true,
598 }
599
600 java_library {
601 name: "bar",
602 srcs: [
603 "a.java",
604 "b.java",
605 "c.java",
606 ],
607 java_resource_dirs: ["java-res"],
608 include_srcs: true,
609 }
610 `)
611
612 // Test a library with include_srcs: true
613 foo := ctx.ModuleForTests("foo", "android_common").Output("withres/foo.jar")
614 fooSrcJar := ctx.ModuleForTests("foo", "android_common").Output("foo.srcjar")
615
616 if g, w := fooSrcJar.Output.String(), foo.Inputs.Strings(); !inList(g, w) {
617 t.Errorf("foo combined jars %v does not contain %q", w, g)
618 }
619
620 if g, w := fooSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
621 t.Errorf("foo source jar args %q is not %q", w, g)
622 }
623
624 // Test a library with include_srcs: true and resources
625 bar := ctx.ModuleForTests("bar", "android_common").Output("withres/bar.jar")
626 barResCombined := ctx.ModuleForTests("bar", "android_common").Output("res-combined/bar.jar")
627 barRes := ctx.ModuleForTests("bar", "android_common").Output("res/bar.jar")
628 barSrcJar := ctx.ModuleForTests("bar", "android_common").Output("bar.srcjar")
629
630 if g, w := barSrcJar.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
631 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
632 }
633
634 if g, w := barRes.Output.String(), barResCombined.Inputs.Strings(); !inList(g, w) {
635 t.Errorf("bar combined resource jars %v does not contain %q", w, g)
636 }
637
638 if g, w := barResCombined.Output.String(), bar.Inputs.Strings(); !inList(g, w) {
639 t.Errorf("bar combined jars %v does not contain %q", w, g)
640 }
641
642 if g, w := barSrcJar.Args["jarArgs"], "-C . -f a.java -f b.java -f c.java"; g != w {
643 t.Errorf("bar source jar args %q is not %q", w, g)
644 }
645
646 if g, w := barRes.Args["jarArgs"], "-C java-res -f java-res/a/a -f java-res/b/b"; g != w {
647 t.Errorf("bar resource jar args %q is not %q", w, g)
648 }
649}
650
Colin Cross54190b32017-10-09 15:34:10 -0700651func TestGeneratedSources(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700652 ctx, _ := testJava(t, `
Colin Cross54190b32017-10-09 15:34:10 -0700653 java_library {
654 name: "foo",
655 srcs: [
656 "a*.java",
657 ":gen",
658 "b*.java",
659 ],
660 }
661
662 genrule {
663 name: "gen",
Colin Cross824bee32017-11-22 17:27:51 -0800664 tool_files: ["java-res/a"],
Colin Cross54190b32017-10-09 15:34:10 -0700665 out: ["gen.java"],
666 }
667 `)
668
669 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
670 genrule := ctx.ModuleForTests("gen", "").Rule("generator")
671
Colin Cross15e86d92017-10-20 15:07:08 -0700672 if filepath.Base(genrule.Output.String()) != "gen.java" {
673 t.Fatalf(`gen output file %v is not ".../gen.java"`, genrule.Output.String())
Colin Cross54190b32017-10-09 15:34:10 -0700674 }
675
676 if len(javac.Inputs) != 3 ||
677 javac.Inputs[0].String() != "a.java" ||
Colin Cross15e86d92017-10-20 15:07:08 -0700678 javac.Inputs[1].String() != genrule.Output.String() ||
Colin Cross54190b32017-10-09 15:34:10 -0700679 javac.Inputs[2].String() != "b.java" {
680 t.Errorf(`foo inputs %v != ["a.java", ".../gen.java", "b.java"]`, javac.Inputs)
681 }
682}
683
Nan Zhang61eaedb2017-11-02 13:28:15 -0700684func TestTurbine(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700685 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700686 java_library {
687 name: "foo",
688 srcs: ["a.java"],
Jiyong Park2d492942018-03-05 17:44:10 +0900689 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700690 }
691
692 java_library {
693 name: "bar",
Colin Cross9bc43432017-12-15 20:20:39 -0800694 srcs: ["b.java"],
Nan Zhang61eaedb2017-11-02 13:28:15 -0700695 static_libs: ["foo"],
Jiyong Park2d492942018-03-05 17:44:10 +0900696 sdk_version: "14",
Nan Zhang61eaedb2017-11-02 13:28:15 -0700697 }
698
699 java_library {
700 name: "baz",
701 srcs: ["c.java"],
702 libs: ["bar"],
703 sdk_version: "14",
704 }
705 `)
706
707 fooTurbine := ctx.ModuleForTests("foo", "android_common").Rule("turbine")
708 barTurbine := ctx.ModuleForTests("bar", "android_common").Rule("turbine")
709 barJavac := ctx.ModuleForTests("bar", "android_common").Rule("javac")
710 barTurbineCombined := ctx.ModuleForTests("bar", "android_common").Description("for turbine")
711 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
712
713 if len(fooTurbine.Inputs) != 1 || fooTurbine.Inputs[0].String() != "a.java" {
714 t.Errorf(`foo inputs %v != ["a.java"]`, fooTurbine.Inputs)
715 }
716
717 fooHeaderJar := filepath.Join(buildDir, ".intermediates", "foo", "android_common", "turbine-combined", "foo.jar")
718 if !strings.Contains(barTurbine.Args["classpath"], fooHeaderJar) {
719 t.Errorf("bar turbine classpath %v does not contain %q", barTurbine.Args["classpath"], fooHeaderJar)
720 }
721 if !strings.Contains(barJavac.Args["classpath"], fooHeaderJar) {
722 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], fooHeaderJar)
723 }
724 if len(barTurbineCombined.Inputs) != 2 || barTurbineCombined.Inputs[1].String() != fooHeaderJar {
725 t.Errorf("bar turbine combineJar inputs %v does not contain %q", barTurbineCombined.Inputs, fooHeaderJar)
726 }
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100727 if !strings.Contains(bazJavac.Args["classpath"], "prebuilts/sdk/14/public/android.jar") {
Nan Zhang61eaedb2017-11-02 13:28:15 -0700728 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
Anton Hanssonf66efeb2018-04-11 13:57:30 +0100729 "prebuilts/sdk/14/public/android.jar")
Nan Zhang61eaedb2017-11-02 13:28:15 -0700730 }
731}
732
733func TestSharding(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700734 ctx, _ := testJava(t, `
Nan Zhang61eaedb2017-11-02 13:28:15 -0700735 java_library {
736 name: "bar",
737 srcs: ["a.java","b.java","c.java"],
738 javac_shard_size: 1
739 }
740 `)
741
742 barHeaderJar := filepath.Join(buildDir, ".intermediates", "bar", "android_common", "turbine-combined", "bar.jar")
743 for i := 0; i < 3; i++ {
744 barJavac := ctx.ModuleForTests("bar", "android_common").Description("javac" + strconv.Itoa(i))
745 if !strings.Contains(barJavac.Args["classpath"], barHeaderJar) {
746 t.Errorf("bar javac classpath %v does not contain %q", barJavac.Args["classpath"], barHeaderJar)
747 }
748 }
749}
750
Nan Zhang581fd212018-01-10 16:06:12 -0800751func TestDroiddoc(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700752 ctx, _ := testJava(t, `
Dan Willemsencc090972018-02-26 14:33:31 -0800753 droiddoc_template {
754 name: "droiddoc-templates-sdk",
755 path: ".",
756 }
Nan Zhang581fd212018-01-10 16:06:12 -0800757 droiddoc {
758 name: "bar-doc",
759 srcs: [
760 "bar-doc/*.java",
Jiyong Park1e440682018-05-23 18:42:04 +0900761 "bar-doc/IFoo.aidl",
Nan Zhang581fd212018-01-10 16:06:12 -0800762 ],
763 exclude_srcs: [
764 "bar-doc/b.java"
765 ],
Dan Willemsencc090972018-02-26 14:33:31 -0800766 custom_template: "droiddoc-templates-sdk",
Nan Zhang581fd212018-01-10 16:06:12 -0800767 hdf: [
768 "android.whichdoc offline",
769 ],
770 knowntags: [
771 "bar-doc/known_oj_tags.txt",
772 ],
773 proofread_file: "libcore-proofread.txt",
774 todo_file: "libcore-docs-todo.html",
775 args: "-offlinemode -title \"libcore\"",
776 }
777 `)
778
Nan Zhang3bb14202018-02-28 10:14:25 -0800779 stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
780 barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
Nan Zhang581fd212018-01-10 16:06:12 -0800781 if stubsJar != barDoc.Output.String() {
782 t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
783 }
Jiyong Park1e440682018-05-23 18:42:04 +0900784 inputs := ctx.ModuleForTests("bar-doc", "android_common").Rule("javadoc").Inputs
785 var javaSrcs []string
786 for _, i := range inputs {
787 javaSrcs = append(javaSrcs, i.Base())
788 }
789 if len(javaSrcs) != 2 || javaSrcs[0] != "a.java" || javaSrcs[1] != "IFoo.java" {
790 t.Errorf("inputs of bar-doc must be []string{\"a.java\", \"IFoo.java\", but was %#v.", javaSrcs)
791 }
Nan Zhang581fd212018-01-10 16:06:12 -0800792}
793
Colin Cross54250902017-12-05 09:28:08 -0800794func TestJarGenrules(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700795 ctx, _ := testJava(t, `
Colin Cross54250902017-12-05 09:28:08 -0800796 java_library {
797 name: "foo",
798 srcs: ["a.java"],
799 }
800
801 java_genrule {
802 name: "jargen",
803 tool_files: ["b.java"],
804 cmd: "$(location b.java) $(in) $(out)",
805 out: ["jargen.jar"],
806 srcs: [":foo"],
807 }
808
809 java_library {
810 name: "bar",
811 static_libs: ["jargen"],
812 srcs: ["c.java"],
813 }
814
815 java_library {
816 name: "baz",
817 libs: ["jargen"],
818 srcs: ["c.java"],
819 }
820 `)
821
822 foo := ctx.ModuleForTests("foo", "android_common").Output("javac/foo.jar")
823 jargen := ctx.ModuleForTests("jargen", "android_common").Output("jargen.jar")
824 bar := ctx.ModuleForTests("bar", "android_common").Output("javac/bar.jar")
825 baz := ctx.ModuleForTests("baz", "android_common").Output("javac/baz.jar")
826 barCombined := ctx.ModuleForTests("bar", "android_common").Output("combined/bar.jar")
827
828 if len(jargen.Inputs) != 1 || jargen.Inputs[0].String() != foo.Output.String() {
829 t.Errorf("expected jargen inputs [%q], got %q", foo.Output.String(), jargen.Inputs.Strings())
830 }
831
832 if !strings.Contains(bar.Args["classpath"], jargen.Output.String()) {
833 t.Errorf("bar classpath %v does not contain %q", bar.Args["classpath"], jargen.Output.String())
834 }
835
836 if !strings.Contains(baz.Args["classpath"], jargen.Output.String()) {
837 t.Errorf("baz classpath %v does not contain %q", baz.Args["classpath"], jargen.Output.String())
838 }
839
840 if len(barCombined.Inputs) != 2 ||
841 barCombined.Inputs[0].String() != bar.Output.String() ||
842 barCombined.Inputs[1].String() != jargen.Output.String() {
843 t.Errorf("bar combined jar inputs %v is not [%q, %q]",
844 barCombined.Inputs.Strings(), bar.Output.String(), jargen.Output.String())
845 }
846}
847
Nan Zhang27e284d2018-02-09 21:03:53 +0000848func TestExcludeFileGroupInSrcs(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700849 ctx, _ := testJava(t, `
Nan Zhang27e284d2018-02-09 21:03:53 +0000850 java_library {
851 name: "foo",
852 srcs: ["a.java", ":foo-srcs"],
853 exclude_srcs: ["a.java", ":foo-excludes"],
854 }
855
856 filegroup {
857 name: "foo-srcs",
858 srcs: ["java-fg/a.java", "java-fg/b.java", "java-fg/c.java"],
859 }
860
861 filegroup {
862 name: "foo-excludes",
863 srcs: ["java-fg/a.java", "java-fg/b.java"],
864 }
865 `)
866
867 javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
868
869 if len(javac.Inputs) != 1 || javac.Inputs[0].String() != "java-fg/c.java" {
870 t.Errorf(`foo inputs %v != ["java-fg/c.java"]`, javac.Inputs)
871 }
872}
Jiyong Parkc678ad32018-04-10 13:07:10 +0900873
Paul Duffin52d398a2019-06-11 12:31:14 +0100874func TestJavaLibrary(t *testing.T) {
875 config := testConfig(nil)
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700876 ctx := testContext("", map[string][]byte{
Paul Duffin52d398a2019-06-11 12:31:14 +0100877 "libcore/Android.bp": []byte(`
878 java_library {
879 name: "core",
880 sdk_version: "none",
881 system_modules: "none",
882 }`),
883 })
884 run(t, ctx, config)
885}
886
Jiyong Parkc678ad32018-04-10 13:07:10 +0900887func TestJavaSdkLibrary(t *testing.T) {
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700888 ctx, _ := testJava(t, `
Jiyong Parkc678ad32018-04-10 13:07:10 +0900889 droiddoc_template {
890 name: "droiddoc-templates-sdk",
891 path: ".",
892 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900893 java_sdk_library {
894 name: "foo",
895 srcs: ["a.java", "b.java"],
896 api_packages: ["foo"],
897 }
898 java_sdk_library {
899 name: "bar",
900 srcs: ["a.java", "b.java"],
901 api_packages: ["bar"],
902 }
903 java_library {
904 name: "baz",
905 srcs: ["c.java"],
906 libs: ["foo", "bar"],
907 sdk_version: "system_current",
908 }
Jiyong Park1be96912018-05-28 18:02:19 +0900909 java_library {
910 name: "qux",
911 srcs: ["c.java"],
912 libs: ["baz"],
913 sdk_version: "system_current",
914 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900915 `)
916
917 // check the existence of the internal modules
918 ctx.ModuleForTests("foo", "android_common")
919 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix, "android_common")
920 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900921 ctx.ModuleForTests("foo"+sdkStubsLibrarySuffix+sdkTestApiSuffix, "android_common")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900922 ctx.ModuleForTests("foo"+sdkDocsSuffix, "android_common")
923 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkSystemApiSuffix, "android_common")
Jiyong Park58c518b2018-05-12 22:29:12 +0900924 ctx.ModuleForTests("foo"+sdkDocsSuffix+sdkTestApiSuffix, "android_common")
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800925 ctx.ModuleForTests("foo"+sdkXmlFileSuffix, "android_arm64_armv8-a")
Jiyong Park58c518b2018-05-12 22:29:12 +0900926 ctx.ModuleForTests("foo.api.public.28", "")
927 ctx.ModuleForTests("foo.api.system.28", "")
928 ctx.ModuleForTests("foo.api.test.28", "")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900929
930 bazJavac := ctx.ModuleForTests("baz", "android_common").Rule("javac")
931 // tests if baz is actually linked to the stubs lib
932 if !strings.Contains(bazJavac.Args["classpath"], "foo.stubs.system.jar") {
933 t.Errorf("baz javac classpath %v does not contain %q", bazJavac.Args["classpath"],
934 "foo.stubs.system.jar")
935 }
936 // ... and not to the impl lib
Sundong Ahn054b19a2018-10-19 13:46:09 +0900937 if strings.Contains(bazJavac.Args["classpath"], "foo.jar") {
Jiyong Parkc678ad32018-04-10 13:07:10 +0900938 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
Sundong Ahn054b19a2018-10-19 13:46:09 +0900939 "foo.jar")
Jiyong Parkc678ad32018-04-10 13:07:10 +0900940 }
941 // test if baz is not linked to the system variant of foo
942 if strings.Contains(bazJavac.Args["classpath"], "foo.stubs.jar") {
943 t.Errorf("baz javac classpath %v should not contain %q", bazJavac.Args["classpath"],
944 "foo.stubs.jar")
945 }
Jiyong Park1be96912018-05-28 18:02:19 +0900946
947 // test if baz has exported SDK lib names foo and bar to qux
948 qux := ctx.ModuleForTests("qux", "android_common")
949 if quxLib, ok := qux.Module().(*Library); ok {
950 sdkLibs := quxLib.ExportedSdkLibs()
951 if len(sdkLibs) != 2 || !android.InList("foo", sdkLibs) || !android.InList("bar", sdkLibs) {
952 t.Errorf("qux should export \"foo\" and \"bar\" but exports %v", sdkLibs)
953 }
954 }
Jiyong Parkc678ad32018-04-10 13:07:10 +0900955}
Zoran Jovanovic8736ce22018-08-21 17:10:29 +0200956
957var compilerFlagsTestCases = []struct {
958 in string
959 out bool
960}{
961 {
962 in: "a",
963 out: false,
964 },
965 {
966 in: "-a",
967 out: true,
968 },
969 {
970 in: "-no-jdk",
971 out: false,
972 },
973 {
974 in: "-no-stdlib",
975 out: false,
976 },
977 {
978 in: "-kotlin-home",
979 out: false,
980 },
981 {
982 in: "-kotlin-home /some/path",
983 out: false,
984 },
985 {
986 in: "-include-runtime",
987 out: false,
988 },
989 {
990 in: "-Xintellij-plugin-root",
991 out: false,
992 },
993}
994
995type mockContext struct {
996 android.ModuleContext
997 result bool
998}
999
1000func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
1001 // CheckBadCompilerFlags calls this function when the flag should be rejected
1002 ctx.result = false
1003}
1004
1005func TestCompilerFlags(t *testing.T) {
1006 for _, testCase := range compilerFlagsTestCases {
1007 ctx := &mockContext{result: true}
1008 CheckKotlincFlags(ctx, []string{testCase.in})
1009 if ctx.result != testCase.out {
1010 t.Errorf("incorrect output:")
1011 t.Errorf(" input: %#v", testCase.in)
1012 t.Errorf(" expected: %#v", testCase.out)
1013 t.Errorf(" got: %#v", ctx.result)
1014 }
1015 }
1016}
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001017
1018// TODO(jungjw): Consider making this more robust by ignoring path order.
1019func checkPatchModuleFlag(t *testing.T, ctx *android.TestContext, moduleName string, expected string) {
1020 variables := ctx.ModuleForTests(moduleName, "android_common").Module().VariablesForTests()
1021 flags := strings.Split(variables["javacFlags"], " ")
1022 got := ""
1023 for _, flag := range flags {
1024 keyEnd := strings.Index(flag, "=")
1025 if keyEnd > -1 && flag[:keyEnd] == "--patch-module" {
1026 got = flag[keyEnd+1:]
1027 break
1028 }
1029 }
1030 if expected != got {
1031 t.Errorf("Unexpected patch-module flag for module %q - expected %q, but got %q", moduleName, expected, got)
1032 }
1033}
1034
1035func TestPatchModule(t *testing.T) {
1036 bp := `
1037 java_library {
1038 name: "foo",
1039 srcs: ["a.java"],
1040 }
1041
1042 java_library {
1043 name: "bar",
1044 srcs: ["b.java"],
Paul Duffina3d09862019-06-11 13:40:47 +01001045 sdk_version: "none",
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001046 system_modules: "none",
1047 patch_module: "java.base",
1048 }
1049
1050 java_library {
1051 name: "baz",
1052 srcs: ["c.java"],
1053 patch_module: "java.base",
1054 }
1055 `
1056
Pete Gillin0c2143e2019-05-02 15:32:11 +01001057 t.Run("Java language level 8", func(t *testing.T) {
1058 // Test default javac -source 1.8 -target 1.8
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001059 ctx, _ := testJava(t, bp)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001060
1061 checkPatchModuleFlag(t, ctx, "foo", "")
1062 checkPatchModuleFlag(t, ctx, "bar", "")
1063 checkPatchModuleFlag(t, ctx, "baz", "")
1064 })
1065
Pete Gillin0c2143e2019-05-02 15:32:11 +01001066 t.Run("Java language level 9", func(t *testing.T) {
1067 // Test again with javac -source 9 -target 9
1068 config := testConfig(map[string]string{"EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9": "true"})
Jaewoong Jungf9a04432019-07-17 11:15:09 -07001069 ctx := testContext(bp, nil)
Jaewoong Jung38e4fb22018-12-12 09:01:34 -08001070 run(t, ctx, config)
1071
1072 checkPatchModuleFlag(t, ctx, "foo", "")
1073 expected := "java.base=.:" + buildDir
1074 checkPatchModuleFlag(t, ctx, "bar", expected)
1075 expected = "java.base=" + strings.Join([]string{".", buildDir, moduleToPath("ext"), moduleToPath("framework")}, ":")
1076 checkPatchModuleFlag(t, ctx, "baz", expected)
1077 })
1078}