1 // Copyright 2012 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 // Mock ServerConnectionManager class for use in client regression tests.
7 #include "sync/test/engine/mock_connection_manager.h"
11 #include "base/location.h"
12 #include "base/strings/stringprintf.h"
13 #include "sync/engine/syncer_proto_util.h"
14 #include "sync/protocol/bookmark_specifics.pb.h"
15 #include "sync/syncable/directory.h"
16 #include "sync/syncable/syncable_write_transaction.h"
17 #include "sync/test/engine/test_id_factory.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 using sync_pb::ClientToServerMessage
;
24 using sync_pb::CommitMessage
;
25 using sync_pb::CommitResponse
;
26 using sync_pb::GetUpdatesMessage
;
27 using sync_pb::SyncEnums
;
31 using syncable::WriteTransaction
;
33 static char kValidAuthToken
[] = "AuthToken";
34 static char kCacheGuid
[] = "kqyg7097kro6GSUod+GSg==";
36 MockConnectionManager::MockConnectionManager(syncable::Directory
* directory
,
37 CancelationSignal
* signal
)
38 : ServerConnectionManager("unused", 0, false, signal
),
39 server_reachable_(true),
40 conflict_all_commits_(false),
41 conflict_n_commits_(0),
43 store_birthday_("Store BDay!"),
44 store_birthday_sent_(false),
46 countdown_to_postbuffer_fail_(0),
47 directory_(directory
),
48 mid_commit_observer_(NULL
),
50 partialThrottling_(false),
51 fail_with_auth_invalid_(false),
52 fail_non_periodic_get_updates_(false),
53 next_position_in_parent_(2),
54 use_legacy_bookmarks_protocol_(false),
55 num_get_updates_requests_(0) {
57 SetAuthToken(kValidAuthToken
);
60 MockConnectionManager::~MockConnectionManager() {
61 EXPECT_TRUE(update_queue_
.empty()) << "Unfetched updates.";
64 void MockConnectionManager::SetCommitTimeRename(string prepend
) {
65 commit_time_rename_prepended_string_
= prepend
;
68 void MockConnectionManager::SetMidCommitCallback(
69 const base::Closure
& callback
) {
70 mid_commit_callback_
= callback
;
73 void MockConnectionManager::SetMidCommitObserver(
74 MockConnectionManager::MidCommitObserver
* observer
) {
75 mid_commit_observer_
= observer
;
78 bool MockConnectionManager::PostBufferToPath(PostBufferParams
* params
,
80 const string
& auth_token
,
81 ScopedServerStatusWatcher
* watcher
) {
82 ClientToServerMessage post
;
83 CHECK(post
.ParseFromString(params
->buffer_in
));
84 CHECK(post
.has_protocol_version());
85 CHECK(post
.has_api_key());
86 CHECK(post
.has_bag_of_chips());
88 requests_
.push_back(post
);
89 client_stuck_
= post
.sync_problem_detected();
90 sync_pb::ClientToServerResponse response
;
94 // If the Directory's locked when we do this, it's a problem as in normal
95 // use this function could take a while to return because it accesses the
96 // network. As we can't test this we do the next best thing and hang here
97 // when there's an issue.
98 CHECK(directory_
->good());
99 WriteTransaction
wt(FROM_HERE
, syncable::UNITTEST
, directory_
);
102 if (auth_token
.empty()) {
103 params
->response
.server_status
= HttpResponse::SYNC_AUTH_ERROR
;
107 if (auth_token
!= kValidAuthToken
) {
108 // Simulate server-side auth failure.
109 params
->response
.server_status
= HttpResponse::SYNC_AUTH_ERROR
;
110 InvalidateAndClearAuthToken();
113 if (--countdown_to_postbuffer_fail_
== 0) {
114 // Fail as countdown hits zero.
115 params
->response
.server_status
= HttpResponse::SYNC_SERVER_ERROR
;
119 if (!server_reachable_
) {
120 params
->response
.server_status
= HttpResponse::CONNECTION_UNAVAILABLE
;
124 // Default to an ok connection.
125 params
->response
.server_status
= HttpResponse::SERVER_CONNECTION_OK
;
126 response
.set_error_code(SyncEnums::SUCCESS
);
127 const string current_store_birthday
= store_birthday();
128 response
.set_store_birthday(current_store_birthday
);
129 if (post
.has_store_birthday() && post
.store_birthday() !=
130 current_store_birthday
) {
131 response
.set_error_code(SyncEnums::NOT_MY_BIRTHDAY
);
132 response
.set_error_message("Merry Unbirthday!");
133 response
.SerializeToString(¶ms
->buffer_out
);
134 store_birthday_sent_
= true;
138 EXPECT_TRUE(!store_birthday_sent_
|| post
.has_store_birthday() ||
139 post
.message_contents() == ClientToServerMessage::AUTHENTICATE
||
140 post
.message_contents() ==
141 ClientToServerMessage::CLEAR_SERVER_DATA
);
142 store_birthday_sent_
= true;
144 if (post
.message_contents() == ClientToServerMessage::COMMIT
) {
145 ProcessCommit(&post
, &response
);
146 } else if (post
.message_contents() == ClientToServerMessage::GET_UPDATES
) {
147 ProcessGetUpdates(&post
, &response
);
148 } else if (post
.message_contents() ==
149 ClientToServerMessage::CLEAR_SERVER_DATA
) {
150 ProcessClearServerData(&post
, &response
);
152 EXPECT_TRUE(false) << "Unknown/unsupported ClientToServerMessage";
157 base::AutoLock
lock(response_code_override_lock_
);
159 response
.set_error_code(SyncEnums::THROTTLED
);
163 if (partialThrottling_
) {
164 sync_pb::ClientToServerResponse_Error
* response_error
=
165 response
.mutable_error();
166 response_error
->set_error_type(SyncEnums::PARTIAL_FAILURE
);
167 for (ModelTypeSet::Iterator it
= throttled_type_
.First(); it
.Good();
169 response_error
->add_error_data_type_ids(
170 GetSpecificsFieldNumberFromModelType(it
.Get()));
172 partialThrottling_
= false;
175 if (fail_with_auth_invalid_
)
176 response
.set_error_code(SyncEnums::AUTH_INVALID
);
179 response
.SerializeToString(¶ms
->buffer_out
);
180 if (post
.message_contents() == ClientToServerMessage::COMMIT
&&
181 !mid_commit_callback_
.is_null()) {
182 mid_commit_callback_
.Run();
183 mid_commit_callback_
.Reset();
185 if (mid_commit_observer_
) {
186 mid_commit_observer_
->Observe();
192 sync_pb::GetUpdatesResponse
* MockConnectionManager::GetUpdateResponse() {
193 if (update_queue_
.empty()) {
196 return &update_queue_
.back();
199 void MockConnectionManager::AddDefaultBookmarkData(sync_pb::SyncEntity
* entity
,
201 if (use_legacy_bookmarks_protocol_
) {
202 sync_pb::SyncEntity_BookmarkData
* data
= entity
->mutable_bookmarkdata();
203 data
->set_bookmark_folder(is_folder
);
206 data
->set_bookmark_url("http://google.com");
209 entity
->set_folder(is_folder
);
210 entity
->mutable_specifics()->mutable_bookmark();
212 entity
->mutable_specifics()->mutable_bookmark()->
213 set_url("http://google.com");
218 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateDirectory(
224 std::string originator_cache_guid
,
225 std::string originator_client_item_id
) {
226 return AddUpdateDirectory(TestIdFactory::FromNumber(id
),
227 TestIdFactory::FromNumber(parent_id
),
231 originator_cache_guid
,
232 originator_client_item_id
);
235 void MockConnectionManager::SetGUClientCommand(
236 sync_pb::ClientCommand
* command
) {
237 gu_client_command_
.reset(command
);
240 void MockConnectionManager::SetCommitClientCommand(
241 sync_pb::ClientCommand
* command
) {
242 commit_client_command_
.reset(command
);
245 void MockConnectionManager::SetTransientErrorId(syncable::Id id
) {
246 transient_error_ids_
.push_back(id
);
249 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateBookmark(
250 int id
, int parent_id
,
251 string name
, int64 version
,
253 string originator_client_item_id
,
254 string originator_cache_guid
) {
255 return AddUpdateBookmark(TestIdFactory::FromNumber(id
),
256 TestIdFactory::FromNumber(parent_id
),
260 originator_client_item_id
,
261 originator_cache_guid
);
264 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateSpecifics(
272 const sync_pb::EntitySpecifics
& specifics
) {
273 sync_pb::SyncEntity
* ent
= AddUpdateMeta(
274 TestIdFactory::FromNumber(id
).GetServerId(),
275 TestIdFactory::FromNumber(parent_id
).GetServerId(),
276 name
, version
, sync_ts
);
277 ent
->set_position_in_parent(position
);
278 ent
->mutable_specifics()->CopyFrom(specifics
);
279 ent
->set_folder(is_dir
);
283 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateSpecifics(
291 const sync_pb::EntitySpecifics
& specifics
,
292 string originator_cache_guid
,
293 string originator_client_item_id
) {
294 sync_pb::SyncEntity
* ent
= AddUpdateSpecifics(
295 id
, parent_id
, name
, version
, sync_ts
, is_dir
, position
, specifics
);
296 ent
->set_originator_cache_guid(originator_cache_guid
);
297 ent
->set_originator_client_item_id(originator_client_item_id
);
301 sync_pb::SyncEntity
* MockConnectionManager::SetNigori(
305 const sync_pb::EntitySpecifics
& specifics
) {
306 sync_pb::SyncEntity
* ent
= GetUpdateResponse()->add_entries();
307 ent
->set_id_string(TestIdFactory::FromNumber(id
).GetServerId());
308 ent
->set_parent_id_string(TestIdFactory::FromNumber(0).GetServerId());
309 ent
->set_server_defined_unique_tag(ModelTypeToRootTag(NIGORI
));
310 ent
->set_name("Nigori");
311 ent
->set_non_unique_name("Nigori");
312 ent
->set_version(version
);
313 ent
->set_sync_timestamp(sync_ts
);
314 ent
->set_mtime(sync_ts
);
316 ent
->set_position_in_parent(0);
317 ent
->set_folder(false);
318 ent
->mutable_specifics()->CopyFrom(specifics
);
322 sync_pb::SyncEntity
* MockConnectionManager::AddUpdatePref(string id
,
327 sync_pb::SyncEntity
* ent
=
328 AddUpdateMeta(id
, parent_id
, " ", version
, sync_ts
);
330 ent
->set_client_defined_unique_tag(client_tag
);
332 sync_pb::EntitySpecifics specifics
;
333 AddDefaultFieldValue(PREFERENCES
, &specifics
);
334 ent
->mutable_specifics()->CopyFrom(specifics
);
339 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateFull(
340 string id
, string parent_id
,
341 string name
, int64 version
,
342 int64 sync_ts
, bool is_dir
) {
343 sync_pb::SyncEntity
* ent
=
344 AddUpdateMeta(id
, parent_id
, name
, version
, sync_ts
);
345 AddDefaultBookmarkData(ent
, is_dir
);
349 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateMeta(
350 string id
, string parent_id
,
351 string name
, int64 version
,
353 sync_pb::SyncEntity
* ent
= GetUpdateResponse()->add_entries();
354 ent
->set_id_string(id
);
355 ent
->set_parent_id_string(parent_id
);
356 ent
->set_non_unique_name(name
);
358 ent
->set_version(version
);
359 ent
->set_sync_timestamp(sync_ts
);
360 ent
->set_mtime(sync_ts
);
362 ent
->set_position_in_parent(GeneratePositionInParent());
364 // This isn't perfect, but it works well enough. This is an update, which
365 // means the ID is a server ID, which means it never changes. By making
366 // kCacheGuid also never change, we guarantee that the same item always has
367 // the same originator_cache_guid and originator_client_item_id.
369 // Unfortunately, neither this class nor the tests that use it explicitly
370 // track sync entitites, so supporting proper cache guids and client item IDs
371 // would require major refactoring. The ID used here ought to be the "c-"
372 // style ID that was sent up on the commit.
373 ent
->set_originator_cache_guid(kCacheGuid
);
374 ent
->set_originator_client_item_id(id
);
379 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateDirectory(
385 std::string originator_cache_guid
,
386 std::string originator_client_item_id
) {
387 sync_pb::SyncEntity
* ret
=
388 AddUpdateFull(id
, parent_id
, name
, version
, sync_ts
, true);
389 ret
->set_originator_cache_guid(originator_cache_guid
);
390 ret
->set_originator_client_item_id(originator_client_item_id
);
394 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateBookmark(
397 string name
, int64 version
,
399 string originator_cache_guid
,
400 string originator_client_item_id
) {
401 sync_pb::SyncEntity
* ret
=
402 AddUpdateFull(id
, parent_id
, name
, version
, sync_ts
, false);
403 ret
->set_originator_cache_guid(originator_cache_guid
);
404 ret
->set_originator_client_item_id(originator_client_item_id
);
408 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateFromLastCommit() {
409 EXPECT_EQ(1, last_sent_commit().entries_size());
410 EXPECT_EQ(1, last_commit_response().entryresponse_size());
411 EXPECT_EQ(CommitResponse::SUCCESS
,
412 last_commit_response().entryresponse(0).response_type());
414 if (last_sent_commit().entries(0).deleted()) {
415 ModelType type
= GetModelType(last_sent_commit().entries(0));
416 AddUpdateTombstone(syncable::Id::CreateFromServerId(
417 last_sent_commit().entries(0).id_string()), type
);
419 sync_pb::SyncEntity
* ent
= GetUpdateResponse()->add_entries();
420 ent
->CopyFrom(last_sent_commit().entries(0));
421 ent
->clear_insert_after_item_id();
422 ent
->clear_old_parent_id();
423 ent
->set_position_in_parent(
424 last_commit_response().entryresponse(0).position_in_parent());
426 last_commit_response().entryresponse(0).version());
428 last_commit_response().entryresponse(0).id_string());
430 // This is the same hack as in AddUpdateMeta. See the comment in that
431 // function for more information.
432 ent
->set_originator_cache_guid(kCacheGuid
);
433 ent
->set_originator_client_item_id(
434 last_commit_response().entryresponse(0).id_string());
436 if (last_sent_commit().entries(0).has_unique_position()) {
437 ent
->mutable_unique_position()->CopyFrom(
438 last_sent_commit().entries(0).unique_position());
441 // Tests don't currently care about the following:
442 // parent_id_string, name, non_unique_name.
444 return GetMutableLastUpdate();
447 void MockConnectionManager::AddUpdateTombstone(
448 const syncable::Id
& id
,
450 // Tombstones have only the ID set and dummy values for the required fields.
451 sync_pb::SyncEntity
* ent
= GetUpdateResponse()->add_entries();
452 ent
->set_id_string(id
.GetServerId());
455 ent
->set_deleted(true);
457 // Make sure we can still extract the ModelType from this tombstone.
458 AddDefaultFieldValue(type
, ent
->mutable_specifics());
461 void MockConnectionManager::SetLastUpdateDeleted() {
462 // Tombstones have only the ID set. Wipe anything else.
463 string id_string
= GetMutableLastUpdate()->id_string();
464 ModelType type
= GetModelType(*GetMutableLastUpdate());
465 GetUpdateResponse()->mutable_entries()->RemoveLast();
466 AddUpdateTombstone(syncable::Id::CreateFromServerId(id_string
), type
);
469 void MockConnectionManager::SetLastUpdateOriginatorFields(
470 const string
& client_id
,
471 const string
& entry_id
) {
472 GetMutableLastUpdate()->set_originator_cache_guid(client_id
);
473 GetMutableLastUpdate()->set_originator_client_item_id(entry_id
);
476 void MockConnectionManager::SetLastUpdateServerTag(const string
& tag
) {
477 GetMutableLastUpdate()->set_server_defined_unique_tag(tag
);
480 void MockConnectionManager::SetLastUpdateClientTag(const string
& tag
) {
481 GetMutableLastUpdate()->set_client_defined_unique_tag(tag
);
484 void MockConnectionManager::SetLastUpdatePosition(int64 server_position
) {
485 GetMutableLastUpdate()->set_position_in_parent(server_position
);
488 void MockConnectionManager::SetNewTimestamp(int ts
) {
489 next_token_
= base::StringPrintf("mock connection ts = %d", ts
);
493 void MockConnectionManager::ApplyToken() {
494 if (!update_queue_
.empty()) {
495 GetUpdateResponse()->clear_new_progress_marker();
496 sync_pb::DataTypeProgressMarker
* new_marker
=
497 GetUpdateResponse()->add_new_progress_marker();
498 new_marker
->set_data_type_id(-1); // Invalid -- clients shouldn't see.
499 new_marker
->set_token(next_token_
);
503 void MockConnectionManager::SetChangesRemaining(int64 timestamp
) {
504 GetUpdateResponse()->set_changes_remaining(timestamp
);
507 void MockConnectionManager::ProcessGetUpdates(
508 sync_pb::ClientToServerMessage
* csm
,
509 sync_pb::ClientToServerResponse
* response
) {
510 CHECK(csm
->has_get_updates());
511 ASSERT_EQ(csm
->message_contents(), ClientToServerMessage::GET_UPDATES
);
512 const GetUpdatesMessage
& gu
= csm
->get_updates();
513 num_get_updates_requests_
++;
514 EXPECT_FALSE(gu
.has_from_timestamp());
515 EXPECT_FALSE(gu
.has_requested_types());
517 if (fail_non_periodic_get_updates_
) {
518 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::PERIODIC
,
519 gu
.caller_info().source());
522 // Verify that the items we're about to send back to the client are of
523 // the types requested by the client. If this fails, it probably indicates
525 EXPECT_TRUE(gu
.fetch_folders());
526 EXPECT_FALSE(gu
.has_requested_types());
527 if (update_queue_
.empty()) {
530 sync_pb::GetUpdatesResponse
* updates
= &update_queue_
.front();
531 for (int i
= 0; i
< updates
->entries_size(); ++i
) {
532 if (!updates
->entries(i
).deleted()) {
533 ModelType entry_type
= GetModelType(updates
->entries(i
));
535 IsModelTypePresentInSpecifics(gu
.from_progress_marker(), entry_type
))
536 << "Syncer did not request updates being provided by the test.";
540 response
->mutable_get_updates()->CopyFrom(*updates
);
542 // Set appropriate progress markers, overriding the value squirreled
543 // away by ApplyToken().
544 std::string token
= response
->get_updates().new_progress_marker(0).token();
545 response
->mutable_get_updates()->clear_new_progress_marker();
546 for (int i
= 0; i
< gu
.from_progress_marker_size(); ++i
) {
547 sync_pb::DataTypeProgressMarker
* new_marker
=
548 response
->mutable_get_updates()->add_new_progress_marker();
549 new_marker
->set_data_type_id(gu
.from_progress_marker(i
).data_type_id());
550 new_marker
->set_token(token
);
553 // Fill the keystore key if requested.
554 if (gu
.need_encryption_key())
555 response
->mutable_get_updates()->add_encryption_keys(keystore_key_
);
557 update_queue_
.pop_front();
559 if (gu_client_command_
) {
560 response
->mutable_client_command()->CopyFrom(*gu_client_command_
.get());
564 void MockConnectionManager::SetKeystoreKey(const std::string
& key
) {
565 // Note: this is not a thread-safe set, ok for now. NOT ok if tests
566 // run the syncer on the background thread while this method is called.
570 bool MockConnectionManager::ShouldConflictThisCommit() {
571 bool conflict
= false;
572 if (conflict_all_commits_
) {
574 } else if (conflict_n_commits_
> 0) {
576 --conflict_n_commits_
;
581 bool MockConnectionManager::ShouldTransientErrorThisId(syncable::Id id
) {
582 return find(transient_error_ids_
.begin(), transient_error_ids_
.end(), id
)
583 != transient_error_ids_
.end();
586 void MockConnectionManager::ProcessCommit(
587 sync_pb::ClientToServerMessage
* csm
,
588 sync_pb::ClientToServerResponse
* response_buffer
) {
589 CHECK(csm
->has_commit());
590 ASSERT_EQ(csm
->message_contents(), ClientToServerMessage::COMMIT
);
591 map
<string
, string
> changed_ids
;
592 const CommitMessage
& commit_message
= csm
->commit();
593 CommitResponse
* commit_response
= response_buffer
->mutable_commit();
594 commit_messages_
.push_back(new CommitMessage
);
595 commit_messages_
.back()->CopyFrom(commit_message
);
596 map
<string
, sync_pb::CommitResponse_EntryResponse
*> response_map
;
597 for (int i
= 0; i
< commit_message
.entries_size() ; i
++) {
598 const sync_pb::SyncEntity
& entry
= commit_message
.entries(i
);
599 CHECK(entry
.has_id_string());
600 string id_string
= entry
.id_string();
601 ASSERT_LT(entry
.name().length(), 256ul) << " name probably too long. True "
602 "server name checking not implemented";
604 if (entry
.version() == 0) {
605 // Relies on our new item string id format. (string representation of a
607 id
= syncable::Id::CreateFromClientString(id_string
);
609 id
= syncable::Id::CreateFromServerId(id_string
);
611 committed_ids_
.push_back(id
);
613 if (response_map
.end() == response_map
.find(id_string
))
614 response_map
[id_string
] = commit_response
->add_entryresponse();
615 sync_pb::CommitResponse_EntryResponse
* er
= response_map
[id_string
];
616 if (ShouldConflictThisCommit()) {
617 er
->set_response_type(CommitResponse::CONFLICT
);
620 if (ShouldTransientErrorThisId(id
)) {
621 er
->set_response_type(CommitResponse::TRANSIENT_ERROR
);
624 er
->set_response_type(CommitResponse::SUCCESS
);
625 er
->set_version(entry
.version() + 1);
626 if (!commit_time_rename_prepended_string_
.empty()) {
627 // Commit time rename sent down from the server.
628 er
->set_name(commit_time_rename_prepended_string_
+ entry
.name());
630 string parent_id_string
= entry
.parent_id_string();
631 // Remap id's we've already assigned.
632 if (changed_ids
.end() != changed_ids
.find(parent_id_string
)) {
633 parent_id_string
= changed_ids
[parent_id_string
];
634 er
->set_parent_id_string(parent_id_string
);
636 if (entry
.has_version() && 0 != entry
.version()) {
637 er
->set_id_string(id_string
); // Allows verification.
639 string new_id
= base::StringPrintf("mock_server:%d", next_new_id_
++);
640 changed_ids
[id_string
] = new_id
;
641 er
->set_id_string(new_id
);
644 commit_responses_
.push_back(new CommitResponse(*commit_response
));
646 if (commit_client_command_
) {
647 response_buffer
->mutable_client_command()->CopyFrom(
648 *commit_client_command_
.get());
652 void MockConnectionManager::ProcessClearServerData(
653 sync_pb::ClientToServerMessage
* csm
,
654 sync_pb::ClientToServerResponse
* response
) {
655 CHECK(csm
->has_clear_server_data());
656 ASSERT_EQ(csm
->message_contents(), ClientToServerMessage::CLEAR_SERVER_DATA
);
657 response
->mutable_clear_server_data();
660 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateDirectory(
662 syncable::Id parent_id
,
666 string originator_cache_guid
,
667 string originator_client_item_id
) {
668 return AddUpdateDirectory(id
.GetServerId(), parent_id
.GetServerId(),
669 name
, version
, sync_ts
, originator_cache_guid
,
670 originator_client_item_id
);
673 sync_pb::SyncEntity
* MockConnectionManager::AddUpdateBookmark(
675 syncable::Id parent_id
,
679 string originator_cache_guid
,
680 string originator_client_item_id
) {
681 return AddUpdateBookmark(id
.GetServerId(), parent_id
.GetServerId(),
682 name
, version
, sync_ts
, originator_cache_guid
,
683 originator_client_item_id
);
686 sync_pb::SyncEntity
* MockConnectionManager::GetMutableLastUpdate() {
687 sync_pb::GetUpdatesResponse
* updates
= GetUpdateResponse();
688 EXPECT_GT(updates
->entries_size(), 0);
689 return updates
->mutable_entries()->Mutable(updates
->entries_size() - 1);
692 void MockConnectionManager::NextUpdateBatch() {
693 update_queue_
.push_back(sync_pb::GetUpdatesResponse::default_instance());
694 SetChangesRemaining(0);
698 const CommitMessage
& MockConnectionManager::last_sent_commit() const {
699 EXPECT_TRUE(!commit_messages_
.empty());
700 return *commit_messages_
.back();
703 const CommitResponse
& MockConnectionManager::last_commit_response() const {
704 EXPECT_TRUE(!commit_responses_
.empty());
705 return *commit_responses_
.back();
708 const sync_pb::ClientToServerMessage
&
709 MockConnectionManager::last_request() const {
710 EXPECT_TRUE(!requests_
.empty());
711 return requests_
.back();
714 const std::vector
<sync_pb::ClientToServerMessage
>&
715 MockConnectionManager::requests() const {
719 bool MockConnectionManager::IsModelTypePresentInSpecifics(
720 const google::protobuf::RepeatedPtrField
<
721 sync_pb::DataTypeProgressMarker
>& filter
,
723 int data_type_id
= GetSpecificsFieldNumberFromModelType(value
);
724 for (int i
= 0; i
< filter
.size(); ++i
) {
725 if (filter
.Get(i
).data_type_id() == data_type_id
) {
732 sync_pb::DataTypeProgressMarker
const*
733 MockConnectionManager::GetProgressMarkerForType(
734 const google::protobuf::RepeatedPtrField
<
735 sync_pb::DataTypeProgressMarker
>& filter
,
737 int data_type_id
= GetSpecificsFieldNumberFromModelType(value
);
738 for (int i
= 0; i
< filter
.size(); ++i
) {
739 if (filter
.Get(i
).data_type_id() == data_type_id
) {
740 return &(filter
.Get(i
));
746 void MockConnectionManager::SetServerReachable() {
747 server_reachable_
= true;
750 void MockConnectionManager::SetServerNotReachable() {
751 server_reachable_
= false;
754 void MockConnectionManager::UpdateConnectionStatus() {
755 if (!server_reachable_
) {
756 server_status_
= HttpResponse::CONNECTION_UNAVAILABLE
;
758 server_status_
= HttpResponse::SERVER_CONNECTION_OK
;
762 void MockConnectionManager::SetServerStatus(
763 HttpResponse::ServerConnectionCode server_status
) {
764 server_status_
= server_status
;
767 } // namespace syncer