File: test_autofill_client.h

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 (753 lines) | stat: -rw-r--r-- 28,424 bytes parent folder | download | duplicates (2)
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FOUNDATIONS_TEST_AUTOFILL_CLIENT_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_FOUNDATIONS_TEST_AUTOFILL_CLIENT_H_

#include <concepts>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/feature_list.h"
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/i18n/rtl.h"
#include "base/notreached.h"
#include "base/scoped_observation.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/country_type.h"
#include "components/autofill/core/browser/crowdsourcing/autofill_crowdsourcing_manager.h"
#include "components/autofill/core/browser/crowdsourcing/mock_autofill_crowdsourcing_manager.h"
#include "components/autofill/core/browser/crowdsourcing/test_votes_uploader.h"
#include "components/autofill/core/browser/data_manager/autofill_ai/entity_data_manager.h"
#include "components/autofill/core/browser/data_manager/test_personal_data_manager.h"
#include "components/autofill/core/browser/data_manager/valuables/test_valuables_data_manager.h"
#include "components/autofill/core/browser/data_manager/valuables/valuables_data_manager.h"
#include "components/autofill/core/browser/data_quality/addresses/test_address_normalizer.h"
#include "components/autofill/core/browser/foundations/autofill_client.h"
#include "components/autofill/core/browser/foundations/autofill_driver_factory.h"
#include "components/autofill/core/browser/integrators/autofill_ai/mock_autofill_ai_delegate.h"
#include "components/autofill/core/browser/integrators/fast_checkout/mock_fast_checkout_client.h"
#include "components/autofill/core/browser/integrators/identity_credential/identity_credential_delegate.h"
#include "components/autofill/core/browser/integrators/optimization_guide/mock_autofill_optimization_guide.h"
#include "components/autofill/core/browser/integrators/password_manager/password_manager_delegate.h"
#include "components/autofill/core/browser/integrators/plus_addresses/autofill_plus_address_delegate.h"
#include "components/autofill/core/browser/logging/log_manager.h"
#include "components/autofill/core/browser/logging/log_router.h"
#include "components/autofill/core/browser/logging/text_log_receiver.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/metrics/form_interactions_ukm_logger.h"
#include "components/autofill/core/browser/payments/test_payments_autofill_client.h"
#include "components/autofill/core/browser/permissions/autofill_ai/autofill_ai_permission_utils.h"
#include "components/autofill/core/browser/single_field_fillers/autocomplete/mock_autocomplete_history_manager.h"
#include "components/autofill/core/browser/single_field_fillers/payments/mock_merchant_promo_code_manager.h"
#include "components/autofill/core/browser/single_field_fillers/single_field_fill_router.h"
#include "components/autofill/core/browser/strike_databases/payments/test_strike_database.h"
#include "components/autofill/core/browser/studies/autofill_ablation_study.h"
#include "components/autofill/core/browser/suggestions/suggestion_hiding_reason.h"
#include "components/autofill/core/browser/suggestions/suggestion_type.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/browser/ui/payments/card_unmask_prompt_options.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service_test_helper.h"
#include "components/autofill/core/browser/webdata/valuables/valuables_table.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/device_reauth/mock_device_authenticator.h"
#include "components/optimization_guide/core/feature_registry/feature_registration.h"
#include "components/optimization_guide/core/model_execution/model_execution_prefs.h"
#include "components/optimization_guide/machine_learning_tflite_buildflags.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_registry.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/account_capabilities_test_mutator.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/translate/core/browser/language_state.h"
#include "components/translate/core/browser/mock_translate_driver.h"
#include "components/ukm/test_ukm_recorder.h"
#include "components/version_info/channel.h"
#include "services/metrics/public/cpp/delegating_ukm_recorder.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#endif

#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
#include "components/autofill/core/browser/ml_model/field_classification_model_handler.h"
#endif

namespace autofill {

// This class is for easier writing of tests. There are two instances of the
// template:
//
// - TestAutofillClient is a simple AutofillClient;
// - TestContentAutofillClient is a ContentAutofillClient, i.e., is associated
//   to a content::WebContents and has a ContentAutofillDriverFactory
//
// As a rule of thumb, TestContentAutofillClient is preferable in tests that
// have a content::WebContents.
//
// If you enable the Finch feature `kAutofillLoggingToTerminal`,
// autofill-internals logs are recorded to LOG(INFO).
template <std::derived_from<AutofillClient> T>
class TestAutofillClientTemplate : public T {
 public:
  using T::T;
  TestAutofillClientTemplate(const TestAutofillClientTemplate&) = delete;
  TestAutofillClientTemplate& operator=(const TestAutofillClientTemplate&) =
      delete;
  ~TestAutofillClientTemplate() override = default;

  base::WeakPtr<AutofillClient> GetWeakPtr() override {
    return weak_ptr_factory_.GetWeakPtr();
  }

  const std::string& GetAppLocale() const override { return app_locale_; }

  version_info::Channel GetChannel() const override {
    return channel_for_testing_;
  }

  bool IsOffTheRecord() const override { return is_off_the_record_; }

  AutofillCrowdsourcingManager& GetCrowdsourcingManager() override {
    if (!crowdsourcing_manager_) {
      crowdsourcing_manager_ =
          std::make_unique<testing::NiceMock<MockAutofillCrowdsourcingManager>>(
              this, GetCurrentLogManager());
    }
    return *crowdsourcing_manager_;
  }

  TestVotesUploader& GetVotesUploader() override {
    if (!votes_uploader_) {
      votes_uploader_ = std::make_unique<TestVotesUploader>(this);
    }
    return *votes_uploader_;
  }

  scoped_refptr<network::SharedURLLoaderFactory> GetURLLoaderFactory()
      override {
    return test_shared_loader_factory_;
  }

  TestPersonalDataManager& GetPersonalDataManager() override {
    if (!test_personal_data_manager_) {
      test_personal_data_manager_ = std::make_unique<TestPersonalDataManager>();
    }
    return *test_personal_data_manager_.get();
  }

  ValuablesDataManager* GetValuablesDataManager() override {
    if (!valuables_data_manager_) {
      valuables_data_manager_ = std::make_unique<TestValuablesDataManager>();
    }
    return valuables_data_manager_.get();
  }

  EntityDataManager* GetEntityDataManager() override {
    return entity_data_manager_non_owning_
               ? entity_data_manager_non_owning_.get()
               : entity_data_manager_.get();
  }

  MockAutofillOptimizationGuide* GetAutofillOptimizationGuide() const override {
    return mock_autofill_optimization_guide_.get();
  }

  void ResetAutofillOptimizationGuide() {
    mock_autofill_optimization_guide_.reset();
  }

  MockAutofillAiDelegate* GetAutofillAiDelegate() override {
    return mock_autofill_ai_delegate_.get();
  }

  SingleFieldFillRouter& GetSingleFieldFillRouter() override {
    if (!single_field_fill_router_) {
      single_field_fill_router_ = std::make_unique<SingleFieldFillRouter>(
          GetAutocompleteHistoryManager(),
          GetPaymentsAutofillClient()->GetIbanManager(),
          GetPaymentsAutofillClient()->GetMerchantPromoCodeManager());
    }
    return *single_field_fill_router_;
  }

  MockAutocompleteHistoryManager* GetAutocompleteHistoryManager() override {
    return &mock_autocomplete_history_manager_;
  }

  AutofillPlusAddressDelegate* GetPlusAddressDelegate() override {
    return plus_address_delegate_.get();
  }

  IdentityCredentialDelegate* GetIdentityCredentialDelegate() override {
    return identity_credential_delegate_.get();
  }

  PasswordManagerDelegate* GetPasswordManagerDelegate(
      const autofill::FieldGlobalId& field_id) override {
    return password_manager_delegate_.get();
  }

  test::AutofillTestingPrefService* GetPrefs() override {
    if (!prefs_) {
      prefs_ = autofill::test::PrefServiceForTesting();
    }
    return prefs_.get();
  }

  const test::AutofillTestingPrefService* GetPrefs() const override {
    return const_cast<TestAutofillClientTemplate*>(this)->GetPrefs();
  }

  syncer::SyncService* GetSyncService() override { return test_sync_service_; }

  signin::IdentityManager* GetIdentityManager() override {
    return const_cast<signin::IdentityManager*>(
        std::as_const(*this).GetIdentityManager());
  }

  const signin::IdentityManager* GetIdentityManager() const override {
    return identity_test_env_.identity_manager();
  }

  FormDataImporter* GetFormDataImporter() override {
    if (!form_data_importer_) {
      form_data_importer_ = std::make_unique<FormDataImporter>(
          /*client=*/this,
          /*history_service=*/nullptr);
    }
    return form_data_importer_.get();
  }

  payments::TestPaymentsAutofillClient* GetPaymentsAutofillClient() override {
    if (!payments_autofill_client_) {
      payments_autofill_client_ =
          std::make_unique<payments::TestPaymentsAutofillClient>(this);
    }
    return payments_autofill_client_.get();
  }

  TestStrikeDatabase* GetStrikeDatabase() override {
    return test_strike_database_.get();
  }

  ukm::TestAutoSetUkmRecorder* GetUkmRecorder() override {
    return &test_ukm_recorder_;
  }

  TestAddressNormalizer* GetAddressNormalizer() override {
    return &test_address_normalizer_;
  }

  FastCheckoutClient* GetFastCheckoutClient() override {
    return &mock_fast_checkout_client_;
  }

#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
  FieldClassificationModelHandler* GetAutofillFieldClassificationModelHandler()
      override {
    return autofill_ml_prediction_model_handler_.get();
  }

  void set_autofill_ml_prediction_model_handler(
      std::unique_ptr<FieldClassificationModelHandler> handler) {
    autofill_ml_prediction_model_handler_ = std::move(handler);
  }

  FieldClassificationModelHandler*
  GetPasswordManagerFieldClassificationModelHandler() override {
    return password_ml_prediction_model_handler_.get();
  }

  void set_password_ml_prediction_model_handler(
      std::unique_ptr<FieldClassificationModelHandler> handler) {
    password_ml_prediction_model_handler_ = std::move(handler);
  }
#endif

  const GURL& GetLastCommittedPrimaryMainFrameURL() const override {
    return last_committed_primary_main_frame_url_;
  }

  url::Origin GetLastCommittedPrimaryMainFrameOrigin() const override {
    return url::Origin::Create(last_committed_primary_main_frame_url_);
  }

  security_state::SecurityLevel GetSecurityLevelForUmaHistograms() override {
    return security_level_;
  }

  translate::LanguageState* GetLanguageState() override {
    return &mock_translate_driver_.GetLanguageState();
  }

  translate::TranslateDriver* GetTranslateDriver() override {
    return &mock_translate_driver_;
  }

  GeoIpCountryCode GetVariationConfigCountryCode() const override {
    return variation_config_country_code_;
  }

  void ShowAutofillSettings(SuggestionType suggestion_type) override {}

  void ConfirmSaveAddressProfile(
      const AutofillProfile& profile,
      const AutofillProfile* original_profile,
      bool is_migration_to_account,
      AutofillClient::AddressProfileSavePromptCallback callback) override {}

  AutofillClient::SuggestionUiSessionId ShowAutofillSuggestions(
      const AutofillClient::PopupOpenArgs& open_args,
      base::WeakPtr<AutofillSuggestionDelegate> delegate) override {
    is_showing_popup_ = true;
    static AutofillClient::SuggestionUiSessionId::Generator generator;
    return generator.GenerateNextId();
  }

  void UpdateAutofillDataListValues(
      base::span<const SelectOption> options) override {}

  void SetAutofillSuggestions(std::vector<Suggestion> suggestions) {
    suggestions_ = std::move(suggestions);
  }

  base::span<const Suggestion> GetAutofillSuggestions() const override {
    return suggestions_;
  }

  void UpdateAutofillSuggestions(
      const std::vector<Suggestion>& suggestions,
      FillingProduct main_filling_product,
      AutofillSuggestionTriggerSource trigger_source) override {}

  std::optional<AutofillClient::SuggestionUiSessionId>
  GetSessionIdForCurrentAutofillSuggestions() const override {
    return suggestion_ui_session_id_;
  }

  void HideAutofillSuggestions(SuggestionHidingReason reason) override {
    popup_hidden_reason_ = reason;
    is_showing_popup_ = false;
  }

  bool IsShowingAutofillPopup() { return is_showing_popup_; }

  SuggestionHidingReason popup_hiding_reason() { return popup_hidden_reason_; }

  bool ShowAutofillFieldIphForFeature(
      const FormFieldData& field,
      AutofillClient::IphFeature feature) override {
    autofill_iph_showing_ = feature;
    return true;
  }

  void HideAutofillFieldIph() override { autofill_iph_showing_ = std::nullopt; }

  bool IsShowingAutofillAiIph() {
    return autofill_iph_showing_ == AutofillClient::IphFeature::kAutofillAi;
  }

  void NotifyIphFeatureUsed(AutofillClient::IphFeature feature) override {
    if (notify_iph_feature_used_mock_callback_) {
      notify_iph_feature_used_mock_callback_->Run(feature);
    }
  }

  bool IsAutofillEnabled() const override {
    return IsAutofillProfileEnabled() || IsAutofillPaymentMethodsEnabled();
  }

  bool IsAutofillProfileEnabled() const override {
    return autofill_profile_enabled_;
  }

  bool IsAutofillPaymentMethodsEnabled() const override {
    return autofill_payment_methods_enabled_;
  }

  bool IsAutocompleteEnabled() const override { return true; }

  bool IsPasswordManagerEnabled() const override { return true; }

  void DidFillForm(AutofillTriggerSource trigger_source,
                   bool is_refill) override {}

  bool IsContextSecure() const override {
    return last_committed_primary_main_frame_url_.SchemeIs("https");
  }

  LogManager* GetCurrentLogManager() override { return log_manager_.get(); }

  autofill_metrics::FormInteractionsUkmLogger& GetFormInteractionsUkmLogger()
      override {
    return form_interactions_ukm_logger_;
  }

  const AutofillAblationStudy& GetAblationStudy() const override {
    static const AutofillAblationStudy default_ablation_study("seed");
    return default_ablation_study;
  }

  bool ShouldFormatForLargeKeyboardAccessory() const override {
    return format_for_large_keyboard_accessory_;
  }

  FormInteractionsFlowId GetCurrentFormInteractionsFlowId() override {
    return {};
  }

  std::unique_ptr<device_reauth::DeviceAuthenticator> GetDeviceAuthenticator()
      override {
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
    if (device_authenticator_) {
      return std::move(device_authenticator_);
    }
    return std::make_unique<device_reauth::MockDeviceAuthenticator>();
#else
    return nullptr;
#endif  // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
  }

  device_reauth::MockDeviceAuthenticator* GetDeviceAuthenticatorPtr() {
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
    return device_authenticator_.get();
#else
    return nullptr;
#endif  // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_ANDROID)
  }

  void SetDeviceAuthenticator(
      std::unique_ptr<device_reauth::MockDeviceAuthenticator>
          device_authenticator) {
    device_authenticator_ = std::move(device_authenticator);
  }

#if BUILDFLAG(IS_IOS)
  bool IsLastQueriedField(FieldGlobalId field_id) override { return true; }
#endif

  void set_test_addresses(
      std::vector<AutofillProfile> test_addresses) override {
    test_addresses_ = std::move(test_addresses);
  }

  base::span<const AutofillProfile> GetTestAddresses() const override {
    return test_addresses_;
  }

  void SetPrefs(std::unique_ptr<test::AutofillTestingPrefService> prefs) {
    prefs_ = std::move(prefs);
  }

  void SetAutofillProfileEnabled(bool autofill_profile_enabled) {
    autofill_profile_enabled_ = autofill_profile_enabled;
    if (PrefService* prefs = GetPrefs()) {
      prefs->SetBoolean(prefs::kAutofillProfileEnabled,
                        autofill_profile_enabled);
    }
    if (!autofill_profile_enabled_) {
      // Profile data is refreshed when this pref is changed.
      GetPersonalDataManager().test_address_data_manager().ClearProfiles();
    }
  }

  void SetAutofillPaymentMethodsEnabled(bool autofill_payment_methods_enabled) {
    autofill_payment_methods_enabled_ = autofill_payment_methods_enabled;
    if (PrefService* prefs = GetPrefs()) {
      prefs->SetBoolean(prefs::kAutofillCreditCardEnabled,
                        autofill_payment_methods_enabled);
    }
    if (!autofill_payment_methods_enabled) {
      // Credit card data is refreshed when this pref is changed.
      GetPersonalDataManager().test_payments_data_manager().ClearCreditCards();
    }
  }

  // Sets up prefs and identity state to simulate an opted-in AutofillAI user.
  // Returns `true` iff the setup was successful.
  bool SetUpPrefsAndIdentityForAutofillAi() {
    SetAutofillProfileEnabled(true);
    GetPrefs()->registry()->RegisterIntegerPref(
        optimization_guide::prefs::
            kAutofillPredictionImprovementsEnterprisePolicyAllowed,
        base::to_underlying(optimization_guide::model_execution::prefs::
                                ModelExecutionEnterprisePolicyValue::kAllow),
        PrefRegistry::LOSSY_PREF);

    identity_test_environment().MakePrimaryAccountAvailable(
        "foo@gmail.com", signin::ConsentLevel::kSignin);
    SetCanUseModelExecutionFeatures(true);
    SetVariationConfigCountryCode(GeoIpCountryCode("US"));
    return SetAutofillAiOptInStatus(*this, true);
  }

  // Updates whether the currently signed in primary account can use model
  // execution features. CHECKs that there is a primary account.
  void SetCanUseModelExecutionFeatures(bool can_use_model_execution) {
    AccountInfo account_info = GetIdentityManager()->FindExtendedAccountInfo(
        GetIdentityManager()->GetPrimaryAccountInfo(
            signin::ConsentLevel::kSignin));
    CHECK(!account_info.account_id.empty());
    AccountCapabilitiesTestMutator(&account_info.capabilities)
        .set_can_use_model_execution_features(can_use_model_execution);
    signin::UpdateAccountInfoForAccount(GetIdentityManager(), account_info);
  }

  void set_entity_data_manager(
      std::unique_ptr<EntityDataManager> entity_data_manager) {
    entity_data_manager_non_owning_ = nullptr;
    entity_data_manager_ = std::move(entity_data_manager);
  }

  void set_entity_data_manager(EntityDataManager* entity_data_manager) {
    entity_data_manager_.reset();
    entity_data_manager_non_owning_ = entity_data_manager;
  }

  void set_payments_autofill_client(
      std::unique_ptr<payments::TestPaymentsAutofillClient> payments_client) {
    payments_autofill_client_ = std::move(payments_client);
  }

  void set_valuables_data_manager(
      std::unique_ptr<ValuablesDataManager> valuables_data_manager) {
    valuables_data_manager_ = std::move(valuables_data_manager);
  }

  void set_single_field_fill_router(
      std::unique_ptr<SingleFieldFillRouter> router) {
    single_field_fill_router_ = std::move(router);
  }

  void set_test_strike_database(
      std::unique_ptr<TestStrikeDatabase> test_strike_database) {
    test_strike_database_ = std::move(test_strike_database);
  }

  void set_sync_service(syncer::SyncService* test_sync_service) {
    test_sync_service_ = test_sync_service;
  }

  void set_security_level(security_state::SecurityLevel security_level) {
    security_level_ = security_level;
  }

  void set_last_committed_primary_main_frame_url(const GURL& url) {
    last_committed_primary_main_frame_url_ = url;
  }

  void SetVariationConfigCountryCode(
      const GeoIpCountryCode& variation_config_country_code) {
    variation_config_country_code_ = variation_config_country_code;
    GetPersonalDataManager()
        .test_address_data_manager()
        .SetVariationCountryCode(variation_config_country_code);
  }

  void set_should_save_autofill_profiles(bool value) {
    should_save_autofill_profiles_ = value;
  }

  void set_format_for_large_keyboard_accessory(
      bool format_for_large_keyboard_accessory) {
    format_for_large_keyboard_accessory_ = format_for_large_keyboard_accessory;
  }

  void set_app_locale(std::string app_locale) {
    app_locale_ = std::move(app_locale);
  }

  void set_channel_for_testing(const version_info::Channel channel) {
    channel_for_testing_ = channel;
  }

  void set_is_off_the_record(bool is_off_the_record) {
    is_off_the_record_ = is_off_the_record;
  }

  void set_crowdsourcing_manager(
      std::unique_ptr<AutofillCrowdsourcingManager> crowdsourcing_manager) {
    crowdsourcing_manager_ = std::move(crowdsourcing_manager);
  }

  void set_shared_url_loader_factory(
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
    test_shared_loader_factory_ = url_loader_factory;
  }

  void set_plus_address_delegate(
      std::unique_ptr<AutofillPlusAddressDelegate> plus_address_delegate) {
    plus_address_delegate_ = std::move(plus_address_delegate);
  }

  void set_identity_credential_delegate(
      std::unique_ptr<IdentityCredentialDelegate>
          identity_credential_delegate) {
    identity_credential_delegate_ = std::move(identity_credential_delegate);
  }

  void set_password_manager_delegate(
      std::unique_ptr<PasswordManagerDelegate> password_manager_delegate) {
    password_manager_delegate_ = std::move(password_manager_delegate);
  }

  void set_suggestion_ui_session_id(
      std::optional<AutofillClient::SuggestionUiSessionId> session_id) {
    suggestion_ui_session_id_ = session_id;
  }

  void set_notify_iph_feature_used_mock_callback(
      base::RepeatingCallback<void(AutofillClient::IphFeature)> callback) {
    notify_iph_feature_used_mock_callback_ = std::move(callback);
  }

  signin::IdentityTestEnvironment& identity_test_environment() {
    return identity_test_env_;
  }

 private:
  ukm::TestAutoSetUkmRecorder test_ukm_recorder_;
  signin::IdentityTestEnvironment identity_test_env_;
  raw_ptr<syncer::SyncService> test_sync_service_ = nullptr;
  std::unique_ptr<AutofillPlusAddressDelegate> plus_address_delegate_;
  std::unique_ptr<IdentityCredentialDelegate> identity_credential_delegate_;
  std::unique_ptr<PasswordManagerDelegate> password_manager_delegate_;
  TestAddressNormalizer test_address_normalizer_;
  std::unique_ptr<::testing::NiceMock<MockAutofillOptimizationGuide>>
      mock_autofill_optimization_guide_ =
          std::make_unique<testing::NiceMock<MockAutofillOptimizationGuide>>();
  std::unique_ptr<::testing::NiceMock<MockAutofillAiDelegate>>
      mock_autofill_ai_delegate_ =
          std::make_unique<testing::NiceMock<MockAutofillAiDelegate>>();
  ::testing::NiceMock<MockAutocompleteHistoryManager>
      mock_autocomplete_history_manager_;
  ::testing::NiceMock<MockFastCheckoutClient> mock_fast_checkout_client_;
  std::unique_ptr<device_reauth::MockDeviceAuthenticator>
      device_authenticator_ = nullptr;

#if BUILDFLAG(BUILD_WITH_TFLITE_LIB)
  std::unique_ptr<FieldClassificationModelHandler>
      autofill_ml_prediction_model_handler_;
  std::unique_ptr<FieldClassificationModelHandler>
      password_ml_prediction_model_handler_;
#endif

  bool autofill_profile_enabled_ = true;
  bool autofill_payment_methods_enabled_ = true;

  // NULL by default.
  std::unique_ptr<test::AutofillTestingPrefService> prefs_;
  std::unique_ptr<TestStrikeDatabase> test_strike_database_;

  std::unique_ptr<TestPersonalDataManager> test_personal_data_manager_;
  std::unique_ptr<ValuablesDataManager> valuables_data_manager_;
  std::unique_ptr<EntityDataManager> entity_data_manager_;
  raw_ptr<EntityDataManager> entity_data_manager_non_owning_ = nullptr;
  // The below objects must be destroyed before `TestPersonalDataManager`
  // because they keep a reference to it.
  std::unique_ptr<payments::TestPaymentsAutofillClient>
      payments_autofill_client_;
  std::unique_ptr<SingleFieldFillRouter> single_field_fill_router_;
  std::unique_ptr<FormDataImporter> form_data_importer_;

  GeoIpCountryCode variation_config_country_code_;

  security_state::SecurityLevel security_level_ =
      security_state::SecurityLevel::NONE;

  bool should_save_autofill_profiles_ = true;

  bool format_for_large_keyboard_accessory_ = false;

  std::string app_locale_ = "en-US";

  version_info::Channel channel_for_testing_ = version_info::Channel::UNKNOWN;

  bool is_off_the_record_ = false;

  bool is_showing_popup_ = false;

  SuggestionHidingReason popup_hidden_reason_;

  std::optional<AutofillClient::IphFeature> autofill_iph_showing_;

  network::TestURLLoaderFactory test_url_loader_factory_;
  scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_ =
      base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
          &test_url_loader_factory_);

  // Test addresses used to allow developers to test their forms.
  std::vector<AutofillProfile> test_addresses_;

  std::vector<Suggestion> suggestions_;

  // A mock translate driver which provides the language state.
  translate::testing::MockTranslateDriver mock_translate_driver_;

  // The last URL submitted in the primary main frame by the user. Set in the
  // constructor.
  GURL last_committed_primary_main_frame_url_{"https://example.test"};

  std::optional<AutofillClient::SuggestionUiSessionId>
      suggestion_ui_session_id_;

  std::optional<base::RepeatingCallback<void(AutofillClient::IphFeature)>>
      notify_iph_feature_used_mock_callback_;

  LogRouter log_router_;
  struct LogToTerminal {
    explicit LogToTerminal(LogRouter& log_router) {
      if (base::FeatureList::IsEnabled(
              features::test::kAutofillLogToTerminal)) {
        log_router.LogToTerminal();
      }
    }
  } log_to_terminal_{log_router_};
  std::unique_ptr<LogManager> log_manager_ =
      LogManager::Create(&log_router_, base::NullCallback());
  autofill_metrics::FormInteractionsUkmLogger form_interactions_ukm_logger_{
      this};

  std::unique_ptr<AutofillCrowdsourcingManager> crowdsourcing_manager_;
  std::unique_ptr<TestVotesUploader> votes_uploader_;

  base::WeakPtrFactory<TestAutofillClientTemplate> weak_ptr_factory_{this};
};

// Base class for TestAutofillClientTemplate to derive from so that the
// AutofillDriverFactory is initialized before the members of
// TestAutofillClientTemplate. This initialization order mimics that of
// ContentAutofillClient and AndroidAutofillClient / ChromeAutofillClient.
class TestAutofillClientBase : public AutofillClient {
 public:
  AutofillDriverFactory& GetAutofillDriverFactory() override;

 protected:
  // Instantiation should only happen through TestAutofillClient.
  TestAutofillClientBase();

 private:
  AutofillDriverFactory autofill_driver_factory_;
};

// A simple `AutofillClient` for tests. Consider `TestContentAutofillClient` as
// an alternative for tests where the content layer is visible.
//
// Consider using TestAutofillClientInjector, especially in browser tests.
using TestAutofillClient = TestAutofillClientTemplate<TestAutofillClientBase>;

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_FOUNDATIONS_TEST_AUTOFILL_CLIENT_H_