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 wxDataViewItemArray sel
;
139 sel
.push_back(m_child1
);
140 sel
.push_back(m_grandchild
);
141 m_dvc
->SetSelections(sel
);
143 // delete unselected item
144 m_dvc
->DeleteItem(m_child2
);
146 m_dvc
->GetSelections(sel
);
148 // m_child1 and its children should be unaffected
149 CPPUNIT_ASSERT_EQUAL( 2, sel
.size() );
150 CPPUNIT_ASSERT( sel
[0] == m_child1
);
151 CPPUNIT_ASSERT( sel
[1] == m_grandchild
);
154 void DataViewCtrlTestCase::TestSelectionFor0and1()
156 wxDataViewItemArray selections
;
158 // Initially there is no selection.
159 CPPUNIT_ASSERT_EQUAL( 0, m_dvc
->GetSelectedItemsCount() );
160 CPPUNIT_ASSERT( !m_dvc
->HasSelection() );
161 CPPUNIT_ASSERT( !m_dvc
->GetSelection().IsOk() );
163 CPPUNIT_ASSERT( !m_dvc
->GetSelections(selections
) );
164 CPPUNIT_ASSERT( selections
.empty() );
167 m_dvc
->Select(m_child1
);
168 CPPUNIT_ASSERT_EQUAL( 1, m_dvc
->GetSelectedItemsCount() );
169 CPPUNIT_ASSERT( m_dvc
->HasSelection() );
170 CPPUNIT_ASSERT( m_dvc
->GetSelection().IsOk() );
171 CPPUNIT_ASSERT_EQUAL( 1, m_dvc
->GetSelections(selections
) );
172 CPPUNIT_ASSERT( selections
[0] == m_child1
);
175 void DataViewCtrlTestCase::GetSelectionForMulti()
177 wxDataViewItemArray selections
;
179 TestSelectionFor0and1();
181 // Also test with more than one selected item.
182 m_dvc
->Select(m_child2
);
184 CPPUNIT_ASSERT_EQUAL( 2, m_dvc
->GetSelectedItemsCount() );
185 CPPUNIT_ASSERT( m_dvc
->HasSelection() );
186 CPPUNIT_ASSERT( !m_dvc
->GetSelection().IsOk() );
187 CPPUNIT_ASSERT_EQUAL( 2, m_dvc
->GetSelections(selections
) );
188 CPPUNIT_ASSERT( selections
[1] == m_child2
);
191 void DataViewCtrlTestCase::GetSelectionForSingle()
196 TestSelectionFor0and1();
199 #endif //wxUSE_DATAVIEWCTRL