1// Copyright 2018 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 TestCollectJavaLibraryPropertiesAddLibsDeps(t *testing.T) { 25 t.Parallel() 26 ctx, _ := testJava(t, 27 ` 28 java_library {name: "Foo"} 29 java_library {name: "Bar"} 30 java_library { 31 name: "javalib", 32 libs: ["Foo", "Bar"], 33 } 34 `) 35 module := ctx.ModuleForTests(t, "javalib", "android_common").Module().(*Library) 36 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey) 37 38 for _, expected := range []string{"Foo", "Bar"} { 39 if !android.InList(expected, dpInfo.Deps) { 40 t.Errorf("Library.IDEInfo() Deps = %v, %v not found", dpInfo.Deps, expected) 41 } 42 } 43} 44 45func TestCollectJavaLibraryPropertiesAddStaticLibsDeps(t *testing.T) { 46 t.Parallel() 47 ctx, _ := testJava(t, 48 ` 49 java_library {name: "Foo"} 50 java_library {name: "Bar"} 51 java_library { 52 name: "javalib", 53 static_libs: ["Foo", "Bar"], 54 } 55 `) 56 module := ctx.ModuleForTests(t, "javalib", "android_common").Module().(*Library) 57 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey) 58 59 for _, expected := range []string{"Foo", "Bar"} { 60 if !android.InList(expected, dpInfo.Deps) { 61 t.Errorf("Library.IDEInfo() Deps = %v, %v not found", dpInfo.Deps, expected) 62 } 63 } 64} 65 66func TestCollectJavaLibraryPropertiesAddScrs(t *testing.T) { 67 t.Parallel() 68 ctx, _ := testJava(t, 69 ` 70 java_library { 71 name: "javalib", 72 srcs: ["Foo.java", "Bar.java"], 73 } 74 `) 75 module := ctx.ModuleForTests(t, "javalib", "android_common").Module().(*Library) 76 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey) 77 78 expected := []string{"Foo.java", "Bar.java"} 79 if !reflect.DeepEqual(dpInfo.Srcs, expected) { 80 t.Errorf("Library.IDEInfo() Srcs = %v, want %v", dpInfo.Srcs, expected) 81 } 82} 83 84func TestCollectJavaLibraryPropertiesAddAidlIncludeDirs(t *testing.T) { 85 t.Parallel() 86 ctx, _ := testJava(t, 87 ` 88 java_library { 89 name: "javalib", 90 aidl: { 91 include_dirs: ["Foo", "Bar"], 92 }, 93 } 94 `) 95 module := ctx.ModuleForTests(t, "javalib", "android_common").Module().(*Library) 96 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey) 97 98 expected := []string{"Foo", "Bar"} 99 if !reflect.DeepEqual(dpInfo.Aidl_include_dirs, expected) { 100 t.Errorf("Library.IDEInfo() Aidl_include_dirs = %v, want %v", dpInfo.Aidl_include_dirs, expected) 101 } 102} 103 104func TestCollectJavaLibraryWithJarJarRules(t *testing.T) { 105 t.Parallel() 106 ctx, _ := testJava(t, 107 ` 108 java_library { 109 name: "javalib", 110 srcs: ["foo.java"], 111 jarjar_rules: "jarjar_rules.txt", 112 } 113 `) 114 module := ctx.ModuleForTests(t, "javalib", "android_common").Module().(*Library) 115 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey) 116 117 android.AssertStringEquals(t, "IdeInfo.Srcs of repackaged library should not be empty", "foo.java", dpInfo.Srcs[0]) 118 android.AssertStringEquals(t, "IdeInfo.Jar_rules of repackaged library should not be empty", "jarjar_rules.txt", dpInfo.Jarjar_rules[0]) 119 if !android.SubstringInList(dpInfo.Jars, "soong/.intermediates/javalib/android_common/jarjar/turbine/javalib.jar") { 120 t.Errorf("IdeInfo.Jars of repackaged library should contain the output of jarjar-ing. All outputs: %v\n", dpInfo.Jars) 121 } 122} 123 124func TestCollectJavaLibraryLinkingAgainstVersionedSdk(t *testing.T) { 125 t.Parallel() 126 ctx := android.GroupFixturePreparers( 127 prepareForJavaTest, 128 FixtureWithPrebuiltApis(map[string][]string{ 129 "29": {}, 130 })).RunTestWithBp(t, 131 ` 132 java_library { 133 name: "javalib", 134 srcs: ["foo.java"], 135 sdk_version: "29", 136 } 137 `) 138 module := ctx.ModuleForTests(t, "javalib", "android_common").Module().(*Library) 139 dpInfo, _ := android.OtherModuleProvider(ctx, module, android.IdeInfoProviderKey) 140 141 android.AssertStringListContains(t, "IdeInfo.Deps should contain versioned sdk module", dpInfo.Deps, "sdk_public_29_android") 142} 143 144func TestDoNotAddNoneSystemModulesToDeps(t *testing.T) { 145 ctx := android.GroupFixturePreparers( 146 prepareForJavaTest, 147 android.FixtureMergeEnv( 148 map[string]string{ 149 "DISABLE_STUB_VALIDATION": "true", 150 }, 151 ), 152 ).RunTestWithBp(t, 153 ` 154 java_library { 155 name: "javalib", 156 srcs: ["foo.java"], 157 sdk_version: "none", 158 system_modules: "none", 159 } 160 161 java_api_library { 162 name: "javalib.stubs", 163 stubs_type: "everything", 164 api_contributions: ["javalib-current.txt"], 165 api_surface: "public", 166 system_modules: "none", 167 } 168 java_api_contribution { 169 name: "javalib-current.txt", 170 api_file: "javalib-current.txt", 171 api_surface: "public", 172 } 173 `) 174 javalib := ctx.ModuleForTests(t, "javalib", "android_common").Module().(*Library) 175 dpInfo, _ := android.OtherModuleProvider(ctx, javalib, android.IdeInfoProviderKey) 176 android.AssertStringListDoesNotContain(t, "IdeInfo.Deps should contain not contain `none`", dpInfo.Deps, "none") 177 178 javalib_stubs := ctx.ModuleForTests(t, "javalib.stubs", "android_common").Module().(*ApiLibrary) 179 dpInfo, _ = android.OtherModuleProvider(ctx, javalib_stubs, android.IdeInfoProviderKey) 180 android.AssertStringListDoesNotContain(t, "IdeInfo.Deps should contain not contain `none`", dpInfo.Deps, "none") 181} 182