diff options
author | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-23 16:47:41 +0000 |
---|---|---|
committer | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-23 16:47:41 +0000 |
commit | 776e749879084abf997bed1838737fd0f8bd5a20 (patch) | |
tree | ad594c86d14afa848c1fe34e6d3d44559363fd6e /chrome/browser/bookmarks/bookmark_table_model_unittest.cc | |
parent | 4b2170afea2d38cd98c7ea0e25a5c45151f7471e (diff) | |
download | chromium_src-776e749879084abf997bed1838737fd0f8bd5a20.zip chromium_src-776e749879084abf997bed1838737fd0f8bd5a20.tar.gz chromium_src-776e749879084abf997bed1838737fd0f8bd5a20.tar.bz2 |
Adds models needed by the bookmark manager. Specifically a
BookmarkTableModel, which will be used to show one of the following:
the children of a folder, recently bookmarked or the results of a
search. And the tree model implementation that shows the folders.
BUG=674
TEST=covered by unit tests
Review URL: http://codereview.chromium.org/8063
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@3814 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/bookmarks/bookmark_table_model_unittest.cc')
-rw-r--r-- | chrome/browser/bookmarks/bookmark_table_model_unittest.cc | 296 |
1 files changed, 296 insertions, 0 deletions
diff --git a/chrome/browser/bookmarks/bookmark_table_model_unittest.cc b/chrome/browser/bookmarks/bookmark_table_model_unittest.cc new file mode 100644 index 0000000..ed2b215 --- /dev/null +++ b/chrome/browser/bookmarks/bookmark_table_model_unittest.cc @@ -0,0 +1,296 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/string_util.h" +#include "base/time.h" +#include "chrome/browser/bookmarks/bookmark_table_model.h" +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +#include "generated_resources.h" + +// Base class for bookmark model tests. +// Initial state of the bookmark model is as follows: +// bb +// url1 (t0) +// f1 +// o +// url2 (t0 + 2) +// f2 +// url3 (t0 + 1) +class BookmarkTableModelTest : public testing::Test, + public views::TableModelObserver { + public: + BookmarkTableModelTest() + : url1_("http://1"), + url2_("http://2"), + url3_("http://3"), + changed_count_(0), + item_changed_count_(0), + added_count_(0), + removed_count_(0) { + } + + virtual void SetUp() { + profile_.reset(new TestingProfile()); + profile_->CreateBookmarkModel(true); + // Populate with some default data. + Time t0 = Time::Now(); + BookmarkNode* bb = bookmark_model()->GetBookmarkBarNode(); + bookmark_model()->AddURLWithCreationTime(bb, 0, L"a", url1_, t0); + bookmark_model()->AddGroup(bb, 1, L"f1"); + + BookmarkNode* other = bookmark_model()->other_node(); + bookmark_model()->AddURLWithCreationTime(other, 0, L"b", + url2_, t0 + TimeDelta::FromDays(2)); + bookmark_model()->AddGroup(other, 1, L"f2"); + bookmark_model()->AddURLWithCreationTime(other, 2, L"c", url3_, + t0 + TimeDelta::FromDays(1)); + } + + virtual void TearDown() { + model_.reset(NULL); + profile_.reset(NULL); + } + + BookmarkModel* bookmark_model() const { + return profile_->GetBookmarkModel(); + } + + virtual void OnModelChanged() { + changed_count_++; + } + + virtual void OnItemsChanged(int start, int length) { + item_changed_count_++; + } + + virtual void OnItemsAdded(int start, int length) { + added_count_++; + } + + virtual void OnItemsRemoved(int start, int length) { + removed_count_++; + } + + void VerifyAndClearOberserverCounts(int changed_count, int item_changed_count, + int added_count, int removed_count) { + EXPECT_EQ(changed_count, changed_count_); + EXPECT_EQ(item_changed_count, item_changed_count_); + EXPECT_EQ(added_count, added_count_); + EXPECT_EQ(removed_count, removed_count_); + ResetCounts(); + } + + void ResetCounts() { + changed_count_ = item_changed_count_ = removed_count_ = added_count_ = 0; + } + + void SetModel(BookmarkTableModel* model) { + if (model_.get()) + model_->SetObserver(NULL); + model_.reset(model); + if (model_.get()) + model_->SetObserver(this); + } + + scoped_ptr<BookmarkTableModel> model_; + + const GURL url1_; + const GURL url2_; + const GURL url3_; + + private: + int changed_count_; + int item_changed_count_; + int added_count_; + int removed_count_; + scoped_ptr<TestingProfile> profile_; +}; + +// Verifies the count when showing various nodes. +TEST_F(BookmarkTableModelTest, FolderInitialState) { + SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( + bookmark_model(), bookmark_model()->GetBookmarkBarNode())); + ASSERT_EQ(2, model_->RowCount()); + EXPECT_TRUE(bookmark_model()->GetBookmarkBarNode()->GetChild(0) == + model_->GetNodeForRow(0)); + EXPECT_TRUE(bookmark_model()->GetBookmarkBarNode()->GetChild(1) == + model_->GetNodeForRow(1)); + + SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( + bookmark_model(), bookmark_model()->other_node())); + EXPECT_EQ(3, model_->RowCount()); +} + +// Verifies adding an item to folder model generates the correct event. +TEST_F(BookmarkTableModelTest, AddToFolder) { + BookmarkNode* other = bookmark_model()->other_node(); + SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( + bookmark_model(), other)); + BookmarkNode* new_node = bookmark_model()->AddURL(other, 0, L"new", url1_); + // Should have gotten notification of the add. + VerifyAndClearOberserverCounts(0, 0, 1, 0); + ASSERT_EQ(4, model_->RowCount()); + EXPECT_TRUE(new_node == model_->GetNodeForRow(0)); + + // Add to the bookmark bar, this shouldn't generate an event. + bookmark_model()->AddURL(bookmark_model()->GetBookmarkBarNode(), 0, L"new", + url1_); + VerifyAndClearOberserverCounts(0, 0, 0, 0); +} + +// Verifies removing an item from folder model generates the correct event. +TEST_F(BookmarkTableModelTest, RemoveFromFolder) { + BookmarkNode* other = bookmark_model()->other_node(); + SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( + bookmark_model(), other)); + bookmark_model()->Remove(other, 0); + + // Should have gotten notification of the remove. + VerifyAndClearOberserverCounts(0, 0, 0, 1); + EXPECT_EQ(2, model_->RowCount()); + + // Remove from the bookmark bar, this shouldn't generate an event. + bookmark_model()->Remove(bookmark_model()->GetBookmarkBarNode(), 0); + VerifyAndClearOberserverCounts(0, 0, 0, 0); +} + +// Verifies changing an item in the folder model generates the correct event. +TEST_F(BookmarkTableModelTest, ChangeFolder) { + BookmarkNode* other = bookmark_model()->other_node(); + SetModel(BookmarkTableModel::CreateBookmarkTableModelForFolder( + bookmark_model(), other)); + bookmark_model()->SetTitle(other->GetChild(0), L"new"); + + // Should have gotten notification of the change. + VerifyAndClearOberserverCounts(0, 1, 0, 0); + EXPECT_EQ(3, model_->RowCount()); + + // Change a node in the bookmark bar, this shouldn't generate an event. + bookmark_model()->SetTitle( + bookmark_model()->GetBookmarkBarNode()->GetChild(0), L"new2"); + VerifyAndClearOberserverCounts(0, 0, 0, 0); +} + +// Verifies show recently added shows the recently added, in order. +TEST_F(BookmarkTableModelTest, RecentlyBookmarkedOrder) { + SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model())); + EXPECT_EQ(3, model_->RowCount()); + + BookmarkNode* bb = bookmark_model()->GetBookmarkBarNode(); + BookmarkNode* other = bookmark_model()->other_node(); + EXPECT_TRUE(other->GetChild(0) == model_->GetNodeForRow(0)); + EXPECT_TRUE(other->GetChild(2) == model_->GetNodeForRow(1)); + EXPECT_TRUE(bb->GetChild(0) == model_->GetNodeForRow(2)); +} + +// Verifies adding an item to recently added notifies observer. +TEST_F(BookmarkTableModelTest, AddToRecentlyBookmarked) { + SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model())); + bookmark_model()->AddURL(bookmark_model()->other_node(), 0, L"new", url1_); + // Should have gotten notification of the add. + VerifyAndClearOberserverCounts(1, 0, 0, 0); + EXPECT_EQ(4, model_->RowCount()); + + // Add a folder, this shouldn't change the model. + bookmark_model()->AddGroup(bookmark_model()->GetBookmarkBarNode(), 0, L"new"); + VerifyAndClearOberserverCounts(0, 0, 0, 0); +} + +// Verifies removing an item from recently added notifies observer. +TEST_F(BookmarkTableModelTest, RemoveFromRecentlyBookmarked) { + SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model())); + bookmark_model()->Remove(bookmark_model()->other_node(), 0); + // Should have gotten notification of the remove. + VerifyAndClearOberserverCounts(1, 0, 0, 0); + EXPECT_EQ(2, model_->RowCount()); + + // Remove a folder, this shouldn't change the model. + bookmark_model()->Remove(bookmark_model()->other_node(), 0); + VerifyAndClearOberserverCounts(0, 0, 0, 0); +} + +// Verifies changing an item in recently added notifies observer. +TEST_F(BookmarkTableModelTest, ChangeRecentlyBookmarked) { + SetModel(BookmarkTableModel::CreateRecentlyBookmarkedModel(bookmark_model())); + bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(0), + L"new"); + // Should have gotten notification of the change. + VerifyAndClearOberserverCounts(0, 1, 0, 0); + + // Change a folder, this shouldn't change the model. + bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(1), + L"new"); + VerifyAndClearOberserverCounts(0, 0, 0, 0); +} + +// Verifies search finds the correct bookmarks. +TEST_F(BookmarkTableModelTest, Search) { + SetModel(BookmarkTableModel::CreateSearchTableModel(bookmark_model(), L"c")); + ASSERT_EQ(1, model_->RowCount()); + EXPECT_TRUE(bookmark_model()->other_node()->GetChild(2) == + model_->GetNodeForRow(0)); + // Make sure IndexOfNode works. + EXPECT_EQ(0, + model_->IndexOfNode(bookmark_model()->other_node()->GetChild(2))); +} + +// Verifies adding an item to search notifies observers. +TEST_F(BookmarkTableModelTest, AddToSearch) { + SetModel(BookmarkTableModel::CreateSearchTableModel(bookmark_model(), L"c")); + BookmarkNode* new_node = + bookmark_model()->AddURL(bookmark_model()->other_node(), 0, L"c", url1_); + // Should have gotten notification of the add. + VerifyAndClearOberserverCounts(0, 0, 1, 0); + ASSERT_EQ(2, model_->RowCount()); + // New node should have gone to end. + EXPECT_TRUE(model_->GetNodeForRow(1) == new_node); + + // Add a folder, this shouldn't change the model. + bookmark_model()->AddGroup(bookmark_model()->GetBookmarkBarNode(), 0, L"new"); + VerifyAndClearOberserverCounts(0, 0, 0, 0); + EXPECT_EQ(2, model_->RowCount()); + + // Add a url that doesn't match search, this shouldn't change the model. + bookmark_model()->AddGroup(bookmark_model()->GetBookmarkBarNode(), 0, L"new"); + VerifyAndClearOberserverCounts(0, 0, 0, 0); + EXPECT_EQ(2, model_->RowCount()); +} + +// Verifies removing an item updates search. +TEST_F(BookmarkTableModelTest, RemoveFromSearch) { + SetModel(BookmarkTableModel::CreateSearchTableModel(bookmark_model(), L"c")); + bookmark_model()->Remove(bookmark_model()->other_node(), 2); + // Should have gotten notification of the remove. + VerifyAndClearOberserverCounts(0, 0, 0, 1); + EXPECT_EQ(0, model_->RowCount()); + + // Remove a folder, this shouldn't change the model. + bookmark_model()->Remove(bookmark_model()->other_node(), 1); + VerifyAndClearOberserverCounts(0, 0, 0, 0); + + // Remove another url that isn't in the model, this shouldn't change anything. + bookmark_model()->Remove(bookmark_model()->other_node(), 0); + VerifyAndClearOberserverCounts(0, 0, 0, 0); +} + +// Verifies changing an item in search notifies observer. +TEST_F(BookmarkTableModelTest, ChangeSearch) { + SetModel(BookmarkTableModel::CreateSearchTableModel(bookmark_model(), L"c")); + bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(2), + L"new"); + // Should have gotten notification of the change. + VerifyAndClearOberserverCounts(0, 1, 0, 0); + + // Change a folder, this shouldn't change the model. + bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(1), + L"new"); + VerifyAndClearOberserverCounts(0, 0, 0, 0); + + // Change a url that isn't in the model, this shouldn't send change. + bookmark_model()->SetTitle(bookmark_model()->other_node()->GetChild(0), + L"new"); + VerifyAndClearOberserverCounts(0, 0, 0, 0); +} |