| 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" | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 22 | ) | 
|  | 23 |  | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 24 | func TestRequired(t *testing.T) { | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 25 | ctx, _ := testJava(t, ` | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 26 | java_library { | 
|  | 27 | name: "foo", | 
|  | 28 | srcs: ["a.java"], | 
|  | 29 | required: ["libfoo"], | 
|  | 30 | } | 
| Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 31 | `) | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 32 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 33 | mod := ctx.ModuleForTests("foo", "android_common").Module() | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 34 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | expected := []string{"libfoo"} | 
|  | 37 | actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"] | 
|  | 38 | if !reflect.DeepEqual(expected, actual) { | 
|  | 39 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) | 
|  | 40 | } | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
|  | 43 | func TestHostdex(t *testing.T) { | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 44 | ctx, _ := testJava(t, ` | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 45 | java_library { | 
|  | 46 | name: "foo", | 
|  | 47 | srcs: ["a.java"], | 
|  | 48 | hostdex: true, | 
|  | 49 | } | 
| Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 50 | `) | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 51 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 52 | mod := ctx.ModuleForTests("foo", "android_common").Module() | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 53 | entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 54 | if len(entriesList) != 2 { | 
|  | 55 | t.Errorf("two entries are expected, but got %d", len(entriesList)) | 
|  | 56 | } | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 57 |  | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 58 | mainEntries := &entriesList[0] | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 59 | expected := []string{"foo"} | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 60 | actual := mainEntries.EntryMap["LOCAL_MODULE"] | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 61 | if !reflect.DeepEqual(expected, actual) { | 
|  | 62 | t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual) | 
|  | 63 | } | 
|  | 64 |  | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 65 | subEntries := &entriesList[1] | 
|  | 66 | expected = []string{"foo-hostdex"} | 
|  | 67 | actual = subEntries.EntryMap["LOCAL_MODULE"] | 
|  | 68 | if !reflect.DeepEqual(expected, actual) { | 
|  | 69 | t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual) | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 70 | } | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
|  | 73 | func TestHostdexRequired(t *testing.T) { | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 74 | ctx, _ := testJava(t, ` | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 75 | java_library { | 
|  | 76 | name: "foo", | 
|  | 77 | srcs: ["a.java"], | 
|  | 78 | hostdex: true, | 
|  | 79 | required: ["libfoo"], | 
|  | 80 | } | 
| Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 81 | `) | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 82 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 83 | mod := ctx.ModuleForTests("foo", "android_common").Module() | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 84 | entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 85 | if len(entriesList) != 2 { | 
|  | 86 | t.Errorf("two entries are expected, but got %d", len(entriesList)) | 
|  | 87 | } | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 88 |  | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 89 | mainEntries := &entriesList[0] | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 90 | expected := []string{"libfoo"} | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 91 | actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"] | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 92 | if !reflect.DeepEqual(expected, actual) { | 
|  | 93 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) | 
|  | 94 | } | 
|  | 95 |  | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 96 | subEntries := &entriesList[1] | 
|  | 97 | expected = []string{"libfoo"} | 
|  | 98 | actual = subEntries.EntryMap["LOCAL_REQUIRED_MODULES"] | 
|  | 99 | if !reflect.DeepEqual(expected, actual) { | 
|  | 100 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 101 | } | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
|  | 104 | func TestHostdexSpecificRequired(t *testing.T) { | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 105 | ctx, _ := testJava(t, ` | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 106 | java_library { | 
|  | 107 | name: "foo", | 
|  | 108 | srcs: ["a.java"], | 
|  | 109 | hostdex: true, | 
|  | 110 | target: { | 
|  | 111 | hostdex: { | 
|  | 112 | required: ["libfoo"], | 
|  | 113 | }, | 
|  | 114 | }, | 
|  | 115 | } | 
| Jaewoong Jung | f9a0443 | 2019-07-17 11:15:09 -0700 | [diff] [blame] | 116 | `) | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 117 |  | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 118 | mod := ctx.ModuleForTests("foo", "android_common").Module() | 
| Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 119 | entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 120 | if len(entriesList) != 2 { | 
|  | 121 | t.Errorf("two entries are expected, but got %d", len(entriesList)) | 
|  | 122 | } | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 123 |  | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 124 | mainEntries := &entriesList[0] | 
|  | 125 | if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok { | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 126 | t.Errorf("Unexpected required modules: %q", r) | 
|  | 127 | } | 
|  | 128 |  | 
| Jiyong Park | 55bd98b | 2019-12-11 17:27:07 +0900 | [diff] [blame] | 129 | subEntries := &entriesList[1] | 
|  | 130 | expected := []string{"libfoo"} | 
|  | 131 | actual := subEntries.EntryMap["LOCAL_REQUIRED_MODULES"] | 
|  | 132 | if !reflect.DeepEqual(expected, actual) { | 
|  | 133 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) | 
| Jaewoong Jung | b0c127c | 2019-08-29 14:56:03 -0700 | [diff] [blame] | 134 | } | 
| Jooyung Han | 12df5fb | 2019-07-11 16:18:47 +0900 | [diff] [blame] | 135 | } | 
| Anton Hansson | 78156ef | 2020-03-27 19:39:48 +0000 | [diff] [blame] | 136 |  | 
| Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 137 | func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) { | 
| Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 138 | result := android.GroupFixturePreparers( | 
|  | 139 | prepareForJavaTest, | 
| Paul Duffin | 163043d | 2021-03-12 19:18:49 +0000 | [diff] [blame] | 140 | PrepareForTestWithJavaSdkLibraryFiles, | 
|  | 141 | FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"), | 
|  | 142 | ).RunTestWithBp(t, ` | 
| Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 143 | java_sdk_library { | 
|  | 144 | name: "foo-shared_library", | 
|  | 145 | srcs: ["a.java"], | 
|  | 146 | } | 
|  | 147 | java_sdk_library { | 
|  | 148 | name: "foo-no_shared_library", | 
|  | 149 | srcs: ["a.java"], | 
|  | 150 | shared_library: false, | 
|  | 151 | } | 
|  | 152 | `) | 
|  | 153 |  | 
|  | 154 | // Verify the existence of internal modules | 
| Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 155 | result.ModuleForTests("foo-shared_library.xml", "android_common") | 
| Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 156 |  | 
|  | 157 | testCases := []struct { | 
|  | 158 | moduleName string | 
|  | 159 | expected   []string | 
|  | 160 | }{ | 
|  | 161 | {"foo-shared_library", []string{"foo-shared_library.xml"}}, | 
|  | 162 | {"foo-no_shared_library", nil}, | 
|  | 163 | } | 
|  | 164 | for _, tc := range testCases { | 
| Paul Duffin | 22b77cd | 2021-03-12 19:15:01 +0000 | [diff] [blame] | 165 | mod := result.ModuleForTests(tc.moduleName, "android_common").Module() | 
|  | 166 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] | 
| Yo Chiang | 07d7507 | 2020-06-05 17:43:19 +0800 | [diff] [blame] | 167 | actual := entries.EntryMap["LOCAL_REQUIRED_MODULES"] | 
|  | 168 | if !reflect.DeepEqual(tc.expected, actual) { | 
|  | 169 | t.Errorf("Unexpected required modules - expected: %q, actual: %q", tc.expected, actual) | 
|  | 170 | } | 
|  | 171 | } | 
|  | 172 | } | 
| Bill Peckham | fb04df4 | 2021-01-11 12:27:24 -0800 | [diff] [blame] | 173 |  | 
|  | 174 | func TestImportSoongDexJar(t *testing.T) { | 
| Paul Duffin | f04311c | 2021-03-22 17:31:51 +0000 | [diff] [blame] | 175 | result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, ` | 
| Bill Peckham | fb04df4 | 2021-01-11 12:27:24 -0800 | [diff] [blame] | 176 | java_import { | 
|  | 177 | name: "my-java-import", | 
|  | 178 | jars: ["a.jar"], | 
|  | 179 | prefer: true, | 
|  | 180 | compile_dex: true, | 
|  | 181 | } | 
|  | 182 | `) | 
|  | 183 |  | 
| Paul Duffin | f04311c | 2021-03-22 17:31:51 +0000 | [diff] [blame] | 184 | mod := result.Module("my-java-import", "android_common") | 
|  | 185 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] | 
|  | 186 | 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] | 187 | actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"] | 
|  | 188 |  | 
| Paul Duffin | f04311c | 2021-03-22 17:31:51 +0000 | [diff] [blame] | 189 | android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar) | 
| Bill Peckham | fb04df4 | 2021-01-11 12:27:24 -0800 | [diff] [blame] | 190 | } | 
| Yuntao Xu | 7a31855 | 2021-05-27 10:30:26 -0700 | [diff] [blame] | 191 |  | 
|  | 192 | func TestAndroidTestHelperApp_LocalDisableTestConfig(t *testing.T) { | 
|  | 193 | ctx, _ := testJava(t, ` | 
|  | 194 | android_test_helper_app { | 
|  | 195 | name: "foo", | 
|  | 196 | srcs: ["a.java"], | 
|  | 197 | } | 
|  | 198 | `) | 
|  | 199 |  | 
|  | 200 | mod := ctx.ModuleForTests("foo", "android_common").Module() | 
|  | 201 | entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] | 
|  | 202 |  | 
|  | 203 | expected := []string{"true"} | 
|  | 204 | actual := entries.EntryMap["LOCAL_DISABLE_TEST_CONFIG"] | 
|  | 205 | if !reflect.DeepEqual(expected, actual) { | 
|  | 206 | t.Errorf("Unexpected flag value - expected: %q, actual: %q", expected, actual) | 
|  | 207 | } | 
|  | 208 | } |