File: autofill_test_utils.cc

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 (366 lines) | stat: -rw-r--r-- 14,330 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// 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.

#include "components/autofill/core/common/autofill_test_utils.h"

#include <algorithm>
#include <string>
#include <utility>
#include <vector>

#include "base/check.h"
#include "base/location.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/types/zip.h"
#include "base/unguessable_token.h"
#include "components/autofill/core/common/autocomplete_parsing_util.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_data_test_api.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/unique_ids.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace autofill::test {

namespace {

FormData ConstructFormWithNameRenderIdAndProtocol(bool is_https) {
  FormData form;
  form.set_name(u"MyForm");
  form.set_renderer_id(MakeFormRendererId());
  std::string_view protocol = is_https ? "https://" : "http://";
  form.set_url(GURL(base::StrCat({protocol, "myform.com/form.html"})));
  form.set_action(GURL(base::StrCat({protocol, "myform.com/submit.html"})));
  form.set_main_frame_origin(url::Origin::Create(
      GURL(base::StrCat({protocol, "myform_root.com/form.html"}))));
  return form;
}

}  // namespace

AutofillTestEnvironment* AutofillTestEnvironment::current_instance_ = nullptr;

AutofillTestEnvironment& AutofillTestEnvironment::GetCurrent(
    const base::Location& location) {
  CHECK(current_instance_)
      << location.ToString() << " "
      << "tried to access the current AutofillTestEnvironment, but none "
         "exists. Add an autofill::test::Autofill(Browser|Unit)TestEnvironment "
         "member to test your test fixture.";
  return *current_instance_;
}

AutofillTestEnvironment::AutofillTestEnvironment(const Options& options) {
  CHECK(!current_instance_) << "An autofill::test::AutofillTestEnvironment has "
                               "already been registered.";
  current_instance_ = this;
  if (options.disable_server_communication) {
    scoped_feature_list_.InitAndDisableFeature(
        features::test::kAutofillServerCommunication);
  }
}

AutofillTestEnvironment::~AutofillTestEnvironment() {
  CHECK_EQ(current_instance_, this);
  current_instance_ = nullptr;
}

LocalFrameToken AutofillTestEnvironment::NextLocalFrameToken() {
  return LocalFrameToken(base::UnguessableToken::CreateForTesting(
      ++local_frame_token_counter_high_, ++local_frame_token_counter_low_));
}

RemoteFrameToken AutofillTestEnvironment::NextRemoteFrameToken() {
  return RemoteFrameToken(base::UnguessableToken::CreateForTesting(
      ++remote_frame_token_counter_high_, ++remote_frame_token_counter_low_));
}

FormRendererId AutofillTestEnvironment::NextFormRendererId() {
  return FormRendererId(++form_renderer_id_counter_);
}

FieldRendererId AutofillTestEnvironment::NextFieldRendererId() {
  return FieldRendererId(++field_renderer_id_counter_);
}

AutofillUnitTestEnvironment::AutofillUnitTestEnvironment(const Options& options)
    : AutofillTestEnvironment(options) {}

AutofillBrowserTestEnvironment::AutofillBrowserTestEnvironment(
    const Options& options)
    : AutofillTestEnvironment(options) {}

LocalFrameToken MakeLocalFrameToken(RandomizeFrame randomize) {
  if (*randomize) {
    return LocalFrameToken(
        AutofillTestEnvironment::GetCurrent().NextLocalFrameToken());
  } else {
    return LocalFrameToken(
        base::UnguessableToken::CreateForTesting(98765, 43210));
  }
}

RemoteFrameToken MakeRemoteFrameToken(RandomizeFrame randomize) {
  if (*randomize) {
    return RemoteFrameToken(
        AutofillTestEnvironment::GetCurrent().NextRemoteFrameToken());
  } else {
    return RemoteFrameToken(
        base::UnguessableToken::CreateForTesting(12345, 67890));
  }
}

FormData CreateFormDataForFrame(FormData form, LocalFrameToken frame_token) {
  form.set_host_frame(frame_token);
  for (FormFieldData& field : test_api(form).fields()) {
    field.set_host_frame(frame_token);
  }
  return form;
}

FormData WithoutValues(FormData form) {
  for (FormFieldData& field : test_api(form).fields()) {
    field.set_value({});
  }
  return form;
}

FormData AsAutofilled(FormData form, bool is_autofilled) {
  for (FormFieldData& field : test_api(form).fields()) {
    field.set_is_autofilled(is_autofilled);
  }
  return form;
}

FormData WithoutUnserializedData(FormData form) {
  form.set_url({});
  form.set_main_frame_origin({});
  form.set_host_frame({});
  for (FormFieldData& field : test_api(form).fields()) {
    field = WithoutUnserializedData(std::move(field));
  }
  return form;
}

FormFieldData WithoutUnserializedData(FormFieldData field) {
  field.set_host_frame({});
  return field;
}

FormFieldData CreateTestFormField(std::string_view label,
                                  std::string_view name,
                                  std::string_view value,
                                  FormControlType type) {
  FormFieldData field;
  field.set_host_frame(MakeLocalFrameToken());
  field.set_renderer_id(MakeFieldRendererId());
  field.set_label(base::UTF8ToUTF16(label));
  field.set_name(base::UTF8ToUTF16(name));
  field.set_value(base::UTF8ToUTF16(value));
  field.set_form_control_type(type);
  field.set_is_focusable(true);
  return field;
}

FormFieldData CreateTestFormField(std::string_view label,
                                  std::string_view name,
                                  std::string_view value,
                                  FormControlType type,
                                  std::string_view autocomplete) {
  FormFieldData field = CreateTestFormField(label, name, value, type);
  field.set_autocomplete_attribute(std::string(autocomplete));
  field.set_parsed_autocomplete(ParseAutocompleteAttribute(autocomplete));
  return field;
}

FormFieldData CreateTestFormField(std::string_view label,
                                  std::string_view name,
                                  std::string_view value,
                                  FormControlType type,
                                  std::string_view autocomplete,
                                  uint64_t max_length) {
  FormFieldData field = CreateTestFormField(label, name, value, type);
  // First, set the `max_length`, as the `parsed_autocomplete` is set based on
  // this value.
  field.set_max_length(max_length);
  field.set_autocomplete_attribute(std::string(autocomplete));
  field.set_parsed_autocomplete(ParseAutocompleteAttribute(autocomplete));
  return field;
}

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) {
  return CreateTestSelectField(label, name, value, /*autocomplete=*/"", values,
                               contents);
}

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) {
  return CreateTestSelectField(label, name, value, autocomplete, values,
                               contents,
                               /*type=*/FormControlType::kSelectOne);
}

FormFieldData CreateTestSelectField(const std::vector<const char*>& values) {
  return CreateTestSelectField(/*label=*/"", /*name=*/"", /*value=*/"",
                               /*autocomplete=*/"", values,
                               /*contents=*/values);
}

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) {
  CHECK(type == FormControlType::kSelectOne);
  FormFieldData field = CreateTestFormField(label, name, value, type);
  field.set_autocomplete_attribute(std::string(autocomplete));
  field.set_parsed_autocomplete(ParseAutocompleteAttribute(autocomplete));

  std::vector<SelectOption> options;
  options.reserve(values.size());
  for (const auto [option_value, option_content] :
       base::zip(values, contents)) {
    options.push_back({
        .value = base::UTF8ToUTF16(option_value),
        .text = base::UTF8ToUTF16(option_content),
    });
  }
  field.set_options(std::move(options));
  return field;
}

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) {
  // Fill the base attributes.
  FormFieldData field =
      CreateTestFormField(label, name, value, FormControlType::kInputText);
  std::vector<SelectOption> datalist_options;
  datalist_options.reserve(values.size());
  for (auto [entry_value, entry_label] : base::zip(values, labels)) {
    datalist_options.push_back({.value = base::UTF8ToUTF16(entry_value),
                                .text = base::UTF8ToUTF16(entry_label)});
  }
  field.set_datalist_options(std::move(datalist_options));
  return field;
}

FormData CreateTestPersonalInformationFormData() {
  FormData form = ConstructFormWithNameRenderIdAndProtocol(/*is_https=*/true);
  form.set_fields({CreateTestFormField("First Name", "firstname", "",
                                       FormControlType::kInputText),
                   CreateTestFormField("Middle Name", "middlename", "",
                                       FormControlType::kInputText),
                   CreateTestFormField("Last Name", "lastname", "",
                                       FormControlType::kInputText),
                   CreateTestFormField("Email", "email", "",
                                       FormControlType::kInputEmail)});
  return form;
}

FormData CreateTestCreditCardFormData(bool is_https,
                                      bool use_month_type,
                                      bool split_names) {
  FormData form = ConstructFormWithNameRenderIdAndProtocol(is_https);

  if (split_names) {
    test_api(form).Append(
        CreateTestFormField("First Name on Card", "firstnameoncard", "",
                            FormControlType::kInputText, "cc-given-name"));
    test_api(form).Append(
        CreateTestFormField("Last Name on Card", "lastnameoncard", "",
                            FormControlType::kInputText, "cc-family=name"));
  } else {
    test_api(form).Append(CreateTestFormField("Name on Card", "nameoncard", "",
                                              FormControlType::kInputText));
  }
  test_api(form).Append(CreateTestFormField("Card Number", "cardnumber", "",
                                            FormControlType::kInputText));
  if (use_month_type) {
    test_api(form).Append(CreateTestFormField("Expiration Date", "ccmonth", "",
                                              FormControlType::kInputMonth));
  } else {
    test_api(form).Append(CreateTestFormField("Expiration Date", "ccmonth", "",
                                              FormControlType::kInputText));
    test_api(form).Append(
        CreateTestFormField("", "ccyear", "", FormControlType::kInputText));
  }
  test_api(form).Append(
      CreateTestFormField("CVC", "cvc", "", FormControlType::kInputText));
  return form;
}

FormData CreateTestIbanFormData(std::string_view value, bool is_https) {
  FormData form = ConstructFormWithNameRenderIdAndProtocol(is_https);
  form.set_fields({CreateTestFormField("IBAN Value:", "iban_value", value,
                                       FormControlType::kInputText)});
  return form;
}

FormData CreateTestLoyaltyCardFormData() {
  FormData form = ConstructFormWithNameRenderIdAndProtocol(/*is_https=*/true);
  form.set_fields(
      {CreateTestFormField("Your loyalty card:", "loyalty_card", /*value=*/"",
                           FormControlType::kInputText)});
  return form;
}

FormData CreateTestMerchantPromoCodeFormData() {
  FormData form = ConstructFormWithNameRenderIdAndProtocol(/*is_https=*/true);
  form.set_fields(
      {CreateTestFormField("Promo code", "promocode", /*value=*/"",
                           FormControlType::kInputText)});
  return form;
}

FormData CreateTestPasswordFormData() {
  std::vector<FormFieldData> fields;
  fields.push_back(
      CreateTestFormField(/*label=*/"Username:", /*name=*/"username",
                          /*value=*/"", FormControlType::kInputText));
  fields.push_back(
      CreateTestFormField(/*label=*/"Password:", /*name=*/"password",
                          /*value=*/"", FormControlType::kInputPassword));
  FormData form;
  form.set_url(GURL("https://www.foo.com"));
  form.set_fields(std::move(fields));
  return form;
}

[[nodiscard]] FormData CreateTestSignupFormData() {
  FormData form = CreateTestPasswordFormData();
  std::vector<FormFieldData> fields = form.ExtractFields();
  fields.push_back(CreateTestFormField(
      /*label=*/"Password (confirm)", /*name=*/"password_2",
      /*value=*/"", FormControlType::kInputPassword));
  form.set_fields(std::move(fields));
  return form;
}

FormData CreateTestUnclassifiedFormData() {
  FormData form;
  form.set_url(GURL("https://www.foo.com"));
  form.set_fields({CreateTestFormField(
      "unclassifiable label", "unclassifiable name", "unclassifiable value",
      FormControlType::kInputText)});
  return form;
}

}  // namespace autofill::test