• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2022 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	"testing"
19
20	"android/soong/android"
21)
22
23func TestAarImportProducesJniPackages(t *testing.T) {
24	t.Parallel()
25	ctx := android.GroupFixturePreparers(
26		PrepareForTestWithJavaDefaultModules,
27	).RunTestWithBp(t, `
28		android_library_import {
29			name: "aar-no-jni",
30			aars: ["aary.aar"],
31		}
32		android_library_import {
33			name: "aar-jni",
34			aars: ["aary.aar"],
35			extract_jni: true,
36		}`)
37
38	testCases := []struct {
39		name       string
40		hasPackage bool
41	}{
42		{
43			name:       "aar-no-jni",
44			hasPackage: false,
45		},
46		{
47			name:       "aar-jni",
48			hasPackage: true,
49		},
50	}
51
52	for _, tc := range testCases {
53		t.Run(tc.name, func(t *testing.T) {
54			t.Parallel()
55			appMod := ctx.Module(tc.name, "android_common")
56			appTestMod := ctx.ModuleForTests(t, tc.name, "android_common")
57
58			info, ok := android.OtherModuleProvider(ctx, appMod, JniPackageProvider)
59			if !ok {
60				t.Errorf("expected android_library_import to have JniPackageProvider")
61			}
62
63			if !tc.hasPackage {
64				if len(info.JniPackages) != 0 {
65					t.Errorf("expected JniPackages to be empty, but got %v", info.JniPackages)
66				}
67				outputFile := "arm64-v8a_jni.zip"
68				jniOutputLibZip := appTestMod.MaybeOutput(outputFile)
69				if jniOutputLibZip.Rule != nil {
70					t.Errorf("did not expect an output file, but found %v", outputFile)
71				}
72				return
73			}
74
75			if len(info.JniPackages) != 1 {
76				t.Errorf("expected a single JniPackage, but got %v", info.JniPackages)
77			}
78
79			outputFile := info.JniPackages[0].String()
80			jniOutputLibZip := appTestMod.Output(outputFile)
81			if jniOutputLibZip.Rule == nil {
82				t.Errorf("did not find output file %v", outputFile)
83			}
84		})
85	}
86}
87
88func TestLibraryFlagsPackages(t *testing.T) {
89	t.Parallel()
90	result := android.GroupFixturePreparers(
91		prepareForJavaTest,
92	).RunTestWithBp(t, `
93		android_library {
94			name: "foo",
95			srcs: ["a.java"],
96			sdk_version: "current",
97			flags_packages: [
98				"bar",
99				"baz",
100			],
101		}
102		aconfig_declarations {
103			name: "bar",
104			package: "com.example.package.bar",
105			container: "com.android.foo",
106			srcs: [
107				"bar.aconfig",
108			],
109		}
110		aconfig_declarations {
111			name: "baz",
112			package: "com.example.package.baz",
113			container: "com.android.foo",
114			srcs: [
115				"baz.aconfig",
116			],
117		}
118	`)
119
120	foo := result.ModuleForTests(t, "foo", "android_common")
121
122	// android_library module depends on aconfig_declarations listed in flags_packages
123	android.AssertBoolEquals(t, "foo expected to depend on bar", true,
124		CheckModuleHasDependency(t, result.TestContext, "foo", "android_common", "bar"))
125
126	android.AssertBoolEquals(t, "foo expected to depend on baz", true,
127		CheckModuleHasDependency(t, result.TestContext, "foo", "android_common", "baz"))
128
129	aapt2LinkRule := foo.Rule("android/soong/java.aapt2Link")
130	linkInFlags := aapt2LinkRule.Args["inFlags"]
131	android.AssertStringDoesContain(t,
132		"aapt2 link command expected to pass feature flags arguments",
133		linkInFlags,
134		"--feature-flags @out/soong/.intermediates/bar/intermediate.txt --feature-flags @out/soong/.intermediates/baz/intermediate.txt",
135	)
136}
137
138func TestAndroidLibraryOutputFilesRel(t *testing.T) {
139	t.Parallel()
140	result := android.GroupFixturePreparers(
141		PrepareForTestWithJavaDefaultModules,
142	).RunTestWithBp(t, `
143		android_library {
144			name: "foo",
145			srcs: ["a.java"],
146			java_resources: ["foo.txt"],
147		}
148
149		android_library_import {
150			name: "bar",
151			aars: ["bar_prebuilt.aar"],
152
153		}
154
155		android_library_import {
156			name: "baz",
157			aars: ["baz_prebuilt.aar"],
158			static_libs: ["foo", "bar"],
159		}
160	`)
161
162	foo := result.ModuleForTests(t, "foo", "android_common")
163	bar := result.ModuleForTests(t, "bar", "android_common")
164	baz := result.ModuleForTests(t, "baz", "android_common")
165
166	fooOutputPaths := foo.OutputFiles(result.TestContext, t, "")
167	barOutputPaths := bar.OutputFiles(result.TestContext, t, "")
168	bazOutputPaths := baz.OutputFiles(result.TestContext, t, "")
169
170	android.AssertPathsRelativeToTopEquals(t, "foo output path",
171		[]string{"out/soong/.intermediates/foo/android_common/withres/foo.jar"}, fooOutputPaths)
172	android.AssertPathsRelativeToTopEquals(t, "bar output path",
173		[]string{"out/soong/.intermediates/bar/android_common/aar/bar.jar"}, barOutputPaths)
174	android.AssertPathsRelativeToTopEquals(t, "baz output path",
175		[]string{"out/soong/.intermediates/baz/android_common/withres/baz.jar"}, bazOutputPaths)
176
177	android.AssertStringEquals(t, "foo relative output path",
178		"foo.jar", fooOutputPaths[0].Rel())
179	android.AssertStringEquals(t, "bar relative output path",
180		"bar.jar", barOutputPaths[0].Rel())
181	android.AssertStringEquals(t, "baz relative output path",
182		"baz.jar", bazOutputPaths[0].Rel())
183}
184