Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / sync / test / engine / mock_model_type_sync_proxy.h
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 SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_SYNC_PROXY_H_
6 #define SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_SYNC_PROXY_H_
7
8 #include <vector>
9
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "sync/engine/model_type_sync_proxy.h"
13 #include "sync/internal_api/public/non_blocking_sync_common.h"
14
15 namespace syncer {
16
17 // Mocks the ModelTypeSyncProxy.
18 //
19 // This mock is made simpler by not using any threads.  It does still have the
20 // ability to defer execution if we need to test race conditions, though.
21 //
22 // It maintains some state to try to make its behavior more realistic.  It
23 // updates this state as it creates commit requests or receives update and
24 // commit responses.
25 //
26 // It keeps a log of all received messages so tests can make assertions based
27 // on their value.
28 class MockModelTypeSyncProxy : public ModelTypeSyncProxy {
29  public:
30   MockModelTypeSyncProxy();
31   ~MockModelTypeSyncProxy() override;
32
33   // Implementation of ModelTypeSyncProxy.
34   void OnCommitCompleted(const DataTypeState& type_state,
35                          const CommitResponseDataList& response_list) override;
36   void OnUpdateReceived(const DataTypeState& type_state,
37                         const UpdateResponseDataList& response_list,
38                         const UpdateResponseDataList& pending_updates) override;
39
40   // By default, this object behaves as if all messages are processed
41   // immediately.  Sometimes it is useful to defer work until later, as might
42   // happen in the real world if the model thread's task queue gets backed up.
43   void SetSynchronousExecution(bool is_synchronous);
44
45   // Runs any work that was deferred while this class was in asynchronous mode.
46   //
47   // This function is not useful unless this object is set to synchronous
48   // execution mode, which is the default.
49   void RunQueuedTasks();
50
51   // Generate commit or deletion requests to be sent to the server.
52   // These functions update local state to keep sequence numbers consistent.
53   //
54   // A real ModelTypeSyncProxy would forward these kinds of messages
55   // directly to its attached ModelTypeSyncWorker.  These methods
56   // return the value to the caller so the test framework can handle them as it
57   // sees fit.
58   CommitRequestData CommitRequest(const std::string& tag_hash,
59                                   const sync_pb::EntitySpecifics& specifics);
60   CommitRequestData DeleteRequest(const std::string& tag_hash);
61
62   // Getters to access the log of received update responses.
63   //
64   // Does not includes repsonses that are in pending tasks.
65   size_t GetNumUpdateResponses() const;
66   UpdateResponseDataList GetNthUpdateResponse(size_t n) const;
67   UpdateResponseDataList GetNthPendingUpdates(size_t n) const;
68   DataTypeState GetNthTypeStateReceivedInUpdateResponse(size_t n) const;
69
70   // Getters to access the log of received commit responses.
71   //
72   // Does not includes repsonses that are in pending tasks.
73   size_t GetNumCommitResponses() const;
74   CommitResponseDataList GetNthCommitResponse(size_t n) const;
75   DataTypeState GetNthTypeStateReceivedInCommitResponse(size_t n) const;
76
77   // Getters to access the lastest update response for a given tag_hash.
78   bool HasUpdateResponse(const std::string& tag_hash) const;
79   UpdateResponseData GetUpdateResponse(const std::string& tag_hash) const;
80
81   // Getters to access the lastest commit response for a given tag_hash.
82   bool HasCommitResponse(const std::string& tag_hash) const;
83   CommitResponseData GetCommitResponse(const std::string& tag_hash) const;
84
85  private:
86   // Process a received commit response.
87   //
88   // Implemented as an Impl method so we can defer its execution in some cases.
89   void OnCommitCompletedImpl(const DataTypeState& type_state,
90                              const CommitResponseDataList& response_list);
91
92   // Process a received update response.
93   //
94   // Implemented as an Impl method so we can defer its execution in some cases.
95   void OnUpdateReceivedImpl(const DataTypeState& type_state,
96                             const UpdateResponseDataList& response_list,
97                             const UpdateResponseDataList& pending_updates);
98
99   // Getter and setter for per-item sequence number tracking.
100   int64 GetCurrentSequenceNumber(const std::string& tag_hash) const;
101   int64 GetNextSequenceNumber(const std::string& tag_hash);
102
103   // Getter and setter for per-item base version tracking.
104   int64 GetBaseVersion(const std::string& tag_hash) const;
105   void SetBaseVersion(const std::string& tag_hash, int64 version);
106
107   // Getters and setter for server-assigned ID values.
108   bool HasServerAssignedId(const std::string& tag_hash) const;
109   const std::string& GetServerAssignedId(const std::string& tag_hash) const;
110   void SetServerAssignedId(const std::string& tag_hash, const std::string& id);
111
112   // State related to the implementation of deferred work.
113   // See SetSynchronousExecution() for details.
114   bool is_synchronous_;
115   std::vector<base::Closure> pending_tasks_;
116
117   // A log of messages received by this object.
118   std::vector<CommitResponseDataList> received_commit_responses_;
119   std::vector<UpdateResponseDataList> received_update_responses_;
120   std::vector<UpdateResponseDataList> received_pending_updates_;
121   std::vector<DataTypeState> type_states_received_on_update_;
122   std::vector<DataTypeState> type_states_received_on_commit_;
123
124   // Latest responses received, indexed by tag_hash.
125   std::map<const std::string, CommitResponseData> commit_response_items_;
126   std::map<const std::string, UpdateResponseData> update_response_items_;
127
128   // The per-item state maps.
129   std::map<const std::string, int64> sequence_numbers_;
130   std::map<const std::string, int64> base_versions_;
131   std::map<const std::string, std::string> assigned_ids_;
132
133   DISALLOW_COPY_AND_ASSIGN(MockModelTypeSyncProxy);
134 };
135
136 }  // namespace syncer
137
138 #endif  // SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_SYNC_PROXY_H_