1// Copyright 2022 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 bp2build 16 17import ( 18 "testing" 19 20 "android/soong/android" 21 "android/soong/java" 22) 23 24func registerJavaApiModules(ctx android.RegistrationContext) { 25 java.RegisterSdkLibraryBuildComponents(ctx) 26 java.RegisterStubsBuildComponents(ctx) 27} 28 29func TestDroidstubsApiContributions(t *testing.T) { 30 bp := ` 31 droidstubs { 32 name: "framework-stubs", 33 check_api: { 34 current: { 35 api_file: "framework.current.txt", 36 }, 37 }, 38 } 39 40 // Modules without check_api should not generate a Bazel API target 41 droidstubs { 42 name: "framework-docs", 43 } 44 45 // java_sdk_library is a macro that creates droidstubs 46 java_sdk_library { 47 name: "module-stubs", 48 srcs: ["A.java"], 49 50 // These api surfaces are added by default, but add them explicitly to make 51 // this test hermetic 52 public: { 53 enabled: true, 54 }, 55 system: { 56 enabled: true, 57 }, 58 59 // Disable other api surfaces to keep unit test scope limited 60 module_lib: { 61 enabled: false, 62 }, 63 test: { 64 enabled: false, 65 }, 66 } 67 ` 68 expectedBazelTargets := []string{ 69 MakeBazelTargetNoRestrictions( 70 "java_api_contribution", 71 "framework-stubs.contribution", 72 AttrNameToString{ 73 "api": `"framework.current.txt"`, 74 "api_surface": `"publicapi"`, 75 "target_compatible_with": `["//build/bazel/platforms/os:android"]`, 76 }), 77 MakeBazelTargetNoRestrictions( 78 "java_api_contribution", 79 "module-stubs.stubs.source.contribution", 80 AttrNameToString{ 81 "api": `"api/current.txt"`, 82 "api_surface": `"publicapi"`, 83 "target_compatible_with": `["//build/bazel/platforms/os:android"]`, 84 }), 85 MakeBazelTargetNoRestrictions( 86 "java_api_contribution", 87 "module-stubs.stubs.source.system.contribution", 88 AttrNameToString{ 89 "api": `"api/system-current.txt"`, 90 "api_surface": `"systemapi"`, 91 "target_compatible_with": `["//build/bazel/platforms/os:android"]`, 92 }), 93 } 94 RunApiBp2BuildTestCase(t, registerJavaApiModules, Bp2buildTestCase{ 95 Blueprint: bp, 96 ExpectedBazelTargets: expectedBazelTargets, 97 Filesystem: map[string]string{ 98 "api/current.txt": "", 99 "api/removed.txt": "", 100 "api/system-current.txt": "", 101 "api/system-removed.txt": "", 102 }, 103 }) 104} 105