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.
8 #include "base/files/file_enumerator.h"
9 #include "base/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/autofill/content/browser/content_autofill_driver.h"
19 #include "components/autofill/content/browser/content_autofill_driver_factory.h"
20 #include "components/autofill/core/browser/autofill_manager.h"
21 #include "components/autofill/core/browser/data_driven_test.h"
22 #include "components/autofill/core/browser/form_structure.h"
25 #if defined(OS_MACOSX)
26 #include "base/mac/foundation_util.h"
32 const base::FilePath::CharType kTestName
[] = FILE_PATH_LITERAL("heuristics");
34 // Convert the |html| snippet to a data URI.
35 GURL
HTMLToDataURI(const std::string
& html
) {
36 // GURL requires data URLs to be UTF-8 and will fail below if it's not.
37 CHECK(base::IsStringUTF8(html
)) << "Input file is not UTF-8.";
38 return GURL(std::string("data:text/html;charset=utf-8,") + html
);
41 const base::FilePath
& GetTestDataDir() {
42 CR_DEFINE_STATIC_LOCAL(base::FilePath
, dir
, ());
44 PathService::Get(chrome::DIR_TEST_DATA
, &dir
);
48 const std::vector
<base::FilePath
> GetTestFiles() {
50 CHECK(PathService::Get(base::DIR_SOURCE_ROOT
, &dir
));
51 dir
= dir
.AppendASCII("chrome/test/data/autofill")
53 .AppendASCII("input");
54 base::FileEnumerator
input_files(dir
, false, base::FileEnumerator::FILES
);
55 std::vector
<base::FilePath
> files
;
56 for (base::FilePath input_file
= input_files
.Next(); !input_file
.empty();
57 input_file
= input_files
.Next()) {
58 files
.push_back(input_file
);
60 std::sort(files
.begin(), files
.end());
62 #if defined(OS_MACOSX)
63 base::mac::ClearAmIBundledCache();
64 #endif // defined(OS_MACOSX)
71 // A data-driven test for verifying Autofill heuristics. Each input is an HTML
72 // file that contains one or more forms. The corresponding output file lists the
73 // heuristically detected type for eachfield.
74 class FormStructureBrowserTest
75 : public InProcessBrowserTest
,
76 public DataDrivenTest
,
77 public ::testing::WithParamInterface
<base::FilePath
> {
79 FormStructureBrowserTest();
80 ~FormStructureBrowserTest() override
;
83 void GenerateResults(const std::string
& input
, std::string
* output
) override
;
85 // Serializes the given |forms| into a string.
86 std::string
FormStructuresToString(const std::vector
<FormStructure
*>& forms
);
89 DISALLOW_COPY_AND_ASSIGN(FormStructureBrowserTest
);
92 FormStructureBrowserTest::FormStructureBrowserTest()
93 : DataDrivenTest(GetTestDataDir()) {
96 FormStructureBrowserTest::~FormStructureBrowserTest() {
99 void FormStructureBrowserTest::GenerateResults(const std::string
& input
,
100 std::string
* output
) {
101 ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
102 HTMLToDataURI(input
)));
104 content::WebContents
* web_contents
=
105 browser()->tab_strip_model()->GetActiveWebContents();
106 ContentAutofillDriver
* autofill_driver
=
107 ContentAutofillDriverFactory::FromWebContents(web_contents
)
108 ->DriverForFrame(web_contents
->GetMainFrame());
109 ASSERT_NE(nullptr, autofill_driver
);
110 AutofillManager
* autofill_manager
= autofill_driver
->autofill_manager();
111 ASSERT_NE(nullptr, autofill_manager
);
112 std::vector
<FormStructure
*> forms
= autofill_manager
->form_structures_
.get();
113 *output
= FormStructuresToString(forms
);
116 std::string
FormStructureBrowserTest::FormStructuresToString(
117 const std::vector
<FormStructure
*>& forms
) {
118 std::string forms_string
;
119 for (const FormStructure
* form
: forms
) {
120 for (const AutofillField
* field
: *form
) {
121 forms_string
+= field
->Type().ToString();
122 forms_string
+= " | " + base::UTF16ToUTF8(field
->name
);
123 forms_string
+= " | " + base::UTF16ToUTF8(field
->label
);
124 forms_string
+= " | " + base::UTF16ToUTF8(field
->value
);
125 forms_string
+= " | " + field
->section();
126 forms_string
+= "\n";
132 IN_PROC_BROWSER_TEST_P(FormStructureBrowserTest
, DataDrivenHeuristics
) {
133 RunOneDataDrivenTest(GetParam(), GetOutputDirectory(kTestName
));
136 INSTANTIATE_TEST_CASE_P(AllForms
,
137 FormStructureBrowserTest
,
138 testing::ValuesIn(GetTestFiles()));
140 } // namespace autofill