rgbd
connection_ros_gtest.cpp
Go to the documentation of this file.
1 #include "test_utils.h"
2 
3 #include <gtest/gtest.h>
4 
5 #include <rgbd/image.h>
6 #include <rgbd/server_ros.h>
7 #include <rgbd/client_ros.h>
8 
9 #include <ros/init.h>
10 #include <ros/console.h>
11 
12 
13 class ROS : public testing::Test
14 {
15 protected:
16  ROS(const std::string& _ns="") : ns(_ns), server(nh)
17  {
18  }
19 
20  virtual ~ROS()
21  {
23  nh.shutdown();
24  }
25 
26  void SetUp() override
27  {
29  server.initialize(ns, true, true, false);
30  }
31 
32  const std::string ns;
33 
34  ros::NodeHandle nh;
38 };
39 
40 class ROSInitialized : public ROS
41 {
42 protected:
43  ROSInitialized(const std::string& _ns="") : ROS(_ns)
44  {
45  }
46 
47  void SetUp() override
48  {
49  ROS::SetUp();
50  client.initialize(ros::names::append(ns, "rgb/image"), ros::names::append(ns, "depth/image"), ros::names::append(ns, "rgb/camera_info"));
51  ros::Time end = ros::Time::now() + ros::Duration(5);
52  bool received = false;
53  while (!received || ros::Time::now() <= end)
54  {
55  server.send(image);
56  ros::Duration(0.1).sleep();
57  received = static_cast<bool>(client.nextImage());
58  if (received)
59  {
60  ros::Duration(2).sleep();
61  EXPECT_FALSE(client.nextImage()) << "SetUp failed, client should not have gotten a new image after getting one image correctly";
62  }
63  }
64  EXPECT_TRUE(received) << "SetUp failed, because client wasn't able to get one image correctly.";
65  }
66 };
67 
68 class ROS_NS : public ROSInitialized
69 {
70 protected:
71  ROS_NS() : ROSInitialized("test_ns")
72  {
73  }
74 };
75 
76 TEST_F(ROS, Initialize)
77 {
78  EXPECT_FALSE(ros::isShuttingDown());
79  EXPECT_FALSE(client.initialized());
80  EXPECT_TRUE(client.initialize("rgb/image", "depth/image", "rgb/camera_info"));
81  EXPECT_TRUE(client.initialized());
82  EXPECT_FALSE(ros::isShuttingDown());
83 }
84 
85 TEST_F(ROS, NextImageBeforeSend)
86 {
87  EXPECT_FALSE(ros::isShuttingDown());
88  EXPECT_TRUE(client.initialize("rgb/image", "depth/image", "rgb/camera_info"));
89  EXPECT_TRUE(client.initialized());
90  rgbd::Image image2;
91  EXPECT_FALSE(client.nextImage(image2));
92  EXPECT_FALSE(ros::isShuttingDown());
93 }
94 
95 TEST_F(ROS, NextImagePtrBeforeSend)
96 {
97  EXPECT_FALSE(ros::isShuttingDown());
98  EXPECT_TRUE(client.initialize("rgb/image", "depth/image", "rgb/camera_info"));
99  EXPECT_TRUE(client.initialized());
100  rgbd::ImagePtr image2 = client.nextImage();
101  EXPECT_FALSE(image2);
102  EXPECT_FALSE(ros::isShuttingDown());
103 }
104 
105 TEST_F(ROSInitialized, NextImagePtr)
106 {
107  EXPECT_FALSE(ros::isShuttingDown());
108  rgbd::ImagePtr image2;
109  server.send(image);
110  ros::Duration(0.01).sleep();
111  image2 = client.nextImage();
112  EXPECT_TRUE(image2);
113  if (image2) // This prevents a crash of the node. Test will still fail because of previous line
114  {
115  EXPECT_EQ(image, *image2);
116  }
117  EXPECT_FALSE(ros::isShuttingDown());
118 }
119 
120 TEST_F(ROSInitialized, NextImageTwice)
121 {
122  EXPECT_FALSE(ros::isShuttingDown());
123  rgbd::Image image2;
124  server.send(image);
125  ros::Duration(0.01).sleep();
126  EXPECT_TRUE(client.nextImage(image2));
127  EXPECT_EQ(image, image2);
128  EXPECT_FALSE(ros::isShuttingDown());
129  image.setTimestamp(image.getTimestamp()+10.);
130  server.send(image);
131  ros::Duration(0.01).sleep();
132  EXPECT_TRUE(client.nextImage(image2));
133  EXPECT_EQ(image, image2);
134  EXPECT_FALSE(ros::isShuttingDown());
135 }
136 
137 TEST_F(ROSInitialized, NextImageTwiceWithoutSend)
138 {
139  EXPECT_FALSE(ros::isShuttingDown());
140  rgbd::Image image2;
141  server.send(image);
142  ros::Duration(0.01).sleep();
143  EXPECT_TRUE(client.nextImage(image2));
144  EXPECT_EQ(image, image2);
145  EXPECT_FALSE(ros::isShuttingDown());
146  EXPECT_FALSE(client.nextImage(image2));
147  EXPECT_FALSE(ros::isShuttingDown());
148 }
149 
150 TEST_F(ROSInitialized, NextImagePtrTwice)
151 {
152  EXPECT_FALSE(ros::isShuttingDown());
153  server.send(image);
154  rgbd::ImagePtr image2;
155  server.send(image);
156  ros::Duration(0.01).sleep();
157  image2 = client.nextImage();
158  EXPECT_TRUE(image2);
159  if (image2) // This prevents a crash of the node. Test will still fail because of previous line
160  {
161  EXPECT_EQ(image, *image2);
162  }
163  EXPECT_FALSE(ros::isShuttingDown());
164  image.setTimestamp(image.getTimestamp()+10.);
165  image2.reset();
166  server.send(image);
167  ros::Duration(0.01).sleep();
168  image2 = client.nextImage();
169  EXPECT_TRUE(image2);
170  if (image2) // This prevents a crash of the node. Test will still fail because of previous line
171  {
172  EXPECT_EQ(image, *image2);
173  }
174  EXPECT_FALSE(ros::isShuttingDown());
175 }
176 
177 TEST_F(ROSInitialized, NextImagePtrTwiceWithoutSend)
178 {
179  EXPECT_FALSE(ros::isShuttingDown());
180  server.send(image);
181  rgbd::ImagePtr image2;
182  server.send(image);
183  ros::Duration(0.01).sleep();
184  image2 = client.nextImage();
185  EXPECT_TRUE(image2);
186  if (image2) // This prevents a crash of the node. Test will still fail because of previous line
187  {
188  EXPECT_EQ(image, *image2);
189  }
190  EXPECT_FALSE(ros::isShuttingDown());
191  image2.reset();
192  image2 = client.nextImage();
193  EXPECT_FALSE(image2);
194  EXPECT_FALSE(ros::isShuttingDown());
195 }
196 
197 TEST_F(ROS_NS, NextImage)
198 {
199  EXPECT_FALSE(ros::isShuttingDown());
200  rgbd::Image image2;
201  server.send(image);
202  ros::Duration(0.01).sleep();
203  EXPECT_TRUE(client.nextImage(image2));
204  EXPECT_EQ(image, image2);
205  EXPECT_FALSE(ros::isShuttingDown());
206 }
207 
208 // Run all the tests that were declared with TEST()
209 int main(int argc, char **argv)
210 {
211  ros::init(argc, argv, "ros_connection");
212  testing::InitGoogleTest(&argc, argv);
213  return RUN_ALL_TESTS();
214 }
ROS::SetUp
void SetUp() override
Definition: connection_ros_gtest.cpp:26
std::string
std::shared_ptr
test_utils.h
rgbd::ClientROS::nextImage
bool nextImage(Image &image)
Get a new Image. If no new image has been received since the last call, no image will be written and ...
Definition: client_ros.cpp:43
ROS::server
rgbd::ServerROS server
Definition: connection_ros_gtest.cpp:36
std::shared_ptr::reset
T reset(T... args)
rgbd::ClientROSBase::deinitialize
bool deinitialize()
Clears the subscribers. initialized will now return false.
Definition: client_ros_base.cpp:44
ROSInitialized::ROSInitialized
ROSInitialized(const std::string &_ns="")
Definition: connection_ros_gtest.cpp:43
rgbd::Image
Definition: image.h:43
main
int main(int argc, char **argv)
Definition: connection_ros_gtest.cpp:209
rgbd::ServerROS
Server which publishes ROS rgb image, depth image and pointcloud messages.
Definition: server_ros.h:16
ROSInitialized
Definition: connection_ros_gtest.cpp:40
rgbd::ServerROS::send
void send(const Image &image)
Publish a new image to the selected ROS topics.
Definition: server_ros.cpp:75
server_ros.h
TEST_F
TEST_F(ROS, Initialize)
Definition: connection_ros_gtest.cpp:76
image.h
ROS::image
rgbd::Image image
Definition: connection_ros_gtest.cpp:35
ROS
Definition: connection_ros_gtest.cpp:13
ROS_NS
Definition: connection_ros_gtest.cpp:68
rgbd::ServerROS::initialize
void initialize(std::string ns="", const bool publish_rgb=false, const bool publish_depth=false, const bool publish_pc=false)
initialize server
Definition: server_ros.cpp:31
ROS::ROS
ROS(const std::string &_ns="")
Definition: connection_ros_gtest.cpp:16
ROSInitialized::SetUp
void SetUp() override
Definition: connection_ros_gtest.cpp:47
ROS::ns
const std::string ns
Definition: connection_ros_gtest.cpp:32
client_ros.h
ROS_NS::ROS_NS
ROS_NS()
Definition: connection_ros_gtest.cpp:71
rgbd::ClientROS
Client which subscribes to regular ROS image topics.
Definition: client_ros.h:31
ROS::client
rgbd::ClientROS client
Definition: connection_ros_gtest.cpp:37
ROS::nh
ros::NodeHandle nh
Definition: connection_ros_gtest.cpp:34
rgbd::ClientROS::initialize
bool initialize(const std::string &rgb_image_topic, const std::string &depth_image_topic, const std::string &cam_info_topic)
Initialize the subscriber.
Definition: client_ros.cpp:29
rgbd::generateRandomImage
rgbd::Image generateRandomImage()
Definition: test_utils.h:36
ROS::~ROS
virtual ~ROS()
Definition: connection_ros_gtest.cpp:20