diff options
author | Nick Korostelev <nkorsote@google.com> | 2014-09-12 09:10:04 -0700 |
---|---|---|
committer | Jeff Brown <jeffbrown@google.com> | 2014-09-15 21:34:23 +0000 |
commit | 7a552ffc0bce492a7b87755490f3df7490dc357c (patch) | |
tree | 8e7a145f1237c756faa1a13b1ec09e4bbb3deda6 | |
parent | 41d41dce8a720eacc151181dc8e9ef7f35f5d625 (diff) | |
download | testing-7a552ffc0bce492a7b87755490f3df7490dc357c.tar.gz |
Removed bogus Looper.prepare() from onStart(). (DO NOT MERGE)
The instrumentation thread for JUnit tests is not a Looper. It calculates all tests that need to be executed, executes them, and exits, ending the instrumentation.
Bug: 17418699
Change-Id: Idcfdd3369472e01996bfae5564b6cb70c3eb7724
-rw-r--r-- | support/src/android/support/test/runner/AndroidJUnitRunner.java | 11 | ||||
-rw-r--r-- | support/tests/src/android/support/test/runner/AndroidJUnitRunnerTest.java | 56 |
2 files changed, 52 insertions, 15 deletions
diff --git a/support/src/android/support/test/runner/AndroidJUnitRunner.java b/support/src/android/support/test/runner/AndroidJUnitRunner.java index f07bf0c..07b9ea0 100644 --- a/support/src/android/support/test/runner/AndroidJUnitRunner.java +++ b/support/src/android/support/test/runner/AndroidJUnitRunner.java @@ -223,21 +223,10 @@ public class AndroidJUnitRunner extends MonitoringInstrumentation { return tagString != null && Boolean.parseBoolean(tagString); } - /** - * Initialize the current thread as a looper. - * <p/> - * Exposed for unit testing. - */ - void prepareLooper() { - Looper.prepare(); - } - @Override public void onStart() { super.onStart(); - prepareLooper(); - if (getBooleanArgument(ARGUMENT_DEBUG)) { Debug.waitForDebugger(); } diff --git a/support/tests/src/android/support/test/runner/AndroidJUnitRunnerTest.java b/support/tests/src/android/support/test/runner/AndroidJUnitRunnerTest.java index 245612a..2fcdca4 100644 --- a/support/tests/src/android/support/test/runner/AndroidJUnitRunnerTest.java +++ b/support/tests/src/android/support/test/runner/AndroidJUnitRunnerTest.java @@ -17,8 +17,13 @@ package android.support.test.runner; import android.content.Context; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.os.Message; import android.support.test.internal.runner.TestRequestBuilder; +import junit.framework.Assert; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -28,20 +33,20 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; -import java.io.Reader; -import java.io.StringReader; /** * Unit tests for {@link AndroidJUnitRunner}. */ public class AndroidJUnitRunnerTest { + public static final int SLEEP_TIME = 300; + + private final Thread mInstantiationThread = Thread.currentThread(); private AndroidJUnitRunner mAndroidJUnitRunner; private PrintStream mStubStream; @@ -54,7 +59,6 @@ public class AndroidJUnitRunnerTest { public void setUp() throws Exception { mAndroidJUnitRunner = new AndroidJUnitRunner() { - @Override TestRequestBuilder createTestRequestBuilder(PrintStream writer, String... packageCodePaths) { @@ -148,4 +152,48 @@ public class AndroidJUnitRunnerTest { Mockito.verify(mMockBuilder).addTestClass("ClassName3"); Mockito.verify(mMockBuilder).addTestMethod("ClassName4", "method2"); } + + /** + * Ensures that the main looper is not blocked and can process + * messages during test execution. + */ + @Test + public void testMainLooperIsAlive() throws InterruptedException { + final boolean[] called = new boolean[1]; + Handler handler = new Handler(Looper.getMainLooper()) { + @Override + public void handleMessage(Message msg) { + called[0] = true; + } + }; + handler.sendEmptyMessage(0); + Thread.sleep(SLEEP_TIME); + Assert.assertTrue(called[0]); + } + + /** + * Ensures that the thread the test runs on has not been + * prepared as a looper. It doesn't make sense for it + * to be a looper because it will be blocked for the entire + * duration of test execution. Tests should instead post + * messages to the main looper or a new handler thread + * of their own as appropriate while running. + */ + @Test + public void testTestThreadIsNotALooper() { + Assert.assertNull(Looper.myLooper()); + } + + /** + * Ensures that tests run on the same thread they were + * instantiated on. This is needed to ensure that + * objects created by the test don't accidentally bind + * to thread-local state belonging to other threads. + * In particular, this ensures that the test cannot + * create Handlers that are bound to the wrong thread. + */ + @Test + public void testTestRunsOnSameThreadAsInstantiation() { + Assert.assertEquals(Thread.currentThread(), mInstantiationThread); + } } |