| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "app/tree_node_iterator.h" | 5 #include "app/tree_node_iterator.h" |
| 6 #include "app/tree_node_model.h" | 6 #include "app/tree_node_model.h" |
| 7 #include "base/hash_tables.h" | 7 #include "base/hash_tables.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_codec.h" | 9 #include "chrome/browser/bookmarks/bookmark_codec.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_model.h" | 10 #include "chrome/browser/bookmarks/bookmark_model.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 147 |
| 148 int reordered_count_; | 148 int reordered_count_; |
| 149 | 149 |
| 150 ObserverDetails observer_details; | 150 ObserverDetails observer_details; |
| 151 }; | 151 }; |
| 152 | 152 |
| 153 TEST_F(BookmarkModelTest, InitialState) { | 153 TEST_F(BookmarkModelTest, InitialState) { |
| 154 const BookmarkNode* bb_node = model.GetBookmarkBarNode(); | 154 const BookmarkNode* bb_node = model.GetBookmarkBarNode(); |
| 155 ASSERT_TRUE(bb_node != NULL); | 155 ASSERT_TRUE(bb_node != NULL); |
| 156 EXPECT_EQ(0, bb_node->GetChildCount()); | 156 EXPECT_EQ(0, bb_node->GetChildCount()); |
| 157 EXPECT_EQ(history::StarredEntry::BOOKMARK_BAR, bb_node->GetType()); | 157 EXPECT_EQ(BookmarkNode::BOOKMARK_BAR, bb_node->GetType()); |
| 158 | 158 |
| 159 const BookmarkNode* other_node = model.other_node(); | 159 const BookmarkNode* other_node = model.other_node(); |
| 160 ASSERT_TRUE(other_node != NULL); | 160 ASSERT_TRUE(other_node != NULL); |
| 161 EXPECT_EQ(0, other_node->GetChildCount()); | 161 EXPECT_EQ(0, other_node->GetChildCount()); |
| 162 EXPECT_EQ(history::StarredEntry::OTHER, other_node->GetType()); | 162 EXPECT_EQ(BookmarkNode::OTHER_NODE, other_node->GetType()); |
| 163 | 163 |
| 164 EXPECT_TRUE(bb_node->id() != other_node->id()); | 164 EXPECT_TRUE(bb_node->id() != other_node->id()); |
| 165 } | 165 } |
| 166 | 166 |
| 167 TEST_F(BookmarkModelTest, AddURL) { | 167 TEST_F(BookmarkModelTest, AddURL) { |
| 168 const BookmarkNode* root = model.GetBookmarkBarNode(); | 168 const BookmarkNode* root = model.GetBookmarkBarNode(); |
| 169 const std::wstring title(L"foo"); | 169 const std::wstring title(L"foo"); |
| 170 const GURL url("http://foo.com"); | 170 const GURL url("http://foo.com"); |
| 171 | 171 |
| 172 const BookmarkNode* new_node = model.AddURL(root, 0, title, url); | 172 const BookmarkNode* new_node = model.AddURL(root, 0, title, url); |
| 173 AssertObserverCount(1, 0, 0, 0, 0); | 173 AssertObserverCount(1, 0, 0, 0, 0); |
| 174 observer_details.AssertEquals(root, NULL, 0, -1); | 174 observer_details.AssertEquals(root, NULL, 0, -1); |
| 175 | 175 |
| 176 ASSERT_EQ(1, root->GetChildCount()); | 176 ASSERT_EQ(1, root->GetChildCount()); |
| 177 ASSERT_EQ(title, new_node->GetTitle()); | 177 ASSERT_EQ(title, new_node->GetTitle()); |
| 178 ASSERT_TRUE(url == new_node->GetURL()); | 178 ASSERT_TRUE(url == new_node->GetURL()); |
| 179 ASSERT_EQ(history::StarredEntry::URL, new_node->GetType()); | 179 ASSERT_EQ(BookmarkNode::URL, new_node->GetType()); |
| 180 ASSERT_TRUE(new_node == model.GetMostRecentlyAddedNodeForURL(url)); | 180 ASSERT_TRUE(new_node == model.GetMostRecentlyAddedNodeForURL(url)); |
| 181 | 181 |
| 182 EXPECT_TRUE(new_node->id() != root->id() && | 182 EXPECT_TRUE(new_node->id() != root->id() && |
| 183 new_node->id() != model.other_node()->id()); | 183 new_node->id() != model.other_node()->id()); |
| 184 } | 184 } |
| 185 | 185 |
| 186 TEST_F(BookmarkModelTest, AddGroup) { | 186 TEST_F(BookmarkModelTest, AddGroup) { |
| 187 const BookmarkNode* root = model.GetBookmarkBarNode(); | 187 const BookmarkNode* root = model.GetBookmarkBarNode(); |
| 188 const std::wstring title(L"foo"); | 188 const std::wstring title(L"foo"); |
| 189 | 189 |
| 190 const BookmarkNode* new_node = model.AddGroup(root, 0, title); | 190 const BookmarkNode* new_node = model.AddGroup(root, 0, title); |
| 191 AssertObserverCount(1, 0, 0, 0, 0); | 191 AssertObserverCount(1, 0, 0, 0, 0); |
| 192 observer_details.AssertEquals(root, NULL, 0, -1); | 192 observer_details.AssertEquals(root, NULL, 0, -1); |
| 193 | 193 |
| 194 ASSERT_EQ(1, root->GetChildCount()); | 194 ASSERT_EQ(1, root->GetChildCount()); |
| 195 ASSERT_EQ(title, new_node->GetTitle()); | 195 ASSERT_EQ(title, new_node->GetTitle()); |
| 196 ASSERT_EQ(history::StarredEntry::USER_GROUP, new_node->GetType()); | 196 ASSERT_EQ(BookmarkNode::FOLDER, new_node->GetType()); |
| 197 | 197 |
| 198 EXPECT_TRUE(new_node->id() != root->id() && | 198 EXPECT_TRUE(new_node->id() != root->id() && |
| 199 new_node->id() != model.other_node()->id()); | 199 new_node->id() != model.other_node()->id()); |
| 200 | 200 |
| 201 // Add another group, just to make sure group_ids are incremented correctly. | 201 // Add another group, just to make sure group_ids are incremented correctly. |
| 202 ClearCounts(); | 202 ClearCounts(); |
| 203 model.AddGroup(root, 0, title); | 203 model.AddGroup(root, 0, title); |
| 204 AssertObserverCount(1, 0, 0, 0, 0); | 204 AssertObserverCount(1, 0, 0, 0, 0); |
| 205 observer_details.AssertEquals(root, NULL, 0, -1); | 205 observer_details.AssertEquals(root, NULL, 0, -1); |
| 206 } | 206 } |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 // Now we should get the notification. | 467 // Now we should get the notification. |
| 468 EXPECT_EQ(1, listener.notification_count_); | 468 EXPECT_EQ(1, listener.notification_count_); |
| 469 ASSERT_FALSE(listener.details_.starred); | 469 ASSERT_FALSE(listener.details_.starred); |
| 470 ASSERT_EQ(1U, listener.details_.changed_urls.size()); | 470 ASSERT_EQ(1U, listener.details_.changed_urls.size()); |
| 471 EXPECT_TRUE(url == *(listener.details_.changed_urls.begin())); | 471 EXPECT_TRUE(url == *(listener.details_.changed_urls.begin())); |
| 472 } | 472 } |
| 473 | 473 |
| 474 namespace { | 474 namespace { |
| 475 | 475 |
| 476 // See comment in PopulateNodeFromString. | 476 // See comment in PopulateNodeFromString. |
| 477 typedef TreeNodeWithValue<history::StarredEntry::Type> TestNode; | 477 typedef TreeNodeWithValue<BookmarkNode::Type> TestNode; |
| 478 | 478 |
| 479 // Does the work of PopulateNodeFromString. index gives the index of the current | 479 // Does the work of PopulateNodeFromString. index gives the index of the current |
| 480 // element in description to process. | 480 // element in description to process. |
| 481 static void PopulateNodeImpl(const std::vector<std::wstring>& description, | 481 static void PopulateNodeImpl(const std::vector<std::wstring>& description, |
| 482 size_t* index, | 482 size_t* index, |
| 483 TestNode* parent) { | 483 TestNode* parent) { |
| 484 while (*index < description.size()) { | 484 while (*index < description.size()) { |
| 485 const std::wstring& element = description[*index]; | 485 const std::wstring& element = description[*index]; |
| 486 (*index)++; | 486 (*index)++; |
| 487 if (element == L"[") { | 487 if (element == L"[") { |
| 488 // Create a new group and recurse to add all the children. | 488 // Create a new group and recurse to add all the children. |
| 489 // Groups are given a unique named by way of an ever increasing integer | 489 // Groups are given a unique named by way of an ever increasing integer |
| 490 // value. The groups need not have a name, but one is assigned to help | 490 // value. The groups need not have a name, but one is assigned to help |
| 491 // in debugging. | 491 // in debugging. |
| 492 static int next_group_id = 1; | 492 static int next_group_id = 1; |
| 493 TestNode* new_node = | 493 TestNode* new_node = |
| 494 new TestNode(IntToWString(next_group_id++), | 494 new TestNode(IntToWString(next_group_id++), |
| 495 history::StarredEntry::USER_GROUP); | 495 BookmarkNode::FOLDER); |
| 496 parent->Add(parent->GetChildCount(), new_node); | 496 parent->Add(parent->GetChildCount(), new_node); |
| 497 PopulateNodeImpl(description, index, new_node); | 497 PopulateNodeImpl(description, index, new_node); |
| 498 } else if (element == L"]") { | 498 } else if (element == L"]") { |
| 499 // End the current group. | 499 // End the current group. |
| 500 return; | 500 return; |
| 501 } else { | 501 } else { |
| 502 // Add a new URL. | 502 // Add a new URL. |
| 503 | 503 |
| 504 // All tokens must be space separated. If there is a [ or ] in the name it | 504 // All tokens must be space separated. If there is a [ or ] in the name it |
| 505 // likely means a space was forgotten. | 505 // likely means a space was forgotten. |
| 506 DCHECK(element.find('[') == std::string::npos); | 506 DCHECK(element.find('[') == std::string::npos); |
| 507 DCHECK(element.find(']') == std::string::npos); | 507 DCHECK(element.find(']') == std::string::npos); |
| 508 parent->Add(parent->GetChildCount(), | 508 parent->Add(parent->GetChildCount(), |
| 509 new TestNode(element, history::StarredEntry::URL)); | 509 new TestNode(element, BookmarkNode::URL)); |
| 510 } | 510 } |
| 511 } | 511 } |
| 512 } | 512 } |
| 513 | 513 |
| 514 // Creates and adds nodes to parent based on description. description consists | 514 // Creates and adds nodes to parent based on description. description consists |
| 515 // of the following tokens (all space separated): | 515 // of the following tokens (all space separated): |
| 516 // [ : creates a new USER_GROUP node. All elements following the [ until the | 516 // [ : creates a new USER_GROUP node. All elements following the [ until the |
| 517 // next balanced ] is encountered are added as children to the node. | 517 // next balanced ] is encountered are added as children to the node. |
| 518 // ] : closes the last group created by [ so that any further nodes are added | 518 // ] : closes the last group created by [ so that any further nodes are added |
| 519 // to the current groups parent. | 519 // to the current groups parent. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 535 SplitStringAlongWhitespace(description, &elements); | 535 SplitStringAlongWhitespace(description, &elements); |
| 536 PopulateNodeImpl(elements, &index, parent); | 536 PopulateNodeImpl(elements, &index, parent); |
| 537 } | 537 } |
| 538 | 538 |
| 539 // Populates the BookmarkNode with the children of parent. | 539 // Populates the BookmarkNode with the children of parent. |
| 540 static void PopulateBookmarkNode(TestNode* parent, | 540 static void PopulateBookmarkNode(TestNode* parent, |
| 541 BookmarkModel* model, | 541 BookmarkModel* model, |
| 542 const BookmarkNode* bb_node) { | 542 const BookmarkNode* bb_node) { |
| 543 for (int i = 0; i < parent->GetChildCount(); ++i) { | 543 for (int i = 0; i < parent->GetChildCount(); ++i) { |
| 544 TestNode* child = parent->GetChild(i); | 544 TestNode* child = parent->GetChild(i); |
| 545 if (child->value == history::StarredEntry::USER_GROUP) { | 545 if (child->value == BookmarkNode::FOLDER) { |
| 546 const BookmarkNode* new_bb_node = | 546 const BookmarkNode* new_bb_node = |
| 547 model->AddGroup(bb_node, i, child->GetTitle()); | 547 model->AddGroup(bb_node, i, child->GetTitle()); |
| 548 PopulateBookmarkNode(child, model, new_bb_node); | 548 PopulateBookmarkNode(child, model, new_bb_node); |
| 549 } else { | 549 } else { |
| 550 model->AddURL(bb_node, i, child->GetTitle(), | 550 model->AddURL(bb_node, i, child->GetTitle(), |
| 551 GURL("http://" + WideToASCII(child->GetTitle()))); | 551 GURL("http://" + WideToASCII(child->GetTitle()))); |
| 552 } | 552 } |
| 553 } | 553 } |
| 554 } | 554 } |
| 555 | 555 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 571 | 571 |
| 572 protected: | 572 protected: |
| 573 // Verifies the contents of the bookmark bar node match the contents of the | 573 // Verifies the contents of the bookmark bar node match the contents of the |
| 574 // TestNode. | 574 // TestNode. |
| 575 void VerifyModelMatchesNode(TestNode* expected, const BookmarkNode* actual) { | 575 void VerifyModelMatchesNode(TestNode* expected, const BookmarkNode* actual) { |
| 576 ASSERT_EQ(expected->GetChildCount(), actual->GetChildCount()); | 576 ASSERT_EQ(expected->GetChildCount(), actual->GetChildCount()); |
| 577 for (int i = 0; i < expected->GetChildCount(); ++i) { | 577 for (int i = 0; i < expected->GetChildCount(); ++i) { |
| 578 TestNode* expected_child = expected->GetChild(i); | 578 TestNode* expected_child = expected->GetChild(i); |
| 579 const BookmarkNode* actual_child = actual->GetChild(i); | 579 const BookmarkNode* actual_child = actual->GetChild(i); |
| 580 ASSERT_EQ(expected_child->GetTitle(), actual_child->GetTitle()); | 580 ASSERT_EQ(expected_child->GetTitle(), actual_child->GetTitle()); |
| 581 if (expected_child->value == history::StarredEntry::USER_GROUP) { | 581 if (expected_child->value == BookmarkNode::FOLDER) { |
| 582 ASSERT_TRUE(actual_child->GetType() == | 582 ASSERT_TRUE(actual_child->GetType() == BookmarkNode::FOLDER); |
| 583 history::StarredEntry::USER_GROUP); | |
| 584 // Recurse throught children. | 583 // Recurse throught children. |
| 585 VerifyModelMatchesNode(expected_child, actual_child); | 584 VerifyModelMatchesNode(expected_child, actual_child); |
| 586 if (HasFatalFailure()) | 585 if (HasFatalFailure()) |
| 587 return; | 586 return; |
| 588 } else { | 587 } else { |
| 589 // No need to check the URL, just the title is enough. | 588 // No need to check the URL, just the title is enough. |
| 590 ASSERT_TRUE(actual_child->GetType() == | 589 ASSERT_TRUE(actual_child->GetType() == BookmarkNode::URL); |
| 591 history::StarredEntry::URL); | |
| 592 } | 590 } |
| 593 } | 591 } |
| 594 } | 592 } |
| 595 | 593 |
| 596 void VerifyNoDuplicateIDs(BookmarkModel* model) { | 594 void VerifyNoDuplicateIDs(BookmarkModel* model) { |
| 597 TreeNodeIterator<const BookmarkNode> it(model->root_node()); | 595 TreeNodeIterator<const BookmarkNode> it(model->root_node()); |
| 598 base::hash_set<int> ids; | 596 base::hash_set<int> ids; |
| 599 while (it.has_next()) | 597 while (it.has_next()) |
| 600 ASSERT_TRUE(ids.insert(it.Next()->id()).second); | 598 ASSERT_TRUE(ids.insert(it.Next()->id()).second); |
| 601 } | 599 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 // http://www.google.com/intl/en/ads/ - Google Advertising | 713 // http://www.google.com/intl/en/ads/ - Google Advertising |
| 716 // F11 | 714 // F11 |
| 717 // http://www.google.com/services/ - Google Business Solutions | 715 // http://www.google.com/services/ - Google Business Solutions |
| 718 // other | 716 // other |
| 719 // OF1 | 717 // OF1 |
| 720 // http://www.google.com/intl/en/about.html - About Google | 718 // http://www.google.com/intl/en/about.html - About Google |
| 721 const BookmarkNode* bbn = bb_model_->GetBookmarkBarNode(); | 719 const BookmarkNode* bbn = bb_model_->GetBookmarkBarNode(); |
| 722 ASSERT_EQ(2, bbn->GetChildCount()); | 720 ASSERT_EQ(2, bbn->GetChildCount()); |
| 723 | 721 |
| 724 const BookmarkNode* child = bbn->GetChild(0); | 722 const BookmarkNode* child = bbn->GetChild(0); |
| 725 ASSERT_EQ(history::StarredEntry::URL, child->GetType()); | 723 ASSERT_EQ(BookmarkNode::URL, child->GetType()); |
| 726 ASSERT_EQ(L"Google", child->GetTitle()); | 724 ASSERT_EQ(L"Google", child->GetTitle()); |
| 727 ASSERT_TRUE(child->GetURL() == GURL("http://www.google.com")); | 725 ASSERT_TRUE(child->GetURL() == GURL("http://www.google.com")); |
| 728 | 726 |
| 729 child = bbn->GetChild(1); | 727 child = bbn->GetChild(1); |
| 730 ASSERT_TRUE(child->is_folder()); | 728 ASSERT_TRUE(child->is_folder()); |
| 731 ASSERT_EQ(L"F1", child->GetTitle()); | 729 ASSERT_EQ(L"F1", child->GetTitle()); |
| 732 ASSERT_EQ(2, child->GetChildCount()); | 730 ASSERT_EQ(2, child->GetChildCount()); |
| 733 | 731 |
| 734 const BookmarkNode* parent = child; | 732 const BookmarkNode* parent = child; |
| 735 child = parent->GetChild(0); | 733 child = parent->GetChild(0); |
| 736 ASSERT_EQ(history::StarredEntry::URL, child->GetType()); | 734 ASSERT_EQ(BookmarkNode::URL, child->GetType()); |
| 737 ASSERT_EQ(L"Google Advertising", child->GetTitle()); | 735 ASSERT_EQ(L"Google Advertising", child->GetTitle()); |
| 738 ASSERT_TRUE(child->GetURL() == GURL("http://www.google.com/intl/en/ads/")); | 736 ASSERT_TRUE(child->GetURL() == GURL("http://www.google.com/intl/en/ads/")); |
| 739 | 737 |
| 740 child = parent->GetChild(1); | 738 child = parent->GetChild(1); |
| 741 ASSERT_TRUE(child->is_folder()); | 739 ASSERT_TRUE(child->is_folder()); |
| 742 ASSERT_EQ(L"F11", child->GetTitle()); | 740 ASSERT_EQ(L"F11", child->GetTitle()); |
| 743 ASSERT_EQ(1, child->GetChildCount()); | 741 ASSERT_EQ(1, child->GetChildCount()); |
| 744 | 742 |
| 745 parent = child; | 743 parent = child; |
| 746 child = parent->GetChild(0); | 744 child = parent->GetChild(0); |
| 747 ASSERT_EQ(history::StarredEntry::URL, child->GetType()); | 745 ASSERT_EQ(BookmarkNode::URL, child->GetType()); |
| 748 ASSERT_EQ(L"Google Business Solutions", child->GetTitle()); | 746 ASSERT_EQ(L"Google Business Solutions", child->GetTitle()); |
| 749 ASSERT_TRUE(child->GetURL() == GURL("http://www.google.com/services/")); | 747 ASSERT_TRUE(child->GetURL() == GURL("http://www.google.com/services/")); |
| 750 | 748 |
| 751 parent = bb_model_->other_node(); | 749 parent = bb_model_->other_node(); |
| 752 ASSERT_EQ(2, parent->GetChildCount()); | 750 ASSERT_EQ(2, parent->GetChildCount()); |
| 753 | 751 |
| 754 child = parent->GetChild(0); | 752 child = parent->GetChild(0); |
| 755 ASSERT_TRUE(child->is_folder()); | 753 ASSERT_TRUE(child->is_folder()); |
| 756 ASSERT_EQ(L"OF1", child->GetTitle()); | 754 ASSERT_EQ(L"OF1", child->GetTitle()); |
| 757 ASSERT_EQ(0, child->GetChildCount()); | 755 ASSERT_EQ(0, child->GetChildCount()); |
| 758 | 756 |
| 759 child = parent->GetChild(1); | 757 child = parent->GetChild(1); |
| 760 ASSERT_EQ(history::StarredEntry::URL, child->GetType()); | 758 ASSERT_EQ(BookmarkNode::URL, child->GetType()); |
| 761 ASSERT_EQ(L"About Google", child->GetTitle()); | 759 ASSERT_EQ(L"About Google", child->GetTitle()); |
| 762 ASSERT_TRUE(child->GetURL() == | 760 ASSERT_TRUE(child->GetURL() == |
| 763 GURL("http://www.google.com/intl/en/about.html")); | 761 GURL("http://www.google.com/intl/en/about.html")); |
| 764 | 762 |
| 765 ASSERT_TRUE(bb_model_->IsBookmarked(GURL("http://www.google.com"))); | 763 ASSERT_TRUE(bb_model_->IsBookmarked(GURL("http://www.google.com"))); |
| 766 } | 764 } |
| 767 }; | 765 }; |
| 768 | 766 |
| 769 // Tests migrating bookmarks from db into file. This copies an old history db | 767 // Tests migrating bookmarks from db into file. This copies an old history db |
| 770 // file containing bookmarks and make sure they are loaded correctly and | 768 // file containing bookmarks and make sure they are loaded correctly and |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 // Make sure we were notified. | 851 // Make sure we were notified. |
| 854 AssertObserverCount(0, 0, 0, 0, 1); | 852 AssertObserverCount(0, 0, 0, 0, 1); |
| 855 | 853 |
| 856 // Make sure the order matches (remember, 'a' and 'C' are folders and | 854 // Make sure the order matches (remember, 'a' and 'C' are folders and |
| 857 // come first). | 855 // come first). |
| 858 EXPECT_TRUE(parent->GetChild(0)->GetTitle() == L"a"); | 856 EXPECT_TRUE(parent->GetChild(0)->GetTitle() == L"a"); |
| 859 EXPECT_TRUE(parent->GetChild(1)->GetTitle() == L"C"); | 857 EXPECT_TRUE(parent->GetChild(1)->GetTitle() == L"C"); |
| 860 EXPECT_TRUE(parent->GetChild(2)->GetTitle() == L"B"); | 858 EXPECT_TRUE(parent->GetChild(2)->GetTitle() == L"B"); |
| 861 EXPECT_TRUE(parent->GetChild(3)->GetTitle() == L"d"); | 859 EXPECT_TRUE(parent->GetChild(3)->GetTitle() == L"d"); |
| 862 } | 860 } |
| OLD | NEW |