Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: components/sync/device_info/device_info_sync_service_unittest.cc

Issue 2388163002: [Sync] Move //components/sync to the syncer namespace, take 2. (Closed)
Patch Set: Rebase. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "components/sync/device_info/device_info_sync_service.h" 5 #include "components/sync/device_info/device_info_sync_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <string> 10 #include <string>
11 11
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "components/sync/api/sync_change.h" 13 #include "components/sync/api/sync_change.h"
14 #include "components/sync/api/sync_change_processor.h" 14 #include "components/sync/api/sync_change_processor.h"
15 #include "components/sync/api/sync_change_processor_wrapper_for_test.h" 15 #include "components/sync/api/sync_change_processor_wrapper_for_test.h"
16 #include "components/sync/api/sync_error_factory_mock.h" 16 #include "components/sync/api/sync_error_factory_mock.h"
17 #include "components/sync/base/time.h" 17 #include "components/sync/base/time.h"
18 #include "components/sync/core/attachments/attachment_service_proxy_for_test.h" 18 #include "components/sync/core/attachments/attachment_service_proxy_for_test.h"
19 #include "components/sync/device_info/device_info_util.h" 19 #include "components/sync/device_info/device_info_util.h"
20 #include "components/sync/device_info/local_device_info_provider_mock.h" 20 #include "components/sync/device_info/local_device_info_provider_mock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 using base::Time; 23 using base::Time;
24 using base::TimeDelta; 24 using base::TimeDelta;
25 using syncer::AttachmentIdList;
26 using syncer::AttachmentServiceProxyForTest;
27 using syncer::ModelType;
28 using syncer::SyncChange;
29 using syncer::SyncChangeList;
30 using syncer::SyncChangeProcessor;
31 using syncer::SyncChangeProcessorWrapperForTest;
32 using syncer::SyncData;
33 using syncer::SyncDataList;
34 using syncer::SyncError;
35 using syncer::SyncErrorFactory;
36 using syncer::SyncErrorFactoryMock;
37 using syncer::SyncMergeResult;
38 using sync_pb::EntitySpecifics; 25 using sync_pb::EntitySpecifics;
39 26
40 namespace sync_driver { 27 namespace syncer {
41 28
42 namespace { 29 namespace {
43 30
44 class TestChangeProcessor : public SyncChangeProcessor { 31 class TestChangeProcessor : public SyncChangeProcessor {
45 public: 32 public:
46 TestChangeProcessor() {} 33 TestChangeProcessor() {}
47 ~TestChangeProcessor() override {} 34 ~TestChangeProcessor() override {}
48 35
49 // SyncChangeProcessor implementation. 36 // SyncChangeProcessor implementation.
50 // Store a copy of all the changes passed in so we can examine them later. 37 // Store a copy of all the changes passed in so we can examine them later.
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 return entity; 116 return entity;
130 } 117 }
131 118
132 // Default |last_updated_timestamp| to now to avoid pulse update on merge. 119 // Default |last_updated_timestamp| to now to avoid pulse update on merge.
133 SyncData CreateRemoteData(const std::string& client_id, 120 SyncData CreateRemoteData(const std::string& client_id,
134 const std::string& client_name, 121 const std::string& client_name,
135 Time last_updated_timestamp = Time::Now()) { 122 Time last_updated_timestamp = Time::Now()) {
136 sync_pb::EntitySpecifics entity( 123 sync_pb::EntitySpecifics entity(
137 CreateEntitySpecifics(client_id, client_name)); 124 CreateEntitySpecifics(client_id, client_name));
138 entity.mutable_device_info()->set_last_updated_timestamp( 125 entity.mutable_device_info()->set_last_updated_timestamp(
139 syncer::TimeToProtoTime(last_updated_timestamp)); 126 TimeToProtoTime(last_updated_timestamp));
140 return SyncData::CreateRemoteData(1, entity, Time(), AttachmentIdList(), 127 return SyncData::CreateRemoteData(1, entity, Time(), AttachmentIdList(),
141 AttachmentServiceProxyForTest::Create()); 128 AttachmentServiceProxyForTest::Create());
142 } 129 }
143 130
144 void AddInitialData(SyncDataList* sync_data_list, 131 void AddInitialData(SyncDataList* sync_data_list,
145 const std::string& client_id, 132 const std::string& client_id,
146 const std::string& client_name) { 133 const std::string& client_name) {
147 SyncData sync_data = CreateRemoteData(client_id, client_name); 134 SyncData sync_data = CreateRemoteData(client_id, client_name);
148 sync_data_list->push_back(sync_data); 135 sync_data_list->push_back(sync_data);
149 } 136 }
150 137
151 void AddChange(SyncChangeList* change_list, 138 void AddChange(SyncChangeList* change_list,
152 SyncChange::SyncChangeType change_type, 139 SyncChange::SyncChangeType change_type,
153 const std::string& client_id, 140 const std::string& client_id,
154 const std::string& client_name) { 141 const std::string& client_name) {
155 SyncData sync_data = CreateRemoteData(client_id, client_name); 142 SyncData sync_data = CreateRemoteData(client_id, client_name);
156 SyncChange sync_change(FROM_HERE, change_type, sync_data); 143 SyncChange sync_change(FROM_HERE, change_type, sync_data);
157 change_list->push_back(sync_change); 144 change_list->push_back(sync_change);
158 } 145 }
159 146
160 protected: 147 protected:
161 // Private method wrappers through friend class. 148 // Private method wrappers through friend class.
162 Time GetLastUpdateTime(const syncer::SyncData& data) { 149 Time GetLastUpdateTime(const SyncData& data) {
163 return sync_service_->GetLastUpdateTime(data); 150 return sync_service_->GetLastUpdateTime(data);
164 } 151 }
165 int CountActiveDevices(const Time now) { 152 int CountActiveDevices(const Time now) {
166 return sync_service_->CountActiveDevices(now); 153 return sync_service_->CountActiveDevices(now);
167 } 154 }
168 void StoreSyncData(const std::string& client_id, 155 void StoreSyncData(const std::string& client_id, const SyncData& sync_data) {
169 const syncer::SyncData& sync_data) {
170 sync_service_->StoreSyncData(client_id, sync_data); 156 sync_service_->StoreSyncData(client_id, sync_data);
171 } 157 }
172 bool IsPulseTimerRunning() { return sync_service_->pulse_timer_.IsRunning(); } 158 bool IsPulseTimerRunning() { return sync_service_->pulse_timer_.IsRunning(); }
173 159
174 // Needs to be created for OneShotTimer to grab the current task runner. 160 // Needs to be created for OneShotTimer to grab the current task runner.
175 base::MessageLoop message_loop_; 161 base::MessageLoop message_loop_;
176 162
177 int num_device_info_changed_callbacks_; 163 int num_device_info_changed_callbacks_;
178 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_; 164 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_;
179 std::unique_ptr<DeviceInfoSyncService> sync_service_; 165 std::unique_ptr<DeviceInfoSyncService> sync_service_;
180 std::unique_ptr<TestChangeProcessor> sync_processor_; 166 std::unique_ptr<TestChangeProcessor> sync_processor_;
181 }; 167 };
182 168
183 namespace { 169 namespace {
184 170
185 // Sync with empty initial data. 171 // Sync with empty initial data.
186 TEST_F(DeviceInfoSyncServiceTest, StartSyncEmptyInitialData) { 172 TEST_F(DeviceInfoSyncServiceTest, StartSyncEmptyInitialData) {
187 EXPECT_FALSE(sync_service_->IsSyncing()); 173 EXPECT_FALSE(sync_service_->IsSyncing());
188 174
189 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 175 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
190 syncer::DEVICE_INFO, SyncDataList(), PassProcessor(), 176 DEVICE_INFO, SyncDataList(), PassProcessor(),
191 CreateAndPassSyncErrorFactory()); 177 CreateAndPassSyncErrorFactory());
192 178
193 EXPECT_TRUE(sync_service_->IsSyncing()); 179 EXPECT_TRUE(sync_service_->IsSyncing());
194 EXPECT_EQ(0, merge_result.num_items_added()); 180 EXPECT_EQ(0, merge_result.num_items_added());
195 EXPECT_EQ(0, merge_result.num_items_modified()); 181 EXPECT_EQ(0, merge_result.num_items_modified());
196 EXPECT_EQ(0, merge_result.num_items_deleted()); 182 EXPECT_EQ(0, merge_result.num_items_deleted());
197 EXPECT_EQ(1, merge_result.num_items_before_association()); 183 EXPECT_EQ(1, merge_result.num_items_before_association());
198 EXPECT_EQ(1, merge_result.num_items_after_association()); 184 EXPECT_EQ(1, merge_result.num_items_after_association());
199 EXPECT_EQ(SyncChange::ACTION_ADD, sync_processor_->change_type_at(0)); 185 EXPECT_EQ(SyncChange::ACTION_ADD, sync_processor_->change_type_at(0));
200 186
201 EXPECT_EQ(1U, sync_processor_->change_list_size()); 187 EXPECT_EQ(1U, sync_processor_->change_list_size());
202 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0)); 188 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
203 189
204 // Should have one device info corresponding to local device info. 190 // Should have one device info corresponding to local device info.
205 EXPECT_EQ(1U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size()); 191 EXPECT_EQ(1U, sync_service_->GetAllSyncData(DEVICE_INFO).size());
206 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); 192 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size());
207 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); 193 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1"));
208 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); 194 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0"));
209 } 195 }
210 196
211 TEST_F(DeviceInfoSyncServiceTest, StopSyncing) { 197 TEST_F(DeviceInfoSyncServiceTest, StopSyncing) {
212 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 198 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
213 syncer::DEVICE_INFO, SyncDataList(), PassProcessor(), 199 DEVICE_INFO, SyncDataList(), PassProcessor(),
214 CreateAndPassSyncErrorFactory()); 200 CreateAndPassSyncErrorFactory());
215 EXPECT_TRUE(sync_service_->IsSyncing()); 201 EXPECT_TRUE(sync_service_->IsSyncing());
216 EXPECT_EQ(1, num_device_info_changed_callbacks_); 202 EXPECT_EQ(1, num_device_info_changed_callbacks_);
217 EXPECT_TRUE(IsPulseTimerRunning()); 203 EXPECT_TRUE(IsPulseTimerRunning());
218 sync_service_->StopSyncing(syncer::DEVICE_INFO); 204 sync_service_->StopSyncing(DEVICE_INFO);
219 EXPECT_FALSE(sync_service_->IsSyncing()); 205 EXPECT_FALSE(sync_service_->IsSyncing());
220 EXPECT_EQ(2, num_device_info_changed_callbacks_); 206 EXPECT_EQ(2, num_device_info_changed_callbacks_);
221 EXPECT_FALSE(IsPulseTimerRunning()); 207 EXPECT_FALSE(IsPulseTimerRunning());
222 } 208 }
223 209
224 // Sync with initial data matching the local device data. 210 // Sync with initial data matching the local device data.
225 TEST_F(DeviceInfoSyncServiceTest, StartSyncMatchingInitialData) { 211 TEST_F(DeviceInfoSyncServiceTest, StartSyncMatchingInitialData) {
226 SyncDataList sync_data; 212 SyncDataList sync_data;
227 AddInitialData(&sync_data, "guid_1", "client_1"); 213 AddInitialData(&sync_data, "guid_1", "client_1");
228 214
229 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 215 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
230 syncer::DEVICE_INFO, sync_data, PassProcessor(), 216 DEVICE_INFO, sync_data, PassProcessor(), CreateAndPassSyncErrorFactory());
231 CreateAndPassSyncErrorFactory());
232 EXPECT_EQ(0, merge_result.num_items_added()); 217 EXPECT_EQ(0, merge_result.num_items_added());
233 EXPECT_EQ(0, merge_result.num_items_modified()); 218 EXPECT_EQ(0, merge_result.num_items_modified());
234 EXPECT_EQ(0, merge_result.num_items_deleted()); 219 EXPECT_EQ(0, merge_result.num_items_deleted());
235 EXPECT_EQ(1, merge_result.num_items_before_association()); 220 EXPECT_EQ(1, merge_result.num_items_before_association());
236 EXPECT_EQ(1, merge_result.num_items_after_association()); 221 EXPECT_EQ(1, merge_result.num_items_after_association());
237 222
238 // No changes expected because the device info matches. 223 // No changes expected because the device info matches.
239 EXPECT_EQ(0U, sync_processor_->change_list_size()); 224 EXPECT_EQ(0U, sync_processor_->change_list_size());
240 225
241 EXPECT_EQ(1U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size()); 226 EXPECT_EQ(1U, sync_service_->GetAllSyncData(DEVICE_INFO).size());
242 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); 227 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size());
243 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); 228 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1"));
244 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); 229 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0"));
245 } 230 }
246 231
247 // Sync with misc initial data. 232 // Sync with misc initial data.
248 TEST_F(DeviceInfoSyncServiceTest, StartSync) { 233 TEST_F(DeviceInfoSyncServiceTest, StartSync) {
249 SyncDataList sync_data; 234 SyncDataList sync_data;
250 AddInitialData(&sync_data, "guid_2", "foo"); 235 AddInitialData(&sync_data, "guid_2", "foo");
251 AddInitialData(&sync_data, "guid_3", "bar"); 236 AddInitialData(&sync_data, "guid_3", "bar");
252 // This guid matches the local device but the client name is different. 237 // This guid matches the local device but the client name is different.
253 AddInitialData(&sync_data, "guid_1", "baz"); 238 AddInitialData(&sync_data, "guid_1", "baz");
254 239
255 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 240 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
256 syncer::DEVICE_INFO, sync_data, PassProcessor(), 241 DEVICE_INFO, sync_data, PassProcessor(), CreateAndPassSyncErrorFactory());
257 CreateAndPassSyncErrorFactory());
258 242
259 EXPECT_EQ(2, merge_result.num_items_added()); 243 EXPECT_EQ(2, merge_result.num_items_added());
260 EXPECT_EQ(1, merge_result.num_items_modified()); 244 EXPECT_EQ(1, merge_result.num_items_modified());
261 EXPECT_EQ(0, merge_result.num_items_deleted()); 245 EXPECT_EQ(0, merge_result.num_items_deleted());
262 EXPECT_EQ(1, merge_result.num_items_before_association()); 246 EXPECT_EQ(1, merge_result.num_items_before_association());
263 EXPECT_EQ(3, merge_result.num_items_after_association()); 247 EXPECT_EQ(3, merge_result.num_items_after_association());
264 248
265 EXPECT_EQ(1U, sync_processor_->change_list_size()); 249 EXPECT_EQ(1U, sync_processor_->change_list_size());
266 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0)); 250 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0));
267 EXPECT_EQ("client_1", sync_processor_->client_name_at(0)); 251 EXPECT_EQ("client_1", sync_processor_->client_name_at(0));
268 252
269 EXPECT_EQ(3U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size()); 253 EXPECT_EQ(3U, sync_service_->GetAllSyncData(DEVICE_INFO).size());
270 EXPECT_EQ(3U, sync_service_->GetAllDeviceInfo().size()); 254 EXPECT_EQ(3U, sync_service_->GetAllDeviceInfo().size());
271 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); 255 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1"));
272 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2")); 256 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2"));
273 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_3")); 257 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_3"));
274 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); 258 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0"));
275 } 259 }
276 260
277 // Process sync change with ACTION_ADD. 261 // Process sync change with ACTION_ADD.
278 // Verify callback. 262 // Verify callback.
279 TEST_F(DeviceInfoSyncServiceTest, ProcessAddChange) { 263 TEST_F(DeviceInfoSyncServiceTest, ProcessAddChange) {
280 EXPECT_EQ(0, num_device_info_changed_callbacks_); 264 EXPECT_EQ(0, num_device_info_changed_callbacks_);
281 265
282 // Start with an empty initial data. 266 // Start with an empty initial data.
283 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 267 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
284 syncer::DEVICE_INFO, SyncDataList(), PassProcessor(), 268 DEVICE_INFO, SyncDataList(), PassProcessor(),
285 CreateAndPassSyncErrorFactory()); 269 CreateAndPassSyncErrorFactory());
286 // There should be only one item corresponding to the local device 270 // There should be only one item corresponding to the local device
287 EXPECT_EQ(1, merge_result.num_items_after_association()); 271 EXPECT_EQ(1, merge_result.num_items_after_association());
288 EXPECT_EQ(1, num_device_info_changed_callbacks_); 272 EXPECT_EQ(1, num_device_info_changed_callbacks_);
289 273
290 // Add a new device info with a non-matching guid. 274 // Add a new device info with a non-matching guid.
291 SyncChangeList change_list; 275 SyncChangeList change_list;
292 AddChange(&change_list, SyncChange::ACTION_ADD, "guid_2", "foo"); 276 AddChange(&change_list, SyncChange::ACTION_ADD, "guid_2", "foo");
293 277
294 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); 278 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
295 EXPECT_FALSE(error.IsSet()); 279 EXPECT_FALSE(error.IsSet());
296 EXPECT_EQ(2, num_device_info_changed_callbacks_); 280 EXPECT_EQ(2, num_device_info_changed_callbacks_);
297 281
298 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size()); 282 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size());
299 283
300 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); 284 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1"));
301 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2")); 285 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2"));
302 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); 286 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0"));
303 } 287 }
304 288
305 // Process multiple sync change with ACTION_UPDATE and ACTION_ADD. 289 // Process multiple sync change with ACTION_UPDATE and ACTION_ADD.
306 // Verify that callback is called multiple times. 290 // Verify that callback is called multiple times.
307 TEST_F(DeviceInfoSyncServiceTest, ProcessMultipleChanges) { 291 TEST_F(DeviceInfoSyncServiceTest, ProcessMultipleChanges) {
308 SyncDataList sync_data; 292 SyncDataList sync_data;
309 AddInitialData(&sync_data, "guid_2", "foo"); 293 AddInitialData(&sync_data, "guid_2", "foo");
310 AddInitialData(&sync_data, "guid_3", "bar"); 294 AddInitialData(&sync_data, "guid_3", "bar");
311 295
312 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 296 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
313 syncer::DEVICE_INFO, sync_data, PassProcessor(), 297 DEVICE_INFO, sync_data, PassProcessor(), CreateAndPassSyncErrorFactory());
314 CreateAndPassSyncErrorFactory());
315 EXPECT_EQ(3, merge_result.num_items_after_association()); 298 EXPECT_EQ(3, merge_result.num_items_after_association());
316 // reset callbacks counter 299 // reset callbacks counter
317 num_device_info_changed_callbacks_ = 0; 300 num_device_info_changed_callbacks_ = 0;
318 301
319 SyncChangeList change_list; 302 SyncChangeList change_list;
320 AddChange(&change_list, SyncChange::ACTION_UPDATE, "guid_2", "foo_2"); 303 AddChange(&change_list, SyncChange::ACTION_UPDATE, "guid_2", "foo_2");
321 304
322 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); 305 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
323 EXPECT_FALSE(error.IsSet()); 306 EXPECT_FALSE(error.IsSet());
324 307
(...skipping 10 matching lines...) Expand all
335 318
336 EXPECT_EQ(2, num_device_info_changed_callbacks_); 319 EXPECT_EQ(2, num_device_info_changed_callbacks_);
337 EXPECT_EQ(4U, sync_service_->GetAllDeviceInfo().size()); 320 EXPECT_EQ(4U, sync_service_->GetAllDeviceInfo().size());
338 EXPECT_EQ("bar_3", sync_service_->GetDeviceInfo("guid_3")->client_name()); 321 EXPECT_EQ("bar_3", sync_service_->GetDeviceInfo("guid_3")->client_name());
339 EXPECT_EQ("baz_4", sync_service_->GetDeviceInfo("guid_4")->client_name()); 322 EXPECT_EQ("baz_4", sync_service_->GetDeviceInfo("guid_4")->client_name());
340 } 323 }
341 324
342 // Process update to the local device info and verify that it is ignored. 325 // Process update to the local device info and verify that it is ignored.
343 TEST_F(DeviceInfoSyncServiceTest, ProcessUpdateChangeMatchingLocalDevice) { 326 TEST_F(DeviceInfoSyncServiceTest, ProcessUpdateChangeMatchingLocalDevice) {
344 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 327 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
345 syncer::DEVICE_INFO, SyncDataList(), PassProcessor(), 328 DEVICE_INFO, SyncDataList(), PassProcessor(),
346 CreateAndPassSyncErrorFactory()); 329 CreateAndPassSyncErrorFactory());
347 EXPECT_EQ(1, merge_result.num_items_after_association()); 330 EXPECT_EQ(1, merge_result.num_items_after_association());
348 // reset callbacks counter 331 // reset callbacks counter
349 num_device_info_changed_callbacks_ = 0; 332 num_device_info_changed_callbacks_ = 0;
350 333
351 SyncChangeList change_list; 334 SyncChangeList change_list;
352 AddChange(&change_list, SyncChange::ACTION_UPDATE, "guid_1", "foo_1"); 335 AddChange(&change_list, SyncChange::ACTION_UPDATE, "guid_1", "foo_1");
353 336
354 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); 337 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
355 EXPECT_FALSE(error.IsSet()); 338 EXPECT_FALSE(error.IsSet());
356 // Callback shouldn't be sent in this case. 339 // Callback shouldn't be sent in this case.
357 EXPECT_EQ(0, num_device_info_changed_callbacks_); 340 EXPECT_EQ(0, num_device_info_changed_callbacks_);
358 // Should still have the old local device Info. 341 // Should still have the old local device Info.
359 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); 342 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size());
360 EXPECT_EQ("client_1", sync_service_->GetDeviceInfo("guid_1")->client_name()); 343 EXPECT_EQ("client_1", sync_service_->GetDeviceInfo("guid_1")->client_name());
361 } 344 }
362 345
363 // Process sync change with ACTION_DELETE. 346 // Process sync change with ACTION_DELETE.
364 TEST_F(DeviceInfoSyncServiceTest, ProcessDeleteChange) { 347 TEST_F(DeviceInfoSyncServiceTest, ProcessDeleteChange) {
365 SyncDataList sync_data; 348 SyncDataList sync_data;
366 AddInitialData(&sync_data, "guid_2", "foo"); 349 AddInitialData(&sync_data, "guid_2", "foo");
367 AddInitialData(&sync_data, "guid_3", "bar"); 350 AddInitialData(&sync_data, "guid_3", "bar");
368 351
369 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 352 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
370 syncer::DEVICE_INFO, sync_data, PassProcessor(), 353 DEVICE_INFO, sync_data, PassProcessor(), CreateAndPassSyncErrorFactory());
371 CreateAndPassSyncErrorFactory());
372 EXPECT_EQ(3, merge_result.num_items_after_association()); 354 EXPECT_EQ(3, merge_result.num_items_after_association());
373 // reset callbacks counter 355 // reset callbacks counter
374 num_device_info_changed_callbacks_ = 0; 356 num_device_info_changed_callbacks_ = 0;
375 357
376 SyncChangeList change_list; 358 SyncChangeList change_list;
377 AddChange(&change_list, SyncChange::ACTION_DELETE, "guid_2", "foo_2"); 359 AddChange(&change_list, SyncChange::ACTION_DELETE, "guid_2", "foo_2");
378 360
379 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); 361 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
380 EXPECT_FALSE(error.IsSet()); 362 EXPECT_FALSE(error.IsSet());
381 363
382 EXPECT_EQ(1, num_device_info_changed_callbacks_); 364 EXPECT_EQ(1, num_device_info_changed_callbacks_);
383 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size()); 365 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size());
384 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_2")); 366 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_2"));
385 } 367 }
386 368
387 // Process sync change with unexpected action. 369 // Process sync change with unexpected action.
388 TEST_F(DeviceInfoSyncServiceTest, ProcessInvalidChange) { 370 TEST_F(DeviceInfoSyncServiceTest, ProcessInvalidChange) {
389 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 371 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
390 syncer::DEVICE_INFO, SyncDataList(), PassProcessor(), 372 DEVICE_INFO, SyncDataList(), PassProcessor(),
391 CreateAndPassSyncErrorFactory()); 373 CreateAndPassSyncErrorFactory());
392 EXPECT_EQ(1, merge_result.num_items_after_association()); 374 EXPECT_EQ(1, merge_result.num_items_after_association());
393 // reset callbacks counter 375 // reset callbacks counter
394 num_device_info_changed_callbacks_ = 0; 376 num_device_info_changed_callbacks_ = 0;
395 377
396 SyncChangeList change_list; 378 SyncChangeList change_list;
397 AddChange(&change_list, (SyncChange::SyncChangeType)100, "guid_2", "foo_2"); 379 AddChange(&change_list, (SyncChange::SyncChangeType)100, "guid_2", "foo_2");
398 380
399 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); 381 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
400 EXPECT_TRUE(error.IsSet()); 382 EXPECT_TRUE(error.IsSet());
401 383
402 // The number of callback should still be zero. 384 // The number of callback should still be zero.
403 EXPECT_EQ(0, num_device_info_changed_callbacks_); 385 EXPECT_EQ(0, num_device_info_changed_callbacks_);
404 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); 386 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size());
405 } 387 }
406 388
407 // Process sync change after unsubscribing from notifications. 389 // Process sync change after unsubscribing from notifications.
408 TEST_F(DeviceInfoSyncServiceTest, ProcessChangesAfterUnsubscribing) { 390 TEST_F(DeviceInfoSyncServiceTest, ProcessChangesAfterUnsubscribing) {
409 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 391 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
410 syncer::DEVICE_INFO, SyncDataList(), PassProcessor(), 392 DEVICE_INFO, SyncDataList(), PassProcessor(),
411 CreateAndPassSyncErrorFactory()); 393 CreateAndPassSyncErrorFactory());
412 EXPECT_EQ(1, merge_result.num_items_after_association()); 394 EXPECT_EQ(1, merge_result.num_items_after_association());
413 // reset callbacks counter 395 // reset callbacks counter
414 num_device_info_changed_callbacks_ = 0; 396 num_device_info_changed_callbacks_ = 0;
415 397
416 SyncChangeList change_list; 398 SyncChangeList change_list;
417 AddChange(&change_list, SyncChange::ACTION_ADD, "guid_2", "foo_2"); 399 AddChange(&change_list, SyncChange::ACTION_ADD, "guid_2", "foo_2");
418 400
419 // Unsubscribe observer before processing changes. 401 // Unsubscribe observer before processing changes.
420 sync_service_->RemoveObserver(this); 402 sync_service_->RemoveObserver(this);
421 403
422 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); 404 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list);
423 EXPECT_FALSE(error.IsSet()); 405 EXPECT_FALSE(error.IsSet());
424 406
425 // The number of callback should still be zero. 407 // The number of callback should still be zero.
426 EXPECT_EQ(0, num_device_info_changed_callbacks_); 408 EXPECT_EQ(0, num_device_info_changed_callbacks_);
427 } 409 }
428 410
429 // While the initial data will match the current device, the last modified time 411 // While the initial data will match the current device, the last modified time
430 // should be greater than the threshold and cause an update anyways. 412 // should be greater than the threshold and cause an update anyways.
431 TEST_F(DeviceInfoSyncServiceTest, StartSyncMatchingButStale) { 413 TEST_F(DeviceInfoSyncServiceTest, StartSyncMatchingButStale) {
432 SyncDataList sync_data; 414 SyncDataList sync_data;
433 sync_data.push_back(CreateRemoteData("guid_1", "foo_1", Time())); 415 sync_data.push_back(CreateRemoteData("guid_1", "foo_1", Time()));
434 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( 416 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing(
435 syncer::DEVICE_INFO, sync_data, PassProcessor(), 417 DEVICE_INFO, sync_data, PassProcessor(), CreateAndPassSyncErrorFactory());
436 CreateAndPassSyncErrorFactory());
437 418
438 EXPECT_EQ(1U, sync_processor_->change_list_size()); 419 EXPECT_EQ(1U, sync_processor_->change_list_size());
439 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0)); 420 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0));
440 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0)); 421 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0));
441 EXPECT_EQ("client_1", sync_processor_->client_name_at(0)); 422 EXPECT_EQ("client_1", sync_processor_->client_name_at(0));
442 } 423 }
443 424
444 TEST_F(DeviceInfoSyncServiceTest, GetLastUpdateTime) { 425 TEST_F(DeviceInfoSyncServiceTest, GetLastUpdateTime) {
445 Time time1(Time() + TimeDelta::FromDays(1)); 426 Time time1(Time() + TimeDelta::FromDays(1));
446 Time time2(Time() + TimeDelta::FromDays(2)); 427 Time time2(Time() + TimeDelta::FromDays(2));
447 428
448 SyncData localA( 429 SyncData localA(
449 SyncData::CreateLocalData("a", "a", CreateEntitySpecifics("a", "a"))); 430 SyncData::CreateLocalData("a", "a", CreateEntitySpecifics("a", "a")));
450 431
451 EntitySpecifics entityB(CreateEntitySpecifics("b", "b")); 432 EntitySpecifics entityB(CreateEntitySpecifics("b", "b"));
452 entityB.mutable_device_info()->set_last_updated_timestamp( 433 entityB.mutable_device_info()->set_last_updated_timestamp(
453 syncer::TimeToProtoTime(time1)); 434 TimeToProtoTime(time1));
454 SyncData localB(SyncData::CreateLocalData("b", "b", entityB)); 435 SyncData localB(SyncData::CreateLocalData("b", "b", entityB));
455 436
456 SyncData remoteC(SyncData::CreateRemoteData( 437 SyncData remoteC(SyncData::CreateRemoteData(
457 1, CreateEntitySpecifics("c", "c"), time2, AttachmentIdList(), 438 1, CreateEntitySpecifics("c", "c"), time2, AttachmentIdList(),
458 AttachmentServiceProxyForTest::Create())); 439 AttachmentServiceProxyForTest::Create()));
459 440
460 EntitySpecifics entityD(CreateEntitySpecifics("d", "d")); 441 EntitySpecifics entityD(CreateEntitySpecifics("d", "d"));
461 entityD.mutable_device_info()->set_last_updated_timestamp( 442 entityD.mutable_device_info()->set_last_updated_timestamp(
462 syncer::TimeToProtoTime(time1)); 443 TimeToProtoTime(time1));
463 SyncData remoteD( 444 SyncData remoteD(
464 SyncData::CreateRemoteData(1, entityD, time2, AttachmentIdList(), 445 SyncData::CreateRemoteData(1, entityD, time2, AttachmentIdList(),
465 AttachmentServiceProxyForTest::Create())); 446 AttachmentServiceProxyForTest::Create()));
466 447
467 EXPECT_EQ(Time(), GetLastUpdateTime(localA)); 448 EXPECT_EQ(Time(), GetLastUpdateTime(localA));
468 EXPECT_EQ(time1, GetLastUpdateTime(localB)); 449 EXPECT_EQ(time1, GetLastUpdateTime(localB));
469 EXPECT_EQ(time2, GetLastUpdateTime(remoteC)); 450 EXPECT_EQ(time2, GetLastUpdateTime(remoteC));
470 EXPECT_EQ(time1, GetLastUpdateTime(remoteD)); 451 EXPECT_EQ(time1, GetLastUpdateTime(remoteD));
471 } 452 }
472 453
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesLocalStale) { 552 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesLocalStale) {
572 sync_pb::EntitySpecifics entity; 553 sync_pb::EntitySpecifics entity;
573 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); 554 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info();
574 specifics.set_cache_guid("stale"); 555 specifics.set_cache_guid("stale");
575 StoreSyncData("stale", SyncData::CreateLocalData("stale", "stale", entity)); 556 StoreSyncData("stale", SyncData::CreateLocalData("stale", "stale", entity));
576 EXPECT_EQ(0, CountActiveDevices(Time() + DeviceInfoUtil::kActiveThreshold)); 557 EXPECT_EQ(0, CountActiveDevices(Time() + DeviceInfoUtil::kActiveThreshold));
577 } 558 }
578 559
579 } // namespace 560 } // namespace
580 561
581 } // namespace sync_driver 562 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/device_info/device_info_sync_service.cc ('k') | components/sync/device_info/device_info_tracker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698