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/mock_ack_handler.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "components/invalidation/ack_handle.h"
9 #include "components/invalidation/invalidation.h"
15 struct AckHandleMatcher
{
16 AckHandleMatcher(const AckHandle
& handle
);
17 bool operator()(const syncer::Invalidation
& invalidation
) const;
19 syncer::AckHandle handle_
;
22 AckHandleMatcher::AckHandleMatcher(const AckHandle
& handle
)
25 bool AckHandleMatcher::operator()(
26 const syncer::Invalidation
& invalidation
) const {
27 return handle_
.Equals(invalidation
.ack_handle());
32 MockAckHandler::MockAckHandler() {}
34 MockAckHandler::~MockAckHandler() {}
36 void MockAckHandler::RegisterInvalidation(Invalidation
* invalidation
) {
37 unacked_invalidations_
.push_back(*invalidation
);
38 invalidation
->SetAckHandler(AsWeakPtr(), base::ThreadTaskRunnerHandle::Get());
41 void MockAckHandler::RegisterUnsentInvalidation(Invalidation
* invalidation
) {
42 unsent_invalidations_
.push_back(*invalidation
);
45 bool MockAckHandler::IsUnacked(const Invalidation
& invalidation
) const {
46 AckHandleMatcher
matcher(invalidation
.ack_handle());
47 InvalidationVector::const_iterator it
= std::find_if(
48 unacked_invalidations_
.begin(),
49 unacked_invalidations_
.end(),
51 return it
!= unacked_invalidations_
.end();
54 bool MockAckHandler::IsAcknowledged(const Invalidation
& invalidation
) const {
55 AckHandleMatcher
matcher(invalidation
.ack_handle());
56 InvalidationVector::const_iterator it
= std::find_if(
57 acked_invalidations_
.begin(),
58 acked_invalidations_
.end(),
60 return it
!= acked_invalidations_
.end();
63 bool MockAckHandler::IsDropped(const Invalidation
& invalidation
) const {
64 AckHandleMatcher
matcher(invalidation
.ack_handle());
65 InvalidationVector::const_iterator it
= std::find_if(
66 dropped_invalidations_
.begin(),
67 dropped_invalidations_
.end(),
69 return it
!= dropped_invalidations_
.end();
72 bool MockAckHandler::IsUnsent(const Invalidation
& invalidation
) const {
73 AckHandleMatcher
matcher(invalidation
.ack_handle());
74 InvalidationVector::const_iterator it1
= std::find_if(
75 unsent_invalidations_
.begin(),
76 unsent_invalidations_
.end(),
78 return it1
!= unsent_invalidations_
.end();
81 bool MockAckHandler::AllInvalidationsAccountedFor() const {
82 return unacked_invalidations_
.empty() && unrecovered_drop_events_
.empty();
85 void MockAckHandler::Acknowledge(
86 const invalidation::ObjectId
& id
,
87 const AckHandle
& handle
) {
88 AckHandleMatcher
matcher(handle
);
89 InvalidationVector::iterator it
= std::find_if(
90 unacked_invalidations_
.begin(),
91 unacked_invalidations_
.end(),
93 if (it
!= unacked_invalidations_
.end()) {
94 acked_invalidations_
.push_back(*it
);
95 unacked_invalidations_
.erase(it
);
98 IdHandleMap::iterator it2
= unrecovered_drop_events_
.find(id
);
99 if (it2
!= unrecovered_drop_events_
.end() && it2
->second
.Equals(handle
)) {
100 unrecovered_drop_events_
.erase(it2
);
104 void MockAckHandler::Drop(
105 const invalidation::ObjectId
& id
,
106 const AckHandle
& handle
) {
107 AckHandleMatcher
matcher(handle
);
108 InvalidationVector::iterator it
= std::find_if(
109 unacked_invalidations_
.begin(),
110 unacked_invalidations_
.end(),
112 if (it
!= unacked_invalidations_
.end()) {
113 dropped_invalidations_
.push_back(*it
);
114 unacked_invalidations_
.erase(it
);
116 unrecovered_drop_events_
.erase(id
);
117 unrecovered_drop_events_
.insert(std::make_pair(id
, handle
));
120 } // namespace syncer