[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 3e31fa4 | 2012-10-04 03:53:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 5 | #include "components/invalidation/invalidation_test_util.h" |
[email protected] | 3e31fa4 | 2012-10-04 03:53:09 | [diff] [blame] | 6 | |
| 7 | #include "base/basictypes.h" |
[email protected] | 0e7f2b7 | 2012-12-05 05:31:12 | [diff] [blame] | 8 | #include "base/json/json_writer.h" |
| 9 | #include "base/json/string_escape.h" |
| 10 | #include "base/values.h" |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 11 | #include "components/invalidation/invalidation.h" |
[email protected] | 3e31fa4 | 2012-10-04 03:53:09 | [diff] [blame] | 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 | |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 23 | class AckHandleEqMatcher : public MatcherInterface<const AckHandle&> { |
[email protected] | 0e7f2b7 | 2012-12-05 05:31:12 | [diff] [blame] | 24 | public: |
| 25 | explicit AckHandleEqMatcher(const AckHandle& expected); |
| 26 | |
| 27 | virtual bool MatchAndExplain(const AckHandle& actual, |
| 28 | MatchResultListener* listener) const; |
| 29 | virtual void DescribeTo(::std::ostream* os) const; |
| 30 | virtual void DescribeNegationTo(::std::ostream* os) const; |
| 31 | |
| 32 | private: |
| 33 | const AckHandle expected_; |
| 34 | |
| 35 | DISALLOW_COPY_AND_ASSIGN(AckHandleEqMatcher); |
| 36 | }; |
| 37 | |
| 38 | AckHandleEqMatcher::AckHandleEqMatcher(const AckHandle& expected) |
| 39 | : expected_(expected) { |
| 40 | } |
| 41 | |
| 42 | bool AckHandleEqMatcher::MatchAndExplain(const AckHandle& actual, |
| 43 | MatchResultListener* listener) const { |
| 44 | return expected_.Equals(actual); |
| 45 | } |
| 46 | |
| 47 | void AckHandleEqMatcher::DescribeTo(::std::ostream* os) const { |
| 48 | *os << " is equal to " << PrintToString(expected_); |
| 49 | } |
| 50 | |
| 51 | void AckHandleEqMatcher::DescribeNegationTo(::std::ostream* os) const { |
| 52 | *os << " isn't equal to " << PrintToString(expected_); |
| 53 | } |
| 54 | |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 55 | class InvalidationEqMatcher : public MatcherInterface<const Invalidation&> { |
[email protected] | 3e31fa4 | 2012-10-04 03:53:09 | [diff] [blame] | 56 | public: |
| 57 | explicit InvalidationEqMatcher(const Invalidation& expected); |
| 58 | |
| 59 | virtual bool MatchAndExplain(const Invalidation& actual, |
| 60 | MatchResultListener* listener) const; |
| 61 | virtual void DescribeTo(::std::ostream* os) const; |
| 62 | virtual void DescribeNegationTo(::std::ostream* os) const; |
| 63 | |
| 64 | private: |
| 65 | const Invalidation expected_; |
| 66 | |
| 67 | DISALLOW_COPY_AND_ASSIGN(InvalidationEqMatcher); |
| 68 | }; |
| 69 | |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 70 | InvalidationEqMatcher::InvalidationEqMatcher(const Invalidation& expected) |
| 71 | : expected_(expected) { |
[email protected] | 3e31fa4 | 2012-10-04 03:53:09 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | bool InvalidationEqMatcher::MatchAndExplain( |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 75 | const Invalidation& actual, |
| 76 | MatchResultListener* listener) const { |
[email protected] | 163d063 | 2013-10-04 03:51:01 | [diff] [blame] | 77 | if (!(expected_.object_id() == actual.object_id())) { |
| 78 | return false; |
| 79 | } |
| 80 | if (expected_.is_unknown_version() && actual.is_unknown_version()) { |
| 81 | return true; |
| 82 | } else if (expected_.is_unknown_version() != actual.is_unknown_version()) { |
| 83 | return false; |
| 84 | } else { |
| 85 | // Neither is unknown version. |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 86 | return expected_.payload() == actual.payload() && |
| 87 | expected_.version() == actual.version(); |
[email protected] | 163d063 | 2013-10-04 03:51:01 | [diff] [blame] | 88 | } |
[email protected] | 3e31fa4 | 2012-10-04 03:53:09 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void InvalidationEqMatcher::DescribeTo(::std::ostream* os) const { |
| 92 | *os << " is equal to " << PrintToString(expected_); |
| 93 | } |
| 94 | |
| 95 | void InvalidationEqMatcher::DescribeNegationTo(::std::ostream* os) const { |
| 96 | *os << " isn't equal to " << PrintToString(expected_); |
| 97 | } |
| 98 | |
| 99 | } // namespace |
| 100 | |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 101 | void PrintTo(const AckHandle& ack_handle, ::std::ostream* os) { |
[email protected] | 0e7f2b7 | 2012-12-05 05:31:12 | [diff] [blame] | 102 | scoped_ptr<base::Value> value(ack_handle.ToValue()); |
| 103 | std::string printable_ack_handle; |
| 104 | base::JSONWriter::Write(value.get(), &printable_ack_handle); |
| 105 | *os << "{ ack_handle: " << printable_ack_handle << " }"; |
| 106 | } |
| 107 | |
| 108 | Matcher<const AckHandle&> Eq(const AckHandle& expected) { |
| 109 | return MakeMatcher(new AckHandleEqMatcher(expected)); |
| 110 | } |
| 111 | |
[email protected] | 163d063 | 2013-10-04 03:51:01 | [diff] [blame] | 112 | void PrintTo(const Invalidation& inv, ::std::ostream* os) { |
| 113 | *os << "{ payload: " << inv.ToString() << " }"; |
[email protected] | 3e31fa4 | 2012-10-04 03:53:09 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | Matcher<const Invalidation&> Eq(const Invalidation& expected) { |
| 117 | return MakeMatcher(new InvalidationEqMatcher(expected)); |
| 118 | } |
| 119 | |
| 120 | } // namespace syncer |