This source file includes following definitions.
- setUp
- getOAuth2TokenServiceOnUiThread
- addObserver
- Feature
- testFireRefreshTokenAvailableNotifiesJavaObservers
- Feature
- testFireRefreshTokenRevokedNotifiesJavaObservers
- Feature
- testFireRefreshTokensLoadedNotifiesJavaObservers
- testValidateAccountsNoAccountsRegisteredAndNoSignedInUser
- testValidateAccountsOneAccountsRegisteredAndNoSignedInUser
- testValidateAccountsOneAccountsRegisteredSignedIn
- testValidateAccountsTwoAccountsRegisteredAndOneSignedIn
- testValidateAccountsNoAccountsRegisteredButSignedIn
- onRefreshTokenAvailable
- onRefreshTokenRevoked
- onRefreshTokensLoaded
- getAvailableCallCount
- getRevokedCallCount
- getLoadedCallCount
- getLastAccount
package org.chromium.chrome.browser.signin;
import android.accounts.Account;
import android.test.UiThreadTest;
import android.test.suitebuilder.annotation.MediumTest;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.shell.ChromeShellTestBase;
import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.signin.ChromeSigninController;
import org.chromium.sync.test.util.AccountHolder;
import org.chromium.sync.test.util.MockAccountManager;
import java.util.concurrent.atomic.AtomicReference;
public class OAuth2TokenServiceIntegrationTest extends ChromeShellTestBase {
private static final Account TEST_ACCOUNT1 =
AccountManagerHelper.createAccountFromName("foo@gmail.com");
private static final Account TEST_ACCOUNT2 =
AccountManagerHelper.createAccountFromName("bar@gmail.com");
private static final AccountHolder TEST_ACCOUNT_HOLDER_1 =
AccountHolder.create().account(TEST_ACCOUNT1).build();
private static final AccountHolder TEST_ACCOUNT_HOLDER_2 =
AccountHolder.create().account(TEST_ACCOUNT2).build();
private AdvancedMockContext mContext;
private OAuth2TokenService mOAuth2TokenService;
private MockAccountManager mAccountManager;
private TestObserver mObserver;
private ChromeSigninController mChromeSigninController;
@Override
protected void setUp() throws Exception {
super.setUp();
clearAppData();
startChromeBrowserProcessSync(getInstrumentation().getTargetContext());
mContext = new AdvancedMockContext(getInstrumentation().getTargetContext());
mAccountManager = new MockAccountManager(mContext, getInstrumentation().getContext());
AccountManagerHelper.overrideAccountManagerHelperForTests(mContext, mAccountManager);
mChromeSigninController = ChromeSigninController.get(mContext);
mOAuth2TokenService = getOAuth2TokenServiceOnUiThread();
mObserver = new TestObserver();
addObserver(mObserver);
}
private static OAuth2TokenService getOAuth2TokenServiceOnUiThread() {
final AtomicReference<OAuth2TokenService> service =
new AtomicReference<OAuth2TokenService>();
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
service.set(OAuth2TokenService.getForProfile(Profile.getLastUsedProfile()));
}
});
return service.get();
}
private void addObserver(final TestObserver observer) {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
mOAuth2TokenService.addObserver(observer);
}
});
}
@MediumTest
@UiThreadTest
@Feature({"Sync"})
public void testFireRefreshTokenAvailableNotifiesJavaObservers() {
assertEquals(0, mObserver.getAvailableCallCount());
mOAuth2TokenService.fireRefreshTokenAvailable(TEST_ACCOUNT1);
assertEquals(1, mObserver.getAvailableCallCount());
assertEquals(TEST_ACCOUNT1, mObserver.getLastAccount());
mOAuth2TokenService.removeObserver(mObserver);
mOAuth2TokenService.fireRefreshTokenAvailable(TEST_ACCOUNT1);
assertEquals(1, mObserver.getAvailableCallCount());
assertEquals(0, mObserver.getRevokedCallCount());
assertEquals(0, mObserver.getLoadedCallCount());
}
@MediumTest
@UiThreadTest
@Feature({"Sync"})
public void testFireRefreshTokenRevokedNotifiesJavaObservers() {
assertEquals(0, mObserver.getRevokedCallCount());
mOAuth2TokenService.fireRefreshTokenRevoked(TEST_ACCOUNT1);
assertEquals(1, mObserver.getRevokedCallCount());
assertEquals(TEST_ACCOUNT1, mObserver.getLastAccount());
mOAuth2TokenService.removeObserver(mObserver);
mOAuth2TokenService.fireRefreshTokenRevoked(TEST_ACCOUNT1);
assertEquals(1, mObserver.getRevokedCallCount());
assertEquals(0, mObserver.getAvailableCallCount());
assertEquals(0, mObserver.getLoadedCallCount());
}
@MediumTest
@UiThreadTest
@Feature({"Sync"})
public void testFireRefreshTokensLoadedNotifiesJavaObservers() {
assertEquals(0, mObserver.getLoadedCallCount());
mOAuth2TokenService.fireRefreshTokensLoaded();
assertEquals(1, mObserver.getLoadedCallCount());
mOAuth2TokenService.removeObserver(mObserver);
mOAuth2TokenService.fireRefreshTokensLoaded();
assertEquals(1, mObserver.getLoadedCallCount());
assertEquals(0, mObserver.getAvailableCallCount());
assertEquals(0, mObserver.getRevokedCallCount());
}
@MediumTest
@UiThreadTest
public void testValidateAccountsNoAccountsRegisteredAndNoSignedInUser() {
mOAuth2TokenService.validateAccounts(mContext);
assertEquals(0, mObserver.getAvailableCallCount());
assertEquals(0, mObserver.getRevokedCallCount());
assertEquals(0, mObserver.getLoadedCallCount());
}
@MediumTest
@UiThreadTest
public void testValidateAccountsOneAccountsRegisteredAndNoSignedInUser() {
mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_1);
mOAuth2TokenService.validateAccounts(mContext);
assertEquals(0, mObserver.getAvailableCallCount());
assertEquals(0, mObserver.getRevokedCallCount());
assertEquals(0, mObserver.getLoadedCallCount());
}
@MediumTest
@UiThreadTest
public void testValidateAccountsOneAccountsRegisteredSignedIn() {
mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_1);
mChromeSigninController.setSignedInAccountName(TEST_ACCOUNT1.name);
mOAuth2TokenService.validateAccounts(mContext);
assertEquals(1, mObserver.getAvailableCallCount());
assertEquals(0, mObserver.getRevokedCallCount());
assertEquals(0, mObserver.getLoadedCallCount());
}
@MediumTest
@UiThreadTest
public void testValidateAccountsTwoAccountsRegisteredAndOneSignedIn() {
mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_1);
mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_2);
mChromeSigninController.setSignedInAccountName(TEST_ACCOUNT1.name);
mOAuth2TokenService.validateAccounts(mContext);
assertEquals(2, mObserver.getAvailableCallCount());
assertEquals(0, mObserver.getRevokedCallCount());
assertEquals(0, mObserver.getLoadedCallCount());
}
@MediumTest
@UiThreadTest
public void testValidateAccountsNoAccountsRegisteredButSignedIn() {
mChromeSigninController.setSignedInAccountName(TEST_ACCOUNT1.name);
mOAuth2TokenService.validateAccounts(mContext);
assertEquals(0, mObserver.getAvailableCallCount());
assertEquals(1, mObserver.getRevokedCallCount());
assertEquals(0, mObserver.getLoadedCallCount());
}
private static class TestObserver implements OAuth2TokenService.OAuth2TokenServiceObserver {
private int mAvailableCallCount;
private int mRevokedCallCount;
private int mLoadedCallCount;
private Account mLastAccount;
@Override
public void onRefreshTokenAvailable(Account account) {
mAvailableCallCount++;
mLastAccount = account;
}
@Override
public void onRefreshTokenRevoked(Account account) {
mRevokedCallCount++;
mLastAccount = account;
}
@Override
public void onRefreshTokensLoaded() {
mLoadedCallCount++;
}
public int getAvailableCallCount() {
return mAvailableCallCount;
}
public int getRevokedCallCount() {
return mRevokedCallCount;
}
public int getLoadedCallCount() {
return mLoadedCallCount;
}
public Account getLastAccount() {
return mLastAccount;
}
}
}