Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1072)

Side by Side Diff: chrome/browser/ui/sync/one_click_signin_helper_unittest.cc

Issue 19567004: Convert SigninTracker to use OAuth2TokenService notifications (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
16 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
15 #include "chrome/browser/signin/fake_signin_manager.h" 17 #include "chrome/browser/signin/fake_signin_manager.h"
18 #include "chrome/browser/signin/profile_oauth2_token_service.h"
19 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16 #include "chrome/browser/signin/signin_manager.h" 20 #include "chrome/browser/signin/signin_manager.h"
17 #include "chrome/browser/signin/signin_manager_factory.h" 21 #include "chrome/browser/signin/signin_manager_factory.h"
18 #include "chrome/browser/signin/signin_names_io_thread.h" 22 #include "chrome/browser/signin/signin_names_io_thread.h"
19 #include "chrome/browser/signin/signin_promo.h" 23 #include "chrome/browser/signin/signin_promo.h"
20 #include "chrome/browser/sync/profile_sync_service_factory.h" 24 #include "chrome/browser/sync/profile_sync_service_factory.h"
21 #include "chrome/browser/sync/profile_sync_service_mock.h" 25 #include "chrome/browser/sync/profile_sync_service_mock.h"
22 #include "chrome/browser/sync/test_profile_sync_service.h" 26 #include "chrome/browser/sync/test_profile_sync_service.h"
23 #include "chrome/browser/ui/sync/one_click_signin_helper.h" 27 #include "chrome/browser/ui/sync/one_click_signin_helper.h"
28 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
29 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
24 #include "chrome/common/pref_names.h" 30 #include "chrome/common/pref_names.h"
25 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 31 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
26 #include "chrome/test/base/testing_browser_process.h" 32 #include "chrome/test/base/testing_browser_process.h"
27 #include "chrome/test/base/testing_pref_service_syncable.h" 33 #include "chrome/test/base/testing_pref_service_syncable.h"
28 #include "chrome/test/base/testing_profile.h" 34 #include "chrome/test/base/testing_profile.h"
29 #include "chrome/test/base/testing_profile_manager.h" 35 #include "chrome/test/base/testing_profile_manager.h"
30 #include "components/autofill/core/common/password_form.h" 36 #include "components/autofill/core/common/password_form.h"
31 #include "content/public/browser/browser_context.h" 37 #include "content/public/browser/browser_context.h"
32 #include "content/public/browser/navigation_details.h" 38 #include "content/public/browser/navigation_details.h"
33 #include "content/public/browser/web_contents.h" 39 #include "content/public/browser/web_contents.h"
(...skipping 21 matching lines...) Expand all
55 "?service=foo&continue=http://foo.google.com"; 61 "?service=foo&continue=http://foo.google.com";
56 62
57 class SigninManagerMock : public FakeSigninManager { 63 class SigninManagerMock : public FakeSigninManager {
58 public: 64 public:
59 explicit SigninManagerMock(Profile* profile) : FakeSigninManager(profile) { 65 explicit SigninManagerMock(Profile* profile) : FakeSigninManager(profile) {
60 Initialize(profile, NULL); 66 Initialize(profile, NULL);
61 } 67 }
62 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username)); 68 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username));
63 }; 69 };
64 70
71 static BrowserContextKeyedService* BuildSigninManagerMock(
72 content::BrowserContext* profile) {
73 return new SigninManagerMock(static_cast<Profile*>(profile));
74 }
75
65 class TestProfileIOData : public ProfileIOData { 76 class TestProfileIOData : public ProfileIOData {
66 public: 77 public:
67 TestProfileIOData(bool is_incognito, PrefService* pref_service, 78 TestProfileIOData(bool is_incognito, PrefService* pref_service,
68 PrefService* local_state, CookieSettings* cookie_settings) 79 PrefService* local_state, CookieSettings* cookie_settings)
69 : ProfileIOData(is_incognito) { 80 : ProfileIOData(is_incognito) {
70 // Initialize the IO members required for these tests, but keep them on 81 // Initialize the IO members required for these tests, but keep them on
71 // this thread since we don't use a background thread here. 82 // this thread since we don't use a background thread here.
72 google_services_username()->Init(prefs::kGoogleServicesUsername, 83 google_services_username()->Init(prefs::kGoogleServicesUsername,
73 pref_service); 84 pref_service);
74 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled, 85 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled,
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 : TestProfileSyncService(NULL, 195 : TestProfileSyncService(NULL,
185 profile, 196 profile,
186 NULL, 197 NULL,
187 ProfileSyncService::MANUAL_START, 198 ProfileSyncService::MANUAL_START,
188 false), // synchronous_backend_init 199 false), // synchronous_backend_init
189 first_setup_in_progress_(false) {} 200 first_setup_in_progress_(false) {}
190 201
191 bool first_setup_in_progress_; 202 bool first_setup_in_progress_;
192 }; 203 };
193 204
194 static BrowserContextKeyedService* BuildSigninManagerMock(
195 content::BrowserContext* profile) {
196 return new SigninManagerMock(static_cast<Profile*>(profile));
197 }
198
199 } // namespace 205 } // namespace
200 206
201 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness { 207 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness {
202 public: 208 public:
203 OneClickSigninHelperTest(); 209 OneClickSigninHelperTest();
204 210
205 virtual void SetUp() OVERRIDE; 211 virtual void SetUp() OVERRIDE;
206 virtual void TearDown() OVERRIDE; 212 virtual void TearDown() OVERRIDE;
207 213
208 // Creates the sign-in manager for tests. If |username| is 214 // Creates the sign-in manager for tests. If |username| is
209 // is not empty, the profile of the mock WebContents will be connected to 215 // is not empty, the profile of the mock WebContents will be connected to
210 // the given account. 216 // the given account.
211 void CreateSigninManager(const std::string& username); 217 void CreateSigninManager(const std::string& username);
212 218
213 // Set the ID of the signin process that the test will assume to be the 219 // Set the ID of the signin process that the test will assume to be the
214 // only process allowed to sign the user in to Chrome. 220 // only process allowed to sign the user in to Chrome.
215 void SetTrustedSigninProcessID(int id); 221 void SetTrustedSigninProcessID(int id);
216 222
217 void AddEmailToOneClickRejectedList(const std::string& email); 223 void AddEmailToOneClickRejectedList(const std::string& email);
218 void EnableOneClick(bool enable); 224 void EnableOneClick(bool enable);
219 void AllowSigninCookies(bool enable); 225 void AllowSigninCookies(bool enable);
220 void SetAllowedUsernamePattern(const std::string& pattern); 226 void SetAllowedUsernamePattern(const std::string& pattern);
221 ProfileSyncServiceMock* CreateProfileSyncServiceMock(); 227 ProfileSyncServiceMock* CreateProfileSyncServiceMock();
222 void SubmitGAIAPassword(OneClickSigninHelper* helper); 228 void SubmitGAIAPassword(OneClickSigninHelper* helper);
223 OneClickSigninHelper* SetupHelperForSignin();
224 229
225 SigninManagerMock* signin_manager_; 230 SigninManagerMock* signin_manager_;
231 FakeProfileOAuth2TokenService* fake_oauth2_token_service_;
226 232
227 protected: 233 protected:
228 GoogleServiceAuthError no_error_; 234 GoogleServiceAuthError no_error_;
229 235
230 private: 236 private:
237 // ChromeRenderViewHostTestHarness overrides:
238 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE;
239
231 // The ID of the signin process the test will assume to be trusted. 240 // The ID of the signin process the test will assume to be trusted.
232 // By default, set to the test RenderProcessHost's process ID, but 241 // By default, set to the test RenderProcessHost's process ID, but
233 // overridden by SetTrustedSigninProcessID. 242 // overridden by SetTrustedSigninProcessID.
234 int trusted_signin_process_id_; 243 int trusted_signin_process_id_;
235 244
236 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); 245 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest);
237 }; 246 };
238 247
239 OneClickSigninHelperTest::OneClickSigninHelperTest() 248 OneClickSigninHelperTest::OneClickSigninHelperTest()
240 : no_error_(GoogleServiceAuthError::NONE), 249 : signin_manager_(NULL),
250 fake_oauth2_token_service_(NULL),
251 no_error_(GoogleServiceAuthError::NONE),
241 trusted_signin_process_id_(-1) { 252 trusted_signin_process_id_(-1) {
242 } 253 }
243 254
244 void OneClickSigninHelperTest::SetUp() { 255 void OneClickSigninHelperTest::SetUp() {
245 signin::ForceWebBasedSigninFlowForTesting(true); 256 signin::ForceWebBasedSigninFlowForTesting(true);
246 content::RenderViewHostTestHarness::SetUp(); 257 content::RenderViewHostTestHarness::SetUp();
247 SetTrustedSigninProcessID(process()->GetID()); 258 SetTrustedSigninProcessID(process()->GetID());
248 } 259 }
249 260
250 void OneClickSigninHelperTest::TearDown() { 261 void OneClickSigninHelperTest::TearDown() {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 325
315 void OneClickSigninHelperTest::SubmitGAIAPassword( 326 void OneClickSigninHelperTest::SubmitGAIAPassword(
316 OneClickSigninHelper* helper) { 327 OneClickSigninHelper* helper) {
317 autofill::PasswordForm password_form; 328 autofill::PasswordForm password_form;
318 password_form.origin = GURL("https://accounts.google.com"); 329 password_form.origin = GURL("https://accounts.google.com");
319 password_form.signon_realm = "https://accounts.google.com"; 330 password_form.signon_realm = "https://accounts.google.com";
320 password_form.password_value = UTF8ToUTF16("password"); 331 password_form.password_value = UTF8ToUTF16("password");
321 helper->PasswordSubmitted(password_form); 332 helper->PasswordSubmitted(password_form);
322 } 333 }
323 334
335 content::BrowserContext* OneClickSigninHelperTest::CreateBrowserContext() {
336 TestingProfile::Builder builder;
337 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
338 FakeProfileOAuth2TokenService::Build);
339 scoped_ptr<TestingProfile> profile = builder.Build();
340
341 fake_oauth2_token_service_ =
342 static_cast<FakeProfileOAuth2TokenService*>(
343 ProfileOAuth2TokenServiceFactory::GetForProfile(profile.get()));
344
345 return profile.release();
346 }
347
324 class OneClickSigninHelperIOTest : public OneClickSigninHelperTest { 348 class OneClickSigninHelperIOTest : public OneClickSigninHelperTest {
325 public: 349 public:
326 OneClickSigninHelperIOTest(); 350 OneClickSigninHelperIOTest();
327 351
328 virtual void SetUp() OVERRIDE; 352 virtual void SetUp() OVERRIDE;
329 353
330 TestProfileIOData* CreateTestProfileIOData(bool is_incognito); 354 TestProfileIOData* CreateTestProfileIOData(bool is_incognito);
331 355
332 protected: 356 protected:
333 TestingProfileManager testing_profile_manager_; 357 TestingProfileManager testing_profile_manager_;
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
647 671
648 // If Chrome signin is triggered from a webstore install, and user chooses to 672 // If Chrome signin is triggered from a webstore install, and user chooses to
649 // config sync, then Chrome should redirect immediately to sync settings page, 673 // config sync, then Chrome should redirect immediately to sync settings page,
650 // and upon successful setup, redirect back to webstore. 674 // and upon successful setup, redirect back to webstore.
651 TEST_F(OneClickSigninHelperTest, SigninFromWebstoreWithConfigSyncfirst) { 675 TEST_F(OneClickSigninHelperTest, SigninFromWebstoreWithConfigSyncfirst) {
652 CreateSigninManager(std::string()); 676 CreateSigninManager(std::string());
653 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)) 677 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_))
654 .WillRepeatedly(Return(true)); 678 .WillRepeatedly(Return(true));
655 679
656 ProfileSyncServiceMock* sync_service = CreateProfileSyncServiceMock(); 680 ProfileSyncServiceMock* sync_service = CreateProfileSyncServiceMock();
657 EXPECT_CALL(*sync_service, SetSetupInProgress(true));
658 EXPECT_CALL(*sync_service, AddObserver(_)).Times(AtLeast(1)); 681 EXPECT_CALL(*sync_service, AddObserver(_)).Times(AtLeast(1));
659 EXPECT_CALL(*sync_service, RemoveObserver(_)).Times(AtLeast(1)); 682 EXPECT_CALL(*sync_service, RemoveObserver(_)).Times(AtLeast(1));
660 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true)); 683 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true));
661 684
662 content::WebContents* contents = web_contents(); 685 content::WebContents* contents = web_contents();
663 686
664 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL); 687 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL);
665 OneClickSigninHelper* helper = 688 OneClickSigninHelper* helper =
666 OneClickSigninHelper::FromWebContents(contents); 689 OneClickSigninHelper::FromWebContents(contents);
667 helper->SetDoNotClearPendingEmailForTesting(); 690 helper->SetDoNotClearPendingEmailForTesting();
691 helper->set_do_not_start_sync_for_testing();
668 692
669 GURL continueUrl("https://chrome.google.com/webstore?source=5"); 693 GURL continueUrl("https://chrome.google.com/webstore?source=5");
670 OneClickSigninHelper::ShowInfoBarUIThread( 694 OneClickSigninHelper::ShowInfoBarUIThread(
671 "session_index", "user@gmail.com", 695 "session_index", "user@gmail.com",
672 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT, 696 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT,
673 signin::SOURCE_WEBSTORE_INSTALL, 697 signin::SOURCE_WEBSTORE_INSTALL,
674 continueUrl, process()->GetID(), rvh()->GetRoutingID()); 698 continueUrl, process()->GetID(), rvh()->GetRoutingID());
675 699
676 SubmitGAIAPassword(helper); 700 SubmitGAIAPassword(helper);
677 701
678 NavigateAndCommit(GURL("https://chrome.google.com/webstore?source=3")); 702 NavigateAndCommit(GURL("https://chrome.google.com/webstore?source=3"));
679 helper->DidStopLoading(rvh()); 703 helper->DidStopLoading(rvh());
680
681 helper->OnStateChanged(); 704 helper->OnStateChanged();
682 EXPECT_EQ(GURL(continueUrl), contents->GetURL()); 705 EXPECT_EQ(GURL(continueUrl), contents->GetURL());
683 EXPECT_EQ("user@gmail.com", signin_manager_->GetAuthenticatedUsername());
684 } 706 }
685 707
686 // Checks that the state of OneClickSigninHelper is cleaned when there is a 708 // Checks that the state of OneClickSigninHelper is cleaned when there is a
687 // navigation away from the sign in flow that is not triggered by the 709 // navigation away from the sign in flow that is not triggered by the
688 // web contents. 710 // web contents.
689 TEST_F(OneClickSigninHelperTest, CleanTransientStateOnNavigate) { 711 TEST_F(OneClickSigninHelperTest, CleanTransientStateOnNavigate) {
690 content::WebContents* contents = web_contents(); 712 content::WebContents* contents = web_contents();
691 713
692 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL); 714 OneClickSigninHelper::CreateForWebContentsWithPasswordManager(contents, NULL);
693 OneClickSigninHelper* helper = 715 OneClickSigninHelper* helper =
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true)); 891 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true));
870 892
871 // Simulate a policy disabling sync by writing kSyncManaged directly. 893 // Simulate a policy disabling sync by writing kSyncManaged directly.
872 // We should still offer to sign in the browser. 894 // We should still offer to sign in the browser.
873 profile()->GetTestingPrefService()->SetManagedPref( 895 profile()->GetTestingPrefService()->SetManagedPref(
874 prefs::kSyncManaged, base::Value::CreateBooleanValue(true)); 896 prefs::kSyncManaged, base::Value::CreateBooleanValue(true));
875 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, 897 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
876 OneClickSigninHelper::CanOfferOnIOThreadImpl( 898 OneClickSigninHelper::CanOfferOnIOThreadImpl(
877 valid_gaia_url_, std::string(), &request_, io_data.get())); 899 valid_gaia_url_, std::string(), &request_, io_data.get()));
878 } 900 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/sync/one_click_signin_helper.cc ('k') | chrome/browser/ui/sync/one_click_signin_sync_starter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698