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 15package java 16 17import ( 18 "reflect" 19 "testing" 20 21 "android/soong/android" 22 "android/soong/cc" 23) 24 25func TestRequired(t *testing.T) { 26 t.Parallel() 27 ctx, _ := testJava(t, ` 28 java_library { 29 name: "foo", 30 srcs: ["a.java"], 31 required: ["libfoo"], 32 } 33 `) 34 35 mod := ctx.ModuleForTests(t, "foo", "android_common").Module() 36 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] 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 } 43} 44 45func TestHostdex(t *testing.T) { 46 t.Parallel() 47 ctx, _ := testJava(t, ` 48 java_library { 49 name: "foo", 50 srcs: ["a.java"], 51 hostdex: true, 52 } 53 `) 54 55 mod := ctx.ModuleForTests(t, "foo", "android_common").Module() 56 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) 57 if len(entriesList) != 2 { 58 t.Errorf("two entries are expected, but got %d", len(entriesList)) 59 } 60 61 mainEntries := &entriesList[0] 62 expected := []string{"foo"} 63 actual := mainEntries.EntryMap["LOCAL_MODULE"] 64 if !reflect.DeepEqual(expected, actual) { 65 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual) 66 } 67 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) 73 } 74} 75 76func TestHostdexRequired(t *testing.T) { 77 t.Parallel() 78 ctx, _ := testJava(t, ` 79 java_library { 80 name: "foo", 81 srcs: ["a.java"], 82 hostdex: true, 83 required: ["libfoo"], 84 } 85 `) 86 87 mod := ctx.ModuleForTests(t, "foo", "android_common").Module() 88 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) 89 if len(entriesList) != 2 { 90 t.Errorf("two entries are expected, but got %d", len(entriesList)) 91 } 92 93 mainEntries := &entriesList[0] 94 expected := []string{"libfoo"} 95 actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"] 96 if !reflect.DeepEqual(expected, actual) { 97 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) 98 } 99 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) 105 } 106} 107 108func TestHostdexSpecificRequired(t *testing.T) { 109 t.Parallel() 110 ctx, _ := testJava(t, ` 111 java_library { 112 name: "foo", 113 srcs: ["a.java"], 114 hostdex: true, 115 target: { 116 hostdex: { 117 required: ["libfoo"], 118 }, 119 }, 120 } 121 `) 122 123 mod := ctx.ModuleForTests(t, "foo", "android_common").Module() 124 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) 125 if len(entriesList) != 2 { 126 t.Errorf("two entries are expected, but got %d", len(entriesList)) 127 } 128 129 mainEntries := &entriesList[0] 130 if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok { 131 t.Errorf("Unexpected required modules: %q", r) 132 } 133 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) 139 } 140} 141 142func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) { 143 t.Parallel() 144 result := android.GroupFixturePreparers( 145 prepareForJavaTest, 146 PrepareForTestWithJavaSdkLibraryFiles, 147 FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"), 148 ).RunTestWithBp(t, ` 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 161 result.ModuleForTests(t, "foo-shared_library.xml", "android_common") 162 163 testCases := []struct { 164 moduleName string 165 expected []string 166 }{ 167 {"foo-shared_library", []string{"foo-shared_library.impl", "foo-shared_library.xml"}}, 168 {"foo-no_shared_library", []string{"foo-no_shared_library.impl"}}, 169 } 170 for _, tc := range testCases { 171 mod := result.ModuleForTests(t, tc.moduleName, "android_common").Module() 172 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] 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} 179 180func TestImportSoongDexJar(t *testing.T) { 181 t.Parallel() 182 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, ` 183 java_import { 184 name: "my-java-import", 185 jars: ["a.jar"], 186 prefer: true, 187 compile_dex: true, 188 } 189 `) 190 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" 194 actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"] 195 196 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar) 197} 198 199func TestAndroidTestHelperApp_LocalDisableTestConfig(t *testing.T) { 200 t.Parallel() 201 ctx, _ := testJava(t, ` 202 android_test_helper_app { 203 name: "foo", 204 srcs: ["a.java"], 205 } 206 `) 207 208 mod := ctx.ModuleForTests(t, "foo", "android_common").Module() 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} 217 218func TestGetOverriddenPackages(t *testing.T) { 219 t.Parallel() 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 { 257 mod := ctx.ModuleForTests(t, expected.name, expected.variantName).Module() 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} 264 265func TestJniAsRequiredDeps(t *testing.T) { 266 t.Parallel() 267 ctx := android.GroupFixturePreparers( 268 PrepareForTestWithJavaDefaultModules, 269 cc.PrepareForTestWithCcDefaultModules, 270 android.PrepareForTestWithAndroidMk, 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 295 }{ 296 { 297 name: "app", 298 expected: []string{"libjni:64"}, 299 }, 300 { 301 name: "app_embedded", 302 expected: nil, 303 }, 304 } 305 306 for _, tc := range testcases { 307 mod := ctx.ModuleForTests(t, tc.name, "android_common").Module() 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) 311 } 312} 313