]>
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 inline std::ostream
& operator<<(std::ostream
& o
, const wxSize
& s
)
27 return o
<< s
.x
<< 'x' << s
.y
;
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 class BoxSizerTestCase
: public CppUnit::TestCase
37 BoxSizerTestCase() { }
40 virtual void tearDown();
43 CPPUNIT_TEST_SUITE( BoxSizerTestCase
);
44 CPPUNIT_TEST( Size1
);
45 CPPUNIT_TEST( Size3
);
46 CPPUNIT_TEST( CalcMin
);
47 CPPUNIT_TEST_SUITE_END();
56 DECLARE_NO_COPY_CLASS(BoxSizerTestCase
)
59 // register in the unnamed registry so that these tests are run by default
60 CPPUNIT_TEST_SUITE_REGISTRATION( BoxSizerTestCase
);
62 // also include in it's own registry so that these tests can be run alone
63 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( BoxSizerTestCase
, "BoxSizerTestCase" );
65 // ----------------------------------------------------------------------------
66 // test initialization
67 // ----------------------------------------------------------------------------
69 void BoxSizerTestCase::setUp()
71 m_win
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
72 m_win
->SetClientSize(127, 35);
74 m_sizer
= new wxBoxSizer(wxHORIZONTAL
);
75 m_win
->SetSizer(m_sizer
);
78 void BoxSizerTestCase::tearDown()
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 void BoxSizerTestCase::Size1()
92 const wxSize sizeTotal
= m_win
->GetClientSize();
93 const wxSize sizeChild
= sizeTotal
/ 2;
96 child
= new wxWindow(m_win
, wxID_ANY
, wxDefaultPosition
, sizeChild
);
99 CPPUNIT_ASSERT_EQUAL( sizeChild
, child
->GetSize() );
102 m_sizer
->Add(child
, wxSizerFlags(1));
104 CPPUNIT_ASSERT_EQUAL( wxSize(sizeTotal
.x
, sizeChild
.y
), child
->GetSize() );
107 m_sizer
->Add(child
, wxSizerFlags(1).Expand());
109 CPPUNIT_ASSERT_EQUAL( sizeTotal
, child
->GetSize() );
112 m_sizer
->Add(child
, wxSizerFlags());
113 m_sizer
->SetItemMinSize(child
, sizeTotal
*2);
115 CPPUNIT_ASSERT_EQUAL( sizeTotal
, child
->GetSize() );
118 m_sizer
->Add(child
, wxSizerFlags().Expand());
119 m_sizer
->SetItemMinSize(child
, sizeTotal
*2);
121 CPPUNIT_ASSERT_EQUAL( sizeTotal
, child
->GetSize() );
124 void BoxSizerTestCase::Size3()
126 // check that various combinations of minimal sizes and proportions work as
127 // expected for different window sizes
128 static const struct LayoutTestData
130 // proportions of the elements
133 // minimal sizes of the elements in the sizer direction
136 // total size and the expected sizes of the elements
140 // if true, don't try the permutations of our test data
144 // Add the given window to the sizer with the corresponding parameters
145 void AddToSizer(wxSizer
*sizer
, wxWindow
*win
, int n
) const
147 sizer
->Add(win
, wxSizerFlags(prop
[n
]));
148 sizer
->SetItemMinSize(win
, wxSize(minsize
[n
], -1));
153 // some really simple cases (no need to permute those, they're
154 // symmetrical anyhow)
155 { { 1, 1, 1, }, { 50, 50, 50, }, 150, { 50, 50, 50, }, true },
156 { { 2, 2, 2, }, { 50, 50, 50, }, 600, { 200, 200, 200, }, true },
158 // items with different proportions and min sizes when there is enough
159 // space to lay them out
160 { { 1, 2, 3, }, { 0, 0, 0, }, 600, { 100, 200, 300, } },
161 { { 1, 2, 3, }, { 100, 100, 100, }, 600, { 100, 200, 300, } },
162 { { 1, 2, 3, }, { 100, 50, 50, }, 600, { 100, 200, 300, } },
163 { { 0, 1, 1, }, { 200, 100, 100, }, 600, { 200, 200, 200, } },
164 { { 0, 1, 2, }, { 300, 100, 100, }, 600, { 300, 100, 200, } },
165 { { 0, 1, 1, }, { 100, 50, 50, }, 300, { 100, 100, 100, } },
166 { { 0, 1, 2, }, { 100, 50, 50, }, 400, { 100, 100, 200, } },
168 // cases when there is not enough space to lay out the items correctly
169 // while still respecting their min sizes
170 { { 0, 1, 1, }, { 100, 150, 50, }, 300, { 100, 150, 50, } },
171 { { 1, 2, 3, }, { 100, 100, 100, }, 300, { 100, 100, 100, } },
172 { { 1, 2, 3, }, { 100, 50, 50, }, 300, { 100, 80, 120, } },
173 { { 1, 2, 3, }, { 100, 10, 10, }, 150, { 100, 20, 30, } },
175 // cases when there is not enough space even for the min sizes (don't
176 // permute in these cases as the layout does depend on the item order
177 // because the first ones have priority)
178 { { 1, 2, 3, }, { 100, 50, 50, }, 150, { 100, 50, 0, }, true },
179 { { 1, 2, 3, }, { 100, 100, 100, }, 200, { 100, 100, 0, }, true },
180 { { 1, 2, 3, }, { 100, 100, 100, }, 150, { 100, 50, 0, }, true },
181 { { 1, 2, 3, }, { 100, 100, 100, }, 50, { 50, 0, 0, }, true },
182 { { 1, 2, 3, }, { 100, 100, 100, }, 0, { 0, 0, 0, }, true },
186 child
[0] = new wxWindow(m_win
, wxID_ANY
);
187 child
[1] = new wxWindow(m_win
, wxID_ANY
);
188 child
[2] = new wxWindow(m_win
, wxID_ANY
);
190 for ( unsigned i
= 0; i
< WXSIZEOF(layoutTestData
); i
++ )
192 LayoutTestData ltd
= layoutTestData
[i
];
194 // the results shouldn't depend on the order of items except in the
195 // case when there is not enough space for even the fixed width items
196 // (in which case the first ones might get enough of it but not the
197 // last ones) so test a couple of permutations of test data unless
198 // specifically disabled for this test case
199 for ( unsigned p
= 0; p
< 3; p
++)
204 // nothing to do, use original data
208 // exchange first and last elements
209 wxSwap(ltd
.prop
[0], ltd
.prop
[2]);
210 wxSwap(ltd
.minsize
[0], ltd
.minsize
[2]);
211 wxSwap(ltd
.sizes
[0], ltd
.sizes
[2]);
215 // exchange the original third and second elements
216 wxSwap(ltd
.prop
[0], ltd
.prop
[1]);
217 wxSwap(ltd
.minsize
[0], ltd
.minsize
[1]);
218 wxSwap(ltd
.sizes
[0], ltd
.sizes
[1]);
225 for ( j
= 0; j
< WXSIZEOF(child
); j
++ )
226 ltd
.AddToSizer(m_sizer
, child
[j
], j
);
228 m_win
->SetClientSize(ltd
.x
, -1);
231 for ( j
= 0; j
< WXSIZEOF(child
); j
++ )
233 WX_ASSERT_EQUAL_MESSAGE
236 "test %lu, permutation #%lu: wrong size for child #%d "
238 static_cast<unsigned long>(i
),
239 static_cast<unsigned long>(p
),
243 ltd
.sizes
[j
], child
[j
]->GetSize().x
247 // don't try other permutations if explicitly disabled
248 if ( ltd
.dontPermute
)
254 void BoxSizerTestCase::CalcMin()
256 static const unsigned NUM_TEST_ITEM
= 3;
258 static const struct CalcMinTestData
260 // proportions of the elements, if one of them is -1 it means to not
261 // use this window at all in this test
262 int prop
[NUM_TEST_ITEM
];
264 // minimal sizes of the elements in the sizer direction
265 int minsize
[NUM_TEST_ITEM
];
267 // the expected minimal sizer size
269 } calcMinTestData
[] =
271 { { 1, 1, -1 }, { 30, 50, 0 }, 100 },
272 { { 1, 1, 0 }, { 30, 50, 20 }, 120 },
273 { { 10, 10, -1 }, { 30, 50, 0 }, 100 },
274 { { 1, 2, 2 }, { 50, 50, 80 }, 250 },
275 { { 1, 2, 2 }, { 100, 50, 80 }, 500 },
279 wxWindow
*child
[NUM_TEST_ITEM
];
280 for ( n
= 0; n
< NUM_TEST_ITEM
; n
++ )
281 child
[n
] = new wxWindow(m_win
, wxID_ANY
);
283 for ( unsigned i
= 0; i
< WXSIZEOF(calcMinTestData
); i
++ )
287 const CalcMinTestData
& cmtd
= calcMinTestData
[i
];
288 for ( n
= 0; n
< NUM_TEST_ITEM
; n
++ )
290 if ( cmtd
.prop
[n
] != -1 )
292 child
[n
]->SetInitialSize(wxSize(cmtd
.minsize
[n
], -1));
293 m_sizer
->Add(child
[n
], wxSizerFlags(cmtd
.prop
[n
]));
297 WX_ASSERT_EQUAL_MESSAGE
300 cmtd
.total
, m_sizer
->CalcMin().x