diff options
author | MÃ¥rten Kongstad <amhk@google.com> | 2023-02-21 09:39:36 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-02-21 09:39:36 +0000 |
commit | f31718e3e3f904c18c7beb47ebe5513ce8211f81 (patch) | |
tree | ecf2b9e3e61dd374d7dd716c566dbaa985ad215e | |
parent | bd00ecdfc545162188b6b1e3a534dc39f8d40bed (diff) | |
parent | 8527ed3104cde783c68b3b120737221123d29e14 (diff) | |
download | modules-utils-f31718e3e3f904c18c7beb47ebe5513ce8211f81.tar.gz |
Merge changes from topic "flagged-api-mvp" am: 8527ed3104
Original change: https://android-review.googlesource.com/c/platform/frameworks/libs/modules-utils/+/2432272
Change-Id: I46f80276804ee95442cb0ddab59dd3781814197e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | java/android/annotation/FlaggedApi.java | 39 | ||||
-rw-r--r-- | java/com/android/modules/testing/utils/Android.bp | 27 | ||||
-rw-r--r-- | java/com/android/modules/testing/utils/FlaggedApiRule.java | 118 | ||||
-rw-r--r-- | javatests/Android.bp | 3 | ||||
-rw-r--r-- | javatests/com/android/modules/testing/utils/FlaggedApiRuleTest.java | 50 |
5 files changed, 237 insertions, 0 deletions
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); + } +} |