| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/message_loop.h" | |
| 6 #include "base/string_util.h" | |
| 7 #include "chrome/browser/bookmarks/bookmark_folder_tree_model.h" | |
| 8 #include "chrome/browser/chrome_thread.h" | |
| 9 #include "chrome/test/testing_profile.h" | |
| 10 #include "grit/generated_resources.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "views/controls/tree/tree_view.h" | |
| 13 | |
| 14 // Base class for bookmark model tests. | |
| 15 // Initial state of the bookmark model is as follows: | |
| 16 // bb | |
| 17 // url1 | |
| 18 // f1 | |
| 19 // f11 | |
| 20 // o | |
| 21 // url2 | |
| 22 // f2 | |
| 23 // url3 | |
| 24 // a1 | |
| 25 // g1 | |
| 26 class BookmarkFolderTreeModelTest : public testing::Test, | |
| 27 public TreeModelObserver { | |
| 28 public: | |
| 29 BookmarkFolderTreeModelTest() | |
| 30 : url1_("http://1"), | |
| 31 url2_("http://2"), | |
| 32 url3_("http://3"), | |
| 33 added_count_(0), | |
| 34 removed_count_(0), | |
| 35 changed_count_(0), | |
| 36 reordered_count_(0), | |
| 37 ui_thread_(ChromeThread::UI, &loop_), | |
| 38 file_thread_(ChromeThread::FILE, &loop_) { | |
| 39 } | |
| 40 | |
| 41 virtual void SetUp() { | |
| 42 profile_.reset(new TestingProfile()); | |
| 43 profile_->CreateBookmarkModel(true); | |
| 44 profile_->BlockUntilBookmarkModelLoaded(); | |
| 45 // Populate with some default data. | |
| 46 const BookmarkNode* bb = bookmark_model()->GetBookmarkBarNode(); | |
| 47 bookmark_model()->AddURL(bb, 0, L"url1", url1_); | |
| 48 const BookmarkNode* f1 = bookmark_model()->AddGroup(bb, 1, L"f1"); | |
| 49 bookmark_model()->AddGroup(f1, 0, L"f11"); | |
| 50 | |
| 51 const BookmarkNode* other = bookmark_model()->other_node(); | |
| 52 bookmark_model()->AddURL(other, 0, L"url2", url2_); | |
| 53 bookmark_model()->AddGroup(other, 1, L"f2"); | |
| 54 bookmark_model()->AddURL(other, 2, L"url3", url3_); | |
| 55 | |
| 56 model_.reset(new BookmarkFolderTreeModel(bookmark_model())); | |
| 57 model_->AddObserver(this); | |
| 58 } | |
| 59 | |
| 60 virtual void TearDown() { | |
| 61 model_.reset(NULL); | |
| 62 profile_.reset(NULL); | |
| 63 } | |
| 64 | |
| 65 BookmarkModel* bookmark_model() const { | |
| 66 return profile_->GetBookmarkModel(); | |
| 67 } | |
| 68 | |
| 69 virtual void TreeNodesAdded(TreeModel* model, | |
| 70 TreeModelNode* parent, | |
| 71 int start, | |
| 72 int count) { | |
| 73 added_count_++; | |
| 74 } | |
| 75 | |
| 76 virtual void TreeNodesRemoved(TreeModel* model, | |
| 77 TreeModelNode* parent, | |
| 78 int start, | |
| 79 int count) { | |
| 80 removed_count_++; | |
| 81 } | |
| 82 | |
| 83 virtual void TreeNodeChanged(TreeModel* model, | |
| 84 TreeModelNode* node) { | |
| 85 changed_count_++; | |
| 86 } | |
| 87 | |
| 88 virtual void TreeNodeChildrenReordered(TreeModel* model, | |
| 89 TreeModelNode* parent) { | |
| 90 reordered_count_++; | |
| 91 } | |
| 92 | |
| 93 void VerifyAndClearObserverCounts(int changed_count, | |
| 94 int added_count, | |
| 95 int removed_count, | |
| 96 int reordered_count) { | |
| 97 EXPECT_EQ(changed_count, changed_count_); | |
| 98 EXPECT_EQ(added_count, added_count_); | |
| 99 EXPECT_EQ(removed_count, removed_count_); | |
| 100 EXPECT_EQ(reordered_count, reordered_count_); | |
| 101 ResetCounts(); | |
| 102 } | |
| 103 | |
| 104 void ResetCounts() { | |
| 105 changed_count_ = removed_count_ = added_count_ = reordered_count_ = 0; | |
| 106 } | |
| 107 | |
| 108 scoped_ptr<BookmarkFolderTreeModel> model_; | |
| 109 | |
| 110 const GURL url1_; | |
| 111 const GURL url2_; | |
| 112 const GURL url3_; | |
| 113 | |
| 114 private: | |
| 115 int changed_count_; | |
| 116 int added_count_; | |
| 117 int removed_count_; | |
| 118 int reordered_count_; | |
| 119 scoped_ptr<TestingProfile> profile_; | |
| 120 MessageLoop loop_; | |
| 121 ChromeThread ui_thread_; | |
| 122 ChromeThread file_thread_; | |
| 123 }; | |
| 124 | |
| 125 // Verifies the root node has 4 nodes, and the contents of the bookmark bar | |
| 126 // and other folders matches the initial state. | |
| 127 TEST_F(BookmarkFolderTreeModelTest, InitialState) { | |
| 128 // Verify the first 4 nodes. | |
| 129 TreeModelNode* root = model_->GetRoot(); | |
| 130 ASSERT_EQ(4, model_->GetChildCount(root)); | |
| 131 EXPECT_EQ(BookmarkFolderTreeModel::BOOKMARK, | |
| 132 model_->GetNodeType(model_->GetChild(root, 0))); | |
| 133 EXPECT_EQ(BookmarkFolderTreeModel::BOOKMARK, | |
| 134 model_->GetNodeType(model_->GetChild(root, 1))); | |
| 135 EXPECT_EQ(BookmarkFolderTreeModel::RECENTLY_BOOKMARKED, | |
| 136 model_->GetNodeType(model_->GetChild(root, 2))); | |
| 137 EXPECT_EQ(BookmarkFolderTreeModel::SEARCH, | |
| 138 model_->GetNodeType(model_->GetChild(root, 3))); | |
| 139 | |
| 140 // Verify the contents of the bookmark bar node. | |
| 141 TreeModelNode* bb_node = model_->GetChild(root, 0); | |
| 142 EXPECT_TRUE(model_->TreeNodeAsBookmarkNode(bb_node) == | |
| 143 bookmark_model()->GetBookmarkBarNode()); | |
| 144 ASSERT_EQ(1, model_->GetChildCount(bb_node)); | |
| 145 EXPECT_TRUE(model_->TreeNodeAsBookmarkNode(model_->GetChild(bb_node, 0)) == | |
| 146 bookmark_model()->GetBookmarkBarNode()->GetChild(1)); | |
| 147 | |
| 148 // Verify the contents of the other folders node. | |
| 149 TreeModelNode* other_node = model_->GetChild(root, 1); | |
| 150 EXPECT_TRUE(model_->TreeNodeAsBookmarkNode(other_node) == | |
| 151 bookmark_model()->other_node()); | |
| 152 ASSERT_EQ(1, model_->GetChildCount(other_node)); | |
| 153 EXPECT_TRUE(model_->TreeNodeAsBookmarkNode(model_->GetChild(other_node, 0)) == | |
| 154 bookmark_model()->other_node()->GetChild(1)); | |
| 155 } | |
| 156 | |
| 157 // Removes a URL node and makes sure we don't get any notification. | |
| 158 TEST_F(BookmarkFolderTreeModelTest, RemoveURL) { | |
| 159 bookmark_model()->Remove(bookmark_model()->GetBookmarkBarNode(), 0); | |
| 160 VerifyAndClearObserverCounts(0, 0, 0, 0); | |
| 161 } | |
| 162 | |
| 163 // Changes the title of a URL and makes sure we don't get any notification. | |
| 164 TEST_F(BookmarkFolderTreeModelTest, ChangeURL) { | |
| 165 bookmark_model()->SetTitle( | |
| 166 bookmark_model()->GetBookmarkBarNode()->GetChild(0), L"BLAH"); | |
| 167 VerifyAndClearObserverCounts(0, 0, 0, 0); | |
| 168 } | |
| 169 | |
| 170 // Adds a URL and make sure we don't get notification. | |
| 171 TEST_F(BookmarkFolderTreeModelTest, AddURL) { | |
| 172 bookmark_model()->AddURL( | |
| 173 bookmark_model()->other_node(), 0, L"url1", url1_); | |
| 174 VerifyAndClearObserverCounts(0, 0, 0, 0); | |
| 175 } | |
| 176 | |
| 177 // Removes a folder and makes sure we get the right notification. | |
| 178 TEST_F(BookmarkFolderTreeModelTest, RemoveFolder) { | |
| 179 bookmark_model()->Remove(bookmark_model()->GetBookmarkBarNode(), 1); | |
| 180 VerifyAndClearObserverCounts(0, 0, 1, 0); | |
| 181 // Make sure the node was removed. | |
| 182 EXPECT_EQ(0, model_->GetRoot()->GetChild(0)->GetChildCount()); | |
| 183 } | |
| 184 | |
| 185 // Adds a folder and makes sure we get the right notification. | |
| 186 TEST_F(BookmarkFolderTreeModelTest, AddFolder) { | |
| 187 const BookmarkNode* new_group = | |
| 188 bookmark_model()->AddGroup( | |
| 189 bookmark_model()->GetBookmarkBarNode(), 0, L"fa"); | |
| 190 VerifyAndClearObserverCounts(0, 1, 0, 0); | |
| 191 // Make sure the node was added at the right place. | |
| 192 // Make sure the node was removed. | |
| 193 ASSERT_EQ(2, model_->GetRoot()->GetChild(0)->GetChildCount()); | |
| 194 EXPECT_TRUE(new_group == model_->TreeNodeAsBookmarkNode( | |
| 195 model_->GetRoot()->GetChild(0)->GetChild(0))); | |
| 196 } | |
| 197 | |
| 198 // Changes the title of a folder and makes sure we don't get any notification. | |
| 199 TEST_F(BookmarkFolderTreeModelTest, ChangeFolder) { | |
| 200 bookmark_model()->SetTitle( | |
| 201 bookmark_model()->GetBookmarkBarNode()->GetChild(1)->GetChild(0), | |
| 202 L"BLAH"); | |
| 203 VerifyAndClearObserverCounts(1, 0, 0, 0); | |
| 204 } | |
| 205 | |
| 206 // Sorts the other folder, making sure the resulting order is correct and the | |
| 207 // appropriate notification is sent. | |
| 208 TEST_F(BookmarkFolderTreeModelTest, Sort) { | |
| 209 const BookmarkNode* other = bookmark_model()->other_node(); | |
| 210 bookmark_model()->AddGroup(other, 3, L"a1"); | |
| 211 bookmark_model()->AddGroup(other, 4, L"g1"); | |
| 212 ResetCounts(); | |
| 213 | |
| 214 bookmark_model()->SortChildren(other); | |
| 215 | |
| 216 // Make sure we got notification. | |
| 217 VerifyAndClearObserverCounts(0, 0, 0, 1); | |
| 218 | |
| 219 // Make sure the resulting order matches. | |
| 220 FolderNode* other_folder_node = | |
| 221 model_->GetFolderNodeForBookmarkNode(bookmark_model()->other_node()); | |
| 222 ASSERT_EQ(3, other_folder_node->GetChildCount()); | |
| 223 EXPECT_TRUE(other_folder_node->GetChild(0)->GetTitle() == L"a1"); | |
| 224 EXPECT_TRUE(other_folder_node->GetChild(1)->GetTitle() == L"f2"); | |
| 225 EXPECT_TRUE(other_folder_node->GetChild(2)->GetTitle() == L"g1"); | |
| 226 } | |
| OLD | NEW |