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_SINGLE_TYPE_MOCK_SERVER_H_
6#define SYNC_TEST_ENGINE_SINGLE_TYPE_MOCK_SERVER_H_
7
8#include "sync/internal_api/public/base/model_type.h"
9#include "sync/internal_api/public/non_blocking_sync_common.h"
10
11namespace syncer {
12
13// A mock server used to test of happy-path update and commit logic.
14//
15// This object supports only one ModelType, which must be specified at
16// initialization time.  It does not support GetUpdates messages.  It does not
17// support simulated errors.
18//
19// This class is useful for testing UpdateHandlers and CommitContributors.
20class SingleTypeMockServer {
21 public:
22  explicit SingleTypeMockServer(syncer::ModelType type);
23  ~SingleTypeMockServer();
24
25  // Generates a SyncEntity representing a server-delivered update containing
26  // the root node for this SingleTypeMockServer's type.
27  sync_pb::SyncEntity TypeRootUpdate();
28
29  // Generates a SyncEntity representing a server-delivered update.
30  //
31  // The |version_offset| parameter allows the caller to simulate reflected
32  // updates, redeliveries, and genuine updates.
33  sync_pb::SyncEntity UpdateFromServer(
34      int64 version_offset,
35      const std::string& tag_hash,
36      const sync_pb::EntitySpecifics& specifics);
37
38  // Generates a SyncEntity representing a server-delivered update to delete
39  // an item.
40  sync_pb::SyncEntity TombstoneFromServer(int64 version_offset,
41                                          const std::string& tag_hash);
42
43  // Generates a response to the specified commit message.
44  //
45  // This does not perform any exhausive testing of the sync protocol.  Many of
46  // the request's fields may safely be left blank, and much of the returned
47  // response will be empty, too.
48  //
49  // This is useful mainly for testing objects that implement the
50  // CommitContributor interface.
51  sync_pb::ClientToServerResponse DoSuccessfulCommit(
52      const sync_pb::ClientToServerMessage& message);
53
54  // Getters to return the commit messages sent to the server through
55  // DoSuccessfulCommit().
56  size_t GetNumCommitMessages() const;
57  sync_pb::ClientToServerMessage GetNthCommitMessage(size_t n) const;
58
59  // Getters to return the most recently committed entities for a given
60  // unique_client_tag hash.
61  bool HasCommitEntity(const std::string& tag_hash) const;
62  sync_pb::SyncEntity GetLastCommittedEntity(const std::string& tag_hash) const;
63
64  // Getters that create realistic-looking progress markers and data type
65  // context.
66  sync_pb::DataTypeProgressMarker GetProgress() const;
67  sync_pb::DataTypeContext GetContext() const;
68
69 private:
70  static std::string GenerateId(const std::string& tag_hash);
71
72  // Get and set our emulated server state.
73  int64 GetServerVersion(const std::string& tag_hash) const;
74  void SetServerVersion(const std::string& tag_hash, int64 version);
75
76  const ModelType type_;
77  const std::string type_root_id_;
78
79  // Server version state maps.
80  std::map<const std::string, int64> server_versions_;
81
82  // Log of messages sent to the server.
83  std::vector<sync_pb::ClientToServerMessage> commit_messages_;
84
85  // Map of most recent commits by tag_hash.
86  std::map<const std::string, sync_pb::SyncEntity> committed_items_;
87
88  DISALLOW_COPY_AND_ASSIGN(SingleTypeMockServer);
89};
90
91}  // namespace syncer
92
93#endif  // SYNC_TEST_ENGINE_SINGLE_TYPE_MOCK_SERVER_H_
94