1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/dataviewctrltest.cpp
3 // Purpose: wxDataViewCtrl unit test
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2011 Vaclav Slavik <vslavik@gmail.com>
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
15 #if wxUSE_DATAVIEWCTRL
22 #include "wx/dataview.h"
24 #include "testableframe.h"
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
30 class DataViewCtrlTestCase
: public CppUnit::TestCase
33 DataViewCtrlTestCase() { }
36 virtual void tearDown();
39 CPPUNIT_TEST_SUITE( DataViewCtrlTestCase
);
40 CPPUNIT_TEST( DeleteSelected
);
41 CPPUNIT_TEST( DeleteNotSelected
);
42 CPPUNIT_TEST( GetSelectionForMulti
);
43 CPPUNIT_TEST( GetSelectionForSingle
);
44 CPPUNIT_TEST_SUITE_END();
46 // Create wxDataViewTreeCtrl with the given style.
47 void Create(long style
);
49 void DeleteSelected();
50 void DeleteNotSelected();
51 void GetSelectionForMulti();
52 void GetSelectionForSingle();
54 void TestSelectionFor0and1();
56 // the dataview control itself
57 wxDataViewTreeCtrl
*m_dvc
;
59 // and some of its items
60 wxDataViewItem m_root
,
65 DECLARE_NO_COPY_CLASS(DataViewCtrlTestCase
)
68 // register in the unnamed registry so that these tests are run by default
69 CPPUNIT_TEST_SUITE_REGISTRATION( DataViewCtrlTestCase
);
71 // also include in its own registry so that these tests can be run alone
72 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataViewCtrlTestCase
, "DataViewCtrlTestCase" );
74 // ----------------------------------------------------------------------------
75 // test initialization
76 // ----------------------------------------------------------------------------
78 void DataViewCtrlTestCase::Create(long style
)
80 m_dvc
= new wxDataViewTreeCtrl(wxTheApp
->GetTopWindow(),
86 m_root
= m_dvc
->AppendContainer(wxDataViewItem(), "The root");
87 m_child1
= m_dvc
->AppendContainer(m_root
, "child1");
88 m_grandchild
= m_dvc
->AppendItem(m_child1
, "grandchild");
89 m_child2
= m_dvc
->AppendItem(m_root
, "child2");
91 m_dvc
->SetSize(400, 200);
92 m_dvc
->ExpandAncestors(m_root
);
97 void DataViewCtrlTestCase::setUp()
99 Create(wxDV_MULTIPLE
);
102 void DataViewCtrlTestCase::tearDown()
110 m_grandchild
= wxDataViewItem();
113 // ----------------------------------------------------------------------------
114 // the tests themselves
115 // ----------------------------------------------------------------------------
117 void DataViewCtrlTestCase::DeleteSelected()
119 wxDataViewItemArray sel
;
120 sel
.push_back(m_child1
);
121 sel
.push_back(m_grandchild
);
122 sel
.push_back(m_child2
);
123 m_dvc
->SetSelections(sel
);
125 // delete a selected item
126 m_dvc
->DeleteItem(m_child1
);
128 m_dvc
->GetSelections(sel
);
130 // m_child1 and its children should be removed from the selection now
131 CPPUNIT_ASSERT_EQUAL( 1, sel
.size() );
132 CPPUNIT_ASSERT( sel
[0] == m_child2
);
135 void DataViewCtrlTestCase::DeleteNotSelected()
137 // TODO not working on OS X as expected
139 wxDataViewItemArray sel
;
140 sel
.push_back(m_child1
);
141 sel
.push_back(m_grandchild
);
142 m_dvc
->SetSelections(sel
);
144 // delete unselected item
145 m_dvc
->DeleteItem(m_child2
);
147 m_dvc
->GetSelections(sel
);
149 // m_child1 and its children should be unaffected
150 CPPUNIT_ASSERT_EQUAL( 2, sel
.size() );
151 CPPUNIT_ASSERT( sel
[0] == m_child1
);
152 CPPUNIT_ASSERT( sel
[1] == m_grandchild
);
156 void DataViewCtrlTestCase::TestSelectionFor0and1()
158 wxDataViewItemArray selections
;
160 // Initially there is no selection.
161 CPPUNIT_ASSERT_EQUAL( 0, m_dvc
->GetSelectedItemsCount() );
162 CPPUNIT_ASSERT( !m_dvc
->HasSelection() );
163 CPPUNIT_ASSERT( !m_dvc
->GetSelection().IsOk() );
165 CPPUNIT_ASSERT( !m_dvc
->GetSelections(selections
) );
166 CPPUNIT_ASSERT( selections
.empty() );
169 m_dvc
->Select(m_child1
);
170 CPPUNIT_ASSERT_EQUAL( 1, m_dvc
->GetSelectedItemsCount() );
171 CPPUNIT_ASSERT( m_dvc
->HasSelection() );
172 CPPUNIT_ASSERT( m_dvc
->GetSelection().IsOk() );
173 CPPUNIT_ASSERT_EQUAL( 1, m_dvc
->GetSelections(selections
) );
174 CPPUNIT_ASSERT( selections
[0] == m_child1
);
177 void DataViewCtrlTestCase::GetSelectionForMulti()
179 wxDataViewItemArray selections
;
181 TestSelectionFor0and1();
183 // Also test with more than one selected item.
184 m_dvc
->Select(m_child2
);
186 CPPUNIT_ASSERT_EQUAL( 2, m_dvc
->GetSelectedItemsCount() );
187 CPPUNIT_ASSERT( m_dvc
->HasSelection() );
188 CPPUNIT_ASSERT( !m_dvc
->GetSelection().IsOk() );
189 CPPUNIT_ASSERT_EQUAL( 2, m_dvc
->GetSelections(selections
) );
190 CPPUNIT_ASSERT( selections
[1] == m_child2
);
193 void DataViewCtrlTestCase::GetSelectionForSingle()
198 TestSelectionFor0and1();
201 #endif //wxUSE_DATAVIEWCTRL