Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / invalidation / impl / unacked_invalidation_set_test_util.cc
blob7201e75414480330b1b29cf0ba122f3ab306a08f
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 #include "components/invalidation/impl/unacked_invalidation_set_test_util.h"
7 #include "base/json/json_string_value_serializer.h"
8 #include "components/invalidation/public/object_id_invalidation_map.h"
9 #include "testing/gmock/include/gmock/gmock-matchers.h"
11 namespace syncer {
13 using ::testing::MakeMatcher;
14 using ::testing::MatchResultListener;
15 using ::testing::Matcher;
16 using ::testing::MatcherInterface;
17 using ::testing::PrintToString;
19 namespace test_util {
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
24 // function.
25 class UnackedInvalidationSetEqMatcher
26 : public testing::MatcherInterface<const UnackedInvalidationSet&> {
27 public:
28 explicit UnackedInvalidationSetEqMatcher(
29 const UnackedInvalidationSet& expected);
31 bool MatchAndExplain(
32 const UnackedInvalidationSet& actual,
33 MatchResultListener* listener) const override;
34 void DescribeTo(::std::ostream* os) const override;
35 void DescribeNegationTo(::std::ostream* os) const override;
37 private:
38 const UnackedInvalidationSet expected_;
40 DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationSetEqMatcher);
43 namespace {
45 struct InvalidationEq {
46 bool operator()(const syncer::Invalidation& a,
47 const syncer::Invalidation& b) const {
48 return a.Equals(b);
52 } // namespace
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
62 // instances.
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(),
70 InvalidationEq());
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.
84 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(
92 base::WeakPtr<AckHandler>(),
93 scoped_refptr<base::SingleThreadTaskRunner>(),
94 &object_id_invalidation_map);
96 return object_id_invalidation_map;
99 class UnackedInvalidationsMapEqMatcher
100 : public testing::MatcherInterface<const UnackedInvalidationsMap&> {
101 public:
102 explicit UnackedInvalidationsMapEqMatcher(
103 const UnackedInvalidationsMap& expected);
105 virtual bool MatchAndExplain(const UnackedInvalidationsMap& actual,
106 MatchResultListener* listener) const;
107 virtual void DescribeTo(::std::ostream* os) const;
108 virtual void DescribeNegationTo(::std::ostream* os) const;
110 private:
111 const UnackedInvalidationsMap expected_;
113 DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationsMapEqMatcher);
116 UnackedInvalidationsMapEqMatcher::UnackedInvalidationsMapEqMatcher(
117 const UnackedInvalidationsMap& expected)
118 : expected_(expected) {
121 bool UnackedInvalidationsMapEqMatcher::MatchAndExplain(
122 const UnackedInvalidationsMap& actual,
123 MatchResultListener* listener) const {
124 ObjectIdInvalidationMap expected_inv =
125 UnackedInvalidationsMapToObjectIdInvalidationMap(expected_);
126 ObjectIdInvalidationMap actual_inv =
127 UnackedInvalidationsMapToObjectIdInvalidationMap(actual);
129 return expected_inv == actual_inv;
132 void UnackedInvalidationsMapEqMatcher::DescribeTo(
133 ::std::ostream* os) const {
134 *os << " is equal to " << PrintToString(expected_);
137 void UnackedInvalidationsMapEqMatcher::DescribeNegationTo(
138 ::std::ostream* os) const {
139 *os << " isn't equal to " << PrintToString(expected_);
142 } // namespace
144 void PrintTo(const UnackedInvalidationSet& invalidations,
145 ::std::ostream* os) {
146 scoped_ptr<base::DictionaryValue> value = invalidations.ToValue();
148 std::string output;
149 JSONStringValueSerializer serializer(&output);
150 serializer.set_pretty_print(true);
151 serializer.Serialize(*value.get());
153 (*os) << output;
156 void PrintTo(const UnackedInvalidationsMap& map, ::std::ostream* os) {
157 scoped_ptr<base::ListValue> list(new base::ListValue);
158 for (UnackedInvalidationsMap::const_iterator it = map.begin();
159 it != map.end(); ++it) {
160 list->Append(it->second.ToValue().release());
163 std::string output;
164 JSONStringValueSerializer serializer(&output);
165 serializer.set_pretty_print(true);
166 serializer.Serialize(*list.get());
168 (*os) << output;
171 Matcher<const UnackedInvalidationSet&> Eq(
172 const UnackedInvalidationSet& expected) {
173 return MakeMatcher(new UnackedInvalidationSetEqMatcher(expected));
176 Matcher<const UnackedInvalidationsMap&> Eq(
177 const UnackedInvalidationsMap& expected) {
178 return MakeMatcher(new UnackedInvalidationsMapEqMatcher(expected));
181 } // namespace test_util