xref: /aosp_15_r20/build/soong/java/ravenwood_test.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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	"runtime"
19	"testing"
20
21	"android/soong/android"
22	"android/soong/etc"
23)
24
25var prepareRavenwoodRuntime = android.GroupFixturePreparers(
26	android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
27		RegisterRavenwoodBuildComponents(ctx)
28	}),
29	android.FixtureAddTextFile("ravenwood/Android.bp", `
30		cc_library_shared {
31			name: "ravenwood-runtime-jni1",
32			host_supported: true,
33			srcs: ["jni.cpp"],
34		}
35		cc_library_shared {
36			name: "ravenwood-runtime-jni2",
37			host_supported: true,
38			srcs: ["jni.cpp"],
39			stem: "libred",
40			shared_libs: [
41				"ravenwood-runtime-jni3",
42			],
43		}
44		cc_library_shared {
45			name: "ravenwood-runtime-jni3",
46			host_supported: true,
47			srcs: ["jni.cpp"],
48		}
49		java_library_static {
50			name: "framework-minus-apex.ravenwood",
51			srcs: ["Framework.java"],
52		}
53		java_library_static {
54			name: "framework-services.ravenwood",
55			srcs: ["Services.java"],
56		}
57		java_library_static {
58			name: "framework-rules.ravenwood",
59			srcs: ["Rules.java"],
60		}
61		android_app {
62			name: "app1",
63			sdk_version: "current",
64		}
65		android_app {
66			name: "app2",
67			sdk_version: "current",
68		}
69		android_app {
70			name: "app3",
71			sdk_version: "current",
72		}
73		prebuilt_font {
74			name: "Font.ttf",
75			src: "Font.ttf",
76		}
77		android_ravenwood_libgroup {
78			name: "ravenwood-runtime",
79			libs: [
80				"framework-minus-apex.ravenwood",
81				"framework-services.ravenwood",
82			],
83			jni_libs: [
84				"ravenwood-runtime-jni1",
85				"ravenwood-runtime-jni2",
86			],
87			data: [
88				":app1",
89			],
90			fonts: [
91				":Font.ttf"
92			],
93		}
94		android_ravenwood_libgroup {
95			name: "ravenwood-utils",
96			libs: [
97				"framework-rules.ravenwood",
98			],
99		}
100	`),
101)
102
103var installPathPrefix = "out/soong/host/linux-x86/testcases"
104
105func TestRavenwoodRuntime(t *testing.T) {
106	if runtime.GOOS != "linux" {
107		t.Skip("requires linux")
108	}
109
110	ctx := android.GroupFixturePreparers(
111		PrepareForIntegrationTestWithJava,
112		etc.PrepareForTestWithPrebuiltEtc,
113		prepareRavenwoodRuntime,
114	).RunTest(t)
115
116	// Verify that our runtime depends on underlying libs
117	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-runtime", "android_common", "framework-minus-apex.ravenwood")
118	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-runtime", "android_common", "framework-services.ravenwood")
119	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-runtime", "android_common", "ravenwood-runtime-jni")
120	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-utils", "android_common", "framework-rules.ravenwood")
121
122	// Verify that we've emitted artifacts in expected location
123	runtime := ctx.ModuleForTests("ravenwood-runtime", "android_common")
124	runtime.Output(installPathPrefix + "/ravenwood-runtime/framework-minus-apex.ravenwood.jar")
125	runtime.Output(installPathPrefix + "/ravenwood-runtime/framework-services.ravenwood.jar")
126	runtime.Output(installPathPrefix + "/ravenwood-runtime/lib64/ravenwood-runtime-jni1.so")
127	runtime.Output(installPathPrefix + "/ravenwood-runtime/lib64/libred.so")
128	runtime.Output(installPathPrefix + "/ravenwood-runtime/lib64/ravenwood-runtime-jni3.so")
129	runtime.Output(installPathPrefix + "/ravenwood-runtime/ravenwood-data/app1.apk")
130	runtime.Output(installPathPrefix + "/ravenwood-runtime/fonts/Font.ttf")
131	utils := ctx.ModuleForTests("ravenwood-utils", "android_common")
132	utils.Output(installPathPrefix + "/ravenwood-utils/framework-rules.ravenwood.jar")
133}
134
135func TestRavenwoodTest(t *testing.T) {
136	if runtime.GOOS != "linux" {
137		t.Skip("requires linux")
138	}
139
140	ctx := android.GroupFixturePreparers(
141		PrepareForIntegrationTestWithJava,
142		etc.PrepareForTestWithPrebuiltEtc,
143		prepareRavenwoodRuntime,
144	).RunTestWithBp(t, `
145		cc_library_shared {
146			name: "jni-lib1",
147			host_supported: true,
148			srcs: ["jni.cpp"],
149		}
150		cc_library_shared {
151			name: "jni-lib2",
152			host_supported: true,
153			srcs: ["jni.cpp"],
154			stem: "libblue",
155			shared_libs: [
156				"jni-lib3",
157			],
158		}
159		cc_library_shared {
160			name: "jni-lib3",
161			host_supported: true,
162			srcs: ["jni.cpp"],
163			stem: "libpink",
164		}
165		java_defaults {
166			name: "ravenwood-test-defaults",
167			jni_libs: ["jni-lib2"],
168		}
169		android_ravenwood_test {
170			name: "ravenwood-test",
171			srcs: ["Test.java"],
172			defaults: ["ravenwood-test-defaults"],
173			jni_libs: [
174				"jni-lib1",
175				"ravenwood-runtime-jni2",
176			],
177			resource_apk: "app2",
178			inst_resource_apk: "app3",
179			sdk_version: "test_current",
180			target_sdk_version: "34",
181			package_name: "a.b.c",
182			inst_package_name: "x.y.z",
183		}
184		android_ravenwood_test {
185			name: "ravenwood-test-empty",
186		}
187	`)
188
189	// Verify that our test depends on underlying libs
190	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-test", "android_common", "ravenwood-buildtime")
191	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-test", "android_common", "ravenwood-utils")
192	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-test", "android_common", "jni-lib")
193
194	module := ctx.ModuleForTests("ravenwood-test", "android_common")
195	classpath := module.Rule("javac").Args["classpath"]
196
197	// Verify that we're linking against test_current
198	android.AssertStringDoesContain(t, "classpath", classpath, "android_test_stubs_current.jar")
199	// Verify that we're linking against utils
200	android.AssertStringDoesContain(t, "classpath", classpath, "framework-rules.ravenwood.jar")
201	// Verify that we're *NOT* linking against runtime
202	android.AssertStringDoesNotContain(t, "classpath", classpath, "framework-minus-apex.ravenwood.jar")
203	android.AssertStringDoesNotContain(t, "classpath", classpath, "framework-services.ravenwood.jar")
204
205	// Verify that we've emitted test artifacts in expected location
206	outputJar := module.Output(installPathPrefix + "/ravenwood-test/ravenwood-test.jar")
207	module.Output(installPathPrefix + "/ravenwood-test/ravenwood-test.config")
208	module.Output(installPathPrefix + "/ravenwood-test/ravenwood.properties")
209	module.Output(installPathPrefix + "/ravenwood-test/lib64/jni-lib1.so")
210	module.Output(installPathPrefix + "/ravenwood-test/lib64/libblue.so")
211	module.Output(installPathPrefix + "/ravenwood-test/lib64/libpink.so")
212	module.Output(installPathPrefix + "/ravenwood-test/ravenwood-res-apks/ravenwood-res.apk")
213	module.Output(installPathPrefix + "/ravenwood-test/ravenwood-res-apks/ravenwood-inst-res.apk")
214
215	module = ctx.ModuleForTests("ravenwood-test-empty", "android_common")
216	module.Output(installPathPrefix + "/ravenwood-test-empty/ravenwood.properties")
217
218	// ravenwood-runtime*.so are included in the runtime, so it shouldn't be emitted.
219	for _, o := range module.AllOutputs() {
220		android.AssertStringDoesNotContain(t, "runtime libs shouldn't be included", o, "/ravenwood-test/lib64/ravenwood-runtime")
221	}
222
223	// Verify that we're going to install underlying libs
224	orderOnly := outputJar.OrderOnly.Strings()
225	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/framework-minus-apex.ravenwood.jar")
226	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/framework-services.ravenwood.jar")
227	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/lib64/ravenwood-runtime-jni1.so")
228	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/lib64/libred.so")
229	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/lib64/ravenwood-runtime-jni3.so")
230	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-utils/framework-rules.ravenwood.jar")
231}
232