Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 86ba608e authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

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
parent 8cf806f5
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -21,3 +21,25 @@ java_library_host {
        "//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"],
}
+7 −0
Original line number Diff line number Diff line
{
    "presubmit": [
        {
            "name": "error_prone_android_framework_test"
        }
    ]
}
+108 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}
+67 −0
Original line number Diff line number Diff line
/*
 * 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();
    }
}
+21 −0
Original line number Diff line number Diff line
/*
 * 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();
}
Loading