OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/sync_driver/device_info_sync_service.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <string> | |
11 | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "components/sync/api/sync_change.h" | |
14 #include "components/sync/api/sync_change_processor.h" | |
15 #include "components/sync/api/sync_change_processor_wrapper_for_test.h" | |
16 #include "components/sync/api/sync_error_factory_mock.h" | |
17 #include "components/sync/base/time.h" | |
18 #include "components/sync/core/attachments/attachment_service_proxy_for_test.h" | |
19 #include "components/sync_driver/device_info_util.h" | |
20 #include "components/sync_driver/local_device_info_provider_mock.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 using base::Time; | |
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; | |
39 | |
40 namespace sync_driver { | |
41 | |
42 namespace { | |
43 | |
44 class TestChangeProcessor : public SyncChangeProcessor { | |
45 public: | |
46 TestChangeProcessor() {} | |
47 ~TestChangeProcessor() override {} | |
48 | |
49 // SyncChangeProcessor implementation. | |
50 // Store a copy of all the changes passed in so we can examine them later. | |
51 SyncError ProcessSyncChanges(const tracked_objects::Location& from_here, | |
52 const SyncChangeList& change_list) override { | |
53 change_list_ = change_list; | |
54 return SyncError(); | |
55 } | |
56 | |
57 // This method isn't used in these tests. | |
58 SyncDataList GetAllSyncData(ModelType type) const override { | |
59 return SyncDataList(); | |
60 } | |
61 | |
62 size_t change_list_size() const { return change_list_.size(); } | |
63 | |
64 SyncChange::SyncChangeType change_type_at(size_t index) const { | |
65 CHECK_LT(index, change_list_size()); | |
66 return change_list_[index].change_type(); | |
67 } | |
68 | |
69 const sync_pb::DeviceInfoSpecifics& device_info_at(size_t index) const { | |
70 CHECK_LT(index, change_list_size()); | |
71 return change_list_[index].sync_data().GetSpecifics().device_info(); | |
72 } | |
73 | |
74 const std::string& cache_guid_at(size_t index) const { | |
75 return device_info_at(index).cache_guid(); | |
76 } | |
77 | |
78 const std::string& client_name_at(size_t index) const { | |
79 return device_info_at(index).client_name(); | |
80 } | |
81 | |
82 private: | |
83 SyncChangeList change_list_; | |
84 }; | |
85 | |
86 } // namespace | |
87 | |
88 class DeviceInfoSyncServiceTest : public testing::Test, | |
89 public DeviceInfoTracker::Observer { | |
90 public: | |
91 DeviceInfoSyncServiceTest() : num_device_info_changed_callbacks_(0) {} | |
92 ~DeviceInfoSyncServiceTest() override {} | |
93 | |
94 void SetUp() override { | |
95 local_device_.reset(new LocalDeviceInfoProviderMock( | |
96 "guid_1", | |
97 "client_1", | |
98 "Chromium 10k", | |
99 "Chrome 10k", | |
100 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | |
101 "device_id")); | |
102 sync_service_.reset(new DeviceInfoSyncService(local_device_.get())); | |
103 sync_processor_.reset(new TestChangeProcessor()); | |
104 // Register observer | |
105 sync_service_->AddObserver(this); | |
106 } | |
107 | |
108 void TearDown() override { sync_service_->RemoveObserver(this); } | |
109 | |
110 void OnDeviceInfoChange() override { num_device_info_changed_callbacks_++; } | |
111 | |
112 std::unique_ptr<SyncChangeProcessor> PassProcessor() { | |
113 return std::unique_ptr<SyncChangeProcessor>( | |
114 new SyncChangeProcessorWrapperForTest(sync_processor_.get())); | |
115 } | |
116 | |
117 std::unique_ptr<SyncErrorFactory> CreateAndPassSyncErrorFactory() { | |
118 return std::unique_ptr<SyncErrorFactory>(new SyncErrorFactoryMock()); | |
119 } | |
120 | |
121 sync_pb::EntitySpecifics CreateEntitySpecifics( | |
122 const std::string& client_id, | |
123 const std::string& client_name) { | |
124 sync_pb::EntitySpecifics entity; | |
125 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); | |
126 | |
127 specifics.set_cache_guid(client_id); | |
128 specifics.set_client_name(client_name); | |
129 specifics.set_chrome_version("Chromium 10k"); | |
130 specifics.set_sync_user_agent("Chrome 10k"); | |
131 specifics.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX); | |
132 specifics.set_signin_scoped_device_id("device_id"); | |
133 return entity; | |
134 } | |
135 | |
136 // Default |last_updated_timestamp| to now to avoid pulse update on merge. | |
137 SyncData CreateRemoteData(const std::string& client_id, | |
138 const std::string& client_name, | |
139 Time last_updated_timestamp = Time::Now()) { | |
140 sync_pb::EntitySpecifics entity( | |
141 CreateEntitySpecifics(client_id, client_name)); | |
142 entity.mutable_device_info()->set_last_updated_timestamp( | |
143 syncer::TimeToProtoTime(last_updated_timestamp)); | |
144 return SyncData::CreateRemoteData(1, entity, Time(), AttachmentIdList(), | |
145 AttachmentServiceProxyForTest::Create()); | |
146 } | |
147 | |
148 void AddInitialData(SyncDataList* sync_data_list, | |
149 const std::string& client_id, | |
150 const std::string& client_name) { | |
151 SyncData sync_data = CreateRemoteData(client_id, client_name); | |
152 sync_data_list->push_back(sync_data); | |
153 } | |
154 | |
155 void AddChange(SyncChangeList* change_list, | |
156 SyncChange::SyncChangeType change_type, | |
157 const std::string& client_id, | |
158 const std::string& client_name) { | |
159 SyncData sync_data = CreateRemoteData(client_id, client_name); | |
160 SyncChange sync_change(FROM_HERE, change_type, sync_data); | |
161 change_list->push_back(sync_change); | |
162 } | |
163 | |
164 protected: | |
165 // Private method wrappers through friend class. | |
166 Time GetLastUpdateTime(const syncer::SyncData& data) { | |
167 return sync_service_->GetLastUpdateTime(data); | |
168 } | |
169 int CountActiveDevices(const Time now) { | |
170 return sync_service_->CountActiveDevices(now); | |
171 } | |
172 void StoreSyncData(const std::string& client_id, | |
173 const syncer::SyncData& sync_data) { | |
174 sync_service_->StoreSyncData(client_id, sync_data); | |
175 } | |
176 bool IsPulseTimerRunning() { return sync_service_->pulse_timer_.IsRunning(); } | |
177 | |
178 // Needs to be created for OneShotTimer to grab the current task runner. | |
179 base::MessageLoop message_loop_; | |
180 | |
181 int num_device_info_changed_callbacks_; | |
182 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_; | |
183 std::unique_ptr<DeviceInfoSyncService> sync_service_; | |
184 std::unique_ptr<TestChangeProcessor> sync_processor_; | |
185 }; | |
186 | |
187 namespace { | |
188 | |
189 // Sync with empty initial data. | |
190 TEST_F(DeviceInfoSyncServiceTest, StartSyncEmptyInitialData) { | |
191 EXPECT_FALSE(sync_service_->IsSyncing()); | |
192 | |
193 SyncMergeResult merge_result = | |
194 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
195 SyncDataList(), | |
196 PassProcessor(), | |
197 CreateAndPassSyncErrorFactory()); | |
198 | |
199 EXPECT_TRUE(sync_service_->IsSyncing()); | |
200 EXPECT_EQ(0, merge_result.num_items_added()); | |
201 EXPECT_EQ(0, merge_result.num_items_modified()); | |
202 EXPECT_EQ(0, merge_result.num_items_deleted()); | |
203 EXPECT_EQ(1, merge_result.num_items_before_association()); | |
204 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
205 EXPECT_EQ(SyncChange::ACTION_ADD, sync_processor_->change_type_at(0)); | |
206 | |
207 EXPECT_EQ(1U, sync_processor_->change_list_size()); | |
208 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0)); | |
209 | |
210 // Should have one device info corresponding to local device info. | |
211 EXPECT_EQ(1U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size()); | |
212 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); | |
213 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); | |
214 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); | |
215 } | |
216 | |
217 TEST_F(DeviceInfoSyncServiceTest, StopSyncing) { | |
218 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( | |
219 syncer::DEVICE_INFO, SyncDataList(), PassProcessor(), | |
220 CreateAndPassSyncErrorFactory()); | |
221 EXPECT_TRUE(sync_service_->IsSyncing()); | |
222 EXPECT_EQ(1, num_device_info_changed_callbacks_); | |
223 EXPECT_TRUE(IsPulseTimerRunning()); | |
224 sync_service_->StopSyncing(syncer::DEVICE_INFO); | |
225 EXPECT_FALSE(sync_service_->IsSyncing()); | |
226 EXPECT_EQ(2, num_device_info_changed_callbacks_); | |
227 EXPECT_FALSE(IsPulseTimerRunning()); | |
228 } | |
229 | |
230 // Sync with initial data matching the local device data. | |
231 TEST_F(DeviceInfoSyncServiceTest, StartSyncMatchingInitialData) { | |
232 SyncDataList sync_data; | |
233 AddInitialData(&sync_data, "guid_1", "client_1"); | |
234 | |
235 SyncMergeResult merge_result = | |
236 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
237 sync_data, | |
238 PassProcessor(), | |
239 CreateAndPassSyncErrorFactory()); | |
240 EXPECT_EQ(0, merge_result.num_items_added()); | |
241 EXPECT_EQ(0, merge_result.num_items_modified()); | |
242 EXPECT_EQ(0, merge_result.num_items_deleted()); | |
243 EXPECT_EQ(1, merge_result.num_items_before_association()); | |
244 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
245 | |
246 // No changes expected because the device info matches. | |
247 EXPECT_EQ(0U, sync_processor_->change_list_size()); | |
248 | |
249 EXPECT_EQ(1U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size()); | |
250 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); | |
251 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); | |
252 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); | |
253 } | |
254 | |
255 // Sync with misc initial data. | |
256 TEST_F(DeviceInfoSyncServiceTest, StartSync) { | |
257 SyncDataList sync_data; | |
258 AddInitialData(&sync_data, "guid_2", "foo"); | |
259 AddInitialData(&sync_data, "guid_3", "bar"); | |
260 // This guid matches the local device but the client name is different. | |
261 AddInitialData(&sync_data, "guid_1", "baz"); | |
262 | |
263 SyncMergeResult merge_result = | |
264 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
265 sync_data, | |
266 PassProcessor(), | |
267 CreateAndPassSyncErrorFactory()); | |
268 | |
269 EXPECT_EQ(2, merge_result.num_items_added()); | |
270 EXPECT_EQ(1, merge_result.num_items_modified()); | |
271 EXPECT_EQ(0, merge_result.num_items_deleted()); | |
272 EXPECT_EQ(1, merge_result.num_items_before_association()); | |
273 EXPECT_EQ(3, merge_result.num_items_after_association()); | |
274 | |
275 EXPECT_EQ(1U, sync_processor_->change_list_size()); | |
276 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0)); | |
277 EXPECT_EQ("client_1", sync_processor_->client_name_at(0)); | |
278 | |
279 EXPECT_EQ(3U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size()); | |
280 EXPECT_EQ(3U, sync_service_->GetAllDeviceInfo().size()); | |
281 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); | |
282 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2")); | |
283 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_3")); | |
284 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); | |
285 } | |
286 | |
287 // Process sync change with ACTION_ADD. | |
288 // Verify callback. | |
289 TEST_F(DeviceInfoSyncServiceTest, ProcessAddChange) { | |
290 EXPECT_EQ(0, num_device_info_changed_callbacks_); | |
291 | |
292 // Start with an empty initial data. | |
293 SyncMergeResult merge_result = | |
294 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
295 SyncDataList(), | |
296 PassProcessor(), | |
297 CreateAndPassSyncErrorFactory()); | |
298 // There should be only one item corresponding to the local device | |
299 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
300 EXPECT_EQ(1, num_device_info_changed_callbacks_); | |
301 | |
302 // Add a new device info with a non-matching guid. | |
303 SyncChangeList change_list; | |
304 AddChange(&change_list, SyncChange::ACTION_ADD, "guid_2", "foo"); | |
305 | |
306 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
307 EXPECT_FALSE(error.IsSet()); | |
308 EXPECT_EQ(2, num_device_info_changed_callbacks_); | |
309 | |
310 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size()); | |
311 | |
312 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); | |
313 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2")); | |
314 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); | |
315 } | |
316 | |
317 // Process multiple sync change with ACTION_UPDATE and ACTION_ADD. | |
318 // Verify that callback is called multiple times. | |
319 TEST_F(DeviceInfoSyncServiceTest, ProcessMultipleChanges) { | |
320 SyncDataList sync_data; | |
321 AddInitialData(&sync_data, "guid_2", "foo"); | |
322 AddInitialData(&sync_data, "guid_3", "bar"); | |
323 | |
324 SyncMergeResult merge_result = | |
325 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
326 sync_data, | |
327 PassProcessor(), | |
328 CreateAndPassSyncErrorFactory()); | |
329 EXPECT_EQ(3, merge_result.num_items_after_association()); | |
330 // reset callbacks counter | |
331 num_device_info_changed_callbacks_ = 0; | |
332 | |
333 SyncChangeList change_list; | |
334 AddChange(&change_list, SyncChange::ACTION_UPDATE, "guid_2", "foo_2"); | |
335 | |
336 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
337 EXPECT_FALSE(error.IsSet()); | |
338 | |
339 EXPECT_EQ(1, num_device_info_changed_callbacks_); | |
340 EXPECT_EQ(3U, sync_service_->GetAllDeviceInfo().size()); | |
341 EXPECT_EQ("foo_2", sync_service_->GetDeviceInfo("guid_2")->client_name()); | |
342 | |
343 change_list.clear(); | |
344 AddChange(&change_list, SyncChange::ACTION_UPDATE, "guid_3", "bar_3"); | |
345 AddChange(&change_list, SyncChange::ACTION_ADD, "guid_4", "baz_4"); | |
346 | |
347 error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
348 EXPECT_FALSE(error.IsSet()); | |
349 | |
350 EXPECT_EQ(2, num_device_info_changed_callbacks_); | |
351 EXPECT_EQ(4U, sync_service_->GetAllDeviceInfo().size()); | |
352 EXPECT_EQ("bar_3", sync_service_->GetDeviceInfo("guid_3")->client_name()); | |
353 EXPECT_EQ("baz_4", sync_service_->GetDeviceInfo("guid_4")->client_name()); | |
354 } | |
355 | |
356 // Process update to the local device info and verify that it is ignored. | |
357 TEST_F(DeviceInfoSyncServiceTest, ProcessUpdateChangeMatchingLocalDevice) { | |
358 SyncMergeResult merge_result = | |
359 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
360 SyncDataList(), | |
361 PassProcessor(), | |
362 CreateAndPassSyncErrorFactory()); | |
363 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
364 // reset callbacks counter | |
365 num_device_info_changed_callbacks_ = 0; | |
366 | |
367 SyncChangeList change_list; | |
368 AddChange(&change_list, SyncChange::ACTION_UPDATE, "guid_1", "foo_1"); | |
369 | |
370 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
371 EXPECT_FALSE(error.IsSet()); | |
372 // Callback shouldn't be sent in this case. | |
373 EXPECT_EQ(0, num_device_info_changed_callbacks_); | |
374 // Should still have the old local device Info. | |
375 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); | |
376 EXPECT_EQ("client_1", sync_service_->GetDeviceInfo("guid_1")->client_name()); | |
377 } | |
378 | |
379 // Process sync change with ACTION_DELETE. | |
380 TEST_F(DeviceInfoSyncServiceTest, ProcessDeleteChange) { | |
381 SyncDataList sync_data; | |
382 AddInitialData(&sync_data, "guid_2", "foo"); | |
383 AddInitialData(&sync_data, "guid_3", "bar"); | |
384 | |
385 SyncMergeResult merge_result = | |
386 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
387 sync_data, | |
388 PassProcessor(), | |
389 CreateAndPassSyncErrorFactory()); | |
390 EXPECT_EQ(3, merge_result.num_items_after_association()); | |
391 // reset callbacks counter | |
392 num_device_info_changed_callbacks_ = 0; | |
393 | |
394 SyncChangeList change_list; | |
395 AddChange(&change_list, SyncChange::ACTION_DELETE, "guid_2", "foo_2"); | |
396 | |
397 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
398 EXPECT_FALSE(error.IsSet()); | |
399 | |
400 EXPECT_EQ(1, num_device_info_changed_callbacks_); | |
401 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size()); | |
402 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_2")); | |
403 } | |
404 | |
405 // Process sync change with unexpected action. | |
406 TEST_F(DeviceInfoSyncServiceTest, ProcessInvalidChange) { | |
407 SyncMergeResult merge_result = | |
408 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
409 SyncDataList(), | |
410 PassProcessor(), | |
411 CreateAndPassSyncErrorFactory()); | |
412 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
413 // reset callbacks counter | |
414 num_device_info_changed_callbacks_ = 0; | |
415 | |
416 SyncChangeList change_list; | |
417 AddChange(&change_list, (SyncChange::SyncChangeType)100, "guid_2", "foo_2"); | |
418 | |
419 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
420 EXPECT_TRUE(error.IsSet()); | |
421 | |
422 // The number of callback should still be zero. | |
423 EXPECT_EQ(0, num_device_info_changed_callbacks_); | |
424 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); | |
425 } | |
426 | |
427 // Process sync change after unsubscribing from notifications. | |
428 TEST_F(DeviceInfoSyncServiceTest, ProcessChangesAfterUnsubscribing) { | |
429 SyncMergeResult merge_result = | |
430 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
431 SyncDataList(), | |
432 PassProcessor(), | |
433 CreateAndPassSyncErrorFactory()); | |
434 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
435 // reset callbacks counter | |
436 num_device_info_changed_callbacks_ = 0; | |
437 | |
438 SyncChangeList change_list; | |
439 AddChange(&change_list, SyncChange::ACTION_ADD, "guid_2", "foo_2"); | |
440 | |
441 // Unsubscribe observer before processing changes. | |
442 sync_service_->RemoveObserver(this); | |
443 | |
444 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
445 EXPECT_FALSE(error.IsSet()); | |
446 | |
447 // The number of callback should still be zero. | |
448 EXPECT_EQ(0, num_device_info_changed_callbacks_); | |
449 } | |
450 | |
451 // While the initial data will match the current device, the last modified time | |
452 // should be greater than the threshold and cause an update anyways. | |
453 TEST_F(DeviceInfoSyncServiceTest, StartSyncMatchingButStale) { | |
454 SyncDataList sync_data; | |
455 sync_data.push_back(CreateRemoteData("guid_1", "foo_1", Time())); | |
456 SyncMergeResult merge_result = sync_service_->MergeDataAndStartSyncing( | |
457 syncer::DEVICE_INFO, sync_data, PassProcessor(), | |
458 CreateAndPassSyncErrorFactory()); | |
459 | |
460 EXPECT_EQ(1U, sync_processor_->change_list_size()); | |
461 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0)); | |
462 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0)); | |
463 EXPECT_EQ("client_1", sync_processor_->client_name_at(0)); | |
464 } | |
465 | |
466 TEST_F(DeviceInfoSyncServiceTest, GetLastUpdateTime) { | |
467 Time time1(Time() + TimeDelta::FromDays(1)); | |
468 Time time2(Time() + TimeDelta::FromDays(2)); | |
469 | |
470 SyncData localA( | |
471 SyncData::CreateLocalData("a", "a", CreateEntitySpecifics("a", "a"))); | |
472 | |
473 EntitySpecifics entityB(CreateEntitySpecifics("b", "b")); | |
474 entityB.mutable_device_info()->set_last_updated_timestamp( | |
475 syncer::TimeToProtoTime(time1)); | |
476 SyncData localB(SyncData::CreateLocalData("b", "b", entityB)); | |
477 | |
478 SyncData remoteC(SyncData::CreateRemoteData( | |
479 1, CreateEntitySpecifics("c", "c"), time2, AttachmentIdList(), | |
480 AttachmentServiceProxyForTest::Create())); | |
481 | |
482 EntitySpecifics entityD(CreateEntitySpecifics("d", "d")); | |
483 entityD.mutable_device_info()->set_last_updated_timestamp( | |
484 syncer::TimeToProtoTime(time1)); | |
485 SyncData remoteD( | |
486 SyncData::CreateRemoteData(1, entityD, time2, AttachmentIdList(), | |
487 AttachmentServiceProxyForTest::Create())); | |
488 | |
489 EXPECT_EQ(Time(), GetLastUpdateTime(localA)); | |
490 EXPECT_EQ(time1, GetLastUpdateTime(localB)); | |
491 EXPECT_EQ(time2, GetLastUpdateTime(remoteC)); | |
492 EXPECT_EQ(time1, GetLastUpdateTime(remoteD)); | |
493 } | |
494 | |
495 // Verifies the number of active devices is 0 when there is no data. | |
496 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesNone) { | |
497 EXPECT_EQ(0, CountActiveDevices(Time())); | |
498 } | |
499 | |
500 // Verifies the number of active devices when we have one active device info. | |
501 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesOneActive) { | |
502 StoreSyncData("active", CreateRemoteData("active", "active", Time())); | |
503 EXPECT_EQ( | |
504 1, CountActiveDevices(Time() + (DeviceInfoUtil::kActiveThreshold / 2))); | |
505 } | |
506 | |
507 // Verifies the number of active devices when we have one stale that hasn't been | |
508 // updated for exactly the threshold is considered stale. | |
509 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesExactlyStale) { | |
510 StoreSyncData("stale", CreateRemoteData("stale", "stale", Time())); | |
511 EXPECT_EQ(0, CountActiveDevices(Time() + DeviceInfoUtil::kActiveThreshold)); | |
512 } | |
513 | |
514 // Verifies the number of active devices when we have a mix of active and stale | |
515 // device infos. | |
516 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesManyMix) { | |
517 StoreSyncData("stale", CreateRemoteData("stale", "stale", Time())); | |
518 StoreSyncData("active1", CreateRemoteData( | |
519 "active1", "active1", | |
520 Time() + DeviceInfoUtil::kActiveThreshold / 2)); | |
521 StoreSyncData("active2", | |
522 CreateRemoteData("active2", "active2", | |
523 Time() + DeviceInfoUtil::kActiveThreshold)); | |
524 EXPECT_EQ(2, CountActiveDevices(Time() + DeviceInfoUtil::kActiveThreshold)); | |
525 } | |
526 | |
527 // Verifies the number of active devices when we have many that are stale. | |
528 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesManyStale) { | |
529 StoreSyncData("stale1", CreateRemoteData("stale1", "stale1", Time())); | |
530 StoreSyncData("stale2", | |
531 CreateRemoteData("stale2", "stale2", | |
532 Time() + DeviceInfoUtil::kActiveThreshold)); | |
533 StoreSyncData("stale3", CreateRemoteData( | |
534 "stale3", "stale3", | |
535 Time() + (DeviceInfoUtil::kActiveThreshold * 2))); | |
536 EXPECT_EQ( | |
537 0, CountActiveDevices(Time() + (DeviceInfoUtil::kActiveThreshold * 3))); | |
538 } | |
539 | |
540 // Verifies the number of active devices when we have devices that claim to have | |
541 // been updated in the future. | |
542 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesFuture) { | |
543 StoreSyncData("now", | |
544 CreateRemoteData("now", "now", | |
545 Time() + DeviceInfoUtil::kActiveThreshold)); | |
546 StoreSyncData( | |
547 "future", | |
548 CreateRemoteData("future", "future", | |
549 Time() + (DeviceInfoUtil::kActiveThreshold * 10))); | |
550 EXPECT_EQ(2, CountActiveDevices(Time() + DeviceInfoUtil::kActiveThreshold)); | |
551 } | |
552 | |
553 // Verifies the number of active devices when they don't have an updated time | |
554 // set, and fallback to checking the SyncData's last modified time. | |
555 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesModifiedTime) { | |
556 sync_pb::EntitySpecifics stale_entity; | |
557 sync_pb::DeviceInfoSpecifics& stale_specifics = | |
558 *stale_entity.mutable_device_info(); | |
559 stale_specifics.set_cache_guid("stale"); | |
560 StoreSyncData("stale", SyncData::CreateRemoteData( | |
561 1, stale_entity, Time(), AttachmentIdList(), | |
562 AttachmentServiceProxyForTest::Create())); | |
563 | |
564 sync_pb::EntitySpecifics active_entity; | |
565 sync_pb::DeviceInfoSpecifics& active_specifics = | |
566 *active_entity.mutable_device_info(); | |
567 active_specifics.set_cache_guid("active"); | |
568 StoreSyncData( | |
569 "active", | |
570 SyncData::CreateRemoteData( | |
571 1, active_entity, Time() + (DeviceInfoUtil::kActiveThreshold / 2), | |
572 AttachmentIdList(), AttachmentServiceProxyForTest::Create())); | |
573 | |
574 EXPECT_EQ(1, CountActiveDevices(Time() + DeviceInfoUtil::kActiveThreshold)); | |
575 } | |
576 | |
577 // Verifies the number of active devices when they don't have an updated time | |
578 // and they're not remote, which means we cannot use SyncData's last modified | |
579 // time. If now is close to uninitialized time, should still be active. | |
580 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesLocalActive) { | |
581 sync_pb::EntitySpecifics entity; | |
582 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); | |
583 specifics.set_cache_guid("active"); | |
584 StoreSyncData("active", | |
585 SyncData::CreateLocalData("active", "active", entity)); | |
586 EXPECT_EQ( | |
587 1, CountActiveDevices(Time() + (DeviceInfoUtil::kActiveThreshold / 2))); | |
588 } | |
589 | |
590 // Verifies the number of active devices when they don't have an updated time | |
591 // and they're not remote, which means we cannot use SyncData's last modified | |
592 // time. If now is far from uninitialized time, should be stale. | |
593 TEST_F(DeviceInfoSyncServiceTest, CountActiveDevicesLocalStale) { | |
594 sync_pb::EntitySpecifics entity; | |
595 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); | |
596 specifics.set_cache_guid("stale"); | |
597 StoreSyncData("stale", SyncData::CreateLocalData("stale", "stale", entity)); | |
598 EXPECT_EQ(0, CountActiveDevices(Time() + DeviceInfoUtil::kActiveThreshold)); | |
599 } | |
600 | |
601 } // namespace | |
602 | |
603 } // namespace sync_driver | |
OLD | NEW |