-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
address_profile_save_manager_unittest.cc
1464 lines (1288 loc) · 63.8 KB
/
address_profile_save_manager_unittest.cc
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 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/address_profile_save_manager.h"
#include "base/strings/strcat.h"
#include "base/strings/string_piece.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "components/autofill/core/browser/autofill_profile_import_process.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/data_model/autofill_structured_address_test_utils.h"
#include "components/autofill/core/browser/metrics/autofill_metrics.h"
#include "components/autofill/core/browser/test_autofill_client.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/browser/test_utils/test_profiles.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/autofill_features.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
namespace {
using UkmAddressProfileImportType =
ukm::builders::Autofill_AddressProfileImport;
using UserDecision = AutofillClient::SaveAddressProfileOfferUserDecision;
// Names of histrogram used for metric collection.
constexpr char kProfileImportTypeHistogram[] =
"Autofill.ProfileImport.ProfileImportType";
constexpr char kSilentUpdatesProfileImportTypeHistogram[] =
"Autofill.ProfileImport.SilentUpdatesProfileImportType";
constexpr char kNewProfileEditsHistogram[] =
"Autofill.ProfileImport.NewProfileEditedType";
constexpr char kProfileUpdateEditsHistogram[] =
"Autofill.ProfileImport.UpdateProfileEditedType";
constexpr char kProfileUpdateAffectedTypesHistogram[] =
"Autofill.ProfileImport.UpdateProfileAffectedType";
constexpr char kNewProfileDecisionHistogram[] =
"Autofill.ProfileImport.NewProfileDecision";
constexpr char kProfileUpdateDecisionHistogram[] =
"Autofill.ProfileImport.UpdateProfileDecision";
constexpr char kNewProfileNumberOfEditsHistogram[] =
"Autofill.ProfileImport.NewProfileNumberOfEditedFields";
constexpr char kProfileUpdateNumberOfEditsHistogram[] =
"Autofill.ProfileImport.UpdateProfileNumberOfEditedFields";
constexpr char kProfileUpdateNumberOfAffectedTypesHistogram[] =
"Autofill.ProfileImport.UpdateProfileNumberOfAffectedFields";
// Histograms related to the country complement logic.
constexpr char kNewProfileWithComplementedCountryDecisionHistogram[] =
"Autofill.ProfileImport.NewProfileWithComplementedCountryDecision";
constexpr char kProfileUpdateWithComplementedCountryDecisionHistogram[] =
"Autofill.ProfileImport.UpdateProfileWithComplementedCountryDecision";
constexpr char kNewProfileEditComplementedCountryHistogram[] =
"Autofill.ProfileImport.NewProfileEditedComplementedCountry";
constexpr char kProfileUpdateEditComplementedCountryHistogram[] =
"Autofill.ProfileImport.UpdateProfileEditedComplementedCountry";
// Histograms related to `kAutofillIgnoreInvalidCountryOnImport`.
// TODO(crbug.com/1362472): Cleanup when launched.
constexpr char kNewProfileWithIgnoredCountryDecisionHistogram[] =
"Autofill.ProfileImport.NewProfileWithIgnoredCountryDecision";
constexpr char kProfileUpdateWithIgnoredCountryDecisionHistogram[] =
"Autofill.ProfileImport.UpdateProfileWithIgnoredCountryDecision";
// Histograms related to |kAutofillRemoveInvalidPhoneNumberOnImport|
// TODO(crbug.com/1298424): Cleanup when launched.
constexpr char kNewProfileWithRemovedPhoneNumberDecisionHistogram[] =
"Autofill.ProfileImport.NewProfileWithRemovedPhoneNumberDecision";
constexpr char kProfileUpdateWithRemovedPhoneNumberDecisionHistogram[] =
"Autofill.ProfileImport.UpdateProfileWithRemovedPhoneNumberDecision";
constexpr char
kSilentUpdatesWithRemovedPhoneNumberProfileImportTypeHistogram[] =
"Autofill.ProfileImport."
"SilentUpdatesWithRemovedPhoneNumberProfileImportType";
class MockPersonalDataManager : public TestPersonalDataManager {
public:
MockPersonalDataManager() = default;
~MockPersonalDataManager() override = default;
MOCK_METHOD(std::string,
SaveImportedProfile,
(const AutofillProfile&),
(override));
};
// This derived version of the AddressProfileSaveManager stores the last import
// for testing purposes and mocks the UI request.
class TestAddressProfileSaveManager : public AddressProfileSaveManager {
public:
// The parameters should outlive the AddressProfileSaveManager.
TestAddressProfileSaveManager(AutofillClient* client,
PersonalDataManager* personal_data_manager);
// Mocks the function that initiates the UI prompt for testing purposes.
MOCK_METHOD(void,
OfferSavePrompt,
(std::unique_ptr<ProfileImportProcess>),
(override));
// Returns a copy of the last finished import process or 'absl::nullopt' if no
// import process was finished.
ProfileImportProcess* last_import();
void OnUserDecisionForTesting(
std::unique_ptr<ProfileImportProcess> import_process,
UserDecision decision,
AutofillProfile edited_profile) {
if (profile_added_while_waiting_for_user_response_) {
personal_data_manager()->AddProfile(
profile_added_while_waiting_for_user_response_.value());
}
import_process->set_prompt_was_shown();
OnUserDecision(std::move(import_process), decision, edited_profile);
}
void SetProfileThatIsAddedInWhileWaitingForUserResponse(
const AutofillProfile& profile) {
profile_added_while_waiting_for_user_response_ = profile;
}
protected:
void ClearPendingImport(
std::unique_ptr<ProfileImportProcess> import_process) override;
// Profile that is passed from the emulated UI respones in case the user
// edited the import candidate.
std::unique_ptr<ProfileImportProcess> last_import_;
// If set, this is a profile that is added in between the import operation
// while the response from the user is pending.
absl::optional<AutofillProfile>
profile_added_while_waiting_for_user_response_;
};
TestAddressProfileSaveManager::TestAddressProfileSaveManager(
AutofillClient* client,
PersonalDataManager* personal_data_manager)
: AddressProfileSaveManager(client, personal_data_manager) {}
void TestAddressProfileSaveManager::ClearPendingImport(
std::unique_ptr<ProfileImportProcess> import_process) {
last_import_ = std::move(import_process);
AddressProfileSaveManager::ClearPendingImport(std::move(import_process));
}
ProfileImportProcess* TestAddressProfileSaveManager::last_import() {
return last_import_.get();
}
// Definition of a test scenario.
struct ImportScenarioTestCase {
std::vector<AutofillProfile> existing_profiles;
AutofillProfile observed_profile;
bool is_prompt_expected;
UserDecision user_decision;
AutofillProfile edited_profile;
AutofillProfileImportType expected_import_type;
bool is_profile_change_expected;
absl::optional<AutofillProfile> merge_candidate;
absl::optional<AutofillProfile> import_candidate;
std::vector<AutofillProfile> expected_final_profiles;
std::vector<AutofillMetrics::SettingsVisibleFieldTypeForMetrics>
expected_edited_types_for_metrics;
std::vector<AutofillMetrics::SettingsVisibleFieldTypeForMetrics>
expected_affeceted_types_in_merge_for_metrics;
bool new_profiles_suppresssed_for_domain;
std::vector<std::string> blocked_guids_for_updates;
absl::optional<AutofillProfile> profile_to_be_added_while_waiting;
bool allow_only_silent_updates = false;
};
class AddressProfileSaveManagerTest
: public testing::Test,
public testing::WithParamInterface<std::tuple<bool, bool, bool, bool>> {
public:
void SetUp() override {
// These parameters would typically be set by `FormDataImporter` when
// creating the `ImportScenarioTestCase::observed_profile`. This step
// precedes the saving logic tested here. They expand the
// `ImportScenarioTestCase`, but are part of the fixture, so they can be
// tested in a parameterized way.
import_metadata_ = {.did_complement_country = std::get<0>(GetParam()),
.did_ignore_invalid_country = std::get<1>(GetParam()),
.phone_import_status = std::get<2>(GetParam())
? PhoneImportStatus::kInvalid
: PhoneImportStatus::kValid,
.did_import_from_unrecognized_autocomplete_field =
std::get<3>(GetParam())};
}
void BlockProfileForUpdates(const std::string& guid) {
while (!mock_personal_data_manager_.IsProfileUpdateBlocked(guid)) {
mock_personal_data_manager_.AddStrikeToBlockProfileUpdate(guid);
}
}
// Tests the |test_scenario|.
void TestImportScenario(ImportScenarioTestCase& test_scenario);
// Verifies the logged ukm data.
void VerifyUkmForAddressImport(const ukm::TestUkmRecorder* ukm_recorder,
ImportScenarioTestCase& test_scenario);
const ProfileImportMetadata& import_metadata() const {
return import_metadata_;
}
protected:
base::test::TaskEnvironment task_environment_;
TestAutofillClient autofill_client_;
MockPersonalDataManager mock_personal_data_manager_;
ProfileImportMetadata import_metadata_;
};
// Expects that none of the histograms `names` has any samples.
void ExpectEmptyHistograms(const base::HistogramTester& histogram_tester,
const std::vector<base::StringPiece>& names) {
for (base::StringPiece name : names)
histogram_tester.ExpectTotalCount(name, 0);
}
// Helper function that tests the reporting of feature-specific metrics around
// the user decision. These metrics usually exist twice, once for new profile
// creations and once for updates. Verifies that:
// - If the feature that is being tested and supposed to emit these metrics is
// enabled (`feature_enabled`), a unique sample in either the
// `new_profile_histogram_name` or the `update_profile_histogram_name`
// histogram is collected, depending on the import type.
// - If the feature is disabled, no metrics are collected.
void TestFeatureSpecificNewOrUpdateProfileMetrics(
const base::HistogramTester& histogram_tester,
const ImportScenarioTestCase& test_scenario,
bool feature_enabled,
base::StringPiece new_profile_histogram_name,
base::StringPiece update_profile_histogram_name) {
bool is_new_profile = test_scenario.expected_import_type ==
AutofillProfileImportType::kNewProfile;
if (feature_enabled) {
histogram_tester.ExpectUniqueSample(is_new_profile
? new_profile_histogram_name
: update_profile_histogram_name,
test_scenario.user_decision, 1);
ExpectEmptyHistograms(histogram_tester,
{!is_new_profile ? new_profile_histogram_name
: update_profile_histogram_name});
} else {
ExpectEmptyHistograms(histogram_tester, {new_profile_histogram_name,
update_profile_histogram_name});
}
}
void AddressProfileSaveManagerTest::TestImportScenario(
ImportScenarioTestCase& test_scenario) {
static const GURL url("https://www.importmyform.com/index.html");
// Assert that there is not a single profile stored in the personal data
// manager.
ASSERT_TRUE(mock_personal_data_manager_.GetProfiles().empty());
TestAddressProfileSaveManager save_manager(&autofill_client_,
&mock_personal_data_manager_);
base::HistogramTester histogram_tester;
if (test_scenario.profile_to_be_added_while_waiting) {
save_manager.SetProfileThatIsAddedInWhileWaitingForUserResponse(
test_scenario.profile_to_be_added_while_waiting.value());
}
// If the domain is blocked for new imports, use the defined limit for the
// initial strikes. Otherwise, use 1.
int initial_strikes =
test_scenario.new_profiles_suppresssed_for_domain
? mock_personal_data_manager_.GetProfileSaveStrikeDatabase()
->GetMaxStrikesLimit()
: 1;
mock_personal_data_manager_.GetProfileSaveStrikeDatabase()->AddStrikes(
initial_strikes, url.host());
ASSERT_EQ(mock_personal_data_manager_.IsNewProfileImportBlockedForDomain(url),
test_scenario.new_profiles_suppresssed_for_domain);
// Add one strike for each existing profile and the maximum number of strikes
// for blocked profiles.
for (const AutofillProfile& profile : test_scenario.existing_profiles) {
mock_personal_data_manager_.AddStrikeToBlockProfileUpdate(profile.guid());
}
for (const std::string& guid : test_scenario.blocked_guids_for_updates) {
BlockProfileForUpdates(guid);
}
// Set up the expectation and response for if a prompt should be shown.
if (test_scenario.is_prompt_expected) {
EXPECT_CALL(save_manager, OfferSavePrompt(testing::_))
.Times(1)
.WillOnce(testing::WithArgs<0>(
[&](std::unique_ptr<ProfileImportProcess> import_process) {
save_manager.OnUserDecisionForTesting(
std::move(import_process), test_scenario.user_decision,
test_scenario.edited_profile);
}));
} else {
EXPECT_CALL(save_manager, OfferSavePrompt).Times(0);
}
// Set the existing profiles to the personal data manager.
mock_personal_data_manager_.SetProfilesForAllSources(
&test_scenario.existing_profiles);
// Initiate the profile import.
save_manager.ImportProfileFromForm(
test_scenario.observed_profile, "en-US", url,
/*allow_only_silent_updates=*/test_scenario.allow_only_silent_updates,
import_metadata());
// Assert that there is a finished import process on record.
ASSERT_NE(save_manager.last_import(), nullptr);
ProfileImportProcess* last_import = save_manager.last_import();
EXPECT_EQ(test_scenario.expected_import_type, last_import->import_type());
// Make a copy of the final profiles in the personal data manager for
// comparison.
std::vector<AutofillProfile> final_profiles;
final_profiles.reserve(test_scenario.expected_final_profiles.size());
for (const auto* profile : mock_personal_data_manager_.GetProfiles())
final_profiles.push_back(*profile);
EXPECT_THAT(test_scenario.expected_final_profiles,
testing::UnorderedElementsAreArray(final_profiles));
// Test that the merge and import candidates are correct.
EXPECT_EQ(test_scenario.merge_candidate, last_import->merge_candidate());
EXPECT_EQ(test_scenario.import_candidate, last_import->import_candidate());
// Test the collection of metrics.
histogram_tester.ExpectUniqueSample(
test_scenario.allow_only_silent_updates
? kSilentUpdatesProfileImportTypeHistogram
: kProfileImportTypeHistogram,
test_scenario.expected_import_type, 1);
if (test_scenario.allow_only_silent_updates &&
import_metadata().phone_import_status == PhoneImportStatus::kInvalid) {
histogram_tester.ExpectUniqueSample(
kSilentUpdatesWithRemovedPhoneNumberProfileImportTypeHistogram,
test_scenario.expected_import_type, 1);
}
const bool is_new_profile = test_scenario.expected_import_type ==
AutofillProfileImportType::kNewProfile;
const bool is_confirmable_merge =
test_scenario.expected_import_type ==
AutofillProfileImportType::kConfirmableMerge ||
test_scenario.expected_import_type ==
AutofillProfileImportType::kConfirmableMergeAndSilentUpdate;
// If the import was neither a new profile or a confirmable merge, test that
// the corresponding histograms are unchanged.
if (!is_new_profile && !is_confirmable_merge) {
ExpectEmptyHistograms(
histogram_tester,
{kNewProfileEditsHistogram, kNewProfileDecisionHistogram,
kNewProfileWithComplementedCountryDecisionHistogram,
kNewProfileWithIgnoredCountryDecisionHistogram,
kProfileUpdateEditsHistogram, kProfileUpdateDecisionHistogram,
kProfileUpdateWithComplementedCountryDecisionHistogram,
kProfileUpdateWithIgnoredCountryDecisionHistogram});
} else {
DCHECK(!is_new_profile || !is_confirmable_merge);
const std::string affected_decision_histo =
is_new_profile ? kNewProfileDecisionHistogram
: kProfileUpdateDecisionHistogram;
const std::string unaffected_decision_histo =
!is_new_profile ? kNewProfileDecisionHistogram
: kProfileUpdateDecisionHistogram;
const std::string affected_edits_histo = is_new_profile
? kNewProfileEditsHistogram
: kProfileUpdateEditsHistogram;
const std::string unaffected_edits_histo =
!is_new_profile ? kNewProfileEditsHistogram
: kProfileUpdateEditsHistogram;
const std::string affected_number_of_edits_histo =
is_new_profile ? kNewProfileNumberOfEditsHistogram
: kProfileUpdateNumberOfEditsHistogram;
const std::string unaffected_number_of_edits_histo =
!is_new_profile ? kNewProfileNumberOfEditsHistogram
: kProfileUpdateNumberOfEditsHistogram;
histogram_tester.ExpectTotalCount(unaffected_decision_histo, 0);
histogram_tester.ExpectTotalCount(unaffected_edits_histo, 0);
histogram_tester.ExpectUniqueSample(affected_decision_histo,
test_scenario.user_decision, 1);
histogram_tester.ExpectTotalCount(
affected_edits_histo,
test_scenario.expected_edited_types_for_metrics.size());
// Metrics related to country complemention.
if (import_metadata().did_complement_country) {
if (is_new_profile) {
histogram_tester.ExpectTotalCount(
kProfileUpdateWithComplementedCountryDecisionHistogram, 0);
histogram_tester.ExpectUniqueSample(
kNewProfileWithComplementedCountryDecisionHistogram,
test_scenario.user_decision, 1);
} else {
histogram_tester.ExpectTotalCount(
kNewProfileWithComplementedCountryDecisionHistogram, 0);
// For updates we only expect a difference if the country changed.
if (test_scenario.observed_profile.GetRawInfo(ADDRESS_HOME_COUNTRY) !=
test_scenario.merge_candidate->GetRawInfo(ADDRESS_HOME_COUNTRY)) {
histogram_tester.ExpectUniqueSample(
kProfileUpdateWithComplementedCountryDecisionHistogram,
test_scenario.user_decision, 1);
} else {
histogram_tester.ExpectTotalCount(
kProfileUpdateWithComplementedCountryDecisionHistogram, 0);
}
}
// In case the country was edited, expect increased metrics.
if (base::Contains(
test_scenario.expected_edited_types_for_metrics,
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kCountry)) {
histogram_tester.ExpectTotalCount(
!is_new_profile ? kNewProfileEditComplementedCountryHistogram
: kProfileUpdateEditComplementedCountryHistogram,
0);
histogram_tester.ExpectTotalCount(
is_new_profile ? kNewProfileEditComplementedCountryHistogram
: kProfileUpdateEditComplementedCountryHistogram,
1);
} else {
histogram_tester.ExpectTotalCount(
kNewProfileEditComplementedCountryHistogram, 0);
histogram_tester.ExpectTotalCount(
kProfileUpdateEditComplementedCountryHistogram, 0);
}
} else {
ExpectEmptyHistograms(
histogram_tester,
{kNewProfileWithComplementedCountryDecisionHistogram,
kProfileUpdateWithComplementedCountryDecisionHistogram,
kNewProfileEditComplementedCountryHistogram,
kProfileUpdateEditComplementedCountryHistogram});
}
// Metrics related to ignoring an invalid country.
TestFeatureSpecificNewOrUpdateProfileMetrics(
histogram_tester, test_scenario,
import_metadata().did_ignore_invalid_country,
kNewProfileWithIgnoredCountryDecisionHistogram,
kProfileUpdateWithIgnoredCountryDecisionHistogram);
// Metrics related to removing invalid phone numbers.
TestFeatureSpecificNewOrUpdateProfileMetrics(
histogram_tester, test_scenario,
import_metadata().phone_import_status == PhoneImportStatus::kInvalid,
kNewProfileWithRemovedPhoneNumberDecisionHistogram,
kProfileUpdateWithRemovedPhoneNumberDecisionHistogram);
for (auto edited_type : test_scenario.expected_edited_types_for_metrics) {
histogram_tester.ExpectBucketCount(affected_edits_histo, edited_type, 1);
}
if (test_scenario.user_decision == UserDecision::kEditAccepted) {
histogram_tester.ExpectUniqueSample(
affected_number_of_edits_histo,
test_scenario.expected_edited_types_for_metrics.size(), 1);
histogram_tester.ExpectTotalCount(unaffected_number_of_edits_histo, 0);
}
if (is_confirmable_merge &&
(test_scenario.user_decision == UserDecision::kAccepted ||
test_scenario.user_decision == UserDecision::kDeclined)) {
std::string changed_histogram_suffix;
switch (test_scenario.user_decision) {
case UserDecision::kAccepted:
changed_histogram_suffix = ".Accepted";
break;
case UserDecision::kDeclined:
changed_histogram_suffix = ".Declined";
break;
default:
NOTREACHED() << "Decision not covered by test logic.";
}
for (auto changed_type :
test_scenario.expected_affeceted_types_in_merge_for_metrics) {
histogram_tester.ExpectBucketCount(
base::StrCat({kProfileUpdateAffectedTypesHistogram,
changed_histogram_suffix}),
changed_type, 1);
}
histogram_tester.ExpectUniqueSample(
base::StrCat({kProfileUpdateNumberOfAffectedTypesHistogram,
changed_histogram_suffix}),
test_scenario.expected_affeceted_types_in_merge_for_metrics.size(),
1);
}
}
// Check that the strike count was incremented if the import of a new profile
// was declined.
if (is_new_profile && last_import->UserDeclined()) {
EXPECT_EQ(2, mock_personal_data_manager_.GetProfileSaveStrikeDatabase()
->GetStrikes(url.host()));
} else if (is_new_profile && last_import->UserAccepted()) {
// If the import of a new profile was accepted, the count should have been
// reset.
EXPECT_EQ(0, mock_personal_data_manager_.GetProfileSaveStrikeDatabase()
->GetStrikes(url.host()));
} else {
// In all other cases, the number of strikes should be unaltered.
EXPECT_EQ(
initial_strikes,
mock_personal_data_manager_.GetProfileSaveStrikeDatabase()->GetStrikes(
url.host()));
}
// Check that the strike count for profile updates is reset if a profile was
// updated.
if (is_confirmable_merge &&
(test_scenario.user_decision == UserDecision::kAccepted ||
test_scenario.user_decision == UserDecision::kEditAccepted)) {
EXPECT_EQ(0, mock_personal_data_manager_.GetProfileUpdateStrikeDatabase()
->GetStrikes(test_scenario.merge_candidate->guid()));
} else if (is_confirmable_merge &&
(test_scenario.user_decision == UserDecision::kDeclined ||
test_scenario.user_decision == UserDecision::kMessageDeclined)) {
// Or that it is incremented if the update was declined.
EXPECT_EQ(2, mock_personal_data_manager_.GetProfileUpdateStrikeDatabase()
->GetStrikes(test_scenario.merge_candidate->guid()));
} else if (test_scenario.merge_candidate.has_value()) {
// In all other cases, the number of strikes should be unaltered.
EXPECT_EQ(1, mock_personal_data_manager_.GetProfileUpdateStrikeDatabase()
->GetStrikes(test_scenario.merge_candidate->guid()));
}
VerifyUkmForAddressImport(autofill_client_.GetTestUkmRecorder(),
test_scenario);
}
void AddressProfileSaveManagerTest::VerifyUkmForAddressImport(
const ukm::TestUkmRecorder* ukm_recorder,
ImportScenarioTestCase& test_scenario) {
ASSERT_TRUE(test_scenario.expected_import_type !=
AutofillProfileImportType::kImportTypeUnspecified);
// Verify logged UKM metrics in all scenarios except:
// 1. Duplicates
// 2. Blocked Domain or blocked profile if it is not a silent update.
bool is_ukm_logged =
test_scenario.expected_import_type !=
AutofillProfileImportType::kDuplicateImport &&
test_scenario.expected_import_type !=
AutofillProfileImportType::kSuppressedNewProfile &&
test_scenario.expected_import_type !=
AutofillProfileImportType::kSuppressedConfirmableMerge &&
test_scenario.expected_import_type !=
AutofillProfileImportType::kUnusableIncompleteProfile &&
test_scenario.expected_import_type !=
AutofillProfileImportType::kSuppressedConfirmableMergeAndSilentUpdate;
auto entries =
ukm_recorder->GetEntriesByName(UkmAddressProfileImportType::kEntryName);
ASSERT_EQ(entries.size(), is_ukm_logged ? 1u : 0u);
if (is_ukm_logged) {
ASSERT_EQ(5u, entries[0]->metrics.size());
ukm_recorder->ExpectEntryMetric(
entries[0],
UkmAddressProfileImportType::kAutocompleteUnrecognizedImportName,
import_metadata().did_import_from_unrecognized_autocomplete_field);
ukm_recorder->ExpectEntryMetric(
entries[0], UkmAddressProfileImportType::kImportTypeName,
static_cast<int64_t>(test_scenario.expected_import_type));
ukm_recorder->ExpectEntryMetric(
entries[0], UkmAddressProfileImportType::kNumberOfEditedFieldsName,
test_scenario.expected_edited_types_for_metrics.size());
ukm_recorder->ExpectEntryMetric(
entries[0], UkmAddressProfileImportType::kPhoneNumberStatusName,
static_cast<int64_t>(import_metadata().phone_import_status));
ukm_recorder->ExpectEntryMetric(
entries[0], UkmAddressProfileImportType::kUserDecisionName,
static_cast<int64_t>(test_scenario.user_decision));
}
}
// Test that a profile is correctly imported when no other profile is stored
// yet.
TEST_P(AddressProfileSaveManagerTest, SaveNewProfile) {
AutofillProfile observed_profile = test::StandardProfile();
ImportScenarioTestCase test_scenario{
.existing_profiles = {},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kAccepted,
.expected_import_type = AutofillProfileImportType::kNewProfile,
.is_profile_change_expected = true,
.merge_candidate = absl::nullopt,
.import_candidate = observed_profile,
.expected_final_profiles = {observed_profile}};
TestImportScenario(test_scenario);
}
// Test that a profile is correctly imported when no other profile is stored
// yet but another profile is added while waiting for the user response.
TEST_P(AddressProfileSaveManagerTest, SaveNewProfile_ProfileAddedWhileWaiting) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile profile_added_while_waiting =
test::DifferentFromStandardProfile();
ImportScenarioTestCase test_scenario{
.existing_profiles = {},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kAccepted,
.expected_import_type = AutofillProfileImportType::kNewProfile,
.is_profile_change_expected = true,
.merge_candidate = absl::nullopt,
.import_candidate = observed_profile,
.expected_final_profiles = {observed_profile,
profile_added_while_waiting},
.profile_to_be_added_while_waiting = profile_added_while_waiting};
TestImportScenario(test_scenario);
}
// Test that a profile is not imported and that the user is not prompted if the
// domain is blocked for importing new profiles.
TEST_P(AddressProfileSaveManagerTest, SaveNewProfileOnBlockedDomain) {
AutofillProfile observed_profile = test::StandardProfile();
ImportScenarioTestCase test_scenario{
.existing_profiles = {},
.observed_profile = observed_profile,
.is_prompt_expected = false,
.user_decision = UserDecision::kUserNotAsked,
.expected_import_type = AutofillProfileImportType::kSuppressedNewProfile,
.is_profile_change_expected = false,
.merge_candidate = absl::nullopt,
.import_candidate = absl::nullopt,
.expected_final_profiles = {},
.new_profiles_suppresssed_for_domain = true};
TestImportScenario(test_scenario);
}
// Test that a profile is correctly imported when no other profile is stored
// yet. Here, `kUserNotAsked` is supplied which is done as a fallback in case
// the UI is unavailable for technical reasons.
TEST_P(AddressProfileSaveManagerTest, SaveNewProfile_UserNotAskedFallback) {
AutofillProfile observed_profile = test::StandardProfile();
ImportScenarioTestCase test_scenario{
.existing_profiles = {},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kUserNotAsked,
.expected_import_type = AutofillProfileImportType::kNewProfile,
.is_profile_change_expected = true,
.merge_candidate = absl::nullopt,
.import_candidate = observed_profile,
.expected_final_profiles = {observed_profile}};
TestImportScenario(test_scenario);
}
// Test that a profile is correctly imported when no other profile is stored
// yet. Here, the profile is edited by the user.
TEST_P(AddressProfileSaveManagerTest, SaveNewProfile_Edited) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile edited_profile = test::DifferentFromStandardProfile();
// The edited profile must have the same GUID then the observed one.
test::CopyGUID(observed_profile, &edited_profile);
ImportScenarioTestCase test_scenario{
.existing_profiles = {},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kEditAccepted,
.edited_profile = edited_profile,
.expected_import_type = AutofillProfileImportType::kNewProfile,
.is_profile_change_expected = true,
.merge_candidate = absl::nullopt,
.import_candidate = observed_profile,
.expected_final_profiles = {edited_profile},
.expected_edited_types_for_metrics = {
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kName,
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kStreetAddress,
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kCity,
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kZip}};
TestImportScenario(test_scenario);
}
// Test that the country complemention metric is correctly increased.
TEST_P(AddressProfileSaveManagerTest, SaveNewProfile_EditedCountry) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile edited_profile = observed_profile;
edited_profile.SetRawInfoWithVerificationStatus(
ADDRESS_HOME_COUNTRY, u"DE", VerificationStatus::kObserved);
ImportScenarioTestCase test_scenario{
.existing_profiles = {},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kEditAccepted,
.edited_profile = edited_profile,
.expected_import_type = AutofillProfileImportType::kNewProfile,
.is_profile_change_expected = true,
.merge_candidate = absl::nullopt,
.import_candidate = observed_profile,
.expected_final_profiles = {edited_profile},
.expected_edited_types_for_metrics = {
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kCountry}};
TestImportScenario(test_scenario);
}
// Test that a decline to import a new profile is handled correctly.
TEST_P(AddressProfileSaveManagerTest, SaveNewProfile_Declined) {
AutofillProfile observed_profile = test::StandardProfile();
ImportScenarioTestCase test_scenario{
.existing_profiles = {},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kDeclined,
.expected_import_type = AutofillProfileImportType::kNewProfile,
.is_profile_change_expected = false,
.merge_candidate = absl::nullopt,
.import_candidate = observed_profile,
.expected_final_profiles = {}};
TestImportScenario(test_scenario);
}
// Test that a decline to import a new profile in the message UI is handled
// correctly.
TEST_P(AddressProfileSaveManagerTest, SaveNewProfile_MessageDeclined) {
AutofillProfile observed_profile = test::StandardProfile();
ImportScenarioTestCase test_scenario{
.existing_profiles = {},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kMessageDeclined,
.expected_import_type = AutofillProfileImportType::kNewProfile,
.is_profile_change_expected = false,
.merge_candidate = absl::nullopt,
.import_candidate = observed_profile,
.expected_final_profiles = {}};
TestImportScenario(test_scenario);
}
// Test that the observation of a duplicate profile has no effect.
TEST_P(AddressProfileSaveManagerTest, ImportDuplicateProfile) {
// Note that the profile is created twice to enforce different GUIDs.
AutofillProfile existing_profile = test::StandardProfile();
AutofillProfile observed_profile = test::StandardProfile();
ImportScenarioTestCase test_scenario{
.existing_profiles = {existing_profile},
.observed_profile = observed_profile,
.is_prompt_expected = false,
.user_decision = UserDecision::kAccepted,
.expected_import_type = AutofillProfileImportType::kDuplicateImport,
.is_profile_change_expected = false,
.merge_candidate = absl::nullopt,
.import_candidate = absl::nullopt,
.expected_final_profiles = {existing_profile}};
TestImportScenario(test_scenario);
}
// Test that the observation of quasi identical profile that has a different
// structure in the name will result in a silent update.
TEST_P(AddressProfileSaveManagerTest, SilentlyUpdateProfile) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile updateable_profile = test::UpdateableStandardProfile();
AutofillProfile final_profile = observed_profile;
test::CopyGUID(updateable_profile, &final_profile);
ImportScenarioTestCase test_scenario{
.existing_profiles = {updateable_profile},
.observed_profile = observed_profile,
.is_prompt_expected = false,
.user_decision = UserDecision::kUserNotAsked,
.expected_import_type = AutofillProfileImportType::kSilentUpdate,
.is_profile_change_expected = true,
.merge_candidate = absl::nullopt,
.import_candidate = absl::nullopt,
.expected_final_profiles = {final_profile}};
TestImportScenario(test_scenario);
}
// Test that the observation of quasi identical profile that has a different
// structure in the name will result in a silent update even though the domain
// is blocked for new profile imports.
TEST_P(AddressProfileSaveManagerTest, SilentlyUpdateProfileOnBlockedDomain) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile updateable_profile = test::UpdateableStandardProfile();
AutofillProfile final_profile = observed_profile;
test::CopyGUID(updateable_profile, &final_profile);
ImportScenarioTestCase test_scenario{
.existing_profiles = {updateable_profile},
.observed_profile = observed_profile,
.is_prompt_expected = false,
.user_decision = UserDecision::kUserNotAsked,
.expected_import_type = AutofillProfileImportType::kSilentUpdate,
.is_profile_change_expected = true,
.merge_candidate = absl::nullopt,
.import_candidate = absl::nullopt,
.expected_final_profiles = {final_profile},
.new_profiles_suppresssed_for_domain = true};
TestImportScenario(test_scenario);
}
// Test that the observation of quasi identical profile that has a different
// structure in the name will result in a silent update even when the profile
// has the legacy property of being verified.
TEST_P(AddressProfileSaveManagerTest, SilentlyUpdateVerifiedProfile) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile updateable_profile = test::UpdateableStandardProfile();
updateable_profile.set_origin(kSettingsOrigin);
ASSERT_TRUE(updateable_profile.IsVerified());
AutofillProfile final_profile = observed_profile;
test::CopyGUID(updateable_profile, &final_profile);
ImportScenarioTestCase test_scenario{
.existing_profiles = {updateable_profile},
.observed_profile = observed_profile,
.is_prompt_expected = false,
.user_decision = UserDecision::kUserNotAsked,
.expected_import_type = AutofillProfileImportType::kSilentUpdate,
.is_profile_change_expected = true,
.merge_candidate = absl::nullopt,
.import_candidate = absl::nullopt,
.expected_final_profiles = {final_profile}};
TestImportScenario(test_scenario);
}
// Test the observation of a profile that can only be merged with a
// settings-visible change. Here, `kUserNotAsked` is returned as the fallback
// mechanism when the UI is not available for technical reasons.
TEST_P(AddressProfileSaveManagerTest,
UserConfirmableMerge_UserNotAskedFallback) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile mergeable_profile = test::SubsetOfStandardProfile();
AutofillProfile final_profile = observed_profile;
test::CopyGUID(mergeable_profile, &final_profile);
ImportScenarioTestCase test_scenario{
.existing_profiles = {mergeable_profile},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kUserNotAsked,
.expected_import_type = AutofillProfileImportType::kConfirmableMerge,
.is_profile_change_expected = true,
.merge_candidate = mergeable_profile,
.import_candidate = final_profile,
.expected_final_profiles = {final_profile}};
TestImportScenario(test_scenario);
}
// Test the observation of a profile that can only be merged with a
// settings-visible change.
TEST_P(AddressProfileSaveManagerTest, UserConfirmableMerge) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile mergeable_profile = test::SubsetOfStandardProfile();
AutofillProfile final_profile = observed_profile;
test::CopyGUID(mergeable_profile, &final_profile);
ImportScenarioTestCase test_scenario{
.existing_profiles = {mergeable_profile},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kAccepted,
.expected_import_type = AutofillProfileImportType::kConfirmableMerge,
.is_profile_change_expected = true,
.merge_candidate = mergeable_profile,
.import_candidate = final_profile,
.expected_final_profiles = {final_profile},
.expected_affeceted_types_in_merge_for_metrics = {
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kZip,
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kCity}};
TestImportScenario(test_scenario);
}
// Test the observation of a profile that can only be merged with a
// settings-visible change but the mergeable profile is blocked for updates.
TEST_P(AddressProfileSaveManagerTest, UserConfirmableMerge_BlockedProfile) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile mergeable_profile = test::SubsetOfStandardProfile();
AutofillProfile final_profile = observed_profile;
test::CopyGUID(mergeable_profile, &final_profile);
ImportScenarioTestCase test_scenario{
.existing_profiles = {mergeable_profile},
.observed_profile = observed_profile,
.is_prompt_expected = false,
.user_decision = UserDecision::kUserNotAsked,
.expected_import_type =
AutofillProfileImportType::kSuppressedConfirmableMerge,
.is_profile_change_expected = false,
.expected_final_profiles = {mergeable_profile},
.blocked_guids_for_updates = {mergeable_profile.guid()}};
TestImportScenario(test_scenario);
}
// Test the observation of a profile that can only be merged with a
// settings-visible change. The existing profile has the legacy property of
// being verified.
TEST_P(AddressProfileSaveManagerTest, UserConfirmableMerge_VerifiedProfile) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile mergeable_profile = test::SubsetOfStandardProfile();
mergeable_profile.set_origin(kSettingsOrigin);
ASSERT_TRUE(mergeable_profile.IsVerified());
AutofillProfile final_profile = observed_profile;
test::CopyGUID(mergeable_profile, &final_profile);
ImportScenarioTestCase test_scenario{
.existing_profiles = {mergeable_profile},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kAccepted,
.expected_import_type = AutofillProfileImportType::kConfirmableMerge,
.is_profile_change_expected = true,
.merge_candidate = mergeable_profile,
.import_candidate = final_profile,
.expected_final_profiles = {final_profile},
.expected_affeceted_types_in_merge_for_metrics = {
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kZip,
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kCity}};
TestImportScenario(test_scenario);
}
// Test the observation of a profile that can only be merged with a
// settings-visible change.
TEST_P(AddressProfileSaveManagerTest, UserConfirmableMerge_Edited) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile mergeable_profile = test::SubsetOfStandardProfile();
AutofillProfile edited_profile = test::DifferentFromStandardProfile();
AutofillProfile import_candidate = observed_profile;
test::CopyGUID(mergeable_profile, &import_candidate);
test::CopyGUID(mergeable_profile, &edited_profile);
ImportScenarioTestCase test_scenario{
.existing_profiles = {mergeable_profile},
.observed_profile = observed_profile,
.is_prompt_expected = true,
.user_decision = UserDecision::kEditAccepted,
.edited_profile = edited_profile,
.expected_import_type = AutofillProfileImportType::kConfirmableMerge,
.is_profile_change_expected = true,
.merge_candidate = mergeable_profile,
.import_candidate = import_candidate,
.expected_final_profiles = {edited_profile},
.expected_edited_types_for_metrics = {
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kName,
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kStreetAddress,
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kCity,
AutofillMetrics::SettingsVisibleFieldTypeForMetrics::kZip}};
TestImportScenario(test_scenario);
}
// Test the observation of a profile that can only be merged with a
// settings-visible change but the import is declined by the user.
TEST_P(AddressProfileSaveManagerTest, UserConfirmableMerge_Declined) {
AutofillProfile observed_profile = test::StandardProfile();
AutofillProfile mergeable_profile = test::SubsetOfStandardProfile();
AutofillProfile final_profile = observed_profile;