Search in sources :

Example 6 with TestRunnable

use of org.robolectric.util.TestRunnable in project robolectric by robolectric.

the class ShadowActivityTest method shouldQueueUiTasksWhenUiThreadIsPaused.

@Test
@LooperMode(LEGACY)
public void shouldQueueUiTasksWhenUiThreadIsPaused() throws Exception {
    shadowOf(getMainLooper()).pause();
    activity = Robolectric.setupActivity(DialogLifeCycleActivity.class);
    TestRunnable runnable = new TestRunnable();
    activity.runOnUiThread(runnable);
    assertFalse(runnable.wasRun);
    shadowOf(getMainLooper()).idle();
    assertTrue(runnable.wasRun);
}
Also used : TestRunnable(org.robolectric.util.TestRunnable) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Example 7 with TestRunnable

use of org.robolectric.util.TestRunnable in project robolectric by robolectric.

the class ShadowViewTest method shouldRemovePostedCallbacksFromMessageQueue.

@Test
public void shouldRemovePostedCallbacksFromMessageQueue() throws Exception {
    TestRunnable runnable = new TestRunnable();
    assertThat(view.postDelayed(runnable, 1)).isTrue();
    assertThat(view.removeCallbacks(runnable)).isTrue();
    shadowMainLooper().idleFor(Duration.ofMillis(1));
    assertThat(runnable.wasRun).isFalse();
}
Also used : TestRunnable(org.robolectric.util.TestRunnable) Test(org.junit.Test)

Example 8 with TestRunnable

use of org.robolectric.util.TestRunnable in project robolectric by robolectric.

the class ShadowViewTest method shouldPostActionsToTheMessageQueueWithDelay.

@Test
public void shouldPostActionsToTheMessageQueueWithDelay() throws Exception {
    shadowMainLooper().pause();
    TestRunnable runnable = new TestRunnable();
    view.postDelayed(runnable, 1);
    assertFalse(runnable.wasRun);
    shadowMainLooper().idleFor(Duration.ofMillis(1));
    assertTrue(runnable.wasRun);
}
Also used : TestRunnable(org.robolectric.util.TestRunnable) Test(org.junit.Test)

Example 9 with TestRunnable

use of org.robolectric.util.TestRunnable in project robolectric by robolectric.

the class ShadowHandlerTest method testPostDelayedTwiceThenRunMainLooperToNextTask_shouldRunMultipleTasks.

@Test
public void testPostDelayedTwiceThenRunMainLooperToNextTask_shouldRunMultipleTasks() {
    TestRunnable task1 = new TestRunnable();
    TestRunnable task2 = new TestRunnable();
    new Handler().postDelayed(task1, 1);
    new Handler().postDelayed(task2, 1);
    ShadowLooper.runMainLooperToNextTask();
    assertThat(task1.wasRun).isTrue();
    assertThat(task2.wasRun).isTrue();
}
Also used : TestRunnable(org.robolectric.util.TestRunnable) Handler(android.os.Handler) Test(org.junit.Test)

Example 10 with TestRunnable

use of org.robolectric.util.TestRunnable in project robolectric by robolectric.

the class ShadowActivityTest method shouldExecutePostedUiTasksInRealisticLooper.

/**
 * The legacy behavior spec-ed in {@link #shouldQueueUiTasksWhenUiThreadIsPaused()} is actually
 * incorrect. The {@link Activity#runOnUiThread} will execute posted tasks inline.
 */
@Test
@LooperMode(Mode.PAUSED)
public void shouldExecutePostedUiTasksInRealisticLooper() throws Exception {
    activity = Robolectric.setupActivity(DialogLifeCycleActivity.class);
    TestRunnable runnable = new TestRunnable();
    activity.runOnUiThread(runnable);
    assertTrue(runnable.wasRun);
}
Also used : TestRunnable(org.robolectric.util.TestRunnable) Test(org.junit.Test) LooperMode(org.robolectric.annotation.LooperMode)

Aggregations

Test (org.junit.Test)10 TestRunnable (org.robolectric.util.TestRunnable)10 Handler (android.os.Handler)4 LooperMode (org.robolectric.annotation.LooperMode)2