GoogleSubscriber implements FlowableOnSubscribe<GoogleSignInResult> { @Override public void subscribe(FlowableEmitter<GoogleSignInResult> emitter) throws Exception { mCallback = new GoogleCallback() { @Override public void onSuccess(GoogleSignInResult 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(GoogleSignInResult result) { if (!emitter.isCancelled()) { Status status = result.getStatus(); emitter.onError(new LoginException(LoginException.GOOGLE_ERROR, status.getStatusCode(), status.getStatusMessage())); } } }; mRxLogin.registerCallback(mCallback); } GoogleSubscriber(RxLogin rxLogin); @Override void subscribe(FlowableEmitter<GoogleSignInResult> emitter);  }
@Test public void testCallOnSuccess() throws Exception { GoogleSignInResult result = mock(GoogleSignInResult.class); mGoogleSubscriber = new GoogleSubscriber(mRxLogin); mGoogleSubscriber.subscribe(mEmitter); verify(mRxLogin).registerCallback(eq(mGoogleSubscriber.mCallback)); mGoogleSubscriber.mCallback.onSuccess(result); verify(mEmitter).onNext(eq(result)); verify(mEmitter).onComplete(); }
@Test public void testCallOnError() throws Exception { GoogleSignInResult result = mock(GoogleSignInResult.class); when(result.getStatus()).thenReturn(new Status(1300, "gg")); mGoogleSubscriber = new GoogleSubscriber(mRxLogin); mGoogleSubscriber.subscribe(mEmitter); verify(mRxLogin).registerCallback(eq(mGoogleSubscriber.mCallback)); mGoogleSubscriber.mCallback.onError(result); verify(mEmitter).onError(any(LoginException.class)); }
@Test public void testCallWithOnCancel() throws Exception { mGoogleSubscriber.subscribe(mEmitter); verify(mRxLogin).registerCallback(eq(mGoogleSubscriber.mCallback)); mGoogleSubscriber.mCallback.onCancel(); verify(mEmitter).onError(any(LoginException.class)); }
@Test public void testCancelWhenSuccess() throws Exception { mGoogleSubscriber.subscribe(mEmitter); when(mEmitter.isCancelled()).thenReturn(true); mGoogleSubscriber.mCallback.onSuccess(mock(GoogleSignInResult.class)); verify(mEmitter, never()).onError(any(Throwable.class)); verify(mEmitter, never()).onNext(any(GoogleSignInResult.class)); verify(mEmitter, never()).onComplete(); }
@Test public void testCancelWhenError() throws Exception { mGoogleSubscriber.subscribe(mEmitter); when(mEmitter.isCancelled()).thenReturn(true); mGoogleSubscriber.mCallback.onError(mock(GoogleSignInResult.class)); verify(mEmitter, never()).onError(any(Throwable.class)); verify(mEmitter, never()).onNext(any(GoogleSignInResult.class)); verify(mEmitter, never()).onComplete(); }
@Test public void testCancelWhenCancel() throws Exception { mGoogleSubscriber.subscribe(mEmitter); when(mEmitter.isCancelled()).thenReturn(true); mGoogleSubscriber.mCallback.onCancel(); verify(mEmitter, never()).onError(any(Throwable.class)); verify(mEmitter, never()).onNext(any(GoogleSignInResult.class)); verify(mEmitter, never()).onComplete(); }