Fix crash when auto-sizing a wxDataViewCtrl column.
[wxWidgets.git] / tests / controls / treelistctrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/treelistctrltest.cpp
3 // Purpose: wxTreeListCtrl unit test.
4 // Author: Vadim Zeitlin
5 // Created: 2011-08-27
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #if wxUSE_TREELISTCTRL
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #include "wx/treelist.h"
22
23 #include "wx/app.h"
24
25 // ----------------------------------------------------------------------------
26 // test class
27 // ----------------------------------------------------------------------------
28
29 class TreeListCtrlTestCase : public CppUnit::TestCase
30 {
31 public:
32 TreeListCtrlTestCase() { }
33
34 virtual void setUp();
35 virtual void tearDown();
36
37 private:
38 CPPUNIT_TEST_SUITE( TreeListCtrlTestCase );
39 CPPUNIT_TEST( Traversal );
40 CPPUNIT_TEST( ItemText );
41 CPPUNIT_TEST( ItemCheck );
42 CPPUNIT_TEST_SUITE_END();
43
44 // Create the control with the given style.
45 void Create(long style);
46
47 // Add an item to the tree and increment m_numItems.
48 wxTreeListItem AddItem(const char *label,
49 wxTreeListItem parent = wxTreeListItem(),
50 const char *numFiles = "",
51 const char *size = "");
52
53
54 // Tests:
55 void Traversal();
56 void ItemText();
57 void ItemCheck();
58
59
60 // The control itself.
61 wxTreeListCtrl *m_treelist;
62
63 // And some of its items.
64 wxTreeListItem m_code,
65 m_code_osx,
66 m_code_osx_cocoa;
67
68 // Also the total number of items in it initially
69 unsigned m_numItems;
70
71 wxDECLARE_NO_COPY_CLASS(TreeListCtrlTestCase);
72 };
73
74 // register in the unnamed registry so that these tests are run by default
75 CPPUNIT_TEST_SUITE_REGISTRATION( TreeListCtrlTestCase );
76
77 // also include in its own registry so that these tests can be run alone
78 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TreeListCtrlTestCase, "TreeListCtrlTestCase" );
79
80 // ----------------------------------------------------------------------------
81 // test initialization
82 // ----------------------------------------------------------------------------
83
84 wxTreeListItem
85 TreeListCtrlTestCase::AddItem(const char *label,
86 wxTreeListItem parent,
87 const char *numFiles,
88 const char *size)
89 {
90 if ( !parent.IsOk() )
91 parent = m_treelist->GetRootItem();
92
93 wxTreeListItem item = m_treelist->AppendItem(parent, label);
94 m_treelist->SetItemText(item, 1, numFiles);
95 m_treelist->SetItemText(item, 2, size);
96
97 m_numItems++;
98
99 return item;
100 }
101
102 void TreeListCtrlTestCase::Create(long style)
103 {
104 m_treelist = new wxTreeListCtrl(wxTheApp->GetTopWindow(),
105 wxID_ANY,
106 wxDefaultPosition,
107 wxSize(400, 200),
108 style);
109
110 m_treelist->AppendColumn("Component");
111 m_treelist->AppendColumn("# Files");
112 m_treelist->AppendColumn("Size");
113
114 // Fill the control with the same data as used in the treelist sample:
115 m_code = AddItem("Code");
116 AddItem("wxMSW", m_code, "313", "3.94 MiB");
117 AddItem("wxGTK", m_code, "180", "1.66 MiB");
118
119 m_code_osx = AddItem("wxOSX", m_code, "265", "2.36 MiB");
120 AddItem("Core", m_code_osx, "31", "347 KiB");
121 AddItem("Carbon", m_code_osx, "91", "1.34 MiB");
122 m_code_osx_cocoa = AddItem("Cocoa", m_code_osx, "46", "512 KiB");
123
124 wxTreeListItem Documentation = AddItem("Documentation");
125 AddItem("HTML", Documentation, "many");
126 AddItem("CHM", Documentation, "1");
127
128 wxTreeListItem Samples = AddItem("Samples");
129 AddItem("minimal", Samples, "1", "7 KiB");
130 AddItem("widgets", Samples, "28", "419 KiB");
131
132 m_treelist->Refresh();
133 m_treelist->Update();
134 }
135
136 void TreeListCtrlTestCase::setUp()
137 {
138 m_numItems = 0;
139 Create(wxTL_MULTIPLE | wxTL_3STATE);
140 }
141
142 void TreeListCtrlTestCase::tearDown()
143 {
144 delete m_treelist;
145 m_treelist = NULL;
146 }
147
148 // ----------------------------------------------------------------------------
149 // the tests themselves
150 // ----------------------------------------------------------------------------
151
152 // Test various tree traversal methods.
153 void TreeListCtrlTestCase::Traversal()
154 {
155 // GetParent() tests:
156 wxTreeListItem root = m_treelist->GetRootItem();
157 CPPUNIT_ASSERT( !m_treelist->GetItemParent(root) );
158
159 CPPUNIT_ASSERT_EQUAL( root, m_treelist->GetItemParent(m_code) );
160 CPPUNIT_ASSERT_EQUAL( m_code, m_treelist->GetItemParent(m_code_osx) );
161
162
163 // GetFirstChild() and GetNextSibling() tests:
164 CPPUNIT_ASSERT_EQUAL( m_code, m_treelist->GetFirstChild(root) );
165 CPPUNIT_ASSERT_EQUAL
166 (
167 m_code_osx,
168 m_treelist->GetNextSibling
169 (
170 m_treelist->GetNextSibling
171 (
172 m_treelist->GetFirstChild(m_code)
173 )
174 )
175 );
176
177 // Get{First,Next}Item() test:
178 unsigned numItems = 0;
179 for ( wxTreeListItem item = m_treelist->GetFirstItem();
180 item.IsOk();
181 item = m_treelist->GetNextItem(item) )
182 {
183 numItems++;
184 }
185
186 CPPUNIT_ASSERT_EQUAL( m_numItems, numItems );
187 }
188
189 // Test accessing items text.
190 void TreeListCtrlTestCase::ItemText()
191 {
192 CPPUNIT_ASSERT_EQUAL( "Cocoa", m_treelist->GetItemText(m_code_osx_cocoa) );
193 CPPUNIT_ASSERT_EQUAL( "46", m_treelist->GetItemText(m_code_osx_cocoa, 1) );
194
195 m_treelist->SetItemText(m_code_osx_cocoa, "wxCocoa");
196 CPPUNIT_ASSERT_EQUAL( "wxCocoa", m_treelist->GetItemText(m_code_osx_cocoa) );
197
198 m_treelist->SetItemText(m_code_osx_cocoa, 1, "47");
199 CPPUNIT_ASSERT_EQUAL( "47", m_treelist->GetItemText(m_code_osx_cocoa, 1) );
200 }
201
202 // Test checking and unchecking items.
203 void TreeListCtrlTestCase::ItemCheck()
204 {
205 CPPUNIT_ASSERT_EQUAL( wxCHK_UNCHECKED,
206 m_treelist->GetCheckedState(m_code) );
207
208 m_treelist->CheckItemRecursively(m_code);
209 CPPUNIT_ASSERT_EQUAL( wxCHK_CHECKED,
210 m_treelist->GetCheckedState(m_code) );
211 CPPUNIT_ASSERT_EQUAL( wxCHK_CHECKED,
212 m_treelist->GetCheckedState(m_code_osx) );
213 CPPUNIT_ASSERT_EQUAL( wxCHK_CHECKED,
214 m_treelist->GetCheckedState(m_code_osx_cocoa) );
215
216 m_treelist->UncheckItem(m_code_osx_cocoa);
217 CPPUNIT_ASSERT_EQUAL( wxCHK_UNCHECKED,
218 m_treelist->GetCheckedState(m_code_osx_cocoa) );
219
220 m_treelist->UpdateItemParentStateRecursively(m_code_osx_cocoa);
221 CPPUNIT_ASSERT_EQUAL( wxCHK_UNDETERMINED,
222 m_treelist->GetCheckedState(m_code_osx) );
223 CPPUNIT_ASSERT_EQUAL( wxCHK_UNDETERMINED,
224 m_treelist->GetCheckedState(m_code) );
225
226 m_treelist->CheckItemRecursively(m_code_osx, wxCHK_UNCHECKED);
227 m_treelist->UpdateItemParentStateRecursively(m_code_osx_cocoa);
228 CPPUNIT_ASSERT_EQUAL( wxCHK_UNCHECKED,
229 m_treelist->GetCheckedState(m_code_osx) );
230 CPPUNIT_ASSERT_EQUAL( wxCHK_UNDETERMINED,
231 m_treelist->GetCheckedState(m_code) );
232 }
233
234 #endif // wxUSE_TREELISTCTRL