base::Time multiplicative operator overloading
[chromium-blink-merge.git] / chrome / browser / signin / account_reconcilor_unittest.cc
blob9b5dfc66337c86d921b52d9fd2690eef90e4ef44
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/browser/test_signin_client.h"
29 #include "components/signin/core/common/profile_management_switches.h"
30 #include "components/signin/core/common/signin_switches.h"
31 #include "content/public/test/test_browser_thread_bundle.h"
32 #include "google_apis/gaia/gaia_constants.h"
33 #include "google_apis/gaia/gaia_urls.h"
34 #include "net/url_request/test_url_fetcher_factory.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 namespace {
40 const char kTestEmail[] = "user@gmail.com";
42 class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> {
43 public:
44 static KeyedService* Build(content::BrowserContext* context);
46 MockAccountReconcilor(ProfileOAuth2TokenService* token_service,
47 SigninManagerBase* signin_manager,
48 SigninClient* client);
49 ~MockAccountReconcilor() override {}
51 MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id));
52 MOCK_METHOD0(PerformLogoutAllAccountsAction, void());
55 // static
56 KeyedService* MockAccountReconcilor::Build(content::BrowserContext* context) {
57 Profile* profile = Profile::FromBrowserContext(context);
58 AccountReconcilor* reconcilor = new MockAccountReconcilor(
59 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
60 SigninManagerFactory::GetForProfile(profile),
61 ChromeSigninClientFactory::GetForProfile(profile));
62 reconcilor->Initialize(false /* start_reconcile_if_tokens_available */);
63 return reconcilor;
66 MockAccountReconcilor::MockAccountReconcilor(
67 ProfileOAuth2TokenService* token_service,
68 SigninManagerBase* signin_manager,
69 SigninClient* client)
70 : testing::StrictMock<AccountReconcilor>(token_service,
71 signin_manager,
72 client) {}
74 } // namespace
76 class AccountReconcilorTest : public ::testing::TestWithParam<bool> {
77 public:
78 AccountReconcilorTest();
79 void SetUp() override;
81 TestingProfile* profile() { return profile_; }
82 FakeSigninManagerForTesting* signin_manager() { return signin_manager_; }
83 FakeProfileOAuth2TokenService* token_service() { return token_service_; }
84 TestSigninClient* test_signin_client() { return test_signin_client_; }
85 base::HistogramTester* histogram_tester() { return &histogram_tester_; }
87 void SetFakeResponse(const std::string& url,
88 const std::string& data,
89 net::HttpStatusCode code,
90 net::URLRequestStatus::Status status) {
91 url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status);
94 MockAccountReconcilor* GetMockReconcilor();
96 void SimulateMergeSessionCompleted(
97 MergeSessionHelper::Observer* observer,
98 const std::string& account_id,
99 const GoogleServiceAuthError& error);
101 void SimulateCookieContentSettingsChanged(
102 content_settings::Observer* observer,
103 const ContentSettingsPattern& primary_pattern);
105 GURL list_accounts_url() { return list_accounts_url_; }
106 GURL get_check_connection_info_url() {
107 return get_check_connection_info_url_;
110 private:
111 content::TestBrowserThreadBundle bundle_;
112 TestingProfile* profile_;
113 FakeSigninManagerForTesting* signin_manager_;
114 FakeProfileOAuth2TokenService* token_service_;
115 TestSigninClient* test_signin_client_;
116 MockAccountReconcilor* mock_reconcilor_;
117 net::FakeURLFetcherFactory url_fetcher_factory_;
118 scoped_ptr<TestingProfileManager> testing_profile_manager_;
119 base::HistogramTester histogram_tester_;
120 GURL list_accounts_url_;
121 GURL get_check_connection_info_url_;
123 DISALLOW_COPY_AND_ASSIGN(AccountReconcilorTest);
126 AccountReconcilorTest::AccountReconcilorTest()
127 : signin_manager_(NULL),
128 token_service_(NULL),
129 test_signin_client_(NULL),
130 mock_reconcilor_(NULL),
131 url_fetcher_factory_(NULL) {}
133 void AccountReconcilorTest::SetUp() {
134 // If it's a non-parameterized test, or we have a parameter of true, set flag.
135 if (!::testing::UnitTest::GetInstance()->current_test_info()->value_param() ||
136 GetParam()) {
137 base::CommandLine::ForCurrentProcess()->AppendSwitch(
138 switches::kEnableNewProfileManagement);
141 list_accounts_url_ = GaiaUrls::GetInstance()->ListAccountsURLWithSource(
142 GaiaConstants::kReconcilorSource);
143 get_check_connection_info_url_ =
144 GaiaUrls::GetInstance()->GetCheckConnectionInfoURLWithSource(
145 GaiaConstants::kReconcilorSource);
147 SetFakeResponse(get_check_connection_info_url().spec(), "[]",
148 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
150 testing_profile_manager_.reset(
151 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
152 ASSERT_TRUE(testing_profile_manager_.get()->SetUp());
154 TestingProfile::TestingFactories factories;
155 factories.push_back(std::make_pair(ChromeSigninClientFactory::GetInstance(),
156 signin::BuildTestSigninClient));
157 factories.push_back(std::make_pair(
158 ProfileOAuth2TokenServiceFactory::GetInstance(),
159 BuildFakeProfileOAuth2TokenService));
160 factories.push_back(std::make_pair(SigninManagerFactory::GetInstance(),
161 FakeSigninManagerBase::Build));
162 factories.push_back(std::make_pair(AccountReconcilorFactory::GetInstance(),
163 MockAccountReconcilor::Build));
165 profile_ = testing_profile_manager_.get()->CreateTestingProfile("name",
166 scoped_ptr<PrefServiceSyncable>(),
167 base::UTF8ToUTF16("name"), 0, std::string(),
168 factories);
170 signin_manager_ =
171 static_cast<FakeSigninManagerForTesting*>(
172 SigninManagerFactory::GetForProfile(profile()));
174 token_service_ =
175 static_cast<FakeProfileOAuth2TokenService*>(
176 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()));
178 test_signin_client_ =
179 static_cast<TestSigninClient*>(
180 ChromeSigninClientFactory::GetForProfile(profile()));
183 MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() {
184 if (!mock_reconcilor_) {
185 mock_reconcilor_ =
186 static_cast<MockAccountReconcilor*>(
187 AccountReconcilorFactory::GetForProfile(profile()));
190 return mock_reconcilor_;
193 void AccountReconcilorTest::SimulateMergeSessionCompleted(
194 MergeSessionHelper::Observer* observer,
195 const std::string& account_id,
196 const GoogleServiceAuthError& error) {
197 observer->MergeSessionCompleted(account_id, error);
200 void AccountReconcilorTest::SimulateCookieContentSettingsChanged(
201 content_settings::Observer* observer,
202 const ContentSettingsPattern& primary_pattern) {
203 observer->OnContentSettingChanged(
204 primary_pattern,
205 ContentSettingsPattern::Wildcard(),
206 CONTENT_SETTINGS_TYPE_COOKIES,
207 std::string());
210 TEST_F(AccountReconcilorTest, Basic) {
211 AccountReconcilor* reconcilor =
212 AccountReconcilorFactory::GetForProfile(profile());
213 ASSERT_TRUE(reconcilor);
214 ASSERT_EQ(token_service(), reconcilor->token_service());
217 #if !defined(OS_CHROMEOS)
219 // This method requires the use of the |TestSigninClient| to be created from the
220 // |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded|
221 // method with an empty implementation. On MacOS, the normal implementation
222 // causes the try_bots to time out.
223 TEST_F(AccountReconcilorTest, SigninManagerRegistration) {
224 AccountReconcilor* reconcilor =
225 AccountReconcilorFactory::GetForProfile(profile());
226 ASSERT_TRUE(reconcilor);
227 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
229 signin_manager()->set_password("password");
230 signin_manager()->OnExternalSigninCompleted(kTestEmail);
231 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
233 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
235 signin_manager()->SignOut(signin_metrics::SIGNOUT_TEST);
236 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService());
239 // This method requires the use of the |TestSigninClient| to be created from the
240 // |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded|
241 // method with an empty implementation. On MacOS, the normal implementation
242 // causes the try_bots to time out.
243 TEST_F(AccountReconcilorTest, Reauth) {
244 signin_manager()->SetAuthenticatedUsername(kTestEmail);
245 signin_manager()->set_password("password");
247 AccountReconcilor* reconcilor =
248 AccountReconcilorFactory::GetForProfile(profile());
249 ASSERT_TRUE(reconcilor);
250 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
252 // Simulate reauth. The state of the reconcilor should not change.
253 signin_manager()->OnExternalSigninCompleted(kTestEmail);
254 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
257 #endif // !defined(OS_CHROMEOS)
259 TEST_F(AccountReconcilorTest, ProfileAlreadyConnected) {
260 signin_manager()->SetAuthenticatedUsername(kTestEmail);
262 AccountReconcilor* reconcilor =
263 AccountReconcilorFactory::GetForProfile(profile());
264 ASSERT_TRUE(reconcilor);
265 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService());
268 TEST_F(AccountReconcilorTest, GetAccountsFromCookieSuccess) {
269 signin_manager()->SetAuthenticatedUsername(kTestEmail);
270 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
271 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction(kTestEmail));
272 AccountReconcilor* reconcilor =
273 AccountReconcilorFactory::GetForProfile(profile());
274 ASSERT_TRUE(reconcilor);
276 SetFakeResponse(list_accounts_url().spec(),
277 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
278 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
280 reconcilor->StartReconcile();
281 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
283 base::RunLoop().RunUntilIdle();
284 ASSERT_TRUE(reconcilor->AreGaiaAccountsSet());
285 const std::vector<std::pair<std::string, bool> >& accounts =
286 reconcilor->GetGaiaAccountsForTesting();
287 ASSERT_EQ(1u, accounts.size());
288 ASSERT_EQ("user@gmail.com", accounts[0].first);
291 TEST_F(AccountReconcilorTest, GetAccountsFromCookieFailure) {
292 signin_manager()->SetAuthenticatedUsername(kTestEmail);
293 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
294 AccountReconcilor* reconcilor =
295 AccountReconcilorFactory::GetForProfile(profile());
296 ASSERT_TRUE(reconcilor);
298 SetFakeResponse(list_accounts_url().spec(), "",
299 net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS);
301 reconcilor->StartReconcile();
302 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
304 base::RunLoop().RunUntilIdle();
305 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
308 TEST_P(AccountReconcilorTest, StartReconcileNoop) {
309 signin_manager()->SetAuthenticatedUsername(kTestEmail);
310 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
312 AccountReconcilor* reconcilor =
313 AccountReconcilorFactory::GetForProfile(profile());
314 ASSERT_TRUE(reconcilor);
316 SetFakeResponse(list_accounts_url().spec(),
317 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
318 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
320 reconcilor->StartReconcile();
321 ASSERT_TRUE(reconcilor->is_reconcile_started_);
322 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
324 base::RunLoop().RunUntilIdle();
325 ASSERT_FALSE(reconcilor->is_reconcile_started_);
327 histogram_tester()->ExpectTotalCount(
328 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun", 1);
329 histogram_tester()->ExpectUniqueSample(
330 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
331 signin_metrics::ACCOUNTS_SAME,
335 TEST_P(AccountReconcilorTest, StartReconcileCookiesDisabled) {
336 signin_manager()->SetAuthenticatedUsername(kTestEmail);
337 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
338 test_signin_client()->set_are_signin_cookies_allowed(false);
340 AccountReconcilor* reconcilor =
341 AccountReconcilorFactory::GetForProfile(profile());
342 ASSERT_TRUE(reconcilor);
344 reconcilor->StartReconcile();
345 ASSERT_FALSE(reconcilor->is_reconcile_started_);
346 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
348 base::RunLoop().RunUntilIdle();
349 ASSERT_FALSE(reconcilor->is_reconcile_started_);
352 TEST_P(AccountReconcilorTest, StartReconcileContentSettings) {
353 signin_manager()->SetAuthenticatedUsername(kTestEmail);
354 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
356 AccountReconcilor* reconcilor =
357 AccountReconcilorFactory::GetForProfile(profile());
358 ASSERT_TRUE(reconcilor);
360 test_signin_client()->set_are_signin_cookies_allowed(false);
361 SimulateCookieContentSettingsChanged(reconcilor,
362 ContentSettingsPattern::Wildcard());
363 ASSERT_FALSE(reconcilor->is_reconcile_started_);
365 test_signin_client()->set_are_signin_cookies_allowed(true);
366 SimulateCookieContentSettingsChanged(reconcilor,
367 ContentSettingsPattern::Wildcard());
368 ASSERT_TRUE(reconcilor->is_reconcile_started_);
371 TEST_P(AccountReconcilorTest, StartReconcileContentSettingsGaiaUrl) {
372 signin_manager()->SetAuthenticatedUsername(kTestEmail);
373 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
375 AccountReconcilor* reconcilor =
376 AccountReconcilorFactory::GetForProfile(profile());
377 ASSERT_TRUE(reconcilor);
379 SimulateCookieContentSettingsChanged(
380 reconcilor,
381 ContentSettingsPattern::FromURL(GaiaUrls::GetInstance()->gaia_url()));
382 ASSERT_TRUE(reconcilor->is_reconcile_started_);
385 TEST_P(AccountReconcilorTest, StartReconcileContentSettingsNonGaiaUrl) {
386 signin_manager()->SetAuthenticatedUsername(kTestEmail);
387 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
389 AccountReconcilor* reconcilor =
390 AccountReconcilorFactory::GetForProfile(profile());
391 ASSERT_TRUE(reconcilor);
393 SimulateCookieContentSettingsChanged(
394 reconcilor,
395 ContentSettingsPattern::FromURL(GURL("http://www.example.com")));
396 ASSERT_FALSE(reconcilor->is_reconcile_started_);
399 TEST_P(AccountReconcilorTest, StartReconcileContentSettingsInvalidPattern) {
400 signin_manager()->SetAuthenticatedUsername(kTestEmail);
401 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
403 AccountReconcilor* reconcilor =
404 AccountReconcilorFactory::GetForProfile(profile());
405 ASSERT_TRUE(reconcilor);
407 scoped_ptr<ContentSettingsPattern::BuilderInterface>
408 builder(ContentSettingsPattern::CreateBuilder(false));
409 builder->Invalid();
411 SimulateCookieContentSettingsChanged(reconcilor, builder->Build());
412 ASSERT_TRUE(reconcilor->is_reconcile_started_);
415 // This is test is needed until chrome changes to use gaia obfuscated id.
416 // The signin manager and token service use the gaia "email" property, which
417 // preserves dots in usernames and preserves case. gaia::ParseListAccountsData()
418 // however uses gaia "displayEmail" which does not preserve case, and then
419 // passes the string through gaia::CanonicalizeEmail() which removes dots. This
420 // tests makes sure that an email like "Dot.S@hmail.com", as seen by the
421 // token service, will be considered the same as "dots@gmail.com" as returned
422 // by gaia::ParseListAccountsData().
423 TEST_P(AccountReconcilorTest, StartReconcileNoopWithDots) {
424 signin_manager()->SetAuthenticatedUsername("Dot.S@gmail.com");
425 token_service()->UpdateCredentials("Dot.S@gmail.com", "refresh_token");
427 AccountReconcilor* reconcilor =
428 AccountReconcilorFactory::GetForProfile(profile());
429 ASSERT_TRUE(reconcilor);
431 SetFakeResponse(list_accounts_url().spec(),
432 "[\"f\", [[\"b\", 0, \"n\", \"dot.s@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
433 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
435 reconcilor->StartReconcile();
436 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
438 base::RunLoop().RunUntilIdle();
439 ASSERT_FALSE(reconcilor->is_reconcile_started_);
441 histogram_tester()->ExpectUniqueSample(
442 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
443 signin_metrics::ACCOUNTS_SAME,
447 TEST_P(AccountReconcilorTest, StartReconcileNoopMultiple) {
448 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
449 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
450 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
452 AccountReconcilor* reconcilor =
453 AccountReconcilorFactory::GetForProfile(profile());
454 ASSERT_TRUE(reconcilor);
456 SetFakeResponse(list_accounts_url().spec(),
457 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
458 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
459 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
461 reconcilor->StartReconcile();
462 ASSERT_FALSE(reconcilor->AreGaiaAccountsSet());
463 base::RunLoop().RunUntilIdle();
464 ASSERT_FALSE(reconcilor->is_reconcile_started_);
466 histogram_tester()->ExpectTotalCount(
467 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun", 1);
468 histogram_tester()->ExpectUniqueSample(
469 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
470 signin_metrics::ACCOUNTS_SAME,
474 TEST_P(AccountReconcilorTest, StartReconcileAddToCookie) {
475 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
476 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
477 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
479 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
481 SetFakeResponse(list_accounts_url().spec(),
482 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
483 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
485 AccountReconcilor* reconcilor = GetMockReconcilor();
486 reconcilor->StartReconcile();
488 base::RunLoop().RunUntilIdle();
489 ASSERT_TRUE(reconcilor->is_reconcile_started_);
490 SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
491 GoogleServiceAuthError::AuthErrorNone());
492 ASSERT_FALSE(reconcilor->is_reconcile_started_);
494 histogram_tester()->ExpectUniqueSample(
495 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
496 signin_metrics::ACCOUNTS_SAME,
498 histogram_tester()->ExpectUniqueSample(
499 "Signin.Reconciler.AddedToCookieJar.FirstRun", 1, 1);
500 histogram_tester()->ExpectUniqueSample(
501 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
504 TEST_P(AccountReconcilorTest, StartReconcileRemoveFromCookie) {
505 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
506 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
508 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
509 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
511 SetFakeResponse(list_accounts_url().spec(),
512 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
513 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
514 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
516 AccountReconcilor* reconcilor = GetMockReconcilor();
517 reconcilor->StartReconcile();
518 ASSERT_TRUE(reconcilor->is_reconcile_started_);
520 base::RunLoop().RunUntilIdle();
521 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
522 GoogleServiceAuthError::AuthErrorNone());
523 ASSERT_FALSE(reconcilor->is_reconcile_started_);
525 histogram_tester()->ExpectUniqueSample(
526 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
527 signin_metrics::ACCOUNTS_SAME,
529 histogram_tester()->ExpectUniqueSample(
530 "Signin.Reconciler.AddedToCookieJar.FirstRun", 0, 1);
531 histogram_tester()->ExpectUniqueSample(
532 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 1, 1);
535 TEST_P(AccountReconcilorTest, StartReconcileAddToCookieTwice) {
536 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
537 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
538 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
540 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
541 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("third@gmail.com"));
543 SetFakeResponse(
544 list_accounts_url().spec(),
545 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
546 net::HTTP_OK,
547 net::URLRequestStatus::SUCCESS);
549 AccountReconcilor* reconcilor = GetMockReconcilor();
550 reconcilor->StartReconcile();
552 base::RunLoop().RunUntilIdle();
553 ASSERT_TRUE(reconcilor->is_reconcile_started_);
554 SimulateMergeSessionCompleted(
555 reconcilor, "other@gmail.com", GoogleServiceAuthError::AuthErrorNone());
556 ASSERT_FALSE(reconcilor->is_reconcile_started_);
558 histogram_tester()->ExpectUniqueSample(
559 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
560 signin_metrics::ACCOUNTS_SAME,
562 histogram_tester()->ExpectUniqueSample(
563 "Signin.Reconciler.AddedToCookieJar.FirstRun", 1, 1);
564 histogram_tester()->ExpectUniqueSample(
565 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
567 // Do another pass after I've added a third account to the token service
569 SetFakeResponse(
570 list_accounts_url().spec(),
571 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
572 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
573 net::HTTP_OK,
574 net::URLRequestStatus::SUCCESS);
575 // This will cause the reconcilor to fire.
576 token_service()->UpdateCredentials("third@gmail.com", "refresh_token");
578 base::RunLoop().RunUntilIdle();
580 ASSERT_TRUE(reconcilor->is_reconcile_started_);
581 SimulateMergeSessionCompleted(
582 reconcilor, "third@gmail.com", GoogleServiceAuthError::AuthErrorNone());
583 ASSERT_FALSE(reconcilor->is_reconcile_started_);
585 histogram_tester()->ExpectUniqueSample(
586 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
587 signin_metrics::ACCOUNTS_SAME,
589 histogram_tester()->ExpectUniqueSample(
590 "Signin.Reconciler.AddedToCookieJar.FirstRun", 1, 1);
591 histogram_tester()->ExpectUniqueSample(
592 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
593 histogram_tester()->ExpectUniqueSample(
594 "Signin.Reconciler.DifferentPrimaryAccounts.SubsequentRun",
595 signin_metrics::ACCOUNTS_SAME,
597 histogram_tester()->ExpectUniqueSample(
598 "Signin.Reconciler.AddedToCookieJar.SubsequentRun", 1, 1);
599 histogram_tester()->ExpectUniqueSample(
600 "Signin.Reconciler.RemovedFromCookieJar.SubsequentRun", 0, 1);
603 TEST_P(AccountReconcilorTest, StartReconcileBadPrimary) {
604 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
605 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
606 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
608 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction());
609 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
610 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com"));
612 SetFakeResponse(list_accounts_url().spec(),
613 "[\"f\", [[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1], "
614 "[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
615 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
617 AccountReconcilor* reconcilor = GetMockReconcilor();
618 reconcilor->StartReconcile();
620 base::RunLoop().RunUntilIdle();
621 ASSERT_TRUE(reconcilor->is_reconcile_started_);
622 SimulateMergeSessionCompleted(reconcilor, "other@gmail.com",
623 GoogleServiceAuthError::AuthErrorNone());
624 ASSERT_TRUE(reconcilor->is_reconcile_started_);
625 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
626 GoogleServiceAuthError::AuthErrorNone());
627 ASSERT_FALSE(reconcilor->is_reconcile_started_);
629 histogram_tester()->ExpectUniqueSample(
630 "Signin.Reconciler.DifferentPrimaryAccounts.FirstRun",
631 signin_metrics::COOKIE_AND_TOKEN_PRIMARIES_DIFFERENT,
633 histogram_tester()->ExpectUniqueSample(
634 "Signin.Reconciler.AddedToCookieJar.FirstRun", 0, 1);
635 histogram_tester()->ExpectUniqueSample(
636 "Signin.Reconciler.RemovedFromCookieJar.FirstRun", 0, 1);
639 TEST_P(AccountReconcilorTest, StartReconcileOnlyOnce) {
640 signin_manager()->SetAuthenticatedUsername(kTestEmail);
641 token_service()->UpdateCredentials(kTestEmail, "refresh_token");
643 AccountReconcilor* reconcilor =
644 AccountReconcilorFactory::GetForProfile(profile());
645 ASSERT_TRUE(reconcilor);
647 SetFakeResponse(list_accounts_url().spec(),
648 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
649 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
651 ASSERT_FALSE(reconcilor->is_reconcile_started_);
652 reconcilor->StartReconcile();
653 ASSERT_TRUE(reconcilor->is_reconcile_started_);
655 base::RunLoop().RunUntilIdle();
656 ASSERT_FALSE(reconcilor->is_reconcile_started_);
659 TEST_P(AccountReconcilorTest, StartReconcileWithSessionInfoExpiredDefault) {
660 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
661 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
662 token_service()->UpdateCredentials("other@gmail.com", "refresh_token");
664 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
666 SetFakeResponse(list_accounts_url().spec(),
667 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0],"
668 "[\"b\", 0, \"n\", \"other@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]",
669 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
671 AccountReconcilor* reconcilor =
672 AccountReconcilorFactory::GetForProfile(profile());
673 ASSERT_TRUE(reconcilor);
675 ASSERT_FALSE(reconcilor->is_reconcile_started_);
676 reconcilor->StartReconcile();
677 ASSERT_TRUE(reconcilor->is_reconcile_started_);
679 base::RunLoop().RunUntilIdle();
680 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
681 GoogleServiceAuthError::AuthErrorNone());
682 ASSERT_FALSE(reconcilor->is_reconcile_started_);
685 TEST_F(AccountReconcilorTest, MergeSessionCompletedWithBogusAccount) {
686 signin_manager()->SetAuthenticatedUsername("user@gmail.com");
687 token_service()->UpdateCredentials("user@gmail.com", "refresh_token");
689 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("user@gmail.com"));
691 SetFakeResponse(list_accounts_url().spec(),
692 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 0]]]",
693 net::HTTP_OK, net::URLRequestStatus::SUCCESS);
695 AccountReconcilor* reconcilor =
696 AccountReconcilorFactory::GetForProfile(profile());
697 ASSERT_TRUE(reconcilor);
699 ASSERT_FALSE(reconcilor->is_reconcile_started_);
700 reconcilor->StartReconcile();
701 ASSERT_TRUE(reconcilor->is_reconcile_started_);
703 base::RunLoop().RunUntilIdle();
705 // If an unknown account id is sent, it should not upset the state.
706 SimulateMergeSessionCompleted(reconcilor, "bogus@gmail.com",
707 GoogleServiceAuthError::AuthErrorNone());
708 ASSERT_TRUE(reconcilor->is_reconcile_started_);
710 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com",
711 GoogleServiceAuthError::AuthErrorNone());
712 ASSERT_FALSE(reconcilor->is_reconcile_started_);
715 INSTANTIATE_TEST_CASE_P(AccountReconcilorMaybeEnabled,
716 AccountReconcilorTest,
717 testing::Bool());