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 1038 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1049 | 1049 |
1050 // Try accepting a suggestion with a password different from the one that was | 1050 // Try accepting a suggestion with a 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_->AcceptSuggestion( |
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 |SelectSuggestion| properly previews the username and password. |
| 1060 TEST_F(PasswordAutofillAgentTest, SelectSuggestion) { |
| 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_->SelectSuggestion( |
| 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_->SelectSuggestion( |
| 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 lenght is 0 in this case. |
| 1094 CheckUsernameSelection(0, username_length); |
| 1095 |
| 1096 // Try selecting a suggestion with a password different from the one that was |
| 1097 // initially sent to the renderer. |
| 1098 EXPECT_TRUE(password_autofill_->SelectSuggestion( |
| 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 |SelectSuggestion| properly sets the username selection range. |
| 1113 TEST_F(PasswordAutofillAgentTest, SelectSuggestionSelectionRange) { |
| 1114 username_element_.setValue(WebString::fromUTF8("ali")); |
| 1115 |
| 1116 // Simulate the browser sending the login info, but set |wait_for_username| |
| 1117 // to prevent the form from being immediately filled. |
| 1118 fill_data_.wait_for_username = true; |
| 1119 SimulateOnFillPasswordForm(fill_data_); |
| 1120 |
| 1121 EXPECT_TRUE(password_autofill_->SelectSuggestion( |
| 1122 username_element_, kAliceUsername, kAlicePassword)); |
| 1123 EXPECT_EQ( |
| 1124 kAliceUsername, |
| 1125 static_cast<std::string>(username_element_.suggestedValue().utf8())); |
| 1126 EXPECT_TRUE(username_element_.isAutofilled()); |
| 1127 EXPECT_EQ( |
| 1128 kAlicePassword, |
| 1129 static_cast<std::string>(password_element_.suggestedValue().utf8())); |
| 1130 EXPECT_TRUE(password_element_.isAutofilled()); |
| 1131 int username_length = strlen(kAliceUsername); |
| 1132 // Selection range starts from the end of matching characters between |
| 1133 // username_element value and suggestedValue. |
| 1134 CheckUsernameSelection(3, username_length); |
| 1135 } |
| 1136 |
1059 // Tests that logging is off by default. | 1137 // Tests that logging is off by default. |
1060 TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_NoMessage) { | 1138 TEST_F(PasswordAutofillAgentTest, OnChangeLoggingState_NoMessage) { |
1061 render_thread_->sink().ClearMessages(); | 1139 render_thread_->sink().ClearMessages(); |
1062 SendVisiblePasswordForms(); | 1140 SendVisiblePasswordForms(); |
1063 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( | 1141 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( |
1064 AutofillHostMsg_RecordSavePasswordProgress::ID); | 1142 AutofillHostMsg_RecordSavePasswordProgress::ID); |
1065 EXPECT_FALSE(message); | 1143 EXPECT_FALSE(message); |
1066 } | 1144 } |
1067 | 1145 |
1068 // Test that logging can be turned on by a message. | 1146 // Test that logging can be turned on by a message. |
(...skipping 23 matching lines...) Expand all Loading... |
1092 ->OnMessageReceived(msg_deactivate)); | 1170 ->OnMessageReceived(msg_deactivate)); |
1093 | 1171 |
1094 render_thread_->sink().ClearMessages(); | 1172 render_thread_->sink().ClearMessages(); |
1095 SendVisiblePasswordForms(); | 1173 SendVisiblePasswordForms(); |
1096 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( | 1174 const IPC::Message* message = render_thread_->sink().GetFirstMessageMatching( |
1097 AutofillHostMsg_RecordSavePasswordProgress::ID); | 1175 AutofillHostMsg_RecordSavePasswordProgress::ID); |
1098 EXPECT_FALSE(message); | 1176 EXPECT_FALSE(message); |
1099 } | 1177 } |
1100 | 1178 |
1101 } // namespace autofill | 1179 } // namespace autofill |
OLD | NEW |