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


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