profile_sync_service_unittest.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <stack> 6#include <vector> 7 8#include "testing/gtest/include/gtest/gtest.h" 9#include "base/message_loop.h" 10#include "base/scoped_ptr.h" 11#include "base/string_util.h" 12#include "base/string16.h" 13#include "chrome/browser/bookmarks/bookmark_model.h" 14#include "chrome/browser/chrome_thread.h" 15#include "chrome/browser/profile.h" 16#include "chrome/browser/sync/engine/syncapi.h" 17#include "chrome/browser/sync/glue/change_processor.h" 18#include "chrome/browser/sync/glue/bookmark_change_processor.h" 19#include "chrome/browser/sync/glue/bookmark_data_type_controller.h" 20#include "chrome/browser/sync/glue/bookmark_model_associator.h" 21#include "chrome/browser/sync/glue/data_type_controller.h" 22#include "chrome/browser/sync/glue/model_associator.h" 23#include "chrome/browser/sync/glue/sync_backend_host.h" 24#include "chrome/browser/sync/glue/sync_backend_host_mock.h" 25#include "chrome/browser/sync/notification_method.h" 26#include "chrome/browser/sync/profile_sync_factory.h" 27#include "chrome/browser/sync/profile_sync_factory_mock.h" 28#include "chrome/browser/sync/test_profile_sync_service.h" 29#include "chrome/browser/sync/profile_sync_test_util.h" 30#include "chrome/common/chrome_switches.h" 31#include "chrome/common/pref_names.h" 32#include "chrome/test/testing_profile.h" 33#include "testing/gmock/include/gmock/gmock.h" 34 35using std::vector; 36using browser_sync::AssociatorInterface; 37using browser_sync::BookmarkChangeProcessor; 38using browser_sync::BookmarkModelAssociator; 39using browser_sync::ChangeProcessor; 40using browser_sync::DataTypeController; 41using browser_sync::ModelAssociator; 42using browser_sync::SyncBackendHost; 43using browser_sync::SyncBackendHostMock; 44using browser_sync::UnrecoverableErrorHandler; 45using testing::_; 46using testing::Return; 47using testing::WithArg; 48using testing::Invoke; 49 50class TestBookmarkModelAssociator : public BookmarkModelAssociator { 51 public: 52 TestBookmarkModelAssociator(ProfileSyncService* service, 53 UnrecoverableErrorHandler* persist_ids_error_handler) 54 : BookmarkModelAssociator(service, persist_ids_error_handler), 55 helper_(new TestModelAssociatorHelper()) { 56 } 57 virtual bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id) { 58 return helper_->GetSyncIdForTaggedNode(this, tag, sync_id); 59 } 60 private: 61 scoped_ptr<TestModelAssociatorHelper> helper_; 62}; 63 64// FakeServerChange constructs a list of sync_api::ChangeRecords while modifying 65// the sync model, and can pass the ChangeRecord list to a 66// sync_api::SyncObserver (i.e., the ProfileSyncService) to test the client 67// change-application behavior. 68// Tests using FakeServerChange should be careful to avoid back-references, 69// since FakeServerChange will send the edits in the order specified. 70class FakeServerChange { 71 public: 72 explicit FakeServerChange(sync_api::WriteTransaction* trans) : trans_(trans) { 73 } 74 75 // Pretend that the server told the syncer to add a bookmark object. 76 int64 Add(const std::wstring& title, 77 const std::string& url, 78 bool is_folder, 79 int64 parent_id, 80 int64 predecessor_id) { 81 sync_api::ReadNode parent(trans_); 82 EXPECT_TRUE(parent.InitByIdLookup(parent_id)); 83 sync_api::WriteNode node(trans_); 84 if (predecessor_id == 0) { 85 EXPECT_TRUE(node.InitByCreation(syncable::BOOKMARKS, parent, NULL)); 86 } else { 87 sync_api::ReadNode predecessor(trans_); 88 EXPECT_TRUE(predecessor.InitByIdLookup(predecessor_id)); 89 EXPECT_EQ(predecessor.GetParentId(), parent.GetId()); 90 EXPECT_TRUE(node.InitByCreation(syncable::BOOKMARKS, parent, 91 &predecessor)); 92 } 93 EXPECT_EQ(node.GetPredecessorId(), predecessor_id); 94 EXPECT_EQ(node.GetParentId(), parent_id); 95 node.SetIsFolder(is_folder); 96 node.SetTitle(title); 97 if (!is_folder) 98 node.SetURL(GURL(url)); 99 sync_api::SyncManager::ChangeRecord record; 100 record.action = sync_api::SyncManager::ChangeRecord::ACTION_ADD; 101 record.id = node.GetId(); 102 changes_.push_back(record); 103 return node.GetId(); 104 } 105 106 // Add a bookmark folder. 107 int64 AddFolder(const std::wstring& title, 108 int64 parent_id, 109 int64 predecessor_id) { 110 return Add(title, std::string(), true, parent_id, predecessor_id); 111 } 112 113 // Add a bookmark. 114 int64 AddURL(const std::wstring& title, 115 const std::string& url, 116 int64 parent_id, 117 int64 predecessor_id) { 118 return Add(title, url, false, parent_id, predecessor_id); 119 } 120 121 // Pretend that the server told the syncer to delete an object. 122 void Delete(int64 id) { 123 { 124 // Delete the sync node. 125 sync_api::WriteNode node(trans_); 126 EXPECT_TRUE(node.InitByIdLookup(id)); 127 EXPECT_FALSE(node.GetFirstChildId()); 128 node.Remove(); 129 } 130 { 131 // Verify the deletion. 132 sync_api::ReadNode node(trans_); 133 EXPECT_FALSE(node.InitByIdLookup(id)); 134 } 135 136 sync_api::SyncManager::ChangeRecord record; 137 record.action = sync_api::SyncManager::ChangeRecord::ACTION_DELETE; 138 record.id = id; 139 // Deletions are always first in the changelist, but we can't actually do 140 // WriteNode::Remove() on the node until its children are moved. So, as 141 // a practical matter, users of FakeServerChange must move or delete 142 // children before parents. Thus, we must insert the deletion record 143 // at the front of the vector. 144 changes_.insert(changes_.begin(), record); 145 } 146 147 // Set a new title value, and return the old value. 148 std::wstring ModifyTitle(int64 id, const std::wstring& new_title) { 149 sync_api::WriteNode node(trans_); 150 EXPECT_TRUE(node.InitByIdLookup(id)); 151 std::wstring old_title = node.GetTitle(); 152 node.SetTitle(new_title); 153 SetModified(id); 154 return old_title; 155 } 156 157 // Set a new parent and predecessor value. Return the old parent id. 158 // We could return the old predecessor id, but it turns out not to be 159 // very useful for assertions. 160 int64 ModifyPosition(int64 id, int64 parent_id, int64 predecessor_id) { 161 sync_api::ReadNode parent(trans_); 162 EXPECT_TRUE(parent.InitByIdLookup(parent_id)); 163 sync_api::WriteNode node(trans_); 164 EXPECT_TRUE(node.InitByIdLookup(id)); 165 int64 old_parent_id = node.GetParentId(); 166 if (predecessor_id == 0) { 167 EXPECT_TRUE(node.SetPosition(parent, NULL)); 168 } else { 169 sync_api::ReadNode predecessor(trans_); 170 EXPECT_TRUE(predecessor.InitByIdLookup(predecessor_id)); 171 EXPECT_EQ(predecessor.GetParentId(), parent.GetId()); 172 EXPECT_TRUE(node.SetPosition(parent, &predecessor)); 173 } 174 SetModified(id); 175 return old_parent_id; 176 } 177 178 // Pass the fake change list to |service|. 179 void ApplyPendingChanges(browser_sync::ChangeProcessor* processor) { 180 processor->ApplyChangesFromSyncModel(trans_, 181 changes_.size() ? &changes_[0] : NULL, changes_.size()); 182 } 183 184 const vector<sync_api::SyncManager::ChangeRecord>& changes() { 185 return changes_; 186 } 187 188 private: 189 // Helper function to push an ACTION_UPDATE record onto the back 190 // of the changelist. 191 void SetModified(int64 id) { 192 // Coalesce multi-property edits. 193 if (changes_.size() > 0 && changes_.back().id == id && 194 changes_.back().action == 195 sync_api::SyncManager::ChangeRecord::ACTION_UPDATE) 196 return; 197 sync_api::SyncManager::ChangeRecord record; 198 record.action = sync_api::SyncManager::ChangeRecord::ACTION_UPDATE; 199 record.id = id; 200 changes_.push_back(record); 201 } 202 203 // The transaction on which everything happens. 204 sync_api::WriteTransaction *trans_; 205 206 // The change list we construct. 207 vector<sync_api::SyncManager::ChangeRecord> changes_; 208}; 209 210class ProfileSyncServiceTest : public testing::Test { 211 protected: 212 enum LoadOption { LOAD_FROM_STORAGE, DELETE_EXISTING_STORAGE }; 213 enum SaveOption { SAVE_TO_STORAGE, DONT_SAVE_TO_STORAGE }; 214 ProfileSyncServiceTest() 215 : ui_thread_(ChromeThread::UI, &message_loop_), 216 file_thread_(ChromeThread::FILE, &message_loop_), 217 model_(NULL), 218 model_associator_(NULL), 219 change_processor_(NULL) { 220 profile_.reset(new TestingProfile()); 221 profile_->set_has_history_service(true); 222 } 223 virtual ~ProfileSyncServiceTest() { 224 // Kill the service before the profile. 225 service_.reset(); 226 profile_.reset(); 227 228 // Ensure that the sync objects destruct to avoid memory leaks. 229 MessageLoop::current()->RunAllPending(); 230 } 231 232 BookmarkModelAssociator* associator() { 233 return model_associator_; 234 } 235 236 BookmarkChangeProcessor* change_processor() { 237 return change_processor_; 238 } 239 240 void StartSyncService() { 241 if (!service_.get()) { 242 service_.reset(new TestProfileSyncService(&factory_, 243 profile_.get(), 244 false, false, NULL)); 245 246 // Register the bookmark data type. 247 model_associator_ = new TestBookmarkModelAssociator(service_.get(), 248 service_.get()); 249 change_processor_ = new BookmarkChangeProcessor(model_associator_, 250 service_.get()); 251 EXPECT_CALL(factory_, CreateBookmarkSyncComponents(_, _)). 252 WillOnce(Return(ProfileSyncFactory::SyncComponents( 253 model_associator_, change_processor_))); 254 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). 255 WillOnce(ReturnNewDataTypeManager()); 256 257 service_->RegisterDataTypeController( 258 new browser_sync::BookmarkDataTypeController(&factory_, 259 profile_.get(), 260 service_.get())); 261 service_->Initialize(); 262 MessageLoop::current()->Run(); 263 } 264 } 265 266 void StopSyncService(SaveOption save) { 267 if (save == DONT_SAVE_TO_STORAGE) 268 service_->DisableForUser(); 269 service_.reset(); 270 } 271 272 // Load (or re-load) the bookmark model. |load| controls use of the 273 // bookmarks file on disk. |save| controls whether the newly loaded 274 // bookmark model will write out a bookmark file as it goes. 275 void LoadBookmarkModel(LoadOption load, SaveOption save) { 276 bool delete_bookmarks = load == DELETE_EXISTING_STORAGE; 277 profile_->CreateBookmarkModel(delete_bookmarks); 278 model_ = profile_->GetBookmarkModel(); 279 // Wait for the bookmarks model to load. 280 profile_->BlockUntilBookmarkModelLoaded(); 281 // This noticeably speeds up the unit tests that request it. 282 if (save == DONT_SAVE_TO_STORAGE) 283 model_->ClearStore(); 284 } 285 286 void ExpectSyncerNodeMatching(sync_api::BaseTransaction* trans, 287 const BookmarkNode* bnode) { 288 sync_api::ReadNode gnode(trans); 289 EXPECT_TRUE(associator()->InitSyncNodeFromChromeId(bnode->id(), &gnode)); 290 // Non-root node titles and parents must match. 291 if (bnode != model_->GetBookmarkBarNode() && 292 bnode != model_->other_node()) { 293 EXPECT_EQ(bnode->GetTitle(), gnode.GetTitle()); 294 EXPECT_EQ(associator()->GetChromeNodeFromSyncId(gnode.GetParentId()), 295 bnode->GetParent()); 296 } 297 EXPECT_EQ(bnode->is_folder(), gnode.GetIsFolder()); 298 if (bnode->is_url()) 299 EXPECT_EQ(bnode->GetURL(), gnode.GetURL()); 300 301 // Check for position matches. 302 int browser_index = bnode->GetParent()->IndexOfChild(bnode); 303 if (browser_index == 0) { 304 EXPECT_EQ(gnode.GetPredecessorId(), 0); 305 } else { 306 const BookmarkNode* bprev = 307 bnode->GetParent()->GetChild(browser_index - 1); 308 sync_api::ReadNode gprev(trans); 309 ASSERT_TRUE(associator()->InitSyncNodeFromChromeId(bprev->id(), 310 &gprev)); 311 EXPECT_EQ(gnode.GetPredecessorId(), gprev.GetId()); 312 EXPECT_EQ(gnode.GetParentId(), gprev.GetParentId()); 313 } 314 if (browser_index == bnode->GetParent()->GetChildCount() - 1) { 315 EXPECT_EQ(gnode.GetSuccessorId(), 0); 316 } else { 317 const BookmarkNode* bnext = 318 bnode->GetParent()->GetChild(browser_index + 1); 319 sync_api::ReadNode gnext(trans); 320 ASSERT_TRUE(associator()->InitSyncNodeFromChromeId(bnext->id(), 321 &gnext)); 322 EXPECT_EQ(gnode.GetSuccessorId(), gnext.GetId()); 323 EXPECT_EQ(gnode.GetParentId(), gnext.GetParentId()); 324 } 325 if (bnode->GetChildCount()) { 326 EXPECT_TRUE(gnode.GetFirstChildId()); 327 } 328 } 329 330 void ExpectSyncerNodeMatching(const BookmarkNode* bnode) { 331 sync_api::ReadTransaction trans(service_->backend_->GetUserShareHandle()); 332 ExpectSyncerNodeMatching(&trans, bnode); 333 } 334 335 void ExpectBrowserNodeMatching(sync_api::BaseTransaction* trans, 336 int64 sync_id) { 337 EXPECT_TRUE(sync_id); 338 const BookmarkNode* bnode = 339 associator()->GetChromeNodeFromSyncId(sync_id); 340 ASSERT_TRUE(bnode); 341 int64 id = associator()->GetSyncIdFromChromeId(bnode->id()); 342 EXPECT_EQ(id, sync_id); 343 ExpectSyncerNodeMatching(trans, bnode); 344 } 345 346 void ExpectBrowserNodeUnknown(int64 sync_id) { 347 EXPECT_FALSE(associator()->GetChromeNodeFromSyncId(sync_id)); 348 } 349 350 void ExpectBrowserNodeKnown(int64 sync_id) { 351 EXPECT_TRUE(associator()->GetChromeNodeFromSyncId(sync_id)); 352 } 353 354 void ExpectSyncerNodeKnown(const BookmarkNode* node) { 355 int64 sync_id = associator()->GetSyncIdFromChromeId(node->id()); 356 EXPECT_NE(sync_id, sync_api::kInvalidId); 357 } 358 359 void ExpectSyncerNodeUnknown(const BookmarkNode* node) { 360 int64 sync_id = associator()->GetSyncIdFromChromeId(node->id()); 361 EXPECT_EQ(sync_id, sync_api::kInvalidId); 362 } 363 364 void ExpectBrowserNodeTitle(int64 sync_id, const std::wstring& title) { 365 const BookmarkNode* bnode = 366 associator()->GetChromeNodeFromSyncId(sync_id); 367 ASSERT_TRUE(bnode); 368 EXPECT_EQ(bnode->GetTitle(), title); 369 } 370 371 void ExpectBrowserNodeURL(int64 sync_id, const std::string& url) { 372 const BookmarkNode* bnode = 373 associator()->GetChromeNodeFromSyncId(sync_id); 374 ASSERT_TRUE(bnode); 375 EXPECT_EQ(GURL(url), bnode->GetURL()); 376 } 377 378 void ExpectBrowserNodeParent(int64 sync_id, int64 parent_sync_id) { 379 const BookmarkNode* node = associator()->GetChromeNodeFromSyncId(sync_id); 380 ASSERT_TRUE(node); 381 const BookmarkNode* parent = 382 associator()->GetChromeNodeFromSyncId(parent_sync_id); 383 EXPECT_TRUE(parent); 384 EXPECT_EQ(node->GetParent(), parent); 385 } 386 387 void ExpectModelMatch(sync_api::BaseTransaction* trans) { 388 const BookmarkNode* root = model_->root_node(); 389 EXPECT_EQ(root->IndexOfChild(model_->GetBookmarkBarNode()), 0); 390 EXPECT_EQ(root->IndexOfChild(model_->other_node()), 1); 391 392 std::stack<int64> stack; 393 stack.push(bookmark_bar_id()); 394 while (!stack.empty()) { 395 int64 id = stack.top(); 396 stack.pop(); 397 if (!id) continue; 398 399 ExpectBrowserNodeMatching(trans, id); 400 401 sync_api::ReadNode gnode(trans); 402 ASSERT_TRUE(gnode.InitByIdLookup(id)); 403 stack.push(gnode.GetFirstChildId()); 404 stack.push(gnode.GetSuccessorId()); 405 } 406 } 407 408 void ExpectModelMatch() { 409 sync_api::ReadTransaction trans(service_->backend_->GetUserShareHandle()); 410 ExpectModelMatch(&trans); 411 } 412 413 int64 other_bookmarks_id() { 414 return associator()->GetSyncIdFromChromeId(model_->other_node()->id()); 415 } 416 417 int64 bookmark_bar_id() { 418 return associator()->GetSyncIdFromChromeId( 419 model_->GetBookmarkBarNode()->id()); 420 } 421 422 SyncBackendHost* backend() { return service_->backend_.get(); } 423 424 // This serves as the "UI loop" on which the ProfileSyncService lives and 425 // operates. It is needed because the SyncBackend can post tasks back to 426 // the service, meaning it can't be null. It doesn't have to be running, 427 // though -- OnInitializationCompleted is the only example (so far) in this 428 // test where we need to Run the loop to swallow a task and then quit, to 429 // avoid leaking the ProfileSyncService (the PostTask will retain the callee 430 // and caller until the task is run). 431 MessageLoop message_loop_; 432 ChromeThread ui_thread_; 433 ChromeThread file_thread_; 434 435 scoped_ptr<TestProfileSyncService> service_; 436 scoped_ptr<TestingProfile> profile_; 437 ProfileSyncFactoryMock factory_; 438 BookmarkModel* model_; 439 TestBookmarkModelAssociator* model_associator_; 440 BookmarkChangeProcessor* change_processor_; 441}; 442 443TEST_F(ProfileSyncServiceTest, InitialState) { 444 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 445 StartSyncService(); 446 447 EXPECT_TRUE( 448 service_->sync_service_url_.spec() == 449 ProfileSyncService::kSyncServerUrl || 450 service_->sync_service_url_.spec() == 451 ProfileSyncService::kDevServerUrl); 452 453 EXPECT_TRUE(other_bookmarks_id()); 454 EXPECT_TRUE(bookmark_bar_id()); 455 456 ExpectModelMatch(); 457} 458 459TEST_F(ProfileSyncServiceTest, AbortedByShutdown) { 460 service_.reset(new TestProfileSyncService(&factory_, profile_.get(), 461 false, true, NULL)); 462 service_->set_num_expected_resumes(0); 463 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). 464 WillOnce(ReturnNewDataTypeManager()); 465 EXPECT_CALL(factory_, CreateBookmarkSyncComponents(_, _)).Times(0); 466 service_->RegisterDataTypeController( 467 new browser_sync::BookmarkDataTypeController(&factory_, 468 profile_.get(), 469 service_.get())); 470 service_->Initialize(); 471 service_.reset(); 472} 473 474TEST_F(ProfileSyncServiceTest, BookmarkModelOperations) { 475 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 476 StartSyncService(); 477 478 // Test addition. 479 const BookmarkNode* folder = 480 model_->AddGroup(model_->other_node(), 0, L"foobar"); 481 ExpectSyncerNodeMatching(folder); 482 ExpectModelMatch(); 483 const BookmarkNode* folder2 = model_->AddGroup(folder, 0, L"nested"); 484 ExpectSyncerNodeMatching(folder2); 485 ExpectModelMatch(); 486 const BookmarkNode* url1 = model_->AddURL( 487 folder, 0, L"Internets #1 Pies Site", GURL("http://www.easypie.com/")); 488 ExpectSyncerNodeMatching(url1); 489 ExpectModelMatch(); 490 const BookmarkNode* url2 = model_->AddURL( 491 folder, 1, L"Airplanes", GURL("http://www.easyjet.com/")); 492 ExpectSyncerNodeMatching(url2); 493 ExpectModelMatch(); 494 495 // Test modification. 496 model_->SetTitle(url2, L"EasyJet"); 497 ExpectModelMatch(); 498 model_->Move(url1, folder2, 0); 499 ExpectModelMatch(); 500 model_->Move(folder2, model_->GetBookmarkBarNode(), 0); 501 ExpectModelMatch(); 502 model_->SetTitle(folder2, L"Not Nested"); 503 ExpectModelMatch(); 504 model_->Move(folder, folder2, 0); 505 ExpectModelMatch(); 506 model_->SetTitle(folder, L"who's nested now?"); 507 ExpectModelMatch(); 508 model_->Copy(url2, model_->GetBookmarkBarNode(), 0); 509 ExpectModelMatch(); 510 511 // Test deletion. 512 // Delete a single item. 513 model_->Remove(url2->GetParent(), url2->GetParent()->IndexOfChild(url2)); 514 ExpectModelMatch(); 515 // Delete an item with several children. 516 model_->Remove(folder2->GetParent(), 517 folder2->GetParent()->IndexOfChild(folder2)); 518 ExpectModelMatch(); 519} 520 521TEST_F(ProfileSyncServiceTest, ServerChangeProcessing) { 522 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 523 StartSyncService(); 524 525 sync_api::WriteTransaction trans(backend()->GetUserShareHandle()); 526 527 FakeServerChange adds(&trans); 528 int64 f1 = adds.AddFolder(L"Server Folder B", bookmark_bar_id(), 0); 529 int64 f2 = adds.AddFolder(L"Server Folder A", bookmark_bar_id(), f1); 530 int64 u1 = adds.AddURL(L"Some old site", "ftp://nifty.andrew.cmu.edu/", 531 bookmark_bar_id(), f2); 532 int64 u2 = adds.AddURL(L"Nifty", "ftp://nifty.andrew.cmu.edu/", f1, 0); 533 // u3 is a duplicate URL 534 int64 u3 = adds.AddURL(L"Nifty2", "ftp://nifty.andrew.cmu.edu/", f1, u2); 535 // u4 is a duplicate title, different URL. 536 adds.AddURL(L"Some old site", "http://slog.thestranger.com/", 537 bookmark_bar_id(), u1); 538 // u5 tests an empty-string title. 539 std::string javascript_url("javascript:(function(){var w=window.open(" \ 540 "'about:blank','gnotesWin','location=0,menubar=0," \ 541 "scrollbars=0,status=0,toolbar=0,width=300," \ 542 "height=300,resizable');});"); 543 adds.AddURL(L"", javascript_url, other_bookmarks_id(), 0); 544 545 vector<sync_api::SyncManager::ChangeRecord>::const_iterator it; 546 // The bookmark model shouldn't yet have seen any of the nodes of |adds|. 547 for (it = adds.changes().begin(); it != adds.changes().end(); ++it) 548 ExpectBrowserNodeUnknown(it->id); 549 550 adds.ApplyPendingChanges(change_processor()); 551 552 // Make sure the bookmark model received all of the nodes in |adds|. 553 for (it = adds.changes().begin(); it != adds.changes().end(); ++it) 554 ExpectBrowserNodeMatching(&trans, it->id); 555 ExpectModelMatch(&trans); 556 557 // Part two: test modifications. 558 FakeServerChange mods(&trans); 559 // Mess with u2, and move it into empty folder f2 560 // TODO(ncarter): Determine if we allow ModifyURL ops or not. 561 /* std::wstring u2_old_url = mods.ModifyURL(u2, L"http://www.google.com"); */ 562 std::wstring u2_old_title = mods.ModifyTitle(u2, L"The Google"); 563 int64 u2_old_parent = mods.ModifyPosition(u2, f2, 0); 564 565 // Now move f1 after u2. 566 std::wstring f1_old_title = mods.ModifyTitle(f1, L"Server Folder C"); 567 int64 f1_old_parent = mods.ModifyPosition(f1, f2, u2); 568 569 // Then add u3 after f1. 570 int64 u3_old_parent = mods.ModifyPosition(u3, f2, f1); 571 572 // Test that the property changes have not yet taken effect. 573 ExpectBrowserNodeTitle(u2, u2_old_title); 574 /* ExpectBrowserNodeURL(u2, u2_old_url); */ 575 ExpectBrowserNodeParent(u2, u2_old_parent); 576 577 ExpectBrowserNodeTitle(f1, f1_old_title); 578 ExpectBrowserNodeParent(f1, f1_old_parent); 579 580 ExpectBrowserNodeParent(u3, u3_old_parent); 581 582 // Apply the changes. 583 mods.ApplyPendingChanges(change_processor()); 584 585 // Check for successful application. 586 for (it = mods.changes().begin(); it != mods.changes().end(); ++it) 587 ExpectBrowserNodeMatching(&trans, it->id); 588 ExpectModelMatch(&trans); 589 590 // Part 3: Test URL deletion. 591 FakeServerChange dels(&trans); 592 dels.Delete(u2); 593 dels.Delete(u3); 594 595 ExpectBrowserNodeKnown(u2); 596 ExpectBrowserNodeKnown(u3); 597 598 dels.ApplyPendingChanges(change_processor()); 599 600 ExpectBrowserNodeUnknown(u2); 601 ExpectBrowserNodeUnknown(u3); 602 ExpectModelMatch(&trans); 603} 604 605// Tests a specific case in ApplyModelChanges where we move the 606// children out from under a parent, and then delete the parent 607// in the same changelist. The delete shows up first in the changelist, 608// requiring the children to be moved to a temporary location. 609TEST_F(ProfileSyncServiceTest, ServerChangeRequiringFosterParent) { 610 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 611 StartSyncService(); 612 613 sync_api::WriteTransaction trans(backend()->GetUserShareHandle()); 614 615 // Stress the immediate children of other_node because that's where 616 // ApplyModelChanges puts a temporary foster parent node. 617 std::string url("http://dev.chromium.org/"); 618 FakeServerChange adds(&trans); 619 int64 f0 = other_bookmarks_id(); // + other_node 620 int64 f1 = adds.AddFolder(L"f1", f0, 0); // + f1 621 int64 f2 = adds.AddFolder(L"f2", f1, 0); // + f2 622 int64 u3 = adds.AddURL( L"u3", url, f2, 0); // + u3 623 int64 u4 = adds.AddURL( L"u4", url, f2, u3); // + u4 624 int64 u5 = adds.AddURL( L"u5", url, f1, f2); // + u5 625 int64 f6 = adds.AddFolder(L"f6", f1, u5); // + f6 626 int64 u7 = adds.AddURL( L"u7", url, f0, f1); // + u7 627 628 vector<sync_api::SyncManager::ChangeRecord>::const_iterator it; 629 // The bookmark model shouldn't yet have seen any of the nodes of |adds|. 630 for (it = adds.changes().begin(); it != adds.changes().end(); ++it) 631 ExpectBrowserNodeUnknown(it->id); 632 633 adds.ApplyPendingChanges(change_processor()); 634 635 // Make sure the bookmark model received all of the nodes in |adds|. 636 for (it = adds.changes().begin(); it != adds.changes().end(); ++it) 637 ExpectBrowserNodeMatching(&trans, it->id); 638 ExpectModelMatch(&trans); 639 640 // We have to do the moves before the deletions, but FakeServerChange will 641 // put the deletion at the front of the changelist. 642 FakeServerChange ops(&trans); 643 ops.ModifyPosition(f6, other_bookmarks_id(), 0); 644 ops.ModifyPosition(u3, other_bookmarks_id(), f1); // Prev == f1 is OK here. 645 ops.ModifyPosition(f2, other_bookmarks_id(), u7); 646 ops.ModifyPosition(u7, f2, 0); 647 ops.ModifyPosition(u4, other_bookmarks_id(), f2); 648 ops.ModifyPosition(u5, f6, 0); 649 ops.Delete(f1); 650 651 ops.ApplyPendingChanges(change_processor()); 652 653 ExpectModelMatch(&trans); 654} 655 656// Simulate a server change record containing a valid but non-canonical URL. 657TEST_F(ProfileSyncServiceTest, ServerChangeWithNonCanonicalURL) { 658 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); 659 StartSyncService(); 660 661 { 662 sync_api::WriteTransaction trans(backend()->GetUserShareHandle()); 663 664 FakeServerChange adds(&trans); 665 std::string url("http://dev.chromium.org"); 666 EXPECT_NE(GURL(url).spec(), url); 667 adds.AddURL(L"u1", url, other_bookmarks_id(), 0); 668 669 adds.ApplyPendingChanges(change_processor()); 670 671 EXPECT_TRUE(model_->other_node()->GetChildCount() == 1); 672 ExpectModelMatch(&trans); 673 } 674 675 // Now reboot the sync service, forcing a merge step. 676 StopSyncService(SAVE_TO_STORAGE); 677 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE); 678 StartSyncService(); 679 680 // There should still be just the one bookmark. 681 EXPECT_TRUE(model_->other_node()->GetChildCount() == 1); 682 ExpectModelMatch(); 683} 684 685// Simulate a server change record containing an invalid URL (per GURL). 686// TODO(ncarter): Disabled due to crashes. Fix bug 1677563. 687TEST_F(ProfileSyncServiceTest, DISABLED_ServerChangeWithInvalidURL) { 688 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); 689 StartSyncService(); 690 691 int child_count = 0; 692 { 693 sync_api::WriteTransaction trans(backend()->GetUserShareHandle()); 694 695 FakeServerChange adds(&trans); 696 std::string url("x"); 697 EXPECT_FALSE(GURL(url).is_valid()); 698 adds.AddURL(L"u1", url, other_bookmarks_id(), 0); 699 700 adds.ApplyPendingChanges(change_processor()); 701 702 // We're lenient about what should happen -- the model could wind up with 703 // the node or without it; but things should be consistent, and we 704 // shouldn't crash. 705 child_count = model_->other_node()->GetChildCount(); 706 EXPECT_TRUE(child_count == 0 || child_count == 1); 707 ExpectModelMatch(&trans); 708 } 709 710 // Now reboot the sync service, forcing a merge step. 711 StopSyncService(SAVE_TO_STORAGE); 712 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE); 713 StartSyncService(); 714 715 // Things ought not to have changed. 716 EXPECT_EQ(model_->other_node()->GetChildCount(), child_count); 717 ExpectModelMatch(); 718} 719 720 721// Test strings that might pose a problem if the titles ever became used as 722// file names in the sync backend. 723TEST_F(ProfileSyncServiceTest, CornerCaseNames) { 724 // TODO(ncarter): Bug 1570238 explains the failure of this test. 725 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 726 StartSyncService(); 727 728 const wchar_t* names[] = { 729 // The empty string. 730 L"", 731 // Illegal Windows filenames. 732 L"CON", L"PRN", L"AUX", L"NUL", L"COM1", L"COM2", L"COM3", L"COM4", 733 L"COM5", L"COM6", L"COM7", L"COM8", L"COM9", L"LPT1", L"LPT2", L"LPT3", 734 L"LPT4", L"LPT5", L"LPT6", L"LPT7", L"LPT8", L"LPT9", 735 // Current/parent directory markers. 736 L".", L"..", L"...", 737 // Files created automatically by the Windows shell. 738 L"Thumbs.db", L".DS_Store", 739 // Names including Win32-illegal characters, and path separators. 740 L"foo/bar", L"foo\\bar", L"foo?bar", L"foo:bar", L"foo|bar", L"foo\"bar", 741 L"foo'bar", L"foo<bar", L"foo>bar", L"foo%bar", L"foo*bar", L"foo]bar", 742 L"foo[bar", 743 }; 744 // Create both folders and bookmarks using each name. 745 GURL url("http://www.doublemint.com"); 746 for (size_t i = 0; i < arraysize(names); ++i) { 747 model_->AddGroup(model_->other_node(), 0, names[i]); 748 model_->AddURL(model_->other_node(), 0, names[i], url); 749 } 750 751 // Verify that the browser model matches the sync model. 752 EXPECT_TRUE(model_->other_node()->GetChildCount() == 2*arraysize(names)); 753 ExpectModelMatch(); 754} 755 756// Stress the internal representation of position by sparse numbers. We want 757// to repeatedly bisect the range of available positions, to force the 758// syncer code to renumber its ranges. Pick a number big enough so that it 759// would exhaust 32bits of room between items a couple of times. 760TEST_F(ProfileSyncServiceTest, RepeatedMiddleInsertion) { 761 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 762 StartSyncService(); 763 764 static const int kTimesToInsert = 256; 765 766 // Create two book-end nodes to insert between. 767 model_->AddGroup(model_->other_node(), 0, L"Alpha"); 768 model_->AddGroup(model_->other_node(), 1, L"Omega"); 769 int count = 2; 770 771 // Test insertion in first half of range by repeatedly inserting in second 772 // position. 773 for (int i = 0; i < kTimesToInsert; ++i) { 774 std::wstring title = std::wstring(L"Pre-insertion ") + IntToWString(i); 775 model_->AddGroup(model_->other_node(), 1, title); 776 count++; 777 } 778 779 // Test insertion in second half of range by repeatedly inserting in 780 // second-to-last position. 781 for (int i = 0; i < kTimesToInsert; ++i) { 782 std::wstring title = std::wstring(L"Post-insertion ") + IntToWString(i); 783 model_->AddGroup(model_->other_node(), count - 1, title); 784 count++; 785 } 786 787 // Verify that the browser model matches the sync model. 788 EXPECT_EQ(model_->other_node()->GetChildCount(), count); 789 ExpectModelMatch(); 790} 791 792// Introduce a consistency violation into the model, and see that it 793// puts itself into a lame, error state. 794TEST_F(ProfileSyncServiceTest, UnrecoverableErrorSuspendsService) { 795 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 796 StartSyncService(); 797 798 // Synchronization should be up and running at this point. 799 EXPECT_TRUE(service_->ShouldPushChanges()); 800 801 // Add a node which will be the target of the consistency violation. 802 const BookmarkNode* node = 803 model_->AddGroup(model_->other_node(), 0, L"node"); 804 ExpectSyncerNodeMatching(node); 805 806 // Now destroy the syncer node as if we were the ProfileSyncService without 807 // updating the ProfileSyncService state. This should introduce 808 // inconsistency between the two models. 809 { 810 sync_api::WriteTransaction trans(service_->backend_->GetUserShareHandle()); 811 sync_api::WriteNode sync_node(&trans); 812 EXPECT_TRUE(associator()->InitSyncNodeFromChromeId(node->id(), 813 &sync_node)); 814 sync_node.Remove(); 815 } 816 // The models don't match at this point, but the ProfileSyncService 817 // doesn't know it yet. 818 ExpectSyncerNodeKnown(node); 819 EXPECT_TRUE(service_->ShouldPushChanges()); 820 821 // Add a child to the inconsistent node. This should cause detection of the 822 // problem and the syncer should stop processing changes. 823 model_->AddGroup(node, 0, L"nested"); 824 EXPECT_FALSE(service_->ShouldPushChanges()); 825 826 // Try to add a node under a totally different parent. This should also 827 // fail -- the ProfileSyncService should stop processing changes after 828 // encountering a consistency violation. 829 model_->AddGroup(model_->GetBookmarkBarNode(), 0, L"unrelated"); 830 EXPECT_FALSE(service_->ShouldPushChanges()); 831 832 // TODO(ncarter): We ought to test the ProfileSyncService state machine 833 // directly here once that's formalized and exposed. 834} 835 836// See what happens if we run model association when there are two exact URL 837// duplicate bookmarks. The BookmarkModelAssociator should not fall over when 838// this happens. 839TEST_F(ProfileSyncServiceTest, MergeDuplicates) { 840 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); 841 StartSyncService(); 842 843 model_->AddURL(model_->other_node(), 0, L"Dup", GURL("http://dup.com/")); 844 model_->AddURL(model_->other_node(), 0, L"Dup", GURL("http://dup.com/")); 845 846 EXPECT_EQ(2, model_->other_node()->GetChildCount()); 847 848 // Restart the sync service to trigger model association. 849 StopSyncService(SAVE_TO_STORAGE); 850 StartSyncService(); 851 852 EXPECT_EQ(2, model_->other_node()->GetChildCount()); 853 ExpectModelMatch(); 854} 855 856struct TestData { 857 const wchar_t* title; 858 const char* url; 859}; 860 861// TODO(ncarter): Integrate the existing TestNode/PopulateNodeFromString code 862// in the bookmark model unittest, to make it simpler to set up test data 863// here (and reduce the amount of duplication among tests), and to reduce the 864// duplication. 865class ProfileSyncServiceTestWithData : public ProfileSyncServiceTest { 866 protected: 867 // Populates or compares children of the given bookmark node from/with the 868 // given test data array with the given size. 869 void PopulateFromTestData(const BookmarkNode* node, 870 const TestData* data, 871 int size); 872 void CompareWithTestData(const BookmarkNode* node, 873 const TestData* data, 874 int size); 875 876 void ExpectBookmarkModelMatchesTestData(); 877 void WriteTestDataToBookmarkModel(); 878}; 879 880namespace { 881 882// Constants for bookmark model that looks like: 883// |-- Bookmark bar 884// | |-- u2, http://www.u2.com/ 885// | |-- f1 886// | | |-- f1u4, http://www.f1u4.com/ 887// | | |-- f1u2, http://www.f1u2.com/ 888// | | |-- f1u3, http://www.f1u3.com/ 889// | | +-- f1u1, http://www.f1u1.com/ 890// | |-- u1, http://www.u1.com/ 891// | +-- f2 892// | |-- f2u2, http://www.f2u2.com/ 893// | |-- f2u4, http://www.f2u4.com/ 894// | |-- f2u3, http://www.f2u3.com/ 895// | +-- f2u1, http://www.f2u1.com/ 896// +-- Other bookmarks 897// |-- f3 898// | |-- f3u4, http://www.f3u4.com/ 899// | |-- f3u2, http://www.f3u2.com/ 900// | |-- f3u3, http://www.f3u3.com/ 901// | +-- f3u1, http://www.f3u1.com/ 902// |-- u4, http://www.u4.com/ 903// |-- u3, http://www.u3.com/ 904// --- f4 905// | |-- f4u1, http://www.f4u1.com/ 906// | |-- f4u2, http://www.f4u2.com/ 907// | |-- f4u3, http://www.f4u3.com/ 908// | +-- f4u4, http://www.f4u4.com/ 909// |-- dup 910// | +-- dupu1, http://www.dupu1.com/ 911// +-- dup 912// +-- dupu2, http://www.dupu1.com/ 913// 914static TestData kBookmarkBarChildren[] = { 915 { L"u2", "http://www.u2.com/" }, 916 { L"f1", NULL }, 917 { L"u1", "http://www.u1.com/" }, 918 { L"f2", NULL }, 919}; 920static TestData kF1Children[] = { 921 { L"f1u4", "http://www.f1u4.com/" }, 922 { L"f1u2", "http://www.f1u2.com/" }, 923 { L"f1u3", "http://www.f1u3.com/" }, 924 { L"f1u1", "http://www.f1u1.com/" }, 925}; 926static TestData kF2Children[] = { 927 { L"f2u2", "http://www.f2u2.com/" }, 928 { L"f2u4", "http://www.f2u4.com/" }, 929 { L"f2u3", "http://www.f2u3.com/" }, 930 { L"f2u1", "http://www.f2u1.com/" }, 931}; 932 933static TestData kOtherBookmarkChildren[] = { 934 { L"f3", NULL }, 935 { L"u4", "http://www.u4.com/" }, 936 { L"u3", "http://www.u3.com/" }, 937 { L"f4", NULL }, 938 { L"dup", NULL }, 939 { L"dup", NULL }, 940}; 941static TestData kF3Children[] = { 942 { L"f3u4", "http://www.f3u4.com/" }, 943 { L"f3u2", "http://www.f3u2.com/" }, 944 { L"f3u3", "http://www.f3u3.com/" }, 945 { L"f3u1", "http://www.f3u1.com/" }, 946}; 947static TestData kF4Children[] = { 948 { L"f4u1", "http://www.f4u1.com/" }, 949 { L"f4u2", "http://www.f4u2.com/" }, 950 { L"f4u3", "http://www.f4u3.com/" }, 951 { L"f4u4", "http://www.f4u4.com/" }, 952}; 953static TestData kDup1Children[] = { 954 { L"dupu1", "http://www.dupu1.com/" }, 955}; 956static TestData kDup2Children[] = { 957 { L"dupu2", "http://www.dupu2.com/" }, 958}; 959 960} // anonymous namespace. 961 962void ProfileSyncServiceTestWithData::PopulateFromTestData( 963 const BookmarkNode* node, const TestData* data, int size) { 964 DCHECK(node); 965 DCHECK(data); 966 DCHECK(node->is_folder()); 967 for (int i = 0; i < size; ++i) { 968 const TestData& item = data[i]; 969 if (item.url) { 970 model_->AddURL(node, i, item.title, GURL(item.url)); 971 } else { 972 model_->AddGroup(node, i, item.title); 973 } 974 } 975} 976 977void ProfileSyncServiceTestWithData::CompareWithTestData( 978 const BookmarkNode* node, const TestData* data, int size) { 979 DCHECK(node); 980 DCHECK(data); 981 DCHECK(node->is_folder()); 982 for (int i = 0; i < size; ++i) { 983 const BookmarkNode* child_node = node->GetChild(i); 984 const TestData& item = data[i]; 985 EXPECT_TRUE(child_node->GetTitle() == item.title); 986 if (item.url) { 987 EXPECT_FALSE(child_node->is_folder()); 988 EXPECT_TRUE(child_node->is_url()); 989 EXPECT_TRUE(child_node->GetURL() == GURL(item.url)); 990 } else { 991 EXPECT_TRUE(child_node->is_folder()); 992 EXPECT_FALSE(child_node->is_url()); 993 } 994 } 995} 996 997// TODO(munjal): We should implement some way of generating random data and can 998// use the same seed to generate the same sequence. 999void ProfileSyncServiceTestWithData::WriteTestDataToBookmarkModel() { 1000 const BookmarkNode* bookmarks_bar_node = model_->GetBookmarkBarNode(); 1001 PopulateFromTestData(bookmarks_bar_node, 1002 kBookmarkBarChildren, 1003 arraysize(kBookmarkBarChildren)); 1004 1005 ASSERT_GE(bookmarks_bar_node->GetChildCount(), 4); 1006 const BookmarkNode* f1_node = bookmarks_bar_node->GetChild(1); 1007 PopulateFromTestData(f1_node, kF1Children, arraysize(kF1Children)); 1008 const BookmarkNode* f2_node = bookmarks_bar_node->GetChild(3); 1009 PopulateFromTestData(f2_node, kF2Children, arraysize(kF2Children)); 1010 1011 const BookmarkNode* other_bookmarks_node = model_->other_node(); 1012 PopulateFromTestData(other_bookmarks_node, 1013 kOtherBookmarkChildren, 1014 arraysize(kOtherBookmarkChildren)); 1015 1016 ASSERT_GE(other_bookmarks_node->GetChildCount(), 6); 1017 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0); 1018 PopulateFromTestData(f3_node, kF3Children, arraysize(kF3Children)); 1019 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3); 1020 PopulateFromTestData(f4_node, kF4Children, arraysize(kF4Children)); 1021 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4); 1022 PopulateFromTestData(dup_node, kDup1Children, arraysize(kDup1Children)); 1023 dup_node = other_bookmarks_node->GetChild(5); 1024 PopulateFromTestData(dup_node, kDup2Children, arraysize(kDup2Children)); 1025 1026 ExpectBookmarkModelMatchesTestData(); 1027} 1028 1029void ProfileSyncServiceTestWithData::ExpectBookmarkModelMatchesTestData() { 1030 const BookmarkNode* bookmark_bar_node = model_->GetBookmarkBarNode(); 1031 CompareWithTestData(bookmark_bar_node, 1032 kBookmarkBarChildren, 1033 arraysize(kBookmarkBarChildren)); 1034 1035 ASSERT_GE(bookmark_bar_node->GetChildCount(), 4); 1036 const BookmarkNode* f1_node = bookmark_bar_node->GetChild(1); 1037 CompareWithTestData(f1_node, kF1Children, arraysize(kF1Children)); 1038 const BookmarkNode* f2_node = bookmark_bar_node->GetChild(3); 1039 CompareWithTestData(f2_node, kF2Children, arraysize(kF2Children)); 1040 1041 const BookmarkNode* other_bookmarks_node = model_->other_node(); 1042 CompareWithTestData(other_bookmarks_node, 1043 kOtherBookmarkChildren, 1044 arraysize(kOtherBookmarkChildren)); 1045 1046 ASSERT_GE(other_bookmarks_node->GetChildCount(), 6); 1047 const BookmarkNode* f3_node = other_bookmarks_node->GetChild(0); 1048 CompareWithTestData(f3_node, kF3Children, arraysize(kF3Children)); 1049 const BookmarkNode* f4_node = other_bookmarks_node->GetChild(3); 1050 CompareWithTestData(f4_node, kF4Children, arraysize(kF4Children)); 1051 const BookmarkNode* dup_node = other_bookmarks_node->GetChild(4); 1052 CompareWithTestData(dup_node, kDup1Children, arraysize(kDup1Children)); 1053 dup_node = other_bookmarks_node->GetChild(5); 1054 CompareWithTestData(dup_node, kDup2Children, arraysize(kDup2Children)); 1055} 1056 1057// Tests persistence of the profile sync service by destroying the 1058// profile sync service and then reloading it from disk. 1059TEST_F(ProfileSyncServiceTestWithData, Persistence) { 1060 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); 1061 StartSyncService(); 1062 1063 WriteTestDataToBookmarkModel(); 1064 1065 ExpectModelMatch(); 1066 1067 // Force both models to discard their data and reload from disk. This 1068 // simulates what would happen if the browser were to shutdown normally, 1069 // and then relaunch. 1070 StopSyncService(SAVE_TO_STORAGE); 1071 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE); 1072 StartSyncService(); 1073 1074 ExpectBookmarkModelMatchesTestData(); 1075 1076 // With the BookmarkModel contents verified, ExpectModelMatch will 1077 // verify the contents of the sync model. 1078 ExpectModelMatch(); 1079} 1080 1081// Tests the merge case when the BookmarkModel is non-empty but the 1082// sync model is empty. This corresponds to uploading browser 1083// bookmarks to an initially empty, new account. 1084TEST_F(ProfileSyncServiceTestWithData, MergeWithEmptySyncModel) { 1085 // Don't start the sync service until we've populated the bookmark model. 1086 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); 1087 1088 WriteTestDataToBookmarkModel(); 1089 1090 // Restart the profile sync service. This should trigger a merge step 1091 // during initialization -- we expect the browser bookmarks to be written 1092 // to the sync service during this call. 1093 StartSyncService(); 1094 1095 // Verify that the bookmark model hasn't changed, and that the sync model 1096 // matches it exactly. 1097 ExpectBookmarkModelMatchesTestData(); 1098 ExpectModelMatch(); 1099} 1100 1101// Tests the merge case when the BookmarkModel is empty but the sync model is 1102// non-empty. This corresponds (somewhat) to a clean install of the browser, 1103// with no bookmarks, connecting to a sync account that has some bookmarks. 1104TEST_F(ProfileSyncServiceTestWithData, MergeWithEmptyBookmarkModel) { 1105 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 1106 StartSyncService(); 1107 1108 WriteTestDataToBookmarkModel(); 1109 1110 ExpectModelMatch(); 1111 1112 // Force the sync service to shut down and write itself to disk. 1113 StopSyncService(SAVE_TO_STORAGE); 1114 1115 // Blow away the bookmark model -- it should be empty afterwards. 1116 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 1117 EXPECT_EQ(model_->GetBookmarkBarNode()->GetChildCount(), 0); 1118 EXPECT_EQ(model_->other_node()->GetChildCount(), 0); 1119 1120 // Now restart the sync service. Starting it should populate the bookmark 1121 // model -- test for consistency. 1122 StartSyncService(); 1123 ExpectBookmarkModelMatchesTestData(); 1124 ExpectModelMatch(); 1125} 1126 1127// Tests the merge cases when both the models are expected to be identical 1128// after the merge. 1129TEST_F(ProfileSyncServiceTestWithData, MergeExpectedIdenticalModels) { 1130 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); 1131 StartSyncService(); 1132 WriteTestDataToBookmarkModel(); 1133 ExpectModelMatch(); 1134 StopSyncService(SAVE_TO_STORAGE); 1135 1136 // At this point both the bookmark model and the server should have the 1137 // exact same data and it should match the test data. 1138 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE); 1139 StartSyncService(); 1140 ExpectBookmarkModelMatchesTestData(); 1141 ExpectModelMatch(); 1142 StopSyncService(SAVE_TO_STORAGE); 1143 1144 // Now reorder some bookmarks in the bookmark model and then merge. Make 1145 // sure we get the order of the server after merge. 1146 LoadBookmarkModel(LOAD_FROM_STORAGE, DONT_SAVE_TO_STORAGE); 1147 ExpectBookmarkModelMatchesTestData(); 1148 const BookmarkNode* bookmark_bar = model_->GetBookmarkBarNode(); 1149 ASSERT_TRUE(bookmark_bar); 1150 ASSERT_GT(bookmark_bar->GetChildCount(), 1); 1151 model_->Move(bookmark_bar->GetChild(0), bookmark_bar, 1); 1152 StartSyncService(); 1153 ExpectModelMatch(); 1154 ExpectBookmarkModelMatchesTestData(); 1155} 1156 1157// Tests the merge cases when both the models are expected to be identical 1158// after the merge. 1159TEST_F(ProfileSyncServiceTestWithData, MergeModelsWithSomeExtras) { 1160 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 1161 WriteTestDataToBookmarkModel(); 1162 ExpectBookmarkModelMatchesTestData(); 1163 1164 // Remove some nodes and reorder some nodes. 1165 const BookmarkNode* bookmark_bar_node = model_->GetBookmarkBarNode(); 1166 int remove_index = 2; 1167 ASSERT_GT(bookmark_bar_node->GetChildCount(), remove_index); 1168 const BookmarkNode* child_node = bookmark_bar_node->GetChild(remove_index); 1169 ASSERT_TRUE(child_node); 1170 ASSERT_TRUE(child_node->is_url()); 1171 model_->Remove(bookmark_bar_node, remove_index); 1172 ASSERT_GT(bookmark_bar_node->GetChildCount(), remove_index); 1173 child_node = bookmark_bar_node->GetChild(remove_index); 1174 ASSERT_TRUE(child_node); 1175 ASSERT_TRUE(child_node->is_folder()); 1176 model_->Remove(bookmark_bar_node, remove_index); 1177 1178 const BookmarkNode* other_node = model_->other_node(); 1179 ASSERT_GE(other_node->GetChildCount(), 1); 1180 const BookmarkNode* f3_node = other_node->GetChild(0); 1181 ASSERT_TRUE(f3_node); 1182 ASSERT_TRUE(f3_node->is_folder()); 1183 remove_index = 2; 1184 ASSERT_GT(f3_node->GetChildCount(), remove_index); 1185 model_->Remove(f3_node, remove_index); 1186 ASSERT_GT(f3_node->GetChildCount(), remove_index); 1187 model_->Remove(f3_node, remove_index); 1188 1189 StartSyncService(); 1190 ExpectModelMatch(); 1191 StopSyncService(SAVE_TO_STORAGE); 1192 1193 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 1194 WriteTestDataToBookmarkModel(); 1195 ExpectBookmarkModelMatchesTestData(); 1196 1197 // Remove some nodes and reorder some nodes. 1198 bookmark_bar_node = model_->GetBookmarkBarNode(); 1199 remove_index = 0; 1200 ASSERT_GT(bookmark_bar_node->GetChildCount(), remove_index); 1201 child_node = bookmark_bar_node->GetChild(remove_index); 1202 ASSERT_TRUE(child_node); 1203 ASSERT_TRUE(child_node->is_url()); 1204 model_->Remove(bookmark_bar_node, remove_index); 1205 ASSERT_GT(bookmark_bar_node->GetChildCount(), remove_index); 1206 child_node = bookmark_bar_node->GetChild(remove_index); 1207 ASSERT_TRUE(child_node); 1208 ASSERT_TRUE(child_node->is_folder()); 1209 model_->Remove(bookmark_bar_node, remove_index); 1210 1211 ASSERT_GE(bookmark_bar_node->GetChildCount(), 2); 1212 model_->Move(bookmark_bar_node->GetChild(0), bookmark_bar_node, 1); 1213 1214 other_node = model_->other_node(); 1215 ASSERT_GE(other_node->GetChildCount(), 1); 1216 f3_node = other_node->GetChild(0); 1217 ASSERT_TRUE(f3_node); 1218 ASSERT_TRUE(f3_node->is_folder()); 1219 remove_index = 0; 1220 ASSERT_GT(f3_node->GetChildCount(), remove_index); 1221 model_->Remove(f3_node, remove_index); 1222 ASSERT_GT(f3_node->GetChildCount(), remove_index); 1223 model_->Remove(f3_node, remove_index); 1224 1225 ASSERT_GE(other_node->GetChildCount(), 4); 1226 model_->Move(other_node->GetChild(0), other_node, 1); 1227 model_->Move(other_node->GetChild(2), other_node, 3); 1228 1229 StartSyncService(); 1230 ExpectModelMatch(); 1231 1232 // After the merge, the model should match the test data. 1233 ExpectBookmarkModelMatchesTestData(); 1234} 1235 1236// Tests that when persisted model associations are used, things work fine. 1237TEST_F(ProfileSyncServiceTestWithData, ModelAssociationPersistence) { 1238 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 1239 WriteTestDataToBookmarkModel(); 1240 StartSyncService(); 1241 ExpectModelMatch(); 1242 // Force the sync service to shut down and write itself to disk. 1243 StopSyncService(SAVE_TO_STORAGE); 1244 // Now restart the sync service. This time it should use the persistent 1245 // associations. 1246 StartSyncService(); 1247 ExpectModelMatch(); 1248} 1249 1250// Tests that when persisted model associations are used, things work fine. 1251TEST_F(ProfileSyncServiceTestWithData, ModelAssociationInvalidPersistence) { 1252 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 1253 WriteTestDataToBookmarkModel(); 1254 StartSyncService(); 1255 ExpectModelMatch(); 1256 // Force the sync service to shut down and write itself to disk. 1257 StopSyncService(SAVE_TO_STORAGE); 1258 // Change the bookmark model before restarting sync service to simulate 1259 // the situation where bookmark model is different from sync model and 1260 // make sure model associator correctly rebuilds associations. 1261 const BookmarkNode* bookmark_bar_node = model_->GetBookmarkBarNode(); 1262 model_->AddURL(bookmark_bar_node, 0, L"xtra", GURL("http://www.xtra.com")); 1263 // Now restart the sync service. This time it will try to use the persistent 1264 // associations and realize that they are invalid and hence will rebuild 1265 // associations. 1266 StartSyncService(); 1267 ExpectModelMatch(); 1268} 1269 1270TEST_F(ProfileSyncServiceTestWithData, SortChildren) { 1271 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 1272 StartSyncService(); 1273 1274 // Write test data to bookmark model and verify that the models match. 1275 WriteTestDataToBookmarkModel(); 1276 const BookmarkNode* folder_added = model_->other_node()->GetChild(0); 1277 ASSERT_TRUE(folder_added); 1278 ASSERT_TRUE(folder_added->is_folder()); 1279 1280 ExpectModelMatch(); 1281 1282 // Sort the other-bookmarks children and expect that hte models match. 1283 model_->SortChildren(folder_added); 1284 ExpectModelMatch(); 1285} 1286 1287// See what happens if we enable sync but then delete the "Sync Data" 1288// folder. 1289TEST_F(ProfileSyncServiceTestWithData, RecoverAfterDeletingSyncDataDirectory) { 1290 LoadBookmarkModel(DELETE_EXISTING_STORAGE, SAVE_TO_STORAGE); 1291 StartSyncService(); 1292 1293 WriteTestDataToBookmarkModel(); 1294 1295 // While the service is running. 1296 FilePath sync_data_directory = backend()->sync_data_folder_path(); 1297 1298 // Simulate a normal shutdown for the sync service (don't disable it for 1299 // the user, which would reset the preferences and delete the sync data 1300 // directory). 1301 StopSyncService(SAVE_TO_STORAGE); 1302 1303 // Now pretend that the user has deleted this directory from the disk. 1304 file_util::Delete(sync_data_directory, true); 1305 1306 // Restart the sync service. 1307 StartSyncService(); 1308 1309 // Make sure we're back in sync. In real life, the user would need 1310 // to reauthenticate before this happens, but in the test, authentication 1311 // is sidestepped. 1312 ExpectBookmarkModelMatchesTestData(); 1313 ExpectModelMatch(); 1314} 1315 1316// Make sure that things still work if sync is not enabled, but some old sync 1317// databases are lingering in the "Sync Data" folder. 1318TEST_F(ProfileSyncServiceTestWithData, TestStartupWithOldSyncData) { 1319 const char* nonsense1 = "reginald"; 1320 const char* nonsense2 = "beartato"; 1321 const char* nonsense3 = "harrison"; 1322 FilePath temp_directory = profile_->GetPath().AppendASCII("Sync Data"); 1323 FilePath sync_file1 = 1324 temp_directory.AppendASCII("BookmarkSyncSettings.sqlite3"); 1325 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); 1326 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); 1327 file_util::CreateDirectory(temp_directory); 1328 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1)); 1329 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2)); 1330 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3)); 1331 1332 LoadBookmarkModel(LOAD_FROM_STORAGE, SAVE_TO_STORAGE); 1333 if (!service_.get()) { 1334 service_.reset( 1335 new TestProfileSyncService(&factory_, profile_.get(), 1336 false, true, NULL)); 1337 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); 1338 1339 model_associator_ = new TestBookmarkModelAssociator(service_.get(), 1340 service_.get()); 1341 change_processor_ = new BookmarkChangeProcessor(model_associator_, 1342 service_.get()); 1343 EXPECT_CALL(factory_, CreateBookmarkSyncComponents(_, _)). 1344 WillOnce(Return(ProfileSyncFactory::SyncComponents( 1345 model_associator_, change_processor_))); 1346 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)). 1347 WillOnce(ReturnNewDataTypeManager()); 1348 1349 service_->RegisterDataTypeController( 1350 new browser_sync::BookmarkDataTypeController(&factory_, 1351 profile_.get(), 1352 service_.get())); 1353 1354 service_->Initialize(); // will call disableForUser because sync setup 1355 // hasn't been completed. 1356 } 1357 1358 ASSERT_FALSE(service_->backend()); 1359 ASSERT_FALSE(service_->HasSyncSetupCompleted()); 1360 1361 // This will actually start up the sync service. 1362 service_->EnableForUser(NULL); 1363 syncable::ModelTypeSet set; 1364 set.insert(syncable::BOOKMARKS); 1365 service_->OnUserChoseDatatypes(false, set); 1366 1367 MessageLoop::current()->Run(); 1368 1369 // Stop the service so we can read the new Sync Data files that were created. 1370 service_.reset(); 1371 1372 // This file should have been deleted when the whole directory was nuked. 1373 ASSERT_FALSE(file_util::PathExists(sync_file3)); 1374 1375 // These two will still exist, but their texts should have changed. 1376 ASSERT_TRUE(file_util::PathExists(sync_file1)); 1377 std::string file1text; 1378 file_util::ReadFileToString(sync_file1, &file1text); 1379 ASSERT_FALSE(file1text.compare(nonsense1) == 0); 1380 1381 ASSERT_TRUE(file_util::PathExists(sync_file2)); 1382 std::string file2text; 1383 file_util::ReadFileToString(sync_file2, &file2text); 1384 ASSERT_FALSE(file2text.compare(nonsense2) == 0); 1385} 1386