add test checking that switching mode doesn't change the controls contents (see ...
[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( ChangeMode );
45 CPPUNIT_TEST_SUITE_END();
46
47 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
48 void ColumnsOrder();
49 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
50 void ItemRect();
51 void ChangeMode();
52
53 wxListCtrl *m_list;
54
55 DECLARE_NO_COPY_CLASS(ListCtrlTestCase)
56 };
57
58 // register in the unnamed registry so that these tests are run by default
59 CPPUNIT_TEST_SUITE_REGISTRATION( ListCtrlTestCase );
60
61 // also include in it's own registry so that these tests can be run alone
62 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ListCtrlTestCase, "ListCtrlTestCase" );
63
64 // ----------------------------------------------------------------------------
65 // test initialization
66 // ----------------------------------------------------------------------------
67
68 void ListCtrlTestCase::setUp()
69 {
70 m_list = new wxListCtrl(wxTheApp->GetTopWindow());
71 m_list->SetWindowStyle(wxLC_REPORT);
72 }
73
74 void ListCtrlTestCase::tearDown()
75 {
76 delete m_list;
77 m_list = NULL;
78 }
79
80 // ----------------------------------------------------------------------------
81 // the tests themselves
82 // ----------------------------------------------------------------------------
83
84 #ifdef wxHAS_LISTCTRL_COLUMN_ORDER
85
86 void ListCtrlTestCase::ColumnsOrder()
87 {
88 int n;
89 wxListItem li;
90 li.SetMask(wxLIST_MASK_TEXT);
91
92 // first set up some columns
93 static const int NUM_COLS = 3;
94
95 m_list->InsertColumn(0, "Column 0");
96 m_list->InsertColumn(1, "Column 1");
97 m_list->InsertColumn(2, "Column 2");
98
99 // and a couple of test items too
100 m_list->InsertItem(0, "Item 0");
101 m_list->SetItem(0, 1, "first in first");
102
103 m_list->InsertItem(1, "Item 1");
104 m_list->SetItem(1, 2, "second in second");
105
106
107 // check that the order is natural in the beginning
108 const wxArrayInt orderOrig = m_list->GetColumnsOrder();
109 for ( n = 0; n < NUM_COLS; n++ )
110 CPPUNIT_ASSERT_EQUAL( n, orderOrig[n] );
111
112 // then rearrange them: using { 2, 0, 1 } order means that column 2 is
113 // shown first, then column 0 and finally column 1
114 wxArrayInt order(3);
115 order[0] = 2;
116 order[1] = 0;
117 order[2] = 1;
118 m_list->SetColumnsOrder(order);
119
120 // check that we get back the same order as we set
121 const wxArrayInt orderNew = m_list->GetColumnsOrder();
122 for ( n = 0; n < NUM_COLS; n++ )
123 CPPUNIT_ASSERT_EQUAL( order[n], orderNew[n] );
124
125 // and the order -> index mappings for individual columns
126 for ( n = 0; n < NUM_COLS; n++ )
127 CPPUNIT_ASSERT_EQUAL( order[n], m_list->GetColumnIndexFromOrder(n) );
128
129 // and also the reverse mapping
130 CPPUNIT_ASSERT_EQUAL( 1, m_list->GetColumnOrder(0) );
131 CPPUNIT_ASSERT_EQUAL( 2, m_list->GetColumnOrder(1) );
132 CPPUNIT_ASSERT_EQUAL( 0, m_list->GetColumnOrder(2) );
133
134
135 // finally check that accessors still use indices, not order
136 CPPUNIT_ASSERT( m_list->GetColumn(0, li) );
137 CPPUNIT_ASSERT_EQUAL( "Column 0", li.GetText() );
138
139 li.SetId(0);
140 li.SetColumn(1);
141 CPPUNIT_ASSERT( m_list->GetItem(li) );
142 CPPUNIT_ASSERT_EQUAL( "first in first", li.GetText() );
143
144 li.SetId(1);
145 li.SetColumn(2);
146 CPPUNIT_ASSERT( m_list->GetItem(li) );
147 CPPUNIT_ASSERT_EQUAL( "second in second", li.GetText() );
148 }
149
150 #endif // wxHAS_LISTCTRL_COLUMN_ORDER
151
152 void ListCtrlTestCase::ItemRect()
153 {
154 // set up for the test
155 m_list->InsertColumn(0, "Column 0", wxLIST_FORMAT_LEFT, 60);
156 m_list->InsertColumn(1, "Column 1", wxLIST_FORMAT_LEFT, 50);
157 m_list->InsertColumn(2, "Column 2", wxLIST_FORMAT_LEFT, 40);
158
159 m_list->InsertItem(0, "Item 0");
160 m_list->SetItem(0, 1, "first column");
161 m_list->SetItem(0, 1, "second column");
162
163 // do test
164 wxRect r;
165 WX_ASSERT_FAILS_WITH_ASSERT( m_list->GetItemRect(1, r) );
166 CPPUNIT_ASSERT( m_list->GetItemRect(0, r) );
167 CPPUNIT_ASSERT_EQUAL( 150, r.GetWidth() );
168
169 CPPUNIT_ASSERT( m_list->GetSubItemRect(0, 0, r) );
170 CPPUNIT_ASSERT_EQUAL( 60, r.GetWidth() );
171
172 CPPUNIT_ASSERT( m_list->GetSubItemRect(0, 1, r) );
173 CPPUNIT_ASSERT_EQUAL( 50, r.GetWidth() );
174
175 CPPUNIT_ASSERT( m_list->GetSubItemRect(0, 2, r) );
176 CPPUNIT_ASSERT_EQUAL( 40, r.GetWidth() );
177
178 WX_ASSERT_FAILS_WITH_ASSERT( m_list->GetSubItemRect(0, 3, r) );
179 }
180
181 void ListCtrlTestCase::ChangeMode()
182 {
183 m_list->InsertColumn(0, "Header");
184 m_list->InsertItem(0, "First");
185 m_list->InsertItem(1, "Second");
186 CPPUNIT_ASSERT_EQUAL( 2, m_list->GetItemCount() );
187
188 // check that switching the mode preserves the items
189 m_list->SetWindowStyle(wxLC_ICON);
190 CPPUNIT_ASSERT_EQUAL( 2, m_list->GetItemCount() );
191 CPPUNIT_ASSERT_EQUAL( "First", m_list->GetItemText(0) );
192
193 // and so does switching back
194 m_list->SetWindowStyle(wxLC_REPORT);
195 CPPUNIT_ASSERT_EQUAL( 2, m_list->GetItemCount() );
196 CPPUNIT_ASSERT_EQUAL( "First", m_list->GetItemText(0) );
197 }
198