Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: chrome/browser/sync/profile_sync_service_bookmark_unittest.cc

Issue 904083002: Use external ID to match native model nodes during bookmark association. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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" // TODO(tim): Remove. Bug 131130. 46 #include "sync/syncable/mutable_entry.h"
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
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 std::string title) { 379 const std::string& title,
380 const std::string& url) {
380 EXPECT_FALSE(model_associator_); 381 EXPECT_FALSE(model_associator_);
381 382
382 syncer::ReadNode parent(trans); 383 syncer::ReadNode parent(trans);
383 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id)); 384 EXPECT_EQ(BaseNode::INIT_OK, parent.InitByIdLookup(parent_id));
384 385
385 sync_pb::BookmarkSpecifics specifics; 386 sync_pb::BookmarkSpecifics specifics;
386 specifics.set_url("http://www.google.com/search?q=" + title); 387 specifics.set_url(url);
387 specifics.set_title(title); 388 specifics.set_title(title);
388 389
389 syncer::WriteNode node(trans); 390 syncer::WriteNode node(trans);
390 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL)); 391 EXPECT_TRUE(node.InitBookmarkByCreation(parent, NULL));
391 node.SetIsFolder(false); 392 node.SetIsFolder(false);
392 node.SetTitle(title); 393 node.SetTitle(title);
393 node.SetBookmarkSpecifics(specifics); 394 node.SetBookmarkSpecifics(specifics);
394 395
395 return node.GetId(); 396 return node.GetId();
396 } 397 }
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 // association code. 782 // association code.
782 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociate) { 783 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociate) {
783 const int kNumBookmarksPerFolder = 10; 784 const int kNumBookmarksPerFolder = 10;
784 const int kNumFolders = 10; 785 const int kNumFolders = 10;
785 786
786 CreatePermanentBookmarkNodes(); 787 CreatePermanentBookmarkNodes();
787 788
788 { 789 {
789 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share()); 790 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
790 for (int i = 0; i < kNumFolders; ++i) { 791 for (int i = 0; i < kNumFolders; ++i) {
791 int64 folder_id = AddFolderToShare(&trans, 792 int64 folder_id =
792 base::StringPrintf("folder%05d", i)); 793 AddFolderToShare(&trans, base::StringPrintf("folder%05d", i));
793 for (int j = 0; j < kNumBookmarksPerFolder; ++j) { 794 for (int j = 0; j < kNumBookmarksPerFolder; ++j) {
794 AddBookmarkToShare(&trans, 795 AddBookmarkToShare(
795 folder_id, 796 &trans, folder_id, base::StringPrintf("bookmark%05d", j),
796 base::StringPrintf("bookmark%05d", j)); 797 base::StringPrintf("http://www.google.com/search?q=%05d", j));
797 } 798 }
798 } 799 }
799 } 800 }
800 801
801 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 802 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
802 StartSync(); 803 StartSync();
803 804
804 ExpectModelMatch(); 805 ExpectModelMatch();
805 } 806 }
806 807
808 TEST_F(ProfileSyncServiceBookmarkTest,
Nicolas Zea 2015/02/06 23:34:19 nit: Could you add comments to these tests describ
stanisc 2015/02/09 19:15:37 Done.
809 InitialModelAssociateWithBookmarkModelNodes) {
810 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
811 const BookmarkNode* folder =
812 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("foobar"));
813 model_->AddFolder(folder, 0, base::ASCIIToUTF16("nested"));
814 model_->AddURL(folder, 0, base::ASCIIToUTF16("Internets #1 Pies Site"),
815 GURL("http://www.easypie.com/"));
816 model_->AddURL(folder, 1, base::ASCIIToUTF16("Airplanes"),
817 GURL("http://www.easyjet.com/"));
818
819 StartSync();
820 ExpectModelMatch();
821 }
822
823 TEST_F(ProfileSyncServiceBookmarkTest, InitialModelAssociateWithDeleteJournal) {
824 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
825 const BookmarkNode* folder = model_->AddFolder(model_->bookmark_bar_node(), 0,
826 base::ASCIIToUTF16("foobar"));
827 const BookmarkNode* bookmark =
828 model_->AddURL(folder, 0, base::ASCIIToUTF16("Airplanes"),
829 GURL("http://www.easyjet.com/"));
830
831 CreatePermanentBookmarkNodes();
832
833 // Create entries matching the folder and the bookmark above.
834 int64 folder_id, bookmark_id;
835 {
836 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
837 folder_id = AddFolderToShare(&trans, "foobar");
838 bookmark_id = AddBookmarkToShare(&trans, folder_id, "Airplanes",
839 "http://www.easyjet.com/");
840 }
841
842 // Associate the bookmark sync node with the native model one and make
843 // it deleted.
844 {
845 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
846 syncer::WriteNode node(&trans);
847 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(bookmark_id));
848
849 node.GetMutableEntryForTest()->PutLocalExternalId(bookmark->id());
850 node.GetMutableEntryForTest()->PutServerIsDel(true);
851 node.GetMutableEntryForTest()->PutIsDel(true);
852 }
853
854 ASSERT_TRUE(AssociateModels());
855 ExpectModelMatch();
856
857 // The bookmark mode should be deleted.
Nicolas Zea 2015/02/06 23:34:19 nit: mode -> node
stanisc 2015/02/09 19:15:37 Done.
858 EXPECT_EQ(0, folder->child_count());
859 }
860
861 // Tests that AssociateModels uses external ID match when associating and
862 // deleting bookmark model nodes.
863 TEST_F(ProfileSyncServiceBookmarkTest,
864 InitialModelAssociateVerifyExternalIdMatch) {
865 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
866 const int kNumFolders = 10;
Nicolas Zea 2015/02/06 23:34:19 Comment describing what the expected directory str
stanisc 2015/02/09 19:15:37 Done.
867 const int kNumBookmarks = 10;
868 const int kFolderToIncludeBookmarks = 7;
869 const int kBookmarkToDelete = 4;
870
871 const BookmarkNode* parent_folder = nullptr;
872 int64 folder_ids[kNumFolders];
873 int64 bookmark_ids[kNumBookmarks];
874
875 // Add native folders - all with the same name.
876 for (int i = 0; i < kNumFolders; i++) {
877 const BookmarkNode* folder = model_->AddFolder(
878 model_->bookmark_bar_node(), i, base::ASCIIToUTF16("folder"));
879 folder_ids[i] = folder->id();
880 if (i == kFolderToIncludeBookmarks) {
881 parent_folder = folder;
882 }
883 }
884
885 // Add native bookmarks - all identical.
886 for (int i = 0; i < kNumBookmarks; i++) {
887 const BookmarkNode* bookmark =
888 model_->AddURL(parent_folder, i, base::ASCIIToUTF16("bookmark"),
889 GURL("http://www.google.com/"));
890 bookmark_ids[i] = bookmark->id();
891 }
892
893 // Number of nodes in bookmark bar before association.
894 int total_node_count = model_->bookmark_bar_node()->GetTotalNodeCount();
895
896 CreatePermanentBookmarkNodes();
897
898 int64 sync_bookmark_id_to_delete = 0;
899 {
900 // Create sync folders matching native folders above.
901 int64 parent_id = 0;
902 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
903 // Create in reverse order because AddFolderToShare passes NULL for
904 // |predecessor| argument.
905 for (int i = kNumFolders - 1; i >= 0; i--) {
906 int64 id = AddFolderToShare(&trans, "folder");
907
908 // Pre-map sync folders to native folders by setting
909 // external ID. This will verify that the association algorithm picks
910 // the right ones despite all of them having identical names.
911 // More specifically this will help to avoid cloning bookmarks from
912 // a wrong folder.
913 syncer::WriteNode node(&trans);
914 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
915 node.GetMutableEntryForTest()->PutLocalExternalId(folder_ids[i]);
916
917 if (i == kFolderToIncludeBookmarks) {
918 parent_id = id;
919 }
920 }
921
922 // Create sync bookmark matching native bookmarks above in reverse order
923 // because AddBookmarkToShare passes NULL for |predecessor| argument.
924 for (int i = kNumBookmarks - 1; i >= 0; i--) {
925 int id = AddBookmarkToShare(&trans, parent_id, "bookmark",
926 "http://www.google.com/");
927
928 // Pre-map sync bookmarks to native bookmarks by setting
929 // external ID. This will verify that the association algorithm picks
930 // the right ones despite all of them having identical names and URLs.
931 syncer::WriteNode node(&trans);
932 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(id));
933 node.GetMutableEntryForTest()->PutLocalExternalId(bookmark_ids[i]);
934
935 if (i == kBookmarkToDelete) {
936 sync_bookmark_id_to_delete = id;
937 }
938 }
939 }
940
941 // Make one bookmark deleted.
942 {
943 syncer::WriteTransaction trans(FROM_HERE, test_user_share_.user_share());
944 syncer::WriteNode node(&trans);
945 EXPECT_EQ(BaseNode::INIT_OK,
946 node.InitByIdLookup(sync_bookmark_id_to_delete));
947
948 node.GetMutableEntryForTest()->PutServerIsDel(true);
949 node.GetMutableEntryForTest()->PutIsDel(true);
950 }
951
952 // Perform association.
953 ASSERT_TRUE(AssociateModels());
954 ExpectModelMatch();
955
956 // Only one native node should have been deleted and no nodes cloned due to
957 // matching folder names.
958 EXPECT_EQ(kNumFolders, model_->bookmark_bar_node()->child_count());
959 EXPECT_EQ(kNumBookmarks - 1, parent_folder->child_count());
960 EXPECT_EQ(total_node_count - 1,
961 model_->bookmark_bar_node()->GetTotalNodeCount());
962
963 // Verify that the right bookmark got deleted and no bookmarks reordered.
964 for (int i = 0; i < parent_folder->child_count(); i++) {
965 int index_in_bookmark_ids = (i < kBookmarkToDelete) ? i : i + 1;
966 EXPECT_EQ(bookmark_ids[index_in_bookmark_ids],
967 parent_folder->GetChild(i)->id());
968 }
969 }
807 970
808 TEST_F(ProfileSyncServiceBookmarkTest, BookmarkModelOperations) { 971 TEST_F(ProfileSyncServiceBookmarkTest, BookmarkModelOperations) {
809 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE); 972 LoadBookmarkModel(DELETE_EXISTING_STORAGE, DONT_SAVE_TO_STORAGE);
810 StartSync(); 973 StartSync();
811 974
812 // Test addition. 975 // Test addition.
813 const BookmarkNode* folder = 976 const BookmarkNode* folder =
814 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("foobar")); 977 model_->AddFolder(model_->other_node(), 0, base::ASCIIToUTF16("foobar"));
815 ExpectSyncerNodeMatching(folder); 978 ExpectSyncerNodeMatching(folder);
816 ExpectModelMatch(); 979 ExpectModelMatch();
(...skipping 1370 matching lines...) Expand 10 before | Expand all | Expand 10 after
2187 ExpectModelMatch(); 2350 ExpectModelMatch();
2188 2351
2189 // Then simulate the add call arriving late. 2352 // Then simulate the add call arriving late.
2190 change_processor_->BookmarkNodeAdded(model_, model_->bookmark_bar_node(), 0); 2353 change_processor_->BookmarkNodeAdded(model_, model_->bookmark_bar_node(), 0);
2191 ExpectModelMatch(); 2354 ExpectModelMatch();
2192 } 2355 }
2193 2356
2194 } // namespace 2357 } // namespace
2195 2358
2196 } // namespace browser_sync 2359 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698