// Copyright 2014 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. #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_SUGGESTION_TEST_HELPERS_H_ #define COMPONENTS_AUTOFILL_CORE_BROWSER_SUGGESTION_TEST_HELPERS_H_ #include "components/autofill/core/browser/suggestion.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" namespace autofill { // Gmock matcher that allows checking a member of the Suggestion class in a // vector. This wraps a GMock container matcher, converts the suggestion // members to a vector, and then runs the container matcher against the result // to test an argument. See SuggestionVectorIdsAre() below. template class SuggestionVectorMembersAreMatcher : public testing::MatcherInterface&> { public: typedef std::vector Container; typedef testing::Matcher ContainerMatcher; SuggestionVectorMembersAreMatcher( const ContainerMatcher& seq_matcher, EltType Suggestion::*elt) : container_matcher_(seq_matcher), element_(elt) { } virtual bool MatchAndExplain(const std::vector& suggestions, testing::MatchResultListener* listener) const { Container container; for (const auto& suggestion : suggestions) container.push_back(suggestion.*element_); return container_matcher_.MatchAndExplain(container, 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_; EltType Suggestion::*element_; }; // Use this matcher to compare a sequence vector's IDs to a list. In an // EXPECT_CALL statement, use the following for an vector argument // to compare the IDs against a constant list: // SuggestionVectorIdsAre(testing::ElementsAre(1, 2, 3, 4)) template inline testing::Matcher&> SuggestionVectorIdsAre(const EltsAreMatcher& elts_are_matcher) { return testing::MakeMatcher( new SuggestionVectorMembersAreMatcher( elts_are_matcher, &Suggestion::frontend_id)); } // Like SuggestionVectorIdsAre above, but tests the values. template inline testing::Matcher&> SuggestionVectorValuesAre(const EltsAreMatcher& elts_are_matcher) { return testing::MakeMatcher( new SuggestionVectorMembersAreMatcher( elts_are_matcher, &Suggestion::value)); } // Like SuggestionVectorIdsAre above, but tests the labels. template inline testing::Matcher&> SuggestionVectorLabelsAre(const EltsAreMatcher& elts_are_matcher) { return testing::MakeMatcher( new SuggestionVectorMembersAreMatcher( elts_are_matcher, &Suggestion::label)); } } // namespace autofill #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_SUGGESTION_TEST_HELPERS_H_