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 "base/message_loop/message_loop.h" | |
6 #include "chrome/browser/sync/glue/device_info_sync_service.h" | |
7 #include "chrome/browser/sync/glue/local_device_info_provider_mock.h" | |
8 #include "content/public/test/test_browser_thread_bundle.h" | |
9 #include "sync/api/attachments/attachment_service_proxy_for_test.h" | |
10 #include "sync/api/sync_change.h" | |
11 #include "sync/api/sync_change_processor.h" | |
12 #include "sync/api/sync_change_processor_wrapper_for_test.h" | |
13 #include "sync/api/sync_error_factory_mock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 using syncer::AttachmentIdList; | |
17 using syncer::AttachmentServiceProxyForTest; | |
18 using syncer::ModelType; | |
19 using syncer::SyncChange; | |
20 using syncer::SyncChangeList; | |
21 using syncer::SyncChangeProcessor; | |
22 using syncer::SyncChangeProcessorWrapperForTest; | |
23 using syncer::SyncData; | |
24 using syncer::SyncDataList; | |
25 using syncer::SyncError; | |
26 using syncer::SyncErrorFactory; | |
27 using syncer::SyncErrorFactoryMock; | |
28 using syncer::SyncMergeResult; | |
29 | |
30 namespace browser_sync { | |
31 | |
32 namespace { | |
33 | |
34 class TestChangeProcessor : public SyncChangeProcessor { | |
35 public: | |
36 TestChangeProcessor() {} | |
37 virtual ~TestChangeProcessor() {} | |
38 | |
39 // SyncChangeProcessor implementation. | |
40 // Store a copy of all the changes passed in so we can examine them later. | |
41 virtual SyncError ProcessSyncChanges( | |
42 const tracked_objects::Location& from_here, | |
43 const SyncChangeList& change_list) OVERRIDE { | |
44 change_list_ = change_list; | |
45 return SyncError(); | |
46 } | |
47 | |
48 // This method isn't used in these tests. | |
49 virtual SyncDataList GetAllSyncData(ModelType type) const OVERRIDE { | |
50 return SyncDataList(); | |
51 } | |
52 | |
53 size_t change_list_size() const { return change_list_.size(); } | |
54 | |
55 SyncChange::SyncChangeType change_type_at(size_t index) const { | |
56 return change_list_[index].change_type(); | |
rlarocque
2014/08/01 20:40:36
operator[] doesn't do bounds checking. Maybe ther
stanisc
2014/08/01 21:37:42
Done.
| |
57 } | |
58 | |
59 const sync_pb::DeviceInfoSpecifics& device_info_at(size_t index) const { | |
60 return change_list_[index].sync_data().GetSpecifics().device_info(); | |
61 } | |
62 | |
63 const std::string& cache_guid_at(size_t index) const { | |
64 return device_info_at(index).cache_guid(); | |
65 } | |
66 | |
67 const std::string& client_name_at(size_t index) const { | |
68 return device_info_at(index).client_name(); | |
69 } | |
70 | |
71 private: | |
72 SyncChangeList change_list_; | |
73 }; | |
74 | |
75 class DeviceInfoSyncServiceTest : public testing::Test { | |
76 public: | |
77 DeviceInfoSyncServiceTest() : num_device_info_changed_callbacks_(0) {} | |
78 virtual ~DeviceInfoSyncServiceTest() {} | |
79 | |
80 virtual void SetUp() OVERRIDE { | |
81 local_device_.reset(new LocalDeviceInfoProviderMock( | |
82 "guid_1", | |
83 "client_1", | |
84 "Chromium 10k", | |
85 "Chrome 10k", | |
86 sync_pb::SyncEnums_DeviceType_TYPE_LINUX, | |
87 "device_id")); | |
88 sync_service_.reset(new DeviceInfoSyncService(local_device_.get())); | |
89 sync_processor_.reset(new TestChangeProcessor()); | |
90 // Register callbacks | |
91 notification_subscription_ = | |
92 sync_service_->RegisterOnDeviceInfoChangedCallback( | |
93 base::Bind(&DeviceInfoSyncServiceTest::OnDeviceInfoChanged, | |
94 base::Unretained(this))); | |
95 } | |
96 | |
97 virtual void TearDown() OVERRIDE {} | |
98 | |
99 scoped_ptr<SyncChangeProcessor> PassProcessor() { | |
100 return scoped_ptr<SyncChangeProcessor>( | |
101 new SyncChangeProcessorWrapperForTest(sync_processor_.get())); | |
102 } | |
103 | |
104 scoped_ptr<SyncErrorFactory> CreateAndPassSyncErrorFactory() { | |
105 return scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock()); | |
106 } | |
107 | |
108 SyncData CreateRemoteData(const std::string& client_id, | |
109 const std::string& client_name) { | |
110 sync_pb::EntitySpecifics entity; | |
111 sync_pb::DeviceInfoSpecifics& specifics = *entity.mutable_device_info(); | |
112 | |
113 specifics.set_cache_guid(client_id); | |
114 specifics.set_client_name(client_name); | |
115 specifics.set_chrome_version("Chromium 10k"); | |
116 specifics.set_sync_user_agent("Chrome 10k"); | |
117 specifics.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX); | |
118 specifics.set_signin_scoped_device_id("device_id"); | |
119 | |
120 return SyncData::CreateRemoteData(1, | |
121 entity, | |
122 base::Time(), | |
123 AttachmentIdList(), | |
124 AttachmentServiceProxyForTest::Create()); | |
125 } | |
126 | |
127 void AddInitialData(SyncDataList& sync_data_list, | |
128 const std::string& client_id, | |
129 const std::string& client_name) { | |
130 SyncData sync_data = CreateRemoteData(client_id, client_name); | |
131 sync_data_list.push_back(sync_data); | |
132 } | |
133 | |
134 void AddChange(SyncChangeList& change_list, | |
135 SyncChange::SyncChangeType change_type, | |
136 const std::string& client_id, | |
137 const std::string& client_name) { | |
138 SyncData sync_data = CreateRemoteData(client_id, client_name); | |
139 SyncChange sync_change(FROM_HERE, change_type, sync_data); | |
140 change_list.push_back(sync_change); | |
141 } | |
142 | |
143 protected: | |
144 void OnDeviceInfoChanged() { num_device_info_changed_callbacks_++; } | |
145 | |
146 int num_device_info_changed_callbacks_; | |
147 scoped_ptr<LocalDeviceInfoProviderMock> local_device_; | |
148 scoped_ptr<DeviceInfoSyncService> sync_service_; | |
149 scoped_ptr<TestChangeProcessor> sync_processor_; | |
150 scoped_ptr<DeviceInfoSyncService::Subscription> notification_subscription_; | |
151 content::TestBrowserThreadBundle thread_bundle_; | |
152 }; | |
153 | |
154 // Sync with empty initial data. | |
155 TEST_F(DeviceInfoSyncServiceTest, StartSyncEmptyInitialData) { | |
156 SyncMergeResult merge_result = | |
157 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
158 SyncDataList(), | |
159 PassProcessor(), | |
160 CreateAndPassSyncErrorFactory()); | |
161 | |
162 EXPECT_EQ(0, merge_result.num_items_added()); | |
163 EXPECT_EQ(0, merge_result.num_items_modified()); | |
164 EXPECT_EQ(0, merge_result.num_items_deleted()); | |
165 EXPECT_EQ(1, merge_result.num_items_before_association()); | |
166 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
167 EXPECT_EQ(SyncChange::ACTION_ADD, sync_processor_->change_type_at(0)); | |
168 | |
169 EXPECT_EQ(1U, sync_processor_->change_list_size()); | |
170 EXPECT_EQ("guid_1", sync_processor_->cache_guid_at(0)); | |
171 | |
172 // Should have one device info corresponding to local device info. | |
173 EXPECT_EQ(1U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size()); | |
174 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); | |
175 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); | |
176 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); | |
177 } | |
178 | |
179 // Sync with initial data matching the local device data. | |
180 TEST_F(DeviceInfoSyncServiceTest, StartSyncMatchingInitialData) { | |
181 SyncDataList sync_data; | |
182 AddInitialData(sync_data, "guid_1", "client_1"); | |
183 | |
184 SyncMergeResult merge_result = | |
185 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
186 sync_data, | |
187 PassProcessor(), | |
188 CreateAndPassSyncErrorFactory()); | |
189 EXPECT_EQ(0, merge_result.num_items_added()); | |
190 EXPECT_EQ(0, merge_result.num_items_modified()); | |
191 EXPECT_EQ(0, merge_result.num_items_deleted()); | |
192 EXPECT_EQ(1, merge_result.num_items_before_association()); | |
193 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
194 | |
195 // No changes expected because the device info matches. | |
196 EXPECT_EQ(0U, sync_processor_->change_list_size()); | |
197 | |
198 EXPECT_EQ(1U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size()); | |
199 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); | |
200 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); | |
201 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); | |
202 } | |
203 | |
204 // Sync with misc initial data. | |
205 TEST_F(DeviceInfoSyncServiceTest, StartSync) { | |
206 SyncDataList sync_data; | |
207 AddInitialData(sync_data, "guid_2", "foo"); | |
208 AddInitialData(sync_data, "guid_3", "bar"); | |
209 // This guid matches the local device but the client name is different. | |
210 AddInitialData(sync_data, "guid_1", "baz"); | |
211 | |
212 SyncMergeResult merge_result = | |
213 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
214 sync_data, | |
215 PassProcessor(), | |
216 CreateAndPassSyncErrorFactory()); | |
217 | |
218 EXPECT_EQ(2, merge_result.num_items_added()); | |
219 EXPECT_EQ(1, merge_result.num_items_modified()); | |
220 EXPECT_EQ(0, merge_result.num_items_deleted()); | |
221 EXPECT_EQ(1, merge_result.num_items_before_association()); | |
222 EXPECT_EQ(3, merge_result.num_items_after_association()); | |
223 | |
224 EXPECT_EQ(1U, sync_processor_->change_list_size()); | |
225 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync_processor_->change_type_at(0)); | |
226 EXPECT_EQ("client_1", sync_processor_->client_name_at(0)); | |
227 | |
228 EXPECT_EQ(3U, sync_service_->GetAllSyncData(syncer::DEVICE_INFO).size()); | |
229 EXPECT_EQ(3U, sync_service_->GetAllDeviceInfo().size()); | |
230 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); | |
231 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2")); | |
232 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_3")); | |
233 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); | |
234 } | |
235 | |
236 // Process sync change with ACTION_ADD. | |
237 // Verify callback. | |
238 TEST_F(DeviceInfoSyncServiceTest, ProcessAddChange) { | |
239 EXPECT_EQ(0, num_device_info_changed_callbacks_); | |
240 | |
241 // Start with an empty initial data. | |
242 SyncMergeResult merge_result = | |
243 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
244 SyncDataList(), | |
245 PassProcessor(), | |
246 CreateAndPassSyncErrorFactory()); | |
247 // There should be only one item corresponding to the local device | |
248 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
249 EXPECT_EQ(1, num_device_info_changed_callbacks_); | |
250 | |
251 // Add a new device info with a non-matching guid. | |
252 SyncChangeList change_list; | |
253 AddChange(change_list, SyncChange::ACTION_ADD, "guid_2", "foo"); | |
254 | |
255 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
256 EXPECT_FALSE(error.IsSet()); | |
257 EXPECT_EQ(2, num_device_info_changed_callbacks_); | |
258 | |
259 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size()); | |
260 | |
261 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_1")); | |
262 EXPECT_TRUE(sync_service_->GetDeviceInfo("guid_2")); | |
263 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_0")); | |
264 } | |
265 | |
266 // Process multiple sync change with ACTION_UPDATE and ACTION_ADD. | |
267 // Verify that callback is called multiple times. | |
268 TEST_F(DeviceInfoSyncServiceTest, ProcessMultipleChanges) { | |
269 SyncDataList sync_data; | |
270 AddInitialData(sync_data, "guid_2", "foo"); | |
271 AddInitialData(sync_data, "guid_3", "bar"); | |
272 | |
273 SyncMergeResult merge_result = | |
274 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
275 sync_data, | |
276 PassProcessor(), | |
277 CreateAndPassSyncErrorFactory()); | |
278 EXPECT_EQ(3, merge_result.num_items_after_association()); | |
279 // reset callbacks counter | |
280 num_device_info_changed_callbacks_ = 0; | |
281 | |
282 SyncChangeList change_list; | |
283 AddChange(change_list, SyncChange::ACTION_UPDATE, "guid_2", "foo_2"); | |
284 | |
285 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
286 EXPECT_FALSE(error.IsSet()); | |
287 | |
288 EXPECT_EQ(1, num_device_info_changed_callbacks_); | |
289 EXPECT_EQ(3U, sync_service_->GetAllDeviceInfo().size()); | |
290 EXPECT_EQ("foo_2", sync_service_->GetDeviceInfo("guid_2")->client_name()); | |
291 | |
292 change_list.clear(); | |
293 AddChange(change_list, SyncChange::ACTION_UPDATE, "guid_3", "bar_3"); | |
294 AddChange(change_list, SyncChange::ACTION_ADD, "guid_4", "baz_4"); | |
295 | |
296 error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
297 EXPECT_FALSE(error.IsSet()); | |
298 | |
299 EXPECT_EQ(2, num_device_info_changed_callbacks_); | |
300 EXPECT_EQ(4U, sync_service_->GetAllDeviceInfo().size()); | |
301 EXPECT_EQ("bar_3", sync_service_->GetDeviceInfo("guid_3")->client_name()); | |
302 EXPECT_EQ("baz_4", sync_service_->GetDeviceInfo("guid_4")->client_name()); | |
303 } | |
304 | |
305 // Process update to the local device info and verify that it is ignored. | |
306 TEST_F(DeviceInfoSyncServiceTest, ProcessUpdateChangeMatchingLocalDevice) { | |
307 SyncMergeResult merge_result = | |
308 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
309 SyncDataList(), | |
310 PassProcessor(), | |
311 CreateAndPassSyncErrorFactory()); | |
312 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
313 // reset callbacks counter | |
314 num_device_info_changed_callbacks_ = 0; | |
315 | |
316 SyncChangeList change_list; | |
317 AddChange(change_list, SyncChange::ACTION_UPDATE, "guid_1", "foo_1"); | |
318 | |
319 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
320 EXPECT_FALSE(error.IsSet()); | |
321 // Callback shouldn't be sent in this case. | |
322 EXPECT_EQ(0, num_device_info_changed_callbacks_); | |
323 // Should still have the old local device Info. | |
324 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); | |
325 EXPECT_EQ("client_1", sync_service_->GetDeviceInfo("guid_1")->client_name()); | |
326 } | |
327 | |
328 // Process sync change with ACTION_DELETE. | |
329 TEST_F(DeviceInfoSyncServiceTest, ProcessDeleteChange) { | |
330 SyncDataList sync_data; | |
331 AddInitialData(sync_data, "guid_2", "foo"); | |
332 AddInitialData(sync_data, "guid_3", "bar"); | |
333 | |
334 SyncMergeResult merge_result = | |
335 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
336 sync_data, | |
337 PassProcessor(), | |
338 CreateAndPassSyncErrorFactory()); | |
339 EXPECT_EQ(3, merge_result.num_items_after_association()); | |
340 // reset callbacks counter | |
341 num_device_info_changed_callbacks_ = 0; | |
342 | |
343 SyncChangeList change_list; | |
344 AddChange(change_list, SyncChange::ACTION_DELETE, "guid_2", "foo_2"); | |
345 | |
346 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
347 EXPECT_FALSE(error.IsSet()); | |
348 | |
349 EXPECT_EQ(1, num_device_info_changed_callbacks_); | |
350 EXPECT_EQ(2U, sync_service_->GetAllDeviceInfo().size()); | |
351 EXPECT_FALSE(sync_service_->GetDeviceInfo("guid_2")); | |
352 } | |
353 | |
354 // Process sync change with unexpected action. | |
355 TEST_F(DeviceInfoSyncServiceTest, ProcessInvalidChange) { | |
356 SyncMergeResult merge_result = | |
357 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
358 SyncDataList(), | |
359 PassProcessor(), | |
360 CreateAndPassSyncErrorFactory()); | |
361 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
362 // reset callbacks counter | |
363 num_device_info_changed_callbacks_ = 0; | |
364 | |
365 SyncChangeList change_list; | |
366 AddChange(change_list, (SyncChange::SyncChangeType)100, "guid_2", "foo_2"); | |
367 | |
368 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
369 EXPECT_TRUE(error.IsSet()); | |
370 | |
371 // The number of callback should still be zero. | |
372 EXPECT_EQ(0, num_device_info_changed_callbacks_); | |
373 EXPECT_EQ(1U, sync_service_->GetAllDeviceInfo().size()); | |
374 } | |
375 | |
376 // Process sync change after unsubscribing from notifications. | |
377 TEST_F(DeviceInfoSyncServiceTest, ProcessChangesAfterUnsubscribing) { | |
378 SyncMergeResult merge_result = | |
379 sync_service_->MergeDataAndStartSyncing(syncer::DEVICE_INFO, | |
380 SyncDataList(), | |
381 PassProcessor(), | |
382 CreateAndPassSyncErrorFactory()); | |
383 EXPECT_EQ(1, merge_result.num_items_after_association()); | |
384 // reset callbacks counter | |
385 num_device_info_changed_callbacks_ = 0; | |
386 | |
387 SyncChangeList change_list; | |
388 AddChange(change_list, SyncChange::ACTION_ADD, "guid_2", "foo_2"); | |
389 | |
390 // Unsubscribe before processing changes. | |
391 notification_subscription_.reset(); | |
392 | |
393 SyncError error = sync_service_->ProcessSyncChanges(FROM_HERE, change_list); | |
394 EXPECT_FALSE(error.IsSet()); | |
395 | |
396 // The number of callback should still be zero. | |
397 EXPECT_EQ(0, num_device_info_changed_callbacks_); | |
398 } | |
399 | |
400 } // namespace | |
401 | |
402 } // namespace browser_sync | |
OLD | NEW |