Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <vector> |
| 6 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 8 | #include "base/path_service.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
| 10 | #include "base/strings/utf_string_conversions.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 12 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 13 | #include "chrome/common/chrome_paths.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 14 | #include "chrome/test/base/in_process_browser_test.h" |
| 15 | #include "chrome/test/base/ui_test_utils.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 16 | #include "components/autofill/content/browser/autofill_driver_impl.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 17 | #include "components/autofill/core/browser/autofill_manager.h" |
| 18 | #include "components/autofill/core/browser/data_driven_test.h" |
| 19 | #include "components/autofill/core/browser/form_structure.h" |
| 20 | #include "url/gurl.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 21 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 22 | namespace autofill { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 25 | const base::FilePath::CharType kTestName[] = FILE_PATH_LITERAL("heuristics"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 26 | |
| 27 | // Convert the |html| snippet to a data URI. |
| 28 | GURL HTMLToDataURI(const std::string& html) { |
| 29 | return GURL(std::string("data:text/html;charset=utf-8,") + html); |
| 30 | } |
| 31 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 32 | const base::FilePath& GetTestDataDir() { |
| 33 | CR_DEFINE_STATIC_LOCAL(base::FilePath, dir, ()); |
| 34 | if (dir.empty()) |
| 35 | PathService::Get(chrome::DIR_TEST_DATA, &dir); |
| 36 | return dir; |
| 37 | } |
| 38 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 39 | } // namespace |
| 40 | |
| 41 | // A data-driven test for verifying Autofill heuristics. Each input is an HTML |
| 42 | // file that contains one or more forms. The corresponding output file lists the |
| 43 | // heuristically detected type for eachfield. |
| 44 | class FormStructureBrowserTest : public InProcessBrowserTest, |
| 45 | public DataDrivenTest { |
| 46 | protected: |
| 47 | FormStructureBrowserTest(); |
| 48 | virtual ~FormStructureBrowserTest(); |
| 49 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 50 | // DataDrivenTest: |
| 51 | virtual void GenerateResults(const std::string& input, |
| 52 | std::string* output) OVERRIDE; |
| 53 | |
| 54 | // Serializes the given |forms| into a string. |
| 55 | std::string FormStructuresToString(const std::vector<FormStructure*>& forms); |
| 56 | |
| 57 | private: |
| 58 | DISALLOW_COPY_AND_ASSIGN(FormStructureBrowserTest); |
| 59 | }; |
| 60 | |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 61 | FormStructureBrowserTest::FormStructureBrowserTest() |
| 62 | : DataDrivenTest(GetTestDataDir()) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | FormStructureBrowserTest::~FormStructureBrowserTest() { |
| 66 | } |
| 67 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 68 | void FormStructureBrowserTest::GenerateResults(const std::string& input, |
| 69 | std::string* output) { |
| 70 | ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(), |
| 71 | HTMLToDataURI(input))); |
| 72 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 73 | AutofillDriverImpl* autofill_driver = AutofillDriverImpl::FromWebContents( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 74 | browser()->tab_strip_model()->GetActiveWebContents()); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 75 | ASSERT_NE(static_cast<AutofillDriverImpl*>(NULL), autofill_driver); |
| 76 | AutofillManager* autofill_manager = autofill_driver->autofill_manager(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 77 | ASSERT_NE(static_cast<AutofillManager*>(NULL), autofill_manager); |
| 78 | std::vector<FormStructure*> forms = autofill_manager->form_structures_.get(); |
| 79 | *output = FormStructureBrowserTest::FormStructuresToString(forms); |
| 80 | } |
| 81 | |
| 82 | std::string FormStructureBrowserTest::FormStructuresToString( |
| 83 | const std::vector<FormStructure*>& forms) { |
| 84 | std::string forms_string; |
| 85 | for (std::vector<FormStructure*>::const_iterator iter = forms.begin(); |
| 86 | iter != forms.end(); |
| 87 | ++iter) { |
| 88 | |
| 89 | for (std::vector<AutofillField*>::const_iterator field_iter = |
| 90 | (*iter)->begin(); |
| 91 | field_iter != (*iter)->end(); |
| 92 | ++field_iter) { |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 93 | forms_string += (*field_iter)->Type().ToString(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 94 | forms_string += " | " + UTF16ToUTF8((*field_iter)->name); |
| 95 | forms_string += " | " + UTF16ToUTF8((*field_iter)->label); |
| 96 | forms_string += " | " + UTF16ToUTF8((*field_iter)->value); |
| 97 | forms_string += "\n"; |
| 98 | } |
| 99 | } |
| 100 | return forms_string; |
| 101 | } |
| 102 | |
| 103 | // Heuristics tests timeout on Windows. See http://crbug.com/85276 |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 104 | // Also on ChromeOS/Aura. See crbug.com/173621 |
| 105 | #if defined(OS_WIN) || defined(USE_AURA) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 106 | #define MAYBE_DataDrivenHeuristics(n) DISABLED_DataDrivenHeuristics##n |
| 107 | #else |
| 108 | #define MAYBE_DataDrivenHeuristics(n) DataDrivenHeuristics##n |
| 109 | #endif |
| 110 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, DataDrivenHeuristics00) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 111 | const base::FilePath::CharType kFileNamePattern[] = |
| 112 | FILE_PATH_LITERAL("00_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 113 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 114 | GetOutputDirectory(kTestName), |
| 115 | kFileNamePattern); |
| 116 | } |
| 117 | |
| 118 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, DataDrivenHeuristics01) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 119 | const base::FilePath::CharType kFileNamePattern[] = |
| 120 | FILE_PATH_LITERAL("01_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 121 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 122 | GetOutputDirectory(kTestName), |
| 123 | kFileNamePattern); |
| 124 | } |
| 125 | |
| 126 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 127 | MAYBE_DataDrivenHeuristics(02)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 128 | const base::FilePath::CharType kFileNamePattern[] = |
| 129 | FILE_PATH_LITERAL("02_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 130 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 131 | GetOutputDirectory(kTestName), |
| 132 | kFileNamePattern); |
| 133 | } |
| 134 | |
| 135 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 136 | MAYBE_DataDrivenHeuristics(03)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 137 | const base::FilePath::CharType kFileNamePattern[] = |
| 138 | FILE_PATH_LITERAL("03_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 139 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 140 | GetOutputDirectory(kTestName), |
| 141 | kFileNamePattern); |
| 142 | } |
| 143 | |
| 144 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 145 | MAYBE_DataDrivenHeuristics(04)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 146 | const base::FilePath::CharType kFileNamePattern[] = |
| 147 | FILE_PATH_LITERAL("04_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 148 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 149 | GetOutputDirectory(kTestName), |
| 150 | kFileNamePattern); |
| 151 | } |
| 152 | |
| 153 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 154 | MAYBE_DataDrivenHeuristics(05)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 155 | const base::FilePath::CharType kFileNamePattern[] = |
| 156 | FILE_PATH_LITERAL("05_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 157 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 158 | GetOutputDirectory(kTestName), |
| 159 | kFileNamePattern); |
| 160 | } |
| 161 | |
| 162 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 163 | MAYBE_DataDrivenHeuristics(06)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 164 | const base::FilePath::CharType kFileNamePattern[] = |
| 165 | FILE_PATH_LITERAL("06_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 166 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 167 | GetOutputDirectory(kTestName), |
| 168 | kFileNamePattern); |
| 169 | } |
| 170 | |
| 171 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 172 | MAYBE_DataDrivenHeuristics(07)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 173 | const base::FilePath::CharType kFileNamePattern[] = |
| 174 | FILE_PATH_LITERAL("07_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 175 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 176 | GetOutputDirectory(kTestName), |
| 177 | kFileNamePattern); |
| 178 | } |
| 179 | |
| 180 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 181 | MAYBE_DataDrivenHeuristics(08)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 182 | const base::FilePath::CharType kFileNamePattern[] = |
| 183 | FILE_PATH_LITERAL("08_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 184 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 185 | GetOutputDirectory(kTestName), |
| 186 | kFileNamePattern); |
| 187 | } |
| 188 | |
| 189 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 190 | MAYBE_DataDrivenHeuristics(09)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 191 | const base::FilePath::CharType kFileNamePattern[] = |
| 192 | FILE_PATH_LITERAL("09_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 193 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 194 | GetOutputDirectory(kTestName), |
| 195 | kFileNamePattern); |
| 196 | } |
| 197 | |
| 198 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 199 | MAYBE_DataDrivenHeuristics(10)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 200 | const base::FilePath::CharType kFileNamePattern[] = |
| 201 | FILE_PATH_LITERAL("10_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 202 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 203 | GetOutputDirectory(kTestName), |
| 204 | kFileNamePattern); |
| 205 | } |
| 206 | |
| 207 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 208 | MAYBE_DataDrivenHeuristics(11)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 209 | const base::FilePath::CharType kFileNamePattern[] = |
| 210 | FILE_PATH_LITERAL("11_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 211 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 212 | GetOutputDirectory(kTestName), |
| 213 | kFileNamePattern); |
| 214 | } |
| 215 | |
| 216 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 217 | MAYBE_DataDrivenHeuristics(12)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 218 | const base::FilePath::CharType kFileNamePattern[] = |
| 219 | FILE_PATH_LITERAL("12_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 220 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 221 | GetOutputDirectory(kTestName), |
| 222 | kFileNamePattern); |
| 223 | } |
| 224 | |
| 225 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 226 | MAYBE_DataDrivenHeuristics(13)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 227 | const base::FilePath::CharType kFileNamePattern[] = |
| 228 | FILE_PATH_LITERAL("13_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 229 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 230 | GetOutputDirectory(kTestName), |
| 231 | kFileNamePattern); |
| 232 | } |
| 233 | |
| 234 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 235 | MAYBE_DataDrivenHeuristics(14)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 236 | const base::FilePath::CharType kFileNamePattern[] = |
| 237 | FILE_PATH_LITERAL("14_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 238 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 239 | GetOutputDirectory(kTestName), |
| 240 | kFileNamePattern); |
| 241 | } |
| 242 | |
| 243 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 244 | MAYBE_DataDrivenHeuristics(15)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 245 | const base::FilePath::CharType kFileNamePattern[] = |
| 246 | FILE_PATH_LITERAL("15_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 247 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 248 | GetOutputDirectory(kTestName), |
| 249 | kFileNamePattern); |
| 250 | } |
| 251 | |
| 252 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 253 | MAYBE_DataDrivenHeuristics(16)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 254 | const base::FilePath::CharType kFileNamePattern[] = |
| 255 | FILE_PATH_LITERAL("16_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 256 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 257 | GetOutputDirectory(kTestName), |
| 258 | kFileNamePattern); |
| 259 | } |
| 260 | |
| 261 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 262 | MAYBE_DataDrivenHeuristics(17)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 263 | const base::FilePath::CharType kFileNamePattern[] = |
| 264 | FILE_PATH_LITERAL("17_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 265 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 266 | GetOutputDirectory(kTestName), |
| 267 | kFileNamePattern); |
| 268 | } |
| 269 | |
| 270 | IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, |
| 271 | MAYBE_DataDrivenHeuristics(20)) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 272 | const base::FilePath::CharType kFileNamePattern[] = |
| 273 | FILE_PATH_LITERAL("20_*.html"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 274 | RunDataDrivenTest(GetInputDirectory(kTestName), |
| 275 | GetOutputDirectory(kTestName), |
| 276 | kFileNamePattern); |
| 277 | } |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 278 | |
| 279 | } // namespace autofill |