]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: tests/controls/headerctrltest.cpp | |
3 | // Purpose: wxHeaderCtrl unit test | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2008-11-26 | |
6 | // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org> | |
7 | /////////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | // ---------------------------------------------------------------------------- | |
10 | // headers | |
11 | // ---------------------------------------------------------------------------- | |
12 | ||
13 | #include "testprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/app.h" | |
21 | #endif // WX_PRECOMP | |
22 | ||
23 | #include "wx/headerctrl.h" | |
24 | ||
25 | // ---------------------------------------------------------------------------- | |
26 | // test class | |
27 | // ---------------------------------------------------------------------------- | |
28 | ||
29 | class HeaderCtrlTestCase : public CppUnit::TestCase | |
30 | { | |
31 | public: | |
32 | HeaderCtrlTestCase() { } | |
33 | ||
34 | virtual void setUp(); | |
35 | virtual void tearDown(); | |
36 | ||
37 | private: | |
38 | CPPUNIT_TEST_SUITE( HeaderCtrlTestCase ); | |
39 | CPPUNIT_TEST( AddDelete ); | |
40 | CPPUNIT_TEST( BestSize ); | |
41 | CPPUNIT_TEST( Reorder ); | |
42 | CPPUNIT_TEST_SUITE_END(); | |
43 | ||
44 | void AddDelete(); | |
45 | void BestSize(); | |
46 | void Reorder(); | |
47 | ||
48 | wxHeaderCtrlSimple *m_header; | |
49 | ||
50 | DECLARE_NO_COPY_CLASS(HeaderCtrlTestCase) | |
51 | }; | |
52 | ||
53 | // register in the unnamed registry so that these tests are run by default | |
54 | CPPUNIT_TEST_SUITE_REGISTRATION( HeaderCtrlTestCase ); | |
55 | ||
56 | // also include in its own registry so that these tests can be run alone | |
57 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( HeaderCtrlTestCase, "HeaderCtrlTestCase" ); | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // test initialization | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | void HeaderCtrlTestCase::setUp() | |
64 | { | |
65 | m_header = new wxHeaderCtrlSimple(wxTheApp->GetTopWindow()); | |
66 | } | |
67 | ||
68 | void HeaderCtrlTestCase::tearDown() | |
69 | { | |
70 | delete m_header; | |
71 | m_header = NULL; | |
72 | } | |
73 | ||
74 | // ---------------------------------------------------------------------------- | |
75 | // the tests themselves | |
76 | // ---------------------------------------------------------------------------- | |
77 | ||
78 | void HeaderCtrlTestCase::AddDelete() | |
79 | { | |
80 | CPPUNIT_ASSERT_EQUAL( 0, m_header->GetColumnCount() ); | |
81 | ||
82 | m_header->AppendColumn(wxHeaderColumnSimple("Column 1")); | |
83 | CPPUNIT_ASSERT_EQUAL( 1, m_header->GetColumnCount() ); | |
84 | ||
85 | m_header->AppendColumn(wxHeaderColumnSimple("Column 2")); | |
86 | CPPUNIT_ASSERT_EQUAL( 2, m_header->GetColumnCount() ); | |
87 | ||
88 | m_header->InsertColumn(wxHeaderColumnSimple("Column 0"), 0); | |
89 | CPPUNIT_ASSERT_EQUAL( 3, m_header->GetColumnCount() ); | |
90 | ||
91 | m_header->DeleteColumn(2); | |
92 | CPPUNIT_ASSERT_EQUAL( 2, m_header->GetColumnCount() ); | |
93 | } | |
94 | ||
95 | void HeaderCtrlTestCase::BestSize() | |
96 | { | |
97 | const wxSize sizeEmpty = m_header->GetBestSize(); | |
98 | // this fails under wxGTK where wxControl::GetBestSize() is 0 in horizontal | |
99 | // direction | |
100 | //CPPUNIT_ASSERT( sizeEmpty.x > 0 ); | |
101 | CPPUNIT_ASSERT( sizeEmpty.y > 0 ); | |
102 | ||
103 | m_header->AppendColumn(wxHeaderColumnSimple("Foo")); | |
104 | m_header->AppendColumn(wxHeaderColumnSimple("Bar")); | |
105 | const wxSize size = m_header->GetBestSize(); | |
106 | CPPUNIT_ASSERT_EQUAL( sizeEmpty.y, size.y ); | |
107 | } | |
108 | ||
109 | void HeaderCtrlTestCase::Reorder() | |
110 | { | |
111 | static const int COL_COUNT = 4; | |
112 | ||
113 | int n; | |
114 | ||
115 | for ( n = 0; n < COL_COUNT; n++ ) | |
116 | m_header->AppendColumn(wxHeaderColumnSimple(wxString::Format("%d", n))); | |
117 | ||
118 | wxArrayInt order = m_header->GetColumnsOrder(); // initial order: [0 1 2 3] | |
119 | for ( n = 0; n < COL_COUNT; n++ ) | |
120 | CPPUNIT_ASSERT_EQUAL( n, order[n] ); | |
121 | ||
122 | wxHeaderCtrl::MoveColumnInOrderArray(order, 0, 2); | |
123 | m_header->SetColumnsOrder(order); // change order to [1 2 0 3] | |
124 | ||
125 | order = m_header->GetColumnsOrder(); | |
126 | CPPUNIT_ASSERT_EQUAL( 1, order[0] ); | |
127 | CPPUNIT_ASSERT_EQUAL( 2, order[1] ); | |
128 | CPPUNIT_ASSERT_EQUAL( 0, order[2] ); | |
129 | CPPUNIT_ASSERT_EQUAL( 3, order[3] ); | |
130 | ||
131 | order[2] = 3; | |
132 | order[3] = 0; | |
133 | m_header->SetColumnsOrder(order); // and now [1 2 3 0] | |
134 | order = m_header->GetColumnsOrder(); | |
135 | CPPUNIT_ASSERT_EQUAL( 1, order[0] ); | |
136 | CPPUNIT_ASSERT_EQUAL( 2, order[1] ); | |
137 | CPPUNIT_ASSERT_EQUAL( 3, order[2] ); | |
138 | CPPUNIT_ASSERT_EQUAL( 0, order[3] ); | |
139 | ||
140 | wxHeaderCtrl::MoveColumnInOrderArray(order, 1, 3); | |
141 | m_header->SetColumnsOrder(order); // finally [2 3 0 1] | |
142 | order = m_header->GetColumnsOrder(); | |
143 | CPPUNIT_ASSERT_EQUAL( 2, order[0] ); | |
144 | CPPUNIT_ASSERT_EQUAL( 3, order[1] ); | |
145 | CPPUNIT_ASSERT_EQUAL( 0, order[2] ); | |
146 | CPPUNIT_ASSERT_EQUAL( 1, order[3] ); | |
147 | } | |
148 |