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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// 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/autofill_metrics.h"
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/histogram_tester.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/autofill_external_delegate.h"
#include "components/autofill/core/browser/autofill_manager.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/test_autofill_client.h"
#include "components/autofill/core/browser/test_autofill_driver.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/webdata/common/web_data_results.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/rect.h"
#include "url/gurl.h"
using base::ASCIIToUTF16;
using base::TimeTicks;
namespace autofill {
namespace {
class TestPersonalDataManager : public PersonalDataManager {
public:
TestPersonalDataManager()
: PersonalDataManager("en-US"), autofill_enabled_(true) {
CreateTestAutofillProfiles(&web_profiles_);
}
using PersonalDataManager::set_database;
using PersonalDataManager::SetPrefService;
// Overridden to avoid a trip to the database. This should be a no-op except
// for the side-effect of logging the profile count.
void LoadProfiles() override {
std::vector<AutofillProfile*> profiles;
web_profiles_.release(&profiles);
WDResult<std::vector<AutofillProfile*> > result(AUTOFILL_PROFILES_RESULT,
profiles);
pending_profiles_query_ = 123;
OnWebDataServiceRequestDone(pending_profiles_query_, &result);
}
// Overridden to avoid a trip to the database.
void LoadCreditCards() override {}
void set_autofill_enabled(bool autofill_enabled) {
autofill_enabled_ = autofill_enabled;
}
bool IsAutofillEnabled() const override { return autofill_enabled_; }
private:
void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
AutofillProfile* profile = new AutofillProfile;
test::SetProfileInfo(profile, "Elvis", "Aaron",
"Presley", "theking@gmail.com", "RCA",
"3734 Elvis Presley Blvd.", "Apt. 10",
"Memphis", "Tennessee", "38116", "US",
"12345678901");
profile->set_guid("00000000-0000-0000-0000-000000000001");
profiles->push_back(profile);
profile = new AutofillProfile;
test::SetProfileInfo(profile, "Charles", "Hardin",
"Holley", "buddy@gmail.com", "Decca",
"123 Apple St.", "unit 6", "Lubbock",
"Texas", "79401", "US", "2345678901");
profile->set_guid("00000000-0000-0000-0000-000000000002");
profiles->push_back(profile);
}
bool autofill_enabled_;
DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
};
class TestFormStructure : public FormStructure {
public:
explicit TestFormStructure(const FormData& form) : FormStructure(form) {}
~TestFormStructure() override {}
void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types,
const std::vector<ServerFieldType>& server_types) {
ASSERT_EQ(field_count(), heuristic_types.size());
ASSERT_EQ(field_count(), server_types.size());
for (size_t i = 0; i < field_count(); ++i) {
AutofillField* form_field = field(i);
ASSERT_TRUE(form_field);
form_field->set_heuristic_type(heuristic_types[i]);
form_field->set_server_type(server_types[i]);
}
UpdateAutofillCount();
}
private:
DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
};
class TestAutofillManager : public AutofillManager {
public:
TestAutofillManager(AutofillDriver* driver,
AutofillClient* autofill_client,
TestPersonalDataManager* personal_manager)
: AutofillManager(driver, autofill_client, personal_manager),
autofill_enabled_(true) {}
~TestAutofillManager() override {}
bool IsAutofillEnabled() const override { return autofill_enabled_; }
void set_autofill_enabled(bool autofill_enabled) {
autofill_enabled_ = autofill_enabled;
}
void AddSeenForm(const FormData& form,
const std::vector<ServerFieldType>& heuristic_types,
const std::vector<ServerFieldType>& server_types) {
FormData empty_form = form;
for (size_t i = 0; i < empty_form.fields.size(); ++i) {
empty_form.fields[i].value = base::string16();
}
// |form_structure| will be owned by |form_structures()|.
TestFormStructure* form_structure = new TestFormStructure(empty_form);
form_structure->SetFieldTypes(heuristic_types, server_types);
form_structures()->push_back(form_structure);
}
void FormSubmitted(const FormData& form, const TimeTicks& timestamp) {
run_loop_.reset(new base::RunLoop());
if (!OnFormSubmitted(form, timestamp))
return;
// Wait for the asynchronous FormSubmitted() call to complete.
run_loop_->Run();
}
void UploadFormDataAsyncCallback(
const FormStructure* submitted_form,
const base::TimeTicks& load_time,
const base::TimeTicks& interaction_time,
const base::TimeTicks& submission_time) override {
run_loop_->Quit();
AutofillManager::UploadFormDataAsyncCallback(submitted_form,
load_time,
interaction_time,
submission_time);
}
private:
bool autofill_enabled_;
scoped_ptr<base::RunLoop> run_loop_;
DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
};
} // namespace
// This is defined in the autofill_metrics.cc implementation file.
int GetFieldTypeGroupMetric(ServerFieldType field_type,
AutofillMetrics::FieldTypeQualityMetric metric);
class AutofillMetricsTest : public testing::Test {
public:
~AutofillMetricsTest() override;
void SetUp() override;
void TearDown() override;
protected:
base::MessageLoop message_loop_;
TestAutofillClient autofill_client_;
scoped_ptr<TestAutofillDriver> autofill_driver_;
scoped_ptr<TestAutofillManager> autofill_manager_;
scoped_ptr<TestPersonalDataManager> personal_data_;
scoped_ptr<AutofillExternalDelegate> external_delegate_;
};
AutofillMetricsTest::~AutofillMetricsTest() {
// Order of destruction is important as AutofillManager relies on
// PersonalDataManager to be around when it gets destroyed.
autofill_manager_.reset();
}
void AutofillMetricsTest::SetUp() {
autofill_client_.SetPrefs(test::PrefServiceForTesting());
// Ensure Mac OS X does not pop up a modal dialog for the Address Book.
test::DisableSystemServices(autofill_client_.GetPrefs());
personal_data_.reset(new TestPersonalDataManager());
personal_data_->set_database(autofill_client_.GetDatabase());
personal_data_->SetPrefService(autofill_client_.GetPrefs());
autofill_driver_.reset(new TestAutofillDriver());
autofill_manager_.reset(new TestAutofillManager(
autofill_driver_.get(), &autofill_client_, personal_data_.get()));
external_delegate_.reset(new AutofillExternalDelegate(
autofill_manager_.get(),
autofill_driver_.get()));
autofill_manager_->SetExternalDelegate(external_delegate_.get());
}
void AutofillMetricsTest::TearDown() {
// Order of destruction is important as AutofillManager relies on
// PersonalDataManager to be around when it gets destroyed.
autofill_manager_.reset();
autofill_driver_.reset();
personal_data_.reset();
}
// Test that we log quality metrics appropriately.
TEST_F(AutofillMetricsTest, QualityMetrics) {
// Set up our form data.
FormData form;
form.name = ASCIIToUTF16("TestForm");
form.origin = GURL("http://example.com/form.html");
form.action = GURL("http://example.com/submit.html");
form.user_submitted = true;
std::vector<ServerFieldType> heuristic_types, server_types;
FormFieldData field;
test::CreateTestFormField(
"Autofilled", "autofilled", "Elvis Aaron Presley", "text", &field);
field.is_autofilled = true;
form.fields.push_back(field);
heuristic_types.push_back(NAME_FULL);
server_types.push_back(NAME_FIRST);
test::CreateTestFormField(
"Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
field.is_autofilled = false;
form.fields.push_back(field);
heuristic_types.push_back(PHONE_HOME_NUMBER);
server_types.push_back(EMAIL_ADDRESS);
test::CreateTestFormField("Empty", "empty", "", "text", &field);
field.is_autofilled = false;
form.fields.push_back(field);
heuristic_types.push_back(NAME_FULL);
server_types.push_back(NAME_FIRST);
test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
field.is_autofilled = false;
form.fields.push_back(field);
heuristic_types.push_back(PHONE_HOME_NUMBER);
server_types.push_back(EMAIL_ADDRESS);
test::CreateTestFormField("Select", "select", "USA", "select-one", &field);
field.is_autofilled = false;
form.fields.push_back(field);
heuristic_types.push_back(UNKNOWN_TYPE);
server_types.push_back(NO_SERVER_DATA);
test::CreateTestFormField("Phone", "phone", "2345678901", "tel", &field);
field.is_autofilled = true;
form.fields.push_back(field);
heuristic_types.push_back(PHONE_HOME_CITY_AND_NUMBER);
server_types.push_back(PHONE_HOME_WHOLE_NUMBER);
// Simulate having seen this form on page load.
autofill_manager_->AddSeenForm(form, heuristic_types, server_types);
// Simulate form submission.
base::HistogramTester histogram_tester;
autofill_manager_->FormSubmitted(form, TimeTicks::Now());
// Heuristic predictions.
// Unknown:
histogram_tester.ExpectBucketCount("Autofill.Quality.HeuristicType",
AutofillMetrics::TYPE_UNKNOWN, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.HeuristicType.ByFieldType",
GetFieldTypeGroupMetric(ADDRESS_HOME_COUNTRY,
AutofillMetrics::TYPE_UNKNOWN),
1);
// Match:
histogram_tester.ExpectBucketCount("Autofill.Quality.HeuristicType",
AutofillMetrics::TYPE_MATCH, 2);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.HeuristicType.ByFieldType",
GetFieldTypeGroupMetric(NAME_FULL, AutofillMetrics::TYPE_MATCH), 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.HeuristicType.ByFieldType",
GetFieldTypeGroupMetric(PHONE_HOME_WHOLE_NUMBER,
AutofillMetrics::TYPE_MATCH),
1);
// Mismatch:
histogram_tester.ExpectBucketCount("Autofill.Quality.HeuristicType",
AutofillMetrics::TYPE_MISMATCH, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.HeuristicType.ByFieldType",
GetFieldTypeGroupMetric(EMAIL_ADDRESS, AutofillMetrics::TYPE_MISMATCH),
1);
// Server predictions:
// Unknown:
histogram_tester.ExpectBucketCount("Autofill.Quality.ServerType",
AutofillMetrics::TYPE_UNKNOWN, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.ServerType.ByFieldType",
GetFieldTypeGroupMetric(ADDRESS_HOME_COUNTRY,
AutofillMetrics::TYPE_UNKNOWN),
1);
// Match:
histogram_tester.ExpectBucketCount("Autofill.Quality.ServerType",
AutofillMetrics::TYPE_MATCH, 2);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.ServerType.ByFieldType",
GetFieldTypeGroupMetric(EMAIL_ADDRESS, AutofillMetrics::TYPE_MATCH), 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.ServerType.ByFieldType",
GetFieldTypeGroupMetric(PHONE_HOME_WHOLE_NUMBER,
AutofillMetrics::TYPE_MATCH),
1);
// Mismatch:
histogram_tester.ExpectBucketCount("Autofill.Quality.ServerType",
AutofillMetrics::TYPE_MISMATCH, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.ServerType.ByFieldType",
GetFieldTypeGroupMetric(NAME_FULL, AutofillMetrics::TYPE_MISMATCH), 1);
// Overall predictions:
// Unknown:
histogram_tester.ExpectBucketCount("Autofill.Quality.PredictedType",
AutofillMetrics::TYPE_UNKNOWN, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.PredictedType.ByFieldType",
GetFieldTypeGroupMetric(ADDRESS_HOME_COUNTRY,
AutofillMetrics::TYPE_UNKNOWN),
1);
// Match:
histogram_tester.ExpectBucketCount("Autofill.Quality.PredictedType",
AutofillMetrics::TYPE_MATCH, 2);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.PredictedType.ByFieldType",
GetFieldTypeGroupMetric(EMAIL_ADDRESS, AutofillMetrics::TYPE_MATCH), 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.PredictedType.ByFieldType",
GetFieldTypeGroupMetric(PHONE_HOME_WHOLE_NUMBER,
AutofillMetrics::TYPE_MATCH),
1);
// Mismatch:
histogram_tester.ExpectBucketCount("Autofill.Quality.PredictedType",
AutofillMetrics::TYPE_MISMATCH, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.PredictedType.ByFieldType",
GetFieldTypeGroupMetric(NAME_FULL, AutofillMetrics::TYPE_MISMATCH), 1);
}
// Test that we behave sanely when the cached form differs from the submitted
// one.
TEST_F(AutofillMetricsTest, SaneMetricsWithCacheMismatch) {
// Set up our form data.
FormData form;
form.name = ASCIIToUTF16("TestForm");
form.origin = GURL("http://example.com/form.html");
form.action = GURL("http://example.com/submit.html");
form.user_submitted = true;
std::vector<ServerFieldType> heuristic_types, server_types;
FormFieldData field;
test::CreateTestFormField(
"Both match", "match", "Elvis Aaron Presley", "text", &field);
field.is_autofilled = true;
form.fields.push_back(field);
heuristic_types.push_back(NAME_FULL);
server_types.push_back(NAME_FULL);
test::CreateTestFormField(
"Both mismatch", "mismatch", "buddy@gmail.com", "text", &field);
field.is_autofilled = false;
form.fields.push_back(field);
heuristic_types.push_back(PHONE_HOME_NUMBER);
server_types.push_back(PHONE_HOME_NUMBER);
test::CreateTestFormField(
"Only heuristics match", "mixed", "Memphis", "text", &field);
field.is_autofilled = false;
form.fields.push_back(field);
heuristic_types.push_back(ADDRESS_HOME_CITY);
server_types.push_back(PHONE_HOME_NUMBER);
test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
field.is_autofilled = false;
form.fields.push_back(field);
heuristic_types.push_back(UNKNOWN_TYPE);
server_types.push_back(UNKNOWN_TYPE);
// Simulate having seen this form with the desired heuristic and server types.
// |form_structure| will be owned by |autofill_manager_|.
autofill_manager_->AddSeenForm(form, heuristic_types, server_types);
// Add a field and re-arrange the remaining form fields before submitting.
std::vector<FormFieldData> cached_fields = form.fields;
form.fields.clear();
test::CreateTestFormField(
"New field", "new field", "Tennessee", "text", &field);
form.fields.push_back(field);
form.fields.push_back(cached_fields[2]);
form.fields.push_back(cached_fields[1]);
form.fields.push_back(cached_fields[3]);
form.fields.push_back(cached_fields[0]);
// Simulate form submission.
base::HistogramTester histogram_tester;
autofill_manager_->FormSubmitted(form, TimeTicks::Now());
// Heuristic predictions.
// Unknown:
histogram_tester.ExpectBucketCount("Autofill.Quality.HeuristicType",
AutofillMetrics::TYPE_UNKNOWN, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.HeuristicType.ByFieldType",
GetFieldTypeGroupMetric(ADDRESS_HOME_STATE,
AutofillMetrics::TYPE_UNKNOWN),
1);
// Match:
histogram_tester.ExpectBucketCount("Autofill.Quality.HeuristicType",
AutofillMetrics::TYPE_MATCH, 2);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.HeuristicType.ByFieldType",
GetFieldTypeGroupMetric(ADDRESS_HOME_CITY, AutofillMetrics::TYPE_MATCH),
1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.HeuristicType.ByFieldType",
GetFieldTypeGroupMetric(NAME_FULL, AutofillMetrics::TYPE_MATCH), 1);
// Mismatch:
histogram_tester.ExpectBucketCount("Autofill.Quality.HeuristicType",
AutofillMetrics::TYPE_MISMATCH, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.HeuristicType.ByFieldType",
GetFieldTypeGroupMetric(EMAIL_ADDRESS, AutofillMetrics::TYPE_MISMATCH),
1);
// Server predictions:
// Unknown:
histogram_tester.ExpectBucketCount("Autofill.Quality.ServerType",
AutofillMetrics::TYPE_UNKNOWN, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.ServerType.ByFieldType",
GetFieldTypeGroupMetric(ADDRESS_HOME_STATE,
AutofillMetrics::TYPE_UNKNOWN),
1);
// Match:
histogram_tester.ExpectBucketCount("Autofill.Quality.ServerType",
AutofillMetrics::TYPE_MATCH, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.ServerType.ByFieldType",
GetFieldTypeGroupMetric(NAME_FULL, AutofillMetrics::TYPE_MATCH), 1);
// Mismatch:
histogram_tester.ExpectBucketCount("Autofill.Quality.ServerType",
AutofillMetrics::TYPE_MISMATCH, 2);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.ServerType.ByFieldType",
GetFieldTypeGroupMetric(ADDRESS_HOME_CITY,
AutofillMetrics::TYPE_MISMATCH),
1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.ServerType.ByFieldType",
GetFieldTypeGroupMetric(EMAIL_ADDRESS, AutofillMetrics::TYPE_MISMATCH),
1);
// Overall predictions:
// Unknown:
histogram_tester.ExpectBucketCount("Autofill.Quality.PredictedType",
AutofillMetrics::TYPE_UNKNOWN, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.PredictedType.ByFieldType",
GetFieldTypeGroupMetric(ADDRESS_HOME_STATE,
AutofillMetrics::TYPE_UNKNOWN),
1);
// Match:
histogram_tester.ExpectBucketCount("Autofill.Quality.PredictedType",
AutofillMetrics::TYPE_MATCH, 1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.PredictedType.ByFieldType",
GetFieldTypeGroupMetric(NAME_FULL, AutofillMetrics::TYPE_MATCH), 1);
// Mismatch:
histogram_tester.ExpectBucketCount("Autofill.Quality.PredictedType",
AutofillMetrics::TYPE_MISMATCH, 2);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.PredictedType.ByFieldType",
GetFieldTypeGroupMetric(ADDRESS_HOME_CITY,
AutofillMetrics::TYPE_MISMATCH),
1);
histogram_tester.ExpectBucketCount(
"Autofill.Quality.PredictedType.ByFieldType",
GetFieldTypeGroupMetric(EMAIL_ADDRESS, AutofillMetrics::TYPE_MISMATCH),
1);
}
// Verify that we correctly log metrics regarding developer engagement.
TEST_F(AutofillMetricsTest, DeveloperEngagement) {
// Start with a non-fillable form.
FormData form;
form.name = ASCIIToUTF16("TestForm");
form.origin = GURL("http://example.com/form.html");
form.action = GURL("http://example.com/submit.html");
FormFieldData field;
test::CreateTestFormField("Name", "name", "", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Email", "email", "", "text", &field);
form.fields.push_back(field);
std::vector<FormData> forms(1, form);
// Ensure no metrics are logged when loading a non-fillable form.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks());
autofill_manager_->Reset();
histogram_tester.ExpectTotalCount("Autofill.DeveloperEngagement", 0);
}
// Add another field to the form, so that it becomes fillable.
test::CreateTestFormField("Phone", "phone", "", "text", &field);
forms.back().fields.push_back(field);
// Expect only the "form parsed" metric to be logged; no metrics about
// author-specified field type hints.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks());
autofill_manager_->Reset();
histogram_tester.ExpectUniqueSample("Autofill.DeveloperEngagement",
AutofillMetrics::FILLABLE_FORM_PARSED,
1);
}
// Add some fields with an author-specified field type to the form.
// We need to add at least three fields, because a form must have at least
// three fillable fields to be considered to be autofillable; and if at least
// one field specifies an explicit type hint, we don't apply any of our usual
// local heuristics to detect field types in the rest of the form.
test::CreateTestFormField("", "", "", "text", &field);
field.autocomplete_attribute = "given-name";
forms.back().fields.push_back(field);
test::CreateTestFormField("", "", "", "text", &field);
field.autocomplete_attribute = "email";
forms.back().fields.push_back(field);
test::CreateTestFormField("", "", "", "text", &field);
field.autocomplete_attribute = "address-line1";
forms.back().fields.push_back(field);
// Expect both the "form parsed" metric and the author-specified field type
// hints metric to be logged.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks());
autofill_manager_->Reset();
histogram_tester.ExpectBucketCount("Autofill.DeveloperEngagement",
AutofillMetrics::FILLABLE_FORM_PARSED,
1);
histogram_tester.ExpectBucketCount(
"Autofill.DeveloperEngagement",
AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS, 1);
}
}
// Test that the profile count is logged correctly.
TEST_F(AutofillMetricsTest, StoredProfileCount) {
// The metric should be logged when the profiles are first loaded.
{
base::HistogramTester histogram_tester;
personal_data_->LoadProfiles();
histogram_tester.ExpectUniqueSample("Autofill.StoredProfileCount", 2, 1);
}
// The metric should only be logged once.
{
base::HistogramTester histogram_tester;
personal_data_->LoadProfiles();
histogram_tester.ExpectTotalCount("Autofill.StoredProfileCount", 0);
}
}
// Test that we correctly log when Autofill is enabled.
TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) {
base::HistogramTester histogram_tester;
personal_data_->set_autofill_enabled(true);
personal_data_->Init(
autofill_client_.GetDatabase(), autofill_client_.GetPrefs(), false);
histogram_tester.ExpectUniqueSample("Autofill.IsEnabled.Startup", true, 1);
}
// Test that we correctly log when Autofill is disabled.
TEST_F(AutofillMetricsTest, AutofillIsDisabledAtStartup) {
base::HistogramTester histogram_tester;
personal_data_->set_autofill_enabled(false);
personal_data_->Init(
autofill_client_.GetDatabase(), autofill_client_.GetPrefs(), false);
histogram_tester.ExpectUniqueSample("Autofill.IsEnabled.Startup", false, 1);
}
// Test that we log the number of Autofill suggestions when filling a form.
TEST_F(AutofillMetricsTest, AddressSuggestionsCount) {
// Set up our form data.
FormData form;
form.name = ASCIIToUTF16("TestForm");
form.origin = GURL("http://example.com/form.html");
form.action = GURL("http://example.com/submit.html");
form.user_submitted = true;
FormFieldData field;
std::vector<ServerFieldType> field_types;
test::CreateTestFormField("Name", "name", "", "text", &field);
form.fields.push_back(field);
field_types.push_back(NAME_FULL);
test::CreateTestFormField("Email", "email", "", "email", &field);
form.fields.push_back(field);
field_types.push_back(EMAIL_ADDRESS);
test::CreateTestFormField("Phone", "phone", "", "tel", &field);
form.fields.push_back(field);
field_types.push_back(PHONE_HOME_NUMBER);
// Simulate having seen this form on page load.
// |form_structure| will be owned by |autofill_manager_|.
autofill_manager_->AddSeenForm(form, field_types, field_types);
{
// Simulate activating the autofill popup for the phone field.
base::HistogramTester histogram_tester;
autofill_manager_->OnQueryFormFieldAutofill(0, form, field, gfx::Rect(),
false);
histogram_tester.ExpectUniqueSample("Autofill.AddressSuggestionsCount", 2,
1);
}
{
// Simulate activating the autofill popup for the email field after typing.
// No new metric should be logged, since we're still on the same page.
test::CreateTestFormField("Email", "email", "b", "email", &field);
base::HistogramTester histogram_tester;
autofill_manager_->OnQueryFormFieldAutofill(0, form, field, gfx::Rect(),
false);
histogram_tester.ExpectTotalCount("Autofill.AddressSuggestionsCount", 0);
}
// Reset the autofill manager state.
autofill_manager_->Reset();
autofill_manager_->AddSeenForm(form, field_types, field_types);
{
// Simulate activating the autofill popup for the email field after typing.
base::HistogramTester histogram_tester;
autofill_manager_->OnQueryFormFieldAutofill(0, form, field, gfx::Rect(),
false);
histogram_tester.ExpectUniqueSample("Autofill.AddressSuggestionsCount", 1,
1);
}
// Reset the autofill manager state again.
autofill_manager_->Reset();
autofill_manager_->AddSeenForm(form, field_types, field_types);
{
// Simulate activating the autofill popup for the email field after typing.
form.fields[0].is_autofilled = true;
base::HistogramTester histogram_tester;
autofill_manager_->OnQueryFormFieldAutofill(0, form, field, gfx::Rect(),
false);
histogram_tester.ExpectTotalCount("Autofill.AddressSuggestionsCount", 0);
}
}
// Test that we log that Autofill is enabled when filling a form.
TEST_F(AutofillMetricsTest, AutofillIsEnabledAtPageLoad) {
base::HistogramTester histogram_tester;
autofill_manager_->set_autofill_enabled(true);
autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks());
histogram_tester.ExpectUniqueSample("Autofill.IsEnabled.PageLoad", true, 1);
}
// Test that we log that Autofill is disabled when filling a form.
TEST_F(AutofillMetricsTest, AutofillIsDisabledAtPageLoad) {
base::HistogramTester histogram_tester;
autofill_manager_->set_autofill_enabled(false);
autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks());
histogram_tester.ExpectUniqueSample("Autofill.IsEnabled.PageLoad", false, 1);
}
// Verify that we correctly log user happiness metrics dealing with form loading
// and form submission.
TEST_F(AutofillMetricsTest, UserHappinessFormLoadAndSubmission) {
// Start with a form with insufficiently many fields.
FormData form;
form.name = ASCIIToUTF16("TestForm");
form.origin = GURL("http://example.com/form.html");
form.action = GURL("http://example.com/submit.html");
form.user_submitted = true;
FormFieldData field;
test::CreateTestFormField("Name", "name", "", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Email", "email", "", "text", &field);
form.fields.push_back(field);
std::vector<FormData> forms(1, form);
// Expect no notifications when the form is first seen.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks());
histogram_tester.ExpectTotalCount("Autofill.UserHappiness", 0);
}
// Expect no notifications when the form is submitted.
{
base::HistogramTester histogram_tester;
autofill_manager_->FormSubmitted(form, TimeTicks::Now());
histogram_tester.ExpectTotalCount("Autofill.UserHappiness", 0);
}
// Add more fields to the form.
test::CreateTestFormField("Phone", "phone", "", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Unknown", "unknown", "", "text", &field);
form.fields.push_back(field);
forms.front() = form;
// Expect a notification when the form is first seen.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks());
histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
AutofillMetrics::FORMS_LOADED, 1);
}
// Expect a notification when the form is submitted.
{
base::HistogramTester histogram_tester;
autofill_manager_->FormSubmitted(form, TimeTicks::Now());
histogram_tester.ExpectUniqueSample(
"Autofill.UserHappiness", AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM,
1);
}
// Fill in two of the fields.
form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
forms.front() = form;
// Expect a notification when the form is submitted.
{
base::HistogramTester histogram_tester;
autofill_manager_->FormSubmitted(form, TimeTicks::Now());
histogram_tester.ExpectUniqueSample(
"Autofill.UserHappiness", AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM,
1);
}
// Fill in the third field.
form.fields[2].value = ASCIIToUTF16("12345678901");
forms.front() = form;
// Expect notifications when the form is submitted.
{
base::HistogramTester histogram_tester;
autofill_manager_->FormSubmitted(form, TimeTicks::Now());
histogram_tester.ExpectUniqueSample(
"Autofill.UserHappiness",
AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE, 1);
}
// Mark one of the fields as autofilled.
form.fields[1].is_autofilled = true;
forms.front() = form;
// Expect notifications when the form is submitted.
{
base::HistogramTester histogram_tester;
autofill_manager_->FormSubmitted(form, TimeTicks::Now());
histogram_tester.ExpectUniqueSample(
"Autofill.UserHappiness",
AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME, 1);
}
// Mark all of the fillable fields as autofilled.
form.fields[0].is_autofilled = true;
form.fields[2].is_autofilled = true;
forms.front() = form;
// Expect notifications when the form is submitted.
{
base::HistogramTester histogram_tester;
autofill_manager_->FormSubmitted(form, TimeTicks::Now());
histogram_tester.ExpectUniqueSample(
"Autofill.UserHappiness",
AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL, 1);
}
// Clear out the third field's value.
form.fields[2].value = base::string16();
forms.front() = form;
// Expect notifications when the form is submitted.
{
base::HistogramTester histogram_tester;
autofill_manager_->FormSubmitted(form, TimeTicks::Now());
histogram_tester.ExpectUniqueSample(
"Autofill.UserHappiness", AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM,
1);
}
}
// Verify that we correctly log user happiness metrics dealing with form
// interaction.
TEST_F(AutofillMetricsTest, UserHappinessFormInteraction) {
// Load a fillable form.
FormData form;
form.name = ASCIIToUTF16("TestForm");
form.origin = GURL("http://example.com/form.html");
form.action = GURL("http://example.com/submit.html");
form.user_submitted = true;
FormFieldData field;
test::CreateTestFormField("Name", "name", "", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Email", "email", "", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Phone", "phone", "", "text", &field);
form.fields.push_back(field);
std::vector<FormData> forms(1, form);
// Expect a notification when the form is first seen.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks());
histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
AutofillMetrics::FORMS_LOADED, 1);
}
// Simulate typing.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
TimeTicks());
histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
AutofillMetrics::USER_DID_TYPE, 1);
}
// Simulate suggestions shown twice for a single edit (i.e. multiple
// keystrokes in a single field).
{
base::HistogramTester histogram_tester;
autofill_manager_->DidShowSuggestions(true);
autofill_manager_->DidShowSuggestions(false);
histogram_tester.ExpectBucketCount("Autofill.UserHappiness",
AutofillMetrics::SUGGESTIONS_SHOWN, 1);
histogram_tester.ExpectBucketCount(
"Autofill.UserHappiness", AutofillMetrics::SUGGESTIONS_SHOWN_ONCE, 1);
}
// Simulate suggestions shown for a different field.
{
base::HistogramTester histogram_tester;
autofill_manager_->DidShowSuggestions(true);
histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
AutofillMetrics::SUGGESTIONS_SHOWN, 1);
}
// Simulate invoking autofill.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
histogram_tester.ExpectBucketCount("Autofill.UserHappiness",
AutofillMetrics::USER_DID_AUTOFILL, 1);
histogram_tester.ExpectBucketCount(
"Autofill.UserHappiness", AutofillMetrics::USER_DID_AUTOFILL_ONCE, 1);
}
// Simulate editing an autofilled field.
{
base::HistogramTester histogram_tester;
SuggestionBackendID guid(
"00000000-0000-0000-0000-000000000001", 0);
autofill_manager_->FillOrPreviewForm(
AutofillDriver::FORM_DATA_ACTION_FILL,
0, form, form.fields.front(),
autofill_manager_->MakeFrontendID(SuggestionBackendID(), guid));
autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
TimeTicks());
// Simulate a second keystroke; make sure we don't log the metric twice.
autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
TimeTicks());
histogram_tester.ExpectBucketCount(
"Autofill.UserHappiness",
AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD, 1);
histogram_tester.ExpectBucketCount(
"Autofill.UserHappiness",
AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE, 1);
}
// Simulate invoking autofill again.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
histogram_tester.ExpectUniqueSample("Autofill.UserHappiness",
AutofillMetrics::USER_DID_AUTOFILL, 1);
}
// Simulate editing another autofilled field.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnTextFieldDidChange(form, form.fields[1], TimeTicks());
histogram_tester.ExpectUniqueSample(
"Autofill.UserHappiness",
AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD, 1);
}
}
// Verify that we correctly log metrics tracking the duration of form fill.
TEST_F(AutofillMetricsTest, FormFillDuration) {
// Load a fillable form.
FormData form;
form.name = ASCIIToUTF16("TestForm");
form.origin = GURL("http://example.com/form.html");
form.action = GURL("http://example.com/submit.html");
form.user_submitted = true;
FormFieldData field;
test::CreateTestFormField("Name", "name", "", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Email", "email", "", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Phone", "phone", "", "text", &field);
form.fields.push_back(field);
std::vector<FormData> forms(1, form);
// Fill additional form.
FormData second_form = form;
test::CreateTestFormField("Second Phone", "second_phone", "", "text", &field);
second_form.fields.push_back(field);
std::vector<FormData> second_forms(1, second_form);
// Fill the field values for form submission.
form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
form.fields[2].value = ASCIIToUTF16("12345678901");
// Fill the field values for form submission.
second_form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
second_form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
second_form.fields[2].value = ASCIIToUTF16("12345678901");
second_form.fields[3].value = ASCIIToUTF16("51512345678");
// Expect only form load metrics to be logged if the form is submitted without
// user interaction.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromLoad.WithAutofill", 0);
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromLoad.WithoutAutofill", 16, 1);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromInteraction.WithAutofill", 0);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
autofill_manager_->Reset();
}
// Expect metric to be logged if the user manually edited a form field.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
TimeTicks::FromInternalValue(3));
autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromLoad.WithAutofill", 0);
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromLoad.WithoutAutofill", 16, 1);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromInteraction.WithAutofill", 0);
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromInteraction.WithoutAutofill", 14, 1);
autofill_manager_->Reset();
}
// Expect metric to be logged if the user autofilled the form.
form.fields[0].is_autofilled = true;
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
autofill_manager_->OnDidFillAutofillFormData(
TimeTicks::FromInternalValue(5));
autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromLoad.WithAutofill", 16, 1);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromLoad.WithoutAutofill", 0);
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromInteraction.WithAutofill", 12, 1);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
autofill_manager_->Reset();
}
// Expect metric to be logged if the user both manually filled some fields
// and autofilled others. Messages can arrive out of order, so make sure they
// take precedence appropriately.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
autofill_manager_->OnDidFillAutofillFormData(
TimeTicks::FromInternalValue(5));
autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
TimeTicks::FromInternalValue(3));
autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromLoad.WithAutofill", 16, 1);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromLoad.WithoutAutofill", 0);
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromInteraction.WithAutofill", 14, 1);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
autofill_manager_->Reset();
}
// Make sure that loading another form doesn't affect metrics from the first
// form.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
autofill_manager_->OnFormsSeen(second_forms,
TimeTicks::FromInternalValue(3));
autofill_manager_->OnDidFillAutofillFormData(
TimeTicks::FromInternalValue(5));
autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
TimeTicks::FromInternalValue(3));
autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromLoad.WithAutofill", 16, 1);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromLoad.WithoutAutofill", 0);
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromInteraction.WithAutofill", 14, 1);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
autofill_manager_->Reset();
}
// Make sure that submitting a form that was loaded later will report the
// later loading time.
{
base::HistogramTester histogram_tester;
autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
autofill_manager_->OnFormsSeen(second_forms,
TimeTicks::FromInternalValue(5));
autofill_manager_->FormSubmitted(second_form,
TimeTicks::FromInternalValue(17));
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromLoad.WithAutofill", 0);
histogram_tester.ExpectUniqueSample(
"Autofill.FillDuration.FromLoad.WithoutAutofill", 12, 1);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromInteraction.WithAutofill", 0);
histogram_tester.ExpectTotalCount(
"Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
autofill_manager_->Reset();
}
}
} // namespace autofill
|