Chromium Code Reviews| 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/utf_string_conversions.h" | |
| 7 #include "chrome/browser/autofill/autofill_manager.h" | 10 #include "chrome/browser/autofill/autofill_manager.h" |
| 8 #include "chrome/browser/autofill/form_structure.h" | 11 #include "chrome/browser/autofill/form_structure.h" |
| 9 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 10 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/common/chrome_paths.h" | |
| 11 #include "chrome/test/in_process_browser_test.h" | 15 #include "chrome/test/in_process_browser_test.h" |
| 12 #include "chrome/test/ui_test_utils.h" | 16 #include "chrome/test/ui_test_utils.h" |
| 13 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 14 | 18 |
| 15 // Test class for verifying proper form structure as determined by AutoFill | 19 // Test class for verifying proper international form structure as determined |
| 16 // heuristics. After a test loads HTML content with a call to |NavigateToURL| | 20 // by AutoFill heuristics. A test inputs each i18n form file |
| 17 // the |AutoFillManager| associated with the tab contents is queried for the | 21 // (form_[language_code].html) from input directory, then loads its HTML |
| 18 // form structures that were loaded and parsed. | 22 // content with a call to |NavigateToURL|, the |AutoFillManager| associated |
| 19 // These form structures are serialized to string form and compared with | 23 // with the tab contents is queried for the form structures that were loaded |
| 20 // expected results. | 24 // and parsed. These form structures are serialized to string form. If this is |
| 25 // the first time test is run, a gold test result file is generated in output | |
| 26 // directory, else the form structures are compared again the existing gold | |
| 27 // result file. | |
| 21 class FormStructureBrowserTest : public InProcessBrowserTest { | 28 class FormStructureBrowserTest : public InProcessBrowserTest { |
| 22 public: | 29 public: |
| 23 FormStructureBrowserTest() {} | 30 FormStructureBrowserTest() {} |
| 24 virtual ~FormStructureBrowserTest() {} | 31 virtual ~FormStructureBrowserTest() {} |
| 25 | 32 |
| 26 protected: | 33 protected: |
| 27 // Returns a vector of form structure objects associated with the given | 34 // Returns a vector of form structure objects associated with the given |
| 28 // |autofill_manager|. | 35 // |autofill_manager|. |
| 29 const std::vector<FormStructure*>& GetFormStructures( | 36 const std::vector<FormStructure*>& GetFormStructures( |
| 30 const AutoFillManager& autofill_manager); | 37 const AutoFillManager& autofill_manager); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 44 const AutoFillManager& autofill_manager) { | 51 const AutoFillManager& autofill_manager) { |
| 45 return autofill_manager.form_structures_.get(); | 52 return autofill_manager.form_structures_.get(); |
| 46 } | 53 } |
| 47 | 54 |
| 48 const std::string FormStructureBrowserTest::FormStructuresToString( | 55 const std::string FormStructureBrowserTest::FormStructuresToString( |
| 49 const std::vector<FormStructure*>& forms) { | 56 const std::vector<FormStructure*>& forms) { |
| 50 std::string forms_string; | 57 std::string forms_string; |
| 51 for (std::vector<FormStructure*>::const_iterator iter = forms.begin(); | 58 for (std::vector<FormStructure*>::const_iterator iter = forms.begin(); |
| 52 iter != forms.end(); | 59 iter != forms.end(); |
| 53 ++iter) { | 60 ++iter) { |
| 54 forms_string += (*iter)->source_url().spec(); | 61 forms_string += (*iter)->source_url().spec(); |
|
dhollowa
2010/12/13 22:11:30
Let's remove these two lines. We don't need to co
vivianz
2010/12/14 22:25:14
On 2010/12/13 22:11:30, dhollowa wrote:
make sens
| |
| 55 forms_string += "\n"; | 62 forms_string += "\n"; |
| 56 | 63 |
| 57 for (std::vector<AutoFillField*>::const_iterator field_iter = | 64 for (std::vector<AutoFillField*>::const_iterator field_iter = |
| 58 (*iter)->begin(); | 65 (*iter)->begin(); |
| 59 field_iter != (*iter)->end(); | 66 field_iter != (*iter)->end(); |
| 60 ++field_iter) { | 67 ++field_iter) { |
| 61 // The field list is NULL-terminated. Exit loop when at the end. | 68 // The field list is NULL-terminated. Exit loop when at the end. |
| 62 if (!*field_iter) | 69 if (!*field_iter) |
| 63 break; | 70 break; |
| 64 forms_string += AutoFillFieldTypeToString((*field_iter)->type()); | 71 forms_string += AutoFillFieldTypeToString((*field_iter)->type()); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 return "CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR"; | 162 return "CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR"; |
| 156 case CREDIT_CARD_TYPE: | 163 case CREDIT_CARD_TYPE: |
| 157 return "CREDIT_CARD_TYPE"; | 164 return "CREDIT_CARD_TYPE"; |
| 158 case CREDIT_CARD_VERIFICATION_CODE: | 165 case CREDIT_CARD_VERIFICATION_CODE: |
| 159 return "CREDIT_CARD_VERIFICATION_CODE"; | 166 return "CREDIT_CARD_VERIFICATION_CODE"; |
| 160 case COMPANY_NAME: | 167 case COMPANY_NAME: |
| 161 return "COMPANY_NAME"; | 168 return "COMPANY_NAME"; |
| 162 default: | 169 default: |
| 163 NOTREACHED() << "Invalid AutoFillFieldType value."; | 170 NOTREACHED() << "Invalid AutoFillFieldType value."; |
| 164 } | 171 } |
| 165 | |
| 166 return std::string(); | 172 return std::string(); |
| 167 } | 173 } |
| 168 | 174 |
| 169 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, BasicFormStructure) { | 175 FilePath GetInputFileDirectory() { |
|
dhollowa
2010/12/13 22:11:30
Please move this to top of the file and wrap it in
vivianz
2010/12/14 22:25:14
On 2010/12/13 22:11:30, dhollowa wrote:
Fixed
| |
| 170 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | 176 FilePath test_data_dir_; |
| 171 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | 177 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); |
| 172 browser(), GURL("data:text/html;charset=utf-8," | 178 test_data_dir_ = test_data_dir_.AppendASCII("autofill_heuristics") |
| 173 "<form action=\"http://www.google.com/\" method=\"POST\">" | 179 .AppendASCII("input"); |
| 174 "<label for=\"firstname\">First name:</label>" | 180 return test_data_dir_; |
| 175 " <input type=\"text\" id=\"firstname\"/><br />" | 181 } |
| 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 | 182 |
| 186 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), | 183 FilePath GetOutputFileDirectory() { |
| 187 VIEW_ID_TAB_CONTAINER)); | 184 FilePath test_data_dir_; |
| 188 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), | 185 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); |
| 189 VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | 186 test_data_dir_ = test_data_dir_.AppendASCII("autofill_heuristics") |
| 187 .AppendASCII("output"); | |
| 188 return test_data_dir_; | |
| 189 } | |
| 190 | 190 |
| 191 AutoFillManager* autofill_manager = | 191 // Write |outputcontent| to |outputfile|. Returns true on success. |
| 192 browser()->GetSelectedTabContents()->GetAutoFillManager(); | 192 bool WriteFile(const FilePath& outputfile, |
| 193 ASSERT_NE(static_cast<AutoFillManager*>(NULL), autofill_manager); | 193 const std::string& outputcontent) { |
| 194 std::vector<FormStructure*> forms = GetFormStructures(*autofill_manager); | 194 int write_size = file_util::WriteFile(outputfile, outputcontent.c_str(), |
| 195 std::string expected("data:text/html;charset=utf-8," | 195 outputcontent.length()); |
| 196 "<form action=\"http://www.google.com/\"" | 196 return write_size == static_cast<int>(outputcontent.length()); |
| 197 " method=\"POST\">" | 197 } |
| 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 | 198 |
| 215 EXPECT_EQ(expected, FormStructureBrowserTest::FormStructuresToString(forms)); | 199 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, I18NFormStructure) { |
|
dhollowa
2010/12/13 22:11:30
Let's rename from I18NFormStructure to HTMLFiles.
vivianz
2010/12/14 22:25:14
On 2010/12/13 22:11:30, dhollowa wrote:
Done.
| |
| 200 FilePath input_file_path = GetInputFileDirectory(); | |
| 201 file_util::FileEnumerator input_file_enumerator(input_file_path, | |
| 202 false, file_util::FileEnumerator::FILES); | |
| 203 | |
| 204 for (input_file_path = input_file_enumerator.Next(); | |
| 205 !input_file_path.empty(); | |
| 206 input_file_path = input_file_enumerator.Next()) { | |
| 207 std::string input_file_source; | |
| 208 ASSERT_TRUE(file_util::ReadFileToString(input_file_path, | |
| 209 &input_file_source)); | |
| 210 | |
| 211 ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser())); | |
| 212 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL( | |
| 213 browser(), GURL(UTF8ToUTF16(input_file_source)))); | |
| 214 | |
| 215 ASSERT_NO_FATAL_FAILURE(ui_test_utils::ClickOnView(browser(), | |
| 216 VIEW_ID_TAB_CONTAINER)); | |
| 217 ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), | |
| 218 VIEW_ID_TAB_CONTAINER_FOCUS_VIEW)); | |
| 219 | |
| 220 AutoFillManager* autofill_manager = | |
| 221 browser()->GetSelectedTabContents()->GetAutoFillManager(); | |
| 222 ASSERT_NE(static_cast<AutoFillManager*>(NULL), autofill_manager); | |
| 223 std::vector<FormStructure*> forms = GetFormStructures(*autofill_manager); | |
| 224 | |
| 225 FilePath output_file_directory = GetOutputFileDirectory(); | |
| 226 FilePath output_file_path = output_file_directory.Append(input_file_path | |
| 227 .BaseName().StripTrailingSeparators().ReplaceExtension(L".out")); | |
|
dhollowa
2010/12/13 22:11:30
The "L" prefix is not cross-platform. You can use
vivianz
2010/12/14 22:25:14
On 2010/12/13 22:11:30, dhollowa wrote:
Done.
| |
| 228 | |
| 229 std::string output_file_source; | |
| 230 if (file_util::ReadFileToString(output_file_path, &output_file_source)) { | |
| 231 EXPECT_EQ(output_file_source, | |
| 232 FormStructureBrowserTest::FormStructuresToString(forms)); | |
| 233 | |
| 234 } else { | |
| 235 ASSERT_TRUE(WriteFile(output_file_path, | |
| 236 FormStructureBrowserTest::FormStructuresToString(forms))); | |
| 237 } | |
| 238 } | |
| 216 } | 239 } |
| OLD | NEW |