]>
git.saurik.com Git - wxWidgets.git/blob - tests/sizers/boxsizer.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/sizers/boxsizer.cpp
3 // Purpose: Unit tests for wxBoxSizer
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2010 Vadim Zeitlin <vadim@wxwidgets.org>
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
25 #include "asserthelper.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class BoxSizerTestCase
: public CppUnit::TestCase
34 BoxSizerTestCase() { }
37 virtual void tearDown();
40 CPPUNIT_TEST_SUITE( BoxSizerTestCase
);
41 CPPUNIT_TEST( Size1
);
42 CPPUNIT_TEST( Size3
);
43 CPPUNIT_TEST( CalcMin
);
44 CPPUNIT_TEST_SUITE_END();
53 DECLARE_NO_COPY_CLASS(BoxSizerTestCase
)
56 // register in the unnamed registry so that these tests are run by default
57 CPPUNIT_TEST_SUITE_REGISTRATION( BoxSizerTestCase
);
59 // also include in its own registry so that these tests can be run alone
60 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( BoxSizerTestCase
, "BoxSizerTestCase" );
62 // ----------------------------------------------------------------------------
63 // test initialization
64 // ----------------------------------------------------------------------------
66 void BoxSizerTestCase::setUp()
68 m_win
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
69 m_win
->SetClientSize(127, 35);
71 m_sizer
= new wxBoxSizer(wxHORIZONTAL
);
72 m_win
->SetSizer(m_sizer
);
75 void BoxSizerTestCase::tearDown()
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 void BoxSizerTestCase::Size1()
89 const wxSize sizeTotal
= m_win
->GetClientSize();
90 const wxSize sizeChild
= sizeTotal
/ 2;
93 child
= new wxWindow(m_win
, wxID_ANY
, wxDefaultPosition
, sizeChild
);
96 CPPUNIT_ASSERT_EQUAL( sizeChild
, child
->GetSize() );
99 m_sizer
->Add(child
, wxSizerFlags(1));
101 CPPUNIT_ASSERT_EQUAL( wxSize(sizeTotal
.x
, sizeChild
.y
), child
->GetSize() );
104 m_sizer
->Add(child
, wxSizerFlags(1).Expand());
106 CPPUNIT_ASSERT_EQUAL( sizeTotal
, child
->GetSize() );
109 m_sizer
->Add(child
, wxSizerFlags());
110 m_sizer
->SetItemMinSize(child
, sizeTotal
*2);
112 CPPUNIT_ASSERT_EQUAL( sizeTotal
, child
->GetSize() );
115 m_sizer
->Add(child
, wxSizerFlags().Expand());
116 m_sizer
->SetItemMinSize(child
, sizeTotal
*2);
118 CPPUNIT_ASSERT_EQUAL( sizeTotal
, child
->GetSize() );
121 void BoxSizerTestCase::Size3()
123 // check that various combinations of minimal sizes and proportions work as
124 // expected for different window sizes
125 static const struct LayoutTestData
127 // proportions of the elements
130 // minimal sizes of the elements in the sizer direction
133 // total size and the expected sizes of the elements
137 // if true, don't try the permutations of our test data
141 // Add the given window to the sizer with the corresponding parameters
142 void AddToSizer(wxSizer
*sizer
, wxWindow
*win
, int n
) const
144 sizer
->Add(win
, wxSizerFlags(prop
[n
]));
145 sizer
->SetItemMinSize(win
, wxSize(minsize
[n
], -1));
150 // some really simple cases (no need to permute those, they're
151 // symmetrical anyhow)
152 { { 1, 1, 1, }, { 50, 50, 50, }, 150, { 50, 50, 50, }, true },
153 { { 2, 2, 2, }, { 50, 50, 50, }, 600, { 200, 200, 200, }, true },
155 // items with different proportions and min sizes when there is enough
156 // space to lay them out
157 { { 1, 2, 3, }, { 0, 0, 0, }, 600, { 100, 200, 300, } },
158 { { 1, 2, 3, }, { 100, 100, 100, }, 600, { 100, 200, 300, } },
159 { { 1, 2, 3, }, { 100, 50, 50, }, 600, { 100, 200, 300, } },
160 { { 0, 1, 1, }, { 200, 100, 100, }, 600, { 200, 200, 200, } },
161 { { 0, 1, 2, }, { 300, 100, 100, }, 600, { 300, 100, 200, } },
162 { { 0, 1, 1, }, { 100, 50, 50, }, 300, { 100, 100, 100, } },
163 { { 0, 1, 2, }, { 100, 50, 50, }, 400, { 100, 100, 200, } },
165 // cases when there is not enough space to lay out the items correctly
166 // while still respecting their min sizes
167 { { 0, 1, 1, }, { 100, 150, 50, }, 300, { 100, 150, 50, } },
168 { { 1, 2, 3, }, { 100, 100, 100, }, 300, { 100, 100, 100, } },
169 { { 1, 2, 3, }, { 100, 50, 50, }, 300, { 100, 80, 120, } },
170 { { 1, 2, 3, }, { 100, 10, 10, }, 150, { 100, 20, 30, } },
172 // cases when there is not enough space even for the min sizes (don't
173 // permute in these cases as the layout does depend on the item order
174 // because the first ones have priority)
175 { { 1, 2, 3, }, { 100, 50, 50, }, 150, { 100, 50, 0, }, true },
176 { { 1, 2, 3, }, { 100, 100, 100, }, 200, { 100, 100, 0, }, true },
177 { { 1, 2, 3, }, { 100, 100, 100, }, 150, { 100, 50, 0, }, true },
178 { { 1, 2, 3, }, { 100, 100, 100, }, 50, { 50, 0, 0, }, true },
179 { { 1, 2, 3, }, { 100, 100, 100, }, 0, { 0, 0, 0, }, true },
183 child
[0] = new wxWindow(m_win
, wxID_ANY
);
184 child
[1] = new wxWindow(m_win
, wxID_ANY
);
185 child
[2] = new wxWindow(m_win
, wxID_ANY
);
187 for ( unsigned i
= 0; i
< WXSIZEOF(layoutTestData
); i
++ )
189 LayoutTestData ltd
= layoutTestData
[i
];
191 // the results shouldn't depend on the order of items except in the
192 // case when there is not enough space for even the fixed width items
193 // (in which case the first ones might get enough of it but not the
194 // last ones) so test a couple of permutations of test data unless
195 // specifically disabled for this test case
196 for ( unsigned p
= 0; p
< 3; p
++)
201 // nothing to do, use original data
205 // exchange first and last elements
206 wxSwap(ltd
.prop
[0], ltd
.prop
[2]);
207 wxSwap(ltd
.minsize
[0], ltd
.minsize
[2]);
208 wxSwap(ltd
.sizes
[0], ltd
.sizes
[2]);
212 // exchange the original third and second elements
213 wxSwap(ltd
.prop
[0], ltd
.prop
[1]);
214 wxSwap(ltd
.minsize
[0], ltd
.minsize
[1]);
215 wxSwap(ltd
.sizes
[0], ltd
.sizes
[1]);
222 for ( j
= 0; j
< WXSIZEOF(child
); j
++ )
223 ltd
.AddToSizer(m_sizer
, child
[j
], j
);
225 m_win
->SetClientSize(ltd
.x
, -1);
228 for ( j
= 0; j
< WXSIZEOF(child
); j
++ )
230 WX_ASSERT_EQUAL_MESSAGE
233 "test %lu, permutation #%lu: wrong size for child #%d "
235 static_cast<unsigned long>(i
),
236 static_cast<unsigned long>(p
),
240 ltd
.sizes
[j
], child
[j
]->GetSize().x
244 // don't try other permutations if explicitly disabled
245 if ( ltd
.dontPermute
)
251 void BoxSizerTestCase::CalcMin()
253 static const unsigned NUM_TEST_ITEM
= 3;
255 static const struct CalcMinTestData
257 // proportions of the elements, if one of them is -1 it means to not
258 // use this window at all in this test
259 int prop
[NUM_TEST_ITEM
];
261 // minimal sizes of the elements in the sizer direction
262 int minsize
[NUM_TEST_ITEM
];
264 // the expected minimal sizer size
266 } calcMinTestData
[] =
268 { { 1, 1, -1 }, { 30, 50, 0 }, 100 },
269 { { 1, 1, 0 }, { 30, 50, 20 }, 120 },
270 { { 10, 10, -1 }, { 30, 50, 0 }, 100 },
271 { { 1, 2, 2 }, { 50, 50, 80 }, 250 },
272 { { 1, 2, 2 }, { 100, 50, 80 }, 500 },
276 wxWindow
*child
[NUM_TEST_ITEM
];
277 for ( n
= 0; n
< NUM_TEST_ITEM
; n
++ )
278 child
[n
] = new wxWindow(m_win
, wxID_ANY
);
280 for ( unsigned i
= 0; i
< WXSIZEOF(calcMinTestData
); i
++ )
284 const CalcMinTestData
& cmtd
= calcMinTestData
[i
];
285 for ( n
= 0; n
< NUM_TEST_ITEM
; n
++ )
287 if ( cmtd
.prop
[n
] != -1 )
289 child
[n
]->SetInitialSize(wxSize(cmtd
.minsize
[n
], -1));
290 m_sizer
->Add(child
[n
], wxSizerFlags(cmtd
.prop
[n
]));
294 WX_ASSERT_EQUAL_MESSAGE
297 cmtd
.total
, m_sizer
->CalcMin().x