Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: components/autofill/core/browser/autofill_manager_unittest.cc

Issue 2829853008: Stores server card as a full server card when upload to server succeeds. (Closed)
Patch Set: Stores server card as a full server card when upload to server succeeds. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/autofill/core/browser/autofill_manager.h" 5 #include "components/autofill/core/browser/autofill_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory> 10 #include <memory>
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 const std::string& app_locale) override { 114 const std::string& app_locale) override {
115 delegate_->OnDidGetUploadDetails( 115 delegate_->OnDidGetUploadDetails(
116 app_locale == "en-US" ? AutofillClient::SUCCESS 116 app_locale == "en-US" ? AutofillClient::SUCCESS
117 : AutofillClient::PERMANENT_FAILURE, 117 : AutofillClient::PERMANENT_FAILURE,
118 ASCIIToUTF16("this is a context token"), 118 ASCIIToUTF16("this is a context token"),
119 std::unique_ptr<base::DictionaryValue>(nullptr)); 119 std::unique_ptr<base::DictionaryValue>(nullptr));
120 } 120 }
121 121
122 void UploadCard(const payments::PaymentsClient::UploadRequestDetails& 122 void UploadCard(const payments::PaymentsClient::UploadRequestDetails&
123 request_details) override { 123 request_details) override {
124 delegate_->OnDidUploadCard(AutofillClient::SUCCESS); 124 delegate_->OnDidUploadCard(AutofillClient::SUCCESS, server_id_);
125 } 125 }
126 126
127 std::string server_id_;
128
127 private: 129 private:
128 payments::PaymentsClientDelegate* const delegate_; 130 payments::PaymentsClientDelegate* const delegate_;
129 131
130 DISALLOW_COPY_AND_ASSIGN(TestPaymentsClient); 132 DISALLOW_COPY_AND_ASSIGN(TestPaymentsClient);
131 }; 133 };
132 134
133 class TestPersonalDataManager : public PersonalDataManager { 135 class TestPersonalDataManager : public PersonalDataManager {
134 public: 136 public:
135 TestPersonalDataManager() 137 TestPersonalDataManager()
136 : PersonalDataManager("en-US"), 138 : PersonalDataManager("en-US"),
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 web_profiles_.push_back(std::move(profile)); 175 web_profiles_.push_back(std::move(profile));
174 } 176 }
175 177
176 void AddCreditCard(const CreditCard& credit_card) override { 178 void AddCreditCard(const CreditCard& credit_card) override {
177 std::unique_ptr<CreditCard> local_credit_card = 179 std::unique_ptr<CreditCard> local_credit_card =
178 base::MakeUnique<CreditCard>(credit_card); 180 base::MakeUnique<CreditCard>(credit_card);
179 local_credit_card->set_modification_date(base::Time::Now()); 181 local_credit_card->set_modification_date(base::Time::Now());
180 local_credit_cards_.push_back(std::move(local_credit_card)); 182 local_credit_cards_.push_back(std::move(local_credit_card));
181 } 183 }
182 184
185 void AddServerCreditCard(const CreditCard& credit_card) override {
186 std::unique_ptr<CreditCard> server_credit_card =
187 base::MakeUnique<CreditCard>(credit_card);
188 server_credit_card->set_modification_date(base::Time::Now());
189 server_credit_cards_.push_back(std::move(server_credit_card));
190 }
191
183 void RecordUseOf(const AutofillDataModel& data_model) override { 192 void RecordUseOf(const AutofillDataModel& data_model) override {
184 CreditCard* credit_card = GetCreditCardWithGUID(data_model.guid().c_str()); 193 CreditCard* credit_card = GetCreditCardWithGUID(data_model.guid().c_str());
185 if (credit_card) 194 if (credit_card)
186 credit_card->RecordAndLogUse(); 195 credit_card->RecordAndLogUse();
187 196
188 AutofillProfile* profile = GetProfileWithGUID(data_model.guid().c_str()); 197 AutofillProfile* profile = GetProfileWithGUID(data_model.guid().c_str());
189 if (profile) 198 if (profile)
190 profile->RecordAndLogUse(); 199 profile->RecordAndLogUse();
191 } 200 }
192 201
(...skipping 16 matching lines...) Expand all
209 })); 218 }));
210 } 219 }
211 } 220 }
212 221
213 void ClearAutofillProfiles() { 222 void ClearAutofillProfiles() {
214 web_profiles_.clear(); 223 web_profiles_.clear();
215 } 224 }
216 225
217 void ClearCreditCards() { 226 void ClearCreditCards() {
218 local_credit_cards_.clear(); 227 local_credit_cards_.clear();
228 server_credit_cards_.clear();
219 } 229 }
220 230
221 // Create Elvis card with whitespace in the credit card number. 231 // Create Elvis card with whitespace in the credit card number.
222 void CreateTestCreditCardWithWhitespace() { 232 void CreateTestCreditCardWithWhitespace() {
223 ClearCreditCards(); 233 ClearCreditCards();
224 std::unique_ptr<CreditCard> credit_card = base::MakeUnique<CreditCard>(); 234 std::unique_ptr<CreditCard> credit_card = base::MakeUnique<CreditCard>();
225 test::SetCreditCardInfo(credit_card.get(), "Elvis Presley", 235 test::SetCreditCardInfo(credit_card.get(), "Elvis Presley",
226 "4234 5678 9012 3456", // Visa 236 "4234 5678 9012 3456", // Visa
227 "04", "2999"); 237 "04", "2999");
228 credit_card->set_guid("00000000-0000-0000-0000-000000000008"); 238 credit_card->set_guid("00000000-0000-0000-0000-000000000008");
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 bool did_interact_with_credit_card_form() const { 531 bool did_interact_with_credit_card_form() const {
522 return did_interact_with_credit_card_form_; 532 return did_interact_with_credit_card_form_;
523 } 533 }
524 534
525 private: 535 private:
526 bool is_incognito_; 536 bool is_incognito_;
527 bool did_interact_with_credit_card_form_; 537 bool did_interact_with_credit_card_form_;
528 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver); 538 DISALLOW_COPY_AND_ASSIGN(MockAutofillDriver);
529 }; 539 };
530 540
541 class TestAutofillExternalDelegate : public AutofillExternalDelegate {
542 public:
543 explicit TestAutofillExternalDelegate(AutofillManager* autofill_manager,
544 AutofillDriver* autofill_driver)
545 : AutofillExternalDelegate(autofill_manager, autofill_driver),
546 on_query_seen_(false),
547 on_suggestions_returned_seen_(false) {}
548 ~TestAutofillExternalDelegate() override {}
549
550 void OnQuery(int query_id,
551 const FormData& form,
552 const FormFieldData& field,
553 const gfx::RectF& bounds) override {
554 on_query_seen_ = true;
555 on_suggestions_returned_seen_ = false;
556 }
557
558 void OnSuggestionsReturned(
559 int query_id,
560 const std::vector<Suggestion>& suggestions) override {
561 on_suggestions_returned_seen_ = true;
562 query_id_ = query_id;
563 suggestions_ = suggestions;
564 }
565
566 void CheckSuggestions(int expected_page_id,
567 size_t expected_num_suggestions,
568 const Suggestion expected_suggestions[]) {
569 // Ensure that these results are from the most recent query.
570 EXPECT_TRUE(on_suggestions_returned_seen_);
571
572 EXPECT_EQ(expected_page_id, query_id_);
573 ASSERT_LE(expected_num_suggestions, suggestions_.size());
574 for (size_t i = 0; i < expected_num_suggestions; ++i) {
575 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
576 EXPECT_EQ(expected_suggestions[i].value, suggestions_[i].value);
577 EXPECT_EQ(expected_suggestions[i].label, suggestions_[i].label);
578 EXPECT_EQ(expected_suggestions[i].icon, suggestions_[i].icon);
579 EXPECT_EQ(expected_suggestions[i].frontend_id,
580 suggestions_[i].frontend_id);
581 }
582 ASSERT_EQ(expected_num_suggestions, suggestions_.size());
583 }
584
585 // Wrappers around the above GetSuggestions call that take a hardcoded number
586 // of expected results so callsites are cleaner.
587 void CheckSuggestions(int expected_page_id, const Suggestion& suggestion0) {
588 std::vector<Suggestion> suggestion_vector;
589 suggestion_vector.push_back(suggestion0);
590 CheckSuggestions(expected_page_id, 1, &suggestion_vector[0]);
591 }
592 void CheckSuggestions(int expected_page_id,
593 const Suggestion& suggestion0,
594 const Suggestion& suggestion1) {
595 std::vector<Suggestion> suggestion_vector;
596 suggestion_vector.push_back(suggestion0);
597 suggestion_vector.push_back(suggestion1);
598 CheckSuggestions(expected_page_id, 2, &suggestion_vector[0]);
599 }
600 void CheckSuggestions(int expected_page_id,
601 const Suggestion& suggestion0,
602 const Suggestion& suggestion1,
603 const Suggestion& suggestion2) {
604 std::vector<Suggestion> suggestion_vector;
605 suggestion_vector.push_back(suggestion0);
606 suggestion_vector.push_back(suggestion1);
607 suggestion_vector.push_back(suggestion2);
608 CheckSuggestions(expected_page_id, 3, &suggestion_vector[0]);
609 }
610 // Check that the autofill suggestions were sent, and that they match a page
611 // but contain no results.
612 void CheckNoSuggestions(int expected_page_id) {
613 CheckSuggestions(expected_page_id, 0, nullptr);
614 }
615 // Check that the autofill suggestions were sent, and that they match a page
616 // and contain a specific number of suggestions.
617 void CheckSuggestionCount(int expected_page_id,
618 size_t expected_num_suggestions) {
619 // Ensure that these results are from the most recent query.
620 EXPECT_TRUE(on_suggestions_returned_seen_);
621
622 EXPECT_EQ(expected_page_id, query_id_);
623 ASSERT_EQ(expected_num_suggestions, suggestions_.size());
624 }
625
626 bool on_query_seen() const { return on_query_seen_; }
627
628 bool on_suggestions_returned_seen() const {
629 return on_suggestions_returned_seen_;
630 }
631
632 private:
633 // Records if OnQuery has been called yet.
634 bool on_query_seen_;
635
636 // Records if OnSuggestionsReturned has been called after the most recent
637 // call to OnQuery.
638 bool on_suggestions_returned_seen_;
639
640 // The query id of the most recent Autofill query.
641 int query_id_;
642
643 // The results returned by the most recent Autofill query.
644 std::vector<Suggestion> suggestions_;
645
646 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate);
647 };
648
649 // Finds the specified UKM metric by |name| in the specified UKM |metrics|.
650 const ukm::Entry_Metric* FindMetric(
651 const char* name,
652 const google::protobuf::RepeatedPtrField<ukm::Entry_Metric>& metrics) {
653 for (const auto& metric : metrics) {
654 if (metric.metric_hash() == base::HashMetricName(name))
655 return &metric;
656 }
657 return nullptr;
658 }
659
660 // Get Ukm sources from the Ukm service.
661 std::vector<const ukm::UkmSource*> GetUkmSources(ukm::TestUkmService* service) {
662 std::vector<const ukm::UkmSource*> sources;
663 for (const auto& kv : service->GetSources())
664 sources.push_back(kv.second.get());
665
666 return sources;
667 }
668
669 } // namespace
670
531 class TestAutofillManager : public AutofillManager { 671 class TestAutofillManager : public AutofillManager {
532 public: 672 public:
533 TestAutofillManager(AutofillDriver* driver, 673 TestAutofillManager(AutofillDriver* driver,
534 AutofillClient* client, 674 AutofillClient* client,
535 TestPersonalDataManager* personal_data) 675 TestPersonalDataManager* personal_data)
536 : AutofillManager(driver, client, personal_data), 676 : AutofillManager(driver, client, personal_data),
537 personal_data_(personal_data), 677 personal_data_(personal_data),
538 autofill_enabled_(true), 678 autofill_enabled_(true),
539 credit_card_upload_enabled_(false), 679 credit_card_upload_enabled_(false),
540 credit_card_was_uploaded_(false), 680 credit_card_was_uploaded_(false),
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 } 762 }
623 763
624 AutofillProfile* GetProfileWithGUID(const char* guid) { 764 AutofillProfile* GetProfileWithGUID(const char* guid) {
625 return personal_data_->GetProfileWithGUID(guid); 765 return personal_data_->GetProfileWithGUID(guid);
626 } 766 }
627 767
628 CreditCard* GetCreditCardWithGUID(const char* guid) { 768 CreditCard* GetCreditCardWithGUID(const char* guid) {
629 return personal_data_->GetCreditCardWithGUID(guid); 769 return personal_data_->GetCreditCardWithGUID(guid);
630 } 770 }
631 771
772 std::vector<CreditCard*> GetLocalCreditCards() const {
773 return personal_data_->GetLocalCreditCards();
774 }
775
776 const std::vector<CreditCard*>& GetCreditCards() const {
777 return personal_data_->GetCreditCards();
778 }
779
632 void AddProfile(std::unique_ptr<AutofillProfile> profile) { 780 void AddProfile(std::unique_ptr<AutofillProfile> profile) {
633 personal_data_->AddProfile(std::move(profile)); 781 personal_data_->AddProfile(std::move(profile));
634 } 782 }
635 783
636 void AddCreditCard(const CreditCard& credit_card) { 784 void AddCreditCard(const CreditCard& credit_card) {
637 personal_data_->AddCreditCard(credit_card); 785 personal_data_->AddCreditCard(credit_card);
638 } 786 }
639 787
640 int GetPackedCreditCardID(int credit_card_id) { 788 int GetPackedCreditCardID(int credit_card_id) {
641 std::string credit_card_guid = 789 std::string credit_card_guid =
642 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id); 790 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id);
643 791
644 return MakeFrontendID(credit_card_guid, std::string()); 792 return MakeFrontendID(credit_card_guid, std::string());
645 } 793 }
646 794
647 void AddSeenForm(std::unique_ptr<FormStructure> form) { 795 void AddSeenForm(std::unique_ptr<FormStructure> form) {
648 form_structures()->push_back(std::move(form)); 796 form_structures()->push_back(std::move(form));
649 } 797 }
650 798
651 void ClearFormStructures() { 799 void ClearFormStructures() {
652 form_structures()->clear(); 800 form_structures()->clear();
653 } 801 }
654 802
803 void ResetPaymentsClientForCardUpload(const char* server_id) {
804 TestPaymentsClient* payments_client =
805 new TestPaymentsClient(driver_->GetURLRequestContext(), this);
806 payments_client->server_id_ = server_id;
807 set_payments_client(payments_client);
808 }
809
655 private: 810 private:
656 void OnDidUploadCard(AutofillClient::PaymentsRpcResult result) override { 811 void OnDidUploadCard(AutofillClient::PaymentsRpcResult result,
812 const std::string& server_id) override {
657 credit_card_was_uploaded_ = true; 813 credit_card_was_uploaded_ = true;
814 AutofillManager::OnDidUploadCard(result, server_id);
658 }; 815 };
659 816
660 // Weak reference. 817 TestPersonalDataManager* personal_data_; // Weak reference.
661 TestPersonalDataManager* personal_data_;
662
663 bool autofill_enabled_; 818 bool autofill_enabled_;
664 bool credit_card_upload_enabled_; 819 bool credit_card_upload_enabled_;
665 bool credit_card_was_uploaded_; 820 bool credit_card_was_uploaded_;
666 bool expected_observed_submission_; 821 bool expected_observed_submission_;
667 822
668 std::unique_ptr<base::RunLoop> run_loop_; 823 std::unique_ptr<base::RunLoop> run_loop_;
669 824
670 std::string submitted_form_signature_; 825 std::string submitted_form_signature_;
671 std::vector<ServerFieldTypeSet> expected_submitted_field_types_; 826 std::vector<ServerFieldTypeSet> expected_submitted_field_types_;
672 827
673 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); 828 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
674 }; 829 };
675 830
676 class TestAutofillExternalDelegate : public AutofillExternalDelegate {
677 public:
678 explicit TestAutofillExternalDelegate(AutofillManager* autofill_manager,
679 AutofillDriver* autofill_driver)
680 : AutofillExternalDelegate(autofill_manager, autofill_driver),
681 on_query_seen_(false),
682 on_suggestions_returned_seen_(false) {}
683 ~TestAutofillExternalDelegate() override {}
684
685 void OnQuery(int query_id,
686 const FormData& form,
687 const FormFieldData& field,
688 const gfx::RectF& bounds) override {
689 on_query_seen_ = true;
690 on_suggestions_returned_seen_ = false;
691 }
692
693 void OnSuggestionsReturned(
694 int query_id,
695 const std::vector<Suggestion>& suggestions) override {
696 on_suggestions_returned_seen_ = true;
697 query_id_ = query_id;
698 suggestions_ = suggestions;
699 }
700
701 void CheckSuggestions(int expected_page_id,
702 size_t expected_num_suggestions,
703 const Suggestion expected_suggestions[]) {
704 // Ensure that these results are from the most recent query.
705 EXPECT_TRUE(on_suggestions_returned_seen_);
706
707 EXPECT_EQ(expected_page_id, query_id_);
708 ASSERT_LE(expected_num_suggestions, suggestions_.size());
709 for (size_t i = 0; i < expected_num_suggestions; ++i) {
710 SCOPED_TRACE(base::StringPrintf("i: %" PRIuS, i));
711 EXPECT_EQ(expected_suggestions[i].value, suggestions_[i].value);
712 EXPECT_EQ(expected_suggestions[i].label, suggestions_[i].label);
713 EXPECT_EQ(expected_suggestions[i].icon, suggestions_[i].icon);
714 EXPECT_EQ(expected_suggestions[i].frontend_id,
715 suggestions_[i].frontend_id);
716 }
717 ASSERT_EQ(expected_num_suggestions, suggestions_.size());
718 }
719
720 // Wrappers around the above GetSuggestions call that take a hardcoded number
721 // of expected results so callsites are cleaner.
722 void CheckSuggestions(int expected_page_id,
723 const Suggestion& suggestion0) {
724 std::vector<Suggestion> suggestion_vector;
725 suggestion_vector.push_back(suggestion0);
726 CheckSuggestions(expected_page_id, 1, &suggestion_vector[0]);
727 }
728 void CheckSuggestions(int expected_page_id,
729 const Suggestion& suggestion0,
730 const Suggestion& suggestion1) {
731 std::vector<Suggestion> suggestion_vector;
732 suggestion_vector.push_back(suggestion0);
733 suggestion_vector.push_back(suggestion1);
734 CheckSuggestions(expected_page_id, 2, &suggestion_vector[0]);
735 }
736 void CheckSuggestions(int expected_page_id,
737 const Suggestion& suggestion0,
738 const Suggestion& suggestion1,
739 const Suggestion& suggestion2) {
740 std::vector<Suggestion> suggestion_vector;
741 suggestion_vector.push_back(suggestion0);
742 suggestion_vector.push_back(suggestion1);
743 suggestion_vector.push_back(suggestion2);
744 CheckSuggestions(expected_page_id, 3, &suggestion_vector[0]);
745 }
746 // Check that the autofill suggestions were sent, and that they match a page
747 // but contain no results.
748 void CheckNoSuggestions(int expected_page_id) {
749 CheckSuggestions(expected_page_id, 0, nullptr);
750 }
751 // Check that the autofill suggestions were sent, and that they match a page
752 // and contain a specific number of suggestions.
753 void CheckSuggestionCount(int expected_page_id,
754 size_t expected_num_suggestions) {
755 // Ensure that these results are from the most recent query.
756 EXPECT_TRUE(on_suggestions_returned_seen_);
757
758 EXPECT_EQ(expected_page_id, query_id_);
759 ASSERT_EQ(expected_num_suggestions, suggestions_.size());
760 }
761
762 bool on_query_seen() const {
763 return on_query_seen_;
764 }
765
766 bool on_suggestions_returned_seen() const {
767 return on_suggestions_returned_seen_;
768 }
769
770 private:
771 // Records if OnQuery has been called yet.
772 bool on_query_seen_;
773
774 // Records if OnSuggestionsReturned has been called after the most recent
775 // call to OnQuery.
776 bool on_suggestions_returned_seen_;
777
778 // The query id of the most recent Autofill query.
779 int query_id_;
780
781 // The results returned by the most recent Autofill query.
782 std::vector<Suggestion> suggestions_;
783
784 DISALLOW_COPY_AND_ASSIGN(TestAutofillExternalDelegate);
785 };
786
787 // Finds the specified UKM metric by |name| in the specified UKM |metrics|.
788 const ukm::Entry_Metric* FindMetric(
789 const char* name,
790 const google::protobuf::RepeatedPtrField<ukm::Entry_Metric>& metrics) {
791 for (const auto& metric : metrics) {
792 if (metric.metric_hash() == base::HashMetricName(name))
793 return &metric;
794 }
795 return nullptr;
796 }
797
798 // Get Ukm sources from the Ukm service.
799 std::vector<const ukm::UkmSource*> GetUkmSources(ukm::TestUkmService* service) {
800 std::vector<const ukm::UkmSource*> sources;
801 for (const auto& kv : service->GetSources())
802 sources.push_back(kv.second.get());
803
804 return sources;
805 }
806
807 } // namespace
808
809 class AutofillManagerTest : public testing::Test { 831 class AutofillManagerTest : public testing::Test {
810 public: 832 public:
811 AutofillManagerTest() : field_trial_list_(nullptr) {} 833 AutofillManagerTest() : field_trial_list_(nullptr) {}
812 834
813 void SetUp() override { 835 void SetUp() override {
814 autofill_client_.SetPrefs(test::PrefServiceForTesting()); 836 autofill_client_.SetPrefs(test::PrefServiceForTesting());
815 personal_data_.set_database(autofill_client_.GetDatabase()); 837 personal_data_.set_database(autofill_client_.GetDatabase());
816 personal_data_.SetPrefService(autofill_client_.GetPrefs()); 838 personal_data_.SetPrefService(autofill_client_.GetPrefs());
817 autofill_driver_.reset(new MockAutofillDriver()); 839 autofill_driver_.reset(new MockAutofillDriver());
818 request_context_ = new net::TestURLRequestContextGetter( 840 request_context_ = new net::TestURLRequestContextGetter(
(...skipping 17 matching lines...) Expand all
836 void TearDown() override { 858 void TearDown() override {
837 // Order of destruction is important as AutofillManager relies on 859 // Order of destruction is important as AutofillManager relies on
838 // PersonalDataManager to be around when it gets destroyed. 860 // PersonalDataManager to be around when it gets destroyed.
839 autofill_manager_.reset(); 861 autofill_manager_.reset();
840 autofill_driver_.reset(); 862 autofill_driver_.reset();
841 863
842 // Remove the AutofillWebDataService so TestPersonalDataManager does not 864 // Remove the AutofillWebDataService so TestPersonalDataManager does not
843 // need to care about removing self as an observer in destruction. 865 // need to care about removing self as an observer in destruction.
844 personal_data_.set_database(scoped_refptr<AutofillWebDataService>(NULL)); 866 personal_data_.set_database(scoped_refptr<AutofillWebDataService>(NULL));
845 personal_data_.SetPrefService(NULL); 867 personal_data_.SetPrefService(NULL);
868 personal_data_.ClearCreditCards();
846 869
847 request_context_ = nullptr; 870 request_context_ = nullptr;
848 } 871 }
849 872
850 void GetAutofillSuggestions(int query_id, 873 void GetAutofillSuggestions(int query_id,
851 const FormData& form, 874 const FormData& form,
852 const FormFieldData& field) { 875 const FormFieldData& field) {
853 autofill_manager_->OnQueryFormFieldAutofill(query_id, form, field, 876 autofill_manager_->OnQueryFormFieldAutofill(query_id, form, field,
854 gfx::RectF()); 877 gfx::RectF());
855 } 878 }
856 879
857 void GetAutofillSuggestions(const FormData& form, 880 void GetAutofillSuggestions(const FormData& form,
858 const FormFieldData& field) { 881 const FormFieldData& field) {
859 GetAutofillSuggestions(kDefaultPageID, form, field); 882 GetAutofillSuggestions(kDefaultPageID, form, field);
860 } 883 }
861 884
862 void AutocompleteSuggestionsReturned( 885 void AutocompleteSuggestionsReturned(
863 const std::vector<base::string16>& result) { 886 const std::vector<base::string16>& result) {
864 autofill_manager_->autocomplete_history_manager_->SendSuggestions(&result); 887 autofill_manager_->autocomplete_history_manager_->SendSuggestions(&result);
865 } 888 }
866 889
867 void FormsSeen(const std::vector<FormData>& forms) { 890 void FormsSeen(const std::vector<FormData>& forms) {
868 autofill_manager_->OnFormsSeen(forms, base::TimeTicks()); 891 autofill_manager_->OnFormsSeen(forms, base::TimeTicks());
869 } 892 }
870 893
871 void FormSubmitted(const FormData& form) { 894 void FormSubmitted(const FormData& form) {
872 autofill_manager_->ResetRunLoop(); 895 autofill_manager_->ResetRunLoop();
873 if (autofill_manager_->OnWillSubmitForm(form, base::TimeTicks::Now())) 896 if (autofill_manager_->OnWillSubmitForm(form, base::TimeTicks::Now()) &&
897 (!personal_data_.GetProfiles().empty() ||
898 !personal_data_.GetCreditCards().empty()))
874 autofill_manager_->WaitForAsyncUploadProcess(); 899 autofill_manager_->WaitForAsyncUploadProcess();
875 autofill_manager_->OnFormSubmitted(form); 900 autofill_manager_->OnFormSubmitted(form);
876 } 901 }
877 902
878 void FillAutofillFormData(int query_id, 903 void FillAutofillFormData(int query_id,
879 const FormData& form, 904 const FormData& form,
880 const FormFieldData& field, 905 const FormFieldData& field,
881 int unique_id) { 906 int unique_id) {
882 autofill_manager_->FillOrPreviewForm( 907 autofill_manager_->FillOrPreviewForm(
883 AutofillDriver::FORM_DATA_ACTION_FILL, query_id, form, field, 908 AutofillDriver::FORM_DATA_ACTION_FILL, query_id, form, field,
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
1112 EXPECT_EQ(expected_num_matching_entries, num_matching_entries); 1137 EXPECT_EQ(expected_num_matching_entries, num_matching_entries);
1113 } 1138 }
1114 1139
1115 protected: 1140 protected:
1116 base::test::ScopedTaskEnvironment scoped_task_environment_; 1141 base::test::ScopedTaskEnvironment scoped_task_environment_;
1117 MockAutofillClient autofill_client_; 1142 MockAutofillClient autofill_client_;
1118 std::unique_ptr<MockAutofillDriver> autofill_driver_; 1143 std::unique_ptr<MockAutofillDriver> autofill_driver_;
1119 std::unique_ptr<TestAutofillManager> autofill_manager_; 1144 std::unique_ptr<TestAutofillManager> autofill_manager_;
1120 std::unique_ptr<TestAutofillExternalDelegate> external_delegate_; 1145 std::unique_ptr<TestAutofillExternalDelegate> external_delegate_;
1121 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 1146 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
1122 TestPaymentsClient* payments_client_;
1123 TestAutofillDownloadManager* download_manager_; 1147 TestAutofillDownloadManager* download_manager_;
1124 TestPersonalDataManager personal_data_; 1148 TestPersonalDataManager personal_data_;
1125 base::FieldTrialList field_trial_list_; 1149 base::FieldTrialList field_trial_list_;
1126 base::test::ScopedFeatureList scoped_feature_list_; 1150 base::test::ScopedFeatureList scoped_feature_list_;
1127 }; 1151 };
1128 1152
1129 class TestFormStructure : public FormStructure { 1153 class TestFormStructure : public FormStructure {
1130 public: 1154 public:
1131 explicit TestFormStructure(const FormData& form) 1155 explicit TestFormStructure(const FormData& form)
1132 : FormStructure(form) {} 1156 : FormStructure(form) {}
(...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after
1771 "04/99", kVisaCard, 1795 "04/99", kVisaCard,
1772 autofill_manager_->GetPackedCreditCardID(4)), 1796 autofill_manager_->GetPackedCreditCardID(4)),
1773 Suggestion("MasterCard" + kUTF8MidlineEllipsis + "8765", "10/98", 1797 Suggestion("MasterCard" + kUTF8MidlineEllipsis + "8765", "10/98",
1774 kMasterCard, autofill_manager_->GetPackedCreditCardID(5))); 1798 kMasterCard, autofill_manager_->GetPackedCreditCardID(5)));
1775 } 1799 }
1776 1800
1777 // Test that we will eventually return the credit card signin promo when there 1801 // Test that we will eventually return the credit card signin promo when there
1778 // are no credit card suggestions and the promo is active. See the tests in 1802 // are no credit card suggestions and the promo is active. See the tests in
1779 // AutofillExternalDelegateTest that test whether the promo is added. 1803 // AutofillExternalDelegateTest that test whether the promo is added.
1780 TEST_F(AutofillManagerTest, GetCreditCardSuggestions_OnlySigninPromo) { 1804 TEST_F(AutofillManagerTest, GetCreditCardSuggestions_OnlySigninPromo) {
1781 // Make sure there are no credit cards.
1782 personal_data_.ClearCreditCards(); 1805 personal_data_.ClearCreditCards();
1783 1806
1784 // Set up our form data. 1807 // Set up our form data.
1785 FormData form; 1808 FormData form;
1786 CreateTestCreditCardFormData(&form, true, false); 1809 CreateTestCreditCardFormData(&form, true, false);
1787 std::vector<FormData> forms(1, form); 1810 std::vector<FormData> forms(1, form);
1788 FormsSeen(forms); 1811 FormsSeen(forms);
1789 FormFieldData field = form.fields[1]; 1812 FormFieldData field = form.fields[1];
1790 1813
1791 ON_CALL(autofill_client_, ShouldShowSigninPromo()) 1814 ON_CALL(autofill_client_, ShouldShowSigninPromo())
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after
2328 &response_page_id, &response_data); 2351 &response_page_id, &response_data);
2329 ExpectFilledCreditCardYearMonthWithYearMonth( 2352 ExpectFilledCreditCardYearMonthWithYearMonth(
2330 response_page_id, response_data, kDefaultPageID, false, "2999", ""); 2353 response_page_id, response_data, kDefaultPageID, false, "2999", "");
2331 } 2354 }
2332 2355
2333 // Test that we correctly fill a credit card form with month input type. 2356 // Test that we correctly fill a credit card form with month input type.
2334 // 4. year non-empty, month empty 2357 // 4. year non-empty, month empty
2335 TEST_F(AutofillManagerTest, FillCreditCardForm_YearMonth) { 2358 TEST_F(AutofillManagerTest, FillCreditCardForm_YearMonth) {
2336 // Same as the SetUp(), but generate 4 credit cards with year month 2359 // Same as the SetUp(), but generate 4 credit cards with year month
2337 // combination. 2360 // combination.
2338 personal_data_.ClearCreditCards();
2339 personal_data_.CreateTestCreditCardsYearAndMonth("2999", "04"); 2361 personal_data_.CreateTestCreditCardsYearAndMonth("2999", "04");
2340 // Set up our form data. 2362 // Set up our form data.
2341 FormData form; 2363 FormData form;
2342 CreateTestCreditCardFormData(&form, true, true); 2364 CreateTestCreditCardFormData(&form, true, true);
2343 std::vector<FormData> forms(1, form); 2365 std::vector<FormData> forms(1, form);
2344 FormsSeen(forms); 2366 FormsSeen(forms);
2345 2367
2346 const char guid[] = "00000000-0000-0000-0000-000000000007"; 2368 const char guid[] = "00000000-0000-0000-0000-000000000007";
2347 int response_page_id = 0; 2369 int response_page_id = 0;
2348 FormData response_data; 2370 FormData response_data;
(...skipping 2227 matching lines...) Expand 10 before | Expand all | Expand 10 after
4576 } 4598 }
4577 4599
4578 // TODO(crbug.com/666704): Flaky on android_n5x_swarming_rel bot. 4600 // TODO(crbug.com/666704): Flaky on android_n5x_swarming_rel bot.
4579 #if defined(OS_ANDROID) 4601 #if defined(OS_ANDROID)
4580 #define MAYBE_UploadCreditCard DISABLED_UploadCreditCard 4602 #define MAYBE_UploadCreditCard DISABLED_UploadCreditCard
4581 #else 4603 #else
4582 #define MAYBE_UploadCreditCard UploadCreditCard 4604 #define MAYBE_UploadCreditCard UploadCreditCard
4583 #endif 4605 #endif
4584 TEST_F(AutofillManagerTest, MAYBE_UploadCreditCard) { 4606 TEST_F(AutofillManagerTest, MAYBE_UploadCreditCard) {
4585 EnableUkmLogging(); 4607 EnableUkmLogging();
4608 personal_data_.ClearCreditCards();
4586 personal_data_.ClearAutofillProfiles(); 4609 personal_data_.ClearAutofillProfiles();
4587 autofill_manager_->set_credit_card_upload_enabled(true); 4610 autofill_manager_->set_credit_card_upload_enabled(true);
4588 4611
4589 // Create, fill and submit an address form in order to establish a recent 4612 // Create, fill and submit an address form in order to establish a recent
4590 // profile which can be selected for the upload request. 4613 // profile which can be selected for the upload request.
4591 FormData address_form; 4614 FormData address_form;
4592 test::CreateTestAddressFormData(&address_form); 4615 test::CreateTestAddressFormData(&address_form);
4593 FormsSeen(std::vector<FormData>(1, address_form)); 4616 FormsSeen(std::vector<FormData>(1, address_form));
4594 ExpectUniqueFillableFormParsedUkm(); 4617 ExpectUniqueFillableFormParsedUkm();
4595 4618
(...skipping 11 matching lines...) Expand all
4607 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111"); 4630 credit_card_form.fields[1].value = ASCIIToUTF16("4111111111111111");
4608 credit_card_form.fields[2].value = ASCIIToUTF16("11"); 4631 credit_card_form.fields[2].value = ASCIIToUTF16("11");
4609 credit_card_form.fields[3].value = ASCIIToUTF16("2017"); 4632 credit_card_form.fields[3].value = ASCIIToUTF16("2017");
4610 credit_card_form.fields[4].value = ASCIIToUTF16("123"); 4633 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4611 4634
4612 base::HistogramTester histogram_tester; 4635 base::HistogramTester histogram_tester;
4613 4636
4614 FormSubmitted(credit_card_form); 4637 FormSubmitted(credit_card_form);
4615 EXPECT_TRUE(autofill_manager_->credit_card_was_uploaded()); 4638 EXPECT_TRUE(autofill_manager_->credit_card_was_uploaded());
4616 4639
4640 // Server did not send a server_id, expect copy of card is not stored.
4641 EXPECT_TRUE(autofill_manager_->GetCreditCards().empty());
4642
4617 // Verify that the correct histogram entry (and only that) was logged. 4643 // Verify that the correct histogram entry (and only that) was logged.
4618 histogram_tester.ExpectUniqueSample("Autofill.CardUploadDecisionExpanded", 4644 histogram_tester.ExpectUniqueSample("Autofill.CardUploadDecisionExpanded",
4619 AutofillMetrics::UPLOAD_OFFERED, 1); 4645 AutofillMetrics::UPLOAD_OFFERED, 1);
4620 // Verify that the correct UKM was logged. 4646 // Verify that the correct UKM was logged.
4621 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED); 4647 ExpectCardUploadDecisionUkm(AutofillMetrics::UPLOAD_OFFERED);
4622 } 4648 }
4623 4649
4624 // TODO(crbug.com/666704): Flaky on android_n5x_swarming_rel bot. 4650 // TODO(crbug.com/666704): Flaky on android_n5x_swarming_rel bot.
4625 #if defined(OS_ANDROID) 4651 #if defined(OS_ANDROID)
4652 #define MAYBE_UploadCreditCardAndSaveCopy DISABLED_UploadCreditCardAndSaveCopy
4653 #else
4654 #define MAYBE_UploadCreditCardAndSaveCopy UploadCreditCardAndSaveCopy
4655 #endif
4656 TEST_F(AutofillManagerTest, MAYBE_UploadCreditCardAndSaveCopy) {
4657 personal_data_.ClearCreditCards();
4658 personal_data_.ClearAutofillProfiles();
4659 autofill_manager_->set_credit_card_upload_enabled(true);
4660
4661 const char* const server_id = "InstrumentData:1234";
4662 autofill_manager_->ResetPaymentsClientForCardUpload(server_id);
4663
4664 // Create, fill and submit an address form in order to establish a recent
4665 // profile which can be selected for the upload request.
4666 FormData address_form;
4667 test::CreateTestAddressFormData(&address_form);
4668 FormsSeen(std::vector<FormData>(1, address_form));
4669
4670 ManuallyFillAddressForm("Flo", "Master", "77401", "US", &address_form);
4671 FormSubmitted(address_form);
4672
4673 // Set up our credit card form data.
4674 FormData credit_card_form;
4675 CreateTestCreditCardFormData(&credit_card_form, true, false);
4676 FormsSeen(std::vector<FormData>(1, credit_card_form));
4677
4678 // Edit the data, and submit.
4679 const char* const card_number = "4111111111111111";
4680 credit_card_form.fields[0].value = ASCIIToUTF16("Flo Master");
4681 credit_card_form.fields[1].value = ASCIIToUTF16(card_number);
4682 credit_card_form.fields[2].value = ASCIIToUTF16("11");
4683 credit_card_form.fields[3].value = ASCIIToUTF16("2017");
4684 credit_card_form.fields[4].value = ASCIIToUTF16("123");
4685
4686 FormSubmitted(credit_card_form);
4687
4688 EXPECT_TRUE(autofill_manager_->credit_card_was_uploaded());
4689 EXPECT_TRUE(autofill_manager_->GetLocalCreditCards().empty());
4690 ASSERT_EQ(1U, autofill_manager_->GetCreditCards().size());
4691 const CreditCard* const saved_card = autofill_manager_->GetCreditCards()[0];
4692 EXPECT_EQ(CreditCard::OK, saved_card->GetServerStatus());
4693 EXPECT_EQ(base::ASCIIToUTF16("1111"), saved_card->LastFourDigits());
4694 EXPECT_EQ(kVisaCard, saved_card->type());
4695 EXPECT_EQ(11, saved_card->expiration_month());
4696 EXPECT_EQ(2017, saved_card->expiration_year());
4697 EXPECT_EQ(server_id, saved_card->server_id());
4698 EXPECT_EQ(CreditCard::FULL_SERVER_CARD, saved_card->record_type());
4699 EXPECT_EQ(base::ASCIIToUTF16(card_number), saved_card->number());
4700 }
4701
4702 // TODO(crbug.com/666704): Flaky on android_n5x_swarming_rel bot.
4703 #if defined(OS_ANDROID)
4626 #define MAYBE_UploadCreditCard_FeatureNotEnabled DISABLED_UploadCreditCard_Featu reNotEnabled 4704 #define MAYBE_UploadCreditCard_FeatureNotEnabled DISABLED_UploadCreditCard_Featu reNotEnabled
4627 #else 4705 #else
4628 #define MAYBE_UploadCreditCard_FeatureNotEnabled UploadCreditCard_FeatureNotEnab led 4706 #define MAYBE_UploadCreditCard_FeatureNotEnabled UploadCreditCard_FeatureNotEnab led
4629 #endif 4707 #endif
4630 TEST_F(AutofillManagerTest, MAYBE_UploadCreditCard_FeatureNotEnabled) { 4708 TEST_F(AutofillManagerTest, MAYBE_UploadCreditCard_FeatureNotEnabled) {
4631 personal_data_.ClearAutofillProfiles(); 4709 personal_data_.ClearAutofillProfiles();
4632 autofill_manager_->set_credit_card_upload_enabled(false); 4710 autofill_manager_->set_credit_card_upload_enabled(false);
4633 4711
4634 // Create, fill and submit an address form in order to establish a recent 4712 // Create, fill and submit an address form in order to establish a recent
4635 // profile which can be selected for the upload request. 4713 // profile which can be selected for the upload request.
(...skipping 1445 matching lines...) Expand 10 before | Expand all | Expand 10 after
6081 autofill_manager_->StartUploadProcess(std::move(form_structure), 6159 autofill_manager_->StartUploadProcess(std::move(form_structure),
6082 base::TimeTicks::Now(), true); 6160 base::TimeTicks::Now(), true);
6083 6161
6084 // Wait for upload to complete (will check expected types as well). 6162 // Wait for upload to complete (will check expected types as well).
6085 autofill_manager_->WaitForAsyncUploadProcess(); 6163 autofill_manager_->WaitForAsyncUploadProcess();
6086 6164
6087 EXPECT_EQ(signature, autofill_manager_->GetSubmittedFormSignature()); 6165 EXPECT_EQ(signature, autofill_manager_->GetSubmittedFormSignature());
6088 } 6166 }
6089 6167
6090 } // namespace autofill 6168 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698