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

Language text Lines 0
MD5 Hash d767a4277c395e1958577ace7c144371
Repository git://github.com/pivotal/robolectric.git View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.xtremelabs.robolectric.util;

import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@RunWith(WithTestDefaultsRunner.class)
public class RobolectricBackgroundExecutorServiceTest {
    private Transcript transcript;
    private RobolectricBackgroundExecutorService executorService;
    private Runnable runnable;

    @Before public void setUp() throws Exception {
        transcript = new Transcript();
        executorService = new RobolectricBackgroundExecutorService();

        Robolectric.getBackgroundScheduler().pause();

        runnable = new Runnable() {
            @Override public void run() {
                transcript.add("background event ran");
            }
        };
    }

    @Test
    public void execute_shouldRunStuffOnBackgroundThread() throws Exception {
        executorService.execute(runnable);

        transcript.assertNoEventsSoFar();

        Robolectric.runBackgroundTasks();
        transcript.assertEventsSoFar("background event ran");
    }

    @Test
    public void submitRunnable_shouldRunStuffOnBackgroundThread() throws Exception {
        Future<String> future = executorService.submit(runnable, "foo");

        transcript.assertNoEventsSoFar();
        assertFalse(future.isDone());

        Robolectric.runBackgroundTasks();
        transcript.assertEventsSoFar("background event ran");
        assertTrue(future.isDone());

        assertEquals("foo", future.get());
    }

    @Test
    public void submitCallable_shouldRunStuffOnBackgroundThread() throws Exception {
        Future<String> future = executorService.submit(new Callable<String>() {
            @Override public String call() throws Exception {
                runnable.run();
                return "foo";
            }
        });

        transcript.assertNoEventsSoFar();
        assertFalse(future.isDone());

        Robolectric.runBackgroundTasks();
        transcript.assertEventsSoFar("background event ran");
        assertTrue(future.isDone());

        assertEquals("foo", future.get());
    }
}
Back to Top