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
diff --git a/tests/testables/Android.bp b/tests/testables/Android.bp
index 7596ee7..f211185 100644
--- a/tests/testables/Android.bp
+++ b/tests/testables/Android.bp
@@ -25,7 +25,10 @@
 
 java_library {
     name: "testables",
-    srcs: ["src/**/*.java"],
+    srcs: [
+        "src/**/*.java",
+        "src/**/*.kt",
+    ],
     libs: [
         "android.test.runner.stubs.system",
         "android.test.mock.stubs.system",
diff --git a/tests/testables/src/android/testing/TestWithLooperRule.java b/tests/testables/src/android/testing/TestWithLooperRule.java
index 37b39c3..10df17f 100644
--- a/tests/testables/src/android/testing/TestWithLooperRule.java
+++ b/tests/testables/src/android/testing/TestWithLooperRule.java
@@ -34,13 +34,13 @@
  * 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 @@
                     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]",
diff --git a/tests/testables/tests/Android.bp b/tests/testables/tests/Android.bp
index 1eb36fa..c23f41a 100644
--- a/tests/testables/tests/Android.bp
+++ b/tests/testables/tests/Android.bp
@@ -34,6 +34,7 @@
         "androidx.core_core-animation",
         "androidx.core_core-ktx",
         "androidx.test.rules",
+        "androidx.test.ext.junit",
         "hamcrest-library",
         "mockito-target-inline-minus-junit4",
         "testables",
diff --git a/tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java b/tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java
new file mode 100644
index 0000000..b7d5e0e
--- /dev/null
+++ b/tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java
@@ -0,0 +1,42 @@
+/*
+ * 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");
+    }
+}
+