| 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 25 matching lines...) Expand all Loading... |
| 36 #include "components/sync_driver/data_type_error_handler_mock.h" | 36 #include "components/sync_driver/data_type_error_handler_mock.h" |
| 37 #include "content/public/test/test_browser_thread_bundle.h" | 37 #include "content/public/test/test_browser_thread_bundle.h" |
| 38 #include "sync/api/sync_error.h" | 38 #include "sync/api/sync_error.h" |
| 39 #include "sync/internal_api/public/change_record.h" | 39 #include "sync/internal_api/public/change_record.h" |
| 40 #include "sync/internal_api/public/read_node.h" | 40 #include "sync/internal_api/public/read_node.h" |
| 41 #include "sync/internal_api/public/read_transaction.h" | 41 #include "sync/internal_api/public/read_transaction.h" |
| 42 #include "sync/internal_api/public/test/test_user_share.h" | 42 #include "sync/internal_api/public/test/test_user_share.h" |
| 43 #include "sync/internal_api/public/write_node.h" | 43 #include "sync/internal_api/public/write_node.h" |
| 44 #include "sync/internal_api/public/write_transaction.h" | 44 #include "sync/internal_api/public/write_transaction.h" |
| 45 #include "sync/internal_api/syncapi_internal.h" | 45 #include "sync/internal_api/syncapi_internal.h" |
| 46 #include "sync/syncable/mutable_entry.h" | 46 #include "sync/syncable/mutable_entry.h" // TODO(tim): Remove. Bug 131130. |
| 47 #include "testing/gmock/include/gmock/gmock.h" | 47 #include "testing/gmock/include/gmock/gmock.h" |
| 48 #include "testing/gtest/include/gtest/gtest.h" | 48 #include "testing/gtest/include/gtest/gtest.h" |
| 49 | 49 |
| 50 namespace browser_sync { | 50 namespace browser_sync { |
| 51 | 51 |
| 52 using bookmarks::BookmarkModel; | 52 using bookmarks::BookmarkModel; |
| 53 using bookmarks::BookmarkNode; | 53 using bookmarks::BookmarkNode; |
| 54 using syncer::BaseNode; | 54 using syncer::BaseNode; |
| 55 using testing::_; | 55 using testing::_; |
| 56 using testing::InvokeWithoutArgs; | 56 using testing::InvokeWithoutArgs; |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 } | 367 } |
| 368 | 368 |
| 369 // Inserts a bookmark directly to the share. | 369 // Inserts a bookmark directly to the share. |
| 370 // Do not use this after model association is complete. | 370 // Do not use this after model association is complete. |
| 371 // | 371 // |
| 372 // This function differs from the AddURL() function declared elsewhere in this | 372 // This function differs from the AddURL() function declared elsewhere in this |
| 373 // file in that it only affects the sync model. It would be invalid to change | 373 // file in that it only affects the sync model. It would be invalid to change |
| 374 // the sync model directly after ModelAssociation. This function can be | 374 // the sync model directly after ModelAssociation. This function can be |
| 375 // invoked prior to model association to set up first-time sync model | 375 // invoked prior to model association to set up first-time sync model |
| 376 // association scenarios. | 376 // association scenarios. |
| 377 int64 AddBookmarkToShare(syncer::WriteTransaction* trans, | 377 int64 AddBookmarkToShare(syncer::WriteTransaction *trans, |
| 378 int64 parent_id, | 378 int64 parent_id, |
| 379 const std::string& title, | 379 std::string title) { |
| 380 const std::string& url) { | |
| 381 EXPECT_FALSE(model_associator_); | 380 EXPECT_FALSE(model_associator_); |
| 382 | 381 |
| 383 syncer::ReadNode parent(trans); | 382 syncer::ReadNode parent(trans); |
| 384 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id)); | 383 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id)); |
| 385 | 384 |
| 386 sync_pb::BookmarkSpecifics specifics; | 385 sync_pb::BookmarkSpecifics specifics; |
| 387 specifics.set_url(url); | 386 specifics.set_url("http://www.google.com/search?q=" + title); |
| 388 specifics.set_title(title); | 387 specifics.set_title(title); |
| 389 | 388 |
| 390 syncer::WriteNode node(trans); | 389 syncer::WriteNode node(trans); |
| 391 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL)); | 390 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL)); |
| 392 node.SetIsFolder(false); | 391 node.SetIsFolder(false); |
| 393 node.SetTitle(title); | 392 node.SetTitle(title); |
| 394 node.SetBookmarkSpecifics(specifics); | 393 node.SetBookmarkSpecifics(specifics); |
| 395 | 394 |
| 396 return node.GetId(); | 395 return node.GetId(); |
| 397 } | 396 } |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 782 // association code. | 781 // association code. |
| 783 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociate) { | 782 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociate) { |
| 784 const int kNumBookmarksPerFolder = 10; | 783 const int kNumBookmarksPerFolder = 10; |
| 785 const int kNumFolders = 10; | 784 const int kNumFolders = 10; |
| 786 | 785 |
| 787 CreatePermanentBookmarkNodes(); | 786 CreatePermanentBookmarkNodes(); |
| 788 | 787 |
| 789 { | 788 { |
| 790 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | 789 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 791 for (int i = 0; i < kNumFolders; ++i) { | 790 for (int i = 0; i < kNumFolders; ++i) { |
| 792 int64 folder_id = | 791 int64 folder_id = AddFolderToShare(&trans, |
| 793 AddFolderToShare(&trans, base::StringPrintf("folder%05d", i)); | 792 base::StringPrintf("folder%05d", i)); |
| 794 for (int j = 0; j < kNumBookmarksPerFolder; ++j) { | 793 for (int j = 0; j < kNumBookmarksPerFolder; ++j) { |
| 795 AddBookmarkToShare( | 794 AddBookmarkToShare(&trans, |
| 796 &trans, folder_id, base::StringPrintf("bookmark%05d", j), | 795 folder_id, |
| 797 base::StringPrintf("http://www.google.com/search?q=%05d", j)); | 796 base::StringPrintf("bookmark%05d", j)); |
| 798 } | 797 } |
| 799 } | 798 } |
| 800 } | 799 } |
| 801 | 800 |
| 802 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | 801 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); |
| 803 StartSync(); | 802 StartSync(); |
| 804 | 803 |
| 805 ExpectModelMatch(); | 804 ExpectModelMatch(); |
| 806 } | 805 } |
| 807 | 806 |
| 808 // Tests bookmark association when nodes exists in the native model only. | |
| 809 // These entries should be copied to Sync directory during association process. | |
| 810 TEST_F(ProfileSyncServiceBookmarkTest, | |
| 811 InitialModelAssociateWithBookmarkModelNodes) { | |
| 812 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
| 813 const BookmarkNode* folder = | |
| 814 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("foobar")); | |
| 815 model_->AddFolder(folder, 0, base::ASCIIToUTF16("nested")); | |
| 816 model_->AddURL(folder, 0, base::ASCIIToUTF16("Internets #1 Pies Site"), | |
| 817 GURL("http://www.easypie.com/")); | |
| 818 model_->AddURL(folder, 1, base::ASCIIToUTF16("Airplanes"), | |
| 819 GURL("http://www.easyjet.com/")); | |
| 820 | |
| 821 StartSync(); | |
| 822 ExpectModelMatch(); | |
| 823 } | |
| 824 | |
| 825 // Tests bookmark association case when there is an entry in the delete journal | |
| 826 // that matches one of native bookmarks. | |
| 827 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociateWithDeleteJournal) { | |
| 828 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
| 829 const BookmarkNode* folder = model_->AddFolder(model_->bookmark_bar_node(), 0, | |
| 830 base::ASCIIToUTF16("foobar")); | |
| 831 const BookmarkNode* bookmark = | |
| 832 model_->AddURL(folder, 0, base::ASCIIToUTF16("Airplanes"), | |
| 833 GURL("http://www.easyjet.com/")); | |
| 834 | |
| 835 CreatePermanentBookmarkNodes(); | |
| 836 | |
| 837 // Create entries matching the folder and the bookmark above. | |
| 838 int64 folder_id, bookmark_id; | |
| 839 { | |
| 840 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
| 841 folder_id = AddFolderToShare(&trans, "foobar"); | |
| 842 bookmark_id = AddBookmarkToShare(&trans, folder_id, "Airplanes", | |
| 843 "http://www.easyjet.com/"); | |
| 844 } | |
| 845 | |
| 846 // Associate the bookmark sync node with the native model one and make | |
| 847 // it deleted. | |
| 848 { | |
| 849 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
| 850 syncer::WriteNode node(&trans); | |
| 851 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(bookmark_id)); | |
| 852 | |
| 853 node.GetMutableEntryForTest()->PutLocalExternalId(bookmark->id()); | |
| 854 node.GetMutableEntryForTest()->PutServerIsDel(true); | |
| 855 node.GetMutableEntryForTest()->PutIsDel(true); | |
| 856 } | |
| 857 | |
| 858 ASSERT_TRUE(AssociateModels()); | |
| 859 ExpectModelMatch(); | |
| 860 | |
| 861 // The bookmark node should be deleted. | |
| 862 EXPECT_EQ(0, folder->child_count()); | |
| 863 } | |
| 864 | |
| 865 // Tests that the external ID is used to match the right folder amoung | |
| 866 // multiple folders with the same name during the native model traversal. | |
| 867 // Also tests that the external ID is used to match the right bookmark | |
| 868 // among multiple identical bookmarks when dealing with the delete journal. | |
| 869 TEST_F(ProfileSyncServiceBookmarkTest, | |
| 870 InitialModelAssociateVerifyExternalIdMatch) { | |
| 871 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | |
| 872 const int kNumFolders = 10; | |
| 873 const int kNumBookmarks = 10; | |
| 874 const int kFolderToIncludeBookmarks = 7; | |
| 875 const int kBookmarkToDelete = 4; | |
| 876 | |
| 877 int64 folder_ids[kNumFolders]; | |
| 878 int64 bookmark_ids[kNumBookmarks]; | |
| 879 | |
| 880 // Create native folders and bookmarks with identical names. Only | |
| 881 // one of the folders contains bookmarks and others are empty. Here is the | |
| 882 // expected tree shape: | |
| 883 // Bookmarks bar | |
| 884 // +- folder (#0) | |
| 885 // +- folder (#1) | |
| 886 // ... | |
| 887 // +- folder (#7) <-- Only this folder contains bookmarks. | |
| 888 // +- bookmark (#0) | |
| 889 // +- bookmark (#1) | |
| 890 // ... | |
| 891 // +- bookmark (#4) <-- Only this one bookmark should be removed later. | |
| 892 // ... | |
| 893 // +- bookmark (#9) | |
| 894 // ... | |
| 895 // +- folder (#9) | |
| 896 | |
| 897 const BookmarkNode* parent_folder = nullptr; | |
| 898 for (int i = 0; i < kNumFolders; i++) { | |
| 899 const BookmarkNode* folder = model_->AddFolder( | |
| 900 model_->bookmark_bar_node(), i, base::ASCIIToUTF16("folder")); | |
| 901 folder_ids[i] = folder->id(); | |
| 902 if (i == kFolderToIncludeBookmarks) { | |
| 903 parent_folder = folder; | |
| 904 } | |
| 905 } | |
| 906 | |
| 907 for (int i = 0; i < kNumBookmarks; i++) { | |
| 908 const BookmarkNode* bookmark = | |
| 909 model_->AddURL(parent_folder, i, base::ASCIIToUTF16("bookmark"), | |
| 910 GURL("http://www.google.com/")); | |
| 911 bookmark_ids[i] = bookmark->id(); | |
| 912 } | |
| 913 | |
| 914 // Number of nodes in bookmark bar before association. | |
| 915 int total_node_count = model_->bookmark_bar_node()->GetTotalNodeCount(); | |
| 916 | |
| 917 CreatePermanentBookmarkNodes(); | |
| 918 | |
| 919 int64 sync_bookmark_id_to_delete = 0; | |
| 920 { | |
| 921 // Create sync folders matching native folders above. | |
| 922 int64 parent_id = 0; | |
| 923 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
| 924 // Create in reverse order because AddFolderToShare passes NULL for | |
| 925 // |predecessor| argument. | |
| 926 for (int i = kNumFolders - 1; i >= 0; i--) { | |
| 927 int64 id = AddFolderToShare(&trans, "folder"); | |
| 928 | |
| 929 // Pre-map sync folders to native folders by setting | |
| 930 // external ID. This will verify that the association algorithm picks | |
| 931 // the right ones despite all of them having identical names. | |
| 932 // More specifically this will help to avoid cloning bookmarks from | |
| 933 // a wrong folder. | |
| 934 syncer::WriteNode node(&trans); | |
| 935 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
| 936 node.GetMutableEntryForTest()->PutLocalExternalId(folder_ids[i]); | |
| 937 | |
| 938 if (i == kFolderToIncludeBookmarks) { | |
| 939 parent_id = id; | |
| 940 } | |
| 941 } | |
| 942 | |
| 943 // Create sync bookmark matching native bookmarks above in reverse order | |
| 944 // because AddBookmarkToShare passes NULL for |predecessor| argument. | |
| 945 for (int i = kNumBookmarks - 1; i >= 0; i--) { | |
| 946 int id = AddBookmarkToShare(&trans, parent_id, "bookmark", | |
| 947 "http://www.google.com/"); | |
| 948 | |
| 949 // Pre-map sync bookmarks to native bookmarks by setting | |
| 950 // external ID. This will verify that the association algorithm picks | |
| 951 // the right ones despite all of them having identical names and URLs. | |
| 952 syncer::WriteNode node(&trans); | |
| 953 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id)); | |
| 954 node.GetMutableEntryForTest()->PutLocalExternalId(bookmark_ids[i]); | |
| 955 | |
| 956 if (i == kBookmarkToDelete) { | |
| 957 sync_bookmark_id_to_delete = id; | |
| 958 } | |
| 959 } | |
| 960 } | |
| 961 | |
| 962 // Make one bookmark deleted. | |
| 963 { | |
| 964 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); | |
| 965 syncer::WriteNode node(&trans); | |
| 966 EXPECT_EQ(BaseNode::INIT_OK, | |
| 967 node.InitByIdLookup(sync_bookmark_id_to_delete)); | |
| 968 | |
| 969 node.GetMutableEntryForTest()->PutServerIsDel(true); | |
| 970 node.GetMutableEntryForTest()->PutIsDel(true); | |
| 971 } | |
| 972 | |
| 973 // Perform association. | |
| 974 ASSERT_TRUE(AssociateModels()); | |
| 975 ExpectModelMatch(); | |
| 976 | |
| 977 // Only one native node should have been deleted and no nodes cloned due to | |
| 978 // matching folder names. | |
| 979 EXPECT_EQ(kNumFolders, model_->bookmark_bar_node()->child_count()); | |
| 980 EXPECT_EQ(kNumBookmarks - 1, parent_folder->child_count()); | |
| 981 EXPECT_EQ(total_node_count - 1, | |
| 982 model_->bookmark_bar_node()->GetTotalNodeCount()); | |
| 983 | |
| 984 // Verify that the right bookmark got deleted and no bookmarks reordered. | |
| 985 for (int i = 0; i < parent_folder->child_count(); i++) { | |
| 986 int index_in_bookmark_ids = (i < kBookmarkToDelete) ? i : i + 1; | |
| 987 EXPECT_EQ(bookmark_ids[index_in_bookmark_ids], | |
| 988 parent_folder->GetChild(i)->id()); | |
| 989 } | |
| 990 } | |
| 991 | 807 |
| 992 TEST_F(ProfileSyncServiceBookmarkTest, BookmarkModelOperations) { | 808 TEST_F(ProfileSyncServiceBookmarkTest, BookmarkModelOperations) { |
| 993 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); | 809 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); |
| 994 StartSync(); | 810 StartSync(); |
| 995 | 811 |
| 996 // Test addition. | 812 // Test addition. |
| 997 const BookmarkNode* folder = | 813 const BookmarkNode* folder = |
| 998 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("foobar")); | 814 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("foobar")); |
| 999 ExpectSyncerNodeMatching(folder); | 815 ExpectSyncerNodeMatching(folder); |
| 1000 ExpectModelMatch(); | 816 ExpectModelMatch(); |
| (...skipping 1370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2371 ExpectModelMatch(); | 2187 ExpectModelMatch(); |
| 2372 | 2188 |
| 2373 // Then simulate the add call arriving late. | 2189 // Then simulate the add call arriving late. |
| 2374 change_processor_->BookmarkNodeAdded(model_, model_->bookmark_bar_node(), 0); | 2190 change_processor_->BookmarkNodeAdded(model_, model_->bookmark_bar_node(), 0); |
| 2375 ExpectModelMatch(); | 2191 ExpectModelMatch(); |
| 2376 } | 2192 } |
| 2377 | 2193 |
| 2378 } // namespace | 2194 } // namespace |
| 2379 | 2195 |
| 2380 } // namespace browser_sync | 2196 } // namespace browser_sync |
| OLD | NEW |