summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/autofill_form_test_utils.h
blob: 0c4c9b936c1fa38960f69fe45b0e604352b9e670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_FORM_TEST_UTILS_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_FORM_TEST_UTILS_H_

#include <vector>

#include "base/optional.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_field_data.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace autofill {
namespace test {

namespace {

// Default label assigned to fields.
constexpr char kLabelText[] = "label";

// Default name attribute assigned to fields.
constexpr char kNameText[] = "name";

// Default form url.
constexpr char kFormUrl[] = "http://example.com/form.html";

// Default form action url.
constexpr char kFormActionUrl[] = "http://example.com/submit.html";

}  // namespace

namespace internal {

// Expected FormFieldData are constructed based on these descriptions.
template <typename = void>
struct FieldDataDescription {
  ServerFieldType role = ServerFieldType::EMPTY_TYPE;
  bool is_focusable = true;
  const char* label = kLabelText;
  const char* name = kNameText;
  base::Optional<const char*> value = base::nullopt;
  const char* autocomplete_attribute = nullptr;
  const char* form_control_type = "text";
  bool should_autocomplete = true;
  base::Optional<bool> is_autofilled = base::nullopt;
};

// Attributes provided to the test form.
template <typename = void>
struct TestFormAttributes {
  const char* description_for_logging;
  std::vector<FieldDataDescription<>> fields;
  base::Optional<FormRendererId> unique_renderer_id = base::nullopt;
  const char* name = "TestForm";
  const char* url = kFormUrl;
  const char* action = kFormActionUrl;
  base::Optional<url::Origin> main_frame_origin = base::nullopt;
  bool is_formless_checkout = false;
  bool is_form_tag = true;
};

// Flags determining whether the corresponding check should be run on the test
// form.
template <typename = void>
struct TestFormFlags {
  // false means the function is not to be called.
  bool determine_heuristic_type = false;
  bool parse_query_response = false;
  // false means the corresponding check is not supposed to run.
  bool is_autofillable = false;
  bool should_be_parsed = false;
  bool should_be_queried = false;
  bool should_be_uploaded = false;
  bool has_author_specified_types = false;
  bool has_author_specified_upi_vpa_hint = false;
  // first value denotes whether the comparison is to be done while second
  // denotes EXPECT_TRUE for true and EXPECT_FALSE for false.
  std::pair<bool, bool> is_complete_credit_card_form = {false, false};
  // base::nullopt means no checking.
  base::Optional<int> field_count = base::nullopt;
  base::Optional<int> autofill_count = base::nullopt;
  base::Optional<int> section_count = base::nullopt;
  base::Optional<int> response_field_count = base::nullopt;
};

// Expected field type values to be verified with the test form.
template <typename = void>
struct ExpectedFieldTypeValues {
  std::vector<HtmlFieldType> expected_html_type = {};
  std::vector<AutofillField::PhonePart> expected_phone_part = {};
  std::vector<ServerFieldType> expected_heuristic_type = {};
  std::vector<ServerFieldType> expected_overall_type = {};
};

// Describes a test case for the parser.
template <typename = void>
struct FormStructureTestCase {
  TestFormAttributes<> form_attributes;
  TestFormFlags<> form_flags;
  ExpectedFieldTypeValues<> expected_field_types;
};

}  // namespace internal

using FieldDataDescription = internal::FieldDataDescription<>;
using TestFormAttributes = internal::TestFormAttributes<>;
using FormStructureTestCase = internal::FormStructureTestCase<>;

// Describes the |form_data|. Use this in SCOPED_TRACE if other logging
// messages might refer to the form.
testing::Message DescribeFormData(const FormData& form_data);

// Returns the form field relevant to the |role|.
FormFieldData CreateFieldByRole(ServerFieldType role);

// Creates a FormData to be fed to the parser.
FormData GetFormData(const TestFormAttributes& test_form_attributes);

class FormStructureTest : public testing::Test {
 protected:
  // Iterates over |test_cases|, creates a FormData for each, runs the parser
  // and checks the results.
  static void CheckFormStructureTestData(
      const std::vector<FormStructureTestCase>& test_cases);
};

}  // namespace test
}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_FORM_TEST_UTILS_H_