BaseSingle extends BaseRx<T> implements SingleOnSubscribe<T> { protected final void handleResolutionResult(int resultCode, ConnectionResult connectionResult) { for (Map.Entry<GoogleApiClient, SingleEmitter<T>> entry : subscriptionInfoMap.entrySet()) { if (!entry.getValue().isDisposed()) { if (resultCode == Activity.RESULT_OK) { try { entry.getKey().connect(); } catch (Throwable ex) { entry.getValue().onError(ex); } } else { entry.getValue().onError(new GoogleAPIConnectionException("Error connecting to GoogleApiClient, resolution was not successful.", connectionResult)); } } } } protected  BaseSingle(@NonNull RxFit rxFit, Long timeout, TimeUnit timeUnit); protected  BaseSingle(@NonNull Context ctx, @NonNull Api<? extends Api.ApiOptions.NotRequiredOptions>[] services, Scope[] scopes); @Override final void subscribe(SingleEmitter<T> subscriber);  }
@Test public void handleResolutionResult_ResultOK() { final Object object = new Object(); BaseSingle<Object> single = spy(new BaseSingle<Object>(rxFit, null, null) { @Override protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleEmitter<? super Object> subscriber) { subscriber.onSuccess(object); } }); SingleEmitter sub = PowerMockito.spy(new SingleEmitter() { @Override public void onSuccess(Object value) { } @Override public void onError(Throwable error) { } @Override public void setDisposable(Disposable s) {} @Override public void setCancellable(Cancellable c) { } @Override public boolean isDisposed() { return false;} }); PowerMockito.doReturn(false).when(sub).isDisposed(); single.subscriptionInfoMap.put(apiClient, sub); single.handleResolutionResult(Activity.RESULT_OK, connectionResult); verify(apiClient).connect(); verifyNoMoreInteractions(apiClient); verify(sub, never()).onError(Matchers.any(Throwable.class)); }
@Test public void handleResolutionResult_ResultOK_ConnectionException() { final Object object = new Object(); BaseSingle<Object> single = spy(new BaseSingle<Object>(rxFit, null, null) { @Override protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleEmitter<? super Object> subscriber) { subscriber.onSuccess(object); } }); SingleEmitter sub = PowerMockito.spy(new SingleEmitter() { @Override public void onSuccess(Object value) { } @Override public void onError(Throwable error) { } @Override public void setDisposable(Disposable s) {} @Override public void setCancellable(Cancellable c) { } @Override public boolean isDisposed() { return false;} }); RuntimeException exception = new RuntimeException(); doThrow(exception).when(apiClient).connect(); PowerMockito.doReturn(false).when(sub).isDisposed(); single.subscriptionInfoMap.put(apiClient, sub); single.handleResolutionResult(Activity.RESULT_OK, connectionResult); verify(apiClient).connect(); verifyNoMoreInteractions(apiClient); verify(sub).onError(exception); }
@Test public void handleResolutionResult_ResultCanceled() { final Object object = new Object(); BaseSingle<Object> single = spy(new BaseSingle<Object>(rxFit, null, null) { @Override protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleEmitter<? super Object> subscriber) { subscriber.onSuccess(object); } }); SingleEmitter sub = PowerMockito.spy(new SingleEmitter() { @Override public void onSuccess(Object value) { } @Override public void onError(Throwable error) { } @Override public void setDisposable(Disposable s) {} @Override public void setCancellable(Cancellable c) { } @Override public boolean isDisposed() { return false;} }); PowerMockito.doReturn(false).when(sub).isDisposed(); single.subscriptionInfoMap.put(apiClient, sub); single.handleResolutionResult(Activity.RESULT_CANCELED, connectionResult); verifyZeroInteractions(apiClient); verify(sub).onError(Matchers.any(GoogleAPIConnectionException.class)); }