ros_subscriber.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #include <gtest/gtest.h>
19 #include <ros/ros.h>
20 #include <sensor_msgs/Image.h>
21 #include <chrono>
22 #include "../test_utils.h"
23 
26 template <typename ROS_T>
27 class MyTestClass
28 {
30  public: MyTestClass(const std::string &_topic)
31  {
32  this->sub = this->n.subscribe(_topic, 1000, &MyTestClass::Cb, this);
33  }
34 
36  public: void Cb(const ROS_T& _msg)
37  {
39  this->callbackExecuted = true;
40  };
41 
43  public: bool callbackExecuted = false;
44 
46  private: ros::NodeHandle n;
47 
49  private: ros::Subscriber sub;
50 };
51 
53 TEST(ROSSubscriberTest, Image)
54 {
55  MyTestClass<sensor_msgs::Image> client("image");
56 
57  using namespace std::chrono_literals;
59  client.callbackExecuted, 10ms, 200);
60 
61  EXPECT_TRUE(client.callbackExecuted);
62 }
63 
65 int main(int argc, char **argv)
66 {
67  ::testing::InitGoogleTest(&argc, argv);
68  ros::init(argc, argv, "ros_image_subscriber");
69 
70  return RUN_ALL_TESTS();
71 }
MyTestClass::sub
ros::Subscriber sub
ROS subscriber;.
Definition: ros_subscriber.cpp:49
MyTestClass::MyTestClass
MyTestClass(const std::string &_topic)
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
ros.h
MyTestClass::n
ros::NodeHandle n
ROS node handle;.
Definition: ros_subscriber.cpp:46
ros_ign_image::testing::compareTestMsg
void compareTestMsg(const std_msgs::Header &_msg)
Compare a message with the populated for testing.
Definition: test_utils.h:97
ros_ign_image::testing::waitUntilBoolVarAndSpin
void waitUntilBoolVarAndSpin(bool &_boolVar, const std::chrono::duration< Rep, Period > &_sleepEach, const int _retries)
Wait until a boolean variable is set to true for a given number of times. This function calls ros::sp...
Definition: test_utils.h:67
main
int main(int argc, char **argv)
Definition: ros_subscriber.cpp:65
MyTestClass::callbackExecuted
bool callbackExecuted
Member variables that flag when the actions are executed.
Definition: ros_subscriber.cpp:43
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
MyTestClass::Cb
void Cb(const IGN_T &_msg)
TEST
TEST(ROSSubscriberTest, Image)
Definition: ros_subscriber.cpp:53
MyTestClass
A class for testing ROS topic subscription.
Definition: ros_subscriber.cpp:27
ros::NodeHandle
ros::Subscriber


ros_ign_image
Author(s):
autogenerated on Sat Mar 11 2023 04:02:19