| 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 | 1e65f94 | 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 | 1e65f94 | 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 |  | 
|  | 159 | without_tag_entries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_without_tag", "android_common").Module()) | 
|  | 160 | with_tag_entries := android.AndroidMkEntriesForTest(t, config, "", ctx.ModuleForTests("foo_with_tag", "android_common").Module()) | 
|  | 161 |  | 
|  | 162 | if len(without_tag_entries) != 2 || len(with_tag_entries) != 2 { | 
|  | 163 | t.Errorf("two mk entries per module expected, got %d and %d", len(without_tag_entries), len(with_tag_entries)) | 
|  | 164 | } | 
|  | 165 | if !with_tag_entries[0].DistFile.Valid() || !strings.Contains(with_tag_entries[0].DistFile.String(), "/javac/foo_with_tag.jar") { | 
|  | 166 | t.Errorf("expected classes.jar DistFile, got %v", with_tag_entries[0].DistFile) | 
|  | 167 | } | 
|  | 168 | if without_tag_entries[0].DistFile.Valid() { | 
|  | 169 | t.Errorf("did not expect explicit DistFile, got %v", without_tag_entries[0].DistFile) | 
|  | 170 | } | 
|  | 171 | } |