Loading tests/testables/Android.bp +4 −1 Original line number Diff line number Diff line Loading @@ -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", Loading tests/testables/src/android/testing/TestWithLooperRule.java +4 −1 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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]", Loading tests/testables/tests/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -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", Loading tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java 0 → 100644 +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"); } } Loading
tests/testables/Android.bp +4 −1 Original line number Diff line number Diff line Loading @@ -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", Loading
tests/testables/src/android/testing/TestWithLooperRule.java +4 −1 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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]", Loading
tests/testables/tests/Android.bp +1 −0 Original line number Diff line number Diff line Loading @@ -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", Loading
tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java 0 → 100644 +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"); } }