]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/layout/canvas_layout_test.cxx
canvas::Layout: support for contents margins.
[simgear.git] / simgear / canvas / layout / canvas_layout_test.cxx
index b05afd48b68a330d58f2f19c1811e0858ff30acf..a95aa76035db2ed0ed776a638123a933cc3d25f6 100644 (file)
@@ -23,6 +23,8 @@
 #include "NasalWidget.hxx"
 
 #include <simgear/debug/logstream.hxx>
+#include <simgear/nasal/cppbind/NasalContext.hxx>
+
 #include <cstdlib>
 
 //------------------------------------------------------------------------------
@@ -44,7 +46,7 @@ class TestWidget:
   public:
     TestWidget( const SGVec2i& min_size,
                 const SGVec2i& size_hint,
-                const SGVec2i& max_size )
+                const SGVec2i& max_size = MAX_SIZE )
     {
       _size_hint = size_hint;
       _min_size = min_size;
@@ -62,16 +64,45 @@ class TestWidget:
     void setMaxSize(const SGVec2i& size) { _max_size = size; }
     void setSizeHint(const SGVec2i& size) { _size_hint = size; }
 
-    virtual void setGeometry(const SGRecti& geom) { _geom = geom; }
-    virtual SGRecti geometry() const { return _geom; }
-
   protected:
 
-    SGRecti _geom;
-
     virtual SGVec2i sizeHintImpl() const { return _size_hint; }
     virtual SGVec2i minimumSizeImpl() const { return _min_size; }
     virtual SGVec2i maximumSizeImpl() const { return _max_size; }
+
+    virtual void visibilityChanged(bool visible)
+    {
+      if( !visible )
+        _geometry.set(0, 0, 0, 0);
+    }
+};
+
+class TestWidgetHFW:
+  public TestWidget
+{
+  public:
+    TestWidgetHFW( const SGVec2i& min_size,
+                   const SGVec2i& size_hint,
+                   const SGVec2i& max_size = MAX_SIZE ):
+      TestWidget(min_size, size_hint, max_size)
+    {
+
+    }
+
+    virtual bool hasHeightForWidth() const
+    {
+      return true;
+    }
+
+    virtual int heightForWidthImpl(int w) const
+    {
+      return _size_hint.x() * _size_hint.y() / w;
+    }
+
+    virtual int minimumHeightForWidthImpl(int w) const
+    {
+      return _min_size.x() * _min_size.y() / w;
+    }
 };
 
 typedef SGSharedPtr<TestWidget> TestWidgetRef;
@@ -188,6 +219,15 @@ BOOST_AUTO_TEST_CASE( horizontal_layout )
 
     BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,   0, 125, 32));
     BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(130, 0, 126, 32));
+
+    BOOST_REQUIRE( hbox.setStretchFactor(w1, 2) );
+    BOOST_REQUIRE( hbox.setStretchFactor(w2, 3) );
+    BOOST_CHECK_EQUAL(hbox.stretch(0), 2);
+    BOOST_CHECK_EQUAL(hbox.stretch(1), 3);
+
+    hbox.removeItem(w1);
+
+    BOOST_CHECK( !hbox.setStretchFactor(w1, 0) );
   }
 }
 
@@ -256,12 +296,355 @@ BOOST_AUTO_TEST_CASE( vertical_layout)
 }
 
 //------------------------------------------------------------------------------
-BOOST_AUTO_TEST_CASE( nasal_layout )
+BOOST_AUTO_TEST_CASE( boxlayout_insert_remove )
+{
+  sc::BoxLayoutRef hbox( new sc::HBoxLayout );
+
+  BOOST_CHECK_EQUAL(hbox->count(), 0);
+  BOOST_CHECK(!hbox->itemAt(0));
+  BOOST_CHECK(!hbox->takeAt(0));
+
+  TestWidgetRef w1( new TestWidget( SGVec2i(16,   16),
+                                    SGVec2i(32,   32),
+                                    SGVec2i(9999, 32) ) ),
+                w2( new TestWidget(*w1) );
+
+  hbox->addItem(w1);
+  BOOST_CHECK_EQUAL(hbox->count(), 1);
+  BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
+  BOOST_CHECK_EQUAL(w1->getParent(), hbox);
+
+  hbox->insertItem(0, w2);
+  BOOST_CHECK_EQUAL(hbox->count(), 2);
+  BOOST_CHECK_EQUAL(hbox->itemAt(0), w2);
+  BOOST_CHECK_EQUAL(hbox->itemAt(1), w1);
+  BOOST_CHECK_EQUAL(w2->getParent(), hbox);
+
+  hbox->removeItem(w2);
+  BOOST_CHECK_EQUAL(hbox->count(), 1);
+  BOOST_CHECK_EQUAL(hbox->itemAt(0), w1);
+  BOOST_CHECK( !w2->getParent() );
+
+  hbox->addItem(w2);
+  BOOST_CHECK_EQUAL(hbox->count(), 2);
+  BOOST_CHECK_EQUAL(w2->getParent(), hbox);
+
+  hbox->clear();
+  BOOST_CHECK_EQUAL(hbox->count(), 0);
+  BOOST_CHECK( !w1->getParent() );
+  BOOST_CHECK( !w2->getParent() );
+}
+
+//------------------------------------------------------------------------------
+BOOST_AUTO_TEST_CASE( boxlayout_visibility )
 {
-  naContext c = naNewContext();
-  naRef me = naNewHash(c);
+  sc::BoxLayoutRef hbox( new sc::HBoxLayout );
+  TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
+                                    SGVec2i(32, 32) ) ),
+                w2( new TestWidget(*w1) ),
+                w3( new TestWidget(*w1) );
+
+  hbox->addItem(w1);
+  hbox->addItem(w2);
+  hbox->addItem(w3);
+
+  BOOST_REQUIRE_EQUAL(hbox->sizeHint().x(), 3 * 32 + 2 * hbox->spacing());
+
+  hbox->setGeometry(SGRecti(0, 0, 69, 32));
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 20, 32));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(25, 0, 20, 32));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(50, 0, 19, 32));
+
+  w2->setVisible(false);
+
+  BOOST_REQUIRE(hbox->isVisible());
+  BOOST_REQUIRE(w1->isVisible());
+  BOOST_REQUIRE(!w2->isVisible());
+  BOOST_REQUIRE(w2->isExplicitlyHidden());
+  BOOST_REQUIRE(w3->isVisible());
+
+  BOOST_CHECK_EQUAL(hbox->sizeHint().x(), 2 * 32 + 1 * hbox->spacing());
+
+  hbox->update();
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 32, 32));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0,  0,  0,  0));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(37, 0, 32, 32));
+
+  hbox->setVisible(false);
+
+  BOOST_REQUIRE(!hbox->isVisible());
+  BOOST_REQUIRE(hbox->isExplicitlyHidden());
+  BOOST_REQUIRE(!w1->isVisible());
+  BOOST_REQUIRE(!w1->isExplicitlyHidden());
+  BOOST_REQUIRE(!w2->isVisible());
+  BOOST_REQUIRE(w2->isExplicitlyHidden());
+  BOOST_REQUIRE(!w3->isVisible());
+  BOOST_REQUIRE(!w3->isExplicitlyHidden());
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 0, 0));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 0, 0, 0));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(0, 0, 0, 0));
+
+  w2->setVisible(true);
+
+  BOOST_REQUIRE(!w2->isVisible());
+  BOOST_REQUIRE(!w2->isExplicitlyHidden());
+
+  hbox->setVisible(true);
+
+  BOOST_REQUIRE(hbox->isVisible());
+  BOOST_REQUIRE(w1->isVisible());
+  BOOST_REQUIRE(w2->isVisible());
+  BOOST_REQUIRE(w3->isVisible());
+
+  hbox->update();
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 20, 32));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(25, 0, 20, 32));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(50, 0, 19, 32));
+}
+
+//------------------------------------------------------------------------------
+BOOST_AUTO_TEST_CASE( boxlayout_contents_margins )
+{
+  sc::Margins m;
+
+  BOOST_REQUIRE(m.isNull());
+
+  m = sc::Margins(5);
 
-  sc::LayoutItemRef nasal_item( new sc::NasalWidget(me) );
+  BOOST_REQUIRE_EQUAL(m.l, 5);
+  BOOST_REQUIRE_EQUAL(m.t, 5);
+  BOOST_REQUIRE_EQUAL(m.r, 5);
+  BOOST_REQUIRE_EQUAL(m.b, 5);
 
-  naFreeContext(c);
+  m = sc::Margins(6, 7);
+
+  BOOST_REQUIRE_EQUAL(m.l, 6);
+  BOOST_REQUIRE_EQUAL(m.t, 7);
+  BOOST_REQUIRE_EQUAL(m.r, 6);
+  BOOST_REQUIRE_EQUAL(m.b, 7);
+
+  BOOST_REQUIRE_EQUAL(m.horiz(), 12);
+  BOOST_REQUIRE_EQUAL(m.vert(), 14);
+  BOOST_REQUIRE(!m.isNull());
+
+  m = sc::Margins(1, 2, 3, 4);
+
+  BOOST_REQUIRE_EQUAL(m.l, 1);
+  BOOST_REQUIRE_EQUAL(m.t, 2);
+  BOOST_REQUIRE_EQUAL(m.r, 3);
+  BOOST_REQUIRE_EQUAL(m.b, 4);
+
+  BOOST_REQUIRE_EQUAL(m.horiz(), 4);
+  BOOST_REQUIRE_EQUAL(m.vert(), 6);
+  BOOST_REQUIRE_EQUAL(m.size(), SGVec2i(4, 6));
+
+  sc::BoxLayoutRef hbox( new sc::HBoxLayout );
+
+  hbox->setContentsMargins(5, 10, 15, 20);
+
+  BOOST_CHECK_EQUAL(hbox->minimumSize(), SGVec2i(20, 30));
+  BOOST_CHECK_EQUAL(hbox->sizeHint(),    SGVec2i(20, 30));
+  BOOST_CHECK_EQUAL(hbox->maximumSize(), SGVec2i(20, 30));
+
+  hbox->setGeometry(SGRecti(0, 0, 30, 40));
+
+  BOOST_CHECK_EQUAL(hbox->contentsRect(), SGRecti(5, 10, 10, 10));
+
+  TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
+                                    SGVec2i(32, 32) ) ),
+                w2( new TestWidget(*w1) ),
+                w3( new TestWidget(*w1) );
+
+  w1->setContentsMargin(5);
+  w2->setContentsMargin(6);
+  w3->setContentsMargin(7);
+
+  BOOST_CHECK_EQUAL(w1->minimumSize(), SGVec2i(26, 26));
+  BOOST_CHECK_EQUAL(w1->sizeHint(),    SGVec2i(42, 42));
+  BOOST_CHECK_EQUAL(w1->maximumSize(), sc::LayoutItem::MAX_SIZE);
+
+  BOOST_CHECK_EQUAL(w2->minimumSize(), SGVec2i(28, 28));
+  BOOST_CHECK_EQUAL(w2->sizeHint(),    SGVec2i(44, 44));
+  BOOST_CHECK_EQUAL(w2->maximumSize(), sc::LayoutItem::MAX_SIZE);
+
+  BOOST_CHECK_EQUAL(w3->minimumSize(), SGVec2i(30, 30));
+  BOOST_CHECK_EQUAL(w3->sizeHint(),    SGVec2i(46, 46));
+  BOOST_CHECK_EQUAL(w3->maximumSize(), sc::LayoutItem::MAX_SIZE);
+
+  hbox->addItem(w1);
+  hbox->addItem(w2);
+  hbox->addItem(w3);
+
+  BOOST_CHECK_EQUAL(hbox->minimumSize(), SGVec2i(114, 60));
+  BOOST_CHECK_EQUAL(hbox->sizeHint(),    SGVec2i(162, 76));
+  BOOST_CHECK_EQUAL(hbox->maximumSize(), sc::LayoutItem::MAX_SIZE);
+
+  hbox->setGeometry(SGRecti(0, 0, hbox->sizeHint().x(), hbox->sizeHint().y()));
+
+  BOOST_CHECK_EQUAL(hbox->contentsRect(), SGRecti(5, 10, 142, 46));
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(5,   10, 42, 46));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(52,  10, 44, 46));
+  BOOST_CHECK_EQUAL(w3->geometry(), SGRecti(101, 10, 46, 46));
+
+  BOOST_CHECK_EQUAL(w1->contentsRect(), SGRecti(10,  15, 32, 36));
+  BOOST_CHECK_EQUAL(w2->contentsRect(), SGRecti(58,  16, 32, 34));
+  BOOST_CHECK_EQUAL(w3->contentsRect(), SGRecti(108, 17, 32, 32));
+}
+
+//------------------------------------------------------------------------------
+BOOST_AUTO_TEST_CASE( boxlayout_hfw )
+{
+  TestWidgetRef w1( new TestWidgetHFW( SGVec2i(16,   16),
+                                       SGVec2i(32,   32) ) ),
+                w2( new TestWidgetHFW( SGVec2i(24,   24),
+                                       SGVec2i(48,   48) ) );
+
+  BOOST_CHECK_EQUAL(w1->heightForWidth(16), 64);
+  BOOST_CHECK_EQUAL(w1->minimumHeightForWidth(16), 16);
+  BOOST_CHECK_EQUAL(w2->heightForWidth(24), 96);
+  BOOST_CHECK_EQUAL(w2->minimumHeightForWidth(24), 24);
+
+  TestWidgetRef w_no_hfw( new TestWidget( SGVec2i(16,   16),
+                                          SGVec2i(32,   32) ) );
+  BOOST_CHECK(!w_no_hfw->hasHeightForWidth());
+  BOOST_CHECK_EQUAL(w_no_hfw->heightForWidth(16), -1);
+  BOOST_CHECK_EQUAL(w_no_hfw->minimumHeightForWidth(16), -1);
+
+  // horizontal
+  sc::HBoxLayout hbox;
+  hbox.setSpacing(5);
+  hbox.addItem(w1);
+  hbox.addItem(w2);
+
+  BOOST_CHECK_EQUAL(hbox.heightForWidth(45), w2->heightForWidth(24));
+  BOOST_CHECK_EQUAL(hbox.heightForWidth(85), w2->heightForWidth(48));
+
+  hbox.addItem(w_no_hfw);
+
+  BOOST_CHECK_EQUAL(hbox.heightForWidth(66), 96);
+  BOOST_CHECK_EQUAL(hbox.heightForWidth(122), 48);
+  BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(66), 24);
+  BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(122), 16);
+
+  hbox.setGeometry(SGRecti(0, 0, 66, 24));
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  16, 24));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(21, 0, 24, 24));
+  BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(50, 0, 16, 24));
+
+  // vertical
+  sc::VBoxLayout vbox;
+  vbox.setSpacing(5);
+  vbox.addItem(w1);
+  vbox.addItem(w2);
+
+  BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 143);
+  BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 74);
+  BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 39);
+  BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 22);
+
+  vbox.addItem(w_no_hfw);
+
+  BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 180);
+  BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 111);
+  BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 60);
+  BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 43);
+
+  SGVec2i min_size = vbox.minimumSize(),
+          size_hint = vbox.sizeHint();
+
+  BOOST_CHECK_EQUAL(min_size, SGVec2i(24, 66));
+  BOOST_CHECK_EQUAL(size_hint, SGVec2i(48, 122));
+
+  vbox.setGeometry(SGRecti(0, 0, 24, 122));
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  24, 33));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 38, 24, 47));
+  BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 90, 24, 32));
+
+  // Vertical layouting modifies size hints, so check if they are correctly
+  // restored
+  BOOST_CHECK_EQUAL(min_size, vbox.minimumSize());
+  BOOST_CHECK_EQUAL(size_hint, vbox.sizeHint());
+
+  vbox.setGeometry(SGRecti(0, 0, 50, 122));
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  50, 25));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 30, 50, 51));
+  BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 86, 50, 36));
+
+  // Same geometry as before -> should get same widget geometry
+  // (check internal size hint cache updates correctly)
+  vbox.setGeometry(SGRecti(0, 0, 24, 122));
+
+  BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  24, 33));
+  BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 38, 24, 47));
+  BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 90, 24, 32));
+}
+
+//------------------------------------------------------------------------------
+// TODO extend to_nasal_helper for automatic argument conversion
+static naRef f_Widget_visibilityChanged(nasal::CallContext ctx)
+{
+  sc::NasalWidget* w = ctx.from_nasal<sc::NasalWidget*>(ctx.me);
+
+  if( !ctx.requireArg<bool>(0) )
+    w->setGeometry(SGRecti(0, 0, -1, -1));
+
+  return naNil();
+}
+
+//------------------------------------------------------------------------------
+BOOST_AUTO_TEST_CASE( nasal_widget )
+{
+  nasal::Context c;
+  nasal::Hash globals = c.newHash();
+
+  nasal::Object::setupGhost();
+  nasal::Ghost<sc::LayoutItemRef>::init("LayoutItem");
+  sc::NasalWidget::setupGhost(globals);
+
+  nasal::Hash me = c.newHash();
+  me.set("visibilityChanged", &f_Widget_visibilityChanged);
+  sc::NasalWidgetRef w( new sc::NasalWidget(me.get_naRef()) );
+
+  // Default layout sizes (no user set values)
+  BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(16, 16));
+  BOOST_CHECK_EQUAL(w->sizeHint(),    SGVec2i(32, 32));
+  BOOST_CHECK_EQUAL(w->maximumSize(), sc::LayoutItem::MAX_SIZE);
+
+  // Changed layout sizes
+  w->setLayoutMinimumSize( SGVec2i(2, 12) );
+  w->setLayoutSizeHint(    SGVec2i(3, 13) );
+  w->setLayoutMaximumSize( SGVec2i(4, 14) );
+
+  BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(2, 12));
+  BOOST_CHECK_EQUAL(w->sizeHint(),    SGVec2i(3, 13));
+  BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(4, 14));
+
+  // User set values (overwrite layout sizes)
+  w->setMinimumSize( SGVec2i(15, 16) );
+  w->setSizeHint(    SGVec2i(17, 18) );
+  w->setMaximumSize( SGVec2i(19, 20) );
+
+  BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(15, 16));
+  BOOST_CHECK_EQUAL(w->sizeHint(),    SGVec2i(17, 18));
+  BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(19, 20));
+
+  // Only vertical user set values (layout/default for horizontal hints)
+  w->setMinimumSize( SGVec2i(0, 21) );
+  w->setSizeHint(    SGVec2i(0, 22) );
+  w->setMaximumSize( SGVec2i(SGLimits<int>::max(), 23) );
+
+  BOOST_CHECK_EQUAL(w->minimumSize(), SGVec2i(2, 21));
+  BOOST_CHECK_EQUAL(w->sizeHint(),    SGVec2i(3, 22));
+  BOOST_CHECK_EQUAL(w->maximumSize(), SGVec2i(4, 23));
+
+  w->setVisible(false);
+  BOOST_CHECK_EQUAL(w->geometry(), SGRecti(0, 0, -1, -1));
 }