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