blob: 2cbf75bc77bf5de88f5e0165e3bcadd138db21e1 [file] [log] [blame]
Brandon Lee5d45c6f2018-08-15 15:35:38 -07001// Copyright 2018 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 (
18 "reflect"
19 "testing"
20
21 "android/soong/android"
22)
23
24func TestCollectJavaLibraryPropertiesAddLibsDeps(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -080025 t.Parallel()
Spandan Das8aac9932024-07-18 23:14:13 +000026 ctx, _ := testJava(t,
27 `
28 java_library {name: "Foo"}
29 java_library {name: "Bar"}
30 java_library {
31 name: "javalib",
32 libs: ["Foo", "Bar"],
33 }
34 `)
35 module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
Cole Faustb36d31d2024-08-27 16:04:28 -070036 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070037
Spandan Das8aac9932024-07-18 23:14:13 +000038 for _, expected := range []string{"Foo", "Bar"} {
39 if !android.InList(expected, dpInfo.Deps) {
40 t.Errorf("Library.IDEInfo() Deps = %v, %v not found", dpInfo.Deps, expected)
41 }
Brandon Lee5d45c6f2018-08-15 15:35:38 -070042 }
43}
44
45func TestCollectJavaLibraryPropertiesAddStaticLibsDeps(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -080046 t.Parallel()
Spandan Das8aac9932024-07-18 23:14:13 +000047 ctx, _ := testJava(t,
48 `
49 java_library {name: "Foo"}
50 java_library {name: "Bar"}
51 java_library {
52 name: "javalib",
53 static_libs: ["Foo", "Bar"],
54 }
55 `)
56 module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
Cole Faustb36d31d2024-08-27 16:04:28 -070057 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070058
Spandan Das8aac9932024-07-18 23:14:13 +000059 for _, expected := range []string{"Foo", "Bar"} {
60 if !android.InList(expected, dpInfo.Deps) {
61 t.Errorf("Library.IDEInfo() Deps = %v, %v not found", dpInfo.Deps, expected)
62 }
Brandon Lee5d45c6f2018-08-15 15:35:38 -070063 }
64}
65
66func TestCollectJavaLibraryPropertiesAddScrs(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -080067 t.Parallel()
Cole Faustb36d31d2024-08-27 16:04:28 -070068 ctx, _ := testJava(t,
69 `
70 java_library {
71 name: "javalib",
72 srcs: ["Foo.java", "Bar.java"],
73 }
74 `)
75 module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
76 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
Brandon Lee5d45c6f2018-08-15 15:35:38 -070077
Cole Faustb36d31d2024-08-27 16:04:28 -070078 expected := []string{"Foo.java", "Bar.java"}
Brandon Lee5d45c6f2018-08-15 15:35:38 -070079 if !reflect.DeepEqual(dpInfo.Srcs, expected) {
80 t.Errorf("Library.IDEInfo() Srcs = %v, want %v", dpInfo.Srcs, expected)
81 }
82}
83
84func TestCollectJavaLibraryPropertiesAddAidlIncludeDirs(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -080085 t.Parallel()
Cole Faustb36d31d2024-08-27 16:04:28 -070086 ctx, _ := testJava(t,
87 `
88 java_library {
89 name: "javalib",
90 aidl: {
91 include_dirs: ["Foo", "Bar"],
92 },
93 }
94 `)
95 module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
96 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
97
Brandon Lee5d45c6f2018-08-15 15:35:38 -070098 expected := []string{"Foo", "Bar"}
Brandon Lee5d45c6f2018-08-15 15:35:38 -070099 if !reflect.DeepEqual(dpInfo.Aidl_include_dirs, expected) {
100 t.Errorf("Library.IDEInfo() Aidl_include_dirs = %v, want %v", dpInfo.Aidl_include_dirs, expected)
101 }
102}
103
Spandan Dasb4cd5df2024-08-08 21:57:22 +0000104func TestCollectJavaLibraryWithJarJarRules(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800105 t.Parallel()
Spandan Dasb4cd5df2024-08-08 21:57:22 +0000106 ctx, _ := testJava(t,
107 `
108 java_library {
109 name: "javalib",
110 srcs: ["foo.java"],
111 jarjar_rules: "jarjar_rules.txt",
112 }
113 `)
114 module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
Cole Faustb36d31d2024-08-27 16:04:28 -0700115 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700116
Spandan Das096b8d62024-10-08 22:41:26 +0000117 android.AssertStringEquals(t, "IdeInfo.Srcs of repackaged library should not be empty", "foo.java", dpInfo.Srcs[0])
Spandan Dasb4cd5df2024-08-08 21:57:22 +0000118 android.AssertStringEquals(t, "IdeInfo.Jar_rules of repackaged library should not be empty", "jarjar_rules.txt", dpInfo.Jarjar_rules[0])
119 if !android.SubstringInList(dpInfo.Jars, "soong/.intermediates/javalib/android_common/jarjar/turbine/javalib.jar") {
120 t.Errorf("IdeInfo.Jars of repackaged library should contain the output of jarjar-ing. All outputs: %v\n", dpInfo.Jars)
Brandon Lee5d45c6f2018-08-15 15:35:38 -0700121 }
122}
Spandan Das6e8bd1c2024-08-09 00:07:03 +0000123
124func TestCollectJavaLibraryLinkingAgainstVersionedSdk(t *testing.T) {
Colin Cross844cb6a2025-01-29 15:53:21 -0800125 t.Parallel()
Spandan Das6e8bd1c2024-08-09 00:07:03 +0000126 ctx := android.GroupFixturePreparers(
127 prepareForJavaTest,
128 FixtureWithPrebuiltApis(map[string][]string{
129 "29": {},
130 })).RunTestWithBp(t,
131 `
132 java_library {
133 name: "javalib",
134 srcs: ["foo.java"],
135 sdk_version: "29",
136 }
137 `)
138 module := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
Cole Faustb36d31d2024-08-27 16:04:28 -0700139 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey)
Spandan Das6e8bd1c2024-08-09 00:07:03 +0000140
Spandan Das6e8bd1c2024-08-09 00:07:03 +0000141 android.AssertStringListContains(t, "IdeInfo.Deps should contain versioned sdk module", dpInfo.Deps, "sdk_public_29_android")
142}
Spandan Das4f443e72024-10-17 00:04:19 +0000143
144func TestDoNotAddNoneSystemModulesToDeps(t *testing.T) {
145 ctx := android.GroupFixturePreparers(
146 prepareForJavaTest,
147 android.FixtureMergeEnv(
148 map[string]string{
149 "DISABLE_STUB_VALIDATION": "true",
150 },
151 ),
152 ).RunTestWithBp(t,
153 `
154 java_library {
155 name: "javalib",
156 srcs: ["foo.java"],
157 sdk_version: "none",
158 system_modules: "none",
159 }
160
161 java_api_library {
162 name: "javalib.stubs",
163 stubs_type: "everything",
164 api_contributions: ["javalib-current.txt"],
165 api_surface: "public",
166 system_modules: "none",
167 }
168 java_api_contribution {
169 name: "javalib-current.txt",
170 api_file: "javalib-current.txt",
171 api_surface: "public",
172 }
173 `)
174 javalib := ctx.ModuleForTests("javalib", "android_common").Module().(*Library)
175 dpInfo, _ := android.OtherModuleProvider(ctx, javalib, android.IdeInfoProviderKey)
176 android.AssertStringListDoesNotContain(t, "IdeInfo.Deps should contain not contain `none`", dpInfo.Deps, "none")
177
178 javalib_stubs := ctx.ModuleForTests("javalib.stubs", "android_common").Module().(*ApiLibrary)
179 dpInfo, _ = android.OtherModuleProvider(ctx, javalib_stubs, android.IdeInfoProviderKey)
180 android.AssertStringListDoesNotContain(t, "IdeInfo.Deps should contain not contain `none`", dpInfo.Deps, "none")
181}