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 )
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; }
77 typedef SGSharedPtr<TestWidget> TestWidgetRef;
79 //------------------------------------------------------------------------------
80 BOOST_AUTO_TEST_CASE( horizontal_layout )
82 sc::BoxLayout box_layout(sc::BoxLayout::BottomToTop);
83 box_layout.setSpacing(5);
85 BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::BottomToTop);
86 BOOST_CHECK_EQUAL(box_layout.spacing(), 5);
88 box_layout.setDirection(sc::BoxLayout::LeftToRight);
89 box_layout.setSpacing(9);
91 BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::LeftToRight);
92 BOOST_CHECK_EQUAL(box_layout.spacing(), 9);
94 TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
97 box_layout.addItem(fixed_size_widget);
99 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(16, 16));
100 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(16, 16));
101 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(16, 16));
103 TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
105 SGVec2i(256, 64) ) );
106 box_layout.addItem(limited_resize_widget);
108 // Combined sizes of both widget plus the padding between them
109 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(41, 16));
110 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(57, 32));
111 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(281, 64));
113 // Test with different spacing/padding
114 box_layout.setSpacing(5);
116 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(37, 16));
117 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(53, 32));
118 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(277, 64));
120 box_layout.setGeometry(SGRecti(0, 0, 128, 32));
122 // Fixed size for first widget and remaining space goes to second widget
123 BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(0, 8, 16, 16));
124 BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(21, 0, 107, 32));
126 TestWidgetRef stretch_widget( new TestWidget( SGVec2i(16, 16),
128 SGVec2i(128, 32) ) );
129 box_layout.addItem(stretch_widget, 1);
132 BOOST_CHECK_EQUAL(box_layout.minimumSize(), SGVec2i(58, 16));
133 BOOST_CHECK_EQUAL(box_layout.sizeHint(), SGVec2i(90, 32));
134 BOOST_CHECK_EQUAL(box_layout.maximumSize(), SGVec2i(410, 64));
136 // Due to the stretch factor only the last widget gets additional space. All
137 // other widgets get the preferred size.
138 BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(0, 8, 16, 16));
139 BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(21, 0, 32, 32));
140 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(58, 0, 70, 32));
142 // Test stretch factor
143 TestWidgetRef fast_stretch( new TestWidget(*stretch_widget) );
144 sc::BoxLayout box_layout_stretch(sc::BoxLayout::LeftToRight);
146 box_layout_stretch.addItem(stretch_widget, 1);
147 box_layout_stretch.addItem(fast_stretch, 2);
149 box_layout_stretch.setGeometry(SGRecti(0,0,128,32));
151 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 41, 32));
152 BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(46, 0, 82, 32));
154 box_layout_stretch.setGeometry(SGRecti(0,0,256,32));
156 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 123, 32));
157 BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(128, 0, 128, 32));
159 // Test superflous space to padding
160 box_layout_stretch.setGeometry(SGRecti(0,0,512,32));
162 BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(83, 0, 128, 32));
163 BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(300, 0, 128, 32));
165 // Test more space then preferred, but less than maximum
168 TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
170 SGVec2i(9999, 32) ) ),
171 w2( new TestWidget(*w1) );
176 hbox.setGeometry( SGRecti(0, 0, 256, 32) );
178 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 126, 32));
179 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(131, 0, 125, 32));
181 hbox.setStretch(0, 1);
182 hbox.setStretch(1, 1);
184 BOOST_CHECK_EQUAL(hbox.stretch(0), 1);
185 BOOST_CHECK_EQUAL(hbox.stretch(1), 1);
189 BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0, 125, 32));
190 BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(130, 0, 126, 32));
194 //------------------------------------------------------------------------------
195 BOOST_AUTO_TEST_CASE( vertical_layout)
197 sc::BoxLayout vbox(sc::BoxLayout::TopToBottom);
200 TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
203 TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
205 SGVec2i(256, 64) ) );
207 vbox.addItem(fixed_size_widget);
208 vbox.addItem(limited_resize_widget);
210 BOOST_CHECK_EQUAL(vbox.minimumSize(), SGVec2i(16, 39));
211 BOOST_CHECK_EQUAL(vbox.sizeHint(), SGVec2i(32, 55));
212 BOOST_CHECK_EQUAL(vbox.maximumSize(), SGVec2i(256, 87));
214 vbox.setGeometry(SGRecti(10, 20, 16, 55));
216 BOOST_CHECK_EQUAL(fixed_size_widget->geometry(), SGRecti(10, 20, 16, 16));
217 BOOST_CHECK_EQUAL(limited_resize_widget->geometry(), SGRecti(10, 43, 16, 32));
219 vbox.setDirection(sc::BoxLayout::BottomToTop);
222 //------------------------------------------------------------------------------
223 BOOST_AUTO_TEST_CASE( nasal_layout )
225 naContext c = naNewContext();
226 naRef me = naNewHash(c);
228 sc::LayoutItemRef nasal_item( new sc::NasalWidget(me) );