property_test.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <stdio.h>
31 
32 #include <gtest/gtest.h>
33 
39 
41 
42 using namespace rviz;
43 
44 TEST(Property, name)
45 {
46  Property p;
47  p.setName("chub");
48  EXPECT_EQ("chub", p.getName().toStdString());
49 }
50 
51 TEST(Property, description)
52 {
53  Property p;
54  p.setDescription("chub");
55  EXPECT_EQ("chub", p.getDescription().toStdString());
56 }
57 
58 TEST(Property, value)
59 {
60  Property p;
61  p.setValue(199);
62  EXPECT_EQ(199, p.getValue().toInt());
63 }
64 
65 TEST(Property, set_value_events)
66 {
67  Property p;
68  p.setValue(0);
69 
71  p.connect(&p, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
72  p.connect(&p, SIGNAL(changed()), &r, SLOT(changed()));
73 
74  p.setValue(17);
75  EXPECT_EQ(" aboutToChange, v=0 changed, v=17", r.result().toStdString());
76 }
77 
78 TEST(Property, children)
79 {
80  Property* display = new Property("Test");
81  new Property("Alpha", 0.5, "The amount of transparency to apply to the grid lines.", display);
82  Property* beta =
83  new Property("Beta Band", 10, "The number of betas to apply to the grid lines.", display);
84  new Property("Gamma Topic", "chubby", "The topic on which to listen for Gamma messages.", display);
85  Property* position = new Property("Position", QVariant(), "Position of the chub.", display);
86  new Property("X", 1.1f, "X component of the position of the chub.", position);
87  new Property("Y", 0.717f, "Y component of the position of the chub.", position);
88 
89  // Sample usage inside the Display which owns the property.
90  int b = beta->getValue().toInt();
91  EXPECT_EQ(10, b);
92 
93  // Sample usage outside the Display
94  beta->setValue(12);
95  EXPECT_EQ(12, display->subProp("Beta Band")->getValue().toInt());
96 
97  // Compound property
98  float y = display->subProp("Position")->subProp("Y")->getValue().toFloat();
99  EXPECT_EQ(0.717f, y);
100 
101  // Accessing a missing property should not crash.
102  printf("Next line should say 'ERROR' but not crash.\n");
103  display->subProp("Position")->subProp("Z")->getValue().toFloat();
104 }
105 
106 TEST(VectorProperty, default_value)
107 {
108  VectorProperty* vp = new VectorProperty();
109  Ogre::Vector3 vec = vp->getVector();
110  EXPECT_EQ(0, vec.x);
111  EXPECT_EQ(0, vec.y);
112  EXPECT_EQ(0, vec.z);
113 }
114 
115 TEST(VectorProperty, set_and_get)
116 {
117  VectorProperty* vp = new VectorProperty();
118  Ogre::Vector3 vec(1, 2, 3);
119  vp->setVector(vec);
120 
121  Ogre::Vector3 vec2 = vp->getVector();
122  EXPECT_EQ(1, vec2.x);
123  EXPECT_EQ(2, vec2.y);
124  EXPECT_EQ(3, vec2.z);
125 }
126 
127 TEST(VectorProperty, set_string)
128 {
129  VectorProperty* vp = new VectorProperty();
130  vp->setValue(QString("1;2;3"));
131 
132  Ogre::Vector3 vec = vp->getVector();
133  EXPECT_EQ(1, vec.x);
134  EXPECT_EQ(2, vec.y);
135  EXPECT_EQ(3, vec.z);
136 
137  vp->setValue(QString("chubby!"));
138 
139  vec = vp->getVector();
140  EXPECT_EQ(1, vec.x);
141  EXPECT_EQ(2, vec.y);
142  EXPECT_EQ(3, vec.z);
143 }
144 
145 TEST(VectorProperty, set_child)
146 {
147  VectorProperty* vp = new VectorProperty();
148  vp->subProp("X")->setValue(0.9);
149  vp->subProp("Y")->setValue(1.1);
150  vp->subProp("Z")->setValue(1.3);
151 
152  Ogre::Vector3 vec = vp->getVector();
153  EXPECT_EQ(0.9f, vec.x);
154  EXPECT_EQ(1.1f, vec.y);
155  EXPECT_EQ(1.3f, vec.z);
156 }
157 
158 TEST(VectorProperty, get_child)
159 {
160  VectorProperty* vp = new VectorProperty("v", Ogre::Vector3(1, 2, 3));
161  EXPECT_EQ(1, vp->subProp("X")->getValue().toFloat());
162  EXPECT_EQ(2, vp->subProp("Y")->getValue().toFloat());
163  EXPECT_EQ(3, vp->subProp("Z")->getValue().toFloat());
164 }
165 
166 TEST(VectorProperty, set_value_events)
167 {
168  VectorProperty p;
169 
171  p.connect(&p, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
172  p.connect(&p, SIGNAL(changed()), &r, SLOT(changed()));
173 
174  p.setVector(Ogre::Vector3(.1, .0001, 1000));
175  EXPECT_EQ(" aboutToChange, v=0; 0; 0 changed, v=0.1; 0.0001; 1000", r.result().toStdString());
176  r.reset();
177 
178  p.subProp("Z")->setValue(2.1);
179  EXPECT_EQ(" aboutToChange, v=0.1; 0.0001; 1000 changed, v=0.1; 0.0001; 2.1", r.result().toStdString());
180 }
181 
182 TEST(QuaternionProperty, default_value)
183 {
185  Ogre::Quaternion quat = qp->getQuaternion();
186  EXPECT_EQ(0, quat.x);
187  EXPECT_EQ(0, quat.y);
188  EXPECT_EQ(0, quat.z);
189  EXPECT_EQ(1, quat.w);
190 }
191 
193 {
195  Ogre::Quaternion quat(4, 1, 2, 3);
196  qp->setQuaternion(quat);
197 
198  Ogre::Quaternion quat2 = qp->getQuaternion();
199  EXPECT_EQ(1, quat2.x);
200  EXPECT_EQ(2, quat2.y);
201  EXPECT_EQ(3, quat2.z);
202  EXPECT_EQ(4, quat2.w);
203 }
204 
206 {
208  qp->setValue(QString("1;2;3;4"));
209 
210  Ogre::Quaternion quat = qp->getQuaternion();
211  EXPECT_EQ(1, quat.x);
212  EXPECT_EQ(2, quat.y);
213  EXPECT_EQ(3, quat.z);
214  EXPECT_EQ(4, quat.w);
215 
216  qp->setValue(QString("chubby!"));
217 
218  quat = qp->getQuaternion();
219  EXPECT_EQ(1, quat.x);
220  EXPECT_EQ(2, quat.y);
221  EXPECT_EQ(3, quat.z);
222  EXPECT_EQ(4, quat.w);
223 }
224 
226 {
228  qp->subProp("X")->setValue(0.9);
229  qp->subProp("Y")->setValue(1.1);
230  qp->subProp("Z")->setValue(1.3);
231  qp->subProp("W")->setValue(1.5);
232 
233  Ogre::Quaternion quat = qp->getQuaternion();
234  EXPECT_EQ(0.9f, quat.x);
235  EXPECT_EQ(1.1f, quat.y);
236  EXPECT_EQ(1.3f, quat.z);
237  EXPECT_EQ(1.5f, quat.w);
238 }
239 
241 {
242  QuaternionProperty* qp = new QuaternionProperty("v", Ogre::Quaternion(4, 1, 2, 3));
243  EXPECT_EQ(1, qp->subProp("X")->getValue().toFloat());
244  EXPECT_EQ(2, qp->subProp("Y")->getValue().toFloat());
245  EXPECT_EQ(3, qp->subProp("Z")->getValue().toFloat());
246  EXPECT_EQ(4, qp->subProp("W")->getValue().toFloat());
247 }
248 
249 TEST(QuaternionProperty, set_value_events)
250 {
252 
254  p.connect(&p, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
255  p.connect(&p, SIGNAL(changed()), &r, SLOT(changed()));
256 
257  p.setQuaternion(Ogre::Quaternion(1, .1, .0001, 1000));
258  EXPECT_EQ(" aboutToChange, v=0; 0; 0; 1 changed, v=0.1; 0.0001; 1000; 1", r.result().toStdString());
259  r.reset();
260 
261  p.subProp("Z")->setValue(2.1);
262  EXPECT_EQ(" aboutToChange, v=0.1; 0.0001; 1000; 1 changed, v=0.1; 0.0001; 2.1; 1",
263  r.result().toStdString());
264 }
265 
266 TEST(ColorProperty, default_value)
267 {
268  ColorProperty* qp = new ColorProperty();
269  QColor color = qp->getColor();
270  EXPECT_EQ(0, color.red());
271  EXPECT_EQ(0, color.green());
272  EXPECT_EQ(0, color.blue());
273 }
274 
275 TEST(ColorProperty, set_and_get)
276 {
277  ColorProperty* qp = new ColorProperty();
278  QColor color(1, 2, 3);
279  qp->setColor(color);
280 
281  QColor color2 = qp->getColor();
282  EXPECT_EQ(1, color2.red());
283  EXPECT_EQ(2, color2.green());
284  EXPECT_EQ(3, color2.blue());
285 }
286 
287 TEST(ColorProperty, set_string)
288 {
289  ColorProperty* qp = new ColorProperty();
290  qp->setValue(QString("1;2;3"));
291 
292  QColor color = qp->getColor();
293  EXPECT_EQ(1, color.red());
294  EXPECT_EQ(2, color.green());
295  EXPECT_EQ(3, color.blue());
296 
297  qp->setValue(QString("chubby!"));
298 
299  color = qp->getColor();
300  EXPECT_EQ(1, color.red());
301  EXPECT_EQ(2, color.green());
302  EXPECT_EQ(3, color.blue());
303 }
304 
305 TEST(ColorProperty, set_string_limits)
306 {
307  ColorProperty* qp = new ColorProperty();
308  qp->setValue(QString("-1;2000;3"));
309 
310  QColor color = qp->getColor();
311  EXPECT_EQ(0, color.red());
312  EXPECT_EQ(255, color.green());
313  EXPECT_EQ(3, color.blue());
314 }
315 
316 TEST(ColorProperty, set_value_events)
317 {
318  ColorProperty p;
319 
321  p.connect(&p, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
322  p.connect(&p, SIGNAL(changed()), &r, SLOT(changed()));
323 
324  p.setColor(QColor(1, 2, 3));
325  EXPECT_EQ(" aboutToChange, v=0; 0; 0 changed, v=1; 2; 3", r.result().toStdString());
326 }
327 
329 {
330  EnumProperty p;
331 
332  EXPECT_EQ(0, p.getOptionInt());
333 
334  p.addOption("chub", 10);
335  EXPECT_EQ(0, p.getOptionInt());
336 
337  p.addOption("foo", 3);
338  p.addOption("bar", 999);
339 
340  p.setValue("chub");
341  EXPECT_EQ(10, p.getOptionInt());
342 
343  p.clearOptions();
344  EXPECT_EQ(0, p.getOptionInt());
345 }
346 
347 int main(int argc, char** argv)
348 {
349  testing::InitGoogleTest(&argc, argv);
350  return RUN_ALL_TESTS();
351 }
virtual bool setVector(const Ogre::Vector3 &vector)
f
bool setValue(const QVariant &new_value) override
Set the new value for this property. Returns true if the new value is different from the old value...
virtual QColor getColor() const
virtual bool setValue(const QVariant &new_value)
Set the new value for this property. Returns true if the new value is different from the old value...
Definition: property.cpp:134
A single element of a property tree, with a name, value, description, and possibly children...
Definition: property.h:100
virtual void clearOptions()
Clear the list of options.
virtual void setName(const QString &name)
Set the name.
Definition: property.cpp:155
virtual Ogre::Vector3 getVector() const
virtual void setDescription(const QString &description)
Set the description.
Definition: property.cpp:169
virtual QString getDescription() const
Return the description.
Definition: property.cpp:174
TEST(Property, name)
virtual Property * subProp(const QString &sub_name)
Return the first child Property with the given name, or the FailureProperty if no child has the name...
Definition: property.cpp:179
virtual void addOption(const QString &option, int value=0)
int main(int argc, char **argv)
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:164
virtual bool setQuaternion(const Ogre::Quaternion &quaternion)
virtual bool setColor(const QColor &color)
bool setValue(const QVariant &new_value) override
Set the new value for this property. Returns true if the new value is different from the old value...
bool setValue(const QVariant &new_value) override
Set the new value for this property. Returns true if the new value is different from the old value...
virtual QVariant getValue() const
Return the value of this Property as a QVariant. If the value has never been set, an invalid QVariant...
Definition: property.cpp:150
virtual int getOptionInt()
Return the int value of the currently-chosen option, or 0 if the current option string does not have ...
r
Enum property.
Definition: enum_property.h:46
virtual Ogre::Quaternion getQuaternion() const


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25