PromiseImpl extends Promise<R> implements ActionHandle { @NonNull @Override public ActionHandle then(@Nullable ResultListener<R> listener) { final R result = mResult; final ResourceCleaner<R> cleaner = mCleaner; mResult = null; if (listener != null) { listener.onResult(result); } else if (cleaner != null && result != null) { cleaner.clean(result); } mResult = null; mCleaner = null; return this; } PromiseImpl(@Nullable R result, @Nullable ResourceCleaner<R> cleaner); @Override void thenNothing(); @NonNull @Override ActionHandle then(@Nullable ResultListener<R> listener); @NonNull @Override ActionHandle then(@NonNull ActorThread thread, @Nullable ResultListener<R> listener); @Nullable @Override R get(); @Override void forget();  }
@Test public void then_callListenerOnCurrentThread_withListener() throws Exception { PromiseImpl<Object> promise = new PromiseImpl<>(mResult, mCleaner); promise.then(mListener); Mockito.verify(mListener).onResult(mResult); }
@Test public void then_cleanResultOnSameThread_withoutThreadAndListener() throws Exception { PromiseImpl<Object> promise = new PromiseImpl<>(mResult, mCleaner); promise.then(null); Mockito.verify(mCleaner).clean(mResult); }
@Test public void then_doNothing_withoutListenerAndResult() throws Exception { PromiseImpl<Object> promise = new PromiseImpl<>(null, mCleaner); promise.then(null); Mockito.verifyZeroInteractions(mCleaner); }
@Test public void then_doNothing_withoutListenerResultAndCleaner() throws Exception { PromiseImpl<Object> promise = new PromiseImpl<>(null, null); promise.then(null); }
@Test public void then_cleanResultOnSameThread_withoutListener() throws Exception { PromiseImpl<Object> promise = new PromiseImpl<>(mResult, mCleaner); promise.then(mActorThread, null); Mockito.verifyZeroInteractions(mActorThread); Mockito.verify(mCleaner).clean(mResult); }
@Test public void then_callListenerOnActorThread_withListener() throws Exception { PromiseImpl<Object> promise = new PromiseImpl<>(mResult, mCleaner); promise.then(mActorThread, mListener); Mockito.verify(mActorThread).bind(Mockito.same(ResultListener.class), mCaptor.capture()); Mockito.verify(mProxy).onResult(mResult); mCaptor.getValue().onResult(mResult); Mockito.verify(mListener).onResult(mResult); }
@Test public void then_cleanResultOnActorThread_withoutListener() throws Exception { Mockito.doThrow(new ResultListenerIsNotSpecifiedException()).when(mListener).onResult(Mockito.any()); PromiseImpl<Object> promise = new PromiseImpl<>(mResult, mCleaner); promise.then(mActorThread, mListener); Mockito.verify(mActorThread).bind(Mockito.same(ResultListener.class), mCaptor.capture()); Mockito.verify(mProxy).onResult(mResult); mCaptor.getValue().onResult(mResult); Mockito.verify(mCleaner).clean(mResult); }
@Test public void then_doNothingOnActorThread_withoutListenerAndResult() throws Exception { Mockito.doThrow(new ResultListenerIsNotSpecifiedException()).when(mListener).onResult(Mockito.any()); PromiseImpl<Object> promise = new PromiseImpl<>(null, mCleaner); promise.then(mActorThread, mListener); Mockito.verify(mActorThread).bind(Mockito.same(ResultListener.class), mCaptor.capture()); Mockito.verify(mProxy).onResult(null); mCaptor.getValue().onResult(null); Mockito.verifyZeroInteractions(mCleaner); }
@Test public void ResultListener_nonNull_exception() { PromiseImpl<Object> promise = new PromiseImpl<>(mResult, mCleaner); promise.then(mActorThread, mListener); Mockito.verify(mActorThread).bind(Mockito.same(ResultListener.class), mCaptor.capture()); ResultListener listener = mCaptor.getValue(); Assert.assertTrue(listener instanceof ExceptionTemplateProvider); Assert.assertNotNull(((ExceptionTemplateProvider) listener).exception()); }