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) 23 24func TestRequired(t *testing.T) { 25 ctx, _ := testJava(t, ` 26 java_library { 27 name: "foo", 28 srcs: ["a.java"], 29 required: ["libfoo"], 30 } 31 `) 32 33 mod := ctx.ModuleForTests("foo", "android_common").Module() 34 entries := android.AndroidMkEntriesForTest(t, ctx, mod)[0] 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 } 41} 42 43func TestHostdex(t *testing.T) { 44 ctx, _ := testJava(t, ` 45 java_library { 46 name: "foo", 47 srcs: ["a.java"], 48 hostdex: true, 49 } 50 `) 51 52 mod := ctx.ModuleForTests("foo", "android_common").Module() 53 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) 54 if len(entriesList) != 2 { 55 t.Errorf("two entries are expected, but got %d", len(entriesList)) 56 } 57 58 mainEntries := &entriesList[0] 59 expected := []string{"foo"} 60 actual := mainEntries.EntryMap["LOCAL_MODULE"] 61 if !reflect.DeepEqual(expected, actual) { 62 t.Errorf("Unexpected module name - expected: %q, actual: %q", expected, actual) 63 } 64 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) 70 } 71} 72 73func TestHostdexRequired(t *testing.T) { 74 ctx, _ := testJava(t, ` 75 java_library { 76 name: "foo", 77 srcs: ["a.java"], 78 hostdex: true, 79 required: ["libfoo"], 80 } 81 `) 82 83 mod := ctx.ModuleForTests("foo", "android_common").Module() 84 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) 85 if len(entriesList) != 2 { 86 t.Errorf("two entries are expected, but got %d", len(entriesList)) 87 } 88 89 mainEntries := &entriesList[0] 90 expected := []string{"libfoo"} 91 actual := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"] 92 if !reflect.DeepEqual(expected, actual) { 93 t.Errorf("Unexpected required modules - expected: %q, actual: %q", expected, actual) 94 } 95 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) 101 } 102} 103 104func TestHostdexSpecificRequired(t *testing.T) { 105 ctx, _ := testJava(t, ` 106 java_library { 107 name: "foo", 108 srcs: ["a.java"], 109 hostdex: true, 110 target: { 111 hostdex: { 112 required: ["libfoo"], 113 }, 114 }, 115 } 116 `) 117 118 mod := ctx.ModuleForTests("foo", "android_common").Module() 119 entriesList := android.AndroidMkEntriesForTest(t, ctx, mod) 120 if len(entriesList) != 2 { 121 t.Errorf("two entries are expected, but got %d", len(entriesList)) 122 } 123 124 mainEntries := &entriesList[0] 125 if r, ok := mainEntries.EntryMap["LOCAL_REQUIRED_MODULES"]; ok { 126 t.Errorf("Unexpected required modules: %q", r) 127 } 128 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) 134 } 135} 136 137func TestJavaSdkLibrary_RequireXmlPermissionFile(t *testing.T) { 138 result := android.GroupFixturePreparers( 139 prepareForJavaTest, 140 PrepareForTestWithJavaSdkLibraryFiles, 141 FixtureWithLastReleaseApis("foo-shared_library", "foo-no_shared_library"), 142 ).RunTestWithBp(t, ` 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 155 result.ModuleForTests("foo-shared_library.xml", "android_common") 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 { 165 mod := result.ModuleForTests(tc.moduleName, "android_common").Module() 166 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0] 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} 173 174func TestImportSoongDexJar(t *testing.T) { 175 result := PrepareForTestWithJavaDefaultModules.RunTestWithBp(t, ` 176 java_import { 177 name: "my-java-import", 178 jars: ["a.jar"], 179 prefer: true, 180 compile_dex: true, 181 } 182 `) 183 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" 187 actualSoongDexJar := entries.EntryMap["LOCAL_SOONG_DEX_JAR"] 188 189 android.AssertStringPathsRelativeToTopEquals(t, "LOCAL_SOONG_DEX_JAR", result.Config, []string{expectedSoongDexJar}, actualSoongDexJar) 190} 191 192func 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} 209