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

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 "content/public/browser/browser_context.h" 36 #include "content/public/browser/browser_context.h"
31 #include "content/public/browser/navigation_details.h" 37 #include "content/public/browser/navigation_details.h"
32 #include "content/public/browser/web_contents.h" 38 #include "content/public/browser/web_contents.h"
33 #include "content/public/common/frame_navigate_params.h" 39 #include "content/public/common/frame_navigate_params.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
76 class FakeLoginUI : public LoginUIService::LoginUI {
77 virtual void FocusUI() OVERRIDE {}
78 virtual void CloseUI() OVERRIDE {}
79 };
80
65 class TestProfileIOData : public ProfileIOData { 81 class TestProfileIOData : public ProfileIOData {
66 public: 82 public:
67 TestProfileIOData(bool is_incognito, PrefService* pref_service, 83 TestProfileIOData(bool is_incognito, PrefService* pref_service,
68 PrefService* local_state, CookieSettings* cookie_settings) 84 PrefService* local_state, CookieSettings* cookie_settings)
69 : ProfileIOData(is_incognito) { 85 : ProfileIOData(is_incognito) {
70 // Initialize the IO members required for these tests, but keep them on 86 // Initialize the IO members required for these tests, but keep them on
71 // this thread since we don't use a background thread here. 87 // this thread since we don't use a background thread here.
72 google_services_username()->Init(prefs::kGoogleServicesUsername, 88 google_services_username()->Init(prefs::kGoogleServicesUsername,
73 pref_service); 89 pref_service);
74 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled, 90 reverse_autologin_enabled()->Init(prefs::kReverseAutologinEnabled,
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 : TestProfileSyncService(NULL, 200 : TestProfileSyncService(NULL,
185 profile, 201 profile,
186 NULL, 202 NULL,
187 ProfileSyncService::MANUAL_START, 203 ProfileSyncService::MANUAL_START,
188 false), // synchronous_backend_init 204 false), // synchronous_backend_init
189 first_setup_in_progress_(false) {} 205 first_setup_in_progress_(false) {}
190 206
191 bool first_setup_in_progress_; 207 bool first_setup_in_progress_;
192 }; 208 };
193 209
194 static BrowserContextKeyedService* BuildSigninManagerMock(
195 content::BrowserContext* profile) {
196 return new SigninManagerMock(static_cast<Profile*>(profile));
197 }
198
199 } // namespace 210 } // namespace
200 211
201 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness { 212 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness {
202 public: 213 public:
203 OneClickSigninHelperTest(); 214 OneClickSigninHelperTest();
204 215
205 virtual void SetUp() OVERRIDE; 216 virtual void SetUp() OVERRIDE;
206 virtual void TearDown() OVERRIDE; 217 virtual void TearDown() OVERRIDE;
207 218
208 // Creates the sign-in manager for tests. If |username| is 219 // Creates the sign-in manager for tests. If |username| is
209 // is not empty, the profile of the mock WebContents will be connected to 220 // is not empty, the profile of the mock WebContents will be connected to
210 // the given account. 221 // the given account.
211 void CreateSigninManager(const std::string& username); 222 void CreateSigninManager(const std::string& username);
212 223
213 // Set the ID of the signin process that the test will assume to be the 224 // 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. 225 // only process allowed to sign the user in to Chrome.
215 void SetTrustedSigninProcessID(int id); 226 void SetTrustedSigninProcessID(int id);
216 227
217 void AddEmailToOneClickRejectedList(const std::string& email); 228 void AddEmailToOneClickRejectedList(const std::string& email);
218 void EnableOneClick(bool enable); 229 void EnableOneClick(bool enable);
219 void AllowSigninCookies(bool enable); 230 void AllowSigninCookies(bool enable);
220 void SetAllowedUsernamePattern(const std::string& pattern); 231 void SetAllowedUsernamePattern(const std::string& pattern);
221 ProfileSyncServiceMock* CreateProfileSyncServiceMock(); 232 ProfileSyncServiceMock* CreateProfileSyncServiceMock();
222 void SubmitGAIAPassword(OneClickSigninHelper* helper); 233 void SubmitGAIAPassword(OneClickSigninHelper* helper);
223 OneClickSigninHelper* SetupHelperForSignin();
224 234
225 SigninManagerMock* signin_manager_; 235 SigninManagerMock* signin_manager_;
236 FakeProfileOAuth2TokenService* fake_oauth2_token_service_;
226 237
227 protected: 238 protected:
228 GoogleServiceAuthError no_error_; 239 GoogleServiceAuthError no_error_;
229 240
230 private: 241 private:
242 // ChromeRenderViewHostTestHarness overrides:
243 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE;
244
231 // The ID of the signin process the test will assume to be trusted. 245 // 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 246 // By default, set to the test RenderProcessHost's process ID, but
233 // overridden by SetTrustedSigninProcessID. 247 // overridden by SetTrustedSigninProcessID.
234 int trusted_signin_process_id_; 248 int trusted_signin_process_id_;
235 249
236 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); 250 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest);
237 }; 251 };
238 252
239 OneClickSigninHelperTest::OneClickSigninHelperTest() 253 OneClickSigninHelperTest::OneClickSigninHelperTest()
240 : no_error_(GoogleServiceAuthError::NONE), 254 : signin_manager_(NULL),
255 fake_oauth2_token_service_(NULL),
256 no_error_(GoogleServiceAuthError::NONE),
241 trusted_signin_process_id_(-1) { 257 trusted_signin_process_id_(-1) {
242 } 258 }
243 259
244 void OneClickSigninHelperTest::SetUp() { 260 void OneClickSigninHelperTest::SetUp() {
245 signin::ForceWebBasedSigninFlowForTesting(true); 261 signin::ForceWebBasedSigninFlowForTesting(true);
246 content::RenderViewHostTestHarness::SetUp(); 262 content::RenderViewHostTestHarness::SetUp();
247 SetTrustedSigninProcessID(process()->GetID()); 263 SetTrustedSigninProcessID(process()->GetID());
248 } 264 }
249 265
250 void OneClickSigninHelperTest::TearDown() { 266 void OneClickSigninHelperTest::TearDown() {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 330
315 void OneClickSigninHelperTest::SubmitGAIAPassword( 331 void OneClickSigninHelperTest::SubmitGAIAPassword(
316 OneClickSigninHelper* helper) { 332 OneClickSigninHelper* helper) {
317 content::PasswordForm password_form; 333 content::PasswordForm password_form;
318 password_form.origin = GURL("https://accounts.google.com"); 334 password_form.origin = GURL("https://accounts.google.com");
319 password_form.signon_realm = "https://accounts.google.com"; 335 password_form.signon_realm = "https://accounts.google.com";
320 password_form.password_value = UTF8ToUTF16("password"); 336 password_form.password_value = UTF8ToUTF16("password");
321 helper->PasswordSubmitted(password_form); 337 helper->PasswordSubmitted(password_form);
322 } 338 }
323 339
340 content::BrowserContext* OneClickSigninHelperTest::CreateBrowserContext() {
341 TestingProfile::Builder builder;
342 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
343 FakeProfileOAuth2TokenService::Build);
344 scoped_ptr<TestingProfile> profile = builder.Build();
345
346 fake_oauth2_token_service_ =
347 static_cast<FakeProfileOAuth2TokenService*>(
348 ProfileOAuth2TokenServiceFactory::GetForProfile(profile.get()));
349
350 return profile.release();
351 }
352
324 class OneClickSigninHelperIOTest : public OneClickSigninHelperTest { 353 class OneClickSigninHelperIOTest : public OneClickSigninHelperTest {
325 public: 354 public:
326 OneClickSigninHelperIOTest(); 355 OneClickSigninHelperIOTest();
327 356
328 virtual void SetUp() OVERRIDE; 357 virtual void SetUp() OVERRIDE;
329 358
330 TestProfileIOData* CreateTestProfileIOData(bool is_incognito); 359 TestProfileIOData* CreateTestProfileIOData(bool is_incognito);
331 360
332 protected: 361 protected:
333 TestingProfileManager testing_profile_manager_; 362 TestingProfileManager testing_profile_manager_;
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 "session_index", "user@gmail.com", 700 "session_index", "user@gmail.com",
672 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT, 701 OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT,
673 signin::SOURCE_WEBSTORE_INSTALL, 702 signin::SOURCE_WEBSTORE_INSTALL,
674 continueUrl, process()->GetID(), rvh()->GetRoutingID()); 703 continueUrl, process()->GetID(), rvh()->GetRoutingID());
675 704
676 SubmitGAIAPassword(helper); 705 SubmitGAIAPassword(helper);
677 706
678 NavigateAndCommit(GURL("https://chrome.google.com/webstore?source=3")); 707 NavigateAndCommit(GURL("https://chrome.google.com/webstore?source=3"));
679 helper->DidStopLoading(rvh()); 708 helper->DidStopLoading(rvh());
680 709
710 fake_oauth2_token_service_->IssueRefreshToken("refresh_token");
711 FakeLoginUI fake_login_ui;
712 LoginUIServiceFactory::GetForProfile(profile())->SetLoginUI(&fake_login_ui);
713 GoogleServiceSigninSuccessDetails details("user@gmail.com", "password");
714 content::NotificationService::current()->Notify(
715 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
Andrew T Wilson (Slow) 2013/09/09 12:08:27 Why do we send out SIGNIN_SUCCESSFUL - does anyone
Roger Tawa OOO till Jul 10th 2013/09/10 14:29:23 Done. Turns out none of this is really needed. I
716 content::Source<Profile>(profile()),
717 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
718 LoginUIServiceFactory::GetForProfile(profile())->
719 LoginUIClosed(&fake_login_ui);
720
681 helper->OnStateChanged(); 721 helper->OnStateChanged();
682 EXPECT_EQ(GURL(continueUrl), contents->GetURL()); 722 EXPECT_EQ(GURL(continueUrl), contents->GetURL());
683 EXPECT_EQ("user@gmail.com", signin_manager_->GetAuthenticatedUsername()); 723 EXPECT_EQ("user@gmail.com", signin_manager_->GetAuthenticatedUsername());
684 } 724 }
685 725
686 // Checks that the state of OneClickSigninHelper is cleaned when there is a 726 // 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 727 // navigation away from the sign in flow that is not triggered by the
688 // web contents. 728 // web contents.
689 TEST_F(OneClickSigninHelperTest, CleanTransientStateOnNavigate) { 729 TEST_F(OneClickSigninHelperTest, CleanTransientStateOnNavigate) {
690 content::WebContents* contents = web_contents(); 730 content::WebContents* contents = web_contents();
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true)); 909 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true));
870 910
871 // Simulate a policy disabling sync by writing kSyncManaged directly. 911 // Simulate a policy disabling sync by writing kSyncManaged directly.
872 // We should still offer to sign in the browser. 912 // We should still offer to sign in the browser.
873 profile()->GetTestingPrefService()->SetManagedPref( 913 profile()->GetTestingPrefService()->SetManagedPref(
874 prefs::kSyncManaged, base::Value::CreateBooleanValue(true)); 914 prefs::kSyncManaged, base::Value::CreateBooleanValue(true));
875 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, 915 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER,
876 OneClickSigninHelper::CanOfferOnIOThreadImpl( 916 OneClickSigninHelper::CanOfferOnIOThreadImpl(
877 valid_gaia_url_, std::string(), &request_, io_data.get())); 917 valid_gaia_url_, std::string(), &request_, io_data.get()));
878 } 918 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698