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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/password_manager/mock_password_store.h" | 10 #include "chrome/browser/password_manager/mock_password_store.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 } | 76 } |
77 | 77 |
78 PasswordForm MakeSimpleForm() { | 78 PasswordForm MakeSimpleForm() { |
79 PasswordForm form; | 79 PasswordForm form; |
80 form.origin = GURL("http://www.google.com/a/LoginAuth"); | 80 form.origin = GURL("http://www.google.com/a/LoginAuth"); |
81 form.action = GURL("http://www.google.com/a/Login"); | 81 form.action = GURL("http://www.google.com/a/Login"); |
82 form.username_element = ASCIIToUTF16("Email"); | 82 form.username_element = ASCIIToUTF16("Email"); |
83 form.password_element = ASCIIToUTF16("Passwd"); | 83 form.password_element = ASCIIToUTF16("Passwd"); |
84 form.username_value = ASCIIToUTF16("google"); | 84 form.username_value = ASCIIToUTF16("google"); |
85 form.password_value = ASCIIToUTF16("password"); | 85 form.password_value = ASCIIToUTF16("password"); |
| 86 // Default to true so we only need to add tests in autocomplete=off cases. |
| 87 form.password_autocomplete_set = true; |
86 form.submit_element = ASCIIToUTF16("signIn"); | 88 form.submit_element = ASCIIToUTF16("signIn"); |
87 form.signon_realm = "http://www.google.com"; | 89 form.signon_realm = "http://www.google.com"; |
88 return form; | 90 return form; |
89 } | 91 } |
90 | 92 |
91 PasswordManager* manager() { | 93 PasswordManager* manager() { |
92 return PasswordManager::FromWebContents(web_contents()); | 94 return PasswordManager::FromWebContents(web_contents()); |
93 } | 95 } |
94 | 96 |
95 // We create a UI thread to satisfy PasswordStore. | 97 // We create a UI thread to satisfy PasswordStore. |
96 content::TestBrowserThread ui_thread_; | 98 content::TestBrowserThread ui_thread_; |
97 | 99 |
98 scoped_refptr<MockPasswordStore> store_; | 100 scoped_refptr<MockPasswordStore> store_; |
99 MockPasswordManagerDelegate delegate_; // Owned by manager_. | 101 MockPasswordManagerDelegate delegate_; // Owned by manager_. |
100 | 102 |
101 TestingProfile* testing_profile_; | 103 TestingProfile* testing_profile_; |
102 }; | 104 }; |
103 | 105 |
104 MATCHER_P(FormMatches, form, "") { | 106 MATCHER_P(FormMatches, form, "") { |
105 return form.signon_realm == arg.signon_realm && | 107 return form.signon_realm == arg.signon_realm && |
106 form.origin == arg.origin && | 108 form.origin == arg.origin && |
107 form.action == arg.action && | 109 form.action == arg.action && |
108 form.username_element == arg.username_element && | 110 form.username_element == arg.username_element && |
109 form.password_element == arg.password_element && | 111 form.password_element == arg.password_element && |
| 112 form.password_autocomplete_set == |
| 113 arg.password_autocomplete_set && |
110 form.submit_element == arg.submit_element; | 114 form.submit_element == arg.submit_element; |
111 } | 115 } |
112 | 116 |
113 TEST_F(PasswordManagerTest, FormSubmitEmptyStore) { | 117 TEST_F(PasswordManagerTest, FormSubmitEmptyStore) { |
114 // Test that observing a newly submitted form shows the save password bar. | 118 // Test that observing a newly submitted form shows the save password bar. |
115 std::vector<PasswordForm*> result; // Empty password store. | 119 std::vector<PasswordForm*> result; // Empty password store. |
116 EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0)); | 120 EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0)); |
117 EXPECT_CALL(*store_, GetLogins(_,_)) | 121 EXPECT_CALL(*store_, GetLogins(_,_)) |
118 .WillOnce(DoAll(WithArg<1>(InvokeConsumer(0, result)), Return(0))); | 122 .WillOnce(DoAll(WithArg<1>(InvokeConsumer(0, result)), Return(0))); |
119 std::vector<PasswordForm> observed; | 123 std::vector<PasswordForm> observed; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 PasswordForm form(MakeSimpleForm()); | 156 PasswordForm form(MakeSimpleForm()); |
153 observed.push_back(form); | 157 observed.push_back(form); |
154 manager()->OnPasswordFormsParsed(observed); // The initial load. | 158 manager()->OnPasswordFormsParsed(observed); // The initial load. |
155 manager()->OnPasswordFormsRendered(observed); // The initial layout. | 159 manager()->OnPasswordFormsRendered(observed); // The initial layout. |
156 | 160 |
157 // Simulate the user generating the password and submitting the form. | 161 // Simulate the user generating the password and submitting the form. |
158 manager()->SetFormHasGeneratedPassword(form); | 162 manager()->SetFormHasGeneratedPassword(form); |
159 manager()->ProvisionallySavePassword(form); | 163 manager()->ProvisionallySavePassword(form); |
160 | 164 |
161 // The user should not be presented with an infobar as they have already given | 165 // The user should not be presented with an infobar as they have already given |
162 // consent. The form should be saved once navigation occurs. | 166 // consent by using the generated password. The form should be saved once |
| 167 // navigation occurs. |
163 EXPECT_CALL(delegate_, | 168 EXPECT_CALL(delegate_, |
164 AddSavePasswordInfoBarIfPermitted(_)).Times(Exactly(0)); | 169 AddSavePasswordInfoBarIfPermitted(_)).Times(Exactly(0)); |
165 EXPECT_CALL(*store_, AddLogin(FormMatches(form))); | 170 EXPECT_CALL(*store_, AddLogin(FormMatches(form))); |
166 | 171 |
167 // Now the password manager waits for the navigation to complete. | 172 // Now the password manager waits for the navigation to complete. |
168 observed.clear(); | 173 observed.clear(); |
169 manager()->OnPasswordFormsParsed(observed); // The post-navigation load. | 174 manager()->OnPasswordFormsParsed(observed); // The post-navigation load. |
170 manager()->OnPasswordFormsRendered(observed); // The post-navigation layout. | 175 manager()->OnPasswordFormsRendered(observed); // The post-navigation layout. |
171 } | 176 } |
172 | 177 |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 Value::CreateBooleanValue(false)); | 368 Value::CreateBooleanValue(false)); |
364 EXPECT_CALL(delegate_, FillPasswordForm(_)); | 369 EXPECT_CALL(delegate_, FillPasswordForm(_)); |
365 EXPECT_CALL(*store_, GetLogins(_, _)) | 370 EXPECT_CALL(*store_, GetLogins(_, _)) |
366 .WillRepeatedly(DoAll(WithArg<1>(InvokeConsumer(0, result)), Return(0))); | 371 .WillRepeatedly(DoAll(WithArg<1>(InvokeConsumer(0, result)), Return(0))); |
367 std::vector<PasswordForm> observed; | 372 std::vector<PasswordForm> observed; |
368 PasswordForm form(MakeSimpleForm()); | 373 PasswordForm form(MakeSimpleForm()); |
369 observed.push_back(form); | 374 observed.push_back(form); |
370 manager()->OnPasswordFormsParsed(observed); | 375 manager()->OnPasswordFormsParsed(observed); |
371 } | 376 } |
372 | 377 |
| 378 TEST_F(PasswordManagerTest, FormNotSavedAutocompleteOff) { |
| 379 // Test password form with non-generated password will not be saved if |
| 380 // autocomplete=off. |
| 381 std::vector<PasswordForm*> result; // Empty password store. |
| 382 EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0)); |
| 383 EXPECT_CALL(*store_, GetLogins(_,_)) |
| 384 .WillOnce(DoAll(WithArg<1>(InvokeConsumer(0, result)), Return(0))); |
| 385 std::vector<PasswordForm> observed; |
| 386 PasswordForm form(MakeSimpleForm()); |
| 387 form.password_autocomplete_set = false; |
| 388 observed.push_back(form); |
| 389 manager()->OnPasswordFormsParsed(observed); // The initial load. |
| 390 manager()->OnPasswordFormsRendered(observed); // The initial layout. |
| 391 |
| 392 // And the form submit contract is to call ProvisionallySavePassword. |
| 393 manager()->ProvisionallySavePassword(form); |
| 394 |
| 395 // Password form should not be saved. |
| 396 EXPECT_CALL(delegate_, |
| 397 AddSavePasswordInfoBarIfPermitted(_)).Times(Exactly(0)); |
| 398 EXPECT_CALL(*store_, AddLogin(FormMatches(form))).Times(Exactly(0)); |
| 399 |
| 400 // Now the password manager waits for the navigation to complete. |
| 401 observed.clear(); |
| 402 manager()->OnPasswordFormsParsed(observed); // The post-navigation load. |
| 403 manager()->OnPasswordFormsRendered(observed); // The post-navigation layout. |
| 404 } |
| 405 |
| 406 TEST_F(PasswordManagerTest, GeneratedPasswordFormSavedAutocompleteOff) { |
| 407 // Test password form with generated password will still be saved if |
| 408 // autocomplete=off. |
| 409 std::vector<PasswordForm*> result; // Empty password store. |
| 410 EXPECT_CALL(delegate_, FillPasswordForm(_)).Times(Exactly(0)); |
| 411 EXPECT_CALL(*store_, GetLogins(_,_)) |
| 412 .WillOnce(DoAll(WithArg<1>(InvokeConsumer(0, result)), Return(0))); |
| 413 std::vector<PasswordForm> observed; |
| 414 PasswordForm form(MakeSimpleForm()); |
| 415 form.password_autocomplete_set = false; |
| 416 observed.push_back(form); |
| 417 manager()->OnPasswordFormsParsed(observed); // The initial load. |
| 418 manager()->OnPasswordFormsRendered(observed); // The initial layout. |
| 419 |
| 420 // Simulate the user generating the password and submitting the form. |
| 421 manager()->SetFormHasGeneratedPassword(form); |
| 422 manager()->ProvisionallySavePassword(form); |
| 423 |
| 424 // The user should not be presented with an infobar as they have already given |
| 425 // consent by using the generated password. The form should be saved once |
| 426 // navigation occurs. |
| 427 EXPECT_CALL(delegate_, |
| 428 AddSavePasswordInfoBarIfPermitted(_)).Times(Exactly(0)); |
| 429 EXPECT_CALL(*store_, AddLogin(FormMatches(form))); |
| 430 |
| 431 // Now the password manager waits for the navigation to complete. |
| 432 observed.clear(); |
| 433 manager()->OnPasswordFormsParsed(observed); // The post-navigation load. |
| 434 manager()->OnPasswordFormsRendered(observed); // The post-navigation layout. |
| 435 } |
OLD | NEW |