OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/password_manager/password_generation_manager.h" | 8 #include "chrome/browser/password_manager/password_generation_manager.h" |
9 #include "chrome/browser/password_manager/password_manager.h" | 9 #include "chrome/browser/password_manager/password_manager.h" |
10 #include "chrome/browser/password_manager/password_manager_delegate_impl.h" | 10 #include "chrome/browser/password_manager/password_manager_delegate_impl.h" |
11 #include "chrome/browser/sync/profile_sync_service.h" | 11 #include "chrome/browser/sync/profile_sync_service.h" |
12 #include "chrome/browser/sync/profile_sync_service_factory.h" | 12 #include "chrome/browser/sync/profile_sync_service_factory.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
16 #include "components/autofill/core/browser/autofill_field.h" | |
17 #include "components/autofill/core/browser/form_structure.h" | |
18 #include "components/autofill/core/common/form_data.h" | |
16 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "url/gurl.h" | |
18 | 22 |
19 class TestPasswordGenerationManager : public PasswordGenerationManager { | 23 class TestPasswordGenerationManager : public PasswordGenerationManager { |
20 public: | 24 public: |
21 explicit TestPasswordGenerationManager(content::WebContents* contents) | 25 explicit TestPasswordGenerationManager(content::WebContents* contents) |
22 : PasswordGenerationManager(contents) {} | 26 : PasswordGenerationManager(contents) {} |
23 virtual ~TestPasswordGenerationManager() {} | 27 virtual ~TestPasswordGenerationManager() {} |
24 | 28 |
25 virtual void SendStateToRenderer(content::RenderViewHost* host, | 29 virtual void SendStateToRenderer(content::RenderViewHost* host, |
26 bool enabled) OVERRIDE { | 30 bool enabled) OVERRIDE { |
27 sent_states_.push_back(enabled); | 31 sent_states_.push_back(enabled); |
28 } | 32 } |
29 | 33 |
34 virtual void SendAccountCreationFormsToRenderer( | |
35 content::RenderViewHost* host, | |
36 const std::vector<GURL>& origins) OVERRIDE { | |
37 sent_account_creation_forms_.insert( | |
38 sent_account_creation_forms_.begin(), origins.begin(), origins.end()); | |
39 } | |
40 | |
30 const std::vector<bool>& GetSentStates() { | 41 const std::vector<bool>& GetSentStates() { |
31 return sent_states_; | 42 return sent_states_; |
32 } | 43 } |
33 | 44 |
45 const std::vector<GURL>& GetSentAccountCreationForms() { | |
46 return sent_account_creation_forms_; | |
47 } | |
48 | |
34 void ClearSentStates() { | 49 void ClearSentStates() { |
35 sent_states_.clear(); | 50 sent_states_.clear(); |
36 } | 51 } |
37 | 52 |
53 void ClearSentAccountCreationForms() { | |
54 sent_account_creation_forms_.clear(); | |
55 } | |
56 | |
38 private: | 57 private: |
39 std::vector<bool> sent_states_; | 58 std::vector<bool> sent_states_; |
59 std::vector<GURL> sent_account_creation_forms_; | |
40 | 60 |
41 DISALLOW_COPY_AND_ASSIGN(TestPasswordGenerationManager); | 61 DISALLOW_COPY_AND_ASSIGN(TestPasswordGenerationManager); |
42 }; | 62 }; |
43 | 63 |
44 class PasswordGenerationManagerTest : public ChromeRenderViewHostTestHarness { | 64 class PasswordGenerationManagerTest : public ChromeRenderViewHostTestHarness { |
45 protected: | 65 protected: |
46 virtual void SetUp() OVERRIDE { | 66 virtual void SetUp() OVERRIDE { |
47 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); | 67 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); |
48 ChromeRenderViewHostTestHarness::SetUp(); | 68 ChromeRenderViewHostTestHarness::SetUp(); |
49 | 69 |
50 password_generation_manager_.reset( | 70 password_generation_manager_.reset( |
51 new TestPasswordGenerationManager(web_contents())); | 71 new TestPasswordGenerationManager(web_contents())); |
52 } | 72 } |
53 | 73 |
54 virtual void TearDown() OVERRIDE { | 74 virtual void TearDown() OVERRIDE { |
55 ChromeRenderViewHostTestHarness::TearDown(); | 75 ChromeRenderViewHostTestHarness::TearDown(); |
56 } | 76 } |
57 | 77 |
58 void UpdateState(bool new_renderer) { | 78 void UpdateState(bool new_renderer) { |
59 password_generation_manager_->UpdateState(NULL, new_renderer); | 79 password_generation_manager_->UpdateState(NULL, new_renderer); |
60 } | 80 } |
61 | 81 |
82 void DetectAccountCreationForms( | |
83 const std::vector<autofill::FormStructure*>& forms) { | |
84 password_generation_manager_->DetectAccountCreationForms(forms); | |
85 } | |
86 | |
62 scoped_ptr<TestPasswordGenerationManager> password_generation_manager_; | 87 scoped_ptr<TestPasswordGenerationManager> password_generation_manager_; |
63 }; | 88 }; |
64 | 89 |
65 class IncognitoPasswordGenerationManagerTest : | 90 class IncognitoPasswordGenerationManagerTest : |
66 public PasswordGenerationManagerTest { | 91 public PasswordGenerationManagerTest { |
67 public: | 92 public: |
68 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { | 93 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { |
69 // Create an incognito profile. | 94 // Create an incognito profile. |
70 TestingProfile::Builder builder; | 95 TestingProfile::Builder builder; |
71 scoped_ptr<TestingProfile> profile = builder.Build(); | 96 scoped_ptr<TestingProfile> profile = builder.Build(); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 password_generation_manager_->ClearSentStates(); | 198 password_generation_manager_->ClearSentStates(); |
174 | 199 |
175 // When a new render_view is created, we send the state even if it's the | 200 // When a new render_view is created, we send the state even if it's the |
176 // same. | 201 // same. |
177 UpdateState(true); | 202 UpdateState(true); |
178 EXPECT_EQ(1u, password_generation_manager_->GetSentStates().size()); | 203 EXPECT_EQ(1u, password_generation_manager_->GetSentStates().size()); |
179 EXPECT_FALSE(password_generation_manager_->GetSentStates()[0]); | 204 EXPECT_FALSE(password_generation_manager_->GetSentStates()[0]); |
180 password_generation_manager_->ClearSentStates(); | 205 password_generation_manager_->ClearSentStates(); |
181 } | 206 } |
182 | 207 |
208 TEST_F(PasswordGenerationManagerTest, DetectAccountCreationForms) { | |
209 autofill::FormData data; | |
210 data.origin = GURL("http://www.yahoo.com/login/"); | |
211 autofill::FormStructure form1(data, ""); | |
212 autofill::AutofillField* field = new autofill::AutofillField(); | |
213 field->set_server_type(autofill::EMAIL_ADDRESS); | |
214 | |
215 form1.fields_.push_back(field); | |
Garrett Casto
2013/08/29 22:45:13
You should probably construct a FormData and then
zysxqn
2013/09/03 23:00:20
Done.
| |
216 field = new autofill::AutofillField(); | |
217 field->set_server_type(autofill::PASSWORD); | |
218 form1.fields_.push_back(field); | |
219 std::vector<autofill::FormStructure*> forms; | |
220 forms.push_back(&form1); | |
221 data.origin = GURL("http://accounts.yahoo.com/"); | |
222 autofill::FormStructure form2(data, ""); | |
223 field = new autofill::AutofillField(); | |
224 field->set_server_type(autofill::EMAIL_ADDRESS); | |
225 form2.fields_.push_back(field); | |
226 field = new autofill::AutofillField(); | |
227 field->set_server_type(autofill::ACCOUNT_CREATION_PASSWORD); | |
228 form2.fields_.push_back(field); | |
229 field = new autofill::AutofillField(); | |
230 field->set_server_type(autofill::PASSWORD); | |
231 form2.fields_.push_back(field); | |
232 forms.push_back(&form2); | |
233 | |
234 DetectAccountCreationForms(forms); | |
235 EXPECT_EQ(1u, | |
236 password_generation_manager_->GetSentAccountCreationForms().size()); | |
237 EXPECT_EQ(GURL("http://accounts.yahoo.com/"), | |
238 password_generation_manager_->GetSentAccountCreationForms()[0]); | |
239 } | |
240 | |
183 TEST_F(IncognitoPasswordGenerationManagerTest, | 241 TEST_F(IncognitoPasswordGenerationManagerTest, |
184 UpdatePasswordSyncStateIncognito) { | 242 UpdatePasswordSyncStateIncognito) { |
185 // Disable password manager by going incognito, and enable syncing. The | 243 // Disable password manager by going incognito, and enable syncing. The |
186 // feature should still be disabled, and nothing will be sent. | 244 // feature should still be disabled, and nothing will be sent. |
187 PasswordManagerDelegateImpl::CreateForWebContents(web_contents()); | 245 PasswordManagerDelegateImpl::CreateForWebContents(web_contents()); |
188 PasswordManager::CreateForWebContentsAndDelegate( | 246 PasswordManager::CreateForWebContentsAndDelegate( |
189 web_contents(), | 247 web_contents(), |
190 PasswordManagerDelegateImpl::FromWebContents(web_contents())); | 248 PasswordManagerDelegateImpl::FromWebContents(web_contents())); |
191 | 249 |
192 PrefService* prefs = profile()->GetPrefs(); | 250 PrefService* prefs = profile()->GetPrefs(); |
193 | 251 |
194 // Allow this test to control what should get synced. | 252 // Allow this test to control what should get synced. |
195 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); | 253 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); |
196 // Always set password generation enabled check box so we can test the | 254 // Always set password generation enabled check box so we can test the |
197 // behavior of password sync. | 255 // behavior of password sync. |
198 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true); | 256 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true); |
199 | 257 |
200 browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs()); | 258 browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs()); |
201 sync_prefs.SetSyncSetupCompleted(); | 259 sync_prefs.SetSyncSetupCompleted(); |
202 UpdateState(false); | 260 UpdateState(false); |
203 EXPECT_EQ(0u, password_generation_manager_->GetSentStates().size()); | 261 EXPECT_EQ(0u, password_generation_manager_->GetSentStates().size()); |
204 } | 262 } |
OLD | NEW |