T getResult() throws Exception { return accountManagerFuture.getResult(); } }
private static Account[] getAccounts(final AccountManager manager) throws OperationCanceledException, AuthenticatorException, IOException { final AccountManagerFuture<Account[]> future = manager .getAccountsByTypeAndFeatures(ACCOUNT_TYPE, null, null, null); final Account[] accounts = future.getResult(); if (accounts != null && accounts.length > 0) { return getPasswordAccessibleAccounts(manager, accounts); } else { return new Account[0]; } }
@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(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(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(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 failedGetAuthToken() throws Exception { when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!")); mAuthenticator.getAuthToken(); }
@Test(expected = AuthFailureError.class) public void failedGetAuthToken() throws Exception { when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!")); mAuthenticator.getAuthToken(); }
@Test public void addAccount_activitySpecified() throws Exception { shadowOf(am).addAuthenticator("google.com"); AccountManagerFuture<Bundle> result = am.addAccount("google.com", "auth_token_type", null, null, activity, null, null); Bundle resultBundle = result.getResult(); assertThat(resultBundle.getString(AccountManager.KEY_ACCOUNT_TYPE)).isEqualTo("google.com"); assertThat(resultBundle.getString(AccountManager.KEY_ACCOUNT_NAME)).isEqualTo("some_user@gmail.com"); }
@Test public void addAccount_noActivitySpecified() throws Exception { shadowOf(am).addAuthenticator("google.com"); AccountManagerFuture<Bundle> result = am.addAccount("google.com", "auth_token_type", null, null, null, null, null); Bundle resultBundle = result.getResult(); assertThat((Intent) resultBundle.getParcelable(AccountManager.KEY_INTENT)).isNotNull(); }
@Test public void testSetAuthenticationErrorOnNextResponse() throws AuthenticatorException, IOException, OperationCanceledException { shadowOf(am).setAuthenticationErrorOnNextResponse(true); try { am.getAccountsByTypeAndFeatures(null, null, null, null).getResult(); fail("should have thrown"); } catch (AuthenticatorException expected) { // Expected } am.getAccountsByTypeAndFeatures(null, null, null, null).getResult(); }
@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 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 addAccount_withNoAuthenticatorForType_throwsExceptionInGetResult() throws Exception { assertThat(shadowOf(am).peekNextAddAccountOptions()).isNull(); AccountManagerFuture<Bundle> futureResult = am.addAccount("google.com", "auth_token_type", null, null, activity, null, null); try { futureResult.getResult(); fail("should have thrown"); } catch (AuthenticatorException expected) { } }
@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 removeAccount_does() throws Exception { Account account = new Account("name", "type"); shadowOf(am).addAccount(account); TestAccountManagerCallback<Boolean> testAccountManagerCallback = new TestAccountManagerCallback<>(); AccountManagerFuture<Boolean> future = am.removeAccount(account, testAccountManagerCallback, null); assertThat(future.getResult()).isTrue(); assertThat(am.getAccountsByType("type")).isEmpty(); assertThat(testAccountManagerCallback.accountManagerFuture).isNotNull(); }
@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 removeAccount_doesNotRemoveAccountOfDifferentName() throws Exception { Account account = new Account("name", "type"); shadowOf(am).addAccount(account); Account wrongAccount = new Account("wrong_name", "type"); AccountManagerFuture<Boolean> future = am.removeAccount(wrongAccount, null, null); assertThat(future.getResult()).isFalse(); assertThat(am.getAccountsByType("type")).isNotEmpty(); }
@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(); }