| 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/utf_string_conversions.h" |
| 5 #include "chrome/browser/content_settings/cookie_settings.h" | 6 #include "chrome/browser/content_settings/cookie_settings.h" |
| 6 #include "chrome/browser/prefs/pref_service.h" | 7 #include "chrome/browser/prefs/pref_service.h" |
| 8 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 7 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/profiles/profile_info_cache.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" |
| 8 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
| 9 #include "chrome/browser/signin/signin_manager_fake.h" | 13 #include "chrome/browser/signin/signin_manager_fake.h" |
| 14 #include "chrome/browser/sync/profile_sync_service_mock.h" |
| 10 #include "chrome/browser/sync/profile_sync_service_factory.h" | 15 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 11 #include "chrome/browser/sync/test_profile_sync_service.h" | |
| 12 #include "chrome/browser/ui/sync/one_click_signin_helper.h" | 16 #include "chrome/browser/ui/sync/one_click_signin_helper.h" |
| 13 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "chrome/test/base/testing_browser_process.h" |
| 14 #include "chrome/test/base/testing_profile.h" | 19 #include "chrome/test/base/testing_profile.h" |
| 20 #include "chrome/test/base/testing_profile_manager.h" |
| 21 |
| 15 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread.h" |
| 18 #include "content/public/test/test_renderer_host.h" | 25 #include "content/public/test/test_renderer_host.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 27 |
| 28 using ::testing::_; |
| 29 using ::testing::Mock; |
| 30 using ::testing::Return; |
| 31 using ::testing::ReturnRef; |
| 32 using ::testing::Values; |
| 33 |
| 21 namespace { | 34 namespace { |
| 22 | 35 |
| 36 class SigninManagerMock : public FakeSigninManager { |
| 37 public: |
| 38 SigninManagerMock() {} |
| 39 MOCK_CONST_METHOD1(IsAllowedUsername, bool(const std::string& username)); |
| 40 }; |
| 41 |
| 23 class OneClickSigninHelperTest : public content::RenderViewHostTestHarness { | 42 class OneClickSigninHelperTest : public content::RenderViewHostTestHarness { |
| 24 public: | 43 public: |
| 25 OneClickSigninHelperTest(); | 44 OneClickSigninHelperTest(); |
| 26 | 45 |
| 27 virtual void SetUp() OVERRIDE; | 46 virtual void SetUp() OVERRIDE; |
| 28 | 47 |
| 29 protected: | 48 protected: |
| 30 // Creates a mock WebContents for tests. If |use_incognito| is true then | 49 // Creates a mock WebContents for tests. If |use_incognito| is true then |
| 31 // a WebContents for an incognito profile is created. If |username| is | 50 // a WebContents for an incognito profile is created. If |username| is |
| 32 // is not empty, the profile of the mock WebContents will be connected to | 51 // is not empty, the profile of the mock WebContents will be connected to |
| 33 // the given account. | 52 // the given account. |
| 34 content::WebContents* CreateMockWebContents(bool use_incognito, | 53 content::WebContents* CreateMockWebContents(bool use_incognito, |
| 35 const std::string& username); | 54 const std::string& username); |
| 36 | 55 |
| 56 void AddEmailToOneClickRejectedList(const std::string& email); |
| 37 void EnableOneClick(bool enable); | 57 void EnableOneClick(bool enable); |
| 38 | 58 |
| 39 void AllowSigninCookies(bool enable); | 59 void AllowSigninCookies(bool enable); |
| 40 | 60 |
| 61 SigninManagerMock* signin_manager_; |
| 62 |
| 41 private: | 63 private: |
| 42 // Members to fake that we are on the UI thread. | 64 // Members to fake that we are on the UI thread. |
| 43 content::TestBrowserThread ui_thread_; | 65 content::TestBrowserThread ui_thread_; |
| 44 | 66 |
| 45 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); | 67 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); |
| 46 }; | 68 }; |
| 47 | 69 |
| 48 OneClickSigninHelperTest::OneClickSigninHelperTest() | 70 OneClickSigninHelperTest::OneClickSigninHelperTest() |
| 49 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | 71 : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
| 50 } | 72 } |
| 51 | 73 |
| 52 void OneClickSigninHelperTest::SetUp() { | 74 void OneClickSigninHelperTest::SetUp() { |
| 53 // Don't call base class so that default browser context and test WebContents | 75 // Don't call base class so that default browser context and test WebContents |
| 54 // are not created now. They will be created in CreateMockWebContents() | 76 // are not created now. They will be created in CreateMockWebContents() |
| 55 // as needed. | 77 // as needed. |
| 56 } | 78 } |
| 57 | 79 |
| 58 class OneClickTestProfileSyncService : public TestProfileSyncService { | 80 static ProfileKeyedService* BuildSigninManagerMock(Profile* profile) { |
| 59 public: | 81 return new SigninManagerMock(); |
| 60 virtual ~OneClickTestProfileSyncService() {} | 82 } |
| 61 | |
| 62 // Helper routine to be used in conjunction with | |
| 63 // ProfileKeyedServiceFactory::SetTestingFactory(). | |
| 64 static ProfileKeyedService* Build(Profile* profile) { | |
| 65 return new OneClickTestProfileSyncService(profile); | |
| 66 } | |
| 67 | |
| 68 // Need to control this for certain tests. | |
| 69 virtual bool FirstSetupInProgress() const OVERRIDE { | |
| 70 return first_setup_in_progress_; | |
| 71 } | |
| 72 | |
| 73 // Controls return value of FirstSetupInProgress. Because some bits | |
| 74 // of UI depend on that value, it's useful to control it separately | |
| 75 // from the internal work and components that are triggered (such as | |
| 76 // ReconfigureDataTypeManager) to facilitate unit tests. | |
| 77 void set_first_setup_in_progress(bool in_progress) { | |
| 78 first_setup_in_progress_ = in_progress; | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 explicit OneClickTestProfileSyncService(Profile* profile) | |
| 83 : TestProfileSyncService(NULL, | |
| 84 profile, | |
| 85 NULL, | |
| 86 ProfileSyncService::MANUAL_START, | |
| 87 false, // synchronous_backend_init | |
| 88 base::Closure()), | |
| 89 first_setup_in_progress_(false) {} | |
| 90 | |
| 91 bool first_setup_in_progress_; | |
| 92 }; | |
| 93 | 83 |
| 94 content::WebContents* OneClickSigninHelperTest::CreateMockWebContents( | 84 content::WebContents* OneClickSigninHelperTest::CreateMockWebContents( |
| 95 bool use_incognito, | 85 bool use_incognito, |
| 96 const std::string& username) { | 86 const std::string& username) { |
| 97 TestingProfile* testing_profile = new TestingProfile(); | 87 TestingProfile* testing_profile = new TestingProfile(); |
| 98 browser_context_.reset(testing_profile); | 88 browser_context_.reset(testing_profile); |
| 99 | 89 |
| 100 testing_profile->set_incognito(use_incognito); | 90 testing_profile->set_incognito(use_incognito); |
| 101 SigninManager* signin_manager = static_cast<SigninManager*>( | 91 signin_manager_ = static_cast<SigninManagerMock*>( |
| 102 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 92 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 103 testing_profile, FakeSigninManager::Build)); | 93 testing_profile, BuildSigninManagerMock)); |
| 94 |
| 104 if (!username.empty()) { | 95 if (!username.empty()) { |
| 105 signin_manager->StartSignIn(username, std::string(), std::string(), | 96 signin_manager_->StartSignIn(username, std::string(), std::string(), |
| 106 std::string()); | 97 std::string()); |
| 107 } | 98 } |
| 99 |
| 108 return CreateTestWebContents(); | 100 return CreateTestWebContents(); |
| 109 } | 101 } |
| 110 | 102 |
| 111 void OneClickSigninHelperTest::EnableOneClick(bool enable) { | 103 void OneClickSigninHelperTest::EnableOneClick(bool enable) { |
| 112 PrefService* pref_service = Profile::FromBrowserContext( | 104 PrefService* pref_service = Profile::FromBrowserContext( |
| 113 browser_context_.get())->GetPrefs(); | 105 browser_context_.get())->GetPrefs(); |
| 114 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); | 106 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); |
| 115 } | 107 } |
| 116 | 108 |
| 109 void OneClickSigninHelperTest::AddEmailToOneClickRejectedList( |
| 110 const std::string& email) { |
| 111 PrefService* pref_service = Profile::FromBrowserContext( |
| 112 browser_context_.get())->GetPrefs(); |
| 113 ListPrefUpdate updater(pref_service, |
| 114 prefs::kReverseAutologinRejectedEmailList); |
| 115 updater->AppendIfNotPresent(Value::CreateStringValue(email)); |
| 116 } |
| 117 |
| 117 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) { | 118 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) { |
| 118 CookieSettings* cookie_settings = | 119 CookieSettings* cookie_settings = |
| 119 CookieSettings::Factory::GetForProfile( | 120 CookieSettings::Factory::GetForProfile( |
| 120 Profile::FromBrowserContext(browser_context_.get())); | 121 Profile::FromBrowserContext(browser_context_.get())); |
| 121 cookie_settings->SetDefaultCookieSetting( | 122 cookie_settings->SetDefaultCookieSetting( |
| 122 enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); | 123 enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); |
| 123 } | 124 } |
| 124 | 125 |
| 125 } // namespace | 126 } // namespace |
| 126 | 127 |
| 127 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) { | 128 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) { |
| 128 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, true)); | 129 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, "user@gmail.com", true)); |
| 129 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, false)); | 130 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, "", false)); |
| 130 } | 131 } |
| 131 | 132 |
| 132 TEST_F(OneClickSigninHelperTest, CanOffer) { | 133 TEST_F(OneClickSigninHelperTest, CanOffer) { |
| 133 content::WebContents* web_contents = CreateMockWebContents(false, ""); | 134 content::WebContents* web_contents = CreateMockWebContents(false, ""); |
| 134 | 135 |
| 136 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)). |
| 137 WillRepeatedly(Return(true)); |
| 138 |
| 135 EnableOneClick(true); | 139 EnableOneClick(true); |
| 136 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, true)); | 140 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, "user@gmail.com", |
| 137 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | 141 true)); |
| 142 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, "", false)); |
| 138 | 143 |
| 139 EnableOneClick(false); | 144 EnableOneClick(false); |
| 140 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 145 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "user@gmail.com", |
| 141 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 146 true)); |
| 142 } | 147 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "", false)); |
| 143 | |
| 144 TEST_F(OneClickSigninHelperTest, CanOfferFirstSetup) { | |
| 145 content::WebContents* web_contents = CreateMockWebContents(false, ""); | |
| 146 | |
| 147 // Invoke OneClickTestProfileSyncService factory function and grab result. | |
| 148 OneClickTestProfileSyncService* sync = | |
| 149 static_cast<OneClickTestProfileSyncService*>( | |
| 150 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 151 static_cast<Profile*>(browser_context()), | |
| 152 OneClickTestProfileSyncService::Build)); | |
| 153 | |
| 154 sync->set_first_setup_in_progress(true); | |
| 155 | |
| 156 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | |
| 157 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | |
| 158 } | 148 } |
| 159 | 149 |
| 160 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) { | 150 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) { |
| 161 content::WebContents* web_contents = CreateMockWebContents(false, | 151 content::WebContents* web_contents = CreateMockWebContents(false, |
| 162 "foo@gmail.com"); | 152 "foo@gmail.com"); |
| 163 | 153 |
| 164 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 154 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)). |
| 165 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | 155 WillRepeatedly(Return(true)); |
| 156 |
| 157 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, |
| 158 "foo@gmail.com", |
| 159 true)); |
| 160 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, |
| 161 "user@gmail.com", |
| 162 true)); |
| 163 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, |
| 164 "", |
| 165 false)); |
| 166 } |
| 167 |
| 168 TEST_F(OneClickSigninHelperTest, CanOfferUsernameNotAllowed) { |
| 169 content::WebContents* web_contents = CreateMockWebContents(false, |
| 170 "foo@gmail.com"); |
| 171 |
| 172 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)). |
| 173 WillRepeatedly(Return(false)); |
| 174 |
| 175 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, |
| 176 "foo@gmail.com", |
| 177 true)); |
| 178 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, |
| 179 "", |
| 180 false)); |
| 181 } |
| 182 |
| 183 TEST_F(OneClickSigninHelperTest, CanOfferWithRejectedEmail) { |
| 184 content::WebContents* web_contents = CreateMockWebContents(false, ""); |
| 185 |
| 186 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)). |
| 187 WillRepeatedly(Return(true)); |
| 188 |
| 189 AddEmailToOneClickRejectedList("foo@gmail.com"); |
| 190 AddEmailToOneClickRejectedList("user@gmail.com"); |
| 191 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "foo@gmail.com", |
| 192 true)); |
| 193 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "user@gmail.com", |
| 194 true)); |
| 195 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, "john@gmail.com", |
| 196 true)); |
| 166 } | 197 } |
| 167 | 198 |
| 168 TEST_F(OneClickSigninHelperTest, CanOfferIncognito) { | 199 TEST_F(OneClickSigninHelperTest, CanOfferIncognito) { |
| 169 content::WebContents* web_contents = CreateMockWebContents(true, ""); | 200 content::WebContents* web_contents = CreateMockWebContents(true, ""); |
| 170 | 201 |
| 171 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 202 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "user@gmail.com", |
| 172 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 203 true)); |
| 204 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "", false)); |
| 173 } | 205 } |
| 174 | 206 |
| 175 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) { | 207 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) { |
| 176 content::WebContents* web_contents = CreateMockWebContents(false, ""); | 208 content::WebContents* web_contents = CreateMockWebContents(false, ""); |
| 177 AllowSigninCookies(false); | 209 AllowSigninCookies(false); |
| 178 | 210 |
| 179 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 211 EXPECT_CALL(*signin_manager_, IsAllowedUsername(_)). |
| 180 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 212 WillRepeatedly(Return(true)); |
| 213 |
| 214 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "user@gmail.com", |
| 215 true)); |
| 216 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "", false)); |
| 181 } | 217 } |
| OLD | NEW |