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 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 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(); }