OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Mock ServerConnectionManager class for use in client regression tests. | 5 // Mock ServerConnectionManager class for use in client regression tests. |
6 | 6 |
7 #include "chrome/test/sync/engine/mock_server_connection.h" | 7 #include "chrome/test/sync/engine/mock_server_connection.h" |
8 | 8 |
9 #include "chrome/browser/sync/engine/syncer_proto_util.h" | 9 #include "chrome/browser/sync/engine/syncer_proto_util.h" |
10 #include "chrome/browser/sync/util/character_set_converters.h" | 10 #include "chrome/browser/sync/util/character_set_converters.h" |
11 #include "chrome/test/sync/engine/test_id_factory.h" | 11 #include "chrome/test/sync/engine/test_id_factory.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using browser_sync::HttpResponse; | 14 using browser_sync::HttpResponse; |
15 using browser_sync::ServerConnectionManager; | 15 using browser_sync::ServerConnectionManager; |
16 using browser_sync::SyncerProtoUtil; | 16 using browser_sync::SyncerProtoUtil; |
17 using browser_sync::TestIdFactory; | 17 using browser_sync::TestIdFactory; |
18 using std::map; | 18 using std::map; |
19 using std::string; | 19 using std::string; |
20 using sync_pb::ClientToServerMessage; | 20 using sync_pb::ClientToServerMessage; |
21 using sync_pb::ClientToServerResponse; | 21 using sync_pb::ClientToServerResponse; |
22 using sync_pb::CommitMessage; | 22 using sync_pb::CommitMessage; |
23 using sync_pb::CommitResponse; | 23 using sync_pb::CommitResponse; |
24 using sync_pb::CommitResponse_EntryResponse; | 24 using sync_pb::CommitResponse_EntryResponse; |
25 using sync_pb::GetUpdatesMessage; | 25 using sync_pb::GetUpdatesMessage; |
26 using sync_pb::SyncEntity; | 26 using sync_pb::SyncEntity; |
27 using syncable::DirectoryManager; | 27 using syncable::DirectoryManager; |
28 using syncable::ScopedDirLookup; | |
29 using syncable::WriteTransaction; | 28 using syncable::WriteTransaction; |
30 | 29 |
31 MockConnectionManager::MockConnectionManager(DirectoryManager* dirmgr, | 30 MockConnectionManager::MockConnectionManager(DirectoryManager* dirmgr, |
32 PathString name) | 31 PathString name) |
33 : ServerConnectionManager("unused", 0, false, "version", "id"), | 32 : ServerConnectionManager("unused", 0, false, "version", "id"), |
34 conflict_all_commits_(false), | 33 conflict_all_commits_(false), |
35 conflict_n_commits_(0), | 34 conflict_n_commits_(0), |
36 next_new_id_(10000), | 35 next_new_id_(10000), |
37 store_birthday_("Store BDay!"), | 36 store_birthday_("Store BDay!"), |
38 store_birthday_sent_(false), | 37 store_birthday_sent_(false), |
39 client_stuck_(false), | 38 client_stuck_(false), |
40 commit_time_rename_prepended_string_(""), | 39 commit_time_rename_prepended_string_(""), |
41 fail_next_postbuffer_(false), | 40 fail_next_postbuffer_(false), |
42 directory_manager_(dirmgr), | 41 directory_(dirmgr, name), |
43 directory_name_(name), | |
44 mid_commit_callback_function_(NULL), | 42 mid_commit_callback_function_(NULL), |
45 mid_commit_observer_(NULL), | |
46 client_command_(NULL), | 43 client_command_(NULL), |
47 next_position_in_parent_(2) { | 44 next_position_in_parent_(2) { |
48 server_reachable_ = true; | 45 server_reachable_ = true; |
| 46 CHECK(directory_.good()); |
49 }; | 47 }; |
50 | 48 |
51 MockConnectionManager::~MockConnectionManager() { | 49 MockConnectionManager::~MockConnectionManager() { |
52 for (size_t i = 0; i < commit_messages_.size(); i++) | 50 for (size_t i = 0; i < commit_messages_.size(); i++) |
53 delete commit_messages_[i]; | 51 delete commit_messages_[i]; |
54 } | 52 } |
55 | 53 |
56 void MockConnectionManager::SetCommitTimeRename(string prepend) { | 54 void MockConnectionManager::SetCommitTimeRename(string prepend) { |
57 commit_time_rename_prepended_string_ = prepend; | 55 commit_time_rename_prepended_string_ = prepend; |
58 } | 56 } |
59 | 57 |
60 void MockConnectionManager::SetMidCommitCallbackFunction( | 58 void MockConnectionManager::SetMidCommitCallbackFunction( |
61 MockConnectionManager::TestCallbackFunction callback) { | 59 MockConnectionManager::TestCallbackFunction callback) { |
62 mid_commit_callback_function_ = callback; | 60 mid_commit_callback_function_ = callback; |
63 } | 61 } |
64 | 62 |
65 void MockConnectionManager::SetMidCommitObserver( | |
66 MockConnectionManager::MidCommitObserver* observer) { | |
67 mid_commit_observer_ = observer; | |
68 } | |
69 | |
70 bool MockConnectionManager::PostBufferToPath(const PostBufferParams* params, | 63 bool MockConnectionManager::PostBufferToPath(const PostBufferParams* params, |
71 const string& path, | 64 const string& path, |
72 const string& auth_token) { | 65 const string& auth_token) { |
73 | |
74 ScopedDirLookup directory(directory_manager_, directory_name_); | |
75 CHECK(directory.good()); | |
76 | |
77 ClientToServerMessage post; | 66 ClientToServerMessage post; |
78 CHECK(post.ParseFromString(params->buffer_in)); | 67 CHECK(post.ParseFromString(params->buffer_in)); |
79 client_stuck_ = post.sync_problem_detected(); | 68 client_stuck_ = post.sync_problem_detected(); |
80 ClientToServerResponse response; | 69 ClientToServerResponse response; |
81 response.Clear(); | 70 response.Clear(); |
82 // If the Directory's locked when we do this, it's a problem as in normal | 71 // If the Directory's locked when we do this, it's a problem as in normal |
83 // use this function could take a while to return because it accesses the | 72 // use this function could take a while to return because it accesses the |
84 // network. As we can't test this we do the next best thing and hang here | 73 // network. As we can't test this we do the next best thing and hang here |
85 // when there's an issue. | 74 // when there's an issue. |
86 { | 75 { |
87 WriteTransaction wt(directory, syncable::UNITTEST, __FILE__, __LINE__); | 76 WriteTransaction wt(directory_, syncable::UNITTEST, __FILE__, __LINE__); |
88 } | 77 } |
89 if (fail_next_postbuffer_) { | 78 if (fail_next_postbuffer_) { |
90 fail_next_postbuffer_ = false; | 79 fail_next_postbuffer_ = false; |
91 return false; | 80 return false; |
92 } | 81 } |
93 // Default to an ok connection. | 82 // Default to an ok connection. |
94 params->response->server_status = HttpResponse::SERVER_CONNECTION_OK; | 83 params->response->server_status = HttpResponse::SERVER_CONNECTION_OK; |
95 response.set_store_birthday(store_birthday_); | 84 response.set_store_birthday(store_birthday_); |
96 if (post.has_store_birthday() && post.store_birthday() != store_birthday_) { | 85 if (post.has_store_birthday() && post.store_birthday() != store_birthday_) { |
97 response.set_error_code(ClientToServerResponse::NOT_MY_BIRTHDAY); | 86 response.set_error_code(ClientToServerResponse::NOT_MY_BIRTHDAY); |
(...skipping 11 matching lines...) Expand all Loading... |
109 ProcessGetUpdates(&post, &response); | 98 ProcessGetUpdates(&post, &response); |
110 } else { | 99 } else { |
111 EXPECT_TRUE(false) << "Unknown/unsupported ClientToServerMessage"; | 100 EXPECT_TRUE(false) << "Unknown/unsupported ClientToServerMessage"; |
112 return false; | 101 return false; |
113 } | 102 } |
114 if (client_command_.get()) { | 103 if (client_command_.get()) { |
115 response.mutable_client_command()->CopyFrom(*client_command_.get()); | 104 response.mutable_client_command()->CopyFrom(*client_command_.get()); |
116 } | 105 } |
117 response.SerializeToString(params->buffer_out); | 106 response.SerializeToString(params->buffer_out); |
118 if (mid_commit_callback_function_) { | 107 if (mid_commit_callback_function_) { |
119 if (mid_commit_callback_function_(directory)) | 108 if (mid_commit_callback_function_(directory_)) |
120 mid_commit_callback_function_ = 0; | 109 mid_commit_callback_function_ = 0; |
121 } | 110 } |
122 if (mid_commit_observer_) { | |
123 mid_commit_observer_->Observe(); | |
124 } | |
125 return result; | 111 return result; |
126 } | 112 } |
127 | 113 |
128 bool MockConnectionManager::IsServerReachable() { | 114 bool MockConnectionManager::IsServerReachable() { |
129 return true; | 115 return true; |
130 } | 116 } |
131 | 117 |
132 bool MockConnectionManager::IsUserAuthenticated() { | 118 bool MockConnectionManager::IsUserAuthenticated() { |
133 return true; | 119 return true; |
134 } | 120 } |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 328 |
343 SyncEntity* MockConnectionManager::GetMutableLastUpdate() { | 329 SyncEntity* MockConnectionManager::GetMutableLastUpdate() { |
344 DCHECK(updates_.entries_size() > 0); | 330 DCHECK(updates_.entries_size() > 0); |
345 return updates_.mutable_entries()->Mutable(updates_.entries_size() - 1); | 331 return updates_.mutable_entries()->Mutable(updates_.entries_size() - 1); |
346 } | 332 } |
347 | 333 |
348 const CommitMessage& MockConnectionManager::last_sent_commit() const { | 334 const CommitMessage& MockConnectionManager::last_sent_commit() const { |
349 DCHECK(!commit_messages_.empty()); | 335 DCHECK(!commit_messages_.empty()); |
350 return *commit_messages_.back(); | 336 return *commit_messages_.back(); |
351 } | 337 } |
OLD | NEW |