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.
5 // TODO(akalin): This file is basically just a unit test for
6 // BookmarkChangeProcessor. Write unit tests for
7 // BookmarkModelAssociator separately.
14 #include "base/command_line.h"
15 #include "base/files/file_path.h"
16 #include "base/location.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/time/time.h"
25 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
26 #include "chrome/browser/sync/glue/bookmark_change_processor.h"
27 #include "chrome/browser/sync/glue/bookmark_model_associator.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/test/base/testing_profile.h"
30 #include "components/bookmarks/core/browser/base_bookmark_model_observer.h"
31 #include "components/bookmarks/core/browser/bookmark_model.h"
32 #include "components/bookmarks/core/test/bookmark_test_helpers.h"
33 #include "components/sync_driver/data_type_error_handler.h"
34 #include "components/sync_driver/data_type_error_handler_mock.h"
35 #include "content/public/test/test_browser_thread.h"
36 #include "sync/api/sync_error.h"
37 #include "sync/internal_api/public/change_record.h"
38 #include "sync/internal_api/public/read_node.h"
39 #include "sync/internal_api/public/read_transaction.h"
40 #include "sync/internal_api/public/test/test_user_share.h"
41 #include "sync/internal_api/public/write_node.h"
42 #include "sync/internal_api/public/write_transaction.h"
43 #include "sync/internal_api/syncapi_internal.h"
44 #include "sync/syncable/mutable_entry.h" // TODO(tim): Remove. Bug 131130.
45 #include "testing/gmock/include/gmock/gmock.h"
46 #include "testing/gtest/include/gtest/gtest.h"
48 namespace browser_sync {
50 using content::BrowserThread;
51 using syncer::BaseNode;
53 using testing::InvokeWithoutArgs;
55 using testing::StrictMock;
57 #if defined(OS_ANDROID)
58 static const bool kExpectMobileBookmarks = true;
60 static const bool kExpectMobileBookmarks = false;
61 #endif // defined(OS_ANDROID)
65 // FakeServerChange constructs a list of syncer::ChangeRecords while modifying
66 // the sync model, and can pass the ChangeRecord list to a
67 // syncer::SyncObserver (i.e., the ProfileSyncService) to test the client
68 // change-application behavior.
69 // Tests using FakeServerChange should be careful to avoid back-references,
70 // since FakeServerChange will send the edits in the order specified.
71 class FakeServerChange {
73 explicit FakeServerChange(syncer::WriteTransaction* trans) : trans_(trans) {
76 // Pretend that the server told the syncer to add a bookmark object.
77 int64 AddWithMetaInfo(const std::string& title,
78 const std::string& url,
79 const BookmarkNode::MetaInfoMap* meta_info_map,
82 int64 predecessor_id) {
83 syncer::ReadNode parent(trans_);
84 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
85 syncer::WriteNode node(trans_);
86 if (predecessor_id == 0) {
87 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL));
89 syncer::ReadNode predecessor(trans_);
90 EXPECT_EQ(BaseNode::INIT_OK, predecessor.InitByIdLookup(predecessor_id));
91 EXPECT_EQ(predecessor.GetParentId(), parent.GetId());
92 EXPECT_TRUE(node.InitBookmarkByCreation(parent, &predecessor));
94 EXPECT_EQ(node.GetPredecessorId(), predecessor_id);
95 EXPECT_EQ(node.GetParentId(), parent_id);
96 node.SetIsFolder(is_folder);
99 sync_pb::BookmarkSpecifics specifics(node.GetBookmarkSpecifics());
101 specifics.set_url(url);
103 SetNodeMetaInfo(*meta_info_map, &specifics);
104 node.SetBookmarkSpecifics(specifics);
106 syncer::ChangeRecord record;
107 record.action = syncer::ChangeRecord::ACTION_ADD;
108 record.id = node.GetId();
109 changes_.push_back(record);
113 int64 Add(const std::string& title,
114 const std::string& url,
117 int64 predecessor_id) {
118 return AddWithMetaInfo(title, url, NULL, is_folder, parent_id,
122 // Add a bookmark folder.
123 int64 AddFolder(const std::string& title,
125 int64 predecessor_id) {
126 return Add(title, std::string(), true, parent_id, predecessor_id);
128 int64 AddFolderWithMetaInfo(const std::string& title,
129 const BookmarkNode::MetaInfoMap* meta_info_map,
131 int64 predecessor_id) {
132 return AddWithMetaInfo(title, std::string(), meta_info_map, true, parent_id,
137 int64 AddURL(const std::string& title,
138 const std::string& url,
140 int64 predecessor_id) {
141 return Add(title, url, false, parent_id, predecessor_id);
143 int64 AddURLWithMetaInfo(const std::string& title,
144 const std::string& url,
145 const BookmarkNode::MetaInfoMap* meta_info_map,
147 int64 predecessor_id) {
148 return AddWithMetaInfo(title, url, meta_info_map, false, parent_id,
152 // Pretend that the server told the syncer to delete an object.
153 void Delete(int64 id) {
155 // Delete the sync node.
156 syncer::WriteNode node(trans_);
157 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
158 if (node.GetIsFolder())
159 EXPECT_FALSE(node.GetFirstChildId());
160 node.GetMutableEntryForTest()->PutServerIsDel(true);
164 // Verify the deletion.
165 syncer::ReadNode node(trans_);
166 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL, node.InitByIdLookup(id));
169 syncer::ChangeRecord record;
170 record.action = syncer::ChangeRecord::ACTION_DELETE;
172 // Deletions are always first in the changelist, but we can't actually do
173 // WriteNode::Remove() on the node until its children are moved. So, as
174 // a practical matter, users of FakeServerChange must move or delete
175 // children before parents. Thus, we must insert the deletion record
176 // at the front of the vector.
177 changes_.insert(changes_.begin(), record);
180 // Set a new title value, and return the old value.
181 std::string ModifyTitle(int64 id, const std::string& new_title) {
182 syncer::WriteNode node(trans_);
183 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
184 std::string old_title = node.GetTitle();
185 node.SetTitle(new_title);
190 // Set a new parent and predecessor value. Return the old parent id.
191 // We could return the old predecessor id, but it turns out not to be
192 // very useful for assertions.
193 int64 ModifyPosition(int64 id, int64 parent_id, int64 predecessor_id) {
194 syncer::ReadNode parent(trans_);
195 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
196 syncer::WriteNode node(trans_);
197 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
198 int64 old_parent_id = node.GetParentId();
199 if (predecessor_id == 0) {
200 EXPECT_TRUE(node.SetPosition(parent, NULL));
202 syncer::ReadNode predecessor(trans_);
203 EXPECT_EQ(BaseNode::INIT_OK, predecessor.InitByIdLookup(predecessor_id));
204 EXPECT_EQ(predecessor.GetParentId(), parent.GetId());
205 EXPECT_TRUE(node.SetPosition(parent, &predecessor));
208 return old_parent_id;
211 void ModifyCreationTime(int64 id, int64 creation_time_us) {
212 syncer::WriteNode node(trans_);
213 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
214 sync_pb::BookmarkSpecifics specifics = node.GetBookmarkSpecifics();
215 specifics.set_creation_time_us(creation_time_us);
216 node.SetBookmarkSpecifics(specifics);
220 void ModifyMetaInfo(int64 id,
221 const BookmarkNode::MetaInfoMap& meta_info_map) {
222 syncer::WriteNode node(trans_);
223 ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
224 sync_pb::BookmarkSpecifics specifics = node.GetBookmarkSpecifics();
225 SetNodeMetaInfo(meta_info_map, &specifics);
226 node.SetBookmarkSpecifics(specifics);
230 // Pass the fake change list to |service|.
231 void ApplyPendingChanges(ChangeProcessor* processor) {
232 processor->ApplyChangesFromSyncModel(
233 trans_, 0, syncer::ImmutableChangeRecordList(&changes_));
236 const syncer::ChangeRecordList& changes() {
241 // Helper function to push an ACTION_UPDATE record onto the back
242 // of the changelist.
243 void SetModified(int64 id) {
244 // Coalesce multi-property edits.
245 if (!changes_.empty() && changes_.back().id == id &&
246 changes_.back().action ==
247 syncer::ChangeRecord::ACTION_UPDATE)
249 syncer::ChangeRecord record;
250 record.action = syncer::ChangeRecord::ACTION_UPDATE;
252 changes_.push_back(record);
255 void SetNodeMetaInfo(const BookmarkNode::MetaInfoMap& meta_info_map,
256 sync_pb::BookmarkSpecifics* specifics) {
257 specifics->clear_meta_info();
258 for (BookmarkNode::MetaInfoMap::const_iterator it =
259 meta_info_map.begin(); it != meta_info_map.end(); ++it) {
260 sync_pb::MetaInfo* meta_info = specifics->add_meta_info();
261 meta_info->set_key(it->first);
262 meta_info->set_value(it->second);
267 // The transaction on which everything happens.
268 syncer::WriteTransaction *trans_;
270 // The change list we construct.
271 syncer::ChangeRecordList changes_;
274 class ExtensiveChangesBookmarkModelObserver : public BaseBookmarkModelObserver {
276 explicit ExtensiveChangesBookmarkModelObserver()
278 completed_count_at_started_(0),
279 completed_count_(0) {}
281 virtual void ExtensiveBookmarkChangesBeginning(
282 BookmarkModel* model) OVERRIDE {
284 completed_count_at_started_ = completed_count_;
287 virtual void ExtensiveBookmarkChangesEnded(BookmarkModel* model) OVERRIDE {
291 virtual void BookmarkModelChanged() OVERRIDE {}
293 int get_started() const {
294 return started_count_;
297 int get_completed_count_at_started() const {
298 return completed_count_at_started_;
301 int get_completed() const {
302 return completed_count_;
307 int completed_count_at_started_;
308 int completed_count_;
310 DISALLOW_COPY_AND_ASSIGN(ExtensiveChangesBookmarkModelObserver);
314 class ProfileSyncServiceBookmarkTest : public testing::Test {
316 enum LoadOption { LOAD_FROM_STORAGE, DELETE_EXISTING_STORAGE };
317 enum SaveOption { SAVE_TO_STORAGE, DONT_SAVE_TO_STORAGE };
319 ProfileSyncServiceBookmarkTest()
321 ui_thread_(BrowserThread::UI, &message_loop_),
322 file_thread_(BrowserThread::FILE, &message_loop_),
323 local_merge_result_(syncer::BOOKMARKS),
324 syncer_merge_result_(syncer::BOOKMARKS) {
327 virtual ~ProfileSyncServiceBookmarkTest() {
329 UnloadBookmarkModel();
332 virtual void SetUp() {
333 test_user_share_.SetUp();
336 virtual void TearDown() {
337 test_user_share_.TearDown();
340 // Inserts a folder directly to the share.
341 // Do not use this after model association is complete.
343 // This function differs from the AddFolder() function declared elsewhere in
344 // this file in that it only affects the sync model. It would be invalid to
345 // change the sync model directly after ModelAssociation. This function can
346 // be invoked prior to model association to set up first-time sync model
347 // association scenarios.
348 int64 AddFolderToShare(syncer::WriteTransaction* trans, std::string title) {
349 EXPECT_FALSE(model_associator_);
351 // Be sure to call CreatePermanentBookmarkNodes(), otherwise this will fail.
352 syncer::ReadNode bookmark_bar(trans);
353 EXPECT_EQ(BaseNode::INIT_OK, bookmark_bar.InitByTagLookup("bookmark_bar"));
355 syncer::WriteNode node(trans);
356 EXPECT_TRUE(node.InitBookmarkByCreation(bookmark_bar, NULL));
357 node.SetIsFolder(true);
358 node.SetTitle(title);
363 // Inserts a bookmark directly to the share.
364 // Do not use this after model association is complete.
366 // This function differs from the AddURL() function declared elsewhere in this
367 // file in that it only affects the sync model. It would be invalid to change
368 // the sync model directly after ModelAssociation. This function can be
369 // invoked prior to model association to set up first-time sync model
370 // association scenarios.
371 int64 AddBookmarkToShare(syncer::WriteTransaction *trans,
374 EXPECT_FALSE(model_associator_);
376 syncer::ReadNode parent(trans);
377 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
379 sync_pb::BookmarkSpecifics specifics;
380 specifics.set_url("http://www.google.com/search?q=" + title);
381 specifics.set_title(title);
383 syncer::WriteNode node(trans);
384 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL));
385 node.SetIsFolder(false);
386 node.SetTitle(title);
387 node.SetBookmarkSpecifics(specifics);
392 // Load (or re-load) the bookmark model. |load| controls use of the
393 // bookmarks file on disk. |save| controls whether the newly loaded
394 // bookmark model will write out a bookmark file as it goes.
395 void LoadBookmarkModel(LoadOption load, SaveOption save) {
396 bool delete_bookmarks = load == DELETE_EXISTING_STORAGE;
397 profile_.CreateBookmarkModel(delete_bookmarks);
398 model_ = BookmarkModelFactory::GetForProfile(&profile_);
399 test::WaitForBookmarkModelToLoad(model_);
400 // This noticeably speeds up the unit tests that request it.
401 if (save == DONT_SAVE_TO_STORAGE)
402 model_->ClearStore();
403 message_loop_.RunUntilIdle();
406 int GetSyncBookmarkCount() {
407 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
408 syncer::ReadNode node(&trans);
409 if (node.InitByTagLookup(syncer::ModelTypeToRootTag(syncer::BOOKMARKS)) !=
410 syncer::BaseNode::INIT_OK)
412 return node.GetTotalNodeCount();
415 // Creates the bookmark root node and the permanent nodes if they don't
417 bool CreatePermanentBookmarkNodes() {
418 bool root_exists = false;
419 syncer::ModelType type = syncer::BOOKMARKS;
421 syncer::WriteTransaction trans(FROM_HERE,
422 test_user_share_.user_share());
423 syncer::ReadNode uber_root(&trans);
424 uber_root.InitByRootLookup();
426 syncer::ReadNode root(&trans);
427 root_exists = (root.InitByTagLookup(syncer::ModelTypeToRootTag(type)) ==
432 if (!syncer::TestUserShare::CreateRoot(type,
433 test_user_share_.user_share()))
437 const int kNumPermanentNodes = 3;
438 const std::string permanent_tags[kNumPermanentNodes] = {
439 "bookmark_bar", "other_bookmarks", "synced_bookmarks"
441 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
442 syncer::ReadNode root(&trans);
443 EXPECT_EQ(BaseNode::INIT_OK, root.InitByTagLookup(
444 syncer::ModelTypeToRootTag(type)));
446 // Loop through creating permanent nodes as necessary.
447 int64 last_child_id = syncer::kInvalidId;
448 for (int i = 0; i < kNumPermanentNodes; ++i) {
449 // First check if the node already exists. This is for tests that involve
450 // persistence and set up sync more than once.
451 syncer::ReadNode lookup(&trans);
452 if (lookup.InitByTagLookup(permanent_tags[i]) ==
453 syncer::ReadNode::INIT_OK) {
454 last_child_id = lookup.GetId();
458 // If it doesn't exist, create the permanent node at the end of the
460 syncer::ReadNode predecessor_node(&trans);
461 syncer::ReadNode* predecessor = NULL;
462 if (last_child_id != syncer::kInvalidId) {
463 EXPECT_EQ(BaseNode::INIT_OK,
464 predecessor_node.InitByIdLookup(last_child_id));
465 predecessor = &predecessor_node;
467 syncer::WriteNode node(&trans);
468 if (!node.InitBookmarkByCreation(root, predecessor))
470 node.SetIsFolder(true);
471 node.GetMutableEntryForTest()->PutUniqueServerTag(permanent_tags[i]);
472 node.SetTitle(permanent_tags[i]);
473 node.SetExternalId(0);
474 last_child_id = node.GetId();
479 bool AssociateModels() {
480 DCHECK(!model_associator_);
482 // Set up model associator.
483 model_associator_.reset(new BookmarkModelAssociator(
484 BookmarkModelFactory::GetForProfile(&profile_),
486 test_user_share_.user_share(),
487 &mock_error_handler_,
488 kExpectMobileBookmarks));
490 local_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS);
491 syncer_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS);
492 int local_count_before = model_->root_node()->GetTotalNodeCount();
493 int syncer_count_before = GetSyncBookmarkCount();
495 syncer::SyncError error = model_associator_->AssociateModels(
496 &local_merge_result_,
497 &syncer_merge_result_);
501 base::MessageLoop::current()->RunUntilIdle();
503 // Verify the merge results were calculated properly.
504 EXPECT_EQ(local_count_before,
505 local_merge_result_.num_items_before_association());
506 EXPECT_EQ(syncer_count_before,
507 syncer_merge_result_.num_items_before_association());
508 EXPECT_EQ(local_merge_result_.num_items_after_association(),
509 local_merge_result_.num_items_before_association() +
510 local_merge_result_.num_items_added() -
511 local_merge_result_.num_items_deleted());
512 EXPECT_EQ(syncer_merge_result_.num_items_after_association(),
513 syncer_merge_result_.num_items_before_association() +
514 syncer_merge_result_.num_items_added() -
515 syncer_merge_result_.num_items_deleted());
516 EXPECT_EQ(model_->root_node()->GetTotalNodeCount(),
517 local_merge_result_.num_items_after_association());
518 EXPECT_EQ(GetSyncBookmarkCount(),
519 syncer_merge_result_.num_items_after_association());
524 test_user_share_.Reload();
526 ASSERT_TRUE(CreatePermanentBookmarkNodes());
527 ASSERT_TRUE(AssociateModels());
529 // Set up change processor.
530 change_processor_.reset(
531 new BookmarkChangeProcessor(&profile_,
532 model_associator_.get(),
533 &mock_error_handler_));
534 change_processor_->Start(test_user_share_.user_share());
538 change_processor_.reset();
539 if (model_associator_) {
540 syncer::SyncError error = model_associator_->DisassociateModels();
541 EXPECT_FALSE(error.IsSet());
543 model_associator_.reset();
545 message_loop_.RunUntilIdle();
547 // TODO(akalin): Actually close the database and flush it to disk
548 // (and make StartSync reload from disk). This would require
549 // refactoring TestUserShare.
552 void UnloadBookmarkModel() {
553 profile_.CreateBookmarkModel(false /* delete_bookmarks */);
555 message_loop_.RunUntilIdle();
558 bool InitSyncNodeFromChromeNode(const BookmarkNode* bnode,
559 syncer::BaseNode* sync_node) {
560 return model_associator_->InitSyncNodeFromChromeId(bnode->id(),
564 void ExpectSyncerNodeMatching(syncer::BaseTransaction* trans,
565 const BookmarkNode* bnode) {
566 std::string truncated_title = base::UTF16ToUTF8(bnode->GetTitle());
567 syncer::SyncAPINameToServerName(truncated_title, &truncated_title);
568 base::TruncateUTF8ToByteSize(truncated_title, 255, &truncated_title);
569 syncer::ServerNameToSyncAPIName(truncated_title, &truncated_title);
571 syncer::ReadNode gnode(trans);
572 ASSERT_TRUE(InitSyncNodeFromChromeNode(bnode, &gnode));
573 // Non-root node titles and parents must match.
574 if (!model_->is_permanent_node(bnode)) {
575 EXPECT_EQ(truncated_title, gnode.GetTitle());
577 model_associator_->GetChromeNodeFromSyncId(gnode.GetParentId()),
580 EXPECT_EQ(bnode->is_folder(), gnode.GetIsFolder());
582 EXPECT_EQ(bnode->url(), GURL(gnode.GetBookmarkSpecifics().url()));
584 // Check that meta info matches.
585 const BookmarkNode::MetaInfoMap* meta_info_map = bnode->GetMetaInfoMap();
586 sync_pb::BookmarkSpecifics specifics = gnode.GetBookmarkSpecifics();
587 if (!meta_info_map) {
588 EXPECT_EQ(0, specifics.meta_info_size());
590 EXPECT_EQ(meta_info_map->size(),
591 static_cast<size_t>(specifics.meta_info_size()));
592 for (int i = 0; i < specifics.meta_info_size(); i++) {
593 BookmarkNode::MetaInfoMap::const_iterator it =
594 meta_info_map->find(specifics.meta_info(i).key());
595 EXPECT_TRUE(it != meta_info_map->end());
596 EXPECT_EQ(it->second, specifics.meta_info(i).value());
600 // Check for position matches.
601 int browser_index = bnode->parent()->GetIndexOf(bnode);
602 if (browser_index == 0) {
603 EXPECT_EQ(gnode.GetPredecessorId(), 0);
605 const BookmarkNode* bprev =
606 bnode->parent()->GetChild(browser_index - 1);
607 syncer::ReadNode gprev(trans);
608 ASSERT_TRUE(InitSyncNodeFromChromeNode(bprev, &gprev));
609 EXPECT_EQ(gnode.GetPredecessorId(), gprev.GetId());
610 EXPECT_EQ(gnode.GetParentId(), gprev.GetParentId());
612 if (browser_index == bnode->parent()->child_count() - 1) {
613 EXPECT_EQ(gnode.GetSuccessorId(), 0);
615 const BookmarkNode* bnext =
616 bnode->parent()->GetChild(browser_index + 1);
617 syncer::ReadNode gnext(trans);
618 ASSERT_TRUE(InitSyncNodeFromChromeNode(bnext, &gnext));
619 EXPECT_EQ(gnode.GetSuccessorId(), gnext.GetId());
620 EXPECT_EQ(gnode.GetParentId(), gnext.GetParentId());
623 EXPECT_TRUE(gnode.GetFirstChildId());
626 void ExpectSyncerNodeMatching(const BookmarkNode* bnode) {
627 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
628 ExpectSyncerNodeMatching(&trans, bnode);
631 void ExpectBrowserNodeMatching(syncer::BaseTransaction* trans,
633 EXPECT_TRUE(sync_id);
634 const BookmarkNode* bnode =
635 model_associator_->GetChromeNodeFromSyncId(sync_id);
637 int64 id = model_associator_->GetSyncIdFromChromeId(bnode->id());
638 EXPECT_EQ(id, sync_id);
639 ExpectSyncerNodeMatching(trans, bnode);
642 void ExpectBrowserNodeUnknown(int64 sync_id) {
643 EXPECT_FALSE(model_associator_->GetChromeNodeFromSyncId(sync_id));
646 void ExpectBrowserNodeKnown(int64 sync_id) {
647 EXPECT_TRUE(model_associator_->GetChromeNodeFromSyncId(sync_id));
650 void ExpectSyncerNodeKnown(const BookmarkNode* node) {
651 int64 sync_id = model_associator_->GetSyncIdFromChromeId(node->id());
652 EXPECT_NE(sync_id, syncer::kInvalidId);
655 void ExpectSyncerNodeUnknown(const BookmarkNode* node) {
656 int64 sync_id = model_associator_->GetSyncIdFromChromeId(node->id());
657 EXPECT_EQ(sync_id, syncer::kInvalidId);
660 void ExpectBrowserNodeTitle(int64 sync_id, const std::string& title) {
661 const BookmarkNode* bnode =
662 model_associator_->GetChromeNodeFromSyncId(sync_id);
664 EXPECT_EQ(bnode->GetTitle(), base::UTF8ToUTF16(title));
667 void ExpectBrowserNodeURL(int64 sync_id, const std::string& url) {
668 const BookmarkNode* bnode =
669 model_associator_->GetChromeNodeFromSyncId(sync_id);
671 EXPECT_EQ(GURL(url), bnode->url());
674 void ExpectBrowserNodeParent(int64 sync_id, int64 parent_sync_id) {
675 const BookmarkNode* node =
676 model_associator_->GetChromeNodeFromSyncId(sync_id);
678 const BookmarkNode* parent =
679 model_associator_->GetChromeNodeFromSyncId(parent_sync_id);
681 EXPECT_EQ(node->parent(), parent);
684 void ExpectModelMatch(syncer::BaseTransaction* trans) {
685 const BookmarkNode* root = model_->root_node();
686 EXPECT_EQ(root->GetIndexOf(model_->bookmark_bar_node()), 0);
687 EXPECT_EQ(root->GetIndexOf(model_->other_node()), 1);
688 EXPECT_EQ(root->GetIndexOf(model_->mobile_node()), 2);
690 std::stack<int64> stack;
691 stack.push(bookmark_bar_id());
692 while (!stack.empty()) {
693 int64 id = stack.top();
697 ExpectBrowserNodeMatching(trans, id);
699 syncer::ReadNode gnode(trans);
700 ASSERT_EQ(BaseNode::INIT_OK, gnode.InitByIdLookup(id));
701 stack.push(gnode.GetSuccessorId());
702 if (gnode.GetIsFolder())
703 stack.push(gnode.GetFirstChildId());
707 void ExpectModelMatch() {
708 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
709 ExpectModelMatch(&trans);
712 int64 mobile_bookmarks_id() {
714 model_associator_->GetSyncIdFromChromeId(model_->mobile_node()->id());
717 int64 other_bookmarks_id() {
719 model_associator_->GetSyncIdFromChromeId(model_->other_node()->id());
722 int64 bookmark_bar_id() {
723 return model_associator_->GetSyncIdFromChromeId(
724 model_->bookmark_bar_node()->id());
728 BookmarkModel* model_;
729 syncer::TestUserShare test_user_share_;
730 scoped_ptr<BookmarkChangeProcessor> change_processor_;
731 StrictMock<DataTypeErrorHandlerMock> mock_error_handler_;
732 scoped_ptr<BookmarkModelAssociator> model_associator_;
735 // Used by both |ui_thread_| and |file_thread_|.
736 base::MessageLoop message_loop_;
737 content::TestBrowserThread ui_thread_;
738 // Needed by |model_|.
739 content::TestBrowserThread file_thread_;
741 syncer::SyncMergeResult local_merge_result_;
742 syncer::SyncMergeResult syncer_merge_result_;
744 TestingProfile profile_;
747 TEST_F(ProfileSyncServiceBookmarkTest, InitialState) {
748 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
751 EXPECT_TRUE(other_bookmarks_id());
752 EXPECT_TRUE(bookmark_bar_id());
753 EXPECT_TRUE(mobile_bookmarks_id());
758 // Populate the sync database then start model association. Sync's bookmarks
759 // should end up being copied into the native model, resulting in a successful
760 // "ExpectModelMatch()".
762 // This code has some use for verifying correctness. It's also a very useful
763 // for profiling bookmark ModelAssociation, an important part of some first-time
764 // sync scenarios. Simply increase the kNumFolders and kNumBookmarksPerFolder
765 // as desired, then run the test under a profiler to find hot spots in the model
767 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociate) {
768 const int kNumBookmarksPerFolder = 10;
769 const int kNumFolders = 10;
771 CreatePermanentBookmarkNodes();
774 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
775 for (int i = 0; i < kNumFolders; ++i) {
776 int64 folder_id = AddFolderToShare(&trans,
777 base::StringPrintf("folder%05d", i));
778 for (int j = 0; j < kNumBookmarksPerFolder; ++j) {
779 AddBookmarkToShare(&trans,
781 base::StringPrintf("bookmark%05d", j));
786 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
793 TEST_F(ProfileSyncServiceBookmarkTest, BookmarkModelOperations) {
794 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
798 const BookmarkNode* folder =
799 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("foobar"));
800 ExpectSyncerNodeMatching(folder);
802 const BookmarkNode* folder2 =
803 model_->AddFolder(folder, 0, base::ASCIIToUTF16("nested"));
804 ExpectSyncerNodeMatching(folder2);
806 const BookmarkNode* url1 = model_->AddURL(
807 folder, 0, base::ASCIIToUTF16("Internets #1 Pies Site"),
808 GURL("http://www.easypie.com/"));
809 ExpectSyncerNodeMatching(url1);
811 const BookmarkNode* url2 = model_->AddURL(
812 folder, 1, base::ASCIIToUTF16("Airplanes"),
813 GURL("http://www.easyjet.com/"));
814 ExpectSyncerNodeMatching(url2);
817 const BookmarkNode* mobile_folder =
818 model_->AddFolder(model_->mobile_node(), 0, base::ASCIIToUTF16("pie"));
819 ExpectSyncerNodeMatching(mobile_folder);
822 // Test modification.
823 model_->SetTitle(url2, base::ASCIIToUTF16("EasyJet"));
825 model_->Move(url1, folder2, 0);
827 model_->Move(folder2, model_->bookmark_bar_node(), 0);
829 model_->SetTitle(folder2, base::ASCIIToUTF16("Not Nested"));
831 model_->Move(folder, folder2, 0);
833 model_->SetTitle(folder, base::ASCIIToUTF16("who's nested now?"));
835 model_->Copy(url2, model_->bookmark_bar_node(), 0);
837 model_->SetTitle(mobile_folder, base::ASCIIToUTF16("strawberry"));
841 // Delete a single item.
842 model_->Remove(url2->parent(), url2->parent()->GetIndexOf(url2));
844 // Delete an item with several children.
845 model_->Remove(folder2->parent(),
846 folder2->parent()->GetIndexOf(folder2));
848 model_->Remove(model_->mobile_node(), 0);
852 TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeProcessing) {
853 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
856 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
858 FakeServerChange adds(&trans);
859 int64 f1 = adds.AddFolder("Server Folder B", bookmark_bar_id(), 0);
860 int64 f2 = adds.AddFolder("Server Folder A", bookmark_bar_id(), f1);
861 int64 u1 = adds.AddURL("Some old site", "ftp://nifty.andrew.cmu.edu/",
862 bookmark_bar_id(), f2);
863 int64 u2 = adds.AddURL("Nifty", "ftp://nifty.andrew.cmu.edu/", f1, 0);
864 // u3 is a duplicate URL
865 int64 u3 = adds.AddURL("Nifty2", "ftp://nifty.andrew.cmu.edu/", f1, u2);
866 // u4 is a duplicate title, different URL.
867 adds.AddURL("Some old site", "http://slog.thestranger.com/",
868 bookmark_bar_id(), u1);
869 // u5 tests an empty-string title.
870 std::string javascript_url(
871 "javascript:(function(){var w=window.open(" \
872 "'about:blank','gnotesWin','location=0,menubar=0," \
873 "scrollbars=0,status=0,toolbar=0,width=300," \
874 "height=300,resizable');});");
875 adds.AddURL(std::string(), javascript_url, other_bookmarks_id(), 0);
876 int64 u6 = adds.AddURL(
877 "Sync1", "http://www.syncable.edu/", mobile_bookmarks_id(), 0);
879 syncer::ChangeRecordList::const_iterator it;
880 // The bookmark model shouldn't yet have seen any of the nodes of |adds|.
881 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
882 ExpectBrowserNodeUnknown(it->id);
884 adds.ApplyPendingChanges(change_processor_.get());
886 // Make sure the bookmark model received all of the nodes in |adds|.
887 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
888 ExpectBrowserNodeMatching(&trans, it->id);
889 ExpectModelMatch(&trans);
891 // Part two: test modifications.
892 FakeServerChange mods(&trans);
893 // Mess with u2, and move it into empty folder f2
894 // TODO(ncarter): Determine if we allow ModifyURL ops or not.
895 /* std::string u2_old_url = mods.ModifyURL(u2, "http://www.google.com"); */
896 std::string u2_old_title = mods.ModifyTitle(u2, "The Google");
897 int64 u2_old_parent = mods.ModifyPosition(u2, f2, 0);
899 // Now move f1 after u2.
900 std::string f1_old_title = mods.ModifyTitle(f1, "Server Folder C");
901 int64 f1_old_parent = mods.ModifyPosition(f1, f2, u2);
903 // Then add u3 after f1.
904 int64 u3_old_parent = mods.ModifyPosition(u3, f2, f1);
906 std::string u6_old_title = mods.ModifyTitle(u6, "Mobile Folder A");
908 // Test that the property changes have not yet taken effect.
909 ExpectBrowserNodeTitle(u2, u2_old_title);
910 /* ExpectBrowserNodeURL(u2, u2_old_url); */
911 ExpectBrowserNodeParent(u2, u2_old_parent);
913 ExpectBrowserNodeTitle(f1, f1_old_title);
914 ExpectBrowserNodeParent(f1, f1_old_parent);
916 ExpectBrowserNodeParent(u3, u3_old_parent);
918 ExpectBrowserNodeTitle(u6, u6_old_title);
920 // Apply the changes.
921 mods.ApplyPendingChanges(change_processor_.get());
923 // Check for successful application.
924 for (it = mods.changes().begin(); it != mods.changes().end(); ++it)
925 ExpectBrowserNodeMatching(&trans, it->id);
926 ExpectModelMatch(&trans);
928 // Part 3: Test URL deletion.
929 FakeServerChange dels(&trans);
934 ExpectBrowserNodeKnown(u2);
935 ExpectBrowserNodeKnown(u3);
937 dels.ApplyPendingChanges(change_processor_.get());
939 ExpectBrowserNodeUnknown(u2);
940 ExpectBrowserNodeUnknown(u3);
941 ExpectBrowserNodeUnknown(u6);
942 ExpectModelMatch(&trans);
945 // Tests a specific case in ApplyModelChanges where we move the
946 // children out from under a parent, and then delete the parent
947 // in the same changelist. The delete shows up first in the changelist,
948 // requiring the children to be moved to a temporary location.
949 TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeRequiringFosterParent) {
950 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
953 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
955 // Stress the immediate children of other_node because that's where
956 // ApplyModelChanges puts a temporary foster parent node.
957 std::string url("http://dev.chromium.org/");
958 FakeServerChange adds(&trans);
959 int64 f0 = other_bookmarks_id(); // + other_node
960 int64 f1 = adds.AddFolder("f1", f0, 0); // + f1
961 int64 f2 = adds.AddFolder("f2", f1, 0); // + f2
962 int64 u3 = adds.AddURL( "u3", url, f2, 0); // + u3 NOLINT
963 int64 u4 = adds.AddURL( "u4", url, f2, u3); // + u4 NOLINT
964 int64 u5 = adds.AddURL( "u5", url, f1, f2); // + u5 NOLINT
965 int64 f6 = adds.AddFolder("f6", f1, u5); // + f6
966 int64 u7 = adds.AddURL( "u7", url, f0, f1); // + u7 NOLINT
968 syncer::ChangeRecordList::const_iterator it;
969 // The bookmark model shouldn't yet have seen any of the nodes of |adds|.
970 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
971 ExpectBrowserNodeUnknown(it->id);
973 adds.ApplyPendingChanges(change_processor_.get());
975 // Make sure the bookmark model received all of the nodes in |adds|.
976 for (it = adds.changes().begin(); it != adds.changes().end(); ++it)
977 ExpectBrowserNodeMatching(&trans, it->id);
978 ExpectModelMatch(&trans);
980 // We have to do the moves before the deletions, but FakeServerChange will
981 // put the deletion at the front of the changelist.
982 FakeServerChange ops(&trans);
983 ops.ModifyPosition(f6, other_bookmarks_id(), 0);
984 ops.ModifyPosition(u3, other_bookmarks_id(), f1); // Prev == f1 is OK here.
985 ops.ModifyPosition(f2, other_bookmarks_id(), u7);
986 ops.ModifyPosition(u7, f2, 0);
987 ops.ModifyPosition(u4, other_bookmarks_id(), f2);
988 ops.ModifyPosition(u5, f6, 0);
991 ops.ApplyPendingChanges(change_processor_.get());
993 ExpectModelMatch(&trans);
996 // Simulate a server change record containing a valid but non-canonical URL.
997 TEST_F(ProfileSyncServiceBookmarkTest, ServerChangeWithNonCanonicalURL) {
998 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1002 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1004 FakeServerChange adds(&trans);
1005 std::string url("http://dev.chromium.org");
1006 EXPECT_NE(GURL(url).spec(), url);
1007 adds.AddURL("u1", url, other_bookmarks_id(), 0);
1009 adds.ApplyPendingChanges(change_processor_.get());
1011 EXPECT_EQ(1, model_->other_node()->child_count());
1012 ExpectModelMatch(&trans);
1015 // Now reboot the sync service, forcing a merge step.
1017 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
1020 // There should still be just the one bookmark.
1021 EXPECT_EQ(1, model_->other_node()->child_count());
1025 // Simulate a server change record containing an invalid URL (per GURL).
1026 // TODO(ncarter): Disabled due to crashes. Fix bug 1677563.
1027 TEST_F(ProfileSyncServiceBookmarkTest, DISABLED_ServerChangeWithInvalidURL) {
1028 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1031 int child_count = 0;
1033 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1035 FakeServerChange adds(&trans);
1036 std::string url("x");
1037 EXPECT_FALSE(GURL(url).is_valid());
1038 adds.AddURL("u1", url, other_bookmarks_id(), 0);
1040 adds.ApplyPendingChanges(change_processor_.get());
1042 // We're lenient about what should happen -- the model could wind up with
1043 // the node or without it; but things should be consistent, and we
1045 child_count = model_->other_node()->child_count();
1046 EXPECT_TRUE(child_count == 0 || child_count == 1);
1047 ExpectModelMatch(&trans);
1050 // Now reboot the sync service, forcing a merge step.
1052 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
1055 // Things ought not to have changed.
1056 EXPECT_EQ(model_->other_node()->child_count(), child_count);
1061 // Test strings that might pose a problem if the titles ever became used as
1062 // file names in the sync backend.
1063 TEST_F(ProfileSyncServiceBookmarkTest, CornerCaseNames) {
1064 // TODO(ncarter): Bug 1570238 explains the failure of this test.
1065 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1068 const char* names[] = {
1069 // The empty string.
1071 // Illegal Windows filenames.
1072 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4",
1073 "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3",
1074 "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
1075 // Current/parent directory markers.
1077 // Files created automatically by the Windows shell.
1078 "Thumbs.db", ".DS_Store",
1079 // Names including Win32-illegal characters, and path separators.
1080 "foo/bar", "foo\\bar", "foo?bar", "foo:bar", "foo|bar", "foo\"bar",
1081 "foo'bar", "foo<bar", "foo>bar", "foo%bar", "foo*bar", "foo]bar",
1083 // A name with title > 255 characters
1084 "012345678901234567890123456789012345678901234567890123456789012345678901"
1085 "234567890123456789012345678901234567890123456789012345678901234567890123"
1086 "456789012345678901234567890123456789012345678901234567890123456789012345"
1087 "678901234567890123456789012345678901234567890123456789012345678901234567"
1090 // Create both folders and bookmarks using each name.
1091 GURL url("http://www.doublemint.com");
1092 for (size_t i = 0; i < arraysize(names); ++i) {
1093 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16(names[i]));
1094 model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16(names[i]), url);
1097 // Verify that the browser model matches the sync model.
1098 EXPECT_EQ(static_cast<size_t>(model_->other_node()->child_count()),
1099 2*arraysize(names));
1102 // Restart and re-associate. Verify things still match.
1104 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
1106 EXPECT_EQ(static_cast<size_t>(model_->other_node()->child_count()),
1107 2*arraysize(names));
1111 // Stress the internal representation of position by sparse numbers. We want
1112 // to repeatedly bisect the range of available positions, to force the
1113 // syncer code to renumber its ranges. Pick a number big enough so that it
1114 // would exhaust 32bits of room between items a couple of times.
1115 TEST_F(ProfileSyncServiceBookmarkTest, RepeatedMiddleInsertion) {
1116 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1119 static const int kTimesToInsert = 256;
1121 // Create two book-end nodes to insert between.
1122 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("Alpha"));
1123 model_->AddFolder(model_->other_node(), 1, base::ASCIIToUTF16("Omega"));
1126 // Test insertion in first half of range by repeatedly inserting in second
1128 for (int i = 0; i < kTimesToInsert; ++i) {
1129 base::string16 title =
1130 base::ASCIIToUTF16("Pre-insertion ") + base::IntToString16(i);
1131 model_->AddFolder(model_->other_node(), 1, title);
1135 // Test insertion in second half of range by repeatedly inserting in
1136 // second-to-last position.
1137 for (int i = 0; i < kTimesToInsert; ++i) {
1138 base::string16 title =
1139 base::ASCIIToUTF16("Post-insertion ") + base::IntToString16(i);
1140 model_->AddFolder(model_->other_node(), count - 1, title);
1144 // Verify that the browser model matches the sync model.
1145 EXPECT_EQ(model_->other_node()->child_count(), count);
1149 // Introduce a consistency violation into the model, and see that it
1150 // puts itself into a lame, error state.
1151 TEST_F(ProfileSyncServiceBookmarkTest, UnrecoverableErrorSuspendsService) {
1152 EXPECT_CALL(mock_error_handler_,
1153 OnSingleDatatypeUnrecoverableError(_, _));
1155 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1158 // Add a node which will be the target of the consistency violation.
1159 const BookmarkNode* node =
1160 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("node"));
1161 ExpectSyncerNodeMatching(node);
1163 // Now destroy the syncer node as if we were the ProfileSyncService without
1164 // updating the ProfileSyncService state. This should introduce
1165 // inconsistency between the two models.
1167 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1168 syncer::WriteNode sync_node(&trans);
1169 ASSERT_TRUE(InitSyncNodeFromChromeNode(node, &sync_node));
1170 sync_node.Tombstone();
1172 // The models don't match at this point, but the ProfileSyncService
1173 // doesn't know it yet.
1174 ExpectSyncerNodeKnown(node);
1176 // Add a child to the inconsistent node. This should cause detection of the
1177 // problem and the syncer should stop processing changes.
1178 model_->AddFolder(node, 0, base::ASCIIToUTF16("nested"));
1181 // See what happens if we run model association when there are two exact URL
1182 // duplicate bookmarks. The BookmarkModelAssociator should not fall over when
1184 TEST_F(ProfileSyncServiceBookmarkTest, MergeDuplicates) {
1185 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1188 model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16("Dup"),
1189 GURL("http://dup.com/"));
1190 model_->AddURL(model_->other_node(), 0, base::ASCIIToUTF16("Dup"),
1191 GURL("http://dup.com/"));
1193 EXPECT_EQ(2, model_->other_node()->child_count());
1195 // Restart the sync service to trigger model association.
1199 EXPECT_EQ(2, model_->other_node()->child_count());
1203 TEST_F(ProfileSyncServiceBookmarkTest, ApplySyncDeletesFromJournal) {
1204 // Initialize sync model and bookmark model as:
1210 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1217 int fixed_sync_bk_count = GetSyncBookmarkCount();
1219 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1220 FakeServerChange adds(&trans);
1221 u0 = adds.AddURL("URL 0", "http://plus.google.com/", bookmark_bar_id(), 0);
1222 f1 = adds.AddFolder("Folder 1", bookmark_bar_id(), u0);
1223 u1 = adds.AddURL("URL 1", "http://www.google.com/", f1, 0);
1224 f2 = adds.AddFolder("Folder 2", f1, u1);
1225 u2 = adds.AddURL("URL 2", "http://mail.google.com/", f2, 0);
1226 adds.ApplyPendingChanges(change_processor_.get());
1230 // Reload bookmark model and disable model saving to make sync changes not
1232 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1233 EXPECT_EQ(6, model_->bookmark_bar_node()->GetTotalNodeCount());
1234 EXPECT_EQ(fixed_sync_bk_count + 5, GetSyncBookmarkCount());
1237 // Remove all folders/bookmarks except u3 added above.
1238 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1239 FakeServerChange dels(&trans);
1244 dels.ApplyPendingChanges(change_processor_.get());
1247 // Bookmark bar itself and u0 remain.
1248 EXPECT_EQ(2, model_->bookmark_bar_node()->GetTotalNodeCount());
1250 // Reload bookmarks including ones deleted in sync model from storage.
1251 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1252 EXPECT_EQ(6, model_->bookmark_bar_node()->GetTotalNodeCount());
1253 // Add a bookmark under f1 when sync is off so that f1 will not be
1254 // deleted even when f1 matches delete journal because it's not empty.
1255 model_->AddURL(model_->bookmark_bar_node()->GetChild(1),
1256 0, base::UTF8ToUTF16("local"), GURL("http://www.youtube.com"));
1257 // Sync model has fixed bookmarks nodes and u3.
1258 EXPECT_EQ(fixed_sync_bk_count + 1, GetSyncBookmarkCount());
1260 // Expect 4 bookmarks after model association because u2, f2, u1 are removed
1261 // by delete journal, f1 is not removed by delete journal because it's
1262 // not empty due to www.youtube.com added above.
1263 EXPECT_EQ(4, model_->bookmark_bar_node()->GetTotalNodeCount());
1264 EXPECT_EQ(base::UTF8ToUTF16("URL 0"),
1265 model_->bookmark_bar_node()->GetChild(0)->GetTitle());
1266 EXPECT_EQ(base::UTF8ToUTF16("Folder 1"),
1267 model_->bookmark_bar_node()->GetChild(1)->GetTitle());
1268 EXPECT_EQ(base::UTF8ToUTF16("local"),
1269 model_->bookmark_bar_node()->GetChild(1)->GetChild(0)->GetTitle());
1272 // Verify purging of delete journals.
1273 // Delete journals for u2, f2, u1 remains because they are used in last
1275 EXPECT_EQ(3u, test_user_share_.GetDeleteJournalSize());
1278 // Reload again and all delete journals should be gone because none is used
1279 // in last association.
1280 ASSERT_TRUE(test_user_share_.Reload());
1281 EXPECT_EQ(0u, test_user_share_.GetDeleteJournalSize());
1289 // Map from bookmark node ID to its version.
1290 typedef std::map<int64, int64> BookmarkNodeVersionMap;
1292 // TODO(ncarter): Integrate the existing TestNode/PopulateNodeFromString code
1293 // in the bookmark model unittest, to make it simpler to set up test data
1294 // here (and reduce the amount of duplication among tests), and to reduce the
1296 class ProfileSyncServiceBookmarkTestWithData
1297 : public ProfileSyncServiceBookmarkTest {
1299 ProfileSyncServiceBookmarkTestWithData();
1302 // Populates or compares children of the given bookmark node from/with the
1303 // given test data array with the given size. |running_count| is updated as
1304 // urls are added. It is used to set the creation date (or test the creation
1305 // date for CompareWithTestData()).
1306 void PopulateFromTestData(const BookmarkNode* node,
1307 const TestData* data,
1309 int* running_count);
1310 void CompareWithTestData(const BookmarkNode* node,
1311 const TestData* data,
1313 int* running_count);
1315 void ExpectBookmarkModelMatchesTestData();
1316 void WriteTestDataToBookmarkModel();
1318 // Verify transaction versions of bookmark nodes and sync nodes are equal
1319 // recursively. If node is in |version_expected|, versions should match
1321 void ExpectTransactionVersionMatch(
1322 const BookmarkNode* node,
1323 const BookmarkNodeVersionMap& version_expected);
1326 const base::Time start_time_;
1328 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceBookmarkTestWithData);
1333 // Constants for bookmark model that looks like:
1335 // | |-- u2, http://www.u2.com/
1337 // | | |-- f1u4, http://www.f1u4.com/
1338 // | | |-- f1u2, http://www.f1u2.com/
1339 // | | |-- f1u3, http://www.f1u3.com/
1340 // | | +-- f1u1, http://www.f1u1.com/
1341 // | |-- u1, http://www.u1.com/
1343 // | |-- f2u2, http://www.f2u2.com/
1344 // | |-- f2u4, http://www.f2u4.com/
1345 // | |-- f2u3, http://www.f2u3.com/
1346 // | +-- f2u1, http://www.f2u1.com/
1347 // +-- Other bookmarks
1349 // | | |-- f3u4, http://www.f3u4.com/
1350 // | | |-- f3u2, http://www.f3u2.com/
1351 // | | |-- f3u3, http://www.f3u3.com/
1352 // | | +-- f3u1, http://www.f3u1.com/
1353 // | |-- u4, http://www.u4.com/
1354 // | |-- u3, http://www.u3.com/
1356 // | | |-- f4u1, http://www.f4u1.com/
1357 // | | |-- f4u2, http://www.f4u2.com/
1358 // | | |-- f4u3, http://www.f4u3.com/
1359 // | | +-- f4u4, http://www.f4u4.com/
1361 // | | +-- dupu1, http://www.dupu1.com/
1363 // | | +-- dupu2, http://www.dupu1.com/
1364 // | +-- ls , http://www.ls.com/
1366 // +-- Mobile bookmarks
1368 // | |-- f5u1, http://www.f5u1.com/
1370 // | |-- f6u1, http://www.f6u1.com/
1371 // | |-- f6u2, http://www.f6u2.com/
1372 // +-- u5, http://www.u5.com/
1374 static TestData kBookmarkBarChildren[] = {
1375 { "u2", "http://www.u2.com/" },
1377 { "u1", "http://www.u1.com/" },
1380 static TestData kF1Children[] = {
1381 { "f1u4", "http://www.f1u4.com/" },
1382 { "f1u2", "http://www.f1u2.com/" },
1383 { "f1u3", "http://www.f1u3.com/" },
1384 { "f1u1", "http://www.f1u1.com/" },
1386 static TestData kF2Children[] = {
1387 { "f2u2", "http://www.f2u2.com/" },
1388 { "f2u4", "http://www.f2u4.com/" },
1389 { "f2u3", "http://www.f2u3.com/" },
1390 { "f2u1", "http://www.f2u1.com/" },
1393 static TestData kOtherBookmarkChildren[] = {
1395 { "u4", "http://www.u4.com/" },
1396 { "u3", "http://www.u3.com/" },
1400 { " ls ", "http://www.ls.com/" }
1402 static TestData kF3Children[] = {
1403 { "f3u4", "http://www.f3u4.com/" },
1404 { "f3u2", "http://www.f3u2.com/" },
1405 { "f3u3", "http://www.f3u3.com/" },
1406 { "f3u1", "http://www.f3u1.com/" },
1408 static TestData kF4Children[] = {
1409 { "f4u1", "http://www.f4u1.com/" },
1410 { "f4u2", "http://www.f4u2.com/" },
1411 { "f4u3", "http://www.f4u3.com/" },
1412 { "f4u4", "http://www.f4u4.com/" },
1414 static TestData kDup1Children[] = {
1415 { "dupu1", "http://www.dupu1.com/" },
1417 static TestData kDup2Children[] = {
1418 { "dupu2", "http://www.dupu2.com/" },
1421 static TestData kMobileBookmarkChildren[] = {
1424 { "u5", "http://www.u5.com/" },
1426 static TestData kF5Children[] = {
1427 { "f5u1", "http://www.f5u1.com/" },
1428 { "f5u2", "http://www.f5u2.com/" },
1430 static TestData kF6Children[] = {
1431 { "f6u1", "http://www.f6u1.com/" },
1432 { "f6u2", "http://www.f6u2.com/" },
1435 } // anonymous namespace.
1437 ProfileSyncServiceBookmarkTestWithData::
1438 ProfileSyncServiceBookmarkTestWithData()
1439 : start_time_(base::Time::Now()) {
1442 void ProfileSyncServiceBookmarkTestWithData::PopulateFromTestData(
1443 const BookmarkNode* node,
1444 const TestData* data,
1446 int* running_count) {
1449 DCHECK(node->is_folder());
1450 for (int i = 0; i < size; ++i) {
1451 const TestData& item = data[i];
1453 const base::Time add_time =
1454 start_time_ + base::TimeDelta::FromMinutes(*running_count);
1455 model_->AddURLWithCreationTimeAndMetaInfo(node,
1457 base::UTF8ToUTF16(item.title),
1462 model_->AddFolder(node, i, base::UTF8ToUTF16(item.title));
1468 void ProfileSyncServiceBookmarkTestWithData::CompareWithTestData(
1469 const BookmarkNode* node,
1470 const TestData* data,
1472 int* running_count) {
1475 DCHECK(node->is_folder());
1476 ASSERT_EQ(size, node->child_count());
1477 for (int i = 0; i < size; ++i) {
1478 const BookmarkNode* child_node = node->GetChild(i);
1479 const TestData& item = data[i];
1480 GURL url = GURL(item.url == NULL ? "" : item.url);
1481 BookmarkNode test_node(url);
1482 test_node.SetTitle(base::UTF8ToUTF16(item.title));
1483 EXPECT_EQ(child_node->GetTitle(), test_node.GetTitle());
1485 EXPECT_FALSE(child_node->is_folder());
1486 EXPECT_TRUE(child_node->is_url());
1487 EXPECT_EQ(child_node->url(), test_node.url());
1488 const base::Time expected_time =
1489 start_time_ + base::TimeDelta::FromMinutes(*running_count);
1490 EXPECT_EQ(expected_time.ToInternalValue(),
1491 child_node->date_added().ToInternalValue());
1493 EXPECT_TRUE(child_node->is_folder());
1494 EXPECT_FALSE(child_node->is_url());
1500 // TODO(munjal): We should implement some way of generating random data and can
1501 // use the same seed to generate the same sequence.
1502 void ProfileSyncServiceBookmarkTestWithData::WriteTestDataToBookmarkModel() {
1503 const BookmarkNode* bookmarks_bar_node = model_->bookmark_bar_node();
1505 PopulateFromTestData(bookmarks_bar_node,
1506 kBookmarkBarChildren,
1507 arraysize(kBookmarkBarChildren),
1510 ASSERT_GE(bookmarks_bar_node->child_count(), 4);
1511 const BookmarkNode* f1_node = bookmarks_bar_node->GetChild(1);
1512 PopulateFromTestData(f1_node, kF1Children, arraysize(kF1Children), &count);
1513 const BookmarkNode* f2_node = bookmarks_bar_node->GetChild(3);
1514 PopulateFromTestData(f2_node, kF2Children, arraysize(kF2Children), &count);
1516 const BookmarkNode* other_bookmarks_node = model_->other_node();
1517 PopulateFromTestData(other_bookmarks_node,
1518 kOtherBookmarkChildren,
1519 arraysize(kOtherBookmarkChildren),
1522 ASSERT_GE(other_bookmarks_node->child_count(), 6);
1523 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0);
1524 PopulateFromTestData(f3_node, kF3Children, arraysize(kF3Children), &count);
1525 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3);
1526 PopulateFromTestData(f4_node, kF4Children, arraysize(kF4Children), &count);
1527 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4);
1528 PopulateFromTestData(dup_node, kDup1Children, arraysize(kDup1Children),
1530 dup_node = other_bookmarks_node->GetChild(5);
1531 PopulateFromTestData(dup_node, kDup2Children, arraysize(kDup2Children),
1534 const BookmarkNode* mobile_bookmarks_node = model_->mobile_node();
1535 PopulateFromTestData(mobile_bookmarks_node,
1536 kMobileBookmarkChildren,
1537 arraysize(kMobileBookmarkChildren),
1540 ASSERT_GE(mobile_bookmarks_node->child_count(), 3);
1541 const BookmarkNode* f5_node = mobile_bookmarks_node->GetChild(0);
1542 PopulateFromTestData(f5_node, kF5Children, arraysize(kF5Children), &count);
1543 const BookmarkNode* f6_node = mobile_bookmarks_node->GetChild(1);
1544 PopulateFromTestData(f6_node, kF6Children, arraysize(kF6Children), &count);
1546 ExpectBookmarkModelMatchesTestData();
1549 void ProfileSyncServiceBookmarkTestWithData::
1550 ExpectBookmarkModelMatchesTestData() {
1551 const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
1553 CompareWithTestData(bookmark_bar_node,
1554 kBookmarkBarChildren,
1555 arraysize(kBookmarkBarChildren),
1558 ASSERT_GE(bookmark_bar_node->child_count(), 4);
1559 const BookmarkNode* f1_node = bookmark_bar_node->GetChild(1);
1560 CompareWithTestData(f1_node, kF1Children, arraysize(kF1Children), &count);
1561 const BookmarkNode* f2_node = bookmark_bar_node->GetChild(3);
1562 CompareWithTestData(f2_node, kF2Children, arraysize(kF2Children), &count);
1564 const BookmarkNode* other_bookmarks_node = model_->other_node();
1565 CompareWithTestData(other_bookmarks_node,
1566 kOtherBookmarkChildren,
1567 arraysize(kOtherBookmarkChildren),
1570 ASSERT_GE(other_bookmarks_node->child_count(), 6);
1571 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0);
1572 CompareWithTestData(f3_node, kF3Children, arraysize(kF3Children), &count);
1573 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3);
1574 CompareWithTestData(f4_node, kF4Children, arraysize(kF4Children), &count);
1575 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4);
1576 CompareWithTestData(dup_node, kDup1Children, arraysize(kDup1Children),
1578 dup_node = other_bookmarks_node->GetChild(5);
1579 CompareWithTestData(dup_node, kDup2Children, arraysize(kDup2Children),
1582 const BookmarkNode* mobile_bookmarks_node = model_->mobile_node();
1583 CompareWithTestData(mobile_bookmarks_node,
1584 kMobileBookmarkChildren,
1585 arraysize(kMobileBookmarkChildren),
1588 ASSERT_GE(mobile_bookmarks_node->child_count(), 3);
1589 const BookmarkNode* f5_node = mobile_bookmarks_node->GetChild(0);
1590 CompareWithTestData(f5_node, kF5Children, arraysize(kF5Children), &count);
1591 const BookmarkNode* f6_node = mobile_bookmarks_node->GetChild(1);
1592 CompareWithTestData(f6_node, kF6Children, arraysize(kF6Children), &count);
1595 // Tests persistence of the profile sync service by unloading the
1596 // database and then reloading it from disk.
1597 TEST_F(ProfileSyncServiceBookmarkTestWithData, Persistence) {
1598 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1601 WriteTestDataToBookmarkModel();
1605 // Force both models to discard their data and reload from disk. This
1606 // simulates what would happen if the browser were to shutdown normally,
1607 // and then relaunch.
1609 UnloadBookmarkModel();
1610 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE);
1613 ExpectBookmarkModelMatchesTestData();
1615 // With the BookmarkModel contents verified, ExpectModelMatch will
1616 // verify the contents of the sync model.
1620 // Tests the merge case when the BookmarkModel is non-empty but the
1621 // sync model is empty. This corresponds to uploading browser
1622 // bookmarks to an initially empty, new account.
1623 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeWithEmptySyncModel) {
1624 // Don't start the sync service until we've populated the bookmark model.
1625 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1627 WriteTestDataToBookmarkModel();
1629 // Restart sync. This should trigger a merge step during
1630 // initialization -- we expect the browser bookmarks to be written
1631 // to the sync service during this call.
1634 // Verify that the bookmark model hasn't changed, and that the sync model
1635 // matches it exactly.
1636 ExpectBookmarkModelMatchesTestData();
1640 // Tests the merge case when the BookmarkModel is empty but the sync model is
1641 // non-empty. This corresponds (somewhat) to a clean install of the browser,
1642 // with no bookmarks, connecting to a sync account that has some bookmarks.
1643 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeWithEmptyBookmarkModel) {
1644 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1647 WriteTestDataToBookmarkModel();
1651 // Force the databse to unload and write itself to disk.
1654 // Blow away the bookmark model -- it should be empty afterwards.
1655 UnloadBookmarkModel();
1656 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1657 EXPECT_EQ(model_->bookmark_bar_node()->child_count(), 0);
1658 EXPECT_EQ(model_->other_node()->child_count(), 0);
1659 EXPECT_EQ(model_->mobile_node()->child_count(), 0);
1661 // Now restart the sync service. Starting it should populate the bookmark
1662 // model -- test for consistency.
1664 ExpectBookmarkModelMatchesTestData();
1668 // Tests the merge cases when both the models are expected to be identical
1670 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeExpectedIdenticalModels) {
1671 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1673 WriteTestDataToBookmarkModel();
1676 UnloadBookmarkModel();
1678 // At this point both the bookmark model and the server should have the
1679 // exact same data and it should match the test data.
1680 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1682 ExpectBookmarkModelMatchesTestData();
1685 UnloadBookmarkModel();
1687 // Now reorder some bookmarks in the bookmark model and then merge. Make
1688 // sure we get the order of the server after merge.
1689 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE);
1690 ExpectBookmarkModelMatchesTestData();
1691 const BookmarkNode* bookmark_bar = model_->bookmark_bar_node();
1692 ASSERT_TRUE(bookmark_bar);
1693 ASSERT_GT(bookmark_bar->child_count(), 1);
1694 model_->Move(bookmark_bar->GetChild(0), bookmark_bar, 1);
1697 ExpectBookmarkModelMatchesTestData();
1700 // Tests the merge cases when both the models are expected to be identical
1702 TEST_F(ProfileSyncServiceBookmarkTestWithData, MergeModelsWithSomeExtras) {
1703 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1704 WriteTestDataToBookmarkModel();
1705 ExpectBookmarkModelMatchesTestData();
1707 // Remove some nodes and reorder some nodes.
1708 const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
1709 int remove_index = 2;
1710 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1711 const BookmarkNode* child_node = bookmark_bar_node->GetChild(remove_index);
1712 ASSERT_TRUE(child_node);
1713 ASSERT_TRUE(child_node->is_url());
1714 model_->Remove(bookmark_bar_node, remove_index);
1715 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1716 child_node = bookmark_bar_node->GetChild(remove_index);
1717 ASSERT_TRUE(child_node);
1718 ASSERT_TRUE(child_node->is_folder());
1719 model_->Remove(bookmark_bar_node, remove_index);
1721 const BookmarkNode* other_node = model_->other_node();
1722 ASSERT_GE(other_node->child_count(), 1);
1723 const BookmarkNode* f3_node = other_node->GetChild(0);
1724 ASSERT_TRUE(f3_node);
1725 ASSERT_TRUE(f3_node->is_folder());
1727 ASSERT_GT(f3_node->child_count(), remove_index);
1728 model_->Remove(f3_node, remove_index);
1729 ASSERT_GT(f3_node->child_count(), remove_index);
1730 model_->Remove(f3_node, remove_index);
1736 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1737 WriteTestDataToBookmarkModel();
1738 ExpectBookmarkModelMatchesTestData();
1740 // Remove some nodes and reorder some nodes.
1741 bookmark_bar_node = model_->bookmark_bar_node();
1743 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1744 child_node = bookmark_bar_node->GetChild(remove_index);
1745 ASSERT_TRUE(child_node);
1746 ASSERT_TRUE(child_node->is_url());
1747 model_->Remove(bookmark_bar_node, remove_index);
1748 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1749 child_node = bookmark_bar_node->GetChild(remove_index);
1750 ASSERT_TRUE(child_node);
1751 ASSERT_TRUE(child_node->is_folder());
1752 model_->Remove(bookmark_bar_node, remove_index);
1754 ASSERT_GE(bookmark_bar_node->child_count(), 2);
1755 model_->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 1);
1757 other_node = model_->other_node();
1758 ASSERT_GE(other_node->child_count(), 1);
1759 f3_node = other_node->GetChild(0);
1760 ASSERT_TRUE(f3_node);
1761 ASSERT_TRUE(f3_node->is_folder());
1763 ASSERT_GT(f3_node->child_count(), remove_index);
1764 model_->Remove(f3_node, remove_index);
1765 ASSERT_GT(f3_node->child_count(), remove_index);
1766 model_->Remove(f3_node, remove_index);
1768 ASSERT_GE(other_node->child_count(), 4);
1769 model_->Move(other_node->GetChild(0), other_node, 1);
1770 model_->Move(other_node->GetChild(2), other_node, 3);
1775 // After the merge, the model should match the test data.
1776 ExpectBookmarkModelMatchesTestData();
1779 // Tests that when persisted model associations are used, things work fine.
1780 TEST_F(ProfileSyncServiceBookmarkTestWithData, ModelAssociationPersistence) {
1781 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1782 WriteTestDataToBookmarkModel();
1785 // Force sync to shut down and write itself to disk.
1787 // Now restart sync. This time it should use the persistent
1793 // Tests that when persisted model associations are used, things work fine.
1794 TEST_F(ProfileSyncServiceBookmarkTestWithData,
1795 ModelAssociationInvalidPersistence) {
1796 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1797 WriteTestDataToBookmarkModel();
1800 // Force sync to shut down and write itself to disk.
1802 // Change the bookmark model before restarting sync service to simulate
1803 // the situation where bookmark model is different from sync model and
1804 // make sure model associator correctly rebuilds associations.
1805 const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
1806 model_->AddURL(bookmark_bar_node, 0, base::ASCIIToUTF16("xtra"),
1807 GURL("http://www.xtra.com"));
1808 // Now restart sync. This time it will try to use the persistent
1809 // associations and realize that they are invalid and hence will rebuild
1815 TEST_F(ProfileSyncServiceBookmarkTestWithData, SortChildren) {
1816 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1819 // Write test data to bookmark model and verify that the models match.
1820 WriteTestDataToBookmarkModel();
1821 const BookmarkNode* folder_added = model_->other_node()->GetChild(0);
1822 ASSERT_TRUE(folder_added);
1823 ASSERT_TRUE(folder_added->is_folder());
1827 // Sort the other-bookmarks children and expect that the models match.
1828 model_->SortChildren(folder_added);
1832 // See what happens if we enable sync but then delete the "Sync Data"
1834 TEST_F(ProfileSyncServiceBookmarkTestWithData,
1835 RecoverAfterDeletingSyncDataDirectory) {
1836 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE);
1839 WriteTestDataToBookmarkModel();
1843 // Nuke the sync DB and reload.
1847 // First attempt fails due to a persistence error.
1848 EXPECT_TRUE(CreatePermanentBookmarkNodes());
1849 EXPECT_FALSE(AssociateModels());
1851 // Second attempt succeeds due to the previous error resetting the native
1852 // transaction version.
1853 model_associator_.reset();
1854 EXPECT_TRUE(CreatePermanentBookmarkNodes());
1855 EXPECT_TRUE(AssociateModels());
1857 // Make sure we're back in sync. In real life, the user would need
1858 // to reauthenticate before this happens, but in the test, authentication
1860 ExpectBookmarkModelMatchesTestData();
1864 // Verify that the bookmark model is updated about whether the
1865 // associator is currently running.
1866 TEST_F(ProfileSyncServiceBookmarkTest, AssociationState) {
1867 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1869 ExtensiveChangesBookmarkModelObserver observer;
1870 model_->AddObserver(&observer);
1874 EXPECT_EQ(1, observer.get_started());
1875 EXPECT_EQ(0, observer.get_completed_count_at_started());
1876 EXPECT_EQ(1, observer.get_completed());
1878 model_->RemoveObserver(&observer);
1881 // Verify that the creation_time_us changes are applied in the local model at
1882 // association time and update time.
1883 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateDateAdded) {
1884 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1885 WriteTestDataToBookmarkModel();
1887 // Start and stop sync in order to create bookmark nodes in the sync db.
1891 // Modify the date_added field of a bookmark so it doesn't match with
1893 const BookmarkNode* bookmark_bar_node = model_->bookmark_bar_node();
1894 int remove_index = 2;
1895 ASSERT_GT(bookmark_bar_node->child_count(), remove_index);
1896 const BookmarkNode* child_node = bookmark_bar_node->GetChild(remove_index);
1897 ASSERT_TRUE(child_node);
1898 EXPECT_TRUE(child_node->is_url());
1899 model_->SetDateAdded(child_node, base::Time::FromInternalValue(10));
1903 // Everything should be back in sync after model association.
1904 ExpectBookmarkModelMatchesTestData();
1907 // Now trigger a change while syncing. We add a new bookmark, sync it, then
1908 // updates it's creation time.
1909 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1910 FakeServerChange adds(&trans);
1911 const std::string kTitle = "Some site";
1912 const std::string kUrl = "http://www.whatwhat.yeah/";
1913 const int kCreationTime = 30;
1914 int64 id = adds.AddURL(kTitle, kUrl,
1915 bookmark_bar_id(), 0);
1916 adds.ApplyPendingChanges(change_processor_.get());
1917 FakeServerChange updates(&trans);
1918 updates.ModifyCreationTime(id, kCreationTime);
1919 updates.ApplyPendingChanges(change_processor_.get());
1921 const BookmarkNode* node = model_->bookmark_bar_node()->GetChild(0);
1923 EXPECT_TRUE(node->is_url());
1924 EXPECT_EQ(base::UTF8ToUTF16(kTitle), node->GetTitle());
1925 EXPECT_EQ(kUrl, node->url().possibly_invalid_spec());
1926 EXPECT_EQ(node->date_added(), base::Time::FromInternalValue(30));
1929 // Tests that changes to the sync nodes meta info gets reflected in the local
1931 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateMetaInfoFromSync) {
1932 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1933 WriteTestDataToBookmarkModel();
1936 // Create bookmark nodes containing meta info.
1937 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
1938 FakeServerChange adds(&trans);
1939 BookmarkNode::MetaInfoMap folder_meta_info;
1940 folder_meta_info["folder"] = "foldervalue";
1941 int64 folder_id = adds.AddFolderWithMetaInfo(
1942 "folder title", &folder_meta_info, bookmark_bar_id(), 0);
1943 BookmarkNode::MetaInfoMap node_meta_info;
1944 node_meta_info["node"] = "nodevalue";
1945 node_meta_info["other"] = "othervalue";
1946 int64 id = adds.AddURLWithMetaInfo("node title", "http://www.foo.com",
1947 &node_meta_info, folder_id, 0);
1948 adds.ApplyPendingChanges(change_processor_.get());
1950 // Verify that the nodes are created with the correct meta info.
1951 ASSERT_LT(0, model_->bookmark_bar_node()->child_count());
1952 const BookmarkNode* folder_node = model_->bookmark_bar_node()->GetChild(0);
1953 ASSERT_TRUE(folder_node->GetMetaInfoMap());
1954 EXPECT_EQ(folder_meta_info, *folder_node->GetMetaInfoMap());
1955 ASSERT_LT(0, folder_node->child_count());
1956 const BookmarkNode* node = folder_node->GetChild(0);
1957 ASSERT_TRUE(node->GetMetaInfoMap());
1958 EXPECT_EQ(node_meta_info, *node->GetMetaInfoMap());
1960 // Update meta info on nodes on server
1961 FakeServerChange updates(&trans);
1962 folder_meta_info.erase("folder");
1963 updates.ModifyMetaInfo(folder_id, folder_meta_info);
1964 node_meta_info["node"] = "changednodevalue";
1965 node_meta_info.erase("other");
1966 node_meta_info["newkey"] = "newkeyvalue";
1967 updates.ModifyMetaInfo(id, node_meta_info);
1968 updates.ApplyPendingChanges(change_processor_.get());
1970 // Confirm that the updated values are reflected in the bookmark nodes.
1971 EXPECT_FALSE(folder_node->GetMetaInfoMap());
1972 ASSERT_TRUE(node->GetMetaInfoMap());
1973 EXPECT_EQ(node_meta_info, *node->GetMetaInfoMap());
1976 // Tests that changes to the local bookmark nodes meta info gets reflected in
1978 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateMetaInfoFromModel) {
1979 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
1980 WriteTestDataToBookmarkModel();
1982 ExpectBookmarkModelMatchesTestData();
1984 const BookmarkNode* folder_node =
1985 model_->AddFolder(model_->bookmark_bar_node(), 0,
1986 base::ASCIIToUTF16("folder title"));
1987 const BookmarkNode* node = model_->AddURL(folder_node, 0,
1988 base::ASCIIToUTF16("node title"),
1989 GURL("http://www.foo.com"));
1992 // Add some meta info and verify sync model matches the changes.
1993 model_->SetNodeMetaInfo(folder_node, "folder", "foldervalue");
1994 model_->SetNodeMetaInfo(node, "node", "nodevalue");
1995 model_->SetNodeMetaInfo(node, "other", "othervalue");
1998 // Change/delete existing meta info and verify.
1999 model_->DeleteNodeMetaInfo(folder_node, "folder");
2000 model_->SetNodeMetaInfo(node, "node", "changednodevalue");
2001 model_->DeleteNodeMetaInfo(node, "other");
2002 model_->SetNodeMetaInfo(node, "newkey", "newkeyvalue");
2006 // Output transaction versions of |node| and nodes under it to |node_versions|.
2007 void GetTransactionVersions(
2008 const BookmarkNode* root,
2009 BookmarkNodeVersionMap* node_versions) {
2010 node_versions->clear();
2011 std::queue<const BookmarkNode*> nodes;
2013 while (!nodes.empty()) {
2014 const BookmarkNode* n = nodes.front();
2017 int64 version = n->sync_transaction_version();
2018 EXPECT_NE(BookmarkNode::kInvalidSyncTransactionVersion, version);
2020 (*node_versions)[n->id()] = version;
2021 for (int i = 0; i < n->child_count(); ++i)
2022 nodes.push(n->GetChild(i));
2026 void ProfileSyncServiceBookmarkTestWithData::ExpectTransactionVersionMatch(
2027 const BookmarkNode* node,
2028 const BookmarkNodeVersionMap& version_expected) {
2029 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
2031 BookmarkNodeVersionMap bnodes_versions;
2032 GetTransactionVersions(node, &bnodes_versions);
2033 for (BookmarkNodeVersionMap::const_iterator it = bnodes_versions.begin();
2034 it != bnodes_versions.end(); ++it) {
2035 syncer::ReadNode sync_node(&trans);
2036 ASSERT_TRUE(model_associator_->InitSyncNodeFromChromeId(it->first,
2038 EXPECT_EQ(sync_node.GetEntry()->GetTransactionVersion(), it->second);
2039 BookmarkNodeVersionMap::const_iterator expected_ver_it =
2040 version_expected.find(it->first);
2041 if (expected_ver_it != version_expected.end())
2042 EXPECT_EQ(expected_ver_it->second, it->second);
2046 // Test transaction versions of model and nodes are incremented after changes
2048 TEST_F(ProfileSyncServiceBookmarkTestWithData, UpdateTransactionVersion) {
2049 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
2051 WriteTestDataToBookmarkModel();
2052 base::MessageLoop::current()->RunUntilIdle();
2054 BookmarkNodeVersionMap initial_versions;
2056 // Verify transaction versions in sync model and bookmark model (saved as
2057 // transaction version of root node) are equal after
2058 // WriteTestDataToBookmarkModel() created bookmarks.
2060 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
2061 EXPECT_GT(trans.GetModelVersion(syncer::BOOKMARKS), 0);
2062 GetTransactionVersions(model_->root_node(), &initial_versions);
2063 EXPECT_EQ(trans.GetModelVersion(syncer::BOOKMARKS),
2064 initial_versions[model_->root_node()->id()]);
2066 ExpectTransactionVersionMatch(model_->bookmark_bar_node(),
2067 BookmarkNodeVersionMap());
2068 ExpectTransactionVersionMatch(model_->other_node(),
2069 BookmarkNodeVersionMap());
2070 ExpectTransactionVersionMatch(model_->mobile_node(),
2071 BookmarkNodeVersionMap());
2073 // Verify model version is incremented and bookmark node versions remain
2075 const BookmarkNode* bookmark_bar = model_->bookmark_bar_node();
2076 model_->Remove(bookmark_bar, 0);
2077 base::MessageLoop::current()->RunUntilIdle();
2078 BookmarkNodeVersionMap new_versions;
2079 GetTransactionVersions(model_->root_node(), &new_versions);
2080 EXPECT_EQ(initial_versions[model_->root_node()->id()] + 1,
2081 new_versions[model_->root_node()->id()]);
2082 ExpectTransactionVersionMatch(model_->bookmark_bar_node(), initial_versions);
2083 ExpectTransactionVersionMatch(model_->other_node(), initial_versions);
2084 ExpectTransactionVersionMatch(model_->mobile_node(), initial_versions);
2086 // Verify model version and version of changed bookmark are incremented and
2087 // versions of others remain same.
2088 const BookmarkNode* changed_bookmark =
2089 model_->bookmark_bar_node()->GetChild(0);
2090 model_->SetTitle(changed_bookmark, base::ASCIIToUTF16("test"));
2091 base::MessageLoop::current()->RunUntilIdle();
2092 GetTransactionVersions(model_->root_node(), &new_versions);
2093 EXPECT_EQ(initial_versions[model_->root_node()->id()] + 2,
2094 new_versions[model_->root_node()->id()]);
2095 EXPECT_LT(initial_versions[changed_bookmark->id()],
2096 new_versions[changed_bookmark->id()]);
2097 initial_versions.erase(changed_bookmark->id());
2098 ExpectTransactionVersionMatch(model_->bookmark_bar_node(), initial_versions);
2099 ExpectTransactionVersionMatch(model_->other_node(), initial_versions);
2100 ExpectTransactionVersionMatch(model_->mobile_node(), initial_versions);
2103 // Test that sync persistence errors are detected and trigger a failed
2105 TEST_F(ProfileSyncServiceBookmarkTestWithData, PersistenceError) {
2106 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
2108 WriteTestDataToBookmarkModel();
2109 base::MessageLoop::current()->RunUntilIdle();
2111 BookmarkNodeVersionMap initial_versions;
2113 // Verify transaction versions in sync model and bookmark model (saved as
2114 // transaction version of root node) are equal after
2115 // WriteTestDataToBookmarkModel() created bookmarks.
2117 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share());
2118 EXPECT_GT(trans.GetModelVersion(syncer::BOOKMARKS), 0);
2119 GetTransactionVersions(model_->root_node(), &initial_versions);
2120 EXPECT_EQ(trans.GetModelVersion(syncer::BOOKMARKS),
2121 initial_versions[model_->root_node()->id()]);
2123 ExpectTransactionVersionMatch(model_->bookmark_bar_node(),
2124 BookmarkNodeVersionMap());
2125 ExpectTransactionVersionMatch(model_->other_node(),
2126 BookmarkNodeVersionMap());
2127 ExpectTransactionVersionMatch(model_->mobile_node(),
2128 BookmarkNodeVersionMap());
2130 // Now shut down sync and artificially increment the native model's version.
2132 int64 root_version = initial_versions[model_->root_node()->id()];
2133 model_->SetNodeSyncTransactionVersion(model_->root_node(), root_version + 1);
2135 // Upon association, bookmarks should fail to associate.
2136 EXPECT_FALSE(AssociateModels());
2141 } // namespace browser_sync