File: autofill_counter_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (519 lines) | stat: -rw-r--r-- 17,818 bytes parent folder | download | duplicates (5)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright 2015 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/browsing_data/core/counters/autofill_counter.h"

#include <array>
#include <memory>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "base/uuid.h"
#include "chrome/browser/autofill/autofill_entity_data_manager_factory.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/webdata_services/web_data_service_factory.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/data_manager/addresses/address_data_manager.h"
#include "components/autofill/core/browser/data_manager/autofill_ai/entity_data_manager.h"
#include "components/autofill/core/browser/data_manager/autofill_ai/entity_data_manager_test_utils.h"
#include "components/autofill/core/browser/data_manager/payments/payments_data_manager.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager_test_utils.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/data_model/payments/credit_card.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/browser/test_utils/test_autofill_clock.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/browsing_data/core/browsing_data_utils.h"
#include "components/browsing_data/core/counters/browsing_data_counter.h"
#include "components/browsing_data/core/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"

namespace {

using browsing_data::AutofillCounter;
typedef base::test::TestFuture<
    std::unique_ptr<browsing_data::BrowsingDataCounter::Result>>
    CounterFuture;
class AutofillCounterTest : public InProcessBrowserTest {
 public:
  AutofillCounterTest() = default;

  AutofillCounterTest(const AutofillCounterTest&) = delete;
  AutofillCounterTest& operator=(const AutofillCounterTest&) = delete;

  ~AutofillCounterTest() override = default;

  void SetUpOnMainThread() override {
    personal_data_manager_ =
        autofill::PersonalDataManagerFactory::GetForBrowserContext(
            browser()->profile());
    web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile(
        browser()->profile(), ServiceAccessType::IMPLICIT_ACCESS);

    SetAutofillDeletionPref(true);
    SetDeletionPeriodPref(browsing_data::TimePeriod::ALL_TIME);
  }

  void TearDownOnMainThread() override {
    // Release our refs to let browser tear down of services complete in the
    // same order as usual.
    personal_data_manager_ = nullptr;
    web_data_service_ = nullptr;
  }

  browsing_data::AutofillCounter GetCounter() {
    return {GetPersonalDataManager(), GetWebDataService(),
            GetEntityDataManager(), /*sync_service=*/nullptr};
  }

  // Autocomplete suggestions --------------------------------------------------

  void AddAutocompleteSuggestion(const std::string& name,
                                 const std::string& value) {
    autofill::FormFieldData field;
    field.set_name(base::ASCIIToUTF16(name));
    field.set_value(base::ASCIIToUTF16(value));

    std::vector<autofill::FormFieldData> form_fields;
    form_fields.push_back(field);
    web_data_service_->AddFormFields(form_fields);
  }

  void RemoveAutocompleteSuggestion(const std::string& name,
                                    const std::string& value) {
    web_data_service_->RemoveFormValueForElementName(
        base::ASCIIToUTF16(name),
        base::ASCIIToUTF16(value));
  }

  void ClearAutocompleteSuggestions() {
    web_data_service_->RemoveFormElementsAddedBetween(
        base::Time(), base::Time::Max());
  }

  // Credit cards --------------------------------------------------------------

  void AddCreditCard(const char* card_number,
                     const char* exp_month,
                     const char* exp_year,
                     const char* billing_address_id) {
    autofill::CreditCard card;
    autofill::test::SetCreditCardInfo(&card, nullptr, card_number, exp_month,
                                      exp_year, billing_address_id);
    credit_card_ids_.push_back(card.guid());
    personal_data_manager_->payments_data_manager().AddCreditCard(card);
    autofill::PersonalDataChangedWaiter(*personal_data_manager_).Wait();
  }

  void RemoveLastCreditCard() {
    personal_data_manager_->payments_data_manager().RemoveByGUID(
        credit_card_ids_.back());
    autofill::PersonalDataChangedWaiter(*personal_data_manager_).Wait();
    credit_card_ids_.pop_back();
  }

  // Addresses -----------------------------------------------------------------

  void AddAddress(const std::string& name,
                  const std::string& surname,
                  const std::string& address) {
    autofill::AutofillProfile profile(
        autofill::i18n_model_definition::kLegacyHierarchyCountryCode);
    std::string id = base::Uuid::GenerateRandomV4().AsLowercaseString();
    address_ids_.push_back(id);
    profile.set_guid(id);
    profile.SetInfo(autofill::NAME_FIRST, base::ASCIIToUTF16(name), "en-US");
    profile.SetInfo(autofill::NAME_LAST, base::ASCIIToUTF16(surname), "en-US");
    profile.SetInfo(autofill::ADDRESS_HOME_LINE1, base::ASCIIToUTF16(address),
                    "en-US");
    personal_data_manager_->address_data_manager().AddProfile(profile);
    autofill::PersonalDataChangedWaiter(*personal_data_manager_).Wait();
  }

  void RemoveLastAddress() {
    personal_data_manager_->address_data_manager().RemoveProfile(
        address_ids_.back());
    autofill::PersonalDataChangedWaiter(*personal_data_manager_).Wait();
    address_ids_.pop_back();
  }

  // Other autofill utils ------------------------------------------------------

  void ClearCreditCardsAndAddresses() {
    while (!credit_card_ids_.empty()) {
      RemoveLastCreditCard();
    }
    while (!address_ids_.empty()) {
      RemoveLastAddress();
    }
  }

  // Other utils ---------------------------------------------------------------

  void SetAutofillDeletionPref(bool value) {
    browser()->profile()->GetPrefs()->SetBoolean(
        browsing_data::prefs::kDeleteFormData, value);
  }

  void SetDeletionPeriodPref(browsing_data::TimePeriod period) {
    browser()->profile()->GetPrefs()->SetInteger(
        browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period));
  }

  autofill::EntityDataManager* GetEntityDataManager() {
    return autofill::AutofillEntityDataManagerFactory::GetForProfile(
        browser()->profile());
  }

  autofill::PersonalDataManager* GetPersonalDataManager() {
    return personal_data_manager_;
  }

  scoped_refptr<autofill::AutofillWebDataService> GetWebDataService() {
    return web_data_service_;
  }

  // Result retrieval ---------------------------------------------

  browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() {
    return num_suggestions_;
  }

  browsing_data::BrowsingDataCounter::ResultInt GetNumCreditCards() {
    return num_credit_cards_;
  }

  browsing_data::BrowsingDataCounter::ResultInt GetNumAddresses() {
    return num_addresses_;
  }

  browsing_data::BrowsingDataCounter::ResultInt GetNumEntities() {
    return num_entities_;
  }

  browsing_data::AutofillCounter::ResultInt GetCounterValue() {
    return counter_value_;
  }

  void WaitForResult() {
    std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result =
        future.Take();
    while (!result->Finished()) {
      future.Clear();
      result = future.Take();
    }

    AutofillCounter::AutofillResult* autofill_result =
        static_cast<AutofillCounter::AutofillResult*>(result.get());
    counter_value_ = autofill_result->Value();
    num_credit_cards_ = autofill_result->num_credit_cards();
    num_addresses_ = autofill_result->num_addresses();
    num_entities_ = autofill_result->num_entities();
  }

 protected:
  CounterFuture future;

 private:
  base::test::ScopedFeatureList feature_list_{
      autofill::features::kAutofillAiWithDataSchema};
  autofill::test::AutofillBrowserTestEnvironment autofill_test_environment_;
  std::vector<std::string> credit_card_ids_;
  std::vector<std::string> address_ids_;

  std::unique_ptr<base::RunLoop> run_loop_;

  raw_ptr<autofill::PersonalDataManager> personal_data_manager_;
  scoped_refptr<autofill::AutofillWebDataService> web_data_service_;

  browsing_data::BrowsingDataCounter::ResultInt counter_value_;
  browsing_data::BrowsingDataCounter::ResultInt num_suggestions_;
  browsing_data::BrowsingDataCounter::ResultInt num_credit_cards_;
  browsing_data::BrowsingDataCounter::ResultInt num_addresses_;
  browsing_data::BrowsingDataCounter::ResultInt num_entities_;
};

// Tests that we count the correct number of autocomplete suggestions.
IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) {
  browsing_data::AutofillCounter counter = GetCounter();
  counter.Init(browser()->profile()->GetPrefs(),
               browsing_data::ClearBrowsingDataTab::ADVANCED,
               future.GetRepeatingCallback());
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(0, GetCounterValue());

  AddAutocompleteSuggestion("email", "example@example.com");
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(1, GetCounterValue());

  AddAutocompleteSuggestion("tel", "+123456789");
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(2, GetCounterValue());

  AddAutocompleteSuggestion("tel", "+987654321");
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(3, GetCounterValue());

  RemoveAutocompleteSuggestion("email", "example@example.com");
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(2, GetCounterValue());

  ClearAutocompleteSuggestions();
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(0, GetCounterValue());
}

IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Entities) {
  browsing_data::AutofillCounter counter = GetCounter();
  counter.Init(browser()->profile()->GetPrefs(),
               browsing_data::ClearBrowsingDataTab::ADVANCED,
               future.GetRepeatingCallback());
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(GetNumEntities(), 0);

  autofill::EntityInstance passport =
      autofill::test::GetPassportEntityInstance();
  GetEntityDataManager()->AddOrUpdateEntityInstance(passport);
  autofill::EntityDataChangedWaiter(GetEntityDataManager()).Wait();
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(GetNumEntities(), 1);

  autofill::EntityInstance drivers_license =
      autofill::test::GetDriversLicenseEntityInstance();
  GetEntityDataManager()->AddOrUpdateEntityInstance(drivers_license);
  autofill::EntityDataChangedWaiter(GetEntityDataManager()).Wait();
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(GetNumEntities(), 2);

  GetEntityDataManager()->RemoveEntityInstance(passport.guid());
  autofill::EntityDataChangedWaiter(GetEntityDataManager()).Wait();
  counter.Restart();
  WaitForResult();
  EXPECT_EQ(GetNumEntities(), 1);
}

// Tests that we count the correct number of credit cards.
IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) {
  Profile* profile = browser()->profile();
  browsing_data::AutofillCounter counter = GetCounter();

  counter.Init(profile->GetPrefs(),
               browsing_data::ClearBrowsingDataTab::ADVANCED,
               future.GetRepeatingCallback());
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(0, GetNumCreditCards());

  AddCreditCard("0000-0000-0000-0000", "1", "2015", "1");
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(1, GetNumCreditCards());

  AddCreditCard("0123-4567-8910-1112", "10", "2015", "1");
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(2, GetNumCreditCards());

  AddCreditCard("1211-1098-7654-3210", "10", "2030", "1");
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(3, GetNumCreditCards());

  RemoveLastCreditCard();
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(2, GetNumCreditCards());

  ClearCreditCardsAndAddresses();
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(0, GetNumCreditCards());
}

// Tests that we count the correct number of addresses.
IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Addresses) {
  Profile* profile = browser()->profile();
  browsing_data::AutofillCounter counter = GetCounter();

  counter.Init(profile->GetPrefs(),
               browsing_data::ClearBrowsingDataTab::ADVANCED,
               future.GetRepeatingCallback());
  counter.Restart();

  WaitForResult();
  EXPECT_EQ(0, GetNumAddresses());

  AddAddress("John", "Doe", "Main Street 12345");
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(1, GetNumAddresses());

  AddAddress("Jane", "Smith", "Main Street 12346");
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(2, GetNumAddresses());

  AddAddress("John", "Smith", "Side Street 47");
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(3, GetNumAddresses());

  RemoveLastAddress();
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(2, GetNumAddresses());

  ClearCreditCardsAndAddresses();
  counter.Restart();
  WaitForResult();

  EXPECT_EQ(0, GetNumAddresses());
}

// Tests that we return the correct complex result when counting more than
// one type of items.
IN_PROC_BROWSER_TEST_F(AutofillCounterTest, ComplexResult) {
  AddAutocompleteSuggestion("email", "example@example.com");
  AddAutocompleteSuggestion("zip", "12345");
  AddAutocompleteSuggestion("tel", "+123456789");
  AddAutocompleteSuggestion("tel", "+987654321");
  AddAutocompleteSuggestion("city", "Munich");

  AddCreditCard("0000-0000-0000-0000", "1", "2015", "1");
  AddCreditCard("1211-1098-7654-3210", "10", "2030", "1");

  AddAddress("John", "Doe", "Main Street 12345");
  AddAddress("Jane", "Smith", "Main Street 12346");
  AddAddress("John", "Smith", "Side Street 47");

  Profile* profile = browser()->profile();
  browsing_data::AutofillCounter counter = GetCounter();

  counter.Init(profile->GetPrefs(),
               browsing_data::ClearBrowsingDataTab::ADVANCED,
               future.GetRepeatingCallback());
  counter.Restart();

  WaitForResult();

  EXPECT_EQ(5, GetCounterValue());
  EXPECT_EQ(2, GetNumCreditCards());
  EXPECT_EQ(3, GetNumAddresses());
}

// Tests that the counting respects time ranges.
IN_PROC_BROWSER_TEST_F(AutofillCounterTest, TimeRanges) {
  autofill::TestAutofillClock test_clock;
  const base::Time kTime1 = base::Time::FromSecondsSinceUnixEpoch(25);
  test_clock.SetNow(kTime1);
  AddAutocompleteSuggestion("email", "example@example.com");
  AddCreditCard("0000-0000-0000-0000", "1", "2015", "1");
  AddAddress("John", "Doe", "Main Street 12345");
  base::ThreadPoolInstance::Get()->FlushForTesting();

  const base::Time kTime2 = kTime1 + base::Seconds(10);
  test_clock.SetNow(kTime2);
  AddCreditCard("0123-4567-8910-1112", "10", "2015", "1");
  AddAddress("Jane", "Smith", "Main Street 12346");
  AddAddress("John", "Smith", "Side Street 47");
  base::ThreadPoolInstance::Get()->FlushForTesting();

  const base::Time kTime3 = kTime2 + base::Seconds(10);
  test_clock.SetNow(kTime3);
  AddAutocompleteSuggestion("tel", "+987654321");
  AddCreditCard("1211-1098-7654-3210", "10", "2030", "1");
  base::ThreadPoolInstance::Get()->FlushForTesting();

  // Test the results for different starting points.
  struct TestCase {
    const base::Time period_start;
    const browsing_data::BrowsingDataCounter::ResultInt
        expected_num_suggestions;
    const browsing_data::BrowsingDataCounter::ResultInt
        expected_num_credit_cards;
    const browsing_data::BrowsingDataCounter::ResultInt expected_num_addresses;
  };
  auto test_cases = std::to_array<TestCase>({
      {base::Time(), 2, 3, 3},
      {kTime1, 2, 3, 3},
      {kTime2, 1, 2, 2},
      {kTime3, 1, 1, 0},
  });

  Profile* profile = browser()->profile();
  browsing_data::AutofillCounter counter = GetCounter();

  counter.Init(profile->GetPrefs(),
               browsing_data::ClearBrowsingDataTab::ADVANCED,
               future.GetRepeatingCallback());

  for (size_t i = 0; i < std::size(test_cases); i++) {
    SCOPED_TRACE(base::StringPrintf("Test case %zu", i));
    const auto& test_case = test_cases[i];
    counter.SetPeriodStartForTesting(test_case.period_start);
    counter.Restart();

    WaitForResult();

    EXPECT_EQ(test_case.expected_num_suggestions, GetCounterValue());
    EXPECT_EQ(test_case.expected_num_credit_cards, GetNumCreditCards());
    EXPECT_EQ(test_case.expected_num_addresses, GetNumAddresses());
    future.Clear();
  }

  // Test the results for different ending points and base::Time as start.
  counter.SetPeriodStartForTesting(base::Time());
  counter.SetPeriodEndForTesting(kTime2);
  counter.Restart();

  WaitForResult();
  EXPECT_EQ(1, GetCounterValue());
  EXPECT_EQ(1, GetNumCreditCards());
  EXPECT_EQ(1, GetNumAddresses());

  counter.SetPeriodEndForTesting(kTime3);
  counter.Restart();

  WaitForResult();

  EXPECT_EQ(1, GetCounterValue());
  EXPECT_EQ(2, GetNumCreditCards());
  EXPECT_EQ(3, GetNumAddresses());
}

}  // namespace