]>
git.saurik.com Git - wxWidgets.git/blob - tests/controls/treectrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/treectrltest.cpp
3 // Purpose: wxTreeCtrl unit test
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/treectrl.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class TreeCtrlTestCase
: public CppUnit::TestCase
33 TreeCtrlTestCase() { }
36 virtual void tearDown();
39 CPPUNIT_TEST_SUITE( TreeCtrlTestCase
);
40 CPPUNIT_TEST( HasChildren
);
41 CPPUNIT_TEST( SelectItemSingle
);
42 CPPUNIT_TEST( PseudoTest_MultiSelect
);
43 CPPUNIT_TEST( SelectItemMulti
);
44 CPPUNIT_TEST( PseudoTest_SetHiddenRoot
);
45 CPPUNIT_TEST( HasChildren
);
46 CPPUNIT_TEST_SUITE_END();
49 void SelectItemSingle();
50 void SelectItemMulti();
51 void PseudoTest_MultiSelect() { ms_multiSelect
= true; }
52 void PseudoTest_SetHiddenRoot() { ms_hiddenRoot
= true; }
54 static bool ms_multiSelect
;
55 static bool ms_hiddenRoot
;
57 // the tree control itself
60 // and some of its items
66 DECLARE_NO_COPY_CLASS(TreeCtrlTestCase
)
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( TreeCtrlTestCase
);
72 // also include in it's own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TreeCtrlTestCase
, "TreeCtrlTestCase" );
75 // ----------------------------------------------------------------------------
76 // test initialization
77 // ----------------------------------------------------------------------------
79 bool TreeCtrlTestCase::ms_multiSelect
= false;
80 bool TreeCtrlTestCase::ms_hiddenRoot
= false;
82 void TreeCtrlTestCase::setUp()
84 m_tree
= new wxTreeCtrl(wxTheApp
->GetTopWindow());
87 m_tree
->ToggleWindowStyle(wxTR_MULTIPLE
);
90 m_tree
->ToggleWindowStyle(wxTR_HIDE_ROOT
); // actually set it
92 m_root
= m_tree
->AddRoot("root");
93 m_child1
= m_tree
->AppendItem(m_root
, "child1");
94 m_child2
= m_tree
->AppendItem(m_root
, "child2");
95 m_grandchild
= m_tree
->AppendItem(m_child1
, "grandchild");
98 void TreeCtrlTestCase::tearDown()
106 m_grandchild
= wxTreeItemId();
109 // ----------------------------------------------------------------------------
110 // the tests themselves
111 // ----------------------------------------------------------------------------
113 void TreeCtrlTestCase::HasChildren()
115 CPPUNIT_ASSERT( m_tree
->HasChildren(m_root
) );
116 CPPUNIT_ASSERT( m_tree
->HasChildren(m_child1
) );
117 CPPUNIT_ASSERT( !m_tree
->HasChildren(m_child2
) );
118 CPPUNIT_ASSERT( !m_tree
->HasChildren(m_grandchild
) );
121 void TreeCtrlTestCase::SelectItemSingle()
123 // this test should be only ran in single-selection control
124 CPPUNIT_ASSERT( !m_tree
->HasFlag(wxTR_MULTIPLE
) );
126 // initially nothing is selected
127 CPPUNIT_ASSERT( !m_tree
->IsSelected(m_child1
) );
129 // selecting an item should make it selected
130 m_tree
->SelectItem(m_child1
);
131 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child1
) );
133 // selecting it again shouldn't change anything
134 m_tree
->SelectItem(m_child1
);
135 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child1
) );
137 // selecting another item should switch the selection to it
138 m_tree
->SelectItem(m_child2
);
139 CPPUNIT_ASSERT( !m_tree
->IsSelected(m_child1
) );
140 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child2
) );
142 // selecting it again still shouldn't change anything
143 m_tree
->SelectItem(m_child2
);
144 CPPUNIT_ASSERT( !m_tree
->IsSelected(m_child1
) );
145 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child2
) );
147 // deselecting an item should remove the selection entirely
148 m_tree
->UnselectItem(m_child2
);
149 CPPUNIT_ASSERT( !m_tree
->IsSelected(m_child1
) );
150 CPPUNIT_ASSERT( !m_tree
->IsSelected(m_child2
) );
153 void TreeCtrlTestCase::SelectItemMulti()
155 // this test should be only ran in multi-selection control
156 CPPUNIT_ASSERT( m_tree
->HasFlag(wxTR_MULTIPLE
) );
158 // initially nothing is selected
159 CPPUNIT_ASSERT( !m_tree
->IsSelected(m_child1
) );
161 // selecting an item should make it selected
162 m_tree
->SelectItem(m_child1
);
163 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child1
) );
165 // selecting it again shouldn't change anything
166 m_tree
->SelectItem(m_child1
);
167 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child1
) );
169 // selecting another item shouldn't deselect the previously selected one
170 m_tree
->SelectItem(m_child2
);
171 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child1
) );
172 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child2
) );
174 // selecting it again still shouldn't change anything
175 m_tree
->SelectItem(m_child2
);
176 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child1
) );
177 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child2
) );
179 // deselecting one of the items should leave the others selected
180 m_tree
->UnselectItem(m_child1
);
181 CPPUNIT_ASSERT( !m_tree
->IsSelected(m_child1
) );
182 CPPUNIT_ASSERT( m_tree
->IsSelected(m_child2
) );