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.
7 #include "base/files/file_path.h"
8 #include "base/path_service.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/ui_test_utils.h"
16 #include "components/autofill/content/browser/content_autofill_driver.h"
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"
25 const base::FilePath::CharType kTestName
[] = FILE_PATH_LITERAL("heuristics");
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
);
32 const base::FilePath
& GetTestDataDir() {
33 CR_DEFINE_STATIC_LOCAL(base::FilePath
, dir
, ());
35 PathService::Get(chrome::DIR_TEST_DATA
, &dir
);
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
{
47 FormStructureBrowserTest();
48 virtual ~FormStructureBrowserTest();
51 virtual void GenerateResults(const std::string
& input
,
52 std::string
* output
) OVERRIDE
;
54 // Serializes the given |forms| into a string.
55 std::string
FormStructuresToString(const std::vector
<FormStructure
*>& forms
);
58 DISALLOW_COPY_AND_ASSIGN(FormStructureBrowserTest
);
61 FormStructureBrowserTest::FormStructureBrowserTest()
62 : DataDrivenTest(GetTestDataDir()) {
65 FormStructureBrowserTest::~FormStructureBrowserTest() {
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
)));
73 ContentAutofillDriver
* autofill_driver
=
74 ContentAutofillDriver::FromWebContents(
75 browser()->tab_strip_model()->GetActiveWebContents());
76 ASSERT_NE(static_cast<ContentAutofillDriver
*>(NULL
), autofill_driver
);
77 AutofillManager
* autofill_manager
= autofill_driver
->autofill_manager();
78 ASSERT_NE(static_cast<AutofillManager
*>(NULL
), autofill_manager
);
79 std::vector
<FormStructure
*> forms
= autofill_manager
->form_structures_
.get();
80 *output
= FormStructureBrowserTest::FormStructuresToString(forms
);
83 std::string
FormStructureBrowserTest::FormStructuresToString(
84 const std::vector
<FormStructure
*>& forms
) {
85 std::string forms_string
;
86 for (std::vector
<FormStructure
*>::const_iterator iter
= forms
.begin();
90 for (std::vector
<AutofillField
*>::const_iterator field_iter
=
92 field_iter
!= (*iter
)->end();
94 forms_string
+= (*field_iter
)->Type().ToString();
95 forms_string
+= " | " + base::UTF16ToUTF8((*field_iter
)->name
);
96 forms_string
+= " | " + base::UTF16ToUTF8((*field_iter
)->label
);
97 forms_string
+= " | " + base::UTF16ToUTF8((*field_iter
)->value
);
104 // Heuristics tests timeout on Windows. See http://crbug.com/85276
105 // Also on ChromeOS/Aura. See crbug.com/173621
106 // On Linux too. See crbug.com/323093
107 #if defined(USE_AURA)
108 #define MAYBE_DataDrivenHeuristics(n) DISABLED_DataDrivenHeuristics##n
110 #define MAYBE_DataDrivenHeuristics(n) DataDrivenHeuristics##n
112 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
, DataDrivenHeuristics00
) {
113 const base::FilePath::CharType kFileNamePattern
[] =
114 FILE_PATH_LITERAL("00_*.html");
115 RunDataDrivenTest(GetInputDirectory(kTestName
),
116 GetOutputDirectory(kTestName
),
120 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
, DataDrivenHeuristics01
) {
121 const base::FilePath::CharType kFileNamePattern
[] =
122 FILE_PATH_LITERAL("01_*.html");
123 RunDataDrivenTest(GetInputDirectory(kTestName
),
124 GetOutputDirectory(kTestName
),
128 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
129 MAYBE_DataDrivenHeuristics(02)) {
130 const base::FilePath::CharType kFileNamePattern
[] =
131 FILE_PATH_LITERAL("02_*.html");
132 RunDataDrivenTest(GetInputDirectory(kTestName
),
133 GetOutputDirectory(kTestName
),
137 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
138 MAYBE_DataDrivenHeuristics(03)) {
139 const base::FilePath::CharType kFileNamePattern
[] =
140 FILE_PATH_LITERAL("03_*.html");
141 RunDataDrivenTest(GetInputDirectory(kTestName
),
142 GetOutputDirectory(kTestName
),
146 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
147 MAYBE_DataDrivenHeuristics(04)) {
148 const base::FilePath::CharType kFileNamePattern
[] =
149 FILE_PATH_LITERAL("04_*.html");
150 RunDataDrivenTest(GetInputDirectory(kTestName
),
151 GetOutputDirectory(kTestName
),
155 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
156 MAYBE_DataDrivenHeuristics(05)) {
157 const base::FilePath::CharType kFileNamePattern
[] =
158 FILE_PATH_LITERAL("05_*.html");
159 RunDataDrivenTest(GetInputDirectory(kTestName
),
160 GetOutputDirectory(kTestName
),
164 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
165 MAYBE_DataDrivenHeuristics(06)) {
166 const base::FilePath::CharType kFileNamePattern
[] =
167 FILE_PATH_LITERAL("06_*.html");
168 RunDataDrivenTest(GetInputDirectory(kTestName
),
169 GetOutputDirectory(kTestName
),
173 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
174 MAYBE_DataDrivenHeuristics(07)) {
175 const base::FilePath::CharType kFileNamePattern
[] =
176 FILE_PATH_LITERAL("07_*.html");
177 RunDataDrivenTest(GetInputDirectory(kTestName
),
178 GetOutputDirectory(kTestName
),
182 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
183 MAYBE_DataDrivenHeuristics(08)) {
184 const base::FilePath::CharType kFileNamePattern
[] =
185 FILE_PATH_LITERAL("08_*.html");
186 RunDataDrivenTest(GetInputDirectory(kTestName
),
187 GetOutputDirectory(kTestName
),
191 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
192 MAYBE_DataDrivenHeuristics(09)) {
193 const base::FilePath::CharType kFileNamePattern
[] =
194 FILE_PATH_LITERAL("09_*.html");
195 RunDataDrivenTest(GetInputDirectory(kTestName
),
196 GetOutputDirectory(kTestName
),
200 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
201 MAYBE_DataDrivenHeuristics(10)) {
202 const base::FilePath::CharType kFileNamePattern
[] =
203 FILE_PATH_LITERAL("10_*.html");
204 RunDataDrivenTest(GetInputDirectory(kTestName
),
205 GetOutputDirectory(kTestName
),
209 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
210 MAYBE_DataDrivenHeuristics(11)) {
211 const base::FilePath::CharType kFileNamePattern
[] =
212 FILE_PATH_LITERAL("11_*.html");
213 RunDataDrivenTest(GetInputDirectory(kTestName
),
214 GetOutputDirectory(kTestName
),
218 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
219 MAYBE_DataDrivenHeuristics(12)) {
220 const base::FilePath::CharType kFileNamePattern
[] =
221 FILE_PATH_LITERAL("12_*.html");
222 RunDataDrivenTest(GetInputDirectory(kTestName
),
223 GetOutputDirectory(kTestName
),
227 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
228 MAYBE_DataDrivenHeuristics(13)) {
229 const base::FilePath::CharType kFileNamePattern
[] =
230 FILE_PATH_LITERAL("13_*.html");
231 RunDataDrivenTest(GetInputDirectory(kTestName
),
232 GetOutputDirectory(kTestName
),
236 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
237 MAYBE_DataDrivenHeuristics(14)) {
238 const base::FilePath::CharType kFileNamePattern
[] =
239 FILE_PATH_LITERAL("14_*.html");
240 RunDataDrivenTest(GetInputDirectory(kTestName
),
241 GetOutputDirectory(kTestName
),
245 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
246 MAYBE_DataDrivenHeuristics(15)) {
247 const base::FilePath::CharType kFileNamePattern
[] =
248 FILE_PATH_LITERAL("15_*.html");
249 RunDataDrivenTest(GetInputDirectory(kTestName
),
250 GetOutputDirectory(kTestName
),
254 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
255 MAYBE_DataDrivenHeuristics(16)) {
256 const base::FilePath::CharType kFileNamePattern
[] =
257 FILE_PATH_LITERAL("16_*.html");
258 RunDataDrivenTest(GetInputDirectory(kTestName
),
259 GetOutputDirectory(kTestName
),
263 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
264 MAYBE_DataDrivenHeuristics(17)) {
265 const base::FilePath::CharType kFileNamePattern
[] =
266 FILE_PATH_LITERAL("17_*.html");
267 RunDataDrivenTest(GetInputDirectory(kTestName
),
268 GetOutputDirectory(kTestName
),
272 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest
,
273 MAYBE_DataDrivenHeuristics(20)) {
274 const base::FilePath::CharType kFileNamePattern
[] =
275 FILE_PATH_LITERAL("20_*.html");
276 RunDataDrivenTest(GetInputDirectory(kTestName
),
277 GetOutputDirectory(kTestName
),
281 } // namespace autofill