ozone: evdev: Dispatch events in task
[chromium-blink-merge.git] / chrome / browser / signin / account_reconcilor_unittest.cc
blob4dd466544a2f6edaec4dc42b5060b0db3c41144f
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 #include "base/command_line.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/run_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/test/histogram_tester.h"
10 #include "base/time/time.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/prefs/pref_service_syncable.h"
13 #include "chrome/browser/signin/account_reconcilor_factory.h"
14 #include "chrome/browser/signin/chrome_signin_client_factory.h"
15 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
16 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
17 #include "chrome/browser/signin/fake_signin_manager.h"
18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
19 #include "chrome/browser/signin/signin_manager_factory.h"
20 #include "chrome/browser/signin/test_signin_client_builder.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "chrome/test/base/testing_profile_manager.h"
24 #include "components/signin/core/browser/account_reconcilor.h"
25 #include "components/signin/core/browser/profile_oauth2_token_service.h"
26 #include "components/signin/core/browser/signin_manager.h"
27 #include "components/signin/core/browser/signin_metrics.h"
28 #include "components/signin/core/common/profile_management_switches.h"
29 #include "components/signin/core/common/signin_switches.h"
30 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "google_apis/gaia/gaia_constants.h"
32 #include "google_apis/gaia/gaia_urls.h"
33 #include "net/url_request/test_url_fetcher_factory.h"
34 #include "testing/gmock/include/gmock/gmock.h"
35 #include "testing/gtest/include/gtest/gtest.h"
37 namespace {
39 const char kTestEmail[] = "user@gmail.com";
41 class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> {
42 public:
43 static KeyedService* Build(content::BrowserContext* context);
45 MockAccountReconcilor(ProfileOAuth2TokenService* token_service,
46 SigninManagerBase* signin_manager,
47 SigninClient* client);
48 virtual ~MockAccountReconcilor() {}
50 virtual void StartFetchingExternalCcResult() override {
51 // Don't do this in tests.
54 MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id));
55 MOCK_METHOD0(PerformLogoutAllAccountsAction, void());
58 // static
59 KeyedService* MockAccountReconcilor::Build(content::BrowserContext* context) {
60 Profile* profile = Profile::FromBrowserContext(context);
61 AccountReconcilor* reconcilor = new MockAccountReconcilor(
62 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
63 SigninManagerFactory::GetForProfile(profile),
64 ChromeSigninClientFactory::GetForProfile(profile));
65 reconcilor->Initialize(false /* start_reconcile_if_tokens_available */);
66 return reconcilor;
69 MockAccountReconcilor::MockAccountReconcilor(
70 ProfileOAuth2TokenService* token_service,
71 SigninManagerBase* signin_manager,
72 SigninClient* client)
73 : testing::StrictMock<AccountReconcilor>(token_service,
74 signin_manager,
75 client) {}
77 } // namespace
79 class AccountReconcilorTest : public ::testing::TestWithParam<bool> {
80 public:
81 AccountReconcilorTest();
82 virtual void SetUp() override;
84 TestingProfile* profile() { return profile_; }
85 FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
86 FakeProfileOAuth2TokenService* token_service() { return token_service_; }
87 base::HistogramTester* histogram_tester() { return &histogram_tester_; }
89 void SetFakeResponse(const std::string& url,
90 const std::string& data,
91 net::HttpStatusCode code,
92 net::URLRequestStatus::Status status) {
93 url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status);
96 MockAccountReconcilor* GetMockReconcilor();
98 void SimulateMergeSessionCompleted(
99 MergeSessionHelper::Observer* observer,
100 const std::string& account_id,
101 const GoogleServiceAuthError& error);
103 GURL list_accounts_url() { return list_accounts_url_; }
105 private:
106 content::TestBrowserThreadBundle bundle_;
107 TestingProfile* profile_;
108 FakeSigninManagerForTesting* signin_manager_;
109 FakeProfileOAuth2TokenService* token_service_;
110 MockAccountReconcilor* mock_reconcilor_;
111 net::FakeURLFetcherFactory url_fetcher_factory_;
112 scoped_ptr<TestingProfileManager> testing_profile_manager_;
113 base::HistogramTester histogram_tester_;
114 GURL list_accounts_url_;
116 DISALLOW_COPY_AND_ASSIGN(AccountReconcilorTest);
119 AccountReconcilorTest::AccountReconcilorTest()
120 : signin_manager_(NULL),
121 token_service_(NULL),
122 mock_reconcilor_(NULL),
123 url_fetcher_factory_(NULL) {}
125 void AccountReconcilorTest::SetUp() {
126 // If it's a non-parameterized test, or we have a parameter of true, set flag.
127 if (!::testing::UnitTest::GetInstance()->current_test_info()->value_param() ||
128 GetParam()) {
129 CommandLine::ForCurrentProcess()->AppendSwitch(
130 switches::kEnableNewProfileManagement);
133 list_accounts_url_ = GaiaUrls::GetInstance()->ListAccountsURLWithSource(
134 GaiaConstants::kReconcilorSource);
136 testing_profile_manager_.reset(
137 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
138 ASSERT_TRUE(testing_profile_manager_.get()->SetUp());
140 TestingProfile::TestingFactories factories;
141 factories.push_back(std::make_pair(ChromeSigninClientFactory::GetInstance(),
142 signin::BuildTestSigninClient));
143 factories.push_back(std::make_pair(
144 ProfileOAuth2TokenServiceFactory::GetInstance(),
145 BuildFakeProfileOAuth2TokenService));
146 factories.push_back(std::make_pair(SigninManagerFactory::GetInstance(),
147 FakeSigninManagerBase::Build));
148 factories.push_back(std::make_pair(AccountReconcilorFactory::GetInstance(),
149 MockAccountReconcilor::Build));
151 profile_ = testing_profile_manager_.get()->CreateTestingProfile("name",
152 scoped_ptr<PrefServiceSyncable>(),
153 base::UTF8ToUTF16("name"), 0, std::string(),
154 factories);
156 signin_manager_ =
157 static_cast<FakeSigninManagerForTesting*>(
158 SigninManagerFactory::GetForProfile(profile()));
160 token_service_ =
161 static_cast<FakeProfileOAuth2TokenService*>(
162 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
165 MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() {
166 if (!mock_reconcilor_) {
167 mock_reconcilor_ =
168 static_cast<MockAccountReconcilor*>(
169 AccountReconcilorFactory::GetForProfile(profile()));
172 return mock_reconcilor_;
175 void AccountReconcilorTest::SimulateMergeSessionCompleted(
176 MergeSessionHelper::Observer* observer,
177 const std::string& account_id,
178 const GoogleServiceAuthError& error) {
179 observer->MergeSessionCompleted(account_id, error);
182 TEST_F(AccountReconcilorTest, Basic) {
183 AccountReconcilor* reconcilor =
184 AccountReconcilorFactory::GetForProfile(profile());
185 ASSERT_TRUE(reconcilor);
186 ASSERT_EQ(token_service(), reconcilor->token_service());
189 #if !defined(OS_CHROMEOS)
191 // This method requires the use of the |TestSigninClient| to be created from the
192 // |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded|
193 // method with an empty implementation. On MacOS, the normal implementation
194 // causes the try_bots to time out.
195 TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
196 AccountReconcilor* reconcilor =
197 AccountReconcilorFactory::GetForProfile(profile());
198 ASSERT_TRUE(reconcilor);
199 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
201 signin_manager()->set_password("password");
202 signin_manager()->OnExternalSigninCompleted(kTestEmail);
203 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
205 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
207 signin_manager()->SignOut(signin_metrics::SIGNOUT_TEST);
208 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
211 // This method requires the use of the |TestSigninClient| to be created from the
212 // |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded|
213 // method with an empty implementation. On MacOS, the normal implementation
214 // causes the try_bots to time out.
215 TEST_F(AccountReconcilorTest, Reauth) {
216 signin_manager()->SetAuthenticatedUsername(kTestEmail);
217 signin_manager()->set_password("password");
219 AccountReconcilor* reconcilor =
220 AccountReconcilorFactory::GetForProfile(profile());
221 ASSERT_TRUE(reconcilor);
222 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
224 // Simulate reauth. The state of the reconcilor should not change.
225 signin_manager()->OnExternalSigninCompleted(kTestEmail);
226 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
229 #endif // !defined(OS_CHROMEOS)
231 TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
232 signin_manager()->SetAuthenticatedUsername(kTestEmail);
234 AccountReconcilor* reconcilor =
235 AccountReconcilorFactory::GetForProfile(profile());
236 ASSERT_TRUE(reconcilor);
237 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
240 TEST_F(AccountReconcilorTest, GetAccountsFromCookieSuccess) {
241 signin_manager()->SetAuthenticatedUsername(kTestEmail);
242 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
243 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction(kTestEmail));
244 AccountReconcilor* reconcilor =
245 AccountReconcilorFactory::GetForProfile(profile());
246 ASSERT_TRUE(reconcilor);
248 SetFakeResponse(list_accounts_url().spec(),
249 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
250 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
252 reconcilor->StartReconcile();
253 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
255 base::RunLoop().RunUntilIdle();
256 ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
257 const std::vector<std::pair<std::string, bool> >& accounts =
258 reconcilor->GetGaiaAccountsForTesting();
259 ASSERT_EQ(1u, accounts.size());
260 ASSERT_EQ("user@gmail.com", accounts[0].first);
263 TEST_F(AccountReconcilorTest, GetAccountsFromCookieFailure) {
264 signin_manager()->SetAuthenticatedUsername(kTestEmail);
265 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
266 AccountReconcilor* reconcilor =
267 AccountReconcilorFactory::GetForProfile(profile());
268 ASSERT_TRUE(reconcilor);
270 SetFakeResponse(list_accounts_url().spec(), "",
271 net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
273 reconcilor->StartReconcile();
274 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
276 base::RunLoop().RunUntilIdle();
277 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
280 TEST_P(AccountReconcilorTest, StartReconcileNoop) {
281 signin_manager()->SetAuthenticatedUsername(kTestEmail);
282 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
284 AccountReconcilor* reconcilor =
285 AccountReconcilorFactory::GetForProfile(profile());
286 ASSERT_TRUE(reconcilor);
288 SetFakeResponse(list_accounts_url().spec(),
289 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
290 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
292 reconcilor->StartReconcile();
293 ASSERT_TRUE(reconcilor->is_reconcile_started_);
294 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
296 base::RunLoop().RunUntilIdle();
297 ASSERT_FALSE(reconcilor->is_reconcile_started_);
299 histogram_tester()->ExpectTotalCount(
300 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun", 1);
301 histogram_tester()->ExpectUniqueSample(
302 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
303 signin_metrics::ACCOUNTS_SAME,
307 // This is test is needed until chrome changes to use gaia obfuscated id.
308 // The signin manager and token service use the gaia "email" property, which
309 // preserves dots in usernames and preserves case. gaia::ParseListAccountsData()
310 // however uses gaia "displayEmail" which does not preserve case, and then
311 // passes the string through gaia::CanonicalizeEmail() which removes dots. This
312 // tests makes sure that an email like "Dot.S@hmail.com", as seen by the
313 // token service, will be considered the same as "dots@gmail.com" as returned
314 // by gaia::ParseListAccountsData().
315 TEST_P(AccountReconcilorTest, StartReconcileNoopWithDots) {
316 signin_manager()->SetAuthenticatedUsername("Dot.S@gmail.com");
317 token_service()->UpdateCredentials("Dot.S@gmail.com", "refresh_token");
319 AccountReconcilor* reconcilor =
320 AccountReconcilorFactory::GetForProfile(profile());
321 ASSERT_TRUE(reconcilor);
323 SetFakeResponse(list_accounts_url().spec(),
324 "[\"f\", [[\"b\", 0, \"n\", \"dot.s@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
325 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
327 reconcilor->StartReconcile();
328 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
330 base::RunLoop().RunUntilIdle();
331 ASSERT_FALSE(reconcilor->is_reconcile_started_);
333 histogram_tester()->ExpectUniqueSample(
334 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
335 signin_metrics::ACCOUNTS_SAME,
339 TEST_P(AccountReconcilorTest, StartReconcileNoopMultiple) {
340 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
341 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
342 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
344 AccountReconcilor* reconcilor =
345 AccountReconcilorFactory::GetForProfile(profile());
346 ASSERT_TRUE(reconcilor);
348 SetFakeResponse(list_accounts_url().spec(),
349 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
350 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
351 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
353 reconcilor->StartReconcile();
354 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
355 base::RunLoop().RunUntilIdle();
356 ASSERT_FALSE(reconcilor->is_reconcile_started_);
358 histogram_tester()->ExpectTotalCount(
359 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun", 1);
360 histogram_tester()->ExpectUniqueSample(
361 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
362 signin_metrics::ACCOUNTS_SAME,
366 TEST_P(AccountReconcilorTest, StartReconcileAddToCookie) {
367 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
368 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
369 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
371 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
373 SetFakeResponse(list_accounts_url().spec(),
374 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
375 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
377 AccountReconcilor* reconcilor = GetMockReconcilor();
378 reconcilor->StartReconcile();
380 base::RunLoop().RunUntilIdle();
381 ASSERT_TRUE(reconcilor->is_reconcile_started_);
382 SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
383 GoogleServiceAuthError::AuthErrorNone());
384 ASSERT_FALSE(reconcilor->is_reconcile_started_);
386 histogram_tester()->ExpectUniqueSample(
387 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
388 signin_metrics::ACCOUNTS_SAME,
390 histogram_tester()->ExpectUniqueSample(
391 "Signin.Reconciler.AddedToCookieJar.FirstRun", 1, 1);
392 histogram_tester()->ExpectUniqueSample(
393 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
396 TEST_P(AccountReconcilorTest, StartReconcileRemoveFromCookie) {
397 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
398 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
400 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
401 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
403 SetFakeResponse(list_accounts_url().spec(),
404 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
405 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
406 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
408 AccountReconcilor* reconcilor = GetMockReconcilor();
409 reconcilor->StartReconcile();
410 ASSERT_TRUE(reconcilor->is_reconcile_started_);
412 base::RunLoop().RunUntilIdle();
413 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
414 GoogleServiceAuthError::AuthErrorNone());
415 ASSERT_FALSE(reconcilor->is_reconcile_started_);
417 histogram_tester()->ExpectUniqueSample(
418 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
419 signin_metrics::ACCOUNTS_SAME,
421 histogram_tester()->ExpectUniqueSample(
422 "Signin.Reconciler.AddedToCookieJar.FirstRun", 0, 1);
423 histogram_tester()->ExpectUniqueSample(
424 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 1, 1);
427 TEST_P(AccountReconcilorTest, StartReconcileAddToCookieTwice) {
428 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
429 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
430 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
432 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
433 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("third@gmail.com"));
435 SetFakeResponse(
436 list_accounts_url().spec(),
437 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
438 net::HTTP_OK,
439 net::URLRequestStatus::SUCCESS);
441 AccountReconcilor* reconcilor = GetMockReconcilor();
442 reconcilor->StartReconcile();
444 base::RunLoop().RunUntilIdle();
445 ASSERT_TRUE(reconcilor->is_reconcile_started_);
446 SimulateMergeSessionCompleted(
447 reconcilor, "other@gmail.com", GoogleServiceAuthError::AuthErrorNone());
448 ASSERT_FALSE(reconcilor->is_reconcile_started_);
450 histogram_tester()->ExpectUniqueSample(
451 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
452 signin_metrics::ACCOUNTS_SAME,
454 histogram_tester()->ExpectUniqueSample(
455 "Signin.Reconciler.AddedToCookieJar.FirstRun", 1, 1);
456 histogram_tester()->ExpectUniqueSample(
457 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
459 // Do another pass after I've added a third account to the token service
461 SetFakeResponse(
462 list_accounts_url().spec(),
463 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
464 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
465 net::HTTP_OK,
466 net::URLRequestStatus::SUCCESS);
467 // This will cause the reconcilor to fire.
468 token_service()->UpdateCredentials("third@gmail.com", "refresh_token");
470 base::RunLoop().RunUntilIdle();
472 ASSERT_TRUE(reconcilor->is_reconcile_started_);
473 SimulateMergeSessionCompleted(
474 reconcilor, "third@gmail.com", GoogleServiceAuthError::AuthErrorNone());
475 ASSERT_FALSE(reconcilor->is_reconcile_started_);
477 histogram_tester()->ExpectUniqueSample(
478 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
479 signin_metrics::ACCOUNTS_SAME,
481 histogram_tester()->ExpectUniqueSample(
482 "Signin.Reconciler.AddedToCookieJar.FirstRun", 1, 1);
483 histogram_tester()->ExpectUniqueSample(
484 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
485 histogram_tester()->ExpectUniqueSample(
486 "Signin.Reconciler.DifferentPrimaryAccounts.SubsequentRun",
487 signin_metrics::ACCOUNTS_SAME,
489 histogram_tester()->ExpectUniqueSample(
490 "Signin.Reconciler.AddedToCookieJar.SubsequentRun", 1, 1);
491 histogram_tester()->ExpectUniqueSample(
492 "Signin.Reconciler.RemovedFromCookieJar.SubsequentRun", 0, 1);
495 TEST_P(AccountReconcilorTest, StartReconcileBadPrimary) {
496 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
497 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
498 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
500 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
501 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
502 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
504 SetFakeResponse(list_accounts_url().spec(),
505 "[\"f\", [[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
506 "[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
507 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
509 AccountReconcilor* reconcilor = GetMockReconcilor();
510 reconcilor->StartReconcile();
512 base::RunLoop().RunUntilIdle();
513 ASSERT_TRUE(reconcilor->is_reconcile_started_);
514 SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
515 GoogleServiceAuthError::AuthErrorNone());
516 ASSERT_TRUE(reconcilor->is_reconcile_started_);
517 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
518 GoogleServiceAuthError::AuthErrorNone());
519 ASSERT_FALSE(reconcilor->is_reconcile_started_);
521 histogram_tester()->ExpectUniqueSample(
522 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
523 signin_metrics::COOKIE_AND_TOKEN_PRIMARIES_DIFFERENT,
525 histogram_tester()->ExpectUniqueSample(
526 "Signin.Reconciler.AddedToCookieJar.FirstRun", 0, 1);
527 histogram_tester()->ExpectUniqueSample(
528 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
531 TEST_P(AccountReconcilorTest, StartReconcileOnlyOnce) {
532 signin_manager()->SetAuthenticatedUsername(kTestEmail);
533 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
535 AccountReconcilor* reconcilor =
536 AccountReconcilorFactory::GetForProfile(profile());
537 ASSERT_TRUE(reconcilor);
539 SetFakeResponse(list_accounts_url().spec(),
540 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
541 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
543 ASSERT_FALSE(reconcilor->is_reconcile_started_);
544 reconcilor->StartReconcile();
545 ASSERT_TRUE(reconcilor->is_reconcile_started_);
547 base::RunLoop().RunUntilIdle();
548 ASSERT_FALSE(reconcilor->is_reconcile_started_);
551 TEST_P(AccountReconcilorTest, StartReconcileWithSessionInfoExpiredDefault) {
552 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
553 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
554 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
556 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
558 SetFakeResponse(list_accounts_url().spec(),
559 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0],"
560 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
561 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
563 AccountReconcilor* reconcilor =
564 AccountReconcilorFactory::GetForProfile(profile());
565 ASSERT_TRUE(reconcilor);
567 ASSERT_FALSE(reconcilor->is_reconcile_started_);
568 reconcilor->StartReconcile();
569 ASSERT_TRUE(reconcilor->is_reconcile_started_);
571 base::RunLoop().RunUntilIdle();
572 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
573 GoogleServiceAuthError::AuthErrorNone());
574 ASSERT_FALSE(reconcilor->is_reconcile_started_);
577 TEST_F(AccountReconcilorTest, MergeSessionCompletedWithBogusAccount) {
578 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
579 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
581 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
583 SetFakeResponse(list_accounts_url().spec(),
584 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
585 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
587 AccountReconcilor* reconcilor =
588 AccountReconcilorFactory::GetForProfile(profile());
589 ASSERT_TRUE(reconcilor);
591 ASSERT_FALSE(reconcilor->is_reconcile_started_);
592 reconcilor->StartReconcile();
593 ASSERT_TRUE(reconcilor->is_reconcile_started_);
595 base::RunLoop().RunUntilIdle();
597 // If an unknown account id is sent, it should not upset the state.
598 SimulateMergeSessionCompleted(reconcilor, "bogus@gmail.com",
599 GoogleServiceAuthError::AuthErrorNone());
600 ASSERT_TRUE(reconcilor->is_reconcile_started_);
602 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
603 GoogleServiceAuthError::AuthErrorNone());
604 ASSERT_FALSE(reconcilor->is_reconcile_started_);
607 INSTANTIATE_TEST_CASE_P(AccountReconcilorMaybeEnabled,
608 AccountReconcilorTest,
609 testing::Bool());