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.
4
5#ifndef COMPONENTS_INVALIDATION_MOCK_ACK_HANDLER_H_
6#define COMPONENTS_INVALIDATION_MOCK_ACK_HANDLER_H_
7
8#include <map>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "base/memory/weak_ptr.h"
13#include "components/invalidation/ack_handler.h"
14#include "components/invalidation/invalidation_export.h"
15#include "components/invalidation/invalidation_util.h"
16
17namespace syncer {
18
19class Invalidation;
20
21// This AckHandler implementation colaborates with the FakeInvalidationService
22// to enable unit tests to assert that invalidations are being acked properly.
23class INVALIDATION_EXPORT MockAckHandler
24  : public AckHandler,
25    public base::SupportsWeakPtr<MockAckHandler> {
26 public:
27  MockAckHandler();
28  virtual ~MockAckHandler();
29
30  // Sets up some internal state to track this invalidation, and modifies it so
31  // that its Acknowledge() and Drop() methods will route back to us.
32  void RegisterInvalidation(Invalidation* invalidation);
33
34  // No one was listening for this invalidation, so no one will receive it or
35  // ack it.  We keep track of it anyway to let tests make assertions about it.
36  void RegisterUnsentInvalidation(Invalidation* invalidation);
37
38  // Returns true if the specified invalidaition has been delivered, but has not
39  // been acknowledged yet.
40  bool IsUnacked(const Invalidation& invalidation) const;
41
42  // Returns true if the specified invalidation has been delivered and
43  // acknowledged.
44  bool IsAcknowledged(const Invalidation& invalidation) const;
45
46  // Returns true if the specified invalidation has been delivered and
47  // dropped.
48  bool IsDropped(const Invalidation& invalidation) const;
49
50  // Returns true if the specified invalidation was never delivered.
51  bool IsUnsent(const Invalidation& invalidation) const;
52
53  // Retruns true if all invalidations have been acked and all drops recovered.
54  bool AllInvalidationsAccountedFor() const;
55
56  // Implementation of AckHandler.
57  virtual void Acknowledge(
58      const invalidation::ObjectId& id,
59      const AckHandle& handle) OVERRIDE;
60  virtual void Drop(
61      const invalidation::ObjectId& id,
62      const AckHandle& handle) OVERRIDE;
63
64 private:
65  typedef std::vector<syncer::Invalidation> InvalidationVector;
66  typedef std::map<invalidation::ObjectId,
67                   AckHandle,
68                   ObjectIdLessThan> IdHandleMap;
69
70  InvalidationVector unsent_invalidations_;
71  InvalidationVector unacked_invalidations_;
72  InvalidationVector acked_invalidations_;
73  InvalidationVector dropped_invalidations_;
74
75  IdHandleMap unrecovered_drop_events_;
76};
77
78}  // namespace syncer
79
80#endif  // COMPONENTS_INVALIDATION_MOCK_ACK_HANDLER_H_
81