Merge "Improve handling of android auto classes." am: 1de2d692ea am: 3b4155d65a

Original change: https://android-review.googlesource.com/c/platform/cts/+/1679177

Change-Id: Ibf00e30962526b918b210cf665f691214395bc5a
diff --git a/tests/signature/api-check/system-annotation/src/java/android/signature/cts/api/AnnotationTest.java b/tests/signature/api-check/system-annotation/src/java/android/signature/cts/api/AnnotationTest.java
index 7adc3e3..a3c46d5 100644
--- a/tests/signature/api-check/system-annotation/src/java/android/signature/cts/api/AnnotationTest.java
+++ b/tests/signature/api-check/system-annotation/src/java/android/signature/cts/api/AnnotationTest.java
@@ -27,6 +27,7 @@
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.function.Predicate;
 
 /**
  * Checks that parts of the device's API that are annotated (e.g. with android.annotation.SystemApi)
@@ -45,6 +46,15 @@
         mAnnotationForExactMatch = instrumentationArgs.getString("annotation-for-exact-match");
     }
 
+    private Predicate<? super JDiffClassDescription> androidAutoClassesFilter() {
+        if (getInstrumentation().getContext().getPackageManager().hasSystemFeature(
+                "android.hardware.type.automotive")) {
+            return clz -> true;
+        } else {
+            return clz -> !clz.getAbsoluteClassName().startsWith("android.car.");
+        }
+    }
+
     /**
      * Tests that the parts of the device's API that are annotated (e.g. with
      * android.annotation.SystemApi) match the API definition.
@@ -81,6 +91,7 @@
             ApiDocumentParser apiDocumentParser = new ApiDocumentParser(TAG);
 
             parseApiResourcesAsStream(apiDocumentParser, mExpectedApiFiles)
+                    .filter(androidAutoClassesFilter())
                     .forEach(complianceChecker::checkSignatureCompliance);
 
             // After done parsing all expected API files, perform any deferred checks.
diff --git a/tests/signature/lib/common/src/android/signature/cts/AbstractApiChecker.java b/tests/signature/lib/common/src/android/signature/cts/AbstractApiChecker.java
index 03c4a85..cb78f83 100644
--- a/tests/signature/lib/common/src/android/signature/cts/AbstractApiChecker.java
+++ b/tests/signature/lib/common/src/android/signature/cts/AbstractApiChecker.java
@@ -63,14 +63,10 @@
                     .findRequiredClass(classDescription, classProvider);
 
             if (runtimeClass == null) {
-                // No class found, notify the observer according to the class type,
-                // if missing a class isn't acceptable.
-                if (!allowMissingClass(classDescription)) {
-                    resultObserver.notifyFailure(FailureType.missing(classDescription),
-                            classDescription.getAbsoluteClassName(),
-                            "Classloader is unable to find " + classDescription
-                                    .getAbsoluteClassName());
-                }
+                resultObserver.notifyFailure(FailureType.missing(classDescription),
+                        classDescription.getAbsoluteClassName(),
+                        "Classloader is unable to find " + classDescription
+                                .getAbsoluteClassName());
                 return null;
             }
 
@@ -109,17 +105,6 @@
 
 
     /**
-     * Checks that a class that exists in the API xml file but that does not exist
-     * in the runtime is allowed or not.
-     *
-     * @param classDescription the class description that is missing.
-     * @return true if missing the class is acceptable.
-     */
-    protected boolean allowMissingClass(JDiffClassDescription classDescription) {
-        return false;
-    }
-
-    /**
      * Checks all fields in test class for compliance with the API xml.
      *
      * @param classDescription a description of a class in an API.
diff --git a/tests/signature/lib/common/src/android/signature/cts/AnnotationChecker.java b/tests/signature/lib/common/src/android/signature/cts/AnnotationChecker.java
index 3f14dad..71ab600 100644
--- a/tests/signature/lib/common/src/android/signature/cts/AnnotationChecker.java
+++ b/tests/signature/lib/common/src/android/signature/cts/AnnotationChecker.java
@@ -39,7 +39,7 @@
     private final Map<String, Set<Field>> annotatedFieldsMap = new HashMap<>();
 
     /**
-     * @param annotationName name of the annotation class for the API type (e.g.
+     * @param annotationSpec name of the annotation class for the API type (e.g.
      *      android.annotation.SystemApi)
      */
     public AnnotationChecker(
@@ -117,17 +117,6 @@
     }
 
     @Override
-    protected boolean allowMissingClass(JDiffClassDescription classDescription) {
-        // A class that exist in the API document is not found in the runtime.
-        // This can happen for classes that are optional (e.g. classes for
-        // Android Auto). This, however, should not be considered as a test
-        // failure, because the purpose of this test is to ensure that every
-        // runtime classes found in the device have more annotations than
-        // the documented.
-        return true;
-    }
-
-    @Override
     protected boolean checkClass(JDiffClassDescription classDescription, Class<?> runtimeClass) {
         // remove the class from the set if found
         annotatedClassesMap.remove(runtimeClass.getName());