| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "components/invalidation/unacked_invalidation_set_test_util.h" | |
| 6 | |
| 7 #include "base/json/json_string_value_serializer.h" | |
| 8 #include "components/invalidation/object_id_invalidation_map.h" | |
| 9 #include "testing/gmock/include/gmock/gmock-matchers.h" | |
| 10 | |
| 11 namespace syncer { | |
| 12 | |
| 13 using ::testing::MakeMatcher; | |
| 14 using ::testing::MatchResultListener; | |
| 15 using ::testing::Matcher; | |
| 16 using ::testing::MatcherInterface; | |
| 17 using ::testing::PrintToString; | |
| 18 | |
| 19 namespace test_util { | |
| 20 | |
| 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); | |
| 30 | |
| 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; | |
| 36 | |
| 37 private: | |
| 38 const UnackedInvalidationSet expected_; | |
| 39 | |
| 40 DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationSetEqMatcher); | |
| 41 }; | |
| 42 | |
| 43 namespace { | |
| 44 | |
| 45 struct InvalidationEq { | |
| 46 bool operator()(const syncer::Invalidation& a, | |
| 47 const syncer::Invalidation& b) const { | |
| 48 return a.Equals(b); | |
| 49 } | |
| 50 }; | |
| 51 | |
| 52 } // namespace | |
| 53 | |
| 54 UnackedInvalidationSetEqMatcher::UnackedInvalidationSetEqMatcher( | |
| 55 const UnackedInvalidationSet& expected) | |
| 56 : expected_(expected) {} | |
| 57 | |
| 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. | |
| 63 // | |
| 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()); | |
| 71 } | |
| 72 | |
| 73 void UnackedInvalidationSetEqMatcher::DescribeTo(::std::ostream* os) const { | |
| 74 *os << " is equal to " << PrintToString(expected_); | |
| 75 } | |
| 76 | |
| 77 void UnackedInvalidationSetEqMatcher::DescribeNegationTo( | |
| 78 ::std::ostream* os) const { | |
| 79 *os << " isn't equal to " << PrintToString(expected_); | |
| 80 } | |
| 81 | |
| 82 // We're done declaring UnackedInvalidationSetEqMatcher. Everything else can | |
| 83 // go into the null namespace. | |
| 84 namespace { | |
| 85 | |
| 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); | |
| 95 } | |
| 96 return object_id_invalidation_map; | |
| 97 } | |
| 98 | |
| 99 class UnackedInvalidationsMapEqMatcher | |
| 100 : public testing::MatcherInterface<const UnackedInvalidationsMap&> { | |
| 101 public: | |
| 102 explicit UnackedInvalidationsMapEqMatcher( | |
| 103 const UnackedInvalidationsMap& expected); | |
| 104 | |
| 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; | |
| 109 | |
| 110 private: | |
| 111 const UnackedInvalidationsMap expected_; | |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationsMapEqMatcher); | |
| 114 }; | |
| 115 | |
| 116 UnackedInvalidationsMapEqMatcher::UnackedInvalidationsMapEqMatcher( | |
| 117 const UnackedInvalidationsMap& expected) | |
| 118 : expected_(expected) { | |
| 119 } | |
| 120 | |
| 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); | |
| 128 | |
| 129 return expected_inv == actual_inv; | |
| 130 } | |
| 131 | |
| 132 void UnackedInvalidationsMapEqMatcher::DescribeTo( | |
| 133 ::std::ostream* os) const { | |
| 134 *os << " is equal to " << PrintToString(expected_); | |
| 135 } | |
| 136 | |
| 137 void UnackedInvalidationsMapEqMatcher::DescribeNegationTo( | |
| 138 ::std::ostream* os) const { | |
| 139 *os << " isn't equal to " << PrintToString(expected_); | |
| 140 } | |
| 141 | |
| 142 } // namespace | |
| 143 | |
| 144 void PrintTo(const UnackedInvalidationSet& invalidations, | |
| 145 ::std::ostream* os) { | |
| 146 scoped_ptr<base::DictionaryValue> value = invalidations.ToValue(); | |
| 147 | |
| 148 std::string output; | |
| 149 JSONStringValueSerializer serializer(&output); | |
| 150 serializer.set_pretty_print(true); | |
| 151 serializer.Serialize(*value.get()); | |
| 152 | |
| 153 (*os) << output; | |
| 154 } | |
| 155 | |
| 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()); | |
| 161 } | |
| 162 | |
| 163 std::string output; | |
| 164 JSONStringValueSerializer serializer(&output); | |
| 165 serializer.set_pretty_print(true); | |
| 166 serializer.Serialize(*list.get()); | |
| 167 | |
| 168 (*os) << output; | |
| 169 } | |
| 170 | |
| 171 Matcher<const UnackedInvalidationSet&> Eq( | |
| 172 const UnackedInvalidationSet& expected) { | |
| 173 return MakeMatcher(new UnackedInvalidationSetEqMatcher(expected)); | |
| 174 } | |
| 175 | |
| 176 Matcher<const UnackedInvalidationsMap&> Eq( | |
| 177 const UnackedInvalidationsMap& expected) { | |
| 178 return MakeMatcher(new UnackedInvalidationsMapEqMatcher(expected)); | |
| 179 } | |
| 180 | |
| 181 } // namespace test_util | |
| 182 | |
| 183 }; | |
| OLD | NEW |