Commit fa281606 authored by Boris Sazonov's avatar Boris Sazonov Committed by Commit Bot

[Signin][Android] Change getGoogleAccounts() return type to List

This CL changes AccountManagerFacade.getGoogleAccounts() return type
from Account[] to List<Account>. This is done to ensure immutability of
this list and for consistency with getGoogleAccountNames().

Bug: None
Change-Id: If703947028dd4ed0c9fdaf18856d97d6b6a52aa5
Reviewed-on: https://chromium-review.googlesource.com/c/1338088
Commit-Queue: Boris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610026}
parent 4aa17434
......@@ -17,6 +17,7 @@ import org.chromium.base.annotations.CalledByNative;
import org.chromium.components.signin.AccountManagerFacade;
import java.io.File;
import java.util.List;
/**
* Helper for issuing intents to the android framework.
......@@ -40,10 +41,10 @@ public abstract class IntentHelper {
static void sendEmail(
String email, String subject, String body, String chooserTitle, String fileToAttach) {
if (TextUtils.isEmpty(email)) {
Account[] accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
if (accounts != null && accounts.length == 1
&& Patterns.EMAIL_ADDRESS.matcher(accounts[0].name).matches()) {
email = accounts[0].name;
List<Account> accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
if (accounts != null && accounts.size() == 1
&& Patterns.EMAIL_ADDRESS.matcher(accounts.get(0).name).matches()) {
email = accounts.get(0).name;
}
}
......
......@@ -376,9 +376,9 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
/** Choose an account to authenticate as for making RPCs to the backend. */
private void chooseAccountAsync(Bundle extras) {
AccountManagerFacade.get().tryGetGoogleAccounts(accounts -> {
if (accounts.length == 1) {
if (accounts.size() == 1) {
// If there's only one account, there aren't any doubts.
onAccountChosen(accounts[0]);
onAccountChosen(accounts.get(0));
return;
}
Account signedIn =
......@@ -414,8 +414,9 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
}
}
private static Account findAccountByName(Account[] accounts, String name) {
for (Account account : accounts) {
private static Account findAccountByName(List<Account> accounts, String name) {
for (int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);
if (account.name.equals(name)) {
return account;
}
......
......@@ -41,11 +41,11 @@ public class ChildAccountService {
ThreadUtils.assertOnUiThread();
final AccountManagerFacade accountManager = AccountManagerFacade.get();
accountManager.tryGetGoogleAccounts(accounts -> {
if (accounts.length != 1) {
if (accounts.size() != 1) {
// Child accounts can't share a device.
callback.onResult(ChildAccountStatus.NOT_CHILD);
} else {
accountManager.checkChildAccountStatus(accounts[0], callback);
accountManager.checkChildAccountStatus(accounts.get(0), callback);
}
});
}
......
......@@ -60,7 +60,7 @@ public abstract class FirstRunFlowSequencer {
// The following are initialized via initializeSharedState().
private boolean mIsAndroidEduDevice;
private @ChildAccountStatus.Status int mChildAccountStatus;
private Account[] mGoogleAccounts;
private List<Account> mGoogleAccounts;
private boolean mForceEduSignIn;
/**
......@@ -113,7 +113,7 @@ public abstract class FirstRunFlowSequencer {
}
@VisibleForTesting
protected Account[] getGoogleAccounts() {
protected List<Account> getGoogleAccounts() {
return AccountManagerFacade.get().tryGetGoogleAccounts();
}
......@@ -163,7 +163,7 @@ public abstract class FirstRunFlowSequencer {
mGoogleAccounts = getGoogleAccounts();
// EDU devices should always have exactly 1 google account, which will be automatically
// signed-in. All FRE screens are skipped in this case.
mForceEduSignIn = mIsAndroidEduDevice && mGoogleAccounts.length == 1 && !isSignedIn();
mForceEduSignIn = mIsAndroidEduDevice && mGoogleAccounts.size() == 1 && !isSignedIn();
}
void processFreEnvironmentPreNative() {
......@@ -195,7 +195,7 @@ public abstract class FirstRunFlowSequencer {
/**
* Called onNativeInitialized() a given flow as completed.
* @param data Resulting FRE properties bundle.
* @param freProperties Resulting FRE properties bundle.
*/
public void onNativeInitialized(Bundle freProperties) {
// We show the sign-in page if sync is allowed, and not signed in, and this is not
......@@ -203,13 +203,13 @@ public abstract class FirstRunFlowSequencer {
// - no "skip the first use hints" is set, or
// - "skip the first use hints" is set, but there is at least one account.
boolean offerSignInOk = isSyncAllowed() && !isSignedIn() && !mForceEduSignIn
&& (!shouldSkipFirstUseHints() || mGoogleAccounts.length > 0);
&& (!shouldSkipFirstUseHints() || !mGoogleAccounts.isEmpty());
freProperties.putBoolean(FirstRunActivity.SHOW_SIGNIN_PAGE, offerSignInOk);
if (mForceEduSignIn || ChildAccountStatus.isChild(mChildAccountStatus)) {
// If the device is an Android EDU device or has a child account, there should be
// exactly account on the device. Force sign-in in to that account.
freProperties.putString(
AccountFirstRunFragment.FORCE_SIGNIN_ACCOUNT_TO, mGoogleAccounts[0].name);
AccountFirstRunFragment.FORCE_SIGNIN_ACCOUNT_TO, mGoogleAccounts.get(0).name);
}
freProperties.putBoolean(
......
......@@ -4,10 +4,8 @@
package org.chromium.chrome.browser.firstrun;
import android.accounts.Account;
import android.content.Context;
import org.chromium.base.Callback;
import org.chromium.base.Log;
import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.externalauth.ExternalAuthUtils;
......@@ -77,14 +75,12 @@ public final class ForcedSigninProcessor {
Log.d(TAG, "Sign in disallowed");
return;
}
AccountManagerFacade.get().tryGetGoogleAccounts(new Callback<Account[]>() {
@Override
public void onResult(Account[] accounts) {
if (accounts.length != 1) {
Log.d(TAG, "Incorrect number of accounts (%d)", accounts.length);
AccountManagerFacade.get().tryGetGoogleAccounts(accounts -> {
if (accounts.size() != 1) {
Log.d(TAG, "Incorrect number of accounts (%d)", accounts.size());
return;
}
signinManager.signIn(accounts[0], null, new SigninManager.SignInCallback() {
signinManager.signIn(accounts.get(0), null, new SigninManager.SignInCallback() {
@Override
public void onSignInComplete() {
if (onComplete != null) {
......@@ -99,7 +95,6 @@ public final class ForcedSigninProcessor {
}
}
});
}
});
}
......
......@@ -201,7 +201,7 @@ public abstract class RequestGenerator {
() -> ProcessInitializationHandler.getInstance().initializePreNative());
int numAccounts = 0;
try {
numAccounts = AccountManagerFacade.get().getGoogleAccounts().length;
numAccounts = AccountManagerFacade.get().getGoogleAccounts().size();
} catch (Exception e) {
Log.e(TAG, "Can't get number of accounts.", e);
}
......
......@@ -15,6 +15,8 @@ import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.ChromeSigninController;
import org.chromium.components.sync.AndroidSyncSettings;
import java.util.List;
/**
* A preference that displays the account currently being synced and allows the user to choose a new
* account to use for syncing. The values used are the account names.
......@@ -47,15 +49,15 @@ public class SyncedAccountPreference extends ListPreference {
// signed in account
}
Account[] accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
String[] accountNames = new String[accounts.length];
String[] accountValues = new String[accounts.length];
List<Account> accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
String[] accountNames = new String[accounts.size()];
String[] accountValues = new String[accounts.size()];
String signedInAccountName = ChromeSigninController.get().getSignedInAccountName();
String signedInSettingsKey = "";
for (int i = 0; i < accounts.length; ++i) {
Account account = accounts[i];
for (int i = 0; i < accounts.size(); ++i) {
Account account = accounts.get(i);
accountNames[i] = account.name;
accountValues[i] = account.name;
boolean isPrimaryAccount = TextUtils.equals(account.name, signedInAccountName);
......
......@@ -49,6 +49,8 @@ import org.chromium.chrome.browser.sync.ui.SyncCustomizationFragment;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.ChromeSigninController;
import java.util.List;
/**
* The settings screen with information and settings related to the user's accounts.
*
......@@ -358,8 +360,9 @@ public class AccountManagementFragment extends PreferenceFragment
accountsCategory.removeAll();
Account[] accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
for (final Account account : accounts) {
List<Account> accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
for (int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);
Preference pref = new Preference(getActivity());
pref.setLayoutResource(R.layout.account_management_account_row);
pref.setTitle(account.name);
......
......@@ -136,10 +136,11 @@ public class AccountTrackerService {
@Override
public String[][] doInBackground() {
Log.d(TAG, "Getting id/email mapping");
String[][] accountIdNameMap = new String[2][accounts.length];
for (int i = 0; i < accounts.length; ++i) {
accountIdNameMap[0][i] = accountIdProvider.getAccountId(accounts[i].name);
accountIdNameMap[1][i] = accounts[i].name;
String[][] accountIdNameMap = new String[2][accounts.size()];
for (int i = 0; i < accounts.size(); ++i) {
accountIdNameMap[0][i] =
accountIdProvider.getAccountId(accounts.get(i).name);
accountIdNameMap[1][i] = accounts.get(i).name;
}
return accountIdNameMap;
}
......@@ -222,9 +223,9 @@ public class AccountTrackerService {
return;
}
String[] accountNames = new String[accounts.length];
for (int i = 0; i < accounts.length; ++i) {
accountNames[i] = accounts[i].name;
String[] accountNames = new String[accounts.size()];
for (int i = 0; i < accounts.size(); ++i) {
accountNames[i] = accounts.get(i).name;
}
if (nativeAreAccountsSeeded(accountNames)) {
mSystemAccountsSeedingStatus = SystemAccountsSeedingStatus.SEEDING_DONE;
......
......@@ -233,8 +233,9 @@ public class SigninHelper {
}
private static boolean accountExists(Account account) {
Account[] accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
for (Account a : accounts) {
List<Account> accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
for (int i = 0; i < accounts.size(); i++) {
Account a = accounts.get(i);
if (a.equals(account)) {
return true;
}
......
......@@ -21,6 +21,7 @@ import org.chromium.components.signin.ChromeSigninController;
import org.chromium.ui.base.WindowAndroid;
import java.util.Collections;
import java.util.List;
import java.util.Set;
/**
......@@ -119,9 +120,9 @@ public class SigninPromoUtil {
ProfileDataCache profileDataCache, PersonalizedSigninPromoView view,
SigninPromoController.OnDismissListener listener) {
DisplayableProfileData profileData = null;
Account[] accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
if (accounts.length > 0) {
String defaultAccountName = accounts[0].name;
List<Account> accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
if (accounts.size() > 0) {
String defaultAccountName = accounts.get(0).name;
profileDataCache.update(Collections.singletonList(defaultAccountName));
profileData = profileDataCache.getProfileDataOrDefault(defaultAccountName);
}
......
......@@ -57,6 +57,7 @@ import org.chromium.components.sync.StopSource;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
......@@ -302,8 +303,8 @@ public class SyncCustomizationFragment extends PreferenceFragment
// We remove the the SyncedAccountPreference if there's only 1 account on the device, so
// it's possible for accountList to be null
if (accountList != null) {
Account[] accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
if (accounts.length <= 1) {
List<Account> accounts = AccountManagerFacade.get().tryGetGoogleAccounts();
if (accounts.size() <= 1) {
getPreferenceScreen().removePreference(accountList);
} else {
accountList.setEnabled(mSyncSwitchPreference.isChecked());
......
......@@ -26,6 +26,9 @@ import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.Feature;
import org.chromium.components.signin.ChildAccountStatus;
import java.util.Collections;
import java.util.List;
/**
* Tests FirstRunFlowSequencer which contains the core logic of what should be shown during the
* first run.
......@@ -49,7 +52,7 @@ public class FirstRunFlowSequencerTest {
public boolean isFirstRunFlowComplete;
public boolean isSignedIn;
public boolean isSyncAllowed;
public Account[] googleAccounts;
public List<Account> googleAccounts;
public boolean hasAnyUserSeenToS;
public boolean shouldSkipFirstUseHints;
public boolean isFirstRunEulaAccepted;
......@@ -83,7 +86,7 @@ public class FirstRunFlowSequencerTest {
}
@Override
public Account[] getGoogleAccounts() {
public List<Account> getGoogleAccounts() {
return googleAccounts;
}
......@@ -140,12 +143,11 @@ public class FirstRunFlowSequencerTest {
@Test
@Feature({"FirstRun"})
public void testFirstRunComplete() {
Account[] accounts = new Account[1];
accounts[0] = new Account(DEFAULT_ACCOUNT, GOOGLE_ACCOUNT_TYPE);
mSequencer.isFirstRunFlowComplete = true;
mSequencer.isSignedIn = false;
mSequencer.isSyncAllowed = true;
mSequencer.googleAccounts = accounts;
mSequencer.googleAccounts =
Collections.singletonList(new Account(DEFAULT_ACCOUNT, GOOGLE_ACCOUNT_TYPE));
mSequencer.hasAnyUserSeenToS = true;
mSequencer.shouldSkipFirstUseHints = false;
mSequencer.isFirstRunEulaAccepted = true;
......@@ -164,7 +166,7 @@ public class FirstRunFlowSequencerTest {
mSequencer.isFirstRunFlowComplete = false;
mSequencer.isSignedIn = false;
mSequencer.isSyncAllowed = true;
mSequencer.googleAccounts = new Account[0];
mSequencer.googleAccounts = Collections.emptyList();
mSequencer.hasAnyUserSeenToS = false;
mSequencer.shouldSkipFirstUseHints = false;
mSequencer.shouldShowDataReductionPage = false;
......@@ -189,12 +191,11 @@ public class FirstRunFlowSequencerTest {
@Test
@Feature({"FirstRun"})
public void testStandardFlowOneChildAccount() {
Account[] accounts = new Account[1];
accounts[0] = new Account(DEFAULT_ACCOUNT, GOOGLE_ACCOUNT_TYPE);
mSequencer.isFirstRunFlowComplete = false;
mSequencer.isSignedIn = false;
mSequencer.isSyncAllowed = true;
mSequencer.googleAccounts = accounts;
mSequencer.googleAccounts =
Collections.singletonList(new Account(DEFAULT_ACCOUNT, GOOGLE_ACCOUNT_TYPE));
mSequencer.hasAnyUserSeenToS = false;
mSequencer.shouldSkipFirstUseHints = false;
mSequencer.shouldShowDataReductionPage = false;
......@@ -224,7 +225,7 @@ public class FirstRunFlowSequencerTest {
mSequencer.isFirstRunFlowComplete = false;
mSequencer.isSignedIn = false;
mSequencer.isSyncAllowed = true;
mSequencer.googleAccounts = new Account[0];
mSequencer.googleAccounts = Collections.emptyList();
mSequencer.hasAnyUserSeenToS = false;
mSequencer.shouldSkipFirstUseHints = false;
mSequencer.shouldShowDataReductionPage = true;
......@@ -253,7 +254,7 @@ public class FirstRunFlowSequencerTest {
mSequencer.isFirstRunFlowComplete = false;
mSequencer.isSignedIn = false;
mSequencer.isSyncAllowed = true;
mSequencer.googleAccounts = new Account[0];
mSequencer.googleAccounts = Collections.emptyList();
mSequencer.hasAnyUserSeenToS = false;
mSequencer.shouldSkipFirstUseHints = false;
mSequencer.shouldShowDataReductionPage = true;
......
......@@ -33,6 +33,8 @@ import org.chromium.components.signin.util.PatternMatcher;
import org.chromium.net.NetworkChangeNotifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
......@@ -83,9 +85,9 @@ public class AccountManagerFacade {
// These two variables should be accessed from either UI thread or during initialization phase.
private PatternMatcher[] mAccountRestrictionPatterns;
private AccountManagerResult<Account[]> mAllAccounts;
private AccountManagerResult<List<Account>> mAllAccounts;
private final AtomicReference<AccountManagerResult<Account[]>> mFilteredAccounts =
private final AtomicReference<AccountManagerResult<List<Account>>> mFilteredAccounts =
new AtomicReference<>();
private final CountDownLatch mPopulateAccountCacheLatch = new CountDownLatch(1);
private final CachedMetrics.TimesHistogramSample mPopulateAccountCacheWaitingTimeHistogram =
......@@ -270,7 +272,9 @@ public class AccountManagerFacade {
@AnyThread
public List<String> tryGetGoogleAccountNames() {
List<String> accountNames = new ArrayList<>();
for (Account account : tryGetGoogleAccounts()) {
List<Account> tryGetGoogleAccounts = tryGetGoogleAccounts();
for (int i = 0; i < tryGetGoogleAccounts.size(); i++) {
Account account = tryGetGoogleAccounts.get(i);
accountNames.add(account.name);
}
return accountNames;
......@@ -291,10 +295,10 @@ public class AccountManagerFacade {
public void getGoogleAccountNames(
final Callback<AccountManagerResult<List<String>>> callback) {
runAfterCacheIsPopulated(() -> {
final AccountManagerResult<Account[]> accounts = mFilteredAccounts.get();
final AccountManagerResult<List<Account>> accounts = mFilteredAccounts.get();
final AccountManagerResult<List<String>> result;
if (accounts.hasValue()) {
List<String> accountNames = new ArrayList<>(accounts.getValue().length);
List<String> accountNames = new ArrayList<>(accounts.getValue().size());
for (Account account : accounts.getValue()) {
accountNames.add(account.name);
}
......@@ -313,8 +317,8 @@ public class AccountManagerFacade {
* Chrome lacks necessary permissions, etc.
*/
@AnyThread
public Account[] getGoogleAccounts() throws AccountManagerDelegateException {
AccountManagerResult<Account[]> maybeAccounts = mFilteredAccounts.get();
public List<Account> getGoogleAccounts() throws AccountManagerDelegateException {
AccountManagerResult<List<Account>> maybeAccounts = mFilteredAccounts.get();
if (maybeAccounts == null) {
try {
// First call to update hasn't finished executing yet, should wait for it
......@@ -336,7 +340,7 @@ public class AccountManagerFacade {
* Asynchronous version of {@link #getGoogleAccounts()}.
*/
@MainThread
public void getGoogleAccounts(final Callback<AccountManagerResult<Account[]>> callback) {
public void getGoogleAccounts(Callback<AccountManagerResult<List<Account>>> callback) {
runAfterCacheIsPopulated(() -> callback.onResult(mFilteredAccounts.get()));
}
......@@ -345,11 +349,11 @@ public class AccountManagerFacade {
* Returns an empty array if an error occurs while getting account list.
*/
@AnyThread
public Account[] tryGetGoogleAccounts() {
public List<Account> tryGetGoogleAccounts() {
try {
return getGoogleAccounts();
} catch (AccountManagerDelegateException e) {
return new Account[0];
return Collections.emptyList();
}
}
......@@ -357,7 +361,7 @@ public class AccountManagerFacade {
* Asynchronous version of {@link #tryGetGoogleAccounts()}.
*/
@MainThread
public void tryGetGoogleAccounts(final Callback<Account[]> callback) {
public void tryGetGoogleAccounts(final Callback<List<Account>> callback) {
runAfterCacheIsPopulated(() -> callback.onResult(tryGetGoogleAccounts()));
}
......@@ -367,7 +371,7 @@ public class AccountManagerFacade {
*/
@AnyThread
public boolean hasGoogleAccounts() {
return tryGetGoogleAccounts().length > 0;
return !tryGetGoogleAccounts().isEmpty();
}
/**
......@@ -398,7 +402,7 @@ public class AccountManagerFacade {
@AnyThread
public Account getAccountFromName(String accountName) {
String canonicalName = canonicalizeName(accountName);
Account[] accounts = tryGetGoogleAccounts();
List<Account> accounts = tryGetGoogleAccounts();
for (Account account : accounts) {
if (canonicalizeName(account.name).equals(canonicalName)) {
return account;
......@@ -615,15 +619,16 @@ public class AccountManagerFacade {
ContextUtils.getApplicationContext().registerReceiver(receiver, filter);
}
private AccountManagerResult<Account[]> getAllAccounts() {
private AccountManagerResult<List<Account>> getAllAccounts() {
try {
return new AccountManagerResult<>(mDelegate.getAccountsSync());
List<Account> accounts = Arrays.asList(mDelegate.getAccountsSync());
return new AccountManagerResult<>(Collections.unmodifiableList(accounts));
} catch (AccountManagerDelegateException ex) {
return new AccountManagerResult<>(ex);
}
}
private AccountManagerResult<Account[]> getFilteredAccounts() {
private AccountManagerResult<List<Account>> getFilteredAccounts() {
if (mAllAccounts.hasException() || mAccountRestrictionPatterns == null) return mAllAccounts;
ArrayList<Account> filteredAccounts = new ArrayList<>();
for (Account account : mAllAccounts.getValue()) {
......@@ -634,7 +639,7 @@ public class AccountManagerFacade {
}
}
}
return new AccountManagerResult<>(filteredAccounts.toArray(new Account[0]));
return new AccountManagerResult<>(Collections.unmodifiableList(filteredAccounts));
}
private static PatternMatcher[] getAccountRestrictionPatterns() {
......@@ -673,7 +678,7 @@ public class AccountManagerFacade {
fireOnAccountsChangedNotification();
}
private void setAllAccounts(AccountManagerResult<Account[]> allAccounts) {
private void setAllAccounts(AccountManagerResult<List<Account>> allAccounts) {
mAllAccounts = allAccounts;
mFilteredAccounts.set(getFilteredAccounts());
fireOnAccountsChangedNotification();
......@@ -741,19 +746,19 @@ public class AccountManagerFacade {
}
}
private class UpdateAccountsTask extends AsyncTask<AccountManagerResult<Account[]>> {
private class UpdateAccountsTask extends AsyncTask<AccountManagerResult<List<Account>>> {
@Override
protected void onPreExecute() {
++mUpdateTasksCounter;
}
@Override
protected AccountManagerResult<Account[]> doInBackground() {
protected AccountManagerResult<List<Account>> doInBackground() {
return getAllAccounts();
}
@Override
protected void onPostExecute(AccountManagerResult<Account[]> allAccounts) {
protected void onPostExecute(AccountManagerResult<List<Account>> allAccounts) {
setAllAccounts(allAccounts);
decrementUpdateCounter();
}
......
......@@ -14,6 +14,8 @@ import android.os.UserManager;
import android.support.test.filters.SmallTest;
import android.support.test.rule.UiThreadTestRule;
import com.google.common.collect.ImmutableList;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
......@@ -22,7 +24,6 @@ import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.chromium.base.Callback;
import org.chromium.base.task.test.CustomShadowAsyncTask;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.components.signin.AccountManagerDelegateException;
......@@ -130,20 +131,20 @@ public class AccountManagerFacadeRobolectricTest {
@Test
@SmallTest
public void testGetAccounts() throws AccountManagerDelegateException {
Assert.assertArrayEquals(new Account[] {}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(), mFacade.getGoogleAccounts());
Account account = addTestAccount("test@gmail.com");
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account), mFacade.getGoogleAccounts());
Account account2 = addTestAccount("test2@gmail.com");
Assert.assertArrayEquals(new Account[] {account, account2}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account, account2), mFacade.getGoogleAccounts());
Account account3 = addTestAccount("test3@gmail.com");
Assert.assertArrayEquals(
new Account[] {account, account2, account3}, mFacade.getGoogleAccounts());
Assert.assertEquals(
ImmutableList.of(account, account2, account3), mFacade.getGoogleAccounts());
removeTestAccount(account2);
Assert.assertArrayEquals(new Account[] {account, account3}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account, account3), mFacade.getGoogleAccounts());
}
@Test
......@@ -151,19 +152,19 @@ public class AccountManagerFacadeRobolectricTest {
public void testGetAccountsWithAccountPattern() throws AccountManagerDelegateException {
setAccountRestrictionPatterns("*@example.com");
Account account = addTestAccount("test@example.com");
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account), mFacade.getGoogleAccounts());
addTestAccount("test@gmail.com"); // Doesn't match the pattern.
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account), mFacade.getGoogleAccounts());
Account account2 = addTestAccount("test2@example.com");
Assert.assertArrayEquals(new Account[] {account, account2}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account, account2), mFacade.getGoogleAccounts());
addTestAccount("test2@gmail.com"); // Doesn't match the pattern.
Assert.assertArrayEquals(new Account[] {account, account2}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account, account2), mFacade.getGoogleAccounts());
removeTestAccount(account);
Assert.assertArrayEquals(new Account[] {account2}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account2), mFacade.getGoogleAccounts());
}
@Test
......@@ -172,44 +173,44 @@ public class AccountManagerFacadeRobolectricTest {
setAccountRestrictionPatterns("test1@example.com", "test2@gmail.com");
addTestAccount("test@gmail.com"); // Doesn't match the pattern.
addTestAccount("test@example.com"); // Doesn't match the pattern.
Assert.assertArrayEquals(new Account[] {}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(), mFacade.getGoogleAccounts());
Account account = addTestAccount("test1@example.com");
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account), mFacade.getGoogleAccounts());
addTestAccount("test2@example.com"); // Doesn't match the pattern.
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account), mFacade.getGoogleAccounts());
Account account2 = addTestAccount("test2@gmail.com");
Assert.assertArrayEquals(new Account[] {account, account2}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account, account2), mFacade.getGoogleAccounts());
}
@Test
@SmallTest
public void testGetAccountsWithAccountPatternsChange() throws AccountManagerDelegateException {
Assert.assertArrayEquals(new Account[] {}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(), mFacade.getGoogleAccounts());
Account account = addTestAccount("test@gmail.com");
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account), mFacade.getGoogleAccounts());
Account account2 = addTestAccount("test2@example.com");
Assert.assertArrayEquals(new Account[] {account, account2}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account, account2), mFacade.getGoogleAccounts());
Account account3 = addTestAccount("test3@gmail.com");
Assert.assertArrayEquals(
new Account[] {account, account2, account3}, mFacade.getGoogleAccounts());
Assert.assertEquals(
ImmutableList.of(account, account2, account3), mFacade.getGoogleAccounts());
setAccountRestrictionPatterns("test@gmail.com");
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account), mFacade.getGoogleAccounts());
setAccountRestrictionPatterns("*@example.com", "test3@gmail.com");
Assert.assertArrayEquals(new Account[] {account2, account3}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account2, account3), mFacade.getGoogleAccounts());
removeTestAccount(account3);
Assert.assertArrayEquals(new Account[] {account2}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account2), mFacade.getGoogleAccounts());
clearAccountRestrictionPatterns();
Assert.assertArrayEquals(new Account[] {account, account2}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account, account2), mFacade.getGoogleAccounts());
}
@Test
......@@ -217,7 +218,7 @@ public class AccountManagerFacadeRobolectricTest {
public void testGetAccountsMultipleMatchingPatterns() throws AccountManagerDelegateException {
setAccountRestrictionPatterns("*@gmail.com", "test@gmail.com");
Account account = addTestAccount("test@gmail.com"); // Matches both patterns
Assert.assertArrayEquals(new Account[] {account}, mFacade.getGoogleAccounts());
Assert.assertEquals(ImmutableList.of(account), mFacade.getGoogleAccounts());
}
@Test
......@@ -256,12 +257,9 @@ public class AccountManagerFacadeRobolectricTest {
private void assertChildAccountStatus(
Account account, @ChildAccountStatus.Status Integer status) {
final AtomicInteger callCount = new AtomicInteger();
AccountManagerFacade.get().checkChildAccountStatus(account, new Callback<Integer>() {
@Override
public void onResult(@ChildAccountStatus.Status Integer result) {
AccountManagerFacade.get().checkChildAccountStatus(account, result -> {
callCount.incrementAndGet();
Assert.assertEquals(result, status);
}
});
Assert.assertEquals(1, callCount.get());
}
......
......@@ -14,6 +14,7 @@ import android.os.StrictMode;
import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.ObserverList;
import org.chromium.base.StrictModeContext;
import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.components.signin.AccountManagerFacade;
......@@ -242,7 +243,7 @@ public class AndroidSyncSettings {
mIsSyncable = shouldBeSyncable;
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
try (StrictModeContext ignored = StrictModeContext.allowDiskWrites()) {
// Make account syncable if there is one.
if (shouldBeSyncable) {
mSyncContentResolverDelegate.setIsSyncable(mAccount, mContractAuthority, 1);
......@@ -250,18 +251,15 @@ public class AndroidSyncSettings {
mSyncContentResolverDelegate.removePeriodicSync(
mAccount, mContractAuthority, Bundle.EMPTY);
}
StrictMode.setThreadPolicy(oldPolicy);
}
// Disable the syncability of Chrome for all other accounts.
ThreadUtils.postOnUiThread(new Runnable() {
@Override
public void run() {
AccountManagerFacade.get().tryGetGoogleAccounts(new Callback<Account[]>() {
@Override
public void onResult(Account[] accounts) {
ThreadUtils.postOnUiThread(() -> {
AccountManagerFacade.get().tryGetGoogleAccounts(accounts -> {
synchronized (mLock) {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
for (Account account : accounts) {
try (StrictModeContext ignored = StrictModeContext.allowDiskWrites()) {
for (int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);
if (!account.equals(mAccount)
&& mSyncContentResolverDelegate.getIsSyncable(
account, mContractAuthority)
......@@ -270,13 +268,11 @@ public class AndroidSyncSettings {
account, mContractAuthority, 0);
}
}
StrictMode.setThreadPolicy(oldPolicy);
}
}
if (callback != null) callback.onResult(true);
}
});
}
});
}
......
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