blob: 934bdae0114919b998c5890b5966c722676c120f [file] [log] [blame]
Jiyong Parkd1063c12019-07-17 20:08:41 +09001// 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 sdk
16
17import (
Jiyong Parkd1063c12019-07-17 20:08:41 +090018 "testing"
Jiyong Parkd1063c12019-07-17 20:08:41 +090019)
20
Paul Duffin82d90432019-11-30 09:24:33 +000021// Needed in an _test.go file in this package to ensure tests run correctly, particularly in IDE.
22func TestMain(m *testing.M) {
23 runTestWithBuildDir(m)
Jiyong Parkd1063c12019-07-17 20:08:41 +090024}
25
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090026func TestDepNotInRequiredSdks(t *testing.T) {
27 testSdkError(t, `module "myjavalib".*depends on "otherlib".*that isn't part of the required SDKs:.*`, `
28 sdk {
29 name: "mysdk",
Paul Duffina0dbf432019-12-05 11:25:53 +000030 java_header_libs: ["sdkmember"],
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090031 }
32
33 sdk_snapshot {
34 name: "mysdk@1",
Paul Duffina0dbf432019-12-05 11:25:53 +000035 java_header_libs: ["sdkmember_mysdk_1"],
Jiyong Parka7bc8ad2019-10-15 15:20:07 +090036 }
37
38 java_import {
39 name: "sdkmember",
40 prefer: false,
41 host_supported: true,
42 }
43
44 java_import {
45 name: "sdkmember_mysdk_1",
46 sdk_member_name: "sdkmember",
47 host_supported: true,
48 }
49
50 java_library {
51 name: "myjavalib",
52 srcs: ["Test.java"],
53 libs: [
54 "sdkmember",
55 "otherlib",
56 ],
57 system_modules: "none",
58 sdk_version: "none",
59 compile_dex: true,
60 host_supported: true,
61 }
62
63 // this lib is no in mysdk
64 java_library {
65 name: "otherlib",
66 srcs: ["Test.java"],
67 system_modules: "none",
68 sdk_version: "none",
69 compile_dex: true,
70 host_supported: true,
71 }
72
73 apex {
74 name: "myapex",
75 java_libs: ["myjavalib"],
76 uses_sdks: ["mysdk@1"],
77 key: "myapex.key",
78 certificate: ":myapex.cert",
79 }
80 `)
81}
Paul Duffin593b3c92019-12-05 14:31:48 +000082
83// Ensure that prebuilt modules have the same effective visibility as the source
84// modules.
85func TestSnapshotVisibility(t *testing.T) {
86 packageBp := `
87 package {
88 default_visibility: ["//other/foo"],
89 }
90
91 sdk {
92 name: "mysdk",
93 visibility: [
94 "//other/foo",
95 // This short form will be replaced with //package:__subpackages__ in the
96 // generated sdk_snapshot.
97 ":__subpackages__",
98 ],
99 java_header_libs: [
100 "myjavalib",
101 "mypublicjavalib",
102 "mydefaultedjavalib",
103 ],
104 }
105
106 java_library {
107 name: "myjavalib",
108 // Uses package default visibility
109 srcs: ["Test.java"],
110 system_modules: "none",
111 sdk_version: "none",
112 }
113
Paul Duffin44885e22020-02-19 16:10:09 +0000114 java_defaults {
115 name: "java-defaults",
116 visibility: ["//other/bar"],
117 }
118
Paul Duffin593b3c92019-12-05 14:31:48 +0000119 java_library {
120 name: "mypublicjavalib",
Paul Duffin44885e22020-02-19 16:10:09 +0000121 defaults: ["java-defaults"],
Paul Duffin593b3c92019-12-05 14:31:48 +0000122 visibility: ["//visibility:public"],
123 srcs: ["Test.java"],
124 system_modules: "none",
125 sdk_version: "none",
126 }
127
128 java_defaults {
129 name: "myjavadefaults",
130 visibility: ["//other/bar"],
131 }
132
133 java_library {
134 name: "mydefaultedjavalib",
135 defaults: ["myjavadefaults"],
136 srcs: ["Test.java"],
137 system_modules: "none",
138 sdk_version: "none",
139 }
140 `
141
142 result := testSdkWithFs(t, ``,
143 map[string][]byte{
144 "package/Test.java": nil,
145 "package/Android.bp": []byte(packageBp),
146 })
147
148 result.CheckSnapshot("mysdk", "android_common", "package",
149 checkAndroidBpContents(`
150// This is auto-generated. DO NOT EDIT.
151
152java_import {
153 name: "mysdk_myjavalib@current",
154 sdk_member_name: "myjavalib",
155 visibility: ["//other/foo:__pkg__"],
156 jars: ["java/myjavalib.jar"],
157}
158
159java_import {
160 name: "myjavalib",
161 prefer: false,
162 visibility: ["//other/foo:__pkg__"],
163 jars: ["java/myjavalib.jar"],
164}
165
166java_import {
167 name: "mysdk_mypublicjavalib@current",
168 sdk_member_name: "mypublicjavalib",
169 visibility: ["//visibility:public"],
170 jars: ["java/mypublicjavalib.jar"],
171}
172
173java_import {
174 name: "mypublicjavalib",
175 prefer: false,
176 visibility: ["//visibility:public"],
177 jars: ["java/mypublicjavalib.jar"],
178}
179
180java_import {
181 name: "mysdk_mydefaultedjavalib@current",
182 sdk_member_name: "mydefaultedjavalib",
183 visibility: ["//other/bar:__pkg__"],
184 jars: ["java/mydefaultedjavalib.jar"],
185}
186
187java_import {
188 name: "mydefaultedjavalib",
189 prefer: false,
190 visibility: ["//other/bar:__pkg__"],
191 jars: ["java/mydefaultedjavalib.jar"],
192}
193
194sdk_snapshot {
195 name: "mysdk@current",
196 visibility: [
197 "//other/foo:__pkg__",
198 "//package:__subpackages__",
199 ],
200 java_header_libs: [
201 "mysdk_myjavalib@current",
202 "mysdk_mypublicjavalib@current",
203 "mysdk_mydefaultedjavalib@current",
204 ],
205}
206`))
207}