- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / android / javatests / src / org / chromium / chrome / browser / signin / OAuth2TokenServiceIntegrationTest.java
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.
4
5 package org.chromium.chrome.browser.signin;
6
7 import android.accounts.Account;
8 import android.test.UiThreadTest;
9 import android.test.suitebuilder.annotation.MediumTest;
10
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;
20
21 import java.util.concurrent.atomic.AtomicReference;
22
23 /**
24  * Integration test for the OAuth2TokenService.
25  *
26  * These tests initialize the native part of the service.
27  */
28 public class OAuth2TokenServiceIntegrationTest extends ChromiumTestShellTestBase {
29
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();
38
39     private AdvancedMockContext mContext;
40     private OAuth2TokenService mOAuth2TokenService;
41     private MockAccountManager mAccountManager;
42     private TestObserver mObserver;
43     private ChromeSigninController mChromeSigninController;
44
45     @Override
46     protected void setUp() throws Exception {
47         super.setUp();
48         clearAppData();
49         startChromeBrowserProcessSync(getInstrumentation().getTargetContext());
50
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);
56
57         // Get a reference to the service.
58         mOAuth2TokenService = getOAuth2TokenServiceOnUiThread();
59
60         // Set up observer.
61         mObserver = new TestObserver();
62         addObserver(mObserver);
63     }
64
65     /**
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.
68      *
69      * @return the OAuth2TokenService.
70      */
71     private static OAuth2TokenService getOAuth2TokenServiceOnUiThread() {
72         final AtomicReference<OAuth2TokenService> service =
73                 new AtomicReference<OAuth2TokenService>();
74         ThreadUtils.runOnUiThreadBlocking(new Runnable() {
75             @Override
76             public void run() {
77                 service.set(OAuth2TokenService.getForProfile(Profile.getLastUsedProfile()));
78             }
79         });
80         return service.get();
81     }
82
83     private void addObserver(final TestObserver observer) {
84         ThreadUtils.runOnUiThreadBlocking(new Runnable() {
85             @Override
86             public void run() {
87                 mOAuth2TokenService.addObserver(observer);
88             }
89         });
90     }
91
92     @MediumTest
93     @UiThreadTest
94     @Feature({"Sync"})
95     public void testFireRefreshTokenAvailableNotifiesJavaObservers() {
96         // Adding an observer should not lead to a callback.
97         assertEquals(0, mObserver.getAvailableCallCount());
98
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());
103
104         // When mOAuth2TokenService, an observer should not be called.
105         mOAuth2TokenService.removeObserver(mObserver);
106         mOAuth2TokenService.fireRefreshTokenAvailable(TEST_ACCOUNT1);
107         assertEquals(1, mObserver.getAvailableCallCount());
108
109         // No other observer interface method should ever have been called.
110         assertEquals(0, mObserver.getRevokedCallCount());
111         assertEquals(0, mObserver.getLoadedCallCount());
112     }
113
114     @MediumTest
115     @UiThreadTest
116     @Feature({"Sync"})
117     public void testFireRefreshTokenRevokedNotifiesJavaObservers() {
118         // Adding an observer should not lead to a callback.
119         assertEquals(0, mObserver.getRevokedCallCount());
120
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());
125
126         // When removed, an observer should not be called.
127         mOAuth2TokenService.removeObserver(mObserver);
128         mOAuth2TokenService.fireRefreshTokenRevoked(TEST_ACCOUNT1);
129         assertEquals(1, mObserver.getRevokedCallCount());
130
131         // No other observer interface method should ever have been called.
132         assertEquals(0, mObserver.getAvailableCallCount());
133         assertEquals(0, mObserver.getLoadedCallCount());
134     }
135
136     @MediumTest
137     @UiThreadTest
138     @Feature({"Sync"})
139     public void testFireRefreshTokensLoadedNotifiesJavaObservers() {
140         // Adding an observer should not lead to a callback.
141         assertEquals(0, mObserver.getLoadedCallCount());
142
143         // An observer should be called with the correct account.
144         mOAuth2TokenService.fireRefreshTokensLoaded();
145         assertEquals(1, mObserver.getLoadedCallCount());
146
147         // When removed, an observer should not be called.
148         mOAuth2TokenService.removeObserver(mObserver);
149         mOAuth2TokenService.fireRefreshTokensLoaded();
150         assertEquals(1, mObserver.getLoadedCallCount());
151
152         // No other observer interface method should ever have been called.
153         assertEquals(0, mObserver.getAvailableCallCount());
154         assertEquals(0, mObserver.getRevokedCallCount());
155     }
156
157     @MediumTest
158     @UiThreadTest
159     public void testValidateAccountsNoAccountsRegisteredAndNoSignedInUser() {
160         // Run test.
161         mOAuth2TokenService.validateAccounts(mContext);
162
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());
167     }
168
169     @MediumTest
170     @UiThreadTest
171     public void testValidateAccountsOneAccountsRegisteredAndNoSignedInUser() {
172         // Add account.
173         mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_1);
174
175         // Run test.
176         mOAuth2TokenService.validateAccounts(mContext);
177
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());
182     }
183
184     @MediumTest
185     @UiThreadTest
186     public void testValidateAccountsOneAccountsRegisteredSignedIn() {
187         // Add account.
188         mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_1);
189
190         // Mark user as signed in.
191         mChromeSigninController.setSignedInAccountName(TEST_ACCOUNT1.name);
192
193         // Run test.
194         mOAuth2TokenService.validateAccounts(mContext);
195
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());
200     }
201
202     @MediumTest
203     @UiThreadTest
204     public void testValidateAccountsTwoAccountsRegisteredAndOneSignedIn() {
205         // Add accounts.
206         mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_1);
207         mAccountManager.addAccountHolderExplicitly(TEST_ACCOUNT_HOLDER_2);
208
209         // Mark user as signed in.
210         mChromeSigninController.setSignedInAccountName(TEST_ACCOUNT1.name);
211
212         // Run test.
213         mOAuth2TokenService.validateAccounts(mContext);
214
215         // Ensure no calls have been made to the observer.
216         assertEquals(1, mObserver.getAvailableCallCount());
217         assertEquals(0, mObserver.getRevokedCallCount());
218         assertEquals(0, mObserver.getLoadedCallCount());
219     }
220
221     @MediumTest
222     @UiThreadTest
223     public void testValidateAccountsNoAccountsRegisteredButSignedIn() {
224         // Mark user as signed in without setting up the account.
225         mChromeSigninController.setSignedInAccountName(TEST_ACCOUNT1.name);
226
227         // Run test.
228         mOAuth2TokenService.validateAccounts(mContext);
229
230         // Ensure no calls have been made to the observer.
231         assertEquals(0, mObserver.getAvailableCallCount());
232         assertEquals(1, mObserver.getRevokedCallCount());
233         assertEquals(0, mObserver.getLoadedCallCount());
234     }
235
236     private static class TestObserver implements OAuth2TokenService.OAuth2TokenServiceObserver {
237         private int mAvailableCallCount;
238         private int mRevokedCallCount;
239         private int mLoadedCallCount;
240         private Account mLastAccount;
241
242         @Override
243         public void onRefreshTokenAvailable(Account account) {
244             mAvailableCallCount++;
245             mLastAccount = account;
246         }
247
248         @Override
249         public void onRefreshTokenRevoked(Account account) {
250             mRevokedCallCount++;
251             mLastAccount = account;
252         }
253
254         @Override
255         public void onRefreshTokensLoaded() {
256             mLoadedCallCount++;
257         }
258
259         public int getAvailableCallCount() {
260             return mAvailableCallCount;
261         }
262
263         public int getRevokedCallCount() {
264             return mRevokedCallCount;
265         }
266
267         public int getLoadedCallCount() {
268             return mLoadedCallCount;
269         }
270
271         public Account getLastAccount() {
272             return mLastAccount;
273         }
274     }
275 }