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 <ros/ros.h>
00033 
00034 #include <yaml-cpp/yaml.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 
00042 #include "mock_display.h"
00043 #include "mock_context.h"
00044 
00045 using namespace rviz;
00046 
00047 TEST( Display, load_properties )
00048 {
00049   std::stringstream input( "Name: sample\n"
00050                            "Enabled: true\n"
00051                            "Count: 7\n"
00052                            "Pi: 3.2\n"
00053                            "Offset: {X: -1, Y: 1.1, Z: 1.1e3}\n"
00054                            "Color: white\n"
00055                            "Style: loosey goosey\n" );
00056 
00057   YAML::Parser parser( input );
00058   YAML::Node node;
00059   parser.GetNextDocument( node );
00060 
00061   MockDisplay d;
00062   d.load( node );
00063 
00064   EXPECT_EQ( 7, d.count_->getValue().toInt() );
00065   EXPECT_EQ( "loosey goosey", d.style_->getValue().toString().toStdString() );
00066   EXPECT_EQ( 3.2f, d.pi_->getValue().toFloat() );
00067   Ogre::Vector3 offset = d.offset_->getVector();
00068   EXPECT_EQ( -1.f, offset.x );
00069   EXPECT_EQ( 1.1f, offset.y );
00070   EXPECT_EQ( 1100.f, offset.z );
00071   EXPECT_EQ( "255; 255; 255", d.color_->getValue().toString().toStdString() );
00072   EXPECT_EQ( true, d.getValue().toBool() );
00073 }
00074 
00075 TEST( DisplayGroup, load_properties )
00076 {
00077   std::stringstream input(
00078     "Name: root\n"
00079     "Enabled: true\n"
00080     "Displays:\n"
00081     " -\n"
00082     "   Class: MockDisplay\n"
00083     "   Name: Steven\n"
00084     "   Enabled: false\n"
00085     "   Count: 17\n"
00086     " -\n"
00087     "   Name: sub group\n"
00088     "   Class: DisplayGroup\n"
00089     "   Enabled: true\n"
00090     "   Displays:\n"
00091     "    -\n"
00092     "      Class: MockDisplay\n"
00093     "      Name: Curly\n"
00094     "      Enabled: false\n"
00095     "      Count: 900\n"
00096     " -\n"
00097     "   Class: BrokenDisplay\n"
00098     "   Name: Joe\n"
00099     "   Enabled: true\n"
00100     "   Count: 33\n"
00101     );
00102 
00103   YAML::Parser parser( input );
00104   YAML::Node node;
00105   parser.GetNextDocument( node );
00106 
00107   MockContext context;
00108 
00109   DisplayGroup g;
00110   g.initialize( &context );
00111   g.load( node );
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   YAML::Emitter out;
00128   d.save( out );
00129 
00130   // Since we instantiated the display directly instead of using the
00131   // DisplayFactory, it won't know its class name.
00132   EXPECT_EQ( std::string( 
00133                "Class: \"\"\n"
00134                "Name: Steven\n"
00135                "Enabled: false\n"
00136                "Count: 37\n"
00137                "Style: chunky\n"
00138                "Pi: 3.14159\n"
00139                "Offset: {X: 1, Y: 2, Z: 3}\n"
00140                "Color: 10; 20; 30"
00141                )
00142              , out.c_str() );
00143 }
00144 
00145 TEST( DisplayGroup, save_properties)
00146 {
00147   DisplayGroup g;
00148   g.setName( "Charles" );
00149 
00150   MockDisplay *d = new MockDisplay;
00151   d->setName( "Steven" );
00152   d->subProp( "Count" )->setValue( 101 );
00153   g.addChild( d );
00154 
00155   d = new MockDisplay;
00156   d->setName( "Katherine" );
00157   d->subProp( "Pi" )->setValue( 1.1 );
00158   g.addChild( d );
00159 
00160   YAML::Emitter out;
00161   g.save( out );
00162 
00163   // Since we instantiated the display directly instead of using the
00164   // DisplayFactory, it won't know its class name.
00165   EXPECT_EQ( std::string( 
00166                "Class: \"\"\n"
00167                "Name: Charles\n"
00168                "Enabled: false\n"
00169                "Displays:\n"
00170                "  - Class: \"\"\n"
00171                "    Name: Steven\n"
00172                "    Enabled: false\n"
00173                "    Count: 101\n"
00174                "    Style: chunky\n"
00175                "    Pi: 3.14159\n"
00176                "    Offset: {X: 1, Y: 2, Z: 3}\n"
00177                "    Color: 10; 20; 30\n"
00178                "  - Class: \"\"\n"
00179                "    Name: Katherine\n"
00180                "    Enabled: false\n"
00181                "    Count: 10\n"
00182                "    Style: chunky\n"
00183                "    Pi: 1.1\n"
00184                "    Offset: {X: 1, Y: 2, Z: 3}\n"
00185                "    Color: 10; 20; 30"
00186                )
00187              , out.c_str() );
00188 }
00189 
00190 TEST( DisplayFactory, class_name )
00191 {
00192   std::stringstream input(
00193     "Displays:\n"
00194     " -\n"
00195     "   Class: MockDisplay\n"
00196     );
00197 
00198   YAML::Parser parser( input );
00199   YAML::Node node;
00200   parser.GetNextDocument( node );
00201 
00202   MockContext context;
00203 
00204   DisplayGroup g;
00205   g.initialize( &context );
00206   g.load( node );
00207 
00208   EXPECT_EQ( 1, g.numChildren() );
00209   EXPECT_EQ( "MockDisplay", g.getDisplayAt( 0 )->getClassId().toStdString() );
00210 }
00211 
00212 TEST( DisplayFactory, failed_display )
00213 {
00214   std::stringstream input(
00215     "Displays:\n"
00216     "  - Class: MissingDisplay\n"
00217     "    Name: Chubbers\n"
00218     "    NumLemurs: 77\n"
00219     "    LemurStyle: chunky\n"
00220     "    SubYaml:\n"
00221     "      - 1\n"
00222     "      - foo: bar\n"
00223     "        food: bard\n"
00224     );
00225 
00226   YAML::Parser parser( input );
00227   YAML::Node node;
00228   parser.GetNextDocument( node );
00229 
00230   MockContext context;
00231 
00232   DisplayGroup g;
00233   g.initialize( &context );
00234   g.load( node );
00235 
00236   EXPECT_EQ( 1, g.numChildren() );
00237   EXPECT_EQ( "MissingDisplay", g.getDisplayAt( 0 )->getClassId().toStdString() );
00238   EXPECT_EQ( 0, g.getDisplayAt( 0 )->numChildren() ); // FailedDisplay does not have any children.
00239 
00240   // When a FailedDisplay is saved, it should write out its contents
00241   // that it was loaded with, so data is not lost.
00242   YAML::Emitter out;
00243   g.save( out );
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.c_str() );
00259 }
00260 
00261 int main( int argc, char **argv ) {
00262   ros::init( argc, argv, "test", ros::init_options::AnonymousName );
00263   testing::InitGoogleTest( &argc, argv );
00264   return RUN_ALL_TESTS();
00265 }


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:35