diff options
author | Nick Korostelev <nkorsote@google.com> | 2014-09-16 08:50:53 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-09-16 08:50:53 +0000 |
commit | 6e21547c5409e17afa90f5ca175f911c766df063 (patch) | |
tree | ba9ffcd248e65dabfec05e42d6b781b9ebeb7ce6 | |
parent | 6fca9dd8b322ba15475d221cc4172a2a84eb93e3 (diff) | |
parent | 7a552ffc0bce492a7b87755490f3df7490dc357c (diff) | |
download | testing-6e21547c5409e17afa90f5ca175f911c766df063.tar.gz |
am 7a552ffc: Removed bogus Looper.prepare() from onStart(). (DO NOT MERGE)
* commit '7a552ffc0bce492a7b87755490f3df7490dc357c':
Removed bogus Looper.prepare() from onStart(). (DO NOT MERGE)
-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); + } } |