File: single_client_wallet_credential_sync_test.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 (618 lines) | stat: -rw-r--r-- 24,806 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
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/notreached.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/browser/sync/test/integration/fake_server_match_status_checker.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/wallet_helper.h"
#include "components/autofill/core/browser/data_manager/payments/payments_data_manager.h"
#include "components/autofill/core/browser/data_manager/payments/payments_data_manager_test_utils.h"
#include "components/autofill/core/browser/data_model/payments/credit_card.h"
#include "components/autofill/core/common/autofill_payments_features.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/features.h"
#include "components/sync/engine/loopback_server/persistent_tombstone_entity.h"
#include "content/public/test/browser_test.h"

namespace {

using autofill::PaymentsDataChangedWaiter;
using autofill::PaymentsDataManager;
using autofill::ServerCvc;
using syncer::kSyncAutofillWalletCredentialData;
using wallet_helper::CreateDefaultSyncWalletCard;
using wallet_helper::CreateDefaultSyncWalletCredential;
using wallet_helper::CreateSyncPaymentsCustomerData;
using wallet_helper::CreateSyncWalletCard;
using wallet_helper::CreateSyncWalletCredential;
using wallet_helper::ExpectDefaultWalletCredentialValues;
using wallet_helper::GetAccountWebDataService;
using wallet_helper::GetPaymentsDataManager;
using wallet_helper::GetProfileWebDataService;
using wallet_helper::kDefaultBillingAddressID;

// A helper class that waits for `AUTOFILL_WALLET_CREDENTIAL` to have the
// expected entries on the FakeServer.
class ServerCvcChecker : public fake_server::FakeServerMatchStatusChecker {
 public:
  explicit ServerCvcChecker(const size_t expected_count);
  ~ServerCvcChecker() override;
  ServerCvcChecker(const ServerCvcChecker&) = delete;
  ServerCvcChecker& operator=(const ServerCvcChecker&) = delete;

  // StatusChangeChecker overrides.
  bool IsExitConditionSatisfied(std::ostream* os) override;

 private:
  const size_t expected_count_;
};

ServerCvcChecker::ServerCvcChecker(const size_t expected_count)
    : expected_count_(expected_count) {}

ServerCvcChecker::~ServerCvcChecker() = default;

bool ServerCvcChecker::IsExitConditionSatisfied(std::ostream* os) {
  return fake_server()
             ->GetSyncEntitiesByDataType(syncer::AUTOFILL_WALLET_CREDENTIAL)
             .size() == expected_count_;
}

#if !BUILDFLAG(IS_CHROMEOS)
std::vector<std::unique_ptr<autofill::CreditCard>> GetServerCards(
    scoped_refptr<autofill::AutofillWebDataService> service) {
  base::test::TestFuture<WebDataServiceBase::Handle,
                         std::unique_ptr<WDTypedResult>>
      future;
  service->GetServerCreditCards(future.GetCallback());
  return static_cast<
             WDResult<std::vector<std::unique_ptr<autofill::CreditCard>>>&>(
             *future.Get<1>())
      .GetValue();
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

}  // namespace

class SingleClientWalletCredentialSyncTest : public SyncTest {
 public:
  SingleClientWalletCredentialSyncTest() : SyncTest(SINGLE_CLIENT) {
    features_.InitWithFeatures(
        /*enabled_features=*/{kSyncAutofillWalletCredentialData,
                              autofill::features::
                                  kAutofillEnableCvcStorageAndFilling},
        /*disabled_features=*/{});
  }

  ~SingleClientWalletCredentialSyncTest() override = default;

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

 protected:
  void WaitForNumberOfCards(size_t expected_count, PaymentsDataManager* paydm) {
    while (paydm->GetCreditCards().size() != expected_count ||
           paydm->HasPendingPaymentQueries()) {
      PaymentsDataChangedWaiter(paydm).Wait();
    }
  }

  void WaitForNoPaymentsCustomerData(PaymentsDataManager* paydm) {
    while (paydm->GetPaymentsCustomerData() != nullptr ||
           paydm->HasPendingPaymentQueries()) {
      PaymentsDataChangedWaiter(paydm).Wait();
    }
  }

  void WaitForCvcOnCard(PaymentsDataManager* paydm) {
    while (!IsCvcAvailableOnAnyCard(paydm)) {
      PaymentsDataChangedWaiter(paydm).Wait();
    }
  }

  bool IsCvcAvailableOnAnyCard(PaymentsDataManager* paydm) {
    for (const autofill::CreditCard* credit_card : paydm->GetCreditCards()) {
      if (!credit_card->cvc().empty()) {
        return true;
      }
    }
    return false;
  }

  void SetDefaultWalletCredentialOnFakeServer() {
    sync_pb::EntitySpecifics entity_specifics =
        CreateDefaultSyncWalletCredential().specifics();

    GetFakeServer()->InjectEntity(
        syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
            /*non_unique_name=*/"credential",
            entity_specifics.mutable_autofill_wallet_credential()
                ->instrument_id(),
            entity_specifics, /*creation_time=*/0, /*last_modified_time=*/0));
  }

  void SetWalletCredentialOnFakeServer(const ServerCvc& server_cvc) {
    sync_pb::EntitySpecifics entity_specifics =
        CreateSyncWalletCredential(server_cvc).specifics();

    GetFakeServer()->InjectEntity(
        syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
            /*non_unique_name=*/"credential",
            entity_specifics.mutable_autofill_wallet_credential()
                ->instrument_id(),
            entity_specifics, /*creation_time=*/1000,
            /*last_modified_time=*/1000));
  }

 private:
  base::test::ScopedFeatureList features_;
};

// Ensures that the `AUTOFILL_WALLET_CREDENTIAL` sync type is enabled by
// default.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest, EnabledByDefault) {
  ASSERT_TRUE(SetupSync());
  ASSERT_TRUE(GetSyncService(0)->GetActiveDataTypes().Has(
      syncer::AUTOFILL_WALLET_CREDENTIAL));
}

// ChromeOS does not support late signin after profile creation, so the test
// below does not apply.
#if !BUILDFLAG(IS_CHROMEOS)
// Verify card and CVC data is synced when the user signs in.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest,
                       DownloadCardCredential) {
  SetDefaultWalletCredentialOnFakeServer();
  GetFakeServer()->SetWalletData({CreateDefaultSyncWalletCard()});

  ASSERT_TRUE(SetupClients());
  ASSERT_TRUE(GetClient(0)->SignInPrimaryAccount());
  ASSERT_TRUE(GetClient(0)->AwaitSyncTransportActive());
  WaitForCvcOnCard(GetPaymentsDataManager(0));

  ASSERT_EQ(syncer::SyncService::TransportState::ACTIVE,
            GetSyncService(0)->GetTransportState());
  ASSERT_TRUE(GetSyncService(0)->GetActiveDataTypes().Has(
      syncer::AUTOFILL_WALLET_CREDENTIAL));

  scoped_refptr<autofill::AutofillWebDataService> profile_data =
      GetProfileWebDataService(0);
  ASSERT_NE(nullptr, profile_data);
  scoped_refptr<autofill::AutofillWebDataService> account_data =
      GetAccountWebDataService(0);
  ASSERT_NE(nullptr, account_data);

  // Check that no data is stored in the profile storage.
  EXPECT_EQ(0U, GetServerCards(profile_data).size());

  // Check that one card is stored in the account storage.
  EXPECT_EQ(1U, GetServerCards(account_data).size());

  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);

  std::vector<const autofill::CreditCard*> cards = paydm->GetCreditCards();
  ASSERT_EQ(1uL, cards.size());
  EXPECT_FALSE(cards[0]->cvc().empty());
  ExpectDefaultWalletCredentialValues(*cards[0]);
}

// Card and CVC data should get cleared from the database when the user signs
// out and different data should get downstreamed when the user signs in with a
// different account.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest,
                       ClearOnSignOutAndDownstreamOnSignIn) {
  SetDefaultWalletCredentialOnFakeServer();
  GetFakeServer()->SetWalletData({CreateDefaultSyncWalletCard()});

  ASSERT_TRUE(SetupClients());
  ASSERT_TRUE(GetClient(0)->SignInPrimaryAccount());
  ASSERT_TRUE(GetClient(0)->AwaitSyncTransportActive());
  WaitForCvcOnCard(GetPaymentsDataManager(0));

  ASSERT_EQ(syncer::SyncService::TransportState::ACTIVE,
            GetSyncService(0)->GetTransportState());
  ASSERT_TRUE(GetSyncService(0)->GetActiveDataTypes().Has(
      syncer::AUTOFILL_WALLET_CREDENTIAL));

  scoped_refptr<autofill::AutofillWebDataService> profile_data =
      GetProfileWebDataService(0);
  ASSERT_NE(nullptr, profile_data);
  scoped_refptr<autofill::AutofillWebDataService> account_data =
      GetAccountWebDataService(0);
  ASSERT_NE(nullptr, account_data);

  // Check that no data is stored in the profile storage.
  EXPECT_EQ(0U, GetServerCards(profile_data).size());

  // Check that one card is stored in the account storage.
  EXPECT_EQ(1U, GetServerCards(account_data).size());

  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);

  std::vector<const autofill::CreditCard*> cards = paydm->GetCreditCards();
  ASSERT_EQ(1uL, cards.size());
  EXPECT_FALSE(cards[0]->cvc().empty());
  ExpectDefaultWalletCredentialValues(*cards[0]);

  GetClient(0)->SignOutPrimaryAccount();
  WaitForNoPaymentsCustomerData(paydm);
  EXPECT_EQ(0U, GetServerCards(profile_data).size());
  EXPECT_EQ(0U, GetServerCards(account_data).size());

  // Verify that sync is stopped.
  ASSERT_EQ(syncer::SyncService::TransportState::DISABLED,
            GetSyncService(0)->GetTransportState());
  ASSERT_FALSE(GetSyncService(0)->GetActiveDataTypes().Has(
      syncer::AUTOFILL_WALLET_CREDENTIAL));

  // Set a different set of cards on the server, then sign in again (this is a
  // good enough approximation of signing in with a different Google account).
  GetFakeServer()->DeleteAllEntitiesForDataType(
      syncer::AUTOFILL_WALLET_CREDENTIAL);
  sync_pb::EntitySpecifics entity_specifics_2;
  *entity_specifics_2.mutable_autofill_wallet_credential() =
      *CreateSyncWalletCredential(
           ServerCvc{/*instrument_id=*/9, /*cvc=*/u"720",
                     /*last_updated_timestamp=*/base::Time::UnixEpoch() +
                         base::Milliseconds(50000)})
           .mutable_specifics()
           ->mutable_autofill_wallet_credential();

  GetFakeServer()->SetWalletData(
      {CreateSyncWalletCard(
           /*name=*/"new-card", /*last_four=*/"0002", kDefaultBillingAddressID,
           /*nickname=*/"", /*instrument_id=*/9),
       CreateSyncPaymentsCustomerData(
           /*customer_id=*/"different")});
  GetFakeServer()->InjectEntity(
      syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
          /*non_unique_name=*/"credential_2",
          entity_specifics_2.mutable_autofill_wallet_credential()
              ->instrument_id(),
          entity_specifics_2, /*creation_time=*/1000,
          /*last_modified_time=*/0));

  ASSERT_TRUE(GetClient(0)->SignInPrimaryAccount());
  ASSERT_TRUE(GetClient(0)->AwaitSyncTransportActive());
  WaitForCvcOnCard(GetPaymentsDataManager(0));

  ASSERT_EQ(syncer::SyncService::TransportState::ACTIVE,
            GetSyncService(0)->GetTransportState());
  ASSERT_TRUE(GetSyncService(0)->GetActiveDataTypes().Has(
      syncer::AUTOFILL_WALLET_CREDENTIAL));

  scoped_refptr<autofill::AutofillWebDataService> profile_data_2 =
      GetProfileWebDataService(0);
  ASSERT_NE(nullptr, profile_data_2);
  scoped_refptr<autofill::AutofillWebDataService> account_data_2 =
      GetAccountWebDataService(0);
  ASSERT_NE(nullptr, account_data_2);

  // Check that no data is stored in the profile storage.
  EXPECT_EQ(0U, GetServerCards(profile_data_2).size());

  // Check that one card is stored in the account storage.
  EXPECT_EQ(1U, GetServerCards(account_data_2).size());

  std::vector<const autofill::CreditCard*> cards_2 = paydm->GetCreditCards();
  ASSERT_EQ(1uL, cards_2.size());

  // Check for CVC data on the correct card with the `instrument_id` of 9.
  ASSERT_EQ(9, cards_2[0]->instrument_id());
  EXPECT_FALSE(cards_2[0]->cvc().empty());
  ASSERT_EQ(u"720", cards_2[0]->cvc());
}

// Verify if 2 cards are synced down along with a single wallet credential
// entity, the credential entity is attached to the correct card.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest,
                       CorrectCvcSyncAttachedToCardEntity) {
  SetDefaultWalletCredentialOnFakeServer();
  GetFakeServer()->SetWalletData(
      {CreateDefaultSyncWalletCard(),
       CreateSyncWalletCard(
           /*name=*/"new-card", /*last_four=*/"0002", kDefaultBillingAddressID,
           /*nickname=*/"", /*instrument_id=*/123),
       CreateSyncPaymentsCustomerData(
           /*customer_id=*/"different")});

  ASSERT_TRUE(SetupClients());
  ASSERT_TRUE(GetClient(0)->SignInPrimaryAccount());
  ASSERT_TRUE(GetClient(0)->AwaitSyncTransportActive());
  WaitForCvcOnCard(GetPaymentsDataManager(0));

  ASSERT_EQ(syncer::SyncService::TransportState::ACTIVE,
            GetSyncService(0)->GetTransportState());
  ASSERT_TRUE(GetSyncService(0)->GetActiveDataTypes().Has(
      syncer::AUTOFILL_WALLET_CREDENTIAL));

  scoped_refptr<autofill::AutofillWebDataService> profile_data =
      GetProfileWebDataService(0);
  ASSERT_NE(nullptr, profile_data);
  scoped_refptr<autofill::AutofillWebDataService> account_data =
      GetAccountWebDataService(0);
  ASSERT_NE(nullptr, account_data);

  // Check that no data is stored in the profile storage.
  EXPECT_EQ(0U, GetServerCards(profile_data).size());

  // Check that two cards are stored in the account storage.
  EXPECT_EQ(2U, GetServerCards(account_data).size());

  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);
  std::vector<const autofill::CreditCard*> cards = paydm->GetCreditCards();
  ASSERT_EQ(2uL, cards.size());

  const autofill::CreditCard* card_with_cvc =
      (cards[0]->instrument_id() != 123) ? cards[0] : cards[1];
  const autofill::CreditCard* card_without_cvc =
      (cards[0]->instrument_id() != 123) ? cards[1] : cards[0];

  // Check for CVC data on the correct card where the `instrument_id` != 123.
  EXPECT_FALSE(card_with_cvc->cvc().empty());
  EXPECT_EQ(u"098", card_with_cvc->cvc());
  EXPECT_TRUE(card_without_cvc->cvc().empty());
}

// Verify that card and CVC data should get cleared from the database when the
// user signs out.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest, ClearOnSignOut) {
  SetDefaultWalletCredentialOnFakeServer();
  GetFakeServer()->SetWalletData({CreateDefaultSyncWalletCard()});
  ASSERT_TRUE(SetupSync());

  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);

  // Make sure the wallet and credential data is in the DB.
  ASSERT_EQ(1uL, paydm->GetCreditCards().size());
  EXPECT_FALSE(paydm->GetCreditCards()[0]->cvc().empty());
  ExpectDefaultWalletCredentialValues(*paydm->GetCreditCards()[0]);

  // Signout, the wallet and credential data should be gone.
  GetClient(0)->SignOutPrimaryAccount();
  WaitForNumberOfCards(0, paydm);

  EXPECT_EQ(0uL, paydm->GetCreditCards().size());
}

// Verify that card and CVC data should get cleared from the database when the
// user signs out from Transport mode.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest,
                       ClearOnSignOutFromTransportMode) {
  SetDefaultWalletCredentialOnFakeServer();
  GetFakeServer()->SetWalletData({CreateDefaultSyncWalletCard()});

  ASSERT_TRUE(SetupClients());
  ASSERT_TRUE(GetClient(0)->SignInPrimaryAccount());
  ASSERT_TRUE(GetClient(0)->AwaitSyncTransportActive());
  WaitForCvcOnCard(GetPaymentsDataManager(0));

  ASSERT_EQ(syncer::SyncService::TransportState::ACTIVE,
            GetSyncService(0)->GetTransportState());
  ASSERT_TRUE(GetSyncService(0)->GetActiveDataTypes().Has(
      syncer::AUTOFILL_WALLET_CREDENTIAL));

  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);

  // Make sure the wallet and credential data is in the DB.
  ASSERT_EQ(1uL, paydm->GetCreditCards().size());
  EXPECT_FALSE(paydm->GetCreditCards()[0]->cvc().empty());
  ExpectDefaultWalletCredentialValues(*paydm->GetCreditCards()[0]);

  // Signout, the wallet and credential data should be gone.
  GetClient(0)->SignOutPrimaryAccount();
  WaitForNumberOfCards(0, paydm);

  EXPECT_EQ(0uL, paydm->GetCreditCards().size());
}

#endif  // !BUILDFLAG(IS_CHROMEOS)

// Verify that card and CVC data should get cleared from the database when the
// sync for Payments is disabled.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest,
                       ClearOnDisablePaymentsSync) {
  SetDefaultWalletCredentialOnFakeServer();
  GetFakeServer()->SetWalletData({CreateDefaultSyncWalletCard()});
  ASSERT_TRUE(SetupSync());

  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);

  // Make sure the wallet and credential data is in the DB.
  ASSERT_EQ(1uL, paydm->GetCreditCards().size());
  EXPECT_FALSE(paydm->GetCreditCards()[0]->cvc().empty());
  ExpectDefaultWalletCredentialValues(*paydm->GetCreditCards()[0]);

  // Disable sync for `kPayments`, the wallet and credential data should be
  // gone.
  ASSERT_TRUE(
      GetClient(0)->DisableSyncForType(syncer::UserSelectableType::kPayments));
  WaitForNumberOfCards(0, paydm);

  EXPECT_EQ(0uL, paydm->GetCreditCards().size());

  // Enable sync for `kPayments`, the wallet and credential data should come
  // back.
  ASSERT_TRUE(
      GetClient(0)->EnableSyncForType(syncer::UserSelectableType::kPayments));

  // Wait until Sync restores the card and it arrives at paydm.
  WaitForNumberOfCards(1, paydm);

  EXPECT_EQ(1uL, paydm->GetCreditCards().size());
}

// Card and CVC data should get cleared from the database when the user enters
// the sync paused state (e.g. persistent auth error).
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest,
                       ClearOnSyncPaused) {
  SetDefaultWalletCredentialOnFakeServer();
  GetFakeServer()->SetWalletData({CreateDefaultSyncWalletCard()});
  ASSERT_TRUE(SetupSync());

  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);

  // Make sure the wallet and credential data is in the DB.
  ASSERT_EQ(1uL, paydm->GetCreditCards().size());
  EXPECT_FALSE(paydm->GetCreditCards()[0]->cvc().empty());
  ExpectDefaultWalletCredentialValues(*paydm->GetCreditCards()[0]);

  // Enter sync paused state, the wallet and credential data should be gone.
  GetClient(0)->EnterSyncPausedStateForPrimaryAccount();
  WaitForNumberOfCards(0, paydm);

  EXPECT_EQ(0uL, paydm->GetCreditCards().size());

  GetClient(0)->ExitSyncPausedStateForPrimaryAccount();
  WaitForNumberOfCards(1, paydm);

  ASSERT_EQ(1uL, paydm->GetCreditCards().size());
  EXPECT_FALSE(paydm->GetCreditCards()[0]->cvc().empty());
  ExpectDefaultWalletCredentialValues(*paydm->GetCreditCards()[0]);
}

// CVC data is using incremental updates. Make sure existing data doesn't get
// replaced when new data is synced down.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest,
                       NewSyncDataShouldBeIncremental) {
  SetDefaultWalletCredentialOnFakeServer();
  GetFakeServer()->SetWalletData(
      {CreateDefaultSyncWalletCard(),
       CreateSyncWalletCard(/*name=*/"card-2", /*last_four=*/"0001",
                            kDefaultBillingAddressID,
                            /*nickname=*/"",
                            /*instrument_id=*/123)});
  ASSERT_TRUE(SetupSync());

  // Make sure the data is in the DB.
  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);

  std::vector<const autofill::CreditCard*> cards = paydm->GetCreditCards();
  ASSERT_EQ(2uL, cards.size());

  const autofill::CreditCard* card_with_cvc_1 =
      (cards[1]->instrument_id() == 123) ? cards[0] : cards[1];
  EXPECT_FALSE(card_with_cvc_1->cvc().empty());
  ExpectDefaultWalletCredentialValues(*card_with_cvc_1);

  // Add credentials for the second card to the server.
  sync_pb::EntitySpecifics entity_specifics_2;
  *entity_specifics_2.mutable_autofill_wallet_credential() =
      *CreateSyncWalletCredential(
           ServerCvc{/*instrument_id=*/123, /*cvc=*/u"720",
                     /*last_updated_timestamp=*/base::Time::UnixEpoch() +
                         base::Milliseconds(50000)})
           .mutable_specifics()
           ->mutable_autofill_wallet_credential();

  GetFakeServer()->InjectEntity(
      syncer::PersistentUniqueClientEntity::CreateFromSpecificsForTesting(
          /*non_unique_name=*/"credential_2",
          entity_specifics_2.mutable_autofill_wallet_credential()
              ->instrument_id(),
          entity_specifics_2, /*creation_time=*/1000,
          /*last_modified_time=*/0));

  PaymentsDataChangedWaiter(paydm).Wait();

  // Make sure both the credential data is present.
  cards = paydm->GetCreditCards();
  ASSERT_EQ(2uL, cards.size());

  card_with_cvc_1 = (cards[1]->instrument_id() == 123) ? cards[0] : cards[1];
  const autofill::CreditCard* card_with_cvc_2 =
      (cards[1]->instrument_id() == 123) ? cards[1] : cards[0];

  EXPECT_FALSE(card_with_cvc_1->cvc().empty());
  EXPECT_FALSE(card_with_cvc_2->cvc().empty());
  ExpectDefaultWalletCredentialValues(*card_with_cvc_1);
  EXPECT_EQ(u"720", card_with_cvc_2->cvc());
}

// Verify that card and CVC data should get cleared from the database when the
// wallet sync is disabled.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest,
                       ClearOnDisableWalletSync) {
  SetDefaultWalletCredentialOnFakeServer();
  GetFakeServer()->SetWalletData({CreateDefaultSyncWalletCard()});
  ASSERT_TRUE(SetupSync());

  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);

  // Make sure the wallet and credential data is in the DB.
  ASSERT_EQ(1uL, paydm->GetCreditCards().size());
  EXPECT_FALSE(paydm->GetCreditCards()[0]->cvc().empty());
  ExpectDefaultWalletCredentialValues(*paydm->GetCreditCards()[0]);

  // Turn off payments sync, the wallet and credential data should be gone.
  ASSERT_TRUE(
      GetClient(0)->DisableSyncForType(syncer::UserSelectableType::kPayments));

  WaitForNumberOfCards(0, paydm);

  EXPECT_EQ(0uL, paydm->GetCreditCards().size());
}

// Verify when the corresponding card of a CVC is deleted from pay.google.com
// and wallet data sync is triggered, it will delete the orphaned CVC from local
// DB and Chrome sync server.
IN_PROC_BROWSER_TEST_F(SingleClientWalletCredentialSyncTest,
                       ReconcileServerCvcForWalletCards) {
  // Set a wallet card on the fake server. This card will be synced first to the
  // client.
  GetFakeServer()->SetWalletData(
      {CreateSyncWalletCard(/*name=*/"card-1", /*last_four=*/"0001",
                            kDefaultBillingAddressID, /*nickname=*/"",
                            /*instrument_id=*/1)});
  ASSERT_TRUE(SetupSync());
  ASSERT_EQ(syncer::SyncService::TransportState::ACTIVE,
            GetSyncService(0)->GetTransportState());

  PaymentsDataManager* paydm = GetPaymentsDataManager(0);
  ASSERT_NE(nullptr, paydm);
  ASSERT_EQ(1uL, paydm->GetCreditCards().size());

  // Add 2 wallet credential entities (CVC) on the fake server. One of them is
  // linked to the card created above and the other credential has no linkage to
  // any cards on the client aka orphaned.
  SetDefaultWalletCredentialOnFakeServer();
  SetWalletCredentialOnFakeServer(
      ServerCvc{/*instrument_id=*/9, /*cvc=*/u"720",
                /*last_updated_timestamp=*/base::Time::UnixEpoch() +
                    base::Milliseconds(50000)});
  WaitForCvcOnCard(paydm);
  EXPECT_FALSE(paydm->GetCreditCards()[0]->cvc().empty());

  // The count for CVCs on the fake server is still 2 as we reconcile the CVC
  // data on the wallet sync bridge.
  EXPECT_TRUE(ServerCvcChecker(/*expected_count=*/2ul).Wait());

  // Creating an update for the card to force a sync of the new data and trigger
  // the reconcile flow on the wallet sync bridge.
  GetFakeServer()->SetWalletData(
      {CreateSyncWalletCard(/*name=*/"card-1-updated", /*last_four=*/"0001",
                            kDefaultBillingAddressID, /*nickname=*/"nickname",
                            /*instrument_id=*/1),
       CreateSyncPaymentsCustomerData(
           /*customer_id=*/"different")});

  // Verify that Chrome sync server has deleted the orphaned CVC by verifying
  // the count and the CVC value on the card.
  EXPECT_TRUE(ServerCvcChecker(/*expected_count=*/1ul).Wait());
  EXPECT_FALSE(paydm->GetCreditCards()[0]->cvc().empty());
}