Add an annotation to skip tests if the APK has target SDK > 31.

This skips a test if the APK it's running in targets an SDK
above 31. This allows us to add such tests to CtsNetTestCases,
which is currently targeting SDK 33.

By using the annotation as an include-annotation filter in
AndroidTestTemplate.xml, this makes it very easy to create a
clone of CtsNetTestCases that targets SDK 31 and which only runs
tests annotated with this annotation. Thus, CtsNetTestCases and
CtsNetTestCasesLatestSdk will run all tests that pass if target
SDK is 33, and CtsNetTestCasesMaxTargetSdk31 will only run the
tests that requires target SDK 31. See other CL in topic.

Bug: 233553525
Test: atest CtsNetTestCasesMaxTargetSdk31:android.net.LinkPropertiesTest
Test: atest CtsNetTestCasesLatestSdk:android.net.LinkPropertiesTest
Change-Id: Ib5df3bbc659d6a737a846fdf57cddee002e4e9ae
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRule.kt b/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRule.kt
index 8b58e71..2d2f5a6 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRule.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/DevSdkIgnoreRule.kt
@@ -17,16 +17,21 @@
 package com.android.testutils
 
 import android.os.Build
+import androidx.test.InstrumentationRegistry
 import com.android.modules.utils.build.SdkLevel
 import kotlin.test.fail
 import org.junit.Assume.assumeTrue
 import org.junit.rules.TestRule
 import org.junit.runner.Description
 import org.junit.runners.model.Statement
+import java.util.regex.Pattern
 
 // TODO: Remove it when Build.VERSION_CODES.SC_V2 is available
 const val SC_V2 = 32
 
+private val MAX_TARGET_SDK_ANNOTATION_RE = Pattern.compile("MaxTargetSdk([0-9]+)$")
+private val targetSdk = InstrumentationRegistry.getContext().applicationInfo.targetSdkVersion
+
 /**
  * Returns true if the development SDK version of the device is in the provided range.
  *
@@ -67,6 +72,14 @@
     }
 }
 
+private fun getMaxTargetSdk(description: Description): Int? {
+    return description.annotations.firstNotNullOfOrNull {
+        MAX_TARGET_SDK_ANNOTATION_RE.matcher(it::class.simpleName).let { m ->
+            if (m.find()) m.group(1).toIntOrNull() else null
+        }
+    }
+}
+
 /**
  * A test rule to ignore tests based on the development SDK level.
  *
@@ -108,11 +121,16 @@
             val ignoreAfter = description.getAnnotation(IgnoreAfter::class.java)
             val ignoreUpTo = description.getAnnotation(IgnoreUpTo::class.java)
 
-            val message = "Skipping test for build ${Build.VERSION.CODENAME} " +
+            val devSdkMessage = "Skipping test for build ${Build.VERSION.CODENAME} " +
                     "with SDK ${Build.VERSION.SDK_INT}"
-            assumeTrue(message, isDevSdkInRange(ignoreClassUpTo, ignoreClassAfter))
-            assumeTrue(message, isDevSdkInRange(ignoreUpTo?.value, ignoreAfter?.value))
-            base.evaluate()
+            assumeTrue(devSdkMessage, isDevSdkInRange(ignoreClassUpTo, ignoreClassAfter))
+            assumeTrue(devSdkMessage, isDevSdkInRange(ignoreUpTo?.value, ignoreAfter?.value))
+
+            val maxTargetSdk = getMaxTargetSdk(description)
+            if (maxTargetSdk != null) {
+                assumeTrue("Skipping test, target SDK $targetSdk greater than $maxTargetSdk",
+                        targetSdk <= maxTargetSdk)
+            }
         }
     }
-}
\ No newline at end of file
+}
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/filters/CtsNetTestCasesMaxTargetSdk31.kt b/staticlibs/testutils/devicetests/com/android/testutils/filters/CtsNetTestCasesMaxTargetSdk31.kt
new file mode 100644
index 0000000..be0103d
--- /dev/null
+++ b/staticlibs/testutils/devicetests/com/android/testutils/filters/CtsNetTestCasesMaxTargetSdk31.kt
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.testutils.filters
+
+/**
+ * Only run this test in the CtsNetTestCasesMaxTargetSdk31 suite.
+ */
+annotation class CtsNetTestCasesMaxTargetSdk31(val reason: String)