00001
00006 #include <rtt/os/startstop.h>
00007 #include <rtt/Logger.hpp>
00008 #include <rtt/deployment/ComponentLoader.hpp>
00009
00010 #include <rtt_roscomm/rtt_rostopic.h>
00011 #include <rtt_roscomm/rosservice.h>
00012 #include <std_msgs/typekit/String.h>
00013 #include <std_srvs/Empty.h>
00014
00015 #include <ros/names.h>
00016 #include <ros/service_manager.h>
00017 #include <ros/this_node.h>
00018
00019 #include <boost/weak_ptr.hpp>
00020
00021 #include <gtest/gtest.h>
00022
00023 #include <algorithm>
00024
00025 TEST(TransportTest, OutOfBandTest)
00026 {
00027 ros::V_string advertised_topics, subscribed_topics;
00028
00029
00030 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_ros", "" ));
00031 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_rosnode", "" ));
00032 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_roscomm", "" ));
00033 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_std_msgs", "" ));
00034
00035 RTT::OutputPort<std_msgs::String> out("out");
00036 RTT::InputPort<std::string> in("in");
00037
00038
00039
00040
00041 std::string topic = ros::names::resolve("~talker");
00042
00043 EXPECT_TRUE(out.createStream(rtt_roscomm::topicLatched(topic)));
00044 EXPECT_TRUE(in.createStream(rtt_roscomm::topic(topic)));
00045
00046
00047 ros::this_node::getAdvertisedTopics(advertised_topics);
00048 EXPECT_TRUE(std::find(advertised_topics.begin(), advertised_topics.end(),
00049 topic) != advertised_topics.end());
00050 ros::this_node::getSubscribedTopics(subscribed_topics);
00051 EXPECT_TRUE(std::find(subscribed_topics.begin(), subscribed_topics.end(),
00052 topic) != subscribed_topics.end());
00053
00054
00055 std_msgs::String sample;
00056 sample.data = "Hello world!";
00057 out.write(sample);
00058 usleep(1000000);
00059
00060
00061 std::string received;
00062 EXPECT_EQ(RTT::NewData, in.read(received) );
00063 EXPECT_EQ(sample.data, received);
00064
00065
00066 out.disconnect();
00067 EXPECT_FALSE(out.connected());
00068 in.disconnect();
00069 EXPECT_FALSE(in.connected());
00070
00071
00072 advertised_topics.clear();
00073 subscribed_topics.clear();
00074 ros::this_node::getAdvertisedTopics(advertised_topics);
00075 EXPECT_FALSE(std::find(advertised_topics.begin(), advertised_topics.end(),
00076 topic) != advertised_topics.end());
00077 ros::this_node::getSubscribedTopics(subscribed_topics);
00078 EXPECT_FALSE(std::find(subscribed_topics.begin(), subscribed_topics.end(),
00079 topic) != subscribed_topics.end());
00080 }
00081
00082 TEST(TransportTest, VectorTest)
00083 {
00084
00085 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_ros", "" ));
00086 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_rosnode", "" ));
00087 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_roscomm", "" ));
00088 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_std_msgs", "" ));
00089
00090 RTT::OutputPort<std::vector<double> > out("out");
00091 RTT::InputPort<std::vector<double> > in("in");
00092
00093
00094 std::string topic = ros::names::resolve("~array_talker");
00095 EXPECT_TRUE(out.connectTo(&in, rtt_roscomm::topicLatched(topic)));
00096
00097
00098
00099
00100 static const double sample_array[] = { 1., 2., 3., 4., 5. };
00101 std::vector<double> sample(sample_array, sample_array + sizeof(sample_array)/sizeof(sample_array[0]));
00102 out.write(sample);
00103 usleep(1000000);
00104
00105
00106 std::vector<double> received;
00107 EXPECT_EQ(RTT::NewData, in.read(received) );
00108 EXPECT_EQ(sample.size(), received.size());
00109 if (sample.size() == received.size()) {
00110 EXPECT_TRUE(std::equal(received.begin(), received.end(), sample.begin()));
00111 }
00112
00113
00114 out.disconnect();
00115 in.disconnect();
00116 EXPECT_FALSE(out.connected());
00117 EXPECT_FALSE(in.connected());
00118 }
00119
00120 static int callback_called = 0;
00121 bool callback(std_srvs::Empty::Request&, std_srvs::Empty::Response&)
00122 {
00123 ++callback_called;
00124 return true;
00125 }
00126
00127 TEST(TransportTest, ServiceServerTest)
00128 {
00129 std::string service = ros::names::resolve("~empty");
00130
00131
00132 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_rosnode", "" ));
00133 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_roscomm", "" ));
00134 ASSERT_TRUE(RTT::ComponentLoader::Instance()->import("rtt_std_srvs", "" ));
00135
00136
00137 RTT::TaskContext *tc = new RTT::TaskContext("TaskContext");
00138 tc->addOperation("empty", &callback);
00139
00140
00141 boost::weak_ptr<rtt_rosservice::ROSService> rosservice;
00142 rosservice = tc->getProvider<rtt_rosservice::ROSService>("rosservice");
00143 ASSERT_FALSE(rosservice.expired());
00144
00145
00146 EXPECT_TRUE(rosservice.lock()->connect("empty", service, "std_srvs/Empty"));
00147
00148
00149 EXPECT_TRUE(ros::ServiceManager::instance()->lookupServicePublication(service).get());
00150
00151
00152 RTT::OperationCaller<bool(std_srvs::Empty::Request&, std_srvs::Empty::Response&)> service_caller("empty");
00153 tc->requires()->addOperationCaller(service_caller);
00154 EXPECT_TRUE(rosservice.lock()->connect("empty", service, "std_srvs/Empty"));
00155 EXPECT_TRUE(service_caller.ready());
00156
00157
00158 EXPECT_EQ(0, callback_called);
00159 std_srvs::Empty empty;
00160 EXPECT_TRUE(service_caller(empty.request, empty.response));
00161 EXPECT_EQ(1, callback_called);
00162 }
00163
00164 int main(int argc, char** argv) {
00165 testing::InitGoogleTest(&argc, argv);
00166
00167
00168 __os_init(argc, argv);
00169
00170 RTT::Logger::log().setStdStream(std::cerr);
00171 RTT::Logger::log().mayLogStdOut(true);
00172 RTT::Logger::log().setLogLevel(RTT::Logger::Debug);
00173
00174 return RUN_ALL_TESTS();
00175 }
00176