OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "sync/engine/download.h" | 5 #include "sync/engine/download.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "sync/engine/sync_directory_update_handler.h" | 9 #include "sync/engine/sync_directory_update_handler.h" |
10 #include "sync/internal_api/public/base/model_type_test_util.h" | 10 #include "sync/internal_api/public/base/model_type_test_util.h" |
11 #include "sync/protocol/sync.pb.h" | 11 #include "sync/protocol/sync.pb.h" |
12 #include "sync/sessions/debug_info_getter.h" | 12 #include "sync/sessions/debug_info_getter.h" |
13 #include "sync/sessions/nudge_tracker.h" | 13 #include "sync/sessions/nudge_tracker.h" |
14 #include "sync/sessions/status_controller.h" | 14 #include "sync/sessions/status_controller.h" |
15 #include "sync/syncable/directory.h" | 15 #include "sync/syncable/directory.h" |
16 #include "sync/test/engine/fake_model_worker.h" | 16 #include "sync/test/engine/fake_model_worker.h" |
17 #include "sync/test/engine/test_directory_setter_upper.h" | 17 #include "sync/test/engine/test_directory_setter_upper.h" |
18 #include "sync/test/sessions/mock_debug_info_getter.h" | 18 #include "sync/test/sessions/mock_debug_info_getter.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace syncer { | 21 namespace syncer { |
22 | 22 |
23 using sessions::MockDebugInfoGetter; | 23 using sessions::MockDebugInfoGetter; |
24 | 24 |
25 // A test fixture for tests exercising download updates functions. | 25 // A test fixture for tests exercising download updates functions. |
26 class DownloadUpdatesTest : public ::testing::Test { | 26 class DownloadUpdatesTest : public ::testing::Test { |
27 protected: | 27 protected: |
28 DownloadUpdatesTest() | 28 DownloadUpdatesTest() {} |
29 : update_handler_map_deleter_(&update_handler_map_) { | |
30 } | |
31 | 29 |
32 virtual void SetUp() { | 30 virtual void SetUp() { |
33 dir_maker_.SetUp(); | 31 dir_maker_.SetUp(); |
34 | 32 |
35 AddUpdateHandler(AUTOFILL, GROUP_DB); | 33 AddUpdateHandler(AUTOFILL, GROUP_DB); |
36 AddUpdateHandler(BOOKMARKS, GROUP_UI); | 34 AddUpdateHandler(BOOKMARKS, GROUP_UI); |
37 AddUpdateHandler(PREFERENCES, GROUP_UI); | 35 AddUpdateHandler(PREFERENCES, GROUP_UI); |
38 } | 36 } |
39 | 37 |
40 virtual void TearDown() { | 38 virtual void TearDown() { |
41 dir_maker_.TearDown(); | 39 dir_maker_.TearDown(); |
42 } | 40 } |
43 | 41 |
44 ModelTypeSet proto_request_types() { | 42 ModelTypeSet proto_request_types() { |
45 ModelTypeSet types; | 43 return proto_request_types_; |
46 for (UpdateHandlerMap::iterator it = update_handler_map_.begin(); | |
47 it != update_handler_map_.end(); ++it) { | |
48 types.Put(it->first); | |
49 } | |
50 return types; | |
51 } | 44 } |
52 | 45 |
53 syncable::Directory* directory() { | 46 syncable::Directory* directory() { |
54 return dir_maker_.directory(); | 47 return dir_maker_.directory(); |
55 } | 48 } |
56 | 49 |
57 UpdateHandlerMap* update_handler_map() { | 50 UpdaterList* updater_list() { |
58 return &update_handler_map_; | 51 return &updater_list_; |
59 } | 52 } |
60 | 53 |
61 void InitFakeUpdateResponse(sync_pb::GetUpdatesResponse* response) { | 54 void InitFakeUpdateResponse(sync_pb::GetUpdatesResponse* response) { |
62 ModelTypeSet types = proto_request_types(); | 55 ModelTypeSet types = proto_request_types(); |
63 | 56 |
64 for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { | 57 for (ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) { |
65 sync_pb::DataTypeProgressMarker* marker = | 58 sync_pb::DataTypeProgressMarker* marker = |
66 response->add_new_progress_marker(); | 59 response->add_new_progress_marker(); |
67 marker->set_data_type_id(GetSpecificsFieldNumberFromModelType(it.Get())); | 60 marker->set_data_type_id(GetSpecificsFieldNumberFromModelType(it.Get())); |
68 marker->set_token("foobarbaz"); | 61 marker->set_token("foobarbaz"); |
69 } | 62 } |
70 | 63 |
71 response->set_changes_remaining(0); | 64 response->set_changes_remaining(0); |
72 } | 65 } |
73 | 66 |
74 private: | 67 private: |
75 void AddUpdateHandler(ModelType type, ModelSafeGroup group) { | 68 void AddUpdateHandler(ModelType type, ModelSafeGroup group) { |
76 DCHECK(directory()); | 69 DCHECK(directory()); |
| 70 |
| 71 proto_request_types_.Put(type); |
| 72 |
77 scoped_refptr<ModelSafeWorker> worker = new FakeModelWorker(group); | 73 scoped_refptr<ModelSafeWorker> worker = new FakeModelWorker(group); |
78 SyncDirectoryUpdateHandler* handler = | 74 SyncDirectoryUpdateHandler* handler = |
79 new SyncDirectoryUpdateHandler(directory(), type, worker); | 75 new SyncDirectoryUpdateHandler(directory(), type, worker); |
80 update_handler_map_.insert(std::make_pair(type, handler)); | 76 updater_list_.RegisterTypeForTest(type, handler); |
81 } | 77 } |
82 | 78 |
83 base::MessageLoop loop_; // Needed for directory init. | 79 base::MessageLoop loop_; // Needed for directory init. |
84 TestDirectorySetterUpper dir_maker_; | 80 TestDirectorySetterUpper dir_maker_; |
85 | 81 |
86 UpdateHandlerMap update_handler_map_; | 82 ModelTypeSet proto_request_types_; |
87 STLValueDeleter<UpdateHandlerMap> update_handler_map_deleter_; | 83 UpdaterList updater_list_; |
88 | 84 |
89 DISALLOW_COPY_AND_ASSIGN(DownloadUpdatesTest); | 85 DISALLOW_COPY_AND_ASSIGN(DownloadUpdatesTest); |
90 }; | 86 }; |
91 | 87 |
92 // Basic test to make sure nudges are expressed properly in the request. | 88 // Basic test to make sure nudges are expressed properly in the request. |
93 TEST_F(DownloadUpdatesTest, BookmarkNudge) { | 89 TEST_F(DownloadUpdatesTest, BookmarkNudge) { |
94 sessions::NudgeTracker nudge_tracker; | 90 sessions::NudgeTracker nudge_tracker; |
95 nudge_tracker.RecordLocalChange(ModelTypeSet(BOOKMARKS)); | 91 nudge_tracker.RecordLocalChange(ModelTypeSet(BOOKMARKS)); |
96 | 92 |
97 sync_pb::ClientToServerMessage msg; | 93 sync_pb::ClientToServerMessage msg; |
98 download::BuildNormalDownloadUpdatesImpl(proto_request_types(), | 94 download::BuildNormalDownloadUpdatesImpl(proto_request_types(), |
99 update_handler_map(), | 95 updater_list(), |
100 nudge_tracker, | 96 nudge_tracker, |
101 msg.mutable_get_updates()); | 97 msg.mutable_get_updates()); |
102 | 98 |
103 const sync_pb::GetUpdatesMessage& gu_msg = msg.get_updates(); | 99 const sync_pb::GetUpdatesMessage& gu_msg = msg.get_updates(); |
104 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::LOCAL, | 100 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::LOCAL, |
105 gu_msg.caller_info().source()); | 101 gu_msg.caller_info().source()); |
106 EXPECT_EQ(sync_pb::SyncEnums::GU_TRIGGER, gu_msg.get_updates_origin()); | 102 EXPECT_EQ(sync_pb::SyncEnums::GU_TRIGGER, gu_msg.get_updates_origin()); |
107 for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) { | 103 for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) { |
108 syncer::ModelType type = GetModelTypeFromSpecificsFieldNumber( | 104 syncer::ModelType type = GetModelTypeFromSpecificsFieldNumber( |
109 gu_msg.from_progress_marker(i).data_type_id()); | 105 gu_msg.from_progress_marker(i).data_type_id()); |
(...skipping 27 matching lines...) Expand all Loading... |
137 BuildInvalidationMap(BOOKMARKS, 1, "bookmark_payload")); | 133 BuildInvalidationMap(BOOKMARKS, 1, "bookmark_payload")); |
138 nudge_tracker.RecordRemoteInvalidation( | 134 nudge_tracker.RecordRemoteInvalidation( |
139 BuildInvalidationMap(PREFERENCES, 1, "preferences_payload")); | 135 BuildInvalidationMap(PREFERENCES, 1, "preferences_payload")); |
140 ModelTypeSet notified_types; | 136 ModelTypeSet notified_types; |
141 notified_types.Put(AUTOFILL); | 137 notified_types.Put(AUTOFILL); |
142 notified_types.Put(BOOKMARKS); | 138 notified_types.Put(BOOKMARKS); |
143 notified_types.Put(PREFERENCES); | 139 notified_types.Put(PREFERENCES); |
144 | 140 |
145 sync_pb::ClientToServerMessage msg; | 141 sync_pb::ClientToServerMessage msg; |
146 download::BuildNormalDownloadUpdatesImpl(proto_request_types(), | 142 download::BuildNormalDownloadUpdatesImpl(proto_request_types(), |
147 update_handler_map(), | 143 updater_list(), |
148 nudge_tracker, | 144 nudge_tracker, |
149 msg.mutable_get_updates()); | 145 msg.mutable_get_updates()); |
150 | 146 |
151 const sync_pb::GetUpdatesMessage& gu_msg = msg.get_updates(); | 147 const sync_pb::GetUpdatesMessage& gu_msg = msg.get_updates(); |
152 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION, | 148 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION, |
153 gu_msg.caller_info().source()); | 149 gu_msg.caller_info().source()); |
154 EXPECT_EQ(sync_pb::SyncEnums::GU_TRIGGER, gu_msg.get_updates_origin()); | 150 EXPECT_EQ(sync_pb::SyncEnums::GU_TRIGGER, gu_msg.get_updates_origin()); |
155 for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) { | 151 for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) { |
156 syncer::ModelType type = GetModelTypeFromSpecificsFieldNumber( | 152 syncer::ModelType type = GetModelTypeFromSpecificsFieldNumber( |
157 gu_msg.from_progress_marker(i).data_type_id()); | 153 gu_msg.from_progress_marker(i).data_type_id()); |
(...skipping 13 matching lines...) Expand all Loading... |
171 EXPECT_FALSE(progress_marker.has_notification_hint()); | 167 EXPECT_FALSE(progress_marker.has_notification_hint()); |
172 EXPECT_EQ(0, gu_trigger.notification_hint_size()); | 168 EXPECT_EQ(0, gu_trigger.notification_hint_size()); |
173 } | 169 } |
174 } | 170 } |
175 } | 171 } |
176 | 172 |
177 TEST_F(DownloadUpdatesTest, ConfigureTest) { | 173 TEST_F(DownloadUpdatesTest, ConfigureTest) { |
178 sync_pb::ClientToServerMessage msg; | 174 sync_pb::ClientToServerMessage msg; |
179 download::BuildDownloadUpdatesForConfigureImpl( | 175 download::BuildDownloadUpdatesForConfigureImpl( |
180 proto_request_types(), | 176 proto_request_types(), |
181 update_handler_map(), | 177 updater_list(), |
182 sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, | 178 sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, |
183 msg.mutable_get_updates()); | 179 msg.mutable_get_updates()); |
184 | 180 |
185 const sync_pb::GetUpdatesMessage& gu_msg = msg.get_updates(); | 181 const sync_pb::GetUpdatesMessage& gu_msg = msg.get_updates(); |
186 | 182 |
187 EXPECT_EQ(sync_pb::SyncEnums::RECONFIGURATION, gu_msg.get_updates_origin()); | 183 EXPECT_EQ(sync_pb::SyncEnums::RECONFIGURATION, gu_msg.get_updates_origin()); |
188 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, | 184 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION, |
189 gu_msg.caller_info().source()); | 185 gu_msg.caller_info().source()); |
190 | 186 |
191 ModelTypeSet progress_types; | 187 ModelTypeSet progress_types; |
192 for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) { | 188 for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) { |
193 syncer::ModelType type = GetModelTypeFromSpecificsFieldNumber( | 189 syncer::ModelType type = GetModelTypeFromSpecificsFieldNumber( |
194 gu_msg.from_progress_marker(i).data_type_id()); | 190 gu_msg.from_progress_marker(i).data_type_id()); |
195 progress_types.Put(type); | 191 progress_types.Put(type); |
196 } | 192 } |
197 EXPECT_TRUE(proto_request_types().Equals(progress_types)); | 193 EXPECT_TRUE(proto_request_types().Equals(progress_types)); |
198 } | 194 } |
199 | 195 |
200 TEST_F(DownloadUpdatesTest, PollTest) { | 196 TEST_F(DownloadUpdatesTest, PollTest) { |
201 sync_pb::ClientToServerMessage msg; | 197 sync_pb::ClientToServerMessage msg; |
202 download::BuildDownloadUpdatesForPollImpl( | 198 download::BuildDownloadUpdatesForPollImpl( |
203 proto_request_types(), | 199 proto_request_types(), |
204 update_handler_map(), | 200 updater_list(), |
205 msg.mutable_get_updates()); | 201 msg.mutable_get_updates()); |
206 | 202 |
207 const sync_pb::GetUpdatesMessage& gu_msg = msg.get_updates(); | 203 const sync_pb::GetUpdatesMessage& gu_msg = msg.get_updates(); |
208 | 204 |
209 EXPECT_EQ(sync_pb::SyncEnums::PERIODIC, gu_msg.get_updates_origin()); | 205 EXPECT_EQ(sync_pb::SyncEnums::PERIODIC, gu_msg.get_updates_origin()); |
210 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::PERIODIC, | 206 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::PERIODIC, |
211 gu_msg.caller_info().source()); | 207 gu_msg.caller_info().source()); |
212 | 208 |
213 ModelTypeSet progress_types; | 209 ModelTypeSet progress_types; |
214 for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) { | 210 for (int i = 0; i < gu_msg.from_progress_marker_size(); ++i) { |
215 syncer::ModelType type = GetModelTypeFromSpecificsFieldNumber( | 211 syncer::ModelType type = GetModelTypeFromSpecificsFieldNumber( |
216 gu_msg.from_progress_marker(i).data_type_id()); | 212 gu_msg.from_progress_marker(i).data_type_id()); |
217 progress_types.Put(type); | 213 progress_types.Put(type); |
218 } | 214 } |
219 EXPECT_TRUE(proto_request_types().Equals(progress_types)); | 215 EXPECT_TRUE(proto_request_types().Equals(progress_types)); |
220 } | 216 } |
221 | 217 |
222 // Verify that a bogus response message is detected. | 218 // Verify that a bogus response message is detected. |
223 TEST_F(DownloadUpdatesTest, InvalidResponse) { | 219 TEST_F(DownloadUpdatesTest, InvalidResponse) { |
224 sync_pb::GetUpdatesResponse gu_response; | 220 sync_pb::GetUpdatesResponse gu_response; |
225 InitFakeUpdateResponse(&gu_response); | 221 InitFakeUpdateResponse(&gu_response); |
226 | 222 |
227 // This field is essential for making the client stop looping. If it's unset | 223 // This field is essential for making the client stop looping. If it's unset |
228 // then something is very wrong. The client should detect this. | 224 // then something is very wrong. The client should detect this. |
229 gu_response.clear_changes_remaining(); | 225 gu_response.clear_changes_remaining(); |
230 | 226 |
231 sessions::StatusController status; | 227 sessions::StatusController status; |
232 SyncerError error = download::ProcessResponse(gu_response, | 228 SyncerError error = download::ProcessResponse(gu_response, |
233 proto_request_types(), | 229 proto_request_types(), |
234 update_handler_map(), | 230 updater_list(), |
235 &status); | 231 &status); |
236 EXPECT_EQ(error, SERVER_RESPONSE_VALIDATION_FAILED); | 232 EXPECT_EQ(error, SERVER_RESPONSE_VALIDATION_FAILED); |
237 } | 233 } |
238 | 234 |
239 // Verify that we correctly detect when there's more work to be done. | 235 // Verify that we correctly detect when there's more work to be done. |
240 TEST_F(DownloadUpdatesTest, MoreToDownloadResponse) { | 236 TEST_F(DownloadUpdatesTest, MoreToDownloadResponse) { |
241 sync_pb::GetUpdatesResponse gu_response; | 237 sync_pb::GetUpdatesResponse gu_response; |
242 InitFakeUpdateResponse(&gu_response); | 238 InitFakeUpdateResponse(&gu_response); |
243 gu_response.set_changes_remaining(1); | 239 gu_response.set_changes_remaining(1); |
244 | 240 |
245 sessions::StatusController status; | 241 sessions::StatusController status; |
246 SyncerError error = download::ProcessResponse(gu_response, | 242 SyncerError error = download::ProcessResponse(gu_response, |
247 proto_request_types(), | 243 proto_request_types(), |
248 update_handler_map(), | 244 updater_list(), |
249 &status); | 245 &status); |
250 EXPECT_EQ(error, SERVER_MORE_TO_DOWNLOAD); | 246 EXPECT_EQ(error, SERVER_MORE_TO_DOWNLOAD); |
251 } | 247 } |
252 | 248 |
253 // A simple scenario: No updates returned and nothing more to download. | 249 // A simple scenario: No updates returned and nothing more to download. |
254 TEST_F(DownloadUpdatesTest, NormalResponseTest) { | 250 TEST_F(DownloadUpdatesTest, NormalResponseTest) { |
255 sync_pb::GetUpdatesResponse gu_response; | 251 sync_pb::GetUpdatesResponse gu_response; |
256 InitFakeUpdateResponse(&gu_response); | 252 InitFakeUpdateResponse(&gu_response); |
257 gu_response.set_changes_remaining(0); | 253 gu_response.set_changes_remaining(0); |
258 | 254 |
259 sessions::StatusController status; | 255 sessions::StatusController status; |
260 SyncerError error = download::ProcessResponse(gu_response, | 256 SyncerError error = download::ProcessResponse(gu_response, |
261 proto_request_types(), | 257 proto_request_types(), |
262 update_handler_map(), | 258 updater_list(), |
263 &status); | 259 &status); |
264 EXPECT_EQ(error, SYNCER_OK); | 260 EXPECT_EQ(error, SYNCER_OK); |
265 } | 261 } |
266 | 262 |
267 class DownloadUpdatesDebugInfoTest : public ::testing::Test { | 263 class DownloadUpdatesDebugInfoTest : public ::testing::Test { |
268 public: | 264 public: |
269 DownloadUpdatesDebugInfoTest() {} | 265 DownloadUpdatesDebugInfoTest() {} |
270 virtual ~DownloadUpdatesDebugInfoTest() {} | 266 virtual ~DownloadUpdatesDebugInfoTest() {} |
271 | 267 |
272 sessions::StatusController* status() { | 268 sessions::StatusController* status() { |
(...skipping 24 matching lines...) Expand all Loading... |
297 TEST_F(DownloadUpdatesDebugInfoTest, VerifyCopyOverwrites) { | 293 TEST_F(DownloadUpdatesDebugInfoTest, VerifyCopyOverwrites) { |
298 sync_pb::DebugInfo debug_info; | 294 sync_pb::DebugInfo debug_info; |
299 AddDebugEvent(); | 295 AddDebugEvent(); |
300 download::CopyClientDebugInfo(debug_info_getter(), &debug_info); | 296 download::CopyClientDebugInfo(debug_info_getter(), &debug_info); |
301 EXPECT_EQ(1, debug_info.events_size()); | 297 EXPECT_EQ(1, debug_info.events_size()); |
302 download::CopyClientDebugInfo(debug_info_getter(), &debug_info); | 298 download::CopyClientDebugInfo(debug_info_getter(), &debug_info); |
303 EXPECT_EQ(1, debug_info.events_size()); | 299 EXPECT_EQ(1, debug_info.events_size()); |
304 } | 300 } |
305 | 301 |
306 } // namespace syncer | 302 } // namespace syncer |
OLD | NEW |