OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 // TODO(akalin): This file is basically just a unit test for | |
6 // BookmarkChangeProcessor. Write unit tests for | |
7 // BookmarkModelAssociator separately. | |
8 | |
9 #include <stddef.h> | |
10 #include <stdint.h> | |
11 | |
12 #include <map> | |
13 #include <memory> | |
14 #include <queue> | |
15 #include <stack> | |
16 #include <utility> | |
17 | |
18 #include "base/bind.h" | |
19 #include "base/bind_helpers.h" | |
20 #include "base/files/file_util.h" | |
21 #include "base/location.h" | |
22 #include "base/macros.h" | |
23 #include "base/memory/ptr_util.h" | |
24 #include "base/run_loop.h" | |
25 #include "base/strings/string16.h" | |
26 #include "base/strings/string_number_conversions.h" | |
27 #include "base/strings/string_util.h" | |
28 #include "base/strings/stringprintf.h" | |
29 #include "base/strings/utf_string_conversions.h" | |
30 #include "base/test/test_message_loop.h" | |
31 #include "base/time/time.h" | |
32 #include "build/build_config.h" | |
33 #include "components/bookmarks/browser/base_bookmark_model_observer.h" | |
34 #include "components/bookmarks/browser/bookmark_model.h" | |
35 #include "components/bookmarks/browser/bookmark_utils.h" | |
36 #include "components/bookmarks/managed/managed_bookmark_service.h" | |
37 #include "components/bookmarks/test/bookmark_test_helpers.h" | |
38 #include "components/bookmarks/test/test_bookmark_client.h" | |
39 #include "components/browser_sync/browser/profile_sync_test_util.h" | |
40 #include "components/sync/api/data_type_error_handler.h" | |
41 #include "components/sync/api/data_type_error_handler_mock.h" | |
42 #include "components/sync/api/sync_error.h" | |
43 #include "components/sync/api/sync_merge_result.h" | |
44 #include "components/sync/core/change_record.h" | |
45 #include "components/sync/core/read_node.h" | |
46 #include "components/sync/core/read_transaction.h" | |
47 #include "components/sync/core/test/test_user_share.h" | |
48 #include "components/sync/core/write_node.h" | |
49 #include "components/sync/core/write_transaction.h" | |
50 #include "components/sync/core_impl/syncapi_internal.h" | |
51 #include "components/sync/driver/fake_sync_client.h" | |
52 #include "components/sync/syncable/mutable_entry.h" | |
53 #include "components/sync/syncable/syncable_id.h" | |
54 #include "components/sync/syncable/syncable_util.h" | |
55 #include "components/sync/syncable/syncable_write_transaction.h" | |
56 #include "components/sync_bookmarks/bookmark_change_processor.h" | |
57 #include "components/sync_bookmarks/bookmark_model_associator.h" | |
58 #include "testing/gmock/include/gmock/gmock.h" | |
59 #include "testing/gtest/include/gtest/gtest.h" | |
60 | |
61 using bookmarks::BookmarkModel; | |
62 using bookmarks::BookmarkNode; | |
63 using sync_bookmarks::BookmarkChangeProcessor; | |
64 using sync_bookmarks::BookmarkModelAssociator; | |
65 using syncer::BaseNode; | |
66 using testing::_; | |
67 using testing::Return; | |
68 using testing::StrictMock; | |
69 | |
70 namespace browser_sync { | |
71 | |
72 namespace { | |
73 | |
74 #if defined(OS_ANDROID) || defined(OS_IOS) | |
75 static const bool kExpectMobileBookmarks = true; | |
76 #else | |
77 static const bool kExpectMobileBookmarks = false; | |
78 #endif // defined(OS_ANDROID) || defined(OS_IOS) | |
79 | |
80 std::string ReturnEmptyString() { | |
81 return std::string(); | |
82 } | |
83 | |
84 void MakeServerUpdate(syncer::WriteTransaction* trans, | |
85 syncer::WriteNode* node) { | |
86 syncer::syncable::ChangeEntryIDAndUpdateChildren( | |
87 trans->GetWrappedWriteTrans(), node->GetMutableEntryForTest(), | |
88 syncer::syncable::Id::CreateFromServerId( | |
89 base::Int64ToString(node->GetId()))); | |
90 node->GetMutableEntryForTest()->PutBaseVersion(10); | |
91 node->GetMutableEntryForTest()->PutIsUnappliedUpdate(true); | |
92 } | |
93 | |
94 void MakeServerUpdate(syncer::WriteTransaction* trans, int64_t id) { | |
95 syncer::WriteNode node(trans); | |
96 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
97 MakeServerUpdate(trans, &node); | |
98 } | |
99 | |
100 // FakeServerChange constructs a list of syncer::ChangeRecords while modifying | |
101 // the sync model, and can pass the ChangeRecord list to a | |
102 // syncer::SyncObserver (i.e., the ProfileSyncService) to test the client | |
103 // change-application behavior. | |
104 // Tests using FakeServerChange should be careful to avoid back-references, | |
105 // since FakeServerChange will send the edits in the order specified. | |
106 class FakeServerChange { | |
107 public: | |
108 explicit FakeServerChange(syncer::WriteTransaction* trans) : trans_(trans) {} | |
109 | |
110 // Pretend that the server told the syncer to add a bookmark object. | |
111 int64_t AddWithMetaInfo(const std::string& title, | |
112 const std::string& url, | |
113 const BookmarkNode::MetaInfoMap* meta_info_map, | |
114 bool is_folder, | |
115 int64_t parent_id, | |
116 int64_t predecessor_id) { | |
117 syncer::ReadNode parent(trans_); | |
118 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id)); | |
119 syncer::WriteNode node(trans_); | |
120 if (predecessor_id == 0) { | |
121 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL)); | |
122 } else { | |
123 syncer::ReadNode predecessor(trans_); | |
124 EXPECT_EQ(BaseNode::INIT_OK, predecessor.InitByIdLookup(predecessor_id)); | |
125 EXPECT_EQ(predecessor.GetParentId(), parent.GetId()); | |
126 EXPECT_TRUE(node.InitBookmarkByCreation(parent, &predecessor)); | |
127 } | |
128 EXPECT_EQ(node.GetPredecessorId(), predecessor_id); | |
129 EXPECT_EQ(node.GetParentId(), parent_id); | |
130 node.SetIsFolder(is_folder); | |
131 node.SetTitle(title); | |
132 | |
133 sync_pb::BookmarkSpecifics specifics(node.GetBookmarkSpecifics()); | |
134 const base::Time creation_time(base::Time::Now()); | |
135 specifics.set_creation_time_us(creation_time.ToInternalValue()); | |
136 if (!is_folder) | |
137 specifics.set_url(url); | |
138 if (meta_info_map) | |
139 SetNodeMetaInfo(*meta_info_map, &specifics); | |
140 node.SetBookmarkSpecifics(specifics); | |
141 | |
142 syncer::ChangeRecord record; | |
143 record.action = syncer::ChangeRecord::ACTION_ADD; | |
144 record.id = node.GetId(); | |
145 changes_.push_back(record); | |
146 return node.GetId(); | |
147 } | |
148 | |
149 int64_t Add(const std::string& title, | |
150 const std::string& url, | |
151 bool is_folder, | |
152 int64_t parent_id, | |
153 int64_t predecessor_id) { | |
154 return AddWithMetaInfo(title, url, NULL, is_folder, parent_id, | |
155 predecessor_id); | |
156 } | |
157 | |
158 // Add a bookmark folder. | |
159 int64_t AddFolder(const std::string& title, | |
160 int64_t parent_id, | |
161 int64_t predecessor_id) { | |
162 return Add(title, std::string(), true, parent_id, predecessor_id); | |
163 } | |
164 int64_t AddFolderWithMetaInfo(const std::string& title, | |
165 const BookmarkNode::MetaInfoMap* meta_info_map, | |
166 int64_t parent_id, | |
167 int64_t predecessor_id) { | |
168 return AddWithMetaInfo(title, std::string(), meta_info_map, true, parent_id, | |
169 predecessor_id); | |
170 } | |
171 | |
172 // Add a bookmark. | |
173 int64_t AddURL(const std::string& title, | |
174 const std::string& url, | |
175 int64_t parent_id, | |
176 int64_t predecessor_id) { | |
177 return Add(title, url, false, parent_id, predecessor_id); | |
178 } | |
179 int64_t AddURLWithMetaInfo(const std::string& title, | |
180 const std::string& url, | |
181 const BookmarkNode::MetaInfoMap* meta_info_map, | |
182 int64_t parent_id, | |
183 int64_t predecessor_id) { | |
184 return AddWithMetaInfo(title, url, meta_info_map, false, parent_id, | |
185 predecessor_id); | |
186 } | |
187 | |
188 // Pretend that the server told the syncer to delete an object. | |
189 void Delete(int64_t id) { | |
190 { | |
191 // Delete the sync node. | |
192 syncer::WriteNode node(trans_); | |
193 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
194 if (node.GetIsFolder()) | |
195 EXPECT_FALSE(node.GetFirstChildId()); | |
196 node.GetMutableEntryForTest()->PutServerIsDel(true); | |
197 node.Tombstone(); | |
198 } | |
199 { | |
200 // Verify the deletion. | |
201 syncer::ReadNode node(trans_); | |
202 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL, node.InitByIdLookup(id)); | |
203 } | |
204 | |
205 syncer::ChangeRecord record; | |
206 record.action = syncer::ChangeRecord::ACTION_DELETE; | |
207 record.id = id; | |
208 // Deletions are always first in the changelist, but we can't actually do | |
209 // WriteNode::Remove() on the node until its children are moved. So, as | |
210 // a practical matter, users of FakeServerChange must move or delete | |
211 // children before parents. Thus, we must insert the deletion record | |
212 // at the front of the vector. | |
213 changes_.insert(changes_.begin(), record); | |
214 } | |
215 | |
216 // Set a new title value, and return the old value. | |
217 std::string ModifyTitle(int64_t id, const std::string& new_title) { | |
218 syncer::WriteNode node(trans_); | |
219 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
220 std::string old_title = node.GetTitle(); | |
221 node.SetTitle(new_title); | |
222 SetModified(id); | |
223 return old_title; | |
224 } | |
225 | |
226 // Set a new parent and predecessor value. Return the old parent id. | |
227 // We could return the old predecessor id, but it turns out not to be | |
228 // very useful for assertions. | |
229 int64_t ModifyPosition(int64_t id, | |
230 int64_t parent_id, | |
231 int64_t predecessor_id) { | |
232 syncer::ReadNode parent(trans_); | |
233 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id)); | |
234 syncer::WriteNode node(trans_); | |
235 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
236 int64_t old_parent_id = node.GetParentId(); | |
237 if (predecessor_id == 0) { | |
238 EXPECT_TRUE(node.SetPosition(parent, NULL)); | |
239 } else { | |
240 syncer::ReadNode predecessor(trans_); | |
241 EXPECT_EQ(BaseNode::INIT_OK, predecessor.InitByIdLookup(predecessor_id)); | |
242 EXPECT_EQ(predecessor.GetParentId(), parent.GetId()); | |
243 EXPECT_TRUE(node.SetPosition(parent, &predecessor)); | |
244 } | |
245 SetModified(id); | |
246 return old_parent_id; | |
247 } | |
248 | |
249 void ModifyCreationTime(int64_t id, int64_t creation_time_us) { | |
250 syncer::WriteNode node(trans_); | |
251 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
252 sync_pb::BookmarkSpecifics specifics = node.GetBookmarkSpecifics(); | |
253 specifics.set_creation_time_us(creation_time_us); | |
254 node.SetBookmarkSpecifics(specifics); | |
255 SetModified(id); | |
256 } | |
257 | |
258 void ModifyMetaInfo(int64_t id, | |
259 const BookmarkNode::MetaInfoMap& meta_info_map) { | |
260 syncer::WriteNode node(trans_); | |
261 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
262 sync_pb::BookmarkSpecifics specifics = node.GetBookmarkSpecifics(); | |
263 SetNodeMetaInfo(meta_info_map, &specifics); | |
264 node.SetBookmarkSpecifics(specifics); | |
265 SetModified(id); | |
266 } | |
267 | |
268 // Pass the fake change list to |service|. | |
269 void ApplyPendingChanges(sync_driver::ChangeProcessor* processor) { | |
270 processor->ApplyChangesFromSyncModel( | |
271 trans_, 0, syncer::ImmutableChangeRecordList(&changes_)); | |
272 } | |
273 | |
274 const syncer::ChangeRecordList& changes() { return changes_; } | |
275 | |
276 private: | |
277 // Helper function to push an ACTION_UPDATE record onto the back | |
278 // of the changelist. | |
279 void SetModified(int64_t id) { | |
280 // Coalesce multi-property edits. | |
281 if (!changes_.empty() && changes_.back().id == id && | |
282 changes_.back().action == syncer::ChangeRecord::ACTION_UPDATE) | |
283 return; | |
284 syncer::ChangeRecord record; | |
285 record.action = syncer::ChangeRecord::ACTION_UPDATE; | |
286 record.id = id; | |
287 changes_.push_back(record); | |
288 } | |
289 | |
290 void SetNodeMetaInfo(const BookmarkNode::MetaInfoMap& meta_info_map, | |
291 sync_pb::BookmarkSpecifics* specifics) { | |
292 specifics->clear_meta_info(); | |
293 // Deliberatly set MetaInfoMap entries in opposite order (compared | |
294 // to the implementation in BookmarkChangeProcessor) to ensure that | |
295 // (a) the implementation isn't sensitive to the order and | |
296 // (b) the original meta info isn't blindly overwritten by | |
297 // BookmarkChangeProcessor unless there is a real change. | |
298 BookmarkNode::MetaInfoMap::const_iterator it = meta_info_map.end(); | |
299 while (it != meta_info_map.begin()) { | |
300 --it; | |
301 sync_pb::MetaInfo* meta_info = specifics->add_meta_info(); | |
302 meta_info->set_key(it->first); | |
303 meta_info->set_value(it->second); | |
304 } | |
305 } | |
306 | |
307 // The transaction on which everything happens. | |
308 syncer::WriteTransaction* trans_; | |
309 | |
310 // The change list we construct. | |
311 syncer::ChangeRecordList changes_; | |
312 DISALLOW_COPY_AND_ASSIGN(FakeServerChange); | |
313 }; | |
314 | |
315 class ExtensiveChangesBookmarkModelObserver | |
316 : public bookmarks::BaseBookmarkModelObserver { | |
317 public: | |
318 ExtensiveChangesBookmarkModelObserver() | |
319 : started_count_(0), | |
320 completed_count_at_started_(0), | |
321 completed_count_(0) {} | |
322 | |
323 void ExtensiveBookmarkChangesBeginning(BookmarkModel* model) override { | |
324 ++started_count_; | |
325 completed_count_at_started_ = completed_count_; | |
326 } | |
327 | |
328 void ExtensiveBookmarkChangesEnded(BookmarkModel* model) override { | |
329 ++completed_count_; | |
330 } | |
331 | |
332 void BookmarkModelChanged() override {} | |
333 | |
334 int get_started() const { return started_count_; } | |
335 | |
336 int get_completed_count_at_started() const { | |
337 return completed_count_at_started_; | |
338 } | |
339 | |
340 int get_completed() const { return completed_count_; } | |
341 | |
342 private: | |
343 int started_count_; | |
344 int completed_count_at_started_; | |
345 int completed_count_; | |
346 | |
347 DISALLOW_COPY_AND_ASSIGN(ExtensiveChangesBookmarkModelObserver); | |
348 }; | |
349 | |
350 class ProfileSyncServiceBookmarkTest : public testing::Test { | |
351 protected: | |
352 enum LoadOption { LOAD_FROM_STORAGE, DELETE_EXISTING_STORAGE }; | |
353 enum SaveOption { SAVE_TO_STORAGE, DONT_SAVE_TO_STORAGE }; | |
354 | |
355 ProfileSyncServiceBookmarkTest() | |
356 : managed_bookmark_service_(new bookmarks::ManagedBookmarkService( | |
357 profile_sync_service_bundle_.pref_service(), | |
358 base::Bind(ReturnEmptyString))), | |
359 local_merge_result_(syncer::BOOKMARKS), | |
360 syncer_merge_result_(syncer::BOOKMARKS) { | |
361 CHECK(data_dir_.CreateUniqueTempDir()); | |
362 browser_sync::ProfileSyncServiceBundle::SyncClientBuilder builder( | |
363 &profile_sync_service_bundle_); | |
364 builder.SetBookmarkModelCallback(base::Bind( | |
365 &ProfileSyncServiceBookmarkTest::model, base::Unretained(this))); | |
366 sync_client_ = builder.Build(); | |
367 bookmarks::RegisterProfilePrefs( | |
368 profile_sync_service_bundle_.pref_service()->registry()); | |
369 } | |
370 | |
371 ~ProfileSyncServiceBookmarkTest() override { | |
372 static_cast<KeyedService*>(managed_bookmark_service_.get())->Shutdown(); | |
373 StopSync(); | |
374 } | |
375 | |
376 void SetUp() override { test_user_share_.SetUp(); } | |
377 | |
378 void TearDown() override { test_user_share_.TearDown(); } | |
379 | |
380 bool CanSyncNode(const BookmarkNode* node) { | |
381 return model_->client()->CanSyncNode(node); | |
382 } | |
383 | |
384 // Inserts a folder directly to the share. | |
385 // Do not use this after model association is complete. | |
386 // | |
387 // This function differs from the AddFolder() function declared elsewhere in | |
388 // this file in that it only affects the sync model. It would be invalid to | |
389 // change the sync model directly after ModelAssociation. This function can | |
390 // be invoked prior to model association to set up first-time sync model | |
391 // association scenarios. | |
392 int64_t AddFolderToShare(syncer::WriteTransaction* trans, | |
393 const std::string& title) { | |
394 EXPECT_FALSE(model_associator_); | |
395 | |
396 // Be sure to call CreatePermanentBookmarkNodes(), otherwise this will fail. | |
397 syncer::ReadNode bookmark_bar(trans); | |
398 EXPECT_EQ(BaseNode::INIT_OK, | |
399 bookmark_bar.InitByTagLookupForBookmarks("bookmark_bar")); | |
400 | |
401 syncer::WriteNode node(trans); | |
402 EXPECT_TRUE(node.InitBookmarkByCreation(bookmark_bar, NULL)); | |
403 node.SetIsFolder(true); | |
404 node.SetTitle(title); | |
405 | |
406 return node.GetId(); | |
407 } | |
408 | |
409 // Inserts a bookmark directly to the share. | |
410 // Do not use this after model association is complete. | |
411 // | |
412 // This function differs from the AddURL() function declared elsewhere in this | |
413 // file in that it only affects the sync model. It would be invalid to change | |
414 // the sync model directly after ModelAssociation. This function can be | |
415 // invoked prior to model association to set up first-time sync model | |
416 // association scenarios. | |
417 int64_t AddBookmarkToShare(syncer::WriteTransaction* trans, | |
418 int64_t parent_id, | |
419 const std::string& title, | |
420 const std::string& url) { | |
421 EXPECT_FALSE(model_associator_); | |
422 | |
423 syncer::ReadNode parent(trans); | |
424 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id)); | |
425 | |
426 sync_pb::BookmarkSpecifics specifics; | |
427 specifics.set_url(url); | |
428 specifics.set_title(title); | |
429 | |
430 syncer::WriteNode node(trans); | |
431 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL)); | |
432 node.SetIsFolder(false); | |
433 node.SetTitle(title); | |
434 node.SetBookmarkSpecifics(specifics); | |
435 | |
436 return node.GetId(); | |
437 } | |
438 | |
439 // Create a BookmarkModel. If |delete_bookmarks| is true, the bookmarks file | |
440 // will be deleted before starting up the BookmarkModel. | |
441 std::unique_ptr<BookmarkModel> CreateBookmarkModel(bool delete_bookmarks) { | |
442 const base::FilePath& data_path = data_dir_.GetPath(); | |
443 auto model = base::MakeUnique<BookmarkModel>( | |
444 base::WrapUnique(new bookmarks::TestBookmarkClient())); | |
445 managed_bookmark_service_->BookmarkModelCreated(model.get()); | |
446 int64_t next_id = 0; | |
447 static_cast<bookmarks::TestBookmarkClient*>(model->client()) | |
448 ->SetExtraNodesToLoad( | |
449 managed_bookmark_service_->GetLoadExtraNodesCallback().Run( | |
450 &next_id)); | |
451 if (delete_bookmarks) { | |
452 base::DeleteFile(data_path.Append(FILE_PATH_LITERAL("dummy_bookmarks")), | |
453 false); | |
454 } | |
455 | |
456 model->Load(profile_sync_service_bundle_.pref_service(), data_path, | |
457 base::ThreadTaskRunnerHandle::Get(), | |
458 base::ThreadTaskRunnerHandle::Get()); | |
459 bookmarks::test::WaitForBookmarkModelToLoad(model.get()); | |
460 return model; | |
461 } | |
462 | |
463 // Load (or re-load) the bookmark model. |load| controls use of the | |
464 // bookmarks file on disk. |save| controls whether the newly loaded | |
465 // bookmark model will write out a bookmark file as it goes. | |
466 void LoadBookmarkModel(LoadOption load, SaveOption save) { | |
467 bool delete_bookmarks = load == DELETE_EXISTING_STORAGE; | |
468 model_.reset(); | |
469 model_ = CreateBookmarkModel(delete_bookmarks); | |
470 // This noticeably speeds up the unit tests that request it. | |
471 if (save == DONT_SAVE_TO_STORAGE) | |
472 model_->ClearStore(); | |
473 base::RunLoop().RunUntilIdle(); | |
474 } | |
475 | |
476 int GetSyncBookmarkCount() { | |
477 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
478 syncer::ReadNode node(&trans); | |
479 if (node.InitTypeRoot(syncer::BOOKMARKS) != BaseNode::INIT_OK) | |
480 return 0; | |
481 return node.GetTotalNodeCount(); | |
482 } | |
483 | |
484 // Creates the bookmark root node and the permanent nodes if they don't | |
485 // already exist. | |
486 bool CreatePermanentBookmarkNodes() { | |
487 bool root_exists = false; | |
488 syncer::ModelType type = syncer::BOOKMARKS; | |
489 { | |
490 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
491 syncer::ReadNode uber_root(&trans); | |
492 uber_root.InitByRootLookup(); | |
493 | |
494 syncer::ReadNode root(&trans); | |
495 root_exists = (root.InitTypeRoot(type) == BaseNode::INIT_OK); | |
496 } | |
497 | |
498 if (!root_exists) { | |
499 if (!syncer::TestUserShare::CreateRoot(type, | |
500 test_user_share_.user_share())) | |
501 return false; | |
502 } | |
503 | |
504 const int kNumPermanentNodes = 3; | |
505 const std::string permanent_tags[kNumPermanentNodes] = { | |
506 "bookmark_bar", "other_bookmarks", "synced_bookmarks", | |
507 }; | |
508 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
509 syncer::ReadNode root(&trans); | |
510 EXPECT_EQ(BaseNode::INIT_OK, root.InitTypeRoot(type)); | |
511 | |
512 // Loop through creating permanent nodes as necessary. | |
513 int64_t last_child_id = syncer::kInvalidId; | |
514 for (int i = 0; i < kNumPermanentNodes; ++i) { | |
515 // First check if the node already exists. This is for tests that involve | |
516 // persistence and set up sync more than once. | |
517 syncer::ReadNode lookup(&trans); | |
518 if (lookup.InitByTagLookupForBookmarks(permanent_tags[i]) == | |
519 syncer::ReadNode::INIT_OK) { | |
520 last_child_id = lookup.GetId(); | |
521 continue; | |
522 } | |
523 | |
524 // If it doesn't exist, create the permanent node at the end of the | |
525 // ordering. | |
526 syncer::ReadNode predecessor_node(&trans); | |
527 syncer::ReadNode* predecessor = NULL; | |
528 if (last_child_id != syncer::kInvalidId) { | |
529 EXPECT_EQ(BaseNode::INIT_OK, | |
530 predecessor_node.InitByIdLookup(last_child_id)); | |
531 predecessor = &predecessor_node; | |
532 } | |
533 syncer::WriteNode node(&trans); | |
534 if (!node.InitBookmarkByCreation(root, predecessor)) | |
535 return false; | |
536 node.SetIsFolder(true); | |
537 node.GetMutableEntryForTest()->PutUniqueServerTag(permanent_tags[i]); | |
538 node.SetTitle(permanent_tags[i]); | |
539 node.SetExternalId(0); | |
540 last_child_id = node.GetId(); | |
541 } | |
542 return true; | |
543 } | |
544 | |
545 bool AssociateModels() { | |
546 DCHECK(!model_associator_); | |
547 | |
548 // Set up model associator. | |
549 model_associator_.reset(new BookmarkModelAssociator( | |
550 model_.get(), sync_client_.get(), test_user_share_.user_share(), | |
551 base::MakeUnique<syncer::DataTypeErrorHandlerMock>(), | |
552 kExpectMobileBookmarks)); | |
553 | |
554 local_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS); | |
555 syncer_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS); | |
556 int local_count_before = model_->root_node()->GetTotalNodeCount(); | |
557 int syncer_count_before = GetSyncBookmarkCount(); | |
558 | |
559 syncer::SyncError error = model_associator_->AssociateModels( | |
560 &local_merge_result_, &syncer_merge_result_); | |
561 if (error.IsSet()) | |
562 return false; | |
563 | |
564 base::RunLoop().RunUntilIdle(); | |
565 | |
566 // Verify the merge results were calculated properly. | |
567 EXPECT_EQ(local_count_before, | |
568 local_merge_result_.num_items_before_association()); | |
569 EXPECT_EQ(syncer_count_before, | |
570 syncer_merge_result_.num_items_before_association()); | |
571 EXPECT_EQ(local_merge_result_.num_items_after_association(), | |
572 local_merge_result_.num_items_before_association() + | |
573 local_merge_result_.num_items_added() - | |
574 local_merge_result_.num_items_deleted()); | |
575 EXPECT_EQ(syncer_merge_result_.num_items_after_association(), | |
576 syncer_merge_result_.num_items_before_association() + | |
577 syncer_merge_result_.num_items_added() - | |
578 syncer_merge_result_.num_items_deleted()); | |
579 EXPECT_EQ(model_->root_node()->GetTotalNodeCount(), | |
580 local_merge_result_.num_items_after_association()); | |
581 EXPECT_EQ(GetSyncBookmarkCount(), | |
582 syncer_merge_result_.num_items_after_association()); | |
583 return true; | |
584 } | |
585 | |
586 void StartSync() { | |
587 test_user_share_.Reload(); | |
588 | |
589 ASSERT_TRUE(CreatePermanentBookmarkNodes()); | |
590 ASSERT_TRUE(AssociateModels()); | |
591 | |
592 // Set up change processor. | |
593 ResetChangeProcessor(); | |
594 change_processor_->Start(test_user_share_.user_share()); | |
595 } | |
596 | |
597 void StopSync() { | |
598 change_processor_.reset(); | |
599 if (model_associator_) { | |
600 syncer::SyncError error = model_associator_->DisassociateModels(); | |
601 EXPECT_FALSE(error.IsSet()); | |
602 } | |
603 model_associator_.reset(); | |
604 | |
605 base::RunLoop().RunUntilIdle(); | |
606 | |
607 // TODO(akalin): Actually close the database and flush it to disk | |
608 // (and make StartSync reload from disk). This would require | |
609 // refactoring TestUserShare. | |
610 } | |
611 | |
612 bool InitSyncNodeFromChromeNode(const BookmarkNode* bnode, | |
613 BaseNode* sync_node) { | |
614 return model_associator_->InitSyncNodeFromChromeId(bnode->id(), sync_node); | |
615 } | |
616 | |
617 void ExpectSyncerNodeMatching(syncer::BaseTransaction* trans, | |
618 const BookmarkNode* bnode) { | |
619 std::string truncated_title = base::UTF16ToUTF8(bnode->GetTitle()); | |
620 syncer::SyncAPINameToServerName(truncated_title, &truncated_title); | |
621 base::TruncateUTF8ToByteSize(truncated_title, 255, &truncated_title); | |
622 syncer::ServerNameToSyncAPIName(truncated_title, &truncated_title); | |
623 | |
624 syncer::ReadNode gnode(trans); | |
625 ASSERT_TRUE(InitSyncNodeFromChromeNode(bnode, &gnode)); | |
626 // Non-root node titles and parents must match. | |
627 if (!model_->is_permanent_node(bnode)) { | |
628 EXPECT_EQ(truncated_title, gnode.GetTitle()); | |
629 EXPECT_EQ(model_associator_->GetChromeNodeFromSyncId(gnode.GetParentId()), | |
630 bnode->parent()); | |
631 } | |
632 EXPECT_EQ(bnode->is_folder(), gnode.GetIsFolder()); | |
633 if (bnode->is_url()) | |
634 EXPECT_EQ(bnode->url(), GURL(gnode.GetBookmarkSpecifics().url())); | |
635 | |
636 // Check that meta info matches. | |
637 const BookmarkNode::MetaInfoMap* meta_info_map = bnode->GetMetaInfoMap(); | |
638 sync_pb::BookmarkSpecifics specifics = gnode.GetBookmarkSpecifics(); | |
639 if (!meta_info_map) { | |
640 EXPECT_EQ(0, specifics.meta_info_size()); | |
641 } else { | |
642 EXPECT_EQ(meta_info_map->size(), | |
643 static_cast<size_t>(specifics.meta_info_size())); | |
644 for (int i = 0; i < specifics.meta_info_size(); i++) { | |
645 BookmarkNode::MetaInfoMap::const_iterator it = | |
646 meta_info_map->find(specifics.meta_info(i).key()); | |
647 EXPECT_TRUE(it != meta_info_map->end()); | |
648 EXPECT_EQ(it->second, specifics.meta_info(i).value()); | |
649 } | |
650 } | |
651 | |
652 // Check for position matches. | |
653 int browser_index = bnode->parent()->GetIndexOf(bnode); | |
654 if (browser_index == 0) { | |
655 EXPECT_EQ(gnode.GetPredecessorId(), 0); | |
656 } else { | |
657 const BookmarkNode* bprev = bnode->parent()->GetChild(browser_index - 1); | |
658 syncer::ReadNode gprev(trans); | |
659 ASSERT_TRUE(InitSyncNodeFromChromeNode(bprev, &gprev)); | |
660 EXPECT_EQ(gnode.GetPredecessorId(), gprev.GetId()); | |
661 EXPECT_EQ(gnode.GetParentId(), gprev.GetParentId()); | |
662 } | |
663 // Note: the managed node is the last child of the root_node but isn't | |
664 // synced; if CanSyncNode() is false then there is no next node to sync. | |
665 const BookmarkNode* bnext = NULL; | |
666 if (browser_index + 1 < bnode->parent()->child_count()) | |
667 bnext = bnode->parent()->GetChild(browser_index + 1); | |
668 if (!bnext || !CanSyncNode(bnext)) { | |
669 EXPECT_EQ(gnode.GetSuccessorId(), 0); | |
670 } else { | |
671 syncer::ReadNode gnext(trans); | |
672 ASSERT_TRUE(InitSyncNodeFromChromeNode(bnext, &gnext)); | |
673 EXPECT_EQ(gnode.GetSuccessorId(), gnext.GetId()); | |
674 EXPECT_EQ(gnode.GetParentId(), gnext.GetParentId()); | |
675 } | |
676 if (!bnode->empty()) | |
677 EXPECT_TRUE(gnode.GetFirstChildId()); | |
678 } | |
679 | |
680 void ExpectSyncerNodeMatching(const BookmarkNode* bnode) { | |
681 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
682 ExpectSyncerNodeMatching(&trans, bnode); | |
683 } | |
684 | |
685 void ExpectBrowserNodeMatching(syncer::BaseTransaction* trans, | |
686 int64_t sync_id) { | |
687 EXPECT_TRUE(sync_id); | |
688 const BookmarkNode* bnode = | |
689 model_associator_->GetChromeNodeFromSyncId(sync_id); | |
690 ASSERT_TRUE(bnode); | |
691 ASSERT_TRUE(CanSyncNode(bnode)); | |
692 | |
693 int64_t id = model_associator_->GetSyncIdFromChromeId(bnode->id()); | |
694 EXPECT_EQ(id, sync_id); | |
695 ExpectSyncerNodeMatching(trans, bnode); | |
696 } | |
697 | |
698 void ExpectBrowserNodeUnknown(int64_t sync_id) { | |
699 EXPECT_FALSE(model_associator_->GetChromeNodeFromSyncId(sync_id)); | |
700 } | |
701 | |
702 void ExpectBrowserNodeKnown(int64_t sync_id) { | |
703 EXPECT_TRUE(model_associator_->GetChromeNodeFromSyncId(sync_id)); | |
704 } | |
705 | |
706 void ExpectSyncerNodeKnown(const BookmarkNode* node) { | |
707 int64_t sync_id = model_associator_->GetSyncIdFromChromeId(node->id()); | |
708 EXPECT_NE(sync_id, syncer::kInvalidId); | |
709 } | |
710 | |
711 void ExpectSyncerNodeUnknown(const BookmarkNode* node) { | |
712 int64_t sync_id = model_associator_->GetSyncIdFromChromeId(node->id()); | |
713 EXPECT_EQ(sync_id, syncer::kInvalidId); | |
714 } | |
715 | |
716 void ExpectBrowserNodeTitle(int64_t sync_id, const std::string& title) { | |
717 const BookmarkNode* bnode = | |
718 model_associator_->GetChromeNodeFromSyncId(sync_id); | |
719 ASSERT_TRUE(bnode); | |
720 EXPECT_EQ(bnode->GetTitle(), base::UTF8ToUTF16(title)); | |
721 } | |
722 | |
723 void ExpectBrowserNodeURL(int64_t sync_id, const std::string& url) { | |
724 const BookmarkNode* bnode = | |
725 model_associator_->GetChromeNodeFromSyncId(sync_id); | |
726 ASSERT_TRUE(bnode); | |
727 EXPECT_EQ(GURL(url), bnode->url()); | |
728 } | |
729 | |
730 void ExpectBrowserNodeParent(int64_t sync_id, int64_t parent_sync_id) { | |
731 const BookmarkNode* node = | |
732 model_associator_->GetChromeNodeFromSyncId(sync_id); | |
733 ASSERT_TRUE(node); | |
734 const BookmarkNode* parent = | |
735 model_associator_->GetChromeNodeFromSyncId(parent_sync_id); | |
736 EXPECT_TRUE(parent); | |
737 EXPECT_EQ(node->parent(), parent); | |
738 } | |
739 | |
740 void ExpectModelMatch(syncer::BaseTransaction* trans) { | |
741 const BookmarkNode* root = model_->root_node(); | |
742 EXPECT_EQ(root->GetIndexOf(model_->bookmark_bar_node()), 0); | |
743 EXPECT_EQ(root->GetIndexOf(model_->other_node()), 1); | |
744 EXPECT_EQ(root->GetIndexOf(model_->mobile_node()), 2); | |
745 | |
746 std::stack<int64_t> stack; | |
747 stack.push(bookmark_bar_id()); | |
748 while (!stack.empty()) { | |
749 int64_t id = stack.top(); | |
750 stack.pop(); | |
751 if (!id) | |
752 continue; | |
753 | |
754 ExpectBrowserNodeMatching(trans, id); | |
755 | |
756 syncer::ReadNode gnode(trans); | |
757 ASSERT_EQ(BaseNode::INIT_OK, gnode.InitByIdLookup(id)); | |
758 stack.push(gnode.GetSuccessorId()); | |
759 if (gnode.GetIsFolder()) | |
760 stack.push(gnode.GetFirstChildId()); | |
761 } | |
762 } | |
763 | |
764 void ExpectModelMatch() { | |
765 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
766 ExpectModelMatch(&trans); | |
767 } | |
768 | |
769 int64_t mobile_bookmarks_id() { | |
770 return model_associator_->GetSyncIdFromChromeId( | |
771 model_->mobile_node()->id()); | |
772 } | |
773 | |
774 int64_t other_bookmarks_id() { | |
775 return model_associator_->GetSyncIdFromChromeId(model_->other_node()->id()); | |
776 } | |
777 | |
778 int64_t bookmark_bar_id() { | |
779 return model_associator_->GetSyncIdFromChromeId( | |
780 model_->bookmark_bar_node()->id()); | |
781 } | |
782 | |
783 BookmarkModel* model() { return model_.get(); } | |
784 | |
785 syncer::TestUserShare* test_user_share() { return &test_user_share_; } | |
786 | |
787 BookmarkChangeProcessor* change_processor() { | |
788 return change_processor_.get(); | |
789 } | |
790 | |
791 void delete_change_processor() { change_processor_.reset(); } | |
792 | |
793 void ResetChangeProcessor() { | |
794 std::unique_ptr<syncer::DataTypeErrorHandlerMock> error_handler = | |
795 base::MakeUnique<syncer::DataTypeErrorHandlerMock>(); | |
796 mock_error_handler_ = error_handler.get(); | |
797 change_processor_ = base::MakeUnique<BookmarkChangeProcessor>( | |
798 sync_client_.get(), model_associator_.get(), std::move(error_handler)); | |
799 } | |
800 | |
801 syncer::DataTypeErrorHandlerMock* mock_error_handler() { | |
802 return mock_error_handler_; | |
803 } | |
804 | |
805 void delete_model_associator() { model_associator_.reset(); } | |
806 | |
807 BookmarkModelAssociator* model_associator() { | |
808 return model_associator_.get(); | |
809 } | |
810 | |
811 bookmarks::ManagedBookmarkService* managed_bookmark_service() { | |
812 return managed_bookmark_service_.get(); | |
813 } | |
814 | |
815 private: | |
816 base::TestMessageLoop message_loop_; | |
817 base::ScopedTempDir data_dir_; | |
818 browser_sync::ProfileSyncServiceBundle profile_sync_service_bundle_; | |
819 | |
820 std::unique_ptr<sync_driver::FakeSyncClient> sync_client_; | |
821 std::unique_ptr<BookmarkModel> model_; | |
822 syncer::TestUserShare test_user_share_; | |
823 std::unique_ptr<BookmarkChangeProcessor> change_processor_; | |
824 syncer::DataTypeErrorHandlerMock* mock_error_handler_; | |
825 std::unique_ptr<BookmarkModelAssociator> model_associator_; | |
826 std::unique_ptr<bookmarks::ManagedBookmarkService> managed_bookmark_service_; | |
827 | |
828 syncer::SyncMergeResult local_merge_result_; | |
829 syncer::SyncMergeResult syncer_merge_result_; | |
830 | |
831 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceBookmarkTest); | |
832 }; | |
833 | |
834 TEST_F(ProfileSyncServiceBookmarkTest, InitialState) { | |
835 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
836 StartSync(); | |
837 | |
838 EXPECT_TRUE(other_bookmarks_id()); | |
839 EXPECT_TRUE(bookmark_bar_id()); | |
840 EXPECT_TRUE(mobile_bookmarks_id()); | |
841 | |
842 ExpectModelMatch(); | |
843 } | |
844 | |
845 // Populate the sync database then start model association. Sync's bookmarks | |
846 // should end up being copied into the native model, resulting in a successful | |
847 // "ExpectModelMatch()". | |
848 // | |
849 // This code has some use for verifying correctness. It's also a very useful | |
850 // for profiling bookmark ModelAssociation, an important part of some first-time | |
851 // sync scenarios. Simply increase the kNumFolders and kNumBookmarksPerFolder | |
852 // as desired, then run the test under a profiler to find hot spots in the model | |
853 // association code. | |
854 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociate) { | |
855 const int kNumBookmarksPerFolder = 10; | |
856 const int kNumFolders = 10; | |
857 | |
858 CreatePermanentBookmarkNodes(); | |
859 | |
860 { | |
861 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
862 for (int i = 0; i < kNumFolders; ++i) { | |
863 int64_t folder_id = | |
864 AddFolderToShare(&trans, base::StringPrintf("folder%05d", i)); | |
865 for (int j = 0; j < kNumBookmarksPerFolder; ++j) { | |
866 AddBookmarkToShare( | |
867 &trans, folder_id, base::StringPrintf("bookmark%05d", j), | |
868 base::StringPrintf("http://www.google.com/search?q=%05d", j)); | |
869 } | |
870 } | |
871 } | |
872 | |
873 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
874 StartSync(); | |
875 | |
876 ExpectModelMatch(); | |
877 } | |
878 | |
879 // Tests bookmark association when nodes exists in the native model only. | |
880 // These entries should be copied to Sync directory during association process. | |
881 TEST_F(ProfileSyncServiceBookmarkTest, | |
882 InitialModelAssociateWithBookmarkModelNodes) { | |
883 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
884 const BookmarkNode* folder = model()->AddFolder(model()->other_node(), 0, | |
885 base::ASCIIToUTF16("foobar")); | |
886 model()->AddFolder(folder, 0, base::ASCIIToUTF16("nested")); | |
887 model()->AddURL(folder, 0, base::ASCIIToUTF16("Internets #1 Pies Site"), | |
888 GURL("http://www.easypie.com/")); | |
889 model()->AddURL(folder, 1, base::ASCIIToUTF16("Airplanes"), | |
890 GURL("http://www.easyjet.com/")); | |
891 | |
892 StartSync(); | |
893 ExpectModelMatch(); | |
894 } | |
895 | |
896 // Tests bookmark association case when there is an entry in the delete journal | |
897 // that matches one of native bookmarks. | |
898 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociateWithDeleteJournal) { | |
899 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
900 const BookmarkNode* folder = model()->AddFolder( | |
901 model()->bookmark_bar_node(), 0, base::ASCIIToUTF16("foobar")); | |
902 const BookmarkNode* bookmark = | |
903 model()->AddURL(folder, 0, base::ASCIIToUTF16("Airplanes"), | |
904 GURL("http://www.easyjet.com/")); | |
905 | |
906 CreatePermanentBookmarkNodes(); | |
907 | |
908 // Create entries matching the folder and the bookmark above. | |
909 int64_t folder_id, bookmark_id; | |
910 { | |
911 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
912 folder_id = AddFolderToShare(&trans, "foobar"); | |
913 bookmark_id = AddBookmarkToShare(&trans, folder_id, "Airplanes", | |
914 "http://www.easyjet.com/"); | |
915 } | |
916 | |
917 // Associate the bookmark sync node with the native model one and make | |
918 // it look like it was deleted by a server update. | |
919 { | |
920 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
921 syncer::WriteNode node(&trans); | |
922 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(bookmark_id)); | |
923 | |
924 node.GetMutableEntryForTest()->PutLocalExternalId(bookmark->id()); | |
925 | |
926 MakeServerUpdate(&trans, &node); | |
927 node.GetMutableEntryForTest()->PutServerIsDel(true); | |
928 node.GetMutableEntryForTest()->PutIsDel(true); | |
929 } | |
930 | |
931 ASSERT_TRUE(AssociateModels()); | |
932 ExpectModelMatch(); | |
933 | |
934 // The bookmark node should be deleted. | |
935 EXPECT_EQ(0, folder->child_count()); | |
936 } | |
937 | |
938 // Tests that the external ID is used to match the right folder amoung | |
939 // multiple folders with the same name during the native model traversal. | |
940 // Also tests that the external ID is used to match the right bookmark | |
941 // among multiple identical bookmarks when dealing with the delete journal. | |
942 TEST_F(ProfileSyncServiceBookmarkTest, | |
943 InitialModelAssociateVerifyExternalIdMatch) { | |
944 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
945 const int kNumFolders = 10; | |
946 const int kNumBookmarks = 10; | |
947 const int kFolderToIncludeBookmarks = 7; | |
948 const int kBookmarkToDelete = 4; | |
949 | |
950 int64_t folder_ids[kNumFolders]; | |
951 int64_t bookmark_ids[kNumBookmarks]; | |
952 | |
953 // Create native folders and bookmarks with identical names. Only | |
954 // one of the folders contains bookmarks and others are empty. Here is the | |
955 // expected tree shape: | |
956 // Bookmarks bar | |
957 // +- folder (#0) | |
958 // +- folder (#1) | |
959 // ... | |
960 // +- folder (#7) <-- Only this folder contains bookmarks. | |
961 // +- bookmark (#0) | |
962 // +- bookmark (#1) | |
963 // ... | |
964 // +- bookmark (#4) <-- Only this one bookmark should be removed later. | |
965 // ... | |
966 // +- bookmark (#9) | |
967 // ... | |
968 // +- folder (#9) | |
969 | |
970 const BookmarkNode* parent_folder = nullptr; | |
971 for (int i = 0; i < kNumFolders; i++) { | |
972 const BookmarkNode* folder = model()->AddFolder( | |
973 model()->bookmark_bar_node(), i, base::ASCIIToUTF16("folder")); | |
974 folder_ids[i] = folder->id(); | |
975 if (i == kFolderToIncludeBookmarks) { | |
976 parent_folder = folder; | |
977 } | |
978 } | |
979 | |
980 for (int i = 0; i < kNumBookmarks; i++) { | |
981 const BookmarkNode* bookmark = | |
982 model()->AddURL(parent_folder, i, base::ASCIIToUTF16("bookmark"), | |
983 GURL("http://www.google.com/")); | |
984 bookmark_ids[i] = bookmark->id(); | |
985 } | |
986 | |
987 // Number of nodes in bookmark bar before association. | |
988 int total_node_count = model()->bookmark_bar_node()->GetTotalNodeCount(); | |
989 | |
990 CreatePermanentBookmarkNodes(); | |
991 | |
992 int64_t sync_bookmark_id_to_delete = 0; | |
993 { | |
994 // Create sync folders matching native folders above. | |
995 int64_t parent_id = 0; | |
996 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
997 // Create in reverse order because AddFolderToShare passes NULL for | |
998 // |predecessor| argument. | |
999 for (int i = kNumFolders - 1; i >= 0; i--) { | |
1000 int64_t id = AddFolderToShare(&trans, "folder"); | |
1001 | |
1002 // Pre-map sync folders to native folders by setting | |
1003 // external ID. This will verify that the association algorithm picks | |
1004 // the right ones despite all of them having identical names. | |
1005 // More specifically this will help to avoid cloning bookmarks from | |
1006 // a wrong folder. | |
1007 syncer::WriteNode node(&trans); | |
1008 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
1009 node.GetMutableEntryForTest()->PutLocalExternalId(folder_ids[i]); | |
1010 | |
1011 if (i == kFolderToIncludeBookmarks) { | |
1012 parent_id = id; | |
1013 } | |
1014 } | |
1015 | |
1016 // Create sync bookmark matching native bookmarks above in reverse order | |
1017 // because AddBookmarkToShare passes NULL for |predecessor| argument. | |
1018 for (int i = kNumBookmarks - 1; i >= 0; i--) { | |
1019 int id = AddBookmarkToShare(&trans, parent_id, "bookmark", | |
1020 "http://www.google.com/"); | |
1021 | |
1022 // Pre-map sync bookmarks to native bookmarks by setting | |
1023 // external ID. This will verify that the association algorithm picks | |
1024 // the right ones despite all of them having identical names and URLs. | |
1025 syncer::WriteNode node(&trans); | |
1026 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
1027 node.GetMutableEntryForTest()->PutLocalExternalId(bookmark_ids[i]); | |
1028 | |
1029 if (i == kBookmarkToDelete) { | |
1030 sync_bookmark_id_to_delete = id; | |
1031 } | |
1032 } | |
1033 } | |
1034 | |
1035 // Make one bookmark deleted. | |
1036 { | |
1037 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
1038 syncer::WriteNode node(&trans); | |
1039 EXPECT_EQ(BaseNode::INIT_OK, | |
1040 node.InitByIdLookup(sync_bookmark_id_to_delete)); | |
1041 | |
1042 MakeServerUpdate(&trans, &node); | |
1043 node.GetMutableEntryForTest()->PutServerIsDel(true); | |
1044 node.GetMutableEntryForTest()->PutIsDel(true); | |
1045 } | |
1046 | |
1047 // Perform association. | |
1048 ASSERT_TRUE(AssociateModels()); | |
1049 ExpectModelMatch(); | |
1050 | |
1051 // Only one native node should have been deleted and no nodes cloned due to | |
1052 // matching folder names. | |
1053 EXPECT_EQ(kNumFolders, model()->bookmark_bar_node()->child_count()); | |
1054 EXPECT_EQ(kNumBookmarks - 1, parent_folder->child_count()); | |
1055 EXPECT_EQ(total_node_count - 1, | |
1056 model()->bookmark_bar_node()->GetTotalNodeCount()); | |
1057 | |
1058 // Verify that the right bookmark got deleted and no bookmarks reordered. | |
1059 for (int i = 0; i < parent_folder->child_count(); i++) { | |
1060 int index_in_bookmark_ids = (i < kBookmarkToDelete) ? i : i + 1; | |
1061 EXPECT_EQ(bookmark_ids[index_in_bookmark_ids], | |
1062 parent_folder->GetChild(i)->id()); | |
1063 } | |
1064 } | |
1065 | |
1066 // Verifies that the bookmark association skips sync nodes with invalid URLs. | |
1067 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociateWithInvalidUrl) { | |
1068 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
1069 // On the local side create a folder and two nodes. | |
1070 const BookmarkNode* folder = model()->AddFolder( | |
1071 model()->bookmark_bar_node(), 0, base::ASCIIToUTF16("folder")); | |
1072 model()->AddURL(folder, 0, base::ASCIIToUTF16("node1"), | |
1073 GURL("http://www.node1.com/")); | |
1074 model()->AddURL(folder, 1, base::ASCIIToUTF16("node2"), | |
1075 GURL("http://www.node2.com/")); | |
1076 | |
1077 // On the sync side create a matching folder, one matching node, one | |
1078 // unmatching node, and one node with an invalid URL. | |
1079 CreatePermanentBookmarkNodes(); | |
1080 { | |
1081 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
1082 int64_t folder_id = AddFolderToShare(&trans, "folder"); | |
1083 // Please note that each AddBookmarkToShare inserts the node at the front | |
1084 // so the actual order of children in the directory will be opposite. | |
1085 AddBookmarkToShare(&trans, folder_id, "node2", "http://www.node2.com/"); | |
1086 AddBookmarkToShare(&trans, folder_id, "node3", ""); | |
1087 AddBookmarkToShare(&trans, folder_id, "node4", "http://www.node4.com/"); | |
1088 } | |
1089 | |
1090 // Perform association. | |
1091 StartSync(); | |
1092 | |
1093 // Concatenate resulting titles of native nodes. | |
1094 std::string native_titles; | |
1095 for (int i = 0; i < folder->child_count(); i++) { | |
1096 if (!native_titles.empty()) | |
1097 native_titles += ","; | |
1098 const BookmarkNode* child = folder->GetChild(i); | |
1099 native_titles += base::UTF16ToUTF8(child->GetTitle()); | |
1100 } | |
1101 | |
1102 // Expect the order of nodes to follow the sync order (see note above), the | |
1103 // node with the invalid URL to be skipped, and the extra native node to be | |
1104 // at the end. | |
1105 EXPECT_EQ("node4,node2,node1", native_titles); | |
1106 } | |
1107 | |
1108 TEST_F(ProfileSyncServiceBookmarkTest, BookmarkModelOperations) { | |
1109 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
1110 StartSync(); | |
1111 | |
1112 // Test addition. | |
1113 const BookmarkNode* folder = model()->AddFolder(model()->other_node(), 0, | |
1114 base::ASCIIToUTF16("foobar")); | |
1115 ExpectSyncerNodeMatching(folder); | |
1116 ExpectModelMatch(); | |
1117 const BookmarkNode* folder2 = | |
1118 model()->AddFolder(folder, 0, base::ASCIIToUTF16("nested")); | |
1119 ExpectSyncerNodeMatching(folder2); | |
1120 ExpectModelMatch(); | |
1121 const BookmarkNode* url1 = | |
1122 model()->AddURL(folder, 0, base::ASCIIToUTF16("Internets #1 Pies Site"), | |
1123 GURL("http://www.easypie.com/")); | |
1124 ExpectSyncerNodeMatching(url1); | |
1125 ExpectModelMatch(); | |
1126 const BookmarkNode* url2 = | |
1127 model()->AddURL(folder, 1, base::ASCIIToUTF16("Airplanes"), | |
1128 GURL("http://www.easyjet.com/")); | |
1129 ExpectSyncerNodeMatching(url2); | |
1130 ExpectModelMatch(); | |
1131 // Test addition. | |
1132 const BookmarkNode* mobile_folder = | |
1133 model()->AddFolder(model()->mobile_node(), 0, base::ASCIIToUTF16("pie")); | |
1134 ExpectSyncerNodeMatching(mobile_folder); | |
1135 ExpectModelMatch(); | |
1136 | |
1137 // Test modification. | |
1138 model()->SetTitle(url2, base::ASCIIToUTF16("EasyJet")); | |
1139 ExpectModelMatch(); | |
1140 model()->Move(url1, folder2, 0); | |
1141 ExpectModelMatch(); | |
1142 model()->Move(folder2, model()->bookmark_bar_node(), 0); | |
1143 ExpectModelMatch(); | |
1144 model()->SetTitle(folder2, base::ASCIIToUTF16("Not Nested")); | |
1145 ExpectModelMatch(); | |
1146 model()->Move(folder, folder2, 0); | |
1147 ExpectModelMatch(); | |
1148 model()->SetTitle(folder, base::ASCIIToUTF16("who's nested now?")); | |
1149 ExpectModelMatch(); | |
1150 model()->Copy(url2, model()->bookmark_bar_node(), 0); | |
1151 ExpectModelMatch(); | |
1152 model()->SetTitle(mobile_folder, base::ASCIIToUTF16("strawberry")); | |
1153 ExpectModelMatch(); | |
1154 | |
1155 // Test deletion. | |
1156 // Delete a single item. | |
1157 model()->Remove(url2); | |
1158 ExpectModelMatch(); | |
1159 // Delete an item with several children. | |
1160 model()->Remove(folder2); | |
1161 ExpectModelMatch(); | |
1162 model()->Remove(model()->mobile_node()->GetChild(0)); | |
1163 ExpectModelMatch(); | |
1164 } | |
1165 | |
1166 TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeProcessing) { | |
1167 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
1168 StartSync(); | |
1169 | |
1170 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
1171 | |
1172 FakeServerChange adds(&trans); | |
1173 int64_t f1 = adds.AddFolder("Server Folder B", bookmark_bar_id(), 0); | |
1174 int64_t f2 = adds.AddFolder("Server Folder A", bookmark_bar_id(), f1); | |
1175 int64_t u1 = adds.AddURL("Some old site", "ftp://nifty.andrew.cmu.edu/", | |
1176 bookmark_bar_id(), f2); | |
1177 int64_t u2 = adds.AddURL("Nifty", "ftp://nifty.andrew.cmu.edu/", f1, 0); | |
1178 // u3 is a duplicate URL | |
1179 int64_t u3 = adds.AddURL("Nifty2", "ftp://nifty.andrew.cmu.edu/", f1, u2); | |
1180 // u4 is a duplicate title, different URL. | |
1181 adds.AddURL("Some old site", "http://slog.thestranger.com/", | |
1182 bookmark_bar_id(), u1); | |
1183 // u5 tests an empty-string title. | |
1184 std::string javascript_url( | |
1185 "javascript:(function(){var w=window.open(" | |
1186 "'about:blank','gnotesWin','location=0,menubar=0," | |
1187 "scrollbars=0,status=0,toolbar=0,width=300," | |
1188 "height=300,resizable');});"); | |
1189 adds.AddURL(std::string(), javascript_url, other_bookmarks_id(), 0); | |
1190 int64_t u6 = adds.AddURL("Sync1", "http://www.syncable.edu/", | |
1191 mobile_bookmarks_id(), 0); | |
1192 | |
1193 syncer::ChangeRecordList::const_iterator it; | |
1194 // The bookmark model shouldn't yet have seen any of the nodes of |adds|. | |
1195 for (it = adds.changes().begin(); it != adds.changes().end(); ++it) | |
1196 ExpectBrowserNodeUnknown(it->id); | |
1197 | |
1198 adds.ApplyPendingChanges(change_processor()); | |
1199 | |
1200 // Make sure the bookmark model received all of the nodes in |adds|. | |
1201 for (it = adds.changes().begin(); it != adds.changes().end(); ++it) | |
1202 ExpectBrowserNodeMatching(&trans, it->id); | |
1203 ExpectModelMatch(&trans); | |
1204 | |
1205 // Part two: test modifications. | |
1206 FakeServerChange mods(&trans); | |
1207 // Mess with u2, and move it into empty folder f2 | |
1208 // TODO(ncarter): Determine if we allow ModifyURL ops or not. | |
1209 /* std::string u2_old_url = mods.ModifyURL(u2, "http://www.google.com"); */ | |
1210 std::string u2_old_title = mods.ModifyTitle(u2, "The Google"); | |
1211 int64_t u2_old_parent = mods.ModifyPosition(u2, f2, 0); | |
1212 | |
1213 // Now move f1 after u2. | |
1214 std::string f1_old_title = mods.ModifyTitle(f1, "Server Folder C"); | |
1215 int64_t f1_old_parent = mods.ModifyPosition(f1, f2, u2); | |
1216 | |
1217 // Then add u3 after f1. | |
1218 int64_t u3_old_parent = mods.ModifyPosition(u3, f2, f1); | |
1219 | |
1220 std::string u6_old_title = mods.ModifyTitle(u6, "Mobile Folder A"); | |
1221 | |
1222 // Test that the property changes have not yet taken effect. | |
1223 ExpectBrowserNodeTitle(u2, u2_old_title); | |
1224 /* ExpectBrowserNodeURL(u2, u2_old_url); */ | |
1225 ExpectBrowserNodeParent(u2, u2_old_parent); | |
1226 | |
1227 ExpectBrowserNodeTitle(f1, f1_old_title); | |
1228 ExpectBrowserNodeParent(f1, f1_old_parent); | |
1229 | |
1230 ExpectBrowserNodeParent(u3, u3_old_parent); | |
1231 | |
1232 ExpectBrowserNodeTitle(u6, u6_old_title); | |
1233 | |
1234 // Apply the changes. | |
1235 mods.ApplyPendingChanges(change_processor()); | |
1236 | |
1237 // Check for successful application. | |
1238 for (it = mods.changes().begin(); it != mods.changes().end(); ++it) | |
1239 ExpectBrowserNodeMatching(&trans, it->id); | |
1240 ExpectModelMatch(&trans); | |
1241 | |
1242 // Part 3: Test URL deletion. | |
1243 FakeServerChange dels(&trans); | |
1244 dels.Delete(u2); | |
1245 dels.Delete(u3); | |
1246 dels.Delete(u6); | |
1247 | |
1248 ExpectBrowserNodeKnown(u2); | |
1249 ExpectBrowserNodeKnown(u3); | |
1250 | |
1251 dels.ApplyPendingChanges(change_processor()); | |
1252 | |
1253 ExpectBrowserNodeUnknown(u2); | |
1254 ExpectBrowserNodeUnknown(u3); | |
1255 ExpectBrowserNodeUnknown(u6); | |
1256 ExpectModelMatch(&trans); | |
1257 } | |
1258 | |
1259 // Tests a specific case in ApplyModelChanges where we move the | |
1260 // children out from under a parent, and then delete the parent | |
1261 // in the same changelist. The delete shows up first in the changelist, | |
1262 // requiring the children to be moved to a temporary location. | |
1263 TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeRequiringFosterParent) { | |
1264 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
1265 StartSync(); | |
1266 | |
1267 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
1268 | |
1269 // Stress the immediate children of other_node because that's where | |
1270 // ApplyModelChanges puts a temporary foster parent node. | |
1271 std::string url("http://dev.chromium.org/"); | |
1272 FakeServerChange adds(&trans); | |
1273 int64_t f0 = other_bookmarks_id(); // + other_node | |
1274 int64_t f1 = adds.AddFolder("f1", f0, 0); // + f1 | |
1275 int64_t f2 = adds.AddFolder("f2", f1, 0); // + f2 | |
1276 int64_t u3 = adds.AddURL("u3", url, f2, 0); // + u3 NOLINT | |
1277 int64_t u4 = adds.AddURL("u4", url, f2, u3); // + u4 NOLINT | |
1278 int64_t u5 = adds.AddURL("u5", url, f1, f2); // + u5 NOLINT | |
1279 int64_t f6 = adds.AddFolder("f6", f1, u5); // + f6 | |
1280 int64_t u7 = adds.AddURL("u7", url, f0, f1); // + u7 NOLINT | |
1281 | |
1282 syncer::ChangeRecordList::const_iterator it; | |
1283 // The bookmark model shouldn't yet have seen any of the nodes of |adds|. | |
1284 for (it = adds.changes().begin(); it != adds.changes().end(); ++it) | |
1285 ExpectBrowserNodeUnknown(it->id); | |
1286 | |
1287 adds.ApplyPendingChanges(change_processor()); | |
1288 | |
1289 // Make sure the bookmark model received all of the nodes in |adds|. | |
1290 for (it = adds.changes().begin(); it != adds.changes().end(); ++it) | |
1291 ExpectBrowserNodeMatching(&trans, it->id); | |
1292 ExpectModelMatch(&trans); | |
1293 | |
1294 // We have to do the moves before the deletions, but FakeServerChange will | |
1295 // put the deletion at the front of the changelist. | |
1296 FakeServerChange ops(&trans); | |
1297 ops.ModifyPosition(f6, other_bookmarks_id(), 0); | |
1298 ops.ModifyPosition(u3, other_bookmarks_id(), f1); // Prev == f1 is OK here. | |
1299 ops.ModifyPosition(f2, other_bookmarks_id(), u7); | |
1300 ops.ModifyPosition(u7, f2, 0); | |
1301 ops.ModifyPosition(u4, other_bookmarks_id(), f2); | |
1302 ops.ModifyPosition(u5, f6, 0); | |
1303 ops.Delete(f1); | |
1304 | |
1305 ops.ApplyPendingChanges(change_processor()); | |
1306 | |
1307 ExpectModelMatch(&trans); | |
1308 } | |
1309 | |
1310 // Simulate a server change record containing a valid but non-canonical URL. | |
1311 TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeWithNonCanonicalURL) { | |
1312 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); | |
1313 StartSync(); | |
1314 | |
1315 { | |
1316 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
1317 | |
1318 FakeServerChange adds(&trans); | |
1319 std::string url("http://dev.chromium.org"); | |
1320 EXPECT_NE(GURL(url).spec(), url); | |
1321 adds.AddURL("u1", url, other_bookmarks_id(), 0); | |
1322 | |
1323 adds.ApplyPendingChanges(change_processor()); | |
1324 | |
1325 EXPECT_EQ(1, model()->other_node()->child_count()); | |
1326 ExpectModelMatch(&trans); | |
1327 } | |
1328 | |
1329 // Now reboot the sync service, forcing a merge step. | |
1330 StopSync(); | |
1331 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE); | |
1332 StartSync(); | |
1333 | |
1334 // There should still be just the one bookmark. | |
1335 EXPECT_EQ(1, model()->other_node()->child_count()); | |
1336 ExpectModelMatch(); | |
1337 } | |
1338 | |
1339 // Simulate a server change record containing an invalid URL (per GURL). | |
1340 // TODO(ncarter): Disabled due to crashes. Fix bug 1677563. | |
1341 TEST_F(ProfileSyncServiceBookmarkTest, DISABLED_ServerChangeWithInvalidURL) { | |
1342 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); | |
1343 StartSync(); | |
1344 | |
1345 int child_count = 0; | |
1346 { | |
1347 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
1348 | |
1349 FakeServerChange adds(&trans); | |
1350 std::string url("x"); | |
1351 EXPECT_FALSE(GURL(url).is_valid()); | |
1352 adds.AddURL("u1", url, other_bookmarks_id(), 0); | |
1353 | |
1354 adds.ApplyPendingChanges(change_processor()); | |
1355 | |
1356 // We're lenient about what should happen -- the model could wind up with | |
1357 // the node or without it; but things should be consistent, and we | |
1358 // shouldn't crash. | |
1359 child_count = model()->other_node()->child_count(); | |
1360 EXPECT_TRUE(child_count == 0 || child_count == 1); | |
1361 ExpectModelMatch(&trans); | |
1362 } | |
1363 | |
1364 // Now reboot the sync service, forcing a merge step. | |
1365 StopSync(); | |
1366 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE); | |
1367 StartSync(); | |
1368 | |
1369 // Things ought not to have changed. | |
1370 EXPECT_EQ(model()->other_node()->child_count(), child_count); | |
1371 ExpectModelMatch(); | |
1372 } | |
1373 | |
1374 // Test strings that might pose a problem if the titles ever became used as | |
1375 // file names in the sync backend. | |
1376 TEST_F(ProfileSyncServiceBookmarkTest, CornerCaseNames) { | |
1377 // TODO(ncarter): Bug 1570238 explains the failure of this test. | |
1378 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); | |
1379 StartSync(); | |
1380 | |
1381 const char* names[] = { | |
1382 // The empty string. | |
1383 "", | |
1384 // Illegal Windows filenames. | |
1385 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", | |
1386 "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", | |
1387 "LPT6", "LPT7", "LPT8", "LPT9", | |
1388 // Current/parent directory markers. | |
1389 ".", "..", "...", | |
1390 // Files created automatically by the Windows shell. | |
1391 "Thumbs.db", ".DS_Store", | |
1392 // Names including Win32-illegal characters, and path separators. | |
1393 "foo/bar", "foo\\bar", "foo?bar", "foo:bar", "foo|bar", "foo\"bar", | |
1394 "foo'bar", "foo<bar", "foo>bar", "foo%bar", "foo*bar", "foo]bar", | |
1395 "foo[bar", | |
1396 // A name with title > 255 characters | |
1397 "012345678901234567890123456789012345678901234567890123456789012345678901" | |
1398 "234567890123456789012345678901234567890123456789012345678901234567890123" | |
1399 "456789012345678901234567890123456789012345678901234567890123456789012345" | |
1400 "678901234567890123456789012345678901234567890123456789012345678901234567" | |
1401 "890123456789"}; | |
1402 // Create both folders and bookmarks using each name. | |
1403 GURL url("http://www.doublemint.com"); | |
1404 for (size_t i = 0; i < arraysize(names); ++i) { | |
1405 model()->AddFolder(model()->other_node(), 0, base::ASCIIToUTF16(names[i])); | |
1406 model()->AddURL(model()->other_node(), 0, base::ASCIIToUTF16(names[i]), | |
1407 url); | |
1408 } | |
1409 | |
1410 // Verify that the browser model matches the sync model. | |
1411 EXPECT_EQ(static_cast<size_t>(model()->other_node()->child_count()), | |
1412 2 * arraysize(names)); | |
1413 ExpectModelMatch(); | |
1414 | |
1415 // Restart and re-associate. Verify things still match. | |
1416 StopSync(); | |
1417 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE); | |
1418 StartSync(); | |
1419 EXPECT_EQ(static_cast<size_t>(model()->other_node()->child_count()), | |
1420 2 * arraysize(names)); | |
1421 ExpectModelMatch(); | |
1422 } | |
1423 | |
1424 // Stress the internal representation of position by sparse numbers. We want | |
1425 // to repeatedly bisect the range of available positions, to force the | |
1426 // syncer code to renumber its ranges. Pick a number big enough so that it | |
1427 // would exhaust 32bits of room between items a couple of times. | |
1428 TEST_F(ProfileSyncServiceBookmarkTest, RepeatedMiddleInsertion) { | |
1429 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
1430 StartSync(); | |
1431 | |
1432 static const int kTimesToInsert = 256; | |
1433 | |
1434 // Create two book-end nodes to insert between. | |
1435 model()->AddFolder(model()->other_node(), 0, base::ASCIIToUTF16("Alpha")); | |
1436 model()->AddFolder(model()->other_node(), 1, base::ASCIIToUTF16("Omega")); | |
1437 int count = 2; | |
1438 | |
1439 // Test insertion in first half of range by repeatedly inserting in second | |
1440 // position. | |
1441 for (int i = 0; i < kTimesToInsert; ++i) { | |
1442 base::string16 title = | |
1443 base::ASCIIToUTF16("Pre-insertion ") + base::IntToString16(i); | |
1444 model()->AddFolder(model()->other_node(), 1, title); | |
1445 count++; | |
1446 } | |
1447 | |
1448 // Test insertion in second half of range by repeatedly inserting in | |
1449 // second-to-last position. | |
1450 for (int i = 0; i < kTimesToInsert; ++i) { | |
1451 base::string16 title = | |
1452 base::ASCIIToUTF16("Post-insertion ") + base::IntToString16(i); | |
1453 model()->AddFolder(model()->other_node(), count - 1, title); | |
1454 count++; | |
1455 } | |
1456 | |
1457 // Verify that the browser model matches the sync model. | |
1458 EXPECT_EQ(model()->other_node()->child_count(), count); | |
1459 ExpectModelMatch(); | |
1460 } | |
1461 | |
1462 // Introduce a consistency violation into the model, and see that it | |
1463 // puts itself into a lame, error state. | |
1464 TEST_F(ProfileSyncServiceBookmarkTest, UnrecoverableErrorSuspendsService) { | |
1465 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
1466 StartSync(); | |
1467 | |
1468 // Add a node which will be the target of the consistency violation. | |
1469 const BookmarkNode* node = | |
1470 model()->AddFolder(model()->other_node(), 0, base::ASCIIToUTF16("node")); | |
1471 ExpectSyncerNodeMatching(node); | |
1472 | |
1473 // Now destroy the syncer node as if we were the ProfileSyncService without | |
1474 // updating the ProfileSyncService state. This should introduce | |
1475 // inconsistency between the two models. | |
1476 { | |
1477 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
1478 syncer::WriteNode sync_node(&trans); | |
1479 ASSERT_TRUE(InitSyncNodeFromChromeNode(node, &sync_node)); | |
1480 sync_node.Tombstone(); | |
1481 } | |
1482 // The models don't match at this point, but the ProfileSyncService | |
1483 // doesn't know it yet. | |
1484 ExpectSyncerNodeKnown(node); | |
1485 | |
1486 mock_error_handler()->ExpectError(syncer::SyncError::DATATYPE_ERROR); | |
1487 | |
1488 // Add a child to the inconsistent node. This should cause detection of the | |
1489 // problem and the syncer should stop processing changes. | |
1490 model()->AddFolder(node, 0, base::ASCIIToUTF16("nested")); | |
1491 } | |
1492 | |
1493 // See what happens if we run model association when there are two exact URL | |
1494 // duplicate bookmarks. The BookmarkModelAssociator should not fall over when | |
1495 // this happens. | |
1496 TEST_F(ProfileSyncServiceBookmarkTest, MergeDuplicates) { | |
1497 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); | |
1498 StartSync(); | |
1499 | |
1500 model()->AddURL(model()->other_node(), 0, base::ASCIIToUTF16("Dup"), | |
1501 GURL("http://dup.com/")); | |
1502 model()->AddURL(model()->other_node(), 0, base::ASCIIToUTF16("Dup"), | |
1503 GURL("http://dup.com/")); | |
1504 | |
1505 EXPECT_EQ(2, model()->other_node()->child_count()); | |
1506 | |
1507 // Restart the sync service to trigger model association. | |
1508 StopSync(); | |
1509 StartSync(); | |
1510 | |
1511 EXPECT_EQ(2, model()->other_node()->child_count()); | |
1512 ExpectModelMatch(); | |
1513 } | |
1514 | |
1515 TEST_F(ProfileSyncServiceBookmarkTest, ApplySyncDeletesFromJournal) { | |
1516 // Initialize sync model and bookmark model as: | |
1517 // URL 0 | |
1518 // Folder 1 | |
1519 // |-- URL 1 | |
1520 // +-- Folder 2 | |
1521 // +-- URL 2 | |
1522 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); | |
1523 int64_t u0 = 0; | |
1524 int64_t f1 = 0; | |
1525 int64_t u1 = 0; | |
1526 int64_t f2 = 0; | |
1527 int64_t u2 = 0; | |
1528 StartSync(); | |
1529 int fixed_sync_bk_count = GetSyncBookmarkCount(); | |
1530 { | |
1531 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
1532 FakeServerChange adds(&trans); | |
1533 u0 = adds.AddURL("URL 0", "http://plus.google.com/", bookmark_bar_id(), 0); | |
1534 f1 = adds.AddFolder("Folder 1", bookmark_bar_id(), u0); | |
1535 u1 = adds.AddURL("URL 1", "http://www.google.com/", f1, 0); | |
1536 f2 = adds.AddFolder("Folder 2", f1, u1); | |
1537 u2 = adds.AddURL("URL 2", "http://mail.google.com/", f2, 0); | |
1538 adds.ApplyPendingChanges(change_processor()); | |
1539 } | |
1540 StopSync(); | |
1541 | |
1542 // Reload bookmark model and disable model saving to make sync changes not | |
1543 // persisted. | |
1544 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE); | |
1545 EXPECT_EQ(6, model()->bookmark_bar_node()->GetTotalNodeCount()); | |
1546 EXPECT_EQ(fixed_sync_bk_count + 5, GetSyncBookmarkCount()); | |
1547 StartSync(); | |
1548 { | |
1549 // Remove all folders/bookmarks except u3 added above. | |
1550 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
1551 MakeServerUpdate(&trans, f1); | |
1552 MakeServerUpdate(&trans, u1); | |
1553 MakeServerUpdate(&trans, f2); | |
1554 MakeServerUpdate(&trans, u2); | |
1555 FakeServerChange dels(&trans); | |
1556 dels.Delete(u2); | |
1557 dels.Delete(f2); | |
1558 dels.Delete(u1); | |
1559 dels.Delete(f1); | |
1560 dels.ApplyPendingChanges(change_processor()); | |
1561 } | |
1562 StopSync(); | |
1563 // Bookmark bar itself and u0 remain. | |
1564 EXPECT_EQ(2, model()->bookmark_bar_node()->GetTotalNodeCount()); | |
1565 | |
1566 // Reload bookmarks including ones deleted in sync model from storage. | |
1567 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE); | |
1568 EXPECT_EQ(6, model()->bookmark_bar_node()->GetTotalNodeCount()); | |
1569 // Add a bookmark under f1 when sync is off so that f1 will not be | |
1570 // deleted even when f1 matches delete journal because it's not empty. | |
1571 model()->AddURL(model()->bookmark_bar_node()->GetChild(1), 0, | |
1572 base::UTF8ToUTF16("local"), GURL("http://www.youtube.com")); | |
1573 // Sync model has fixed bookmarks nodes and u3. | |
1574 EXPECT_EQ(fixed_sync_bk_count + 1, GetSyncBookmarkCount()); | |
1575 StartSync(); | |
1576 // Expect 4 bookmarks after model association because u2, f2, u1 are removed | |
1577 // by delete journal, f1 is not removed by delete journal because it's | |
1578 // not empty due to www.youtube.com added above. | |
1579 EXPECT_EQ(4, model()->bookmark_bar_node()->GetTotalNodeCount()); | |
1580 EXPECT_EQ(base::UTF8ToUTF16("URL 0"), | |
1581 model()->bookmark_bar_node()->GetChild(0)->GetTitle()); | |
1582 EXPECT_EQ(base::UTF8ToUTF16("Folder 1"), | |
1583 model()->bookmark_bar_node()->GetChild(1)->GetTitle()); | |
1584 EXPECT_EQ(base::UTF8ToUTF16("local"), | |
1585 model()->bookmark_bar_node()->GetChild(1)->GetChild(0)->GetTitle()); | |
1586 StopSync(); | |
1587 | |
1588 // Verify purging of delete journals. | |
1589 // Delete journals for u2, f2, u1 remains because they are used in last | |
1590 // association. | |
1591 EXPECT_EQ(3u, test_user_share()->GetDeleteJournalSize()); | |
1592 StartSync(); | |
1593 StopSync(); | |
1594 // Reload again and all delete journals should be gone because none is used | |
1595 // in last association. | |
1596 ASSERT_TRUE(test_user_share()->Reload()); | |
1597 EXPECT_EQ(0u, test_user_share()->GetDeleteJournalSize()); | |
1598 } | |
1599 | |
1600 struct TestData { | |
1601 const char* title; | |
1602 const char* url; | |
1603 }; | |
1604 | |
1605 // Map from bookmark node ID to its version. | |
1606 typedef std::map<int64_t, int64_t> BookmarkNodeVersionMap; | |
1607 | |
1608 // TODO(ncarter): Integrate the existing TestNode/PopulateNodeFromString code | |
1609 // in the bookmark model unittest, to make it simpler to set up test data | |
1610 // here (and reduce the amount of duplication among tests), and to reduce the | |
1611 // duplication. | |
1612 class ProfileSyncServiceBookmarkTestWithData | |
1613 : public ProfileSyncServiceBookmarkTest { | |
1614 public: | |
1615 ProfileSyncServiceBookmarkTestWithData(); | |
1616 | |
1617 protected: | |
1618 // Populates or compares children of the given bookmark node from/with the | |
1619 // given test data array with the given size. |running_count| is updated as | |
1620 // urls are added. It is used to set the creation date (or test the creation | |
1621 // date for CompareWithTestData()). | |
1622 void PopulateFromTestData(const BookmarkNode* node, | |
1623 const TestData* data, | |
1624 int size, | |
1625 int* running_count); | |
1626 void CompareWithTestData(const BookmarkNode* node, | |
1627 const TestData* data, | |
1628 int size, | |
1629 int* running_count); | |
1630 | |
1631 void ExpectBookmarkModelMatchesTestData(); | |
1632 void WriteTestDataToBookmarkModel(); | |
1633 | |
1634 // Output transaction versions of |node| and nodes under it to | |
1635 // |node_versions|. | |
1636 void GetTransactionVersions(const BookmarkNode* root, | |
1637 BookmarkNodeVersionMap* node_versions); | |
1638 | |
1639 // Verify transaction versions of bookmark nodes and sync nodes are equal | |
1640 // recursively. If node is in |version_expected|, versions should match | |
1641 // there, too. | |
1642 void ExpectTransactionVersionMatch( | |
1643 const BookmarkNode* node, | |
1644 const BookmarkNodeVersionMap& version_expected); | |
1645 | |
1646 private: | |
1647 const base::Time start_time_; | |
1648 | |
1649 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceBookmarkTestWithData); | |
1650 }; | |
1651 | |
1652 namespace { | |
1653 | |
1654 // Constants for bookmark model that looks like: | |
1655 // |-- Bookmark bar | |
1656 // | |-- u2, http://www.u2.com/ | |
1657 // | |-- f1 | |
1658 // | | |-- f1u4, http://www.f1u4.com/ | |
1659 // | | |-- f1u2, http://www.f1u2.com/ | |
1660 // | | |-- f1u3, http://www.f1u3.com/ | |
1661 // | | +-- f1u1, http://www.f1u1.com/ | |
1662 // | |-- u1, http://www.u1.com/ | |
1663 // | +-- f2 | |
1664 // | |-- f2u2, http://www.f2u2.com/ | |
1665 // | |-- f2u4, http://www.f2u4.com/ | |
1666 // | |-- f2u3, http://www.f2u3.com/ | |
1667 // | +-- f2u1, http://www.f2u1.com/ | |
1668 // +-- Other bookmarks | |
1669 // | |-- f3 | |
1670 // | | |-- f3u4, http://www.f3u4.com/ | |
1671 // | | |-- f3u2, http://www.f3u2.com/ | |
1672 // | | |-- f3u3, http://www.f3u3.com/ | |
1673 // | | +-- f3u1, http://www.f3u1.com/ | |
1674 // | |-- u4, http://www.u4.com/ | |
1675 // | |-- u3, http://www.u3.com/ | |
1676 // | --- f4 | |
1677 // | | |-- f4u1, http://www.f4u1.com/ | |
1678 // | | |-- f4u2, http://www.f4u2.com/ | |
1679 // | | |-- f4u3, http://www.f4u3.com/ | |
1680 // | | +-- f4u4, http://www.f4u4.com/ | |
1681 // | |-- dup | |
1682 // | | +-- dupu1, http://www.dupu1.com/ | |
1683 // | +-- dup | |
1684 // | | +-- dupu2, http://www.dupu1.com/ | |
1685 // | +-- ls , http://www.ls.com/ | |
1686 // | | |
1687 // +-- Mobile bookmarks | |
1688 // |-- f5 | |
1689 // | |-- f5u1, http://www.f5u1.com/ | |
1690 // |-- f6 | |
1691 // | |-- f6u1, http://www.f6u1.com/ | |
1692 // | |-- f6u2, http://www.f6u2.com/ | |
1693 // +-- u5, http://www.u5.com/ | |
1694 | |
1695 static TestData kBookmarkBarChildren[] = { | |
1696 {"u2", "http://www.u2.com/"}, | |
1697 {"f1", NULL}, | |
1698 {"u1", "http://www.u1.com/"}, | |
1699 {"f2", NULL}, | |
1700 }; | |
1701 static TestData kF1Children[] = { | |
1702 {"f1u4", "http://www.f1u4.com/"}, | |
1703 {"f1u2", "http://www.f1u2.com/"}, | |
1704 {"f1u3", "http://www.f1u3.com/"}, | |
1705 {"f1u1", "http://www.f1u1.com/"}, | |
1706 }; | |
1707 static TestData kF2Children[] = { | |
1708 {"f2u2", "http://www.f2u2.com/"}, | |
1709 {"f2u4", "http://www.f2u4.com/"}, | |
1710 {"f2u3", "http://www.f2u3.com/"}, | |
1711 {"f2u1", "http://www.f2u1.com/"}, | |
1712 }; | |
1713 | |
1714 static TestData kOtherBookmarkChildren[] = {{"f3", NULL}, | |
1715 {"u4", "http://www.u4.com/"}, | |
1716 {"u3", "http://www.u3.com/"}, | |
1717 {"f4", NULL}, | |
1718 {"dup", NULL}, | |
1719 {"dup", NULL}, | |
1720 {" ls ", "http://www.ls.com/"}}; | |
1721 static TestData kF3Children[] = { | |
1722 {"f3u4", "http://www.f3u4.com/"}, | |
1723 {"f3u2", "http://www.f3u2.com/"}, | |
1724 {"f3u3", "http://www.f3u3.com/"}, | |
1725 {"f3u1", "http://www.f3u1.com/"}, | |
1726 }; | |
1727 static TestData kF4Children[] = { | |
1728 {"f4u1", "http://www.f4u1.com/"}, | |
1729 {"f4u2", "http://www.f4u2.com/"}, | |
1730 {"f4u3", "http://www.f4u3.com/"}, | |
1731 {"f4u4", "http://www.f4u4.com/"}, | |
1732 }; | |
1733 static TestData kDup1Children[] = { | |
1734 {"dupu1", "http://www.dupu1.com/"}, | |
1735 }; | |
1736 static TestData kDup2Children[] = { | |
1737 {"dupu2", "http://www.dupu2.com/"}, | |
1738 }; | |
1739 | |
1740 static TestData kMobileBookmarkChildren[] = { | |
1741 {"f5", NULL}, | |
1742 {"f6", NULL}, | |
1743 {"u5", "http://www.u5.com/"}, | |
1744 }; | |
1745 static TestData kF5Children[] = { | |
1746 {"f5u1", "http://www.f5u1.com/"}, | |
1747 {"f5u2", "http://www.f5u2.com/"}, | |
1748 }; | |
1749 static TestData kF6Children[] = { | |
1750 {"f6u1", "http://www.f6u1.com/"}, | |
1751 {"f6u2", "http://www.f6u2.com/"}, | |
1752 }; | |
1753 | |
1754 } // anonymous namespace. | |
1755 | |
1756 ProfileSyncServiceBookmarkTestWithData::ProfileSyncServiceBookmarkTestWithData() | |
1757 : start_time_(base::Time::Now()) {} | |
1758 | |
1759 void ProfileSyncServiceBookmarkTestWithData::PopulateFromTestData( | |
1760 const BookmarkNode* node, | |
1761 const TestData* data, | |
1762 int size, | |
1763 int* running_count) { | |
1764 DCHECK(node); | |
1765 DCHECK(data); | |
1766 DCHECK(node->is_folder()); | |
1767 for (int i = 0; i < size; ++i) { | |
1768 const TestData& item = data[i]; | |
1769 if (item.url) { | |
1770 const base::Time add_time = | |
1771 start_time_ + base::TimeDelta::FromMinutes(*running_count); | |
1772 model()->AddURLWithCreationTimeAndMetaInfo( | |
1773 node, i, base::UTF8ToUTF16(item.title), GURL(item.url), add_time, | |
1774 NULL); | |
1775 } else { | |
1776 model()->AddFolder(node, i, base::UTF8ToUTF16(item.title)); | |
1777 } | |
1778 (*running_count)++; | |
1779 } | |
1780 } | |
1781 | |
1782 void ProfileSyncServiceBookmarkTestWithData::CompareWithTestData( | |
1783 const BookmarkNode* node, | |
1784 const TestData* data, | |
1785 int size, | |
1786 int* running_count) { | |
1787 DCHECK(node); | |
1788 DCHECK(data); | |
1789 DCHECK(node->is_folder()); | |
1790 ASSERT_EQ(size, node->child_count()); | |
1791 for (int i = 0; i < size; ++i) { | |
1792 const BookmarkNode* child_node = node->GetChild(i); | |
1793 const TestData& item = data[i]; | |
1794 GURL url = GURL(item.url == NULL ? "" : item.url); | |
1795 BookmarkNode test_node(url); | |
1796 test_node.SetTitle(base::UTF8ToUTF16(item.title)); | |
1797 EXPECT_EQ(child_node->GetTitle(), test_node.GetTitle()); | |
1798 if (item.url) { | |
1799 EXPECT_FALSE(child_node->is_folder()); | |
1800 EXPECT_TRUE(child_node->is_url()); | |
1801 EXPECT_EQ(child_node->url(), test_node.url()); | |
1802 const base::Time expected_time = | |
1803 start_time_ + base::TimeDelta::FromMinutes(*running_count); | |
1804 EXPECT_EQ(expected_time.ToInternalValue(), | |
1805 child_node->date_added().ToInternalValue()); | |
1806 } else { | |
1807 EXPECT_TRUE(child_node->is_folder()); | |
1808 EXPECT_FALSE(child_node->is_url()); | |
1809 } | |
1810 (*running_count)++; | |
1811 } | |
1812 } | |
1813 | |
1814 // TODO(munjal): We should implement some way of generating random data and can | |
1815 // use the same seed to generate the same sequence. | |
1816 void ProfileSyncServiceBookmarkTestWithData::WriteTestDataToBookmarkModel() { | |
1817 const BookmarkNode* bookmarks_bar_node = model()->bookmark_bar_node(); | |
1818 int count = 0; | |
1819 PopulateFromTestData(bookmarks_bar_node, kBookmarkBarChildren, | |
1820 arraysize(kBookmarkBarChildren), &count); | |
1821 | |
1822 ASSERT_GE(bookmarks_bar_node->child_count(), 4); | |
1823 const BookmarkNode* f1_node = bookmarks_bar_node->GetChild(1); | |
1824 PopulateFromTestData(f1_node, kF1Children, arraysize(kF1Children), &count); | |
1825 const BookmarkNode* f2_node = bookmarks_bar_node->GetChild(3); | |
1826 PopulateFromTestData(f2_node, kF2Children, arraysize(kF2Children), &count); | |
1827 | |
1828 const BookmarkNode* other_bookmarks_node = model()->other_node(); | |
1829 PopulateFromTestData(other_bookmarks_node, kOtherBookmarkChildren, | |
1830 arraysize(kOtherBookmarkChildren), &count); | |
1831 | |
1832 ASSERT_GE(other_bookmarks_node->child_count(), 6); | |
1833 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0); | |
1834 PopulateFromTestData(f3_node, kF3Children, arraysize(kF3Children), &count); | |
1835 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3); | |
1836 PopulateFromTestData(f4_node, kF4Children, arraysize(kF4Children), &count); | |
1837 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4); | |
1838 PopulateFromTestData(dup_node, kDup1Children, arraysize(kDup1Children), | |
1839 &count); | |
1840 dup_node = other_bookmarks_node->GetChild(5); | |
1841 PopulateFromTestData(dup_node, kDup2Children, arraysize(kDup2Children), | |
1842 &count); | |
1843 | |
1844 const BookmarkNode* mobile_bookmarks_node = model()->mobile_node(); | |
1845 PopulateFromTestData(mobile_bookmarks_node, kMobileBookmarkChildren, | |
1846 arraysize(kMobileBookmarkChildren), &count); | |
1847 | |
1848 ASSERT_GE(mobile_bookmarks_node->child_count(), 3); | |
1849 const BookmarkNode* f5_node = mobile_bookmarks_node->GetChild(0); | |
1850 PopulateFromTestData(f5_node, kF5Children, arraysize(kF5Children), &count); | |
1851 const BookmarkNode* f6_node = mobile_bookmarks_node->GetChild(1); | |
1852 PopulateFromTestData(f6_node, kF6Children, arraysize(kF6Children), &count); | |
1853 | |
1854 ExpectBookmarkModelMatchesTestData(); | |
1855 } | |
1856 | |
1857 void ProfileSyncServiceBookmarkTestWithData:: | |
1858 ExpectBookmarkModelMatchesTestData() { | |
1859 const BookmarkNode* bookmark_bar_node = model()->bookmark_bar_node(); | |
1860 int count = 0; | |
1861 CompareWithTestData(bookmark_bar_node, kBookmarkBarChildren, | |
1862 arraysize(kBookmarkBarChildren), &count); | |
1863 | |
1864 ASSERT_GE(bookmark_bar_node->child_count(), 4); | |
1865 const BookmarkNode* f1_node = bookmark_bar_node->GetChild(1); | |
1866 CompareWithTestData(f1_node, kF1Children, arraysize(kF1Children), &count); | |
1867 const BookmarkNode* f2_node = bookmark_bar_node->GetChild(3); | |
1868 CompareWithTestData(f2_node, kF2Children, arraysize(kF2Children), &count); | |
1869 | |
1870 const BookmarkNode* other_bookmarks_node = model()->other_node(); | |
1871 CompareWithTestData(other_bookmarks_node, kOtherBookmarkChildren, | |
1872 arraysize(kOtherBookmarkChildren), &count); | |
1873 | |
1874 ASSERT_GE(other_bookmarks_node->child_count(), 6); | |
1875 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0); | |
1876 CompareWithTestData(f3_node, kF3Children, arraysize(kF3Children), &count); | |
1877 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3); | |
1878 CompareWithTestData(f4_node, kF4Children, arraysize(kF4Children), &count); | |
1879 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4); | |
1880 CompareWithTestData(dup_node, kDup1Children, arraysize(kDup1Children), | |
1881 &count); | |
1882 dup_node = other_bookmarks_node->GetChild(5); | |
1883 CompareWithTestData(dup_node, kDup2Children, arraysize(kDup2Children), | |
1884 &count); | |
1885 | |
1886 const BookmarkNode* mobile_bookmarks_node = model()->mobile_node(); | |
1887 CompareWithTestData(mobile_bookmarks_node, kMobileBookmarkChildren, | |
1888 arraysize(kMobileBookmarkChildren), &count); | |
1889 | |
1890 ASSERT_GE(mobile_bookmarks_node->child_count(), 3); | |
1891 const BookmarkNode* f5_node = mobile_bookmarks_node->GetChild(0); | |
1892 CompareWithTestData(f5_node, kF5Children, arraysize(kF5Children), &count); | |
1893 const BookmarkNode* f6_node = mobile_bookmarks_node->GetChild(1); | |
1894 CompareWithTestData(f6_node, kF6Children, arraysize(kF6Children), &count); | |
1895 } | |
1896 | |
1897 // Tests persistence of the profile sync service by unloading the | |
1898 // database and then reloading it from disk. | |
1899 TEST_F(ProfileSyncServiceBookmarkTestWithData, Persistence) { | |
1900 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); | |
1901 StartSync(); | |
1902 | |
1903 WriteTestDataToBookmarkModel(); | |
1904 | |
1905 ExpectModelMatch(); | |
1906 | |
1907 // Force both models to discard their data and reload from disk. This | |
1908 // simulates what would happen if the browser were to shutdown normally, | |
1909 // and then relaunch. | |
1910 StopSync(); | |
1911 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE); | |
1912 StartSync(); | |
1913 | |
1914 ExpectBookmarkModelMatchesTestData(); | |
1915 | |
1916 // With the BookmarkModel contents verified, ExpectModelMatch will | |
1917 // verify the contents of the sync model. | |
1918 ExpectModelMatch(); | |
1919 } | |
1920 | |
1921 // Tests the merge case when the BookmarkModel is non-empty but the | |
1922 // sync model is empty. This corresponds to uploading browser | |
1923 // bookmarks to an initially empty, new account. | |
1924 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeWithEmptySyncModel) { | |
1925 // Don't start the sync service until we've populated the bookmark model. | |
1926 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); | |
1927 | |
1928 WriteTestDataToBookmarkModel(); | |
1929 | |
1930 // Restart sync. This should trigger a merge step during | |
1931 // initialization -- we expect the browser bookmarks to be written | |
1932 // to the sync service during this call. | |
1933 StartSync(); | |
1934 | |
1935 // Verify that the bookmark model hasn't changed, and that the sync model | |
1936 // matches it exactly. | |
1937 ExpectBookmarkModelMatchesTestData(); | |
1938 ExpectModelMatch(); | |
1939 } | |
1940 | |
1941 // Tests the merge case when the BookmarkModel is empty but the sync model is | |
1942 // non-empty. This corresponds (somewhat) to a clean install of the browser, | |
1943 // with no bookmarks, connecting to a sync account that has some bookmarks. | |
1944 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeWithEmptyBookmarkModel) { | |
1945 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
1946 StartSync(); | |
1947 | |
1948 WriteTestDataToBookmarkModel(); | |
1949 | |
1950 ExpectModelMatch(); | |
1951 | |
1952 // Force the databse to unload and write itself to disk. | |
1953 StopSync(); | |
1954 | |
1955 // Blow away the bookmark model -- it should be empty afterwards. | |
1956 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
1957 EXPECT_EQ(model()->bookmark_bar_node()->child_count(), 0); | |
1958 EXPECT_EQ(model()->other_node()->child_count(), 0); | |
1959 EXPECT_EQ(model()->mobile_node()->child_count(), 0); | |
1960 | |
1961 // Now restart the sync service. Starting it should populate the bookmark | |
1962 // model -- test for consistency. | |
1963 StartSync(); | |
1964 ExpectBookmarkModelMatchesTestData(); | |
1965 ExpectModelMatch(); | |
1966 } | |
1967 | |
1968 // Tests the merge cases when both the models are expected to be identical | |
1969 // after the merge. | |
1970 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeExpectedIdenticalModels) { | |
1971 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); | |
1972 StartSync(); | |
1973 WriteTestDataToBookmarkModel(); | |
1974 ExpectModelMatch(); | |
1975 StopSync(); | |
1976 | |
1977 // At this point both the bookmark model and the server should have the | |
1978 // exact same data and it should match the test data. | |
1979 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE); | |
1980 StartSync(); | |
1981 ExpectBookmarkModelMatchesTestData(); | |
1982 ExpectModelMatch(); | |
1983 StopSync(); | |
1984 | |
1985 // Now reorder some bookmarks in the bookmark model and then merge. Make | |
1986 // sure we get the order of the server after merge. | |
1987 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE); | |
1988 ExpectBookmarkModelMatchesTestData(); | |
1989 const BookmarkNode* bookmark_bar = model()->bookmark_bar_node(); | |
1990 ASSERT_TRUE(bookmark_bar); | |
1991 ASSERT_GT(bookmark_bar->child_count(), 1); | |
1992 model()->Move(bookmark_bar->GetChild(0), bookmark_bar, 1); | |
1993 StartSync(); | |
1994 ExpectModelMatch(); | |
1995 ExpectBookmarkModelMatchesTestData(); | |
1996 } | |
1997 | |
1998 // Tests the merge cases when both the models are expected to be identical | |
1999 // after the merge. | |
2000 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeModelsWithSomeExtras) { | |
2001 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2002 WriteTestDataToBookmarkModel(); | |
2003 ExpectBookmarkModelMatchesTestData(); | |
2004 | |
2005 // Remove some nodes and reorder some nodes. | |
2006 const BookmarkNode* bookmark_bar_node = model()->bookmark_bar_node(); | |
2007 int remove_index = 2; | |
2008 ASSERT_GT(bookmark_bar_node->child_count(), remove_index); | |
2009 const BookmarkNode* child_node = bookmark_bar_node->GetChild(remove_index); | |
2010 ASSERT_TRUE(child_node); | |
2011 ASSERT_TRUE(child_node->is_url()); | |
2012 model()->Remove(bookmark_bar_node->GetChild(remove_index)); | |
2013 ASSERT_GT(bookmark_bar_node->child_count(), remove_index); | |
2014 child_node = bookmark_bar_node->GetChild(remove_index); | |
2015 ASSERT_TRUE(child_node); | |
2016 ASSERT_TRUE(child_node->is_folder()); | |
2017 model()->Remove(bookmark_bar_node->GetChild(remove_index)); | |
2018 | |
2019 const BookmarkNode* other_node = model()->other_node(); | |
2020 ASSERT_GE(other_node->child_count(), 1); | |
2021 const BookmarkNode* f3_node = other_node->GetChild(0); | |
2022 ASSERT_TRUE(f3_node); | |
2023 ASSERT_TRUE(f3_node->is_folder()); | |
2024 remove_index = 2; | |
2025 ASSERT_GT(f3_node->child_count(), remove_index); | |
2026 model()->Remove(f3_node->GetChild(remove_index)); | |
2027 ASSERT_GT(f3_node->child_count(), remove_index); | |
2028 model()->Remove(f3_node->GetChild(remove_index)); | |
2029 | |
2030 StartSync(); | |
2031 ExpectModelMatch(); | |
2032 StopSync(); | |
2033 | |
2034 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2035 WriteTestDataToBookmarkModel(); | |
2036 ExpectBookmarkModelMatchesTestData(); | |
2037 | |
2038 // Remove some nodes and reorder some nodes. | |
2039 bookmark_bar_node = model()->bookmark_bar_node(); | |
2040 remove_index = 0; | |
2041 ASSERT_GT(bookmark_bar_node->child_count(), remove_index); | |
2042 child_node = bookmark_bar_node->GetChild(remove_index); | |
2043 ASSERT_TRUE(child_node); | |
2044 ASSERT_TRUE(child_node->is_url()); | |
2045 model()->Remove(bookmark_bar_node->GetChild(remove_index)); | |
2046 ASSERT_GT(bookmark_bar_node->child_count(), remove_index); | |
2047 child_node = bookmark_bar_node->GetChild(remove_index); | |
2048 ASSERT_TRUE(child_node); | |
2049 ASSERT_TRUE(child_node->is_folder()); | |
2050 model()->Remove(bookmark_bar_node->GetChild(remove_index)); | |
2051 | |
2052 ASSERT_GE(bookmark_bar_node->child_count(), 2); | |
2053 model()->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 1); | |
2054 | |
2055 other_node = model()->other_node(); | |
2056 ASSERT_GE(other_node->child_count(), 1); | |
2057 f3_node = other_node->GetChild(0); | |
2058 ASSERT_TRUE(f3_node); | |
2059 ASSERT_TRUE(f3_node->is_folder()); | |
2060 remove_index = 0; | |
2061 ASSERT_GT(f3_node->child_count(), remove_index); | |
2062 model()->Remove(f3_node->GetChild(remove_index)); | |
2063 ASSERT_GT(f3_node->child_count(), remove_index); | |
2064 model()->Remove(f3_node->GetChild(remove_index)); | |
2065 | |
2066 ASSERT_GE(other_node->child_count(), 4); | |
2067 model()->Move(other_node->GetChild(0), other_node, 1); | |
2068 model()->Move(other_node->GetChild(2), other_node, 3); | |
2069 | |
2070 StartSync(); | |
2071 ExpectModelMatch(); | |
2072 | |
2073 // After the merge, the model should match the test data. | |
2074 ExpectBookmarkModelMatchesTestData(); | |
2075 } | |
2076 | |
2077 // Tests that when persisted model associations are used, things work fine. | |
2078 TEST_F(ProfileSyncServiceBookmarkTestWithData, ModelAssociationPersistence) { | |
2079 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2080 WriteTestDataToBookmarkModel(); | |
2081 StartSync(); | |
2082 ExpectModelMatch(); | |
2083 // Force sync to shut down and write itself to disk. | |
2084 StopSync(); | |
2085 // Now restart sync. This time it should use the persistent | |
2086 // associations. | |
2087 StartSync(); | |
2088 ExpectModelMatch(); | |
2089 } | |
2090 | |
2091 // Tests that when persisted model associations are used, things work fine. | |
2092 TEST_F(ProfileSyncServiceBookmarkTestWithData, | |
2093 ModelAssociationInvalidPersistence) { | |
2094 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2095 WriteTestDataToBookmarkModel(); | |
2096 StartSync(); | |
2097 ExpectModelMatch(); | |
2098 // Force sync to shut down and write itself to disk. | |
2099 StopSync(); | |
2100 // Change the bookmark model before restarting sync service to simulate | |
2101 // the situation where bookmark model is different from sync model and | |
2102 // make sure model associator correctly rebuilds associations. | |
2103 const BookmarkNode* bookmark_bar_node = model()->bookmark_bar_node(); | |
2104 model()->AddURL(bookmark_bar_node, 0, base::ASCIIToUTF16("xtra"), | |
2105 GURL("http://www.xtra.com")); | |
2106 // Now restart sync. This time it will try to use the persistent | |
2107 // associations and realize that they are invalid and hence will rebuild | |
2108 // associations. | |
2109 StartSync(); | |
2110 ExpectModelMatch(); | |
2111 } | |
2112 | |
2113 TEST_F(ProfileSyncServiceBookmarkTestWithData, SortChildren) { | |
2114 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2115 StartSync(); | |
2116 | |
2117 // Write test data to bookmark model and verify that the models match. | |
2118 WriteTestDataToBookmarkModel(); | |
2119 const BookmarkNode* folder_added = model()->other_node()->GetChild(0); | |
2120 ASSERT_TRUE(folder_added); | |
2121 ASSERT_TRUE(folder_added->is_folder()); | |
2122 | |
2123 ExpectModelMatch(); | |
2124 | |
2125 // Sort the other-bookmarks children and expect that the models match. | |
2126 model()->SortChildren(folder_added); | |
2127 ExpectModelMatch(); | |
2128 } | |
2129 | |
2130 // See what happens if we enable sync but then delete the "Sync Data" | |
2131 // folder. | |
2132 TEST_F(ProfileSyncServiceBookmarkTestWithData, | |
2133 RecoverAfterDeletingSyncDataDirectory) { | |
2134 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); | |
2135 StartSync(); | |
2136 | |
2137 WriteTestDataToBookmarkModel(); | |
2138 | |
2139 StopSync(); | |
2140 | |
2141 // Nuke the sync DB and reload. | |
2142 TearDown(); | |
2143 SetUp(); | |
2144 | |
2145 // First attempt fails due to a persistence error. | |
2146 EXPECT_TRUE(CreatePermanentBookmarkNodes()); | |
2147 EXPECT_FALSE(AssociateModels()); | |
2148 | |
2149 // Second attempt succeeds due to the previous error resetting the native | |
2150 // transaction version. | |
2151 delete_model_associator(); | |
2152 EXPECT_TRUE(CreatePermanentBookmarkNodes()); | |
2153 EXPECT_TRUE(AssociateModels()); | |
2154 | |
2155 // Make sure we're back in sync. In real life, the user would need | |
2156 // to reauthenticate before this happens, but in the test, authentication | |
2157 // is sidestepped. | |
2158 ExpectBookmarkModelMatchesTestData(); | |
2159 ExpectModelMatch(); | |
2160 } | |
2161 | |
2162 // Verify that the bookmark model is updated about whether the | |
2163 // associator is currently running. | |
2164 TEST_F(ProfileSyncServiceBookmarkTest, AssociationState) { | |
2165 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2166 | |
2167 ExtensiveChangesBookmarkModelObserver observer; | |
2168 model()->AddObserver(&observer); | |
2169 | |
2170 StartSync(); | |
2171 | |
2172 EXPECT_EQ(1, observer.get_started()); | |
2173 EXPECT_EQ(0, observer.get_completed_count_at_started()); | |
2174 EXPECT_EQ(1, observer.get_completed()); | |
2175 | |
2176 model()->RemoveObserver(&observer); | |
2177 } | |
2178 | |
2179 // Verify that the creation_time_us changes are applied in the local model at | |
2180 // association time and update time. | |
2181 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateDateAdded) { | |
2182 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2183 WriteTestDataToBookmarkModel(); | |
2184 | |
2185 // Start and stop sync in order to create bookmark nodes in the sync db. | |
2186 StartSync(); | |
2187 StopSync(); | |
2188 | |
2189 // Modify the date_added field of a bookmark so it doesn't match with | |
2190 // the sync data. | |
2191 const BookmarkNode* bookmark_bar_node = model()->bookmark_bar_node(); | |
2192 int modified_index = 2; | |
2193 ASSERT_GT(bookmark_bar_node->child_count(), modified_index); | |
2194 const BookmarkNode* child_node = bookmark_bar_node->GetChild(modified_index); | |
2195 ASSERT_TRUE(child_node); | |
2196 EXPECT_TRUE(child_node->is_url()); | |
2197 model()->SetDateAdded(child_node, base::Time::FromInternalValue(10)); | |
2198 | |
2199 StartSync(); | |
2200 StopSync(); | |
2201 | |
2202 // Verify that transaction versions are in sync between the native model | |
2203 // and Sync. | |
2204 { | |
2205 syncer::ReadTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
2206 int64_t sync_version = trans.GetModelVersion(syncer::BOOKMARKS); | |
2207 int64_t native_version = model()->root_node()->sync_transaction_version(); | |
2208 EXPECT_EQ(native_version, sync_version); | |
2209 } | |
2210 | |
2211 // Since the version is in sync the association above should have skipped | |
2212 // updating the native node above. That is expected optimization (see | |
2213 // crbug/464907. | |
2214 EXPECT_EQ(child_node->date_added(), base::Time::FromInternalValue(10)); | |
2215 | |
2216 // Reset transaction version on the native model to trigger updating data | |
2217 // for all bookmark nodes. | |
2218 model()->SetNodeSyncTransactionVersion( | |
2219 model()->root_node(), syncer::syncable::kInvalidTransactionVersion); | |
2220 | |
2221 StartSync(); | |
2222 | |
2223 // Everything should be back in sync after model association. | |
2224 ExpectBookmarkModelMatchesTestData(); | |
2225 ExpectModelMatch(); | |
2226 | |
2227 // Now trigger a change while syncing. We add a new bookmark, sync it, then | |
2228 // updates it's creation time. | |
2229 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
2230 FakeServerChange adds(&trans); | |
2231 const std::string kTitle = "Some site"; | |
2232 const std::string kUrl = "http://www.whatwhat.yeah/"; | |
2233 const int kCreationTime = 30; | |
2234 int64_t id = adds.AddURL(kTitle, kUrl, bookmark_bar_id(), 0); | |
2235 adds.ApplyPendingChanges(change_processor()); | |
2236 FakeServerChange updates(&trans); | |
2237 updates.ModifyCreationTime(id, kCreationTime); | |
2238 updates.ApplyPendingChanges(change_processor()); | |
2239 | |
2240 const BookmarkNode* node = model()->bookmark_bar_node()->GetChild(0); | |
2241 ASSERT_TRUE(node); | |
2242 EXPECT_TRUE(node->is_url()); | |
2243 EXPECT_EQ(base::UTF8ToUTF16(kTitle), node->GetTitle()); | |
2244 EXPECT_EQ(kUrl, node->url().possibly_invalid_spec()); | |
2245 EXPECT_EQ(node->date_added(), base::Time::FromInternalValue(30)); | |
2246 } | |
2247 | |
2248 // Verifies that the transaction version in the native bookmark model gets | |
2249 // updated and synced with the sync transaction version even when the | |
2250 // association doesn't modify any sync nodes. This is necessary to ensure | |
2251 // that the native transaction doesn't get stuck at "unset" version and skips | |
2252 // any further consistency checks. | |
2253 TEST_F(ProfileSyncServiceBookmarkTestWithData, | |
2254 NativeTransactionVersionUpdated) { | |
2255 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2256 WriteTestDataToBookmarkModel(); | |
2257 | |
2258 // Start sync in order to create bookmark nodes in the sync db. | |
2259 StartSync(); | |
2260 StopSync(); | |
2261 | |
2262 // Reset transaction version on the native mode to "unset". | |
2263 model()->SetNodeSyncTransactionVersion( | |
2264 model()->root_node(), syncer::syncable::kInvalidTransactionVersion); | |
2265 | |
2266 // Restart sync. | |
2267 StartSync(); | |
2268 StopSync(); | |
2269 | |
2270 // Verify that the native transaction version has been updated and is now | |
2271 // in sync with the sync version. | |
2272 { | |
2273 syncer::ReadTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
2274 int64_t sync_version = trans.GetModelVersion(syncer::BOOKMARKS); | |
2275 int64_t native_version = model()->root_node()->sync_transaction_version(); | |
2276 EXPECT_EQ(native_version, sync_version); | |
2277 } | |
2278 } | |
2279 | |
2280 // Tests that changes to the sync nodes meta info gets reflected in the local | |
2281 // bookmark model. | |
2282 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateMetaInfoFromSync) { | |
2283 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2284 WriteTestDataToBookmarkModel(); | |
2285 StartSync(); | |
2286 | |
2287 // Create bookmark nodes containing meta info. | |
2288 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
2289 FakeServerChange adds(&trans); | |
2290 BookmarkNode::MetaInfoMap folder_meta_info; | |
2291 folder_meta_info["folder"] = "foldervalue"; | |
2292 int64_t folder_id = adds.AddFolderWithMetaInfo( | |
2293 "folder title", &folder_meta_info, bookmark_bar_id(), 0); | |
2294 BookmarkNode::MetaInfoMap node_meta_info; | |
2295 node_meta_info["node"] = "nodevalue"; | |
2296 node_meta_info["other"] = "othervalue"; | |
2297 int64_t id = adds.AddURLWithMetaInfo("node title", "http://www.foo.com", | |
2298 &node_meta_info, folder_id, 0); | |
2299 adds.ApplyPendingChanges(change_processor()); | |
2300 | |
2301 // Verify that the nodes are created with the correct meta info. | |
2302 ASSERT_LT(0, model()->bookmark_bar_node()->child_count()); | |
2303 const BookmarkNode* folder_node = model()->bookmark_bar_node()->GetChild(0); | |
2304 ASSERT_TRUE(folder_node->GetMetaInfoMap()); | |
2305 EXPECT_EQ(folder_meta_info, *folder_node->GetMetaInfoMap()); | |
2306 ASSERT_LT(0, folder_node->child_count()); | |
2307 const BookmarkNode* node = folder_node->GetChild(0); | |
2308 ASSERT_TRUE(node->GetMetaInfoMap()); | |
2309 EXPECT_EQ(node_meta_info, *node->GetMetaInfoMap()); | |
2310 | |
2311 // Update meta info on nodes on server | |
2312 FakeServerChange updates(&trans); | |
2313 folder_meta_info.erase("folder"); | |
2314 updates.ModifyMetaInfo(folder_id, folder_meta_info); | |
2315 node_meta_info["node"] = "changednodevalue"; | |
2316 node_meta_info.erase("other"); | |
2317 node_meta_info["newkey"] = "newkeyvalue"; | |
2318 updates.ModifyMetaInfo(id, node_meta_info); | |
2319 updates.ApplyPendingChanges(change_processor()); | |
2320 | |
2321 // Confirm that the updated values are reflected in the bookmark nodes. | |
2322 EXPECT_FALSE(folder_node->GetMetaInfoMap()); | |
2323 ASSERT_TRUE(node->GetMetaInfoMap()); | |
2324 EXPECT_EQ(node_meta_info, *node->GetMetaInfoMap()); | |
2325 } | |
2326 | |
2327 // Tests that changes to the local bookmark nodes meta info gets reflected in | |
2328 // the sync nodes. | |
2329 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateMetaInfoFromModel) { | |
2330 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2331 WriteTestDataToBookmarkModel(); | |
2332 StartSync(); | |
2333 ExpectBookmarkModelMatchesTestData(); | |
2334 | |
2335 const BookmarkNode* folder_node = model()->AddFolder( | |
2336 model()->bookmark_bar_node(), 0, base::ASCIIToUTF16("folder title")); | |
2337 const BookmarkNode* node = | |
2338 model()->AddURL(folder_node, 0, base::ASCIIToUTF16("node title"), | |
2339 GURL("http://www.foo.com")); | |
2340 ExpectModelMatch(); | |
2341 | |
2342 // Add some meta info and verify sync model matches the changes. | |
2343 model()->SetNodeMetaInfo(folder_node, "folder", "foldervalue"); | |
2344 model()->SetNodeMetaInfo(node, "node", "nodevalue"); | |
2345 model()->SetNodeMetaInfo(node, "other", "othervalue"); | |
2346 ExpectModelMatch(); | |
2347 | |
2348 // Change/delete existing meta info and verify. | |
2349 model()->DeleteNodeMetaInfo(folder_node, "folder"); | |
2350 model()->SetNodeMetaInfo(node, "node", "changednodevalue"); | |
2351 model()->DeleteNodeMetaInfo(node, "other"); | |
2352 model()->SetNodeMetaInfo(node, "newkey", "newkeyvalue"); | |
2353 ExpectModelMatch(); | |
2354 } | |
2355 | |
2356 // Tests that node's specifics doesn't get unnecessarily overwritten (causing | |
2357 // a subsequent commit) when BookmarkChangeProcessor handles a notification | |
2358 // (such as BookmarkMetaInfoChanged) without an actual data change. | |
2359 TEST_F(ProfileSyncServiceBookmarkTestWithData, MetaInfoPreservedOnNonChange) { | |
2360 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2361 WriteTestDataToBookmarkModel(); | |
2362 StartSync(); | |
2363 | |
2364 std::string orig_specifics; | |
2365 int64_t sync_id; | |
2366 const BookmarkNode* bookmark; | |
2367 | |
2368 // Create bookmark folder node containing meta info. | |
2369 { | |
2370 syncer::WriteTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
2371 FakeServerChange adds(&trans); | |
2372 | |
2373 int64_t folder_id = adds.AddFolder("folder title", bookmark_bar_id(), 0); | |
2374 | |
2375 BookmarkNode::MetaInfoMap node_meta_info; | |
2376 node_meta_info["one"] = "1"; | |
2377 node_meta_info["two"] = "2"; | |
2378 node_meta_info["three"] = "3"; | |
2379 | |
2380 sync_id = adds.AddURLWithMetaInfo("node title", "http://www.foo.com/", | |
2381 &node_meta_info, folder_id, 0); | |
2382 | |
2383 // Verify that the node propagates to the bookmark model | |
2384 adds.ApplyPendingChanges(change_processor()); | |
2385 | |
2386 bookmark = model()->bookmark_bar_node()->GetChild(0)->GetChild(0); | |
2387 EXPECT_EQ(node_meta_info, *bookmark->GetMetaInfoMap()); | |
2388 | |
2389 syncer::ReadNode sync_node(&trans); | |
2390 EXPECT_EQ(BaseNode::INIT_OK, sync_node.InitByIdLookup(sync_id)); | |
2391 orig_specifics = sync_node.GetBookmarkSpecifics().SerializeAsString(); | |
2392 } | |
2393 | |
2394 // Force change processor to update the sync node. | |
2395 change_processor()->BookmarkMetaInfoChanged(model(), bookmark); | |
2396 | |
2397 // Read bookmark specifics again and verify that there is no change. | |
2398 { | |
2399 syncer::ReadTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
2400 syncer::ReadNode sync_node(&trans); | |
2401 EXPECT_EQ(BaseNode::INIT_OK, sync_node.InitByIdLookup(sync_id)); | |
2402 std::string new_specifics = | |
2403 sync_node.GetBookmarkSpecifics().SerializeAsString(); | |
2404 ASSERT_EQ(orig_specifics, new_specifics); | |
2405 } | |
2406 } | |
2407 | |
2408 void ProfileSyncServiceBookmarkTestWithData::GetTransactionVersions( | |
2409 const BookmarkNode* root, | |
2410 BookmarkNodeVersionMap* node_versions) { | |
2411 node_versions->clear(); | |
2412 std::queue<const BookmarkNode*> nodes; | |
2413 nodes.push(root); | |
2414 while (!nodes.empty()) { | |
2415 const BookmarkNode* n = nodes.front(); | |
2416 nodes.pop(); | |
2417 | |
2418 int64_t version = n->sync_transaction_version(); | |
2419 EXPECT_NE(BookmarkNode::kInvalidSyncTransactionVersion, version); | |
2420 | |
2421 (*node_versions)[n->id()] = version; | |
2422 for (int i = 0; i < n->child_count(); ++i) { | |
2423 if (!CanSyncNode(n->GetChild(i))) | |
2424 continue; | |
2425 nodes.push(n->GetChild(i)); | |
2426 } | |
2427 } | |
2428 } | |
2429 | |
2430 void ProfileSyncServiceBookmarkTestWithData::ExpectTransactionVersionMatch( | |
2431 const BookmarkNode* node, | |
2432 const BookmarkNodeVersionMap& version_expected) { | |
2433 syncer::ReadTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
2434 | |
2435 BookmarkNodeVersionMap bnodes_versions; | |
2436 GetTransactionVersions(node, &bnodes_versions); | |
2437 for (BookmarkNodeVersionMap::const_iterator it = bnodes_versions.begin(); | |
2438 it != bnodes_versions.end(); ++it) { | |
2439 syncer::ReadNode sync_node(&trans); | |
2440 ASSERT_TRUE( | |
2441 model_associator()->InitSyncNodeFromChromeId(it->first, &sync_node)); | |
2442 EXPECT_EQ(sync_node.GetTransactionVersion(), it->second); | |
2443 BookmarkNodeVersionMap::const_iterator expected_ver_it = | |
2444 version_expected.find(it->first); | |
2445 if (expected_ver_it != version_expected.end()) | |
2446 EXPECT_EQ(expected_ver_it->second, it->second); | |
2447 } | |
2448 } | |
2449 | |
2450 // Test transaction versions of model and nodes are incremented after changes | |
2451 // are applied. | |
2452 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateTransactionVersion) { | |
2453 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2454 StartSync(); | |
2455 WriteTestDataToBookmarkModel(); | |
2456 base::RunLoop().RunUntilIdle(); | |
2457 | |
2458 BookmarkNodeVersionMap initial_versions; | |
2459 | |
2460 // Verify transaction versions in sync model and bookmark model (saved as | |
2461 // transaction version of root node) are equal after | |
2462 // WriteTestDataToBookmarkModel() created bookmarks. | |
2463 { | |
2464 syncer::ReadTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
2465 EXPECT_GT(trans.GetModelVersion(syncer::BOOKMARKS), 0); | |
2466 GetTransactionVersions(model()->root_node(), &initial_versions); | |
2467 EXPECT_EQ(trans.GetModelVersion(syncer::BOOKMARKS), | |
2468 initial_versions[model()->root_node()->id()]); | |
2469 } | |
2470 ExpectTransactionVersionMatch(model()->bookmark_bar_node(), | |
2471 BookmarkNodeVersionMap()); | |
2472 ExpectTransactionVersionMatch(model()->other_node(), | |
2473 BookmarkNodeVersionMap()); | |
2474 ExpectTransactionVersionMatch(model()->mobile_node(), | |
2475 BookmarkNodeVersionMap()); | |
2476 | |
2477 // Verify model version is incremented and bookmark node versions remain | |
2478 // the same. | |
2479 const BookmarkNode* bookmark_bar = model()->bookmark_bar_node(); | |
2480 model()->Remove(bookmark_bar->GetChild(0)); | |
2481 base::RunLoop().RunUntilIdle(); | |
2482 BookmarkNodeVersionMap new_versions; | |
2483 GetTransactionVersions(model()->root_node(), &new_versions); | |
2484 EXPECT_EQ(initial_versions[model()->root_node()->id()] + 1, | |
2485 new_versions[model()->root_node()->id()]); | |
2486 ExpectTransactionVersionMatch(model()->bookmark_bar_node(), initial_versions); | |
2487 ExpectTransactionVersionMatch(model()->other_node(), initial_versions); | |
2488 ExpectTransactionVersionMatch(model()->mobile_node(), initial_versions); | |
2489 | |
2490 // Verify model version and version of changed bookmark are incremented and | |
2491 // versions of others remain same. | |
2492 const BookmarkNode* changed_bookmark = | |
2493 model()->bookmark_bar_node()->GetChild(0); | |
2494 model()->SetTitle(changed_bookmark, base::ASCIIToUTF16("test")); | |
2495 base::RunLoop().RunUntilIdle(); | |
2496 GetTransactionVersions(model()->root_node(), &new_versions); | |
2497 EXPECT_EQ(initial_versions[model()->root_node()->id()] + 2, | |
2498 new_versions[model()->root_node()->id()]); | |
2499 EXPECT_LT(initial_versions[changed_bookmark->id()], | |
2500 new_versions[changed_bookmark->id()]); | |
2501 initial_versions.erase(changed_bookmark->id()); | |
2502 ExpectTransactionVersionMatch(model()->bookmark_bar_node(), initial_versions); | |
2503 ExpectTransactionVersionMatch(model()->other_node(), initial_versions); | |
2504 ExpectTransactionVersionMatch(model()->mobile_node(), initial_versions); | |
2505 } | |
2506 | |
2507 // Test that sync persistence errors are detected and trigger a failed | |
2508 // association. | |
2509 TEST_F(ProfileSyncServiceBookmarkTestWithData, PersistenceError) { | |
2510 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2511 StartSync(); | |
2512 WriteTestDataToBookmarkModel(); | |
2513 base::RunLoop().RunUntilIdle(); | |
2514 | |
2515 BookmarkNodeVersionMap initial_versions; | |
2516 | |
2517 // Verify transaction versions in sync model and bookmark model (saved as | |
2518 // transaction version of root node) are equal after | |
2519 // WriteTestDataToBookmarkModel() created bookmarks. | |
2520 { | |
2521 syncer::ReadTransaction trans(FROM_HERE, test_user_share()->user_share()); | |
2522 EXPECT_GT(trans.GetModelVersion(syncer::BOOKMARKS), 0); | |
2523 GetTransactionVersions(model()->root_node(), &initial_versions); | |
2524 EXPECT_EQ(trans.GetModelVersion(syncer::BOOKMARKS), | |
2525 initial_versions[model()->root_node()->id()]); | |
2526 } | |
2527 ExpectTransactionVersionMatch(model()->bookmark_bar_node(), | |
2528 BookmarkNodeVersionMap()); | |
2529 ExpectTransactionVersionMatch(model()->other_node(), | |
2530 BookmarkNodeVersionMap()); | |
2531 ExpectTransactionVersionMatch(model()->mobile_node(), | |
2532 BookmarkNodeVersionMap()); | |
2533 | |
2534 // Now shut down sync and artificially increment the native model's version. | |
2535 StopSync(); | |
2536 int64_t root_version = initial_versions[model()->root_node()->id()]; | |
2537 model()->SetNodeSyncTransactionVersion(model()->root_node(), | |
2538 root_version + 1); | |
2539 | |
2540 // Upon association, bookmarks should fail to associate. | |
2541 EXPECT_FALSE(AssociateModels()); | |
2542 } | |
2543 | |
2544 // It's possible for update/add calls from the bookmark model to be out of | |
2545 // order, or asynchronous. Handle that without triggering an error. | |
2546 TEST_F(ProfileSyncServiceBookmarkTest, UpdateThenAdd) { | |
2547 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2548 StartSync(); | |
2549 | |
2550 EXPECT_TRUE(other_bookmarks_id()); | |
2551 EXPECT_TRUE(bookmark_bar_id()); | |
2552 EXPECT_TRUE(mobile_bookmarks_id()); | |
2553 | |
2554 ExpectModelMatch(); | |
2555 | |
2556 // Now destroy the change processor then add a bookmark, to simulate | |
2557 // missing the Update call. | |
2558 delete_change_processor(); | |
2559 const BookmarkNode* node = | |
2560 model()->AddURL(model()->bookmark_bar_node(), 0, | |
2561 base::ASCIIToUTF16("title"), GURL("http://www.url.com")); | |
2562 | |
2563 // Recreate the change processor then update that bookmark. Sync should | |
2564 // receive the update call and gracefully treat that as if it were an add. | |
2565 ResetChangeProcessor(); | |
2566 change_processor()->Start(test_user_share()->user_share()); | |
2567 model()->SetTitle(node, base::ASCIIToUTF16("title2")); | |
2568 ExpectModelMatch(); | |
2569 | |
2570 // Then simulate the add call arriving late. | |
2571 change_processor()->BookmarkNodeAdded(model(), model()->bookmark_bar_node(), | |
2572 0); | |
2573 ExpectModelMatch(); | |
2574 } | |
2575 | |
2576 // Verify operations on native nodes that shouldn't be propagated to Sync. | |
2577 TEST_F(ProfileSyncServiceBookmarkTest, TestUnsupportedNodes) { | |
2578 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
2579 StartSync(); | |
2580 | |
2581 // Initial number of bookmarks on the sync side. | |
2582 int sync_bookmark_count = GetSyncBookmarkCount(); | |
2583 | |
2584 // Create a bookmark under managed_node() permanent folder. | |
2585 const BookmarkNode* folder = managed_bookmark_service()->managed_node(); | |
2586 const BookmarkNode* node = model()->AddURL( | |
2587 folder, 0, base::ASCIIToUTF16("node"), GURL("http://www.node.com/")); | |
2588 | |
2589 // Verify that these changes are ignored by Sync. | |
2590 EXPECT_EQ(sync_bookmark_count, GetSyncBookmarkCount()); | |
2591 int64_t sync_id = model_associator()->GetSyncIdFromChromeId(node->id()); | |
2592 EXPECT_EQ(syncer::kInvalidId, sync_id); | |
2593 | |
2594 // Verify that Sync ignores deleting this node. | |
2595 model()->Remove(node); | |
2596 EXPECT_EQ(sync_bookmark_count, GetSyncBookmarkCount()); | |
2597 } | |
2598 | |
2599 } // namespace | |
2600 | |
2601 } // namespace browser_sync | |
OLD | NEW |