• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ui/base/models/tree_node_iterator.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/base/models/tree_node_model.h"
9 
10 namespace ui {
11 
12 namespace {
13 
14 typedef TreeNodeWithValue<int> TestNode;
15 
PruneOdd(TestNode * node)16 bool PruneOdd(TestNode* node) {
17   return node->value % 2;
18 }
19 
PruneEven(TestNode * node)20 bool PruneEven(TestNode* node) {
21   return !PruneOdd(node);
22 }
23 
TEST(TreeNodeIteratorTest,Basic)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);
33 
34   TreeNodeIterator<TestNode> iterator(&root);
35   ASSERT_TRUE(iterator.has_next());
36   ASSERT_EQ(root.GetChild(0), iterator.Next());
37 
38   ASSERT_TRUE(iterator.has_next());
39   ASSERT_EQ(root.GetChild(1), iterator.Next());
40 
41   ASSERT_TRUE(iterator.has_next());
42   ASSERT_EQ(root.GetChild(2), iterator.Next());
43 
44   ASSERT_TRUE(iterator.has_next());
45   ASSERT_EQ(f4, iterator.Next());
46 
47   ASSERT_TRUE(iterator.has_next());
48   ASSERT_EQ(f4->GetChild(0), iterator.Next());
49 
50   ASSERT_FALSE(iterator.has_next());
51 }
52 
53 // The tree used for testing:
54 // * + 1
55 //   + 2
56 //   + 3 + 4 + 5
57 //       + 7
TEST(TreeNodeIteratorTest,Prune)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);
68 
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());
73 
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());
82 }
83 
84 }  // namespace
85 
86 }  // namespace ui
87