RequestCredentialsTask extends GoogleApiClientConnectTask { @Override void onConnected(final SmartLock smartLock, final Activity activity) { Log.v(TAG, "Requesting credentials from SmartLock"); getCredentialsApi().request(smartLock.getCredentialClient(), smartLock.newCredentialRequest()).setResultCallback(new ResultCallback<CredentialRequestResult>() { @Override public void onResult(CredentialRequestResult credentialRequestResult) { final com.google.android.gms.common.api.Status status = credentialRequestResult.getStatus(); if (status.isSuccess()) { final Credential credential = credentialRequestResult.getCredential(); Log.v(TAG, "Retrieved credentials for type " + credential.getAccountType()); smartLock.onCredentialsRetrieved(activity, credential); } else { smartLock.onCredentialRetrievalError(activity, status, SmartLock.SMART_LOCK_READ); } } }); } RequestCredentialsTask(Activity activity);   }
@Test public void shouldRequestCredentials() throws Exception { task.onConnected(smartLock, activity); verify(credentialsApi).request(eq(googleApiClient), eq(credentialRequest)); }
@Test public void shouldSetResultCallback() throws Exception { task.onConnected(smartLock, activity); verify(pendingResult).setResultCallback(Matchers.<ResultCallback<CredentialRequestResult>>any()); }
@Test public void shouldHandleRequestSuccess() throws Exception { when(status.isSuccess()).thenReturn(true); when(credentialRequestResult.getCredential()).thenReturn(credential); task.onConnected(smartLock, activity); verify(pendingResult).setResultCallback(captor.capture()); final ResultCallback<CredentialRequestResult> resultCallback = captor.getValue(); resultCallback.onResult(credentialRequestResult); verify(smartLock).onCredentialsRetrieved(eq(activity), eq(credential)); }
@Test public void shouldHandleRequestFailure() throws Exception { when(status.isSuccess()).thenReturn(false); task.onConnected(smartLock, activity); verify(pendingResult).setResultCallback(captor.capture()); final ResultCallback<CredentialRequestResult> resultCallback = captor.getValue(); resultCallback.onResult(credentialRequestResult); verify(smartLock).onCredentialRetrievalError(eq(activity), eq(status), eq(SmartLock.SMART_LOCK_READ)); }