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