1// Copyright (C) 2021 The Android Open Source Project 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 sdk 16 17import ( 18 "fmt" 19 "path/filepath" 20 "testing" 21 22 "android/soong/android" 23 "android/soong/java" 24 "github.com/google/blueprint" 25) 26 27type fakeMemberTrait struct { 28 android.SdkMemberTraitBase 29} 30 31type fakeMemberType struct { 32 android.SdkMemberTypeBase 33} 34 35func (t *fakeMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) { 36 for _, name := range names { 37 ctx.AddVariationDependencies(nil, dependencyTag, name) 38 39 if ctx.RequiresTrait(name, extraTrait) { 40 ctx.AddVariationDependencies(nil, dependencyTag, name+"_extra") 41 } 42 if ctx.RequiresTrait(name, specialTrait) { 43 ctx.AddVariationDependencies(nil, dependencyTag, name+"_special") 44 } 45 } 46} 47 48func (t *fakeMemberType) IsInstance(module android.Module) bool { 49 return true 50} 51 52func (t *fakeMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule { 53 moduleType := "java_import" 54 if ctx.RequiresTrait(extraTrait) { 55 moduleType = "java_test_import" 56 } 57 return ctx.SnapshotBuilder().AddPrebuiltModule(member, moduleType) 58} 59 60func (t *fakeMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties { 61 return &fakeMemberTypeProperties{} 62} 63 64type fakeMemberTypeProperties struct { 65 android.SdkMemberPropertiesBase 66 67 path android.Path 68} 69 70func (t *fakeMemberTypeProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) { 71 headerJars := variant.(java.ApexDependency).HeaderJars() 72 if len(headerJars) != 1 { 73 panic(fmt.Errorf("there must be only one header jar from %q", variant.Name())) 74 } 75 76 t.path = headerJars[0] 77} 78 79func (t *fakeMemberTypeProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) { 80 if t.path != nil { 81 relative := filepath.Join("javalibs", t.path.Base()) 82 ctx.SnapshotBuilder().CopyToSnapshot(t.path, relative) 83 propertySet.AddProperty("jars", []string{relative}) 84 } 85} 86 87var ( 88 extraTrait = &fakeMemberTrait{ 89 SdkMemberTraitBase: android.SdkMemberTraitBase{ 90 PropertyName: "extra", 91 }, 92 } 93 94 specialTrait = &fakeMemberTrait{ 95 SdkMemberTraitBase: android.SdkMemberTraitBase{ 96 PropertyName: "special", 97 }, 98 } 99 100 fakeType = &fakeMemberType{ 101 SdkMemberTypeBase: android.SdkMemberTypeBase{ 102 PropertyName: "fake_members", 103 SupportsSdk: true, 104 Traits: []android.SdkMemberTrait{ 105 extraTrait, 106 specialTrait, 107 }, 108 }, 109 } 110) 111 112func init() { 113 android.RegisterSdkMemberTrait(extraTrait) 114 android.RegisterSdkMemberTrait(specialTrait) 115 android.RegisterSdkMemberType(fakeType) 116} 117 118func TestBasicTrait_WithoutTrait(t *testing.T) { 119 result := android.GroupFixturePreparers( 120 prepareForSdkTestWithJava, 121 android.FixtureWithRootAndroidBp(` 122 sdk { 123 name: "mysdk", 124 fake_members: ["myjavalib"], 125 } 126 127 java_library { 128 name: "myjavalib", 129 srcs: ["Test.java"], 130 system_modules: "none", 131 sdk_version: "none", 132 } 133 `), 134 ).RunTest(t) 135 136 CheckSnapshot(t, result, "mysdk", "", 137 checkUnversionedAndroidBpContents(` 138// This is auto-generated. DO NOT EDIT. 139 140java_import { 141 name: "myjavalib", 142 prefer: false, 143 visibility: ["//visibility:public"], 144 apex_available: ["//apex_available:platform"], 145 jars: ["javalibs/myjavalib.jar"], 146} 147`), 148 checkVersionedAndroidBpContents(` 149// This is auto-generated. DO NOT EDIT. 150 151java_import { 152 name: "mysdk_myjavalib@current", 153 sdk_member_name: "myjavalib", 154 visibility: ["//visibility:public"], 155 apex_available: ["//apex_available:platform"], 156 jars: ["javalibs/myjavalib.jar"], 157} 158 159sdk_snapshot { 160 name: "mysdk@current", 161 visibility: ["//visibility:public"], 162 fake_members: ["mysdk_myjavalib@current"], 163} 164`), 165 ) 166} 167 168func TestBasicTrait_MultipleTraits(t *testing.T) { 169 result := android.GroupFixturePreparers( 170 prepareForSdkTestWithJava, 171 android.FixtureWithRootAndroidBp(` 172 sdk { 173 name: "mysdk", 174 fake_members: ["myjavalib", "anotherjavalib"], 175 traits: { 176 extra: ["myjavalib"], 177 special: ["myjavalib", "anotherjavalib"], 178 }, 179 } 180 181 java_library { 182 name: "myjavalib", 183 srcs: ["Test.java"], 184 system_modules: "none", 185 sdk_version: "none", 186 } 187 188 java_library { 189 name: "myjavalib_extra", 190 srcs: ["Test.java"], 191 system_modules: "none", 192 sdk_version: "none", 193 } 194 195 java_library { 196 name: "myjavalib_special", 197 srcs: ["Test.java"], 198 system_modules: "none", 199 sdk_version: "none", 200 } 201 202 java_library { 203 name: "anotherjavalib", 204 srcs: ["Test.java"], 205 system_modules: "none", 206 sdk_version: "none", 207 } 208 209 java_library { 210 name: "anotherjavalib_special", 211 srcs: ["Test.java"], 212 system_modules: "none", 213 sdk_version: "none", 214 } 215 `), 216 ).RunTest(t) 217 218 CheckSnapshot(t, result, "mysdk", "", 219 checkUnversionedAndroidBpContents(` 220// This is auto-generated. DO NOT EDIT. 221 222java_test_import { 223 name: "myjavalib", 224 prefer: false, 225 visibility: ["//visibility:public"], 226 apex_available: ["//apex_available:platform"], 227 jars: ["javalibs/myjavalib.jar"], 228} 229 230java_import { 231 name: "myjavalib_extra", 232 prefer: false, 233 visibility: ["//visibility:public"], 234 apex_available: ["//apex_available:platform"], 235 jars: ["javalibs/myjavalib_extra.jar"], 236} 237 238java_import { 239 name: "myjavalib_special", 240 prefer: false, 241 visibility: ["//visibility:public"], 242 apex_available: ["//apex_available:platform"], 243 jars: ["javalibs/myjavalib_special.jar"], 244} 245 246java_import { 247 name: "anotherjavalib", 248 prefer: false, 249 visibility: ["//visibility:public"], 250 apex_available: ["//apex_available:platform"], 251 jars: ["javalibs/anotherjavalib.jar"], 252} 253 254java_import { 255 name: "anotherjavalib_special", 256 prefer: false, 257 visibility: ["//visibility:public"], 258 apex_available: ["//apex_available:platform"], 259 jars: ["javalibs/anotherjavalib_special.jar"], 260} 261`), 262 ) 263} 264 265func TestTraitUnsupportedByMemberType(t *testing.T) { 266 android.GroupFixturePreparers( 267 prepareForSdkTestWithJava, 268 android.FixtureWithRootAndroidBp(` 269 sdk { 270 name: "mysdk", 271 java_header_libs: ["myjavalib"], 272 traits: { 273 extra: ["myjavalib"], 274 }, 275 } 276 277 java_library { 278 name: "myjavalib", 279 srcs: ["Test.java"], 280 system_modules: "none", 281 sdk_version: "none", 282 } 283 `), 284 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern( 285 `\Qsdk member "myjavalib" has traits [extra] that are unsupported by its member type "java_header_libs"\E`)). 286 RunTest(t) 287} 288