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

Unified Diff: components/autofill/core/browser/autofill_external_delegate_unittest.cc

Issue 772253003: Create an autofill Suggestion class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: self review 2 Created 6 years 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 side-by-side diff with in-line comments
Download patch
Index: components/autofill/core/browser/autofill_external_delegate_unittest.cc
diff --git a/components/autofill/core/browser/autofill_external_delegate_unittest.cc b/components/autofill/core/browser/autofill_external_delegate_unittest.cc
index 251d738d0b11009e956083f87a0000f1f51d2514..57610348d27f378b2d34eaa1501aa41f7367f188 100644
--- a/components/autofill/core/browser/autofill_external_delegate_unittest.cc
+++ b/components/autofill/core/browser/autofill_external_delegate_unittest.cc
@@ -35,6 +35,51 @@ const int kQueryId = 5;
// A constant value to use as an Autofill profile ID.
const int kAutofillProfileId = 1;
+// Gmock matcher that allows checking the frontend IDs of a sequence of
+// suggestions. This wraps a GMock container matcher, converts the suggestion
+// frontend IDs to a vector of ints, and then runs the container matcher
+// against the result to test an argument. See SuggestionVectorIdsAre() below.
+class SuggestionVectorIdsAreMatcher
+ : public testing::MatcherInterface<const std::vector<Suggestion>&> {
+ public:
+ typedef std::vector<int> Container;
+ typedef testing::Matcher<Container> ContainerMatcher;
+
+ explicit SuggestionVectorIdsAreMatcher(const ContainerMatcher& seq_matcher)
+ : container_matcher_(seq_matcher) {
+ }
+
+ virtual bool MatchAndExplain(const std::vector<Suggestion>& suggestions,
+ testing::MatchResultListener* listener) const {
+ Container frontend_ids;
+ for (const auto& suggestion : suggestions)
+ frontend_ids.push_back(suggestion.frontend_id);
+ return container_matcher_.MatchAndExplain(frontend_ids, listener);
+ }
+
+ virtual void DescribeTo(::std::ostream* os) const {
+ container_matcher_.DescribeTo(os);
+ }
+
+ virtual void DescribeNegationTo(::std::ostream* os) const {
+ container_matcher_.DescribeNegationTo(os);
+ }
+
+ private:
+ ContainerMatcher container_matcher_;
+};
+
+// Use this matcher to compare a sequence vector's IDs to a list. In an
+// EXPECT_CALL statement, use the following for an vector<Suggestion> argument
+// to compare the IDs against a constant list:
+// SuggestionVectorIdsAre(testing::ElementsAre(1, 2, 3, 4))
+template<class EltsAreMatcher>
+inline testing::Matcher<const std::vector<Suggestion>&>
+SuggestionVectorIdsAre(const EltsAreMatcher& elts_are_matcher) {
+ return MakeMatcher(
+ new SuggestionVectorIdsAreMatcher(elts_are_matcher));
+}
+
class MockAutofillDriver : public TestAutofillDriver {
public:
MockAutofillDriver() {}
@@ -58,13 +103,10 @@ class MockAutofillClient : public autofill::TestAutofillClient {
MOCK_METHOD1(ScanCreditCard,
void(const CreditCardScanCallback& callbacK));
- MOCK_METHOD7(ShowAutofillPopup,
+ MOCK_METHOD4(ShowAutofillPopup,
void(const gfx::RectF& element_bounds,
base::i18n::TextDirection text_direction,
- const std::vector<base::string16>& values,
- const std::vector<base::string16>& labels,
- const std::vector<base::string16>& icons,
- const std::vector<int>& identifiers,
+ const std::vector<Suggestion>& suggestions,
base::WeakPtr<AutofillPopupDelegate> delegate));
MOCK_METHOD2(UpdateAutofillPopupDataListValues,
@@ -151,25 +193,17 @@ TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) {
autofill_client_,
ShowAutofillPopup(_,
_,
- _,
- _,
- _,
- testing::ElementsAre(
+ SuggestionVectorIdsAre(testing::ElementsAre(
kAutofillProfileId,
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
- static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS)),
+ static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS))),
_));
// This should call ShowAutofillPopup.
- std::vector<base::string16> autofill_item;
- autofill_item.push_back(base::string16());
- std::vector<int> autofill_ids;
- autofill_ids.push_back(kAutofillProfileId);
- external_delegate_->OnSuggestionsReturned(kQueryId,
- autofill_item,
- autofill_item,
- autofill_item,
- autofill_ids);
+ std::vector<Suggestion> autofill_item;
+ autofill_item.push_back(Suggestion());
+ autofill_item[0].frontend_id = kAutofillProfileId;
+ external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
EXPECT_CALL(*autofill_manager_,
FillOrPreviewForm(
@@ -178,7 +212,8 @@ TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) {
// This should trigger a call to hide the popup since we've selected an
// option.
- external_delegate_->DidAcceptSuggestion(autofill_item[0], autofill_ids[0]);
+ external_delegate_->DidAcceptSuggestion(autofill_item[0].value,
+ autofill_item[0].frontend_id);
}
// Test that data list elements for a node will appear in the Autofill popup.
@@ -200,27 +235,19 @@ TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateDataList) {
autofill_client_,
ShowAutofillPopup(_,
_,
- _,
- _,
- _,
- testing::ElementsAre(
+ SuggestionVectorIdsAre(testing::ElementsAre(
static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY),
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
kAutofillProfileId,
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
- static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS)),
+ static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS))),
_));
// This should call ShowAutofillPopup.
- std::vector<base::string16> autofill_item;
- autofill_item.push_back(base::string16());
- std::vector<int> autofill_ids;
- autofill_ids.push_back(kAutofillProfileId);
- external_delegate_->OnSuggestionsReturned(kQueryId,
- autofill_item,
- autofill_item,
- autofill_item,
- autofill_ids);
+ std::vector<Suggestion> autofill_item;
+ autofill_item.push_back(Suggestion());
+ autofill_item[0].frontend_id = kAutofillProfileId;
+ external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
// Try calling OnSuggestionsReturned with no Autofill values and ensure
// the datalist items are still shown.
@@ -230,26 +257,19 @@ TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateDataList) {
ShowAutofillPopup(
_,
_,
- _,
- _,
- _,
- testing::ElementsAre(static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY)),
+ SuggestionVectorIdsAre(testing::ElementsAre(
+ static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY))),
_));
- autofill_item = std::vector<base::string16>();
- autofill_ids = std::vector<int>();
- external_delegate_->OnSuggestionsReturned(kQueryId,
- autofill_item,
- autofill_item,
- autofill_item,
- autofill_ids);
+ autofill_item.clear();
+ external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
}
// Test that datalist values can get updated while a popup is showing.
TEST_F(AutofillExternalDelegateUnitTest, UpdateDataListWhileShowingPopup) {
IssueOnQuery(kQueryId);
- EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _, _, _, _))
+ EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _))
.Times(0);
// Make sure just setting the data list values doesn't cause the popup to
@@ -269,27 +289,19 @@ TEST_F(AutofillExternalDelegateUnitTest, UpdateDataListWhileShowingPopup) {
autofill_client_,
ShowAutofillPopup(_,
_,
- _,
- _,
- _,
- testing::ElementsAre(
+ SuggestionVectorIdsAre(testing::ElementsAre(
static_cast<int>(POPUP_ITEM_ID_DATALIST_ENTRY),
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
kAutofillProfileId,
static_cast<int>(POPUP_ITEM_ID_SEPARATOR),
- static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS)),
+ static_cast<int>(POPUP_ITEM_ID_AUTOFILL_OPTIONS))),
_));
// Ensure the popup is displayed.
- std::vector<base::string16> autofill_item;
- autofill_item.push_back(base::string16());
- std::vector<int> autofill_ids;
- autofill_ids.push_back(kAutofillProfileId);
- external_delegate_->OnSuggestionsReturned(kQueryId,
- autofill_item,
- autofill_item,
- autofill_item,
- autofill_ids);
+ std::vector<Suggestion> autofill_item;
+ autofill_item.push_back(Suggestion());
+ autofill_item[0].frontend_id = kAutofillProfileId;
+ external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
// This would normally get called from ShowAutofillPopup, but it is mocked so
// we need to call OnPopupShown ourselves.
@@ -319,22 +331,15 @@ TEST_F(AutofillExternalDelegateUnitTest, AutofillWarnings) {
ShowAutofillPopup(
_,
_,
- _,
- _,
- _,
- testing::ElementsAre(static_cast<int>(POPUP_ITEM_ID_WARNING_MESSAGE)),
+ SuggestionVectorIdsAre(testing::ElementsAre(
+ static_cast<int>(POPUP_ITEM_ID_WARNING_MESSAGE))),
_));
// This should call ShowAutofillPopup.
- std::vector<base::string16> autofill_item;
- autofill_item.push_back(base::string16());
- std::vector<int> autofill_ids;
- autofill_ids.push_back(POPUP_ITEM_ID_WARNING_MESSAGE);
- external_delegate_->OnSuggestionsReturned(kQueryId,
- autofill_item,
- autofill_item,
- autofill_item,
- autofill_ids);
+ std::vector<Suggestion> autofill_item;
+ autofill_item.push_back(Suggestion());
+ autofill_item[0].frontend_id = POPUP_ITEM_ID_WARNING_MESSAGE;
+ external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
}
// Test that the Autofill popup doesn't display a warning explaining why
@@ -354,20 +359,15 @@ TEST_F(AutofillExternalDelegateUnitTest, NoAutofillWarningsWithoutSuggestions) {
external_delegate_->OnQuery(kQueryId, form, field, element_bounds, true);
- EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _, _, _, _))
+ EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _))
.Times(0);
EXPECT_CALL(autofill_client_, HideAutofillPopup()).Times(1);
// This should not call ShowAutofillPopup.
- std::vector<base::string16> autofill_item;
- autofill_item.push_back(base::string16());
- std::vector<int> autofill_ids;
- autofill_ids.push_back(POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY);
- external_delegate_->OnSuggestionsReturned(kQueryId,
- autofill_item,
- autofill_item,
- autofill_item,
- autofill_ids);
+ std::vector<Suggestion> autofill_item;
+ autofill_item.push_back(Suggestion());
+ autofill_item[0].frontend_id = POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY;
+ external_delegate_->OnSuggestionsReturned(kQueryId, autofill_item);
}
// Test that the Autofill delegate doesn't try and fill a form with a
@@ -410,7 +410,7 @@ TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearPreviewedForm) {
// Test that the popup is hidden once we are done editing the autofill field.
TEST_F(AutofillExternalDelegateUnitTest,
ExternalDelegateHidePopupAfterEditing) {
- EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _, _, _, _));
+ EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _));
autofill::GenerateTestAutofillPopup(external_delegate_.get());
EXPECT_CALL(autofill_client_, HideAutofillPopup());
@@ -474,20 +474,15 @@ TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateHideWarning) {
external_delegate_->OnQuery(kQueryId, form, field, element_bounds, false);
- std::vector<base::string16> autofill_items;
- autofill_items.push_back(base::string16());
- std::vector<int> autofill_ids;
- autofill_ids.push_back(POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY);
+ std::vector<Suggestion> autofill_items;
+ autofill_items.push_back(Suggestion());
+ autofill_items[0].frontend_id = POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY;
// Ensure the popup tries to hide itself, since it is not allowed to show
// anything.
EXPECT_CALL(autofill_client_, HideAutofillPopup());
- external_delegate_->OnSuggestionsReturned(kQueryId,
- autofill_items,
- autofill_items,
- autofill_items,
- autofill_ids);
+ external_delegate_->OnSuggestionsReturned(kQueryId, autofill_items);
}
TEST_F(AutofillExternalDelegateUnitTest, IgnoreAutocompleteOffForAutofill) {
@@ -499,20 +494,15 @@ TEST_F(AutofillExternalDelegateUnitTest, IgnoreAutocompleteOffForAutofill) {
external_delegate_->OnQuery(kQueryId, form, field, element_bounds, false);
- std::vector<base::string16> autofill_items;
- autofill_items.push_back(base::string16());
- std::vector<int> autofill_ids;
- autofill_ids.push_back(POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY);
+ std::vector<Suggestion> autofill_items;
+ autofill_items.push_back(Suggestion());
+ autofill_items[0].frontend_id = POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY;
// Ensure the popup tries to show itself, despite autocomplete="off".
- EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _, _, _, _));
+ EXPECT_CALL(autofill_client_, ShowAutofillPopup(_, _, _, _));
EXPECT_CALL(autofill_client_, HideAutofillPopup()).Times(0);
- external_delegate_->OnSuggestionsReturned(kQueryId,
- autofill_items,
- autofill_items,
- autofill_items,
- autofill_ids);
+ external_delegate_->OnSuggestionsReturned(kQueryId, autofill_items);
}
TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateFillFieldWithValue) {

Powered by Google App Engine
This is Rietveld 408576698