| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/hash_tables.h" | 10 #include "base/hash_tables.h" |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 const BookmarkNode* bb_node = model.GetBookmarkBarNode(); | 171 const BookmarkNode* bb_node = model.GetBookmarkBarNode(); |
| 172 ASSERT_TRUE(bb_node != NULL); | 172 ASSERT_TRUE(bb_node != NULL); |
| 173 EXPECT_EQ(0, bb_node->child_count()); | 173 EXPECT_EQ(0, bb_node->child_count()); |
| 174 EXPECT_EQ(BookmarkNode::BOOKMARK_BAR, bb_node->type()); | 174 EXPECT_EQ(BookmarkNode::BOOKMARK_BAR, bb_node->type()); |
| 175 | 175 |
| 176 const BookmarkNode* other_node = model.other_node(); | 176 const BookmarkNode* other_node = model.other_node(); |
| 177 ASSERT_TRUE(other_node != NULL); | 177 ASSERT_TRUE(other_node != NULL); |
| 178 EXPECT_EQ(0, other_node->child_count()); | 178 EXPECT_EQ(0, other_node->child_count()); |
| 179 EXPECT_EQ(BookmarkNode::OTHER_NODE, other_node->type()); | 179 EXPECT_EQ(BookmarkNode::OTHER_NODE, other_node->type()); |
| 180 | 180 |
| 181 const BookmarkNode* synced_node = model.synced_node(); |
| 182 ASSERT_TRUE(synced_node != NULL); |
| 183 EXPECT_EQ(0, synced_node->child_count()); |
| 184 EXPECT_EQ(BookmarkNode::SYNCED, synced_node->type()); |
| 185 |
| 181 EXPECT_TRUE(bb_node->id() != other_node->id()); | 186 EXPECT_TRUE(bb_node->id() != other_node->id()); |
| 187 EXPECT_TRUE(bb_node->id() != synced_node->id()); |
| 188 EXPECT_TRUE(other_node->id() != synced_node->id()); |
| 182 } | 189 } |
| 183 | 190 |
| 184 TEST_F(BookmarkModelTest, AddURL) { | 191 TEST_F(BookmarkModelTest, AddURL) { |
| 185 const BookmarkNode* root = model.GetBookmarkBarNode(); | 192 const BookmarkNode* root = model.GetBookmarkBarNode(); |
| 186 const string16 title(ASCIIToUTF16("foo")); | 193 const string16 title(ASCIIToUTF16("foo")); |
| 187 const GURL url("http://foo.com"); | 194 const GURL url("http://foo.com"); |
| 188 | 195 |
| 189 const BookmarkNode* new_node = model.AddURL(root, 0, title, url); | 196 const BookmarkNode* new_node = model.AddURL(root, 0, title, url); |
| 190 AssertObserverCount(1, 0, 0, 0, 0); | 197 AssertObserverCount(1, 0, 0, 0, 0); |
| 191 observer_details.AssertEquals(root, NULL, 0, -1); | 198 observer_details.AssertEquals(root, NULL, 0, -1); |
| 192 | 199 |
| 193 ASSERT_EQ(1, root->child_count()); | 200 ASSERT_EQ(1, root->child_count()); |
| 194 ASSERT_EQ(title, new_node->GetTitle()); | 201 ASSERT_EQ(title, new_node->GetTitle()); |
| 195 ASSERT_TRUE(url == new_node->GetURL()); | 202 ASSERT_TRUE(url == new_node->GetURL()); |
| 196 ASSERT_EQ(BookmarkNode::URL, new_node->type()); | 203 ASSERT_EQ(BookmarkNode::URL, new_node->type()); |
| 197 ASSERT_TRUE(new_node == model.GetMostRecentlyAddedNodeForURL(url)); | 204 ASSERT_TRUE(new_node == model.GetMostRecentlyAddedNodeForURL(url)); |
| 198 | 205 |
| 199 EXPECT_TRUE(new_node->id() != root->id() && | 206 EXPECT_TRUE(new_node->id() != root->id() && |
| 200 new_node->id() != model.other_node()->id()); | 207 new_node->id() != model.other_node()->id() && |
| 208 new_node->id() != model.synced_node()->id()); |
| 209 } |
| 210 |
| 211 TEST_F(BookmarkModelTest, AddURLToSyncedBookmarks) { |
| 212 const BookmarkNode* root = model.synced_node(); |
| 213 const string16 title(ASCIIToUTF16("foo")); |
| 214 const GURL url("http://foo.com"); |
| 215 |
| 216 const BookmarkNode* new_node = model.AddURL(root, 0, title, url); |
| 217 AssertObserverCount(1, 0, 0, 0, 0); |
| 218 observer_details.AssertEquals(root, NULL, 0, -1); |
| 219 |
| 220 ASSERT_EQ(1, root->child_count()); |
| 221 ASSERT_EQ(title, new_node->GetTitle()); |
| 222 ASSERT_TRUE(url == new_node->GetURL()); |
| 223 ASSERT_EQ(BookmarkNode::URL, new_node->type()); |
| 224 ASSERT_TRUE(new_node == model.GetMostRecentlyAddedNodeForURL(url)); |
| 225 |
| 226 EXPECT_TRUE(new_node->id() != root->id() && |
| 227 new_node->id() != model.other_node()->id() && |
| 228 new_node->id() != model.synced_node()->id()); |
| 201 } | 229 } |
| 202 | 230 |
| 203 TEST_F(BookmarkModelTest, AddFolder) { | 231 TEST_F(BookmarkModelTest, AddFolder) { |
| 204 const BookmarkNode* root = model.GetBookmarkBarNode(); | 232 const BookmarkNode* root = model.GetBookmarkBarNode(); |
| 205 const string16 title(ASCIIToUTF16("foo")); | 233 const string16 title(ASCIIToUTF16("foo")); |
| 206 | 234 |
| 207 const BookmarkNode* new_node = model.AddFolder(root, 0, title); | 235 const BookmarkNode* new_node = model.AddFolder(root, 0, title); |
| 208 AssertObserverCount(1, 0, 0, 0, 0); | 236 AssertObserverCount(1, 0, 0, 0, 0); |
| 209 observer_details.AssertEquals(root, NULL, 0, -1); | 237 observer_details.AssertEquals(root, NULL, 0, -1); |
| 210 | 238 |
| 211 ASSERT_EQ(1, root->child_count()); | 239 ASSERT_EQ(1, root->child_count()); |
| 212 ASSERT_EQ(title, new_node->GetTitle()); | 240 ASSERT_EQ(title, new_node->GetTitle()); |
| 213 ASSERT_EQ(BookmarkNode::FOLDER, new_node->type()); | 241 ASSERT_EQ(BookmarkNode::FOLDER, new_node->type()); |
| 214 | 242 |
| 215 EXPECT_TRUE(new_node->id() != root->id() && | 243 EXPECT_TRUE(new_node->id() != root->id() && |
| 216 new_node->id() != model.other_node()->id()); | 244 new_node->id() != model.other_node()->id() && |
| 245 new_node->id() != model.synced_node()->id()); |
| 217 | 246 |
| 218 // Add another folder, just to make sure folder_ids are incremented correctly. | 247 // Add another folder, just to make sure folder_ids are incremented correctly. |
| 219 ClearCounts(); | 248 ClearCounts(); |
| 220 model.AddFolder(root, 0, title); | 249 model.AddFolder(root, 0, title); |
| 221 AssertObserverCount(1, 0, 0, 0, 0); | 250 AssertObserverCount(1, 0, 0, 0, 0); |
| 222 observer_details.AssertEquals(root, NULL, 0, -1); | 251 observer_details.AssertEquals(root, NULL, 0, -1); |
| 223 } | 252 } |
| 224 | 253 |
| 225 TEST_F(BookmarkModelTest, RemoveURL) { | 254 TEST_F(BookmarkModelTest, RemoveURL) { |
| 226 const BookmarkNode* root = model.GetBookmarkBarNode(); | 255 const BookmarkNode* root = model.GetBookmarkBarNode(); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 | 403 |
| 375 // Tests that adding a URL to a folder updates the last modified time. | 404 // Tests that adding a URL to a folder updates the last modified time. |
| 376 TEST_F(BookmarkModelTest, ParentForNewNodes) { | 405 TEST_F(BookmarkModelTest, ParentForNewNodes) { |
| 377 ASSERT_EQ(model.GetBookmarkBarNode(), model.GetParentForNewNodes()); | 406 ASSERT_EQ(model.GetBookmarkBarNode(), model.GetParentForNewNodes()); |
| 378 | 407 |
| 379 const string16 title(ASCIIToUTF16("foo")); | 408 const string16 title(ASCIIToUTF16("foo")); |
| 380 const GURL url("http://foo.com"); | 409 const GURL url("http://foo.com"); |
| 381 | 410 |
| 382 model.AddURL(model.other_node(), 0, title, url); | 411 model.AddURL(model.other_node(), 0, title, url); |
| 383 ASSERT_EQ(model.other_node(), model.GetParentForNewNodes()); | 412 ASSERT_EQ(model.other_node(), model.GetParentForNewNodes()); |
| 413 |
| 414 model.AddURL(model.synced_node(), 0, title, url); |
| 415 ASSERT_EQ(model.synced_node(), model.GetParentForNewNodes()); |
| 384 } | 416 } |
| 385 | 417 |
| 386 // Make sure recently modified stays in sync when adding a URL. | 418 // Make sure recently modified stays in sync when adding a URL. |
| 387 TEST_F(BookmarkModelTest, MostRecentlyModifiedFolders) { | 419 TEST_F(BookmarkModelTest, MostRecentlyModifiedFolders) { |
| 388 // Add a folder. | 420 // Add a folder. |
| 389 const BookmarkNode* folder = model.AddFolder(model.other_node(), 0, | 421 const BookmarkNode* folder = model.AddFolder(model.other_node(), 0, |
| 390 ASCIIToUTF16("foo")); | 422 ASCIIToUTF16("foo")); |
| 391 // Add a URL to it. | 423 // Add a URL to it. |
| 392 model.AddURL(folder, 0, ASCIIToUTF16("blah"), GURL("http://foo.com")); | 424 model.AddURL(folder, 0, ASCIIToUTF16("blah"), GURL("http://foo.com")); |
| 393 | 425 |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 753 | 785 |
| 754 // Creates a set of nodes in the bookmark bar model, then recreates the | 786 // Creates a set of nodes in the bookmark bar model, then recreates the |
| 755 // bookmark bar model which triggers loading from the db and checks the loaded | 787 // bookmark bar model which triggers loading from the db and checks the loaded |
| 756 // structure to make sure it is what we first created. | 788 // structure to make sure it is what we first created. |
| 757 TEST_F(BookmarkModelTestWithProfile, CreateAndRestore) { | 789 TEST_F(BookmarkModelTestWithProfile, CreateAndRestore) { |
| 758 struct TestData { | 790 struct TestData { |
| 759 // Structure of the children of the bookmark bar model node. | 791 // Structure of the children of the bookmark bar model node. |
| 760 const std::string bbn_contents; | 792 const std::string bbn_contents; |
| 761 // Structure of the children of the other node. | 793 // Structure of the children of the other node. |
| 762 const std::string other_contents; | 794 const std::string other_contents; |
| 795 // Structure of the children of the synced node. |
| 796 const std::string synced_contents; |
| 763 } data[] = { | 797 } data[] = { |
| 764 // See PopulateNodeFromString for a description of these strings. | 798 // See PopulateNodeFromString for a description of these strings. |
| 765 { "", "" }, | 799 { "", "" }, |
| 766 { "a", "b" }, | 800 { "a", "b" }, |
| 767 { "a [ b ]", "" }, | 801 { "a [ b ]", "" }, |
| 768 { "", "[ b ] a [ c [ d e [ f ] ] ]" }, | 802 { "", "[ b ] a [ c [ d e [ f ] ] ]" }, |
| 769 { "a [ b ]", "" }, | 803 { "a [ b ]", "" }, |
| 770 { "a b c [ d e [ f ] ]", "g h i [ j k [ l ] ]"}, | 804 { "a b c [ d e [ f ] ]", "g h i [ j k [ l ] ]"}, |
| 771 }; | 805 }; |
| 772 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 806 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 773 // Recreate the profile. We need to reset with NULL first so that the last | 807 // Recreate the profile. We need to reset with NULL first so that the last |
| 774 // HistoryService releases the locks on the files it creates and we can | 808 // HistoryService releases the locks on the files it creates and we can |
| 775 // delete them. | 809 // delete them. |
| 776 profile_.reset(NULL); | 810 profile_.reset(NULL); |
| 777 profile_.reset(new TestingProfile()); | 811 profile_.reset(new TestingProfile()); |
| 778 profile_->CreateBookmarkModel(true); | 812 profile_->CreateBookmarkModel(true); |
| 779 profile_->CreateHistoryService(true, false); | 813 profile_->CreateHistoryService(true, false); |
| 780 BlockTillBookmarkModelLoaded(); | 814 BlockTillBookmarkModelLoaded(); |
| 781 | 815 |
| 782 TestNode bbn; | 816 TestNode bbn; |
| 783 PopulateNodeFromString(data[i].bbn_contents, &bbn); | 817 PopulateNodeFromString(data[i].bbn_contents, &bbn); |
| 784 PopulateBookmarkNode(&bbn, bb_model_, bb_model_->GetBookmarkBarNode()); | 818 PopulateBookmarkNode(&bbn, bb_model_, bb_model_->GetBookmarkBarNode()); |
| 785 | 819 |
| 786 TestNode other; | 820 TestNode other; |
| 787 PopulateNodeFromString(data[i].other_contents, &other); | 821 PopulateNodeFromString(data[i].other_contents, &other); |
| 788 PopulateBookmarkNode(&other, bb_model_, bb_model_->other_node()); | 822 PopulateBookmarkNode(&other, bb_model_, bb_model_->other_node()); |
| 789 | 823 |
| 824 TestNode synced; |
| 825 PopulateNodeFromString(data[i].synced_contents, &synced); |
| 826 PopulateBookmarkNode(&synced, bb_model_, bb_model_->synced_node()); |
| 827 |
| 790 profile_->CreateBookmarkModel(false); | 828 profile_->CreateBookmarkModel(false); |
| 791 BlockTillBookmarkModelLoaded(); | 829 BlockTillBookmarkModelLoaded(); |
| 792 | 830 |
| 793 VerifyModelMatchesNode(&bbn, bb_model_->GetBookmarkBarNode()); | 831 VerifyModelMatchesNode(&bbn, bb_model_->GetBookmarkBarNode()); |
| 794 VerifyModelMatchesNode(&other, bb_model_->other_node()); | 832 VerifyModelMatchesNode(&other, bb_model_->other_node()); |
| 833 VerifyModelMatchesNode(&synced, bb_model_->synced_node()); |
| 795 VerifyNoDuplicateIDs(bb_model_); | 834 VerifyNoDuplicateIDs(bb_model_); |
| 796 } | 835 } |
| 797 } | 836 } |
| 798 | 837 |
| 799 // Test class that creates a BookmarkModel with a real history backend. | 838 // Test class that creates a BookmarkModel with a real history backend. |
| 800 class BookmarkModelTestWithProfile2 : public BookmarkModelTestWithProfile { | 839 class BookmarkModelTestWithProfile2 : public BookmarkModelTestWithProfile { |
| 801 public: | 840 public: |
| 802 virtual void SetUp() { | 841 virtual void SetUp() { |
| 803 profile_.reset(new TestingProfile()); | 842 profile_.reset(new TestingProfile()); |
| 804 } | 843 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 ASSERT_TRUE(child->is_folder()); | 893 ASSERT_TRUE(child->is_folder()); |
| 855 ASSERT_EQ(ASCIIToUTF16("OF1"), child->GetTitle()); | 894 ASSERT_EQ(ASCIIToUTF16("OF1"), child->GetTitle()); |
| 856 ASSERT_EQ(0, child->child_count()); | 895 ASSERT_EQ(0, child->child_count()); |
| 857 | 896 |
| 858 child = parent->GetChild(1); | 897 child = parent->GetChild(1); |
| 859 ASSERT_EQ(BookmarkNode::URL, child->type()); | 898 ASSERT_EQ(BookmarkNode::URL, child->type()); |
| 860 ASSERT_EQ(ASCIIToUTF16("About Google"), child->GetTitle()); | 899 ASSERT_EQ(ASCIIToUTF16("About Google"), child->GetTitle()); |
| 861 ASSERT_TRUE(child->GetURL() == | 900 ASSERT_TRUE(child->GetURL() == |
| 862 GURL("http://www.google.com/intl/en/about.html")); | 901 GURL("http://www.google.com/intl/en/about.html")); |
| 863 | 902 |
| 903 parent = bb_model_->synced_node(); |
| 904 ASSERT_EQ(0, parent->child_count()); |
| 905 |
| 864 ASSERT_TRUE(bb_model_->IsBookmarked(GURL("http://www.google.com"))); | 906 ASSERT_TRUE(bb_model_->IsBookmarked(GURL("http://www.google.com"))); |
| 865 } | 907 } |
| 866 | 908 |
| 867 void VerifyUniqueIDs() { | 909 void VerifyUniqueIDs() { |
| 868 std::set<int64> ids; | 910 std::set<int64> ids; |
| 869 bool has_unique = true; | 911 bool has_unique = true; |
| 870 VerifyUniqueIDImpl(bb_model_->GetBookmarkBarNode(), &ids, &has_unique); | 912 VerifyUniqueIDImpl(bb_model_->GetBookmarkBarNode(), &ids, &has_unique); |
| 871 VerifyUniqueIDImpl(bb_model_->other_node(), &ids, &has_unique); | 913 VerifyUniqueIDImpl(bb_model_->other_node(), &ids, &has_unique); |
| 872 ASSERT_TRUE(has_unique); | 914 ASSERT_TRUE(has_unique); |
| 873 } | 915 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 988 // Make sure we were notified. | 1030 // Make sure we were notified. |
| 989 AssertObserverCount(0, 0, 0, 0, 1); | 1031 AssertObserverCount(0, 0, 0, 0, 1); |
| 990 | 1032 |
| 991 // Make sure the order matches (remember, 'a' and 'C' are folders and | 1033 // Make sure the order matches (remember, 'a' and 'C' are folders and |
| 992 // come first). | 1034 // come first). |
| 993 EXPECT_EQ(parent->GetChild(0)->GetTitle(), ASCIIToUTF16("a")); | 1035 EXPECT_EQ(parent->GetChild(0)->GetTitle(), ASCIIToUTF16("a")); |
| 994 EXPECT_EQ(parent->GetChild(1)->GetTitle(), ASCIIToUTF16("C")); | 1036 EXPECT_EQ(parent->GetChild(1)->GetTitle(), ASCIIToUTF16("C")); |
| 995 EXPECT_EQ(parent->GetChild(2)->GetTitle(), ASCIIToUTF16("B")); | 1037 EXPECT_EQ(parent->GetChild(2)->GetTitle(), ASCIIToUTF16("B")); |
| 996 EXPECT_EQ(parent->GetChild(3)->GetTitle(), ASCIIToUTF16("d")); | 1038 EXPECT_EQ(parent->GetChild(3)->GetTitle(), ASCIIToUTF16("d")); |
| 997 } | 1039 } |
| OLD | NEW |