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"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/base/models/tree_node_model.h"
14 typedef TreeNodeWithValue
<int> TestNode
;
16 bool PruneOdd(TestNode
* node
) {
17 return node
->value
% 2;
20 bool PruneEven(TestNode
* node
) {
21 return !PruneOdd(node
);
24 TEST(TreeNodeIteratorTest
, Basic
) {
26 root
.Add(new TestNode(), 0);
27 root
.Add(new TestNode(), 1);
28 TestNode
* f3
= new TestNode();
30 TestNode
* f4
= new TestNode();
32 f4
->Add(new TestNode(), 0);
34 TreeNodeIterator
<TestNode
> iterator(&root
);
35 ASSERT_TRUE(iterator
.has_next());
36 ASSERT_EQ(root
.GetChild(0), iterator
.Next());
38 ASSERT_TRUE(iterator
.has_next());
39 ASSERT_EQ(root
.GetChild(1), iterator
.Next());
41 ASSERT_TRUE(iterator
.has_next());
42 ASSERT_EQ(root
.GetChild(2), iterator
.Next());
44 ASSERT_TRUE(iterator
.has_next());
45 ASSERT_EQ(f4
, iterator
.Next());
47 ASSERT_TRUE(iterator
.has_next());
48 ASSERT_EQ(f4
->GetChild(0), iterator
.Next());
50 ASSERT_FALSE(iterator
.has_next());
53 // The tree used for testing:
58 TEST(TreeNodeIteratorTest
, Prune
) {
60 root
.Add(new TestNode(1), 0);
61 root
.Add(new TestNode(2), 1);
62 TestNode
* f3
= new TestNode(3);
64 TestNode
* f4
= new TestNode(4);
66 f4
->Add(new TestNode(5), 0);
67 f3
->Add(new TestNode(7), 1);
69 TreeNodeIterator
<TestNode
> odd_iterator(&root
, PruneOdd
);
70 ASSERT_TRUE(odd_iterator
.has_next());
71 ASSERT_EQ(2, odd_iterator
.Next()->value
);
72 ASSERT_FALSE(odd_iterator
.has_next());
74 TreeNodeIterator
<TestNode
> even_iterator(&root
, PruneEven
);
75 ASSERT_TRUE(even_iterator
.has_next());
76 ASSERT_EQ(1, even_iterator
.Next()->value
);
77 ASSERT_TRUE(even_iterator
.has_next());
78 ASSERT_EQ(3, even_iterator
.Next()->value
);
79 ASSERT_TRUE(even_iterator
.has_next());
80 ASSERT_EQ(7, even_iterator
.Next()->value
);
81 ASSERT_FALSE(even_iterator
.has_next());