Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package sdk |
| 16 | |
| 17 | import ( |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 18 | "testing" |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 19 | ) |
| 20 | |
Paul Duffin | 82d9043 | 2019-11-30 09:24:33 +0000 | [diff] [blame] | 21 | // Needed in an _test.go file in this package to ensure tests run correctly, particularly in IDE. |
| 22 | func TestMain(m *testing.M) { |
| 23 | runTestWithBuildDir(m) |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 24 | } |
| 25 | |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 26 | func 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 Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 30 | java_header_libs: ["sdkmember"], |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | sdk_snapshot { |
| 34 | name: "mysdk@1", |
Paul Duffin | a0dbf43 | 2019-12-05 11:25:53 +0000 | [diff] [blame] | 35 | java_header_libs: ["sdkmember_mysdk_1"], |
Jiyong Park | a7bc8ad | 2019-10-15 15:20:07 +0900 | [diff] [blame] | 36 | } |
| 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 Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 82 | |
| 83 | // Ensure that prebuilt modules have the same effective visibility as the source |
| 84 | // modules. |
| 85 | func 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 Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 114 | java_defaults { |
| 115 | name: "java-defaults", |
| 116 | visibility: ["//other/bar"], |
| 117 | } |
| 118 | |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 119 | java_library { |
| 120 | name: "mypublicjavalib", |
Paul Duffin | 44885e2 | 2020-02-19 16:10:09 +0000 | [diff] [blame] | 121 | defaults: ["java-defaults"], |
Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 122 | 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 | |
| 152 | java_import { |
| 153 | name: "mysdk_myjavalib@current", |
| 154 | sdk_member_name: "myjavalib", |
| 155 | visibility: ["//other/foo:__pkg__"], |
| 156 | jars: ["java/myjavalib.jar"], |
| 157 | } |
| 158 | |
| 159 | java_import { |
| 160 | name: "myjavalib", |
| 161 | prefer: false, |
| 162 | visibility: ["//other/foo:__pkg__"], |
| 163 | jars: ["java/myjavalib.jar"], |
| 164 | } |
| 165 | |
| 166 | java_import { |
| 167 | name: "mysdk_mypublicjavalib@current", |
| 168 | sdk_member_name: "mypublicjavalib", |
| 169 | visibility: ["//visibility:public"], |
| 170 | jars: ["java/mypublicjavalib.jar"], |
| 171 | } |
| 172 | |
| 173 | java_import { |
| 174 | name: "mypublicjavalib", |
| 175 | prefer: false, |
| 176 | visibility: ["//visibility:public"], |
| 177 | jars: ["java/mypublicjavalib.jar"], |
| 178 | } |
| 179 | |
| 180 | java_import { |
| 181 | name: "mysdk_mydefaultedjavalib@current", |
| 182 | sdk_member_name: "mydefaultedjavalib", |
| 183 | visibility: ["//other/bar:__pkg__"], |
| 184 | jars: ["java/mydefaultedjavalib.jar"], |
| 185 | } |
| 186 | |
| 187 | java_import { |
| 188 | name: "mydefaultedjavalib", |
| 189 | prefer: false, |
| 190 | visibility: ["//other/bar:__pkg__"], |
| 191 | jars: ["java/mydefaultedjavalib.jar"], |
| 192 | } |
| 193 | |
| 194 | sdk_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 | } |