Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / autofill / form_structure_browsertest.cc
blob335ea9873e6150b606b91b5e5d904d8bab47a559
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.
5 #include <vector>
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/autofill_driver_impl.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"
20 #include "url/gurl.h"
22 namespace autofill {
23 namespace {
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, ());
34 if (dir.empty())
35 PathService::Get(chrome::DIR_TEST_DATA, &dir);
36 return dir;
39 } // namespace
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();
50 // DataDrivenTest:
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);
57 private:
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 AutofillDriverImpl* autofill_driver = AutofillDriverImpl::FromWebContents(
74 browser()->tab_strip_model()->GetActiveWebContents());
75 ASSERT_NE(static_cast<AutofillDriverImpl*>(NULL), autofill_driver);
76 AutofillManager* autofill_manager = autofill_driver->autofill_manager();
77 ASSERT_NE(static_cast<AutofillManager*>(NULL), autofill_manager);
78 std::vector<FormStructure*> forms = autofill_manager->form_structures_.get();
79 *output = FormStructureBrowserTest::FormStructuresToString(forms);
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) {
89 for (std::vector<AutofillField*>::const_iterator field_iter =
90 (*iter)->begin();
91 field_iter != (*iter)->end();
92 ++field_iter) {
93 forms_string += (*field_iter)->Type().ToString();
94 forms_string += " | " + base::UTF16ToUTF8((*field_iter)->name);
95 forms_string += " | " + base::UTF16ToUTF8((*field_iter)->label);
96 forms_string += " | " + base::UTF16ToUTF8((*field_iter)->value);
97 forms_string += "\n";
100 return forms_string;
103 // Heuristics tests timeout on Windows. See http://crbug.com/85276
104 // Also on ChromeOS/Aura. See crbug.com/173621
105 // On Linux too. See crbug.com/323093
106 #if defined(OS_WIN) || defined(USE_AURA) || defined(OS_LINUX)
107 #define MAYBE_DataDrivenHeuristics(n) DISABLED_DataDrivenHeuristics##n
108 #else
109 #define MAYBE_DataDrivenHeuristics(n) DataDrivenHeuristics##n
110 #endif
111 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, DataDrivenHeuristics00) {
112 const base::FilePath::CharType kFileNamePattern[] =
113 FILE_PATH_LITERAL("00_*.html");
114 RunDataDrivenTest(GetInputDirectory(kTestName),
115 GetOutputDirectory(kTestName),
116 kFileNamePattern);
119 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, DataDrivenHeuristics01) {
120 const base::FilePath::CharType kFileNamePattern[] =
121 FILE_PATH_LITERAL("01_*.html");
122 RunDataDrivenTest(GetInputDirectory(kTestName),
123 GetOutputDirectory(kTestName),
124 kFileNamePattern);
127 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
128 MAYBE_DataDrivenHeuristics(02)) {
129 const base::FilePath::CharType kFileNamePattern[] =
130 FILE_PATH_LITERAL("02_*.html");
131 RunDataDrivenTest(GetInputDirectory(kTestName),
132 GetOutputDirectory(kTestName),
133 kFileNamePattern);
136 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
137 MAYBE_DataDrivenHeuristics(03)) {
138 const base::FilePath::CharType kFileNamePattern[] =
139 FILE_PATH_LITERAL("03_*.html");
140 RunDataDrivenTest(GetInputDirectory(kTestName),
141 GetOutputDirectory(kTestName),
142 kFileNamePattern);
145 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
146 MAYBE_DataDrivenHeuristics(04)) {
147 const base::FilePath::CharType kFileNamePattern[] =
148 FILE_PATH_LITERAL("04_*.html");
149 RunDataDrivenTest(GetInputDirectory(kTestName),
150 GetOutputDirectory(kTestName),
151 kFileNamePattern);
154 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
155 MAYBE_DataDrivenHeuristics(05)) {
156 const base::FilePath::CharType kFileNamePattern[] =
157 FILE_PATH_LITERAL("05_*.html");
158 RunDataDrivenTest(GetInputDirectory(kTestName),
159 GetOutputDirectory(kTestName),
160 kFileNamePattern);
163 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
164 MAYBE_DataDrivenHeuristics(06)) {
165 const base::FilePath::CharType kFileNamePattern[] =
166 FILE_PATH_LITERAL("06_*.html");
167 RunDataDrivenTest(GetInputDirectory(kTestName),
168 GetOutputDirectory(kTestName),
169 kFileNamePattern);
172 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
173 MAYBE_DataDrivenHeuristics(07)) {
174 const base::FilePath::CharType kFileNamePattern[] =
175 FILE_PATH_LITERAL("07_*.html");
176 RunDataDrivenTest(GetInputDirectory(kTestName),
177 GetOutputDirectory(kTestName),
178 kFileNamePattern);
181 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
182 MAYBE_DataDrivenHeuristics(08)) {
183 const base::FilePath::CharType kFileNamePattern[] =
184 FILE_PATH_LITERAL("08_*.html");
185 RunDataDrivenTest(GetInputDirectory(kTestName),
186 GetOutputDirectory(kTestName),
187 kFileNamePattern);
190 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
191 MAYBE_DataDrivenHeuristics(09)) {
192 const base::FilePath::CharType kFileNamePattern[] =
193 FILE_PATH_LITERAL("09_*.html");
194 RunDataDrivenTest(GetInputDirectory(kTestName),
195 GetOutputDirectory(kTestName),
196 kFileNamePattern);
199 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
200 MAYBE_DataDrivenHeuristics(10)) {
201 const base::FilePath::CharType kFileNamePattern[] =
202 FILE_PATH_LITERAL("10_*.html");
203 RunDataDrivenTest(GetInputDirectory(kTestName),
204 GetOutputDirectory(kTestName),
205 kFileNamePattern);
208 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
209 MAYBE_DataDrivenHeuristics(11)) {
210 const base::FilePath::CharType kFileNamePattern[] =
211 FILE_PATH_LITERAL("11_*.html");
212 RunDataDrivenTest(GetInputDirectory(kTestName),
213 GetOutputDirectory(kTestName),
214 kFileNamePattern);
217 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
218 MAYBE_DataDrivenHeuristics(12)) {
219 const base::FilePath::CharType kFileNamePattern[] =
220 FILE_PATH_LITERAL("12_*.html");
221 RunDataDrivenTest(GetInputDirectory(kTestName),
222 GetOutputDirectory(kTestName),
223 kFileNamePattern);
226 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
227 MAYBE_DataDrivenHeuristics(13)) {
228 const base::FilePath::CharType kFileNamePattern[] =
229 FILE_PATH_LITERAL("13_*.html");
230 RunDataDrivenTest(GetInputDirectory(kTestName),
231 GetOutputDirectory(kTestName),
232 kFileNamePattern);
235 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
236 MAYBE_DataDrivenHeuristics(14)) {
237 const base::FilePath::CharType kFileNamePattern[] =
238 FILE_PATH_LITERAL("14_*.html");
239 RunDataDrivenTest(GetInputDirectory(kTestName),
240 GetOutputDirectory(kTestName),
241 kFileNamePattern);
244 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
245 MAYBE_DataDrivenHeuristics(15)) {
246 const base::FilePath::CharType kFileNamePattern[] =
247 FILE_PATH_LITERAL("15_*.html");
248 RunDataDrivenTest(GetInputDirectory(kTestName),
249 GetOutputDirectory(kTestName),
250 kFileNamePattern);
253 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
254 MAYBE_DataDrivenHeuristics(16)) {
255 const base::FilePath::CharType kFileNamePattern[] =
256 FILE_PATH_LITERAL("16_*.html");
257 RunDataDrivenTest(GetInputDirectory(kTestName),
258 GetOutputDirectory(kTestName),
259 kFileNamePattern);
262 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
263 MAYBE_DataDrivenHeuristics(17)) {
264 const base::FilePath::CharType kFileNamePattern[] =
265 FILE_PATH_LITERAL("17_*.html");
266 RunDataDrivenTest(GetInputDirectory(kTestName),
267 GetOutputDirectory(kTestName),
268 kFileNamePattern);
271 IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest,
272 MAYBE_DataDrivenHeuristics(20)) {
273 const base::FilePath::CharType kFileNamePattern[] =
274 FILE_PATH_LITERAL("20_*.html");
275 RunDataDrivenTest(GetInputDirectory(kTestName),
276 GetOutputDirectory(kTestName),
277 kFileNamePattern);
280 } // namespace autofill