| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/notifier/unacked_invalidation_set.h" | 5 #include "sync/notifier/unacked_invalidation_set.h" |
| 6 | 6 |
| 7 #include "base/json/json_string_value_serializer.h" | 7 #include "base/json/json_string_value_serializer.h" |
| 8 #include "sync/notifier/object_id_invalidation_map.h" | 8 #include "sync/notifier/object_id_invalidation_map.h" |
| 9 #include "sync/notifier/single_object_invalidation_set.h" | 9 #include "sync/notifier/single_object_invalidation_set.h" |
| 10 #include "testing/gmock/include/gmock/gmock-matchers.h" | 10 #include "sync/notifier/unacked_invalidation_set_test_util.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace syncer { | 13 namespace syncer { |
| 14 | 14 |
| 15 // Start with some helper functions and classes. | |
| 16 | |
| 17 using ::testing::MakeMatcher; | |
| 18 using ::testing::MatchResultListener; | |
| 19 using ::testing::Matcher; | |
| 20 using ::testing::MatcherInterface; | |
| 21 using ::testing::PrintToString; | |
| 22 | |
| 23 void PrintTo( | |
| 24 const UnackedInvalidationSet& invalidations, ::std::ostream* os); | |
| 25 | |
| 26 void PrintTo( | |
| 27 const UnackedInvalidationsMap& map, ::std::ostream* os); | |
| 28 | |
| 29 ::testing::Matcher<const UnackedInvalidationSet&> Eq( | |
| 30 const UnackedInvalidationSet& expected); | |
| 31 | |
| 32 ::testing::Matcher<const UnackedInvalidationsMap&> Eq( | |
| 33 const UnackedInvalidationsMap& expected); | |
| 34 | |
| 35 class UnackedInvalidationSetEqMatcher | |
| 36 : public testing::MatcherInterface<const UnackedInvalidationSet&> { | |
| 37 public: | |
| 38 explicit UnackedInvalidationSetEqMatcher( | |
| 39 const UnackedInvalidationSet& expected); | |
| 40 | |
| 41 virtual bool MatchAndExplain( | |
| 42 const UnackedInvalidationSet& actual, | |
| 43 MatchResultListener* listener) const OVERRIDE; | |
| 44 virtual void DescribeTo(::std::ostream* os) const OVERRIDE; | |
| 45 virtual void DescribeNegationTo(::std::ostream* os) const OVERRIDE; | |
| 46 | |
| 47 private: | |
| 48 const UnackedInvalidationSet expected_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationSetEqMatcher); | |
| 51 }; | |
| 52 | |
| 53 UnackedInvalidationSetEqMatcher::UnackedInvalidationSetEqMatcher( | |
| 54 const UnackedInvalidationSet& expected) | |
| 55 : expected_(expected) {} | |
| 56 | |
| 57 namespace { | |
| 58 | |
| 59 struct InvalidationEq { | |
| 60 bool operator()(const syncer::Invalidation& a, | |
| 61 const syncer::Invalidation& b) const { | |
| 62 return a.Equals(b); | |
| 63 } | |
| 64 }; | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 bool UnackedInvalidationSetEqMatcher::MatchAndExplain( | |
| 69 const UnackedInvalidationSet& actual, | |
| 70 MatchResultListener* listener) const { | |
| 71 // Use our friendship with this class to compare the internals of two | |
| 72 // instances. | |
| 73 // | |
| 74 // Note that the registration status is intentionally not considered | |
| 75 // when performing this comparison. | |
| 76 return expected_.object_id_ == actual.object_id_ | |
| 77 && std::equal(expected_.invalidations_.begin(), | |
| 78 expected_.invalidations_.end(), | |
| 79 actual.invalidations_.begin(), | |
| 80 InvalidationEq()); | |
| 81 } | |
| 82 | |
| 83 void UnackedInvalidationSetEqMatcher::DescribeTo(::std::ostream* os) const { | |
| 84 *os << " is equal to " << PrintToString(expected_); | |
| 85 } | |
| 86 | |
| 87 void UnackedInvalidationSetEqMatcher::DescribeNegationTo( | |
| 88 ::std::ostream* os) const { | |
| 89 *os << " isn't equal to " << PrintToString(expected_); | |
| 90 } | |
| 91 | |
| 92 namespace { | |
| 93 | |
| 94 ObjectIdInvalidationMap UnackedInvalidationsMapToObjectIdInvalidationMap( | |
| 95 const UnackedInvalidationsMap& state_map) { | |
| 96 ObjectIdInvalidationMap object_id_invalidation_map; | |
| 97 for (UnackedInvalidationsMap::const_iterator it = state_map.begin(); | |
| 98 it != state_map.end(); ++it) { | |
| 99 it->second.ExportInvalidations(syncer::WeakHandle<AckHandler>(), | |
| 100 &object_id_invalidation_map); | |
| 101 } | |
| 102 return object_id_invalidation_map; | |
| 103 } | |
| 104 | |
| 105 class UnackedInvalidationsMapEqMatcher | |
| 106 : public testing::MatcherInterface<const UnackedInvalidationsMap&> { | |
| 107 public: | |
| 108 explicit UnackedInvalidationsMapEqMatcher( | |
| 109 const UnackedInvalidationsMap& expected); | |
| 110 | |
| 111 virtual bool MatchAndExplain(const UnackedInvalidationsMap& actual, | |
| 112 MatchResultListener* listener) const; | |
| 113 virtual void DescribeTo(::std::ostream* os) const; | |
| 114 virtual void DescribeNegationTo(::std::ostream* os) const; | |
| 115 | |
| 116 private: | |
| 117 const UnackedInvalidationsMap expected_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationsMapEqMatcher); | |
| 120 }; | |
| 121 | |
| 122 UnackedInvalidationsMapEqMatcher::UnackedInvalidationsMapEqMatcher( | |
| 123 const UnackedInvalidationsMap& expected) | |
| 124 : expected_(expected) { | |
| 125 } | |
| 126 | |
| 127 bool UnackedInvalidationsMapEqMatcher::MatchAndExplain( | |
| 128 const UnackedInvalidationsMap& actual, | |
| 129 MatchResultListener* listener) const { | |
| 130 ObjectIdInvalidationMap expected_inv = | |
| 131 UnackedInvalidationsMapToObjectIdInvalidationMap(expected_); | |
| 132 ObjectIdInvalidationMap actual_inv = | |
| 133 UnackedInvalidationsMapToObjectIdInvalidationMap(actual); | |
| 134 | |
| 135 return expected_inv == actual_inv; | |
| 136 } | |
| 137 | |
| 138 void UnackedInvalidationsMapEqMatcher::DescribeTo( | |
| 139 ::std::ostream* os) const { | |
| 140 *os << " is equal to " << PrintToString(expected_); | |
| 141 } | |
| 142 | |
| 143 void UnackedInvalidationsMapEqMatcher::DescribeNegationTo( | |
| 144 ::std::ostream* os) const { | |
| 145 *os << " isn't equal to " << PrintToString(expected_); | |
| 146 } | |
| 147 | |
| 148 } // namespace | |
| 149 | |
| 150 void PrintTo(const UnackedInvalidationSet& invalidations, | |
| 151 ::std::ostream* os) { | |
| 152 scoped_ptr<base::DictionaryValue> value = invalidations.ToValue(); | |
| 153 | |
| 154 std::string output; | |
| 155 JSONStringValueSerializer serializer(&output); | |
| 156 serializer.set_pretty_print(true); | |
| 157 serializer.Serialize(*value.get()); | |
| 158 | |
| 159 (*os) << output; | |
| 160 } | |
| 161 | |
| 162 void PrintTo(const UnackedInvalidationsMap& map, ::std::ostream* os) { | |
| 163 scoped_ptr<base::ListValue> list(new base::ListValue); | |
| 164 for (UnackedInvalidationsMap::const_iterator it = map.begin(); | |
| 165 it != map.end(); ++it) { | |
| 166 list->Append(it->second.ToValue().release()); | |
| 167 } | |
| 168 | |
| 169 std::string output; | |
| 170 JSONStringValueSerializer serializer(&output); | |
| 171 serializer.set_pretty_print(true); | |
| 172 serializer.Serialize(*list.get()); | |
| 173 | |
| 174 (*os) << output; | |
| 175 } | |
| 176 | |
| 177 Matcher<const UnackedInvalidationSet&> Eq( | |
| 178 const UnackedInvalidationSet& expected) { | |
| 179 return MakeMatcher(new UnackedInvalidationSetEqMatcher(expected)); | |
| 180 } | |
| 181 | |
| 182 Matcher<const UnackedInvalidationsMap&> Eq( | |
| 183 const UnackedInvalidationsMap& expected) { | |
| 184 return MakeMatcher(new UnackedInvalidationsMapEqMatcher(expected)); | |
| 185 } | |
| 186 | |
| 187 class UnackedInvalidationSetTest : public testing::Test { | 15 class UnackedInvalidationSetTest : public testing::Test { |
| 188 public: | 16 public: |
| 189 UnackedInvalidationSetTest() | 17 UnackedInvalidationSetTest() |
| 190 : kObjectId_(10, "ASDF"), | 18 : kObjectId_(10, "ASDF"), |
| 191 unacked_invalidations_(kObjectId_) {} | 19 unacked_invalidations_(kObjectId_) {} |
| 192 | 20 |
| 193 SingleObjectInvalidationSet GetStoredInvalidations() { | 21 SingleObjectInvalidationSet GetStoredInvalidations() { |
| 194 ObjectIdInvalidationMap map; | 22 ObjectIdInvalidationMap map; |
| 195 unacked_invalidations_.ExportInvalidations(WeakHandle<AckHandler>(), &map); | 23 unacked_invalidations_.ExportInvalidations(WeakHandle<AckHandler>(), &map); |
| 196 ObjectIdSet ids = map.GetObjectIds(); | 24 ObjectIdSet ids = map.GetObjectIds(); |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 UnackedInvalidationSet SerializeDeserialize() { | 184 UnackedInvalidationSet SerializeDeserialize() { |
| 357 scoped_ptr<base::DictionaryValue> value = unacked_invalidations_.ToValue(); | 185 scoped_ptr<base::DictionaryValue> value = unacked_invalidations_.ToValue(); |
| 358 UnackedInvalidationSet deserialized(kObjectId_); | 186 UnackedInvalidationSet deserialized(kObjectId_); |
| 359 deserialized.ResetFromValue(*value.get()); | 187 deserialized.ResetFromValue(*value.get()); |
| 360 return deserialized; | 188 return deserialized; |
| 361 } | 189 } |
| 362 }; | 190 }; |
| 363 | 191 |
| 364 TEST_F(UnackedInvalidationSetSerializationTest, Empty) { | 192 TEST_F(UnackedInvalidationSetSerializationTest, Empty) { |
| 365 UnackedInvalidationSet deserialized = SerializeDeserialize(); | 193 UnackedInvalidationSet deserialized = SerializeDeserialize(); |
| 366 EXPECT_THAT(unacked_invalidations_, Eq(deserialized)); | 194 EXPECT_THAT(unacked_invalidations_, test_util::Eq(deserialized)); |
| 367 } | 195 } |
| 368 | 196 |
| 369 TEST_F(UnackedInvalidationSetSerializationTest, OneInvalidation) { | 197 TEST_F(UnackedInvalidationSetSerializationTest, OneInvalidation) { |
| 370 Invalidation inv = Invalidation::Init(kObjectId_, 10, "payload"); | 198 Invalidation inv = Invalidation::Init(kObjectId_, 10, "payload"); |
| 371 unacked_invalidations_.Add(inv); | 199 unacked_invalidations_.Add(inv); |
| 372 | 200 |
| 373 UnackedInvalidationSet deserialized = SerializeDeserialize(); | 201 UnackedInvalidationSet deserialized = SerializeDeserialize(); |
| 374 EXPECT_THAT(unacked_invalidations_, Eq(deserialized)); | 202 EXPECT_THAT(unacked_invalidations_, test_util::Eq(deserialized)); |
| 375 } | 203 } |
| 376 | 204 |
| 377 TEST_F(UnackedInvalidationSetSerializationTest, WithUnknownVersion) { | 205 TEST_F(UnackedInvalidationSetSerializationTest, WithUnknownVersion) { |
| 378 Invalidation inv1 = Invalidation::Init(kObjectId_, 10, "payload"); | 206 Invalidation inv1 = Invalidation::Init(kObjectId_, 10, "payload"); |
| 379 Invalidation inv2 = Invalidation::InitUnknownVersion(kObjectId_); | 207 Invalidation inv2 = Invalidation::InitUnknownVersion(kObjectId_); |
| 380 Invalidation inv3 = Invalidation::InitUnknownVersion(kObjectId_); | 208 Invalidation inv3 = Invalidation::InitUnknownVersion(kObjectId_); |
| 381 unacked_invalidations_.Add(inv1); | 209 unacked_invalidations_.Add(inv1); |
| 382 unacked_invalidations_.Add(inv2); | 210 unacked_invalidations_.Add(inv2); |
| 383 unacked_invalidations_.Add(inv3); | 211 unacked_invalidations_.Add(inv3); |
| 384 | 212 |
| 385 UnackedInvalidationSet deserialized = SerializeDeserialize(); | 213 UnackedInvalidationSet deserialized = SerializeDeserialize(); |
| 386 EXPECT_THAT(unacked_invalidations_, Eq(deserialized)); | 214 EXPECT_THAT(unacked_invalidations_, test_util::Eq(deserialized)); |
| 387 } | 215 } |
| 388 | 216 |
| 389 } // namespace | 217 } // namespace |
| 390 | 218 |
| 391 } // namespace syncer | 219 } // namespace syncer |
| OLD | NEW |