Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | "android/soong/dexpreopt" |
| 22 | ) |
| 23 | |
| 24 | // Contains some simple tests for platform_bootclasspath. |
| 25 | |
| 26 | var prepareForTestWithPlatformBootclasspath = android.GroupFixturePreparers( |
| 27 | PrepareForTestWithJavaDefaultModules, |
| 28 | dexpreopt.PrepareForTestByEnablingDexpreopt, |
| 29 | ) |
| 30 | |
| 31 | func TestPlatformBootclasspath(t *testing.T) { |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 32 | preparer := android.GroupFixturePreparers( |
| 33 | prepareForTestWithPlatformBootclasspath, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 34 | FixtureConfigureBootJars("platform:foo", "platform:bar"), |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 35 | android.FixtureWithRootAndroidBp(` |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 36 | platform_bootclasspath { |
| 37 | name: "platform-bootclasspath", |
| 38 | } |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 39 | |
| 40 | java_library { |
| 41 | name: "bar", |
| 42 | srcs: ["a.java"], |
| 43 | system_modules: "none", |
| 44 | sdk_version: "none", |
| 45 | compile_dex: true, |
| 46 | } |
| 47 | `), |
| 48 | ) |
| 49 | |
| 50 | var addSourceBootclassPathModule = android.FixtureAddTextFile("source/Android.bp", ` |
| 51 | java_library { |
| 52 | name: "foo", |
| 53 | srcs: ["a.java"], |
| 54 | system_modules: "none", |
| 55 | sdk_version: "none", |
| 56 | compile_dex: true, |
| 57 | } |
| 58 | `) |
| 59 | |
| 60 | var addPrebuiltBootclassPathModule = android.FixtureAddTextFile("prebuilt/Android.bp", ` |
| 61 | java_import { |
| 62 | name: "foo", |
| 63 | jars: ["a.jar"], |
| 64 | compile_dex: true, |
| 65 | prefer: false, |
| 66 | } |
| 67 | `) |
| 68 | |
| 69 | var addPrebuiltPreferredBootclassPathModule = android.FixtureAddTextFile("prebuilt/Android.bp", ` |
| 70 | java_import { |
| 71 | name: "foo", |
| 72 | jars: ["a.jar"], |
| 73 | compile_dex: true, |
| 74 | prefer: true, |
| 75 | } |
| 76 | `) |
| 77 | |
| 78 | t.Run("missing", func(t *testing.T) { |
| 79 | preparer. |
| 80 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"platform-bootclasspath" depends on undefined module "foo"`)). |
| 81 | RunTest(t) |
| 82 | }) |
| 83 | |
| 84 | t.Run("source", func(t *testing.T) { |
| 85 | result := android.GroupFixturePreparers( |
| 86 | preparer, |
| 87 | addSourceBootclassPathModule, |
| 88 | ).RunTest(t) |
| 89 | |
| 90 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 91 | "platform:foo", |
| 92 | "platform:bar", |
| 93 | }) |
| 94 | }) |
| 95 | |
| 96 | t.Run("prebuilt", func(t *testing.T) { |
| 97 | result := android.GroupFixturePreparers( |
| 98 | preparer, |
| 99 | addPrebuiltBootclassPathModule, |
| 100 | ).RunTest(t) |
| 101 | |
| 102 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 103 | "platform:prebuilt_foo", |
| 104 | "platform:bar", |
| 105 | }) |
| 106 | }) |
| 107 | |
| 108 | t.Run("source+prebuilt - source preferred", func(t *testing.T) { |
| 109 | result := android.GroupFixturePreparers( |
| 110 | preparer, |
| 111 | addSourceBootclassPathModule, |
| 112 | addPrebuiltBootclassPathModule, |
| 113 | ).RunTest(t) |
| 114 | |
| 115 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 116 | "platform:foo", |
| 117 | "platform:bar", |
| 118 | }) |
| 119 | }) |
| 120 | |
| 121 | t.Run("source+prebuilt - prebuilt preferred", func(t *testing.T) { |
| 122 | result := android.GroupFixturePreparers( |
| 123 | preparer, |
| 124 | addSourceBootclassPathModule, |
| 125 | addPrebuiltPreferredBootclassPathModule, |
| 126 | ).RunTest(t) |
| 127 | |
| 128 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 129 | "platform:prebuilt_foo", |
| 130 | "platform:bar", |
| 131 | }) |
| 132 | }) |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 133 | } |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 134 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 135 | func TestPlatformBootclasspathVariant(t *testing.T) { |
| 136 | result := android.GroupFixturePreparers( |
| 137 | prepareForTestWithPlatformBootclasspath, |
| 138 | android.FixtureWithRootAndroidBp(` |
| 139 | platform_bootclasspath { |
| 140 | name: "platform-bootclasspath", |
| 141 | } |
| 142 | `), |
| 143 | ).RunTest(t) |
| 144 | |
| 145 | variants := result.ModuleVariantsForTests("platform-bootclasspath") |
| 146 | android.AssertIntEquals(t, "expect 1 variant", 1, len(variants)) |
| 147 | } |
| 148 | |
| 149 | func TestPlatformBootclasspath_ClasspathFragmentPaths(t *testing.T) { |
| 150 | result := android.GroupFixturePreparers( |
| 151 | prepareForTestWithPlatformBootclasspath, |
| 152 | android.FixtureWithRootAndroidBp(` |
| 153 | platform_bootclasspath { |
| 154 | name: "platform-bootclasspath", |
| 155 | } |
| 156 | `), |
| 157 | ).RunTest(t) |
| 158 | |
| 159 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 160 | android.AssertStringEquals(t, "output filepath", p.Name()+".pb", p.ClasspathFragmentBase.outputFilepath.Base()) |
| 161 | android.AssertPathRelativeToTopEquals(t, "install filepath", "out/soong/target/product/test_device/system/etc/classpaths", p.ClasspathFragmentBase.installDirPath) |
| 162 | } |
| 163 | |
| 164 | func TestPlatformBootclasspathModule_AndroidMkEntries(t *testing.T) { |
| 165 | preparer := android.GroupFixturePreparers( |
| 166 | prepareForTestWithPlatformBootclasspath, |
| 167 | android.FixtureWithRootAndroidBp(` |
| 168 | platform_bootclasspath { |
| 169 | name: "platform-bootclasspath", |
| 170 | } |
| 171 | `), |
| 172 | ) |
| 173 | |
| 174 | t.Run("AndroidMkEntries", func(t *testing.T) { |
| 175 | result := preparer.RunTest(t) |
| 176 | |
| 177 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 178 | |
| 179 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 180 | android.AssertIntEquals(t, "AndroidMkEntries count", 2, len(entries)) |
| 181 | }) |
| 182 | |
| 183 | t.Run("hiddenapi-flags-entry", func(t *testing.T) { |
| 184 | result := preparer.RunTest(t) |
| 185 | |
| 186 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 187 | |
| 188 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 189 | got := entries[0].OutputFile |
| 190 | android.AssertBoolEquals(t, "valid output path", true, got.Valid()) |
| 191 | android.AssertSame(t, "output filepath", p.hiddenAPIFlagsCSV, got.Path()) |
| 192 | }) |
| 193 | |
| 194 | t.Run("classpath-fragment-entry", func(t *testing.T) { |
| 195 | result := preparer.RunTest(t) |
| 196 | |
| 197 | want := map[string][]string{ |
| 198 | "LOCAL_MODULE": {"platform-bootclasspath"}, |
| 199 | "LOCAL_MODULE_CLASS": {"ETC"}, |
| 200 | "LOCAL_INSTALLED_MODULE_STEM": {"platform-bootclasspath.pb"}, |
| 201 | // Output and Install paths are tested separately in TestPlatformBootclasspath_ClasspathFragmentPaths |
| 202 | } |
| 203 | |
| 204 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 205 | |
| 206 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 207 | got := entries[1] |
| 208 | for k, expectedValue := range want { |
| 209 | if value, ok := got.EntryMap[k]; ok { |
| 210 | android.AssertDeepEquals(t, k, expectedValue, value) |
| 211 | } else { |
| 212 | t.Errorf("No %s defined, saw %q", k, got.EntryMap) |
| 213 | } |
| 214 | } |
| 215 | }) |
| 216 | } |
| 217 | |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 218 | func TestPlatformBootclasspath_Dist(t *testing.T) { |
| 219 | result := android.GroupFixturePreparers( |
| 220 | prepareForTestWithPlatformBootclasspath, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 221 | FixtureConfigureBootJars("platform:foo", "platform:bar"), |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 222 | android.PrepareForTestWithAndroidMk, |
| 223 | android.FixtureWithRootAndroidBp(` |
| 224 | platform_bootclasspath { |
| 225 | name: "platform-bootclasspath", |
| 226 | dists: [ |
| 227 | { |
| 228 | targets: ["droidcore"], |
| 229 | tag: "hiddenapi-flags.csv", |
| 230 | }, |
| 231 | ], |
| 232 | } |
| 233 | |
| 234 | java_library { |
| 235 | name: "bar", |
| 236 | srcs: ["a.java"], |
| 237 | system_modules: "none", |
| 238 | sdk_version: "none", |
| 239 | compile_dex: true, |
| 240 | } |
| 241 | |
| 242 | java_library { |
| 243 | name: "foo", |
| 244 | srcs: ["a.java"], |
| 245 | system_modules: "none", |
| 246 | sdk_version: "none", |
| 247 | compile_dex: true, |
| 248 | } |
| 249 | `), |
| 250 | ).RunTest(t) |
| 251 | |
| 252 | platformBootclasspath := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 253 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, platformBootclasspath) |
| 254 | goals := entries[0].GetDistForGoals(platformBootclasspath) |
| 255 | android.AssertStringEquals(t, "platform dist goals phony", ".PHONY: droidcore\n", goals[0]) |
| 256 | android.AssertStringEquals(t, "platform dist goals call", "$(call dist-for-goals,droidcore,out/soong/hiddenapi/hiddenapi-flags.csv:hiddenapi-flags.csv)\n", android.StringRelativeToTop(result.Config, goals[1])) |
| 257 | } |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 258 | |
Paul Duffin | 85dee5d | 2021-04-13 00:14:38 +0100 | [diff] [blame] | 259 | func TestPlatformBootclasspath_HiddenAPIMonolithicFiles(t *testing.T) { |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 260 | result := android.GroupFixturePreparers( |
| 261 | hiddenApiFixtureFactory, |
| 262 | PrepareForTestWithJavaSdkLibraryFiles, |
| 263 | FixtureWithLastReleaseApis("bar"), |
| 264 | FixtureConfigureBootJars("platform:foo", "platform:bar"), |
| 265 | ).RunTestWithBp(t, ` |
| 266 | java_library { |
| 267 | name: "foo", |
| 268 | srcs: ["a.java"], |
| 269 | compile_dex: true, |
| 270 | |
| 271 | hiddenapi_additional_annotations: [ |
| 272 | "foo-hiddenapi-annotations", |
| 273 | ], |
| 274 | } |
| 275 | |
| 276 | java_library { |
| 277 | name: "foo-hiddenapi-annotations", |
| 278 | srcs: ["a.java"], |
| 279 | compile_dex: true, |
| 280 | } |
| 281 | |
| 282 | java_import { |
| 283 | name: "foo", |
| 284 | jars: ["a.jar"], |
| 285 | compile_dex: true, |
| 286 | prefer: false, |
| 287 | } |
| 288 | |
| 289 | java_sdk_library { |
| 290 | name: "bar", |
| 291 | srcs: ["a.java"], |
| 292 | compile_dex: true, |
| 293 | } |
| 294 | |
| 295 | platform_bootclasspath { |
| 296 | name: "myplatform-bootclasspath", |
| 297 | } |
| 298 | `) |
| 299 | |
| 300 | platformBootclasspath := result.ModuleForTests("myplatform-bootclasspath", "android_common") |
| 301 | indexRule := platformBootclasspath.Rule("platform-bootclasspath-monolithic-hiddenapi-index") |
| 302 | CheckHiddenAPIRuleInputs(t, ` |
| 303 | .intermediates/bar/android_common/hiddenapi/index.csv |
| 304 | .intermediates/foo/android_common/hiddenapi/index.csv |
| 305 | `, |
| 306 | indexRule) |
| 307 | |
| 308 | // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that |
| 309 | // creates the index.csv file. |
| 310 | foo := result.ModuleForTests("foo", "android_common") |
| 311 | indexParams := foo.Output("hiddenapi/index.csv") |
| 312 | CheckHiddenAPIRuleInputs(t, ` |
| 313 | .intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar |
| 314 | .intermediates/foo/android_common/javac/foo.jar |
| 315 | `, indexParams) |
| 316 | } |