Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / account_reconcilor_unittest.cc
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 #include "base/memory/scoped_ptr.h"
6 #include "base/run_loop.h"
7 #include "base/time/time.h"
8 #include "chrome/browser/signin/account_reconcilor.h"
9 #include "chrome/browser/signin/account_reconcilor_factory.h"
10 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
11 #include "chrome/browser/signin/fake_profile_oauth2_token_service_wrapper.h"
12 #include "chrome/browser/signin/fake_signin_manager.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "chrome/browser/signin/signin_manager.h"
16 #include "chrome/browser/signin/signin_manager_factory.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "google_apis/gaia/gaia_urls.h"
20 #include "net/url_request/test_url_fetcher_factory.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace {
25
26 const char kTestEmail[] = "user@gmail.com";
27
28 class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> {
29  public:
30   static BrowserContextKeyedService* Build(content::BrowserContext* profile);
31
32   explicit MockAccountReconcilor(Profile* profile);
33   virtual ~MockAccountReconcilor() {}
34
35   MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id));
36   MOCK_METHOD1(StartRemoveAction, void(const std::string& account_id));
37   MOCK_METHOD3(
38       FinishRemoveAction,
39       void(const std::string& account_id,
40            const GoogleServiceAuthError& error,
41            const std::vector<std::pair<std::string, bool> >& accounts));
42   MOCK_METHOD2(PerformAddToChromeAction, void(const std::string& account_id,
43                                               int session_index));
44   MOCK_METHOD0(PerformLogoutAllAccountsAction, void());
45 };
46
47 // static
48 BrowserContextKeyedService* MockAccountReconcilor::Build(
49     content::BrowserContext* profile) {
50   return new MockAccountReconcilor(static_cast<Profile*>(profile));
51 }
52
53 MockAccountReconcilor::MockAccountReconcilor(Profile* profile)
54     : testing::StrictMock<AccountReconcilor>(profile) {
55 }
56
57 }  // namespace
58
59 class AccountReconcilorTest : public testing::Test {
60  public:
61   AccountReconcilorTest();
62   virtual void SetUp() OVERRIDE;
63   virtual void TearDown() OVERRIDE;
64
65   TestingProfile* profile() { return profile_.get(); }
66   FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
67   FakeProfileOAuth2TokenService* token_service() { return token_service_; }
68
69   void SetFakeResponse(const std::string& url,
70                        const std::string& data,
71                        net::HttpStatusCode code,
72                        net::URLRequestStatus::Status status) {
73     url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status);
74   }
75
76   MockAccountReconcilor* GetMockReconcilor();
77
78   void SimulateMergeSessionCompleted(
79       MergeSessionHelper::Observer* observer,
80       const std::string& account_id,
81       const GoogleServiceAuthError& error);
82
83   void SimulateRefreshTokenFetched(
84       AccountReconcilor* reconcilor,
85       const std::string& account_id,
86       const std::string& refresh_token);
87
88 private:
89   content::TestBrowserThreadBundle bundle_;
90   scoped_ptr<TestingProfile> profile_;
91   FakeSigninManagerForTesting* signin_manager_;
92   FakeProfileOAuth2TokenService* token_service_;
93   MockAccountReconcilor* mock_reconcilor_;
94   net::FakeURLFetcherFactory url_fetcher_factory_;
95 };
96
97 AccountReconcilorTest::AccountReconcilorTest()
98     : signin_manager_(NULL),
99       token_service_(NULL),
100       mock_reconcilor_(NULL),
101       url_fetcher_factory_(NULL) {}
102
103 void AccountReconcilorTest::SetUp() {
104   TestingProfile::Builder builder;
105   builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
106                             FakeProfileOAuth2TokenServiceWrapper::Build);
107   builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
108                             FakeSigninManagerBase::Build);
109   builder.AddTestingFactory(AccountReconcilorFactory::GetInstance(),
110                             MockAccountReconcilor::Build);
111   profile_ = builder.Build();
112
113   signin_manager_ =
114       static_cast<FakeSigninManagerForTesting*>(
115           SigninManagerFactory::GetForProfile(profile()));
116
117   token_service_ =
118       static_cast<FakeProfileOAuth2TokenService*>(
119           ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
120 }
121
122 void AccountReconcilorTest::TearDown() {
123   // Destroy the profile before all threads are torn down.
124   profile_.reset();
125 }
126
127 MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() {
128   if (!mock_reconcilor_) {
129     mock_reconcilor_ =
130         static_cast<MockAccountReconcilor*>(
131             AccountReconcilorFactory::GetForProfile(profile()));
132   }
133
134   return mock_reconcilor_;
135 }
136
137 void AccountReconcilorTest::SimulateMergeSessionCompleted(
138     MergeSessionHelper::Observer* observer,
139     const std::string& account_id,
140     const GoogleServiceAuthError& error) {
141   observer->MergeSessionCompleted(account_id, error);
142 }
143
144 void AccountReconcilorTest::SimulateRefreshTokenFetched(
145     AccountReconcilor* reconcilor,
146     const std::string& account_id,
147     const std::string& refresh_token) {
148   reconcilor->HandleRefreshTokenFetched(account_id, refresh_token);
149 }
150
151 TEST_F(AccountReconcilorTest, Basic) {
152   AccountReconcilor* reconcilor =
153       AccountReconcilorFactory::GetForProfile(profile());
154   ASSERT_TRUE(reconcilor);
155   ASSERT_EQ(profile(), reconcilor->profile());
156 }
157
158 #if !defined(OS_CHROMEOS)
159
160 TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
161   AccountReconcilor* reconcilor =
162       AccountReconcilorFactory::GetForProfile(profile());
163   ASSERT_TRUE(reconcilor);
164   ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
165   ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
166
167   signin_manager()->OnExternalSigninCompleted(kTestEmail);
168   ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
169   ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
170
171   signin_manager()->SignOut();
172   ASSERT_FALSE(reconcilor->IsPeriodicReconciliationRunning());
173   ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
174 }
175
176 TEST_F(AccountReconcilorTest, Reauth) {
177   signin_manager()->SetAuthenticatedUsername(kTestEmail);
178
179   AccountReconcilor* reconcilor =
180       AccountReconcilorFactory::GetForProfile(profile());
181   ASSERT_TRUE(reconcilor);
182   ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
183   ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
184
185   // Simulate reauth.  The state of the reconcilor should not change.
186   signin_manager()->OnExternalSigninCompleted(kTestEmail);
187   ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
188   ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
189 }
190
191 #endif  // !defined(OS_CHROMEOS)
192
193 TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
194   signin_manager()->SetAuthenticatedUsername(kTestEmail);
195
196   AccountReconcilor* reconcilor =
197       AccountReconcilorFactory::GetForProfile(profile());
198   ASSERT_TRUE(reconcilor);
199   ASSERT_TRUE(reconcilor->IsPeriodicReconciliationRunning());
200   ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
201 }
202
203 TEST_F(AccountReconcilorTest, GetAccountsFromCookieSuccess) {
204   signin_manager()->SetAuthenticatedUsername(kTestEmail);
205   token_service()->UpdateCredentials(kTestEmail, "refresh_token");
206   AccountReconcilor* reconcilor =
207       AccountReconcilorFactory::GetForProfile(profile());
208   ASSERT_TRUE(reconcilor);
209
210   SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
211       "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
212       net::HTTP_OK, net::URLRequestStatus::SUCCESS);
213
214   reconcilor->StartReconcile();
215   ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
216
217   base::RunLoop().RunUntilIdle();
218   ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
219   const std::vector<std::pair<std::string, bool> >& accounts =
220       reconcilor->GetGaiaAccountsForTesting();
221   ASSERT_EQ(1u, accounts.size());
222   ASSERT_EQ("user@gmail.com", accounts[0].first);
223 }
224
225 TEST_F(AccountReconcilorTest, GetAccountsFromCookieFailure) {
226   signin_manager()->SetAuthenticatedUsername(kTestEmail);
227   token_service()->UpdateCredentials(kTestEmail, "refresh_token");
228   AccountReconcilor* reconcilor =
229       AccountReconcilorFactory::GetForProfile(profile());
230   ASSERT_TRUE(reconcilor);
231
232   SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(), "",
233       net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
234
235   reconcilor->StartReconcile();
236   ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
237
238   base::RunLoop().RunUntilIdle();
239   ASSERT_EQ(0u, reconcilor->GetGaiaAccountsForTesting().size());
240 }
241
242 TEST_F(AccountReconcilorTest, ValidateAccountsFromTokens) {
243   signin_manager()->SetAuthenticatedUsername(kTestEmail);
244   token_service()->UpdateCredentials(kTestEmail, "refresh_token");
245
246   AccountReconcilor* reconcilor =
247       AccountReconcilorFactory::GetForProfile(profile());
248   ASSERT_TRUE(reconcilor);
249
250   reconcilor->ValidateAccountsFromTokenService();
251   ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
252
253   SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
254       "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
255   token_service()->IssueTokenForAllPendingRequests("access_token",
256       base::Time::Now() + base::TimeDelta::FromHours(1));
257
258   base::RunLoop().RunUntilIdle();
259   ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
260   ASSERT_EQ(1u, reconcilor->GetValidChromeAccountsForTesting().size());
261   ASSERT_EQ(0u, reconcilor->GetInvalidChromeAccountsForTesting().size());
262 }
263
264 TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedUserInfo) {
265   signin_manager()->SetAuthenticatedUsername(kTestEmail);
266   token_service()->UpdateCredentials(kTestEmail, "refresh_token");
267
268   AccountReconcilor* reconcilor =
269       AccountReconcilorFactory::GetForProfile(profile());
270   ASSERT_TRUE(reconcilor);
271
272   reconcilor->ValidateAccountsFromTokenService();
273   ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
274
275   SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
276       "", net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
277   token_service()->IssueTokenForAllPendingRequests("access_token",
278       base::Time::Now() + base::TimeDelta::FromHours(1));
279
280   base::RunLoop().RunUntilIdle();
281   ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
282   ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
283   ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
284 }
285
286 TEST_F(AccountReconcilorTest, ValidateAccountsFromTokensFailedTokenRequest) {
287   signin_manager()->SetAuthenticatedUsername(kTestEmail);
288   token_service()->UpdateCredentials(kTestEmail, "refresh_token");
289
290   AccountReconcilor* reconcilor =
291       AccountReconcilorFactory::GetForProfile(profile());
292   ASSERT_TRUE(reconcilor);
293
294   reconcilor->ValidateAccountsFromTokenService();
295   ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
296
297   token_service()->IssueErrorForAllPendingRequests(
298       GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
299
300   base::RunLoop().RunUntilIdle();
301   ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
302   ASSERT_EQ(0u, reconcilor->GetValidChromeAccountsForTesting().size());
303   ASSERT_EQ(1u, reconcilor->GetInvalidChromeAccountsForTesting().size());
304 }
305
306 TEST_F(AccountReconcilorTest, StartReconcileNoop) {
307   signin_manager()->SetAuthenticatedUsername(kTestEmail);
308   token_service()->UpdateCredentials(kTestEmail, "refresh_token");
309
310   AccountReconcilor* reconcilor =
311       AccountReconcilorFactory::GetForProfile(profile());
312   ASSERT_TRUE(reconcilor);
313
314   SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
315       "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
316       net::HTTP_OK, net::URLRequestStatus::SUCCESS);
317   SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
318       "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
319
320   reconcilor->StartReconcile();
321   ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
322   ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
323
324   base::RunLoop().RunUntilIdle();
325   ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
326   ASSERT_EQ(1u, reconcilor->GetGaiaAccountsForTesting().size());
327   ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
328
329   token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
330       base::Time::Now() + base::TimeDelta::FromHours(1));
331
332   base::RunLoop().RunUntilIdle();
333   ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
334   ASSERT_FALSE(reconcilor->is_reconcile_started_);
335 }
336
337 TEST_F(AccountReconcilorTest, StartReconcileNoopMultiple) {
338   signin_manager()->SetAuthenticatedUsername("user@gmail.com");
339   token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
340   token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
341
342   AccountReconcilor* reconcilor =
343       AccountReconcilorFactory::GetForProfile(profile());
344   ASSERT_TRUE(reconcilor);
345
346   SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
347       "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
348                "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
349       net::HTTP_OK, net::URLRequestStatus::SUCCESS);
350   SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
351       "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
352
353   reconcilor->StartReconcile();
354   ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
355   ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
356
357   base::RunLoop().RunUntilIdle();
358   ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
359   ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
360   ASSERT_EQ(2u, reconcilor->GetGaiaAccountsForTesting().size());
361
362   token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
363       base::Time::Now() + base::TimeDelta::FromHours(1));
364
365   base::RunLoop().RunUntilIdle();
366   ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked());
367
368   token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
369       base::Time::Now() + base::TimeDelta::FromHours(1));
370
371   base::RunLoop().RunUntilIdle();
372   ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked());
373   ASSERT_FALSE(reconcilor->is_reconcile_started_);
374 }
375
376 TEST_F(AccountReconcilorTest, StartReconcileAddToCookie) {
377   signin_manager()->SetAuthenticatedUsername("user@gmail.com");
378   token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
379   token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
380
381   EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
382
383   SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
384       "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
385       net::HTTP_OK, net::URLRequestStatus::SUCCESS);
386   SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
387       "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
388
389   AccountReconcilor* reconcilor = GetMockReconcilor();
390   reconcilor->StartReconcile();
391   token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
392       base::Time::Now() + base::TimeDelta::FromHours(1));
393   token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
394       base::Time::Now() + base::TimeDelta::FromHours(1));
395
396   base::RunLoop().RunUntilIdle();
397   ASSERT_TRUE(reconcilor->is_reconcile_started_);
398   SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
399                                 GoogleServiceAuthError::AuthErrorNone());
400   ASSERT_FALSE(reconcilor->is_reconcile_started_);
401 }
402
403 TEST_F(AccountReconcilorTest, StartReconcileAddToChrome) {
404   signin_manager()->SetAuthenticatedUsername("user@gmail.com");
405   token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
406
407   EXPECT_CALL(*GetMockReconcilor(),
408               PerformAddToChromeAction("other@gmail.com", 1));
409
410   SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
411       "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
412                "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
413       net::HTTP_OK, net::URLRequestStatus::SUCCESS);
414   SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
415       "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
416
417   AccountReconcilor* reconcilor = GetMockReconcilor();
418   reconcilor->StartReconcile();
419   token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
420       base::Time::Now() + base::TimeDelta::FromHours(1));
421
422   base::RunLoop().RunUntilIdle();
423   ASSERT_TRUE(reconcilor->is_reconcile_started_);
424   SimulateRefreshTokenFetched(reconcilor, "other@gmail.com", "");
425   ASSERT_FALSE(reconcilor->is_reconcile_started_);
426 }
427
428 TEST_F(AccountReconcilorTest, StartReconcileBadPrimary) {
429   signin_manager()->SetAuthenticatedUsername("user@gmail.com");
430   token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
431   token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
432
433   EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
434   EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
435   EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
436
437   SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
438       "[\"f\", [[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
439                "[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
440       net::HTTP_OK, net::URLRequestStatus::SUCCESS);
441   SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
442       "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
443
444   AccountReconcilor* reconcilor = GetMockReconcilor();
445   reconcilor->StartReconcile();
446   token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
447       base::Time::Now() + base::TimeDelta::FromHours(1));
448   token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
449       base::Time::Now() + base::TimeDelta::FromHours(1));
450
451   base::RunLoop().RunUntilIdle();
452   ASSERT_TRUE(reconcilor->is_reconcile_started_);
453   SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
454                                 GoogleServiceAuthError::AuthErrorNone());
455   ASSERT_TRUE(reconcilor->is_reconcile_started_);
456   SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
457                                 GoogleServiceAuthError::AuthErrorNone());
458   ASSERT_FALSE(reconcilor->is_reconcile_started_);
459 }
460
461 TEST_F(AccountReconcilorTest, StartReconcileOnlyOnce) {
462   signin_manager()->SetAuthenticatedUsername(kTestEmail);
463   token_service()->UpdateCredentials(kTestEmail, "refresh_token");
464
465   AccountReconcilor* reconcilor =
466       AccountReconcilorFactory::GetForProfile(profile());
467   ASSERT_TRUE(reconcilor);
468
469   SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
470       "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
471       net::HTTP_OK, net::URLRequestStatus::SUCCESS);
472   SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
473       "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
474
475   ASSERT_FALSE(reconcilor->is_reconcile_started_);
476   reconcilor->StartReconcile();
477   ASSERT_TRUE(reconcilor->is_reconcile_started_);
478
479   token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
480       base::Time::Now() + base::TimeDelta::FromHours(1));
481
482   base::RunLoop().RunUntilIdle();
483   ASSERT_FALSE(reconcilor->is_reconcile_started_);
484 }
485
486 TEST_F(AccountReconcilorTest, StartReconcileWithSessionInfoExpiredDefault) {
487   signin_manager()->SetAuthenticatedUsername("user@gmail.com");
488   token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
489   token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
490
491   EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
492
493   SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(),
494       "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0],"
495                "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
496       net::HTTP_OK, net::URLRequestStatus::SUCCESS);
497   SetFakeResponse("https://www.googleapis.com/oauth2/v1/userinfo",
498       "{\"id\":\"foo\"}", net::HTTP_OK, net::URLRequestStatus::SUCCESS);
499
500   AccountReconcilor* reconcilor =
501       AccountReconcilorFactory::GetForProfile(profile());
502   ASSERT_TRUE(reconcilor);
503
504   ASSERT_FALSE(reconcilor->is_reconcile_started_);
505   reconcilor->StartReconcile();
506   ASSERT_TRUE(reconcilor->is_reconcile_started_);
507
508   token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token",
509       base::Time::Now() + base::TimeDelta::FromHours(1));
510   token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token",
511       base::Time::Now() + base::TimeDelta::FromHours(1));
512
513   base::RunLoop().RunUntilIdle();
514   SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
515                                 GoogleServiceAuthError::AuthErrorNone());
516   ASSERT_FALSE(reconcilor->is_reconcile_started_);
517 }