summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--JavaLibrary.bp13
-rw-r--r--luni/src/main/java/android/compat/Compatibility.java5
-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.java64
-rw-r--r--test-rules/src/test/java/libcore/junit/util/compat/CoreCompatChangeRuleTest.java77
5 files changed, 164 insertions, 39 deletions
diff --git a/JavaLibrary.bp b/JavaLibrary.bp
index c0777ffd07..8e8301b554 100644
--- a/JavaLibrary.bp
+++ b/JavaLibrary.bp
@@ -492,8 +492,10 @@ java_library_static {
// Builds platform_compat test rules
java_library_static {
- name: "platform_compat-test-rules",
- visibility: ["//visibility:public"],
+ name: "core-compat-test-rules",
+ visibility: [
+ "//frameworks/base/tests/PlatformCompatGating/test-rules",
+ ],
srcs: [
"luni/src/main/java/android/compat/**/*.java",
"test-rules/src/platform_compat/**/*.java",
@@ -503,12 +505,12 @@ java_library_static {
static_libs: [
"junit",
"guava",
- "android-support-test",
- "app-compat-annotations",
],
- platform_apis: true,
+ sdk_version: "none",
+ system_modules: "core-all-system-modules",
// This builds classes that are in the java.base Java module:
patch_module: "java.base",
+ hostdex: true,
}
// Builds the core-tests-support library used by various tests.
@@ -606,6 +608,7 @@ java_test {
static_libs: [
"archive-patcher",
+ "core-compat-test-rules",
"core-java-9-language-tests",
"core-test-rules",
"core-tests-support",
diff --git a/luni/src/main/java/android/compat/Compatibility.java b/luni/src/main/java/android/compat/Compatibility.java
index 1fbdf76a68..e5d7cbb8d8 100644
--- a/luni/src/main/java/android/compat/Compatibility.java
+++ b/luni/src/main/java/android/compat/Compatibility.java
@@ -121,13 +121,14 @@ public final class Compatibility {
}
@CorePlatformApi
protected void reportChange(long changeId) {
- throw new IllegalStateException(String.format(
+ System.logW(String.format(
"No Compatibility callbacks set! Reporting change %d", changeId));
}
@CorePlatformApi
protected boolean isChangeEnabled(long changeId) {
- throw new IllegalStateException(String.format(
+ System.logW(String.format(
"No Compatibility callbacks set! Querying change %d", changeId));
+ return true;
}
}
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>
* &#64;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