#ifndef COMPONENTS_SYNC_TEST_MOCK_CONNECTION_MANAGER_H_
#define COMPONENTS_SYNC_TEST_MOCK_CONNECTION_MANAGER_H_
#include <stdint.h>
#include <bitset>
#include <list>
#include <memory>
#include <string>
#include <vector>
#include "base/compiler_specific.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/synchronization/lock.h"
#include "components/sync/base/model_type.h"
#include "components/sync/base/unique_position.h"
#include "components/sync/engine/net/server_connection_manager.h"
#include "third_party/protobuf/src/google/protobuf/repeated_field.h"
namespace sync_pb {
class EntitySpecifics;
class SyncEntity;
class ClientCommand;
class CommitMessage;
class CommitResponse;
class ClientToServerMessage;
class ClientToServerResponse;
class DataTypeProgressMarker;
class GetUpdatesResponse;
class ClientCommand;
}
namespace syncer {
class MockConnectionManager : public ServerConnectionManager {
public:
class MidCommitObserver {
public:
virtual void Observe() = 0;
protected:
virtual ~MidCommitObserver() {}
};
MockConnectionManager();
MockConnectionManager(const MockConnectionManager&) = delete;
MockConnectionManager& operator=(const MockConnectionManager&) = delete;
~MockConnectionManager() override;
HttpResponse PostBuffer(const std::string& buffer_in,
const std::string& access_token,
bool allow_batching,
std::string* buffer_out) override;
void SetMidCommitCallback(base::OnceClosure callback);
void SetMidCommitObserver(MidCommitObserver* observer);
sync_pb::SyncEntity* AddUpdateDirectory(
const std::string& id,
const std::string& parent_id,
const std::string& name,
int64_t version,
int64_t sync_ts,
const std::string& originator_cache_guid,
const std::string& originator_client_item_id);
sync_pb::SyncEntity* AddUpdateBookmark(
const std::string& id,
const std::string& parent_id,
const std::string& name,
int64_t version,
int64_t sync_ts,
const std::string& originator_cache_guid,
const std::string& originator_client_item_id);
sync_pb::SyncEntity* AddUpdateSpecifics(
const std::string& id,
const std::string& parent_id,
const std::string& name,
int64_t version,
int64_t sync_ts,
bool is_dir,
const sync_pb::EntitySpecifics& specifics);
sync_pb::SyncEntity* AddUpdateSpecifics(
const std::string& id,
const std::string& parent_id,
const std::string& name,
int64_t version,
int64_t sync_ts,
bool is_dir,
const sync_pb::EntitySpecifics& specifics,
const std::string& originator_cache_guid,
const std::string& originator_client_item_id);
sync_pb::SyncEntity* SetNigori(const std::string& id,
int64_t version,
int64_t sync_ts,
const sync_pb::EntitySpecifics& specifics);
sync_pb::SyncEntity* AddUpdatePref(const std::string& id,
const std::string& parent_id,
const std::string& client_tag,
int64_t version,
int64_t sync_ts);
sync_pb::SyncEntity* AddUpdateFromLastCommit();
void AddUpdateTombstone(const std::string& id, ModelType type);
void SetLastUpdateDeleted();
void SetLastUpdateServerTag(const std::string& tag);
void SetLastUpdateClientTag(const std::string& tag);
void SetLastUpdateOriginatorFields(const std::string& client_id,
const std::string& entry_id);
void SetNewTimestamp(int ts);
void SetChangesRemaining(int64_t count);
void NextUpdateBatch();
void FailNextPostBufferToPathCall() { countdown_to_postbuffer_fail_ = 1; }
void FailNthPostBufferToPathCall(int n) { countdown_to_postbuffer_fail_ = n; }
void SetKeystoreKey(const std::string& key);
void FailNonPeriodicGetUpdates() { fail_non_periodic_get_updates_ = true; }
bool client_stuck() const { return client_stuck_; }
void SetGUClientCommand(std::unique_ptr<sync_pb::ClientCommand> command);
void SetCommitClientCommand(std::unique_ptr<sync_pb::ClientCommand> command);
void SetTransientErrorId(const std::string&);
const std::vector<std::string>& committed_ids() const {
return committed_ids_;
}
const std::vector<std::unique_ptr<sync_pb::CommitMessage>>& commit_messages()
const {
return commit_messages_;
}
const std::vector<std::unique_ptr<sync_pb::CommitResponse>>&
commit_responses() const {
return commit_responses_;
}
const sync_pb::CommitMessage& last_sent_commit() const;
const sync_pb::CommitResponse& last_commit_response() const;
const sync_pb::ClientToServerMessage& last_request() const;
const std::vector<sync_pb::ClientToServerMessage>& requests() const;
void set_conflict_all_commits(bool value) { conflict_all_commits_ = value; }
void set_next_new_id(int value) { next_new_id_ = value; }
void set_conflict_n_commits(int value) { conflict_n_commits_ = value; }
void set_store_birthday(const std::string& new_birthday) {
base::AutoLock lock(store_birthday_lock_);
store_birthday_ = new_birthday;
}
void set_throttling(bool value) { throttling_ = value; }
void set_partial_failure(bool value) { partial_failure_ = value; }
int GetAndClearNumGetUpdatesRequests() {
int result = num_get_updates_requests_;
num_get_updates_requests_ = 0;
return result;
}
void ExpectGetUpdatesRequestTypes(ModelTypeSet expected_filter) {
expected_filter_ = expected_filter;
}
void SetPartialFailureTypes(ModelTypeSet types) {
partial_failure_type_ = types;
}
void SetServerReachable();
void SetServerNotReachable();
void UpdateConnectionStatus();
using ServerConnectionManager::SetServerResponse;
const std::string store_birthday() {
base::AutoLock lock(store_birthday_lock_);
return store_birthday_;
}
void ClearUpdatesQueue() { update_queue_.clear(); }
sync_pb::SyncEntity* GetMutableLastUpdate();
sync_pb::DataTypeProgressMarker* AddUpdateProgressMarker();
void ResetAccessToken() { ClearAccessToken(); }
private:
sync_pb::SyncEntity* AddUpdateFull(const std::string& id,
const std::string& parentid,
const std::string& name,
int64_t version,
int64_t sync_ts,
bool is_dir);
sync_pb::SyncEntity* AddUpdateMeta(const std::string& id,
const std::string& parentid,
const std::string& name,
int64_t version,
int64_t sync_ts);
bool ProcessGetUpdates(sync_pb::ClientToServerMessage* csm,
sync_pb::ClientToServerResponse* response);
bool ProcessCommit(sync_pb::ClientToServerMessage* csm,
sync_pb::ClientToServerResponse* response_buffer);
bool ProcessClearServerData(sync_pb::ClientToServerMessage* csm,
sync_pb::ClientToServerResponse* response);
void AddDefaultBookmarkData(sync_pb::SyncEntity* entity, bool is_folder);
bool ShouldConflictThisCommit();
bool ShouldTransientErrorThisId(const std::string& id);
sync_pb::GetUpdatesResponse* GetUpdateResponse();
void ApplyToken();
bool IsModelTypePresentInSpecifics(
const google::protobuf::RepeatedPtrField<sync_pb::DataTypeProgressMarker>&
filter,
ModelType value);
sync_pb::DataTypeProgressMarker const* GetProgressMarkerForType(
const google::protobuf::RepeatedPtrField<sync_pb::DataTypeProgressMarker>&
filter,
ModelType value);
bool server_reachable_;
std::vector<std::string> committed_ids_;
std::vector<std::string> transient_error_ids_;
bool conflict_all_commits_;
int conflict_n_commits_;
std::vector<std::unique_ptr<sync_pb::CommitMessage>> commit_messages_;
std::vector<std::unique_ptr<sync_pb::CommitResponse>> commit_responses_;
int next_new_id_;
std::string store_birthday_;
base::Lock store_birthday_lock_;
bool store_birthday_sent_;
bool client_stuck_;
int countdown_to_postbuffer_fail_;
std::list<sync_pb::GetUpdatesResponse> update_queue_;
base::OnceClosure mid_commit_callback_;
raw_ptr<MidCommitObserver> mid_commit_observer_;
std::string keystore_key_;
bool throttling_;
bool partial_failure_;
base::Lock response_code_override_lock_;
bool fail_non_periodic_get_updates_;
std::unique_ptr<sync_pb::ClientCommand> gu_client_command_;
std::unique_ptr<sync_pb::ClientCommand> commit_client_command_;
ModelTypeSet expected_filter_;
ModelTypeSet partial_failure_type_;
int num_get_updates_requests_;
std::string next_token_;
std::vector<sync_pb::ClientToServerMessage> requests_;
};
}
#endif