]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/dataviewctrltest.cpp
244a2ec5181223934f4b58fa45b495377ea4d13d
[wxWidgets.git] / tests / controls / dataviewctrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/dataviewctrltest.cpp
3 // Purpose: wxDataViewCtrl unit test
4 // Author: Vaclav Slavik
5 // Created: 2011-08-08
6 // RCS-ID: $Id$
7 // Copyright: (c) 2011 Vaclav Slavik <vslavik@gmail.com>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #if wxUSE_DATAVIEWCTRL
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #include "wx/app.h"
23 #include "wx/dataview.h"
24
25 #include "testableframe.h"
26
27 // ----------------------------------------------------------------------------
28 // test class
29 // ----------------------------------------------------------------------------
30
31 class DataViewCtrlTestCase : public CppUnit::TestCase
32 {
33 public:
34 DataViewCtrlTestCase() { }
35
36 virtual void setUp();
37 virtual void tearDown();
38
39 private:
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();
46
47 // Create wxDataViewTreeCtrl with the given style.
48 void Create(long style);
49
50 void DeleteSelected();
51 void DeleteNotSelected();
52 void GetSelectionForMulti();
53 void GetSelectionForSingle();
54
55 void TestSelectionFor0and1();
56
57 // the dataview control itself
58 wxDataViewTreeCtrl *m_dvc;
59
60 // and some of its items
61 wxDataViewItem m_root,
62 m_child1,
63 m_child2,
64 m_grandchild;
65
66 DECLARE_NO_COPY_CLASS(DataViewCtrlTestCase)
67 };
68
69 // register in the unnamed registry so that these tests are run by default
70 CPPUNIT_TEST_SUITE_REGISTRATION( DataViewCtrlTestCase );
71
72 // also include in its own registry so that these tests can be run alone
73 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DataViewCtrlTestCase, "DataViewCtrlTestCase" );
74
75 // ----------------------------------------------------------------------------
76 // test initialization
77 // ----------------------------------------------------------------------------
78
79 void DataViewCtrlTestCase::Create(long style)
80 {
81 m_dvc = new wxDataViewTreeCtrl(wxTheApp->GetTopWindow(),
82 wxID_ANY,
83 wxDefaultPosition,
84 wxSize(400, 200),
85 style);
86
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");
91
92 m_dvc->SetSize(400, 200);
93 m_dvc->ExpandAncestors(m_root);
94 m_dvc->Refresh();
95 m_dvc->Update();
96 }
97
98 void DataViewCtrlTestCase::setUp()
99 {
100 Create(wxDV_MULTIPLE);
101 }
102
103 void DataViewCtrlTestCase::tearDown()
104 {
105 delete m_dvc;
106 m_dvc = NULL;
107
108 m_root =
109 m_child1 =
110 m_child2 =
111 m_grandchild = wxDataViewItem();
112 }
113
114 // ----------------------------------------------------------------------------
115 // the tests themselves
116 // ----------------------------------------------------------------------------
117
118 void DataViewCtrlTestCase::DeleteSelected()
119 {
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);
125
126 // delete a selected item
127 m_dvc->DeleteItem(m_child1);
128
129 m_dvc->GetSelections(sel);
130
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 );
134 }
135
136 void DataViewCtrlTestCase::DeleteNotSelected()
137 {
138 // TODO not working on OS X as expected
139 #ifndef __WXOSX__
140 wxDataViewItemArray sel;
141 sel.push_back(m_child1);
142 sel.push_back(m_grandchild);
143 m_dvc->SetSelections(sel);
144
145 // delete unselected item
146 m_dvc->DeleteItem(m_child2);
147
148 m_dvc->GetSelections(sel);
149
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 );
154 #endif
155 }
156
157 void DataViewCtrlTestCase::TestSelectionFor0and1()
158 {
159 wxDataViewItemArray selections;
160
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() );
165
166 CPPUNIT_ASSERT( !m_dvc->GetSelections(selections) );
167 CPPUNIT_ASSERT( selections.empty() );
168
169 // Select one item.
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 );
176 }
177
178 void DataViewCtrlTestCase::GetSelectionForMulti()
179 {
180 wxDataViewItemArray selections;
181
182 TestSelectionFor0and1();
183
184 // Also test with more than one selected item.
185 m_dvc->Select(m_child2);
186
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 );
192 }
193
194 void DataViewCtrlTestCase::GetSelectionForSingle()
195 {
196 delete m_dvc;
197 Create(0);
198
199 TestSelectionFor0and1();
200 }
201
202 #endif //wxUSE_DATAVIEWCTRL