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>
26 #include <simgear/nasal/cppbind/NasalContext.hxx>
30 //------------------------------------------------------------------------------
31 struct SetLogLevelFixture
35 sglog().set_log_priority(SG_DEBUG);
38 BOOST_GLOBAL_FIXTURE(SetLogLevelFixture);
40 //------------------------------------------------------------------------------
41 namespace sc = simgear::canvas;
47 TestWidget( const SGVec2i& min_size,
48 const SGVec2i& size_hint,
49 const SGVec2i& max_size = MAX_SIZE )
51 _size_hint = size_hint;
56 TestWidget(const TestWidget& rhs)
58 _size_hint = rhs._size_hint;
59 _min_size = rhs._min_size;
60 _max_size = rhs._max_size;
63 void setMinSize(const SGVec2i& size) { _min_size = size; }
64 void setMaxSize(const SGVec2i& size) { _max_size = size; }
65 void setSizeHint(const SGVec2i& size) { _size_hint = size; }
69 virtual SGVec2i sizeHintImpl() const { return _size_hint; }
70 virtual SGVec2i minimumSizeImpl() const { return _min_size; }
71 virtual SGVec2i maximumSizeImpl() const { return _max_size; }
73 virtual void visibilityChanged(bool visible)
76 _geometry.set(0, 0, 0, 0);
84 TestWidgetHFW( const SGVec2i& min_size,
85 const SGVec2i& size_hint,
86 const SGVec2i& max_size = MAX_SIZE ):
87 TestWidget(min_size, size_hint, max_size)
92 virtual bool hasHeightForWidth() const
97 virtual int heightForWidthImpl(int w) const
99 return _size_hint.x() * _size_hint.y() / w;
102 virtual int minimumHeightForWidthImpl(int w) const
104 return _min_size.x() * _min_size.y() / w;
108 typedef SGSharedPtr<TestWidget> TestWidgetRef;
110 //------------------------------------------------------------------------------
111 BOOST_AUTO_TEST_CASE( horizontal_layout )
113 sc::BoxLayout box_layout(sc::BoxLayout::BottomToTop);
114 box_layout.setSpacing(5);
116 BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::BottomToTop);
117 BOOST_CHECK_EQUAL(box_layout.spacing(), 5);
119 box_layout.setDirection(sc::BoxLayout::LeftToRight);
120 box_layout.setSpacing(9);
122 BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::LeftToRight);
123 BOOST_CHECK_EQUAL(box_layout.spacing(), 9);
125 TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
128 box_layout.addItem(fixed_size_widget);
130 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(16, 16));
131 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(16, 16));
132 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(16, 16));
134 TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
136 SGVec2i(256, 64) ) );
137 box_layout.addItem(limited_resize_widget);
139 // Combined sizes of both widget plus the padding between them
140 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(41, 16));
141 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(57, 32));
142 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(281, 64));
144 // Test with different spacing/padding
145 box_layout.setSpacing(5);
147 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(37, 16));
148 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(53, 32));
149 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(277, 64));
151 box_layout.setGeometry(SGRecti(0, 0, 128, 32));
153 // Fixed size for first widget and remaining space goes to second widget
154 BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(0, 8, 16, 16));
155 BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(21, 0, 107, 32));
157 TestWidgetRef stretch_widget( new TestWidget( SGVec2i(16, 16),
159 SGVec2i(128, 32) ) );
160 box_layout.addItem(stretch_widget, 1);
163 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(58, 16));
164 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(90, 32));
165 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(410, 64));
167 // Due to the stretch factor only the last widget gets additional space. All
168 // other widgets get the preferred size.
169 BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(0, 8, 16, 16));
170 BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(21, 0, 32, 32));
171 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(58, 0, 70, 32));
173 // Test stretch factor
174 TestWidgetRef fast_stretch( new TestWidget(*stretch_widget) );
175 sc::BoxLayout box_layout_stretch(sc::BoxLayout::LeftToRight);
177 box_layout_stretch.addItem(stretch_widget, 1);
178 box_layout_stretch.addItem(fast_stretch, 2);
180 box_layout_stretch.setGeometry(SGRecti(0,0,128,32));
182 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 41, 32));
183 BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(46, 0, 82, 32));
185 box_layout_stretch.setGeometry(SGRecti(0,0,256,32));
187 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 123, 32));
188 BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(128, 0, 128, 32));
190 // Test superflous space to padding
191 box_layout_stretch.setGeometry(SGRecti(0,0,512,32));
193 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(83, 0, 128, 32));
194 BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(300, 0, 128, 32));
196 // Test more space then preferred, but less than maximum
199 TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
201 SGVec2i(9999, 32) ) ),
202 w2( new TestWidget(*w1) );
207 hbox.setGeometry( SGRecti(0, 0, 256, 32) );
209 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 126, 32));
210 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(131, 0, 125, 32));
212 hbox.setStretch(0, 1);
213 hbox.setStretch(1, 1);
215 BOOST_CHECK_EQUAL(hbox.stretch(0), 1);
216 BOOST_CHECK_EQUAL(hbox.stretch(1), 1);
220 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 125, 32));
221 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(130, 0, 126, 32));
223 BOOST_REQUIRE( hbox.setStretchFactor(w1, 2) );
224 BOOST_REQUIRE( hbox.setStretchFactor(w2, 3) );
225 BOOST_CHECK_EQUAL(hbox.stretch(0), 2);
226 BOOST_CHECK_EQUAL(hbox.stretch(1), 3);
230 BOOST_CHECK( !hbox.setStretchFactor(w1, 0) );
234 //------------------------------------------------------------------------------
235 BOOST_AUTO_TEST_CASE( spacer_layouting )
238 TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
240 SGVec2i(9999, 9999) ) ),
241 w2( new TestWidget(*w1) );
247 BOOST_CHECK_EQUAL(hbox.minimumSize(), SGVec2i(37, 16));
248 BOOST_CHECK_EQUAL(hbox.sizeHint(), SGVec2i(69, 32));
249 BOOST_CHECK_EQUAL(hbox.maximumSize(), sc::LayoutItem::MAX_SIZE);
251 hbox.setGeometry(SGRecti(0, 0, 256, 40));
253 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 32, 40));
254 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(37, 0, 32, 40));
256 // now center with increased spacing between both widgets
257 hbox.insertStretch(0, 1);
258 hbox.insertSpacing(2, 10);
260 BOOST_CHECK_EQUAL(hbox.minimumSize(), SGVec2i(47, 16));
261 BOOST_CHECK_EQUAL(hbox.sizeHint(), SGVec2i(79, 32));
262 BOOST_CHECK_EQUAL(hbox.maximumSize(), sc::LayoutItem::MAX_SIZE);
266 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(88, 0, 32, 40));
267 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(135, 0, 32, 40));
270 //------------------------------------------------------------------------------
271 BOOST_AUTO_TEST_CASE( vertical_layout)
273 sc::BoxLayout vbox(sc::BoxLayout::TopToBottom);
276 TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
279 TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
281 SGVec2i(256, 64) ) );
283 vbox.addItem(fixed_size_widget);
284 vbox.addItem(limited_resize_widget);
286 BOOST_CHECK_EQUAL(vbox.minimumSize(), SGVec2i(16, 39));
287 BOOST_CHECK_EQUAL(vbox.sizeHint(), SGVec2i(32, 55));
288 BOOST_CHECK_EQUAL(vbox.maximumSize(), SGVec2i(256, 87));
290 vbox.setGeometry(SGRecti(10, 20, 16, 55));
292 BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(10, 20, 16, 16));
293 BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(10, 43, 16, 32));
295 vbox.setDirection(sc::BoxLayout::BottomToTop);
298 //------------------------------------------------------------------------------
299 BOOST_AUTO_TEST_CASE( boxlayout_insert_remove )
301 sc::BoxLayoutRef hbox( new sc::HBoxLayout );
303 BOOST_CHECK_EQUAL(hbox->count(), 0);
304 BOOST_CHECK(!hbox->itemAt(0));
305 BOOST_CHECK(!hbox->takeAt(0));
307 TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
309 SGVec2i(9999, 32) ) ),
310 w2( new TestWidget(*w1) );
313 BOOST_CHECK_EQUAL(hbox->count(), 1);
314 BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
315 BOOST_CHECK_EQUAL(w1->getParent(), hbox);
317 hbox->insertItem(0, w2);
318 BOOST_CHECK_EQUAL(hbox->count(), 2);
319 BOOST_CHECK_EQUAL(hbox->itemAt(0), w2);
320 BOOST_CHECK_EQUAL(hbox->itemAt(1), w1);
321 BOOST_CHECK_EQUAL(w2->getParent(), hbox);
323 hbox->removeItem(w2);
324 BOOST_CHECK_EQUAL(hbox->count(), 1);
325 BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
326 BOOST_CHECK( !w2->getParent() );
329 BOOST_CHECK_EQUAL(hbox->count(), 2);
330 BOOST_CHECK_EQUAL(w2->getParent(), hbox);
333 BOOST_CHECK_EQUAL(hbox->count(), 0);
334 BOOST_CHECK( !w1->getParent() );
335 BOOST_CHECK( !w2->getParent() );
338 //------------------------------------------------------------------------------
339 BOOST_AUTO_TEST_CASE( boxlayout_visibility )
341 sc::BoxLayoutRef hbox( new sc::HBoxLayout );
342 TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
344 w2( new TestWidget(*w1) ),
345 w3( new TestWidget(*w1) );
351 BOOST_REQUIRE_EQUAL(hbox->sizeHint().x(), 3 * 32 + 2 * hbox->spacing());
353 hbox->setGeometry(SGRecti(0, 0, 69, 32));
355 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 20, 32));
356 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(25, 0, 20, 32));
357 BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(50, 0, 19, 32));
359 w2->setVisible(false);
361 BOOST_REQUIRE(hbox->isVisible());
362 BOOST_REQUIRE(w1->isVisible());
363 BOOST_REQUIRE(!w2->isVisible());
364 BOOST_REQUIRE(w2->isExplicitlyHidden());
365 BOOST_REQUIRE(w3->isVisible());
367 BOOST_CHECK_EQUAL(hbox->sizeHint().x(), 2 * 32 + 1 * hbox->spacing());
371 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 32, 32));
372 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 0, 0, 0));
373 BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(37, 0, 32, 32));
375 hbox->setVisible(false);
377 BOOST_REQUIRE(!hbox->isVisible());
378 BOOST_REQUIRE(hbox->isExplicitlyHidden());
379 BOOST_REQUIRE(!w1->isVisible());
380 BOOST_REQUIRE(!w1->isExplicitlyHidden());
381 BOOST_REQUIRE(!w2->isVisible());
382 BOOST_REQUIRE(w2->isExplicitlyHidden());
383 BOOST_REQUIRE(!w3->isVisible());
384 BOOST_REQUIRE(!w3->isExplicitlyHidden());
386 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 0, 0));
387 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 0, 0, 0));
388 BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(0, 0, 0, 0));
390 w2->setVisible(true);
392 BOOST_REQUIRE(!w2->isVisible());
393 BOOST_REQUIRE(!w2->isExplicitlyHidden());
395 hbox->setVisible(true);
397 BOOST_REQUIRE(hbox->isVisible());
398 BOOST_REQUIRE(w1->isVisible());
399 BOOST_REQUIRE(w2->isVisible());
400 BOOST_REQUIRE(w3->isVisible());
404 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 20, 32));
405 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(25, 0, 20, 32));
406 BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(50, 0, 19, 32));
409 //------------------------------------------------------------------------------
410 BOOST_AUTO_TEST_CASE( boxlayout_contents_margins )
414 BOOST_REQUIRE(m.isNull());
418 BOOST_REQUIRE_EQUAL(m.l, 5);
419 BOOST_REQUIRE_EQUAL(m.t, 5);
420 BOOST_REQUIRE_EQUAL(m.r, 5);
421 BOOST_REQUIRE_EQUAL(m.b, 5);
423 m = sc::Margins(6, 7);
425 BOOST_REQUIRE_EQUAL(m.l, 6);
426 BOOST_REQUIRE_EQUAL(m.t, 7);
427 BOOST_REQUIRE_EQUAL(m.r, 6);
428 BOOST_REQUIRE_EQUAL(m.b, 7);
430 BOOST_REQUIRE_EQUAL(m.horiz(), 12);
431 BOOST_REQUIRE_EQUAL(m.vert(), 14);
432 BOOST_REQUIRE(!m.isNull());
434 m = sc::Margins(1, 2, 3, 4);
436 BOOST_REQUIRE_EQUAL(m.l, 1);
437 BOOST_REQUIRE_EQUAL(m.t, 2);
438 BOOST_REQUIRE_EQUAL(m.r, 3);
439 BOOST_REQUIRE_EQUAL(m.b, 4);
441 BOOST_REQUIRE_EQUAL(m.horiz(), 4);
442 BOOST_REQUIRE_EQUAL(m.vert(), 6);
443 BOOST_REQUIRE_EQUAL(m.size(), SGVec2i(4, 6));
445 sc::BoxLayoutRef hbox( new sc::HBoxLayout );
447 hbox->setContentsMargins(5, 10, 15, 20);
449 BOOST_CHECK_EQUAL(hbox->minimumSize(), SGVec2i(20, 30));
450 BOOST_CHECK_EQUAL(hbox->sizeHint(), SGVec2i(20, 30));
451 BOOST_CHECK_EQUAL(hbox->maximumSize(), SGVec2i(20, 30));
453 hbox->setGeometry(SGRecti(0, 0, 30, 40));
455 BOOST_CHECK_EQUAL(hbox->contentsRect(), SGRecti(5, 10, 10, 10));
457 TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
459 w2( new TestWidget(*w1) ),
460 w3( new TestWidget(*w1) );
462 w1->setContentsMargin(5);
463 w2->setContentsMargin(6);
464 w3->setContentsMargin(7);
466 BOOST_CHECK_EQUAL(w1->minimumSize(), SGVec2i(26, 26));
467 BOOST_CHECK_EQUAL(w1->sizeHint(), SGVec2i(42, 42));
468 BOOST_CHECK_EQUAL(w1->maximumSize(), sc::LayoutItem::MAX_SIZE);
470 BOOST_CHECK_EQUAL(w2->minimumSize(), SGVec2i(28, 28));
471 BOOST_CHECK_EQUAL(w2->sizeHint(), SGVec2i(44, 44));
472 BOOST_CHECK_EQUAL(w2->maximumSize(), sc::LayoutItem::MAX_SIZE);
474 BOOST_CHECK_EQUAL(w3->minimumSize(), SGVec2i(30, 30));
475 BOOST_CHECK_EQUAL(w3->sizeHint(), SGVec2i(46, 46));
476 BOOST_CHECK_EQUAL(w3->maximumSize(), sc::LayoutItem::MAX_SIZE);
482 BOOST_CHECK_EQUAL(hbox->minimumSize(), SGVec2i(114, 60));
483 BOOST_CHECK_EQUAL(hbox->sizeHint(), SGVec2i(162, 76));
484 BOOST_CHECK_EQUAL(hbox->maximumSize(), sc::LayoutItem::MAX_SIZE);
486 hbox->setGeometry(SGRecti(0, 0, hbox->sizeHint().x(), hbox->sizeHint().y()));
488 BOOST_CHECK_EQUAL(hbox->contentsRect(), SGRecti(5, 10, 142, 46));
490 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(5, 10, 42, 46));
491 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(52, 10, 44, 46));
492 BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(101, 10, 46, 46));
494 BOOST_CHECK_EQUAL(w1->contentsRect(), SGRecti(10, 15, 32, 36));
495 BOOST_CHECK_EQUAL(w2->contentsRect(), SGRecti(58, 16, 32, 34));
496 BOOST_CHECK_EQUAL(w3->contentsRect(), SGRecti(108, 17, 32, 32));
499 //------------------------------------------------------------------------------
500 BOOST_AUTO_TEST_CASE( boxlayout_hfw )
502 TestWidgetRef w1( new TestWidgetHFW( SGVec2i(16, 16),
504 w2( new TestWidgetHFW( SGVec2i(24, 24),
507 BOOST_CHECK_EQUAL(w1->heightForWidth(16), 64);
508 BOOST_CHECK_EQUAL(w1->minimumHeightForWidth(16), 16);
509 BOOST_CHECK_EQUAL(w2->heightForWidth(24), 96);
510 BOOST_CHECK_EQUAL(w2->minimumHeightForWidth(24), 24);
512 TestWidgetRef w_no_hfw( new TestWidget( SGVec2i(16, 16),
514 BOOST_CHECK(!w_no_hfw->hasHeightForWidth());
515 BOOST_CHECK_EQUAL(w_no_hfw->heightForWidth(16), -1);
516 BOOST_CHECK_EQUAL(w_no_hfw->minimumHeightForWidth(16), -1);
524 BOOST_CHECK_EQUAL(hbox.heightForWidth(45), w2->heightForWidth(24));
525 BOOST_CHECK_EQUAL(hbox.heightForWidth(85), w2->heightForWidth(48));
527 hbox.addItem(w_no_hfw);
529 BOOST_CHECK_EQUAL(hbox.heightForWidth(66), 96);
530 BOOST_CHECK_EQUAL(hbox.heightForWidth(122), 48);
531 BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(66), 24);
532 BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(122), 16);
534 hbox.setGeometry(SGRecti(0, 0, 66, 24));
536 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 16, 24));
537 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(21, 0, 24, 24));
538 BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(50, 0, 16, 24));
546 BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 143);
547 BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 74);
548 BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 39);
549 BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 22);
551 vbox.addItem(w_no_hfw);
553 BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 180);
554 BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 111);
555 BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 60);
556 BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 43);
558 SGVec2i min_size = vbox.minimumSize(),
559 size_hint = vbox.sizeHint();
561 BOOST_CHECK_EQUAL(min_size, SGVec2i(24, 66));
562 BOOST_CHECK_EQUAL(size_hint, SGVec2i(48, 122));
564 vbox.setGeometry(SGRecti(0, 0, 24, 122));
566 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 24, 33));
567 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 38, 24, 47));
568 BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 90, 24, 32));
570 // Vertical layouting modifies size hints, so check if they are correctly
572 BOOST_CHECK_EQUAL(min_size, vbox.minimumSize());
573 BOOST_CHECK_EQUAL(size_hint, vbox.sizeHint());
575 vbox.setGeometry(SGRecti(0, 0, 50, 122));
577 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 50, 25));
578 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 30, 50, 51));
579 BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 86, 50, 36));
581 // Same geometry as before -> should get same widget geometry
582 // (check internal size hint cache updates correctly)
583 vbox.setGeometry(SGRecti(0, 0, 24, 122));
585 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 24, 33));
586 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 38, 24, 47));
587 BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 90, 24, 32));
590 //------------------------------------------------------------------------------
591 // TODO extend to_nasal_helper for automatic argument conversion
592 static naRef f_Widget_visibilityChanged(nasal::CallContext ctx)
594 sc::NasalWidget* w = ctx.from_nasal<sc::NasalWidget*>(ctx.me);
596 if( !ctx.requireArg<bool>(0) )
597 w->setGeometry(SGRecti(0, 0, -1, -1));
602 //------------------------------------------------------------------------------
603 BOOST_AUTO_TEST_CASE( nasal_widget )
606 nasal::Hash globals = c.newHash();
608 nasal::Object::setupGhost();
609 nasal::Ghost<sc::LayoutItemRef>::init("LayoutItem");
610 sc::NasalWidget::setupGhost(globals);
612 nasal::Hash me = c.newHash();
613 me.set("visibilityChanged", &f_Widget_visibilityChanged);
614 sc::NasalWidgetRef w( new sc::NasalWidget(me.get_naRef()) );
616 // Default layout sizes (no user set values)
617 BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(16, 16));
618 BOOST_CHECK_EQUAL(w->sizeHint(), SGVec2i(32, 32));
619 BOOST_CHECK_EQUAL(w->maximumSize(), sc::LayoutItem::MAX_SIZE);
621 // Changed layout sizes
622 w->setLayoutMinimumSize( SGVec2i(2, 12) );
623 w->setLayoutSizeHint( SGVec2i(3, 13) );
624 w->setLayoutMaximumSize( SGVec2i(4, 14) );
626 BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(2, 12));
627 BOOST_CHECK_EQUAL(w->sizeHint(), SGVec2i(3, 13));
628 BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(4, 14));
630 // User set values (overwrite layout sizes)
631 w->setMinimumSize( SGVec2i(15, 16) );
632 w->setSizeHint( SGVec2i(17, 18) );
633 w->setMaximumSize( SGVec2i(19, 20) );
635 BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(15, 16));
636 BOOST_CHECK_EQUAL(w->sizeHint(), SGVec2i(17, 18));
637 BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(19, 20));
639 // Only vertical user set values (layout/default for horizontal hints)
640 w->setMinimumSize( SGVec2i(0, 21) );
641 w->setSizeHint( SGVec2i(0, 22) );
642 w->setMaximumSize( SGVec2i(SGLimits<int>::max(), 23) );
644 BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(2, 21));
645 BOOST_CHECK_EQUAL(w->sizeHint(), SGVec2i(3, 22));
646 BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(4, 23));
648 w->setVisible(false);
649 BOOST_CHECK_EQUAL(w->geometry(), SGRecti(0, 0, -1, -1));