Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / sync / test / engine / mock_commit_queue.h
blob25dd8f7b57c5d9a0991a4c61552a4cf759cbe0e9
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 #ifndef SYNC_TEST_ENGINE_MOCK_COMMIT_QUEUE_H_
6 #define SYNC_TEST_ENGINE_MOCK_COMMIT_QUEUE_H_
8 #include <vector>
10 #include "base/macros.h"
11 #include "sync/engine/commit_queue.h"
12 #include "sync/internal_api/public/non_blocking_sync_common.h"
14 namespace syncer_v2 {
16 // Receives and records commit requests sent through the CommitQueue.
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 MockCommitQueue : public CommitQueue {
22 public:
23 MockCommitQueue();
24 ~MockCommitQueue() override;
26 // Implementation of CommitQueue.
27 void EnqueueForCommit(const CommitRequestDataList& list) override;
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;
36 // Functions to produce state as though it came from a real server and had
37 // been filtered through a real CommitQueue.
39 // Returns an UpdateResponseData representing an update received from
40 // the server. Updates server state accordingly.
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);
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);
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);
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);
65 private:
66 // Generate an ID string.
67 static std::string GenerateId(const std::string& tag_hash);
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);
73 // A record of past commits requests.
74 std::vector<CommitRequestDataList> commit_request_lists_;
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_;
80 // Name of the encryption key in use on other clients.
81 std::string server_encryption_key_name_;
83 DISALLOW_COPY_AND_ASSIGN(MockCommitQueue);
86 } // namespace syncer
88 #endif // SYNC_TEST_ENGINE_MOCK_COMMIT_QUEUE_H_