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

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

Powered by Google App Engine
This is Rietveld 408576698