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