| 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 13 matching lines...) Expand all Loading... |
| 65 : testing::StrictMock<AccountReconcilor>(token_service, | 73 : testing::StrictMock<AccountReconcilor>(token_service, |
| 66 signin_manager, | 74 signin_manager, |
| 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; | |
| 76 | 83 |
| 77 TestingProfile* profile() { return profile_.get(); } | 84 TestingProfile* profile() { return profile_; } |
| 78 FakeSigninManagerForTesting* signin_manager() { return signin_manager_; } | 85 FakeSigninManagerForTesting* signin_manager() { return signin_manager_; } |
| 79 FakeProfileOAuth2TokenService* token_service() { return token_service_; } | 86 FakeProfileOAuth2TokenService* token_service() { return token_service_; } |
| 80 | 87 |
| 81 void SetFakeResponse(const std::string& url, | 88 void SetFakeResponse(const std::string& url, |
| 82 const std::string& data, | 89 const std::string& data, |
| 83 net::HttpStatusCode code, | 90 net::HttpStatusCode code, |
| 84 net::URLRequestStatus::Status status) { | 91 net::URLRequestStatus::Status status) { |
| 85 url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status); | 92 url_fetcher_factory_.SetFakeResponse(GURL(url), data, code, status); |
| 86 } | 93 } |
| 87 | 94 |
| 88 MockAccountReconcilor* GetMockReconcilor(); | 95 MockAccountReconcilor* GetMockReconcilor(); |
| 89 | 96 |
| 90 void SimulateMergeSessionCompleted( | 97 void SimulateMergeSessionCompleted( |
| 91 MergeSessionHelper::Observer* observer, | 98 MergeSessionHelper::Observer* observer, |
| 92 const std::string& account_id, | 99 const std::string& account_id, |
| 93 const GoogleServiceAuthError& error); | 100 const GoogleServiceAuthError& error); |
| 94 | 101 |
| 95 void SimulateRefreshTokenFetched( | 102 void SimulateRefreshTokenFetched( |
| 96 AccountReconcilor* reconcilor, | 103 AccountReconcilor* reconcilor, |
| 97 const std::string& account_id, | 104 const std::string& account_id, |
| 98 const std::string& refresh_token); | 105 const std::string& refresh_token); |
| 99 | 106 |
| 100 private: | 107 private: |
| 101 content::TestBrowserThreadBundle bundle_; | 108 content::TestBrowserThreadBundle bundle_; |
| 102 scoped_ptr<TestingProfile> profile_; | 109 TestingProfile* profile_; |
| 103 FakeSigninManagerForTesting* signin_manager_; | 110 FakeSigninManagerForTesting* signin_manager_; |
| 104 FakeProfileOAuth2TokenService* token_service_; | 111 FakeProfileOAuth2TokenService* token_service_; |
| 105 MockAccountReconcilor* mock_reconcilor_; | 112 MockAccountReconcilor* mock_reconcilor_; |
| 106 net::FakeURLFetcherFactory url_fetcher_factory_; | 113 net::FakeURLFetcherFactory url_fetcher_factory_; |
| 114 scoped_ptr<TestingProfileManager> testing_profile_manager_; |
| 107 }; | 115 }; |
| 108 | 116 |
| 109 AccountReconcilorTest::AccountReconcilorTest() | 117 AccountReconcilorTest::AccountReconcilorTest() |
| 110 : signin_manager_(NULL), | 118 : signin_manager_(NULL), |
| 111 token_service_(NULL), | 119 token_service_(NULL), |
| 112 mock_reconcilor_(NULL), | 120 mock_reconcilor_(NULL), |
| 113 url_fetcher_factory_(NULL) {} | 121 url_fetcher_factory_(NULL) {} |
| 114 | 122 |
| 115 void AccountReconcilorTest::SetUp() { | 123 void AccountReconcilorTest::SetUp() { |
| 116 TestingProfile::Builder builder; | 124 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 117 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 125 switches::kNewProfileManagement); |
| 118 BuildFakeProfileOAuth2TokenService); | 126 |
| 119 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), | 127 testing_profile_manager_.reset( |
| 120 FakeSigninManagerBase::Build); | 128 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 121 builder.AddTestingFactory(AccountReconcilorFactory::GetInstance(), | 129 ASSERT_TRUE(testing_profile_manager_.get()->SetUp()); |
| 122 MockAccountReconcilor::Build); | 130 |
| 123 profile_ = builder.Build(); | 131 TestingProfile::TestingFactories factories; |
| 132 factories.push_back(std::make_pair(ChromeSigninClientFactory::GetInstance(), |
| 133 signin::BuildTestSigninClient)); |
| 134 factories.push_back(std::make_pair( |
| 135 ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 136 BuildFakeProfileOAuth2TokenService)); |
| 137 factories.push_back(std::make_pair(SigninManagerFactory::GetInstance(), |
| 138 FakeSigninManagerBase::Build)); |
| 139 factories.push_back(std::make_pair(AccountReconcilorFactory::GetInstance(), |
| 140 MockAccountReconcilor::Build)); |
| 141 |
| 142 profile_ = testing_profile_manager_.get()->CreateTestingProfile("name", |
| 143 scoped_ptr<PrefServiceSyncable>(), |
| 144 base::UTF8ToUTF16("name"), 0, std::string(), |
| 145 factories); |
| 124 | 146 |
| 125 signin_manager_ = | 147 signin_manager_ = |
| 126 static_cast<FakeSigninManagerForTesting*>( | 148 static_cast<FakeSigninManagerForTesting*>( |
| 127 SigninManagerFactory::GetForProfile(profile())); | 149 SigninManagerFactory::GetForProfile(profile())); |
| 128 | 150 |
| 129 token_service_ = | 151 token_service_ = |
| 130 static_cast<FakeProfileOAuth2TokenService*>( | 152 static_cast<FakeProfileOAuth2TokenService*>( |
| 131 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())); | 153 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())); |
| 132 } | 154 } |
| 133 | 155 |
| 134 void AccountReconcilorTest::TearDown() { | |
| 135 // Destroy the profile before all threads are torn down. | |
| 136 profile_.reset(); | |
| 137 } | |
| 138 | |
| 139 MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() { | 156 MockAccountReconcilor* AccountReconcilorTest::GetMockReconcilor() { |
| 140 if (!mock_reconcilor_) { | 157 if (!mock_reconcilor_) { |
| 141 mock_reconcilor_ = | 158 mock_reconcilor_ = |
| 142 static_cast<MockAccountReconcilor*>( | 159 static_cast<MockAccountReconcilor*>( |
| 143 AccountReconcilorFactory::GetForProfile(profile())); | 160 AccountReconcilorFactory::GetForProfile(profile())); |
| 144 } | 161 } |
| 145 | 162 |
| 146 return mock_reconcilor_; | 163 return mock_reconcilor_; |
| 147 } | 164 } |
| 148 | 165 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 162 | 179 |
| 163 TEST_F(AccountReconcilorTest, Basic) { | 180 TEST_F(AccountReconcilorTest, Basic) { |
| 164 AccountReconcilor* reconcilor = | 181 AccountReconcilor* reconcilor = |
| 165 AccountReconcilorFactory::GetForProfile(profile()); | 182 AccountReconcilorFactory::GetForProfile(profile()); |
| 166 ASSERT_TRUE(reconcilor); | 183 ASSERT_TRUE(reconcilor); |
| 167 ASSERT_EQ(token_service(), reconcilor->token_service()); | 184 ASSERT_EQ(token_service(), reconcilor->token_service()); |
| 168 } | 185 } |
| 169 | 186 |
| 170 #if !defined(OS_CHROMEOS) | 187 #if !defined(OS_CHROMEOS) |
| 171 | 188 |
| 189 // This method requires the use of the |TestSigninClient| to be created from the |
| 190 // |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded| |
| 191 // method with an empty implementation. On MacOS, the normal implementation |
| 192 // causes the try_bots to time out. |
| 172 TEST_F(AccountReconcilorTest, SigninManagerRegistration) { | 193 TEST_F(AccountReconcilorTest, SigninManagerRegistration) { |
| 173 AccountReconcilor* reconcilor = | 194 AccountReconcilor* reconcilor = |
| 174 AccountReconcilorFactory::GetForProfile(profile()); | 195 AccountReconcilorFactory::GetForProfile(profile()); |
| 175 ASSERT_TRUE(reconcilor); | 196 ASSERT_TRUE(reconcilor); |
| 176 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService()); | 197 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService()); |
| 177 | 198 |
| 199 signin_manager()->set_password("password"); |
| 178 signin_manager()->OnExternalSigninCompleted(kTestEmail); | 200 signin_manager()->OnExternalSigninCompleted(kTestEmail); |
| 179 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); | 201 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); |
| 180 | 202 |
| 181 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction()); | 203 EXPECT_CALL(*GetMockReconcilor(), PerformLogoutAllAccountsAction()); |
| 182 | 204 |
| 183 signin_manager()->SignOut(); | 205 signin_manager()->SignOut(); |
| 184 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService()); | 206 ASSERT_FALSE(reconcilor->IsRegisteredWithTokenService()); |
| 185 } | 207 } |
| 186 | 208 |
| 209 // This method requires the use of the |TestSigninClient| to be created from the |
| 210 // |ChromeSigninClientFactory| because it overrides the |GoogleSigninSucceeded| |
| 211 // method with an empty implementation. On MacOS, the normal implementation |
| 212 // causes the try_bots to time out. |
| 187 TEST_F(AccountReconcilorTest, Reauth) { | 213 TEST_F(AccountReconcilorTest, Reauth) { |
| 188 signin_manager()->SetAuthenticatedUsername(kTestEmail); | 214 signin_manager()->SetAuthenticatedUsername(kTestEmail); |
| 215 signin_manager()->set_password("password"); |
| 189 | 216 |
| 190 AccountReconcilor* reconcilor = | 217 AccountReconcilor* reconcilor = |
| 191 AccountReconcilorFactory::GetForProfile(profile()); | 218 AccountReconcilorFactory::GetForProfile(profile()); |
| 192 ASSERT_TRUE(reconcilor); | 219 ASSERT_TRUE(reconcilor); |
| 193 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); | 220 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); |
| 194 | 221 |
| 195 // Simulate reauth. The state of the reconcilor should not change. | 222 // Simulate reauth. The state of the reconcilor should not change. |
| 196 signin_manager()->OnExternalSigninCompleted(kTestEmail); | 223 signin_manager()->OnExternalSigninCompleted(kTestEmail); |
| 197 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); | 224 ASSERT_TRUE(reconcilor->IsRegisteredWithTokenService()); |
| 198 } | 225 } |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 557 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token", | 584 token_service()->IssueAllTokensForAccount("user@gmail.com", "access_token", |
| 558 base::Time::Now() + base::TimeDelta::FromHours(1)); | 585 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 559 token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token", | 586 token_service()->IssueAllTokensForAccount("other@gmail.com", "access_token", |
| 560 base::Time::Now() + base::TimeDelta::FromHours(1)); | 587 base::Time::Now() + base::TimeDelta::FromHours(1)); |
| 561 | 588 |
| 562 base::RunLoop().RunUntilIdle(); | 589 base::RunLoop().RunUntilIdle(); |
| 563 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com", | 590 SimulateMergeSessionCompleted(reconcilor, "user@gmail.com", |
| 564 GoogleServiceAuthError::AuthErrorNone()); | 591 GoogleServiceAuthError::AuthErrorNone()); |
| 565 ASSERT_FALSE(reconcilor->is_reconcile_started_); | 592 ASSERT_FALSE(reconcilor->is_reconcile_started_); |
| 566 } | 593 } |
| OLD | NEW |