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