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)
23
24var prepareRavenwoodRuntime = android.GroupFixturePreparers(
25	android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
26		RegisterRavenwoodBuildComponents(ctx)
27	}),
28	android.FixtureAddTextFile("ravenwood/Android.bp", `
29		cc_library_shared {
30			name: "ravenwood-runtime-jni1",
31			host_supported: true,
32			srcs: ["jni.cpp"],
33		}
34		cc_library_shared {
35			name: "ravenwood-runtime-jni2",
36			host_supported: true,
37			srcs: ["jni.cpp"],
38			stem: "libred",
39			shared_libs: [
40				"ravenwood-runtime-jni3",
41			],
42		}
43		cc_library_shared {
44			name: "ravenwood-runtime-jni3",
45			host_supported: true,
46			srcs: ["jni.cpp"],
47		}
48		java_library_static {
49			name: "framework-minus-apex.ravenwood",
50			srcs: ["Framework.java"],
51		}
52		java_library_static {
53			name: "framework-services.ravenwood",
54			srcs: ["Services.java"],
55		}
56		java_library_static {
57			name: "framework-rules.ravenwood",
58			srcs: ["Rules.java"],
59		}
60		android_ravenwood_libgroup {
61			name: "ravenwood-runtime",
62			libs: [
63				"framework-minus-apex.ravenwood",
64				"framework-services.ravenwood",
65			],
66			jni_libs: [
67				"ravenwood-runtime-jni1",
68				"ravenwood-runtime-jni2",
69			],
70		}
71		android_ravenwood_libgroup {
72			name: "ravenwood-utils",
73			libs: [
74				"framework-rules.ravenwood",
75			],
76		}
77	`),
78)
79
80var installPathPrefix = "out/soong/host/linux-x86/testcases"
81
82func TestRavenwoodRuntime(t *testing.T) {
83	if runtime.GOOS != "linux" {
84		t.Skip("requires linux")
85	}
86
87	ctx := android.GroupFixturePreparers(
88		PrepareForIntegrationTestWithJava,
89		prepareRavenwoodRuntime,
90	).RunTest(t)
91
92	// Verify that our runtime depends on underlying libs
93	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-runtime", "android_common", "framework-minus-apex.ravenwood")
94	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-runtime", "android_common", "framework-services.ravenwood")
95	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-runtime", "android_common", "ravenwood-runtime-jni")
96	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-utils", "android_common", "framework-rules.ravenwood")
97
98	// Verify that we've emitted artifacts in expected location
99	runtime := ctx.ModuleForTests("ravenwood-runtime", "android_common")
100	runtime.Output(installPathPrefix + "/ravenwood-runtime/framework-minus-apex.ravenwood.jar")
101	runtime.Output(installPathPrefix + "/ravenwood-runtime/framework-services.ravenwood.jar")
102	runtime.Output(installPathPrefix + "/ravenwood-runtime/lib64/ravenwood-runtime-jni1.so")
103	runtime.Output(installPathPrefix + "/ravenwood-runtime/lib64/libred.so")
104	runtime.Output(installPathPrefix + "/ravenwood-runtime/lib64/ravenwood-runtime-jni3.so")
105	utils := ctx.ModuleForTests("ravenwood-utils", "android_common")
106	utils.Output(installPathPrefix + "/ravenwood-utils/framework-rules.ravenwood.jar")
107}
108
109func TestRavenwoodTest(t *testing.T) {
110	if runtime.GOOS != "linux" {
111		t.Skip("requires linux")
112	}
113
114	ctx := android.GroupFixturePreparers(
115		PrepareForIntegrationTestWithJava,
116		prepareRavenwoodRuntime,
117	).RunTestWithBp(t, `
118	cc_library_shared {
119		name: "jni-lib1",
120		host_supported: true,
121		srcs: ["jni.cpp"],
122	}
123	cc_library_shared {
124		name: "jni-lib2",
125		host_supported: true,
126		srcs: ["jni.cpp"],
127		stem: "libblue",
128		shared_libs: [
129			"jni-lib3",
130		],
131	}
132	cc_library_shared {
133		name: "jni-lib3",
134		host_supported: true,
135		srcs: ["jni.cpp"],
136		stem: "libpink",
137	}
138	android_ravenwood_test {
139			name: "ravenwood-test",
140			srcs: ["Test.java"],
141			jni_libs: [
142				"jni-lib1",
143				"jni-lib2",
144				"ravenwood-runtime-jni2",
145			],
146			sdk_version: "test_current",
147		}
148	`)
149
150	// Verify that our test depends on underlying libs
151	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-test", "android_common", "ravenwood-buildtime")
152	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-test", "android_common", "ravenwood-utils")
153	CheckModuleHasDependency(t, ctx.TestContext, "ravenwood-test", "android_common", "jni-lib")
154
155	module := ctx.ModuleForTests("ravenwood-test", "android_common")
156	classpath := module.Rule("javac").Args["classpath"]
157
158	// Verify that we're linking against test_current
159	android.AssertStringDoesContain(t, "classpath", classpath, "android_test_stubs_current.jar")
160	// Verify that we're linking against utils
161	android.AssertStringDoesContain(t, "classpath", classpath, "framework-rules.ravenwood.jar")
162	// Verify that we're *NOT* linking against runtime
163	android.AssertStringDoesNotContain(t, "classpath", classpath, "framework-minus-apex.ravenwood.jar")
164	android.AssertStringDoesNotContain(t, "classpath", classpath, "framework-services.ravenwood.jar")
165
166	// Verify that we've emitted test artifacts in expected location
167	outputJar := module.Output(installPathPrefix + "/ravenwood-test/ravenwood-test.jar")
168	module.Output(installPathPrefix + "/ravenwood-test/ravenwood-test.config")
169	module.Output(installPathPrefix + "/ravenwood-test/lib64/jni-lib1.so")
170	module.Output(installPathPrefix + "/ravenwood-test/lib64/libblue.so")
171	module.Output(installPathPrefix + "/ravenwood-test/lib64/libpink.so")
172
173	// ravenwood-runtime*.so are included in the runtime, so it shouldn't be emitted.
174	for _, o := range module.AllOutputs() {
175		android.AssertStringDoesNotContain(t, "runtime libs shouldn't be included", o, "/ravenwood-test/lib64/ravenwood-runtime")
176	}
177
178	// Verify that we're going to install underlying libs
179	orderOnly := outputJar.OrderOnly.Strings()
180	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/framework-minus-apex.ravenwood.jar")
181	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/framework-services.ravenwood.jar")
182	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/lib64/ravenwood-runtime-jni1.so")
183	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/lib64/libred.so")
184	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-runtime/lib64/ravenwood-runtime-jni3.so")
185	android.AssertStringListContains(t, "orderOnly", orderOnly, installPathPrefix+"/ravenwood-utils/framework-rules.ravenwood.jar")
186}
187