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" |
Anton Hansson | 78156ef | 2020-03-27 19:39:48 +0000 | [diff] [blame] | 19 | "strings" |
Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 20 | "testing" |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 21 | |
| 22 | "android/soong/android" |
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) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 26 | ctx, config := 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() |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 35 | entries := android.AndroidMkEntriesForTest(t, config, "", 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) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 45 | ctx, config := 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() |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 54 | entriesList := android.AndroidMkEntriesForTest(t, config, "", mod) |
| 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) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 75 | ctx, config := 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() |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 85 | entriesList := android.AndroidMkEntriesForTest(t, config, "", mod) |
| 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) { |
Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 106 | ctx, config := 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() |
Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 120 | entriesList := android.AndroidMkEntriesForTest(t, config, "", mod) |
| 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 | |
| 138 | func TestDistWithTag(t *testing.T) { |
| 139 | ctx, config := testJava(t, ` |
| 140 | java_library { |
| 141 | name: "foo_without_tag", |
| 142 | srcs: ["a.java"], |
| 143 | compile_dex: true, |
| 144 | dist: { |
| 145 | targets: ["hi"], |
| 146 | }, |
| 147 | } |
| 148 | java_library { |
| 149 | name: "foo_with_tag", |
| 150 | srcs: ["a.java"], |
| 151 | compile_dex: true, |
| 152 | dist: { |
| 153 | targets: ["hi"], |
| 154 | tag: ".jar", |
| 155 | }, |
| 156 | } |
| 157 | `) |
| 158 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame^] | 159 | withoutTagEntries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_without_tag", "android_common").Module()) |
| 160 | withTagEntries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_with_tag", "android_common").Module()) |
Anton Hansson | 78156ef | 2020-03-27 19:39:48 +0000 | [diff] [blame] | 161 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame^] | 162 | if len(withoutTagEntries) != 2 || len(withTagEntries) != 2 { |
| 163 | t.Errorf("two mk entries per module expected, got %d and %d", len(withoutTagEntries), len(withTagEntries)) |
Anton Hansson | 78156ef | 2020-03-27 19:39:48 +0000 | [diff] [blame] | 164 | } |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame^] | 165 | if len(withTagEntries[0].DistFiles[".jar"]) != 1 || |
| 166 | !strings.Contains(withTagEntries[0].DistFiles[".jar"][0].String(), "/javac/foo_with_tag.jar") { |
| 167 | t.Errorf("expected DistFiles to contain classes.jar, got %v", withTagEntries[0].DistFiles) |
Anton Hansson | 78156ef | 2020-03-27 19:39:48 +0000 | [diff] [blame] | 168 | } |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame^] | 169 | if len(withoutTagEntries[0].DistFiles[".jar"]) > 0 { |
| 170 | t.Errorf("did not expect explicit DistFile for .jar tag, got %v", withoutTagEntries[0].DistFiles[".jar"]) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestDistWithDest(t *testing.T) { |
| 175 | ctx, config := testJava(t, ` |
| 176 | java_library { |
| 177 | name: "foo", |
| 178 | srcs: ["a.java"], |
| 179 | compile_dex: true, |
| 180 | dist: { |
| 181 | targets: ["my_goal"], |
| 182 | dest: "my/custom/dest/dir", |
| 183 | }, |
| 184 | } |
| 185 | `) |
| 186 | |
| 187 | module := ctx.ModuleForTests("foo", "android_common").Module() |
| 188 | entries := android.AndroidMkEntriesForTest(t, config, "", module) |
| 189 | if len(entries) != 2 { |
| 190 | t.Errorf("Expected 2 AndroidMk entries, got %d", len(entries)) |
| 191 | } |
| 192 | |
| 193 | distStrings := entries[0].GetDistForGoals(module) |
| 194 | |
| 195 | if len(distStrings) != 2 { |
| 196 | t.Errorf("Expected 2 entries for dist: PHONY and dist-for-goals, but got %q", distStrings) |
| 197 | } |
| 198 | |
| 199 | if distStrings[0] != ".PHONY: my_goal\n" { |
| 200 | t.Errorf("Expected .PHONY entry to declare my_goal, but got: %s", distStrings[0]) |
| 201 | } |
| 202 | |
| 203 | if !strings.Contains(distStrings[1], "$(call dist-for-goals,my_goal") || |
| 204 | !strings.Contains(distStrings[1], ".intermediates/foo/android_common/dex/foo.jar:my/custom/dest/dir") { |
| 205 | t.Errorf( |
| 206 | "Expected dist-for-goals entry to contain my_goal and new dest dir, but got: %s", distStrings[1]) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | func TestDistsWithAllProperties(t *testing.T) { |
| 211 | ctx, config := testJava(t, ` |
| 212 | java_library { |
| 213 | name: "foo", |
| 214 | srcs: ["a.java"], |
| 215 | compile_dex: true, |
| 216 | dist: { |
| 217 | targets: ["baz"], |
| 218 | }, |
| 219 | dists: [ |
| 220 | { |
| 221 | targets: ["bar"], |
| 222 | tag: ".jar", |
| 223 | dest: "bar.jar", |
| 224 | dir: "bar/dir", |
| 225 | suffix: ".qux", |
| 226 | }, |
| 227 | ] |
| 228 | } |
| 229 | `) |
| 230 | |
| 231 | module := ctx.ModuleForTests("foo", "android_common").Module() |
| 232 | entries := android.AndroidMkEntriesForTest(t, config, "", module) |
| 233 | if len(entries) != 2 { |
| 234 | t.Errorf("Expected 2 AndroidMk entries, got %d", len(entries)) |
| 235 | } |
| 236 | |
| 237 | distStrings := entries[0].GetDistForGoals(module) |
| 238 | |
| 239 | if len(distStrings) != 4 { |
| 240 | t.Errorf("Expected 4 entries for dist: PHONY and dist-for-goals, but got %d", len(distStrings)) |
| 241 | } |
| 242 | |
| 243 | if distStrings[0] != ".PHONY: bar\n" { |
| 244 | t.Errorf("Expected .PHONY entry to declare bar, but got: %s", distStrings[0]) |
| 245 | } |
| 246 | |
| 247 | if !strings.Contains(distStrings[1], "$(call dist-for-goals,bar") || |
| 248 | !strings.Contains( |
| 249 | distStrings[1], |
| 250 | ".intermediates/foo/android_common/javac/foo.jar:bar/dir/bar.qux.jar") { |
| 251 | t.Errorf( |
| 252 | "Expected dist-for-goals entry to contain bar and new dest dir, but got: %s", distStrings[1]) |
| 253 | } |
| 254 | |
| 255 | if distStrings[2] != ".PHONY: baz\n" { |
| 256 | t.Errorf("Expected .PHONY entry to declare baz, but got: %s", distStrings[2]) |
| 257 | } |
| 258 | |
| 259 | if !strings.Contains(distStrings[3], "$(call dist-for-goals,baz") || |
| 260 | !strings.Contains(distStrings[3], ".intermediates/foo/android_common/dex/foo.jar:foo.jar") { |
| 261 | t.Errorf( |
| 262 | "Expected dist-for-goals entry to contain my_other_goal and new dest dir, but got: %s", |
| 263 | distStrings[3]) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func TestDistsWithTag(t *testing.T) { |
| 268 | ctx, config := testJava(t, ` |
| 269 | java_library { |
| 270 | name: "foo_without_tag", |
| 271 | srcs: ["a.java"], |
| 272 | compile_dex: true, |
| 273 | dists: [ |
| 274 | { |
| 275 | targets: ["hi"], |
| 276 | }, |
| 277 | ], |
| 278 | } |
| 279 | java_library { |
| 280 | name: "foo_with_tag", |
| 281 | srcs: ["a.java"], |
| 282 | compile_dex: true, |
| 283 | dists: [ |
| 284 | { |
| 285 | targets: ["hi"], |
| 286 | tag: ".jar", |
| 287 | }, |
| 288 | ], |
| 289 | } |
| 290 | `) |
| 291 | |
| 292 | moduleWithoutTag := ctx.ModuleForTests("foo_without_tag", "android_common").Module() |
| 293 | moduleWithTag := ctx.ModuleForTests("foo_with_tag", "android_common").Module() |
| 294 | |
| 295 | withoutTagEntries := android.AndroidMkEntriesForTest(t, config, "", moduleWithoutTag) |
| 296 | withTagEntries := android.AndroidMkEntriesForTest(t, config, "", moduleWithTag) |
| 297 | |
| 298 | if len(withoutTagEntries) != 2 || len(withTagEntries) != 2 { |
| 299 | t.Errorf("two mk entries per module expected, got %d and %d", len(withoutTagEntries), len(withTagEntries)) |
| 300 | } |
| 301 | |
| 302 | distFilesWithoutTag := withoutTagEntries[0].DistFiles |
| 303 | distFilesWithTag := withTagEntries[0].DistFiles |
| 304 | |
| 305 | if len(distFilesWithTag[".jar"]) != 1 || |
| 306 | !strings.Contains(distFilesWithTag[".jar"][0].String(), "/javac/foo_with_tag.jar") { |
| 307 | t.Errorf("expected foo_with_tag's .jar-tagged DistFiles to contain classes.jar, got %v", distFilesWithTag[".jar"]) |
| 308 | } |
| 309 | if len(distFilesWithoutTag[".jar"]) > 0 { |
| 310 | t.Errorf("did not expect foo_without_tag's .jar-tagged DistFiles to contain files, but got %v", distFilesWithoutTag[".jar"]) |
Anton Hansson | 78156ef | 2020-03-27 19:39:48 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 313 | |
| 314 | func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) { |
| 315 | ctx, config := testJava(t, ` |
| 316 | java_sdk_library { |
| 317 | name: "foo-shared_library", |
| 318 | srcs: ["a.java"], |
| 319 | } |
| 320 | java_sdk_library { |
| 321 | name: "foo-no_shared_library", |
| 322 | srcs: ["a.java"], |
| 323 | shared_library: false, |
| 324 | } |
| 325 | `) |
| 326 | |
| 327 | // Verify the existence of internal modules |
| 328 | ctx.ModuleForTests("foo-shared_library.xml", "android_common") |
| 329 | |
| 330 | testCases := []struct { |
| 331 | moduleName string |
| 332 | expected []string |
| 333 | }{ |
| 334 | {"foo-shared_library", []string{"foo-shared_library.xml"}}, |
| 335 | {"foo-no_shared_library", nil}, |
| 336 | } |
| 337 | for _, tc := range testCases { |
| 338 | mod := ctx.ModuleForTests(tc.moduleName, "android_common").Module() |
| 339 | entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0] |
| 340 | actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"] |
| 341 | if !reflect.DeepEqual(tc.expected, actual) { |
| 342 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual) |
| 343 | } |
| 344 | } |
| 345 | } |