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.
5 #include "sync/internal_api/public/base/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 "sync/internal_api/public/base/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
24 : public MatcherInterface
<const AckHandle
&> {
26 explicit AckHandleEqMatcher(const AckHandle
& expected
);
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;
34 const AckHandle expected_
;
36 DISALLOW_COPY_AND_ASSIGN(AckHandleEqMatcher
);
39 AckHandleEqMatcher::AckHandleEqMatcher(const AckHandle
& expected
)
40 : expected_(expected
) {
43 bool AckHandleEqMatcher::MatchAndExplain(const AckHandle
& actual
,
44 MatchResultListener
* listener
) const {
45 return expected_
.Equals(actual
);
48 void AckHandleEqMatcher::DescribeTo(::std::ostream
* os
) const {
49 *os
<< " is equal to " << PrintToString(expected_
);
52 void AckHandleEqMatcher::DescribeNegationTo(::std::ostream
* os
) const {
53 *os
<< " isn't equal to " << PrintToString(expected_
);
56 class InvalidationEqMatcher
57 : public MatcherInterface
<const Invalidation
&> {
59 explicit InvalidationEqMatcher(const Invalidation
& expected
);
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;
67 const Invalidation expected_
;
69 DISALLOW_COPY_AND_ASSIGN(InvalidationEqMatcher
);
72 InvalidationEqMatcher::InvalidationEqMatcher(
73 const Invalidation
& expected
) : expected_(expected
) {
76 bool InvalidationEqMatcher::MatchAndExplain(
77 const Invalidation
& actual
, MatchResultListener
* listener
) const {
78 if (!(expected_
.object_id() == actual
.object_id())) {
81 if (expected_
.is_unknown_version() && actual
.is_unknown_version()) {
83 } else if (expected_
.is_unknown_version() != actual
.is_unknown_version()) {
86 // Neither is unknown version.
87 return expected_
.payload() == actual
.payload()
88 && expected_
.version() == actual
.version();
92 void InvalidationEqMatcher::DescribeTo(::std::ostream
* os
) const {
93 *os
<< " is equal to " << PrintToString(expected_
);
96 void InvalidationEqMatcher::DescribeNegationTo(::std::ostream
* os
) const {
97 *os
<< " isn't equal to " << PrintToString(expected_
);
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
<< " }";
109 Matcher
<const AckHandle
&> Eq(const AckHandle
& expected
) {
110 return MakeMatcher(new AckHandleEqMatcher(expected
));
113 void PrintTo(const Invalidation
& inv
, ::std::ostream
* os
) {
114 *os
<< "{ payload: " << inv
.ToString() << " }";
117 Matcher
<const Invalidation
&> Eq(const Invalidation
& expected
) {
118 return MakeMatcher(new InvalidationEqMatcher(expected
));
121 } // namespace syncer