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 = new Property( "Beta Band", 10, "The number of betas to apply to the grid lines.", display );
83  new Property( "Gamma Topic", "chubby", "The topic on which to listen for Gamma messages.", display );
84  Property* position = new Property( "Position", QVariant(), "Position of the chub.", display );
85  new Property( "X", 1.1f, "X component of the position of the chub.", position );
86  new Property( "Y", 0.717f, "Y component of the position of the chub.", position );
87 
88  // Sample usage inside the Display which owns the property.
89  int b = beta->getValue().toInt();
90  EXPECT_EQ( 10, b );
91 
92  // Sample usage outside the Display
93  beta->setValue( 12 );
94  EXPECT_EQ( 12, display->subProp( "Beta Band" )->getValue().toInt() );
95 
96  // Compound property
97  float y = display->subProp( "Position" )->subProp( "Y" )->getValue().toFloat();
98  EXPECT_EQ( 0.717f, y );
99 
100  // Accessing a missing property should not crash.
101  printf( "Next line should say 'ERROR' but not crash.\n" );
102  display->subProp( "Position" )->subProp( "Z" )->getValue().toFloat();
103 }
104 
105 TEST( VectorProperty, default_value )
106 {
107  VectorProperty* vp = new VectorProperty();
108  Ogre::Vector3 vec = vp->getVector();
109  EXPECT_EQ( 0, vec.x );
110  EXPECT_EQ( 0, vec.y );
111  EXPECT_EQ( 0, vec.z );
112 }
113 
114 TEST( VectorProperty, set_and_get )
115 {
116  VectorProperty* vp = new VectorProperty();
117  Ogre::Vector3 vec( 1, 2, 3 );
118  vp->setVector( vec );
119 
120  Ogre::Vector3 vec2 = vp->getVector();
121  EXPECT_EQ( 1, vec2.x );
122  EXPECT_EQ( 2, vec2.y );
123  EXPECT_EQ( 3, vec2.z );
124 }
125 
126 TEST( VectorProperty, set_string )
127 {
128  VectorProperty* vp = new VectorProperty();
129  vp->setValue( QString( "1;2;3" ));
130 
131  Ogre::Vector3 vec = vp->getVector();
132  EXPECT_EQ( 1, vec.x );
133  EXPECT_EQ( 2, vec.y );
134  EXPECT_EQ( 3, vec.z );
135 
136  vp->setValue( QString( "chubby!" ));
137 
138  vec = vp->getVector();
139  EXPECT_EQ( 1, vec.x );
140  EXPECT_EQ( 2, vec.y );
141  EXPECT_EQ( 3, vec.z );
142 }
143 
144 TEST( VectorProperty, set_child )
145 {
146  VectorProperty* vp = new VectorProperty();
147  vp->subProp( "X" )->setValue( 0.9 );
148  vp->subProp( "Y" )->setValue( 1.1 );
149  vp->subProp( "Z" )->setValue( 1.3 );
150 
151  Ogre::Vector3 vec = vp->getVector();
152  EXPECT_EQ( 0.9f, vec.x );
153  EXPECT_EQ( 1.1f, vec.y );
154  EXPECT_EQ( 1.3f, vec.z );
155 }
156 
157 TEST( VectorProperty, get_child )
158 {
159  VectorProperty* vp = new VectorProperty( "v", Ogre::Vector3( 1, 2, 3 ));
160  EXPECT_EQ( 1, vp->subProp( "X" )->getValue().toFloat() );
161  EXPECT_EQ( 2, vp->subProp( "Y" )->getValue().toFloat() );
162  EXPECT_EQ( 3, vp->subProp( "Z" )->getValue().toFloat() );
163 }
164 
165 TEST( VectorProperty, set_value_events )
166 {
167  VectorProperty p;
168 
170  p.connect( &p, SIGNAL( aboutToChange() ), &r, SLOT( aboutToChange() ));
171  p.connect( &p, SIGNAL( changed() ), &r, SLOT( changed() ));
172 
173  p.setVector( Ogre::Vector3( .1, .0001, 1000 ));
174  EXPECT_EQ( " aboutToChange, v=0; 0; 0 changed, v=0.1; 0.0001; 1000", r.result().toStdString() );
175  r.reset();
176 
177  p.subProp( "Z" )->setValue( 2.1 );
178  EXPECT_EQ( " aboutToChange, v=0.1; 0.0001; 1000 changed, v=0.1; 0.0001; 2.1", r.result().toStdString() );
179 }
180 
181 TEST( QuaternionProperty, default_value )
182 {
184  Ogre::Quaternion quat = qp->getQuaternion();
185  EXPECT_EQ( 0, quat.x );
186  EXPECT_EQ( 0, quat.y );
187  EXPECT_EQ( 0, quat.z );
188  EXPECT_EQ( 1, quat.w );
189 }
190 
191 TEST( QuaternionProperty, set_and_get )
192 {
194  Ogre::Quaternion quat( 4, 1, 2, 3 );
195  qp->setQuaternion( quat );
196 
197  Ogre::Quaternion quat2 = qp->getQuaternion();
198  EXPECT_EQ( 1, quat2.x );
199  EXPECT_EQ( 2, quat2.y );
200  EXPECT_EQ( 3, quat2.z );
201  EXPECT_EQ( 4, quat2.w );
202 }
203 
204 TEST( QuaternionProperty, set_string )
205 {
207  qp->setValue( QString( "1;2;3;4" ));
208 
209  Ogre::Quaternion quat = qp->getQuaternion();
210  EXPECT_EQ( 1, quat.x );
211  EXPECT_EQ( 2, quat.y );
212  EXPECT_EQ( 3, quat.z );
213  EXPECT_EQ( 4, quat.w );
214 
215  qp->setValue( QString( "chubby!" ));
216 
217  quat = qp->getQuaternion();
218  EXPECT_EQ( 1, quat.x );
219  EXPECT_EQ( 2, quat.y );
220  EXPECT_EQ( 3, quat.z );
221  EXPECT_EQ( 4, quat.w );
222 }
223 
225 {
227  qp->subProp( "X" )->setValue( 0.9 );
228  qp->subProp( "Y" )->setValue( 1.1 );
229  qp->subProp( "Z" )->setValue( 1.3 );
230  qp->subProp( "W" )->setValue( 1.5 );
231 
232  Ogre::Quaternion quat = qp->getQuaternion();
233  EXPECT_EQ( 0.9f, quat.x );
234  EXPECT_EQ( 1.1f, quat.y );
235  EXPECT_EQ( 1.3f, quat.z );
236  EXPECT_EQ( 1.5f, quat.w );
237 }
238 
240 {
241  QuaternionProperty* qp = new QuaternionProperty( "v", Ogre::Quaternion( 4, 1, 2, 3 ));
242  EXPECT_EQ( 1, qp->subProp( "X" )->getValue().toFloat() );
243  EXPECT_EQ( 2, qp->subProp( "Y" )->getValue().toFloat() );
244  EXPECT_EQ( 3, qp->subProp( "Z" )->getValue().toFloat() );
245  EXPECT_EQ( 4, qp->subProp( "W" )->getValue().toFloat() );
246 }
247 
248 TEST( QuaternionProperty, set_value_events )
249 {
251 
253  p.connect( &p, SIGNAL( aboutToChange() ), &r, SLOT( aboutToChange() ));
254  p.connect( &p, SIGNAL( changed() ), &r, SLOT( changed() ));
255 
256  p.setQuaternion( Ogre::Quaternion( 1, .1, .0001, 1000 ));
257  EXPECT_EQ( " aboutToChange, v=0; 0; 0; 1 changed, v=0.1; 0.0001; 1000; 1", r.result().toStdString() );
258  r.reset();
259 
260  p.subProp( "Z" )->setValue( 2.1 );
261  EXPECT_EQ( " aboutToChange, v=0.1; 0.0001; 1000; 1 changed, v=0.1; 0.0001; 2.1; 1", r.result().toStdString() );
262 }
263 
264 TEST( ColorProperty, default_value )
265 {
266  ColorProperty* qp = new ColorProperty();
267  QColor color = qp->getColor();
268  EXPECT_EQ( 0, color.red() );
269  EXPECT_EQ( 0, color.green() );
270  EXPECT_EQ( 0, color.blue() );
271 }
272 
273 TEST( ColorProperty, set_and_get )
274 {
275  ColorProperty* qp = new ColorProperty();
276  QColor color( 1, 2, 3 );
277  qp->setColor( color );
278 
279  QColor color2 = qp->getColor();
280  EXPECT_EQ( 1, color2.red() );
281  EXPECT_EQ( 2, color2.green() );
282  EXPECT_EQ( 3, color2.blue() );
283 }
284 
285 TEST( ColorProperty, set_string )
286 {
287  ColorProperty* qp = new ColorProperty();
288  qp->setValue( QString( "1;2;3" ));
289 
290  QColor color = qp->getColor();
291  EXPECT_EQ( 1, color.red() );
292  EXPECT_EQ( 2, color.green() );
293  EXPECT_EQ( 3, color.blue() );
294 
295  qp->setValue( QString( "chubby!" ));
296 
297  color = qp->getColor();
298  EXPECT_EQ( 1, color.red() );
299  EXPECT_EQ( 2, color.green() );
300  EXPECT_EQ( 3, color.blue() );
301 }
302 
303 TEST( ColorProperty, set_string_limits )
304 {
305  ColorProperty* qp = new ColorProperty();
306  qp->setValue( QString( "-1;2000;3" ));
307 
308  QColor color = qp->getColor();
309  EXPECT_EQ( 0, color.red() );
310  EXPECT_EQ( 255, color.green() );
311  EXPECT_EQ( 3, color.blue() );
312 }
313 
314 TEST( ColorProperty, set_value_events )
315 {
316  ColorProperty p;
317 
319  p.connect( &p, SIGNAL( aboutToChange() ), &r, SLOT( aboutToChange() ));
320  p.connect( &p, SIGNAL( changed() ), &r, SLOT( changed() ));
321 
322  p.setColor( QColor( 1, 2, 3 ));
323  EXPECT_EQ( " aboutToChange, v=0; 0; 0 changed, v=1; 2; 3", r.result().toStdString() );
324 }
325 
327 {
328  EnumProperty p;
329 
330  EXPECT_EQ( 0, p.getOptionInt() );
331 
332  p.addOption( "chub", 10 );
333  EXPECT_EQ( 0, p.getOptionInt() );
334 
335  p.addOption( "foo", 3 );
336  p.addOption( "bar", 999 );
337 
338  p.setValue( "chub" );
339  EXPECT_EQ( 10, p.getOptionInt() );
340 
341  p.clearOptions();
342  EXPECT_EQ( 0, p.getOptionInt() );
343 }
344 
345 int main( int argc, char **argv ) {
346  testing::InitGoogleTest( &argc, argv );
347  return RUN_ALL_TESTS();
348 }
virtual Ogre::Quaternion getQuaternion() const
virtual QColor getColor() const
virtual bool setVector(const Ogre::Vector3 &vector)
f
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:130
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:150
TFSIMD_FORCE_INLINE const tfScalar & y() const
virtual void setDescription(const QString &description)
Set the description.
Definition: property.cpp:164
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...
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:174
virtual void addOption(const QString &option, int value=0)
int main(int argc, char **argv)
virtual bool setQuaternion(const Ogre::Quaternion &quaternion)
virtual bool setColor(const QColor &color)
virtual Ogre::Vector3 getVector() 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...
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:145
virtual QString getDescription() const
Return the description.
Definition: property.cpp:169
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...
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:47
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:159


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:51