/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

  1. package com.xtremelabs.robolectric.util;
  2. import com.xtremelabs.robolectric.Robolectric;
  3. import com.xtremelabs.robolectric.WithTestDefaultsRunner;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import java.util.concurrent.Callable;
  8. import java.util.concurrent.Future;
  9. import static org.junit.Assert.assertEquals;
  10. import static org.junit.Assert.assertFalse;
  11. import static org.junit.Assert.assertTrue;
  12. @RunWith(WithTestDefaultsRunner.class)
  13. public class RobolectricBackgroundExecutorServiceTest {
  14. private Transcript transcript;
  15. private RobolectricBackgroundExecutorService executorService;
  16. private Runnable runnable;
  17. @Before public void setUp() throws Exception {
  18. transcript = new Transcript();
  19. executorService = new RobolectricBackgroundExecutorService();
  20. Robolectric.getBackgroundScheduler().pause();
  21. runnable = new Runnable() {
  22. @Override public void run() {
  23. transcript.add("background event ran");
  24. }
  25. };
  26. }
  27. @Test
  28. public void execute_shouldRunStuffOnBackgroundThread() throws Exception {
  29. executorService.execute(runnable);
  30. transcript.assertNoEventsSoFar();
  31. Robolectric.runBackgroundTasks();
  32. transcript.assertEventsSoFar("background event ran");
  33. }
  34. @Test
  35. public void submitRunnable_shouldRunStuffOnBackgroundThread() throws Exception {
  36. Future<String> future = executorService.submit(runnable, "foo");
  37. transcript.assertNoEventsSoFar();
  38. assertFalse(future.isDone());
  39. Robolectric.runBackgroundTasks();
  40. transcript.assertEventsSoFar("background event ran");
  41. assertTrue(future.isDone());
  42. assertEquals("foo", future.get());
  43. }
  44. @Test
  45. public void submitCallable_shouldRunStuffOnBackgroundThread() throws Exception {
  46. Future<String> future = executorService.submit(new Callable<String>() {
  47. @Override public String call() throws Exception {
  48. runnable.run();
  49. return "foo";
  50. }
  51. });
  52. transcript.assertNoEventsSoFar();
  53. assertFalse(future.isDone());
  54. Robolectric.runBackgroundTasks();
  55. transcript.assertEventsSoFar("background event ran");
  56. assertTrue(future.isDone());
  57. assertEquals("foo", future.get());
  58. }
  59. }