]>
Commit | Line | Data |
---|---|---|
869c7a94 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/sizers/boxsizer.cpp | |
3 | // Purpose: Unit tests for wxBoxSizer | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2010-03-06 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2010 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 | #include "wx/sizer.h" | |
23 | #endif // WX_PRECOMP | |
24 | ||
25 | inline std::ostream& operator<<(std::ostream& o, const wxSize& s) | |
26 | { | |
27 | return o << s.x << 'x' << s.y; | |
28 | } | |
29 | ||
30 | // ---------------------------------------------------------------------------- | |
31 | // test class | |
32 | // ---------------------------------------------------------------------------- | |
33 | ||
34 | class BoxSizerTestCase : public CppUnit::TestCase | |
35 | { | |
36 | public: | |
37 | BoxSizerTestCase() { } | |
38 | ||
39 | virtual void setUp(); | |
40 | virtual void tearDown(); | |
41 | ||
42 | private: | |
43 | CPPUNIT_TEST_SUITE( BoxSizerTestCase ); | |
44 | CPPUNIT_TEST( Size1 ); | |
729f53d4 | 45 | CPPUNIT_TEST( Size3 ); |
869c7a94 VZ |
46 | CPPUNIT_TEST_SUITE_END(); |
47 | ||
48 | void Size1(); | |
729f53d4 | 49 | void Size3(); |
869c7a94 VZ |
50 | |
51 | wxWindow *m_win; | |
52 | wxSizer *m_sizer; | |
53 | ||
54 | DECLARE_NO_COPY_CLASS(BoxSizerTestCase) | |
55 | }; | |
56 | ||
57 | // register in the unnamed registry so that these tests are run by default | |
58 | CPPUNIT_TEST_SUITE_REGISTRATION( BoxSizerTestCase ); | |
59 | ||
60 | // also include in it's own registry so that these tests can be run alone | |
61 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( BoxSizerTestCase, "BoxSizerTestCase" ); | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | // test initialization | |
65 | // ---------------------------------------------------------------------------- | |
66 | ||
67 | void BoxSizerTestCase::setUp() | |
68 | { | |
69 | m_win = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY); | |
70 | m_win->SetClientSize(127, 35); | |
71 | ||
72 | m_sizer = new wxBoxSizer(wxHORIZONTAL); | |
73 | m_win->SetSizer(m_sizer); | |
74 | } | |
75 | ||
76 | void BoxSizerTestCase::tearDown() | |
77 | { | |
78 | delete m_win; | |
79 | m_win = NULL; | |
80 | ||
81 | m_sizer = NULL; | |
82 | } | |
83 | ||
84 | // ---------------------------------------------------------------------------- | |
85 | // tests themselves | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
88 | void BoxSizerTestCase::Size1() | |
89 | { | |
90 | const wxSize sizeTotal = m_win->GetClientSize(); | |
91 | const wxSize sizeChild = sizeTotal / 2; | |
92 | ||
93 | wxWindow * const | |
94 | child = new wxWindow(m_win, wxID_ANY, wxDefaultPosition, sizeChild); | |
95 | m_sizer->Add(child); | |
96 | m_win->Layout(); | |
97 | CPPUNIT_ASSERT_EQUAL( sizeChild, child->GetSize() ); | |
729f53d4 | 98 | |
869c7a94 VZ |
99 | m_sizer->Clear(); |
100 | m_sizer->Add(child, wxSizerFlags(1)); | |
101 | m_win->Layout(); | |
102 | CPPUNIT_ASSERT_EQUAL( wxSize(sizeTotal.x, sizeChild.y), child->GetSize() ); | |
103 | ||
104 | m_sizer->Clear(); | |
105 | m_sizer->Add(child, wxSizerFlags(1).Expand()); | |
106 | m_win->Layout(); | |
107 | CPPUNIT_ASSERT_EQUAL( sizeTotal, child->GetSize() ); | |
26022721 | 108 | |
a71aeb24 VZ |
109 | m_sizer->Clear(); |
110 | m_sizer->Add(child, wxSizerFlags()); | |
111 | m_sizer->SetItemMinSize(child, sizeTotal*2); | |
112 | m_win->Layout(); | |
113 | CPPUNIT_ASSERT_EQUAL( sizeTotal, child->GetSize() ); | |
114 | ||
26022721 VZ |
115 | m_sizer->Clear(); |
116 | m_sizer->Add(child, wxSizerFlags().Expand()); | |
117 | m_sizer->SetItemMinSize(child, sizeTotal*2); | |
118 | m_win->Layout(); | |
119 | CPPUNIT_ASSERT_EQUAL( sizeTotal, child->GetSize() ); | |
869c7a94 VZ |
120 | } |
121 | ||
729f53d4 VZ |
122 | void BoxSizerTestCase::Size3() |
123 | { | |
124 | // check that various combinations of minimal sizes and proportions work as | |
125 | // expected for different window sizes | |
126 | static const struct LayoutTestData | |
127 | { | |
128 | // proportions of the elements | |
129 | int prop[3]; | |
130 | ||
131 | // minimal sizes of the elements in the sizer direction | |
132 | int minsize[3]; | |
133 | ||
134 | // total size and the expected sizes of the elements | |
135 | int x, | |
136 | sizes[3]; | |
137 | ||
138 | // if true, don't try the permutations of our test data | |
139 | bool dontPermute; | |
140 | ||
141 | ||
142 | // Add the given window to the sizer with the corresponding parameters | |
143 | void AddToSizer(wxSizer *sizer, wxWindow *win, int n) const | |
144 | { | |
145 | sizer->Add(win, wxSizerFlags(prop[n])); | |
146 | sizer->SetItemMinSize(win, wxSize(minsize[n], -1)); | |
147 | } | |
148 | ||
149 | } layoutTestData[] = | |
150 | { | |
151 | // some really simple cases (no need to permute those, they're | |
152 | // symmetrical anyhow) | |
153 | { { 1, 1, 1, }, { 50, 50, 50, }, 150, { 50, 50, 50, }, true }, | |
154 | { { 2, 2, 2, }, { 50, 50, 50, }, 600, { 200, 200, 200, }, true }, | |
155 | ||
156 | // items with different proportions and min sizes when there is enough | |
157 | // space to lay them out | |
158 | { { 1, 2, 3, }, { 0, 0, 0, }, 600, { 100, 200, 300, } }, | |
159 | { { 1, 2, 3, }, { 100, 100, 100, }, 600, { 100, 200, 300, } }, | |
160 | { { 1, 2, 3, }, { 100, 50, 50, }, 600, { 100, 200, 300, } }, | |
161 | { { 0, 1, 1, }, { 200, 100, 100, }, 600, { 200, 200, 200, } }, | |
162 | { { 0, 1, 2, }, { 300, 100, 100, }, 600, { 300, 100, 200, } }, | |
163 | { { 0, 1, 1, }, { 100, 50, 50, }, 300, { 100, 100, 100, } }, | |
164 | { { 0, 1, 2, }, { 100, 50, 50, }, 400, { 100, 100, 200, } }, | |
165 | ||
166 | // cases when there is not enough space to lay out the items correctly | |
167 | // while still respecting their min sizes | |
168 | { { 0, 1, 1, }, { 100, 150, 50, }, 300, { 100, 150, 50, } }, | |
169 | { { 1, 2, 3, }, { 100, 100, 100, }, 300, { 100, 100, 100, } }, | |
170 | { { 1, 2, 3, }, { 100, 50, 50, }, 300, { 100, 80, 120, } }, | |
171 | { { 1, 2, 3, }, { 100, 10, 10, }, 150, { 100, 20, 30, } }, | |
172 | ||
173 | // cases when there is not enough space even for the min sizes (don't | |
174 | // permute in these cases as the layout does depend on the item order | |
175 | // because the first ones have priority) | |
176 | { { 1, 2, 3, }, { 100, 50, 50, }, 150, { 100, 50, 0, }, true }, | |
177 | { { 1, 2, 3, }, { 100, 100, 100, }, 200, { 100, 100, 0, }, true }, | |
178 | { { 1, 2, 3, }, { 100, 100, 100, }, 150, { 100, 50, 0, }, true }, | |
179 | { { 1, 2, 3, }, { 100, 100, 100, }, 50, { 50, 0, 0, }, true }, | |
180 | { { 1, 2, 3, }, { 100, 100, 100, }, 0, { 0, 0, 0, }, true }, | |
181 | }; | |
182 | ||
183 | wxWindow *child[3]; | |
184 | child[0] = new wxWindow(m_win, wxID_ANY); | |
185 | child[1] = new wxWindow(m_win, wxID_ANY); | |
186 | child[2] = new wxWindow(m_win, wxID_ANY); | |
187 | ||
729f53d4 VZ |
188 | for ( unsigned i = 0; i < WXSIZEOF(layoutTestData); i++ ) |
189 | { | |
190 | LayoutTestData ltd = layoutTestData[i]; | |
191 | ||
192 | // the results shouldn't depend on the order of items except in the | |
193 | // case when there is not enough space for even the fixed width items | |
194 | // (in which case the first ones might get enough of it but not the | |
195 | // last ones) so test a couple of permutations of test data unless | |
196 | // specifically disabled for this test case | |
197 | for ( unsigned p = 0; p < 3; p++) | |
198 | { | |
199 | switch ( p ) | |
200 | { | |
201 | case 0: | |
202 | // nothing to do, use original data | |
203 | break; | |
204 | ||
205 | case 1: | |
206 | // exchange first and last elements | |
207 | wxSwap(ltd.prop[0], ltd.prop[2]); | |
208 | wxSwap(ltd.minsize[0], ltd.minsize[2]); | |
209 | wxSwap(ltd.sizes[0], ltd.sizes[2]); | |
210 | break; | |
211 | ||
212 | case 2: | |
213 | // exchange the original third and second elements | |
214 | wxSwap(ltd.prop[0], ltd.prop[1]); | |
215 | wxSwap(ltd.minsize[0], ltd.minsize[1]); | |
216 | wxSwap(ltd.sizes[0], ltd.sizes[1]); | |
217 | break; | |
218 | } | |
219 | ||
220 | m_sizer->Clear(); | |
be8332eb VZ |
221 | |
222 | unsigned j; | |
729f53d4 VZ |
223 | for ( j = 0; j < WXSIZEOF(child); j++ ) |
224 | ltd.AddToSizer(m_sizer, child[j], j); | |
225 | ||
226 | m_win->SetClientSize(ltd.x, -1); | |
227 | m_win->Layout(); | |
228 | ||
229 | for ( j = 0; j < WXSIZEOF(child); j++ ) | |
230 | { | |
231 | WX_ASSERT_EQUAL_MESSAGE | |
232 | ( | |
233 | ( | |
234 | "test %lu, permutation #%d: wrong size for child #%d " | |
235 | "for total size %d", | |
236 | static_cast<unsigned long>(i), | |
237 | static_cast<unsigned long>(p), | |
238 | j, | |
239 | ltd.x | |
240 | ), | |
241 | ltd.sizes[j], child[j]->GetSize().x | |
242 | ); | |
243 | } | |
244 | ||
245 | // don't try other permutations if explicitly disabled | |
246 | if ( ltd.dontPermute ) | |
247 | break; | |
248 | } | |
249 | } | |
250 | } |