display_test.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <gtest/gtest.h>
00031 
00032 #include <QApplication>
00033 
00034 #include <ros/ros.h>
00035 
00036 #include <sstream>
00037 
00038 #include <rviz/properties/vector_property.h>
00039 #include <rviz/properties/color_property.h>
00040 #include <rviz/display_group.h>
00041 #include <rviz/config.h>
00042 #include <rviz/yaml_config_reader.h>
00043 #include <rviz/yaml_config_writer.h>
00044 
00045 #include "mock_display.h"
00046 #include "mock_context.h"
00047 
00048 using namespace rviz;
00049 
00050 TEST( Display, load_properties )
00051 {
00052   std::stringstream input( "Name: sample\n"
00053                            "Enabled: true\n"
00054                            "Count: 7\n"
00055                            "Pi: 3.2\n"
00056                            "Offset: {X: -1, Y: 1.1, Z: 1.1e3}\n"
00057                            "Color: white\n"
00058                            "Style: loosey goosey\n" );
00059 
00060   rviz::YamlConfigReader reader;
00061   rviz::Config config;
00062   reader.readStream(config, input);
00063 
00064   MockDisplay d;
00065   d.load( config );
00066 
00067   EXPECT_EQ( 7, d.count_->getValue().toInt() );
00068   EXPECT_EQ( "loosey goosey", d.style_->getValue().toString().toStdString() );
00069   EXPECT_EQ( 3.2f, d.pi_->getValue().toFloat() );
00070   Ogre::Vector3 offset = d.offset_->getVector();
00071   EXPECT_EQ( -1.f, offset.x );
00072   EXPECT_EQ( 1.1f, offset.y );
00073   EXPECT_EQ( 1100.f, offset.z );
00074   EXPECT_EQ( "255; 255; 255", d.color_->getValue().toString().toStdString() );
00075   EXPECT_EQ( true, d.getValue().toBool() );
00076 }
00077 
00078 TEST( DisplayGroup, load_properties )
00079 {
00080   std::stringstream input(
00081     "Name: root\n"
00082     "Enabled: true\n"
00083     "Displays:\n"
00084     " -\n"
00085     "   Class: MockDisplay\n"
00086     "   Name: Steven\n"
00087     "   Enabled: false\n"
00088     "   Count: 17\n"
00089     " -\n"
00090     "   Name: sub group\n"
00091     "   Class: DisplayGroup\n"
00092     "   Enabled: true\n"
00093     "   Displays:\n"
00094     "    -\n"
00095     "      Class: MockDisplay\n"
00096     "      Name: Curly\n"
00097     "      Enabled: false\n"
00098     "      Count: 900\n"
00099     " -\n"
00100     "   Class: BrokenDisplay\n"
00101     "   Name: Joe\n"
00102     "   Enabled: true\n"
00103     "   Count: 33\n"
00104     );
00105 
00106   rviz::YamlConfigReader reader;
00107   rviz::Config config;
00108   reader.readStream(config, input);
00109 
00110   DisplayGroup g;
00111   g.load( config );
00112 
00113   EXPECT_EQ( true, g.getValue().toBool() );
00114   EXPECT_EQ( false, g.subProp("Steven")->getValue().toBool() );
00115   EXPECT_EQ( 17, g.subProp("Steven")->subProp("Count")->getValue().toInt() );
00116   EXPECT_EQ( 900, g.subProp("sub group")->subProp("Curly")->subProp("Count")->getValue().toInt() );
00117   EXPECT_EQ( "The class required for this display, 'BrokenDisplay', could not be loaded.",
00118              g.subProp("Joe")->getDescription().left( 74 ).toStdString());
00119 }
00120 
00121 TEST( Display, save_properties)
00122 {
00123   MockDisplay d;
00124   d.setName( "Steven" );
00125   d.subProp( "Count" )->setValue( 37 );
00126 
00127   rviz::YamlConfigWriter writer;
00128   rviz::Config config;
00129   d.save( config );
00130   QString out = writer.writeString(config);
00131 
00132   // Since we instantiated the display directly instead of using the
00133   // DisplayFactory, it won't know its class name.
00134   EXPECT_EQ( std::string(
00135                "Class: \"\"\n"
00136                "Name: Steven\n"
00137                "Enabled: false\n"
00138                "Count: 37\n"
00139                "Style: chunky\n"
00140                "Pi: 3.14159\n"
00141                "Offset: {X: 1, Y: 2, Z: 3}\n"
00142                "Color: 10; 20; 30"
00143                )
00144              , out.toStdString().c_str() );
00145 }
00146 
00147 TEST( DisplayGroup, save_properties)
00148 {
00149   DisplayGroup g;
00150   g.setName( "Charles" );
00151 
00152   MockDisplay *d = new MockDisplay;
00153   d->setName( "Steven" );
00154   d->subProp( "Count" )->setValue( 101 );
00155   g.addChild( d );
00156 
00157   d = new MockDisplay;
00158   d->setName( "Katherine" );
00159   d->subProp( "Pi" )->setValue( 1.1 );
00160   g.addChild( d );
00161 
00162   rviz::YamlConfigWriter writer;
00163   rviz::Config config;
00164   g.save( config );
00165   QString out = writer.writeString(config);
00166 
00167   // Since we instantiated the display directly instead of using the
00168   // DisplayFactory, it won't know its class name.
00169   EXPECT_EQ( std::string(
00170                "Class: \"\"\n"
00171                "Name: Charles\n"
00172                "Enabled: false\n"
00173                "Displays:\n"
00174                "  - Class: \"\"\n"
00175                "    Name: Steven\n"
00176                "    Enabled: false\n"
00177                "    Count: 101\n"
00178                "    Style: chunky\n"
00179                "    Pi: 3.14159\n"
00180                "    Offset: {X: 1, Y: 2, Z: 3}\n"
00181                "    Color: 10; 20; 30\n"
00182                "  - Class: \"\"\n"
00183                "    Name: Katherine\n"
00184                "    Enabled: false\n"
00185                "    Count: 10\n"
00186                "    Style: chunky\n"
00187                "    Pi: 1.1\n"
00188                "    Offset: {X: 1, Y: 2, Z: 3}\n"
00189                "    Color: 10; 20; 30"
00190                )
00191              , out.toStdString().c_str() );
00192 }
00193 
00194 TEST( DisplayFactory, class_name )
00195 {
00196   std::stringstream input(
00197     "Displays:\n"
00198     " -\n"
00199     "   Class: MockDisplay\n"
00200     );
00201 
00202   rviz::YamlConfigReader reader;
00203   rviz::Config config;
00204   reader.readStream(config, input);
00205 
00206   DisplayGroup g;
00207   g.load( config );
00208 
00209   EXPECT_EQ( 1, g.numChildren() );
00210   EXPECT_EQ( "MockDisplay", g.getDisplayAt( 0 )->getClassId().toStdString() );
00211 }
00212 
00213 TEST( DisplayFactory, failed_display )
00214 {
00215   std::stringstream input(
00216     "Displays:\n"
00217     "  - Class: MissingDisplay\n"
00218     "    Name: Chubbers\n"
00219     "    NumLemurs: 77\n"
00220     "    LemurStyle: chunky\n"
00221     "    SubYaml:\n"
00222     "      - 1\n"
00223     "      - foo: bar\n"
00224     "        food: bard\n"
00225     );
00226 
00227   rviz::YamlConfigReader reader;
00228   rviz::Config config;
00229   reader.readStream(config, input);
00230 
00231   DisplayGroup g;
00232   g.load( config );
00233 
00234   EXPECT_EQ( 1, g.numChildren() );
00235   EXPECT_EQ( "MissingDisplay", g.getDisplayAt( 0 )->getClassId().toStdString() );
00236   EXPECT_EQ( 0, g.getDisplayAt( 0 )->numChildren() ); // FailedDisplay does not have any children.
00237 
00238   // When a FailedDisplay is saved, it should write out its contents
00239   // that it was loaded with, so data is not lost.
00240   rviz::YamlConfigWriter writer;
00241   rviz::Config config2;
00242   g.save( config2 );
00243   QString out = writer.writeString(config);
00244   EXPECT_EQ( std::string(
00245                "Class: \"\"\n"
00246                "Name: \"\"\n"
00247                "Enabled: false\n"
00248                "Displays:\n"
00249                "  - Class: MissingDisplay\n"
00250                "    LemurStyle: chunky\n"
00251                "    Name: Chubbers\n"
00252                "    NumLemurs: 77\n"
00253                "    SubYaml:\n"
00254                "      - 1\n"
00255                "      - foo: bar\n"
00256                "        food: bard"
00257                )
00258              , out.toStdString().c_str() );
00259 }
00260 
00261 int main( int argc, char **argv ) {
00262   ros::init( argc, argv, "display_test", ros::init_options::AnonymousName );
00263   QApplication app(argc, argv);
00264   testing::InitGoogleTest( &argc, argv );
00265   return RUN_ALL_TESTS();
00266 }


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Jun 6 2019 18:02:15