TwitterSubscriber implements FlowableOnSubscribe<Result<TwitterSession>> { @Override public void subscribe(FlowableEmitter<Result<TwitterSession>> emitter) throws Exception { mCallback = new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> result) { if (!emitter.isCancelled()) { emitter.onNext(result); emitter.onComplete(); } } @Override public void failure(TwitterException e) { if (!emitter.isCancelled()) { emitter.onError(new LoginException(LoginException.TWITTER_ERROR, e)); } } }; mClient.authorize(mActivity, mCallback); mRxLogin.registerCallback(mCallback); } TwitterSubscriber(RxLogin rxLogin, Activity activity, TwitterAuthClient client); @Override void subscribe(FlowableEmitter<Result<TwitterSession>> emitter);  }
@Test public void testCallOnSuccess() throws Exception { Result<TwitterSession> result = mock(Result.class); mTwitterSubscriber.subscribe(mEmitter); verify(mRxLogin).registerCallback(eq(mTwitterSubscriber.mCallback)); mTwitterSubscriber.mCallback.success(result); verify(mEmitter).onNext(eq(result)); verify(mEmitter).onComplete(); }
@Test public void testCallOnError() throws Exception { mTwitterSubscriber.subscribe(mEmitter); verify(mRxLogin).registerCallback(eq(mTwitterSubscriber.mCallback)); mTwitterSubscriber.mCallback.failure(mock(TwitterException.class)); verify(mEmitter).onError(any(LoginException.class)); }
@Test public void testCancelWhenSuccess() throws Exception { mTwitterSubscriber.subscribe(mEmitter); when(mEmitter.isCancelled()).thenReturn(true); mTwitterSubscriber.mCallback.success(mock(Result.class)); verify(mEmitter, never()).onError(any(Throwable.class)); verify(mEmitter, never()).onNext(any(Result.class)); verify(mEmitter, never()).onComplete(); }
@Test public void testCancelWhenError() throws Exception { mTwitterSubscriber.subscribe(mEmitter); when(mEmitter.isCancelled()).thenReturn(true); mTwitterSubscriber.mCallback.failure(mock(TwitterException.class)); verify(mEmitter, never()).onError(any(Throwable.class)); verify(mEmitter, never()).onNext(any(Result.class)); verify(mEmitter, never()).onComplete(); }