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