OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/prefs/pref_service.h" | 5 #include "base/prefs/pref_service.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "base/values.h" | 7 #include "base/values.h" |
8 #include "chrome/browser/chrome_notification_types.h" | |
8 #include "chrome/browser/content_settings/cookie_settings.h" | 9 #include "chrome/browser/content_settings/cookie_settings.h" |
9 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | 10 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
10 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 11 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/profiles/profile_info_cache.h" | 13 #include "chrome/browser/profiles/profile_info_cache.h" |
13 #include "chrome/browser/profiles/profile_io_data.h" | 14 #include "chrome/browser/profiles/profile_io_data.h" |
14 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
15 #include "chrome/browser/signin/fake_signin_manager.h" | 16 #include "chrome/browser/signin/fake_signin_manager.h" |
17 #include "chrome/browser/signin/profile_oauth2_token_service.h" | |
18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
16 #include "chrome/browser/signin/signin_manager.h" | 19 #include "chrome/browser/signin/signin_manager.h" |
17 #include "chrome/browser/signin/signin_manager_factory.h" | 20 #include "chrome/browser/signin/signin_manager_factory.h" |
18 #include "chrome/browser/signin/signin_names_io_thread.h" | 21 #include "chrome/browser/signin/signin_names_io_thread.h" |
19 #include "chrome/browser/signin/signin_promo.h" | 22 #include "chrome/browser/signin/signin_promo.h" |
20 #include "chrome/browser/sync/profile_sync_service_factory.h" | 23 #include "chrome/browser/sync/profile_sync_service_factory.h" |
21 #include "chrome/browser/sync/profile_sync_service_mock.h" | 24 #include "chrome/browser/sync/profile_sync_service_mock.h" |
22 #include "chrome/browser/sync/test_profile_sync_service.h" | 25 #include "chrome/browser/sync/test_profile_sync_service.h" |
23 #include "chrome/browser/ui/sync/one_click_signin_helper.h" | 26 #include "chrome/browser/ui/sync/one_click_signin_helper.h" |
27 #include "chrome/browser/ui/webui/signin/login_ui_service.h" | |
28 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h" | |
24 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
25 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 30 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
26 #include "chrome/test/base/testing_browser_process.h" | 31 #include "chrome/test/base/testing_browser_process.h" |
27 #include "chrome/test/base/testing_pref_service_syncable.h" | 32 #include "chrome/test/base/testing_pref_service_syncable.h" |
28 #include "chrome/test/base/testing_profile.h" | 33 #include "chrome/test/base/testing_profile.h" |
29 #include "chrome/test/base/testing_profile_manager.h" | 34 #include "chrome/test/base/testing_profile_manager.h" |
30 #include "content/public/browser/browser_context.h" | 35 #include "content/public/browser/browser_context.h" |
31 #include "content/public/browser/navigation_details.h" | 36 #include "content/public/browser/navigation_details.h" |
32 #include "content/public/browser/web_contents.h" | 37 #include "content/public/browser/web_contents.h" |
33 #include "content/public/common/frame_navigate_params.h" | 38 #include "content/public/common/frame_navigate_params.h" |
(...skipping 21 matching lines...) Expand all Loading... | |
55 "?service=foo&continue=http://foo.google.com"; | 60 "?service=foo&continue=http://foo.google.com"; |
56 | 61 |
57 class SigninManagerMock : public FakeSigninManager { | 62 class SigninManagerMock : public FakeSigninManager { |
58 public: | 63 public: |
59 explicit SigninManagerMock(Profile* profile) : FakeSigninManager(profile) { | 64 explicit SigninManagerMock(Profile* profile) : FakeSigninManager(profile) { |
60 Initialize(profile, NULL); | 65 Initialize(profile, NULL); |
61 } | 66 } |
62 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username)); | 67 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username)); |
63 }; | 68 }; |
64 | 69 |
70 static BrowserContextKeyedService* BuildSigninManagerMock( | |
71 content::BrowserContext* profile) { | |
72 return new SigninManagerMock(static_cast<Profile*>(profile)); | |
73 } | |
74 | |
75 class OAuth2TokenServiceMock : public ProfileOAuth2TokenService { | |
Andrew T Wilson (Slow)
2013/08/19 15:23:06
Technically this isn't a Mock, it's a Fake. Again,
Roger Tawa OOO till Jul 10th
2013/08/29 19:28:41
Done.
| |
76 public: | |
77 // For testing: set the refresh token to be used. | |
78 void set_refresh_token(const std::string& refresh_token) { | |
79 refresh_token_ = refresh_token; | |
80 } | |
81 | |
82 protected: | |
83 virtual std::string GetRefreshToken() OVERRIDE { return refresh_token_; } | |
84 | |
85 private: | |
86 // OAuth2TokenService implementation. | |
87 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE { | |
88 return NULL; | |
89 } | |
90 | |
91 std::string refresh_token_; | |
92 }; | |
93 | |
94 static BrowserContextKeyedService* BuildOAuth2TokenServiceMock( | |
95 content::BrowserContext* profile) { | |
96 OAuth2TokenServiceMock* token_service = new OAuth2TokenServiceMock; | |
97 token_service->Initialize(static_cast<Profile*>(profile)); | |
98 return token_service; | |
99 } | |
100 | |
101 class FakeLoginUI : public LoginUIService::LoginUI { | |
102 virtual void FocusUI() OVERRIDE {} | |
103 virtual void CloseUI() OVERRIDE {} | |
104 }; | |
105 | |
65 class TestProfileIOData : public ProfileIOData { | 106 class TestProfileIOData : public ProfileIOData { |
66 public: | 107 public: |
67 TestProfileIOData(bool is_incognito, PrefService* pref_service, | 108 TestProfileIOData(bool is_incognito, PrefService* pref_service, |
68 PrefService* local_state, CookieSettings* cookie_settings) | 109 PrefService* local_state, CookieSettings* cookie_settings) |
69 : ProfileIOData(is_incognito) { | 110 : ProfileIOData(is_incognito) { |
70 // Initialize the IO members required for these tests, but keep them on | 111 // Initialize the IO members required for these tests, but keep them on |
71 // this thread since we don't use a background thread here. | 112 // this thread since we don't use a background thread here. |
72 google_services_username()->Init(prefs::kGoogleServicesUsername, | 113 google_services_username()->Init(prefs::kGoogleServicesUsername, |
73 pref_service); | 114 pref_service); |
74 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled, | 115 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled, |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 : TestProfileSyncService(NULL, | 225 : TestProfileSyncService(NULL, |
185 profile, | 226 profile, |
186 NULL, | 227 NULL, |
187 ProfileSyncService::MANUAL_START, | 228 ProfileSyncService::MANUAL_START, |
188 false), // synchronous_backend_init | 229 false), // synchronous_backend_init |
189 first_setup_in_progress_(false) {} | 230 first_setup_in_progress_(false) {} |
190 | 231 |
191 bool first_setup_in_progress_; | 232 bool first_setup_in_progress_; |
192 }; | 233 }; |
193 | 234 |
194 static BrowserContextKeyedService* BuildSigninManagerMock( | |
195 content::BrowserContext* profile) { | |
196 return new SigninManagerMock(static_cast<Profile*>(profile)); | |
197 } | |
198 | |
199 } // namespace | 235 } // namespace |
200 | 236 |
201 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness { | 237 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness { |
202 public: | 238 public: |
203 OneClickSigninHelperTest(); | 239 OneClickSigninHelperTest(); |
204 | 240 |
205 virtual void SetUp() OVERRIDE; | 241 virtual void SetUp() OVERRIDE; |
206 virtual void TearDown() OVERRIDE; | 242 virtual void TearDown() OVERRIDE; |
207 | 243 |
208 // Creates the sign-in manager for tests. If |use_incognito| is true then | 244 // Creates the sign-in manager for tests. If |use_incognito| is true then |
209 // a WebContents for an incognito profile is created. If |username| is | 245 // a WebContents for an incognito profile is created. If |username| is |
210 // is not empty, the profile of the mock WebContents will be connected to | 246 // is not empty, the profile of the mock WebContents will be connected to |
211 // the given account. | 247 // the given account. |
212 void CreateSigninManager(bool use_incognito, const std::string& username); | 248 void CreateSigninManager(bool use_incognito, const std::string& username); |
213 | 249 |
250 // Creates the oauth2 token service, and provides a way to fire a dummy | |
251 // fresh token available event. | |
252 void CreateOAuth2TokenService(); | |
253 | |
214 // Set the ID of the signin process that the test will assume to be the | 254 // Set the ID of the signin process that the test will assume to be the |
215 // only process allowed to sign the user in to Chrome. | 255 // only process allowed to sign the user in to Chrome. |
216 void SetTrustedSigninProcessID(int id); | 256 void SetTrustedSigninProcessID(int id); |
217 | 257 |
218 void AddEmailToOneClickRejectedList(const std::string& email); | 258 void AddEmailToOneClickRejectedList(const std::string& email); |
219 void EnableOneClick(bool enable); | 259 void EnableOneClick(bool enable); |
220 void AllowSigninCookies(bool enable); | 260 void AllowSigninCookies(bool enable); |
221 void SetAllowedUsernamePattern(const std::string& pattern); | 261 void SetAllowedUsernamePattern(const std::string& pattern); |
222 ProfileSyncServiceMock* CreateProfileSyncServiceMock(); | 262 ProfileSyncServiceMock* CreateProfileSyncServiceMock(); |
223 void SubmitGAIAPassword(OneClickSigninHelper* helper); | 263 void SubmitGAIAPassword(OneClickSigninHelper* helper); |
224 OneClickSigninHelper* SetupHelperForSignin(); | 264 OneClickSigninHelper* SetupHelperForSignin(); |
225 | 265 |
226 SigninManagerMock* signin_manager_; | 266 SigninManagerMock* signin_manager_; |
267 OAuth2TokenServiceMock* token_service_; | |
227 | 268 |
228 protected: | 269 protected: |
229 GoogleServiceAuthError no_error_; | 270 GoogleServiceAuthError no_error_; |
230 | 271 |
231 private: | 272 private: |
232 // The ID of the signin process the test will assume to be trusted. | 273 // The ID of the signin process the test will assume to be trusted. |
233 // By default, set to the test RenderProcessHost's process ID, but | 274 // By default, set to the test RenderProcessHost's process ID, but |
234 // overridden by SetTrustedSigninProcessID. | 275 // overridden by SetTrustedSigninProcessID. |
235 int trusted_signin_process_id_; | 276 int trusted_signin_process_id_; |
236 | 277 |
237 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); | 278 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); |
238 }; | 279 }; |
239 | 280 |
240 OneClickSigninHelperTest::OneClickSigninHelperTest() | 281 OneClickSigninHelperTest::OneClickSigninHelperTest() |
241 : no_error_(GoogleServiceAuthError::NONE), | 282 : signin_manager_(NULL), |
283 token_service_(NULL), | |
284 no_error_(GoogleServiceAuthError::NONE), | |
242 trusted_signin_process_id_(-1) { | 285 trusted_signin_process_id_(-1) { |
243 } | 286 } |
244 | 287 |
245 void OneClickSigninHelperTest::SetUp() { | 288 void OneClickSigninHelperTest::SetUp() { |
246 signin::ForceWebBasedSigninFlowForTesting(true); | 289 signin::ForceWebBasedSigninFlowForTesting(true); |
247 content::RenderViewHostTestHarness::SetUp(); | 290 content::RenderViewHostTestHarness::SetUp(); |
248 SetTrustedSigninProcessID(process()->GetID()); | 291 SetTrustedSigninProcessID(process()->GetID()); |
249 } | 292 } |
250 | 293 |
251 void OneClickSigninHelperTest::TearDown() { | 294 void OneClickSigninHelperTest::TearDown() { |
(...skipping 14 matching lines...) Expand all Loading... | |
266 profile(), BuildSigninManagerMock)); | 309 profile(), BuildSigninManagerMock)); |
267 if (signin_manager_) | 310 if (signin_manager_) |
268 signin_manager_->SetSigninProcess(trusted_signin_process_id_); | 311 signin_manager_->SetSigninProcess(trusted_signin_process_id_); |
269 | 312 |
270 if (!username.empty()) { | 313 if (!username.empty()) { |
271 ASSERT_TRUE(signin_manager_); | 314 ASSERT_TRUE(signin_manager_); |
272 signin_manager_->SetAuthenticatedUsername(username); | 315 signin_manager_->SetAuthenticatedUsername(username); |
273 } | 316 } |
274 } | 317 } |
275 | 318 |
319 void OneClickSigninHelperTest::CreateOAuth2TokenService() { | |
320 token_service_ = static_cast<OAuth2TokenServiceMock*>( | |
321 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
322 profile(), BuildOAuth2TokenServiceMock)); | |
323 } | |
324 | |
276 void OneClickSigninHelperTest::EnableOneClick(bool enable) { | 325 void OneClickSigninHelperTest::EnableOneClick(bool enable) { |
277 PrefService* pref_service = profile()->GetPrefs(); | 326 PrefService* pref_service = profile()->GetPrefs(); |
278 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); | 327 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); |
279 } | 328 } |
280 | 329 |
281 void OneClickSigninHelperTest::AddEmailToOneClickRejectedList( | 330 void OneClickSigninHelperTest::AddEmailToOneClickRejectedList( |
282 const std::string& email) { | 331 const std::string& email) { |
283 PrefService* pref_service = profile()->GetPrefs(); | 332 PrefService* pref_service = profile()->GetPrefs(); |
284 ListPrefUpdate updater(pref_service, | 333 ListPrefUpdate updater(pref_service, |
285 prefs::kReverseAutologinRejectedEmailList); | 334 prefs::kReverseAutologinRejectedEmailList); |
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 OneClickSigninHelper::ShowInfoBarUIThread( | 684 OneClickSigninHelper::ShowInfoBarUIThread( |
636 "session_index", "email", OneClickSigninHelper::AUTO_ACCEPT_ACCEPTED, | 685 "session_index", "email", OneClickSigninHelper::AUTO_ACCEPT_ACCEPTED, |
637 signin::SOURCE_UNKNOWN, GURL(), process()->GetID(), | 686 signin::SOURCE_UNKNOWN, GURL(), process()->GetID(), |
638 rvh()->GetRoutingID()); | 687 rvh()->GetRoutingID()); |
639 } | 688 } |
640 | 689 |
641 // If Chrome signin is triggered from a webstore install, and user chooses to | 690 // If Chrome signin is triggered from a webstore install, and user chooses to |
642 // config sync, then Chrome should redirect immediately to sync settings page, | 691 // config sync, then Chrome should redirect immediately to sync settings page, |
643 // and upon successful setup, redirect back to webstore. | 692 // and upon successful setup, redirect back to webstore. |
644 TEST_F(OneClickSigninHelperTest, SigninFromWebstoreWithConfigSyncfirst) { | 693 TEST_F(OneClickSigninHelperTest, SigninFromWebstoreWithConfigSyncfirst) { |
694 CreateOAuth2TokenService(); | |
645 CreateSigninManager(false, std::string()); | 695 CreateSigninManager(false, std::string()); |
646 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)) | 696 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)) |
647 .WillRepeatedly(Return(true)); | 697 .WillRepeatedly(Return(true)); |
648 | 698 |
649 CreateProfileSyncServiceMock(); | 699 CreateProfileSyncServiceMock(); |
650 | 700 |
651 content::WebContents* contents = web_contents(); | 701 content::WebContents* contents = web_contents(); |
652 | 702 |
653 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL); | 703 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL); |
654 OneClickSigninHelper* helper = | 704 OneClickSigninHelper* helper = |
655 OneClickSigninHelper::FromWebContents(contents); | 705 OneClickSigninHelper::FromWebContents(contents); |
656 helper->SetDoNotClearPendingEmailForTesting(); | 706 helper->SetDoNotClearPendingEmailForTesting(); |
657 | 707 |
658 GURL continueUrl("https://chrome.google.com/webstore?source=5"); | 708 GURL continueUrl("https://chrome.google.com/webstore?source=5"); |
659 OneClickSigninHelper::ShowInfoBarUIThread( | 709 OneClickSigninHelper::ShowInfoBarUIThread( |
660 "session_index", "user@gmail.com", | 710 "session_index", "user@gmail.com", |
661 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT, | 711 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT, |
662 signin::SOURCE_WEBSTORE_INSTALL, | 712 signin::SOURCE_WEBSTORE_INSTALL, |
663 continueUrl, process()->GetID(), rvh()->GetRoutingID()); | 713 continueUrl, process()->GetID(), rvh()->GetRoutingID()); |
664 | 714 |
665 SubmitGAIAPassword(helper); | 715 SubmitGAIAPassword(helper); |
666 | 716 |
667 NavigateAndCommit(GURL("https://chrome.google.com/webstore?source=3")); | 717 NavigateAndCommit(GURL("https://chrome.google.com/webstore?source=3")); |
668 helper->DidStopLoading(rvh()); | 718 helper->DidStopLoading(rvh()); |
669 | 719 |
720 token_service_->set_refresh_token("refresh_token"); | |
721 FakeLoginUI fake_login_ui; | |
722 LoginUIServiceFactory::GetForProfile(profile())->SetLoginUI(&fake_login_ui); | |
723 GoogleServiceSigninSuccessDetails details("user@gmail.com", "password"); | |
724 content::NotificationService::current()->Notify( | |
725 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | |
726 content::Source<Profile>(profile()), | |
727 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | |
728 LoginUIServiceFactory::GetForProfile(profile())-> | |
729 LoginUIClosed(&fake_login_ui); | |
730 | |
670 helper->OnStateChanged(); | 731 helper->OnStateChanged(); |
671 EXPECT_EQ(GURL(continueUrl), contents->GetURL()); | 732 EXPECT_EQ(GURL(continueUrl), contents->GetURL()); |
672 EXPECT_EQ("user@gmail.com", signin_manager_->GetAuthenticatedUsername()); | 733 EXPECT_EQ("user@gmail.com", signin_manager_->GetAuthenticatedUsername()); |
673 } | 734 } |
674 | 735 |
675 // Checks that the state of OneClickSigninHelper is cleaned when there is a | 736 // Checks that the state of OneClickSigninHelper is cleaned when there is a |
676 // navigation away from the sign in flow that is not triggered by the | 737 // navigation away from the sign in flow that is not triggered by the |
677 // web contents. | 738 // web contents. |
678 TEST_F(OneClickSigninHelperTest, CleanTransientStateOnNavigate) { | 739 TEST_F(OneClickSigninHelperTest, CleanTransientStateOnNavigate) { |
679 content::WebContents* contents = web_contents(); | 740 content::WebContents* contents = web_contents(); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
842 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true)); | 903 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true)); |
843 | 904 |
844 // Simulate a policy disabling sync by writing kSyncManaged directly. | 905 // Simulate a policy disabling sync by writing kSyncManaged directly. |
845 // We should still offer to sign in the browser. | 906 // We should still offer to sign in the browser. |
846 profile()->GetTestingPrefService()->SetManagedPref( | 907 profile()->GetTestingPrefService()->SetManagedPref( |
847 prefs::kSyncManaged, base::Value::CreateBooleanValue(true)); | 908 prefs::kSyncManaged, base::Value::CreateBooleanValue(true)); |
848 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | 909 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, |
849 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 910 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
850 valid_gaia_url_, std::string(), &request_, io_data.get())); | 911 valid_gaia_url_, std::string(), &request_, io_data.get())); |
851 } | 912 } |
OLD | NEW |