blob: 1d98b180de402ad66905ed31f46e9b314ed2ffb3 [file] [log] [blame]
Jooyung Han12df5fb2019-07-11 16:18:47 +09001// Copyright 2019 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 (
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070018 "reflect"
Jooyung Han12df5fb2019-07-11 16:18:47 +090019 "testing"
Jaewoong Jungf9a04432019-07-17 11:15:09 -070020
21 "android/soong/android"
Yu Shanb7646e42024-05-17 20:09:23 +000022 "android/soong/cc"
Jooyung Han12df5fb2019-07-11 16:18:47 +090023)
24
Jooyung Han12df5fb2019-07-11 16:18:47 +090025func TestRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070026 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090027 java_library {
28 name: "foo",
29 srcs: ["a.java"],
30 required: ["libfoo"],
31 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070032 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090033
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070034 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070035 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070036
37 expected := []string{"libfoo"}
38 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
39 if !reflect.DeepEqual(expected, actual) {
40 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
41 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090042}
43
44func TestHostdex(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070045 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090046 java_library {
47 name: "foo",
48 srcs: ["a.java"],
49 hostdex: true,
50 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070051 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090052
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070053 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070054 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +090055 if len(entriesList) != 2 {
56 t.Errorf("two entries are expected, but got %d", len(entriesList))
57 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070058
Jiyong Park55bd98b2019-12-11 17:27:07 +090059 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070060 expected := []string{"foo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090061 actual := mainEntries.EntryMap["LOCAL_MODULE"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070062 if !reflect.DeepEqual(expected, actual) {
63 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
64 }
65
Jiyong Park55bd98b2019-12-11 17:27:07 +090066 subEntries := &entriesList[1]
67 expected = []string{"foo-hostdex"}
68 actual = subEntries.EntryMap["LOCAL_MODULE"]
69 if !reflect.DeepEqual(expected, actual) {
70 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070071 }
Jooyung Han12df5fb2019-07-11 16:18:47 +090072}
73
74func TestHostdexRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -070075 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +090076 java_library {
77 name: "foo",
78 srcs: ["a.java"],
79 hostdex: true,
80 required: ["libfoo"],
81 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -070082 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +090083
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070084 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -070085 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +090086 if len(entriesList) != 2 {
87 t.Errorf("two entries are expected, but got %d", len(entriesList))
88 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070089
Jiyong Park55bd98b2019-12-11 17:27:07 +090090 mainEntries := &entriesList[0]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070091 expected := []string{"libfoo"}
Jiyong Park55bd98b2019-12-11 17:27:07 +090092 actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
Jaewoong Jungb0c127c2019-08-29 14:56:03 -070093 if !reflect.DeepEqual(expected, actual) {
94 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
95 }
96
Jiyong Park55bd98b2019-12-11 17:27:07 +090097 subEntries := &entriesList[1]
98 expected = []string{"libfoo"}
99 actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
100 if !reflect.DeepEqual(expected, actual) {
101 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700102 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900103}
104
105func TestHostdexSpecificRequired(t *testing.T) {
Colin Crossaa255532020-07-03 13:18:24 -0700106 ctx, _ := testJava(t, `
Jooyung Han12df5fb2019-07-11 16:18:47 +0900107 java_library {
108 name: "foo",
109 srcs: ["a.java"],
110 hostdex: true,
111 target: {
112 hostdex: {
113 required: ["libfoo"],
114 },
115 },
116 }
Jaewoong Jungf9a04432019-07-17 11:15:09 -0700117 `)
Jooyung Han12df5fb2019-07-11 16:18:47 +0900118
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700119 mod := ctx.ModuleForTests("foo", "android_common").Module()
Colin Crossaa255532020-07-03 13:18:24 -0700120 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
Jiyong Park55bd98b2019-12-11 17:27:07 +0900121 if len(entriesList) != 2 {
122 t.Errorf("two entries are expected, but got %d", len(entriesList))
123 }
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700124
Jiyong Park55bd98b2019-12-11 17:27:07 +0900125 mainEntries := &entriesList[0]
126 if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700127 t.Errorf("Unexpected required modules: %q", r)
128 }
129
Jiyong Park55bd98b2019-12-11 17:27:07 +0900130 subEntries := &entriesList[1]
131 expected := []string{"libfoo"}
132 actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
133 if !reflect.DeepEqual(expected, actual) {
134 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
Jaewoong Jungb0c127c2019-08-29 14:56:03 -0700135 }
Jooyung Han12df5fb2019-07-11 16:18:47 +0900136}
Anton Hansson78156ef2020-03-27 19:39:48 +0000137
Yo Chiang07d75072020-06-05 17:43:19 +0800138func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
Paul Duffin71ae5942021-03-22 15:36:52 +0000139 result := android.GroupFixturePreparers(
140 prepareForJavaTest,
Paul Duffin163043d2021-03-12 19:18:49 +0000141 PrepareForTestWithJavaSdkLibraryFiles,
142 FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"),
143 ).RunTestWithBp(t, `
Yo Chiang07d75072020-06-05 17:43:19 +0800144 java_sdk_library {
145 name: "foo-shared_library",
146 srcs: ["a.java"],
147 }
148 java_sdk_library {
149 name: "foo-no_shared_library",
150 srcs: ["a.java"],
151 shared_library: false,
152 }
153 `)
154
155 // Verify the existence of internal modules
Paul Duffin22b77cd2021-03-12 19:15:01 +0000156 result.ModuleForTests("foo-shared_library.xml", "android_common")
Yo Chiang07d75072020-06-05 17:43:19 +0800157
158 testCases := []struct {
159 moduleName string
160 expected []string
161 }{
Jihoon Kanga3a05462024-04-05 00:36:44 +0000162 {"foo-shared_library", []string{"foo-shared_library.impl", "foo-shared_library.xml"}},
163 {"foo-no_shared_library", []string{"foo-no_shared_library.impl"}},
Yo Chiang07d75072020-06-05 17:43:19 +0800164 }
165 for _, tc := range testCases {
Paul Duffin22b77cd2021-03-12 19:15:01 +0000166 mod := result.ModuleForTests(tc.moduleName, "android_common").Module()
167 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Yo Chiang07d75072020-06-05 17:43:19 +0800168 actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
169 if !reflect.DeepEqual(tc.expected, actual) {
170 t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
171 }
172 }
173}
Bill Peckhamfb04df42021-01-11 12:27:24 -0800174
175func TestImportSoongDexJar(t *testing.T) {
Paul Duffinf04311c2021-03-22 17:31:51 +0000176 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, `
Bill Peckhamfb04df42021-01-11 12:27:24 -0800177 java_import {
178 name: "my-java-import",
179 jars: ["a.jar"],
180 prefer: true,
181 compile_dex: true,
182 }
183 `)
184
Paul Duffinf04311c2021-03-22 17:31:51 +0000185 mod := result.Module("my-java-import", "android_common")
186 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
187 expectedSoongDexJar := "out/soong/.intermediates/my-java-import/android_common/dex/my-java-import.jar"
Bill Peckhamfb04df42021-01-11 12:27:24 -0800188 actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"]
189
Paul Duffinf04311c2021-03-22 17:31:51 +0000190 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar)
Bill Peckhamfb04df42021-01-11 12:27:24 -0800191}
Yuntao Xu7a318552021-05-27 10:30:26 -0700192
193func TestAndroidTestHelperApp_LocalDisableTestConfig(t *testing.T) {
194 ctx, _ := testJava(t, `
195 android_test_helper_app {
196 name: "foo",
197 srcs: ["a.java"],
198 }
199 `)
200
201 mod := ctx.ModuleForTests("foo", "android_common").Module()
202 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
203
204 expected := []string{"true"}
205 actual := entries.EntryMap["LOCAL_DISABLE_TEST_CONFIG"]
206 if !reflect.DeepEqual(expected, actual) {
207 t.Errorf("Unexpected flag value - expected: %q, actual: %q", expected, actual)
208 }
209}
zhidou198f5892022-02-17 02:33:12 +0000210
211func TestGetOverriddenPackages(t *testing.T) {
212 ctx, _ := testJava(
213 t, `
214 android_app {
215 name: "foo",
216 srcs: ["a.java"],
217 sdk_version: "current",
218 overrides: ["qux"]
219 }
220
221 override_android_app {
222 name: "foo_override",
223 base: "foo",
224 overrides: ["bar"]
225 }
226 `)
227
228 expectedVariants := []struct {
229 name string
230 moduleName string
231 variantName string
232 overrides []string
233 }{
234 {
235 name: "foo",
236 moduleName: "foo",
237 variantName: "android_common",
238 overrides: []string{"qux"},
239 },
240 {
241 name: "foo",
242 moduleName: "foo_override",
243 variantName: "android_common_foo_override",
244 overrides: []string{"bar", "foo"},
245 },
246 }
247
248 for _, expected := range expectedVariants {
249 mod := ctx.ModuleForTests(expected.name, expected.variantName).Module()
250 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
251 actual := entries.EntryMap["LOCAL_OVERRIDES_PACKAGES"]
252
253 android.AssertDeepEquals(t, "overrides property", expected.overrides, actual)
254 }
255}
Yu Shanb7646e42024-05-17 20:09:23 +0000256
Jiyong Park25b92222024-05-17 22:58:54 +0000257func TestJniAsRequiredDeps(t *testing.T) {
Yu Shanb7646e42024-05-17 20:09:23 +0000258 ctx := android.GroupFixturePreparers(
259 PrepareForTestWithJavaDefaultModules,
260 cc.PrepareForTestWithCcDefaultModules,
261 android.PrepareForTestWithAndroidMk,
Jiyong Park25b92222024-05-17 22:58:54 +0000262 ).RunTestWithBp(t, `
263 android_app {
264 name: "app",
265 jni_libs: ["libjni"],
266 platform_apis: true,
267 }
268
269 android_app {
270 name: "app_embedded",
271 jni_libs: ["libjni"],
272 platform_apis: true,
273 use_embedded_native_libs: true,
274 }
275
276 cc_library {
277 name: "libjni",
278 system_shared_libs: [],
279 stl: "none",
280 }
281 `)
282
283 testcases := []struct {
284 name string
285 expected []string
Yu Shanb7646e42024-05-17 20:09:23 +0000286 }{
Jiyong Park25b92222024-05-17 22:58:54 +0000287 {
288 name: "app",
Spandan Dasd4530d62024-09-26 00:46:12 +0000289 expected: []string{"libjni:64"},
Jiyong Park25b92222024-05-17 22:58:54 +0000290 },
291 {
292 name: "app_embedded",
293 expected: nil,
294 },
Yu Shanb7646e42024-05-17 20:09:23 +0000295 }
296
Jiyong Park25b92222024-05-17 22:58:54 +0000297 for _, tc := range testcases {
298 mod := ctx.ModuleForTests(tc.name, "android_common").Module()
299 entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, mod)[0]
300 required := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
301 android.AssertDeepEquals(t, "unexpected required deps", tc.expected, required)
Yu Shanb7646e42024-05-17 20:09:23 +0000302 }
303}