41#include <gmock/gmock.h>
42#include <gtest/gtest.h>
45#include <rclcpp/rclcpp.hpp>
46#include <tf2_ros/buffer.h>
64TEST(CurrentStateMonitorTests, StartCreateSubscriptionTest)
66 auto mock_middleware_handle = std::make_unique<MockMiddlewareHandle>();
68 EXPECT_CALL(*mock_middleware_handle, createJointStateSubscription);
73 std::make_shared<tf2_ros::Buffer>(std::make_shared<rclcpp::Clock>()),
false
80TEST(CurrentStateMonitorTests, StartActiveTest)
85 std::make_shared<tf2_ros::Buffer>(std::make_shared<rclcpp::Clock>()),
false
92 EXPECT_TRUE(current_state_monitor.isActive());
95TEST(CurrentStateMonitorTests, StopResetSubscriptionTest)
97 auto mock_middleware_handle = std::make_unique<MockMiddlewareHandle>();
100 EXPECT_CALL(*mock_middleware_handle, resetJointStateSubscription);
101 EXPECT_CALL(*mock_middleware_handle, now);
106 std::make_shared<tf2_ros::Buffer>(std::make_shared<rclcpp::Clock>()),
false
111 current_state_monitor.stopStateMonitor();
114TEST(CurrentStateMonitorTests, StopNotActiveTest)
116 auto mock_middleware_handle = std::make_unique<MockMiddlewareHandle>();
121 std::make_shared<tf2_ros::Buffer>(std::make_shared<rclcpp::Clock>()),
false
126 current_state_monitor.stopStateMonitor();
129 EXPECT_FALSE(current_state_monitor.isActive());
132TEST(CurrentStateMonitorTests, DestructStopTest)
134 auto mock_middleware_handle = std::make_unique<MockMiddlewareHandle>();
137 EXPECT_CALL(*mock_middleware_handle, resetJointStateSubscription);
138 EXPECT_CALL(*mock_middleware_handle, now);
144 std::make_shared<tf2_ros::Buffer>(std::make_shared<rclcpp::Clock>()),
false
147 EXPECT_TRUE(current_state_monitor.isActive());
152TEST(CurrentStateMonitorTests, NoModelTest)
155 moveit::core::RobotModelPtr robot_model =
nullptr;
160 std::make_unique<MockMiddlewareHandle>(), robot_model,
161 std::make_shared<tf2_ros::Buffer>(std::make_shared<rclcpp::Clock>()),
false),
162 std::invalid_argument);
165TEST(CurrentStateMonitorTests, HaveCompleteStateConstructFalse)
170 std::make_shared<tf2_ros::Buffer>(std::make_shared<rclcpp::Clock>()),
false
175 EXPECT_FALSE(current_state_monitor.haveCompleteState());
178TEST(CurrentStateMonitorTests, WaitForCompleteStateWaits)
180 auto mock_middleware_handle = std::make_unique<MockMiddlewareHandle>();
182 auto nanoseconds_slept = std::chrono::nanoseconds(0);
183 ON_CALL(*mock_middleware_handle, sleepFor)
184 .WillByDefault(testing::Invoke([&](
const std::chrono::nanoseconds& nanoseconds) {
185 nanoseconds_slept += nanoseconds;
192 std::make_shared<tf2_ros::Buffer>(std::make_shared<rclcpp::Clock>()),
false
199 EXPECT_NEAR(nanoseconds_slept.count(), 1e+9, 1e3);
204 ::testing::InitGoogleTest(&argc, argv);
205 return RUN_ALL_TESTS();
Dependency injection class for testing the CurrentStateMonitor.
virtual std::string getJointStateTopicName() const =0
Get the joint state topic name.
virtual void createStaticTfSubscription(TfCallback callback)=0
Creates a static transform message subscription.
virtual void resetTfSubscriptions()=0
Reset the static & dynamic transform subscriptions.
virtual void resetJointStateSubscription()=0
Reset the joint state subscription.
virtual bool sleepFor(const std::chrono::nanoseconds &nanoseconds) const =0
Block for the specified amount of time.
virtual void createJointStateSubscription(const std::string &topic, JointStateUpdateCallback callback)=0
Creates a joint state subscription.
std::function< void(const tf2_msgs::msg::TFMessage::ConstSharedPtr)> TfCallback
virtual std::string getDynamicTfTopicName() const =0
Get the dynamic transform topic name.
virtual rclcpp::Time now() const =0
Get the current time.
virtual void createDynamicTfSubscription(TfCallback callback)=0
Creates a dynamic transform message subscription.
virtual bool ok() const =0
Uses rclcpp::ok to check the context status.
virtual std::string getStaticTfTopicName() const =0
Get the static transform topic name.
Monitors the joint_states topic and tf to maintain the current state of the robot.
void startStateMonitor(const std::string &joint_states_topic="joint_states")
Start monitoring joint states on a particular topic.
bool waitForCompleteState(double wait_time_s) const
Wait for at most wait_time_s seconds until the complete robot state is known.
int main(int argc, char **argv)
TEST(CurrentStateMonitorTests, StartCreateSubscriptionTest)
moveit::core::RobotModelPtr loadTestingRobotModel(const std::string &package_name, const std::string &urdf_relative_path, const std::string &srdf_relative_path)
Loads a robot model given a URDF and SRDF file in a package.
std::function< void(sensor_msgs::msg::JointState::ConstSharedPtr)> JointStateUpdateCallback
MOCK_METHOD(void, resetTfSubscriptions,(),(override))
MOCK_METHOD(std::string, getDynamicTfTopicName,(),(const, override))
MOCK_METHOD(bool, ok,(),(const, override))
MOCK_METHOD(void, resetJointStateSubscription,(),(override))
MOCK_METHOD(rclcpp::Time, now,(),(const, override))
MOCK_METHOD(void, createJointStateSubscription,(const std::string &topic, planning_scene_monitor::JointStateUpdateCallback callback),(override))
MOCK_METHOD(bool, sleepFor,(const std::chrono::nanoseconds &nanoseconds),(const, override))
MOCK_METHOD(void, createStaticTfSubscription,(TfCallback callback),(override))
MOCK_METHOD(std::string, getJointStateTopicName,(),(const, override))
MOCK_METHOD(void, createDynamicTfSubscription,(TfCallback callback),(override))
MOCK_METHOD(std::string, getStaticTfTopicName,(),(const, override))