| 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 #include <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/base_paths.h" | 8 #include "base/base_paths.h" |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 912 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 profile_->CreateBookmarkModel(false); | 923 profile_->CreateBookmarkModel(false); |
| 924 BlockTillBookmarkModelLoaded(); | 924 BlockTillBookmarkModelLoaded(); |
| 925 | 925 |
| 926 VerifyModelMatchesNode(&bbn, bb_model_->bookmark_bar_node()); | 926 VerifyModelMatchesNode(&bbn, bb_model_->bookmark_bar_node()); |
| 927 VerifyModelMatchesNode(&other, bb_model_->other_node()); | 927 VerifyModelMatchesNode(&other, bb_model_->other_node()); |
| 928 VerifyModelMatchesNode(&mobile, bb_model_->mobile_node()); | 928 VerifyModelMatchesNode(&mobile, bb_model_->mobile_node()); |
| 929 VerifyNoDuplicateIDs(bb_model_); | 929 VerifyNoDuplicateIDs(bb_model_); |
| 930 } | 930 } |
| 931 } | 931 } |
| 932 | 932 |
| 933 // Test class that creates a BookmarkModel with a real history backend. | |
| 934 class BookmarkModelTestWithProfile2 : public BookmarkModelTestWithProfile { | |
| 935 public: | |
| 936 virtual void SetUp() { | |
| 937 profile_.reset(new TestingProfile()); | |
| 938 } | |
| 939 | |
| 940 protected: | |
| 941 // Verifies the state of the model matches that of the state in the saved | |
| 942 // history file. | |
| 943 void VerifyExpectedState() { | |
| 944 // Here's the structure we expect: | |
| 945 // bbn | |
| 946 // www.google.com - Google | |
| 947 // F1 | |
| 948 // http://www.google.com/intl/en/ads/ - Google Advertising | |
| 949 // F11 | |
| 950 // http://www.google.com/services/ - Google Business Solutions | |
| 951 // other | |
| 952 // OF1 | |
| 953 // http://www.google.com/intl/en/about.html - About Google | |
| 954 const BookmarkNode* bbn = bb_model_->bookmark_bar_node(); | |
| 955 ASSERT_EQ(2, bbn->child_count()); | |
| 956 | |
| 957 const BookmarkNode* child = bbn->GetChild(0); | |
| 958 ASSERT_EQ(BookmarkNode::URL, child->type()); | |
| 959 ASSERT_EQ(ASCIIToUTF16("Google"), child->GetTitle()); | |
| 960 ASSERT_TRUE(child->url() == GURL("http://www.google.com")); | |
| 961 | |
| 962 child = bbn->GetChild(1); | |
| 963 ASSERT_TRUE(child->is_folder()); | |
| 964 ASSERT_EQ(ASCIIToUTF16("F1"), child->GetTitle()); | |
| 965 ASSERT_EQ(2, child->child_count()); | |
| 966 | |
| 967 const BookmarkNode* parent = child; | |
| 968 child = parent->GetChild(0); | |
| 969 ASSERT_EQ(BookmarkNode::URL, child->type()); | |
| 970 ASSERT_EQ(ASCIIToUTF16("Google Advertising"), child->GetTitle()); | |
| 971 ASSERT_TRUE(child->url() == GURL("http://www.google.com/intl/en/ads/")); | |
| 972 | |
| 973 child = parent->GetChild(1); | |
| 974 ASSERT_TRUE(child->is_folder()); | |
| 975 ASSERT_EQ(ASCIIToUTF16("F11"), child->GetTitle()); | |
| 976 ASSERT_EQ(1, child->child_count()); | |
| 977 | |
| 978 parent = child; | |
| 979 child = parent->GetChild(0); | |
| 980 ASSERT_EQ(BookmarkNode::URL, child->type()); | |
| 981 ASSERT_EQ(ASCIIToUTF16("Google Business Solutions"), child->GetTitle()); | |
| 982 ASSERT_TRUE(child->url() == GURL("http://www.google.com/services/")); | |
| 983 | |
| 984 parent = bb_model_->other_node(); | |
| 985 ASSERT_EQ(2, parent->child_count()); | |
| 986 | |
| 987 child = parent->GetChild(0); | |
| 988 ASSERT_TRUE(child->is_folder()); | |
| 989 ASSERT_EQ(ASCIIToUTF16("OF1"), child->GetTitle()); | |
| 990 ASSERT_EQ(0, child->child_count()); | |
| 991 | |
| 992 child = parent->GetChild(1); | |
| 993 ASSERT_EQ(BookmarkNode::URL, child->type()); | |
| 994 ASSERT_EQ(ASCIIToUTF16("About Google"), child->GetTitle()); | |
| 995 ASSERT_TRUE(child->url() == | |
| 996 GURL("http://www.google.com/intl/en/about.html")); | |
| 997 | |
| 998 parent = bb_model_->mobile_node(); | |
| 999 ASSERT_EQ(0, parent->child_count()); | |
| 1000 | |
| 1001 ASSERT_TRUE(bb_model_->IsBookmarked(GURL("http://www.google.com"))); | |
| 1002 } | |
| 1003 | |
| 1004 void VerifyUniqueIDs() { | |
| 1005 std::set<int64> ids; | |
| 1006 bool has_unique = true; | |
| 1007 VerifyUniqueIDImpl(bb_model_->bookmark_bar_node(), &ids, &has_unique); | |
| 1008 VerifyUniqueIDImpl(bb_model_->other_node(), &ids, &has_unique); | |
| 1009 ASSERT_TRUE(has_unique); | |
| 1010 } | |
| 1011 | |
| 1012 private: | |
| 1013 void VerifyUniqueIDImpl(const BookmarkNode* node, | |
| 1014 std::set<int64>* ids, | |
| 1015 bool* has_unique) { | |
| 1016 if (!*has_unique) | |
| 1017 return; | |
| 1018 if (ids->count(node->id()) != 0) { | |
| 1019 *has_unique = false; | |
| 1020 return; | |
| 1021 } | |
| 1022 ids->insert(node->id()); | |
| 1023 for (int i = 0; i < node->child_count(); ++i) { | |
| 1024 VerifyUniqueIDImpl(node->GetChild(i), ids, has_unique); | |
| 1025 if (!*has_unique) | |
| 1026 return; | |
| 1027 } | |
| 1028 } | |
| 1029 }; | |
| 1030 | |
| 1031 // Tests migrating bookmarks from db into file. This copies an old history db | |
| 1032 // file containing bookmarks and make sure they are loaded correctly and | |
| 1033 // persisted correctly. | |
| 1034 TEST_F(BookmarkModelTestWithProfile2, MigrateFromDBToFileTest) { | |
| 1035 // Copy db file over that contains starred table. | |
| 1036 FilePath old_history_path; | |
| 1037 PathService::Get(chrome::DIR_TEST_DATA, &old_history_path); | |
| 1038 old_history_path = old_history_path.AppendASCII("bookmarks"); | |
| 1039 old_history_path = old_history_path.AppendASCII("History_with_starred"); | |
| 1040 FilePath new_history_path = profile_->GetPath(); | |
| 1041 file_util::Delete(new_history_path, true); | |
| 1042 file_util::CreateDirectory(new_history_path); | |
| 1043 FilePath new_history_file = new_history_path.Append( | |
| 1044 chrome::kHistoryFilename); | |
| 1045 ASSERT_TRUE(file_util::CopyFile(old_history_path, new_history_file)); | |
| 1046 | |
| 1047 // Create the history service making sure it doesn't blow away the file we | |
| 1048 // just copied. | |
| 1049 profile_->CreateHistoryService(false, false); | |
| 1050 profile_->CreateBookmarkModel(true); | |
| 1051 BlockTillBookmarkModelLoaded(); | |
| 1052 | |
| 1053 // Make sure we loaded OK. | |
| 1054 VerifyExpectedState(); | |
| 1055 if (HasFatalFailure()) | |
| 1056 return; | |
| 1057 | |
| 1058 // Make sure the ids are unique. | |
| 1059 VerifyUniqueIDs(); | |
| 1060 if (HasFatalFailure()) | |
| 1061 return; | |
| 1062 | |
| 1063 // Create again. This time we shouldn't load from history at all. | |
| 1064 profile_->CreateBookmarkModel(false); | |
| 1065 BlockTillBookmarkModelLoaded(); | |
| 1066 | |
| 1067 // Make sure we loaded OK. | |
| 1068 VerifyExpectedState(); | |
| 1069 if (HasFatalFailure()) | |
| 1070 return; | |
| 1071 | |
| 1072 VerifyUniqueIDs(); | |
| 1073 if (HasFatalFailure()) | |
| 1074 return; | |
| 1075 | |
| 1076 // Recreate the history service (with a clean db). Do this just to make sure | |
| 1077 // we're loading correctly from the bookmarks file. | |
| 1078 profile_->CreateHistoryService(true, false); | |
| 1079 profile_->CreateBookmarkModel(false); | |
| 1080 BlockTillBookmarkModelLoaded(); | |
| 1081 VerifyExpectedState(); | |
| 1082 VerifyUniqueIDs(); | |
| 1083 } | |
| 1084 | |
| 1085 // Simple test that removes a bookmark. This test exercises the code paths in | 933 // Simple test that removes a bookmark. This test exercises the code paths in |
| 1086 // History that block till bookmark bar model is loaded. | 934 // History that block till bookmark bar model is loaded. |
| 1087 TEST_F(BookmarkModelTestWithProfile2, RemoveNotification) { | 935 TEST_F(BookmarkModelTestWithProfile, RemoveNotification) { |
| 936 profile_.reset(new TestingProfile()); |
| 937 |
| 1088 profile_->CreateHistoryService(false, false); | 938 profile_->CreateHistoryService(false, false); |
| 1089 profile_->CreateBookmarkModel(true); | 939 profile_->CreateBookmarkModel(true); |
| 1090 BlockTillBookmarkModelLoaded(); | 940 BlockTillBookmarkModelLoaded(); |
| 1091 | 941 |
| 1092 // Add a URL. | 942 // Add a URL. |
| 1093 GURL url("http://www.google.com"); | 943 GURL url("http://www.google.com"); |
| 1094 bookmark_utils::AddIfNotBookmarked(bb_model_, url, string16()); | 944 bookmark_utils::AddIfNotBookmarked(bb_model_, url, string16()); |
| 1095 | 945 |
| 1096 HistoryServiceFactory::GetForProfile( | 946 HistoryServiceFactory::GetForProfile( |
| 1097 profile_.get(), Profile::EXPLICIT_ACCESS)->AddPage( | 947 profile_.get(), Profile::EXPLICIT_ACCESS)->AddPage( |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1191 AssertExtensiveChangesObserverCount(1, 0); | 1041 AssertExtensiveChangesObserverCount(1, 0); |
| 1192 model_.EndExtensiveChanges(); | 1042 model_.EndExtensiveChanges(); |
| 1193 EXPECT_TRUE(model_.IsDoingExtensiveChanges()); | 1043 EXPECT_TRUE(model_.IsDoingExtensiveChanges()); |
| 1194 AssertExtensiveChangesObserverCount(1, 0); | 1044 AssertExtensiveChangesObserverCount(1, 0); |
| 1195 model_.EndExtensiveChanges(); | 1045 model_.EndExtensiveChanges(); |
| 1196 EXPECT_FALSE(model_.IsDoingExtensiveChanges()); | 1046 EXPECT_FALSE(model_.IsDoingExtensiveChanges()); |
| 1197 AssertExtensiveChangesObserverCount(1, 1); | 1047 AssertExtensiveChangesObserverCount(1, 1); |
| 1198 } | 1048 } |
| 1199 | 1049 |
| 1200 } // namespace | 1050 } // namespace |
| OLD | NEW |