Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +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 java |
| 16 | |
| 17 | import ( |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 18 | "reflect" |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 19 | "testing" |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
Yu Shan | b7646e4 | 2024-05-17 20:09:23 +0000 | [diff] [blame] | 22 | "android/soong/cc" |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 23 | ) |
| 24 | |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 25 | func TestRequired(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 26 | t.Parallel() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 27 | ctx, _ := testJava(t, ` |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 28 | java_library { |
| 29 | name: "foo", |
| 30 | srcs: ["a.java"], |
| 31 | required: ["libfoo"], |
| 32 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 33 | `) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 34 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame^] | 35 | mod := ctx.ModuleForTests(t, "foo", "android_common").Module() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 36 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 37 | |
| 38 | expected := []string{"libfoo"} |
| 39 | actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"] |
| 40 | if !reflect.DeepEqual(expected, actual) { |
| 41 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) |
| 42 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | func TestHostdex(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 46 | t.Parallel() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 47 | ctx, _ := testJava(t, ` |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 48 | java_library { |
| 49 | name: "foo", |
| 50 | srcs: ["a.java"], |
| 51 | hostdex: true, |
| 52 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 53 | `) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 54 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame^] | 55 | mod := ctx.ModuleForTests(t, "foo", "android_common").Module() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 56 | entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 57 | if len(entriesList) != 2 { |
| 58 | t.Errorf("two entries are expected, but got %d", len(entriesList)) |
| 59 | } |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 60 | |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 61 | mainEntries := &entriesList[0] |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 62 | expected := []string{"foo"} |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 63 | actual := mainEntries.EntryMap["LOCAL_MODULE"] |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 64 | if !reflect.DeepEqual(expected, actual) { |
| 65 | t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual) |
| 66 | } |
| 67 | |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 68 | subEntries := &entriesList[1] |
| 69 | expected = []string{"foo-hostdex"} |
| 70 | actual = subEntries.EntryMap["LOCAL_MODULE"] |
| 71 | if !reflect.DeepEqual(expected, actual) { |
| 72 | t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual) |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 73 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | func TestHostdexRequired(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 77 | t.Parallel() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 78 | ctx, _ := testJava(t, ` |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 79 | java_library { |
| 80 | name: "foo", |
| 81 | srcs: ["a.java"], |
| 82 | hostdex: true, |
| 83 | required: ["libfoo"], |
| 84 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 85 | `) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 86 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame^] | 87 | mod := ctx.ModuleForTests(t, "foo", "android_common").Module() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 88 | entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 89 | if len(entriesList) != 2 { |
| 90 | t.Errorf("two entries are expected, but got %d", len(entriesList)) |
| 91 | } |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 92 | |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 93 | mainEntries := &entriesList[0] |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 94 | expected := []string{"libfoo"} |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 95 | actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"] |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 96 | if !reflect.DeepEqual(expected, actual) { |
| 97 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) |
| 98 | } |
| 99 | |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 100 | subEntries := &entriesList[1] |
| 101 | expected = []string{"libfoo"} |
| 102 | actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"] |
| 103 | if !reflect.DeepEqual(expected, actual) { |
| 104 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 105 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | func TestHostdexSpecificRequired(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 109 | t.Parallel() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 110 | ctx, _ := testJava(t, ` |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 111 | java_library { |
| 112 | name: "foo", |
| 113 | srcs: ["a.java"], |
| 114 | hostdex: true, |
| 115 | target: { |
| 116 | hostdex: { |
| 117 | required: ["libfoo"], |
| 118 | }, |
| 119 | }, |
| 120 | } |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 121 | `) |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 122 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame^] | 123 | mod := ctx.ModuleForTests(t, "foo", "android_common").Module() |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 124 | entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 125 | if len(entriesList) != 2 { |
| 126 | t.Errorf("two entries are expected, but got %d", len(entriesList)) |
| 127 | } |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 128 | |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 129 | mainEntries := &entriesList[0] |
| 130 | if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok { |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 131 | t.Errorf("Unexpected required modules: %q", r) |
| 132 | } |
| 133 | |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 134 | subEntries := &entriesList[1] |
| 135 | expected := []string{"libfoo"} |
| 136 | actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"] |
| 137 | if !reflect.DeepEqual(expected, actual) { |
| 138 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) |
Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 139 | } |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 140 | } |
Anton Hansson | 78156ef | 2020-03-27 19:39:48 +0000 | [diff] [blame] | 141 | |
Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 142 | func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 143 | t.Parallel() |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 144 | result := android.GroupFixturePreparers( |
| 145 | prepareForJavaTest, |
Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 146 | PrepareForTestWithJavaSdkLibraryFiles, |
| 147 | FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"), |
| 148 | ).RunTestWithBp(t, ` |
Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 149 | java_sdk_library { |
| 150 | name: "foo-shared_library", |
| 151 | srcs: ["a.java"], |
| 152 | } |
| 153 | java_sdk_library { |
| 154 | name: "foo-no_shared_library", |
| 155 | srcs: ["a.java"], |
| 156 | shared_library: false, |
| 157 | } |
| 158 | `) |
| 159 | |
| 160 | // Verify the existence of internal modules |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame^] | 161 | result.ModuleForTests(t, "foo-shared_library.xml", "android_common") |
Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 162 | |
| 163 | testCases := []struct { |
| 164 | moduleName string |
| 165 | expected []string |
| 166 | }{ |
Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 167 | {"foo-shared_library", []string{"foo-shared_library.impl", "foo-shared_library.xml"}}, |
| 168 | {"foo-no_shared_library", []string{"foo-no_shared_library.impl"}}, |
Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 169 | } |
| 170 | for _, tc := range testCases { |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame^] | 171 | mod := result.ModuleForTests(t, tc.moduleName, "android_common").Module() |
Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 172 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] |
Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 173 | actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"] |
| 174 | if !reflect.DeepEqual(tc.expected, actual) { |
| 175 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual) |
| 176 | } |
| 177 | } |
| 178 | } |
Bill Peckham | fb04df4 | 2021-01-11 12:27:24 -0800 | [diff] [blame] | 179 | |
| 180 | func TestImportSoongDexJar(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 181 | t.Parallel() |
Paul Duffin | f04311c | 2021-03-22 17:31:51 +0000 | [diff] [blame] | 182 | result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, ` |
Bill Peckham | fb04df4 | 2021-01-11 12:27:24 -0800 | [diff] [blame] | 183 | java_import { |
| 184 | name: "my-java-import", |
| 185 | jars: ["a.jar"], |
| 186 | prefer: true, |
| 187 | compile_dex: true, |
| 188 | } |
| 189 | `) |
| 190 | |
Paul Duffin | f04311c | 2021-03-22 17:31:51 +0000 | [diff] [blame] | 191 | mod := result.Module("my-java-import", "android_common") |
| 192 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] |
| 193 | expectedSoongDexJar := "out/soong/.intermediates/my-java-import/android_common/dex/my-java-import.jar" |
Bill Peckham | fb04df4 | 2021-01-11 12:27:24 -0800 | [diff] [blame] | 194 | actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"] |
| 195 | |
Paul Duffin | f04311c | 2021-03-22 17:31:51 +0000 | [diff] [blame] | 196 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar) |
Bill Peckham | fb04df4 | 2021-01-11 12:27:24 -0800 | [diff] [blame] | 197 | } |
Yuntao Xu | 7a31855 | 2021-05-27 10:30:26 -0700 | [diff] [blame] | 198 | |
| 199 | func TestAndroidTestHelperApp_LocalDisableTestConfig(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 200 | t.Parallel() |
Yuntao Xu | 7a31855 | 2021-05-27 10:30:26 -0700 | [diff] [blame] | 201 | ctx, _ := testJava(t, ` |
| 202 | android_test_helper_app { |
| 203 | name: "foo", |
| 204 | srcs: ["a.java"], |
| 205 | } |
| 206 | `) |
| 207 | |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame^] | 208 | mod := ctx.ModuleForTests(t, "foo", "android_common").Module() |
Yuntao Xu | 7a31855 | 2021-05-27 10:30:26 -0700 | [diff] [blame] | 209 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
| 210 | |
| 211 | expected := []string{"true"} |
| 212 | actual := entries.EntryMap["LOCAL_DISABLE_TEST_CONFIG"] |
| 213 | if !reflect.DeepEqual(expected, actual) { |
| 214 | t.Errorf("Unexpected flag value - expected: %q, actual: %q", expected, actual) |
| 215 | } |
| 216 | } |
zhidou | 198f589 | 2022-02-17 02:33:12 +0000 | [diff] [blame] | 217 | |
| 218 | func TestGetOverriddenPackages(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 219 | t.Parallel() |
zhidou | 198f589 | 2022-02-17 02:33:12 +0000 | [diff] [blame] | 220 | ctx, _ := testJava( |
| 221 | t, ` |
| 222 | android_app { |
| 223 | name: "foo", |
| 224 | srcs: ["a.java"], |
| 225 | sdk_version: "current", |
| 226 | overrides: ["qux"] |
| 227 | } |
| 228 | |
| 229 | override_android_app { |
| 230 | name: "foo_override", |
| 231 | base: "foo", |
| 232 | overrides: ["bar"] |
| 233 | } |
| 234 | `) |
| 235 | |
| 236 | expectedVariants := []struct { |
| 237 | name string |
| 238 | moduleName string |
| 239 | variantName string |
| 240 | overrides []string |
| 241 | }{ |
| 242 | { |
| 243 | name: "foo", |
| 244 | moduleName: "foo", |
| 245 | variantName: "android_common", |
| 246 | overrides: []string{"qux"}, |
| 247 | }, |
| 248 | { |
| 249 | name: "foo", |
| 250 | moduleName: "foo_override", |
| 251 | variantName: "android_common_foo_override", |
| 252 | overrides: []string{"bar", "foo"}, |
| 253 | }, |
| 254 | } |
| 255 | |
| 256 | for _, expected := range expectedVariants { |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame^] | 257 | mod := ctx.ModuleForTests(t, expected.name, expected.variantName).Module() |
zhidou | 198f589 | 2022-02-17 02:33:12 +0000 | [diff] [blame] | 258 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] |
| 259 | actual := entries.EntryMap["LOCAL_OVERRIDES_PACKAGES"] |
| 260 | |
| 261 | android.AssertDeepEquals(t, "overrides property", expected.overrides, actual) |
| 262 | } |
| 263 | } |
Yu Shan | b7646e4 | 2024-05-17 20:09:23 +0000 | [diff] [blame] | 264 | |
Jiyong Park | 25b9222 | 2024-05-17 22:58:54 +0000 | [diff] [blame] | 265 | func TestJniAsRequiredDeps(t *testing.T) { |
Colin Cross | 844cb6a | 2025-01-29 15:53:21 -0800 | [diff] [blame] | 266 | t.Parallel() |
Yu Shan | b7646e4 | 2024-05-17 20:09:23 +0000 | [diff] [blame] | 267 | ctx := android.GroupFixturePreparers( |
| 268 | PrepareForTestWithJavaDefaultModules, |
| 269 | cc.PrepareForTestWithCcDefaultModules, |
| 270 | android.PrepareForTestWithAndroidMk, |
Jiyong Park | 25b9222 | 2024-05-17 22:58:54 +0000 | [diff] [blame] | 271 | ).RunTestWithBp(t, ` |
| 272 | android_app { |
| 273 | name: "app", |
| 274 | jni_libs: ["libjni"], |
| 275 | platform_apis: true, |
| 276 | } |
| 277 | |
| 278 | android_app { |
| 279 | name: "app_embedded", |
| 280 | jni_libs: ["libjni"], |
| 281 | platform_apis: true, |
| 282 | use_embedded_native_libs: true, |
| 283 | } |
| 284 | |
| 285 | cc_library { |
| 286 | name: "libjni", |
| 287 | system_shared_libs: [], |
| 288 | stl: "none", |
| 289 | } |
| 290 | `) |
| 291 | |
| 292 | testcases := []struct { |
| 293 | name string |
| 294 | expected []string |
Yu Shan | b7646e4 | 2024-05-17 20:09:23 +0000 | [diff] [blame] | 295 | }{ |
Jiyong Park | 25b9222 | 2024-05-17 22:58:54 +0000 | [diff] [blame] | 296 | { |
| 297 | name: "app", |
Spandan Das | d4530d6 | 2024-09-26 00:46:12 +0000 | [diff] [blame] | 298 | expected: []string{"libjni:64"}, |
Jiyong Park | 25b9222 | 2024-05-17 22:58:54 +0000 | [diff] [blame] | 299 | }, |
| 300 | { |
| 301 | name: "app_embedded", |
| 302 | expected: nil, |
| 303 | }, |
Yu Shan | b7646e4 | 2024-05-17 20:09:23 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Jiyong Park | 25b9222 | 2024-05-17 22:58:54 +0000 | [diff] [blame] | 306 | for _, tc := range testcases { |
Colin Cross | 90607e9 | 2025-02-11 14:58:07 -0800 | [diff] [blame^] | 307 | mod := ctx.ModuleForTests(t, tc.name, "android_common").Module() |
Jiyong Park | 25b9222 | 2024-05-17 22:58:54 +0000 | [diff] [blame] | 308 | entries := android.AndroidMkEntriesForTest(t, ctx.TestContext, mod)[0] |
| 309 | required := entries.EntryMap["LOCAL_REQUIRED_MODULES"] |
| 310 | android.AssertDeepEquals(t, "unexpected required deps", tc.expected, required) |
Yu Shan | b7646e4 | 2024-05-17 20:09:23 +0000 | [diff] [blame] | 311 | } |
| 312 | } |