diff options
author | Andrei Onea <andreionea@google.com> | 2019-11-18 14:24:53 +0000 |
---|---|---|
committer | Andrei-Valentin Onea <andreionea@google.com> | 2019-11-19 12:22:12 +0000 |
commit | 2829b54276da3865786d48c1fd9c5687e21fc557 (patch) | |
tree | d7571a9d3877ad723f6af10fecf2f800cb30050e /test-rules | |
parent | 0874da63b953e079bc3f2545f0930cd81211b648 (diff) |
Revert "Revert submission from topic "separate-testing"."
This reverts commit d38b50d216334359b9969606cc611b8be217887f.
The art buildbot was previously failing due to a transitive
dependency on "api-stubs-docs" and "framework-res", via the
unneeded "app-compat-annotations". This fix removes that dependency.
Bug: 140367850
Test: atest CoreCompatChangeRuleTest
Test: art/tools/buildbot-build.sh --host
Change-Id: I34ca27ca5c56ebb23bbb858c287809bc93671427
Diffstat (limited to 'test-rules')
-rw-r--r-- | test-rules/src/platform_compat/java/libcore/junit/util/CoreCompatChangeRule.java (renamed from test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java) | 44 | ||||
-rw-r--r-- | test-rules/src/test/java/android/compat/testing/DummyApi.java | 64 | ||||
-rw-r--r-- | test-rules/src/test/java/libcore/junit/util/compat/CoreCompatChangeRuleTest.java | 77 |
3 files changed, 153 insertions, 32 deletions
diff --git a/test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java b/test-rules/src/platform_compat/java/libcore/junit/util/CoreCompatChangeRule.java index d9c800ab44..59842c2306 100644 --- a/test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java +++ b/test-rules/src/platform_compat/java/libcore/junit/util/CoreCompatChangeRule.java @@ -14,19 +14,11 @@ * limitations under the License. */ -package android.compat; +package libcore.junit.util.compat; -import android.app.Instrumentation; +import android.compat.Compatibility; import android.compat.Compatibility.Callbacks; import android.compat.Compatibility.ChangeConfig; -import android.content.Context; -import android.os.RemoteException; -import android.os.ServiceManager; -import android.support.test.InstrumentationRegistry; -import android.util.ArraySet; - -import com.android.internal.compat.CompatibilityChangeConfig; -import com.android.internal.compat.IPlatformCompat; import org.junit.rules.TestRule; import org.junit.runner.Description; @@ -37,11 +29,9 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.HashSet; -import java.util.Map; import java.util.Set; import com.google.common.primitives.Longs; - /** * Allows tests to specify the which change to disable. * @@ -50,7 +40,7 @@ import com.google.common.primitives.Longs; * * <pre> * @Rule - * public TestRule compatChangeRule = new CompatChangeRule(); + * public TestRule compatChangeRule = new CoreCompatChangeRule(); * </pre> * * <p>Each test method that needs to disable a specific change needs to be annotated @@ -71,7 +61,8 @@ import com.google.common.primitives.Longs; * * </pre> */ -public class CompatChangeRule implements TestRule { +public class CoreCompatChangeRule implements TestRule { + @Override public Statement apply(final Statement statement, Description description) { Set<Long> enabled = new HashSet<>(); @@ -89,9 +80,13 @@ public class CompatChangeRule implements TestRule { ChangeConfig config = new ChangeConfig(enabled, disabled); if (config.isEmpty()) { throw new IllegalArgumentException("Added a CompatChangeRule without specifying any " - + "@EnableCompatChanges or @DisableCompatChanges !"); + + "@EnableCompatChanges or @DisableCompatChanges !"); } - return new CompatChangeStatement(statement, config); + return createStatementForConfig(statement, config); + } + + protected Statement createStatementForConfig(final Statement statement, ChangeConfig config) { + return new CompatChangeStatement(statement, config); } private static class CompatChangeStatement extends Statement { @@ -105,24 +100,9 @@ public class CompatChangeRule implements TestRule { @Override public void evaluate() throws Throwable { - Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); - String packageName = instrumentation.getTargetContext().getPackageName(); - IPlatformCompat platformCompat = IPlatformCompat.Stub - .asInterface(ServiceManager.getService(Context.PLATFORM_COMPAT_SERVICE)); - if (platformCompat == null) { - throw new IllegalStateException("Could not get IPlatformCompat service!"); - } Compatibility.setOverrides(config); try { - platformCompat.setOverridesForTest(new CompatibilityChangeConfig(config), - packageName); - try { - testStatement.evaluate(); - } finally { - platformCompat.clearOverrides(packageName); - } - } catch (RemoteException e) { - throw new RuntimeException("Could not call IPlatformCompat binder method!", e); + testStatement.evaluate(); } finally { Compatibility.clearOverrides(); } diff --git a/test-rules/src/test/java/android/compat/testing/DummyApi.java b/test-rules/src/test/java/android/compat/testing/DummyApi.java new file mode 100644 index 0000000000..dfe40cc022 --- /dev/null +++ b/test-rules/src/test/java/android/compat/testing/DummyApi.java @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2016 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.compat.testing; + +import android.compat.Compatibility; + +/** + * This is a dummy API to test gating + * + * @hide + */ +public class DummyApi { + + public static final long CHANGE_ID = 666013; + public static final long CHANGE_ID_1 = 666014; + public static final long CHANGE_ID_2 = 666015; + + /** + * Dummy method + * @return "A" if change is enabled, "B" otherwise. + */ + public static String dummyFunc() { + if (Compatibility.isChangeEnabled(CHANGE_ID)) { + return "A"; + } + return "B"; + } + + /** + * Dummy combined method + * @return "0" if {@link CHANGE_ID_1} is disabled and {@link CHANGE_ID_2} is disabled, + "1" if {@link CHANGE_ID_1} is disabled and {@link CHANGE_ID_2} is enabled, + "2" if {@link CHANGE_ID_1} is enabled and {@link CHANGE_ID_2} is disabled, + "3" if {@link CHANGE_ID_1} is enabled and {@link CHANGE_ID_2} is enabled. + */ + public static String dummyCombinedFunc() { + if (!Compatibility.isChangeEnabled(CHANGE_ID_1) + && !Compatibility.isChangeEnabled(CHANGE_ID_2)) { + return "0"; + } else if (!Compatibility.isChangeEnabled(CHANGE_ID_1) + && Compatibility.isChangeEnabled(CHANGE_ID_2)) { + return "1"; + } else if (Compatibility.isChangeEnabled(CHANGE_ID_1) + && !Compatibility.isChangeEnabled(CHANGE_ID_2)) { + return "2"; + } + return "3"; + } + +}
\ No newline at end of file diff --git a/test-rules/src/test/java/libcore/junit/util/compat/CoreCompatChangeRuleTest.java b/test-rules/src/test/java/libcore/junit/util/compat/CoreCompatChangeRuleTest.java new file mode 100644 index 0000000000..87b9446eac --- /dev/null +++ b/test-rules/src/test/java/libcore/junit/util/compat/CoreCompatChangeRuleTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2018 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 libcore.junit.util.compat; + +import static com.google.common.truth.Truth.assertThat; + +import android.compat.testing.DummyApi; + +import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges; +import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TestRule; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Tests for compatibility change gating. + */ +@RunWith(JUnit4.class) +public class CoreCompatChangeRuleTest { + + @Rule + public TestRule compatChangeRule = new CoreCompatChangeRule(); + + @Test + @EnableCompatChanges({DummyApi.CHANGE_ID}) + public void testDummyGatingPositive() { + assertThat(DummyApi.dummyFunc()).isEqualTo("A"); + } + + @Test + @DisableCompatChanges({DummyApi.CHANGE_ID}) + public void testDummyGatingNegative() { + assertThat(DummyApi.dummyFunc()).isEqualTo("B"); + } + + @Test + @DisableCompatChanges({DummyApi.CHANGE_ID_1, DummyApi.CHANGE_ID_2}) + public void testDummyGatingCombined0() { + assertThat(DummyApi.dummyCombinedFunc()).isEqualTo("0"); + } + + @Test + @DisableCompatChanges({DummyApi.CHANGE_ID_1}) + @EnableCompatChanges({DummyApi.CHANGE_ID_2}) + public void testDummyGatingCombined1() { + assertThat(DummyApi.dummyCombinedFunc()).isEqualTo("1"); + } + + @Test + @EnableCompatChanges({DummyApi.CHANGE_ID_1}) + @DisableCompatChanges({DummyApi.CHANGE_ID_2}) + public void testDummyGatingCombined2() { + assertThat(DummyApi.dummyCombinedFunc()).isEqualTo("2"); + } + + @Test + @EnableCompatChanges({DummyApi.CHANGE_ID_1, DummyApi.CHANGE_ID_2}) + public void testDummyGatingCombined3() { + assertThat(DummyApi.dummyCombinedFunc()).isEqualTo("3"); + } +}
\ No newline at end of file |