Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [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 java |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Paul Duffin | cee7e66 | 2020-07-09 17:32:57 +0100 | [diff] [blame] | 19 | "reflect" |
| 20 | "sort" |
| 21 | "testing" |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
Colin Cross | f28329d | 2020-02-15 11:00:10 -0800 | [diff] [blame] | 24 | "android/soong/cc" |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 25 | "android/soong/dexpreopt" |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 26 | "android/soong/python" |
| 27 | |
Paul Duffin | cee7e66 | 2020-07-09 17:32:57 +0100 | [diff] [blame] | 28 | "github.com/google/blueprint" |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 31 | func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) android.Config { |
| 32 | bp += GatherRequiredDepsForTest() |
| 33 | |
| 34 | mockFS := map[string][]byte{ |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 35 | "api/current.txt": nil, |
| 36 | "api/removed.txt": nil, |
| 37 | "api/system-current.txt": nil, |
| 38 | "api/system-removed.txt": nil, |
| 39 | "api/test-current.txt": nil, |
| 40 | "api/test-removed.txt": nil, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 41 | |
Anton Hansson | dff2c78 | 2020-12-21 17:10:01 +0000 | [diff] [blame] | 42 | "prebuilts/sdk/tools/core-lambda-stubs.jar": nil, |
| 43 | "prebuilts/sdk/Android.bp": []byte(`prebuilt_apis { name: "sdk", api_dirs: ["14", "28", "30", "current"], imports_sdk_version: "none", imports_compile_dex:true,}`), |
Paul Duffin | 0c5bae5 | 2020-06-02 13:00:08 +0100 | [diff] [blame] | 44 | |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 45 | "bin.py": nil, |
| 46 | python.StubTemplateHost: []byte(`PYTHON_BINARY = '%interpreter%' |
| 47 | MAIN_FILE = '%main%'`), |
| 48 | |
Paul Duffin | 0c5bae5 | 2020-06-02 13:00:08 +0100 | [diff] [blame] | 49 | // For java_sdk_library |
Colin Cross | 300b266 | 2020-06-11 12:12:30 -0700 | [diff] [blame] | 50 | "api/module-lib-current.txt": nil, |
| 51 | "api/module-lib-removed.txt": nil, |
| 52 | "api/system-server-current.txt": nil, |
| 53 | "api/system-server-removed.txt": nil, |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Anton Hansson | dff2c78 | 2020-12-21 17:10:01 +0000 | [diff] [blame] | 56 | levels := []string{"14", "28", "29", "30", "current"} |
| 57 | libs := []string{ |
| 58 | "android", "foo", "bar", "sdklib", "barney", "betty", "foo-shared_library", |
| 59 | "foo-no_shared_library", "core-for-system-modules", "quuz", "qux", "fred", |
| 60 | "runtime-library", |
| 61 | } |
| 62 | for k, v := range prebuiltApisFilesForLibs(levels, libs) { |
| 63 | mockFS[k] = v |
| 64 | } |
| 65 | |
Colin Cross | f28329d | 2020-02-15 11:00:10 -0800 | [diff] [blame] | 66 | cc.GatherRequiredFilesForTest(mockFS) |
| 67 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 68 | for k, v := range fs { |
| 69 | mockFS[k] = v |
| 70 | } |
| 71 | |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 72 | if env == nil { |
| 73 | env = make(map[string]string) |
| 74 | } |
| 75 | if env["ANDROID_JAVA8_HOME"] == "" { |
| 76 | env["ANDROID_JAVA8_HOME"] = "jdk8" |
| 77 | } |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 78 | config := android.TestArchConfig(buildDir, env, bp, mockFS) |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 79 | |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 80 | return config |
| 81 | } |
| 82 | |
Anton Hansson | dff2c78 | 2020-12-21 17:10:01 +0000 | [diff] [blame] | 83 | func prebuiltApisFilesForLibs(apiLevels []string, sdkLibs []string) map[string][]byte { |
| 84 | fs := make(map[string][]byte) |
| 85 | for _, level := range apiLevels { |
| 86 | for _, lib := range sdkLibs { |
| 87 | for _, scope := range []string{"public", "system", "module-lib", "system-server", "test"} { |
| 88 | fs[fmt.Sprintf("prebuilts/sdk/%s/%s/%s.jar", level, scope, lib)] = nil |
Anton Hansson | 370fd0b | 2021-01-22 15:05:04 +0000 | [diff] [blame^] | 89 | // No finalized API files for "current" |
| 90 | if level != "current" { |
| 91 | fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, scope, lib)] = nil |
| 92 | fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, scope, lib)] = nil |
| 93 | } |
Anton Hansson | dff2c78 | 2020-12-21 17:10:01 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | fs[fmt.Sprintf("prebuilts/sdk/%s/public/framework.aidl", level)] = nil |
| 97 | } |
| 98 | return fs |
| 99 | } |
| 100 | |
Paul Duffin | c059c8c | 2021-01-20 17:13:52 +0000 | [diff] [blame] | 101 | // Register build components provided by this package that are needed by tests. |
| 102 | // |
| 103 | // In particular this must register all the components that are used in the `Android.bp` snippet |
| 104 | // returned by GatherRequiredDepsForTest() |
| 105 | func RegisterRequiredBuildComponentsForTest(ctx android.RegistrationContext) { |
| 106 | RegisterAARBuildComponents(ctx) |
| 107 | RegisterAppBuildComponents(ctx) |
| 108 | RegisterAppImportBuildComponents(ctx) |
| 109 | RegisterAppSetBuildComponents(ctx) |
| 110 | RegisterDexpreoptBootJarsComponents(ctx) |
| 111 | RegisterDocsBuildComponents(ctx) |
| 112 | RegisterGenRuleBuildComponents(ctx) |
| 113 | RegisterJavaBuildComponents(ctx) |
| 114 | RegisterPrebuiltApisBuildComponents(ctx) |
| 115 | RegisterRuntimeResourceOverlayBuildComponents(ctx) |
| 116 | RegisterSdkLibraryBuildComponents(ctx) |
| 117 | RegisterStubsBuildComponents(ctx) |
| 118 | RegisterSystemModulesBuildComponents(ctx) |
| 119 | } |
| 120 | |
| 121 | // Gather the module definitions needed by tests that depend upon code from this package. |
| 122 | // |
| 123 | // Returns an `Android.bp` snippet that defines the modules that are needed by this package. |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 124 | func GatherRequiredDepsForTest() string { |
| 125 | var bp string |
| 126 | |
| 127 | extraModules := []string{ |
| 128 | "core-lambda-stubs", |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 129 | "ext", |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 130 | "android_stubs_current", |
| 131 | "android_system_stubs_current", |
| 132 | "android_test_stubs_current", |
Jiyong Park | 50146e9 | 2020-01-30 18:00:15 +0900 | [diff] [blame] | 133 | "android_module_lib_stubs_current", |
Anton Hansson | ba6ab2e | 2020-03-19 15:23:38 +0000 | [diff] [blame] | 134 | "android_system_server_stubs_current", |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 135 | "core.current.stubs", |
Pete Gillin | 1f41dbf | 2020-06-02 15:59:45 +0100 | [diff] [blame] | 136 | "legacy.core.platform.api.stubs", |
| 137 | "stable.core.platform.api.stubs", |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 138 | "kotlin-stdlib", |
Colin Cross | 0b03d97 | 2019-05-13 11:06:25 -0700 | [diff] [blame] | 139 | "kotlin-stdlib-jdk7", |
| 140 | "kotlin-stdlib-jdk8", |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 141 | "kotlin-annotations", |
| 142 | } |
| 143 | |
| 144 | for _, extra := range extraModules { |
| 145 | bp += fmt.Sprintf(` |
| 146 | java_library { |
| 147 | name: "%s", |
| 148 | srcs: ["a.java"], |
Paul Duffin | 52d398a | 2019-06-11 12:31:14 +0100 | [diff] [blame] | 149 | sdk_version: "none", |
Pete Gillin | 84c3807 | 2020-07-09 18:03:41 +0100 | [diff] [blame] | 150 | system_modules: "stable-core-platform-api-stubs-system-modules", |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 151 | compile_dex: true, |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 152 | } |
| 153 | `, extra) |
| 154 | } |
| 155 | |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 156 | // For class loader context and <uses-library> tests. |
| 157 | dexpreoptModules := []string{"android.test.runner"} |
| 158 | dexpreoptModules = append(dexpreoptModules, dexpreopt.CompatUsesLibs...) |
| 159 | dexpreoptModules = append(dexpreoptModules, dexpreopt.OptionalCompatUsesLibs...) |
| 160 | |
| 161 | for _, extra := range dexpreoptModules { |
| 162 | bp += fmt.Sprintf(` |
| 163 | java_library { |
| 164 | name: "%s", |
| 165 | srcs: ["a.java"], |
| 166 | sdk_version: "none", |
| 167 | system_modules: "stable-core-platform-api-stubs-system-modules", |
| 168 | compile_dex: true, |
| 169 | installable: true, |
| 170 | } |
| 171 | `, extra) |
| 172 | } |
| 173 | |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 174 | bp += ` |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 175 | java_library { |
| 176 | name: "framework", |
| 177 | srcs: ["a.java"], |
Paul Duffin | a3d0986 | 2019-06-11 13:40:47 +0100 | [diff] [blame] | 178 | sdk_version: "none", |
Pete Gillin | 84c3807 | 2020-07-09 18:03:41 +0100 | [diff] [blame] | 179 | system_modules: "stable-core-platform-api-stubs-system-modules", |
Colin Cross | 3047fa2 | 2019-04-18 10:56:44 -0700 | [diff] [blame] | 180 | aidl: { |
| 181 | export_include_dirs: ["framework/aidl"], |
| 182 | }, |
| 183 | } |
| 184 | |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 185 | android_app { |
| 186 | name: "framework-res", |
Paul Duffin | 50c217c | 2019-06-12 13:25:22 +0100 | [diff] [blame] | 187 | sdk_version: "core_platform", |
Ulya Trafimovich | 24813e1 | 2020-10-07 15:05:21 +0100 | [diff] [blame] | 188 | }` |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 189 | |
| 190 | systemModules := []string{ |
Neil Fuller | ba88c41 | 2018-10-21 22:57:26 +0100 | [diff] [blame] | 191 | "core-current-stubs-system-modules", |
Pete Gillin | 1f41dbf | 2020-06-02 15:59:45 +0100 | [diff] [blame] | 192 | "legacy-core-platform-api-stubs-system-modules", |
Pete Gillin | 40a0642 | 2020-07-01 10:59:00 +0100 | [diff] [blame] | 193 | "stable-core-platform-api-stubs-system-modules", |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | for _, extra := range systemModules { |
| 197 | bp += fmt.Sprintf(` |
| 198 | java_system_modules { |
Paul Duffin | 68289b0 | 2019-09-20 13:50:52 +0100 | [diff] [blame] | 199 | name: "%[1]s", |
| 200 | libs: ["%[1]s-lib"], |
| 201 | } |
| 202 | java_library { |
| 203 | name: "%[1]s-lib", |
| 204 | sdk_version: "none", |
| 205 | system_modules: "none", |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 206 | } |
| 207 | `, extra) |
| 208 | } |
| 209 | |
Paul Duffin | 1ab6186 | 2021-01-20 17:44:53 +0000 | [diff] [blame] | 210 | // Make sure that the dex_bootjars singleton module is instantiated for the tests. |
| 211 | bp += ` |
| 212 | dex_bootjars { |
| 213 | name: "dex_bootjars", |
| 214 | } |
| 215 | ` |
| 216 | |
Colin Cross | b197453 | 2019-02-15 10:37:39 -0800 | [diff] [blame] | 217 | return bp |
| 218 | } |
Paul Duffin | cee7e66 | 2020-07-09 17:32:57 +0100 | [diff] [blame] | 219 | |
| 220 | func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) { |
| 221 | t.Helper() |
| 222 | module := ctx.ModuleForTests(name, variant).Module() |
| 223 | deps := []string{} |
| 224 | ctx.VisitDirectDeps(module, func(m blueprint.Module) { |
| 225 | deps = append(deps, m.Name()) |
| 226 | }) |
| 227 | sort.Strings(deps) |
| 228 | |
| 229 | if actual := deps; !reflect.DeepEqual(expected, actual) { |
| 230 | t.Errorf("expected %#q, found %#q", expected, actual) |
| 231 | } |
| 232 | } |