1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_SUGGESTION_TEST_HELPERS_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_SUGGESTION_TEST_HELPERS_H_
8 #include "components/autofill/core/browser/suggestion.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 // Gmock matcher that allows checking a member of the Suggestion class in a
15 // vector. This wraps a GMock container matcher, converts the suggestion
16 // members to a vector, and then runs the container matcher against the result
17 // to test an argument. See SuggestionVectorIdsAre() below.
18 template<typename EltType
>
19 class SuggestionVectorMembersAreMatcher
20 : public testing::MatcherInterface
<const std::vector
<Suggestion
>&> {
22 typedef std::vector
<EltType
> Container
;
23 typedef testing::Matcher
<Container
> ContainerMatcher
;
25 SuggestionVectorMembersAreMatcher(
26 const ContainerMatcher
& seq_matcher
,
27 EltType
Suggestion::*elt
)
28 : container_matcher_(seq_matcher
),
32 virtual bool MatchAndExplain(const std::vector
<Suggestion
>& suggestions
,
33 testing::MatchResultListener
* listener
) const {
35 for (const auto& suggestion
: suggestions
)
36 container
.push_back(suggestion
.*element_
);
37 return container_matcher_
.MatchAndExplain(container
, listener
);
40 virtual void DescribeTo(::std::ostream
* os
) const {
41 container_matcher_
.DescribeTo(os
);
44 virtual void DescribeNegationTo(::std::ostream
* os
) const {
45 container_matcher_
.DescribeNegationTo(os
);
49 ContainerMatcher container_matcher_
;
50 EltType
Suggestion::*element_
;
53 // Use this matcher to compare a sequence vector's IDs to a list. In an
54 // EXPECT_CALL statement, use the following for an vector<Suggestion> argument
55 // to compare the IDs against a constant list:
56 // SuggestionVectorIdsAre(testing::ElementsAre(1, 2, 3, 4))
57 template<class EltsAreMatcher
>
58 inline testing::Matcher
<const std::vector
<Suggestion
>&>
59 SuggestionVectorIdsAre(const EltsAreMatcher
& elts_are_matcher
) {
60 return testing::MakeMatcher(
61 new SuggestionVectorMembersAreMatcher
<int>(
62 elts_are_matcher
, &Suggestion::frontend_id
));
65 // Like SuggestionVectorIdsAre above, but tests the values.
66 template<class EltsAreMatcher
>
67 inline testing::Matcher
<const std::vector
<Suggestion
>&>
68 SuggestionVectorValuesAre(const EltsAreMatcher
& elts_are_matcher
) {
69 return testing::MakeMatcher(
70 new SuggestionVectorMembersAreMatcher
<base::string16
>(
71 elts_are_matcher
, &Suggestion::value
));
74 // Like SuggestionVectorIdsAre above, but tests the labels.
75 template<class EltsAreMatcher
>
76 inline testing::Matcher
<const std::vector
<Suggestion
>&>
77 SuggestionVectorLabelsAre(const EltsAreMatcher
& elts_are_matcher
) {
78 return testing::MakeMatcher(
79 new SuggestionVectorMembersAreMatcher
<base::string16
>(
80 elts_are_matcher
, &Suggestion::label
));
83 } // namespace autofill
85 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_SUGGESTION_TEST_HELPERS_H_