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.
5 #include "components/invalidation/impl/invalidation_test_util.h"
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 "components/invalidation/public/invalidation.h"
15 using ::testing::MakeMatcher
;
16 using ::testing::MatchResultListener
;
17 using ::testing::Matcher
;
18 using ::testing::MatcherInterface
;
19 using ::testing::PrintToString
;
23 class AckHandleEqMatcher
: public MatcherInterface
<const AckHandle
&> {
25 explicit AckHandleEqMatcher(const AckHandle
& expected
);
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;
33 const AckHandle expected_
;
35 DISALLOW_COPY_AND_ASSIGN(AckHandleEqMatcher
);
38 AckHandleEqMatcher::AckHandleEqMatcher(const AckHandle
& expected
)
39 : expected_(expected
) {
42 bool AckHandleEqMatcher::MatchAndExplain(const AckHandle
& actual
,
43 MatchResultListener
* listener
) const {
44 return expected_
.Equals(actual
);
47 void AckHandleEqMatcher::DescribeTo(::std::ostream
* os
) const {
48 *os
<< " is equal to " << PrintToString(expected_
);
51 void AckHandleEqMatcher::DescribeNegationTo(::std::ostream
* os
) const {
52 *os
<< " isn't equal to " << PrintToString(expected_
);
55 class InvalidationEqMatcher
: public MatcherInterface
<const Invalidation
&> {
57 explicit InvalidationEqMatcher(const Invalidation
& expected
);
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;
65 const Invalidation expected_
;
67 DISALLOW_COPY_AND_ASSIGN(InvalidationEqMatcher
);
70 InvalidationEqMatcher::InvalidationEqMatcher(const Invalidation
& expected
)
71 : expected_(expected
) {
74 bool InvalidationEqMatcher::MatchAndExplain(
75 const Invalidation
& actual
,
76 MatchResultListener
* listener
) const {
77 if (!(expected_
.object_id() == actual
.object_id())) {
80 if (expected_
.is_unknown_version() && actual
.is_unknown_version()) {
82 } else if (expected_
.is_unknown_version() != actual
.is_unknown_version()) {
85 // Neither is unknown version.
86 return expected_
.payload() == actual
.payload() &&
87 expected_
.version() == actual
.version();
91 void InvalidationEqMatcher::DescribeTo(::std::ostream
* os
) const {
92 *os
<< " is equal to " << PrintToString(expected_
);
95 void InvalidationEqMatcher::DescribeNegationTo(::std::ostream
* os
) const {
96 *os
<< " isn't equal to " << PrintToString(expected_
);
101 void PrintTo(const AckHandle
& ack_handle
, ::std::ostream
* os
) {
102 std::string printable_ack_handle
;
103 base::JSONWriter::Write(*ack_handle
.ToValue(), &printable_ack_handle
);
104 *os
<< "{ ack_handle: " << printable_ack_handle
<< " }";
107 Matcher
<const AckHandle
&> Eq(const AckHandle
& expected
) {
108 return MakeMatcher(new AckHandleEqMatcher(expected
));
111 void PrintTo(const Invalidation
& inv
, ::std::ostream
* os
) {
112 *os
<< "{ payload: " << inv
.ToString() << " }";
115 Matcher
<const Invalidation
&> Eq(const Invalidation
& expected
) {
116 return MakeMatcher(new InvalidationEqMatcher(expected
));
119 } // namespace syncer