1// Copyright (c) 2011 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
7#include "base/file_path.h"
8#include "base/string_util.h"
9#include "base/utf_string_conversions.h"
10#include "chrome/browser/autofill/autofill_manager.h"
11#include "chrome/browser/autofill/data_driven_test.h"
12#include "chrome/browser/autofill/form_structure.h"
13#include "chrome/browser/ui/browser.h"
14#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
15#include "chrome/test/in_process_browser_test.h"
16#include "chrome/test/ui_test_utils.h"
17#include "googleurl/src/gurl.h"
18
19namespace {
20
21const FilePath::CharType kTestName[] = FILE_PATH_LITERAL("heuristics");
22const FilePath::CharType kFileNamePattern[] = FILE_PATH_LITERAL("*.html");
23
24// Convert the |html| snippet to a data URI.
25GURL HTMLToDataURI(const std::string& html) {
26  return GURL(std::string("data:text/html;charset=utf-8,") + html);
27}
28
29}  // namespace
30
31// A data-driven test for verifying Autofill heuristics. Each input is an HTML
32// file that contains one or more forms. The corresponding output file lists the
33// heuristically detected type for eachfield.
34class FormStructureBrowserTest : public InProcessBrowserTest,
35                                 public DataDrivenTest {
36 protected:
37  FormStructureBrowserTest();
38  virtual ~FormStructureBrowserTest();
39
40  // DataDrivenTest:
41  virtual void GenerateResults(const std::string& input,
42                               std::string* output);
43
44  // Serializes the given |forms| into a string.
45  std::string FormStructuresToString(const std::vector<FormStructure*>& forms);
46
47 private:
48  DISALLOW_COPY_AND_ASSIGN(FormStructureBrowserTest);
49};
50
51FormStructureBrowserTest::FormStructureBrowserTest() {
52}
53
54FormStructureBrowserTest::~FormStructureBrowserTest() {
55}
56
57void FormStructureBrowserTest::GenerateResults(const std::string& input,
58                                               std::string* output) {
59  ASSERT_NO_FATAL_FAILURE(ui_test_utils::NavigateToURL(browser(),
60                                                       HTMLToDataURI(input)));
61
62  AutofillManager* autofill_manager =
63      browser()->GetSelectedTabContentsWrapper()->autofill_manager();
64  ASSERT_NE(static_cast<AutofillManager*>(NULL), autofill_manager);
65  std::vector<FormStructure*> forms = autofill_manager->form_structures_.get();
66  *output = FormStructureBrowserTest::FormStructuresToString(forms);
67}
68
69std::string FormStructureBrowserTest::FormStructuresToString(
70    const std::vector<FormStructure*>& forms) {
71  std::string forms_string;
72  for (std::vector<FormStructure*>::const_iterator iter = forms.begin();
73       iter != forms.end();
74       ++iter) {
75
76    for (std::vector<AutofillField*>::const_iterator field_iter =
77            (*iter)->begin();
78         field_iter != (*iter)->end();
79         ++field_iter) {
80      // The field list is NULL-terminated.  Exit loop when at the end.
81      if (!*field_iter)
82        break;
83      forms_string += AutofillType::FieldTypeToString((*field_iter)->type());
84      forms_string += "\n";
85    }
86  }
87  return forms_string;
88}
89
90IN_PROC_BROWSER_TEST_F(FormStructureBrowserTest, DataDrivenHeuristics) {
91  RunDataDrivenTest(GetInputDirectory(kTestName),
92                    GetOutputDirectory(kTestName),
93                    kFileNamePattern);
94}
95