1// Copyright 2023 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 runJavaSdkLibraryTestCaseWithRegistrationCtxFunc(t *testing.T, tc Bp2buildTestCase, registrationCtxFunc func(ctx android.RegistrationContext)) { 25 t.Helper() 26 (&tc).ModuleTypeUnderTest = "java_sdk_library" 27 (&tc).ModuleTypeUnderTestFactory = java.SdkLibraryFactory 28 RunBp2BuildTestCase(t, registrationCtxFunc, tc) 29} 30 31func runJavaSdkLibraryTestCase(t *testing.T, tc Bp2buildTestCase) { 32 t.Helper() 33 runJavaSdkLibraryTestCaseWithRegistrationCtxFunc(t, tc, func(ctx android.RegistrationContext) {}) 34} 35 36func TestJavaSdkLibraryApiSurfaceGeneral(t *testing.T) { 37 runJavaSdkLibraryTestCase(t, Bp2buildTestCase{ 38 Description: "limited java_sdk_library for api surfaces, general conversion", 39 Filesystem: map[string]string{ 40 "build/soong/scripts/gen-java-current-api-files.sh": "", 41 "api/current.txt": "", 42 "api/system-current.txt": "", 43 "api/test-current.txt": "", 44 "api/module-lib-current.txt": "", 45 "api/system-server-current.txt": "", 46 "api/removed.txt": "", 47 "api/system-removed.txt": "", 48 "api/test-removed.txt": "", 49 "api/module-lib-removed.txt": "", 50 "api/system-server-removed.txt": "", 51 }, 52 Blueprint: `java_sdk_library { 53 name: "java-sdk-lib", 54 srcs: ["a.java"], 55 public: {enabled: true}, 56 system: {enabled: true}, 57 test: {enabled: true}, 58 module_lib: {enabled: true}, 59 system_server: {enabled: true}, 60}`, 61 ExpectedBazelTargets: []string{ 62 MakeBazelTarget("java_sdk_library", "java-sdk-lib", AttrNameToString{ 63 "public": `"api/current.txt"`, 64 "system": `"api/system-current.txt"`, 65 "test": `"api/test-current.txt"`, 66 "module_lib": `"api/module-lib-current.txt"`, 67 "system_server": `"api/system-server-current.txt"`, 68 }), 69 }, 70 }) 71} 72 73func TestJavaSdkLibraryApiSurfacePublicDefault(t *testing.T) { 74 runJavaSdkLibraryTestCase(t, Bp2buildTestCase{ 75 Description: "limited java_sdk_library for api surfaces, public prop uses default value", 76 Filesystem: map[string]string{ 77 "build/soong/scripts/gen-java-current-api-files.sh": "", 78 "api/current.txt": "", 79 "api/system-current.txt": "", 80 "api/test-current.txt": "", 81 "api/module-lib-current.txt": "", 82 "api/system-server-current.txt": "", 83 "api/removed.txt": "", 84 "api/system-removed.txt": "", 85 "api/test-removed.txt": "", 86 "api/module-lib-removed.txt": "", 87 "api/system-server-removed.txt": "", 88 }, 89 Blueprint: `java_sdk_library { 90 name: "java-sdk-lib", 91 srcs: ["a.java"], 92 system: {enabled: false}, 93 test: {enabled: false}, 94 module_lib: {enabled: false}, 95 system_server: {enabled: false}, 96}`, 97 ExpectedBazelTargets: []string{ 98 MakeBazelTarget("java_sdk_library", "java-sdk-lib", AttrNameToString{ 99 "public": `"api/current.txt"`, 100 }), 101 }, 102 }) 103} 104 105func TestJavaSdkLibraryApiSurfacePublicNotEnabled(t *testing.T) { 106 runJavaSdkLibraryTestCase(t, Bp2buildTestCase{ 107 Description: "limited java_sdk_library for api surfaces, public enable is false", 108 Filesystem: map[string]string{ 109 "build/soong/scripts/gen-java-current-api-files.sh": "", 110 "api/current.txt": "", 111 "api/removed.txt": "", 112 }, 113 Blueprint: `java_sdk_library { 114 name: "java-sdk-lib", 115 srcs: ["a.java"], 116 public: {enabled: false}, 117}`, 118 ExpectedBazelTargets: []string{ 119 MakeBazelTarget("java_sdk_library", "java-sdk-lib", AttrNameToString{}), 120 }, 121 }) 122} 123 124func TestJavaSdkLibraryApiSurfaceNoScopeIsSet(t *testing.T) { 125 runJavaSdkLibraryTestCase(t, Bp2buildTestCase{ 126 Description: "limited java_sdk_library for api surfaces, none of the api scopes is set", 127 Filesystem: map[string]string{ 128 "build/soong/scripts/gen-java-current-api-files.sh": "", 129 "api/current.txt": "", 130 "api/system-current.txt": "", 131 "api/test-current.txt": "", 132 "api/removed.txt": "", 133 "api/system-removed.txt": "", 134 "api/test-removed.txt": "", 135 }, 136 Blueprint: `java_sdk_library { 137 name: "java-sdk-lib", 138 srcs: ["a.java"], 139}`, 140 ExpectedBazelTargets: []string{ 141 MakeBazelTarget("java_sdk_library", "java-sdk-lib", AttrNameToString{ 142 "public": `"api/current.txt"`, 143 "system": `"api/system-current.txt"`, 144 "test": `"api/test-current.txt"`, 145 }), 146 }, 147 }) 148} 149