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