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"); | |
Ilya Sherman
2010/12/16 00:24:03
nit: this line is still indented two extra spaces
vivianz
2010/12/16 02:00:14
On 2010/12/16 00:24:03, Ilya Sherman wrote:
Done.
| |
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()); | |
Ilya Sherman
2010/12/16 00:24:03
nit: this line is still indented two extra spaces
vivianz
2010/12/16 02:00:14
On 2010/12/16 00:24:03, Ilya Sherman wrote:
Done.
| |
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 } // namespace | |
51 | |
15 // Test class for verifying proper form structure as determined by AutoFill | 52 // Test class for verifying proper form structure as determined by AutoFill |
16 // heuristics. After a test loads HTML content with a call to |NavigateToURL| | 53 // 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 | 54 // loads its HTMLcontent with a call to |NavigateToURL|, the |AutoFillManager| |
18 // form structures that were loaded and parsed. | 55 // associated with the tab contents is queried for the form structures that |
19 // These form structures are serialized to string form and compared with | 56 // were loaded and parsed. These form structures are serialized to string form. |
20 // expected results. | 57 // If this is the first time test is run, a gold test result file is generated |
58 // in output directory, else the form structures are compared again the | |
59 // existing result file. | |
21 class FormStructureBrowserTest : public InProcessBrowserTest { | 60 class FormStructureBrowserTest : public InProcessBrowserTest { |
22 public: | 61 public: |
23 FormStructureBrowserTest() {} | 62 FormStructureBrowserTest() {} |
24 virtual ~FormStructureBrowserTest() {} | 63 virtual ~FormStructureBrowserTest() {} |
25 | 64 |
26 protected: | 65 protected: |
27 // Returns a vector of form structure objects associated with the given | 66 // Returns a vector of form structure objects associated with the given |
28 // |autofill_manager|. | 67 // |autofill_manager|. |
29 const std::vector<FormStructure*>& GetFormStructures( | 68 const std::vector<FormStructure*>& GetFormStructures( |
30 const AutoFillManager& autofill_manager); | 69 const AutoFillManager& autofill_manager); |
(...skipping 13 matching lines...) Expand all Loading... | |
44 const AutoFillManager& autofill_manager) { | 83 const AutoFillManager& autofill_manager) { |
45 return autofill_manager.form_structures_.get(); | 84 return autofill_manager.form_structures_.get(); |
46 } | 85 } |
47 | 86 |
48 const std::string FormStructureBrowserTest::FormStructuresToString( | 87 const std::string FormStructureBrowserTest::FormStructuresToString( |
49 const std::vector<FormStructure*>& forms) { | 88 const std::vector<FormStructure*>& forms) { |
50 std::string forms_string; | 89 std::string forms_string; |
51 for (std::vector<FormStructure*>::const_iterator iter = forms.begin(); | 90 for (std::vector<FormStructure*>::const_iterator iter = forms.begin(); |
52 iter != forms.end(); | 91 iter != forms.end(); |
53 ++iter) { | 92 ++iter) { |
54 forms_string += (*iter)->source_url().spec(); | |
55 forms_string += "\n"; | |
56 | 93 |
57 for (std::vector<AutoFillField*>::const_iterator field_iter = | 94 for (std::vector<AutoFillField*>::const_iterator field_iter = |
58 (*iter)->begin(); | 95 (*iter)->begin(); |
59 field_iter != (*iter)->end(); | 96 field_iter != (*iter)->end(); |
60 ++field_iter) { | 97 ++field_iter) { |
61 // The field list is NULL-terminated. Exit loop when at the end. | 98 // The field list is NULL-terminated. Exit loop when at the end. |
62 if (!*field_iter) | 99 if (!*field_iter) |
63 break; | 100 break; |
64 forms_string += AutoFillFieldTypeToString((*field_iter)->type()); | 101 forms_string += AutoFillFieldTypeToString((*field_iter)->type()); |
65 forms_string += "\n"; | 102 forms_string += " "; |
GeorgeY
2010/12/16 00:46:42
Isn't it easier to do
svn pset svn:eol-style LF ch
dhollowa
2010/12/16 01:10:22
Yes, this should return to "\n". And we should ad
vivianz
2010/12/16 02:00:14
On 2010/12/16 00:46:42, GeorgeY wrote:
ok, sounds
vivianz
2010/12/16 02:00:14
On 2010/12/16 01:10:22, dhollowa wrote:
let's try
dhollowa
2010/12/16 16:35:53
Looks like the svn props didn't work. I'm still s
| |
66 } | 103 } |
67 } | 104 } |
68 return forms_string; | 105 return forms_string; |
69 } | 106 } |
70 | 107 |
71 const std::string FormStructureBrowserTest::AutoFillFieldTypeToString( | 108 const std::string FormStructureBrowserTest::AutoFillFieldTypeToString( |
72 AutoFillFieldType type) { | 109 AutoFillFieldType type) { |
73 switch (type) { | 110 switch (type) { |
74 case NO_SERVER_DATA: | 111 case NO_SERVER_DATA: |
75 return "NO_SERVER_DATA"; | 112 return "NO_SERVER_DATA"; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 return "CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR"; | 192 return "CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR"; |
156 case CREDIT_CARD_TYPE: | 193 case CREDIT_CARD_TYPE: |
157 return "CREDIT_CARD_TYPE"; | 194 return "CREDIT_CARD_TYPE"; |
158 case CREDIT_CARD_VERIFICATION_CODE: | 195 case CREDIT_CARD_VERIFICATION_CODE: |
159 return "CREDIT_CARD_VERIFICATION_CODE"; | 196 return "CREDIT_CARD_VERIFICATION_CODE"; |
160 case COMPANY_NAME: | 197 case COMPANY_NAME: |
161 return "COMPANY_NAME"; | 198 return "COMPANY_NAME"; |
162 default: | 199 default: |
163 NOTREACHED() << "Invalid AutoFillFieldType value."; | 200 NOTREACHED() << "Invalid AutoFillFieldType value."; |
164 } | 201 } |
165 | |
166 return std::string(); | 202 return std::string(); |
167 } | 203 } |
168 | 204 |
169 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, BasicFormStructure) { | 205 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, HTMLFiles) { |
170 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 206 FilePath input_file_path = GetInputFileDirectory(); |
171 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | 207 file_util::FileEnumerator input_file_enumerator(input_file_path, |
172 browser(), GURL("data:text/html;charset=utf-8," | 208 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 | 209 |
186 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), | 210 for (input_file_path = input_file_enumerator.Next(); |
187 VIEW_ID_TAB_CONTAINER)); | 211 !input_file_path.empty(); |
188 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), | 212 input_file_path = input_file_enumerator.Next()) { |
189 VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | |
190 | 213 |
191 AutoFillManager* autofill_manager = | 214 std::string input_file_source; |
192 browser()->GetSelectedTabContents()->GetAutoFillManager(); | |
193 ASSERT_NE(static_cast<AutoFillManager*>(NULL), autofill_manager); | |
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 | 215 |
215 EXPECT_EQ(expected, FormStructureBrowserTest::FormStructuresToString(forms)); | 216 ASSERT_TRUE(file_util::ReadFileToString(input_file_path, |
217 &input_file_source)); | |
218 input_file_source = ConvertToURLFormat(input_file_source); | |
219 | |
220 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
221 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | |
222 browser(), GURL(UTF8ToUTF16(input_file_source)))); | |
223 | |
224 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), | |
225 VIEW_ID_TAB_CONTAINER)); | |
226 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), | |
227 VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | |
Ilya Sherman
2010/12/16 00:24:03
nit: please either indent VIEW_ID_TAB... to align
vivianz
2010/12/16 02:00:14
On 2010/12/16 00:24:03, Ilya Sherman wrote:
Done.
| |
228 | |
229 AutoFillManager* autofill_manager = | |
230 browser()->GetSelectedTabContents()->GetAutoFillManager(); | |
231 ASSERT_NE(static_cast<AutoFillManager*>(NULL), autofill_manager); | |
232 std::vector<FormStructure*> forms = GetFormStructures(*autofill_manager); | |
233 | |
234 FilePath output_file_directory = GetOutputFileDirectory(); | |
235 FilePath output_file_path = output_file_directory.Append( | |
236 input_file_path.BaseName().StripTrailingSeparators().ReplaceExtension( | |
237 FILE_PATH_LITERAL(".out"))); | |
238 | |
239 std::string output_file_source; | |
240 if (file_util::ReadFileToString(output_file_path, &output_file_source)) { | |
241 EXPECT_EQ(output_file_source, | |
dhollowa
2010/12/16 01:10:22
This will change to:
EXPECT_TRUE(CompareText(..
| |
242 FormStructureBrowserTest::FormStructuresToString(forms)); | |
243 | |
244 } else { | |
245 ASSERT_TRUE(WriteFile( | |
246 output_file_path, | |
247 FormStructureBrowserTest::FormStructuresToString(forms))); | |
248 } | |
249 } | |
216 } | 250 } |
OLD | NEW |