Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / models / tree_node_iterator_unittest.cc
blob4acf89ce5531b6a329e3b964aec70f3e3178bd97
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 "base/bind.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/base/models/tree_node_model.h"
11 namespace ui {
13 namespace {
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) {
26 TestNode root;
27 root.Add(new TestNode(), 0);
28 root.Add(new TestNode(), 1);
29 TestNode* f3 = new TestNode();
30 root.Add(f3, 2);
31 TestNode* f4 = new TestNode();
32 f3->Add(f4, 0);
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:
55 // * + 1
56 // + 2
57 // + 3 + 4 + 5
58 // + 7
59 TEST(TreeNodeIteratorTest, Prune) {
60 TestNode root;
61 root.Add(new TestNode(1), 0);
62 root.Add(new TestNode(2), 1);
63 TestNode* f3 = new TestNode(3);
64 root.Add(f3, 2);
65 TestNode* f4 = new TestNode(4);
66 f3->Add(f4, 0);
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());
85 } // namespace
87 } // namespace ui