Enable wxAny<double>::GetAs<unsigned long>() test.
[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 wxDataViewItemArray sel;
139 sel.push_back(m_child1);
140 sel.push_back(m_grandchild);
141 m_dvc->SetSelections(sel);
142
143 // delete unselected item
144 m_dvc->DeleteItem(m_child2);
145
146 m_dvc->GetSelections(sel);
147
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 );
152 }
153
154 void DataViewCtrlTestCase::TestSelectionFor0and1()
155 {
156 wxDataViewItemArray selections;
157
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() );
162
163 CPPUNIT_ASSERT( !m_dvc->GetSelections(selections) );
164 CPPUNIT_ASSERT( selections.empty() );
165
166 // Select one item.
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 );
173 }
174
175 void DataViewCtrlTestCase::GetSelectionForMulti()
176 {
177 wxDataViewItemArray selections;
178
179 TestSelectionFor0and1();
180
181 // Also test with more than one selected item.
182 m_dvc->Select(m_child2);
183
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 );
189 }
190
191 void DataViewCtrlTestCase::GetSelectionForSingle()
192 {
193 delete m_dvc;
194 Create(0);
195
196 TestSelectionFor0and1();
197 }
198
199 #endif //wxUSE_DATAVIEWCTRL