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