From d432656e60dd6b3e9a1acb14001bc2d2b886789d Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Thu, 10 Dec 2009 11:16:43 -0800 Subject: [PATCH] Add support for @UiThreadTest on setUp() and tearDown() in InstrumentationTestCase. --- .../android/test/InstrumentationTestCase.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/core/java/android/test/InstrumentationTestCase.java b/core/java/android/test/InstrumentationTestCase.java index 22d95d16b2e7b..6d92ea38da785 100644 --- a/core/java/android/test/InstrumentationTestCase.java +++ b/core/java/android/test/InstrumentationTestCase.java @@ -147,6 +147,42 @@ public class InstrumentationTestCase extends TestCase { } } + @Override + public void runBare() throws Throwable { + runMethod("setUp"); + + try { + runTest(); + } finally { + runMethod("tearDown"); + } + } + + private Throwable[] runMethod(String name) throws Throwable { + final Throwable[] exceptions = new Throwable[1]; + final Method m = getClass().getMethod(name, (Class[]) null); + + if (m.isAnnotationPresent(UiThreadTest.class)) { + getInstrumentation().runOnMainSync(new Runnable() { + public void run() { + try { + m.invoke(this); + } catch (Throwable throwable) { + exceptions[0] = throwable; + } + } + }); + if (exceptions[0] != null) { + throw exceptions[0]; + } + exceptions[0] = null; + } else { + m.invoke(this); + } + + return exceptions; + } + /** * Runs the current unit test. If the unit test is annotated with * {@link android.test.UiThreadTest}, the test is run on the UI thread.