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 ctx, _ := testJava(t, ` 27 java_library { 28 name: "foo", 29 srcs: ["a.java"], 30 required: ["libfoo"], 31 } 32 `) 33 34 mod := ctx.ModuleForTests("foo", "android_common").Module() 35 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] 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 } 42} 43 44func TestHostdex(t *testing.T) { 45 ctx, _ := testJava(t, ` 46 java_library { 47 name: "foo", 48 srcs: ["a.java"], 49 hostdex: true, 50 } 51 `) 52 53 mod := ctx.ModuleForTests("foo", "android_common").Module() 54 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) 55 if len(entriesList) != 2 { 56 t.Errorf("two entries are expected, but got %d", len(entriesList)) 57 } 58 59 mainEntries := &entriesList[0] 60 expected := []string{"foo"} 61 actual := mainEntries.EntryMap["LOCAL_MODULE"] 62 if !reflect.DeepEqual(expected, actual) { 63 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual) 64 } 65 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) 71 } 72} 73 74func TestHostdexRequired(t *testing.T) { 75 ctx, _ := testJava(t, ` 76 java_library { 77 name: "foo", 78 srcs: ["a.java"], 79 hostdex: true, 80 required: ["libfoo"], 81 } 82 `) 83 84 mod := ctx.ModuleForTests("foo", "android_common").Module() 85 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) 86 if len(entriesList) != 2 { 87 t.Errorf("two entries are expected, but got %d", len(entriesList)) 88 } 89 90 mainEntries := &entriesList[0] 91 expected := []string{"libfoo"} 92 actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"] 93 if !reflect.DeepEqual(expected, actual) { 94 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) 95 } 96 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) 102 } 103} 104 105func TestHostdexSpecificRequired(t *testing.T) { 106 ctx, _ := testJava(t, ` 107 java_library { 108 name: "foo", 109 srcs: ["a.java"], 110 hostdex: true, 111 target: { 112 hostdex: { 113 required: ["libfoo"], 114 }, 115 }, 116 } 117 `) 118 119 mod := ctx.ModuleForTests("foo", "android_common").Module() 120 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) 121 if len(entriesList) != 2 { 122 t.Errorf("two entries are expected, but got %d", len(entriesList)) 123 } 124 125 mainEntries := &entriesList[0] 126 if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok { 127 t.Errorf("Unexpected required modules: %q", r) 128 } 129 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) 135 } 136} 137 138func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) { 139 result := android.GroupFixturePreparers( 140 prepareForJavaTest, 141 PrepareForTestWithJavaSdkLibraryFiles, 142 FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"), 143 ).RunTestWithBp(t, ` 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 156 result.ModuleForTests("foo-shared_library.xml", "android_common") 157 158 testCases := []struct { 159 moduleName string 160 expected []string 161 }{ 162 {"foo-shared_library", []string{"foo-shared_library.impl", "foo-shared_library.xml"}}, 163 {"foo-no_shared_library", []string{"foo-no_shared_library.impl"}}, 164 } 165 for _, tc := range testCases { 166 mod := result.ModuleForTests(tc.moduleName, "android_common").Module() 167 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] 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} 174 175func TestImportSoongDexJar(t *testing.T) { 176 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, ` 177 java_import { 178 name: "my-java-import", 179 jars: ["a.jar"], 180 prefer: true, 181 compile_dex: true, 182 } 183 `) 184 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" 188 actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"] 189 190 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar) 191} 192 193func 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} 210 211func 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} 256 257func TestJniAsRequiredDeps(t *testing.T) { 258 ctx := android.GroupFixturePreparers( 259 PrepareForTestWithJavaDefaultModules, 260 cc.PrepareForTestWithCcDefaultModules, 261 android.PrepareForTestWithAndroidMk, 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 286 }{ 287 { 288 name: "app", 289 expected: []string{"libjni:64"}, 290 }, 291 { 292 name: "app_embedded", 293 expected: nil, 294 }, 295 } 296 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) 302 } 303} 304