OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "app/tree_node_model.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 |
| 8 class TreeNodeModelTest : public testing::Test, public TreeModelObserver { |
| 9 public: |
| 10 TreeNodeModelTest() |
| 11 : added_count_(0), |
| 12 removed_count_(0), |
| 13 reordered_count_(0), |
| 14 changed_count_(0) {} |
| 15 |
| 16 void AssertObserverCount(int added_count, int removed_count, |
| 17 int reordered_count, int changed_count) { |
| 18 ASSERT_EQ(added_count, added_count_); |
| 19 ASSERT_EQ(removed_count, removed_count_); |
| 20 ASSERT_EQ(reordered_count, reordered_count_); |
| 21 ASSERT_EQ(changed_count, changed_count_); |
| 22 } |
| 23 |
| 24 void ClearCounts() { |
| 25 added_count_ = removed_count_ = reordered_count_ = changed_count_ = 0; |
| 26 } |
| 27 |
| 28 // Begin TreeModelObserver implementation. |
| 29 virtual void TreeNodesAdded(TreeModel* model, TreeModelNode* parent, |
| 30 int start, int count) { |
| 31 added_count_++; |
| 32 } |
| 33 virtual void TreeNodesRemoved(TreeModel* model, TreeModelNode* parent, |
| 34 int start, int count) { |
| 35 removed_count_++; |
| 36 } |
| 37 virtual void TreeNodeChildrenReordered(TreeModel* model, |
| 38 TreeModelNode* parent) { |
| 39 reordered_count_++; |
| 40 } |
| 41 virtual void TreeNodeChanged(TreeModel* model, TreeModelNode* node) { |
| 42 changed_count_++; |
| 43 } |
| 44 // End TreeModelObserver implementation. |
| 45 |
| 46 private: |
| 47 int added_count_; |
| 48 int removed_count_; |
| 49 int reordered_count_; |
| 50 int changed_count_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(TreeNodeModelTest); |
| 53 }; |
| 54 |
| 55 // Verify if the model is properly adding a new node in the tree and |
| 56 // notifying the observers. |
| 57 // The tree looks like this: |
| 58 // root |
| 59 // |-- child1 |
| 60 // | |-- foo1 |
| 61 // | |-- foo2 |
| 62 // +-- child2 |
| 63 TEST_F(TreeNodeModelTest, AddNode) { |
| 64 TreeNodeWithValue<int>* root = |
| 65 new TreeNodeWithValue<int>(L"root", 0); |
| 66 TreeNodeModel<TreeNodeWithValue<int> > model(root); |
| 67 model.AddObserver(this); |
| 68 ClearCounts(); |
| 69 |
| 70 // Create the first root child. |
| 71 TreeNodeWithValue<int>* child1 = |
| 72 new TreeNodeWithValue<int>(L"child 1", 1); |
| 73 model.Add(root, 0, child1); |
| 74 |
| 75 AssertObserverCount(1, 0, 0, 0); |
| 76 |
| 77 // Add two nodes under the |child1|. |
| 78 TreeNodeWithValue<int>* foo1 = |
| 79 new TreeNodeWithValue<int>(L"foo1", 3); |
| 80 TreeNodeWithValue<int>* foo2 = |
| 81 new TreeNodeWithValue<int>(L"foo2", 4); |
| 82 child1->Add(0, foo1); |
| 83 child1->Add(1, foo2); |
| 84 |
| 85 // Create the second root child. |
| 86 TreeNodeWithValue<int>* child2 = |
| 87 new TreeNodeWithValue<int>(L"child 2", 2); |
| 88 root->Add(1, child2); |
| 89 |
| 90 // Check if there is two nodes under the root. |
| 91 ASSERT_EQ(2, model.GetChildCount(root)); |
| 92 |
| 93 // Check if there is two nodes under |child1|. |
| 94 ASSERT_EQ(2, model.GetChildCount(child1)); |
| 95 |
| 96 // Check if there is none nodes under |child2|. |
| 97 ASSERT_EQ(0, model.GetChildCount(child2)); |
| 98 } |
| 99 |
| 100 // Verify if the model is properly removing a node from the tree |
| 101 // and notifying the observers. |
| 102 TEST_F(TreeNodeModelTest, RemoveNode) { |
| 103 TreeNodeWithValue<int>* root = |
| 104 new TreeNodeWithValue<int>(L"root", 0); |
| 105 TreeNodeModel<TreeNodeWithValue<int> > model(root); |
| 106 model.AddObserver(this); |
| 107 ClearCounts(); |
| 108 |
| 109 // Create the first child node. |
| 110 TreeNodeWithValue<int>* child1 = |
| 111 new TreeNodeWithValue<int>(L"child 1", 1); |
| 112 |
| 113 // And add it to the root node. |
| 114 root->Add(0, child1); |
| 115 |
| 116 ASSERT_EQ(1, model.GetChildCount(root)); |
| 117 |
| 118 // Now remove the |child1| from root and release the memory. |
| 119 delete model.Remove(root, 0); |
| 120 |
| 121 AssertObserverCount(0, 1, 0, 0); |
| 122 |
| 123 ASSERT_EQ(0, model.GetChildCount(root)); |
| 124 } |
| 125 |
| 126 // Verify if the nodes added under the root are all deleted when calling |
| 127 // RemoveAll. Note that is responsability of the caller to free the memory |
| 128 // of the nodes removed after RemoveAll is called. |
| 129 // The tree looks like this: |
| 130 // root |
| 131 // |-- child1 |
| 132 // | |-- foo1 |
| 133 // | |-- child0 |
| 134 // | |-- child1 |
| 135 // +-------|-- child2 |
| 136 TEST_F(TreeNodeModelTest, RemoveAllNodes) { |
| 137 TreeNodeWithValue<int>* root = |
| 138 new TreeNodeWithValue<int>(L"root", 0); |
| 139 TreeNodeModel<TreeNodeWithValue<int> > model(root); |
| 140 model.AddObserver(this); |
| 141 ClearCounts(); |
| 142 |
| 143 // Create the first child node. |
| 144 TreeNodeWithValue<int>* child1 = |
| 145 new TreeNodeWithValue<int>(L"child 1", 1); |
| 146 model.Add(root, 0, child1); |
| 147 |
| 148 TreeNodeWithValue<int>* foo1 = |
| 149 new TreeNodeWithValue<int>(L"foo1", 2); |
| 150 model.Add(child1, 0, foo1); |
| 151 |
| 152 // Add some nodes to |foo1|. |
| 153 for (int i = 0; i < 3; ++i) |
| 154 model.Add(foo1, i, new TreeNodeWithValue<int>(L"child" + i, i)); |
| 155 |
| 156 ASSERT_EQ(3, model.GetChildCount(foo1)); |
| 157 |
| 158 // Now remove all nodes from root. |
| 159 root->RemoveAll(); |
| 160 |
| 161 // Release memory, so we don't leak. |
| 162 delete child1; |
| 163 |
| 164 ASSERT_EQ(0, model.GetChildCount(root)); |
| 165 } |
| 166 |
| 167 // Verify if the model returns correct indexes for the specified nodes. |
| 168 // The tree looks like this: |
| 169 // root |
| 170 // |-- child1 |
| 171 // | |-- foo1 |
| 172 // +-- child2 |
| 173 TEST_F(TreeNodeModelTest, IndexOfChild) { |
| 174 TreeNodeWithValue<int>* root = |
| 175 new TreeNodeWithValue<int>(L"root", 0); |
| 176 TreeNodeModel<TreeNodeWithValue<int> > model(root); |
| 177 model.AddObserver(this); |
| 178 ClearCounts(); |
| 179 |
| 180 TreeNodeWithValue<int>* child1 = |
| 181 new TreeNodeWithValue<int>(L"child 1", 1); |
| 182 model.Add(root, 0, child1); |
| 183 |
| 184 TreeNodeWithValue<int>* child2 = |
| 185 new TreeNodeWithValue<int>(L"child 2", 2); |
| 186 model.Add(root, 1, child2); |
| 187 |
| 188 TreeNodeWithValue<int>* foo1 = |
| 189 new TreeNodeWithValue<int>(L"foo1", 0); |
| 190 model.Add(child1, 0, foo1); |
| 191 |
| 192 ASSERT_EQ(0, model.IndexOfChild(root, child1)); |
| 193 ASSERT_EQ(1, model.IndexOfChild(root, child2)); |
| 194 ASSERT_EQ(0, model.IndexOfChild(child1, foo1)); |
| 195 ASSERT_EQ(-1, model.IndexOfChild(root, foo1)); |
| 196 } |
| 197 |
| 198 // Verify whether a specified node has or not an ancestor. |
| 199 // The tree looks like this: |
| 200 // root |
| 201 // |-- child1 |
| 202 // |-- child2 |
| 203 TEST_F(TreeNodeModelTest, HasAncestor) { |
| 204 TreeNodeWithValue<int>* root = |
| 205 new TreeNodeWithValue<int>(L"root", 0); |
| 206 TreeNodeModel<TreeNodeWithValue<int> > model(root); |
| 207 |
| 208 TreeNodeWithValue<int>* child1 = |
| 209 new TreeNodeWithValue<int>(L"child 1", 0); |
| 210 model.Add(root, 0, child1); |
| 211 |
| 212 TreeNodeWithValue<int>* child2 = |
| 213 new TreeNodeWithValue<int>(L"child 2", 1); |
| 214 model.Add(root, 1, child2); |
| 215 |
| 216 ASSERT_TRUE(root->HasAncestor(root)); |
| 217 ASSERT_FALSE(root->HasAncestor(child1)); |
| 218 ASSERT_TRUE(child1->HasAncestor(root)); |
| 219 ASSERT_FALSE(child1->HasAncestor(child2)); |
| 220 ASSERT_FALSE(child2->HasAncestor(child1)); |
| 221 } |
| 222 |
| 223 // The tree looks like this: |
| 224 // root |
| 225 // |-- child1 |
| 226 // | |-- child2 |
| 227 // | |-- child3 |
| 228 // |-- foo1 |
| 229 // | |-- foo2 |
| 230 // | |-- foo3 |
| 231 // | |-- foo4 |
| 232 // +-- bar1 |
| 233 // |
| 234 // The TotalNodeCount of root is: 9 |
| 235 // The TotalNodeCount of child1 is: 3 |
| 236 // The TotalNodeCount of bar1 is: 1 |
| 237 // And so on... |
| 238 // The purpose here is to verify if the function returns the total of nodes |
| 239 // under the specifed node correctly. The count should include the node it self. |
| 240 TEST_F(TreeNodeModelTest, GetTotalNodeCount) { |
| 241 TreeNodeWithValue<int>* root = |
| 242 new TreeNodeWithValue<int>(L"root", 0); |
| 243 TreeNodeModel<TreeNodeWithValue<int> > model(root); |
| 244 |
| 245 TreeNodeWithValue<int>* child1 = |
| 246 new TreeNodeWithValue<int>(L"child1", 1); |
| 247 TreeNodeWithValue<int>* child2 = |
| 248 new TreeNodeWithValue<int>(L"child2", 2); |
| 249 TreeNodeWithValue<int>* child3 = |
| 250 new TreeNodeWithValue<int>(L"child3", 3); |
| 251 TreeNodeWithValue<int>* foo1 = |
| 252 new TreeNodeWithValue<int>(L"foo1", 4); |
| 253 TreeNodeWithValue<int>* foo2 = |
| 254 new TreeNodeWithValue<int>(L"foo2", 5); |
| 255 TreeNodeWithValue<int>* foo3 = |
| 256 new TreeNodeWithValue<int>(L"foo3", 6); |
| 257 TreeNodeWithValue<int>* foo4 = |
| 258 new TreeNodeWithValue<int>(L"foo4", 7); |
| 259 TreeNodeWithValue<int>* bar1 = |
| 260 new TreeNodeWithValue<int>(L"bar1", 8); |
| 261 |
| 262 model.Add(root, 0, child1); |
| 263 model.Add(child1, 0, child2); |
| 264 model.Add(child2, 0, child3); |
| 265 |
| 266 model.Add(root, 1, foo1); |
| 267 model.Add(foo1, 0, foo2); |
| 268 model.Add(foo1, 1, foo4); |
| 269 model.Add(foo2, 0, foo3); |
| 270 |
| 271 model.Add(root, 0, bar1); |
| 272 |
| 273 ASSERT_EQ(9, root->GetTotalNodeCount()); |
| 274 ASSERT_EQ(3, child1->GetTotalNodeCount()); |
| 275 ASSERT_EQ(1, bar1->GetTotalNodeCount()); |
| 276 ASSERT_EQ(2, foo2->GetTotalNodeCount()); |
| 277 } |
OLD | NEW |