• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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/internal_api/public/base/invalidation_test_util.h"
6 
7 #include "base/basictypes.h"
8 #include "base/json/json_writer.h"
9 #include "base/json/string_escape.h"
10 #include "base/values.h"
11 #include "sync/internal_api/public/base/invalidation.h"
12 
13 namespace syncer {
14 
15 using ::testing::MakeMatcher;
16 using ::testing::MatchResultListener;
17 using ::testing::Matcher;
18 using ::testing::MatcherInterface;
19 using ::testing::PrintToString;
20 
21 namespace {
22 
23 class AckHandleEqMatcher
24     : public MatcherInterface<const AckHandle&> {
25  public:
26   explicit AckHandleEqMatcher(const AckHandle& expected);
27 
28   virtual bool MatchAndExplain(const AckHandle& actual,
29                                MatchResultListener* listener) const;
30   virtual void DescribeTo(::std::ostream* os) const;
31   virtual void DescribeNegationTo(::std::ostream* os) const;
32 
33  private:
34   const AckHandle expected_;
35 
36   DISALLOW_COPY_AND_ASSIGN(AckHandleEqMatcher);
37 };
38 
AckHandleEqMatcher(const AckHandle & expected)39 AckHandleEqMatcher::AckHandleEqMatcher(const AckHandle& expected)
40     : expected_(expected) {
41 }
42 
MatchAndExplain(const AckHandle & actual,MatchResultListener * listener) const43 bool AckHandleEqMatcher::MatchAndExplain(const AckHandle& actual,
44                                          MatchResultListener* listener) const {
45   return expected_.Equals(actual);
46 }
47 
DescribeTo(::std::ostream * os) const48 void AckHandleEqMatcher::DescribeTo(::std::ostream* os) const {
49   *os << " is equal to " << PrintToString(expected_);
50 }
51 
DescribeNegationTo(::std::ostream * os) const52 void AckHandleEqMatcher::DescribeNegationTo(::std::ostream* os) const {
53   *os << " isn't equal to " << PrintToString(expected_);
54 }
55 
56 class InvalidationEqMatcher
57     : public MatcherInterface<const Invalidation&> {
58  public:
59   explicit InvalidationEqMatcher(const Invalidation& expected);
60 
61   virtual bool MatchAndExplain(const Invalidation& actual,
62                                MatchResultListener* listener) const;
63   virtual void DescribeTo(::std::ostream* os) const;
64   virtual void DescribeNegationTo(::std::ostream* os) const;
65 
66  private:
67   const Invalidation expected_;
68 
69   DISALLOW_COPY_AND_ASSIGN(InvalidationEqMatcher);
70 };
71 
InvalidationEqMatcher(const Invalidation & expected)72 InvalidationEqMatcher::InvalidationEqMatcher(
73     const Invalidation& expected) : expected_(expected) {
74 }
75 
MatchAndExplain(const Invalidation & actual,MatchResultListener * listener) const76 bool InvalidationEqMatcher::MatchAndExplain(
77     const Invalidation& actual, MatchResultListener* listener) const {
78   if (!(expected_.object_id() == actual.object_id())) {
79     return false;
80   }
81   if (expected_.is_unknown_version() && actual.is_unknown_version()) {
82     return true;
83   } else if (expected_.is_unknown_version() != actual.is_unknown_version()) {
84     return false;
85   } else {
86     // Neither is unknown version.
87     return expected_.payload() == actual.payload()
88         && expected_.version() == actual.version();
89   }
90 }
91 
DescribeTo(::std::ostream * os) const92 void InvalidationEqMatcher::DescribeTo(::std::ostream* os) const {
93   *os << " is equal to " << PrintToString(expected_);
94 }
95 
DescribeNegationTo(::std::ostream * os) const96 void InvalidationEqMatcher::DescribeNegationTo(::std::ostream* os) const {
97   *os << " isn't equal to " << PrintToString(expected_);
98 }
99 
100 }  // namespace
101 
PrintTo(const AckHandle & ack_handle,::std::ostream * os)102 void PrintTo(const AckHandle& ack_handle, ::std::ostream* os ) {
103   scoped_ptr<base::Value> value(ack_handle.ToValue());
104   std::string printable_ack_handle;
105   base::JSONWriter::Write(value.get(), &printable_ack_handle);
106   *os << "{ ack_handle: " << printable_ack_handle << " }";
107 }
108 
Eq(const AckHandle & expected)109 Matcher<const AckHandle&> Eq(const AckHandle& expected) {
110   return MakeMatcher(new AckHandleEqMatcher(expected));
111 }
112 
PrintTo(const Invalidation & inv,::std::ostream * os)113 void PrintTo(const Invalidation& inv, ::std::ostream* os) {
114   *os << "{ payload: " << inv.ToString() << " }";
115 }
116 
Eq(const Invalidation & expected)117 Matcher<const Invalidation&> Eq(const Invalidation& expected) {
118   return MakeMatcher(new InvalidationEqMatcher(expected));
119 }
120 
121 }  // namespace syncer
122