| 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/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/content_settings/cookie_settings.h" | 8 #include "chrome/browser/content_settings/cookie_settings.h" |
| 9 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | 9 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
| 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 10 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 226 |
| 227 virtual void SetUp() OVERRIDE; | 227 virtual void SetUp() OVERRIDE; |
| 228 virtual void TearDown() OVERRIDE; | 228 virtual void TearDown() OVERRIDE; |
| 229 | 229 |
| 230 // Creates the sign-in manager for tests. If |use_incognito| is true then | 230 // Creates the sign-in manager for tests. If |use_incognito| is true then |
| 231 // a WebContents for an incognito profile is created. If |username| is | 231 // a WebContents for an incognito profile is created. If |username| is |
| 232 // is not empty, the profile of the mock WebContents will be connected to | 232 // is not empty, the profile of the mock WebContents will be connected to |
| 233 // the given account. | 233 // the given account. |
| 234 void CreateSigninManager(bool use_incognito, const std::string& username); | 234 void CreateSigninManager(bool use_incognito, const std::string& username); |
| 235 | 235 |
| 236 // Set the ID of the signin process that the test will assume to be the |
| 237 // only process allowed to sign the user in to Chrome. |
| 238 void SetTrustedSigninProcessID(int id); |
| 239 |
| 236 void AddEmailToOneClickRejectedList(const std::string& email); | 240 void AddEmailToOneClickRejectedList(const std::string& email); |
| 237 void EnableOneClick(bool enable); | 241 void EnableOneClick(bool enable); |
| 238 void AllowSigninCookies(bool enable); | 242 void AllowSigninCookies(bool enable); |
| 239 void SetAllowedUsernamePattern(const std::string& pattern); | 243 void SetAllowedUsernamePattern(const std::string& pattern); |
| 240 | 244 |
| 241 SigninManagerMock* signin_manager_; | 245 SigninManagerMock* signin_manager_; |
| 242 | 246 |
| 243 protected: | 247 protected: |
| 244 TestingProfile* profile_; | 248 TestingProfile* profile_; |
| 245 | 249 |
| 246 private: | 250 private: |
| 247 // Members to fake that we are on the UI thread. | 251 // Members to fake that we are on the UI thread. |
| 248 content::TestBrowserThread ui_thread_; | 252 content::TestBrowserThread ui_thread_; |
| 249 | 253 |
| 254 // The ID of the signin process the test will assume to be trusted. |
| 255 // By default, set to the test RenderProcessHost's process ID, but |
| 256 // overridden by SetTrustedSigninProcessID. |
| 257 int trusted_signin_process_id_; |
| 258 |
| 250 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); | 259 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); |
| 251 }; | 260 }; |
| 252 | 261 |
| 253 OneClickSigninHelperTest::OneClickSigninHelperTest() | 262 OneClickSigninHelperTest::OneClickSigninHelperTest() |
| 254 : profile_(NULL), | 263 : profile_(NULL), |
| 255 ui_thread_(content::BrowserThread::UI, &message_loop_) { | 264 ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 265 trusted_signin_process_id_(-1) { |
| 256 } | 266 } |
| 257 | 267 |
| 258 void OneClickSigninHelperTest::SetUp() { | 268 void OneClickSigninHelperTest::SetUp() { |
| 259 SyncPromoUI::ForceWebBasedSigninFlowForTesting(true); | 269 SyncPromoUI::ForceWebBasedSigninFlowForTesting(true); |
| 260 profile_ = new TestingProfile(); | 270 profile_ = new TestingProfile(); |
| 261 browser_context_.reset(profile_); | 271 browser_context_.reset(profile_); |
| 262 content::RenderViewHostTestHarness::SetUp(); | 272 content::RenderViewHostTestHarness::SetUp(); |
| 273 SetTrustedSigninProcessID(process()->GetID()); |
| 263 } | 274 } |
| 264 | 275 |
| 265 void OneClickSigninHelperTest::TearDown() { | 276 void OneClickSigninHelperTest::TearDown() { |
| 266 SyncPromoUI::ForceWebBasedSigninFlowForTesting(false); | 277 SyncPromoUI::ForceWebBasedSigninFlowForTesting(false); |
| 267 content::RenderViewHostTestHarness::TearDown(); | 278 content::RenderViewHostTestHarness::TearDown(); |
| 268 } | 279 } |
| 269 | 280 |
| 281 void OneClickSigninHelperTest::SetTrustedSigninProcessID(int id) { |
| 282 trusted_signin_process_id_ = id; |
| 283 } |
| 284 |
| 270 void OneClickSigninHelperTest::CreateSigninManager( | 285 void OneClickSigninHelperTest::CreateSigninManager( |
| 271 bool use_incognito, | 286 bool use_incognito, |
| 272 const std::string& username) { | 287 const std::string& username) { |
| 273 profile_->set_incognito(use_incognito); | 288 profile_->set_incognito(use_incognito); |
| 274 signin_manager_ = static_cast<SigninManagerMock*>( | 289 signin_manager_ = static_cast<SigninManagerMock*>( |
| 275 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 290 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 276 profile_, BuildSigninManagerMock)); | 291 profile_, BuildSigninManagerMock)); |
| 277 | 292 if (signin_manager_) |
| 293 signin_manager_->SetSigninProcess(trusted_signin_process_id_); |
| 278 if (!username.empty()) { | 294 if (!username.empty()) { |
| 295 ASSERT_TRUE(signin_manager_); |
| 279 signin_manager_->StartSignIn(username, std::string(), std::string(), | 296 signin_manager_->StartSignIn(username, std::string(), std::string(), |
| 280 std::string()); | 297 std::string()); |
| 281 } | 298 } |
| 282 } | 299 } |
| 283 | 300 |
| 284 void OneClickSigninHelperTest::EnableOneClick(bool enable) { | 301 void OneClickSigninHelperTest::EnableOneClick(bool enable) { |
| 285 PrefService* pref_service = Profile::FromBrowserContext( | 302 PrefService* pref_service = Profile::FromBrowserContext( |
| 286 browser_context_.get())->GetPrefs(); | 303 browser_context_.get())->GetPrefs(); |
| 287 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); | 304 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); |
| 288 } | 305 } |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 EXPECT_FALSE(OneClickSigninHelper::CanOffer( | 583 EXPECT_FALSE(OneClickSigninHelper::CanOffer( |
| 567 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL, | 584 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL, |
| 568 "user@gmail.com", &error_message)); | 585 "user@gmail.com", &error_message)); |
| 569 EXPECT_EQ("", error_message); | 586 EXPECT_EQ("", error_message); |
| 570 EXPECT_FALSE(OneClickSigninHelper::CanOffer( | 587 EXPECT_FALSE(OneClickSigninHelper::CanOffer( |
| 571 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY, | 588 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_INTERSTITAL_ONLY, |
| 572 "", &error_message)); | 589 "", &error_message)); |
| 573 EXPECT_EQ("", error_message); | 590 EXPECT_EQ("", error_message); |
| 574 } | 591 } |
| 575 | 592 |
| 593 TEST_F(OneClickSigninHelperTest, CanOfferUntrustedProcess) { |
| 594 content::MockRenderProcessHost trusted(browser_context_.get()); |
| 595 ASSERT_NE(trusted.GetID(), process()->GetID()); |
| 596 // Make sure the RenderProcessHost used by the test is untrusted. |
| 597 SetTrustedSigninProcessID(trusted.GetID()); |
| 598 CreateSigninManager(false, ""); |
| 599 |
| 600 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)). |
| 601 WillRepeatedly(Return(true)); |
| 602 |
| 603 EnableOneClick(true); |
| 604 EXPECT_FALSE(OneClickSigninHelper::CanOffer( |
| 605 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL, |
| 606 "user@gmail.com", NULL)); |
| 607 } |
| 608 |
| 576 TEST_F(OneClickSigninHelperTest, CanOfferDisabledByPolicy) { | 609 TEST_F(OneClickSigninHelperTest, CanOfferDisabledByPolicy) { |
| 577 CreateSigninManager(false, ""); | 610 CreateSigninManager(false, ""); |
| 578 | 611 |
| 579 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)). | 612 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)). |
| 580 WillRepeatedly(Return(true)); | 613 WillRepeatedly(Return(true)); |
| 581 | 614 |
| 582 EnableOneClick(true); | 615 EnableOneClick(true); |
| 583 EXPECT_TRUE(OneClickSigninHelper::CanOffer( | 616 EXPECT_TRUE(OneClickSigninHelper::CanOffer( |
| 584 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL, | 617 web_contents(), OneClickSigninHelper::CAN_OFFER_FOR_ALL, |
| 585 "user@gmail.com", NULL)); | 618 "user@gmail.com", NULL)); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true)); | 845 prefs::kSigninAllowed, base::Value::CreateBooleanValue(true)); |
| 813 | 846 |
| 814 // Simulate a policy disabling sync by writing kSyncManaged directly. | 847 // Simulate a policy disabling sync by writing kSyncManaged directly. |
| 815 // We should still offer to sign in the browser. | 848 // We should still offer to sign in the browser. |
| 816 profile_->GetTestingPrefService()->SetManagedPref( | 849 profile_->GetTestingPrefService()->SetManagedPref( |
| 817 prefs::kSyncManaged, base::Value::CreateBooleanValue(true)); | 850 prefs::kSyncManaged, base::Value::CreateBooleanValue(true)); |
| 818 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, | 851 EXPECT_EQ(OneClickSigninHelper::CAN_OFFER, |
| 819 OneClickSigninHelper::CanOfferOnIOThreadImpl( | 852 OneClickSigninHelper::CanOfferOnIOThreadImpl( |
| 820 valid_gaia_url_, "", &request_, io_data.get())); | 853 valid_gaia_url_, "", &request_, io_data.get())); |
| 821 } | 854 } |
| OLD | NEW |