Home | History | Annotate | Download | only in util
      1 package com.xtremelabs.robolectric.util;
      2 
      3 import org.junit.Before;
      4 import org.junit.Test;
      5 
      6 import static org.hamcrest.CoreMatchers.equalTo;
      7 import static org.junit.Assert.assertThat;
      8 
      9 public class SchedulerTest {
     10     private Transcript transcript;
     11     private Scheduler scheduler;
     12 
     13     @Before
     14     public void setUp() throws Exception {
     15         scheduler = new Scheduler();
     16         scheduler.pause();
     17         transcript = new Transcript();
     18     }
     19 
     20     @Test
     21     public void testTick_ReturnsTrueIffSomeJobWasRun() throws Exception {
     22         scheduler.postDelayed(new AddToTranscript("one"), 0);
     23         scheduler.postDelayed(new AddToTranscript("two"), 0);
     24         scheduler.postDelayed(new AddToTranscript("three"), 1000);
     25 
     26         assertThat(scheduler.advanceBy(0), equalTo(true));
     27         transcript.assertEventsSoFar("one", "two");
     28 
     29         assertThat(scheduler.advanceBy(0), equalTo(false));
     30         transcript.assertNoEventsSoFar();
     31 
     32         assertThat(scheduler.advanceBy(1000), equalTo(true));
     33         transcript.assertEventsSoFar("three");
     34     }
     35 
     36     @Test
     37     public void testShadowPostDelayed() throws Exception {
     38         scheduler.postDelayed(new AddToTranscript("one"), 1000);
     39         scheduler.postDelayed(new AddToTranscript("two"), 2000);
     40         scheduler.postDelayed(new AddToTranscript("three"), 3000);
     41 
     42         scheduler.advanceBy(1000);
     43         transcript.assertEventsSoFar("one");
     44 
     45         scheduler.advanceBy(500);
     46         transcript.assertNoEventsSoFar();
     47 
     48         scheduler.advanceBy(501);
     49         transcript.assertEventsSoFar("two");
     50 
     51         scheduler.advanceBy(999);
     52         transcript.assertEventsSoFar("three");
     53     }
     54 
     55     @Test
     56     public void testShadowPostAtFrontOfQueue() throws Exception {
     57         scheduler.post(new AddToTranscript("one"));
     58         scheduler.post(new AddToTranscript("two"));
     59         scheduler.postAtFrontOfQueue(new AddToTranscript("three"));
     60 
     61         scheduler.runOneTask();
     62         transcript.assertEventsSoFar("three");
     63 
     64         scheduler.runOneTask();
     65         transcript.assertEventsSoFar("one");
     66 
     67         scheduler.runOneTask();
     68         transcript.assertEventsSoFar("two");
     69     }
     70 
     71     @Test
     72     public void testShadowPostAtFrontOfQueue_whenUnpaused() throws Exception {
     73         scheduler.unPause();
     74         scheduler.postAtFrontOfQueue(new AddToTranscript("three"));
     75         transcript.assertEventsSoFar("three");
     76     }
     77 
     78     @Test
     79     public void testShadowPostDelayed_WhenMoreItemsAreAdded() throws Exception {
     80         scheduler.postDelayed(new Runnable() {
     81             @Override
     82             public void run() {
     83                 transcript.add("one");
     84                 scheduler.postDelayed(new Runnable() {
     85                     @Override
     86                     public void run() {
     87                         transcript.add("two");
     88                         scheduler.postDelayed(new AddToTranscript("three"), 1000);
     89                     }
     90                 }, 1000);
     91             }
     92         }, 1000);
     93 
     94         scheduler.advanceBy(1000);
     95         transcript.assertEventsSoFar("one");
     96 
     97         scheduler.advanceBy(500);
     98         transcript.assertNoEventsSoFar();
     99 
    100         scheduler.advanceBy(501);
    101         transcript.assertEventsSoFar("two");
    102 
    103         scheduler.advanceBy(999);
    104         transcript.assertEventsSoFar("three");
    105     }
    106 
    107     @Test
    108     public void removeShouldRemoveAllInstancesOfRunnableFromQueue() throws Exception {
    109         scheduler.post(new TestRunnable());
    110         TestRunnable runnable = new TestRunnable();
    111         scheduler.post(runnable);
    112         scheduler.post(runnable);
    113         assertThat(scheduler.enqueuedTaskCount(), equalTo(3));
    114         scheduler.remove(runnable);
    115         assertThat(scheduler.enqueuedTaskCount(), equalTo(1));
    116         scheduler.advanceToLastPostedRunnable();
    117         assertThat(runnable.wasRun, equalTo(false));
    118     }
    119 
    120     @Test
    121     public void resetShouldUnPause() throws Exception {
    122         scheduler.pause();
    123 
    124         TestRunnable runnable = new TestRunnable();
    125         scheduler.post(runnable);
    126 
    127         assertThat(runnable.wasRun, equalTo(false));
    128 
    129         scheduler.reset();
    130         scheduler.post(runnable);
    131         assertThat(runnable.wasRun, equalTo(true));
    132     }
    133 
    134     @Test
    135     public void resetShouldClearPendingRunnables() throws Exception {
    136         scheduler.pause();
    137 
    138         TestRunnable runnable1 = new TestRunnable();
    139         scheduler.post(runnable1);
    140 
    141         assertThat(runnable1.wasRun, equalTo(false));
    142 
    143         scheduler.reset();
    144 
    145         TestRunnable runnable2 = new TestRunnable();
    146         scheduler.post(runnable2);
    147 
    148         assertThat(runnable1.wasRun, equalTo(false));
    149         assertThat(runnable2.wasRun, equalTo(true));
    150     }
    151 
    152     private class AddToTranscript implements Runnable {
    153         private String event;
    154 
    155         public AddToTranscript(String event) {
    156             this.event = event;
    157         }
    158 
    159         @Override
    160         public void run() {
    161             transcript.add(event);
    162         }
    163     }
    164 }
    165