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
3 // found in the LICENSE file.
5 #include "sync/notifier/unacked_invalidation_set_test_util.h"
7 #include "base/json/json_string_value_serializer.h"
8 #include "sync/notifier/object_id_invalidation_map.h"
9 #include "testing/gmock/include/gmock/gmock-matchers.h"
13 using ::testing::MakeMatcher
;
14 using ::testing::MatchResultListener
;
15 using ::testing::Matcher
;
16 using ::testing::MatcherInterface
;
17 using ::testing::PrintToString
;
21 // This class needs to be declared outside the null namespace so the
22 // UnackedInvalidationSet can declare it as a friend. This class needs access
23 // to the UnackedInvalidationSet internals to implement its comparispon
25 class UnackedInvalidationSetEqMatcher
26 : public testing::MatcherInterface
<const UnackedInvalidationSet
&> {
28 explicit UnackedInvalidationSetEqMatcher(
29 const UnackedInvalidationSet
& expected
);
31 virtual bool MatchAndExplain(
32 const UnackedInvalidationSet
& actual
,
33 MatchResultListener
* listener
) const OVERRIDE
;
34 virtual void DescribeTo(::std::ostream
* os
) const OVERRIDE
;
35 virtual void DescribeNegationTo(::std::ostream
* os
) const OVERRIDE
;
38 const UnackedInvalidationSet expected_
;
40 DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationSetEqMatcher
);
45 struct InvalidationEq
{
46 bool operator()(const syncer::Invalidation
& a
,
47 const syncer::Invalidation
& b
) const {
54 UnackedInvalidationSetEqMatcher::UnackedInvalidationSetEqMatcher(
55 const UnackedInvalidationSet
& expected
)
56 : expected_(expected
) {}
58 bool UnackedInvalidationSetEqMatcher::MatchAndExplain(
59 const UnackedInvalidationSet
& actual
,
60 MatchResultListener
* listener
) const {
61 // Use our friendship with this class to compare the internals of two
64 // Note that the registration status is intentionally not considered
65 // when performing this comparison.
66 return expected_
.object_id_
== actual
.object_id_
67 && std::equal(expected_
.invalidations_
.begin(),
68 expected_
.invalidations_
.end(),
69 actual
.invalidations_
.begin(),
73 void UnackedInvalidationSetEqMatcher::DescribeTo(::std::ostream
* os
) const {
74 *os
<< " is equal to " << PrintToString(expected_
);
77 void UnackedInvalidationSetEqMatcher::DescribeNegationTo(
78 ::std::ostream
* os
) const {
79 *os
<< " isn't equal to " << PrintToString(expected_
);
82 // We're done declaring UnackedInvalidationSetEqMatcher. Everything else can
83 // go into the null namespace.
86 ObjectIdInvalidationMap
UnackedInvalidationsMapToObjectIdInvalidationMap(
87 const UnackedInvalidationsMap
& state_map
) {
88 ObjectIdInvalidationMap object_id_invalidation_map
;
89 for (UnackedInvalidationsMap::const_iterator it
= state_map
.begin();
90 it
!= state_map
.end(); ++it
) {
91 it
->second
.ExportInvalidations(syncer::WeakHandle
<AckHandler
>(),
92 &object_id_invalidation_map
);
94 return object_id_invalidation_map
;
97 class UnackedInvalidationsMapEqMatcher
98 : public testing::MatcherInterface
<const UnackedInvalidationsMap
&> {
100 explicit UnackedInvalidationsMapEqMatcher(
101 const UnackedInvalidationsMap
& expected
);
103 virtual bool MatchAndExplain(const UnackedInvalidationsMap
& actual
,
104 MatchResultListener
* listener
) const;
105 virtual void DescribeTo(::std::ostream
* os
) const;
106 virtual void DescribeNegationTo(::std::ostream
* os
) const;
109 const UnackedInvalidationsMap expected_
;
111 DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationsMapEqMatcher
);
114 UnackedInvalidationsMapEqMatcher::UnackedInvalidationsMapEqMatcher(
115 const UnackedInvalidationsMap
& expected
)
116 : expected_(expected
) {
119 bool UnackedInvalidationsMapEqMatcher::MatchAndExplain(
120 const UnackedInvalidationsMap
& actual
,
121 MatchResultListener
* listener
) const {
122 ObjectIdInvalidationMap expected_inv
=
123 UnackedInvalidationsMapToObjectIdInvalidationMap(expected_
);
124 ObjectIdInvalidationMap actual_inv
=
125 UnackedInvalidationsMapToObjectIdInvalidationMap(actual
);
127 return expected_inv
== actual_inv
;
130 void UnackedInvalidationsMapEqMatcher::DescribeTo(
131 ::std::ostream
* os
) const {
132 *os
<< " is equal to " << PrintToString(expected_
);
135 void UnackedInvalidationsMapEqMatcher::DescribeNegationTo(
136 ::std::ostream
* os
) const {
137 *os
<< " isn't equal to " << PrintToString(expected_
);
142 void PrintTo(const UnackedInvalidationSet
& invalidations
,
143 ::std::ostream
* os
) {
144 scoped_ptr
<base::DictionaryValue
> value
= invalidations
.ToValue();
147 JSONStringValueSerializer
serializer(&output
);
148 serializer
.set_pretty_print(true);
149 serializer
.Serialize(*value
.get());
154 void PrintTo(const UnackedInvalidationsMap
& map
, ::std::ostream
* os
) {
155 scoped_ptr
<base::ListValue
> list(new base::ListValue
);
156 for (UnackedInvalidationsMap::const_iterator it
= map
.begin();
157 it
!= map
.end(); ++it
) {
158 list
->Append(it
->second
.ToValue().release());
162 JSONStringValueSerializer
serializer(&output
);
163 serializer
.set_pretty_print(true);
164 serializer
.Serialize(*list
.get());
169 Matcher
<const UnackedInvalidationSet
&> Eq(
170 const UnackedInvalidationSet
& expected
) {
171 return MakeMatcher(new UnackedInvalidationSetEqMatcher(expected
));
174 Matcher
<const UnackedInvalidationsMap
&> Eq(
175 const UnackedInvalidationsMap
& expected
) {
176 return MakeMatcher(new UnackedInvalidationsMapEqMatcher(expected
));
179 } // namespace test_util