display_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 <gtest/gtest.h>
31 
32 #include <QApplication>
33 
34 #include <ros/ros.h>
35 
36 #include <sstream>
37 
40 #include <rviz/display_group.h>
41 #include <rviz/config.h>
44 
45 #include "mock_display.h"
46 #include "mock_context.h"
47 
48 using namespace rviz;
49 
50 TEST( Display, load_properties )
51 {
52  std::stringstream input( "Name: sample\n"
53  "Enabled: true\n"
54  "Count: 7\n"
55  "Pi: 3.2\n"
56  "Offset: {X: -1, Y: 1.1, Z: 1.1e3}\n"
57  "Color: white\n"
58  "Style: loosey goosey\n" );
59 
62  reader.readStream(config, input);
63 
64  MockDisplay d;
65  d.load( config );
66 
67  EXPECT_EQ( 7, d.count_->getValue().toInt() );
68  EXPECT_EQ( "loosey goosey", d.style_->getValue().toString().toStdString() );
69  EXPECT_EQ( 3.2f, d.pi_->getValue().toFloat() );
70  Ogre::Vector3 offset = d.offset_->getVector();
71  EXPECT_EQ( -1.f, offset.x );
72  EXPECT_EQ( 1.1f, offset.y );
73  EXPECT_EQ( 1100.f, offset.z );
74  EXPECT_EQ( "255; 255; 255", d.color_->getValue().toString().toStdString() );
75  EXPECT_EQ( true, d.getValue().toBool() );
76 }
77 
78 TEST( DisplayGroup, load_properties )
79 {
80  std::stringstream input(
81  "Name: root\n"
82  "Enabled: true\n"
83  "Displays:\n"
84  " -\n"
85  " Class: MockDisplay\n"
86  " Name: Steven\n"
87  " Enabled: false\n"
88  " Count: 17\n"
89  " -\n"
90  " Name: sub group\n"
91  " Class: DisplayGroup\n"
92  " Enabled: true\n"
93  " Displays:\n"
94  " -\n"
95  " Class: MockDisplay\n"
96  " Name: Curly\n"
97  " Enabled: false\n"
98  " Count: 900\n"
99  " -\n"
100  " Class: BrokenDisplay\n"
101  " Name: Joe\n"
102  " Enabled: true\n"
103  " Count: 33\n"
104  );
105 
106  rviz::YamlConfigReader reader;
108  reader.readStream(config, input);
109 
110  DisplayGroup g;
111  g.load( config );
112 
113  EXPECT_EQ( true, g.getValue().toBool() );
114  EXPECT_EQ( false, g.subProp("Steven")->getValue().toBool() );
115  EXPECT_EQ( 17, g.subProp("Steven")->subProp("Count")->getValue().toInt() );
116  EXPECT_EQ( 900, g.subProp("sub group")->subProp("Curly")->subProp("Count")->getValue().toInt() );
117  EXPECT_EQ( "The class required for this display, 'BrokenDisplay', could not be loaded.",
118  g.subProp("Joe")->getDescription().left( 74 ).toStdString());
119 }
120 
121 TEST( Display, save_properties)
122 {
123  MockDisplay d;
124  d.setName( "Steven" );
125  d.subProp( "Count" )->setValue( 37 );
126 
127  rviz::YamlConfigWriter writer;
129  d.save( config );
130  QString out = writer.writeString(config);
131 
132  // Since we instantiated the display directly instead of using the
133  // DisplayFactory, it won't know its class name.
134  EXPECT_EQ( std::string(
135  "Class: \"\"\n"
136  "Name: Steven\n"
137  "Enabled: false\n"
138  "Count: 37\n"
139  "Style: chunky\n"
140  "Pi: 3.14159\n"
141  "Offset: {X: 1, Y: 2, Z: 3}\n"
142  "Color: 10; 20; 30"
143  )
144  , out.toStdString().c_str() );
145 }
146 
147 TEST( DisplayGroup, save_properties)
148 {
149  DisplayGroup g;
150  g.setName( "Charles" );
151 
152  MockDisplay *d = new MockDisplay;
153  d->setName( "Steven" );
154  d->subProp( "Count" )->setValue( 101 );
155  g.addChild( d );
156 
157  d = new MockDisplay;
158  d->setName( "Katherine" );
159  d->subProp( "Pi" )->setValue( 1.1 );
160  g.addChild( d );
161 
162  rviz::YamlConfigWriter writer;
164  g.save( config );
165  QString out = writer.writeString(config);
166 
167  // Since we instantiated the display directly instead of using the
168  // DisplayFactory, it won't know its class name.
169  EXPECT_EQ( std::string(
170  "Class: \"\"\n"
171  "Name: Charles\n"
172  "Enabled: false\n"
173  "Displays:\n"
174  " - Class: \"\"\n"
175  " Name: Steven\n"
176  " Enabled: false\n"
177  " Count: 101\n"
178  " Style: chunky\n"
179  " Pi: 3.14159\n"
180  " Offset: {X: 1, Y: 2, Z: 3}\n"
181  " Color: 10; 20; 30\n"
182  " - Class: \"\"\n"
183  " Name: Katherine\n"
184  " Enabled: false\n"
185  " Count: 10\n"
186  " Style: chunky\n"
187  " Pi: 1.1\n"
188  " Offset: {X: 1, Y: 2, Z: 3}\n"
189  " Color: 10; 20; 30"
190  )
191  , out.toStdString().c_str() );
192 }
193 
194 TEST( DisplayFactory, class_name )
195 {
196  std::stringstream input(
197  "Displays:\n"
198  " -\n"
199  " Class: MockDisplay\n"
200  );
201 
202  rviz::YamlConfigReader reader;
204  reader.readStream(config, input);
205 
206  DisplayGroup g;
207  g.load( config );
208 
209  EXPECT_EQ( 1, g.numChildren() );
210  EXPECT_EQ( "MockDisplay", g.getDisplayAt( 0 )->getClassId().toStdString() );
211 }
212 
213 TEST( DisplayFactory, failed_display )
214 {
215  std::stringstream input(
216  "Displays:\n"
217  " - Class: MissingDisplay\n"
218  " Name: Chubbers\n"
219  " NumLemurs: 77\n"
220  " LemurStyle: chunky\n"
221  " SubYaml:\n"
222  " - 1\n"
223  " - foo: bar\n"
224  " food: bard\n"
225  );
226 
227  rviz::YamlConfigReader reader;
229  reader.readStream(config, input);
230 
231  DisplayGroup g;
232  g.load( config );
233 
234  EXPECT_EQ( 1, g.numChildren() );
235  EXPECT_EQ( "MissingDisplay", g.getDisplayAt( 0 )->getClassId().toStdString() );
236  EXPECT_EQ( 0, g.getDisplayAt( 0 )->numChildren() ); // FailedDisplay does not have any children.
237 
238  // When a FailedDisplay is saved, it should write out its contents
239  // that it was loaded with, so data is not lost.
240  rviz::YamlConfigWriter writer;
241  rviz::Config config2;
242  g.save( config2 );
243  QString out = writer.writeString(config);
244  EXPECT_EQ( std::string(
245  "Class: \"\"\n"
246  "Name: \"\"\n"
247  "Enabled: false\n"
248  "Displays:\n"
249  " - Class: MissingDisplay\n"
250  " LemurStyle: chunky\n"
251  " Name: Chubbers\n"
252  " NumLemurs: 77\n"
253  " SubYaml:\n"
254  " - 1\n"
255  " - foo: bar\n"
256  " food: bard"
257  )
258  , out.toStdString().c_str() );
259 }
260 
261 int main( int argc, char **argv ) {
262  ros::init( argc, argv, "display_test", ros::init_options::AnonymousName );
263  QApplication app(argc, argv);
264  testing::InitGoogleTest( &argc, argv );
265  return RUN_ALL_TESTS();
266 }
virtual void save(Config config) const
Save subproperties and the list of displays in this group to the given Config node.
app
d
TEST(Display, load_properties)
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
VectorProperty * offset_
Definition: mock_display.h:49
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
config
QString writeString(const Config &config, const QString &filename="data string")
Write config data to a string, and return it. This potentially changes the return values of error() a...
virtual void load(const Config &config)
Load subproperties and the list of displays in this group from the given Config node, which must be a map.
void readStream(Config &config, std::istream &in, const QString &filename="data stream")
Read config data from a std::istream. This potentially changes the return value sof error()...
int main(int argc, char **argv)
virtual int numChildren() const
Return the number of child objects (Property or otherwise).
Definition: property.h:215
Configuration data storage class.
Definition: config.h:125
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 int numChildren() const
Return the number of child objects (Property and Display).
Property * style_
Definition: mock_display.h:47
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
Definition: display.h:85
A Display object which stores other Displays as children.
Definition: display_group.h:47
virtual void save(Config config) const
Write this display to the given Config node.
Definition: display.cpp:260
Property * pi_
Definition: mock_display.h:48
virtual Display * getDisplayAt(int index) const
Return the index-th Display in this group, or NULL if the index is invalid.
virtual Ogre::Vector3 getVector() const
virtual void load(const Config &config)
Load the settings for this display from the given Config node, which must be a map.
Definition: display.cpp:242
void setName(const QString &name)
Overridden from Property to set associated widget title to the new name.
Definition: display.cpp:410
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
Property * count_
Definition: mock_display.h:46
ColorProperty * color_
Definition: mock_display.h:50
virtual void addChild(Property *child, int index=-1)
Add a child Property or Display.


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