]>
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_SUITE_END();
54 DECLARE_NO_COPY_CLASS(BoxSizerTestCase
)
57 // register in the unnamed registry so that these tests are run by default
58 CPPUNIT_TEST_SUITE_REGISTRATION( BoxSizerTestCase
);
60 // also include in it's own registry so that these tests can be run alone
61 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( BoxSizerTestCase
, "BoxSizerTestCase" );
63 // ----------------------------------------------------------------------------
64 // test initialization
65 // ----------------------------------------------------------------------------
67 void BoxSizerTestCase::setUp()
69 m_win
= new wxWindow(wxTheApp
->GetTopWindow(), wxID_ANY
);
70 m_win
->SetClientSize(127, 35);
72 m_sizer
= new wxBoxSizer(wxHORIZONTAL
);
73 m_win
->SetSizer(m_sizer
);
76 void BoxSizerTestCase::tearDown()
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 void BoxSizerTestCase::Size1()
90 const wxSize sizeTotal
= m_win
->GetClientSize();
91 const wxSize sizeChild
= sizeTotal
/ 2;
94 child
= new wxWindow(m_win
, wxID_ANY
, wxDefaultPosition
, sizeChild
);
97 CPPUNIT_ASSERT_EQUAL( sizeChild
, child
->GetSize() );
100 m_sizer
->Add(child
, wxSizerFlags(1));
102 CPPUNIT_ASSERT_EQUAL( wxSize(sizeTotal
.x
, sizeChild
.y
), child
->GetSize() );
105 m_sizer
->Add(child
, wxSizerFlags(1).Expand());
107 CPPUNIT_ASSERT_EQUAL( sizeTotal
, child
->GetSize() );
110 m_sizer
->Add(child
, wxSizerFlags());
111 m_sizer
->SetItemMinSize(child
, sizeTotal
*2);
113 CPPUNIT_ASSERT_EQUAL( sizeTotal
, child
->GetSize() );
116 m_sizer
->Add(child
, wxSizerFlags().Expand());
117 m_sizer
->SetItemMinSize(child
, sizeTotal
*2);
119 CPPUNIT_ASSERT_EQUAL( sizeTotal
, child
->GetSize() );
122 void BoxSizerTestCase::Size3()
124 // check that various combinations of minimal sizes and proportions work as
125 // expected for different window sizes
126 static const struct LayoutTestData
128 // proportions of the elements
131 // minimal sizes of the elements in the sizer direction
134 // total size and the expected sizes of the elements
138 // if true, don't try the permutations of our test data
142 // Add the given window to the sizer with the corresponding parameters
143 void AddToSizer(wxSizer
*sizer
, wxWindow
*win
, int n
) const
145 sizer
->Add(win
, wxSizerFlags(prop
[n
]));
146 sizer
->SetItemMinSize(win
, wxSize(minsize
[n
], -1));
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 },
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, } },
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, } },
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 },
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
);
188 for ( unsigned i
= 0; i
< WXSIZEOF(layoutTestData
); i
++ )
190 LayoutTestData ltd
= layoutTestData
[i
];
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
++)
202 // nothing to do, use original data
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]);
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]);
223 for ( j
= 0; j
< WXSIZEOF(child
); j
++ )
224 ltd
.AddToSizer(m_sizer
, child
[j
], j
);
226 m_win
->SetClientSize(ltd
.x
, -1);
229 for ( j
= 0; j
< WXSIZEOF(child
); j
++ )
231 WX_ASSERT_EQUAL_MESSAGE
234 "test %lu, permutation #%d: wrong size for child #%d "
236 static_cast<unsigned long>(i
),
237 static_cast<unsigned long>(p
),
241 ltd
.sizes
[j
], child
[j
]->GetSize().x
245 // don't try other permutations if explicitly disabled
246 if ( ltd
.dontPermute
)