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

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

Issue 208453002: Add "previewing on hover" support for password field. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update code as per review comments on SelectionRange. Created 6 years, 7 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 (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 "base/strings/string_util.h" 5 #include "base/strings/string_util.h"
6 #include "base/strings/utf_string_conversions.h" 6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/test/base/chrome_render_view_test.h" 7 #include "chrome/test/base/chrome_render_view_test.h"
8 #include "components/autofill/content/common/autofill_messages.h" 8 #include "components/autofill/content/common/autofill_messages.h"
9 #include "components/autofill/content/renderer/autofill_agent.h" 9 #include "components/autofill/content/renderer/autofill_agent.h"
10 #include "components/autofill/content/renderer/form_autofill_util.h" 10 #include "components/autofill/content/renderer/form_autofill_util.h"
(...skipping 1005 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 ASCIIToUTF16("usernameOnchangeCalled ? 1 : 0"), 1016 ASCIIToUTF16("usernameOnchangeCalled ? 1 : 0"),
1017 &username_onchange_called)); 1017 &username_onchange_called));
1018 EXPECT_EQ(1, username_onchange_called); 1018 EXPECT_EQ(1, username_onchange_called);
1019 ASSERT_TRUE( 1019 ASSERT_TRUE(
1020 ExecuteJavaScriptAndReturnIntValue( 1020 ExecuteJavaScriptAndReturnIntValue(
1021 ASCIIToUTF16("passwordOnchangeCalled ? 1 : 0"), 1021 ASCIIToUTF16("passwordOnchangeCalled ? 1 : 0"),
1022 &password_onchange_called)); 1022 &password_onchange_called));
1023 EXPECT_EQ(1, password_onchange_called); 1023 EXPECT_EQ(1, password_onchange_called);
1024 } 1024 }
1025 1025
1026 // Tests that |AcceptSuggestion| properly fills the username and password. 1026 // Tests that |FillSuggestion| properly fills the username and password.
1027 TEST_F(PasswordAutofillAgentTest, AcceptSuggestion) { 1027 TEST_F(PasswordAutofillAgentTest, FillSuggestion) {
1028 // Simulate the browser sending the login info, but set |wait_for_username| 1028 // Simulate the browser sending the login info, but set |wait_for_username|
1029 // to prevent the form from being immediately filled. 1029 // to prevent the form from being immediately filled.
1030 fill_data_.wait_for_username = true; 1030 fill_data_.wait_for_username = true;
1031 SimulateOnFillPasswordForm(fill_data_); 1031 SimulateOnFillPasswordForm(fill_data_);
1032 1032
1033 // Neither field should have been autocompleted. 1033 // Neither field should have been autocompleted.
1034 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 1034 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1035 1035
1036 // If the password field is not autocompletable, it should not be affected. 1036 // If the password field is not autocompletable, it should not be affected.
1037 SetElementReadOnly(password_element_, true); 1037 SetElementReadOnly(password_element_, true);
1038 EXPECT_FALSE(password_autofill_->AcceptSuggestion( 1038 EXPECT_FALSE(password_autofill_->FillSuggestion(
1039 username_element_, kAliceUsername, kAlicePassword)); 1039 username_element_, kAliceUsername, kAlicePassword));
1040 CheckTextFieldsDOMState(std::string(), false, std::string(), false); 1040 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1041 SetElementReadOnly(password_element_, false); 1041 SetElementReadOnly(password_element_, false);
1042 1042
1043 // After accepting the suggestion, both fields should be autocompleted. 1043 // After filling with the suggestion, both fields should be autocompleted.
1044 EXPECT_TRUE(password_autofill_->AcceptSuggestion( 1044 EXPECT_TRUE(password_autofill_->FillSuggestion(
1045 username_element_, kAliceUsername, kAlicePassword)); 1045 username_element_, kAliceUsername, kAlicePassword));
1046 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true); 1046 CheckTextFieldsDOMState(kAliceUsername, true, kAlicePassword, true);
1047 int username_length = strlen(kAliceUsername); 1047 int username_length = strlen(kAliceUsername);
1048 CheckUsernameSelection(username_length, username_length); 1048 CheckUsernameSelection(username_length, username_length);
1049 1049
1050 // Try accepting a suggestion with a password different from the one that was 1050 // Try Filling with a suggestion with password different from the one that was
1051 // initially sent to the renderer. 1051 // initially sent to the renderer.
1052 EXPECT_TRUE(password_autofill_->AcceptSuggestion( 1052 EXPECT_TRUE(password_autofill_->FillSuggestion(
1053 username_element_, kBobUsername, kCarolPassword)); 1053 username_element_, kBobUsername, kCarolPassword));
1054 CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true); 1054 CheckTextFieldsDOMState(kBobUsername, true, kCarolPassword, true);
1055 username_length = strlen(kBobUsername); 1055 username_length = strlen(kBobUsername);
1056 CheckUsernameSelection(username_length, username_length); 1056 CheckUsernameSelection(username_length, username_length);
1057 } 1057 }
1058 1058
1059 // Tests that |PreviewSuggestion| properly previews the username and password.
1060 TEST_F(PasswordAutofillAgentTest, PreviewSuggestion) {
1061 // Simulate the browser sending the login info, but set |wait_for_username|
1062 // to prevent the form from being immediately filled.
1063 fill_data_.wait_for_username = true;
1064 SimulateOnFillPasswordForm(fill_data_);
1065
1066 // Neither field should have been autocompleted.
1067 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1068
1069 // If the password field is not autocompletable, it should not be affected.
1070 SetElementReadOnly(password_element_, true);
1071 EXPECT_FALSE(password_autofill_->PreviewSuggestion(
1072 username_element_, kAliceUsername, kAlicePassword));
1073 EXPECT_EQ(std::string(), username_element_.suggestedValue().utf8());
1074 EXPECT_FALSE(username_element_.isAutofilled());
1075 EXPECT_EQ(std::string(), password_element_.suggestedValue().utf8());
1076 EXPECT_FALSE(password_element_.isAutofilled());
1077 SetElementReadOnly(password_element_, false);
1078
1079 // After selecting the suggestion, both fields should be previewed
1080 // with suggested values.
1081 EXPECT_TRUE(password_autofill_->PreviewSuggestion(
1082 username_element_, kAliceUsername, kAlicePassword));
1083 EXPECT_EQ(
1084 kAliceUsername,
1085 static_cast<std::string>(username_element_.suggestedValue().utf8()));
1086 EXPECT_TRUE(username_element_.isAutofilled());
1087 EXPECT_EQ(
1088 kAlicePassword,
1089 static_cast<std::string>(password_element_.suggestedValue().utf8()));
1090 EXPECT_TRUE(password_element_.isAutofilled());
1091 int username_length = strlen(kAliceUsername);
1092 CheckUsernameSelection(0, username_length);
1093
1094 // Try previewing with a password different from the one that was initially
1095 // sent to the renderer.
1096 EXPECT_TRUE(password_autofill_->PreviewSuggestion(
1097 username_element_, kBobUsername, kCarolPassword));
1098 EXPECT_EQ(
1099 kBobUsername,
1100 static_cast<std::string>(username_element_.suggestedValue().utf8()));
1101 EXPECT_TRUE(username_element_.isAutofilled());
1102 EXPECT_EQ(
1103 kCarolPassword,
1104 static_cast<std::string>(password_element_.suggestedValue().utf8()));
1105 EXPECT_TRUE(password_element_.isAutofilled());
1106 username_length = strlen(kBobUsername);
1107 CheckUsernameSelection(0, username_length);
1108 }
1109
1110 // Tests that |PreviewSuggestion| properly sets the username selection range.
1111 TEST_F(PasswordAutofillAgentTest, PreviewSuggestionSelectionRange) {
1112 username_element_.setValue(WebString::fromUTF8("ali"));
1113 username_element_.setAutofilled(true);
1114
1115 CheckTextFieldsDOMState("ali", true, std::string(), false);
1116
1117 // Simulate the browser sending the login info, but set |wait_for_username|
1118 // to prevent the form from being immediately filled.
1119 fill_data_.wait_for_username = true;
1120 SimulateOnFillPasswordForm(fill_data_);
1121
1122 EXPECT_TRUE(password_autofill_->PreviewSuggestion(
1123 username_element_, kAliceUsername, kAlicePassword));
1124 EXPECT_EQ(
1125 kAliceUsername,
1126 static_cast<std::string>(username_element_.suggestedValue().utf8()));
1127 EXPECT_TRUE(username_element_.isAutofilled());
1128 EXPECT_EQ(
1129 kAlicePassword,
1130 static_cast<std::string>(password_element_.suggestedValue().utf8()));
1131 EXPECT_TRUE(password_element_.isAutofilled());
1132 int username_length = strlen(kAliceUsername);
1133 CheckUsernameSelection(3, username_length);
1134 }
1135
1136 // Tests that |ClearPreview| properly clears previewed username and password
1137 // with password being previously autofilled.
1138 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithPasswordAutofilled) {
1139 password_element_.setValue(WebString::fromUTF8("sec"));
1140 password_element_.setAutofilled(true);
1141
1142 // Simulate the browser sending the login info, but set |wait_for_username|
1143 // to prevent the form from being immediately filled.
1144 fill_data_.wait_for_username = true;
1145 SimulateOnFillPasswordForm(fill_data_);
1146
1147 CheckTextFieldsDOMState(std::string(), false, "sec", true);
1148
1149 EXPECT_TRUE(password_autofill_->PreviewSuggestion(
1150 username_element_, kAliceUsername, kAlicePassword));
1151
1152 EXPECT_TRUE(password_autofill_->DidClearAutofillSelection(
1153 username_element_));
1154
1155 EXPECT_TRUE(username_element_.value().isEmpty());
1156 EXPECT_TRUE(username_element_.suggestedValue().isEmpty());
1157 EXPECT_FALSE(username_element_.isAutofilled());
1158 EXPECT_EQ(ASCIIToUTF16("sec"), password_element_.value());
1159 EXPECT_TRUE(password_element_.suggestedValue().isEmpty());
1160 EXPECT_TRUE(password_element_.isAutofilled());
1161 CheckUsernameSelection(0, 0);
1162 }
1163
1164 // Tests that |ClearPreview| properly clears previewed username and password
1165 // with username being previously autofilled.
1166 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithUsernameAutofilled) {
1167 username_element_.setValue(WebString::fromUTF8("ali"));
1168 username_element_.setAutofilled(true);
1169
1170 // Simulate the browser sending the login info, but set |wait_for_username|
1171 // to prevent the form from being immediately filled.
1172 fill_data_.wait_for_username = true;
1173 SimulateOnFillPasswordForm(fill_data_);
1174
1175 CheckTextFieldsDOMState("ali", true, std::string(), false);
1176
1177 EXPECT_TRUE(password_autofill_->PreviewSuggestion(
1178 username_element_, kAliceUsername, kAlicePassword));
1179
1180 EXPECT_TRUE(password_autofill_->DidClearAutofillSelection(
1181 username_element_));
1182
1183 EXPECT_EQ(ASCIIToUTF16("ali"), username_element_.value());
1184 EXPECT_TRUE(username_element_.suggestedValue().isEmpty());
1185 EXPECT_TRUE(username_element_.isAutofilled());
1186 EXPECT_TRUE(password_element_.value().isEmpty());
1187 EXPECT_TRUE(password_element_.suggestedValue().isEmpty());
1188 EXPECT_FALSE(password_element_.isAutofilled());
1189 CheckUsernameSelection(3, 3);
1190 }
1191
1192 // Tests that |ClearPreview| properly clears previewed username and password
1193 // with username and password being previously autofilled.
1194 TEST_F(PasswordAutofillAgentTest,
1195 ClearPreviewWithAutofilledUsernameAndPassword) {
1196 username_element_.setValue(WebString::fromUTF8("ali"));
1197 username_element_.setAutofilled(true);
1198 password_element_.setValue(WebString::fromUTF8("sec"));
1199 password_element_.setAutofilled(true);
1200
1201 // Simulate the browser sending the login info, but set |wait_for_username|
1202 // to prevent the form from being immediately filled.
1203 fill_data_.wait_for_username = true;
1204 SimulateOnFillPasswordForm(fill_data_);
1205
1206 CheckTextFieldsDOMState("ali", true, "sec", true);
1207
1208 EXPECT_TRUE(password_autofill_->PreviewSuggestion(
1209 username_element_, kAliceUsername, kAlicePassword));
1210
1211 EXPECT_TRUE(password_autofill_->DidClearAutofillSelection(
1212 username_element_));
1213
1214 EXPECT_EQ(ASCIIToUTF16("ali"), username_element_.value());
1215 EXPECT_TRUE(username_element_.suggestedValue().isEmpty());
1216 EXPECT_TRUE(username_element_.isAutofilled());
1217 EXPECT_EQ(ASCIIToUTF16("sec"), password_element_.value());
1218 EXPECT_TRUE(password_element_.suggestedValue().isEmpty());
1219 EXPECT_TRUE(password_element_.isAutofilled());
1220 CheckUsernameSelection(3, 3);
1221 }
1222
1223 // Tests that |ClearPreview| properly clears previewed username and password
1224 // with neither username nor password being previously autofilled.
1225 TEST_F(PasswordAutofillAgentTest,
1226 ClearPreviewWithNotAutofilledUsernameAndPassword) {
1227 // Simulate the browser sending the login info, but set |wait_for_username|
1228 // to prevent the form from being immediately filled.
1229 fill_data_.wait_for_username = true;
1230 SimulateOnFillPasswordForm(fill_data_);
1231
1232 CheckTextFieldsDOMState(std::string(), false, std::string(), false);
1233
1234 EXPECT_TRUE(password_autofill_->PreviewSuggestion(
1235 username_element_, kAliceUsername, kAlicePassword));
1236
1237 EXPECT_TRUE(password_autofill_->DidClearAutofillSelection(
1238 username_element_));
1239
1240 EXPECT_TRUE(username_element_.value().isEmpty());
1241 EXPECT_TRUE(username_element_.suggestedValue().isEmpty());
1242 EXPECT_FALSE(username_element_.isAutofilled());
1243 EXPECT_TRUE(password_element_.value().isEmpty());
1244 EXPECT_TRUE(password_element_.suggestedValue().isEmpty());
1245 EXPECT_FALSE(password_element_.isAutofilled());
1246 CheckUsernameSelection(0, 0);
1247 }
1248
1249 // Tests that |ClearPreview| properly restores the original selection range of
1250 // username field that has initially been filled by inline autocomplete.
1251 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithInlineAutocompletedUsername) {
1252 // Simulate the browser sending back the login info.
1253 SimulateOnFillPasswordForm(fill_data_);
1254
1255 // Clear the text fields to start fresh.
1256 ClearUsernameAndPasswordFields();
1257
1258 // Simulate the user typing in the first letter of 'alice', a stored username.
1259 SimulateUsernameChange("a", true);
1260 // Both the username and password text fields should reflect selection of the
1261 // stored login.
1262 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true);
1263 // The selection should have been set to 'lice', the last 4 letters.
1264 CheckUsernameSelection(1, 5);
1265
1266 EXPECT_TRUE(password_autofill_->PreviewSuggestion(
1267 username_element_, "alicia", "secret"));
1268 EXPECT_EQ(
1269 "alicia",
1270 static_cast<std::string>(username_element_.suggestedValue().utf8()));
1271 EXPECT_TRUE(username_element_.isAutofilled());
1272 EXPECT_EQ(
1273 "secret",
1274 static_cast<std::string>(password_element_.suggestedValue().utf8()));
1275 EXPECT_TRUE(password_element_.isAutofilled());
1276 CheckUsernameSelection(1, 6);
1277
1278 EXPECT_TRUE(password_autofill_->DidClearAutofillSelection(
1279 username_element_));
1280
1281 EXPECT_EQ(kAliceUsername, username_element_.value().utf8());
1282 EXPECT_TRUE(username_element_.suggestedValue().isEmpty());
1283 EXPECT_TRUE(username_element_.isAutofilled());
1284 EXPECT_TRUE(password_element_.value().isEmpty());
1285 EXPECT_TRUE(password_element_.suggestedValue().isEmpty());
1286 EXPECT_TRUE(password_element_.isAutofilled());
1287 CheckUsernameSelection(1, 5);
1288 }
1289
1059 // Tests that logging is off by default. 1290 // Tests that logging is off by default.
1060 TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_NoMessage) { 1291 TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_NoMessage) {
1061 render_thread_->sink().ClearMessages(); 1292 render_thread_->sink().ClearMessages();
1062 SendVisiblePasswordForms(); 1293 SendVisiblePasswordForms();
1063 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( 1294 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching(
1064 AutofillHostMsg_RecordSavePasswordProgress::ID); 1295 AutofillHostMsg_RecordSavePasswordProgress::ID);
1065 EXPECT_FALSE(message); 1296 EXPECT_FALSE(message);
1066 } 1297 }
1067 1298
1068 // Test that logging can be turned on by a message. 1299 // Test that logging can be turned on by a message.
(...skipping 23 matching lines...) Expand all
1092 ->OnMessageReceived(msg_deactivate)); 1323 ->OnMessageReceived(msg_deactivate));
1093 1324
1094 render_thread_->sink().ClearMessages(); 1325 render_thread_->sink().ClearMessages();
1095 SendVisiblePasswordForms(); 1326 SendVisiblePasswordForms();
1096 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( 1327 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching(
1097 AutofillHostMsg_RecordSavePasswordProgress::ID); 1328 AutofillHostMsg_RecordSavePasswordProgress::ID);
1098 EXPECT_FALSE(message); 1329 EXPECT_FALSE(message);
1099 } 1330 }
1100 1331
1101 } // namespace autofill 1332 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698