File: tree_node_model_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (335 lines) | stat: -rw-r--r-- 10,471 bytes parent folder | download | duplicates (9)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/models/tree_node_model.h"

#include <memory>
#include <string>

#include "base/compiler_specific.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::ASCIIToUTF16;

namespace ui {

class TreeNodeModelTest : public testing::Test, public TreeModelObserver {
 public:
  TreeNodeModelTest() = default;

  TreeNodeModelTest(const TreeNodeModelTest&) = delete;
  TreeNodeModelTest& operator=(const TreeNodeModelTest&) = delete;

  ~TreeNodeModelTest() override = default;

 protected:
  std::string GetObserverCountStateAndClear() {
    std::string result(base::StringPrintf("added=%d removed=%d changed=%d",
        added_count_, removed_count_, changed_count_));
    added_count_ = removed_count_ = changed_count_ = 0;
    return result;
  }

 private:
  // Overridden from TreeModelObserver:
  void TreeNodeAdded(TreeModel* model,
                     TreeModelNode* parent,
                     size_t index) override {
    added_count_++;
  }
  void TreeNodeRemoved(TreeModel* model,
                       TreeModelNode* parent,
                       size_t index) override {
    removed_count_++;
  }
  void TreeNodeChanged(TreeModel* model, TreeModelNode* node) override {
    changed_count_++;
  }

  int added_count_ = 0;
  int removed_count_ = 0;
  int changed_count_ = 0;
};

typedef TreeNodeWithValue<int> TestNode;

// Verifies 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) {
  TreeNodeModel<TestNode> model(std::make_unique<TestNode>());
  TestNode* root = model.GetRoot();
  model.AddObserver(this);

  TestNode* child1 = model.Add(root, std::make_unique<TestNode>(), 0);

  EXPECT_EQ("added=1 removed=0 changed=0", GetObserverCountStateAndClear());

  for (size_t i = 0; i < 2; ++i)
    child1->Add(std::make_unique<TestNode>(), i);

  TestNode* child2 = model.Add(root, std::make_unique<TestNode>(), 1);

  EXPECT_EQ("added=1 removed=0 changed=0", GetObserverCountStateAndClear());

  EXPECT_EQ(2u, root->children().size());
  EXPECT_EQ(2u, child1->children().size());
  EXPECT_EQ(0u, child2->children().size());
}

// Verifies if the model is properly removing a node from the tree
// and notifying the observers.
TEST_F(TreeNodeModelTest, RemoveNode) {
  TreeNodeModel<TestNode> model(std::make_unique<TestNode>());
  TestNode* root = model.GetRoot();
  model.AddObserver(this);

  TestNode* child1 = root->Add(std::make_unique<TestNode>(), 0);

  EXPECT_FALSE(root->children().empty());

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

  EXPECT_EQ("added=0 removed=1 changed=0", GetObserverCountStateAndClear());

  EXPECT_TRUE(root->children().empty());
}

// Verifies if the nodes added under the root are all deleted when calling
// DeleteAll.
// The tree looks like this:
// root
// +-- child1
//     +-- foo
//         +-- bar0
//         +-- bar1
//         +-- bar2
// +-- child2
// +-- child3
TEST_F(TreeNodeModelTest, DeleteAllNodes) {
  TestNode root;

  TestNode* child1 = root.Add(std::make_unique<TestNode>(), 0);
  root.Add(std::make_unique<TestNode>(), 1);  // child2
  root.Add(std::make_unique<TestNode>(), 2);  // child3

  TestNode* foo = child1->Add(std::make_unique<TestNode>(), 0);

  // Add some nodes to |foo|.
  for (size_t i = 0; i < 3; ++i)
    foo->Add(std::make_unique<TestNode>(), i);  // bar[n]

  EXPECT_EQ(3u, root.children().size());
  EXPECT_EQ(1u, child1->children().size());
  EXPECT_EQ(3u, foo->children().size());

  // Now remove the child nodes from root.
  root.DeleteAll();

  EXPECT_EQ(0u, root.children().size());
  EXPECT_TRUE(root.children().empty());
}

// Verifies if GetIndexOf() returns the correct index for the specified node.
// The tree looks like this:
// root
// +-- child1
//     +-- foo1
// +-- child2
TEST_F(TreeNodeModelTest, GetIndexOf) {
  TestNode root;

  TestNode* child1 = root.Add(std::make_unique<TestNode>(), 0);
  TestNode* child2 = root.Add(std::make_unique<TestNode>(), 1);
  TestNode* foo1 = child1->Add(std::make_unique<TestNode>(), 0);

  EXPECT_FALSE(root.GetIndexOf(&root).has_value());
  EXPECT_EQ(0u, root.GetIndexOf(child1));
  EXPECT_EQ(1u, root.GetIndexOf(child2));
  EXPECT_FALSE(root.GetIndexOf(foo1).has_value());

  EXPECT_FALSE(child1->GetIndexOf(&root).has_value());
  EXPECT_FALSE(child1->GetIndexOf(child1).has_value());
  EXPECT_FALSE(child1->GetIndexOf(child2).has_value());
  EXPECT_EQ(0u, child1->GetIndexOf(foo1));

  EXPECT_FALSE(child2->GetIndexOf(&root).has_value());
  EXPECT_FALSE(child2->GetIndexOf(child2).has_value());
  EXPECT_FALSE(child2->GetIndexOf(child1).has_value());
  EXPECT_FALSE(child2->GetIndexOf(foo1).has_value());
}

// Verifies whether a specified node has or not an ancestor.
// The tree looks like this:
// root
// +-- child1
//     +-- foo1
// +-- child2
TEST_F(TreeNodeModelTest, HasAncestor) {
  TestNode root;

  TestNode* child1 = root.Add(std::make_unique<TestNode>(), 0);
  TestNode* child2 = root.Add(std::make_unique<TestNode>(), 1);

  TestNode* foo1 = child1->Add(std::make_unique<TestNode>(), 0);

  EXPECT_TRUE(root.HasAncestor(&root));
  EXPECT_FALSE(root.HasAncestor(child1));
  EXPECT_FALSE(root.HasAncestor(child2));
  EXPECT_FALSE(root.HasAncestor(foo1));

  EXPECT_TRUE(child1->HasAncestor(child1));
  EXPECT_TRUE(child1->HasAncestor(&root));
  EXPECT_FALSE(child1->HasAncestor(child2));
  EXPECT_FALSE(child1->HasAncestor(foo1));

  EXPECT_TRUE(child2->HasAncestor(child2));
  EXPECT_TRUE(child2->HasAncestor(&root));
  EXPECT_FALSE(child2->HasAncestor(child1));
  EXPECT_FALSE(child2->HasAncestor(foo1));

  EXPECT_TRUE(foo1->HasAncestor(foo1));
  EXPECT_TRUE(foo1->HasAncestor(child1));
  EXPECT_TRUE(foo1->HasAncestor(&root));
  EXPECT_FALSE(foo1->HasAncestor(child2));
}

// Verifies if GetTotalNodeCount returns the correct number of nodes from the
// node specified. The count should include the node itself.
// 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 child2 and foo2 is: 2
// The TotalNodeCount of bar1 is:            1
// And so on...
TEST_F(TreeNodeModelTest, GetTotalNodeCount) {
  TestNode root;

  TestNode* child1 = root.Add(std::make_unique<TestNode>(), 0);
  TestNode* child2 = child1->Add(std::make_unique<TestNode>(), 0);
  child2->Add(std::make_unique<TestNode>(), 0);  // child3

  TestNode* foo1 = root.Add(std::make_unique<TestNode>(), 1);
  TestNode* foo2 = foo1->Add(std::make_unique<TestNode>(), 0);
  foo2->Add(std::make_unique<TestNode>(), 0);  // foo3
  foo1->Add(std::make_unique<TestNode>(), 1);  // foo4

  TestNode* bar1 = root.Add(std::make_unique<TestNode>(), 2);

  EXPECT_EQ(9u, root.GetTotalNodeCount());
  EXPECT_EQ(3u, child1->GetTotalNodeCount());
  EXPECT_EQ(2u, child2->GetTotalNodeCount());
  EXPECT_EQ(2u, foo2->GetTotalNodeCount());
  EXPECT_EQ(1u, bar1->GetTotalNodeCount());
}

// Makes sure that we are notified when the node is renamed,
// also makes sure the node is properly renamed.
TEST_F(TreeNodeModelTest, SetTitle) {
  TreeNodeModel<TestNode> model(std::make_unique<TestNode>(u"root", 0));
  TestNode* root = model.GetRoot();
  model.AddObserver(this);

  const std::u16string title(u"root2");
  model.SetTitle(root, title);
  EXPECT_EQ("added=0 removed=0 changed=1", GetObserverCountStateAndClear());
  EXPECT_EQ(title, root->GetTitle());
}

TEST_F(TreeNodeModelTest, BasicOperations) {
  TestNode root;
  EXPECT_EQ(0u, root.children().size());

  TestNode* child1 = root.Add(std::make_unique<TestNode>());
  EXPECT_EQ(1u, root.children().size());
  EXPECT_EQ(&root, child1->parent());

  TestNode* child2 = root.Add(std::make_unique<TestNode>());
  EXPECT_EQ(2u, root.children().size());
  EXPECT_EQ(child1->parent(), child2->parent());

  std::unique_ptr<TestNode> c2 = root.Remove(1);
  EXPECT_EQ(1u, root.children().size());
  EXPECT_EQ(NULL, child2->parent());

  std::unique_ptr<TestNode> c1 = root.Remove(0);
  EXPECT_EQ(0u, root.children().size());
}

TEST_F(TreeNodeModelTest, IsRoot) {
  TestNode root;
  EXPECT_TRUE(root.is_root());

  TestNode* child1 = root.Add(std::make_unique<TestNode>());
  EXPECT_FALSE(child1->is_root());
}

TEST_F(TreeNodeModelTest, ReorderChildren) {
  TestNode root;

  TestNode* child0 = root.Add(std::make_unique<TestNode>(), 0);
  TestNode* child1 = root.Add(std::make_unique<TestNode>(), 1);
  TestNode* child2 = root.Add(std::make_unique<TestNode>(), 2);
  TestNode* child3 = root.Add(std::make_unique<TestNode>(), 3);

  ASSERT_EQ(4u, root.children().size());
  ASSERT_EQ(child0, root.children()[0].get());
  ASSERT_EQ(child1, root.children()[1].get());
  ASSERT_EQ(child2, root.children()[2].get());
  ASSERT_EQ(child3, root.children()[3].get());

  root.ReorderChildren({3, 1, 2, 0});

  ASSERT_EQ(4u, root.children().size());
  EXPECT_EQ(child3, root.children()[0].get());
  EXPECT_EQ(child1, root.children()[1].get());
  EXPECT_EQ(child2, root.children()[2].get());
  EXPECT_EQ(child0, root.children()[3].get());
}

TEST_F(TreeNodeModelTest, SortChildren) {
  TestNode root;

  TestNode* child3 = root.Add(std::make_unique<TestNode>(3), 0);
  TestNode* child1 = root.Add(std::make_unique<TestNode>(1), 1);
  TestNode* child2 = root.Add(std::make_unique<TestNode>(2), 2);
  TestNode* child0 = root.Add(std::make_unique<TestNode>(0), 3);

  ASSERT_EQ(4u, root.children().size());
  ASSERT_EQ(child3, root.children()[0].get());
  ASSERT_EQ(child1, root.children()[1].get());
  ASSERT_EQ(child2, root.children()[2].get());
  ASSERT_EQ(child0, root.children()[3].get());

  root.SortChildren([](const std::unique_ptr<TestNode>& lhs,
                       const std::unique_ptr<TestNode>& rhs) {
    return lhs->value < rhs->value;
  });

  ASSERT_EQ(4u, root.children().size());
  EXPECT_EQ(child0, root.children()[0].get());
  EXPECT_EQ(child1, root.children()[1].get());
  EXPECT_EQ(child2, root.children()[2].get());
  EXPECT_EQ(child3, root.children()[3].get());
}

}  // namespace ui