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

Side by Side Diff: chrome/renderer/autofill/password_autofill_agent_browsertest.cc

Issue 2317983002: Enable manual password autofilling if the username field is read-only. (Closed)
Patch Set: Add IsAutocompletable check. Small tests refactoring. Created 4 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
« no previous file with comments | « no previous file | components/autofill/content/renderer/password_autofill_agent.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <tuple> 5 #include <tuple>
6 6
7 #include "base/feature_list.h" 7 #include "base/feature_list.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 SimulateUserInputChangeForElement(&password_element_, password); 391 SimulateUserInputChangeForElement(&password_element_, password);
392 } 392 }
393 393
394 void CheckTextFieldsStateForElements(const WebInputElement& username_element, 394 void CheckTextFieldsStateForElements(const WebInputElement& username_element,
395 const std::string& username, 395 const std::string& username,
396 bool username_autofilled, 396 bool username_autofilled,
397 const WebInputElement& password_element, 397 const WebInputElement& password_element,
398 const std::string& password, 398 const std::string& password,
399 bool password_autofilled, 399 bool password_autofilled,
400 bool checkSuggestedValue) { 400 bool checkSuggestedValue) {
401 EXPECT_EQ(username, 401 EXPECT_EQ(username, username_element.value().utf8());
402 static_cast<std::string>(username_element.value().utf8()));
403 EXPECT_EQ(username_autofilled, username_element.isAutofilled()); 402 EXPECT_EQ(username_autofilled, username_element.isAutofilled());
404 EXPECT_EQ(password, 403 EXPECT_EQ(password, checkSuggestedValue
405 static_cast<std::string>( 404 ? password_element.suggestedValue().utf8()
406 checkSuggestedValue ? password_element.suggestedValue().utf8() 405 : password_element.value().utf8())
407 : password_element.value().utf8()))
408 << "checkSuggestedValue == " << checkSuggestedValue; 406 << "checkSuggestedValue == " << checkSuggestedValue;
409 EXPECT_EQ(password_autofilled, password_element.isAutofilled()); 407 EXPECT_EQ(password_autofilled, password_element.isAutofilled());
410 } 408 }
411 409
412 // Checks the DOM-accessible value of the username element and the 410 // Checks the DOM-accessible value of the username element and the
413 // *suggested* value of the password element. 411 // *suggested* value of the password element.
414 void CheckTextFieldsState(const std::string& username, 412 void CheckTextFieldsState(const std::string& username,
415 bool username_autofilled, 413 bool username_autofilled,
416 const std::string& password, 414 const std::string& password,
417 bool password_autofilled) { 415 bool password_autofilled) {
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after
990 // Try Filling with a suggestion with password different from the one that was 988 // Try Filling with a suggestion with password different from the one that was
991 // initially sent to the renderer. 989 // initially sent to the renderer.
992 EXPECT_TRUE(password_autofill_agent_->FillSuggestion( 990 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
993 username_element_, ASCIIToUTF16(kBobUsername), 991 username_element_, ASCIIToUTF16(kBobUsername),
994 ASCIIToUTF16(kCarolPassword))); 992 ASCIIToUTF16(kCarolPassword)));
995 CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true); 993 CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true);
996 username_length = strlen(kBobUsername); 994 username_length = strlen(kBobUsername);
997 CheckUsernameSelection(username_length, username_length); 995 CheckUsernameSelection(username_length, username_length);
998 } 996 }
999 997
998 // Tests that |FillSuggestion| properly fills the password if username is
999 // read-only.
1000 TEST_F(PasswordAutofillAgentTest, FillSuggestionIfUsernameReadOnly) {
1001 // Simulate the browser sending the login info.
1002 SetElementReadOnly(username_element_, true);
1003 SimulateOnFillPasswordForm(fill_data_);
1004
1005 // Neither field should have been autocompleted.
1006 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1007
1008 // Username field is not autocompletable, it should not be affected.
1009 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
1010 password_element_, ASCIIToUTF16(kAliceUsername),
1011 ASCIIToUTF16(kAlicePassword)));
1012 CheckTextFieldsDOMState(std::string(), false, kAlicePassword, true);
1013
1014 // Try Filling with a suggestion with password different from the one that was
1015 // initially sent to the renderer.
1016 EXPECT_TRUE(password_autofill_agent_->FillSuggestion(
1017 password_element_, ASCIIToUTF16(kBobUsername),
1018 ASCIIToUTF16(kCarolPassword)));
1019 CheckTextFieldsDOMState(std::string(), false, kCarolPassword, true);
1020 }
1021
1000 // Tests that |PreviewSuggestion| properly previews the username and password. 1022 // Tests that |PreviewSuggestion| properly previews the username and password.
1001 TEST_F(PasswordAutofillAgentTest, PreviewSuggestion) { 1023 TEST_F(PasswordAutofillAgentTest, PreviewSuggestion) {
1002 // Simulate the browser sending the login info, but set |wait_for_username| 1024 // Simulate the browser sending the login info, but set |wait_for_username|
1003 // to prevent the form from being immediately filled. 1025 // to prevent the form from being immediately filled.
1004 fill_data_.wait_for_username = true; 1026 fill_data_.wait_for_username = true;
1005 SimulateOnFillPasswordForm(fill_data_); 1027 SimulateOnFillPasswordForm(fill_data_);
1006 1028
1007 // Neither field should have been autocompleted. 1029 // Neither field should have been autocompleted.
1008 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 1030 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1009 1031
1010 // If the password field is not autocompletable, it should not be affected. 1032 // If the password field is not autocompletable, it should not be affected.
1011 SetElementReadOnly(password_element_, true); 1033 SetElementReadOnly(password_element_, true);
1012 EXPECT_FALSE(password_autofill_agent_->PreviewSuggestion( 1034 EXPECT_FALSE(password_autofill_agent_->PreviewSuggestion(
1013 username_element_, kAliceUsername, kAlicePassword)); 1035 username_element_, kAliceUsername, kAlicePassword));
1014 EXPECT_EQ(std::string(), username_element_.suggestedValue().utf8()); 1036 EXPECT_EQ(std::string(), username_element_.suggestedValue().utf8());
1015 EXPECT_FALSE(username_element_.isAutofilled()); 1037 EXPECT_FALSE(username_element_.isAutofilled());
1016 EXPECT_EQ(std::string(), password_element_.suggestedValue().utf8()); 1038 EXPECT_EQ(std::string(), password_element_.suggestedValue().utf8());
1017 EXPECT_FALSE(password_element_.isAutofilled()); 1039 EXPECT_FALSE(password_element_.isAutofilled());
1018 SetElementReadOnly(password_element_, false); 1040 SetElementReadOnly(password_element_, false);
1019 1041
1020 // After selecting the suggestion, both fields should be previewed 1042 // After selecting the suggestion, both fields should be previewed
1021 // with suggested values. 1043 // with suggested values.
1022 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1044 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1023 username_element_, kAliceUsername, kAlicePassword)); 1045 username_element_, kAliceUsername, kAlicePassword));
1024 EXPECT_EQ( 1046 EXPECT_EQ(kAliceUsername, username_element_.suggestedValue().utf8());
1025 kAliceUsername,
1026 static_cast<std::string>(username_element_.suggestedValue().utf8()));
1027 EXPECT_TRUE(username_element_.isAutofilled()); 1047 EXPECT_TRUE(username_element_.isAutofilled());
1028 EXPECT_EQ( 1048 EXPECT_EQ(kAlicePassword, password_element_.suggestedValue().utf8());
1029 kAlicePassword,
1030 static_cast<std::string>(password_element_.suggestedValue().utf8()));
1031 EXPECT_TRUE(password_element_.isAutofilled()); 1049 EXPECT_TRUE(password_element_.isAutofilled());
1032 int username_length = strlen(kAliceUsername); 1050 int username_length = strlen(kAliceUsername);
1033 CheckUsernameSelection(0, username_length); 1051 CheckUsernameSelection(0, username_length);
1034 1052
1035 // Try previewing with a password different from the one that was initially 1053 // Try previewing with a password different from the one that was initially
1036 // sent to the renderer. 1054 // sent to the renderer.
1037 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1055 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1038 username_element_, kBobUsername, kCarolPassword)); 1056 username_element_, kBobUsername, kCarolPassword));
1039 EXPECT_EQ( 1057 EXPECT_EQ(kBobUsername, username_element_.suggestedValue().utf8());
1040 kBobUsername,
1041 static_cast<std::string>(username_element_.suggestedValue().utf8()));
1042 EXPECT_TRUE(username_element_.isAutofilled()); 1058 EXPECT_TRUE(username_element_.isAutofilled());
1043 EXPECT_EQ( 1059 EXPECT_EQ(kCarolPassword, password_element_.suggestedValue().utf8());
1044 kCarolPassword,
1045 static_cast<std::string>(password_element_.suggestedValue().utf8()));
1046 EXPECT_TRUE(password_element_.isAutofilled()); 1060 EXPECT_TRUE(password_element_.isAutofilled());
1047 username_length = strlen(kBobUsername); 1061 username_length = strlen(kBobUsername);
1048 CheckUsernameSelection(0, username_length); 1062 CheckUsernameSelection(0, username_length);
1049 } 1063 }
1050 1064
1065 // Tests that |PreviewSuggestion| properly previews the password if username is
1066 // read-only.
1067 TEST_F(PasswordAutofillAgentTest, PreviewSuggestionIfUsernameReadOnly) {
1068 // Simulate the browser sending the login info.
1069 SetElementReadOnly(username_element_, true);
1070 SimulateOnFillPasswordForm(fill_data_);
1071
1072 // Neither field should have been autocompleted.
1073 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1074
1075 // Username field is not autocompletable, it should not be affected.
1076 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1077 password_element_, kAliceUsername, kAlicePassword));
1078 EXPECT_EQ(std::string(), username_element_.suggestedValue().utf8());
1079 EXPECT_FALSE(username_element_.isAutofilled());
1080
1081 // Password field must be autofilled.
1082 EXPECT_EQ(kAlicePassword, password_element_.suggestedValue().utf8());
1083 EXPECT_TRUE(password_element_.isAutofilled());
1084
1085 // Try previewing with a password different from the one that was initially
1086 // sent to the renderer.
1087 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1088 password_element_, kBobUsername, kCarolPassword));
1089 EXPECT_EQ(std::string(), username_element_.suggestedValue().utf8());
1090 EXPECT_FALSE(username_element_.isAutofilled());
1091 EXPECT_EQ(kCarolPassword, password_element_.suggestedValue().utf8());
1092 EXPECT_TRUE(password_element_.isAutofilled());
1093 }
1094
1051 // Tests that |PreviewSuggestion| properly sets the username selection range. 1095 // Tests that |PreviewSuggestion| properly sets the username selection range.
1052 TEST_F(PasswordAutofillAgentTest, PreviewSuggestionSelectionRange) { 1096 TEST_F(PasswordAutofillAgentTest, PreviewSuggestionSelectionRange) {
1053 username_element_.setValue(WebString::fromUTF8("ali")); 1097 username_element_.setValue(WebString::fromUTF8("ali"));
1054 username_element_.setSelectionRange(3, 3); 1098 username_element_.setSelectionRange(3, 3);
1055 username_element_.setAutofilled(true); 1099 username_element_.setAutofilled(true);
1056 1100
1057 CheckTextFieldsDOMState("ali", true, std::string(), false); 1101 CheckTextFieldsDOMState("ali", true, std::string(), false);
1058 1102
1059 // Simulate the browser sending the login info, but set |wait_for_username| 1103 // Simulate the browser sending the login info, but set |wait_for_username|
1060 // to prevent the form from being immediately filled. 1104 // to prevent the form from being immediately filled.
1061 fill_data_.wait_for_username = true; 1105 fill_data_.wait_for_username = true;
1062 SimulateOnFillPasswordForm(fill_data_); 1106 SimulateOnFillPasswordForm(fill_data_);
1063 1107
1064 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion( 1108 EXPECT_TRUE(password_autofill_agent_->PreviewSuggestion(
1065 username_element_, kAliceUsername, kAlicePassword)); 1109 username_element_, kAliceUsername, kAlicePassword));
1066 EXPECT_EQ( 1110 EXPECT_EQ(kAliceUsername, username_element_.suggestedValue().utf8());
1067 kAliceUsername,
1068 static_cast<std::string>(username_element_.suggestedValue().utf8()));
1069 EXPECT_TRUE(username_element_.isAutofilled()); 1111 EXPECT_TRUE(username_element_.isAutofilled());
1070 EXPECT_EQ( 1112 EXPECT_EQ(kAlicePassword, password_element_.suggestedValue().utf8());
1071 kAlicePassword,
1072 static_cast<std::string>(password_element_.suggestedValue().utf8()));
1073 EXPECT_TRUE(password_element_.isAutofilled()); 1113 EXPECT_TRUE(password_element_.isAutofilled());
1074 int username_length = strlen(kAliceUsername); 1114 int username_length = strlen(kAliceUsername);
1075 CheckUsernameSelection(3, username_length); 1115 CheckUsernameSelection(3, username_length);
1076 } 1116 }
1077 1117
1078 // Tests that |ClearPreview| properly clears previewed username and password 1118 // Tests that |ClearPreview| properly clears previewed username and password
1079 // with password being previously autofilled. 1119 // with password being previously autofilled.
1080 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithPasswordAutofilled) { 1120 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithPasswordAutofilled) {
1081 password_element_.setValue(WebString::fromUTF8("sec")); 1121 password_element_.setValue(WebString::fromUTF8("sec"));
1082 password_element_.setAutofilled(true); 1122 password_element_.setAutofilled(true);
(...skipping 1334 matching lines...) Expand 10 before | Expand all | Expand 10 after
2417 EXPECT_FALSE(render_thread_->sink().GetFirstMessageMatching( 2457 EXPECT_FALSE(render_thread_->sink().GetFirstMessageMatching(
2418 AutofillHostMsg_ShowPasswordSuggestions::ID)); 2458 AutofillHostMsg_ShowPasswordSuggestions::ID));
2419 2459
2420 // But when the user clicks on the autofilled password field again it should 2460 // But when the user clicks on the autofilled password field again it should
2421 // still produce a suggestion dropdown. 2461 // still produce a suggestion dropdown.
2422 SimulateElementClick("password"); 2462 SimulateElementClick("password");
2423 CheckSuggestions("", false); 2463 CheckSuggestions("", false);
2424 } 2464 }
2425 2465
2426 } // namespace autofill 2466 } // namespace autofill
OLDNEW
« no previous file with comments | « no previous file | components/autofill/content/renderer/password_autofill_agent.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698