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 
34 #ifdef RVIZ_DEPRECATE_QT4_SLOTS
35 #undef RVIZ_DEPRECATE_QT4_SLOTS
36 #endif
37 
43 
45 
46 using namespace rviz;
47 
48 TEST(Property, name)
49 {
50  Property p;
51  p.setName("chub");
52  EXPECT_EQ("chub", p.getName().toStdString());
53 }
54 
55 TEST(Property, description)
56 {
57  Property p;
58  p.setDescription("chub");
59  EXPECT_EQ("chub", p.getDescription().toStdString());
60 }
61 
62 TEST(Property, value)
63 {
64  Property p;
65  p.setValue(199);
66  EXPECT_EQ(199, p.getValue().toInt());
67 }
68 
69 TEST(Property, set_value_events_qt4)
70 {
72  Property p("", 1, "", nullptr, SLOT(changed()), &r);
73  p.connect(&p, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
74  p.setValue(17);
75  EXPECT_EQ(" aboutToChange, v=1 changed, v=17", r.result.toStdString());
76  r.reset();
77 
78  // initialize from parent
79  Property p2("", 2, "", &p, SIGNAL(changed()));
80  p2.connect(&p2, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
81  p2.setValue(27);
82  EXPECT_EQ(" aboutToChange, v=2 changed, v=17", r.result.toStdString());
83  r.reset();
84 
85  // initialize from parent
86  Property p3("", 3, "", &p, SIGNAL(changed()));
87  p3.connect(&p3, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
88  p3.setValue(37);
89  EXPECT_EQ(" aboutToChange, v=3 changed, v=17", r.result.toStdString());
90 }
91 
92 TEST(Property, set_value_events_lambda)
93 {
95  Property p(
96  "", 1, "", nullptr, [&r] { r.changed(); }, &r);
97  p.connect(&p, &Property::aboutToChange, &r, [&r] { r.aboutToChange(); });
98  p.setValue(17);
99  EXPECT_EQ(" aboutToChange, v=1 changed, v=17", r.result.toStdString());
100  r.reset();
101 
102  // initialize from parent
103  Property p2("", 2, "", &p, [&r] {
104  r.changed();
105  r.result += " free lambda";
106  });
108  p2.setValue(27);
109  EXPECT_EQ(" aboutToChange, v=2 free lambda", r.result.toStdString());
110  r.reset();
111 
112  // initialize from receiver with parent
113  Property p3(
114  "", 3, "", &p, [&r] { r.changed(); }, &r);
116  p3.setValue(37);
117  EXPECT_EQ(" aboutToChange, v=3 changed, v=37", r.result.toStdString());
118  r.reset();
119 }
120 
121 TEST(Property, set_value_events_method_pointer)
122 {
124  Property p("", 1, "", nullptr, &MockPropertyChangeReceiver::changed, &r);
126  p.setValue(17);
127  EXPECT_EQ(" aboutToChange, v=1 changed, v=17", r.result.toStdString());
128  r.reset();
129 
130  // initialize from parent
131  Property p2("", 2, "", &p, &Property::changed);
133  p2.setValue(27);
134  EXPECT_EQ(" aboutToChange, v=2 changed, v=17", r.result.toStdString());
135  r.reset();
136 
137  // initialize from receiver with parent
138  Property p3("", 3, "", &p, &MockPropertyChangeReceiver::changed, &r);
140  p3.setValue(37);
141  EXPECT_EQ(" aboutToChange, v=3 changed, v=37", r.result.toStdString());
142  r.reset();
143 
144 #if 0 // This should fail to compile due to receiver type mismatching slot
145  Property mismatching_parent("", 0, "", &p, &MockPropertyChangeReceiver::changed);
146 #endif
147 
148  // A receiver nullptr is gracefully handled (i.e. ignored) by Qt
149  Property null_receiver("", 4, "", &p, &MockPropertyChangeReceiver::changed,
150  static_cast<MockPropertyChangeReceiver*>(nullptr));
151  null_receiver.setValue(47);
152  EXPECT_EQ("", r.result.toStdString());
153  r.reset();
154 }
155 
156 TEST(Property, children)
157 {
158  Property* display = new Property("Test");
159  new Property("Alpha", 0.5, "The amount of transparency to apply to the grid lines.", display);
160  Property* beta =
161  new Property("Beta Band", 10, "The number of betas to apply to the grid lines.", display);
162  new Property("Gamma Topic", "chubby", "The topic on which to listen for Gamma messages.", display);
163  Property* position = new Property("Position", QVariant(), "Position of the chub.", display);
164  new Property("X", 1.1f, "X component of the position of the chub.", position);
165  new Property("Y", 0.717f, "Y component of the position of the chub.", position);
166 
167  // Sample usage inside the Display which owns the property.
168  int b = beta->getValue().toInt();
169  EXPECT_EQ(10, b);
170 
171  // Sample usage outside the Display
172  beta->setValue(12);
173  EXPECT_EQ(12, display->subProp("Beta Band")->getValue().toInt());
174 
175  // Compound property
176  float y = display->subProp("Position")->subProp("Y")->getValue().toFloat();
177  EXPECT_EQ(0.717f, y);
178 
179  // Accessing a missing property should not crash.
180  printf("Next line should say 'ERROR' but not crash.\n");
181  display->subProp("Position")->subProp("Z")->getValue().toFloat();
182 }
183 
184 TEST(VectorProperty, default_value)
185 {
186  VectorProperty vp;
187  Ogre::Vector3 vec = vp.getVector();
188  EXPECT_EQ(0, vec.x);
189  EXPECT_EQ(0, vec.y);
190  EXPECT_EQ(0, vec.z);
191 }
192 
193 TEST(VectorProperty, set_and_get)
194 {
195  VectorProperty vp;
196  Ogre::Vector3 vec(1, 2, 3);
197  vp.setVector(vec);
198 
199  Ogre::Vector3 vec2 = vp.getVector();
200  EXPECT_EQ(1, vec2.x);
201  EXPECT_EQ(2, vec2.y);
202  EXPECT_EQ(3, vec2.z);
203 }
204 
205 TEST(VectorProperty, set_string)
206 {
207  VectorProperty vp;
208  vp.setValue(QString("1;2;3"));
209 
210  Ogre::Vector3 vec = vp.getVector();
211  EXPECT_EQ(1, vec.x);
212  EXPECT_EQ(2, vec.y);
213  EXPECT_EQ(3, vec.z);
214 
215  vp.setValue(QString("chubby!"));
216 
217  vec = vp.getVector();
218  EXPECT_EQ(1, vec.x);
219  EXPECT_EQ(2, vec.y);
220  EXPECT_EQ(3, vec.z);
221 }
222 
223 TEST(VectorProperty, set_child)
224 {
225  VectorProperty vp;
226  vp.subProp("X")->setValue(0.9);
227  vp.subProp("Y")->setValue(1.1);
228  vp.subProp("Z")->setValue(1.3);
229 
230  Ogre::Vector3 vec = vp.getVector();
231  EXPECT_EQ(0.9f, vec.x);
232  EXPECT_EQ(1.1f, vec.y);
233  EXPECT_EQ(1.3f, vec.z);
234 }
235 
236 TEST(VectorProperty, get_child)
237 {
238  VectorProperty vp("v", Ogre::Vector3(1, 2, 3));
239  EXPECT_EQ(1, vp.subProp("X")->getValue().toFloat());
240  EXPECT_EQ(2, vp.subProp("Y")->getValue().toFloat());
241  EXPECT_EQ(3, vp.subProp("Z")->getValue().toFloat());
242 }
243 
244 TEST(VectorProperty, set_value_events)
245 {
247  VectorProperty p("", Ogre::Vector3::ZERO, "", nullptr, SLOT(changed()), &r);
248  p.connect(&p, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
249 
250  p.setVector(Ogre::Vector3(.1, .0001, 1000));
251  EXPECT_EQ(" aboutToChange, v=0; 0; 0 changed, v=0.1; 0.0001; 1000", r.result.toStdString());
252  r.reset();
253 
254  p.subProp("Z")->setValue(2.1);
255  EXPECT_EQ(" aboutToChange, v=0.1; 0.0001; 1000 changed, v=0.1; 0.0001; 2.1", r.result.toStdString());
256 }
257 
258 TEST(QuaternionProperty, default_value)
259 {
261  Ogre::Quaternion quat = qp.getQuaternion();
262  EXPECT_EQ(0, quat.x);
263  EXPECT_EQ(0, quat.y);
264  EXPECT_EQ(0, quat.z);
265  EXPECT_EQ(1, quat.w);
266 }
267 
269 {
271  Ogre::Quaternion quat(4, 1, 2, 3);
272  qp.setQuaternion(quat);
273 
274  Ogre::Quaternion quat2 = qp.getQuaternion();
275  EXPECT_EQ(1, quat2.x);
276  EXPECT_EQ(2, quat2.y);
277  EXPECT_EQ(3, quat2.z);
278  EXPECT_EQ(4, quat2.w);
279 }
280 
282 {
284  qp.setValue(QString("1;2;3;4"));
285 
286  Ogre::Quaternion quat = qp.getQuaternion();
287  EXPECT_EQ(1, quat.x);
288  EXPECT_EQ(2, quat.y);
289  EXPECT_EQ(3, quat.z);
290  EXPECT_EQ(4, quat.w);
291 
292  qp.setValue(QString("chubby!"));
293 
294  quat = qp.getQuaternion();
295  EXPECT_EQ(1, quat.x);
296  EXPECT_EQ(2, quat.y);
297  EXPECT_EQ(3, quat.z);
298  EXPECT_EQ(4, quat.w);
299 }
300 
302 {
304  qp.subProp("X")->setValue(0.9);
305  qp.subProp("Y")->setValue(1.1);
306  qp.subProp("Z")->setValue(1.3);
307  qp.subProp("W")->setValue(1.5);
308 
309  Ogre::Quaternion quat = qp.getQuaternion();
310  EXPECT_EQ(0.9f, quat.x);
311  EXPECT_EQ(1.1f, quat.y);
312  EXPECT_EQ(1.3f, quat.z);
313  EXPECT_EQ(1.5f, quat.w);
314 }
315 
317 {
318  QuaternionProperty qp("v", Ogre::Quaternion(4, 1, 2, 3));
319  EXPECT_EQ(1, qp.subProp("X")->getValue().toFloat());
320  EXPECT_EQ(2, qp.subProp("Y")->getValue().toFloat());
321  EXPECT_EQ(3, qp.subProp("Z")->getValue().toFloat());
322  EXPECT_EQ(4, qp.subProp("W")->getValue().toFloat());
323 }
324 
325 TEST(QuaternionProperty, set_value_events)
326 {
328  QuaternionProperty p("", Ogre::Quaternion::IDENTITY, "", nullptr, SLOT(changed()), &r);
329  p.connect(&p, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
330 
331  p.setQuaternion(Ogre::Quaternion(1, .1, .0001, 1000));
332  EXPECT_EQ(" aboutToChange, v=0; 0; 0; 1 changed, v=0.1; 0.0001; 1000; 1", r.result.toStdString());
333  r.reset();
334 
335  p.subProp("Z")->setValue(2.1);
336  EXPECT_EQ(" aboutToChange, v=0.1; 0.0001; 1000; 1 changed, v=0.1; 0.0001; 2.1; 1",
337  r.result.toStdString());
338 }
339 
340 TEST(ColorProperty, default_value)
341 {
342  ColorProperty qp;
343  QColor color = qp.getColor();
344  EXPECT_EQ(0, color.red());
345  EXPECT_EQ(0, color.green());
346  EXPECT_EQ(0, color.blue());
347 }
348 
349 TEST(ColorProperty, set_and_get)
350 {
351  ColorProperty qp;
352  QColor color(1, 2, 3);
353  qp.setColor(color);
354 
355  QColor color2 = qp.getColor();
356  EXPECT_EQ(1, color2.red());
357  EXPECT_EQ(2, color2.green());
358  EXPECT_EQ(3, color2.blue());
359 }
360 
361 TEST(ColorProperty, set_string)
362 {
363  ColorProperty qp;
364  qp.setValue(QString("1;2;3"));
365 
366  QColor color = qp.getColor();
367  EXPECT_EQ(1, color.red());
368  EXPECT_EQ(2, color.green());
369  EXPECT_EQ(3, color.blue());
370 
371  qp.setValue(QString("chubby!"));
372 
373  color = qp.getColor();
374  EXPECT_EQ(1, color.red());
375  EXPECT_EQ(2, color.green());
376  EXPECT_EQ(3, color.blue());
377 }
378 
379 TEST(ColorProperty, set_string_limits)
380 {
381  ColorProperty qp;
382  qp.setValue(QString("-1;2000;3"));
383 
384  QColor color = qp.getColor();
385  EXPECT_EQ(0, color.red());
386  EXPECT_EQ(255, color.green());
387  EXPECT_EQ(3, color.blue());
388 }
389 
390 TEST(ColorProperty, set_value_events)
391 {
393  ColorProperty p("", QColor(0, 0, 0), "", nullptr, SLOT(changed()), &r);
394  p.connect(&p, SIGNAL(aboutToChange()), &r, SLOT(aboutToChange()));
395 
396  p.setColor(QColor(1, 2, 3));
397  EXPECT_EQ(" aboutToChange, v=0; 0; 0 changed, v=1; 2; 3", r.result.toStdString());
398 }
399 
401 {
402  EnumProperty p;
403 
404  EXPECT_EQ(0, p.getOptionInt());
405 
406  p.addOption("chub", 10);
407  EXPECT_EQ(0, p.getOptionInt());
408 
409  p.addOption("foo", 3);
410  p.addOption("bar", 999);
411 
412  p.setValue("chub");
413  EXPECT_EQ(10, p.getOptionInt());
414 
415  p.clearOptions();
416  EXPECT_EQ(0, p.getOptionInt());
417 }
418 
419 int main(int argc, char** argv)
420 {
421  testing::InitGoogleTest(&argc, argv);
422  return RUN_ALL_TESTS();
423 }
rviz::MockPropertyChangeReceiver::aboutToChange
void aboutToChange()
Definition: mock_property_change_receiver.cpp:36
rviz::EnumProperty::getOptionInt
virtual int getOptionInt()
Return the int value of the currently-chosen option, or 0 if the current option string does not have ...
Definition: enum_property.cpp:56
rviz::EnumProperty::clearOptions
virtual void clearOptions()
Clear the list of options.
Definition: enum_property.cpp:44
rviz::ColorProperty::getColor
virtual QColor getColor() const
Definition: color_property.h:79
rviz::Property::setName
virtual void setName(const QString &name)
Set the name.
Definition: property.cpp:155
rviz::QuaternionProperty::setValue
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,...
Definition: quaternion_property.cpp:81
property.h
mock_property_change_receiver.h
rviz::MockPropertyChangeReceiver::changed
void changed()
Definition: mock_property_change_receiver.cpp:42
rviz::Property::subProp
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
rviz::ColorProperty::setValue
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,...
Definition: color_property.cpp:65
rviz::VectorProperty::setValue
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,...
Definition: vector_property.cpp:75
rviz::Property::getValue
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
f
f
rviz::ColorProperty
Definition: color_property.h:40
rviz::MockPropertyChangeReceiver::result
QString result
Definition: mock_property_change_receiver.h:41
rviz::Property::connect
QMetaObject::Connection connect(const QObject *receiver, const char *slot, Qt::ConnectionType type=Qt::AutoConnection)
Connect changed() signal to given slot of receiver.
Definition: property.cpp:78
quaternion_property.h
rviz::EnumProperty
Enum property.
Definition: enum_property.h:46
rviz::QuaternionProperty
Definition: quaternion_property.h:38
rviz::Property
A single element of a property tree, with a name, value, description, and possibly children.
Definition: property.h:100
TEST
TEST(Property, name)
Definition: property_test.cpp:48
rviz::EnumProperty::addOption
virtual void addOption(const QString &option, int value=0)
Definition: enum_property.cpp:50
rviz::Property::setValue
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
rviz
Definition: add_display_dialog.cpp:54
rviz::VectorProperty::setVector
virtual bool setVector(const Ogre::Vector3 &vector)
Definition: vector_property.cpp:55
rviz::QuaternionProperty::setQuaternion
virtual bool setQuaternion(const Ogre::Quaternion &quaternion)
Definition: quaternion_property.cpp:60
color_property.h
rviz::MockPropertyChangeReceiver::reset
void reset()
Definition: mock_property_change_receiver.h:43
rviz::Property::setDescription
virtual void setDescription(const QString &description)
Set the description.
Definition: property.cpp:169
rviz::MockPropertyChangeReceiver
Definition: mock_property_change_receiver.h:36
rviz::Property::getName
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:164
rviz::Property::getDescription
virtual QString getDescription() const
Return the description.
Definition: property.cpp:174
main
int main(int argc, char **argv)
Definition: property_test.cpp:419
vector_property.h
rviz::Property::changed
void changed()
Emitted by setValue() just after the value has changed.
rviz::VectorProperty
Definition: vector_property.h:39
rviz::QuaternionProperty::getQuaternion
virtual Ogre::Quaternion getQuaternion() const
Definition: quaternion_property.h:72
rviz::VectorProperty::getVector
virtual Ogre::Vector3 getVector() const
Definition: vector_property.h:73
rviz::Property::aboutToChange
void aboutToChange()
Emitted by setValue() just before the value has changed.
rviz::ColorProperty::setColor
virtual bool setColor(const QColor &color)
Definition: color_property.cpp:50
enum_property.h


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Dec 13 2024 03:31:02