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

Commit 2d37d6e3 authored by Matthew Reynolds's avatar Matthew Reynolds
Browse files

Fixed TestWithLooperRule switch for UiThreadStatement and ExpectException

When switching from AndroidTestRunner to AndroidJUnit4, the annotation @Test(expected = <Exception> wrap the test method, which is delegated out to AndroidTestRunner if it's there, but a switch statement in wrapMethodInStatement is called in the AndroidJUnit4 case, which caused an exception for the uhandled case.

Bug: 352170965
Test: manual test with atest
Flag: TEST_ONLY
Change-Id: Ib45be25234519a22031b5774e9c83fcad59c1c47
parent 2fe03850
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -25,7 +25,10 @@ package {

java_library {
    name: "testables",
    srcs: ["src/**/*.java"],
    srcs: [
        "src/**/*.java",
        "src/**/*.kt",
    ],
    libs: [
        "android.test.runner.stubs.system",
        "android.test.mock.stubs.system",
+4 −1
Original line number Diff line number Diff line
@@ -34,13 +34,13 @@ import java.util.List;
 * Looper for the Statement.
 */
public class TestWithLooperRule implements MethodRule {

    /*
     * This rule requires to be the inner most Rule, so the next statement is RunAfters
     * instead of another rule. You can set it by '@Rule(order = Integer.MAX_VALUE)'
     */
    @Override
    public Statement apply(Statement base, FrameworkMethod method, Object target) {

        // getting testRunner check, if AndroidTestingRunning then we skip this rule
        RunWith runWithAnnotation = target.getClass().getAnnotation(RunWith.class);
        if (runWithAnnotation != null) {
@@ -97,6 +97,9 @@ public class TestWithLooperRule implements MethodRule {
                    case "InvokeParameterizedMethod":
                        this.wrapFieldMethodFor(next, "frameworkMethod", method, target);
                        return;
                    case "ExpectException":
                        next = this.getNextStatement(next, "next");
                        break;
                    default:
                        throw new Exception(
                                String.format("Unexpected Statement received: [%s]",
+1 −0
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ android_test {
        "androidx.core_core-animation",
        "androidx.core_core-ktx",
        "androidx.test.rules",
        "androidx.test.ext.junit",
        "hamcrest-library",
        "mockito-target-inline-minus-junit4",
        "testables",
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.testing;

import android.testing.TestableLooper.RunWithLooper;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Test that TestableLooper now handles expected exceptions in tests
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWithLooper
public class TestableLooperJUnit4Test {
    @Rule
    public final TestWithLooperRule mTestWithLooperRule = new TestWithLooperRule();

    @Test(expected = Exception.class)
    public void testException() throws Exception {
        throw new Exception("this exception is expected");
    }
}