File: autofill_feedback_data_unittest.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 (245 lines) | stat: -rw-r--r-- 9,271 bytes parent folder | download | duplicates (6)
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
// 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/browser/autofill_feedback_data.h"

#include "base/json/json_reader.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "components/autofill/core/browser/foundations/test_autofill_client.h"
#include "components/autofill/core/browser/foundations/test_autofill_driver.h"
#include "components/autofill/core/browser/foundations/test_browser_autofill_manager.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_test_utils.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_data_test_api.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace autofill {
namespace {

using test::CreateTestFormField;

constexpr char kExpectedFeedbackDataJSON[] = R"({
   "formStructures": [ {
      "formSignature": "4232380759432074174",
      "hostFrame": "00000000000181CD000000000000A8CA",
      "idAttribute": "",
      "mainFrameUrl": "https://myform_root.com",
      "nameAttribute": "",
      "rendererId": "11",
      "sourceUrl": "https://myform.com",
      "fields": [ {
         "autocompleteAttribute": "cc-given-name",
         "fieldSignature": "3879476562",
         "fieldType": "NAME_FIRST",
         "heuristicType": "CREDIT_CARD_NAME_FIRST",
         "hostFormSignature": "0",
         "htmlType": "HTML_TYPE_CREDIT_CARD_NAME_FIRST",
         "idAttribute": "",
         "isEmpty": true,
         "isFocusable": true,
         "isVisible": true,
         "labelAttribute": "First Name on Card",
         "parseableNameAttribute": "",
         "placeholderAttribute": "",
         "rank": "0",
         "rankInHostForm": "0",
         "rankInHostFormSignatureGroup": "0",
         "rankInSignatureGroup": "0",
         "section": "firstnameoncard_0_11",
         "serverType": "NO_SERVER_DATA",
         "serverTypeIsOverride": false
      }, {
         "autocompleteAttribute": "cc-family-name",
         "fieldSignature": "3213606822",
         "fieldType": "NAME_LAST",
         "heuristicType": "CREDIT_CARD_NAME_LAST",
         "hostFormSignature": "0",
         "htmlType": "HTML_TYPE_CREDIT_CARD_NAME_LAST",
         "idAttribute": "",
         "isEmpty": true,
         "isFocusable": true,
         "isVisible": true,
         "labelAttribute": "Last Name on Card",
         "parseableNameAttribute": "",
         "placeholderAttribute": "",
         "rank": "1",
         "rankInHostForm": "1",
         "rankInHostFormSignatureGroup": "0",
         "rankInSignatureGroup": "0",
         "section": "firstnameoncard_0_11",
         "serverType": "NO_SERVER_DATA",
         "serverTypeIsOverride": false
      }, {
         "autocompleteAttribute": "",
         "fieldSignature": "1029417091",
         "fieldType": "EMAIL_ADDRESS",
         "heuristicType": "EMAIL_ADDRESS",
         "hostFormSignature": "0",
         "htmlType": "HTML_TYPE_UNSPECIFIED",
         "idAttribute": "",
         "isEmpty": true,
         "isFocusable": true,
         "isVisible": true,
         "labelAttribute": "Email",
         "parseableNameAttribute": "",
         "placeholderAttribute": "",
         "rank": "2",
         "rankInHostForm": "2",
         "rankInHostFormSignatureGroup": "0",
         "rankInSignatureGroup": "0",
         "section": "email_0_13",
         "serverType": "NO_SERVER_DATA",
         "serverTypeIsOverride": false
      } ]
   } ]
})";

FormData CreateFeedbackTestFormData() {
  FormData form;
  form.set_host_frame(test::MakeLocalFrameToken(test::RandomizeFrame(false)));
  form.set_renderer_id(test::MakeFormRendererId());
  form.set_name(u"MyForm");
  form.set_url(GURL("https://myform.com/form.html"));
  form.set_action(GURL("https://myform.com/submit.html"));
  form.set_main_frame_origin(
      url::Origin::Create(GURL("https://myform_root.com/form.html")));
  form.set_fields(
      {CreateTestFormField("First Name on Card", "firstnameoncard", "",
                           FormControlType::kInputText, "cc-given-name"),
       CreateTestFormField("Last Name on Card", "lastnameoncard", "",
                           FormControlType::kInputText, "cc-family-name"),
       CreateTestFormField("Email", "email", "",
                           FormControlType::kInputEmail)});
  for (FormFieldData& field : test_api(form).fields()) {
    field.set_host_frame(form.host_frame());
  }
  return form;
}

class AutofillFeedbackDataUnitTest : public testing::Test {
 protected:
  AutofillFeedbackDataUnitTest() = default;
  void SetUp() override {
    autofill_driver_ = std::make_unique<TestAutofillDriver>(&autofill_client_);
    browser_autofill_manager_ =
        std::make_unique<TestBrowserAutofillManager>(autofill_driver_.get());
  }

  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  test::AutofillUnitTestEnvironment autofill_test_environment_;
  TestAutofillClient autofill_client_;
  std::unique_ptr<TestAutofillDriver> autofill_driver_;
  std::unique_ptr<TestBrowserAutofillManager> browser_autofill_manager_;
};

TEST_F(AutofillFeedbackDataUnitTest, CreatesCompleteReport) {
  FormData form = CreateFeedbackTestFormData();
  browser_autofill_manager_->OnFormsSeen(
      /*updated_forms=*/{form},
      /*removed_forms=*/{});

  base::Value::Dict autofill_feedback_data =
      data_logs::FetchAutofillFeedbackData(browser_autofill_manager_.get());

  ASSERT_OK_AND_ASSIGN(
      auto expected_data,
      base::JSONReader::ReadAndReturnValueWithError(
          kExpectedFeedbackDataJSON,
          base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS));
  ASSERT_TRUE(expected_data.is_dict());
  EXPECT_EQ(autofill_feedback_data, expected_data.GetDict());
}

TEST_F(AutofillFeedbackDataUnitTest, IncludesLastAutofillEventLogEntry) {
  FormData form = CreateFeedbackTestFormData();
  FormFieldData field = form.fields()[0];
  browser_autofill_manager_->OnFormsSeen(
      /*updated_forms=*/{form},
      /*removed_forms=*/{});

  // Simulates an autofill event.
  Suggestion suggestion(u"TestValue", SuggestionType::kIbanEntry);
  browser_autofill_manager_->OnSingleFieldSuggestionSelected(
      suggestion, form.global_id(), field.global_id());

  ASSERT_OK_AND_ASSIGN(
      auto expected_data,
      base::JSONReader::ReadAndReturnValueWithError(
          kExpectedFeedbackDataJSON,
          base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS));
  ASSERT_TRUE(expected_data.is_dict());

  // Update the expected data with a last_autofill_event entry.
  base::Value::Dict last_autofill_event;
  last_autofill_event.Set("associatedCountry", "");
  last_autofill_event.Set("type", "SingleFieldFillerIban");
  expected_data.GetDict().Set("lastAutofillEvent",
                              std::move(last_autofill_event));

  EXPECT_EQ(
      data_logs::FetchAutofillFeedbackData(browser_autofill_manager_.get()),
      expected_data.GetDict());
}

TEST_F(AutofillFeedbackDataUnitTest,
       NotIncludeLastAutofillEventIfExceedTimeLimit) {
  FormData form = CreateFeedbackTestFormData();
  const FormFieldData& field = form.fields()[0];
  browser_autofill_manager_->OnFormsSeen(
      /*updated_forms=*/{form},
      /*removed_forms=*/{});

  // Simulates an autofill event.
  Suggestion suggestion(u"TestValue", SuggestionType::kIbanEntry);
  browser_autofill_manager_->OnSingleFieldSuggestionSelected(
      suggestion, form.global_id(), field.global_id());

  // Advance the clock 4 minutes should disregard the last autofill event log.
  task_environment_.FastForwardBy(base::Minutes(4));

  // Expected data does not contain the last_autofill_event entry.
  ASSERT_OK_AND_ASSIGN(
      auto expected_data,
      base::JSONReader::ReadAndReturnValueWithError(
          kExpectedFeedbackDataJSON,
          base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS));
  ASSERT_TRUE(expected_data.is_dict());

  EXPECT_EQ(
      data_logs::FetchAutofillFeedbackData(browser_autofill_manager_.get()),
      expected_data.GetDict());
}

TEST_F(AutofillFeedbackDataUnitTest, IncludesExtraLogs) {
  FormData form = CreateFeedbackTestFormData();
  browser_autofill_manager_->OnFormsSeen(
      /*updated_forms=*/{form},
      /*removed_forms=*/{});

  base::Value::Dict extra_logs;
  extra_logs.Set("triggerFormSignature", "123");
  extra_logs.Set("triggerFieldSignature", "456");

  base::Value::Dict autofill_feedback_data =
      data_logs::FetchAutofillFeedbackData(browser_autofill_manager_.get(),
                                           extra_logs.Clone());

  ASSERT_OK_AND_ASSIGN(
      auto expected_data,
      base::JSONReader::ReadAndReturnValueWithError(
          kExpectedFeedbackDataJSON,
          base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS));
  ASSERT_TRUE(expected_data.is_dict());
  // Include extra logs in the expected report.
  expected_data.GetDict().Merge(std::move(extra_logs));
  EXPECT_EQ(autofill_feedback_data, expected_data.GetDict());
}

}  // namespace
}  // namespace autofill