1 /***********************************************************************
2  *    created:    28/10/2011
3  *    author:     Martin Preisler
4  *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2011 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 
28 #include "CEGUI/Element.h"
29 #include "CEGUI/System.h"
30 
31 #include <boost/test/unit_test.hpp>
32 #include <boost/timer.hpp>
33 
34 /*
35  * Give the renderer / system an initial size.  Without this default min/max
36  * sizes are always forced to zero.  Perhaps there is a better way to deal
37  * with this situation?
38  *
39  * NB: Put this here instead of global fixture in case we don't want to
40  * pollute that minimal setup by adding this.
41  */
42 struct ElementTestsFixture
43 {
ElementTestsFixtureElementTestsFixture44     ElementTestsFixture()
45     {
46         CEGUI::System::getSingleton().notifyDisplaySizeChanged(CEGUI::Sizef(800, 600));
47     }
48 };
49 
BOOST_FIXTURE_TEST_SUITE(Element,ElementTestsFixture)50 BOOST_FIXTURE_TEST_SUITE(Element, ElementTestsFixture)
51 
52 BOOST_AUTO_TEST_CASE(RelativeSizing)
53 {
54     CEGUI::Element* root = new CEGUI::Element();
55     root->setSize(CEGUI::USize(100.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
56     CEGUI::Element* child = new CEGUI::Element();
57     child->setSize(CEGUI::USize(50.0f * CEGUI::UDim::percent(), 25.0f * CEGUI::UDim::percent()));
58     root->addChild(child);
59 
60     BOOST_CHECK_EQUAL(root->getPixelSize(), CEGUI::Sizef(100.0f, 100.0f));
61     BOOST_CHECK_EQUAL(child->getPixelSize(), CEGUI::Sizef(50.0f, 25.0f));
62 
63     CEGUI::Element* innerChild = new CEGUI::Element();
64     child->addChild(innerChild);
65     innerChild->setSize(CEGUI::USize(200.0f * CEGUI::UDim::percent(), 100.0f * CEGUI::UDim::percent()));
66     BOOST_CHECK_EQUAL(innerChild->getPixelSize(), CEGUI::Sizef(100.0f, 25.0f));
67 
68     delete innerChild;
69     delete child;
70     delete root;
71 }
72 
BOOST_AUTO_TEST_CASE(RelativePositioning)73 BOOST_AUTO_TEST_CASE(RelativePositioning)
74 {
75     CEGUI::Element* root = new CEGUI::Element();
76     root->setSize(CEGUI::USize(100.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
77     CEGUI::Element* child = new CEGUI::Element();
78     child->setPosition(CEGUI::UVector2(50.0f * CEGUI::UDim::percent(), 50.0f * CEGUI::UDim::percent()));
79     child->setSize(CEGUI::USize(10.0f * CEGUI::UDim::px(), 10 * CEGUI::UDim::px()));
80     root->addChild(child);
81 
82     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(50.0f, 50.0f, 60.0f, 60.0f));
83 
84     delete child;
85     delete root;
86 }
87 
88 // TODO: RelativeRotation!
89 
BOOST_AUTO_TEST_CASE(MinMaxSize)90 BOOST_AUTO_TEST_CASE(MinMaxSize)
91 {
92     CEGUI::Element* root = new CEGUI::Element();
93     root->setSize(CEGUI::USize(100.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
94     root->setMaxSize(CEGUI::USize(50.0f * CEGUI::UDim::px(), 50 * CEGUI::UDim::px()));;
95 
96     BOOST_CHECK_EQUAL(root->getPixelSize(), CEGUI::Sizef(50.0f, 50.0f));
97     BOOST_CHECK_EQUAL(root->getSize(), CEGUI::USize(100.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
98 
99     root->setMaxSize(CEGUI::USize(75.0f * CEGUI::UDim::px(), 75.0f * CEGUI::UDim::px()));;
100 
101     BOOST_CHECK_EQUAL(root->getPixelSize(), CEGUI::Sizef(75.0f, 75.0f));
102     BOOST_CHECK_EQUAL(root->getSize(), CEGUI::USize(100.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
103 
104     root->setMaxSize(CEGUI::USize(1000.0f * CEGUI::UDim::px(), 1000 * CEGUI::UDim::px()));;
105 
106     root->setMinSize(CEGUI::USize(125.0f * CEGUI::UDim::px(), 125.0f * CEGUI::UDim::px()));
107 
108     BOOST_CHECK_EQUAL(root->getPixelSize(), CEGUI::Sizef(125.0f, 125.0f));
109     BOOST_CHECK_EQUAL(root->getSize(), CEGUI::USize(100.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
110 
111     delete root;
112 }
113 
BOOST_AUTO_TEST_CASE(HorizontalLeftAlignment)114 BOOST_AUTO_TEST_CASE(HorizontalLeftAlignment)
115 {
116     CEGUI::Element* root = new CEGUI::Element();
117     root->setSize(CEGUI::USize(100.0f * CEGUI::UDim::px(), 0.0f * CEGUI::UDim::px()));
118     CEGUI::Element* child = new CEGUI::Element();
119     child->setSize(CEGUI::USize(50.0f * CEGUI::UDim::px(), 0.0f * CEGUI::UDim::px()));
120     root->addChild(child);
121 
122     // even though it is the default at the point of writing the test, we have to make sure this fact doesn't change!
123     BOOST_CHECK_EQUAL(root->getHorizontalAlignment(), CEGUI::HA_LEFT);
124     BOOST_CHECK_EQUAL(child->getHorizontalAlignment(), CEGUI::HA_LEFT);
125 
126     BOOST_CHECK_EQUAL(root->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 0, 100, 0));
127     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 0, 50, 0));
128 
129     child->setPosition(CEGUI::UVector2(10.0f * CEGUI::UDim::px(), 0.0f * CEGUI::UDim::px()));
130 
131     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(10, 0, 60, 0));
132 
133     delete child;
134     delete root;
135 }
136 
BOOST_AUTO_TEST_CASE(HorizontalCentreAlignment)137 BOOST_AUTO_TEST_CASE(HorizontalCentreAlignment)
138 {
139     CEGUI::Element* root = new CEGUI::Element();
140     root->setSize(CEGUI::USize(100.0f * CEGUI::UDim::px(), 0.0f * CEGUI::UDim::px()));
141     CEGUI::Element* child = new CEGUI::Element();
142     child->setSize(CEGUI::USize(50.0f * CEGUI::UDim::px(), 0.0f * CEGUI::UDim::px()));
143     root->addChild(child);
144 
145     // even though it is the default at the point of writing the test, we have to make sure this fact doesn't change!
146     BOOST_CHECK_EQUAL(root->getHorizontalAlignment(), CEGUI::HA_LEFT);
147     child->setHorizontalAlignment(CEGUI::HA_CENTRE);
148 
149     BOOST_CHECK_EQUAL(root->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 0, 100, 0));
150     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(25, 0, 75, 0));
151 
152     child->setPosition(CEGUI::UVector2(10.0f * CEGUI::UDim::px(), 0.0f * CEGUI::UDim::px()));
153 
154     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(35, 0, 85, 0));
155 
156     delete child;
157     delete root;
158 }
159 
BOOST_AUTO_TEST_CASE(HorizontalRightAlignment)160 BOOST_AUTO_TEST_CASE(HorizontalRightAlignment)
161 {
162     CEGUI::Element* root = new CEGUI::Element();
163     root->setSize(CEGUI::USize(100.0f * CEGUI::UDim::px(), 0.0f * CEGUI::UDim::px()));
164     CEGUI::Element* child = new CEGUI::Element();
165     child->setSize(CEGUI::USize(50.0f * CEGUI::UDim::px(), 0.0f * CEGUI::UDim::px()));
166     root->addChild(child);
167 
168     // even though it is the default at the point of writing the test, we have to make sure this fact doesn't change!
169     BOOST_CHECK_EQUAL(root->getHorizontalAlignment(), CEGUI::HA_LEFT);
170     child->setHorizontalAlignment(CEGUI::HA_RIGHT);
171 
172     BOOST_CHECK_EQUAL(root->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 0, 100, 0));
173     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(50, 0, 100, 0));
174 
175     child->setPosition(CEGUI::UVector2(-10.0f * CEGUI::UDim::px(), 0.0f * CEGUI::UDim::px()));
176 
177     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(40, 0, 90, 0));
178 
179     delete child;
180     delete root;
181 }
182 
BOOST_AUTO_TEST_CASE(VerticalTopAlignment)183 BOOST_AUTO_TEST_CASE(VerticalTopAlignment)
184 {
185     CEGUI::Element* root = new CEGUI::Element();
186     root->setSize(CEGUI::USize(0.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
187     CEGUI::Element* child = new CEGUI::Element();
188     child->setSize(CEGUI::USize(0.0f * CEGUI::UDim::px(), 50.0f * CEGUI::UDim::px()));
189     root->addChild(child);
190 
191     // even though it is the default at the point of writing the test, we have to make sure this fact doesn't change!
192     BOOST_CHECK_EQUAL(root->getVerticalAlignment(), CEGUI::VA_TOP);
193     BOOST_CHECK_EQUAL(child->getVerticalAlignment(), CEGUI::VA_TOP);
194 
195     BOOST_CHECK_EQUAL(root->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 0, 0, 100));
196     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 0, 0, 50));
197 
198     child->setPosition(CEGUI::UVector2(0.0f * CEGUI::UDim::px(), 5.0f * CEGUI::UDim::px()));
199 
200     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 5, 0, 55));
201 
202     delete child;
203     delete root;
204 }
205 
BOOST_AUTO_TEST_CASE(VerticalCentreAlignment)206 BOOST_AUTO_TEST_CASE(VerticalCentreAlignment)
207 {
208     CEGUI::Element* root = new CEGUI::Element();
209     root->setSize(CEGUI::USize(0.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
210     CEGUI::Element* child = new CEGUI::Element();
211     child->setSize(CEGUI::USize(0.0f * CEGUI::UDim::px(), 50.0f * CEGUI::UDim::px()));
212     root->addChild(child);
213 
214     // even though it is the default at the point of writing the test, we have to make sure this fact doesn't change!
215     BOOST_CHECK_EQUAL(root->getVerticalAlignment(), CEGUI::VA_TOP);
216     child->setVerticalAlignment(CEGUI::VA_CENTRE);
217 
218     BOOST_CHECK_EQUAL(root->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 0, 0, 100));
219     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 25, 0, 75));
220 
221     child->setPosition(CEGUI::UVector2(0.0f * CEGUI::UDim::px(), 5.0f * CEGUI::UDim::px()));
222 
223     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 30, 0, 80));
224 
225     delete child;
226     delete root;
227 }
228 
BOOST_AUTO_TEST_CASE(VerticalBottomAlignment)229 BOOST_AUTO_TEST_CASE(VerticalBottomAlignment)
230 {
231     CEGUI::Element* root = new CEGUI::Element();
232     root->setSize(CEGUI::USize(0.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
233     CEGUI::Element* child = new CEGUI::Element();
234     child->setSize(CEGUI::USize(0.0f * CEGUI::UDim::px(), 50.0f * CEGUI::UDim::px()));
235     root->addChild(child);
236 
237     // even though it is the default at the point of writing the test, we have to make sure this fact doesn't change!
238     BOOST_CHECK_EQUAL(root->getVerticalAlignment(), CEGUI::VA_TOP);
239     child->setVerticalAlignment(CEGUI::VA_BOTTOM);
240 
241     BOOST_CHECK_EQUAL(root->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 0, 0, 100));
242     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 50, 0, 100));
243 
244     child->setPosition(CEGUI::UVector2(0.0f * CEGUI::UDim::px(), -5.0f * CEGUI::UDim::px()));
245 
246     BOOST_CHECK_EQUAL(child->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 45, 0, 95));
247 
248     delete child;
249     delete root;
250 }
251 
BOOST_AUTO_TEST_CASE(AspectLocking)252 BOOST_AUTO_TEST_CASE(AspectLocking)
253 {
254     CEGUI::Element* root = new CEGUI::Element();
255     root->setSize(CEGUI::USize(100.0f * CEGUI::UDim::px(), 100 * CEGUI::UDim::px()));
256 
257     // even though it is the default at the point of writing the test, we have to make sure this fact doesn't change!
258     BOOST_CHECK_EQUAL(root->getAspectMode(), CEGUI::AM_IGNORE);
259     BOOST_CHECK_EQUAL(root->getPixelSize(), CEGUI::Sizef(100, 100));
260 
261     root->setAspectMode(CEGUI::AM_SHRINK);
262     root->setAspectRatio(1.0f / 2.0f);
263     // todo: should have tolerances or something, or does boost do that automatically?
264     BOOST_CHECK_EQUAL(root->getPixelSize(), CEGUI::Sizef(50, 100));
265 
266     root->setAspectMode(CEGUI::AM_EXPAND);
267     root->setAspectRatio(1.0f / 2.0f);
268     // todo: should have tolerances or something, or does boost do that automatically?
269     BOOST_CHECK_EQUAL(root->getPixelSize(), CEGUI::Sizef(100, 200));
270 
271     root->setAspectMode(CEGUI::AM_SHRINK);
272     root->setAspectRatio(2.0f / 1.0f);
273     // todo: should have tolerances or something, or does boost do that automatically?
274     BOOST_CHECK_EQUAL(root->getPixelSize(), CEGUI::Sizef(100, 50));
275 
276     root->setAspectMode(CEGUI::AM_EXPAND);
277     root->setAspectRatio(2.0f / 1.0f);
278     // todo: should have tolerances or something, or does boost do that automatically?
279     BOOST_CHECK_EQUAL(root->getPixelSize(), CEGUI::Sizef(200, 100));
280 
281     delete root;
282 }
283 
BOOST_AUTO_TEST_CASE(PixelAlignment)284 BOOST_AUTO_TEST_CASE(PixelAlignment)
285 {
286     CEGUI::Element* root = new CEGUI::Element();
287     root->setPosition(CEGUI::UVector2(0.2f * CEGUI::UDim::px(), 0.2f * CEGUI::UDim::px()));
288     root->setSize(CEGUI::USize(100.0f * CEGUI::UDim::px(), 100.0f * CEGUI::UDim::px()));
289 
290     // even though it is the default at the point of writing the test, we have to make sure this fact doesn't change!
291     BOOST_CHECK(root->isPixelAligned());
292 
293     // todo: should have tolerances or something, or does boost do that automatically?
294     BOOST_CHECK_EQUAL(root->getUnclippedOuterRect().get(), CEGUI::Rectf(0, 0, 100, 100));
295 
296     root->setPixelAligned(false);
297 
298     // todo: should have tolerances or something, or does boost do that automatically?
299     BOOST_CHECK_EQUAL(root->getUnclippedOuterRect().get(), CEGUI::Rectf(0.2f, 0.2f, 100.2f, 100.2f));
300 
301     delete root;
302 }
303 
304 BOOST_AUTO_TEST_SUITE_END()
305