test_nodehandles_different_namespaces.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * test_nodehandles_different_namespaces.cpp
3 *
4 * Software License Agreement (BSD License)
5 *
6 * Copyright (c) 2016, University of Patras
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 * * Neither the name of University of Patras nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 * Authors: Aris Synodinos
37 *********************************************************************/
38 
39 #include <gtest/gtest.h>
40 #include <std_msgs/Bool.h>
41 #include <std_msgs/Byte.h>
42 #include <std_msgs/Empty.h>
43 #include <std_msgs/String.h>
44 #include <std_msgs/Float32.h>
45 #include <std_msgs/Float64.h>
46 #include <ros/ros.h>
47 
48 TEST(DifferentNamespaces, NodeletPrivateNodehandle) {
49  ros::NodeHandle nh;
50  ros::Duration(2).sleep();
51  ros::master::V_TopicInfo master_topics;
52  ros::master::getTopics(master_topics);
53  bool found_topic = false;
54 
55  for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
56  const ros::master::TopicInfo& info = *it;
57  if (info.datatype.compare("std_msgs/Bool") == 0) {
58  found_topic = true;
59  EXPECT_STREQ("/nodelet_namespace/nodehandle_test/private", info.name.c_str());
60  }
61  }
62  EXPECT_TRUE(found_topic);
63 }
64 
65 TEST(DifferentNamespaces, NodeletNamespacedNodehandle) {
66  ros::NodeHandle nh;
67  ros::Duration(2).sleep();
68  ros::master::V_TopicInfo master_topics;
69  ros::master::getTopics(master_topics);
70  bool found_topic = false;
71 
72  for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
73  const ros::master::TopicInfo& info = *it;
74  if (info.datatype.compare("std_msgs/Byte") == 0) {
75  found_topic = true;
76  EXPECT_STREQ("/nodelet_namespace/namespaced", info.name.c_str());
77  }
78  }
79  EXPECT_TRUE(found_topic);
80 }
81 
82 TEST(DifferentNamespaces, NodeletGlobalNodehandle) {
83  ros::NodeHandle nh;
84  ros::Duration(2).sleep();
85  ros::master::V_TopicInfo master_topics;
86  ros::master::getTopics(master_topics);
87  bool found_topic = false;
88 
89  for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
90  const ros::master::TopicInfo& info = *it;
91  if (info.datatype.compare("std_msgs/Time") == 0) {
92  found_topic = true;
93  EXPECT_STREQ("/global", info.name.c_str());
94  }
95  }
96  EXPECT_TRUE(found_topic);
97 }
98 
99 TEST(DifferentNamespaces, NodePrivateNodehandle) {
100  ros::NodeHandle nh("~");
101  ros::Publisher pub = nh.advertise<std_msgs::Empty>("private", 10);
102  ros::master::V_TopicInfo master_topics;
103  ros::master::getTopics(master_topics);
104  bool found_topic = false;
105 
106  for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
107  const ros::master::TopicInfo& info = *it;
108  if (info.datatype.compare("std_msgs/Empty") == 0) {
109  found_topic = true;
110  EXPECT_STREQ("/test_namespace/test_nodehandles/private", info.name.c_str());
111  }
112  }
113  EXPECT_TRUE(found_topic);
114 }
115 
116 TEST(DifferentNamespaces, NodeNamespacedNodehandle) {
117  ros::NodeHandle nh;
118  ros::Publisher pub = nh.advertise<std_msgs::String>("namespaced", 10);
119  ros::master::V_TopicInfo master_topics;
120  ros::master::getTopics(master_topics);
121  bool found_topic = false;
122 
123  for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
124  const ros::master::TopicInfo& info = *it;
125  if (info.datatype.compare("std_msgs/String") == 0) {
126  found_topic = true;
127  EXPECT_STREQ("/test_namespace/namespaced", info.name.c_str());
128  }
129  }
130  EXPECT_TRUE(found_topic);
131 }
132 
133 TEST(DifferentNamespaces, NodeGlobalNodehandle) {
134  ros::NodeHandle nh;
135  ros::Publisher pub = nh.advertise<std_msgs::Float32>("/public", 10);
136  ros::master::V_TopicInfo master_topics;
137  ros::master::getTopics(master_topics);
138  bool found_topic = false;
139 
140  for (ros::master::V_TopicInfo::iterator it = master_topics.begin() ; it != master_topics.end(); it++) {
141  const ros::master::TopicInfo& info = *it;
142  if (info.datatype.compare("std_msgs/Float32") == 0) {
143  found_topic = true;
144  EXPECT_STREQ("/public", info.name.c_str());
145  }
146  }
147  EXPECT_TRUE(found_topic);
148 }
149 
150 int main(int argc, char **argv) {
151  testing::InitGoogleTest(&argc, argv);
152  ros::init(argc, argv, "test_nodehandles_different_namespaces");
153  return RUN_ALL_TESTS();
154 }
bool sleep() const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL bool getTopics(V_TopicInfo &topics)
std::vector< TopicInfo > V_TopicInfo
TEST(DifferentNamespaces, NodeletPrivateNodehandle)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int main(int argc, char **argv)


test_nodelet
Author(s): Tully Foote
autogenerated on Sat Jul 18 2020 03:17:57