Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / base / models / tree_node_iterator_unittest.cc
blob263f8d430fd31d23138642e1b1d4dab5487c59a8
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"
10 namespace ui {
12 namespace {
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) {
25 TestNode root;
26 root.Add(new TestNode(), 0);
27 root.Add(new TestNode(), 1);
28 TestNode* f3 = new TestNode();
29 root.Add(f3, 2);
30 TestNode* f4 = new TestNode();
31 f3->Add(f4, 0);
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:
54 // * + 1
55 // + 2
56 // + 3 + 4 + 5
57 // + 7
58 TEST(TreeNodeIteratorTest, Prune) {
59 TestNode root;
60 root.Add(new TestNode(1), 0);
61 root.Add(new TestNode(2), 1);
62 TestNode* f3 = new TestNode(3);
63 root.Add(f3, 2);
64 TestNode* f4 = new TestNode(4);
65 f3->Add(f4, 0);
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());
84 } // namespace
86 } // namespace ui