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.
5 #include "sync/notifier/mock_ack_handler.h"
7 #include "sync/internal_api/public/base/ack_handle.h"
8 #include "sync/internal_api/public/base/invalidation.h"
14 struct AckHandleMatcher
{
15 AckHandleMatcher(const AckHandle
& handle
);
16 bool operator()(const syncer::Invalidation
& invalidation
) const;
18 syncer::AckHandle handle_
;
21 AckHandleMatcher::AckHandleMatcher(const AckHandle
& handle
)
24 bool AckHandleMatcher::operator()(
25 const syncer::Invalidation
& invalidation
) const {
26 return handle_
.Equals(invalidation
.ack_handle());
31 MockAckHandler::MockAckHandler() {}
33 MockAckHandler::~MockAckHandler() {}
35 void MockAckHandler::RegisterInvalidation(Invalidation
* invalidation
) {
36 unacked_invalidations_
.push_back(*invalidation
);
37 invalidation
->set_ack_handler(WeakHandleThis());
40 void MockAckHandler::RegisterUnsentInvalidation(Invalidation
* invalidation
) {
41 unsent_invalidations_
.push_back(*invalidation
);
44 bool MockAckHandler::IsUnacked(const Invalidation
& invalidation
) const {
45 AckHandleMatcher
matcher(invalidation
.ack_handle());
46 InvalidationVector::const_iterator it
= std::find_if(
47 unacked_invalidations_
.begin(),
48 unacked_invalidations_
.end(),
50 return it
!= unacked_invalidations_
.end();
53 bool MockAckHandler::IsAcknowledged(const Invalidation
& invalidation
) const {
54 AckHandleMatcher
matcher(invalidation
.ack_handle());
55 InvalidationVector::const_iterator it
= std::find_if(
56 acked_invalidations_
.begin(),
57 acked_invalidations_
.end(),
59 return it
!= acked_invalidations_
.end();
62 bool MockAckHandler::IsDropped(const Invalidation
& invalidation
) const {
63 AckHandleMatcher
matcher(invalidation
.ack_handle());
64 InvalidationVector::const_iterator it
= std::find_if(
65 dropped_invalidations_
.begin(),
66 dropped_invalidations_
.end(),
68 return it
!= dropped_invalidations_
.end();
71 bool MockAckHandler::IsUnsent(const Invalidation
& invalidation
) const {
72 AckHandleMatcher
matcher(invalidation
.ack_handle());
73 InvalidationVector::const_iterator it1
= std::find_if(
74 unsent_invalidations_
.begin(),
75 unsent_invalidations_
.end(),
77 return it1
!= unsent_invalidations_
.end();
80 bool MockAckHandler::AllInvalidationsAccountedFor() const {
81 return unacked_invalidations_
.empty() && unrecovered_drop_events_
.empty();
84 void MockAckHandler::Acknowledge(
85 const invalidation::ObjectId
& id
,
86 const AckHandle
& handle
) {
87 AckHandleMatcher
matcher(handle
);
88 InvalidationVector::iterator it
= std::find_if(
89 unacked_invalidations_
.begin(),
90 unacked_invalidations_
.end(),
92 if (it
!= unacked_invalidations_
.end()) {
93 acked_invalidations_
.push_back(*it
);
94 unacked_invalidations_
.erase(it
);
97 IdHandleMap::iterator it2
= unrecovered_drop_events_
.find(id
);
98 if (it2
!= unrecovered_drop_events_
.end() && it2
->second
.Equals(handle
)) {
99 unrecovered_drop_events_
.erase(it2
);
103 void MockAckHandler::Drop(
104 const invalidation::ObjectId
& id
,
105 const AckHandle
& handle
) {
106 AckHandleMatcher
matcher(handle
);
107 InvalidationVector::iterator it
= std::find_if(
108 unacked_invalidations_
.begin(),
109 unacked_invalidations_
.end(),
111 if (it
!= unacked_invalidations_
.end()) {
112 dropped_invalidations_
.push_back(*it
);
113 unacked_invalidations_
.erase(it
);
115 unrecovered_drop_events_
.erase(id
);
116 unrecovered_drop_events_
.insert(std::make_pair(id
, handle
));
119 WeakHandle
<AckHandler
> MockAckHandler::WeakHandleThis() {
120 return WeakHandle
<AckHandler
>(AsWeakPtr());
123 } // namespace syncer