@Test public void clearShouldRemoveTasks() { final AtomicInteger counter = new AtomicInteger(0); goro.schedule("q1", new Callable<Object>() { @Override public Object call() throws Exception { return counter.incrementAndGet(); } }); goro.getExecutor("q1").execute(new Runnable() { @Override public void run() { counter.incrementAndGet(); } }); goro.clear("q1"); testingQueues.executeAll(); assertThat(counter.get()).describedAs("some task has been executed").isZero(); assertThat(testingQueues.getLastQueueName()).isEqualTo("q1"); }
@Test public void shouldInvokeErrorOnListeners() { goro.addTaskListener(listener); final Exception error = new Exception(); Callable<?> task = new Callable<Object>() { @Override public Object call() throws Exception { throw error; } }; goro.schedule(task); testingQueues.executeAll(); InOrder order = inOrder(listener); order.verify(listener).onTaskSchedule(task, Goro.DEFAULT_QUEUE); order.verify(listener).onTaskStart(task); order.verify(listener).onTaskError(task, error); }
@Test(expected = GoroException.class) public void removeListenerShouldThrowOnUnknownListener() { goro.removeTaskListener(mock(GoroListener.class)); }
@SuppressWarnings("unchecked") @Test public void observersShouldBeCalledOnTheirExecutors() { FutureObserver observer = mock(FutureObserver.class); final Runnable[] commands = new Runnable[1]; //noinspection NullableProblems Executor executor = new Executor() { @Override public void execute(final Runnable command) { commands[0] = command; } }; goro.schedule(mock(Callable.class)).subscribe(executor, observer); verify(observer, never()).onSuccess(any()); testingQueues.executeAll(); assertThat(commands[0]).isNotNull(); commands[0].run(); verify(observer).onSuccess(any()); }
@Before public void init() { context = Robolectric.setupActivity(Activity.class); shadowContext = Shadows.shadowOf(context); testingQueues = new TestingQueues(); serviceInstance = spy(new Goro.GoroImpl(testingQueues)); serviceCompName = new ComponentName(context, GoroService.class); GoroService service = new GoroService(); binder = new GoroService.GoroBinderImpl(serviceInstance, service.new GoroTasksListener()); ShadowApplication.getInstance() .setComponentNameAndServiceForBindService( serviceCompName, binder ); reset(serviceInstance); }
@Before public void init() { queues = new TestingQueues(); GoroService.setup(application, new Goro.GoroImpl(queues)); service = new GoroService(); service.onCreate(); task = new Task(new Callable<String>() { @Override public String call() throws Exception { executed = true; return "ok"; } }); executed = false; }
public static GoroImpl createGoroWith(TestingQueues queues) { return new GoroImpl(queues); }
@Override public <T> ObservableFuture<T> schedule(final Callable<T> task) { return schedule(DEFAULT_QUEUE, task); }
@Override public <T> ObservableFuture<T> schedule(final Callable<T> task) { return schedule(DEFAULT_QUEUE, task); }
@Test(expected = IllegalArgumentException.class) public void scheduleShouldThrowWhenTaskIsNull() { goro.schedule("1", null); }
@SuppressWarnings("unchecked") @Test public void errorsShouldBePassedToObservers() throws Exception { FutureObserver observer = mock(FutureObserver.class); Callable<?> task = mock(Callable.class); Exception e = new Exception(); doThrow(e).when(task).call(); goro.schedule(task).subscribe(observer); testingQueues.executeAll(); verify(observer).onError(e); }
@Test public void shouldPassNullQueueNameWhenAsked() { goro.schedule(null, mock(Callable.class)); assertThat(testingQueues.getLastQueueName()).isNull(); }
/** * Creates a new Goro instance which uses {@link android.os.AsyncTask#THREAD_POOL_EXECUTOR} * to delegate tasks on Post-Honeycomb devices or create a separate thread pool on earlier * Android versions. * @return instance of Goro */ public static Goro create() { return new GoroImpl(); }
@Test public void shouldInvokeCancelOnListeners() { goro.addTaskListener(listener); Callable<?> task = mock(Callable.class); goro.schedule(task).cancel(true); testingQueues.executeAll(); verify(listener, never()).onTaskStart(task); verify(listener).onTaskCancel(task); }
@Test public void shouldInvokeStartOnListeners() { goro.addTaskListener(listener); Callable<?> task = mock(Callable.class); goro.schedule(task); testingQueues.executeAll(); verify(listener).onTaskStart(task); }
/** * Creates a new Goro instance which uses the specified executor to delegate tasks. * @param delegateExecutor executor Goro delegates tasks to * @return instance of Goro */ public static Goro createWithDelegate(final Executor delegateExecutor) { GoroImpl goro = new GoroImpl(); goro.queues.setDelegateExecutor(delegateExecutor); return goro; }
@Test public void getExecutorShouldReturnSerialExecutor() { Executor executor = goro.getExecutor(null); assertThat(testingQueues.getLastQueueName()).isEqualTo(Goro.DEFAULT_QUEUE); Runnable task = mock(Runnable.class); executor.execute(task); verify(task, never()).run(); testingQueues.executeAll(); verify(task).run(); }
@Test public void shouldInvokeFinishOnListeners() throws Exception { goro.addTaskListener(listener); Callable<?> task = mock(Callable.class); Object result = new Object(); doReturn(result).when(task).call(); goro.schedule(task); testingQueues.executeAll(); InOrder order = inOrder(listener); order.verify(listener).onTaskStart(task); order.verify(listener).onTaskFinish(task, result); }