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 "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 Loading... |
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 // Selection range starts from the end of matching characters between |
| 1093 // username_element value and suggestedValue. value length is 0 in this case. |
| 1094 CheckUsernameSelection(0, username_length); |
| 1095 |
| 1096 // Try previewing with a password different from the one that was initially |
| 1097 // sent to the renderer. |
| 1098 EXPECT_TRUE(password_autofill_->PreviewSuggestion( |
| 1099 username_element_, kBobUsername, kCarolPassword)); |
| 1100 EXPECT_EQ( |
| 1101 kBobUsername, |
| 1102 static_cast<std::string>(username_element_.suggestedValue().utf8())); |
| 1103 EXPECT_TRUE(username_element_.isAutofilled()); |
| 1104 EXPECT_EQ( |
| 1105 kCarolPassword, |
| 1106 static_cast<std::string>(password_element_.suggestedValue().utf8())); |
| 1107 EXPECT_TRUE(password_element_.isAutofilled()); |
| 1108 username_length = strlen(kBobUsername); |
| 1109 CheckUsernameSelection(0, username_length); |
| 1110 } |
| 1111 |
| 1112 // Tests that |PreviewSuggestion| properly sets the username selection range. |
| 1113 TEST_F(PasswordAutofillAgentTest, PreviewSuggestionSelectionRange) { |
| 1114 username_element_.setValue(WebString::fromUTF8("ali")); |
| 1115 username_element_.setAutofilled(true); |
| 1116 |
| 1117 CheckTextFieldsDOMState("ali", true, std::string(), false); |
| 1118 |
| 1119 // Simulate the browser sending the login info, but set |wait_for_username| |
| 1120 // to prevent the form from being immediately filled. |
| 1121 fill_data_.wait_for_username = true; |
| 1122 SimulateOnFillPasswordForm(fill_data_); |
| 1123 |
| 1124 EXPECT_TRUE(password_autofill_->PreviewSuggestion( |
| 1125 username_element_, kAliceUsername, kAlicePassword)); |
| 1126 EXPECT_EQ( |
| 1127 kAliceUsername, |
| 1128 static_cast<std::string>(username_element_.suggestedValue().utf8())); |
| 1129 EXPECT_TRUE(username_element_.isAutofilled()); |
| 1130 EXPECT_EQ( |
| 1131 kAlicePassword, |
| 1132 static_cast<std::string>(password_element_.suggestedValue().utf8())); |
| 1133 EXPECT_TRUE(password_element_.isAutofilled()); |
| 1134 int username_length = strlen(kAliceUsername); |
| 1135 // Selection range starts from the end of matching characters between |
| 1136 // username_element value and suggestedValue. |
| 1137 CheckUsernameSelection(3, username_length); |
| 1138 } |
| 1139 |
| 1140 // Tests that |ClearPreview| properly clear previewed username and password |
| 1141 // with password being previously autofilled. |
| 1142 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithPasswordAutofilled) { |
| 1143 password_element_.setValue(WebString::fromUTF8("sec")); |
| 1144 password_element_.setAutofilled(true); |
| 1145 |
| 1146 // Simulate the browser sending the login info, but set |wait_for_username| |
| 1147 // to prevent the form from being immediately filled. |
| 1148 fill_data_.wait_for_username = true; |
| 1149 SimulateOnFillPasswordForm(fill_data_); |
| 1150 |
| 1151 CheckTextFieldsDOMState(std::string(), false, "sec", true); |
| 1152 |
| 1153 EXPECT_TRUE(password_autofill_->PreviewSuggestion( |
| 1154 username_element_, kAliceUsername, kAlicePassword)); |
| 1155 |
| 1156 EXPECT_TRUE(password_autofill_->DidClearAutofillSelection( |
| 1157 username_element_)); |
| 1158 |
| 1159 EXPECT_TRUE(username_element_.value().isEmpty()); |
| 1160 EXPECT_TRUE(username_element_.suggestedValue().isEmpty()); |
| 1161 EXPECT_FALSE(username_element_.isAutofilled()); |
| 1162 EXPECT_EQ(ASCIIToUTF16("sec"), password_element_.value()); |
| 1163 EXPECT_TRUE(password_element_.suggestedValue().isEmpty()); |
| 1164 EXPECT_TRUE(password_element_.isAutofilled()); |
| 1165 } |
| 1166 |
| 1167 // Tests that |ClearPreview| properly clear previewed username and password |
| 1168 // with username being previously autofilled. |
| 1169 TEST_F(PasswordAutofillAgentTest, ClearPreviewWithUserNameAutofilled) { |
| 1170 username_element_.setValue(WebString::fromUTF8("ali")); |
| 1171 username_element_.setAutofilled(true); |
| 1172 |
| 1173 // Simulate the browser sending the login info, but set |wait_for_username| |
| 1174 // to prevent the form from being immediately filled. |
| 1175 fill_data_.wait_for_username = true; |
| 1176 SimulateOnFillPasswordForm(fill_data_); |
| 1177 |
| 1178 CheckTextFieldsDOMState("ali", true, std::string(), false); |
| 1179 |
| 1180 EXPECT_TRUE(password_autofill_->PreviewSuggestion( |
| 1181 username_element_, kAliceUsername, kAlicePassword)); |
| 1182 |
| 1183 EXPECT_TRUE(password_autofill_->DidClearAutofillSelection( |
| 1184 username_element_)); |
| 1185 |
| 1186 EXPECT_EQ(ASCIIToUTF16("ali"), username_element_.value()); |
| 1187 EXPECT_TRUE(username_element_.suggestedValue().isEmpty()); |
| 1188 EXPECT_TRUE(username_element_.isAutofilled()); |
| 1189 EXPECT_TRUE(password_element_.value().isEmpty()); |
| 1190 EXPECT_TRUE(password_element_.suggestedValue().isEmpty()); |
| 1191 EXPECT_FALSE(password_element_.isAutofilled()); |
| 1192 } |
| 1193 |
| 1194 // Tests that |ClearPreview| properly clear previewed username and password |
| 1195 // with username and password being previously autofilled. |
| 1196 TEST_F(PasswordAutofillAgentTest, |
| 1197 ClearPreviewWithAutofilledUserNameAndPassword) { |
| 1198 username_element_.setValue(WebString::fromUTF8("ali")); |
| 1199 username_element_.setAutofilled(true); |
| 1200 password_element_.setValue(WebString::fromUTF8("sec")); |
| 1201 password_element_.setAutofilled(true); |
| 1202 |
| 1203 // Simulate the browser sending the login info, but set |wait_for_username| |
| 1204 // to prevent the form from being immediately filled. |
| 1205 fill_data_.wait_for_username = true; |
| 1206 SimulateOnFillPasswordForm(fill_data_); |
| 1207 |
| 1208 CheckTextFieldsDOMState("ali", true, "sec", true); |
| 1209 |
| 1210 EXPECT_TRUE(password_autofill_->PreviewSuggestion( |
| 1211 username_element_, kAliceUsername, kAlicePassword)); |
| 1212 |
| 1213 EXPECT_TRUE(password_autofill_->DidClearAutofillSelection( |
| 1214 username_element_)); |
| 1215 |
| 1216 EXPECT_EQ(ASCIIToUTF16("ali"), username_element_.value()); |
| 1217 EXPECT_TRUE(username_element_.suggestedValue().isEmpty()); |
| 1218 EXPECT_TRUE(username_element_.isAutofilled()); |
| 1219 EXPECT_EQ(ASCIIToUTF16("sec"), password_element_.value()); |
| 1220 EXPECT_TRUE(password_element_.suggestedValue().isEmpty()); |
| 1221 EXPECT_TRUE(password_element_.isAutofilled()); |
| 1222 } |
| 1223 |
| 1224 // Tests that |ClearPreview| properly clear previewed username and password |
| 1225 // with neither username nor password being previously autofilled. |
| 1226 TEST_F(PasswordAutofillAgentTest, |
| 1227 ClearPreviewWithNotAutofilledUserNameAndPassword) { |
| 1228 // Simulate the browser sending the login info, but set |wait_for_username| |
| 1229 // to prevent the form from being immediately filled. |
| 1230 fill_data_.wait_for_username = true; |
| 1231 SimulateOnFillPasswordForm(fill_data_); |
| 1232 |
| 1233 CheckTextFieldsDOMState(std::string(), false, std::string(), false); |
| 1234 |
| 1235 EXPECT_TRUE(password_autofill_->PreviewSuggestion( |
| 1236 username_element_, kAliceUsername, kAlicePassword)); |
| 1237 |
| 1238 EXPECT_TRUE(password_autofill_->DidClearAutofillSelection( |
| 1239 username_element_)); |
| 1240 |
| 1241 EXPECT_TRUE(username_element_.value().isEmpty()); |
| 1242 EXPECT_TRUE(username_element_.suggestedValue().isEmpty()); |
| 1243 EXPECT_FALSE(username_element_.isAutofilled()); |
| 1244 EXPECT_TRUE(password_element_.value().isEmpty()); |
| 1245 EXPECT_TRUE(password_element_.suggestedValue().isEmpty()); |
| 1246 EXPECT_FALSE(password_element_.isAutofilled()); |
| 1247 } |
| 1248 |
1059 // Tests that logging is off by default. | 1249 // Tests that logging is off by default. |
1060 TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_NoMessage) { | 1250 TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_NoMessage) { |
1061 render_thread_->sink().ClearMessages(); | 1251 render_thread_->sink().ClearMessages(); |
1062 SendVisiblePasswordForms(); | 1252 SendVisiblePasswordForms(); |
1063 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( | 1253 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( |
1064 AutofillHostMsg_RecordSavePasswordProgress::ID); | 1254 AutofillHostMsg_RecordSavePasswordProgress::ID); |
1065 EXPECT_FALSE(message); | 1255 EXPECT_FALSE(message); |
1066 } | 1256 } |
1067 | 1257 |
1068 // Test that logging can be turned on by a message. | 1258 // Test that logging can be turned on by a message. |
(...skipping 23 matching lines...) Expand all Loading... |
1092 ->OnMessageReceived(msg_deactivate)); | 1282 ->OnMessageReceived(msg_deactivate)); |
1093 | 1283 |
1094 render_thread_->sink().ClearMessages(); | 1284 render_thread_->sink().ClearMessages(); |
1095 SendVisiblePasswordForms(); | 1285 SendVisiblePasswordForms(); |
1096 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( | 1286 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( |
1097 AutofillHostMsg_RecordSavePasswordProgress::ID); | 1287 AutofillHostMsg_RecordSavePasswordProgress::ID); |
1098 EXPECT_FALSE(message); | 1288 EXPECT_FALSE(message); |
1099 } | 1289 } |
1100 | 1290 |
1101 } // namespace autofill | 1291 } // namespace autofill |
OLD | NEW |