File: votes_uploader_unittest.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie-proposed-updates
  • size: 6,193,184 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (336 lines) | stat: -rw-r--r-- 13,316 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 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/crowdsourcing/votes_uploader.h"

#include <memory>

#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/crowdsourcing/mock_autofill_crowdsourcing_manager.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/foundations/autofill_driver_factory.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/test_utils/autofill_test_utils.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_data_test_api.h"
#include "components/autofill/core/common/language_code.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::ElementsAre;
using testing::Return;
using testing::Truly;

namespace autofill {
namespace {

class AutofillVotesUploaderTest : public testing::Test {
 public:
  AutofillVotesUploaderTest() = default;
  ~AutofillVotesUploaderTest() override = default;

  void SetUp() override {
    autofill_client_.SetPrefs(test::PrefServiceForTesting());
    AddTestProfile();
  }

 protected:
  using enum AutofillDriver::LifecycleState;

  std::unique_ptr<FormStructure> CreateTestForm() {
    return std::make_unique<FormStructure>(test::CreateTestAddressFormData());
  }

  std::unique_ptr<FormStructure> CreateCreditCardForm() {
    return std::make_unique<FormStructure>(test::CreateTestCreditCardFormData(
        /*is_https=*/true, /*use_month_type=*/false));
  }

  void AddTestProfile() {
    autofill_client_.GetPersonalDataManager().address_data_manager().AddProfile(
        test::GetFullProfile());
  }

  void AddTestCreditCard() {
    autofill_client_.GetPersonalDataManager()
        .payments_data_manager()
        .AddCreditCard(test::GetCreditCard());
  }

  MockAutofillCrowdsourcingManager& GetCrowdsourcingManager() {
    return static_cast<MockAutofillCrowdsourcingManager&>(
        autofill_client_.GetCrowdsourcingManager());
  }

  auto QuitRunLoopAndReturnTrue(base::RunLoop& run_loop) {
    return [&run_loop](auto&&...) {
      run_loop.Quit();
      return true;
    };
  }

  void TriggerFrameStateChange(TestAutofillDriver& driver,
                               AutofillDriver::LifecycleState from,
                               AutofillDriver::LifecycleState to) {
    static_cast<AutofillDriverFactory::Observer&>(
        autofill_client_.GetVotesUploader())
        .OnAutofillDriverStateChanged(
            autofill_client_.GetAutofillDriverFactory(), driver, from, to);
  }

  std::unique_ptr<FormStructure> CreateTestFormWithFrame(
      const TestAutofillDriver& driver) {
    FormData form_data = test::CreateTestAddressFormData();
    form_data.set_host_frame(driver.GetFrameToken());
    return std::make_unique<FormStructure>(form_data);
  }

  bool MaybeStartVoteUploadProcess(std::unique_ptr<FormStructure> form,
                                   bool observed_submission) {
    return autofill_client_.GetVotesUploader().MaybeStartVoteUploadProcess(
        std::move(form), observed_submission, LanguageCode("en"),
        base::TimeTicks::Now(), u"", ukm::kInvalidSourceId);
  }

  test::AutofillUnitTestEnvironment autofill_test_environment_;
  base::test::TaskEnvironment task_environment_;
  TestAutofillClient autofill_client_;
};

// Test basic vote upload process for form submission.
TEST_F(AutofillVotesUploaderTest, BasicVoteUpload) {
  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest)
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));

  EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateTestForm(),
                                          /*observed_submission=*/true));
  run_loop.Run();
}

// Test that vote upload returns false when no local profile data is available.
TEST_F(AutofillVotesUploaderTest, NoLocalProfileReturnsFalse) {
  autofill_client_.GetPersonalDataManager()
      .address_data_manager()
      .RemoveLocalProfilesModifiedBetween(base::Time::Min(), base::Time::Max());

  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest).Times(0);
  EXPECT_FALSE(MaybeStartVoteUploadProcess(CreateTestForm(),
                                           /*observed_submission=*/true));
  task_environment_.RunUntilIdle();
}

// Test that pending votes don't trigger immediate upload while submission votes
// do.
TEST_F(AutofillVotesUploaderTest, PendingVotesVsSubmissionVotes) {
  // Test pending vote (should not trigger immediate upload).
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest).Times(0);
  EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateTestForm(),
                                          /*observed_submission=*/false));
  task_environment_.RunUntilIdle();

  // Test submission vote (should trigger immediate upload).
  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest)
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));

  EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateTestForm(),
                                          /*observed_submission=*/true));
  run_loop.Run();
}

// Test vote upload for credit card forms.
TEST_F(AutofillVotesUploaderTest, CreditCardFormUpload) {
  AddTestCreditCard();

  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest)
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));

  EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateCreditCardForm(),
                                          /*observed_submission=*/true));
  run_loop.Run();
}

// Test that votes for distinct forms are uploaded separately.
TEST_F(AutofillVotesUploaderTest, DistinctFormsUploadedSeparately) {
  std::unique_ptr<FormStructure> form1 = CreateTestForm();
  std::unique_ptr<FormStructure> form2 = CreateCreditCardForm();
  ASSERT_NE(form1->form_signature(), form2->form_signature());

  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest)
      .Times(2)
      .WillOnce(Return(true))
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));

  // Enqueue votes for two different forms.
  EXPECT_TRUE(MaybeStartVoteUploadProcess(std::move(form1),
                                          /*observed_submission=*/true));
  EXPECT_TRUE(MaybeStartVoteUploadProcess(std::move(form2),
                                          /*observed_submission=*/true));
  run_loop.Run();
}

// Test that only the most recent vote for the same form is uploaded.
TEST_F(AutofillVotesUploaderTest, MostRecentVoteForSameFormWins) {
  TestAutofillDriver test_driver(&autofill_client_);

  FormData form_data = test::CreateTestAddressFormData();
  form_data.set_host_frame(test_driver.GetFrameToken());

  auto form1 = std::make_unique<FormStructure>(form_data);
  auto form2 = std::make_unique<FormStructure>(form_data);
  form2->fields()[0]->set_value(
      test::GetFullProfile().GetInfo(NAME_FULL, "en"));

  ASSERT_EQ(form1->form_signature(), form2->form_signature());
  ASSERT_TRUE(form1->fields()[0]->value().empty());
  ASSERT_FALSE(form2->fields()[0]->value().empty());

  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(),
              StartUploadRequest(
                  ElementsAre(Truly([](const AutofillUploadContents& upload) {
                    return upload.field_data_size() >= 1 &&
                           upload.field_data(0).autofill_type_size() == 1 &&
                           upload.field_data(0).autofill_type(0) == NAME_FULL;
                  })),
                  _, _))
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));

  // Enqueue first pending vote for a form.
  EXPECT_TRUE(MaybeStartVoteUploadProcess(std::move(form1),
                                          /*observed_submission=*/false));
  // Enqueue second pending vote for the same form with field value set.
  EXPECT_TRUE(MaybeStartVoteUploadProcess(std::move(form2),
                                          /*observed_submission=*/false));

  // Flush the votes.
  TriggerFrameStateChange(test_driver, kActive, kInactive);
  run_loop.Run();
}

// Test that pending votes don't trigger immediate upload.
TEST_F(AutofillVotesUploaderTest, PendingVotesNoImmediateUpload) {
  // Test that pending votes are stored without immediate upload.
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest).Times(0);

  for (int i = 0; i < 3; ++i) {
    EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateTestForm(),
                                            /*observed_submission=*/false));
  }
  task_environment_.RunUntilIdle();
}

// Test that pending votes are flushed when a submission happens.
TEST_F(AutofillVotesUploaderTest, PendingVotesFlushedOnSubmission) {
  // Store some pending votes.
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest).Times(0);

  for (int i = 0; i < 3; ++i) {
    EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateTestForm(),
                                            /*observed_submission=*/false));
  }
  task_environment_.RunUntilIdle();

  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest)
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));
  // A submission should trigger upload of pending votes.
  MaybeStartVoteUploadProcess(CreateTestForm(),
                              /*observed_submission=*/true);
  run_loop.Run();
}

// Test that pending votes are flushed when frame becomes inactive.
TEST_F(AutofillVotesUploaderTest, PendingVotesFlushedOnFrameInactive) {
  TestAutofillDriver test_driver(&autofill_client_);

  // Store a pending vote for a specific frame token that matches our test
  // driver.
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest).Times(0);
  // Create form and associate it with the test driver's frame.
  EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateTestFormWithFrame(test_driver),
                                          /*observed_submission=*/false));
  task_environment_.RunUntilIdle();

  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest)
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));
  // Trigger frame inactive state change - this should flush pending votes.
  TriggerFrameStateChange(test_driver, kActive, kInactive);
  run_loop.Run();
}

// Test that pending votes are flushed when frame is reset.
TEST_F(AutofillVotesUploaderTest, PendingVotesFlushedOnFrameReset) {
  TestAutofillDriver test_driver(&autofill_client_);

  // Store a pending vote.
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest).Times(0);
  EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateTestFormWithFrame(test_driver),
                                          /*observed_submission=*/false));
  task_environment_.RunUntilIdle();

  // First transition to PendingReset should not flush votes.
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest).Times(0);
  TriggerFrameStateChange(test_driver, kActive, kPendingReset);
  task_environment_.RunUntilIdle();

  // Then transition out of PendingReset - this should flush pending votes.
  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest)
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));
  TriggerFrameStateChange(test_driver, kPendingReset, kActive);
  run_loop.Run();
}

// Test that pending votes are flushed when frame is deleted.
TEST_F(AutofillVotesUploaderTest, PendingVotesFlushedOnFrameDeletion) {
  TestAutofillDriver test_driver(&autofill_client_);

  // Store a pending vote.
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest).Times(0);
  EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateTestFormWithFrame(test_driver),
                                          /*observed_submission=*/false));
  task_environment_.RunUntilIdle();

  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest)
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));
  // Trigger frame deletion - this should flush pending votes.
  TriggerFrameStateChange(test_driver, kActive, kPendingDeletion);
  run_loop.Run();
}

// Test that multiple pending votes can be added and queue functionality works.
TEST_F(AutofillVotesUploaderTest, PendingVotesQueueFunctionality) {
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest).Times(0);

  // Add several pending votes to verify they are queued properly.
  for (int i = 0; i < 8; ++i) {
    EXPECT_TRUE(MaybeStartVoteUploadProcess(CreateTestForm(),
                                            /*observed_submission=*/false));
  }
  task_environment_.RunUntilIdle();

  // Verify that a submission still triggers upload.
  base::RunLoop run_loop;
  EXPECT_CALL(GetCrowdsourcingManager(), StartUploadRequest)
      .WillOnce(QuitRunLoopAndReturnTrue(run_loop));

  MaybeStartVoteUploadProcess(CreateTestForm(),
                              /*observed_submission=*/true);
  run_loop.Run();
}

}  // namespace
}  // namespace autofill