This source file includes following definitions.
- setUp
- Feature
- testGetAccountsNoAccountsRegistered
- Feature
- testGetAccountsOneAccountRegistered
- Feature
- testGetAccountsTwoAccountsRegistered
- Feature
- testGetOAuth2AccessTokenWithTimeoutOnSuccess
- Feature
- testGetOAuth2AccessTokenWithTimeoutOnError
- runTestOfGetOAuth2AccessTokenWithTimeout
package org.chromium.chrome.browser.signin;
import android.accounts.Account;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.Feature;
import org.chromium.sync.signin.AccountManagerHelper;
import org.chromium.sync.test.util.AccountHolder;
import org.chromium.sync.test.util.MockAccountManager;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
public class OAuth2TokenServiceTest extends InstrumentationTestCase {
private AdvancedMockContext mContext;
private MockAccountManager mAccountManager;
@Override
protected void setUp() throws Exception {
super.setUp();
mContext = new AdvancedMockContext(getInstrumentation().getTargetContext());
mAccountManager = new MockAccountManager(mContext, getInstrumentation().getContext());
AccountManagerHelper.overrideAccountManagerHelperForTests(mContext, mAccountManager);
}
@SmallTest
@Feature({"Sync"})
public void testGetAccountsNoAccountsRegistered() {
String[] accounts = OAuth2TokenService.getAccounts(mContext);
assertEquals("There should be no accounts registered", 0, accounts.length);
}
@SmallTest
@Feature({"Sync"})
public void testGetAccountsOneAccountRegistered() {
Account account1 = AccountManagerHelper.createAccountFromName("foo@gmail.com");
AccountHolder accountHolder1 = AccountHolder.create().account(account1).build();
mAccountManager.addAccountHolderExplicitly(accountHolder1);
String[] accounts = OAuth2TokenService.getAccounts(mContext);
assertEquals("There should be one registered account", 1, accounts.length);
assertEquals("The account should be " + account1, account1.name, accounts[0]);
}
@SmallTest
@Feature({"Sync"})
public void testGetAccountsTwoAccountsRegistered() {
Account account1 = AccountManagerHelper.createAccountFromName("foo@gmail.com");
AccountHolder accountHolder1 = AccountHolder.create().account(account1).build();
mAccountManager.addAccountHolderExplicitly(accountHolder1);
Account account2 = AccountManagerHelper.createAccountFromName("bar@gmail.com");
AccountHolder accountHolder2 = AccountHolder.create().account(account2).build();
mAccountManager.addAccountHolderExplicitly(accountHolder2);
String[] accounts = OAuth2TokenService.getAccounts(mContext);
assertEquals("There should be one registered account", 2, accounts.length);
assertTrue("The list should contain " + account1,
Arrays.asList(accounts).contains(account1.name));
assertTrue("The list should contain " + account2,
Arrays.asList(accounts).contains(account2.name));
}
@SmallTest
@Feature({"Sync"})
public void testGetOAuth2AccessTokenWithTimeoutOnSuccess() {
String authToken = "someToken";
runTestOfGetOAuth2AccessTokenWithTimeout(authToken);
}
@SmallTest
@Feature({"Sync"})
public void testGetOAuth2AccessTokenWithTimeoutOnError() {
String authToken = null;
runTestOfGetOAuth2AccessTokenWithTimeout(authToken);
}
private void runTestOfGetOAuth2AccessTokenWithTimeout(String expectedToken) {
String scope = "http://example.com/scope";
Account account = AccountManagerHelper.createAccountFromName("test@gmail.com");
String oauth2Scope = "oauth2:" + scope;
AccountHolder accountHolder =
AccountHolder.create()
.account(account)
.hasBeenAccepted(oauth2Scope, true)
.authToken(oauth2Scope, expectedToken).build();
mAccountManager.addAccountHolderExplicitly(accountHolder);
String accessToken =
OAuth2TokenService.getOAuth2AccessTokenWithTimeout(
mContext, null, account, scope, 5, TimeUnit.SECONDS);
assertEquals(expectedToken, accessToken);
}
}