1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/dataviewctrltest.cpp
3 // Purpose: wxDataViewCtrl unit test
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2011 Vaclav Slavik <vslavik@gmail.com>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
16 #if wxUSE_DATAVIEWCTRL
23 #include "wx/dataview.h"
25 #include "testableframe.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class DataViewCtrlTestCase
: public CppUnit::TestCase
34 DataViewCtrlTestCase() { }
37 virtual void tearDown();
40 CPPUNIT_TEST_SUITE( DataViewCtrlTestCase
);
41 CPPUNIT_TEST( DeleteSelected
);
42 CPPUNIT_TEST( DeleteNotSelected
);
43 CPPUNIT_TEST( GetSelectionForMulti
);
44 CPPUNIT_TEST( GetSelectionForSingle
);
45 CPPUNIT_TEST_SUITE_END();
47 // Create wxDataViewTreeCtrl with the given style.
48 void Create(long style
);
50 void DeleteSelected();
51 void DeleteNotSelected();
52 void GetSelectionForMulti();
53 void GetSelectionForSingle();
55 void TestSelectionFor0and1();
57 // the dataview control itself
58 wxDataViewTreeCtrl
*m_dvc
;
60 // and some of its items
61 wxDataViewItem m_root
,
66 DECLARE_NO_COPY_CLASS(DataViewCtrlTestCase
)
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( DataViewCtrlTestCase
);
72 // also include in its own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataViewCtrlTestCase
, "DataViewCtrlTestCase" );
75 // ----------------------------------------------------------------------------
76 // test initialization
77 // ----------------------------------------------------------------------------
79 void DataViewCtrlTestCase::Create(long style
)
81 m_dvc
= new wxDataViewTreeCtrl(wxTheApp
->GetTopWindow(),
87 m_root
= m_dvc
->AppendContainer(wxDataViewItem(), "The root");
88 m_child1
= m_dvc
->AppendContainer(m_root
, "child1");
89 m_grandchild
= m_dvc
->AppendItem(m_child1
, "grandchild");
90 m_child2
= m_dvc
->AppendItem(m_root
, "child2");
92 m_dvc
->SetSize(400, 200);
93 m_dvc
->ExpandAncestors(m_root
);
98 void DataViewCtrlTestCase::setUp()
100 Create(wxDV_MULTIPLE
);
103 void DataViewCtrlTestCase::tearDown()
111 m_grandchild
= wxDataViewItem();
114 // ----------------------------------------------------------------------------
115 // the tests themselves
116 // ----------------------------------------------------------------------------
118 void DataViewCtrlTestCase::DeleteSelected()
120 wxDataViewItemArray sel
;
121 sel
.push_back(m_child1
);
122 sel
.push_back(m_grandchild
);
123 sel
.push_back(m_child2
);
124 m_dvc
->SetSelections(sel
);
126 // delete a selected item
127 m_dvc
->DeleteItem(m_child1
);
129 m_dvc
->GetSelections(sel
);
131 // m_child1 and its children should be removed from the selection now
132 CPPUNIT_ASSERT_EQUAL( 1, sel
.size() );
133 CPPUNIT_ASSERT( sel
[0] == m_child2
);
136 void DataViewCtrlTestCase::DeleteNotSelected()
138 // TODO not working on OS X as expected
140 wxDataViewItemArray sel
;
141 sel
.push_back(m_child1
);
142 sel
.push_back(m_grandchild
);
143 m_dvc
->SetSelections(sel
);
145 // delete unselected item
146 m_dvc
->DeleteItem(m_child2
);
148 m_dvc
->GetSelections(sel
);
150 // m_child1 and its children should be unaffected
151 CPPUNIT_ASSERT_EQUAL( 2, sel
.size() );
152 CPPUNIT_ASSERT( sel
[0] == m_child1
);
153 CPPUNIT_ASSERT( sel
[1] == m_grandchild
);
157 void DataViewCtrlTestCase::TestSelectionFor0and1()
159 wxDataViewItemArray selections
;
161 // Initially there is no selection.
162 CPPUNIT_ASSERT_EQUAL( 0, m_dvc
->GetSelectedItemsCount() );
163 CPPUNIT_ASSERT( !m_dvc
->HasSelection() );
164 CPPUNIT_ASSERT( !m_dvc
->GetSelection().IsOk() );
166 CPPUNIT_ASSERT( !m_dvc
->GetSelections(selections
) );
167 CPPUNIT_ASSERT( selections
.empty() );
170 m_dvc
->Select(m_child1
);
171 CPPUNIT_ASSERT_EQUAL( 1, m_dvc
->GetSelectedItemsCount() );
172 CPPUNIT_ASSERT( m_dvc
->HasSelection() );
173 CPPUNIT_ASSERT( m_dvc
->GetSelection().IsOk() );
174 CPPUNIT_ASSERT_EQUAL( 1, m_dvc
->GetSelections(selections
) );
175 CPPUNIT_ASSERT( selections
[0] == m_child1
);
178 void DataViewCtrlTestCase::GetSelectionForMulti()
180 wxDataViewItemArray selections
;
182 TestSelectionFor0and1();
184 // Also test with more than one selected item.
185 m_dvc
->Select(m_child2
);
187 CPPUNIT_ASSERT_EQUAL( 2, m_dvc
->GetSelectedItemsCount() );
188 CPPUNIT_ASSERT( m_dvc
->HasSelection() );
189 CPPUNIT_ASSERT( !m_dvc
->GetSelection().IsOk() );
190 CPPUNIT_ASSERT_EQUAL( 2, m_dvc
->GetSelections(selections
) );
191 CPPUNIT_ASSERT( selections
[1] == m_child2
);
194 void DataViewCtrlTestCase::GetSelectionForSingle()
199 TestSelectionFor0and1();
202 #endif //wxUSE_DATAVIEWCTRL