Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / sync / test / fake_server / fake_server.h
blobe01de194d3e6e0a5e037ea8f6729e163b5e2eea9
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_FAKE_SERVER_FAKE_SERVER_H_
6 #define SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/containers/scoped_ptr_map.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/threading/thread_checker.h"
18 #include "base/values.h"
19 #include "sync/internal_api/public/base/model_type.h"
20 #include "sync/protocol/sync.pb.h"
21 #include "sync/test/fake_server/fake_server_entity.h"
23 namespace fake_server {
25 // A fake version of the Sync server used for testing. This class is not thread
26 // safe.
27 class FakeServer {
28 public:
29 class Observer {
30 public:
31 virtual ~Observer() {}
33 // Called after FakeServer has processed a successful commit. The types
34 // updated as part of the commit are passed in |committed_model_types|.
35 virtual void OnCommit(
36 const std::string& committer_id,
37 syncer::ModelTypeSet committed_model_types) = 0;
40 FakeServer();
41 virtual ~FakeServer();
43 // Handles a /command POST (with the given |request|) to the server. Three
44 // output arguments, |error_code|, |response_code|, and |response|, are used
45 // to pass data back to the caller. The command has failed if the value
46 // pointed to by |error_code| is nonzero. |completion_closure| will be called
47 // immediately before return.
48 void HandleCommand(const std::string& request,
49 const base::Closure& completion_closure,
50 int* error_code,
51 int* response_code,
52 std::string* response);
54 // Creates a DicionaryValue representation of all entities present in the
55 // server. The dictionary keys are the strings generated by ModelTypeToString
56 // and the values are ListValues containing StringValue versions of entity
57 // names.
58 scoped_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue();
60 // Returns all entities stored by the server of the given |model_type|.
61 // This method returns SyncEntity protocol buffer objects (instead of
62 // FakeServerEntity) so that callers can inspect datatype-specific data
63 // (e.g., the URL of a session tab).
64 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType(
65 syncer::ModelType model_type);
67 // Adds |entity| to the server's collection of entities. This method makes no
68 // guarantees that the added entity will result in successful server
69 // operations.
70 void InjectEntity(scoped_ptr<FakeServerEntity> entity);
72 // Modifies the entity on the server with the given |id|. The entity's
73 // EntitySpecifics are replaced with |updated_specifics| and its version is
74 // updated. If the given |id| does not exist or the ModelType of
75 // |updated_specifics| does not match the entity, false is returned.
76 // Otherwise, true is returned to represent a successful modification.
78 // This method sometimes updates entity data beyond EntitySpecifics. For
79 // example, in the case of a bookmark, changing the BookmarkSpecifics title
80 // field will modify the top-level entity's name field.
81 bool ModifyEntitySpecifics(const std::string& id,
82 const sync_pb::EntitySpecifics& updated_specifics);
84 bool ModifyBookmarkEntity(const std::string& id,
85 const std::string& parent_id,
86 const sync_pb::EntitySpecifics& updated_specifics);
88 // Clears server data simulating a "dashboard stop and clear" and sets a new
89 // store birthday.
90 void ClearServerData();
92 // Puts the server in a state where it acts as if authentication has
93 // succeeded.
94 void SetAuthenticated();
96 // Puts the server in a state where all commands will fail with an
97 // authentication error.
98 void SetUnauthenticated();
100 // Force the server to return |error_type| in the error_code field of
101 // ClientToServerResponse on all subsequent sync requests. This method should
102 // not be called if TriggerActionableError has previously been called. Returns
103 // true if error triggering was successfully configured.
104 bool TriggerError(const sync_pb::SyncEnums::ErrorType& error_type);
106 // Force the server to return the given data as part of the error field of
107 // ClientToServerResponse on all subsequent sync requests. This method should
108 // not be called if TriggerError has previously been called. Returns true if
109 // error triggering was successfully configured.
110 bool TriggerActionableError(
111 const sync_pb::SyncEnums::ErrorType& error_type,
112 const std::string& description,
113 const std::string& url,
114 const sync_pb::SyncEnums::Action& action);
116 // Instructs the server to send triggered errors on every other request
117 // (starting with the first one after this call). This feature can be used to
118 // test the resiliency of the client when communicating with a problematic
119 // server or flaky network connection. This method should only be called
120 // after a call to TriggerError or TriggerActionableError. Returns true if
121 // triggered error alternating was successful.
122 bool EnableAlternatingTriggeredErrors();
124 // Adds |observer| to FakeServer's observer list. This should be called
125 // before the Profile associated with |observer| is connected to the server.
126 void AddObserver(Observer* observer);
128 // Removes |observer| from the FakeServer's observer list. This method
129 // must be called if AddObserver was ever called with |observer|.
130 void RemoveObserver(Observer* observer);
132 // Undoes the effects of DisableNetwork.
133 void EnableNetwork();
135 // Forces every request to fail in a way that simulates a network failure.
136 // This can be used to trigger exponential backoff in the client.
137 void DisableNetwork();
139 // Returns the entity ID of the Bookmark Bar folder.
140 std::string GetBookmarkBarFolderId() const;
142 // Returns the current FakeServer as a WeakPtr.
143 base::WeakPtr<FakeServer> AsWeakPtr();
145 private:
146 typedef base::ScopedPtrMap<std::string, scoped_ptr<FakeServerEntity>>
147 EntityMap;
149 // Processes a GetUpdates call.
150 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates,
151 sync_pb::GetUpdatesResponse* response);
153 // Processes a Commit call.
154 bool HandleCommitRequest(const sync_pb::CommitMessage& message,
155 const std::string& invalidator_client_id,
156 sync_pb::CommitResponse* response);
158 // Creates and saves a permanent folder for Bookmarks (e.g., Bookmark Bar).
159 bool CreatePermanentBookmarkFolder(const std::string& server_tag,
160 const std::string& name);
162 // Inserts the default permanent items in |entities_|.
163 bool CreateDefaultPermanentItems();
165 // Saves a |entity| to |entities_|.
166 void SaveEntity(scoped_ptr<FakeServerEntity> entity);
168 // Commits a client-side SyncEntity to the server as a FakeServerEntity.
169 // The client that sent the commit is identified via |client_guid|. The
170 // parent ID string present in |client_entity| should be ignored in favor
171 // of |parent_id|. If the commit is successful, the entity's server ID string
172 // is returned and a new FakeServerEntity is saved in |entities_|.
173 std::string CommitEntity(
174 const sync_pb::SyncEntity& client_entity,
175 sync_pb::CommitResponse_EntryResponse* entry_response,
176 const std::string& client_guid,
177 const std::string& parent_id);
179 // Populates |entry_response| based on the stored entity identified by
180 // |entity_id|. It is assumed that the entity identified by |entity_id| has
181 // already been stored using SaveEntity.
182 void BuildEntryResponseForSuccessfulCommit(
183 const std::string& entity_id,
184 sync_pb::CommitResponse_EntryResponse* entry_response);
186 // Determines whether the SyncEntity with id_string |id| is a child of an
187 // entity with id_string |potential_parent_id|.
188 bool IsChild(const std::string& id, const std::string& potential_parent_id);
190 // Creates and saves tombstones for all children of the entity with the given
191 // |id|. A tombstone is not created for the entity itself.
192 void DeleteChildren(const std::string& id);
194 // Returns whether a triggered error should be sent for the request.
195 bool ShouldSendTriggeredError() const;
197 // Updates the |entity| to a new version and increments the version counter
198 // that the server uses to assign versions.
199 void UpdateEntityVersion(FakeServerEntity* entity);
201 // Returns the store birthday.
202 std::string GetStoreBirthday() const;
204 // This is the last version number assigned to an entity. The next entity will
205 // have a version number of version_ + 1.
206 int64 version_;
208 // The current store birthday value.
209 int64 store_birthday_;
211 // Whether the server should act as if incoming connections are properly
212 // authenticated.
213 bool authenticated_;
215 // All SyncEntity objects saved by the server. The key value is the entity's
216 // id string.
217 EntityMap entities_;
219 // All Keystore keys known to the server.
220 std::vector<std::string> keystore_keys_;
222 // Used as the error_code field of ClientToServerResponse on all responses
223 // except when |triggered_actionable_error_| is set.
224 sync_pb::SyncEnums::ErrorType error_type_;
226 // Used as the error field of ClientToServerResponse when its pointer is not
227 // NULL.
228 scoped_ptr<sync_pb::ClientToServerResponse_Error> triggered_actionable_error_;
230 // These values are used in tandem to return a triggered error (either
231 // |error_type_| or |triggered_actionable_error_|) on every other request.
232 // |alternate_triggered_errors_| is set if this feature is enabled and
233 // |request_counter_| is used to send triggered errors on odd-numbered
234 // requests. Note that |request_counter_| can be reset and is not necessarily
235 // indicative of the total number of requests handled during the object's
236 // lifetime.
237 bool alternate_triggered_errors_;
238 int request_counter_;
240 // FakeServer's observers.
241 base::ObserverList<Observer, true> observers_;
243 // When true, the server operates normally. When false, a failure is returned
244 // on every request. This is used to simulate a network failure on the client.
245 bool network_enabled_;
247 // Used to verify that FakeServer is only used from one thread.
248 base::ThreadChecker thread_checker_;
250 // Creates WeakPtr versions of the current FakeServer. This must be the last
251 // data member!
252 base::WeakPtrFactory<FakeServer> weak_ptr_factory_;
255 } // namespace fake_server
257 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_