diff --git a/tests/testables/src/android/testing/TestableInstrumentation.java b/tests/testables/src/android/testing/TestableInstrumentation.java index 3207b486b3294..c35dc68e071fe 100644 --- a/tests/testables/src/android/testing/TestableInstrumentation.java +++ b/tests/testables/src/android/testing/TestableInstrumentation.java @@ -38,22 +38,26 @@ public class TestableInstrumentation extends AndroidJUnitRunner { @Override public void onCreate(Bundle arguments) { - sManager = new MainLooperManager(); - Log.setWtfHandler((tag, what, system) -> { - if (system) { - Log.e(TAG, "WTF!!", what); - } else { - // These normally kill the app, but we don't want that in a test, instead we want - // it to throw. - throw new RuntimeException(what); - } - }); + if (TestableLooper.HOLD_MAIN_THREAD) { + sManager = new MainLooperManager(); + Log.setWtfHandler((tag, what, system) -> { + if (system) { + Log.e(TAG, "WTF!!", what); + } else { + // These normally kill the app, but we don't want that in a test, instead we want + // it to throw. + throw new RuntimeException(what); + } + }); + } super.onCreate(arguments); } @Override public void finish(int resultCode, Bundle results) { - sManager.destroy(); + if (TestableLooper.HOLD_MAIN_THREAD) { + sManager.destroy(); + } super.finish(resultCode, results); } diff --git a/tests/testables/src/android/testing/TestableLooper.java b/tests/testables/src/android/testing/TestableLooper.java index f8d223ab91b60..8b4cba12b0e63 100644 --- a/tests/testables/src/android/testing/TestableLooper.java +++ b/tests/testables/src/android/testing/TestableLooper.java @@ -39,6 +39,12 @@ import java.util.Map; */ public class TestableLooper { + /** + * Whether to hold onto the main thread through all tests in an attempt to + * catch crashes. + */ + public static final boolean HOLD_MAIN_THREAD = false; + private Looper mLooper; private MessageQueue mQueue; private MessageHandler mMessageHandler; @@ -77,7 +83,7 @@ public class TestableLooper { */ public void destroy() { mQueueWrapper.release(); - if (mLooper == Looper.getMainLooper()) { + if (HOLD_MAIN_THREAD && mLooper == Looper.getMainLooper()) { TestableInstrumentation.releaseMain(); } } @@ -199,7 +205,7 @@ public class TestableLooper { } private static TestLooperManager acquireLooperManager(Looper l) { - if (l == Looper.getMainLooper()) { + if (HOLD_MAIN_THREAD && l == Looper.getMainLooper()) { TestableInstrumentation.acquireMain(); } return InstrumentationRegistry.getInstrumentation().acquireLooperManager(l); @@ -291,7 +297,7 @@ public class TestableLooper { if (set) { mTestableLooper.mQueueWrapper.release(); mTestableLooper.mQueueWrapper = null; - if (mLooper == Looper.getMainLooper()) { + if (HOLD_MAIN_THREAD && mLooper == Looper.getMainLooper()) { TestableInstrumentation.releaseMain(); } }