Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: chrome/browser/password_manager/password_generation_manager_unittest.cc

Issue 23432002: Generate passwords only for forms that autofill server marks as account creation forms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/password_manager/password_generation_manager.h" 9 #include "chrome/browser/password_manager/password_generation_manager.h"
9 #include "chrome/browser/password_manager/password_manager.h" 10 #include "chrome/browser/password_manager/password_manager.h"
10 #include "chrome/browser/password_manager/password_manager_delegate_impl.h" 11 #include "chrome/browser/password_manager/password_manager_delegate_impl.h"
11 #include "chrome/browser/sync/profile_sync_service.h" 12 #include "chrome/browser/sync/profile_sync_service.h"
12 #include "chrome/browser/sync/profile_sync_service_factory.h" 13 #include "chrome/browser/sync/profile_sync_service_factory.h"
13 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
15 #include "chrome/test/base/testing_profile.h" 16 #include "chrome/test/base/testing_profile.h"
17 #include "components/autofill/content/browser/autocheckout_page_meta_data.h"
18 #include "components/autofill/core/browser/autofill_field.h"
19 #include "components/autofill/core/browser/autofill_metrics.h"
20 #include "components/autofill/core/browser/form_structure.h"
21 #include "components/autofill/core/common/form_data.h"
22 #include "components/autofill/core/common/form_field_data.h"
16 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
17 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "url/gurl.h"
26
27 namespace {
28
29 // Unlike the base AutofillMetrics, exposes copy and assignment constructors,
30 // which are handy for briefer test code. The AutofillMetrics class is
31 // stateless, so this is safe.
32 class TestAutofillMetrics : public autofill::AutofillMetrics {
33 public:
34 TestAutofillMetrics() {}
35 virtual ~TestAutofillMetrics() {}
36 };
37
38 } // anonymous namespace
18 39
19 class TestPasswordGenerationManager : public PasswordGenerationManager { 40 class TestPasswordGenerationManager : public PasswordGenerationManager {
20 public: 41 public:
21 explicit TestPasswordGenerationManager(content::WebContents* contents) 42 explicit TestPasswordGenerationManager(content::WebContents* contents)
22 : PasswordGenerationManager(contents) {} 43 : PasswordGenerationManager(contents) {}
23 virtual ~TestPasswordGenerationManager() {} 44 virtual ~TestPasswordGenerationManager() {}
24 45
25 virtual void SendStateToRenderer(content::RenderViewHost* host, 46 virtual void SendStateToRenderer(content::RenderViewHost* host,
26 bool enabled) OVERRIDE { 47 bool enabled) OVERRIDE {
27 sent_states_.push_back(enabled); 48 sent_states_.push_back(enabled);
28 } 49 }
29 50
51 virtual void SendAccountCreationFormsToRenderer(
52 content::RenderViewHost* host,
53 const std::vector<GURL>& origins) OVERRIDE {
54 sent_account_creation_forms_.insert(
55 sent_account_creation_forms_.begin(), origins.begin(), origins.end());
56 }
57
30 const std::vector<bool>& GetSentStates() { 58 const std::vector<bool>& GetSentStates() {
31 return sent_states_; 59 return sent_states_;
32 } 60 }
33 61
62 const std::vector<GURL>& GetSentAccountCreationForms() {
63 return sent_account_creation_forms_;
64 }
65
34 void ClearSentStates() { 66 void ClearSentStates() {
35 sent_states_.clear(); 67 sent_states_.clear();
36 } 68 }
37 69
70 void ClearSentAccountCreationForms() {
71 sent_account_creation_forms_.clear();
72 }
73
38 private: 74 private:
39 std::vector<bool> sent_states_; 75 std::vector<bool> sent_states_;
76 std::vector<GURL> sent_account_creation_forms_;
40 77
41 DISALLOW_COPY_AND_ASSIGN(TestPasswordGenerationManager); 78 DISALLOW_COPY_AND_ASSIGN(TestPasswordGenerationManager);
42 }; 79 };
43 80
44 class PasswordGenerationManagerTest : public ChromeRenderViewHostTestHarness { 81 class PasswordGenerationManagerTest : public ChromeRenderViewHostTestHarness {
45 protected: 82 protected:
46 virtual void SetUp() OVERRIDE { 83 virtual void SetUp() OVERRIDE {
47 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD); 84 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD);
48 ChromeRenderViewHostTestHarness::SetUp(); 85 ChromeRenderViewHostTestHarness::SetUp();
49 86
50 password_generation_manager_.reset( 87 password_generation_manager_.reset(
51 new TestPasswordGenerationManager(web_contents())); 88 new TestPasswordGenerationManager(web_contents()));
52 } 89 }
53 90
54 virtual void TearDown() OVERRIDE { 91 virtual void TearDown() OVERRIDE {
55 ChromeRenderViewHostTestHarness::TearDown(); 92 ChromeRenderViewHostTestHarness::TearDown();
56 } 93 }
57 94
58 void UpdateState(bool new_renderer) { 95 void UpdateState(bool new_renderer) {
59 password_generation_manager_->UpdateState(NULL, new_renderer); 96 password_generation_manager_->UpdateState(NULL, new_renderer);
60 } 97 }
61 98
99 void DetectAccountCreationForms(
100 const std::vector<autofill::FormStructure*>& forms) {
101 password_generation_manager_->DetectAccountCreationForms(forms);
102 }
103
62 scoped_ptr<TestPasswordGenerationManager> password_generation_manager_; 104 scoped_ptr<TestPasswordGenerationManager> password_generation_manager_;
63 }; 105 };
64 106
65 class IncognitoPasswordGenerationManagerTest : 107 class IncognitoPasswordGenerationManagerTest :
66 public PasswordGenerationManagerTest { 108 public PasswordGenerationManagerTest {
67 public: 109 public:
68 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE { 110 virtual content::BrowserContext* CreateBrowserContext() OVERRIDE {
69 // Create an incognito profile. 111 // Create an incognito profile.
70 TestingProfile::Builder builder; 112 TestingProfile::Builder builder;
71 scoped_ptr<TestingProfile> profile = builder.Build(); 113 scoped_ptr<TestingProfile> profile = builder.Build();
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 password_generation_manager_->ClearSentStates(); 215 password_generation_manager_->ClearSentStates();
174 216
175 // When a new render_view is created, we send the state even if it's the 217 // When a new render_view is created, we send the state even if it's the
176 // same. 218 // same.
177 UpdateState(true); 219 UpdateState(true);
178 EXPECT_EQ(1u, password_generation_manager_->GetSentStates().size()); 220 EXPECT_EQ(1u, password_generation_manager_->GetSentStates().size());
179 EXPECT_FALSE(password_generation_manager_->GetSentStates()[0]); 221 EXPECT_FALSE(password_generation_manager_->GetSentStates()[0]);
180 password_generation_manager_->ClearSentStates(); 222 password_generation_manager_->ClearSentStates();
181 } 223 }
182 224
225 TEST_F(PasswordGenerationManagerTest, DetectAccountCreationForms) {
226 autofill::FormData login_form;
227 login_form.origin = GURL("http://www.yahoo.com/login/");
228 autofill::FormFieldData username;
229 username.label = ASCIIToUTF16("username");
230 username.name = ASCIIToUTF16("login");
231 username.form_control_type = "text";
232 login_form.fields.push_back(username);
233 autofill::FormFieldData password;
234 password.label = ASCIIToUTF16("password");
235 password.name = ASCIIToUTF16("password");
236 password.form_control_type = "password";
237 login_form.fields.push_back(password);
238 autofill::FormStructure form1(login_form, std::string());
239 std::vector<autofill::FormStructure*> forms;
240 forms.push_back(&form1);
241 autofill::FormData account_creation_form;
242 account_creation_form.origin = GURL("http://accounts.yahoo.com/");
243 account_creation_form.fields.push_back(username);
244 account_creation_form.fields.push_back(password);
245 autofill::FormFieldData confirm_password;
246 confirm_password.label = ASCIIToUTF16("confirm_password");
247 confirm_password.name = ASCIIToUTF16("password");
248 confirm_password.form_control_type = "password";
249 account_creation_form.fields.push_back(confirm_password);
250 autofill::FormStructure form2(account_creation_form, std::string());
251 forms.push_back(&form2);
252
253 // Simulate the server response to set the field types.
254 autofill::AutocheckoutPageMetaData page_meta_data;
255 const char * const kServerResponse =
Ilya Sherman 2013/09/03 23:58:01 nit: "char *" -> "char*"
zysxqn 2013/09/04 17:26:20 Done.
256 "<autofillqueryresponse>"
257 "<field autofilltype=\"9\" />"
258 "<field autofilltype=\"75\" />"
259 "<field autofilltype=\"9\" />"
260 "<field autofilltype=\"76\" />"
261 "<field autofilltype=\"75\" />"
262 "</autofillqueryresponse>";
263 autofill::FormStructure::ParseQueryResponse(
264 kServerResponse,
265 forms,
266 &page_meta_data,
267 TestAutofillMetrics());
268
269 DetectAccountCreationForms(forms);
270 EXPECT_EQ(1u,
271 password_generation_manager_->GetSentAccountCreationForms().size());
272 EXPECT_EQ(GURL("http://accounts.yahoo.com/"),
273 password_generation_manager_->GetSentAccountCreationForms()[0]);
274 }
275
183 TEST_F(IncognitoPasswordGenerationManagerTest, 276 TEST_F(IncognitoPasswordGenerationManagerTest,
184 UpdatePasswordSyncStateIncognito) { 277 UpdatePasswordSyncStateIncognito) {
185 // Disable password manager by going incognito, and enable syncing. The 278 // Disable password manager by going incognito, and enable syncing. The
186 // feature should still be disabled, and nothing will be sent. 279 // feature should still be disabled, and nothing will be sent.
187 PasswordManagerDelegateImpl::CreateForWebContents(web_contents()); 280 PasswordManagerDelegateImpl::CreateForWebContents(web_contents());
188 PasswordManager::CreateForWebContentsAndDelegate( 281 PasswordManager::CreateForWebContentsAndDelegate(
189 web_contents(), 282 web_contents(),
190 PasswordManagerDelegateImpl::FromWebContents(web_contents())); 283 PasswordManagerDelegateImpl::FromWebContents(web_contents()));
191 284
192 PrefService* prefs = profile()->GetPrefs(); 285 PrefService* prefs = profile()->GetPrefs();
193 286
194 // Allow this test to control what should get synced. 287 // Allow this test to control what should get synced.
195 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); 288 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false);
196 // Always set password generation enabled check box so we can test the 289 // Always set password generation enabled check box so we can test the
197 // behavior of password sync. 290 // behavior of password sync.
198 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true); 291 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true);
199 292
200 browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs()); 293 browser_sync::SyncPrefs sync_prefs(profile()->GetPrefs());
201 sync_prefs.SetSyncSetupCompleted(); 294 sync_prefs.SetSyncSetupCompleted();
202 UpdateState(false); 295 UpdateState(false);
203 EXPECT_EQ(0u, password_generation_manager_->GetSentStates().size()); 296 EXPECT_EQ(0u, password_generation_manager_->GetSentStates().size());
204 } 297 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698