]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/layout/canvas_layout_test.cxx
canvas::Layout: add clear method to remove all items.
[simgear.git] / simgear / canvas / layout / canvas_layout_test.cxx
1 // Testing canvas layouting system
2 //
3 // Copyright (C) 2014  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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
18
19 #define BOOST_TEST_MODULE canvas_layout
20 #include <BoostTestTargetConfig.h>
21
22 #include "BoxLayout.hxx"
23 #include "NasalWidget.hxx"
24
25 #include <simgear/debug/logstream.hxx>
26 #include <cstdlib>
27
28 //------------------------------------------------------------------------------
29 struct SetLogLevelFixture
30 {
31   SetLogLevelFixture()
32   {
33     sglog().set_log_priority(SG_DEBUG);
34   }
35 };
36 BOOST_GLOBAL_FIXTURE(SetLogLevelFixture);
37
38 //------------------------------------------------------------------------------
39 namespace sc = simgear::canvas;
40
41 class TestWidget:
42   public sc::LayoutItem
43 {
44   public:
45     TestWidget( const SGVec2i& min_size,
46                 const SGVec2i& size_hint,
47                 const SGVec2i& max_size = MAX_SIZE )
48     {
49       _size_hint = size_hint;
50       _min_size = min_size;
51       _max_size = max_size;
52     }
53
54     TestWidget(const TestWidget& rhs)
55     {
56       _size_hint = rhs._size_hint;
57       _min_size = rhs._min_size;
58       _max_size = rhs._max_size;
59     }
60
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; }
64
65     virtual void setGeometry(const SGRecti& geom) { _geom = geom; }
66     virtual SGRecti geometry() const { return _geom; }
67
68   protected:
69
70     SGRecti _geom;
71
72     virtual SGVec2i sizeHintImpl() const { return _size_hint; }
73     virtual SGVec2i minimumSizeImpl() const { return _min_size; }
74     virtual SGVec2i maximumSizeImpl() const { return _max_size; }
75 };
76
77 class TestWidgetHFW:
78   public TestWidget
79 {
80   public:
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)
85     {
86
87     }
88
89     virtual bool hasHeightForWidth() const
90     {
91       return true;
92     }
93
94     virtual int heightForWidth(int w) const
95     {
96       return _size_hint.x() * _size_hint.y() / w;
97     }
98
99     virtual int minimumHeightForWidth(int w) const
100     {
101       return _min_size.x() * _min_size.y() / w;
102     }
103 };
104
105 typedef SGSharedPtr<TestWidget> TestWidgetRef;
106
107 //------------------------------------------------------------------------------
108 BOOST_AUTO_TEST_CASE( horizontal_layout )
109 {
110   sc::BoxLayout box_layout(sc::BoxLayout::BottomToTop);
111   box_layout.setSpacing(5);
112
113   BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::BottomToTop);
114   BOOST_CHECK_EQUAL(box_layout.spacing(), 5);
115
116   box_layout.setDirection(sc::BoxLayout::LeftToRight);
117   box_layout.setSpacing(9);
118
119   BOOST_CHECK_EQUAL(box_layout.direction(), sc::BoxLayout::LeftToRight);
120   BOOST_CHECK_EQUAL(box_layout.spacing(), 9);
121
122   TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
123                                                    SGVec2i(16, 16),
124                                                    SGVec2i(16, 16) ) );
125   box_layout.addItem(fixed_size_widget);
126
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));
130
131   TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
132                                                        SGVec2i(32, 32),
133                                                        SGVec2i(256, 64) ) );
134   box_layout.addItem(limited_resize_widget);
135
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));
140
141   // Test with different spacing/padding
142   box_layout.setSpacing(5);
143
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));
147
148   box_layout.setGeometry(SGRecti(0, 0, 128, 32));
149
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));
153
154   TestWidgetRef stretch_widget( new TestWidget( SGVec2i(16, 16),
155                                                 SGVec2i(32, 32),
156                                                 SGVec2i(128, 32) ) );
157   box_layout.addItem(stretch_widget, 1);
158   box_layout.update();
159
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));
163
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));
169
170   // Test stretch factor
171   TestWidgetRef fast_stretch( new TestWidget(*stretch_widget) );
172   sc::BoxLayout box_layout_stretch(sc::BoxLayout::LeftToRight);
173
174   box_layout_stretch.addItem(stretch_widget, 1);
175   box_layout_stretch.addItem(fast_stretch, 2);
176
177   box_layout_stretch.setGeometry(SGRecti(0,0,128,32));
178
179   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 41, 32));
180   BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(46, 0, 82, 32));
181
182   box_layout_stretch.setGeometry(SGRecti(0,0,256,32));
183
184   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(0, 0, 123, 32));
185   BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(128, 0, 128, 32));
186
187   // Test superflous space to padding
188   box_layout_stretch.setGeometry(SGRecti(0,0,512,32));
189
190   BOOST_CHECK_EQUAL(stretch_widget->geometry(), SGRecti(83, 0, 128, 32));
191   BOOST_CHECK_EQUAL(fast_stretch->geometry(), SGRecti(300, 0, 128, 32));
192
193   // Test more space then preferred, but less than maximum
194   {
195     sc::HBoxLayout hbox;
196     TestWidgetRef w1( new TestWidget( SGVec2i(16,   16),
197                                       SGVec2i(32,   32),
198                                       SGVec2i(9999, 32) ) ),
199                   w2( new TestWidget(*w1) );
200
201     hbox.addItem(w1);
202     hbox.addItem(w2);
203
204     hbox.setGeometry( SGRecti(0, 0, 256, 32) );
205
206     BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,   0, 126, 32));
207     BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(131, 0, 125, 32));
208
209     hbox.setStretch(0, 1);
210     hbox.setStretch(1, 1);
211
212     BOOST_CHECK_EQUAL(hbox.stretch(0), 1);
213     BOOST_CHECK_EQUAL(hbox.stretch(1), 1);
214
215     hbox.update();
216
217     BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,   0, 125, 32));
218     BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(130, 0, 126, 32));
219   }
220 }
221
222 //------------------------------------------------------------------------------
223 BOOST_AUTO_TEST_CASE( spacer_layouting )
224 {
225   sc::HBoxLayout hbox;
226   TestWidgetRef w1( new TestWidget( SGVec2i(16, 16),
227                                     SGVec2i(32, 32),
228                                     SGVec2i(9999, 9999) ) ),
229                 w2( new TestWidget(*w1) );
230
231   hbox.addItem(w1);
232   hbox.addItem(w2);
233   hbox.addStretch(1);
234
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);
238
239   hbox.setGeometry(SGRecti(0, 0, 256, 40));
240
241   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0,  0, 32, 40));
242   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(37, 0, 32, 40));
243
244   // now center with increased spacing between both widgets
245   hbox.insertStretch(0, 1);
246   hbox.insertSpacing(2, 10);
247
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);
251
252   hbox.update();
253
254   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(88,  0, 32, 40));
255   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(135, 0, 32, 40));
256 }
257
258 //------------------------------------------------------------------------------
259 BOOST_AUTO_TEST_CASE( vertical_layout)
260 {
261   sc::BoxLayout vbox(sc::BoxLayout::TopToBottom);
262   vbox.setSpacing(7);
263
264   TestWidgetRef fixed_size_widget( new TestWidget( SGVec2i(16, 16),
265                                                    SGVec2i(16, 16),
266                                                    SGVec2i(16, 16) ) );
267   TestWidgetRef limited_resize_widget( new TestWidget( SGVec2i(16, 16),
268                                                        SGVec2i(32, 32),
269                                                        SGVec2i(256, 64) ) );
270
271   vbox.addItem(fixed_size_widget);
272   vbox.addItem(limited_resize_widget);
273
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));
277
278   vbox.setGeometry(SGRecti(10, 20, 16, 55));
279
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));
282
283   vbox.setDirection(sc::BoxLayout::BottomToTop);
284 }
285
286 //------------------------------------------------------------------------------
287 BOOST_AUTO_TEST_CASE( boxlayout_insert_remove )
288 {
289   sc::HBoxLayout hbox;
290
291   BOOST_CHECK_EQUAL(hbox.count(), 0);
292   BOOST_CHECK(!hbox.itemAt(0));
293   BOOST_CHECK(!hbox.takeAt(0));
294
295   TestWidgetRef w1( new TestWidget( SGVec2i(16,   16),
296                                     SGVec2i(32,   32),
297                                     SGVec2i(9999, 32) ) ),
298                 w2( new TestWidget(*w1) );
299
300   hbox.addItem(w1);
301   BOOST_CHECK_EQUAL(hbox.count(), 1);
302   BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
303
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);
308
309   hbox.removeItem(w2);
310   BOOST_CHECK_EQUAL(hbox.count(), 1);
311   BOOST_CHECK_EQUAL(hbox.itemAt(0), w1);
312
313   hbox.addItem(w2);
314   BOOST_CHECK_EQUAL(hbox.count(), 2);
315
316   hbox.clear();
317   BOOST_CHECK_EQUAL(hbox.count(), 0);
318 }
319
320 //------------------------------------------------------------------------------
321 BOOST_AUTO_TEST_CASE( boxlayout_hfw )
322 {
323   TestWidgetRef w1( new TestWidgetHFW( SGVec2i(16,   16),
324                                        SGVec2i(32,   32) ) ),
325                 w2( new TestWidgetHFW( SGVec2i(24,   24),
326                                        SGVec2i(48,   48) ) );
327
328   BOOST_CHECK_EQUAL(w1->heightForWidth(16), 64);
329   BOOST_CHECK_EQUAL(w1->minimumHeightForWidth(16), 16);
330   BOOST_CHECK_EQUAL(w2->heightForWidth(24), 96);
331   BOOST_CHECK_EQUAL(w2->minimumHeightForWidth(24), 24);
332
333   TestWidgetRef w_no_hfw( new TestWidget( SGVec2i(16,   16),
334                                           SGVec2i(32,   32) ) );
335   BOOST_CHECK(!w_no_hfw->hasHeightForWidth());
336   BOOST_CHECK_EQUAL(w_no_hfw->heightForWidth(16), -1);
337   BOOST_CHECK_EQUAL(w_no_hfw->minimumHeightForWidth(16), -1);
338
339   // horizontal
340   sc::HBoxLayout hbox;
341   hbox.setSpacing(5);
342   hbox.addItem(w1);
343   hbox.addItem(w2);
344
345   BOOST_CHECK_EQUAL(hbox.heightForWidth(45), w2->heightForWidth(24));
346   BOOST_CHECK_EQUAL(hbox.heightForWidth(85), w2->heightForWidth(48));
347
348   hbox.addItem(w_no_hfw);
349
350   BOOST_CHECK_EQUAL(hbox.heightForWidth(66), 96);
351   BOOST_CHECK_EQUAL(hbox.heightForWidth(122), 48);
352   BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(66), 24);
353   BOOST_CHECK_EQUAL(hbox.minimumHeightForWidth(122), 16);
354
355   hbox.setGeometry(SGRecti(0, 0, 66, 24));
356
357   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  16, 24));
358   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(21, 0, 24, 24));
359   BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(50, 0, 16, 24));
360
361   // vertical
362   sc::VBoxLayout vbox;
363   vbox.setSpacing(5);
364   vbox.addItem(w1);
365   vbox.addItem(w2);
366
367   BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 143);
368   BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 74);
369   BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 39);
370   BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 22);
371
372   vbox.addItem(w_no_hfw);
373
374   BOOST_CHECK_EQUAL(vbox.heightForWidth(24), 180);
375   BOOST_CHECK_EQUAL(vbox.heightForWidth(48), 111);
376   BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(24), 60);
377   BOOST_CHECK_EQUAL(vbox.minimumHeightForWidth(48), 43);
378
379   SGVec2i min_size = vbox.minimumSize(),
380           size_hint = vbox.sizeHint();
381
382   BOOST_CHECK_EQUAL(min_size, SGVec2i(24, 66));
383   BOOST_CHECK_EQUAL(size_hint, SGVec2i(48, 122));
384
385   vbox.setGeometry(SGRecti(0, 0, 24, 122));
386
387   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  24, 33));
388   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 38, 24, 47));
389   BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 90, 24, 32));
390
391   // Vertical layouting modifies size hints, so check if they are correctly
392   // restored
393   BOOST_CHECK_EQUAL(min_size, vbox.minimumSize());
394   BOOST_CHECK_EQUAL(size_hint, vbox.sizeHint());
395
396   vbox.setGeometry(SGRecti(0, 0, 50, 122));
397
398   BOOST_CHECK_EQUAL(w1->geometry(), SGRecti(0, 0,  50, 44));
399   BOOST_CHECK_EQUAL(w2->geometry(), SGRecti(0, 49, 50, 70));
400   BOOST_CHECK_EQUAL(w_no_hfw->geometry(), SGRecti(0, 124, 50, 56));
401 }
402
403 //------------------------------------------------------------------------------
404 BOOST_AUTO_TEST_CASE( nasal_layout )
405 {
406   naContext c = naNewContext();
407   naRef me = naNewHash(c);
408
409   sc::LayoutItemRef nasal_item( new sc::NasalWidget(me) );
410
411   naFreeContext(c);
412 }