Commit 76c8ca54 authored by Alice Wang's avatar Alice Wang Committed by Commit Bot

[Android][Test] Refactor AccountPickerDelegateTest

This CL refactors AccountPickerDelegateTest by moving some common part
to the test setup.

Bug: 1121536
Change-Id: I758ed411a0e9e91129e917d214a3ad4e0afb3e38
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2444313Reviewed-by: default avatarTanmoy Mollik <triploblastic@chromium.org>
Commit-Queue: Alice Wang <aliceywang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813260}
parent e16cd9fc
...@@ -93,6 +93,8 @@ public class AccountPickerDelegateTest { ...@@ -93,6 +93,8 @@ public class AccountPickerDelegateTest {
private AccountPickerDelegate mDelegate; private AccountPickerDelegate mDelegate;
private CoreAccountInfo mCoreAccountInfo;
@Before @Before
public void setUp() { public void setUp() {
initMocks(this); initMocks(this);
...@@ -105,6 +107,10 @@ public class AccountPickerDelegateTest { ...@@ -105,6 +107,10 @@ public class AccountPickerDelegateTest {
.thenReturn(mIdentityManagerMock); .thenReturn(mIdentityManagerMock);
when(IdentityServicesProvider.get().getSigninManager(any())).thenReturn(mSigninManagerMock); when(IdentityServicesProvider.get().getSigninManager(any())).thenReturn(mSigninManagerMock);
Account account =
mAccountManagerTestRule.addAccount(AccountManagerTestRule.TEST_ACCOUNT_EMAIL);
mCoreAccountInfo = mAccountManagerTestRule.toCoreAccountInfo(account.name);
mDelegate = new AccountPickerDelegate( mDelegate = new AccountPickerDelegate(
mWindowAndroidMock, mTabMock, mWebSigninBridgeFactoryMock, CONTINUE_URL); mWindowAndroidMock, mTabMock, mWebSigninBridgeFactoryMock, CONTINUE_URL);
when(mWebSigninBridgeFactoryMock.create(eq(mProfileMock), any(), eq(mDelegate))) when(mWebSigninBridgeFactoryMock.create(eq(mProfileMock), any(), eq(mDelegate)))
...@@ -118,14 +124,11 @@ public class AccountPickerDelegateTest { ...@@ -118,14 +124,11 @@ public class AccountPickerDelegateTest {
@Test @Test
public void testSignInSucceeded() { public void testSignInSucceeded() {
Account account = mDelegate.signIn(mCoreAccountInfo, error -> {});
mAccountManagerTestRule.addAccount(AccountManagerTestRule.TEST_ACCOUNT_EMAIL);
CoreAccountInfo coreAccountInfo = mAccountManagerTestRule.toCoreAccountInfo(account.name);
mDelegate.signIn(coreAccountInfo, error -> {});
InOrder calledInOrder = inOrder(mWebSigninBridgeFactoryMock, mSigninManagerMock); InOrder calledInOrder = inOrder(mWebSigninBridgeFactoryMock, mSigninManagerMock);
calledInOrder.verify(mWebSigninBridgeFactoryMock) calledInOrder.verify(mWebSigninBridgeFactoryMock)
.create(mProfileMock, coreAccountInfo, mDelegate); .create(mProfileMock, mCoreAccountInfo, mDelegate);
calledInOrder.verify(mSigninManagerMock).signin(eq(coreAccountInfo), any()); calledInOrder.verify(mSigninManagerMock).signin(eq(mCoreAccountInfo), any());
mDelegate.onSigninSucceeded(); mDelegate.onSigninSucceeded();
verify(mTabMock).loadUrl(mLoadUrlParamsCaptor.capture()); verify(mTabMock).loadUrl(mLoadUrlParamsCaptor.capture());
LoadUrlParams loadUrlParams = mLoadUrlParamsCaptor.getValue(); LoadUrlParams loadUrlParams = mLoadUrlParamsCaptor.getValue();
...@@ -134,17 +137,14 @@ public class AccountPickerDelegateTest { ...@@ -134,17 +137,14 @@ public class AccountPickerDelegateTest {
@Test @Test
public void testSignInAborted() { public void testSignInAborted() {
Account account =
mAccountManagerTestRule.addAccount(AccountManagerTestRule.TEST_ACCOUNT_EMAIL);
CoreAccountInfo coreAccountInfo = mAccountManagerTestRule.toCoreAccountInfo(account.name);
doAnswer(invocation -> { doAnswer(invocation -> {
SigninManager.SignInCallback callback = invocation.getArgument(1); SigninManager.SignInCallback callback = invocation.getArgument(1);
callback.onSignInAborted(); callback.onSignInAborted();
return null; return null;
}) })
.when(mSigninManagerMock) .when(mSigninManagerMock)
.signin(eq(coreAccountInfo), any()); .signin(eq(mCoreAccountInfo), any());
mDelegate.signIn(coreAccountInfo, error -> {}); mDelegate.signIn(mCoreAccountInfo, error -> {});
verify(mWebSigninBridgeMock).destroy(); verify(mWebSigninBridgeMock).destroy();
} }
...@@ -153,30 +153,24 @@ public class AccountPickerDelegateTest { ...@@ -153,30 +153,24 @@ public class AccountPickerDelegateTest {
// In case an error is fired because cookies are taking longer to generate than usual, // In case an error is fired because cookies are taking longer to generate than usual,
// if user retries the sign-in from the error screen, we need to sign out the user // if user retries the sign-in from the error screen, we need to sign out the user
// first before signing in again. // first before signing in again.
Account account = mDelegate.signIn(mCoreAccountInfo, error -> {});
mAccountManagerTestRule.addAccount(AccountManagerTestRule.TEST_ACCOUNT_EMAIL); when(mIdentityManagerMock.getPrimaryAccountInfo(anyInt())).thenReturn(mCoreAccountInfo);
CoreAccountInfo coreAccountInfo = mAccountManagerTestRule.toCoreAccountInfo(account.name);
mDelegate.signIn(coreAccountInfo, error -> {});
when(mIdentityManagerMock.getPrimaryAccountInfo(anyInt())).thenReturn(coreAccountInfo);
mDelegate.signIn(coreAccountInfo, error -> {}); mDelegate.signIn(mCoreAccountInfo, error -> {});
InOrder calledInOrder = inOrder(mWebSigninBridgeMock, mSigninManagerMock, InOrder calledInOrder = inOrder(mWebSigninBridgeMock, mSigninManagerMock,
mWebSigninBridgeFactoryMock, mSigninManagerMock); mWebSigninBridgeFactoryMock, mSigninManagerMock);
calledInOrder.verify(mWebSigninBridgeMock).destroy(); calledInOrder.verify(mWebSigninBridgeMock).destroy();
calledInOrder.verify(mSigninManagerMock).signOut(anyInt()); calledInOrder.verify(mSigninManagerMock).signOut(anyInt());
calledInOrder.verify(mWebSigninBridgeFactoryMock) calledInOrder.verify(mWebSigninBridgeFactoryMock)
.create(mProfileMock, coreAccountInfo, mDelegate); .create(mProfileMock, mCoreAccountInfo, mDelegate);
calledInOrder.verify(mSigninManagerMock).signin(eq(coreAccountInfo), any()); calledInOrder.verify(mSigninManagerMock).signin(eq(mCoreAccountInfo), any());
} }
@Test @Test
public void testSignInFailedWithConnectionError() { public void testSignInFailedWithConnectionError() {
Account account =
mAccountManagerTestRule.addAccount(AccountManagerTestRule.TEST_ACCOUNT_EMAIL);
CoreAccountInfo coreAccountInfo = mAccountManagerTestRule.toCoreAccountInfo(account.name);
Callback<GoogleServiceAuthError> mockCallback = mock(Callback.class); Callback<GoogleServiceAuthError> mockCallback = mock(Callback.class);
GoogleServiceAuthError error = new GoogleServiceAuthError(State.CONNECTION_FAILED); GoogleServiceAuthError error = new GoogleServiceAuthError(State.CONNECTION_FAILED);
mDelegate.signIn(coreAccountInfo, mockCallback); mDelegate.signIn(mCoreAccountInfo, mockCallback);
mDelegate.onSigninFailed(error); mDelegate.onSigninFailed(error);
verify(mockCallback).onResult(error); verify(mockCallback).onResult(error);
// WebSigninBridge should be kept alive in case cookies are taking longer to // WebSigninBridge should be kept alive in case cookies are taking longer to
......
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