1 // Testing canvas layouting system
3 // Copyright (C) 2014 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define BOOST_TEST_MODULE canvas_layout
20 #include <BoostTestTargetConfig.h>
22 #include "BoxLayout.hxx"
23 #include "NasalWidget.hxx"
25 #include <simgear/debug/logstream.hxx>
28 //------------------------------------------------------------------------------
29 struct SetLogLevelFixture
33 sglog().set_log_priority(SG_DEBUG);
36 BOOST_GLOBAL_FIXTURE(SetLogLevelFixture);
38 //------------------------------------------------------------------------------
39 namespace sc = simgear::canvas;
45 TestWidget( const SGVec2i& min_size,
46 const SGVec2i& size_hint,
47 const SGVec2i& max_size = MAX_SIZE )
49 _size_hint = size_hint;
54 TestWidget(const TestWidget& rhs)
56 _size_hint = rhs._size_hint;
57 _min_size = rhs._min_size;
58 _max_size = rhs._max_size;
61 void setMinSize(const SGVec2i& size) { _min_size = size; }
62 void setMaxSize(const SGVec2i& size) { _max_size = size; }
63 void setSizeHint(const SGVec2i& size) { _size_hint = size; }
65 virtual void setGeometry(const SGRecti& geom) { _geom = geom; }
66 virtual SGRecti geometry() const { return _geom; }
72 virtual SGVec2i sizeHintImpl() const { return _size_hint; }
73 virtual SGVec2i minimumSizeImpl() const { return _min_size; }
74 virtual SGVec2i maximumSizeImpl() const { return _max_size; }
81 TestWidgetHFW( const SGVec2i& min_size,
82 const SGVec2i& size_hint,
83 const SGVec2i& max_size = MAX_SIZE ):
84 TestWidget(min_size, size_hint, max_size)
89 virtual bool hasHeightForWidth() const
94 virtual int heightForWidth(int w) const
96 return _size_hint.x() * _size_hint.y() / w;
99 virtual int minimumHeightForWidth(int w) const
101 return _min_size.x() * _min_size.y() / w;
105 typedef SGSharedPtr<TestWidget> TestWidgetRef;
107 //------------------------------------------------------------------------------
108 BOOST_AUTO_TEST_CASE( horizontal_layout )
110 sc::BoxLayout box_layout(sc::BoxLayout::BottomToTop);
111 box_layout.setSpacing(5);
113 BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::BottomToTop);
114 BOOST_CHECK_EQUAL(box_layout.spacing(), 5);
116 box_layout.setDirection(sc::BoxLayout::LeftToRight);
117 box_layout.setSpacing(9);
119 BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::LeftToRight);
120 BOOST_CHECK_EQUAL(box_layout.spacing(), 9);
122 TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
125 box_layout.addItem(fixed_size_widget);
127 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(16, 16));
128 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(16, 16));
129 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(16, 16));
131 TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
133 SGVec2i(256, 64) ) );
134 box_layout.addItem(limited_resize_widget);
136 // Combined sizes of both widget plus the padding between them
137 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(41, 16));
138 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(57, 32));
139 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(281, 64));
141 // Test with different spacing/padding
142 box_layout.setSpacing(5);
144 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(37, 16));
145 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(53, 32));
146 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(277, 64));
148 box_layout.setGeometry(SGRecti(0, 0, 128, 32));
150 // Fixed size for first widget and remaining space goes to second widget
151 BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(0, 8, 16, 16));
152 BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(21, 0, 107, 32));
154 TestWidgetRef stretch_widget( new TestWidget( SGVec2i(16, 16),
156 SGVec2i(128, 32) ) );
157 box_layout.addItem(stretch_widget, 1);
160 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(58, 16));
161 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(90, 32));
162 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(410, 64));
164 // Due to the stretch factor only the last widget gets additional space. All
165 // other widgets get the preferred size.
166 BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(0, 8, 16, 16));
167 BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(21, 0, 32, 32));
168 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(58, 0, 70, 32));
170 // Test stretch factor
171 TestWidgetRef fast_stretch( new TestWidget(*stretch_widget) );
172 sc::BoxLayout box_layout_stretch(sc::BoxLayout::LeftToRight);
174 box_layout_stretch.addItem(stretch_widget, 1);
175 box_layout_stretch.addItem(fast_stretch, 2);
177 box_layout_stretch.setGeometry(SGRecti(0,0,128,32));
179 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 41, 32));
180 BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(46, 0, 82, 32));
182 box_layout_stretch.setGeometry(SGRecti(0,0,256,32));
184 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 123, 32));
185 BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(128, 0, 128, 32));
187 // Test superflous space to padding
188 box_layout_stretch.setGeometry(SGRecti(0,0,512,32));
190 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(83, 0, 128, 32));
191 BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(300, 0, 128, 32));
193 // Test more space then preferred, but less than maximum
196 TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
198 SGVec2i(9999, 32) ) ),
199 w2( new TestWidget(*w1) );
204 hbox.setGeometry( SGRecti(0, 0, 256, 32) );
206 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 126, 32));
207 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(131, 0, 125, 32));
209 hbox.setStretch(0, 1);
210 hbox.setStretch(1, 1);
212 BOOST_CHECK_EQUAL(hbox.stretch(0), 1);
213 BOOST_CHECK_EQUAL(hbox.stretch(1), 1);
217 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 125, 32));
218 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(130, 0, 126, 32));
222 //------------------------------------------------------------------------------
223 BOOST_AUTO_TEST_CASE( spacer_layouting )
226 TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
228 SGVec2i(9999, 9999) ) ),
229 w2( new TestWidget(*w1) );
235 BOOST_CHECK_EQUAL(hbox.minimumSize(), SGVec2i(37, 16));
236 BOOST_CHECK_EQUAL(hbox.sizeHint(), SGVec2i(69, 32));
237 BOOST_CHECK_EQUAL(hbox.maximumSize(), sc::LayoutItem::MAX_SIZE);
239 hbox.setGeometry(SGRecti(0, 0, 256, 40));
241 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 32, 40));
242 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(37, 0, 32, 40));
244 // now center with increased spacing between both widgets
245 hbox.insertStretch(0, 1);
246 hbox.insertSpacing(2, 10);
248 BOOST_CHECK_EQUAL(hbox.minimumSize(), SGVec2i(47, 16));
249 BOOST_CHECK_EQUAL(hbox.sizeHint(), SGVec2i(79, 32));
250 BOOST_CHECK_EQUAL(hbox.maximumSize(), sc::LayoutItem::MAX_SIZE);
254 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(88, 0, 32, 40));
255 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(135, 0, 32, 40));
258 //------------------------------------------------------------------------------
259 BOOST_AUTO_TEST_CASE( vertical_layout)
261 sc::BoxLayout vbox(sc::BoxLayout::TopToBottom);
264 TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
267 TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
269 SGVec2i(256, 64) ) );
271 vbox.addItem(fixed_size_widget);
272 vbox.addItem(limited_resize_widget);
274 BOOST_CHECK_EQUAL(vbox.minimumSize(), SGVec2i(16, 39));
275 BOOST_CHECK_EQUAL(vbox.sizeHint(), SGVec2i(32, 55));
276 BOOST_CHECK_EQUAL(vbox.maximumSize(), SGVec2i(256, 87));
278 vbox.setGeometry(SGRecti(10, 20, 16, 55));
280 BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(10, 20, 16, 16));
281 BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(10, 43, 16, 32));
283 vbox.setDirection(sc::BoxLayout::BottomToTop);
286 //------------------------------------------------------------------------------
287 BOOST_AUTO_TEST_CASE( boxlayout_insert_remove )
291 BOOST_CHECK_EQUAL(hbox.count(), 0);
292 BOOST_CHECK(!hbox.itemAt(0));
293 BOOST_CHECK(!hbox.takeAt(0));
295 TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
297 SGVec2i(9999, 32) ) ),
298 w2( new TestWidget(*w1) );
301 BOOST_CHECK_EQUAL(hbox.count(), 1);
302 BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
304 hbox.insertItem(0, w2);
305 BOOST_CHECK_EQUAL(hbox.count(), 2);
306 BOOST_CHECK_EQUAL(hbox.itemAt(0), w2);
307 BOOST_CHECK_EQUAL(hbox.itemAt(1), w1);
310 BOOST_CHECK_EQUAL(hbox.count(), 1);
311 BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
314 //------------------------------------------------------------------------------
315 BOOST_AUTO_TEST_CASE( boxlayout_hfw )
317 TestWidgetRef w1( new TestWidgetHFW( SGVec2i(16, 16),
319 w2( new TestWidgetHFW( SGVec2i(24, 24),
322 BOOST_CHECK_EQUAL(w1->heightForWidth(16), 64);
323 BOOST_CHECK_EQUAL(w1->minimumHeightForWidth(16), 16);
324 BOOST_CHECK_EQUAL(w2->heightForWidth(24), 96);
325 BOOST_CHECK_EQUAL(w2->minimumHeightForWidth(24), 24);
327 TestWidgetRef w_no_hfw( new TestWidget( SGVec2i(16, 16),
329 BOOST_CHECK(!w_no_hfw->hasHeightForWidth());
330 BOOST_CHECK_EQUAL(w_no_hfw->heightForWidth(16), -1);
331 BOOST_CHECK_EQUAL(w_no_hfw->minimumHeightForWidth(16), -1);
339 BOOST_CHECK_EQUAL(hbox.heightForWidth(45), w2->heightForWidth(24));
340 BOOST_CHECK_EQUAL(hbox.heightForWidth(85), w2->heightForWidth(48));
342 hbox.addItem(w_no_hfw);
344 BOOST_CHECK_EQUAL(hbox.heightForWidth(66), 96);
345 BOOST_CHECK_EQUAL(hbox.heightForWidth(122), 48);
346 BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(66), 24);
347 BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(122), 16);
349 hbox.setGeometry(SGRecti(0, 0, 66, 24));
351 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 16, 24));
352 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(21, 0, 24, 24));
353 BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(50, 0, 16, 24));
361 BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 143);
362 BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 74);
363 BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 39);
364 BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 22);
366 vbox.addItem(w_no_hfw);
368 BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 180);
369 BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 111);
370 BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 60);
371 BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 43);
373 SGVec2i min_size = vbox.minimumSize(),
374 size_hint = vbox.sizeHint();
376 BOOST_CHECK_EQUAL(min_size, SGVec2i(24, 66));
377 BOOST_CHECK_EQUAL(size_hint, SGVec2i(48, 122));
379 vbox.setGeometry(SGRecti(0, 0, 24, 122));
381 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 24, 33));
382 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 38, 24, 47));
383 BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 90, 24, 32));
385 // Vertical layouting modifies size hints, so check if they are correctly
387 BOOST_CHECK_EQUAL(min_size, vbox.minimumSize());
388 BOOST_CHECK_EQUAL(size_hint, vbox.sizeHint());
390 vbox.setGeometry(SGRecti(0, 0, 50, 122));
392 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 50, 44));
393 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 49, 50, 70));
394 BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 124, 50, 56));
397 //------------------------------------------------------------------------------
398 BOOST_AUTO_TEST_CASE( nasal_layout )
400 naContext c = naNewContext();
401 naRef me = naNewHash(c);
403 sc::LayoutItemRef nasal_item( new sc::NasalWidget(me) );