Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | |
| 5 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 7 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "chrome/browser/prefs/pref_service_syncable.h" | |
| 8 #include "chrome/browser/signin/account_reconcilor_factory.h" | 11 #include "chrome/browser/signin/account_reconcilor_factory.h" |
| 9 #include "chrome/browser/signin/chrome_signin_client_factory.h" | 12 #include "chrome/browser/signin/chrome_signin_client_factory.h" |
| 10 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 13 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| 11 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | 14 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" |
| 12 #include "chrome/browser/signin/fake_signin_manager.h" | 15 #include "chrome/browser/signin/fake_signin_manager.h" |
| 13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 14 #include "chrome/browser/signin/signin_manager_factory.h" | 17 #include "chrome/browser/signin/signin_manager_factory.h" |
| 18 #include "chrome/browser/signin/test_signin_client_builder.h" | |
| 19 #include "chrome/test/base/testing_browser_process.h" | |
| 15 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "chrome/test/base/testing_profile_manager.h" | |
| 16 #include "components/signin/core/browser/account_reconcilor.h" | 22 #include "components/signin/core/browser/account_reconcilor.h" |
| 17 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 23 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
| 18 #include "components/signin/core/browser/signin_manager.h" | 24 #include "components/signin/core/browser/signin_manager.h" |
| 25 //#include "components/signin/core/browser/test_signin_client.h" | |
| 26 #include "components/signin/core/common/signin_switches.h" | |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 27 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "google_apis/gaia/gaia_urls.h" | 28 #include "google_apis/gaia/gaia_urls.h" |
| 21 #include "net/url_request/test_url_fetcher_factory.h" | 29 #include "net/url_request/test_url_fetcher_factory.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 32 |
| 25 namespace { | 33 namespace { |
| 26 | 34 |
| 27 const char kTestEmail[] = "user@gmail.com"; | 35 const char kTestEmail[] = "user@gmail.com"; |
| 28 | 36 |
| 29 class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> { | 37 class MockAccountReconcilor : public testing::StrictMock<AccountReconcilor> { |
| 30 public: | 38 public: |
| 31 static KeyedService* Build(content::BrowserContext* context); | 39 static KeyedService* Build(content::BrowserContext* context); |
| 32 | 40 |
| 33 MockAccountReconcilor(ProfileOAuth2TokenService* token_service, | 41 MockAccountReconcilor(ProfileOAuth2TokenService* token_service, |
| 34 SigninManagerBase* signin_manager, | 42 SigninManagerBase* signin_manager, |
| 35 SigninClient* client); | 43 SigninClient* client); |
| 36 virtual ~MockAccountReconcilor() {} | 44 virtual ~MockAccountReconcilor() {} |
| 37 | 45 |
| 38 MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id)); | 46 MOCK_METHOD1(PerformMergeAction, void(const std::string& account_id)); |
| 39 MOCK_METHOD1(StartRemoveAction, void(const std::string& account_id)); | 47 MOCK_METHOD1(PerformStartRemoveAction, void(const std::string& account_id)); |
| 40 MOCK_METHOD3( | 48 MOCK_METHOD3( |
| 41 FinishRemoveAction, | 49 PerformFinishRemoveAction, |
| 42 void(const std::string& account_id, | 50 void(const std::string& account_id, |
| 43 const GoogleServiceAuthError& error, | 51 const GoogleServiceAuthError& error, |
| 44 const std::vector<std::pair<std::string, bool> >& accounts)); | 52 const std::vector<std::pair<std::string, bool> >& accounts)); |
| 45 MOCK_METHOD2(PerformAddToChromeAction, void(const std::string& account_id, | 53 MOCK_METHOD2(PerformAddToChromeAction, void(const std::string& account_id, |
| 46 int session_index)); | 54 int session_index)); |
| 47 MOCK_METHOD0(PerformLogoutAllAccountsAction, void()); | 55 MOCK_METHOD0(PerformLogoutAllAccountsAction, void()); |
| 48 }; | 56 }; |
| 49 | 57 |
| 50 // static | 58 // static |
| 51 KeyedService* MockAccountReconcilor::Build(content::BrowserContext* context) { | 59 KeyedService* MockAccountReconcilor::Build(content::BrowserContext* context) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 67 client) {} | 75 client) {} |
| 68 | 76 |
| 69 } // namespace | 77 } // namespace |
| 70 | 78 |
| 71 class AccountReconcilorTest : public testing::Test { | 79 class AccountReconcilorTest : public testing::Test { |
| 72 public: | 80 public: |
| 73 AccountReconcilorTest(); | 81 AccountReconcilorTest(); |
| 74 virtual void SetUp() OVERRIDE; | 82 virtual void SetUp() OVERRIDE; |
| 75 virtual void TearDown() OVERRIDE; | 83 virtual void TearDown() OVERRIDE; |
| 76 | 84 |
| 77 TestingProfile* profile() { return profile_.get(); } | 85 TestingProfile* profile() { return profile_; } |
| 78 FakeSigninManagerForTesting* signin_manager() { return signin_manager_; } | 86 FakeSigninManagerForTesting* signin_manager() { return signin_manager_; } |
| 79 FakeProfileOAuth2TokenService* token_service() { return token_service_; } | 87 FakeProfileOAuth2TokenService* token_service() { return token_service_; } |
| 80 | 88 |
| 81 void SetFakeResponse(const std::string& url, | 89 void SetFakeResponse(const std::string& url, |
| 82 const std::string& data, | 90 const std::string& data, |
| 83 net::HttpStatusCode code, | 91 net::HttpStatusCode code, |
| 84 net::URLRequestStatus::Status status) { | 92 net::URLRequestStatus::Status status) { |
| 85 url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status); | 93 url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status); |
| 86 } | 94 } |
| 87 | 95 |
| 88 MockAccountReconcilor* GetMockReconcilor(); | 96 MockAccountReconcilor* GetMockReconcilor(); |
| 89 | 97 |
| 90 void SimulateMergeSessionCompleted( | 98 void SimulateMergeSessionCompleted( |
| 91 MergeSessionHelper::Observer* observer, | 99 MergeSessionHelper::Observer* observer, |
| 92 const std::string& account_id, | 100 const std::string& account_id, |
| 93 const GoogleServiceAuthError& error); | 101 const GoogleServiceAuthError& error); |
| 94 | 102 |
| 95 void SimulateRefreshTokenFetched( | 103 void SimulateRefreshTokenFetched( |
| 96 AccountReconcilor* reconcilor, | 104 AccountReconcilor* reconcilor, |
| 97 const std::string& account_id, | 105 const std::string& account_id, |
| 98 const std::string& refresh_token); | 106 const std::string& refresh_token); |
| 99 | 107 |
| 100 private: | 108 private: |
| 101 content::TestBrowserThreadBundle bundle_; | 109 content::TestBrowserThreadBundle bundle_; |
| 102 scoped_ptr<TestingProfile> profile_; | 110 TestingProfile* profile_; |
| 103 FakeSigninManagerForTesting* signin_manager_; | 111 FakeSigninManagerForTesting* signin_manager_; |
| 104 FakeProfileOAuth2TokenService* token_service_; | 112 FakeProfileOAuth2TokenService* token_service_; |
| 105 MockAccountReconcilor* mock_reconcilor_; | 113 MockAccountReconcilor* mock_reconcilor_; |
| 106 net::FakeURLFetcherFactory url_fetcher_factory_; | 114 net::FakeURLFetcherFactory url_fetcher_factory_; |
| 115 TestingProfileManager* testing_profile_manager_; | |
|
Roger Tawa OOO till Jul 10th
2014/05/27 19:15:38
Use a scoped_ptr<TestingProfileManager> instead of
Mike Lerman
2014/05/27 19:43:03
Done.
| |
| 107 }; | 116 }; |
| 108 | 117 |
| 109 AccountReconcilorTest::AccountReconcilorTest() | 118 AccountReconcilorTest::AccountReconcilorTest() |
| 110 : signin_manager_(NULL), | 119 : signin_manager_(NULL), |
| 111 token_service_(NULL), | 120 token_service_(NULL), |
| 112 mock_reconcilor_(NULL), | 121 mock_reconcilor_(NULL), |
| 113 url_fetcher_factory_(NULL) {} | 122 url_fetcher_factory_(NULL) {} |
| 114 | 123 |
| 115 void AccountReconcilorTest::SetUp() { | 124 void AccountReconcilorTest::SetUp() { |
| 116 TestingProfile::Builder builder; | 125 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 117 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 126 switches::kNewProfileManagement); |
| 118 BuildFakeProfileOAuth2TokenService); | 127 |
| 119 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | 128 testing_profile_manager_ = |
| 120 FakeSigninManagerBase::Build); | 129 new TestingProfileManager(TestingBrowserProcess::GetGlobal()); |
| 121 builder.AddTestingFactory(AccountReconcilorFactory::GetInstance(), | 130 ASSERT_TRUE(testing_profile_manager_->SetUp()); |
| 122 MockAccountReconcilor::Build); | 131 |
| 123 profile_ = builder.Build(); | 132 TestingProfile::TestingFactories factories; |
| 133 factories.push_back(std::make_pair(ChromeSigninClientFactory::GetInstance(), | |
| 134 signin::BuildTestSigninClient)); | |
| 135 factories.push_back(std::make_pair( | |
| 136 ProfileOAuth2TokenServiceFactory::GetInstance(), | |
| 137 BuildFakeProfileOAuth2TokenService)); | |
| 138 factories.push_back(std::make_pair(SigninManagerFactory::GetInstance(), | |
| 139 FakeSigninManagerBase::Build)); | |
| 140 factories.push_back(std::make_pair(AccountReconcilorFactory::GetInstance(), | |
| 141 MockAccountReconcilor::Build)); | |
| 142 | |
| 143 profile_ = testing_profile_manager_->CreateTestingProfile("name", | |
| 144 scoped_ptr<PrefServiceSyncable>(), | |
| 145 base::UTF8ToUTF16("name"), 0, std::string(), | |
| 146 factories); | |
| 124 | 147 |
| 125 signin_manager_ = | 148 signin_manager_ = |
| 126 static_cast<FakeSigninManagerForTesting*>( | 149 static_cast<FakeSigninManagerForTesting*>( |
| 127 SigninManagerFactory::GetForProfile(profile())); | 150 SigninManagerFactory::GetForProfile(profile())); |
| 128 | 151 |
| 129 token_service_ = | 152 token_service_ = |
| 130 static_cast<FakeProfileOAuth2TokenService*>( | 153 static_cast<FakeProfileOAuth2TokenService*>( |
| 131 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())); | 154 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())); |
| 132 } | 155 } |
| 133 | 156 |
| 134 void AccountReconcilorTest::TearDown() { | 157 void AccountReconcilorTest::TearDown() { |
| 135 // Destroy the profile before all threads are torn down. | 158 // The |testing_profile_manager_| will handle destroying the profile. |
| 136 profile_.reset(); | 159 delete testing_profile_manager_; |
| 137 } | 160 } |
| 138 | 161 |
| 139 MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() { | 162 MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() { |
| 140 if (!mock_reconcilor_) { | 163 if (!mock_reconcilor_) { |
| 141 mock_reconcilor_ = | 164 mock_reconcilor_ = |
| 142 static_cast<MockAccountReconcilor*>( | 165 static_cast<MockAccountReconcilor*>( |
| 143 AccountReconcilorFactory::GetForProfile(profile())); | 166 AccountReconcilorFactory::GetForProfile(profile())); |
| 144 } | 167 } |
| 145 | 168 |
| 146 return mock_reconcilor_; | 169 return mock_reconcilor_; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 162 | 185 |
| 163 TEST_F(AccountReconcilorTest, Basic) { | 186 TEST_F(AccountReconcilorTest, Basic) { |
| 164 AccountReconcilor* reconcilor = | 187 AccountReconcilor* reconcilor = |
| 165 AccountReconcilorFactory::GetForProfile(profile()); | 188 AccountReconcilorFactory::GetForProfile(profile()); |
| 166 ASSERT_TRUE(reconcilor); | 189 ASSERT_TRUE(reconcilor); |
| 167 ASSERT_EQ(token_service(), reconcilor->token_service()); | 190 ASSERT_EQ(token_service(), reconcilor->token_service()); |
| 168 } | 191 } |
| 169 | 192 |
| 170 #if !defined(OS_CHROMEOS) | 193 #if !defined(OS_CHROMEOS) |
| 171 | 194 |
| 195 // This method requires the use of the |TestSigninClient| to be created from the | |
| 196 // |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded| | |
| 197 // method with an empty implementation. On MacOS, the normal implementation | |
| 198 // causes the try_bots to time out. | |
| 172 TEST_F(AccountReconcilorTest, SigninManagerRegistration) { | 199 TEST_F(AccountReconcilorTest, SigninManagerRegistration) { |
| 173 AccountReconcilor* reconcilor = | 200 AccountReconcilor* reconcilor = |
| 174 AccountReconcilorFactory::GetForProfile(profile()); | 201 AccountReconcilorFactory::GetForProfile(profile()); |
| 175 ASSERT_TRUE(reconcilor); | 202 ASSERT_TRUE(reconcilor); |
| 176 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService()); | 203 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService()); |
| 177 | 204 |
| 205 signin_manager()->set_password("password"); | |
| 178 signin_manager()->OnExternalSigninCompleted(kTestEmail); | 206 signin_manager()->OnExternalSigninCompleted(kTestEmail); |
| 179 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); | 207 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); |
| 180 | 208 |
| 181 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction()); | 209 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction()); |
| 182 | 210 |
| 183 signin_manager()->SignOut(); | 211 signin_manager()->SignOut(); |
| 184 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService()); | 212 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService()); |
| 185 } | 213 } |
| 186 | 214 |
| 215 // This method requires the use of the |TestSigninClient| to be created from the | |
| 216 // |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded| | |
| 217 // method with an empty implementation. On MacOS, the normal implementation | |
| 218 // causes the try_bots to time out. | |
| 187 TEST_F(AccountReconcilorTest, Reauth) { | 219 TEST_F(AccountReconcilorTest, Reauth) { |
| 188 signin_manager()->SetAuthenticatedUsername(kTestEmail); | 220 signin_manager()->SetAuthenticatedUsername(kTestEmail); |
| 221 signin_manager()->set_password("password"); | |
| 189 | 222 |
| 190 AccountReconcilor* reconcilor = | 223 AccountReconcilor* reconcilor = |
| 191 AccountReconcilorFactory::GetForProfile(profile()); | 224 AccountReconcilorFactory::GetForProfile(profile()); |
| 192 ASSERT_TRUE(reconcilor); | 225 ASSERT_TRUE(reconcilor); |
| 193 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); | 226 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); |
| 194 | 227 |
| 195 // Simulate reauth. The state of the reconcilor should not change. | 228 // Simulate reauth. The state of the reconcilor should not change. |
| 196 signin_manager()->OnExternalSigninCompleted(kTestEmail); | 229 signin_manager()->OnExternalSigninCompleted(kTestEmail); |
| 197 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); | 230 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); |
| 198 } | 231 } |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked()); | 448 ASSERT_FALSE(reconcilor->AreAllRefreshTokensChecked()); |
| 416 | 449 |
| 417 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token", | 450 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token", |
| 418 base::Time::Now() + base::TimeDelta::FromHours(1)); | 451 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 419 | 452 |
| 420 base::RunLoop().RunUntilIdle(); | 453 base::RunLoop().RunUntilIdle(); |
| 421 ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked()); | 454 ASSERT_TRUE(reconcilor->AreAllRefreshTokensChecked()); |
| 422 ASSERT_FALSE(reconcilor->is_reconcile_started_); | 455 ASSERT_FALSE(reconcilor->is_reconcile_started_); |
| 423 } | 456 } |
| 424 | 457 |
| 458 | |
|
Roger Tawa OOO till Jul 10th
2014/05/27 19:15:38
Remove extra line.
Mike Lerman
2014/05/27 19:43:03
Done.
| |
| 425 TEST_F(AccountReconcilorTest, StartReconcileAddToCookie) { | 459 TEST_F(AccountReconcilorTest, StartReconcileAddToCookie) { |
| 426 signin_manager()->SetAuthenticatedUsername("user@gmail.com"); | 460 signin_manager()->SetAuthenticatedUsername("user@gmail.com"); |
| 427 token_service()->UpdateCredentials("user@gmail.com", "refresh_token"); | 461 token_service()->UpdateCredentials("user@gmail.com", "refresh_token"); |
| 428 token_service()->UpdateCredentials("other@gmail.com", "refresh_token"); | 462 token_service()->UpdateCredentials("other@gmail.com", "refresh_token"); |
| 429 | 463 |
| 430 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com")); | 464 EXPECT_CALL(*GetMockReconcilor(), PerformMergeAction("other@gmail.com")); |
| 431 | 465 |
| 432 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(), | 466 SetFakeResponse(GaiaUrls::GetInstance()->list_accounts_url().spec(), |
| 433 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]", | 467 "[\"f\", [[\"b\", 0, \"n\", \"user@gmail.com\", \"p\", 0, 0, 0, 0, 1]]]", |
| 434 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 468 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 557 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token", | 591 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token", |
| 558 base::Time::Now() + base::TimeDelta::FromHours(1)); | 592 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 559 token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token", | 593 token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token", |
| 560 base::Time::Now() + base::TimeDelta::FromHours(1)); | 594 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 561 | 595 |
| 562 base::RunLoop().RunUntilIdle(); | 596 base::RunLoop().RunUntilIdle(); |
| 563 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com", | 597 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com", |
| 564 GoogleServiceAuthError::AuthErrorNone()); | 598 GoogleServiceAuthError::AuthErrorNone()); |
| 565 ASSERT_FALSE(reconcilor->is_reconcile_started_); | 599 ASSERT_FALSE(reconcilor->is_reconcile_started_); |
| 566 } | 600 } |
| OLD | NEW |