Chromium Code Reviews| 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/prefs/scoped_user_pref_update.h" | |
| 7 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/signin/signin_manager_factory.h" | 9 #include "chrome/browser/signin/signin_manager_factory.h" |
| 9 #include "chrome/browser/signin/signin_manager_fake.h" | 10 #include "chrome/browser/signin/signin_manager_fake.h" |
| 10 #include "chrome/browser/ui/sync/one_click_signin_helper.h" | 11 #include "chrome/browser/ui/sync/one_click_signin_helper.h" |
| 11 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 13 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 16 #include "content/public/test/test_renderer_host.h" | 17 #include "content/public/test/test_renderer_host.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 class OneClickSigninHelperTest : public content::RenderViewHostTestHarness { | 22 class OneClickSigninHelperTest : public content::RenderViewHostTestHarness { |
| 22 public: | 23 public: |
| 23 OneClickSigninHelperTest(); | 24 OneClickSigninHelperTest(); |
| 24 | 25 |
| 25 virtual void SetUp() OVERRIDE; | 26 virtual void SetUp() OVERRIDE; |
| 26 | 27 |
| 27 protected: | 28 protected: |
| 28 // Creates a mock WebContents for tests. If |use_incognito| is true then | 29 // Creates a mock WebContents for tests. If |use_incognito| is true then |
| 29 // a WebContents for an incognito profile is created. If |username| is | 30 // a WebContents for an incognito profile is created. If |username| is |
| 30 // is not empty, the profile of the mock WebContents will be connected to | 31 // is not empty, the profile of the mock WebContents will be connected to |
| 31 // the given account. | 32 // the given account. |
| 32 content::WebContents* CreateMockWebContents(bool use_incognito, | 33 content::WebContents* CreateMockWebContents(bool use_incognito, |
| 33 const std::string& username); | 34 const std::string& username); |
| 34 | 35 |
| 36 void AddEmailToOneClickRejectedList(const std::string& email); | |
| 35 void EnableOneClick(bool enable); | 37 void EnableOneClick(bool enable); |
| 36 | 38 |
| 37 void AllowSigninCookies(bool enable); | 39 void AllowSigninCookies(bool enable); |
| 38 | 40 |
| 39 private: | 41 private: |
| 40 // Members to fake that we are on the UI thread. | 42 // Members to fake that we are on the UI thread. |
| 41 content::TestBrowserThread ui_thread_; | 43 content::TestBrowserThread ui_thread_; |
| 42 | 44 |
| 43 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); | 45 DISALLOW_COPY_AND_ASSIGN(OneClickSigninHelperTest); |
| 44 }; | 46 }; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 70 | 72 |
| 71 return CreateTestWebContents(); | 73 return CreateTestWebContents(); |
| 72 } | 74 } |
| 73 | 75 |
| 74 void OneClickSigninHelperTest::EnableOneClick(bool enable) { | 76 void OneClickSigninHelperTest::EnableOneClick(bool enable) { |
| 75 PrefService* pref_service = Profile::FromBrowserContext( | 77 PrefService* pref_service = Profile::FromBrowserContext( |
| 76 browser_context_.get())->GetPrefs(); | 78 browser_context_.get())->GetPrefs(); |
| 77 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); | 79 pref_service->SetBoolean(prefs::kReverseAutologinEnabled, enable); |
| 78 } | 80 } |
| 79 | 81 |
| 82 void OneClickSigninHelperTest::AddEmailToOneClickRejectedList( | |
| 83 const std::string& email) { | |
| 84 PrefService* pref_service = Profile::FromBrowserContext( | |
| 85 browser_context_.get())->GetPrefs(); | |
| 86 ListPrefUpdate updater(pref_service, | |
| 87 prefs::kReverseAutologinRejectedEmailList); | |
| 88 updater->AppendIfNotPresent(Value::CreateStringValue(email)); | |
| 89 } | |
| 90 | |
| 80 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) { | 91 void OneClickSigninHelperTest::AllowSigninCookies(bool enable) { |
| 81 CookieSettings* cookie_settings = | 92 CookieSettings* cookie_settings = |
| 82 CookieSettings::Factory::GetForProfile( | 93 CookieSettings::Factory::GetForProfile( |
| 83 Profile::FromBrowserContext(browser_context_.get())); | 94 Profile::FromBrowserContext(browser_context_.get())); |
| 84 cookie_settings->SetDefaultCookieSetting( | 95 cookie_settings->SetDefaultCookieSetting( |
| 85 enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); | 96 enable ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); |
| 86 } | 97 } |
| 87 | 98 |
| 88 } // namespace | 99 } // namespace |
| 89 | 100 |
| 90 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) { | 101 TEST_F(OneClickSigninHelperTest, CanOfferNoContents) { |
| 91 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, true)); | 102 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, "user@gmail.com", true)); |
| 92 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, false)); | 103 EXPECT_FALSE(OneClickSigninHelper::CanOffer(NULL, "user@gmail.com", false)); |
|
Roger Tawa OOO till Jul 10th
2012/06/15 17:56:00
Should probably always use an empty email when che
Mathieu
2012/06/21 13:13:00
Done.
| |
| 93 } | 104 } |
| 94 | 105 |
| 95 TEST_F(OneClickSigninHelperTest, CanOffer) { | 106 TEST_F(OneClickSigninHelperTest, CanOffer) { |
| 96 content::WebContents* web_contents = CreateMockWebContents(false, ""); | 107 content::WebContents* web_contents = CreateMockWebContents(false, ""); |
| 97 | 108 |
| 98 EnableOneClick(true); | 109 EnableOneClick(true); |
| 99 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, true)); | 110 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, "", true)); |
| 100 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | 111 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, "", false)); |
| 101 | 112 |
| 102 EnableOneClick(false); | 113 EnableOneClick(false); |
| 103 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 114 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "", true)); |
| 104 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 115 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "", false)); |
| 105 } | 116 } |
| 106 | 117 |
| 107 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) { | 118 TEST_F(OneClickSigninHelperTest, CanOfferProfileConnected) { |
| 108 content::WebContents* web_contents = CreateMockWebContents(false, | 119 content::WebContents* web_contents = CreateMockWebContents(false, |
| 109 "foo@gmail.com"); | 120 "foo@gmail.com"); |
| 110 | 121 |
| 111 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 122 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, |
| 112 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, false)); | 123 "foo@gmail.com", |
| 124 true)); | |
|
Roger Tawa OOO till Jul 10th
2012/06/15 17:56:00
should probably add another EXPECT_FALSE case with
Mathieu
2012/06/21 13:13:00
Done.
| |
| 125 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, | |
| 126 "foo@gmail.com", | |
| 127 false)); | |
| 128 } | |
| 129 | |
| 130 TEST_F(OneClickSigninHelperTest, CanOfferWithRejectedEmail) { | |
| 131 content::WebContents* web_contents = CreateMockWebContents(false, ""); | |
| 132 | |
| 133 AddEmailToOneClickRejectedList("foo@gmail.com"); | |
| 134 AddEmailToOneClickRejectedList("user@gmail.com"); | |
| 135 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "foo@gmail.com", | |
| 136 true)); | |
| 137 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "user@gmail.com", | |
| 138 true)); | |
| 139 EXPECT_TRUE(OneClickSigninHelper::CanOffer(web_contents, "john@gmail.com", | |
| 140 true)); | |
| 113 } | 141 } |
| 114 | 142 |
| 115 TEST_F(OneClickSigninHelperTest, CanOfferIncognito) { | 143 TEST_F(OneClickSigninHelperTest, CanOfferIncognito) { |
| 116 content::WebContents* web_contents = CreateMockWebContents(true, ""); | 144 content::WebContents* web_contents = CreateMockWebContents(true, ""); |
| 117 | 145 |
| 118 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 146 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "", true)); |
| 119 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 147 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "", false)); |
| 120 } | 148 } |
| 121 | 149 |
| 122 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) { | 150 TEST_F(OneClickSigninHelperTest, CanOfferNoSigninCookies) { |
| 123 content::WebContents* web_contents = CreateMockWebContents(false, ""); | 151 content::WebContents* web_contents = CreateMockWebContents(false, ""); |
| 124 AllowSigninCookies(false); | 152 AllowSigninCookies(false); |
| 125 | 153 |
| 126 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, true)); | 154 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "", true)); |
| 127 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, false)); | 155 EXPECT_FALSE(OneClickSigninHelper::CanOffer(web_contents, "", false)); |
| 128 } | 156 } |
| OLD | NEW |