Commit 8abd79f4 authored by Boris Sazonov's avatar Boris Sazonov Committed by Commit Bot

[Signin][Android] Filter account list when child account is present

This CL implements account list filtering when there are multiple
accounts on the device and one of these accounts is a child account.
In this case AccountManagerFacade will expose the child account only.

Bug: 779568
Change-Id: I70fb00fc692e3f54c779ac618167afd9ad728c09
Reviewed-on: https://chromium-review.googlesource.com/819414Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523524}
parent d5f7284f
......@@ -571,7 +571,19 @@ public class AccountManagerFacade {
private AccountManagerResult<Account[]> getAccountManagerResult() {
try {
return new AccountManagerResult<>(mDelegate.getAccountsSync());
Account[] accounts = mDelegate.getAccountsSync();
if (accounts.length <= 1) return new AccountManagerResult<>(accounts);
ArrayList<Account> filteredAccounts = new ArrayList<>();
for (Account account : accounts) {
if (hasFeatures(account, new String[] {FEATURE_IS_CHILD_ACCOUNT_KEY})) {
filteredAccounts.add(account);
}
}
// Don't filter if there are no child accounts
if (filteredAccounts.isEmpty()) return new AccountManagerResult<>(accounts);
return new AccountManagerResult<>(filteredAccounts.toArray(new Account[0]));
} catch (AccountManagerDelegateException ex) {
return new AccountManagerResult<>(ex);
}
......
......@@ -5,6 +5,7 @@
package org.chromium.components.signin.test;
import android.accounts.Account;
import android.support.test.filters.MediumTest;
import android.support.test.filters.SmallTest;
import android.support.test.rule.UiThreadTestRule;
......@@ -15,6 +16,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.components.signin.AccountManagerDelegateException;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.ProfileDataSource;
import org.chromium.components.signin.test.util.AccountHolder;
......@@ -64,6 +66,42 @@ public class AccountManagerFacadeTest {
Assert.assertTrue(mFacade.hasAccountForName("te.st.me@gmail.com"));
}
@Test
@MediumTest
public void testGetAccountsMultipleAccounts() throws AccountManagerDelegateException {
Assert.assertArrayEquals(new Account[] {}, mFacade.getGoogleAccounts());
Account account = addTestAccount("test@gmail.com");
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Account account2 = addTestAccount("test2@gmail.com");
Assert.assertArrayEquals(new Account[] {account, account2}, mFacade.getGoogleAccounts());
Account account3 = addTestAccount("test3@gmail.com");
Assert.assertArrayEquals(
new Account[] {account, account2, account3}, mFacade.getGoogleAccounts());
removeTestAccount(account2);
Assert.assertArrayEquals(new Account[] {account, account3}, mFacade.getGoogleAccounts());
}
@Test
@MediumTest
public void testGetAccountsChildAccountFiltering() throws AccountManagerDelegateException {
Account account = addTestAccount("test@gmail.com");
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Account childAccount = addChildTestAccount("child@gmail.com");
Assert.assertArrayEquals(new Account[] {childAccount}, mFacade.getGoogleAccounts());
Account account2 = addTestAccount("test2@gmail.com");
Assert.assertArrayEquals(new Account[] {childAccount}, mFacade.getGoogleAccounts());
// If child account is gone, non-child accounts should be exposed again
removeTestAccount(childAccount);
Assert.assertArrayEquals(new Account[] {account, account2}, mFacade.getGoogleAccounts());
}
@Test
@SmallTest
public void testProfileDataSource() throws Throwable {
......@@ -92,4 +130,19 @@ public class AccountManagerFacadeTest {
mDelegate.addAccountHolderBlocking(holder);
return account;
}
private Account addChildTestAccount(String accountName) {
Account account = AccountManagerFacade.createAccountFromName(accountName);
AccountHolder holder =
AccountHolder.builder(account)
.alwaysAccept(true)
.addFeature(AccountManagerFacade.FEATURE_IS_CHILD_ACCOUNT_KEY)
.build();
mDelegate.addAccountHolderBlocking(holder);
return account;
}
private void removeTestAccount(Account account) {
mDelegate.removeAccountHolderBlocking(AccountHolder.builder(account).build());
}
}
......@@ -29,7 +29,7 @@ import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
......@@ -112,7 +112,7 @@ public class FakeAccountManagerDelegate implements AccountManagerDelegate {
/** Use {@link FakeProfileDataSource}. */
public static final int ENABLE_PROFILE_DATA_SOURCE = 1;
private final Set<AccountHolder> mAccounts = new HashSet<>();
private final Set<AccountHolder> mAccounts = new LinkedHashSet<>();
private final ObserverList<AccountsChangeObserver> mObservers = new ObserverList<>();
private boolean mRegisterObserversCalled;
private FakeProfileDataSource mFakeProfileDataSource;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment