Merge "Revert submission from topic "separate-testing"."
diff --git a/JavaLibrary.bp b/JavaLibrary.bp
index ec47b5a..c0777ff 100644
--- a/JavaLibrary.bp
+++ b/JavaLibrary.bp
@@ -492,10 +492,8 @@
 
 // Builds platform_compat test rules
 java_library_static {
-    name: "core-compat-test-rules",
-    visibility: [
-        "//frameworks/base/tests/PlatformCompatGating/test-rules",
-    ],
+    name: "platform_compat-test-rules",
+    visibility: ["//visibility:public"],
     srcs: [
         "luni/src/main/java/android/compat/**/*.java",
         "test-rules/src/platform_compat/**/*.java",
@@ -505,13 +503,12 @@
     static_libs: [
         "junit",
         "guava",
+        "android-support-test",
         "app-compat-annotations",
     ],
-    sdk_version: "none",
-    system_modules: "core-all-system-modules",
+    platform_apis: true,
     // 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.
@@ -609,7 +606,6 @@
 
     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 e5d7cbb..1fbdf76 100644
--- a/luni/src/main/java/android/compat/Compatibility.java
+++ b/luni/src/main/java/android/compat/Compatibility.java
@@ -121,14 +121,13 @@
         }
         @CorePlatformApi
         protected void reportChange(long changeId) {
-            System.logW(String.format(
+            throw new IllegalStateException(String.format(
                     "No Compatibility callbacks set! Reporting change %d", changeId));
         }
         @CorePlatformApi
         protected boolean isChangeEnabled(long changeId) {
-            System.logW(String.format(
+            throw new IllegalStateException(String.format(
                     "No Compatibility callbacks set! Querying change %d", changeId));
-            return true;
         }
     }
 
diff --git a/test-rules/src/platform_compat/java/libcore/junit/util/CoreCompatChangeRule.java b/test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java
similarity index 70%
rename from test-rules/src/platform_compat/java/libcore/junit/util/CoreCompatChangeRule.java
rename to test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java
index 59842c2..1bae1a2 100644
--- a/test-rules/src/platform_compat/java/libcore/junit/util/CoreCompatChangeRule.java
+++ b/test-rules/src/platform_compat/java/android/compat/CompatChangeRule.java
@@ -14,11 +14,19 @@
  * limitations under the License.
  */
 
-package libcore.junit.util.compat;
+package android.compat;
 
-import android.compat.Compatibility;
+import android.app.Instrumentation;
 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;
@@ -29,9 +37,11 @@
 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.
  *
@@ -40,7 +50,7 @@
  *
  * <pre>
  * &#64;Rule
- * public TestRule compatChangeRule = new CoreCompatChangeRule();
+ * public TestRule compatChangeRule = new CompatChangeRule();
  * </pre>
  *
  * <p>Each test method that needs to disable a specific change needs to be annotated
@@ -61,8 +71,7 @@
  *
  * </pre>
  */
-public class CoreCompatChangeRule implements TestRule {
-
+public class CompatChangeRule implements TestRule {
     @Override
     public Statement apply(final Statement statement, Description description) {
         Set<Long> enabled = new HashSet<>();
@@ -80,13 +89,9 @@
         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 createStatementForConfig(statement, config);
-    }
-
-    protected Statement createStatementForConfig(final Statement statement, ChangeConfig config) {
-            return new CompatChangeStatement(statement, config);
+        return new CompatChangeStatement(statement, config);
     }
 
     private static class CompatChangeStatement extends Statement {
@@ -100,9 +105,23 @@
 
         @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 {
-                testStatement.evaluate();
+                platformCompat.setOverrides(new CompatibilityChangeConfig(config), packageName);
+                try {
+                    testStatement.evaluate();
+                } finally {
+                    platformCompat.clearOverrides(packageName);
+                }
+            } catch(RemoteException e) {
+                throw new RuntimeException("Could not call IPlatformCompat binder method!", e);
             } 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
deleted file mode 100644
index dfe40cc..0000000
--- a/test-rules/src/test/java/android/compat/testing/DummyApi.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * 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
deleted file mode 100644
index 87b9446..0000000
--- a/test-rules/src/test/java/libcore/junit/util/compat/CoreCompatChangeRuleTest.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * 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