diff --git a/source/modulo_controllers/test/test_controller_interface.cpp b/source/modulo_controllers/test/test_controller_interface.cpp index f8219650..67cf4f05 100644 --- a/source/modulo_controllers/test/test_controller_interface.cpp +++ b/source/modulo_controllers/test/test_controller_interface.cpp @@ -3,14 +3,13 @@ #include #include +#include #include #include #include "modulo_controllers/ControllerInterface.hpp" #include "test_modulo_controllers/communication_nodes.hpp" -#include - using namespace modulo_controllers; using namespace state_representation; using namespace std::chrono_literals; @@ -28,6 +27,12 @@ class FriendControllerInterface : public ControllerInterface { } }; +sensor_msgs::msg::Image make_image_msg(double width) { + auto msg = sensor_msgs::msg::Image(); + msg.width = width; + return msg; +} + using BoolT = std::tuple; using DoubleT = std::tuple; using DoubleVecT = std::tuple, std_msgs::msg::Float64MultiArray>; @@ -35,6 +40,7 @@ using IntT = std::tuple; using StringT = std::tuple; using CartesianStateT = std::tuple; using JointStateT = std::tuple; +using ImageT = std::tuple; template T write_std_msg(const T& message_data) { @@ -51,6 +57,12 @@ T write_state_msg(const T& message_data) { return copy; } +ImageT write_image_msg(const ImageT& message_data) { + auto copy = message_data; + std::get<1>(copy) = std::get<0>(message_data); + return copy; +} + template T read_std_msg(const T& message_data) { auto copy = message_data; @@ -65,6 +77,12 @@ T read_state_msg(const T& message_data) { return copy; } +ImageT read_image_msg(const ImageT& message_data) { + auto copy = message_data; + std::get<0>(copy) = std::get<1>(message_data); + return copy; +} + template bool std_msg_equal(const T& sent, const T& received) { return std::get<0>(sent) == std::get<0>(received); @@ -76,12 +94,16 @@ bool encoded_state_equal(const T& sent, const T& received) { return equal && std::get<0>(sent).data().isApprox(std::get<0>(received).data()); } +bool sensor_msg_equal(const ImageT& sent, const ImageT& received) { + return std::get<0>(sent).width == std::get<0>(received).width; +} + template using SignalT = std::vector, std::function, std::function>>; static std::tuple< SignalT, SignalT, SignalT, SignalT, SignalT, SignalT, - SignalT> + SignalT, SignalT> signal_test_cases{ {std::make_tuple( std::make_tuple(true, std_msgs::msg::Bool()), write_std_msg, read_std_msg, @@ -102,7 +124,9 @@ static std::tuple< write_state_msg, read_state_msg, encoded_state_equal)}, {std::make_tuple( std::make_tuple(JointState::Random("test", 3), modulo_core::EncodedState()), write_state_msg, - read_state_msg, encoded_state_equal)}}; + read_state_msg, encoded_state_equal)}, + {std::make_tuple( + std::make_tuple(make_image_msg(1), make_image_msg(2)), write_image_msg, read_image_msg, sensor_msg_equal)}}; template class ControllerInterfaceTest : public ::testing::Test { @@ -179,7 +203,6 @@ TYPED_TEST_P(ControllerInterfaceTest, OutputTest) { for (auto [message_data, write_func, read_func, validation_func] : this->test_cases_) { auto data = std::get<0>(message_data); this->interface_->template write_output("output", data); - // rclcpp::spin_some(this->interface_->get_node()->get_node_base_interface()); auto return_code = rclcpp::spin_until_future_complete(test_node.get_node_base_interface(), test_node.get_sub_future(), 200ms); ASSERT_EQ(return_code, rclcpp::FutureReturnCode::SUCCESS); @@ -188,40 +211,7 @@ TYPED_TEST_P(ControllerInterfaceTest, OutputTest) { } } -TYPED_TEST_P(ControllerInterfaceTest, CustomOutputTest) { - auto interface = std::make_unique(); - interface->init("controller_interface", "", 0, "", interface->define_custom_node_options()); - interface->get_node()->set_parameter({"hardware_name", "test"}); - interface->get_node()->set_parameter({"input_validity_period", 0.1}); - - interface->add_output("output", "/output"); - auto node_state = interface->get_node()->configure(); - ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE); - - node_state = interface->get_node()->activate(); - ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE); - - interface->write_output("output", sensor_msgs::msg::Image()); -} - -TYPED_TEST_P(ControllerInterfaceTest, CustomInputTest) { - auto interface = std::make_unique(); - interface->init("controller_interface", "", 0, "", interface->define_custom_node_options()); - interface->get_node()->set_parameter({"hardware_name", "test"}); - interface->get_node()->set_parameter({"input_validity_period", 0.1}); - - interface->add_input("input", "/input"); - auto node_state = interface->get_node()->configure(); - ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE); - - node_state = interface->get_node()->activate(); - ASSERT_EQ(node_state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE); - - auto msg = interface->read_input("input"); -} - -REGISTER_TYPED_TEST_CASE_P( - ControllerInterfaceTest, ConfigureErrorTest, InputTest, OutputTest, CustomOutputTest, CustomInputTest); +REGISTER_TYPED_TEST_CASE_P(ControllerInterfaceTest, ConfigureErrorTest, InputTest, OutputTest); -typedef ::testing::Types SignalTypes; +typedef ::testing::Types SignalTypes; INSTANTIATE_TYPED_TEST_CASE_P(TestPrefix, ControllerInterfaceTest, SignalTypes);