@SuppressWarnings("deprecation") @Override public String getAuthToken() throws AuthFailureError { AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount, mAuthTokenType, mNotifyAuthFailure, null, null); Bundle result; try { result = future.getResult(); } catch (Exception e) { throw new AuthFailureError("Error while retrieving auth token", e); } String authToken = null; if (future.isDone() && !future.isCancelled()) { if (result.containsKey(AccountManager.KEY_INTENT)) { Intent intent = result.getParcelable(AccountManager.KEY_INTENT); throw new AuthFailureError(intent); } authToken = result.getString(AccountManager.KEY_AUTHTOKEN); } if (authToken == null) { throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType); } return authToken; }
@Test(expected = AuthFailureError.class) public void resultContainsIntent() throws Exception { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putParcelable(AccountManager.KEY_INTENT, intent); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); mAuthenticator.getAuthToken(); }
@SuppressWarnings("deprecation") @Override public String getAuthToken() throws AuthFailureError { AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount, mAuthTokenType, mNotifyAuthFailure, null, null); Bundle result; try { result = future.getResult(); } catch (Exception e) { throw new AuthFailureError("Error while retrieving auth token", e); } String authToken = null; if (future.isDone() && !future.isCancelled()) { if (result.containsKey(AccountManager.KEY_INTENT)) { Intent intent = result.getParcelable(AccountManager.KEY_INTENT); throw new AuthFailureError(intent); } authToken = result.getString(AccountManager.KEY_AUTHTOKEN); } if (authToken == null) { throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType); } return authToken; }
@Test(expected = AuthFailureError.class) public void missingAuthToken() throws Exception { Bundle bundle = new Bundle(); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); mAuthenticator.getAuthToken(); }
@Test(expected = AuthFailureError.class) public void resultContainsIntent() throws Exception { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putParcelable(AccountManager.KEY_INTENT, intent); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); mAuthenticator.getAuthToken(); }
@Test public void goodToken() throws Exception { Bundle bundle = new Bundle(); bundle.putString(AccountManager.KEY_AUTHTOKEN, "monkey"); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); Assert.assertEquals("monkey", mAuthenticator.getAuthToken()); }
@SuppressWarnings("deprecation") @Override public String getAuthToken() throws AuthFailureError { AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount, mAuthTokenType, mNotifyAuthFailure, null, null); Bundle result; try { result = future.getResult(); } catch (Exception e) { throw new AuthFailureError("Error while retrieving auth token", e); } String authToken = null; if (future.isDone() && !future.isCancelled()) { if (result.containsKey(AccountManager.KEY_INTENT)) { Intent intent = result.getParcelable(AccountManager.KEY_INTENT); throw new AuthFailureError(intent); } authToken = result.getString(AccountManager.KEY_AUTHTOKEN); } if (authToken == null) { throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType); } return authToken; }
@Test(expected = AuthFailureError.class) public void missingAuthToken() throws Exception { Bundle bundle = new Bundle(); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); mAuthenticator.getAuthToken(); }
@Test public void goodToken() throws Exception { Bundle bundle = new Bundle(); bundle.putString(AccountManager.KEY_AUTHTOKEN, "monkey"); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); Assert.assertEquals("monkey", mAuthenticator.getAuthToken()); }
@Test public void addAccount_shouldCallCallback() throws Exception { shadowOf(am).addAuthenticator("google.com"); TestAccountManagerCallback<Bundle> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Bundle> result = am.addAccount("google.com", "auth_token_type", null, null, activity, callback, new Handler()); assertThat(callback.hasBeenCalled()).isFalse(); assertThat(result.isDone()).isFalse(); shadowOf(am).addAccount(new Account("thebomb@google.com", "google.com")); assertThat(result.isDone()).isTrue(); assertThat(callback.accountManagerFuture).isNotNull(); Bundle resultBundle = callback.getResult(); assertThat(resultBundle.getString(AccountManager.KEY_ACCOUNT_TYPE)).isEqualTo("google.com"); assertThat(resultBundle.getString(AccountManager.KEY_ACCOUNT_NAME)).isEqualTo("thebomb@google.com"); }
@Test public void addAccount_noAuthenticatorDefined() throws Exception { AccountManagerFuture<Bundle> future = am.addAccount("unknown_account_type", "auth_token_type", null, null, activity, null, null); try { future.getResult(); fail("addAccount() should throw an authenticator exception if no authenticator was registered for this account type"); } catch(AuthenticatorException e) { // Expected } assertThat(future.isDone()).isTrue(); }
@Test public void getHasFeatures_returnsTrueWhenAllFeaturesSatisfied() throws Exception { Account account = new Account("name", "google.com"); shadowOf(am).addAccount(account); shadowOf(am).setFeatures(account, new String[] { "FEATURE_1", "FEATURE_2" }); TestAccountManagerCallback<Boolean> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Boolean> future = am.hasFeatures(account, new String[] { "FEATURE_1", "FEATURE_2" }, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult().booleanValue()).isEqualTo(true); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test public void getAuthToken_withActivity_returnsCorrectToken() throws Exception { Account account = new Account("name", "google.com"); shadowOf(am).addAccount(account); shadowOf(am).addAuthenticator("google.com"); am.setAuthToken(account, "auth_token_type", "token1"); TestAccountManagerCallback<Bundle> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Bundle> future = am.getAuthToken(account, "auth_token_type", new Bundle(), activity, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_NAME)).isEqualTo(account.name); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_TYPE)).isEqualTo(account.type); assertThat(future.getResult().getString(AccountManager.KEY_AUTHTOKEN)).isEqualTo("token1"); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test public void getHasFeatures_returnsFalseWhenAllFeaturesNotSatisfied() throws Exception { Account account = new Account("name", "google.com"); shadowOf(am).addAccount(account); shadowOf(am).setFeatures(account, new String[] { "FEATURE_1" }); TestAccountManagerCallback<Boolean> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Boolean> future = am.hasFeatures(account, new String[] { "FEATURE_1", "FEATURE_2" }, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult().booleanValue()).isEqualTo(false); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test public void getAuthToken_withActivity_returnsAuthIntent() throws Exception { Account account = new Account("name", "google.com"); shadowOf(am).addAccount(account); shadowOf(am).addAuthenticator("google.com"); TestAccountManagerCallback<Bundle> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Bundle> future = am.getAuthToken(account, "auth_token_type", new Bundle(), activity, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_NAME)) .isEqualTo(account.name); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_TYPE)) .isEqualTo(account.type); assertThat(future.getResult().getString(AccountManager.KEY_AUTHTOKEN)).isNull(); assertThat((Intent) future.getResult().getParcelable(AccountManager.KEY_INTENT)).isNotNull(); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test public void getAuthToken_withNotifyAuthFailureSetToFalse_returnsAuthIntent() throws Exception { Account account = new Account("name", "google.com"); shadowOf(am).addAccount(account); shadowOf(am).addAuthenticator("google.com"); TestAccountManagerCallback<Bundle> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Bundle> future = am.getAuthToken( account, "auth_token_type", new Bundle(), /* notifyAuthFailure= */ false, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_NAME)) .isEqualTo(account.name); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_TYPE)) .isEqualTo(account.type); assertThat(future.getResult().getString(AccountManager.KEY_AUTHTOKEN)).isNull(); assertThat((Intent) future.getResult().getParcelable(AccountManager.KEY_INTENT)).isNotNull(); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test public void getAuthToken_withNotifyAuthFailureSetToFalse_returnsCorrectToken() throws Exception { Account account = new Account("name", "google.com"); shadowOf(am).addAccount(account); shadowOf(am).addAuthenticator("google.com"); am.setAuthToken(account, "auth_token_type", "token1"); TestAccountManagerCallback<Bundle> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Bundle> future = am.getAuthToken( account, "auth_token_type", new Bundle(), /* notifyAuthFailure= */ false, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_NAME)) .isEqualTo(account.name); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_TYPE)) .isEqualTo(account.type); assertThat(future.getResult().getString(AccountManager.KEY_AUTHTOKEN)).isEqualTo("token1"); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test public void getAccountsByTypeAndFeatures() throws Exception { Account accountWithCorrectTypeAndFeatures = new Account("account_1", "google.com"); shadowOf(am).addAccount(accountWithCorrectTypeAndFeatures); shadowOf(am).setFeatures(accountWithCorrectTypeAndFeatures, new String[] { "FEATURE_1", "FEATURE_2" }); Account accountWithCorrectTypeButNotFeatures = new Account("account_2", "google.com"); shadowOf(am).addAccount(accountWithCorrectTypeButNotFeatures); shadowOf(am).setFeatures(accountWithCorrectTypeButNotFeatures, new String[] { "FEATURE_1" }); Account accountWithCorrectFeaturesButNotType = new Account("account_3", "facebook.com"); shadowOf(am).addAccount(accountWithCorrectFeaturesButNotType); shadowOf(am).setFeatures(accountWithCorrectFeaturesButNotType, new String[] { "FEATURE_1", "FEATURE_2" }); TestAccountManagerCallback<Account[]> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Account[]> future = am.getAccountsByTypeAndFeatures("google.com", new String[] { "FEATURE_1", "FEATURE_2" }, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult()).asList().containsExactly(accountWithCorrectTypeAndFeatures); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test public void getAccountsByTypeAndFeatures_returnsAllAccountsForNullFeature() throws Exception { Account accountWithCorrectTypeAndFeatures = new Account("account_1", "google.com"); shadowOf(am).addAccount(accountWithCorrectTypeAndFeatures); shadowOf(am).setFeatures( accountWithCorrectTypeAndFeatures, new String[] { "FEATURE_1", "FEATURE_2" }); Account accountWithCorrectTypeButNotFeatures = new Account("account_2", "google.com"); shadowOf(am).addAccount(accountWithCorrectTypeButNotFeatures); shadowOf(am).setFeatures(accountWithCorrectTypeButNotFeatures, new String[] { "FEATURE_1" }); Account accountWithCorrectFeaturesButNotType = new Account("account_3", "facebook.com"); shadowOf(am).addAccount(accountWithCorrectFeaturesButNotType); shadowOf(am).setFeatures( accountWithCorrectFeaturesButNotType, new String[] { "FEATURE_1", "FEATURE_2" }); TestAccountManagerCallback<Account[]> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Account[]> future = am.getAccountsByTypeAndFeatures("google.com", null, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult()).asList() .containsExactly(accountWithCorrectTypeAndFeatures, accountWithCorrectTypeButNotFeatures); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test(expected = AuthFailureError.class) public void resultContainsIntent() throws Exception { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putParcelable(AccountManager.KEY_INTENT, intent); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); mAuthenticator.getAuthToken(); }