blob: 9540a6b50552efb333949e6f019e0fdea03eb3c5 [file] [log] [blame]
Jiakai Zhanga8d86602021-09-26 09:02:17 +00001// Copyright (C) 2021 The Android Open Source Project
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 sdk
16
17import (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/dexpreopt"
22 "android/soong/java"
23)
24
Paul Duffin4e7d1c42022-05-13 13:12:19 +000025func testSnapshotWithSystemServerClasspathFragment(t *testing.T, sdk string, targetBuildRelease string, expectedSdkSnapshot string) {
Jiakai Zhanga8d86602021-09-26 09:02:17 +000026 result := android.GroupFixturePreparers(
27 prepareForSdkTestWithJava,
28 java.PrepareForTestWithJavaDefaultModules,
29 java.PrepareForTestWithJavaSdkLibraryFiles,
30 java.FixtureWithLastReleaseApis("mysdklibrary"),
31 dexpreopt.FixtureSetApexSystemServerJars("myapex:mylib", "myapex:mysdklibrary"),
Paul Duffinf861df72022-07-01 15:56:06 +000032 android.FixtureModifyEnv(func(env map[string]string) {
Paul Duffin4e7d1c42022-05-13 13:12:19 +000033 if targetBuildRelease != "latest" {
34 env["SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE"] = targetBuildRelease
35 }
Paul Duffinf861df72022-07-01 15:56:06 +000036 }),
Jiakai Zhanga8d86602021-09-26 09:02:17 +000037 prepareForSdkTestWithApex,
38
Paul Duffin4e7d1c42022-05-13 13:12:19 +000039 android.FixtureWithRootAndroidBp(sdk+`
Jiakai Zhanga8d86602021-09-26 09:02:17 +000040 apex {
41 name: "myapex",
42 key: "myapex.key",
43 min_sdk_version: "2",
44 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
45 }
46
47 systemserverclasspath_fragment {
48 name: "mysystemserverclasspathfragment",
49 apex_available: ["myapex"],
50 contents: [
51 "mylib",
52 "mysdklibrary",
53 ],
54 }
55
56 java_library {
57 name: "mylib",
58 apex_available: ["myapex"],
59 srcs: ["Test.java"],
60 system_modules: "none",
61 sdk_version: "none",
62 min_sdk_version: "2",
63 compile_dex: true,
64 permitted_packages: ["mylib"],
65 }
66
67 java_sdk_library {
68 name: "mysdklibrary",
69 apex_available: ["myapex"],
70 srcs: ["Test.java"],
71 shared_library: false,
72 public: {enabled: true},
73 min_sdk_version: "2",
74 }
75 `),
76 ).RunTest(t)
77
78 CheckSnapshot(t, result, "mysdk", "",
Paul Duffinf861df72022-07-01 15:56:06 +000079 checkAndroidBpContents(expectedSdkSnapshot),
80 )
81}
82
83func TestSnapshotWithSystemServerClasspathFragment(t *testing.T) {
Paul Duffin4e7d1c42022-05-13 13:12:19 +000084
85 commonSdk := `
86sdk {
87 name: "mysdk",
88 systemserverclasspath_fragments: ["mysystemserverclasspathfragment"],
89 java_sdk_libs: [
90 // This is not strictly needed as it should be automatically added to the sdk_snapshot as
91 // a java_sdk_libs module because it is used in the mysystemserverclasspathfragment's
92 // contents property. However, it is specified here to ensure that duplicates are
93 // correctly deduped.
94 "mysdklibrary",
95 ],
96}
97 `
98
99 expectedLatestSnapshot := `
100// This is auto-generated. DO NOT EDIT.
101
102java_sdk_library_import {
103 name: "mysdklibrary",
104 prefer: false,
105 visibility: ["//visibility:public"],
106 apex_available: ["myapex"],
107 shared_library: false,
108 public: {
109 jars: ["sdk_library/public/mysdklibrary-stubs.jar"],
110 stub_srcs: ["sdk_library/public/mysdklibrary_stub_sources"],
111 current_api: "sdk_library/public/mysdklibrary.txt",
112 removed_api: "sdk_library/public/mysdklibrary-removed.txt",
113 sdk_version: "current",
114 },
115}
116
117java_import {
118 name: "mylib",
119 prefer: false,
120 visibility: ["//visibility:public"],
121 apex_available: ["myapex"],
Paul Duffinc61783b2022-10-20 17:21:40 +0100122 jars: [":mysdk_mylib-error"],
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000123 permitted_packages: ["mylib"],
124}
125
Paul Duffinc61783b2022-10-20 17:21:40 +0100126genrule {
127 name: "mysdk_mylib-error",
128 visibility: ["//visibility:private"],
129 out: ["this-file-will-never-be-created.jar"],
130 tool_files: ["scripts/invalid_implementation_jar.sh"],
131 cmd: "$(location scripts/invalid_implementation_jar.sh) mylib",
132}
133
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000134prebuilt_systemserverclasspath_fragment {
135 name: "mysystemserverclasspathfragment",
136 prefer: false,
137 visibility: ["//visibility:public"],
138 apex_available: ["myapex"],
139 contents: [
140 "mylib",
141 "mysdklibrary",
142 ],
143}
144`
145
Paul Duffinf861df72022-07-01 15:56:06 +0000146 t.Run("target-s", func(t *testing.T) {
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000147 testSnapshotWithSystemServerClasspathFragment(t, commonSdk, "S", `
Paul Duffinf861df72022-07-01 15:56:06 +0000148// This is auto-generated. DO NOT EDIT.
149
150java_sdk_library_import {
151 name: "mysdklibrary",
152 prefer: false,
153 visibility: ["//visibility:public"],
154 apex_available: ["myapex"],
155 shared_library: false,
156 public: {
157 jars: ["sdk_library/public/mysdklibrary-stubs.jar"],
158 stub_srcs: ["sdk_library/public/mysdklibrary_stub_sources"],
159 current_api: "sdk_library/public/mysdklibrary.txt",
160 removed_api: "sdk_library/public/mysdklibrary-removed.txt",
161 sdk_version: "current",
162 },
163}
164`)
165 })
166
167 t.Run("target-t", func(t *testing.T) {
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000168 testSnapshotWithSystemServerClasspathFragment(t, commonSdk, "Tiramisu", `
Jiakai Zhanga8d86602021-09-26 09:02:17 +0000169// This is auto-generated. DO NOT EDIT.
170
171java_sdk_library_import {
172 name: "mysdklibrary",
173 prefer: false,
174 visibility: ["//visibility:public"],
175 apex_available: ["myapex"],
176 shared_library: false,
177 public: {
178 jars: ["sdk_library/public/mysdklibrary-stubs.jar"],
179 stub_srcs: ["sdk_library/public/mysdklibrary_stub_sources"],
180 current_api: "sdk_library/public/mysdklibrary.txt",
181 removed_api: "sdk_library/public/mysdklibrary-removed.txt",
182 sdk_version: "current",
183 },
184}
185
186java_import {
187 name: "mylib",
188 prefer: false,
189 visibility: ["//visibility:public"],
190 apex_available: ["myapex"],
Paul Duffinc61783b2022-10-20 17:21:40 +0100191 jars: [":mysdk_mylib-error"],
Jiakai Zhanga8d86602021-09-26 09:02:17 +0000192 permitted_packages: ["mylib"],
193}
194
Paul Duffinc61783b2022-10-20 17:21:40 +0100195genrule {
196 name: "mysdk_mylib-error",
197 visibility: ["//visibility:private"],
198 out: ["this-file-will-never-be-created.jar"],
199 tool_files: ["scripts/invalid_implementation_jar.sh"],
200 cmd: "$(location scripts/invalid_implementation_jar.sh) mylib",
201}
202
Jiakai Zhanga8d86602021-09-26 09:02:17 +0000203prebuilt_systemserverclasspath_fragment {
204 name: "mysystemserverclasspathfragment",
205 prefer: false,
206 visibility: ["//visibility:public"],
207 apex_available: ["myapex"],
208 contents: [
209 "mylib",
210 "mysdklibrary",
211 ],
212}
Paul Duffinf861df72022-07-01 15:56:06 +0000213`)
214 })
Paul Duffin4e7d1c42022-05-13 13:12:19 +0000215
216 t.Run("added-directly", func(t *testing.T) {
217 testSnapshotWithSystemServerClasspathFragment(t, commonSdk, `latest`, expectedLatestSnapshot)
218 })
219
220 t.Run("added-via-apex", func(t *testing.T) {
221 testSnapshotWithSystemServerClasspathFragment(t, `
222 sdk {
223 name: "mysdk",
224 apexes: ["myapex"],
225 }
226 `, `latest`, expectedLatestSnapshot)
227 })
Jiakai Zhanga8d86602021-09-26 09:02:17 +0000228}