1 // Copyright (c) 2011 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.
5 #include "ui/base/models/tree_node_iterator.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/base/models/tree_node_model.h"
15 typedef TreeNodeWithValue
<int> TestNode
;
17 bool PruneOdd(TestNode
* node
) {
18 return node
->value
% 2;
21 bool PruneEven(TestNode
* node
) {
22 return !PruneOdd(node
);
25 TEST(TreeNodeIteratorTest
, Basic
) {
27 root
.Add(new TestNode(), 0);
28 root
.Add(new TestNode(), 1);
29 TestNode
* f3
= new TestNode();
31 TestNode
* f4
= new TestNode();
33 f4
->Add(new TestNode(), 0);
35 TreeNodeIterator
<TestNode
> iterator(&root
);
36 ASSERT_TRUE(iterator
.has_next());
37 ASSERT_EQ(root
.GetChild(0), iterator
.Next());
39 ASSERT_TRUE(iterator
.has_next());
40 ASSERT_EQ(root
.GetChild(1), iterator
.Next());
42 ASSERT_TRUE(iterator
.has_next());
43 ASSERT_EQ(root
.GetChild(2), iterator
.Next());
45 ASSERT_TRUE(iterator
.has_next());
46 ASSERT_EQ(f4
, iterator
.Next());
48 ASSERT_TRUE(iterator
.has_next());
49 ASSERT_EQ(f4
->GetChild(0), iterator
.Next());
51 ASSERT_FALSE(iterator
.has_next());
54 // The tree used for testing:
59 TEST(TreeNodeIteratorTest
, Prune
) {
61 root
.Add(new TestNode(1), 0);
62 root
.Add(new TestNode(2), 1);
63 TestNode
* f3
= new TestNode(3);
65 TestNode
* f4
= new TestNode(4);
67 f4
->Add(new TestNode(5), 0);
68 f3
->Add(new TestNode(7), 1);
70 TreeNodeIterator
<TestNode
> odd_iterator(&root
, base::Bind(&PruneOdd
));
71 ASSERT_TRUE(odd_iterator
.has_next());
72 ASSERT_EQ(2, odd_iterator
.Next()->value
);
73 ASSERT_FALSE(odd_iterator
.has_next());
75 TreeNodeIterator
<TestNode
> even_iterator(&root
, base::Bind(&PruneEven
));
76 ASSERT_TRUE(even_iterator
.has_next());
77 ASSERT_EQ(1, even_iterator
.Next()->value
);
78 ASSERT_TRUE(even_iterator
.has_next());
79 ASSERT_EQ(3, even_iterator
.Next()->value
);
80 ASSERT_TRUE(even_iterator
.has_next());
81 ASSERT_EQ(7, even_iterator
.Next()->value
);
82 ASSERT_FALSE(even_iterator
.has_next());