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