FacebookSubscriber implements FlowableOnSubscribe<LoginResult> { @Override public void subscribe(FlowableEmitter<LoginResult> emitter) throws Exception { mCallback = new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult result) { if (!emitter.isCancelled()) { emitter.onNext(result); emitter.onComplete(); } } @Override public void onCancel() { if (!emitter.isCancelled()) { emitter.onError(new LoginException(LoginException.LOGIN_CANCELED)); } } @Override public void onError(FacebookException error) { if (!emitter.isCancelled()) { emitter.onError(new LoginException(LoginException.FACEBOOK_ERROR, error)); } } }; mRxLogin.registerCallback(mCallback); } FacebookSubscriber(RxLogin rxLogin); @Override void subscribe(FlowableEmitter<LoginResult> emitter);  }
@Test public void testCallOnSuccess() throws Exception { LoginResult result = mock(LoginResult.class); mFacebookSubscriber.subscribe(mEmitter); verify(mRxLogin).registerCallback(eq(mFacebookSubscriber.mCallback)); mFacebookSubscriber.mCallback.onSuccess(result); verify(mEmitter).onNext(eq(result)); verify(mEmitter).onComplete(); }
@Test public void testCallOnError() throws Exception { mFacebookSubscriber.subscribe(mEmitter); verify(mRxLogin).registerCallback(eq(mFacebookSubscriber.mCallback)); mFacebookSubscriber.mCallback.onError(new FacebookException()); verify(mEmitter).onError(any(LoginException.class)); }
@Test public void testCallWithOnCancel() throws Exception { mFacebookSubscriber.subscribe(mEmitter); verify(mRxLogin).registerCallback(eq(mFacebookSubscriber.mCallback)); mFacebookSubscriber.mCallback.onCancel(); verify(mEmitter).onError(any(LoginException.class)); }
@Test public void testCancelWhenSuccess() throws Exception { mFacebookSubscriber.subscribe(mEmitter); when(mEmitter.isCancelled()).thenReturn(true); mFacebookSubscriber.mCallback.onSuccess(mock(LoginResult.class)); verify(mEmitter, never()).onError(any(Throwable.class)); verify(mEmitter, never()).onNext(any(LoginResult.class)); verify(mEmitter, never()).onComplete(); }
@Test public void testCancelWhenError() throws Exception { mFacebookSubscriber.subscribe(mEmitter); when(mEmitter.isCancelled()).thenReturn(true); mFacebookSubscriber.mCallback.onError(new FacebookException()); verify(mEmitter, never()).onError(any(Throwable.class)); verify(mEmitter, never()).onNext(any(LoginResult.class)); verify(mEmitter, never()).onComplete(); }
@Test public void testCancelWhenCancel() throws Exception { mFacebookSubscriber.subscribe(mEmitter); when(mEmitter.isCancelled()).thenReturn(true); mFacebookSubscriber.mCallback.onCancel(); mFacebookSubscriber.mCallback.onError(new FacebookException()); verify(mEmitter, never()).onError(any(Throwable.class)); verify(mEmitter, never()).onNext(any(LoginResult.class)); verify(mEmitter, never()).onComplete(); }