Loading tests/testables/src/android/testing/TestableInstrumentation.java +1 −1 Original line number Original line Diff line number Diff line Loading @@ -77,7 +77,7 @@ public class TestableInstrumentation extends AndroidJUnitRunner { private TestLooperManager mManager; private TestLooperManager mManager; public MainLooperManager() { public MainLooperManager() { mMainHandler = new Handler(Looper.getMainLooper()); mMainHandler = Handler.createAsync(Looper.getMainLooper()); startManaging(); startManaging(); } } Loading tests/testables/src/android/testing/TestableLooper.java +7 −7 Original line number Original line Diff line number Diff line Loading @@ -51,12 +51,12 @@ public class TestableLooper { this(acquireLooperManager(l), l); this(acquireLooperManager(l), l); } } private TestableLooper(TestLooperManager wrapper, Looper l) throws Exception { private TestableLooper(TestLooperManager wrapper, Looper l) { mQueueWrapper = wrapper; mQueueWrapper = wrapper; setupQueue(l); setupQueue(l); } } private TestableLooper(Looper looper, boolean b) throws Exception { private TestableLooper(Looper looper, boolean b) { setupQueue(looper); setupQueue(looper); } } Loading @@ -64,7 +64,7 @@ public class TestableLooper { return mLooper; return mLooper; } } private void setupQueue(Looper l) throws Exception { private void setupQueue(Looper l) { mLooper = l; mLooper = l; mQueue = mLooper.getQueue(); mQueue = mLooper.getQueue(); mHandler = new Handler(mLooper); mHandler = new Handler(mLooper); Loading @@ -75,7 +75,7 @@ public class TestableLooper { * the looper will not be available for any subsequent tests. This is * the looper will not be available for any subsequent tests. This is * automatically handled for tests using {@link RunWithLooper}. * automatically handled for tests using {@link RunWithLooper}. */ */ public void destroy() throws NoSuchFieldException, IllegalAccessException { public void destroy() { mQueueWrapper.release(); mQueueWrapper.release(); if (mLooper == Looper.getMainLooper()) { if (mLooper == Looper.getMainLooper()) { TestableInstrumentation.releaseMain(); TestableInstrumentation.releaseMain(); Loading Loading @@ -133,7 +133,7 @@ public class TestableLooper { if (mMessageHandler != null) { if (mMessageHandler != null) { if (mMessageHandler.onMessageHandled(result)) { if (mMessageHandler.onMessageHandled(result)) { result.getTarget().dispatchMessage(result); mQueueWrapper.execute(result); mQueueWrapper.recycle(result); mQueueWrapper.recycle(result); } else { } else { mQueueWrapper.recycle(result); mQueueWrapper.recycle(result); Loading @@ -141,7 +141,7 @@ public class TestableLooper { return false; return false; } } } else { } else { result.getTarget().dispatchMessage(result); mQueueWrapper.execute(result); mQueueWrapper.recycle(result); mQueueWrapper.recycle(result); } } } else { } else { Loading Loading @@ -238,7 +238,7 @@ public class TestableLooper { super(base.getMethod()); super(base.getMethod()); mLooper = other.mLooper; mLooper = other.mLooper; mTestableLooper = other; mTestableLooper = other; mHandler = new Handler(mLooper); mHandler = Handler.createAsync(mLooper); } } public static FrameworkMethod get(FrameworkMethod base, boolean setAsMain, Object test) { public static FrameworkMethod get(FrameworkMethod base, boolean setAsMain, Object test) { Loading tests/testables/tests/src/android/testing/TestableLooperTest.java +19 −0 Original line number Original line Diff line number Diff line Loading @@ -16,6 +16,7 @@ package android.testing; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock; Loading Loading @@ -158,4 +159,22 @@ public class TestableLooperTest { public void testMainLooperAnnotation() { public void testMainLooperAnnotation() { assertEquals(Looper.myLooper(), Looper.getMainLooper()); assertEquals(Looper.myLooper(), Looper.getMainLooper()); } } @Test public void testCorrectLooperExecution() throws Exception { boolean[] hasRun = new boolean[] { false }; Runnable r = () -> { assertEquals("Should run on main looper", Looper.getMainLooper(), Looper.myLooper()); hasRun[0] = true; }; TestableLooper testableLooper = new TestableLooper(Looper.getMainLooper()); try { new Handler(Looper.getMainLooper()).post(r); testableLooper.processAllMessages(); assertTrue(hasRun[0]); } finally { testableLooper.destroy(); } } } } Loading
tests/testables/src/android/testing/TestableInstrumentation.java +1 −1 Original line number Original line Diff line number Diff line Loading @@ -77,7 +77,7 @@ public class TestableInstrumentation extends AndroidJUnitRunner { private TestLooperManager mManager; private TestLooperManager mManager; public MainLooperManager() { public MainLooperManager() { mMainHandler = new Handler(Looper.getMainLooper()); mMainHandler = Handler.createAsync(Looper.getMainLooper()); startManaging(); startManaging(); } } Loading
tests/testables/src/android/testing/TestableLooper.java +7 −7 Original line number Original line Diff line number Diff line Loading @@ -51,12 +51,12 @@ public class TestableLooper { this(acquireLooperManager(l), l); this(acquireLooperManager(l), l); } } private TestableLooper(TestLooperManager wrapper, Looper l) throws Exception { private TestableLooper(TestLooperManager wrapper, Looper l) { mQueueWrapper = wrapper; mQueueWrapper = wrapper; setupQueue(l); setupQueue(l); } } private TestableLooper(Looper looper, boolean b) throws Exception { private TestableLooper(Looper looper, boolean b) { setupQueue(looper); setupQueue(looper); } } Loading @@ -64,7 +64,7 @@ public class TestableLooper { return mLooper; return mLooper; } } private void setupQueue(Looper l) throws Exception { private void setupQueue(Looper l) { mLooper = l; mLooper = l; mQueue = mLooper.getQueue(); mQueue = mLooper.getQueue(); mHandler = new Handler(mLooper); mHandler = new Handler(mLooper); Loading @@ -75,7 +75,7 @@ public class TestableLooper { * the looper will not be available for any subsequent tests. This is * the looper will not be available for any subsequent tests. This is * automatically handled for tests using {@link RunWithLooper}. * automatically handled for tests using {@link RunWithLooper}. */ */ public void destroy() throws NoSuchFieldException, IllegalAccessException { public void destroy() { mQueueWrapper.release(); mQueueWrapper.release(); if (mLooper == Looper.getMainLooper()) { if (mLooper == Looper.getMainLooper()) { TestableInstrumentation.releaseMain(); TestableInstrumentation.releaseMain(); Loading Loading @@ -133,7 +133,7 @@ public class TestableLooper { if (mMessageHandler != null) { if (mMessageHandler != null) { if (mMessageHandler.onMessageHandled(result)) { if (mMessageHandler.onMessageHandled(result)) { result.getTarget().dispatchMessage(result); mQueueWrapper.execute(result); mQueueWrapper.recycle(result); mQueueWrapper.recycle(result); } else { } else { mQueueWrapper.recycle(result); mQueueWrapper.recycle(result); Loading @@ -141,7 +141,7 @@ public class TestableLooper { return false; return false; } } } else { } else { result.getTarget().dispatchMessage(result); mQueueWrapper.execute(result); mQueueWrapper.recycle(result); mQueueWrapper.recycle(result); } } } else { } else { Loading Loading @@ -238,7 +238,7 @@ public class TestableLooper { super(base.getMethod()); super(base.getMethod()); mLooper = other.mLooper; mLooper = other.mLooper; mTestableLooper = other; mTestableLooper = other; mHandler = new Handler(mLooper); mHandler = Handler.createAsync(mLooper); } } public static FrameworkMethod get(FrameworkMethod base, boolean setAsMain, Object test) { public static FrameworkMethod get(FrameworkMethod base, boolean setAsMain, Object test) { Loading
tests/testables/tests/src/android/testing/TestableLooperTest.java +19 −0 Original line number Original line Diff line number Diff line Loading @@ -16,6 +16,7 @@ package android.testing; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock; Loading Loading @@ -158,4 +159,22 @@ public class TestableLooperTest { public void testMainLooperAnnotation() { public void testMainLooperAnnotation() { assertEquals(Looper.myLooper(), Looper.getMainLooper()); assertEquals(Looper.myLooper(), Looper.getMainLooper()); } } @Test public void testCorrectLooperExecution() throws Exception { boolean[] hasRun = new boolean[] { false }; Runnable r = () -> { assertEquals("Should run on main looper", Looper.getMainLooper(), Looper.myLooper()); hasRun[0] = true; }; TestableLooper testableLooper = new TestableLooper(Looper.getMainLooper()); try { new Handler(Looper.getMainLooper()).post(r); testableLooper.processAllMessages(); assertTrue(hasRun[0]); } finally { testableLooper.destroy(); } } } }