summaryrefslogtreecommitdiffstats
path: root/app/tree_node_model_unittest.cc
blob: 4621d94fbdd92e60ee57328e81d50851f81c0c80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) 2010 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 "app/tree_node_model.h"
#include "base/string16.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

class TreeNodeModelTest : public testing::Test, public TreeModelObserver {
 public:
  TreeNodeModelTest()
      : added_count_(0),
        removed_count_(0),
        changed_count_(0) {}

  void AssertObserverCount(int added_count, int removed_count,
                           int changed_count) {
    ASSERT_EQ(added_count, added_count_);
    ASSERT_EQ(removed_count, removed_count_);
    ASSERT_EQ(changed_count, changed_count_);
  }

  void ClearCounts() {
    added_count_ = removed_count_ = changed_count_ = 0;
  }

  // Begin TreeModelObserver implementation.
  virtual void TreeNodesAdded(TreeModel* model, TreeModelNode* parent,
                              int start, int count) {
    added_count_++;
  }
  virtual void TreeNodesRemoved(TreeModel* model, TreeModelNode* parent,
                                int start, int count) {
    removed_count_++;
  }
  virtual void TreeNodeChanged(TreeModel* model, TreeModelNode* node) {
    changed_count_++;
  }
  // End TreeModelObserver implementation.

 private:
  int added_count_;
  int removed_count_;
  int changed_count_;

  DISALLOW_COPY_AND_ASSIGN(TreeNodeModelTest);
};

// Verify if the model is properly adding a new node in the tree and
// notifying the observers.
// The tree looks like this:
// root
// |-- child1
// |   |-- foo1
// |   |-- foo2
// +-- child2
TEST_F(TreeNodeModelTest, AddNode) {
  TreeNodeWithValue<int>* root =
      new TreeNodeWithValue<int>(ASCIIToUTF16("root"), 0);
  TreeNodeModel<TreeNodeWithValue<int> > model(root);
  model.AddObserver(this);
  ClearCounts();

  // Create the first root child.
  TreeNodeWithValue<int>* child1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child 1"), 1);
  model.Add(root, 0, child1);

  AssertObserverCount(1, 0, 0);

  // Add two nodes under the |child1|.
  TreeNodeWithValue<int>* foo1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("foo1"), 3);
  TreeNodeWithValue<int>* foo2 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("foo2"), 4);
  child1->Add(0, foo1);
  child1->Add(1, foo2);

  // Create the second root child.
  TreeNodeWithValue<int>* child2 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child 2"), 2);
  root->Add(1, child2);

  // Check if there is two nodes under the root.
  ASSERT_EQ(2, model.GetChildCount(root));

  // Check if there is two nodes under |child1|.
  ASSERT_EQ(2, model.GetChildCount(child1));

  // Check if there is none nodes under |child2|.
  ASSERT_EQ(0, model.GetChildCount(child2));
}

// Verify if the model is properly removing a node from the tree
// and notifying the observers.
TEST_F(TreeNodeModelTest, RemoveNode) {
  TreeNodeWithValue<int>* root =
      new TreeNodeWithValue<int>(ASCIIToUTF16("root"), 0);
  TreeNodeModel<TreeNodeWithValue<int> > model(root);
  model.AddObserver(this);
  ClearCounts();

  // Create the first child node.
  TreeNodeWithValue<int>* child1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child 1"), 1);

  // And add it to the root node.
  root->Add(0, child1);

  ASSERT_EQ(1, model.GetChildCount(root));

  // Now remove the |child1| from root and release the memory.
  delete model.Remove(root, 0);

  AssertObserverCount(0, 1, 0);

  ASSERT_EQ(0, model.GetChildCount(root));
}

// Verify if the nodes added under the root are all deleted when calling
// RemoveAll. Note that is responsability of the caller to free the memory
// of the nodes removed after RemoveAll is called.
// The tree looks like this:
// root
// |-- child1
// |   |-- foo1
// |       |-- child0
// |       |-- child1
// +-------|-- child2
TEST_F(TreeNodeModelTest, RemoveAllNodes) {
  TreeNodeWithValue<int>* root =
      new TreeNodeWithValue<int>(ASCIIToUTF16("root"), 0);
  TreeNodeModel<TreeNodeWithValue<int> > model(root);
  model.AddObserver(this);
  ClearCounts();

  // Create the first child node.
  TreeNodeWithValue<int>* child1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child 1"), 1);
  model.Add(root, 0, child1);

  TreeNodeWithValue<int>* foo1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("foo1"), 2);
  model.Add(child1, 0, foo1);

  // Add some nodes to |foo1|.
  for (int i = 0; i < 3; ++i) {
    model.Add(foo1, i,
              new TreeNodeWithValue<int>(ASCIIToUTF16("child") +
                                             base::IntToString16(i), i));
  }

  ASSERT_EQ(3, model.GetChildCount(foo1));

  // Now remove all nodes from root.
  root->RemoveAll();

  // Release memory, so we don't leak.
  delete child1;

  ASSERT_EQ(0, model.GetChildCount(root));
}

// Verify if the model returns correct indexes for the specified nodes.
// The tree looks like this:
// root
// |-- child1
// |   |-- foo1
// +-- child2
TEST_F(TreeNodeModelTest, IndexOfChild) {
  TreeNodeWithValue<int>* root =
      new TreeNodeWithValue<int>(ASCIIToUTF16("root"), 0);
  TreeNodeModel<TreeNodeWithValue<int> > model(root);
  model.AddObserver(this);
  ClearCounts();

  TreeNodeWithValue<int>* child1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child 1"), 1);
  model.Add(root, 0, child1);

  TreeNodeWithValue<int>* child2 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child 2"), 2);
  model.Add(root, 1, child2);

  TreeNodeWithValue<int>* foo1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("foo1"), 0);
  model.Add(child1, 0, foo1);

  ASSERT_EQ(0, model.IndexOfChild(root, child1));
  ASSERT_EQ(1, model.IndexOfChild(root, child2));
  ASSERT_EQ(0, model.IndexOfChild(child1, foo1));
  ASSERT_EQ(-1, model.IndexOfChild(root, foo1));
}

// Verify whether a specified node has or not an ancestor.
// The tree looks like this:
// root
// |-- child1
// |-- child2
TEST_F(TreeNodeModelTest, HasAncestor) {
  TreeNodeWithValue<int>* root =
      new TreeNodeWithValue<int>(ASCIIToUTF16("root"), 0);
  TreeNodeModel<TreeNodeWithValue<int> > model(root);

  TreeNodeWithValue<int>* child1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child 1"), 0);
  model.Add(root, 0, child1);

  TreeNodeWithValue<int>* child2 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child 2"), 1);
  model.Add(root, 1, child2);

  ASSERT_TRUE(root->HasAncestor(root));
  ASSERT_FALSE(root->HasAncestor(child1));
  ASSERT_TRUE(child1->HasAncestor(root));
  ASSERT_FALSE(child1->HasAncestor(child2));
  ASSERT_FALSE(child2->HasAncestor(child1));
}

// The tree looks like this:
// root
// |-- child1
// |   |-- child2
// |       |-- child3
// |-- foo1
// |   |-- foo2
// |       |-- foo3
// |   |-- foo4
// +-- bar1
//
// The TotalNodeCount of root is:   9
// The TotalNodeCount of child1 is: 3
// The TotalNodeCount of bar1 is:   1
// And so on...
// The purpose here is to verify if the function returns the total of nodes
// under the specifed node correctly. The count should include the node it self.
TEST_F(TreeNodeModelTest, GetTotalNodeCount) {
  TreeNodeWithValue<int>* root =
      new TreeNodeWithValue<int>(ASCIIToUTF16("root"), 0);
  TreeNodeModel<TreeNodeWithValue<int> > model(root);

  TreeNodeWithValue<int>* child1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child1"), 1);
  TreeNodeWithValue<int>* child2 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child2"), 2);
  TreeNodeWithValue<int>* child3 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("child3"), 3);
  TreeNodeWithValue<int>* foo1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("foo1"), 4);
  TreeNodeWithValue<int>* foo2 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("foo2"), 5);
  TreeNodeWithValue<int>* foo3 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("foo3"), 6);
  TreeNodeWithValue<int>* foo4 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("foo4"), 7);
  TreeNodeWithValue<int>* bar1 =
      new TreeNodeWithValue<int>(ASCIIToUTF16("bar1"), 8);

  model.Add(root, 0, child1);
  model.Add(child1, 0, child2);
  model.Add(child2, 0, child3);

  model.Add(root, 1, foo1);
  model.Add(foo1, 0, foo2);
  model.Add(foo1, 1, foo4);
  model.Add(foo2, 0, foo3);

  model.Add(root, 0, bar1);

  ASSERT_EQ(9, root->GetTotalNodeCount());
  ASSERT_EQ(3, child1->GetTotalNodeCount());
  ASSERT_EQ(1, bar1->GetTotalNodeCount());
  ASSERT_EQ(2, foo2->GetTotalNodeCount());
}

// Makes sure that we are notified when the node is renamed,
// also makes sure the node is properly renamed.
TEST_F(TreeNodeModelTest, SetTitle) {
  TreeNodeWithValue<int>* root =
      new TreeNodeWithValue<int>(ASCIIToUTF16("root"), 0);
  TreeNodeModel<TreeNodeWithValue<int> > model(root);
  model.AddObserver(this);
  ClearCounts();

  const string16 title(ASCIIToUTF16("root2"));
  model.SetTitle(root, title);
  AssertObserverCount(0, 0, 1);
  EXPECT_EQ(title, root->GetTitle());
}