• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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 (
18	"reflect"
19	"testing"
20
21	"android/soong/android"
22	"android/soong/cc"
23
24	"github.com/google/blueprint/proptools"
25)
26
27func TestRequired(t *testing.T) {
28	ctx, _ := testJava(t, `
29		java_library {
30			name: "foo",
31			srcs: ["a.java"],
32			required: ["libfoo"],
33		}
34	`)
35
36	mod := ctx.ModuleForTests("foo", "android_common").Module()
37	entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
38
39	expected := []string{"libfoo"}
40	actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
41	if !reflect.DeepEqual(expected, actual) {
42		t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
43	}
44}
45
46func TestHostdex(t *testing.T) {
47	ctx, _ := testJava(t, `
48		java_library {
49			name: "foo",
50			srcs: ["a.java"],
51			hostdex: true,
52		}
53	`)
54
55	mod := ctx.ModuleForTests("foo", "android_common").Module()
56	entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
57	if len(entriesList) != 2 {
58		t.Errorf("two entries are expected, but got %d", len(entriesList))
59	}
60
61	mainEntries := &entriesList[0]
62	expected := []string{"foo"}
63	actual := mainEntries.EntryMap["LOCAL_MODULE"]
64	if !reflect.DeepEqual(expected, actual) {
65		t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
66	}
67
68	subEntries := &entriesList[1]
69	expected = []string{"foo-hostdex"}
70	actual = subEntries.EntryMap["LOCAL_MODULE"]
71	if !reflect.DeepEqual(expected, actual) {
72		t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual)
73	}
74}
75
76func TestHostdexRequired(t *testing.T) {
77	ctx, _ := testJava(t, `
78		java_library {
79			name: "foo",
80			srcs: ["a.java"],
81			hostdex: true,
82			required: ["libfoo"],
83		}
84	`)
85
86	mod := ctx.ModuleForTests("foo", "android_common").Module()
87	entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
88	if len(entriesList) != 2 {
89		t.Errorf("two entries are expected, but got %d", len(entriesList))
90	}
91
92	mainEntries := &entriesList[0]
93	expected := []string{"libfoo"}
94	actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
95	if !reflect.DeepEqual(expected, actual) {
96		t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
97	}
98
99	subEntries := &entriesList[1]
100	expected = []string{"libfoo"}
101	actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
102	if !reflect.DeepEqual(expected, actual) {
103		t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
104	}
105}
106
107func TestHostdexSpecificRequired(t *testing.T) {
108	ctx, _ := testJava(t, `
109		java_library {
110			name: "foo",
111			srcs: ["a.java"],
112			hostdex: true,
113			target: {
114				hostdex: {
115					required: ["libfoo"],
116				},
117			},
118		}
119	`)
120
121	mod := ctx.ModuleForTests("foo", "android_common").Module()
122	entriesList := android.AndroidMkEntriesForTest(t, ctx, mod)
123	if len(entriesList) != 2 {
124		t.Errorf("two entries are expected, but got %d", len(entriesList))
125	}
126
127	mainEntries := &entriesList[0]
128	if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok {
129		t.Errorf("Unexpected required modules: %q", r)
130	}
131
132	subEntries := &entriesList[1]
133	expected := []string{"libfoo"}
134	actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"]
135	if !reflect.DeepEqual(expected, actual) {
136		t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual)
137	}
138}
139
140func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) {
141	result := android.GroupFixturePreparers(
142		prepareForJavaTest,
143		PrepareForTestWithJavaSdkLibraryFiles,
144		FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"),
145	).RunTestWithBp(t, `
146		java_sdk_library {
147			name: "foo-shared_library",
148			srcs: ["a.java"],
149		}
150		java_sdk_library {
151			name: "foo-no_shared_library",
152			srcs: ["a.java"],
153			shared_library: false,
154		}
155		`)
156
157	// Verify the existence of internal modules
158	result.ModuleForTests("foo-shared_library.xml", "android_common")
159
160	testCases := []struct {
161		moduleName string
162		expected   []string
163	}{
164		{"foo-shared_library", []string{"foo-shared_library.xml"}},
165		{"foo-no_shared_library", nil},
166	}
167	for _, tc := range testCases {
168		mod := result.ModuleForTests(tc.moduleName, "android_common").Module()
169		entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
170		actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"]
171		if !reflect.DeepEqual(tc.expected, actual) {
172			t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual)
173		}
174	}
175}
176
177func TestImportSoongDexJar(t *testing.T) {
178	result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, `
179		java_import {
180			name: "my-java-import",
181			jars: ["a.jar"],
182			prefer: true,
183			compile_dex: true,
184		}
185	`)
186
187	mod := result.Module("my-java-import", "android_common")
188	entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
189	expectedSoongDexJar := "out/soong/.intermediates/my-java-import/android_common/dex/my-java-import.jar"
190	actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"]
191
192	android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar)
193}
194
195func TestAndroidTestHelperApp_LocalDisableTestConfig(t *testing.T) {
196	ctx, _ := testJava(t, `
197		android_test_helper_app {
198			name: "foo",
199			srcs: ["a.java"],
200		}
201	`)
202
203	mod := ctx.ModuleForTests("foo", "android_common").Module()
204	entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
205
206	expected := []string{"true"}
207	actual := entries.EntryMap["LOCAL_DISABLE_TEST_CONFIG"]
208	if !reflect.DeepEqual(expected, actual) {
209		t.Errorf("Unexpected flag value - expected: %q, actual: %q", expected, actual)
210	}
211}
212
213func TestGetOverriddenPackages(t *testing.T) {
214	ctx, _ := testJava(
215		t, `
216		android_app {
217			name: "foo",
218			srcs: ["a.java"],
219			sdk_version: "current",
220			overrides: ["qux"]
221		}
222
223		override_android_app {
224			name: "foo_override",
225			base: "foo",
226			overrides: ["bar"]
227		}
228		`)
229
230	expectedVariants := []struct {
231		name        string
232		moduleName  string
233		variantName string
234		overrides   []string
235	}{
236		{
237			name:        "foo",
238			moduleName:  "foo",
239			variantName: "android_common",
240			overrides:   []string{"qux"},
241		},
242		{
243			name:        "foo",
244			moduleName:  "foo_override",
245			variantName: "android_common_foo_override",
246			overrides:   []string{"bar", "foo"},
247		},
248	}
249
250	for _, expected := range expectedVariants {
251		mod := ctx.ModuleForTests(expected.name, expected.variantName).Module()
252		entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0]
253		actual := entries.EntryMap["LOCAL_OVERRIDES_PACKAGES"]
254
255		android.AssertDeepEquals(t, "overrides property", expected.overrides, actual)
256	}
257}
258
259func TestJniPartition(t *testing.T) {
260	bp := `
261		cc_library {
262			name: "libjni_system",
263			system_shared_libs: [],
264			sdk_version: "current",
265			stl: "none",
266		}
267
268		cc_library {
269			name: "libjni_system_ext",
270			system_shared_libs: [],
271			sdk_version: "current",
272			stl: "none",
273			system_ext_specific: true,
274		}
275
276		cc_library {
277			name: "libjni_odm",
278			system_shared_libs: [],
279			sdk_version: "current",
280			stl: "none",
281			device_specific: true,
282		}
283
284		cc_library {
285			name: "libjni_product",
286			system_shared_libs: [],
287			sdk_version: "current",
288			stl: "none",
289			product_specific: true,
290		}
291
292		cc_library {
293			name: "libjni_vendor",
294			system_shared_libs: [],
295			sdk_version: "current",
296			stl: "none",
297			soc_specific: true,
298		}
299
300		android_app {
301			name: "test_app_system_jni_system",
302			privileged: true,
303			platform_apis: true,
304			certificate: "platform",
305			jni_libs: ["libjni_system"],
306		}
307
308		android_app {
309			name: "test_app_system_jni_system_ext",
310			privileged: true,
311			platform_apis: true,
312			certificate: "platform",
313			jni_libs: ["libjni_system_ext"],
314		}
315
316		android_app {
317			name: "test_app_system_ext_jni_system",
318			privileged: true,
319			platform_apis: true,
320			certificate: "platform",
321			jni_libs: ["libjni_system"],
322			system_ext_specific: true
323		}
324
325		android_app {
326			name: "test_app_system_ext_jni_system_ext",
327			sdk_version: "core_platform",
328			jni_libs: ["libjni_system_ext"],
329			system_ext_specific: true
330		}
331
332		android_app {
333			name: "test_app_product_jni_product",
334			sdk_version: "core_platform",
335			jni_libs: ["libjni_product"],
336			product_specific: true
337		}
338
339		android_app {
340			name: "test_app_vendor_jni_odm",
341			sdk_version: "core_platform",
342			jni_libs: ["libjni_odm"],
343			soc_specific: true
344		}
345
346		android_app {
347			name: "test_app_odm_jni_vendor",
348			sdk_version: "core_platform",
349			jni_libs: ["libjni_vendor"],
350			device_specific: true
351		}
352		android_app {
353			name: "test_app_system_jni_multiple",
354			privileged: true,
355			platform_apis: true,
356			certificate: "platform",
357			jni_libs: ["libjni_system", "libjni_system_ext"],
358		}
359		android_app {
360			name: "test_app_vendor_jni_multiple",
361			sdk_version: "core_platform",
362			jni_libs: ["libjni_odm", "libjni_vendor"],
363			soc_specific: true
364		}
365		`
366	arch := "arm64"
367	ctx := android.GroupFixturePreparers(
368		PrepareForTestWithJavaDefaultModules,
369		cc.PrepareForTestWithCcDefaultModules,
370		android.PrepareForTestWithAndroidMk,
371		android.FixtureModifyConfig(func(config android.Config) {
372			config.TestProductVariables.DeviceArch = proptools.StringPtr(arch)
373		}),
374	).
375		RunTestWithBp(t, bp)
376	testCases := []struct {
377		name           string
378		partitionNames []string
379		partitionTags  []string
380	}{
381		{"test_app_system_jni_system", []string{"libjni_system"}, []string{""}},
382		{"test_app_system_jni_system_ext", []string{"libjni_system_ext"}, []string{"_SYSTEM_EXT"}},
383		{"test_app_system_ext_jni_system", []string{"libjni_system"}, []string{""}},
384		{"test_app_system_ext_jni_system_ext", []string{"libjni_system_ext"}, []string{"_SYSTEM_EXT"}},
385		{"test_app_product_jni_product", []string{"libjni_product"}, []string{"_PRODUCT"}},
386		{"test_app_vendor_jni_odm", []string{"libjni_odm"}, []string{"_ODM"}},
387		{"test_app_odm_jni_vendor", []string{"libjni_vendor"}, []string{"_VENDOR"}},
388		{"test_app_system_jni_multiple", []string{"libjni_system", "libjni_system_ext"}, []string{"", "_SYSTEM_EXT"}},
389		{"test_app_vendor_jni_multiple", []string{"libjni_odm", "libjni_vendor"}, []string{"_ODM", "_VENDOR"}},
390	}
391
392	for _, test := range testCases {
393		t.Run(test.name, func(t *testing.T) {
394			mod := ctx.ModuleForTests(test.name, "android_common").Module()
395			entry := android.AndroidMkEntriesForTest(t, ctx.TestContext, mod)[0]
396			for i := range test.partitionNames {
397				actual := entry.EntryMap["LOCAL_SOONG_JNI_LIBS_PARTITION_"+arch][i]
398				expected := test.partitionNames[i] + ":" + test.partitionTags[i]
399				android.AssertStringEquals(t, "Expected and actual differ", expected, actual)
400			}
401		})
402	}
403}
404