1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chrome.browser.signin;
7 import android.accounts.Account;
8 import android.test.UiThreadTest;
9 import android.test.suitebuilder.annotation.MediumTest;
11 import org.chromium.base.ThreadUtils;
12 import org.chromium.base.test.util.AdvancedMockContext;
13 import org.chromium.base.test.util.Feature;
14 import org.chromium.chrome.browser.profiles.Profile;
15 import org.chromium.chrome.testshell.ChromiumTestShellTestBase;
16 import org.chromium.sync.signin.AccountManagerHelper;
17 import org.chromium.sync.signin.ChromeSigninController;
18 import org.chromium.sync.test.util.AccountHolder;
19 import org.chromium.sync.test.util.MockAccountManager;
21 import java.util.concurrent.atomic.AtomicReference;
24 * Integration test for the OAuth2TokenService.
26 * These tests initialize the native part of the service.
28 public class OAuth2TokenServiceIntegrationTest extends ChromiumTestShellTestBase {
30 private static final Account TEST_ACCOUNT1 =
31 AccountManagerHelper.createAccountFromName("foo@gmail.com");
32 private static final Account TEST_ACCOUNT2 =
33 AccountManagerHelper.createAccountFromName("bar@gmail.com");
34 private static final AccountHolder TEST_ACCOUNT_HOLDER_1 =
35 AccountHolder.create().account(TEST_ACCOUNT1).build();
36 private static final AccountHolder TEST_ACCOUNT_HOLDER_2 =
37 AccountHolder.create().account(TEST_ACCOUNT2).build();
39 private AdvancedMockContext mContext;
40 private OAuth2TokenService mOAuth2TokenService;
41 private MockAccountManager mAccountManager;
42 private TestObserver mObserver;
43 private ChromeSigninController mChromeSigninController;
46 protected void setUp() throws Exception {
49 startChromeBrowserProcessSync(getInstrumentation().getTargetContext());
51 // Set up AccountManager.
52 mContext = new AdvancedMockContext(getInstrumentation().getTargetContext());
53 mAccountManager = new MockAccountManager(mContext, getInstrumentation().getContext());
54 AccountManagerHelper.overrideAccountManagerHelperForTests(mContext, mAccountManager);
55 mChromeSigninController = ChromeSigninController.get(mContext);
57 // Get a reference to the service.
58 mOAuth2TokenService = getOAuth2TokenServiceOnUiThread();
61 mObserver = new TestObserver();
62 addObserver(mObserver);
66 * The {@link OAuth2TokenService} and the {@link Profile} can only be accessed from the UI
67 * thread, so this helper method is a convenience method to retrieve it.
69 * @return the OAuth2TokenService.
71 private static OAuth2TokenService getOAuth2TokenServiceOnUiThread() {
72 final AtomicReference<OAuth2TokenService> service =
73 new AtomicReference<OAuth2TokenService>();
74 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
77 service.set(OAuth2TokenService.getForProfile(Profile.getLastUsedProfile()));
83 private void addObserver(final TestObserver observer) {
84 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
87 mOAuth2TokenService.addObserver(observer);
95 public void testFireRefreshTokenAvailableNotifiesJavaObservers() {
96 // Adding an observer should not lead to a callback.
97 assertEquals(0, mObserver.getAvailableCallCount());
99 // An observer should be called with the correct account.
100 mOAuth2TokenService.fireRefreshTokenAvailable(TEST_ACCOUNT1);
101 assertEquals(1, mObserver.getAvailableCallCount());
102 assertEquals(TEST_ACCOUNT1, mObserver.getLastAccount());
104 // When mOAuth2TokenService, an observer should not be called.
105 mOAuth2TokenService.removeObserver(mObserver);
106 mOAuth2TokenService.fireRefreshTokenAvailable(TEST_ACCOUNT1);
107 assertEquals(1, mObserver.getAvailableCallCount());
109 // No other observer interface method should ever have been called.
110 assertEquals(0, mObserver.getRevokedCallCount());
111 assertEquals(0, mObserver.getLoadedCallCount());
117 public void testFireRefreshTokenRevokedNotifiesJavaObservers() {
118 // Adding an observer should not lead to a callback.
119 assertEquals(0, mObserver.getRevokedCallCount());
121 // An observer should be called with the correct account.
122 mOAuth2TokenService.fireRefreshTokenRevoked(TEST_ACCOUNT1);
123 assertEquals(1, mObserver.getRevokedCallCount());
124 assertEquals(TEST_ACCOUNT1, mObserver.getLastAccount());
126 // When removed, an observer should not be called.
127 mOAuth2TokenService.removeObserver(mObserver);
128 mOAuth2TokenService.fireRefreshTokenRevoked(TEST_ACCOUNT1);
129 assertEquals(1, mObserver.getRevokedCallCount());
131 // No other observer interface method should ever have been called.
132 assertEquals(0, mObserver.getAvailableCallCount());
133 assertEquals(0, mObserver.getLoadedCallCount());
139 public void testFireRefreshTokensLoadedNotifiesJavaObservers() {
140 // Adding an observer should not lead to a callback.
141 assertEquals(0, mObserver.getLoadedCallCount());
143 // An observer should be called with the correct account.
144 mOAuth2TokenService.fireRefreshTokensLoaded();
145 assertEquals(1, mObserver.getLoadedCallCount());
147 // When removed, an observer should not be called.
148 mOAuth2TokenService.removeObserver(mObserver);
149 mOAuth2TokenService.fireRefreshTokensLoaded();
150 assertEquals(1, mObserver.getLoadedCallCount());
152 // No other observer interface method should ever have been called.
153 assertEquals(0, mObserver.getAvailableCallCount());
154 assertEquals(0, mObserver.getRevokedCallCount());
159 public void testValidateAccountsNoAccountsRegisteredAndNoSignedInUser() {
161 mOAuth2TokenService.validateAccounts(mContext);
163 // Ensure no calls have been made to the observer.
164 assertEquals(0, mObserver.getAvailableCallCount());
165 assertEquals(0, mObserver.getRevokedCallCount());
166 assertEquals(0, mObserver.getLoadedCallCount());
171 public void testValidateAccountsOneAccountsRegisteredAndNoSignedInUser() {
173 mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_1);
176 mOAuth2TokenService.validateAccounts(mContext);
178 // Ensure no calls have been made to the observer.
179 assertEquals(0, mObserver.getAvailableCallCount());
180 assertEquals(0, mObserver.getRevokedCallCount());
181 assertEquals(0, mObserver.getLoadedCallCount());
186 public void testValidateAccountsOneAccountsRegisteredSignedIn() {
188 mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_1);
190 // Mark user as signed in.
191 mChromeSigninController.setSignedInAccountName(TEST_ACCOUNT1.name);
194 mOAuth2TokenService.validateAccounts(mContext);
196 // Ensure no calls have been made to the observer.
197 assertEquals(1, mObserver.getAvailableCallCount());
198 assertEquals(0, mObserver.getRevokedCallCount());
199 assertEquals(0, mObserver.getLoadedCallCount());
204 public void testValidateAccountsTwoAccountsRegisteredAndOneSignedIn() {
206 mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_1);
207 mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_2);
209 // Mark user as signed in.
210 mChromeSigninController.setSignedInAccountName(TEST_ACCOUNT1.name);
213 mOAuth2TokenService.validateAccounts(mContext);
215 // All accounts will be notified. It is up to the observer
216 // to design if any action is needed.
217 assertEquals(2, mObserver.getAvailableCallCount());
218 assertEquals(0, mObserver.getRevokedCallCount());
219 assertEquals(0, mObserver.getLoadedCallCount());
224 public void testValidateAccountsNoAccountsRegisteredButSignedIn() {
225 // Mark user as signed in without setting up the account.
226 mChromeSigninController.setSignedInAccountName(TEST_ACCOUNT1.name);
229 mOAuth2TokenService.validateAccounts(mContext);
231 // Ensure no calls have been made to the observer.
232 assertEquals(0, mObserver.getAvailableCallCount());
233 assertEquals(1, mObserver.getRevokedCallCount());
234 assertEquals(0, mObserver.getLoadedCallCount());
237 private static class TestObserver implements OAuth2TokenService.OAuth2TokenServiceObserver {
238 private int mAvailableCallCount;
239 private int mRevokedCallCount;
240 private int mLoadedCallCount;
241 private Account mLastAccount;
244 public void onRefreshTokenAvailable(Account account) {
245 mAvailableCallCount++;
246 mLastAccount = account;
250 public void onRefreshTokenRevoked(Account account) {
252 mLastAccount = account;
256 public void onRefreshTokensLoaded() {
260 public int getAvailableCallCount() {
261 return mAvailableCallCount;
264 public int getRevokedCallCount() {
265 return mRevokedCallCount;
268 public int getLoadedCallCount() {
269 return mLoadedCallCount;
272 public Account getLastAccount() {