aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-03-08 01:35:02 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2023-03-08 01:35:02 +0000
commitbefb5246ccbaf9db78544ad4847ba351fc64789b (patch)
treeecf2b9e3e61dd374d7dd716c566dbaa985ad215e
parent1eb90217d2038cfdee1c101757a1d7379b74c208 (diff)
parentf31718e3e3f904c18c7beb47ebe5513ce8211f81 (diff)
downloadmodules-utils-aml_ads_331710270.tar.gz
Snap for 9706066 from f31718e3e3f904c18c7beb47ebe5513ce8211f81 to mainline-adservices-releaseaml_ads_331710270
Change-Id: I4276408f91508d92b34a1b7d023b09eca990f353
-rw-r--r--build/include/android-modules-utils/sdk_level.h2
-rw-r--r--java/android/annotation/FlaggedApi.java39
-rw-r--r--java/com/android/modules/testing/utils/Android.bp27
-rw-r--r--java/com/android/modules/testing/utils/FlaggedApiRule.java118
-rw-r--r--javatests/Android.bp3
-rw-r--r--javatests/com/android/modules/testing/utils/FlaggedApiRuleTest.java50
6 files changed, 238 insertions, 1 deletions
diff --git a/build/include/android-modules-utils/sdk_level.h b/build/include/android-modules-utils/sdk_level.h
index a517df4..30f0484 100644
--- a/build/include/android-modules-utils/sdk_level.h
+++ b/build/include/android-modules-utils/sdk_level.h
@@ -35,7 +35,7 @@ inline void GetCodename(char (&codename)[PROP_VALUE_MAX]) {
// Checks if the codename is a matching or higher version than the device's
// codename.
-static bool IsAtLeastPreReleaseCodename(const char *codename) {
+[[maybe_unused]] static bool IsAtLeastPreReleaseCodename(const char *codename) {
char deviceCodename[PROP_VALUE_MAX];
GetCodename(deviceCodename);
return strcmp(deviceCodename, "REL") != 0 &&
diff --git a/java/android/annotation/FlaggedApi.java b/java/android/annotation/FlaggedApi.java
new file mode 100644
index 0000000..f9f07cf
--- /dev/null
+++ b/java/android/annotation/FlaggedApi.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2023 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 android.annotation;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PACKAGE;
+import static java.lang.annotation.ElementType.TYPE;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Indicates an API can be made hidden or public based on decisions in build time.
+ * </p>
+ * This annotation should only appear on API that are already public and not marked
+ * <pre>@hide</pre>.
+ *
+ * @hide
+ */
+@Target({TYPE, FIELD, METHOD, CONSTRUCTOR, ANNOTATION_TYPE, PACKAGE})
+@Retention(RetentionPolicy.SOURCE)
+public @interface FlaggedApi {}
diff --git a/java/com/android/modules/testing/utils/Android.bp b/java/com/android/modules/testing/utils/Android.bp
new file mode 100644
index 0000000..0dacf87
--- /dev/null
+++ b/java/com/android/modules/testing/utils/Android.bp
@@ -0,0 +1,27 @@
+//
+// Copyright (C) 2023 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 {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+java_library {
+ name: "modules-testing-flaggedapi-rule",
+ srcs: ["FlaggedApiRule.java"],
+ defaults: ["modules-utils-defaults"],
+ static_libs: [
+ "junit",
+ ],
+}
diff --git a/java/com/android/modules/testing/utils/FlaggedApiRule.java b/java/com/android/modules/testing/utils/FlaggedApiRule.java
new file mode 100644
index 0000000..acd3913
--- /dev/null
+++ b/java/com/android/modules/testing/utils/FlaggedApiRule.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2023 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.modules.testing.utils;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.CONSTRUCTOR;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PACKAGE;
+import static java.lang.annotation.ElementType.TYPE;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+import org.junit.AssumptionViolatedException;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * A JUnit Rule to skip tests that rely on <pre>@FlaggedApi</pre> symbols, if those symbols have
+ * been configured to be hidden.
+ *
+ * <p>This rule prevents the tests from breaking when public symbols are made hidden because of
+ * <pre>@FlaggedApi</pre>. To tell the JUnit framework which tests that rely on
+ * <pre>@FlaggedApi</pre> symbols, annotate the test with <pre>@FlaggedApiRule.UsesFlaggedApi</pre>.
+ *
+ * <p>Example usage:
+ *
+ * <pre>
+ * @RunWith(AndroidJUnit4.class)
+ * public class Test {
+ * @Rule
+ * public final FlaggedApiRule mFlaggedApiRule = FlaggedApiRule.getInstance();
+ *
+ * @Test
+ * @FlaggedApiRule.UsesFlaggedApi
+ * public void testFlaggedApiSymbol() throws Exception {
+ * // test that calls a @FlaggedApi method
+ * }
+ *
+ * @Test
+ * public void testThatWillAlwaysRun() throws Eception {
+ * // test that calls a non-@FlaggedApi method
+ * }
+ * }
+ * </pre>
+ *
+ * <p>Note: because <pre>@FlaggedApiRule.UsesFlaggedApi</pre> can be completely skipped, make sure
+ * to have other tests that verify the non-<pre>@FlaggedApi</pre> parts of your code, or your test
+ * coverage may be smaller than expected.
+ *
+ * <p>Note: requires JUnit 4.
+ *
+ * @see android.annotation.FlaggedApi
+ */
+public abstract class FlaggedApiRule implements TestRule {
+ private static final boolean SKIP_TESTS_ANNOTATED_FLAGGED_API = false;
+
+ private static final FlaggedApiRule sInstance;
+
+ static {
+ if (SKIP_TESTS_ANNOTATED_FLAGGED_API) {
+ sInstance = new SkipTestsAnnotatedWithUsesFlaggedApiRule();
+ } else {
+ sInstance = new DoNothingRule();
+ }
+ }
+
+ public static FlaggedApiRule getInstance() {
+ return sInstance;
+ }
+
+ @VisibleForTesting
+ protected static final class SkipTestsAnnotatedWithUsesFlaggedApiRule extends FlaggedApiRule {
+ @Override
+ public Statement apply(Statement statement, Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ if (description.getAnnotation(UsesFlaggedApi.class) != null) {
+ throw new AssumptionViolatedException("Skip @UsesFlaggedApi annotated test");
+ }
+ statement.evaluate();
+ }
+ };
+ }
+ }
+
+ @VisibleForTesting
+ protected static final class DoNothingRule extends FlaggedApiRule {
+ @Override
+ public Statement apply(Statement statement, Description description) {
+ return statement;
+ }
+ }
+
+ @Target({TYPE, FIELD, METHOD, CONSTRUCTOR, ANNOTATION_TYPE, PACKAGE})
+ @Retention(RetentionPolicy.RUNTIME)
+ public @interface UsesFlaggedApi {}
+}
diff --git a/javatests/Android.bp b/javatests/Android.bp
index fa10bc7..910d824 100644
--- a/javatests/Android.bp
+++ b/javatests/Android.bp
@@ -24,13 +24,16 @@ android_test {
min_sdk_version: "29",
srcs: [
+ "com/android/internal/testing/*.java",
"com/android/internal/util/*.java",
+ "com/android/modules/testing/utils/*.java",
"com/android/modules/utils/*.java",
],
static_libs: [
"androidx.test.rules",
"androidx.test.runner",
+ "modules-testing-flaggedapi-rule",
"modules-utils-backgroundthread",
"modules-utils-bytesmatcher",
"modules-utils-handlerexecutor",
diff --git a/javatests/com/android/modules/testing/utils/FlaggedApiRuleTest.java b/javatests/com/android/modules/testing/utils/FlaggedApiRuleTest.java
new file mode 100644
index 0000000..592fa11
--- /dev/null
+++ b/javatests/com/android/modules/testing/utils/FlaggedApiRuleTest.java
@@ -0,0 +1,50 @@
+/**
+ * Copyright (C) 2023 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.modules.testing.utils;
+
+import static com.android.modules.testing.utils.FlaggedApiRule.UsesFlaggedApi;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.runners.Parameterized;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.Rule;
+
+@RunWith(Parameterized.class)
+public class FlaggedApiRuleTest {
+ @Rule
+ @Parameterized.Parameter
+ public FlaggedApiRule mFlaggedApiRule;
+
+ @Parameterized.Parameters
+ public static Object[] data() {
+ return new FlaggedApiRule[]{
+ new FlaggedApiRule.DoNothingRule(),
+ new FlaggedApiRule.SkipTestsAnnotatedWithUsesFlaggedApiRule(),
+ };
+ }
+
+ @Test
+ @UsesFlaggedApi
+ public void testIsSkippedIfRuleSaysSo() {
+ // This @Test will be called twice, with two different FlaggedApiRule objects (see the
+ // @Parameterized.Parameters annotated method above). One of the Rules will make JUnit skip
+ // this @Test: assert this is the case by checking the current @Rule when the method is
+ // actually called.
+ assertTrue(mFlaggedApiRule instanceof FlaggedApiRule.DoNothingRule);
+ }
+}