File: autofill_test_utils.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (243 lines) | stat: -rw-r--r-- 9,714 bytes parent folder | download | duplicates (4)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_TEST_UTILS_H_
#define COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_TEST_UTILS_H_

#include <string_view>
#include <vector>

#include "base/location.h"
#include "base/test/scoped_feature_list.h"
#include "base/types/strong_alias.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/unique_ids.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace autofill {

class FormFieldData;

namespace test {

// The AutofillTestEnvironment encapsulates global state for test data that
// should be reset automatically after each test.
class AutofillTestEnvironment {
 public:
  struct Options {
    bool disable_server_communication = true;
  };

  static AutofillTestEnvironment& GetCurrent(const base::Location& = FROM_HERE);

  AutofillTestEnvironment(const AutofillTestEnvironment&) = delete;
  AutofillTestEnvironment& operator=(const AutofillTestEnvironment&) = delete;
  ~AutofillTestEnvironment();

  LocalFrameToken NextLocalFrameToken();
  RemoteFrameToken NextRemoteFrameToken();
  FormRendererId NextFormRendererId();
  FieldRendererId NextFieldRendererId();

 protected:
  explicit AutofillTestEnvironment(const Options& options = {
                                       .disable_server_communication = false});

 private:
  static AutofillTestEnvironment* current_instance_;
  base::test::ScopedFeatureList scoped_feature_list_;

  // Use some distinct 64 bit numbers to start the counters.
  uint64_t local_frame_token_counter_high_ = 0xAAAAAAAAAAAAAAAA;
  uint64_t local_frame_token_counter_low_ = 0xBBBBBBBBBBBBBBBB;
  uint64_t remote_frame_token_counter_high_ = 0xBBBBBBBBBBBBBBBB;
  uint64_t remote_frame_token_counter_low_ = 0xAAAAAAAAAAAAAAAA;
  FormRendererId::underlying_type form_renderer_id_counter_ = 10;
  FieldRendererId::underlying_type field_renderer_id_counter_ = 10;
};

// This encapsulates global unittest state. By default this environment
// enables the `kAutofillServerCommunication` feature.
class AutofillUnitTestEnvironment : public AutofillTestEnvironment {
 public:
  explicit AutofillUnitTestEnvironment(
      const Options& options = {.disable_server_communication = false});
};

// This encapsulates global browsertest state. By default this environment
// disables the `kAutofillServerCommunication` feature.
class AutofillBrowserTestEnvironment : public AutofillTestEnvironment {
 public:
  explicit AutofillBrowserTestEnvironment(
      const Options& options = {.disable_server_communication = true});
};

using RandomizeFrame = base::StrongAlias<struct RandomizeFrameTag, bool>;

// Creates non-empty LocalFrameToken.
//
// If `randomize` is true, the LocalFrameToken changes for successive calls.
// Within each unit test, the generated values are deterministically predictable
// (because the test's AutofillTestEnvironment restarts the generation).
//
// If `randomize` is false, the LocalFrameToken is stable across multiple calls.
LocalFrameToken MakeLocalFrameToken(
    RandomizeFrame randomize = RandomizeFrame(true));

// Creates non-empty RemoteFrameToken.
//
// If `randomize` is true, the RemoteFrameToken changes for successive calls.
// Within each unit test, the generated values are deterministically predictable
// (because the test's AutofillTestEnvironment restarts the generation).
//
// If `randomize` is false, the RemoteFrameToken is stable across multiple
// calls.
RemoteFrameToken MakeRemoteFrameToken(
    RandomizeFrame randomize = RandomizeFrame(true));

// Creates new, pairwise distinct FormRendererIds.
inline FormRendererId MakeFormRendererId() {
  return AutofillTestEnvironment::GetCurrent().NextFormRendererId();
}

// Creates new, pairwise distinct FieldRendererIds.
inline FieldRendererId MakeFieldRendererId() {
  return AutofillTestEnvironment::GetCurrent().NextFieldRendererId();
}

// Creates new, pairwise distinct FormGlobalIds. If `randomize` is true, the
// LocalFrameToken is generated randomly, otherwise it is stable across multiple
// calls.
inline FormGlobalId MakeFormGlobalId(
    RandomizeFrame randomize = RandomizeFrame(true)) {
  return {MakeLocalFrameToken(randomize), MakeFormRendererId()};
}

// Creates new, pairwise distinct FieldGlobalIds. If `randomize` is true, the
// LocalFrameToken is generated randomly, otherwise it is stable.
inline FieldGlobalId MakeFieldGlobalId(
    RandomizeFrame randomize = RandomizeFrame(true)) {
  return {MakeLocalFrameToken(randomize), MakeFieldRendererId()};
}

// Returns a copy of `form` in which the host frame of its and its fields is
// set to `frame_token`.
FormData CreateFormDataForFrame(FormData form, LocalFrameToken frame_token);

// Returns a copy of `form` with cleared values.
FormData WithoutValues(FormData form);

// Returns a copy of `form` with `is_autofilled` set as specified.
FormData AsAutofilled(FormData form, bool is_autofilled = true);

// Strips those members from `form` and `field` that are not serialized via
// mojo, i.e., resets them to `{}`.
FormData WithoutUnserializedData(FormData form);
FormFieldData WithoutUnserializedData(FormFieldData field);

// A valid France IBAN number.
inline constexpr char kIbanValue[] = "FR76 3000 6000 0112 3456 7890 189";
inline constexpr char16_t kIbanValue16[] = u"FR76 3000 6000 0112 3456 7890 189";
// Two valid Switzerland IBAN numbers.
inline constexpr char kIbanValue_1[] = "CH56 0483 5012 3456 7800 9";
inline constexpr char kIbanValue_2[] = "CH93 0076 2011 6238 5295 7";

// Provides a quick way to populate a `FormFieldData`.
[[nodiscard]] FormFieldData CreateTestFormField(std::string_view label,
                                                std::string_view name,
                                                std::string_view value,
                                                FormControlType type);

[[nodiscard]] FormFieldData CreateTestFormField(std::string_view label,
                                                std::string_view name,
                                                std::string_view value,
                                                FormControlType type,
                                                std::string_view autocomplete);
[[nodiscard]] FormFieldData CreateTestFormField(std::string_view label,
                                                std::string_view name,
                                                std::string_view value,
                                                FormControlType type,
                                                std::string_view autocomplete,
                                                uint64_t max_length);

// Provides a quick way to populate a select field.
[[nodiscard]] FormFieldData CreateTestSelectField(
    std::string_view label,
    std::string_view name,
    std::string_view value,
    const std::vector<const char*>& values,
    const std::vector<const char*>& contents);

[[nodiscard]] FormFieldData CreateTestSelectField(
    std::string_view label,
    std::string_view name,
    std::string_view value,
    std::string_view autocomplete,
    const std::vector<const char*>& values,
    const std::vector<const char*>& contents);

[[nodiscard]] FormFieldData CreateTestSelectField(
    const std::vector<const char*>& values);

[[nodiscard]] FormFieldData CreateTestSelectField(
    std::string_view label,
    std::string_view name,
    std::string_view value,
    std::string_view autocomplete,
    const std::vector<const char*>& values,
    const std::vector<const char*>& contents,
    FormControlType type);

// Provides a quick way to populate a datalist field.
[[nodiscard]] FormFieldData CreateTestDatalistField(
    std::string_view label,
    std::string_view name,
    std::string_view value,
    const std::vector<const char*>& values,
    const std::vector<const char*>& labels);

// Populates `form` with data corresponding to a simple personal information
// form, including name and email, but no address-related fields.
[[nodiscard]] FormData CreateTestPersonalInformationFormData();

// Populates `form` with data corresponding to a simple credit card form.
[[nodiscard]] FormData CreateTestCreditCardFormData(bool is_https,
                                                    bool use_month_type,
                                                    bool split_names = false);

// Populates `form_data` with data corresponding to an IBAN form (a form with a
// single IBAN field).
[[nodiscard]] FormData CreateTestIbanFormData(
    std::string_view value = kIbanValue,
    bool is_https = true);

// Populates `form_data` with data corresponding to a loyalty card form (a form
// with a single loyalty card field).
[[nodiscard]] FormData CreateTestLoyaltyCardFormData();

// Populates `form_data` with data corresponding to a merchant promo code form
// (a form with a single merchant promo code field).
[[nodiscard]] FormData CreateTestMerchantPromoCodeFormData();

// Creates a `FormData` with a username and a password field.
[[nodiscard]] FormData CreateTestPasswordFormData();

// Creates a `FormData` that mimics a signup form (username field and two
// password fields).
[[nodiscard]] FormData CreateTestSignupFormData();

// Creates a `FormData` with a single unclassified field.
[[nodiscard]] FormData CreateTestUnclassifiedFormData();

MATCHER_P(DeepEqualsFormData,
          form_data,
          negation ? "does not equal" : "equals") {
  return FormData::DeepEqual(arg, form_data);
}

}  // namespace test

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_TEST_UTILS_H_