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