diff options
Diffstat (limited to 'chrome/browser/bookmark_bar_model_unittest.cc')
-rw-r--r-- | chrome/browser/bookmark_bar_model_unittest.cc | 293 |
1 files changed, 176 insertions, 117 deletions
diff --git a/chrome/browser/bookmark_bar_model_unittest.cc b/chrome/browser/bookmark_bar_model_unittest.cc index 5a95963..2f52e19 100644 --- a/chrome/browser/bookmark_bar_model_unittest.cc +++ b/chrome/browser/bookmark_bar_model_unittest.cc @@ -28,10 +28,10 @@ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "base/string_util.h" -#ifdef USE_BOOKMARK_CODEC #include "chrome/browser/bookmark_codec.h" -#endif // USE_BOOKMARK_CODEC #include "chrome/browser/bookmark_bar_model.h" +#include "chrome/common/chrome_constants.h" +#include "chrome/common/chrome_paths.h" #include "chrome/test/testing_profile.h" #include "chrome/views/tree_node_model.h" #include "testing/gtest/include/gtest/gtest.h" @@ -130,10 +130,6 @@ class BookmarkBarModelTest : public testing::Test, ASSERT_EQ(changed_count, this->changed_count); } - history::UIStarID GetMaxGroupID() { - return GetMaxGroupID(model.root_node()); - } - void AssertNodesEqual(BookmarkBarNode* expected, BookmarkBarNode* actual) { ASSERT_TRUE(expected); ASSERT_TRUE(actual); @@ -170,26 +166,20 @@ class BookmarkBarModelTest : public testing::Test, int changed_count; ObserverDetails observer_details; - - private: - history::UIStarID GetMaxGroupID(BookmarkBarNode* node) { - history::UIStarID max_id = node->GetGroupID(); - for (int i = 0; i < node->GetChildCount(); ++i) { - max_id = std::max(max_id, GetMaxGroupID(node->GetChild(i))); - } - return max_id; - } - - DISALLOW_EVIL_CONSTRUCTORS(BookmarkBarModelTest); }; TEST_F(BookmarkBarModelTest, InitialState) { - BookmarkBarNode* root = model.GetBookmarkBarNode(); - ASSERT_TRUE(root != NULL); - ASSERT_EQ(0, root->GetChildCount()); - ASSERT_EQ(history::StarredEntry::BOOKMARK_BAR, root->GetType()); - ASSERT_EQ(HistoryService::kBookmarkBarID, root->GetStarID()); - ASSERT_EQ(HistoryService::kBookmarkBarID, root->GetGroupID()); + BookmarkBarNode* bb_node = model.GetBookmarkBarNode(); + ASSERT_TRUE(bb_node != NULL); + EXPECT_EQ(0, bb_node->GetChildCount()); + EXPECT_EQ(history::StarredEntry::BOOKMARK_BAR, bb_node->GetType()); + + BookmarkBarNode* other_node = model.other_node(); + ASSERT_TRUE(other_node != NULL); + EXPECT_EQ(0, other_node->GetChildCount()); + EXPECT_EQ(history::StarredEntry::OTHER, other_node->GetType()); + + EXPECT_TRUE(bb_node->id() != other_node->id()); } TEST_F(BookmarkBarModelTest, AddURL) { @@ -206,19 +196,14 @@ TEST_F(BookmarkBarModelTest, AddURL) { ASSERT_TRUE(url == new_node->GetURL()); ASSERT_EQ(history::StarredEntry::URL, new_node->GetType()); ASSERT_TRUE(new_node == model.GetNodeByURL(url)); - ASSERT_EQ(new_node->GetGroupID(), new_node->GetEntry().group_id); - ASSERT_EQ(new_node->GetTitle(), new_node->GetEntry().title); - ASSERT_EQ(new_node->GetParent()->GetGroupID(), - new_node->GetEntry().parent_group_id); - ASSERT_EQ(0, new_node->GetEntry().visual_order); - ASSERT_TRUE(new_node->GetURL() == new_node->GetEntry().url); - ASSERT_EQ(history::StarredEntry::URL, new_node->GetEntry().type); + + EXPECT_TRUE(new_node->id() != root->id() && + new_node->id() != model.other_node()->id()); } TEST_F(BookmarkBarModelTest, AddGroup) { BookmarkBarNode* root = model.GetBookmarkBarNode(); const std::wstring title(L"foo"); - const history::UIStarID max_group_id = GetMaxGroupID(); BookmarkBarNode* new_node = model.AddGroup(root, 0, title); AssertObserverCount(1, 0, 0, 0); @@ -227,20 +212,15 @@ TEST_F(BookmarkBarModelTest, AddGroup) { ASSERT_EQ(1, root->GetChildCount()); ASSERT_EQ(title, new_node->GetTitle()); ASSERT_EQ(history::StarredEntry::USER_GROUP, new_node->GetType()); - ASSERT_EQ(max_group_id + 1, new_node->GetGroupID()); - ASSERT_EQ(new_node->GetGroupID(), new_node->GetEntry().group_id); - ASSERT_EQ(new_node->GetTitle(), new_node->GetEntry().title); - ASSERT_EQ(new_node->GetParent()->GetGroupID(), - new_node->GetEntry().parent_group_id); - ASSERT_EQ(0, new_node->GetEntry().visual_order); - ASSERT_EQ(history::StarredEntry::USER_GROUP, new_node->GetEntry().type); + + EXPECT_TRUE(new_node->id() != root->id() && + new_node->id() != model.other_node()->id()); // Add another group, just to make sure group_ids are incremented correctly. ClearCounts(); BookmarkBarNode* new_node2 = model.AddGroup(root, 0, title); AssertObserverCount(1, 0, 0, 0); observer_details.AssertEquals(root, NULL, 0, -1); - ASSERT_EQ(max_group_id + 2, new_node2->GetGroupID()); } TEST_F(BookmarkBarModelTest, RemoveURL) { @@ -282,15 +262,6 @@ TEST_F(BookmarkBarModelTest, RemoveGroup) { ASSERT_TRUE(model.GetNodeByURL(url) == NULL); } -TEST_F(BookmarkBarModelTest, VisualOrderIncrements) { - BookmarkBarNode* root = model.GetBookmarkBarNode(); - BookmarkBarNode* group1 = model.AddGroup(root, 0, L"foo"); - EXPECT_EQ(0, group1->GetEntry().visual_order); - BookmarkBarNode* group2 = model.AddGroup(root, 0, L"foo"); - EXPECT_EQ(1, group1->GetEntry().visual_order); - EXPECT_EQ(0, group2->GetEntry().visual_order); -} - TEST_F(BookmarkBarModelTest, SetTitle) { BookmarkBarNode* root = model.GetBookmarkBarNode(); std::wstring title(L"foo"); @@ -333,22 +304,6 @@ TEST_F(BookmarkBarModelTest, Move) { EXPECT_EQ(0, root->GetChildCount()); } -TEST_F(BookmarkBarModelTest, RemoveFromBookmarkBar) { - BookmarkBarNode* root = model.GetBookmarkBarNode(); - const std::wstring title(L"foo"); - const GURL url("http://foo.com"); - - BookmarkBarNode* new_node = model.AddURL(root, 0, title, url); - - ClearCounts(); - - model.RemoveFromBookmarkBar(new_node); - AssertObserverCount(0, 1, 0, 0); - observer_details.AssertEquals(root, model.other_node(), 0, 0); - - ASSERT_EQ(0, root->GetChildCount()); -} - // Tests that adding a URL to a folder updates the last modified time. TEST_F(BookmarkBarModelTest, ParentForNewNodes) { ASSERT_EQ(model.GetBookmarkBarNode(), model.GetParentForNewNodes()); @@ -383,6 +338,42 @@ TEST_F(BookmarkBarModelTest, MostRecentlyModifiedGroups) { ASSERT_TRUE(most_recent_groups[0] != group); } +// Make sure MostRecentlyAddedEntries stays in sync. +TEST_F(BookmarkBarModelTest, MostRecentlyAddedEntries) { + // Add a couple of nodes such that the following holds for the time of the + // nodes: n1 > n2 > n3 > n4. + Time base_time = Time::Now(); + BookmarkBarNode* n1 = model.AddURL( + model.GetBookmarkBarNode(), 0, L"blah", GURL("http://foo.com/0")); + BookmarkBarNode* n2 = model.AddURL( + model.GetBookmarkBarNode(), 1, L"blah", GURL("http://foo.com/1")); + BookmarkBarNode* n3 = model.AddURL( + model.GetBookmarkBarNode(), 2, L"blah", GURL("http://foo.com/2")); + BookmarkBarNode* n4 = model.AddURL( + model.GetBookmarkBarNode(), 3, L"blah", GURL("http://foo.com/3")); + n1->date_added_ = base_time + TimeDelta::FromDays(4); + n2->date_added_ = base_time + TimeDelta::FromDays(3); + n3->date_added_ = base_time + TimeDelta::FromDays(2); + n4->date_added_ = base_time + TimeDelta::FromDays(1); + + // Make sure order is honored. + std::vector<BookmarkBarNode*> recently_added; + model.GetMostRecentlyAddedEntries(2, &recently_added); + ASSERT_EQ(2, recently_added.size()); + ASSERT_TRUE(n1 == recently_added[0]); + ASSERT_TRUE(n2 == recently_added[1]); + + // swap 1 and 2, then check again. + recently_added.clear(); + std::swap(n1->date_added_, n2->date_added_); + model.GetMostRecentlyAddedEntries(4, &recently_added); + ASSERT_EQ(4, recently_added.size()); + ASSERT_TRUE(n2 == recently_added[0]); + ASSERT_TRUE(n1 == recently_added[1]); + ASSERT_TRUE(n3 == recently_added[2]); + ASSERT_TRUE(n4 == recently_added[3]); +} + namespace { // See comment in PopulateNodeFromString. @@ -471,11 +462,9 @@ static void PopulateBookmarkBarNode(TestNode* parent, class BookmarkBarModelTestWithProfile : public testing::Test, public BookmarkBarModelObserver { public: - BookmarkBarModelTestWithProfile() {} - virtual void SetUp() { profile_.reset(new TestingProfile()); - profile_->CreateHistoryService(); + profile_->CreateHistoryService(true); } virtual void TearDown() { @@ -489,19 +478,21 @@ class BookmarkBarModelTestWithProfile : public testing::Test, // Verifies the contents of the bookmark bar node match the contents of the // TestNode. void VerifyModelMatchesNode(TestNode* expected, BookmarkBarNode* actual) { - EXPECT_EQ(expected->GetChildCount(), actual->GetChildCount()); + ASSERT_EQ(expected->GetChildCount(), actual->GetChildCount()); for (int i = 0; i < expected->GetChildCount(); ++i) { TestNode* expected_child = expected->GetChild(i); BookmarkBarNode* actual_child = actual->GetChild(i); - EXPECT_EQ(expected_child->GetTitle(), actual_child->GetTitle()); + ASSERT_EQ(expected_child->GetTitle(), actual_child->GetTitle()); if (expected_child->value == history::StarredEntry::USER_GROUP) { - EXPECT_TRUE(actual_child->GetType() == + ASSERT_TRUE(actual_child->GetType() == history::StarredEntry::USER_GROUP); // Recurse throught children. VerifyModelMatchesNode(expected_child, actual_child); + if (HasFatalFailure()) + return; } else { // No need to check the URL, just the title is enough. - EXPECT_TRUE(actual_child->GetType() == + ASSERT_TRUE(actual_child->GetType() == history::StarredEntry::URL); } } @@ -510,12 +501,13 @@ class BookmarkBarModelTestWithProfile : public testing::Test, // Creates the bookmark bar model. If the bookmark bar model has already been // created a new one is created and the old one destroyed. void CreateBookmarkBarModel() { - // NOTE: this order is important. We need to make sure the backend has - // processed all pending requests from the current BookmarkBarModel before - // destroying it. By creating a new BookmarkBarModel first and blocking - // until done, we ensure all requests from the old model have completed. + bb_model_.reset(NULL); + BookmarkBarModel* new_model = new BookmarkBarModel(profile_.get()); - BlockTillLoaded(new_model); + if (!new_model->IsLoaded()) + BlockTillLoaded(new_model); + else + new_model->AddObserver(this); bb_model_.reset(new_model); } @@ -526,7 +518,7 @@ class BookmarkBarModelTestWithProfile : public testing::Test, // Need to shutdown the old one before creating a new one. profile_.reset(NULL); profile_.reset(new TestingProfile()); - profile_->CreateHistoryService(); + profile_->CreateHistoryService(true); } scoped_ptr<BookmarkBarModel> bb_model_; @@ -558,8 +550,6 @@ class BookmarkBarModelTestWithProfile : public testing::Test, BookmarkBarNode* node) {} virtual void BookmarkNodeFavIconLoaded(BookmarkBarModel* model, BookmarkBarNode* node) {} - - DISALLOW_EVIL_CONSTRUCTORS(BookmarkBarModelTestWithProfile); }; // Creates a set of nodes in the bookmark bar model, then recreates the @@ -601,44 +591,113 @@ TEST_F(BookmarkBarModelTestWithProfile, CreateAndRestore) { } } -#ifdef USE_BOOKMARK_CODEC -// Creates a set of nodes in the bookmark bar model, then recreates the -// bookmark bar model which triggers loading from the db and checks the loaded -// structure to make sure it is what we first created. -TEST_F(BookmarkBarModelTest, TestJSONCodec) { - struct TestData { - // Structure of the children of the bookmark bar model node. - const std::wstring bbn_contents; - // Structure of the children of the other node. - const std::wstring other_contents; - } data[] = { - // See PopulateNodeFromString for a description of these strings. - { L"", L"" }, - { L"a", L"b" }, - { L"a [ b ]", L"" }, - { L"", L"[ b ] a [ c [ d e [ f ] ] ]" }, - { L"a [ b ]", L"" }, - { L"a b c [ d e [ f ] ]", L"g h i [ j k [ l ] ]"}, - }; - for (int i = 0; i < arraysize(data); ++i) { - BookmarkBarModel expected_model(NULL); - - TestNode bbn; - PopulateNodeFromString(data[i].bbn_contents, &bbn); - PopulateBookmarkBarNode(&bbn, &expected_model, - expected_model.GetBookmarkBarNode()); - - TestNode other; - PopulateNodeFromString(data[i].other_contents, &other); - PopulateBookmarkBarNode(&other, &expected_model, - expected_model.other_node()); - - BookmarkBarModel actual_model(NULL); - BookmarkCodec codec; - scoped_ptr<Value> encoded_value(codec.Encode(&expected_model)); - codec.Decode(&actual_model, *(encoded_value.get())); +// Test class that creates a BookmarkBarModel with a real history backend. +class BookmarkBarModelTestWithProfile2 : + public BookmarkBarModelTestWithProfile { + public: + virtual void SetUp() { + profile_.reset(new TestingProfile()); + } - AssertModelsEqual(&expected_model, &actual_model); + protected: + // Verifies the state of the model matches that of the state in the saved + // history file. + void VerifyExpectedState() { + // Here's the structure we expect: + // bbn + // www.google.com - Google + // F1 + // http://www.google.com/intl/en/ads/ - Google Advertising + // F11 + // http://www.google.com/services/ - Google Business Solutions + // other + // OF1 + // http://www.google.com/intl/en/about.html - About Google + BookmarkBarNode* bbn = bb_model_->GetBookmarkBarNode(); + ASSERT_EQ(2, bbn->GetChildCount()); + + BookmarkBarNode* child = bbn->GetChild(0); + ASSERT_EQ(history::StarredEntry::URL, child->GetType()); + ASSERT_EQ(L"Google", child->GetTitle()); + ASSERT_TRUE(child->GetURL() == GURL("http://www.google.com")); + + child = bbn->GetChild(1); + ASSERT_TRUE(child->is_folder()); + ASSERT_EQ(L"F1", child->GetTitle()); + ASSERT_EQ(2, child->GetChildCount()); + + BookmarkBarNode* parent = child; + child = parent->GetChild(0); + ASSERT_EQ(history::StarredEntry::URL, child->GetType()); + ASSERT_EQ(L"Google Advertising", child->GetTitle()); + ASSERT_TRUE(child->GetURL() == GURL("http://www.google.com/intl/en/ads/")); + + child = parent->GetChild(1); + ASSERT_TRUE(child->is_folder()); + ASSERT_EQ(L"F11", child->GetTitle()); + ASSERT_EQ(1, child->GetChildCount()); + + parent = child; + child = parent->GetChild(0); + ASSERT_EQ(history::StarredEntry::URL, child->GetType()); + ASSERT_EQ(L"Google Business Solutions", child->GetTitle()); + ASSERT_TRUE(child->GetURL() == GURL("http://www.google.com/services/")); + + parent = bb_model_->other_node(); + ASSERT_EQ(2, parent->GetChildCount()); + + child = parent->GetChild(0); + ASSERT_TRUE(child->is_folder()); + ASSERT_EQ(L"OF1", child->GetTitle()); + ASSERT_EQ(0, child->GetChildCount()); + + child = parent->GetChild(1); + ASSERT_EQ(history::StarredEntry::URL, child->GetType()); + ASSERT_EQ(L"About Google", child->GetTitle()); + ASSERT_TRUE(child->GetURL() == + GURL("http://www.google.com/intl/en/about.html")); + + ASSERT_TRUE(bb_model_->GetNodeByURL(GURL("http://www.google.com")) != NULL); } +}; + +// Tests migrating bookmarks from db into file. This copies an old history db +// file containing bookmarks and make sure they are loaded correctly and +// persisted correctly. +TEST_F(BookmarkBarModelTestWithProfile2, MigrateFromDBToFileTest) { + // Copy db file over that contains starred table. + std::wstring old_history_path; + PathService::Get(chrome::DIR_TEST_DATA, &old_history_path); + file_util::AppendToPath(&old_history_path, L"bookmarks"); + file_util::AppendToPath(&old_history_path, L"History_with_starred"); + std::wstring new_history_path = profile_->GetPath(); + file_util::Delete(new_history_path, true); + file_util::CreateDirectory(new_history_path); + file_util::AppendToPath(&new_history_path, chrome::kHistoryFilename); + file_util::CopyFile(old_history_path, new_history_path); + + // Create the history service making sure it doesn't blow away the file we + // just copied. + profile_->CreateHistoryService(false); + + CreateBookmarkBarModel(); + + // Make sure we loaded OK. + VerifyExpectedState(); + if (HasFatalFailure()) + return; + + // Create again. This time we shouldn't load from history at all. + CreateBookmarkBarModel(); + + // Make sure we loaded OK. + VerifyExpectedState(); + if (HasFatalFailure()) + return; + + // Recreate the history service (with a clean db). Do this just to make sure + // we're loading correctly from the bookmarks file. + profile_->CreateHistoryService(true); + CreateBookmarkBarModel(); + VerifyExpectedState(); } -#endif // USE_BOOKMARK_CODEC |