1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/treelistctrltest.cpp
3 // Purpose: wxTreeListCtrl unit test.
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
16 #if wxUSE_TREELISTCTRL
22 #include "wx/treelist.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class TreeListCtrlTestCase
: public CppUnit::TestCase
33 TreeListCtrlTestCase() { }
36 virtual void tearDown();
39 CPPUNIT_TEST_SUITE( TreeListCtrlTestCase
);
40 CPPUNIT_TEST( Traversal
);
41 CPPUNIT_TEST( ItemText
);
42 CPPUNIT_TEST( ItemCheck
);
43 CPPUNIT_TEST_SUITE_END();
45 // Create the control with the given style.
46 void Create(long style
);
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
= "");
61 // The control itself.
62 wxTreeListCtrl
*m_treelist
;
64 // And some of its items.
65 wxTreeListItem m_code
,
69 // Also the total number of items in it initially
72 wxDECLARE_NO_COPY_CLASS(TreeListCtrlTestCase
);
75 // register in the unnamed registry so that these tests are run by default
76 CPPUNIT_TEST_SUITE_REGISTRATION( TreeListCtrlTestCase
);
78 // also include in its own registry so that these tests can be run alone
79 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TreeListCtrlTestCase
, "TreeListCtrlTestCase" );
81 // ----------------------------------------------------------------------------
82 // test initialization
83 // ----------------------------------------------------------------------------
86 TreeListCtrlTestCase::AddItem(const char *label
,
87 wxTreeListItem parent
,
92 parent
= m_treelist
->GetRootItem();
94 wxTreeListItem item
= m_treelist
->AppendItem(parent
, label
);
95 m_treelist
->SetItemText(item
, 1, numFiles
);
96 m_treelist
->SetItemText(item
, 2, size
);
103 void TreeListCtrlTestCase::Create(long style
)
105 m_treelist
= new wxTreeListCtrl(wxTheApp
->GetTopWindow(),
111 m_treelist
->AppendColumn("Component");
112 m_treelist
->AppendColumn("# Files");
113 m_treelist
->AppendColumn("Size");
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");
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");
125 wxTreeListItem Documentation
= AddItem("Documentation");
126 AddItem("HTML", Documentation
, "many");
127 AddItem("CHM", Documentation
, "1");
129 wxTreeListItem Samples
= AddItem("Samples");
130 AddItem("minimal", Samples
, "1", "7 KiB");
131 AddItem("widgets", Samples
, "28", "419 KiB");
133 m_treelist
->Refresh();
134 m_treelist
->Update();
137 void TreeListCtrlTestCase::setUp()
140 Create(wxTL_MULTIPLE
| wxTL_3STATE
);
143 void TreeListCtrlTestCase::tearDown()
149 // ----------------------------------------------------------------------------
150 // the tests themselves
151 // ----------------------------------------------------------------------------
153 // Test various tree traversal methods.
154 void TreeListCtrlTestCase::Traversal()
156 // GetParent() tests:
157 wxTreeListItem root
= m_treelist
->GetRootItem();
158 CPPUNIT_ASSERT( !m_treelist
->GetItemParent(root
) );
160 CPPUNIT_ASSERT_EQUAL( root
, m_treelist
->GetItemParent(m_code
) );
161 CPPUNIT_ASSERT_EQUAL( m_code
, m_treelist
->GetItemParent(m_code_osx
) );
164 // GetFirstChild() and GetNextSibling() tests:
165 CPPUNIT_ASSERT_EQUAL( m_code
, m_treelist
->GetFirstChild(root
) );
169 m_treelist
->GetNextSibling
171 m_treelist
->GetNextSibling
173 m_treelist
->GetFirstChild(m_code
)
178 // Get{First,Next}Item() test:
179 unsigned numItems
= 0;
180 for ( wxTreeListItem item
= m_treelist
->GetFirstItem();
182 item
= m_treelist
->GetNextItem(item
) )
187 CPPUNIT_ASSERT_EQUAL( m_numItems
, numItems
);
190 // Test accessing items text.
191 void TreeListCtrlTestCase::ItemText()
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) );
196 m_treelist
->SetItemText(m_code_osx_cocoa
, "wxCocoa");
197 CPPUNIT_ASSERT_EQUAL( "wxCocoa", m_treelist
->GetItemText(m_code_osx_cocoa
) );
199 m_treelist
->SetItemText(m_code_osx_cocoa
, 1, "47");
200 CPPUNIT_ASSERT_EQUAL( "47", m_treelist
->GetItemText(m_code_osx_cocoa
, 1) );
203 // Test checking and unchecking items.
204 void TreeListCtrlTestCase::ItemCheck()
206 CPPUNIT_ASSERT_EQUAL( wxCHK_UNCHECKED
,
207 m_treelist
->GetCheckedState(m_code
) );
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
) );
217 m_treelist
->UncheckItem(m_code_osx_cocoa
);
218 CPPUNIT_ASSERT_EQUAL( wxCHK_UNCHECKED
,
219 m_treelist
->GetCheckedState(m_code_osx_cocoa
) );
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
) );
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
) );
235 #endif // wxUSE_TREELISTCTRL