Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1 | // Copyright 2020 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 ( |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 19 | "path/filepath" |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 20 | "testing" |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 21 | |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 22 | "android/soong/android" |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 23 | |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 27 | // TODO(b/177892522): Move these tests into a more appropriate place. |
| 28 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 29 | func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer { |
| 30 | return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 31 | variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir |
| 32 | }) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 35 | var prepareForTestWithDefaultPlatformBootclasspath = android.FixtureAddTextFile("frameworks/base/boot/Android.bp", ` |
| 36 | platform_bootclasspath { |
| 37 | name: "platform-bootclasspath", |
| 38 | } |
| 39 | `) |
| 40 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 41 | var hiddenApiFixtureFactory = android.GroupFixturePreparers( |
| 42 | prepareForJavaTest, PrepareForTestWithHiddenApiBuildComponents) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 43 | |
| 44 | func TestHiddenAPISingleton(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 45 | result := android.GroupFixturePreparers( |
| 46 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 47 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 48 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 49 | ).RunTestWithBp(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 50 | java_library { |
| 51 | name: "foo", |
| 52 | srcs: ["a.java"], |
| 53 | compile_dex: true, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 54 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 55 | `) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 56 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 57 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 58 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 59 | want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 60 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 63 | func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) { |
Paul Duffin | b6f53c0 | 2021-05-14 07:52:42 +0100 | [diff] [blame] | 64 | expectedErrorMessage := "module prebuilt_foo{os:android,arch:common} does not provide a dex jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 65 | |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 66 | android.GroupFixturePreparers( |
| 67 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 68 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 69 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 70 | ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)). |
| 71 | RunTestWithBp(t, ` |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 72 | java_library { |
| 73 | name: "foo", |
| 74 | srcs: ["a.java"], |
| 75 | compile_dex: true, |
| 76 | } |
| 77 | |
| 78 | java_import { |
| 79 | name: "foo", |
| 80 | jars: ["a.jar"], |
| 81 | prefer: true, |
| 82 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 83 | `) |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 86 | func TestHiddenAPISingletonWithPrebuilt(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 87 | result := android.GroupFixturePreparers( |
| 88 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 89 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 90 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 91 | ).RunTestWithBp(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 92 | java_import { |
| 93 | name: "foo", |
| 94 | jars: ["a.jar"], |
| 95 | compile_dex: true, |
| 96 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 97 | `) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 98 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 99 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 100 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 101 | want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 102 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 106 | result := android.GroupFixturePreparers( |
| 107 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 108 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 109 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 110 | ).RunTestWithBp(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 111 | java_library { |
| 112 | name: "foo", |
| 113 | srcs: ["a.java"], |
| 114 | compile_dex: true, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 115 | } |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 116 | |
| 117 | java_import { |
| 118 | name: "foo", |
| 119 | jars: ["a.jar"], |
| 120 | compile_dex: true, |
| 121 | prefer: false, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 122 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 123 | `) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 124 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 125 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 126 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 127 | fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 128 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 129 | |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 130 | prebuiltJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/dex/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 131 | android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 135 | result := android.GroupFixturePreparers( |
| 136 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 137 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 138 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 139 | ).RunTestWithBp(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 140 | java_library { |
| 141 | name: "foo", |
| 142 | srcs: ["a.java"], |
| 143 | compile_dex: true, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 144 | } |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 145 | |
| 146 | java_import { |
| 147 | name: "foo", |
| 148 | jars: ["a.jar"], |
| 149 | compile_dex: true, |
| 150 | prefer: true, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 151 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 152 | `) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 153 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 154 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 155 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 156 | prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 157 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 158 | |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 159 | fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 160 | android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 161 | } |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 162 | |
| 163 | func TestHiddenAPISingletonSdks(t *testing.T) { |
| 164 | testCases := []struct { |
| 165 | name string |
| 166 | unbundledBuild bool |
| 167 | publicStub string |
| 168 | systemStub string |
| 169 | testStub string |
| 170 | corePlatformStub string |
Paul Duffin | dc92abb | 2021-03-13 08:28:35 +0000 | [diff] [blame] | 171 | |
| 172 | // Additional test preparer |
| 173 | preparer android.FixturePreparer |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 174 | }{ |
| 175 | { |
| 176 | name: "testBundled", |
| 177 | unbundledBuild: false, |
| 178 | publicStub: "android_stubs_current", |
| 179 | systemStub: "android_system_stubs_current", |
| 180 | testStub: "android_test_stubs_current", |
| 181 | corePlatformStub: "legacy.core.platform.api.stubs", |
Paul Duffin | dc92abb | 2021-03-13 08:28:35 +0000 | [diff] [blame] | 182 | preparer: android.GroupFixturePreparers(), |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 183 | }, { |
| 184 | name: "testUnbundled", |
| 185 | unbundledBuild: true, |
| 186 | publicStub: "sdk_public_current_android", |
| 187 | systemStub: "sdk_system_current_android", |
| 188 | testStub: "sdk_test_current_android", |
| 189 | corePlatformStub: "legacy.core.platform.api.stubs", |
Paul Duffin | dc92abb | 2021-03-13 08:28:35 +0000 | [diff] [blame] | 190 | preparer: PrepareForTestWithPrebuiltsOfCurrentApi, |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 191 | }, |
| 192 | } |
| 193 | for _, tc := range testCases { |
| 194 | t.Run(tc.name, func(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 195 | result := android.GroupFixturePreparers( |
| 196 | hiddenApiFixtureFactory, |
Paul Duffin | dc92abb | 2021-03-13 08:28:35 +0000 | [diff] [blame] | 197 | tc.preparer, |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 198 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 199 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 200 | variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild) |
| 201 | }), |
| 202 | ).RunTest(t) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 203 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 204 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 205 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 206 | wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild) |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 207 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 208 | |
| 209 | wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild) |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 210 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 211 | |
| 212 | wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild) |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 213 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 214 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 215 | wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub) |
| 216 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 217 | }) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func generateDexedPath(subDir, dex, module string) string { |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 222 | return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 225 | func generateDexPath(moduleDir string, module string) string { |
| 226 | return generateDexedPath(filepath.Join(moduleDir, module), "dex", module) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | func generateSdkDexPath(module string, unbundled bool) string { |
| 230 | if unbundled { |
| 231 | return generateDexedPath("prebuilts/sdk/"+module, "dex", module) |
| 232 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 233 | return generateDexPath(defaultJavaDir, module) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 234 | } |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 235 | |
| 236 | func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) { |
| 237 | |
| 238 | // The idea behind this test is to ensure that when the build is |
| 239 | // confugured with a PrebuiltHiddenApiDir that the rules for the |
| 240 | // hiddenapi singleton copy the prebuilts to the typical output |
| 241 | // location, and then use that output location for the hiddenapi encode |
| 242 | // dex step. |
| 243 | |
| 244 | // Where to find the prebuilt hiddenapi files: |
| 245 | prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi" |
| 246 | |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 247 | result := android.GroupFixturePreparers( |
| 248 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 249 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 250 | fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir), |
| 251 | ).RunTestWithBp(t, ` |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 252 | java_import { |
| 253 | name: "foo", |
| 254 | jars: ["a.jar"], |
| 255 | compile_dex: true, |
| 256 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 257 | `) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 258 | |
| 259 | expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv" |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 260 | expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv" |
| 261 | expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv" |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 262 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 263 | foo := result.ModuleForTests("foo", "android_common") |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 264 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 265 | hiddenAPI := result.SingletonForTests("hiddenapi") |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 266 | cpRule := hiddenAPI.Rule("Cp") |
| 267 | actualCpInput := cpRule.BuildParams.Input |
| 268 | actualCpOutput := cpRule.BuildParams.Output |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 269 | encodeDexRule := foo.Rule("hiddenAPIEncodeDex") |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 270 | actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"] |
| 271 | |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 272 | android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 273 | |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 274 | android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 275 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 276 | android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 277 | } |
Paul Duffin | 0d58658 | 2021-05-14 15:03:30 +0100 | [diff] [blame] | 278 | |
| 279 | func TestHiddenAPIEncoding_JavaSdkLibrary(t *testing.T) { |
| 280 | |
| 281 | result := android.GroupFixturePreparers( |
| 282 | hiddenApiFixtureFactory, |
| 283 | FixtureConfigureBootJars("platform:foo"), |
| 284 | PrepareForTestWithJavaSdkLibraryFiles, |
| 285 | FixtureWithLastReleaseApis("foo"), |
| 286 | |
| 287 | // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding |
| 288 | // is disabled. |
| 289 | android.FixtureAddTextFile("frameworks/base/Android.bp", ""), |
| 290 | ).RunTestWithBp(t, ` |
| 291 | java_sdk_library { |
| 292 | name: "foo", |
| 293 | srcs: ["a.java"], |
| 294 | shared_library: false, |
| 295 | compile_dex: true, |
| 296 | public: {enabled: true}, |
| 297 | } |
| 298 | `) |
| 299 | |
| 300 | checkDexEncoded := func(t *testing.T, name, unencodedDexJar, encodedDexJar string) { |
| 301 | moduleForTests := result.ModuleForTests(name, "android_common") |
| 302 | |
| 303 | encodeDexRule := moduleForTests.Rule("hiddenAPIEncodeDex") |
| 304 | actualUnencodedDexJar := encodeDexRule.Input |
| 305 | |
| 306 | // Make sure that the module has its dex jar encoded. |
| 307 | android.AssertStringEquals(t, "encode embedded java_library", unencodedDexJar, actualUnencodedDexJar.String()) |
| 308 | |
| 309 | // Make sure that the encoded dex jar is the exported one. |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 310 | exportedDexJar := moduleForTests.Module().(UsesLibraryDependency).DexJarBuildPath().Path() |
Paul Duffin | 0d58658 | 2021-05-14 15:03:30 +0100 | [diff] [blame] | 311 | android.AssertPathRelativeToTopEquals(t, "encode embedded java_library", encodedDexJar, exportedDexJar) |
| 312 | } |
| 313 | |
| 314 | // The java_library embedded with the java_sdk_library must be dex encoded. |
| 315 | t.Run("foo", func(t *testing.T) { |
| 316 | expectedUnencodedDexJar := "out/soong/.intermediates/foo/android_common/aligned/foo.jar" |
| 317 | expectedEncodedDexJar := "out/soong/.intermediates/foo/android_common/hiddenapi/foo.jar" |
| 318 | checkDexEncoded(t, "foo", expectedUnencodedDexJar, expectedEncodedDexJar) |
| 319 | }) |
| 320 | |
| 321 | // The dex jar of the child implementation java_library of the java_sdk_library is not currently |
| 322 | // dex encoded. |
| 323 | t.Run("foo.impl", func(t *testing.T) { |
| 324 | fooImpl := result.ModuleForTests("foo.impl", "android_common") |
| 325 | encodeDexRule := fooImpl.MaybeRule("hiddenAPIEncodeDex") |
| 326 | if encodeDexRule.Rule != nil { |
| 327 | t.Errorf("foo.impl is not expected to be encoded") |
| 328 | } |
| 329 | }) |
| 330 | } |