]> git.saurik.com Git - wxWidgets.git/blob - tests/controls/listctrltest.cpp
Remove scroll units duplication in wxGrid to fix setting them.
[wxWidgets.git] / tests / controls / listctrltest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/controls/listctrltest.cpp
3 // Purpose: wxListCtrl unit test
4 // Author: Vadim Zeitlin
5 // Created: 2008-11-26
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/listctrl.h"
25
26 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class ListCtrlTestCase : public CppUnit::TestCase
31 {
32 public:
33 ListCtrlTestCase() { }
34
35 virtual void setUp();
36 virtual void tearDown();
37
38 private:
39 CPPUNIT_TEST_SUITE( ListCtrlTestCase );
40 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
41 CPPUNIT_TEST( ColumnsOrder );
42 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
43 CPPUNIT_TEST( ItemRect );
44 CPPUNIT_TEST( ItemText );
45 CPPUNIT_TEST( ChangeMode );
46 CPPUNIT_TEST_SUITE_END();
47
48 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
49 void ColumnsOrder();
50 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
51 void ItemRect();
52 void ItemText();
53 void ChangeMode();
54
55 wxListCtrl *m_list;
56
57 DECLARE_NO_COPY_CLASS(ListCtrlTestCase)
58 };
59
60 // register in the unnamed registry so that these tests are run by default
61 CPPUNIT_TEST_SUITE_REGISTRATION( ListCtrlTestCase );
62
63 // also include in it's own registry so that these tests can be run alone
64 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListCtrlTestCase, "ListCtrlTestCase" );
65
66 // ----------------------------------------------------------------------------
67 // test initialization
68 // ----------------------------------------------------------------------------
69
70 void ListCtrlTestCase::setUp()
71 {
72 m_list = new wxListCtrl(wxTheApp->GetTopWindow());
73 m_list->SetWindowStyle(wxLC_REPORT);
74 }
75
76 void ListCtrlTestCase::tearDown()
77 {
78 delete m_list;
79 m_list = NULL;
80 }
81
82 // ----------------------------------------------------------------------------
83 // the tests themselves
84 // ----------------------------------------------------------------------------
85
86 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
87
88 void ListCtrlTestCase::ColumnsOrder()
89 {
90 int n;
91 wxListItem li;
92 li.SetMask(wxLIST_MASK_TEXT);
93
94 // first set up some columns
95 static const int NUM_COLS = 3;
96
97 m_list->InsertColumn(0, "Column 0");
98 m_list->InsertColumn(1, "Column 1");
99 m_list->InsertColumn(2, "Column 2");
100
101 // and a couple of test items too
102 m_list->InsertItem(0, "Item 0");
103 m_list->SetItem(0, 1, "first in first");
104
105 m_list->InsertItem(1, "Item 1");
106 m_list->SetItem(1, 2, "second in second");
107
108
109 // check that the order is natural in the beginning
110 const wxArrayInt orderOrig = m_list->GetColumnsOrder();
111 for ( n = 0; n < NUM_COLS; n++ )
112 CPPUNIT_ASSERT_EQUAL( n, orderOrig[n] );
113
114 // then rearrange them: using { 2, 0, 1 } order means that column 2 is
115 // shown first, then column 0 and finally column 1
116 wxArrayInt order(3);
117 order[0] = 2;
118 order[1] = 0;
119 order[2] = 1;
120 m_list->SetColumnsOrder(order);
121
122 // check that we get back the same order as we set
123 const wxArrayInt orderNew = m_list->GetColumnsOrder();
124 for ( n = 0; n < NUM_COLS; n++ )
125 CPPUNIT_ASSERT_EQUAL( order[n], orderNew[n] );
126
127 // and the order -> index mappings for individual columns
128 for ( n = 0; n < NUM_COLS; n++ )
129 CPPUNIT_ASSERT_EQUAL( order[n], m_list->GetColumnIndexFromOrder(n) );
130
131 // and also the reverse mapping
132 CPPUNIT_ASSERT_EQUAL( 1, m_list->GetColumnOrder(0) );
133 CPPUNIT_ASSERT_EQUAL( 2, m_list->GetColumnOrder(1) );
134 CPPUNIT_ASSERT_EQUAL( 0, m_list->GetColumnOrder(2) );
135
136
137 // finally check that accessors still use indices, not order
138 CPPUNIT_ASSERT( m_list->GetColumn(0, li) );
139 CPPUNIT_ASSERT_EQUAL( "Column 0", li.GetText() );
140
141 li.SetId(0);
142 li.SetColumn(1);
143 CPPUNIT_ASSERT( m_list->GetItem(li) );
144 CPPUNIT_ASSERT_EQUAL( "first in first", li.GetText() );
145
146 li.SetId(1);
147 li.SetColumn(2);
148 CPPUNIT_ASSERT( m_list->GetItem(li) );
149 CPPUNIT_ASSERT_EQUAL( "second in second", li.GetText() );
150 }
151
152 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
153
154 void ListCtrlTestCase::ItemRect()
155 {
156 // set up for the test
157 m_list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60);
158 m_list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50);
159 m_list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40);
160
161 m_list->InsertItem(0, "Item 0");
162 m_list->SetItem(0, 1, "first column");
163 m_list->SetItem(0, 1, "second column");
164
165 // do test
166 wxRect r;
167 WX_ASSERT_FAILS_WITH_ASSERT( m_list->GetItemRect(1, r) );
168 CPPUNIT_ASSERT( m_list->GetItemRect(0, r) );
169 CPPUNIT_ASSERT_EQUAL( 150, r.GetWidth() );
170
171 CPPUNIT_ASSERT( m_list->GetSubItemRect(0, 0, r) );
172 CPPUNIT_ASSERT_EQUAL( 60, r.GetWidth() );
173
174 CPPUNIT_ASSERT( m_list->GetSubItemRect(0, 1, r) );
175 CPPUNIT_ASSERT_EQUAL( 50, r.GetWidth() );
176
177 CPPUNIT_ASSERT( m_list->GetSubItemRect(0, 2, r) );
178 CPPUNIT_ASSERT_EQUAL( 40, r.GetWidth() );
179
180 WX_ASSERT_FAILS_WITH_ASSERT( m_list->GetSubItemRect(0, 3, r) );
181 }
182
183 void ListCtrlTestCase::ItemText()
184 {
185 m_list->InsertColumn(0, "First");
186 m_list->InsertColumn(1, "Second");
187
188 m_list->InsertItem(0, "0,0");
189 CPPUNIT_ASSERT_EQUAL( "0,0", m_list->GetItemText(0) );
190 CPPUNIT_ASSERT_EQUAL( "", m_list->GetItemText(0, 1) );
191
192 m_list->SetItem(0, 1, "0,1");
193 CPPUNIT_ASSERT_EQUAL( "0,1", m_list->GetItemText(0, 1) );
194 }
195
196 void ListCtrlTestCase::ChangeMode()
197 {
198 m_list->InsertColumn(0, "Header");
199 m_list->InsertItem(0, "First");
200 m_list->InsertItem(1, "Second");
201 CPPUNIT_ASSERT_EQUAL( 2, m_list->GetItemCount() );
202
203 // check that switching the mode preserves the items
204 m_list->SetWindowStyle(wxLC_ICON);
205 CPPUNIT_ASSERT_EQUAL( 2, m_list->GetItemCount() );
206 CPPUNIT_ASSERT_EQUAL( "First", m_list->GetItemText(0) );
207
208 // and so does switching back
209 m_list->SetWindowStyle(wxLC_REPORT);
210 CPPUNIT_ASSERT_EQUAL( 2, m_list->GetItemCount() );
211 CPPUNIT_ASSERT_EQUAL( "First", m_list->GetItemText(0) );
212 }
213