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