Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(726)

Side by Side Diff: ui/views/controls/tree/tree_view_unittest.cc

Issue 2379863002: Fix object ownership in ui/base/models. (Closed)
Patch Set: fix Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/tree/tree_view.cc ('k') | ui/views/examples/tree_view_example.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/views/controls/tree/tree_view.h" 5 #include "ui/views/controls/tree/tree_view.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h"
10 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "ui/base/models/tree_node_model.h" 13 #include "ui/base/models/tree_node_model.h"
13 #include "ui/views/controls/prefix_selector.h" 14 #include "ui/views/controls/prefix_selector.h"
14 #include "ui/views/controls/textfield/textfield.h" 15 #include "ui/views/controls/textfield/textfield.h"
15 #include "ui/views/test/views_test_base.h" 16 #include "ui/views/test/views_test_base.h"
16 17
17 using ui::TreeModel; 18 using ui::TreeModel;
18 using ui::TreeModelNode; 19 using ui::TreeModelNode;
19 using ui::TreeNode; 20 using ui::TreeNode;
(...skipping 12 matching lines...) Expand all
32 }; 33 };
33 34
34 // Creates the following structure: 35 // Creates the following structure:
35 // 'root' 36 // 'root'
36 // 'a' 37 // 'a'
37 // 'b' 38 // 'b'
38 // 'b1' 39 // 'b1'
39 // 'c' 40 // 'c'
40 class TreeViewTest : public ViewsTestBase { 41 class TreeViewTest : public ViewsTestBase {
41 public: 42 public:
42 TreeViewTest() : model_(new TestNode) { 43 TreeViewTest() : model_(base::MakeUnique<TestNode>()) {
43 static_cast<TestNode*>(model_.GetRoot())->SetTitle(ASCIIToUTF16("root")); 44 static_cast<TestNode*>(model_.GetRoot())->SetTitle(ASCIIToUTF16("root"));
44 Add(model_.GetRoot(), 0, "a"); 45 Add(model_.GetRoot(), 0, "a");
45 Add(Add(model_.GetRoot(), 1, "b"), 0, "b1"); 46 Add(Add(model_.GetRoot(), 1, "b"), 0, "b1");
46 Add(model_.GetRoot(), 2, "c"); 47 Add(model_.GetRoot(), 2, "c");
47 } 48 }
48 49
49 protected: 50 protected:
50 TestNode* Add(TestNode* parent, 51 TestNode* Add(TestNode* parent,
51 int index, 52 int index,
52 const std::string& title); 53 const std::string& title);
53 54
54 std::string TreeViewContentsAsString(); 55 std::string TreeViewContentsAsString();
55 56
56 std::string GetSelectedNodeTitle(); 57 std::string GetSelectedNodeTitle();
57 58
58 std::string GetEditingNodeTitle(); 59 std::string GetEditingNodeTitle();
59 60
60 TestNode* GetNodeByTitle(const std::string& title); 61 TestNode* GetNodeByTitle(const std::string& title);
61 62
62 void IncrementSelection(bool next); 63 void IncrementSelection(bool next);
63 void CollapseOrSelectParent(); 64 void CollapseOrSelectParent();
64 void ExpandOrSelectChild(); 65 void ExpandOrSelectChild();
65 int GetRowCount(); 66 int GetRowCount();
66 PrefixSelector* selector() { return tree_.GetPrefixSelector(); } 67 PrefixSelector* selector() { return tree_.GetPrefixSelector(); }
67 68
68 ui::TreeNodeModel<TestNode > model_; 69 ui::TreeNodeModel<TestNode> model_;
69 TreeView tree_; 70 TreeView tree_;
70 71
71 private: 72 private:
72 std::string InternalNodeAsString(TreeView::InternalNode* node); 73 std::string InternalNodeAsString(TreeView::InternalNode* node);
73 74
74 TestNode* GetNodeByTitleImpl(TestNode* node, const base::string16& title); 75 TestNode* GetNodeByTitleImpl(TestNode* node, const base::string16& title);
75 76
76 DISALLOW_COPY_AND_ASSIGN(TreeViewTest); 77 DISALLOW_COPY_AND_ASSIGN(TreeViewTest);
77 }; 78 };
78 79
79 TestNode* TreeViewTest::Add(TestNode* parent, 80 TestNode* TreeViewTest::Add(TestNode* parent,
80 int index, 81 int index,
81 const std::string& title) { 82 const std::string& title) {
82 TestNode* new_node = new TestNode; 83 std::unique_ptr<TestNode> new_node = base::MakeUnique<TestNode>();
83 new_node->SetTitle(ASCIIToUTF16(title)); 84 new_node->SetTitle(ASCIIToUTF16(title));
84 model_.Add(parent, new_node, index); 85 return model_.Add(parent, std::move(new_node), index);
85 return new_node;
86 } 86 }
87 87
88 std::string TreeViewTest::TreeViewContentsAsString() { 88 std::string TreeViewTest::TreeViewContentsAsString() {
89 return InternalNodeAsString(&tree_.root_); 89 return InternalNodeAsString(&tree_.root_);
90 } 90 }
91 91
92 std::string TreeViewTest::GetSelectedNodeTitle() { 92 std::string TreeViewTest::GetSelectedNodeTitle() {
93 TreeModelNode* model_node = tree_.GetSelectedNode(); 93 TreeModelNode* model_node = tree_.GetSelectedNode();
94 return model_node ? base::UTF16ToASCII(model_node->GetTitle()) 94 return model_node ? base::UTF16ToASCII(model_node->GetTitle())
95 : std::string(); 95 : std::string();
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 247
248 // Remove c11, which shouldn't have any effect on the tree. 248 // Remove c11, which shouldn't have any effect on the tree.
249 EXPECT_EQ("root [a b c]", TreeViewContentsAsString()); 249 EXPECT_EQ("root [a b c]", TreeViewContentsAsString());
250 EXPECT_EQ("root", GetSelectedNodeTitle()); 250 EXPECT_EQ("root", GetSelectedNodeTitle());
251 EXPECT_EQ(4, GetRowCount()); 251 EXPECT_EQ(4, GetRowCount());
252 252
253 // Expand b1, then collapse it and remove its only child, b1. This shouldn't 253 // Expand b1, then collapse it and remove its only child, b1. This shouldn't
254 // effect the tree. 254 // effect the tree.
255 tree_.Expand(GetNodeByTitle("b")); 255 tree_.Expand(GetNodeByTitle("b"));
256 tree_.Collapse(GetNodeByTitle("b")); 256 tree_.Collapse(GetNodeByTitle("b"));
257 delete model_.Remove(GetNodeByTitle("b1")->parent(), GetNodeByTitle("b1")); 257 model_.Remove(GetNodeByTitle("b1")->parent(), GetNodeByTitle("b1"));
258 EXPECT_EQ("root [a b c]", TreeViewContentsAsString()); 258 EXPECT_EQ("root [a b c]", TreeViewContentsAsString());
259 EXPECT_EQ("root", GetSelectedNodeTitle()); 259 EXPECT_EQ("root", GetSelectedNodeTitle());
260 EXPECT_EQ(4, GetRowCount()); 260 EXPECT_EQ(4, GetRowCount());
261 261
262 // Remove 'b'. 262 // Remove 'b'.
263 delete model_.Remove(GetNodeByTitle("b")->parent(), GetNodeByTitle("b")); 263 model_.Remove(GetNodeByTitle("b")->parent(), GetNodeByTitle("b"));
264 EXPECT_EQ("root [a c]", TreeViewContentsAsString()); 264 EXPECT_EQ("root [a c]", TreeViewContentsAsString());
265 EXPECT_EQ("root", GetSelectedNodeTitle()); 265 EXPECT_EQ("root", GetSelectedNodeTitle());
266 EXPECT_EQ(3, GetRowCount()); 266 EXPECT_EQ(3, GetRowCount());
267 267
268 // Remove 'c11', shouldn't visually change anything. 268 // Remove 'c11', shouldn't visually change anything.
269 delete model_.Remove(GetNodeByTitle("c11")->parent(), GetNodeByTitle("c11")); 269 model_.Remove(GetNodeByTitle("c11")->parent(), GetNodeByTitle("c11"));
270 EXPECT_EQ("root [a c]", TreeViewContentsAsString()); 270 EXPECT_EQ("root [a c]", TreeViewContentsAsString());
271 EXPECT_EQ("root", GetSelectedNodeTitle()); 271 EXPECT_EQ("root", GetSelectedNodeTitle());
272 EXPECT_EQ(3, GetRowCount()); 272 EXPECT_EQ(3, GetRowCount());
273 273
274 // Select 'c1', remove 'c' and make sure selection changes. 274 // Select 'c1', remove 'c' and make sure selection changes.
275 tree_.SetSelectedNode(GetNodeByTitle("c1")); 275 tree_.SetSelectedNode(GetNodeByTitle("c1"));
276 EXPECT_EQ("c1", GetSelectedNodeTitle()); 276 EXPECT_EQ("c1", GetSelectedNodeTitle());
277 delete model_.Remove(GetNodeByTitle("c")->parent(), GetNodeByTitle("c")); 277 model_.Remove(GetNodeByTitle("c")->parent(), GetNodeByTitle("c"));
278 EXPECT_EQ("root [a]", TreeViewContentsAsString()); 278 EXPECT_EQ("root [a]", TreeViewContentsAsString());
279 EXPECT_EQ("root", GetSelectedNodeTitle()); 279 EXPECT_EQ("root", GetSelectedNodeTitle());
280 EXPECT_EQ(2, GetRowCount()); 280 EXPECT_EQ(2, GetRowCount());
281 281
282 tree_.SetRootShown(false); 282 tree_.SetRootShown(false);
283 // Add 'b' select it and remove it. Because we're not showing the root 283 // Add 'b' select it and remove it. Because we're not showing the root
284 // selection should change to 'a'. 284 // selection should change to 'a'.
285 Add(GetNodeByTitle("root"), 1, "b"); 285 Add(GetNodeByTitle("root"), 1, "b");
286 tree_.SetSelectedNode(GetNodeByTitle("b")); 286 tree_.SetSelectedNode(GetNodeByTitle("b"));
287 delete model_.Remove(GetNodeByTitle("b")->parent(), GetNodeByTitle("b")); 287 model_.Remove(GetNodeByTitle("b")->parent(), GetNodeByTitle("b"));
288 EXPECT_EQ("root [a]", TreeViewContentsAsString()); 288 EXPECT_EQ("root [a]", TreeViewContentsAsString());
289 EXPECT_EQ("a", GetSelectedNodeTitle()); 289 EXPECT_EQ("a", GetSelectedNodeTitle());
290 EXPECT_EQ(1, GetRowCount()); 290 EXPECT_EQ(1, GetRowCount());
291 } 291 }
292 292
293 // Verifies changing a node title works. 293 // Verifies changing a node title works.
294 TEST_F(TreeViewTest, TreeNodeChanged) { 294 TEST_F(TreeViewTest, TreeNodeChanged) {
295 // Add c1 as a child of c and c11 as a child of c1. 295 // Add c1 as a child of c and c11 as a child of c1.
296 Add(Add(GetNodeByTitle("c"), 0, "c1"), 0, "c11"); 296 Add(Add(GetNodeByTitle("c"), 0, "c1"), 0, "c11");
297 tree_.SetModel(&model_); 297 tree_.SetModel(&model_);
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 tree_.SetSelectedNode(GetNodeByTitle("root")); 415 tree_.SetSelectedNode(GetNodeByTitle("root"));
416 ExpandOrSelectChild(); 416 ExpandOrSelectChild();
417 tree_.SetEditable(true); 417 tree_.SetEditable(true);
418 tree_.StartEditing(GetNodeByTitle("a")); 418 tree_.StartEditing(GetNodeByTitle("a"));
419 tree_.editor()->SetText(ASCIIToUTF16("a changed")); 419 tree_.editor()->SetText(ASCIIToUTF16("a changed"));
420 tree_.OnDidChangeFocus(NULL, NULL); 420 tree_.OnDidChangeFocus(NULL, NULL);
421 EXPECT_TRUE(GetNodeByTitle("a changed") != NULL); 421 EXPECT_TRUE(GetNodeByTitle("a changed") != NULL);
422 } 422 }
423 423
424 } // namespace views 424 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/tree/tree_view.cc ('k') | ui/views/examples/tree_view_example.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698