OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/string16.h" | 5 #include "base/strings/string16.h" |
6 #include "base/strings/string_util.h" | 6 #include "base/strings/string_util.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "components/autofill/content/renderer/form_autofill_util.h" | 9 #include "components/autofill/content/renderer/form_autofill_util.h" |
10 #include "components/autofill/content/renderer/password_form_conversion_utils.h" | 10 #include "components/autofill/content/renderer/password_form_conversion_utils.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 "<INPUT type=\"submit\" name=\"%s\" value=\"Submit\"/>", | 103 "<INPUT type=\"submit\" name=\"%s\" value=\"Submit\"/>", |
104 name); | 104 name); |
105 } | 105 } |
106 | 106 |
107 // Returns the HTML code for the form containing the fields that have been | 107 // Returns the HTML code for the form containing the fields that have been |
108 // added so far. | 108 // added so far. |
109 std::string ProduceHTML() const { | 109 std::string ProduceHTML() const { |
110 return html_ + "</FORM>"; | 110 return html_ + "</FORM>"; |
111 } | 111 } |
112 | 112 |
| 113 // Appends a field of |type| without name or id attribute at the end of the |
| 114 // form. |
| 115 void AddAnonymousInputField(const char* type) { |
| 116 std::string type_attribute(type ? base::StringPrintf("type=\"%s\"", type) |
| 117 : ""); |
| 118 base::StringAppendF(&html_, "<INPUT %s/>", type_attribute.c_str()); |
| 119 } |
| 120 |
113 private: | 121 private: |
114 std::string html_; | 122 std::string html_; |
115 | 123 |
116 DISALLOW_COPY_AND_ASSIGN(PasswordFormBuilder); | 124 DISALLOW_COPY_AND_ASSIGN(PasswordFormBuilder); |
117 }; | 125 }; |
118 | 126 |
119 // RenderViewTest-based tests crash on Android | 127 // RenderViewTest-based tests crash on Android |
120 // http://crbug.com/187500 | 128 // http://crbug.com/187500 |
121 #if defined(OS_ANDROID) | 129 #if defined(OS_ANDROID) |
122 #define MAYBE_PasswordFormConversionUtilsTest \ | 130 #define MAYBE_PasswordFormConversionUtilsTest \ |
(...skipping 852 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 builder->AddPasswordField("password", "", nullptr); | 983 builder->AddPasswordField("password", "", nullptr); |
976 builder->AddHiddenField("rart", ""); | 984 builder->AddHiddenField("rart", ""); |
977 builder->AddHiddenField("continue", "https://passwords.google.com"); | 985 builder->AddHiddenField("continue", "https://passwords.google.com"); |
978 html = builder->ProduceHTML(); | 986 html = builder->ProduceHTML(); |
979 LoadWebFormFromHTML(html, &form); | 987 LoadWebFormFromHTML(html, &form); |
980 form.getFormControlElements(control_elements); | 988 form.getFormControlElements(control_elements); |
981 EXPECT_FALSE(IsGaiaReauthenticationForm( | 989 EXPECT_FALSE(IsGaiaReauthenticationForm( |
982 GURL("https://google.com"), control_elements)); | 990 GURL("https://google.com"), control_elements)); |
983 } | 991 } |
984 | 992 |
| 993 TEST_F(MAYBE_PasswordFormConversionUtilsTest, |
| 994 IdentifyingFieldsWithoutNameOrIdAttributes) { |
| 995 const char* kEmpty = nullptr; |
| 996 const struct { |
| 997 const char* username_fieldname; |
| 998 const char* password_fieldname; |
| 999 const char* new_password_fieldname; |
| 1000 const char* expected_username_element; |
| 1001 const char* expected_password_element; |
| 1002 const char* expected_new_password_element; |
| 1003 } test_cases[] = { |
| 1004 {"username", "password", "new_password", "username", "password", |
| 1005 "new_password"}, |
| 1006 {"username", "password", kEmpty, "username", "password", |
| 1007 "anonymous_new_password"}, |
| 1008 {"username", kEmpty, kEmpty, "username", "anonymous_password", |
| 1009 "anonymous_new_password"}, |
| 1010 {kEmpty, kEmpty, kEmpty, "anonymous_username", "anonymous_password", |
| 1011 "anonymous_new_password"}, |
| 1012 }; |
| 1013 |
| 1014 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
| 1015 SCOPED_TRACE(testing::Message() |
| 1016 << "Iteration " << i << ", expected_username " |
| 1017 << test_cases[i].expected_username_element |
| 1018 << ", expected_password" |
| 1019 << test_cases[i].expected_password_element |
| 1020 << ", expected_new_password " |
| 1021 << test_cases[i].expected_new_password_element); |
| 1022 |
| 1023 PasswordFormBuilder builder(kTestFormActionURL); |
| 1024 if (test_cases[i].username_fieldname == kEmpty) { |
| 1025 builder.AddAnonymousInputField("text"); |
| 1026 } else { |
| 1027 builder.AddTextField(test_cases[i].username_fieldname, "", kEmpty); |
| 1028 } |
| 1029 |
| 1030 if (test_cases[i].password_fieldname == kEmpty) { |
| 1031 builder.AddAnonymousInputField("password"); |
| 1032 } else { |
| 1033 builder.AddPasswordField(test_cases[i].password_fieldname, "", kEmpty); |
| 1034 } |
| 1035 |
| 1036 if (test_cases[i].new_password_fieldname == kEmpty) { |
| 1037 builder.AddAnonymousInputField("password"); |
| 1038 } else { |
| 1039 builder.AddPasswordField(test_cases[i].new_password_fieldname, "", |
| 1040 kEmpty); |
| 1041 } |
| 1042 std::string html = builder.ProduceHTML(); |
| 1043 |
| 1044 scoped_ptr<PasswordForm> password_form; |
| 1045 LoadHTMLAndConvertForm(html, &password_form, nullptr); |
| 1046 EXPECT_TRUE(password_form); |
| 1047 |
| 1048 EXPECT_EQ(base::UTF8ToUTF16(test_cases[i].expected_username_element), |
| 1049 password_form->username_element); |
| 1050 EXPECT_EQ(base::UTF8ToUTF16(test_cases[i].expected_password_element), |
| 1051 password_form->password_element); |
| 1052 EXPECT_EQ(base::UTF8ToUTF16(test_cases[i].expected_new_password_element), |
| 1053 password_form->new_password_element); |
| 1054 } |
| 1055 } |
| 1056 |
985 } // namespace autofill | 1057 } // namespace autofill |
OLD | NEW |