OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <vector> | 5 #include <vector> |
6 | 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/path_service.h" |
| 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" |
7 #include "chrome/browser/autofill/autofill_manager.h" | 11 #include "chrome/browser/autofill/autofill_manager.h" |
8 #include "chrome/browser/autofill/form_structure.h" | 12 #include "chrome/browser/autofill/form_structure.h" |
9 #include "chrome/browser/tab_contents/tab_contents.h" | 13 #include "chrome/browser/tab_contents/tab_contents.h" |
10 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/common/chrome_paths.h" |
11 #include "chrome/test/in_process_browser_test.h" | 16 #include "chrome/test/in_process_browser_test.h" |
12 #include "chrome/test/ui_test_utils.h" | 17 #include "chrome/test/ui_test_utils.h" |
13 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
14 | 19 |
| 20 namespace { |
| 21 |
| 22 FilePath GetInputFileDirectory() { |
| 23 FilePath test_data_dir_; |
| 24 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); |
| 25 test_data_dir_ = test_data_dir_.AppendASCII("autofill_heuristics") |
| 26 .AppendASCII("input"); |
| 27 return test_data_dir_; |
| 28 } |
| 29 |
| 30 FilePath GetOutputFileDirectory() { |
| 31 FilePath test_data_dir_; |
| 32 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); |
| 33 test_data_dir_ = test_data_dir_.AppendASCII("autofill_heuristics") |
| 34 .AppendASCII("output"); |
| 35 return test_data_dir_; |
| 36 } |
| 37 |
| 38 // Write |content| to |file|. Returns true on success. |
| 39 bool WriteFile(const FilePath& file, const std::string& content) { |
| 40 int write_size = file_util::WriteFile(file, content.c_str(), |
| 41 content.length()); |
| 42 return write_size == static_cast<int>(content.length()); |
| 43 } |
| 44 |
| 45 // Convert |html| to URL format, and return the converted string. |
| 46 const std::string ConvertToURLFormat(const std::string& html) { |
| 47 return std::string("data:text/html;charset=utf-8,") + html; |
| 48 } |
| 49 |
| 50 // Compare |output_file_source| with |form_string|. Returns true when they are |
| 51 // identical. |
| 52 bool CompareText(const std::string& output_file_source, |
| 53 const std::string& form_string) { |
| 54 std::string output_file = output_file_source; |
| 55 std::string form = form_string; |
| 56 |
| 57 ReplaceSubstringsAfterOffset(&output_file, 0, "\r\n", " "); |
| 58 ReplaceSubstringsAfterOffset(&output_file, 0, "\r", " "); |
| 59 ReplaceSubstringsAfterOffset(&output_file, 0, "\n", " "); |
| 60 ReplaceSubstringsAfterOffset(&form, 0, "\n", " "); |
| 61 |
| 62 return (output_file == form); |
| 63 } |
| 64 |
| 65 } // namespace |
| 66 |
15 // Test class for verifying proper form structure as determined by AutoFill | 67 // Test class for verifying proper form structure as determined by AutoFill |
16 // heuristics. After a test loads HTML content with a call to |NavigateToURL| | 68 // heuristics. A test inputs each form file(e.g. form_[language_code].html), |
17 // the |AutoFillManager| associated with the tab contents is queried for the | 69 // loads its HTML content with a call to |NavigateToURL|, the |AutoFillManager| |
18 // form structures that were loaded and parsed. | 70 // associated with the tab contents is queried for the form structures that |
19 // These form structures are serialized to string form and compared with | 71 // were loaded and parsed. These form structures are serialized to string form. |
20 // expected results. | 72 // If this is the first time test is run, a gold test result file is generated |
| 73 // in output directory, else the form structures are compared against the |
| 74 // existing result file. |
21 class FormStructureBrowserTest : public InProcessBrowserTest { | 75 class FormStructureBrowserTest : public InProcessBrowserTest { |
22 public: | 76 public: |
23 FormStructureBrowserTest() {} | 77 FormStructureBrowserTest() {} |
24 virtual ~FormStructureBrowserTest() {} | 78 virtual ~FormStructureBrowserTest() {} |
25 | 79 |
26 protected: | 80 protected: |
27 // Returns a vector of form structure objects associated with the given | 81 // Returns a vector of form structure objects associated with the given |
28 // |autofill_manager|. | 82 // |autofill_manager|. |
29 const std::vector<FormStructure*>& GetFormStructures( | 83 const std::vector<FormStructure*>& GetFormStructures( |
30 const AutoFillManager& autofill_manager); | 84 const AutoFillManager& autofill_manager); |
(...skipping 13 matching lines...) Expand all Loading... |
44 const AutoFillManager& autofill_manager) { | 98 const AutoFillManager& autofill_manager) { |
45 return autofill_manager.form_structures_.get(); | 99 return autofill_manager.form_structures_.get(); |
46 } | 100 } |
47 | 101 |
48 const std::string FormStructureBrowserTest::FormStructuresToString( | 102 const std::string FormStructureBrowserTest::FormStructuresToString( |
49 const std::vector<FormStructure*>& forms) { | 103 const std::vector<FormStructure*>& forms) { |
50 std::string forms_string; | 104 std::string forms_string; |
51 for (std::vector<FormStructure*>::const_iterator iter = forms.begin(); | 105 for (std::vector<FormStructure*>::const_iterator iter = forms.begin(); |
52 iter != forms.end(); | 106 iter != forms.end(); |
53 ++iter) { | 107 ++iter) { |
54 forms_string += (*iter)->source_url().spec(); | |
55 forms_string += "\n"; | |
56 | 108 |
57 for (std::vector<AutoFillField*>::const_iterator field_iter = | 109 for (std::vector<AutoFillField*>::const_iterator field_iter = |
58 (*iter)->begin(); | 110 (*iter)->begin(); |
59 field_iter != (*iter)->end(); | 111 field_iter != (*iter)->end(); |
60 ++field_iter) { | 112 ++field_iter) { |
61 // The field list is NULL-terminated. Exit loop when at the end. | 113 // The field list is NULL-terminated. Exit loop when at the end. |
62 if (!*field_iter) | 114 if (!*field_iter) |
63 break; | 115 break; |
64 forms_string += AutoFillFieldTypeToString((*field_iter)->type()); | 116 forms_string += AutoFillFieldTypeToString((*field_iter)->type()); |
65 forms_string += "\n"; | 117 forms_string += "\n"; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 return "CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR"; | 207 return "CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR"; |
156 case CREDIT_CARD_TYPE: | 208 case CREDIT_CARD_TYPE: |
157 return "CREDIT_CARD_TYPE"; | 209 return "CREDIT_CARD_TYPE"; |
158 case CREDIT_CARD_VERIFICATION_CODE: | 210 case CREDIT_CARD_VERIFICATION_CODE: |
159 return "CREDIT_CARD_VERIFICATION_CODE"; | 211 return "CREDIT_CARD_VERIFICATION_CODE"; |
160 case COMPANY_NAME: | 212 case COMPANY_NAME: |
161 return "COMPANY_NAME"; | 213 return "COMPANY_NAME"; |
162 default: | 214 default: |
163 NOTREACHED() << "Invalid AutoFillFieldType value."; | 215 NOTREACHED() << "Invalid AutoFillFieldType value."; |
164 } | 216 } |
165 | |
166 return std::string(); | 217 return std::string(); |
167 } | 218 } |
168 | 219 |
169 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, BasicFormStructure) { | 220 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, HTMLFiles) { |
170 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 221 FilePath input_file_path = GetInputFileDirectory(); |
171 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | 222 file_util::FileEnumerator input_file_enumerator(input_file_path, |
172 browser(), GURL("data:text/html;charset=utf-8," | 223 false, file_util::FileEnumerator::FILES, FILE_PATH_LITERAL("*.html")); |
173 "<form action=\"http://www.google.com/\" method=\"POST\">" | |
174 "<label for=\"firstname\">First name:</label>" | |
175 " <input type=\"text\" id=\"firstname\"/><br />" | |
176 "<label for=\"lastname\">Last name:</label>" | |
177 " <input type=\"text\" id=\"lastname\" /><br />" | |
178 "<label for=\"address1\">Address line 1:</label>" | |
179 " <input type=\"text\" id=\"address1\" /><br />" | |
180 "<label for=\"address2\">Address line 2:</label>" | |
181 " <input type=\"text\" id=\"address2\" /><br />" | |
182 "<label for=\"city\">City:</label>" | |
183 " <input type=\"text\" id=\"city\" /><br />" | |
184 "</form>"))); | |
185 | 224 |
186 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), | 225 for (input_file_path = input_file_enumerator.Next(); |
187 VIEW_ID_TAB_CONTAINER)); | 226 !input_file_path.empty(); |
188 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), | 227 input_file_path = input_file_enumerator.Next()) { |
189 VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 228 std::string input_file_source; |
190 | 229 |
191 AutoFillManager* autofill_manager = | 230 ASSERT_TRUE(file_util::ReadFileToString(input_file_path, |
192 browser()->GetSelectedTabContents()->GetAutoFillManager(); | 231 &input_file_source)); |
193 ASSERT_NE(static_cast<AutoFillManager*>(NULL), autofill_manager); | 232 input_file_source = ConvertToURLFormat(input_file_source); |
194 std::vector<FormStructure*> forms = GetFormStructures(*autofill_manager); | |
195 std::string expected("data:text/html;charset=utf-8," | |
196 "<form action=\"http://www.google.com/\"" | |
197 " method=\"POST\">" | |
198 "<label for=\"firstname\">First name:</label>" | |
199 " <input type=\"text\" id=\"firstname\"/><br />" | |
200 "<label for=\"lastname\">Last name:</label>" | |
201 " <input type=\"text\" id=\"lastname\" /><br />" | |
202 "<label for=\"address1\">Address line 1:</label>" | |
203 " <input type=\"text\" id=\"address1\" /><br />" | |
204 "<label for=\"address2\">Address line 2:</label>" | |
205 " <input type=\"text\" id=\"address2\" /><br />" | |
206 "<label for=\"city\">City:</label>" | |
207 " <input type=\"text\" id=\"city\" /><br />" | |
208 "</form>\n" | |
209 "NAME_FIRST\n" | |
210 "NAME_LAST\n" | |
211 "ADDRESS_HOME_LINE1\n" | |
212 "ADDRESS_HOME_LINE2\n" | |
213 "ADDRESS_HOME_CITY\n"); | |
214 | 233 |
215 EXPECT_EQ(expected, FormStructureBrowserTest::FormStructuresToString(forms)); | 234 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); |
| 235 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( |
| 236 browser(), GURL(input_file_source))); |
| 237 |
| 238 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), |
| 239 VIEW_ID_TAB_CONTAINER)); |
| 240 ASSERT_TRUE(ui_test_utils::IsViewFocused( |
| 241 browser(), |
| 242 VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); |
| 243 |
| 244 AutoFillManager* autofill_manager = |
| 245 browser()->GetSelectedTabContents()->GetAutoFillManager(); |
| 246 ASSERT_NE(static_cast<AutoFillManager*>(NULL), autofill_manager); |
| 247 std::vector<FormStructure*> forms = GetFormStructures(*autofill_manager); |
| 248 |
| 249 FilePath output_file_directory = GetOutputFileDirectory(); |
| 250 FilePath output_file_path = output_file_directory.Append( |
| 251 input_file_path.BaseName().StripTrailingSeparators().ReplaceExtension( |
| 252 FILE_PATH_LITERAL(".out"))); |
| 253 |
| 254 std::string output_file_source; |
| 255 if (file_util::ReadFileToString(output_file_path, &output_file_source)) { |
| 256 ASSERT_TRUE(CompareText( |
| 257 output_file_source, |
| 258 FormStructureBrowserTest::FormStructuresToString(forms))); |
| 259 |
| 260 } else { |
| 261 ASSERT_TRUE(WriteFile( |
| 262 output_file_path, |
| 263 FormStructureBrowserTest::FormStructuresToString(forms))); |
| 264 } |
| 265 } |
216 } | 266 } |
OLD | NEW |