Commit a1c88d09 authored by Alice Wang's avatar Alice Wang Committed by Commit Bot

[Signin] Migrate AccountManagerFacade override delegate to setInstanceForTests

This CL migrates the usage of
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests to
AccountManagerFacadeProvider.setInstanceForTests.

Bug: 1063866
Change-Id: I089ae89158cfdfac92638c5a718a9faf2a53b9d2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2129527Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Commit-Queue: Alice Wang <aliceywang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756334}
parent cb2c5a8a
......@@ -18,10 +18,12 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.AccountManagerFacadeProvider;
import org.chromium.components.signin.AccountUtils;
import org.chromium.components.signin.test.util.AccountHolder;
import org.chromium.components.signin.test.util.FakeAccountManagerDelegate;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* Unit Test for {@link FirstRunUtils}.
......@@ -63,7 +65,10 @@ public class FirstRunUtilsTest {
private void setUpAccountManager(String accountType) {
mAccountManager = new FakeAuthenticationAccountManager(accountType);
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(mAccountManager);
TestThreadUtils.runOnUiThreadBlocking(() -> {
AccountManagerFacadeProvider.setInstanceForTests(
new AccountManagerFacade(mAccountManager));
});
}
private void addTestAccount() {
......
......@@ -23,9 +23,11 @@ import org.chromium.chrome.test.omaha.AttributeFinder;
import org.chromium.chrome.test.omaha.MockRequestGenerator;
import org.chromium.chrome.test.omaha.MockRequestGenerator.DeviceType;
import org.chromium.chrome.test.omaha.MockRequestGenerator.SignedInStatus;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.AccountManagerFacadeProvider;
import org.chromium.components.signin.test.util.AccountHolder;
import org.chromium.components.signin.test.util.FakeAccountManagerDelegate;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* Unit tests for the RequestGenerator class.
......@@ -179,7 +181,10 @@ public class RequestGeneratorTest {
for (Account account : accounts) {
accountManager.addAccountHolderExplicitly(AccountHolder.builder(account).build());
}
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(accountManager);
TestThreadUtils.runOnUiThreadBlocking(() -> {
AccountManagerFacadeProvider.setInstanceForTests(
new AccountManagerFacade(accountManager));
});
String sessionId = "random_session_id";
String requestId = "random_request_id";
......
......@@ -40,7 +40,7 @@ public class SigninHelperTest {
@After
public void tearDown() {
AccountManagerFacadeProvider.resetAccountManagerFacadeForTests();
AccountManagerFacadeProvider.resetInstanceForTests();
}
@Test
......
......@@ -23,6 +23,7 @@ import org.chromium.chrome.browser.preferences.ChromePreferenceKeys;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.components.signin.AccountManagerDelegate;
import org.chromium.components.signin.AccountManagerDelegateException;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.AccountManagerFacadeProvider;
import java.util.HashSet;
......@@ -62,7 +63,8 @@ public class ToSAckedReceiverTest {
Account[] accounts = new Account[1];
accounts[0] = new Account(GOOGLE_ACCOUNT, "LegitAccount");
Mockito.doReturn(accounts).when(accountManagerDelegate).getAccountsSync();
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(accountManagerDelegate);
AccountManagerFacadeProvider.setInstanceForTests(
new AccountManagerFacade(accountManagerDelegate));
Assert.assertTrue(ToSAckedReceiver.checkAnyUserHasSeenToS());
}
}
......@@ -42,7 +42,7 @@ public class VoiceRecognitionUtilTest {
@After
public void tearDown() {
AccountManagerFacadeProvider.resetAccountManagerFacadeForTests();
AccountManagerFacadeProvider.resetInstanceForTests();
}
private static class IntentTestPackageManager extends MockPackageManager {
......
......@@ -19,12 +19,14 @@ import org.chromium.chrome.browser.suggestions.tile.TileSectionType;
import org.chromium.chrome.browser.suggestions.tile.TileSource;
import org.chromium.chrome.browser.suggestions.tile.TileTitleSource;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.AccountManagerFacadeProvider;
import org.chromium.components.signin.AccountUtils;
import org.chromium.components.signin.test.util.AccountHolder;
import org.chromium.components.signin.test.util.FakeAccountManagerDelegate;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
import java.util.ArrayList;
......@@ -97,7 +99,10 @@ public class NewTabPageTestUtils {
public static void setUpTestAccount() {
FakeAccountManagerDelegate fakeAccountManager = new FakeAccountManagerDelegate(
FakeAccountManagerDelegate.ENABLE_PROFILE_DATA_SOURCE);
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(fakeAccountManager);
TestThreadUtils.runOnUiThreadBlocking(() -> {
AccountManagerFacadeProvider.setInstanceForTests(
new AccountManagerFacade(fakeAccountManager));
});
Account account = AccountUtils.createAccountFromName("test@gmail.com");
fakeAccountManager.addAccountHolderExplicitly(new AccountHolder.Builder(account).build());
assertFalse(AccountManagerFacadeProvider.getInstance().isUpdatePending().get());
......
......@@ -18,6 +18,7 @@ import org.chromium.chrome.browser.signin.IdentityServicesProvider;
import org.chromium.chrome.browser.signin.SigninManager;
import org.chromium.chrome.browser.signin.SigninPreferencesManager;
import org.chromium.chrome.browser.sync.ProfileSyncService;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.AccountManagerFacadeProvider;
import org.chromium.components.signin.AccountTrackerService;
import org.chromium.components.signin.AccountUtils;
......@@ -53,7 +54,10 @@ public final class SigninTestUtil {
public static void setUpAuthForTest() {
sAccountManager = new FakeAccountManagerDelegate(
FakeAccountManagerDelegate.DISABLE_PROFILE_DATA_SOURCE);
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(sAccountManager);
TestThreadUtils.runOnUiThreadBlocking(() -> {
AccountManagerFacadeProvider.setInstanceForTests(
new AccountManagerFacade(sAccountManager));
});
}
/**
......
......@@ -11,6 +11,7 @@ import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.AccountManagerFacadeProvider;
import org.chromium.components.signin.test.util.FakeAccountManagerDelegate;
import org.chromium.content_public.browser.test.NativeLibraryTestRule;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
/**
* Loads native and initializes the browser process for Paint Preview instrumentation tests.
......@@ -25,7 +26,10 @@ public class PaintPreviewTestRule extends NativeLibraryTestRule {
private void setUp() {
mAccountManager = new FakeAccountManagerDelegate(
FakeAccountManagerDelegate.DISABLE_PROFILE_DATA_SOURCE);
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(mAccountManager);
TestThreadUtils.runOnUiThreadBlocking(() -> {
AccountManagerFacadeProvider.setInstanceForTests(
new AccountManagerFacade(mAccountManager));
});
loadNativeLibraryAndInitBrowserProcess();
}
......
......@@ -84,7 +84,8 @@ public class AccountManagerFacade {
/**
* @param delegate the AccountManagerDelegate to use as a backend
*/
AccountManagerFacade(AccountManagerDelegate delegate) {
@VisibleForTesting
public AccountManagerFacade(AccountManagerDelegate delegate) {
ThreadUtils.assertOnUiThread();
mDelegate = delegate;
mDelegate.registerObservers();
......
......@@ -26,7 +26,7 @@ public class AccountManagerFacadeProvider {
/**
* Initializes AccountManagerFacade singleton instance. Can only be called once.
* Tests can override the instance with {@link #overrideAccountManagerFacadeForTests}.
* Tests can override the instance with {@link #setInstanceForTests}.
*
* @param delegate the AccountManagerDelegate to use
*/
......@@ -42,27 +42,23 @@ public class AccountManagerFacadeProvider {
}
/**
* Overrides AccountManagerFacade singleton instance for tests. Only for use in Tests.
* Overrides any previous or future calls to {@link #initializeAccountManagerFacade}.
*
* @param delegate the AccountManagerDelegate to use
* Sets the test instance.
*/
@VisibleForTesting
@AnyThread
public static void overrideAccountManagerFacadeForTests(AccountManagerDelegate delegate) {
public static void setInstanceForTests(AccountManagerFacade accountManagerFacade) {
ThreadUtils.runOnUiThreadBlocking(() -> {
sTestingInstance = new AccountManagerFacade(delegate);
sTestingInstance = accountManagerFacade;
sAtomicInstance.set(sTestingInstance);
});
}
/**
* Resets custom AccountManagerFacade set with {@link #overrideAccountManagerFacadeForTests}.
* Only for use in Tests.
* Resets the test instance set with {@link #setInstanceForTests}.
*/
@VisibleForTesting
@AnyThread
public static void resetAccountManagerFacadeForTests() {
public static void resetInstanceForTests() {
ThreadUtils.runOnUiThreadBlocking(() -> {
sTestingInstance = null;
sAtomicInstance.set(sInstance);
......@@ -71,7 +67,7 @@ public class AccountManagerFacadeProvider {
/**
* Singleton instance getter. Singleton must be initialized before calling this by
* {@link #initializeAccountManagerFacade} or {@link #overrideAccountManagerFacadeForTests}.
* {@link #initializeAccountManagerFacade} or {@link #setInstanceForTests}.
*
* @return a singleton instance
*/
......
......@@ -10,6 +10,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;
......@@ -86,10 +88,11 @@ public final class ChildAccountInfoFetcher {
mNativeAccountFetcherService, mAccountId, isChildAccount);
}
@VisibleForTesting
@CalledByNative
private static void initializeForTests() {
AccountManagerDelegate delegate = new SystemAccountManagerDelegate();
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(delegate);
AccountManagerFacadeProvider.setInstanceForTests(new AccountManagerFacade(delegate));
}
@NativeMethods
......
......@@ -36,7 +36,9 @@ public class AccountManagerFacadeTest {
@Before
public void setUp() {
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(mDelegate);
ThreadUtils.runOnUiThreadBlocking(() -> {
AccountManagerFacadeProvider.setInstanceForTests(new AccountManagerFacade(mDelegate));
});
}
@Test
......
......@@ -53,7 +53,10 @@ public class AccountManagerTestRule implements TestRule {
: FakeAccountManagerDelegate.ENABLE_BLOCK_GET_ACCOUNTS;
mDelegate = new FakeAccountManagerDelegate(
mProfileDataSourceFlag, blockGetAccountsFlag);
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(mDelegate);
ThreadUtils.runOnUiThreadBlocking(() -> {
AccountManagerFacadeProvider.setInstanceForTests(
new AccountManagerFacade(mDelegate));
});
statement.evaluate();
}
};
......
......@@ -63,7 +63,8 @@ public class AccountManagerFacadeRobolectricTest {
mDelegate = new FakeAccountManagerDelegate(
FakeAccountManagerDelegate.ENABLE_PROFILE_DATA_SOURCE);
Assert.assertFalse(mDelegate.isRegisterObserversCalled());
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(mDelegate);
// TODO(https://crbug.com/1067633): Remove AccountManagerFacadeProvider in this test
AccountManagerFacadeProvider.setInstanceForTests(new AccountManagerFacade(mDelegate));
Assert.assertTrue(mDelegate.isRegisterObserversCalled());
mFacade = AccountManagerFacadeProvider.getInstance();
}
......
......@@ -20,6 +20,7 @@ import org.chromium.base.ThreadUtils;
import org.chromium.base.test.BaseJUnit4ClassRunner;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.Feature;
import org.chromium.components.signin.AccountManagerFacade;
import org.chromium.components.signin.AccountManagerFacadeProvider;
import org.chromium.components.signin.AccountUtils;
import org.chromium.components.signin.ChromeSigninController;
......@@ -140,7 +141,10 @@ public class AndroidSyncSettingsTest {
private void setupTestAccounts() {
mAccountManager = new FakeAccountManagerDelegate(
FakeAccountManagerDelegate.DISABLE_PROFILE_DATA_SOURCE);
AccountManagerFacadeProvider.overrideAccountManagerFacadeForTests(mAccountManager);
ThreadUtils.runOnUiThreadBlocking(() -> {
AccountManagerFacadeProvider.setInstanceForTests(
new AccountManagerFacade(mAccountManager));
});
mAccount = addTestAccount("account@example.com");
mAlternateAccount = addTestAccount("alternate@example.com");
}
......
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