blob: c497275511bbe3a1e94ac97d863e33bb0841285c [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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)2a99a7e2013-03-28 15:31:22 +00007#include "base/files/file_path.h"
Ben Murdochca12bfa2013-07-23 11:17:05 +01008#include "base/path_service.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01009#include "base/strings/string_util.h"
10#include "base/strings/utf_string_conversions.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011#include "chrome/browser/ui/browser.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000012#include "chrome/browser/ui/tabs/tab_strip_model.h"
Ben Murdochca12bfa2013-07-23 11:17:05 +010013#include "chrome/common/chrome_paths.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "chrome/test/base/in_process_browser_test.h"
15#include "chrome/test/base/ui_test_utils.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010016#include "components/autofill/content/browser/autofill_driver_impl.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010017#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)58218062012-11-14 11:43:16 +000021
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010022namespace autofill {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023namespace {
24
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000025const base::FilePath::CharType kTestName[] = FILE_PATH_LITERAL("heuristics");
Torne (Richard Coles)58218062012-11-14 11:43:16 +000026
27// Convert the |html| snippet to a data URI.
28GURL HTMLToDataURI(const std::string& html) {
29 return GURL(std::string("data:text/html;charset=utf-8,") + html);
30}
31
Ben Murdochca12bfa2013-07-23 11:17:05 +010032const 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)58218062012-11-14 11:43:16 +000039} // 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.
44class FormStructureBrowserTest : public InProcessBrowserTest,
45 public DataDrivenTest {
46 protected:
47 FormStructureBrowserTest();
48 virtual ~FormStructureBrowserTest();
49
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050 // 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 Murdochca12bfa2013-07-23 11:17:05 +010061FormStructureBrowserTest::FormStructureBrowserTest()
62 : DataDrivenTest(GetTestDataDir()) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000063}
64
65FormStructureBrowserTest::~FormStructureBrowserTest() {
66}
67
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068void 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)7d4cd472013-06-19 11:58:07 +010073 AutofillDriverImpl* autofill_driver = AutofillDriverImpl::FromWebContents(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000074 browser()->tab_strip_model()->GetActiveWebContents());
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010075 ASSERT_NE(static_cast<AutofillDriverImpl*>(NULL), autofill_driver);
76 AutofillManager* autofill_manager = autofill_driver->autofill_manager();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000077 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
82std::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 Murdochbb1529c2013-08-08 10:24:53 +010093 forms_string += (*field_iter)->Type().ToString();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000094 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)2a99a7e2013-03-28 15:31:22 +0000104// Also on ChromeOS/Aura. See crbug.com/173621
105#if defined(OS_WIN) || defined(USE_AURA)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000106#define MAYBE_DataDrivenHeuristics(n) DISABLED_DataDrivenHeuristics##n
107#else
108#define MAYBE_DataDrivenHeuristics(n) DataDrivenHeuristics##n
109#endif
110IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, DataDrivenHeuristics00) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000111 const base::FilePath::CharType kFileNamePattern[] =
112 FILE_PATH_LITERAL("00_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000113 RunDataDrivenTest(GetInputDirectory(kTestName),
114 GetOutputDirectory(kTestName),
115 kFileNamePattern);
116}
117
118IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, DataDrivenHeuristics01) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000119 const base::FilePath::CharType kFileNamePattern[] =
120 FILE_PATH_LITERAL("01_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000121 RunDataDrivenTest(GetInputDirectory(kTestName),
122 GetOutputDirectory(kTestName),
123 kFileNamePattern);
124}
125
126IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
127 MAYBE_DataDrivenHeuristics(02)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000128 const base::FilePath::CharType kFileNamePattern[] =
129 FILE_PATH_LITERAL("02_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000130 RunDataDrivenTest(GetInputDirectory(kTestName),
131 GetOutputDirectory(kTestName),
132 kFileNamePattern);
133}
134
135IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
136 MAYBE_DataDrivenHeuristics(03)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000137 const base::FilePath::CharType kFileNamePattern[] =
138 FILE_PATH_LITERAL("03_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000139 RunDataDrivenTest(GetInputDirectory(kTestName),
140 GetOutputDirectory(kTestName),
141 kFileNamePattern);
142}
143
144IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
145 MAYBE_DataDrivenHeuristics(04)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000146 const base::FilePath::CharType kFileNamePattern[] =
147 FILE_PATH_LITERAL("04_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000148 RunDataDrivenTest(GetInputDirectory(kTestName),
149 GetOutputDirectory(kTestName),
150 kFileNamePattern);
151}
152
153IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
154 MAYBE_DataDrivenHeuristics(05)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000155 const base::FilePath::CharType kFileNamePattern[] =
156 FILE_PATH_LITERAL("05_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000157 RunDataDrivenTest(GetInputDirectory(kTestName),
158 GetOutputDirectory(kTestName),
159 kFileNamePattern);
160}
161
162IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
163 MAYBE_DataDrivenHeuristics(06)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000164 const base::FilePath::CharType kFileNamePattern[] =
165 FILE_PATH_LITERAL("06_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000166 RunDataDrivenTest(GetInputDirectory(kTestName),
167 GetOutputDirectory(kTestName),
168 kFileNamePattern);
169}
170
171IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
172 MAYBE_DataDrivenHeuristics(07)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000173 const base::FilePath::CharType kFileNamePattern[] =
174 FILE_PATH_LITERAL("07_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000175 RunDataDrivenTest(GetInputDirectory(kTestName),
176 GetOutputDirectory(kTestName),
177 kFileNamePattern);
178}
179
180IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
181 MAYBE_DataDrivenHeuristics(08)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000182 const base::FilePath::CharType kFileNamePattern[] =
183 FILE_PATH_LITERAL("08_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000184 RunDataDrivenTest(GetInputDirectory(kTestName),
185 GetOutputDirectory(kTestName),
186 kFileNamePattern);
187}
188
189IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
190 MAYBE_DataDrivenHeuristics(09)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000191 const base::FilePath::CharType kFileNamePattern[] =
192 FILE_PATH_LITERAL("09_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000193 RunDataDrivenTest(GetInputDirectory(kTestName),
194 GetOutputDirectory(kTestName),
195 kFileNamePattern);
196}
197
198IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
199 MAYBE_DataDrivenHeuristics(10)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000200 const base::FilePath::CharType kFileNamePattern[] =
201 FILE_PATH_LITERAL("10_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000202 RunDataDrivenTest(GetInputDirectory(kTestName),
203 GetOutputDirectory(kTestName),
204 kFileNamePattern);
205}
206
207IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
208 MAYBE_DataDrivenHeuristics(11)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000209 const base::FilePath::CharType kFileNamePattern[] =
210 FILE_PATH_LITERAL("11_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000211 RunDataDrivenTest(GetInputDirectory(kTestName),
212 GetOutputDirectory(kTestName),
213 kFileNamePattern);
214}
215
216IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
217 MAYBE_DataDrivenHeuristics(12)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000218 const base::FilePath::CharType kFileNamePattern[] =
219 FILE_PATH_LITERAL("12_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000220 RunDataDrivenTest(GetInputDirectory(kTestName),
221 GetOutputDirectory(kTestName),
222 kFileNamePattern);
223}
224
225IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
226 MAYBE_DataDrivenHeuristics(13)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000227 const base::FilePath::CharType kFileNamePattern[] =
228 FILE_PATH_LITERAL("13_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000229 RunDataDrivenTest(GetInputDirectory(kTestName),
230 GetOutputDirectory(kTestName),
231 kFileNamePattern);
232}
233
234IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
235 MAYBE_DataDrivenHeuristics(14)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000236 const base::FilePath::CharType kFileNamePattern[] =
237 FILE_PATH_LITERAL("14_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000238 RunDataDrivenTest(GetInputDirectory(kTestName),
239 GetOutputDirectory(kTestName),
240 kFileNamePattern);
241}
242
243IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
244 MAYBE_DataDrivenHeuristics(15)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000245 const base::FilePath::CharType kFileNamePattern[] =
246 FILE_PATH_LITERAL("15_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000247 RunDataDrivenTest(GetInputDirectory(kTestName),
248 GetOutputDirectory(kTestName),
249 kFileNamePattern);
250}
251
252IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
253 MAYBE_DataDrivenHeuristics(16)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000254 const base::FilePath::CharType kFileNamePattern[] =
255 FILE_PATH_LITERAL("16_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000256 RunDataDrivenTest(GetInputDirectory(kTestName),
257 GetOutputDirectory(kTestName),
258 kFileNamePattern);
259}
260
261IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
262 MAYBE_DataDrivenHeuristics(17)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000263 const base::FilePath::CharType kFileNamePattern[] =
264 FILE_PATH_LITERAL("17_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000265 RunDataDrivenTest(GetInputDirectory(kTestName),
266 GetOutputDirectory(kTestName),
267 kFileNamePattern);
268}
269
270IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
271 MAYBE_DataDrivenHeuristics(20)) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000272 const base::FilePath::CharType kFileNamePattern[] =
273 FILE_PATH_LITERAL("20_*.html");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000274 RunDataDrivenTest(GetInputDirectory(kTestName),
275 GetOutputDirectory(kTestName),
276 kFileNamePattern);
277}
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100278
279} // namespace autofill