| 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 "chrome/browser/content_settings/cookie_settings.h" | 5 #include "chrome/browser/content_settings/cookie_settings.h" |
| 6 #include "chrome/browser/prefs/pref_service.h" | 6 #include "chrome/browser/prefs/pref_service.h" |
| 7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/profiles/profile_manager.h" | |
| 9 #include "chrome/browser/signin/signin_manager_factory.h" | 8 #include "chrome/browser/signin/signin_manager_factory.h" |
| 10 #include "chrome/browser/signin/signin_manager_fake.h" | 9 #include "chrome/browser/signin/signin_manager_fake.h" |
| 11 #include "chrome/browser/ui/sync/one_click_signin_helper.h" | 10 #include "chrome/browser/ui/sync/one_click_signin_helper.h" |
| 12 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 12 #include "chrome/test/base/testing_profile.h" |
| 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 15 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/navigation_controller.h" | |
| 17 #include "content/public/browser/site_instance.h" | |
| 18 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 20 #include "content/public/test/web_contents_tester.h" | 16 #include "content/public/test/test_renderer_host.h" |
| 21 #include "testing/gmock/include/gmock/gmock-actions.h" | |
| 22 #include "testing/gmock/include/gmock/gmock.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 18 |
| 25 namespace { | 19 namespace { |
| 26 | 20 |
| 27 class OneClickSigninHelperTest : public ChromeRenderViewHostTestHarness { | 21 class OneClickSigninHelperTest : public content::RenderViewHostTestHarness { |
| 28 public: | 22 public: |
| 29 OneClickSigninHelperTest() : signin_manager_(NULL) { | 23 OneClickSigninHelperTest(); |
| 30 } | |
| 31 | 24 |
| 32 virtual void TearDown() OVERRIDE; | 25 virtual void SetUp() OVERRIDE; |
| 33 | 26 |
| 34 protected: | 27 protected: |
| 35 // Marks the current thread as the UI thread, so that calls that assume | |
| 36 // they are called on that thread work. | |
| 37 void MarkCurrentThreadAsUIThread(); | |
| 38 | |
| 39 // Creates a mock WebContents for tests. If |use_incognito| is true then | 28 // Creates a mock WebContents for tests. If |use_incognito| is true then |
| 40 // a WebContents for an incognito profile is created. | 29 // a WebContents for an incognito profile is created. If |username| is |
| 41 content::WebContents* CreateMockWebContents(bool use_incognito); | 30 // is not empty, the profile of the mock WebContents will be connected to |
| 31 // the given account. |
| 32 content::WebContents* CreateMockWebContents(bool use_incognito, |
| 33 const std::string& username); |
| 42 | 34 |
| 43 void EnableOneClick(bool enable); | 35 void EnableOneClick(bool enable); |
| 44 | 36 |
| 45 void AllowSigninCookies(bool enable); | 37 void AllowSigninCookies(bool enable); |
| 46 | 38 |
| 47 // Marks the profile as connected to the given account. If |username| is the | |
| 48 // empty string, the profile is not connected. | |
| 49 void ConnectProfileToAccount(const std::string& username); | |
| 50 | |
| 51 private: | 39 private: |
| 52 // Members to fake that we are on the UI thread. | 40 // Members to fake that we are on the UI thread. |
| 53 scoped_ptr<content::TestBrowserThread> ui_thread_; | 41 content::TestBrowserThread ui_thread_; |
| 54 | |
| 55 // Mock objects used during tests. The objects need to be torn down in the | |
| 56 // correct order, see TearDown(). | |
| 57 scoped_ptr<content::WebContents> web_contents_; | |
| 58 scoped_ptr<TestingProfile> profile_; | |
| 59 SigninManager* signin_manager_; | |
| 60 | 42 |
| 61 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); | 43 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); |
| 62 }; | 44 }; |
| 63 | 45 |
| 64 void OneClickSigninHelperTest::TearDown() { | 46 OneClickSigninHelperTest::OneClickSigninHelperTest() |
| 65 // Destroy things in proper order. | 47 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
| 66 web_contents_.reset(); | |
| 67 profile_.reset(); | |
| 68 ui_thread_.reset(); | |
| 69 MessageLoop::current()->RunAllPending(); | |
| 70 ChromeRenderViewHostTestHarness::TearDown(); | |
| 71 } | 48 } |
| 72 | 49 |
| 73 void OneClickSigninHelperTest::MarkCurrentThreadAsUIThread() { | 50 void OneClickSigninHelperTest::SetUp() { |
| 74 ui_thread_.reset(new content::TestBrowserThread( | 51 // Don't call base class so that default browser context and test WebContents |
| 75 content::BrowserThread::UI, &message_loop_)); | 52 // are not created now. They will be created in CreateMockWebContents() |
| 53 // as needed. |
| 76 } | 54 } |
| 77 | 55 |
| 78 content::WebContents* OneClickSigninHelperTest::CreateMockWebContents( | 56 content::WebContents* OneClickSigninHelperTest::CreateMockWebContents( |
| 79 bool use_incognito) { | 57 bool use_incognito, |
| 80 EXPECT_TRUE(web_contents_.get() == NULL); | 58 const std::string& username) { |
| 59 TestingProfile* testing_profile = new TestingProfile(); |
| 60 browser_context_.reset(testing_profile); |
| 81 | 61 |
| 82 profile_.reset(new TestingProfile()); | 62 testing_profile->set_incognito(use_incognito); |
| 83 signin_manager_ = static_cast<SigninManager*>( | 63 SigninManager* signin_manager = static_cast<SigninManager*>( |
| 84 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 64 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 85 profile_.get(), FakeSigninManager::Build)); | 65 testing_profile, FakeSigninManager::Build)); |
| 66 if (!username.empty()) { |
| 67 signin_manager->StartSignIn(username, std::string(), std::string(), |
| 68 std::string()); |
| 69 } |
| 86 | 70 |
| 87 profile_->set_incognito(use_incognito); | 71 return CreateTestWebContents(); |
| 88 web_contents_.reset(content::WebContentsTester::CreateTestWebContents( | |
| 89 profile_.get(), NULL)); | |
| 90 return web_contents_.get(); | |
| 91 } | 72 } |
| 92 | 73 |
| 93 void OneClickSigninHelperTest::EnableOneClick(bool enable) { | 74 void OneClickSigninHelperTest::EnableOneClick(bool enable) { |
| 94 PrefService* pref_service = profile_->GetPrefs(); | 75 PrefService* pref_service = Profile::FromBrowserContext( |
| 76 browser_context_.get())->GetPrefs(); |
| 95 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); | 77 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); |
| 96 } | 78 } |
| 97 | 79 |
| 98 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) { | 80 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) { |
| 99 CookieSettings* cookie_settings = | 81 CookieSettings* cookie_settings = |
| 100 CookieSettings::Factory::GetForProfile(profile_.get()); | 82 CookieSettings::Factory::GetForProfile( |
| 83 Profile::FromBrowserContext(browser_context_.get())); |
| 101 cookie_settings->SetDefaultCookieSetting( | 84 cookie_settings->SetDefaultCookieSetting( |
| 102 enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); | 85 enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); |
| 103 } | 86 } |
| 104 | 87 |
| 105 void OneClickSigninHelperTest::ConnectProfileToAccount( | |
| 106 const std::string& username) { | |
| 107 signin_manager_->StartSignIn(username, std::string(), std::string(), | |
| 108 std::string()); | |
| 109 } | |
| 110 | |
| 111 } // namespace | 88 } // namespace |
| 112 | 89 |
| 113 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) { | 90 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) { |
| 114 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, true)); | 91 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, true)); |
| 115 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, false)); | 92 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, false)); |
| 116 } | 93 } |
| 117 | 94 |
| 118 TEST_F(OneClickSigninHelperTest, CanOffer) { | 95 TEST_F(OneClickSigninHelperTest, CanOffer) { |
| 119 MarkCurrentThreadAsUIThread(); | 96 content::WebContents* web_contents = CreateMockWebContents(false, ""); |
| 120 content::WebContents* web_contents = CreateMockWebContents(false); | |
| 121 | 97 |
| 122 EnableOneClick(true); | 98 EnableOneClick(true); |
| 123 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, true)); | 99 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, true)); |
| 124 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | 100 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); |
| 125 | 101 |
| 126 EnableOneClick(false); | 102 EnableOneClick(false); |
| 127 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 103 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); |
| 128 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 104 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); |
| 129 } | 105 } |
| 130 | 106 |
| 131 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) { | 107 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) { |
| 132 MarkCurrentThreadAsUIThread(); | 108 content::WebContents* web_contents = CreateMockWebContents(false, |
| 133 content::WebContents* web_contents = CreateMockWebContents(false); | 109 "foo@gmail.com"); |
| 134 ConnectProfileToAccount("foo@gmail.com"); | |
| 135 | 110 |
| 136 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 111 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); |
| 137 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | 112 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); |
| 138 } | 113 } |
| 139 | 114 |
| 140 TEST_F(OneClickSigninHelperTest, CanOfferIncognito) { | 115 TEST_F(OneClickSigninHelperTest, CanOfferIncognito) { |
| 141 MarkCurrentThreadAsUIThread(); | 116 content::WebContents* web_contents = CreateMockWebContents(true, ""); |
| 142 content::WebContents* web_contents = CreateMockWebContents(true); | |
| 143 | 117 |
| 144 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 118 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); |
| 145 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 119 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); |
| 146 } | 120 } |
| 147 | 121 |
| 148 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) { | 122 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) { |
| 149 MarkCurrentThreadAsUIThread(); | 123 content::WebContents* web_contents = CreateMockWebContents(false, ""); |
| 150 content::WebContents* web_contents = CreateMockWebContents(true); | |
| 151 AllowSigninCookies(false); | 124 AllowSigninCookies(false); |
| 152 | 125 |
| 153 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 126 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); |
| 154 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 127 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); |
| 155 } | 128 } |
| OLD | NEW |