Add tests for framework-specific Error Prone.

We recently started writing custom Error Prone checkers, but it's
been painfully slow to develop against the giant source tree, so
this change adds tests to verify existing behavior and to enable TDD
for future checkers.

Bug: 155703208
Test: atest error_prone_android_framework_test
Change-Id: I7ea7484db5d19e812354703e561a499077329098
diff --git a/errorprone/Android.bp b/errorprone/Android.bp
index 098f4bf..c5f1892 100644
--- a/errorprone/Android.bp
+++ b/errorprone/Android.bp
@@ -21,3 +21,25 @@
         "//external/dagger2:dagger2-auto-service",
     ],
 }
+
+java_test_host {
+    name: "error_prone_android_framework_test",
+    test_suites: ["general-tests"],
+    srcs: ["tests/java/**/*.java"],
+    java_resource_dirs: ["tests/res"],
+    java_resources: [":error_prone_android_framework_testdata"],
+    static_libs: [
+        "error_prone_android_framework_lib",
+        "error_prone_test_helpers",
+        "hamcrest-library",
+        "hamcrest",
+        "platform-test-annotations",
+        "junit",
+    ],
+}
+
+filegroup {
+    name: "error_prone_android_framework_testdata",
+    path: "tests/res",
+    srcs: ["tests/res/**/*.java"],
+}
diff --git a/errorprone/TEST_MAPPING b/errorprone/TEST_MAPPING
new file mode 100644
index 0000000..ee4552f
--- /dev/null
+++ b/errorprone/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+    "presubmit": [
+        {
+            "name": "error_prone_android_framework_test"
+        }
+    ]
+}
diff --git a/errorprone/tests/java/com/google/errorprone/bugpatterns/android/RethrowFromSystemCheckerTest.java b/errorprone/tests/java/com/google/errorprone/bugpatterns/android/RethrowFromSystemCheckerTest.java
new file mode 100644
index 0000000..32efbf2
--- /dev/null
+++ b/errorprone/tests/java/com/google/errorprone/bugpatterns/android/RethrowFromSystemCheckerTest.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2020 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.google.errorprone.bugpatterns.android;
+
+import com.google.errorprone.CompilationTestHelper;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class RethrowFromSystemCheckerTest {
+    private CompilationTestHelper compilationHelper;
+
+    @Before
+    public void setUp() {
+        compilationHelper = CompilationTestHelper.newInstance(
+                RethrowFromSystemChecker.class, getClass());
+    }
+
+    @Test
+    public void testValid() {
+        compilationHelper
+                .addSourceFile("/android/annotation/SystemService.java")
+                .addSourceFile("/android/foo/IFooService.java")
+                .addSourceFile("/android/os/IInterface.java")
+                .addSourceFile("/android/os/RemoteException.java")
+                .addSourceLines("FooManager.java",
+                        "import android.annotation.SystemService;",
+                        "import android.foo.IFooService;",
+                        "import android.os.RemoteException;",
+                        "@SystemService(\"foo\") public class FooManager {",
+                        "  IFooService mService;",
+                        "  void bar() {",
+                        "    try {",
+                        "      mService.bar();",
+                        "    } catch (RemoteException e) {",
+                        "      throw e.rethrowFromSystemServer();",
+                        "    }",
+                        "  }",
+                        "}")
+                .doTest();
+    }
+
+    @Test
+    public void testInvalid() {
+        compilationHelper
+                .addSourceFile("/android/annotation/SystemService.java")
+                .addSourceFile("/android/foo/IFooService.java")
+                .addSourceFile("/android/os/IInterface.java")
+                .addSourceFile("/android/os/RemoteException.java")
+                .addSourceLines("FooManager.java",
+                        "import android.annotation.SystemService;",
+                        "import android.foo.IFooService;",
+                        "import android.os.RemoteException;",
+                        "@SystemService(\"foo\") public class FooManager {",
+                        "  IFooService mService;",
+                        "  void bar() {",
+                        "    try {",
+                        "      mService.bar();",
+                        "    // BUG: Diagnostic contains:",
+                        "    } catch (RemoteException e) {",
+                        "      e.printStackTrace();",
+                        "    }",
+                        "  }",
+                        "}")
+                .doTest();
+    }
+
+    @Test
+    public void testIgnored() {
+        compilationHelper
+                .addSourceFile("/android/annotation/SystemService.java")
+                .addSourceFile("/android/foo/IFooService.java")
+                .addSourceFile("/android/os/IInterface.java")
+                .addSourceFile("/android/os/RemoteException.java")
+                .addSourceLines("FooManager.java",
+                        "import android.annotation.SystemService;",
+                        "import android.foo.IFooService;",
+                        "import android.os.RemoteException;",
+                        "@SystemService(\"foo\") public class FooManager {",
+                        "  IFooService mService;",
+                        "  void bar() {",
+                        "    try {",
+                        "      mService.bar();",
+                        "    // BUG: Diagnostic contains:",
+                        "    } catch (RemoteException ignored) {",
+                        "    }",
+                        "  }",
+                        "}")
+                .doTest();
+    }
+}
diff --git a/errorprone/tests/java/com/google/errorprone/bugpatterns/android/TargetSdkCheckerTest.java b/errorprone/tests/java/com/google/errorprone/bugpatterns/android/TargetSdkCheckerTest.java
new file mode 100644
index 0000000..99a21c9
--- /dev/null
+++ b/errorprone/tests/java/com/google/errorprone/bugpatterns/android/TargetSdkCheckerTest.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2020 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.google.errorprone.bugpatterns.android;
+
+import com.google.errorprone.CompilationTestHelper;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class TargetSdkCheckerTest {
+    private CompilationTestHelper compilationHelper;
+
+    @Before
+    public void setUp() {
+        compilationHelper = CompilationTestHelper.newInstance(
+                TargetSdkChecker.class, getClass());
+    }
+
+    @Test
+    public void testValid() {
+        compilationHelper
+                .addSourceFile("/android/os/Build.java")
+                .addSourceLines("Example.java",
+                        "import android.os.Build;",
+                        "public class Example {",
+                        "  void test(int targetSdkVersion) {",
+                        "    boolean res = targetSdkVersion >= Build.VERSION_CODES.DONUT;",
+                        "    if (targetSdkVersion < Build.VERSION_CODES.DONUT) { }",
+                        "  }",
+                        "}")
+                .doTest();
+    }
+
+    @Test
+    public void testInvalid() {
+        compilationHelper
+                .addSourceFile("/android/os/Build.java")
+                .addSourceLines("Example.java",
+                        "import android.os.Build;",
+                        "public class Example {",
+                        "  void test(int targetSdkVersion) {",
+                        "    // BUG: Diagnostic contains:",
+                        "    boolean res = targetSdkVersion > Build.VERSION_CODES.DONUT;",
+                        "    // BUG: Diagnostic contains:",
+                        "    if (targetSdkVersion <= Build.VERSION_CODES.DONUT) { }",
+                        "  }",
+                        "}")
+                .doTest();
+    }
+}
diff --git a/errorprone/tests/res/android/annotation/SystemService.java b/errorprone/tests/res/android/annotation/SystemService.java
new file mode 100644
index 0000000..b84edcb
--- /dev/null
+++ b/errorprone/tests/res/android/annotation/SystemService.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright (C) 2020 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;
+
+public @interface SystemService {
+    String value();
+}
diff --git a/errorprone/tests/res/android/content/Context.java b/errorprone/tests/res/android/content/Context.java
new file mode 100644
index 0000000..7ba3fbb
--- /dev/null
+++ b/errorprone/tests/res/android/content/Context.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2020 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.content;
+
+public class Context {
+    public int getUserId() {
+        return 0;
+    }
+}
diff --git a/errorprone/tests/res/android/foo/IFooService.java b/errorprone/tests/res/android/foo/IFooService.java
new file mode 100644
index 0000000..1ae7253
--- /dev/null
+++ b/errorprone/tests/res/android/foo/IFooService.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2020 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.foo;
+
+import android.os.RemoteException;
+
+public interface IFooService extends android.os.IInterface {
+    public void bar() throws RemoteException;
+    public void baz(String baz, int userId) throws RemoteException;
+}
diff --git a/errorprone/tests/res/android/os/Build.java b/errorprone/tests/res/android/os/Build.java
new file mode 100644
index 0000000..bbf7ef2
--- /dev/null
+++ b/errorprone/tests/res/android/os/Build.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2020 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.os;
+
+public class Build {
+    public static class VERSION_CODES {
+        public static final int DONUT = 4;
+    }
+}
diff --git a/errorprone/tests/res/android/os/IInterface.java b/errorprone/tests/res/android/os/IInterface.java
new file mode 100644
index 0000000..1a8a37c
--- /dev/null
+++ b/errorprone/tests/res/android/os/IInterface.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2020 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.os;
+
+public interface IInterface {
+}
diff --git a/errorprone/tests/res/android/os/RemoteException.java b/errorprone/tests/res/android/os/RemoteException.java
new file mode 100644
index 0000000..afe1988
--- /dev/null
+++ b/errorprone/tests/res/android/os/RemoteException.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2020 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.os;
+
+public class RemoteException extends Exception {
+    public RuntimeException rethrowFromSystemServer() {
+        throw new RuntimeException(this);
+    }
+}