OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 // Mock ServerConnectionManager class for use in client unit tests. |
| 6 // |
| 7 |
| 8 #ifndef CHROME_TEST_SYNC_ENGINE_MOCK_SERVER_CONNECTION_H_ |
| 9 #define CHROME_TEST_SYNC_ENGINE_MOCK_SERVER_CONNECTION_H_ |
| 10 |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "chrome/browser/sync/engine/net/server_connection_manager.h" |
| 15 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 16 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 17 |
| 18 using std::string; |
| 19 using std::vector; |
| 20 |
| 21 namespace syncable { |
| 22 class DirectoryManager; |
| 23 class ScopedDirLookup; |
| 24 } |
| 25 namespace browser_sync { |
| 26 struct HttpResponse; |
| 27 } |
| 28 |
| 29 class MockConnectionManager |
| 30 : public browser_sync::ServerConnectionManager { |
| 31 public: |
| 32 // A callback function type. These can be set to be called when server |
| 33 // activity would normally take place. This aids simulation of race |
| 34 // conditions. |
| 35 typedef bool (*TestCallbackFunction)(syncable::Directory* dir); |
| 36 |
| 37 MockConnectionManager(syncable::DirectoryManager* dirmgr, PathString name); |
| 38 virtual ~MockConnectionManager(); |
| 39 |
| 40 // Overridden ServerConnectionManager functions. |
| 41 virtual bool PostBufferToPath(const PostBufferParams*, |
| 42 const string& path, |
| 43 const string& auth_token); |
| 44 |
| 45 virtual bool IsServerReachable(); |
| 46 virtual bool IsUserAuthenticated(); |
| 47 |
| 48 // Control of commit response. |
| 49 void SetMidCommitCallbackFunction(TestCallbackFunction callback); |
| 50 |
| 51 // Set this if you want commit to perform commit time rename. |
| 52 // Will request that the client renames all commited entries, |
| 53 // prepending this string. |
| 54 void SetCommitTimeRename(string prepend); |
| 55 |
| 56 // Control of get updates response. All updates set will only be returned |
| 57 // once. This mock object doesn't simulate a changelist, it simulates server |
| 58 // responses. |
| 59 void ResetUpdates(); |
| 60 // Generic versions of AddUpdate functions. Tests using these function should |
| 61 // compile for both the int64 and string id based versions of the server. |
| 62 // The SyncEntity returned is only valid until the Sync is completed |
| 63 // (e.g. with SyncShare.) It allows to add further entity properties before |
| 64 // sync, using AddUpdateExtendedAttributes. |
| 65 sync_pb::SyncEntity* AddUpdateDirectory(syncable::Id id, |
| 66 syncable::Id parent_id, |
| 67 string name, |
| 68 int64 version, |
| 69 int64 sync_ts); |
| 70 sync_pb::SyncEntity* AddUpdateBookmark(syncable::Id id, |
| 71 syncable::Id parent_id, |
| 72 string name, |
| 73 int64 version, |
| 74 int64 sync_ts); |
| 75 // Versions of the AddUpdate functions that accept integer IDs. |
| 76 sync_pb::SyncEntity* AddUpdateDirectory(int id, |
| 77 int parent_id, |
| 78 string name, |
| 79 int64 version, |
| 80 int64 sync_ts); |
| 81 sync_pb::SyncEntity* AddUpdateBookmark(int id, |
| 82 int parent_id, |
| 83 string name, |
| 84 int64 version, |
| 85 int64 sync_ts); |
| 86 // New protocol versions of the AddUpdate functions. |
| 87 sync_pb::SyncEntity* AddUpdateDirectory(string id, |
| 88 string parent_id, |
| 89 string name, |
| 90 int64 version, |
| 91 int64 sync_ts); |
| 92 sync_pb::SyncEntity* AddUpdateBookmark(string id, |
| 93 string parent_id, |
| 94 string name, |
| 95 int64 version, |
| 96 int64 sync_ts); |
| 97 void AddUpdateExtendedAttributes(sync_pb::SyncEntity* ent, |
| 98 PathString* xattr_key, |
| 99 syncable::Blob* xattr_value, |
| 100 int xattr_count); |
| 101 // Prepare to add checksums |
| 102 void SetLastUpdateDeleted(); |
| 103 void SetLastUpdateSingletonTag(const string& tag); |
| 104 void SetLastUpdateOriginatorFields(const string& client_id, |
| 105 const string& entry_id); |
| 106 void SetLastUpdatePosition(int64 position_in_parent); |
| 107 void SetNewTimestamp(int64 ts); |
| 108 void SetNewestTimestamp(int64 ts); |
| 109 |
| 110 void FailNextPostBufferToPathCall() { fail_next_postbuffer_ = true; } |
| 111 |
| 112 // Simple inspectors |
| 113 bool client_stuck() const { return client_stuck_; } |
| 114 |
| 115 sync_pb::ClientCommand* GetNextClientCommand(); |
| 116 |
| 117 const vector<syncable::Id>& committed_ids() const { return committed_ids_; } |
| 118 const vector<sync_pb::CommitMessage*>& commit_messages() const { |
| 119 return commit_messages_; |
| 120 } |
| 121 // Retrieve the last sent commit message. |
| 122 const sync_pb::CommitMessage& last_sent_commit() const; |
| 123 |
| 124 void set_conflict_all_commits(bool value) { |
| 125 conflict_all_commits_ = value; |
| 126 } |
| 127 void set_next_new_id(int value) { |
| 128 next_new_id_ = value; |
| 129 } |
| 130 void set_conflict_n_commits(int value) { |
| 131 conflict_n_commits_ = value; |
| 132 } |
| 133 |
| 134 private: |
| 135 sync_pb::SyncEntity* AddUpdateFull(syncable::Id id, syncable::Id parentid, |
| 136 string name, int64 version, |
| 137 int64 sync_ts, |
| 138 bool is_dir); |
| 139 sync_pb::SyncEntity* AddUpdateFull(string id, string parentid, string name, |
| 140 int64 version, int64 sync_ts, |
| 141 bool is_dir); |
| 142 // Functions to handle the various types of server request. |
| 143 void ProcessGetUpdates(sync_pb::ClientToServerMessage* csm, |
| 144 sync_pb::ClientToServerResponse* response); |
| 145 void ProcessCommit(sync_pb::ClientToServerMessage* csm, |
| 146 sync_pb::ClientToServerResponse* response_buffer); |
| 147 // Locate the most recent update message for purpose of alteration. |
| 148 sync_pb::SyncEntity* GetMutableLastUpdate(); |
| 149 |
| 150 // Determine if one entry in a commit should be rejected with a conflict. |
| 151 bool ShouldConflictThisCommit(); |
| 152 |
| 153 // Generate a numeric position_in_parent value. We use a global |
| 154 // counter that only decreases; this simulates new objects always |
| 155 // being added to the front of the ordering. |
| 156 int64 GeneratePositionInParent() { |
| 157 return next_position_in_parent_--; |
| 158 } |
| 159 |
| 160 // All IDs that have been committed. |
| 161 vector<syncable::Id> committed_ids_; |
| 162 |
| 163 // Control of when/if we return conflicts. |
| 164 bool conflict_all_commits_; |
| 165 int conflict_n_commits_; |
| 166 |
| 167 // Commit messages we've sent |
| 168 vector<sync_pb::CommitMessage*> commit_messages_; |
| 169 |
| 170 // The next id the mock will return to a commit. |
| 171 int next_new_id_; |
| 172 |
| 173 // The store birthday we send to the client. |
| 174 string store_birthday_; |
| 175 bool store_birthday_sent_; |
| 176 bool client_stuck_; |
| 177 string commit_time_rename_prepended_string_; |
| 178 |
| 179 // fail on the next call to PostBufferToPath() |
| 180 bool fail_next_postbuffer_; |
| 181 |
| 182 // Our directory. |
| 183 syncable::ScopedDirLookup directory_; |
| 184 |
| 185 // The updates we'll return to the next request. |
| 186 sync_pb::GetUpdatesResponse updates_; |
| 187 TestCallbackFunction mid_commit_callback_function_; |
| 188 |
| 189 scoped_ptr<sync_pb::ClientCommand> client_command_; |
| 190 |
| 191 // The next value to use for the position_in_parent property. |
| 192 int64 next_position_in_parent_; |
| 193 |
| 194 DISALLOW_COPY_AND_ASSIGN(MockConnectionManager); |
| 195 }; |
| 196 |
| 197 #endif // CHROME_TEST_SYNC_ENGINE_MOCK_SERVER_CONNECTION_H_ |
OLD | NEW |