$search
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2009, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 ********************************************************************/ 00034 00035 // Bring in my package's API, which is what I'm testing 00036 #include "ros/ros.h" 00037 #include "topic_tools/shape_shifter.h" 00038 #include "std_msgs/String.h" 00039 #include "std_msgs/Int32.h" 00040 00041 // Bring in gtest 00042 #include <gtest/gtest.h> 00043 00044 class ShapeShifterSubscriber : public testing::Test 00045 { 00046 public: 00047 00048 bool success; 00049 00050 void messageCallbackInt(const topic_tools::ShapeShifter::ConstPtr& msg) 00051 { 00052 try { 00053 std_msgs::Int32::Ptr s = msg->instantiate<std_msgs::Int32>(); 00054 } catch (topic_tools::ShapeShifterException& e) 00055 { 00056 success = true; 00057 } 00058 } 00059 00060 void messageCallbackString(const topic_tools::ShapeShifter::ConstPtr& msg) 00061 { 00062 try { 00063 std_msgs::String::Ptr s = msg->instantiate<std_msgs::String>(); 00064 if (s->data == "chatter") 00065 success = true; 00066 } catch (topic_tools::ShapeShifterException& e) 00067 { 00068 00069 } 00070 } 00071 00072 void messageCallbackLoopback(const topic_tools::ShapeShifter::ConstPtr& msg) 00073 { 00074 try { 00075 std_msgs::String::Ptr s = msg->instantiate<std_msgs::String>(); 00076 printf("Got data: %s", s->data.c_str()); 00077 if (s->data == "abc123") 00078 success = true; 00079 } catch (topic_tools::ShapeShifterException& e) 00080 { 00081 printf("Instantiate failed!\n"); 00082 } 00083 } 00084 00085 protected: 00086 ShapeShifterSubscriber() {} 00087 00088 void SetUp() 00089 { 00090 success = false; 00091 } 00092 00093 void TearDown() {} 00094 }; 00095 00096 00097 TEST_F(ShapeShifterSubscriber, testInstantiateString) 00098 { 00099 ros::NodeHandle nh; 00100 ros::Subscriber sub = nh.subscribe<topic_tools::ShapeShifter>("input",1,&ShapeShifterSubscriber::messageCallbackString, (ShapeShifterSubscriber*)this); 00101 00102 ros::Time t1(ros::Time::now()+ros::Duration(10.0)); 00103 00104 while(ros::Time::now() < t1 && !success) 00105 { 00106 ros::WallDuration(0.01).sleep(); 00107 ros::spinOnce(); 00108 } 00109 00110 EXPECT_FALSE(topic_tools::ShapeShifter::uses_old_API_); 00111 00112 if(success) 00113 SUCCEED(); 00114 else 00115 FAIL(); 00116 } 00117 00118 TEST_F(ShapeShifterSubscriber, testInstantiateInt) 00119 { 00120 ros::NodeHandle nh; 00121 ros::Subscriber sub = nh.subscribe<topic_tools::ShapeShifter>("input",1,&ShapeShifterSubscriber::messageCallbackInt, (ShapeShifterSubscriber*)this); 00122 00123 ros::Time t1(ros::Time::now()+ros::Duration(10.0)); 00124 00125 while(ros::Time::now() < t1 && !success) 00126 { 00127 ros::WallDuration(0.01).sleep(); 00128 ros::spinOnce(); 00129 } 00130 00131 EXPECT_FALSE(topic_tools::ShapeShifter::uses_old_API_); 00132 00133 if(success) 00134 SUCCEED(); 00135 else 00136 FAIL(); 00137 } 00138 00139 TEST_F(ShapeShifterSubscriber, testLoopback) 00140 { 00141 ros::NodeHandle nh; 00142 ros::Subscriber sub = nh.subscribe<topic_tools::ShapeShifter>("loopback",1,&ShapeShifterSubscriber::messageCallbackLoopback, (ShapeShifterSubscriber*)this); 00143 00144 ros::Time t1(ros::Time::now()+ros::Duration(10.0)); 00145 00146 ros::Publisher pub = nh.advertise<std_msgs::String>("loopback", 1); 00147 std_msgs::String s; 00148 s.data = "abc123"; 00149 pub.publish(s); 00150 00151 while(ros::Time::now() < t1 && !success) 00152 { 00153 ros::WallDuration(0.01).sleep(); 00154 ros::spinOnce(); 00155 } 00156 00157 EXPECT_FALSE(topic_tools::ShapeShifter::uses_old_API_); 00158 00159 if(success) 00160 SUCCEED(); 00161 else 00162 FAIL(); 00163 } 00164 00165 int main(int argc, char **argv){ 00166 ros::init(argc, argv, "test_shapeshifter"); 00167 00168 testing::InitGoogleTest(&argc, argv); 00169 return RUN_ALL_TESTS(); 00170 }