Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / sync / test / engine / mock_model_type_sync_worker.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_WORKER_H_
6 #define SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_SYNC_WORKER_H_
7
8 #include <vector>
9
10 #include "base/macros.h"
11 #include "sync/engine/model_type_sync_worker.h"
12 #include "sync/internal_api/public/non_blocking_sync_common.h"
13
14 namespace syncer {
15
16 // Receives and records commit requests sent through the ModelTypeSyncWorker.
17 //
18 // This class also includes features intended to help mock out server behavior.
19 // It has some basic functionality to keep track of server state and generate
20 // plausible UpdateResponseData and CommitResponseData messages.
21 class MockModelTypeSyncWorker : public ModelTypeSyncWorker {
22  public:
23   MockModelTypeSyncWorker();
24   virtual ~MockModelTypeSyncWorker();
25
26   // Implementation of ModelTypeSyncWorker.
27   virtual void EnqueueForCommit(const CommitRequestDataList& list) OVERRIDE;
28
29   // Getters to inspect the requests sent to this object.
30   size_t GetNumCommitRequestLists() const;
31   CommitRequestDataList GetNthCommitRequestList(size_t n) const;
32   bool HasCommitRequestForTagHash(const std::string& tag_hash) const;
33   CommitRequestData GetLatestCommitRequestForTagHash(
34       const std::string& tag_hash) const;
35
36   // Functions to produce state as though it came from a real server and had
37   // been filtered through a real ModelTypeSyncWorker.
38
39   // Returns an UpdateResponseData representing an update received from
40   // the server.  Updates server state accordingly.
41   //
42   // The |version_offset| field can be used to emulate stale data (ie. versions
43   // going backwards), reflections and redeliveries (ie. several instances of
44   // the same version) or new updates.
45   UpdateResponseData UpdateFromServer(
46       int64 version_offset,
47       const std::string& tag_hash,
48       const sync_pb::EntitySpecifics& specifics);
49
50   // Returns an UpdateResponseData representing a tombstone update from the
51   // server.  Updates server state accordingly.
52   UpdateResponseData TombstoneFromServer(int64 version_offset,
53                                          const std::string& tag_hash);
54
55   // Returns a commit response that indicates a successful commit of the
56   // given |request_data|.  Updates server state accordingly.
57   CommitResponseData SuccessfulCommitResponse(
58       const CommitRequestData& request_data);
59
60   // Sets the encryption key name used for updates from the server.
61   // (ie. the key other clients are using to encrypt their commits.)
62   // The default value is an empty string, which indicates no encryption.
63   void SetServerEncryptionKey(const std::string& key_name);
64
65  private:
66   // Generate an ID string.
67   static std::string GenerateId(const std::string& tag_hash);
68
69   // Retrieve or set the server version.
70   int64 GetServerVersion(const std::string& tag_hash);
71   void SetServerVersion(const std::string& tag_hash, int64 version);
72
73   // A record of past commits requests.
74   std::vector<CommitRequestDataList> commit_request_lists_;
75
76   // Map of versions by client tag.
77   // This is an essential part of the mocked server state.
78   std::map<const std::string, int64> server_versions_;
79
80   // Name of the encryption key in use on other clients.
81   std::string server_encryption_key_name_;
82
83   DISALLOW_COPY_AND_ASSIGN(MockModelTypeSyncWorker);
84 };
85
86 }  // namespace syncer
87
88 #endif  // SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_SYNC_WORKER_H_