OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | |
5 #include "base/string16.h" | 6 #include "base/string16.h" |
6 #include "base/string_number_conversions.h" | |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "ui/base/models/tree_node_model.h" | 9 #include "ui/base/models/tree_node_model.h" |
10 | 10 |
11 namespace ui { | 11 namespace ui { |
12 | 12 |
13 class TreeNodeModelTest : public testing::Test, public TreeModelObserver { | 13 class TreeNodeModelTest : public testing::Test, public TreeModelObserver { |
14 public: | 14 public: |
15 TreeNodeModelTest() | 15 TreeNodeModelTest() |
16 : added_count_(0), | 16 : added_count_(0), |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 | 120 |
121 ASSERT_EQ(0, model.GetChildCount(root)); | 121 ASSERT_EQ(0, model.GetChildCount(root)); |
122 } | 122 } |
123 | 123 |
124 // Verify if the nodes added under the root are all deleted when calling | 124 // Verify if the nodes added under the root are all deleted when calling |
125 // RemoveAll. Note that is responsability of the caller to free the memory | 125 // RemoveAll. Note that is responsability of the caller to free the memory |
126 // of the nodes removed after RemoveAll is called. | 126 // of the nodes removed after RemoveAll is called. |
127 // The tree looks like this: | 127 // The tree looks like this: |
128 // root | 128 // root |
129 // |-- child1 | 129 // |-- child1 |
130 // | |-- foo1 | 130 // | |-- foo |
131 // | |-- child0 | 131 // | |-- bar0 |
132 // | |-- child1 | 132 // | |-- bar1 |
133 // +-------|-- child2 | 133 // | |-- bar2 |
134 // |-- child2 | |
135 // +-- child3 | |
134 TEST_F(TreeNodeModelTest, RemoveAllNodes) { | 136 TEST_F(TreeNodeModelTest, RemoveAllNodes) { |
135 TreeNodeWithValue<int>* root = | 137 TreeNodeWithValue<int> root; |
136 new TreeNodeWithValue<int>(ASCIIToUTF16("root"), 0); | |
137 TreeNodeModel<TreeNodeWithValue<int> > model(root); | |
138 model.AddObserver(this); | |
139 ClearCounts(); | |
140 | 138 |
141 // Create the first child node. | 139 TreeNodeWithValue<int> child1; |
142 TreeNodeWithValue<int>* child1 = | 140 TreeNodeWithValue<int> child2; |
143 new TreeNodeWithValue<int>(ASCIIToUTF16("child 1"), 1); | 141 TreeNodeWithValue<int> child3; |
144 model.Add(root, child1, 0); | |
145 | 142 |
146 TreeNodeWithValue<int>* foo1 = | 143 root.Add(&child1, 0); |
147 new TreeNodeWithValue<int>(ASCIIToUTF16("foo1"), 2); | 144 root.Add(&child2, 1); |
148 model.Add(child1, foo1, 0); | 145 root.Add(&child3, 2); |
149 | 146 |
150 // Add some nodes to |foo1|. | 147 TreeNodeWithValue<int>* foo = new TreeNodeWithValue<int>(2); |
151 for (int i = 0; i < 3; ++i) { | 148 child1.Add(foo, 0); |
152 model.Add(foo1, | |
153 new TreeNodeWithValue<int>(ASCIIToUTF16("child") + | |
154 base::IntToString16(i), i), i); | |
155 } | |
156 | 149 |
157 ASSERT_EQ(3, model.GetChildCount(foo1)); | 150 // Add some nodes to |foo|. |
151 for (int i = 0; i < 3; ++i) | |
152 foo->Add(new TreeNodeWithValue<int>(i), i); | |
153 | |
154 ASSERT_EQ(3, root.child_count()); | |
155 ASSERT_EQ(1, child1.child_count()); | |
156 ASSERT_EQ(3, foo->child_count()); | |
158 | 157 |
159 // Now remove all nodes from root. | 158 // Now remove all nodes from root. |
160 root->RemoveAll(); | 159 root.RemoveAll(); |
161 | 160 |
162 // Release memory, so we don't leak. | 161 ASSERT_EQ(0, root.child_count()); |
163 delete child1; | 162 ASSERT_TRUE(root.empty()); |
sky
2011/06/10 15:45:59
You should add assertions that child1 & foo still
tfarina
2011/06/10 16:05:07
Done.
| |
164 | |
165 ASSERT_EQ(0, model.GetChildCount(root)); | |
166 } | 163 } |
167 | 164 |
168 // Verify if the model returns correct indexes for the specified nodes. | 165 // Verify if the model returns correct indexes for the specified nodes. |
169 // The tree looks like this: | 166 // The tree looks like this: |
170 // root | 167 // root |
171 // |-- child1 | 168 // |-- child1 |
172 // | |-- foo1 | 169 // | |-- foo1 |
173 // +-- child2 | 170 // +-- child2 |
174 TEST_F(TreeNodeModelTest, GetIndexOf) { | 171 TEST_F(TreeNodeModelTest, GetIndexOf) { |
175 TreeNodeWithValue<int> root; | 172 TreeNodeWithValue<int> root; |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 TEST_F(TreeNodeModelTest, IsRoot) { | 320 TEST_F(TreeNodeModelTest, IsRoot) { |
324 TreeNodeWithValue<int> root; | 321 TreeNodeWithValue<int> root; |
325 EXPECT_TRUE(root.is_root()); | 322 EXPECT_TRUE(root.is_root()); |
326 | 323 |
327 TreeNodeWithValue<int>* child1 = new TreeNodeWithValue<int>(1); | 324 TreeNodeWithValue<int>* child1 = new TreeNodeWithValue<int>(1); |
328 root.Add(child1, root.child_count()); | 325 root.Add(child1, root.child_count()); |
329 EXPECT_FALSE(child1->is_root()); | 326 EXPECT_FALSE(child1->is_root()); |
330 } | 327 } |
331 | 328 |
332 } // namespace ui | 329 } // namespace ui |
OLD | NEW |