1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/treelistctrltest.cpp
3 // Purpose: wxTreeListCtrl unit test.
4 // Author: Vadim Zeitlin
6 // Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
15 #if wxUSE_TREELISTCTRL
21 #include "wx/treelist.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 class TreeListCtrlTestCase
: public CppUnit::TestCase
32 TreeListCtrlTestCase() { }
35 virtual void tearDown();
38 CPPUNIT_TEST_SUITE( TreeListCtrlTestCase
);
39 CPPUNIT_TEST( Traversal
);
40 CPPUNIT_TEST( ItemText
);
41 CPPUNIT_TEST( ItemCheck
);
42 CPPUNIT_TEST_SUITE_END();
44 // Create the control with the given style.
45 void Create(long style
);
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
= "");
60 // The control itself.
61 wxTreeListCtrl
*m_treelist
;
63 // And some of its items.
64 wxTreeListItem m_code
,
68 // Also the total number of items in it initially
71 wxDECLARE_NO_COPY_CLASS(TreeListCtrlTestCase
);
74 // register in the unnamed registry so that these tests are run by default
75 CPPUNIT_TEST_SUITE_REGISTRATION( TreeListCtrlTestCase
);
77 // also include in its own registry so that these tests can be run alone
78 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TreeListCtrlTestCase
, "TreeListCtrlTestCase" );
80 // ----------------------------------------------------------------------------
81 // test initialization
82 // ----------------------------------------------------------------------------
85 TreeListCtrlTestCase::AddItem(const char *label
,
86 wxTreeListItem parent
,
91 parent
= m_treelist
->GetRootItem();
93 wxTreeListItem item
= m_treelist
->AppendItem(parent
, label
);
94 m_treelist
->SetItemText(item
, 1, numFiles
);
95 m_treelist
->SetItemText(item
, 2, size
);
102 void TreeListCtrlTestCase::Create(long style
)
104 m_treelist
= new wxTreeListCtrl(wxTheApp
->GetTopWindow(),
110 m_treelist
->AppendColumn("Component");
111 m_treelist
->AppendColumn("# Files");
112 m_treelist
->AppendColumn("Size");
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");
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");
124 wxTreeListItem Documentation
= AddItem("Documentation");
125 AddItem("HTML", Documentation
, "many");
126 AddItem("CHM", Documentation
, "1");
128 wxTreeListItem Samples
= AddItem("Samples");
129 AddItem("minimal", Samples
, "1", "7 KiB");
130 AddItem("widgets", Samples
, "28", "419 KiB");
132 m_treelist
->Refresh();
133 m_treelist
->Update();
136 void TreeListCtrlTestCase::setUp()
139 Create(wxTL_MULTIPLE
| wxTL_3STATE
);
142 void TreeListCtrlTestCase::tearDown()
148 // ----------------------------------------------------------------------------
149 // the tests themselves
150 // ----------------------------------------------------------------------------
152 // Test various tree traversal methods.
153 void TreeListCtrlTestCase::Traversal()
155 // GetParent() tests:
156 wxTreeListItem root
= m_treelist
->GetRootItem();
157 CPPUNIT_ASSERT( !m_treelist
->GetItemParent(root
) );
159 CPPUNIT_ASSERT_EQUAL( root
, m_treelist
->GetItemParent(m_code
) );
160 CPPUNIT_ASSERT_EQUAL( m_code
, m_treelist
->GetItemParent(m_code_osx
) );
163 // GetFirstChild() and GetNextSibling() tests:
164 CPPUNIT_ASSERT_EQUAL( m_code
, m_treelist
->GetFirstChild(root
) );
168 m_treelist
->GetNextSibling
170 m_treelist
->GetNextSibling
172 m_treelist
->GetFirstChild(m_code
)
177 // Get{First,Next}Item() test:
178 unsigned numItems
= 0;
179 for ( wxTreeListItem item
= m_treelist
->GetFirstItem();
181 item
= m_treelist
->GetNextItem(item
) )
186 CPPUNIT_ASSERT_EQUAL( m_numItems
, numItems
);
189 // Test accessing items text.
190 void TreeListCtrlTestCase::ItemText()
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) );
195 m_treelist
->SetItemText(m_code_osx_cocoa
, "wxCocoa");
196 CPPUNIT_ASSERT_EQUAL( "wxCocoa", m_treelist
->GetItemText(m_code_osx_cocoa
) );
198 m_treelist
->SetItemText(m_code_osx_cocoa
, 1, "47");
199 CPPUNIT_ASSERT_EQUAL( "47", m_treelist
->GetItemText(m_code_osx_cocoa
, 1) );
202 // Test checking and unchecking items.
203 void TreeListCtrlTestCase::ItemCheck()
205 CPPUNIT_ASSERT_EQUAL( wxCHK_UNCHECKED
,
206 m_treelist
->GetCheckedState(m_code
) );
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
) );
216 m_treelist
->UncheckItem(m_code_osx_cocoa
);
217 CPPUNIT_ASSERT_EQUAL( wxCHK_UNCHECKED
,
218 m_treelist
->GetCheckedState(m_code_osx_cocoa
) );
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
) );
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
) );
234 #endif // wxUSE_TREELISTCTRL