| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // TODO(akalin): This file is basically just a unit test for | 5 // TODO(akalin): This file is basically just a unit test for |
| 6 // BookmarkChangeProcessor. Write unit tests for | 6 // BookmarkChangeProcessor. Write unit tests for |
| 7 // BookmarkModelAssociator separately. | 7 // BookmarkModelAssociator separately. |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <queue> | 10 #include <queue> |
| (...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 node.SetIsFolder(true); | 420 node.SetIsFolder(true); |
| 421 node.GetMutableEntryForTest()->Put( | 421 node.GetMutableEntryForTest()->Put( |
| 422 syncer::syncable::UNIQUE_SERVER_TAG, permanent_tags[i]); | 422 syncer::syncable::UNIQUE_SERVER_TAG, permanent_tags[i]); |
| 423 node.SetTitle(UTF8ToWide(permanent_tags[i])); | 423 node.SetTitle(UTF8ToWide(permanent_tags[i])); |
| 424 node.SetExternalId(0); | 424 node.SetExternalId(0); |
| 425 last_child_id = node.GetId(); | 425 last_child_id = node.GetId(); |
| 426 } | 426 } |
| 427 return true; | 427 return true; |
| 428 } | 428 } |
| 429 | 429 |
| 430 void StartSync() { | 430 bool AssociateModels() { |
| 431 test_user_share_.Reload(); | 431 DCHECK(!model_associator_); |
| 432 | |
| 433 ASSERT_TRUE(CreatePermanentBookmarkNodes()); | |
| 434 | 432 |
| 435 // Set up model associator. | 433 // Set up model associator. |
| 436 model_associator_.reset(new BookmarkModelAssociator( | 434 model_associator_.reset(new BookmarkModelAssociator( |
| 437 BookmarkModelFactory::GetForProfile(&profile_), | 435 BookmarkModelFactory::GetForProfile(&profile_), |
| 438 &profile_, | 436 &profile_, |
| 439 test_user_share_.user_share(), | 437 test_user_share_.user_share(), |
| 440 &mock_error_handler_, | 438 &mock_error_handler_, |
| 441 kExpectMobileBookmarks)); | 439 kExpectMobileBookmarks)); |
| 442 | 440 |
| 443 local_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS); | 441 local_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS); |
| 444 syncer_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS); | 442 syncer_merge_result_ = syncer::SyncMergeResult(syncer::BOOKMARKS); |
| 445 int local_count_before = model_->root_node()->GetTotalNodeCount(); | 443 int local_count_before = model_->root_node()->GetTotalNodeCount(); |
| 446 int syncer_count_before = GetSyncBookmarkCount(); | 444 int syncer_count_before = GetSyncBookmarkCount(); |
| 447 | 445 |
| 448 syncer::SyncError error = model_associator_->AssociateModels( | 446 syncer::SyncError error = model_associator_->AssociateModels( |
| 449 &local_merge_result_, | 447 &local_merge_result_, |
| 450 &syncer_merge_result_); | 448 &syncer_merge_result_); |
| 451 EXPECT_FALSE(error.IsSet()); | 449 if (error.IsSet()) |
| 450 return false; |
| 451 |
| 452 base::MessageLoop::current()->RunUntilIdle(); |
| 452 | 453 |
| 453 // Verify the merge results were calculated properly. | 454 // Verify the merge results were calculated properly. |
| 454 EXPECT_EQ(local_count_before, | 455 EXPECT_EQ(local_count_before, |
| 455 local_merge_result_.num_items_before_association()); | 456 local_merge_result_.num_items_before_association()); |
| 456 EXPECT_EQ(syncer_count_before, | 457 EXPECT_EQ(syncer_count_before, |
| 457 syncer_merge_result_.num_items_before_association()); | 458 syncer_merge_result_.num_items_before_association()); |
| 458 EXPECT_EQ(local_merge_result_.num_items_after_association(), | 459 EXPECT_EQ(local_merge_result_.num_items_after_association(), |
| 459 local_merge_result_.num_items_before_association() + | 460 local_merge_result_.num_items_before_association() + |
| 460 local_merge_result_.num_items_added() - | 461 local_merge_result_.num_items_added() - |
| 461 local_merge_result_.num_items_deleted()); | 462 local_merge_result_.num_items_deleted()); |
| 462 EXPECT_EQ(syncer_merge_result_.num_items_after_association(), | 463 EXPECT_EQ(syncer_merge_result_.num_items_after_association(), |
| 463 syncer_merge_result_.num_items_before_association() + | 464 syncer_merge_result_.num_items_before_association() + |
| 464 syncer_merge_result_.num_items_added() - | 465 syncer_merge_result_.num_items_added() - |
| 465 syncer_merge_result_.num_items_deleted()); | 466 syncer_merge_result_.num_items_deleted()); |
| 466 EXPECT_EQ(model_->root_node()->GetTotalNodeCount(), | 467 EXPECT_EQ(model_->root_node()->GetTotalNodeCount(), |
| 467 local_merge_result_.num_items_after_association()); | 468 local_merge_result_.num_items_after_association()); |
| 468 EXPECT_EQ(GetSyncBookmarkCount(), | 469 EXPECT_EQ(GetSyncBookmarkCount(), |
| 469 syncer_merge_result_.num_items_after_association()); | 470 syncer_merge_result_.num_items_after_association()); |
| 471 return true; |
| 472 } |
| 470 | 473 |
| 471 base::MessageLoop::current()->RunUntilIdle(); | 474 void StartSync() { |
| 475 test_user_share_.Reload(); |
| 476 |
| 477 ASSERT_TRUE(CreatePermanentBookmarkNodes()); |
| 478 ASSERT_TRUE(AssociateModels()); |
| 472 | 479 |
| 473 // Set up change processor. | 480 // Set up change processor. |
| 474 change_processor_.reset( | 481 change_processor_.reset( |
| 475 new BookmarkChangeProcessor(model_associator_.get(), | 482 new BookmarkChangeProcessor(model_associator_.get(), |
| 476 &mock_error_handler_)); | 483 &mock_error_handler_)); |
| 477 change_processor_->Start(&profile_, test_user_share_.user_share()); | 484 change_processor_->Start(&profile_, test_user_share_.user_share()); |
| 478 } | 485 } |
| 479 | 486 |
| 480 void StopSync() { | 487 void StopSync() { |
| 481 change_processor_.reset(); | 488 change_processor_.reset(); |
| (...skipping 1255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1737 StartSync(); | 1744 StartSync(); |
| 1738 | 1745 |
| 1739 WriteTestDataToBookmarkModel(); | 1746 WriteTestDataToBookmarkModel(); |
| 1740 | 1747 |
| 1741 StopSync(); | 1748 StopSync(); |
| 1742 | 1749 |
| 1743 // Nuke the sync DB and reload. | 1750 // Nuke the sync DB and reload. |
| 1744 TearDown(); | 1751 TearDown(); |
| 1745 SetUp(); | 1752 SetUp(); |
| 1746 | 1753 |
| 1747 StartSync(); | 1754 // First attempt fails due to a persistence error. |
| 1755 EXPECT_TRUE(CreatePermanentBookmarkNodes()); |
| 1756 EXPECT_FALSE(AssociateModels()); |
| 1757 |
| 1758 // Second attempt succeeds due to the previous error resetting the native |
| 1759 // transaction version. |
| 1760 model_associator_.reset(); |
| 1761 EXPECT_TRUE(CreatePermanentBookmarkNodes()); |
| 1762 EXPECT_TRUE(AssociateModels()); |
| 1748 | 1763 |
| 1749 // Make sure we're back in sync. In real life, the user would need | 1764 // Make sure we're back in sync. In real life, the user would need |
| 1750 // to reauthenticate before this happens, but in the test, authentication | 1765 // to reauthenticate before this happens, but in the test, authentication |
| 1751 // is sidestepped. | 1766 // is sidestepped. |
| 1752 ExpectBookmarkModelMatchesTestData(); | 1767 ExpectBookmarkModelMatchesTestData(); |
| 1753 ExpectModelMatch(); | 1768 ExpectModelMatch(); |
| 1754 } | 1769 } |
| 1755 | 1770 |
| 1756 // Verify that the bookmark model is updated about whether the | 1771 // Verify that the bookmark model is updated about whether the |
| 1757 // associator is currently running. | 1772 // associator is currently running. |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1911 EXPECT_EQ(initial_versions[model_->root_node()->id()] + 2, | 1926 EXPECT_EQ(initial_versions[model_->root_node()->id()] + 2, |
| 1912 new_versions[model_->root_node()->id()]); | 1927 new_versions[model_->root_node()->id()]); |
| 1913 EXPECT_LT(initial_versions[changed_bookmark->id()], | 1928 EXPECT_LT(initial_versions[changed_bookmark->id()], |
| 1914 new_versions[changed_bookmark->id()]); | 1929 new_versions[changed_bookmark->id()]); |
| 1915 initial_versions.erase(changed_bookmark->id()); | 1930 initial_versions.erase(changed_bookmark->id()); |
| 1916 ExpectTransactionVersionMatch(model_->bookmark_bar_node(), initial_versions); | 1931 ExpectTransactionVersionMatch(model_->bookmark_bar_node(), initial_versions); |
| 1917 ExpectTransactionVersionMatch(model_->other_node(), initial_versions); | 1932 ExpectTransactionVersionMatch(model_->other_node(), initial_versions); |
| 1918 ExpectTransactionVersionMatch(model_->mobile_node(), initial_versions); | 1933 ExpectTransactionVersionMatch(model_->mobile_node(), initial_versions); |
| 1919 } | 1934 } |
| 1920 | 1935 |
| 1936 // Test that sync persistence errors are detected and trigger a failed |
| 1937 // association. |
| 1938 TEST_F(ProfileSyncServiceBookmarkTestWithData, PersistenceError) { |
| 1939 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); |
| 1940 StartSync(); |
| 1941 WriteTestDataToBookmarkModel(); |
| 1942 base::MessageLoop::current()->RunUntilIdle(); |
| 1943 |
| 1944 BookmarkNodeVersionMap initial_versions; |
| 1945 |
| 1946 // Verify transaction versions in sync model and bookmark model (saved as |
| 1947 // transaction version of root node) are equal after |
| 1948 // WriteTestDataToBookmarkModel() created bookmarks. |
| 1949 { |
| 1950 syncer::ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 1951 EXPECT_GT(trans.GetModelVersion(syncer::BOOKMARKS), 0); |
| 1952 GetTransactionVersions(model_->root_node(), &initial_versions); |
| 1953 EXPECT_EQ(trans.GetModelVersion(syncer::BOOKMARKS), |
| 1954 initial_versions[model_->root_node()->id()]); |
| 1955 } |
| 1956 ExpectTransactionVersionMatch(model_->bookmark_bar_node(), |
| 1957 BookmarkNodeVersionMap()); |
| 1958 ExpectTransactionVersionMatch(model_->other_node(), |
| 1959 BookmarkNodeVersionMap()); |
| 1960 ExpectTransactionVersionMatch(model_->mobile_node(), |
| 1961 BookmarkNodeVersionMap()); |
| 1962 |
| 1963 // Now shut down sync and artificially increment the native model's version. |
| 1964 StopSync(); |
| 1965 int64 root_version = initial_versions[model_->root_node()->id()]; |
| 1966 model_->SetNodeMetaInfo(model_->root_node(), kBookmarkTransactionVersionKey, |
| 1967 base::Int64ToString(root_version+1)); |
| 1968 |
| 1969 // Upon association, bookmarks should fail to associate. |
| 1970 EXPECT_FALSE(AssociateModels()); |
| 1971 } |
| 1972 |
| 1921 } // namespace | 1973 } // namespace |
| 1922 | 1974 |
| 1923 } // namespace browser_sync | 1975 } // namespace browser_sync |
| OLD | NEW |