1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tools.metalava.stub 18 19 import com.android.tools.lint.checks.infrastructure.TestFile 20 import com.android.tools.metalava.ARG_EXCLUDE_ANNOTATION 21 import com.android.tools.metalava.DriverTest 22 import com.android.tools.metalava.model.ANDROIDX_NONNULL 23 import com.android.tools.metalava.model.text.FileFormat 24 import com.android.tools.metalava.testing.java 25 import org.intellij.lang.annotations.Language 26 27 abstract class AbstractStubsTest : DriverTest() { checkStubsnull28 protected fun checkStubs( 29 // source is a wrapper for stubFiles. When passing multiple stub Java files to test, 30 // use stubFiles. 31 @Language("JAVA") source: String = "", 32 stubFiles: Array<TestFile> = emptyArray(), 33 warnings: String? = "", 34 api: String? = null, 35 extraArguments: Array<String> = emptyArray(), 36 docStubs: Boolean = false, 37 showAnnotations: Array<String> = emptyArray(), 38 skipEmitPackages: List<String> = listOf("java.lang", "java.util", "java.io"), 39 format: FileFormat = FileFormat.LATEST, 40 sourceFiles: Array<TestFile> = emptyArray(), 41 signatureSources: Array<String> = emptyArray(), 42 checkTextStubEquivalence: Boolean = false 43 ) { 44 val stubFilesArr = if (source.isNotEmpty()) arrayOf(java(source)) else stubFiles 45 check( 46 sourceFiles = sourceFiles, 47 signatureSources = signatureSources, 48 showAnnotations = showAnnotations, 49 stubFiles = stubFilesArr, 50 expectedIssues = warnings, 51 checkCompilation = true, 52 api = api, 53 extraArguments = extraArguments, 54 docStubs = docStubs, 55 skipEmitPackages = skipEmitPackages, 56 format = format 57 ) 58 if (checkTextStubEquivalence) { 59 if (stubFilesArr.isEmpty()) { 60 addError( 61 "Stub files may not be empty when checkTextStubEquivalence is set to true." 62 ) 63 return 64 } 65 if (docStubs) { 66 addError("From-text stub generation is not supported for documentation stub.") 67 return 68 } 69 if (stubFilesArr.any { it !is TestFile.JavaTestFile }) { 70 addError("From-text stub generation is only supported for Java stubs.") 71 return 72 } 73 check( 74 signatureSources = arrayOf(readFile(getApiFile())), 75 showAnnotations = showAnnotations, 76 stubFiles = stubFilesArr, 77 expectedIssues = warnings, 78 checkCompilation = true, 79 extraArguments = arrayOf(*extraArguments, ARG_EXCLUDE_ANNOTATION, ANDROIDX_NONNULL), 80 skipEmitPackages = skipEmitPackages, 81 format = format 82 ) 83 } 84 } 85 } 86