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

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

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

Powered by Google App Engine
This is Rietveld 408576698