PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/test/java/com/xtremelabs/robolectric/util/RobolectricBackgroundExecutorServiceTest.java

http://github.com/pivotal/robolectric
Java | 77 lines | 59 code | 18 blank | 0 comment | 0 complexity | d767a4277c395e1958577ace7c144371 MD5 | raw file
Possible License(s): Apache-2.0, MIT
 1package com.xtremelabs.robolectric.util;
 2
 3import com.xtremelabs.robolectric.Robolectric;
 4import com.xtremelabs.robolectric.WithTestDefaultsRunner;
 5import org.junit.Before;
 6import org.junit.Test;
 7import org.junit.runner.RunWith;
 8
 9import java.util.concurrent.Callable;
10import java.util.concurrent.Future;
11
12import static org.junit.Assert.assertEquals;
13import static org.junit.Assert.assertFalse;
14import static org.junit.Assert.assertTrue;
15
16@RunWith(WithTestDefaultsRunner.class)
17public class RobolectricBackgroundExecutorServiceTest {
18    private Transcript transcript;
19    private RobolectricBackgroundExecutorService executorService;
20    private Runnable runnable;
21
22    @Before public void setUp() throws Exception {
23        transcript = new Transcript();
24        executorService = new RobolectricBackgroundExecutorService();
25
26        Robolectric.getBackgroundScheduler().pause();
27
28        runnable = new Runnable() {
29            @Override public void run() {
30                transcript.add("background event ran");
31            }
32        };
33    }
34
35    @Test
36    public void execute_shouldRunStuffOnBackgroundThread() throws Exception {
37        executorService.execute(runnable);
38
39        transcript.assertNoEventsSoFar();
40
41        Robolectric.runBackgroundTasks();
42        transcript.assertEventsSoFar("background event ran");
43    }
44
45    @Test
46    public void submitRunnable_shouldRunStuffOnBackgroundThread() throws Exception {
47        Future<String> future = executorService.submit(runnable, "foo");
48
49        transcript.assertNoEventsSoFar();
50        assertFalse(future.isDone());
51
52        Robolectric.runBackgroundTasks();
53        transcript.assertEventsSoFar("background event ran");
54        assertTrue(future.isDone());
55
56        assertEquals("foo", future.get());
57    }
58
59    @Test
60    public void submitCallable_shouldRunStuffOnBackgroundThread() throws Exception {
61        Future<String> future = executorService.submit(new Callable<String>() {
62            @Override public String call() throws Exception {
63                runnable.run();
64                return "foo";
65            }
66        });
67
68        transcript.assertNoEventsSoFar();
69        assertFalse(future.isDone());
70
71        Robolectric.runBackgroundTasks();
72        transcript.assertEventsSoFar("background event ran");
73        assertTrue(future.isDone());
74
75        assertEquals("foo", future.get());
76    }
77}