Home | History | Annotate | Download | only in notifier
      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.
      4 
      5 #include "sync/notifier/unacked_invalidation_set_test_util.h"
      6 
      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"
     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   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;
     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(syncer::WeakHandle<AckHandler>(),
     92                                    &object_id_invalidation_map);
     93   }
     94   return object_id_invalidation_map;
     95 }
     96 
     97 class UnackedInvalidationsMapEqMatcher
     98     : public testing::MatcherInterface<const UnackedInvalidationsMap&> {
     99  public:
    100   explicit UnackedInvalidationsMapEqMatcher(
    101       const UnackedInvalidationsMap& expected);
    102 
    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;
    107 
    108  private:
    109   const UnackedInvalidationsMap expected_;
    110 
    111   DISALLOW_COPY_AND_ASSIGN(UnackedInvalidationsMapEqMatcher);
    112 };
    113 
    114 UnackedInvalidationsMapEqMatcher::UnackedInvalidationsMapEqMatcher(
    115     const UnackedInvalidationsMap& expected)
    116     : expected_(expected) {
    117 }
    118 
    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);
    126 
    127   return expected_inv == actual_inv;
    128 }
    129 
    130 void UnackedInvalidationsMapEqMatcher::DescribeTo(
    131     ::std::ostream* os) const {
    132   *os << " is equal to " << PrintToString(expected_);
    133 }
    134 
    135 void UnackedInvalidationsMapEqMatcher::DescribeNegationTo(
    136     ::std::ostream* os) const {
    137   *os << " isn't equal to " << PrintToString(expected_);
    138 }
    139 
    140 }  // namespace
    141 
    142 void PrintTo(const UnackedInvalidationSet& invalidations,
    143              ::std::ostream* os) {
    144   scoped_ptr<base::DictionaryValue> value = invalidations.ToValue();
    145 
    146   std::string output;
    147   JSONStringValueSerializer serializer(&output);
    148   serializer.set_pretty_print(true);
    149   serializer.Serialize(*value.get());
    150 
    151   (*os) << output;
    152 }
    153 
    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());
    159   }
    160 
    161   std::string output;
    162   JSONStringValueSerializer serializer(&output);
    163   serializer.set_pretty_print(true);
    164   serializer.Serialize(*list.get());
    165 
    166   (*os) << output;
    167 }
    168 
    169 Matcher<const UnackedInvalidationSet&> Eq(
    170     const UnackedInvalidationSet& expected) {
    171   return MakeMatcher(new UnackedInvalidationSetEqMatcher(expected));
    172 }
    173 
    174 Matcher<const UnackedInvalidationsMap&> Eq(
    175     const UnackedInvalidationsMap& expected) {
    176   return MakeMatcher(new UnackedInvalidationsMapEqMatcher(expected));
    177 }
    178 
    179 }  // namespace test_util
    180 
    181 };
    182