1 /***********************************************************************
2 * created: 11/6/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/Window.h"
29 #include "CEGUI/WindowManager.h"
30
31 #include <boost/test/unit_test.hpp>
32
33 /*
34 * Used to bring some Windows up for testing
35 *
36 * This is for exception safety, no matter what happens in the tests,
37 * its destructor will be called
38 */
39 struct LayoutSetupFixture
40 {
LayoutSetupFixtureLayoutSetupFixture41 LayoutSetupFixture()
42 {
43 d_root = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow");
44 d_root->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 0), CEGUI::UDim(0, 0)));
45 d_root->setSize(CEGUI::USize(CEGUI::UDim(1, 0), CEGUI::UDim(1, 0)));
46
47 d_insideRoot = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow");
48 d_insideRoot->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 100), CEGUI::UDim(0, 50)));
49 d_insideRoot->setSize(CEGUI::USize(CEGUI::UDim(0.5f, 0), CEGUI::UDim(0.5f, 0)));
50 d_root->addChild(d_insideRoot);
51
52 d_insideInsideRoot = CEGUI::WindowManager::getSingleton().createWindow("DefaultWindow");
53 d_insideInsideRoot->setPosition(CEGUI::UVector2(CEGUI::UDim(0, 100), CEGUI::UDim(0, 50)));
54 d_insideInsideRoot->setSize(CEGUI::USize(CEGUI::UDim(0.5f, 0), CEGUI::UDim(0.5f, 0)));
55 d_insideRoot->addChild(d_insideInsideRoot);
56
57 CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(d_root);
58 CEGUI::System::getSingleton().notifyDisplaySizeChanged(CEGUI::Sizef(800, 600));
59 }
60
~LayoutSetupFixtureLayoutSetupFixture61 ~LayoutSetupFixture()
62 {
63 CEGUI::System::getSingleton().getDefaultGUIContext().setRootWindow(0);
64
65 CEGUI::WindowManager::getSingleton().destroyWindow(d_root);
66 }
67
68 CEGUI::Window* d_root;
69 CEGUI::Window* d_insideRoot;
70 CEGUI::Window* d_insideInsideRoot;
71 };
72
BOOST_FIXTURE_TEST_SUITE(Window,LayoutSetupFixture)73 BOOST_FIXTURE_TEST_SUITE(Window, LayoutSetupFixture)
74
75 BOOST_AUTO_TEST_CASE(Defaults)
76 {
77 /*
78 * We check these to ensure we don't change them between releases, as people very likely depend on them!
79 */
80
81 BOOST_CHECK(!d_root->isActive());
82
83 BOOST_CHECK(!d_root->isDisabled());
84 BOOST_CHECK(!d_root->isEffectiveDisabled());
85
86 BOOST_CHECK(d_root->isVisible());
87 BOOST_CHECK(d_root->isEffectiveVisible());
88 }
89
BOOST_AUTO_TEST_CASE(PropertyInheritance)90 BOOST_AUTO_TEST_CASE(PropertyInheritance)
91 {
92 /*
93 * Alpha and Disabled state should get "inherited"/propagated to children via effective alpha/disabled
94 */
95
96 d_root->setAlpha(0.5f);
97 d_insideRoot->setAlpha(0.5f);
98
99 BOOST_CHECK_EQUAL(d_insideInsideRoot->getEffectiveAlpha(), 0.25f);
100
101 d_root->setAlpha(1.0f);
102 d_insideRoot->setAlpha(1.0f);
103
104 d_root->setDisabled(true);
105
106 BOOST_CHECK_EQUAL(d_insideRoot->isEffectiveDisabled(), true);
107 BOOST_CHECK_EQUAL(d_insideInsideRoot->isEffectiveDisabled(), true);
108
109 d_root->setDisabled(false);
110
111 BOOST_CHECK_EQUAL(d_insideRoot->isEffectiveDisabled(), false);
112 BOOST_CHECK_EQUAL(d_insideInsideRoot->isEffectiveDisabled(), false);
113 }
114
BOOST_AUTO_TEST_CASE(UnifiedDimensions)115 BOOST_AUTO_TEST_CASE(UnifiedDimensions)
116 {
117 /*
118 * Basic relative UDim tests
119 */
120 BOOST_CHECK_EQUAL(d_insideInsideRoot->getPixelSize(), CEGUI::Sizef(200, 150));
121 BOOST_CHECK_EQUAL(d_insideInsideRoot->getUnclippedOuterRect().get(), CEGUI::Rectf(200, 100, 400, 250));
122 }
123
BOOST_AUTO_TEST_CASE(HitTesting)124 BOOST_AUTO_TEST_CASE(HitTesting)
125 {
126 /*
127 * We know where the windows are so lets check whether CEGUI reports correct hits for them
128 */
129
130 BOOST_CHECK(d_insideInsideRoot->isHit(CEGUI::Vector2f(300, 150)));
131
132 d_insideInsideRoot->setDisabled(true);
133 BOOST_CHECK(!d_insideInsideRoot->isHit(CEGUI::Vector2f(300, 150), false));
134 BOOST_CHECK(d_insideInsideRoot->isHit(CEGUI::Vector2f(300, 150), true));
135 d_insideInsideRoot->setDisabled(false);
136
137 d_root->setDisabled(true);
138 BOOST_CHECK(!d_insideInsideRoot->isHit(CEGUI::Vector2f(300, 150), false));
139 BOOST_CHECK(d_insideInsideRoot->isHit(CEGUI::Vector2f(300, 150), true));
140 d_root->setDisabled(false);
141 }
142
BOOST_AUTO_TEST_CASE(Hierarchy)143 BOOST_AUTO_TEST_CASE(Hierarchy)
144 {
145 CEGUI::Window* child = d_insideInsideRoot->createChild("DefaultWindow");
146 BOOST_CHECK(d_insideInsideRoot->isChild(child));
147 d_insideInsideRoot->destroyChild(child);
148
149 child = d_insideInsideRoot->createChild("DefaultWindow");
150 child->setDestroyedByParent(false);
151 d_insideInsideRoot->removeChild(child);
152 BOOST_CHECK(!d_insideInsideRoot->isChild(child));
153 CEGUI::WindowManager::getSingleton().destroyWindow(child);
154 }
155
BOOST_AUTO_TEST_CASE(RecursiveSearch)156 BOOST_AUTO_TEST_CASE(RecursiveSearch)
157 {
158 int previousID[3];
159 previousID[0] = d_root->getID();
160 previousID[1] = d_insideRoot->getID();
161 previousID[2] = d_insideInsideRoot->getID();
162
163 d_root->setID(2);
164 d_insideRoot->setID(3);
165 d_insideInsideRoot->setID(5);
166
167 BOOST_CHECK_EQUAL(d_insideInsideRoot, d_root->getChildRecursive(5)); // finding the correct window
168 BOOST_CHECK_EQUAL(d_insideRoot, d_root->getChildRecursive(3));
169 BOOST_CHECK_EQUAL(d_insideInsideRoot, d_insideRoot->getChildRecursive(5));
170
171 BOOST_CHECK(0 == d_root->getChildRecursive(10)); // not finding these
172 BOOST_CHECK(0 == d_root->getChildRecursive(6));
173 BOOST_CHECK(0 == d_insideRoot->getChildRecursive(10));
174
175 d_root->setID(previousID[0]);
176 d_insideRoot->setID(previousID[1]);
177 d_insideInsideRoot->setID(previousID[2]);
178 }
179
180 BOOST_AUTO_TEST_SUITE_END()
181