1 /*
2  * Copyright (C) 2011 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 android.signature.cts.api;
18 
19 import android.os.Build;
20 import android.os.Bundle;
21 import android.signature.cts.AnnotationChecker;
22 import android.signature.cts.ApiDocumentParser;
23 
24 import com.android.compatibility.common.util.PropertyUtil;
25 
26 /**
27  * Checks that parts of the device's API that are annotated (e.g. with android.annotation.SystemApi)
28  * match the API definition.
29  */
30 public class AnnotationTest extends AbstractApiTest {
31 
32     private static final String TAG = AnnotationTest.class.getSimpleName();
33 
34     private String[] expectedApiFiles;
35     private String annotationForExactMatch;
36 
37     @Override
initializeFromArgs(Bundle instrumentationArgs)38     protected void initializeFromArgs(Bundle instrumentationArgs) throws Exception {
39         expectedApiFiles = getCommaSeparatedList(instrumentationArgs, "expected-api-files");
40         annotationForExactMatch = instrumentationArgs.getString("annotation-for-exact-match");
41     }
42 
43     /**
44      * Tests that the parts of the device's API that are annotated (e.g. with
45      * android.annotation.SystemApi) match the API definition.
46      */
testAnnotation()47     public void testAnnotation() {
48         if ("true".equals(PropertyUtil.getProperty("ro.treble.enabled")) &&
49                 PropertyUtil.getFirstApiLevel() > Build.VERSION_CODES.O_MR1) {
50             runWithTestResultObserver(resultObserver -> {
51                 AnnotationChecker complianceChecker = new AnnotationChecker(resultObserver,
52                         classProvider, annotationForExactMatch);
53 
54                 ApiDocumentParser apiDocumentParser = new ApiDocumentParser(TAG);
55 
56                 parseApiResourcesAsStream(apiDocumentParser, expectedApiFiles)
57                         .forEach(complianceChecker::checkSignatureCompliance);
58 
59                 // After done parsing all expected API files, perform any deferred checks.
60                 complianceChecker.checkDeferred();
61             });
62         }
63     }
64 }
65