diff --git a/parallel_gripper_controller/CMakeLists.txt b/parallel_gripper_controller/CMakeLists.txt index 56aa623a6f..75380a953f 100644 --- a/parallel_gripper_controller/CMakeLists.txt +++ b/parallel_gripper_controller/CMakeLists.txt @@ -48,17 +48,19 @@ if(BUILD_TESTING) find_package(controller_manager REQUIRED) find_package(ros2_control_test_assets REQUIRED) - ament_add_gmock(test_load_parallel_gripper_action_controllers - test/test_load_parallel_gripper_action_controller.cpp - ) + add_rostest_with_parameters_gmock(test_load_parallel_gripper_action_controllers + test/test_load_parallel_gripper_action_controller.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test/gripper_action_controller_params.yaml) + target_include_directories(test_load_parallel_gripper_action_controllers PRIVATE include) ament_target_dependencies(test_load_parallel_gripper_action_controllers controller_manager ros2_control_test_assets ) - ament_add_gmock(test_parallel_gripper_controller - test/test_parallel_gripper_controller.cpp - ) + add_rostest_with_parameters_gmock(test_parallel_gripper_controller + test/test_parallel_gripper_controller.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test/gripper_action_controller_params.yaml) + target_include_directories(test_parallel_gripper_controller PRIVATE include) target_link_libraries(test_parallel_gripper_controller parallel_gripper_action_controller ) diff --git a/parallel_gripper_controller/include/parallel_gripper_controller/parallel_gripper_action_controller_impl.hpp b/parallel_gripper_controller/include/parallel_gripper_controller/parallel_gripper_action_controller_impl.hpp index bcfbb3b5c5..d37cb87fc8 100644 --- a/parallel_gripper_controller/include/parallel_gripper_controller/parallel_gripper_action_controller_impl.hpp +++ b/parallel_gripper_controller/include/parallel_gripper_controller/parallel_gripper_action_controller_impl.hpp @@ -44,7 +44,6 @@ controller_interface::CallbackReturn GripperActionController::on_init() try { param_listener_ = std::make_shared(get_node()); - params_ = param_listener_->get_params(); } catch (const std::exception & e) { @@ -245,6 +244,7 @@ controller_interface::CallbackReturn GripperActionController::on_configure( RCLCPP_ERROR(logger, "Joint name cannot be empty"); return controller_interface::CallbackReturn::ERROR; } + RCLCPP_INFO(logger, "Joint name is : %s", params_.joint.c_str()); return controller_interface::CallbackReturn::SUCCESS; } diff --git a/parallel_gripper_controller/src/gripper_action_controller_parameters.yaml b/parallel_gripper_controller/src/gripper_action_controller_parameters.yaml index 9b0dbb8a05..10b0df0958 100644 --- a/parallel_gripper_controller/src/gripper_action_controller_parameters.yaml +++ b/parallel_gripper_controller/src/gripper_action_controller_parameters.yaml @@ -11,6 +11,9 @@ parallel_gripper_action_controller: joint: { type: string, read_only: true, + validation: { + not_empty<>: null + } } state_interfaces: { type: string_array, diff --git a/parallel_gripper_controller/test/gripper_action_controller_params.yaml b/parallel_gripper_controller/test/gripper_action_controller_params.yaml new file mode 100644 index 0000000000..521960e18c --- /dev/null +++ b/parallel_gripper_controller/test/gripper_action_controller_params.yaml @@ -0,0 +1,7 @@ +test_gripper_action_position_controller: + ros__parameters: + joint: "joint1" + +test_gripper_action_position_controller_empty_joint: + ros__parameters: + joint: "" diff --git a/parallel_gripper_controller/test/test_load_parallel_gripper_action_controller.cpp b/parallel_gripper_controller/test/test_load_parallel_gripper_action_controller.cpp index 6ba99ac248..35a10a8c31 100644 --- a/parallel_gripper_controller/test/test_load_parallel_gripper_action_controller.cpp +++ b/parallel_gripper_controller/test/test_load_parallel_gripper_action_controller.cpp @@ -22,8 +22,6 @@ TEST(TestLoadGripperActionControllers, load_controller) { - rclcpp::init(0, nullptr); - std::shared_ptr executor = std::make_shared(); @@ -32,12 +30,16 @@ TEST(TestLoadGripperActionControllers, load_controller) ASSERT_NE( cm.load_controller( - "test_gripper_action_position_controller", "position_controllers/GripperActionController"), - nullptr); - ASSERT_NE( - cm.load_controller( - "test_gripper_action_effort_controller", "effort_controllers/GripperActionController"), + "test_gripper_action_position_controller", + "parallel_gripper_action_controller/GripperActionController"), nullptr); +} +int main(int argc, char ** argv) +{ + ::testing::InitGoogleMock(&argc, argv); + rclcpp::init(argc, argv); + int result = RUN_ALL_TESTS(); rclcpp::shutdown(); + return result; } diff --git a/parallel_gripper_controller/test/test_parallel_gripper_controller.cpp b/parallel_gripper_controller/test/test_parallel_gripper_controller.cpp index d9a5ae958b..417d87d40f 100644 --- a/parallel_gripper_controller/test/test_parallel_gripper_controller.cpp +++ b/parallel_gripper_controller/test/test_parallel_gripper_controller.cpp @@ -34,9 +34,9 @@ using GoalHandle = rclcpp_action::ServerGoalHandle; using testing::SizeIs; using testing::UnorderedElementsAre; -void GripperControllerTest::SetUpTestCase() { rclcpp::init(0, nullptr); } +void GripperControllerTest::SetUpTestCase() {} -void GripperControllerTest::TearDownTestCase() { rclcpp::shutdown(); } +void GripperControllerTest::TearDownTestCase() {} void GripperControllerTest::SetUp() { @@ -46,11 +46,13 @@ void GripperControllerTest::SetUp() void GripperControllerTest::TearDown() { controller_.reset(nullptr); } -void GripperControllerTest::SetUpController() +void GripperControllerTest::SetUpController( + const std::string & controller_name = "test_gripper_action_position_controller", + controller_interface::return_type expected_result = controller_interface::return_type::OK) { const auto result = - controller_->init("gripper_controller", "", 0, "", controller_->define_custom_node_options()); - ASSERT_EQ(result, controller_interface::return_type::OK); + controller_->init(controller_name, "", 0, "", controller_->define_custom_node_options()); + ASSERT_EQ(result, expected_result); std::vector command_ifs; command_ifs.emplace_back(this->joint_1_cmd_); @@ -62,7 +64,9 @@ void GripperControllerTest::SetUpController() TEST_F(GripperControllerTest, ParametersNotSet) { - this->SetUpController(); + this->SetUpController( + "test_gripper_action_position_controller_no_parameters", + controller_interface::return_type::ERROR); // configure failed, 'joints' parameter not set ASSERT_EQ( @@ -72,9 +76,9 @@ TEST_F(GripperControllerTest, ParametersNotSet) TEST_F(GripperControllerTest, JointParameterIsEmpty) { - this->SetUpController(); - - this->controller_->get_node()->set_parameter({"joint", ""}); + this->SetUpController( + "test_gripper_action_position_controller_empty_joint", + controller_interface::return_type::ERROR); // configure failed, 'joints' is empty ASSERT_EQ( @@ -86,7 +90,9 @@ TEST_F(GripperControllerTest, ConfigureParamsSuccess) { this->SetUpController(); - this->controller_->get_node()->set_parameter({"joint", "joint_1"}); + this->controller_->get_node()->set_parameter({"joint", "joint1"}); + + rclcpp::spin_some(this->controller_->get_node()->get_node_base_interface()); // configure successful ASSERT_EQ( @@ -98,29 +104,14 @@ TEST_F(GripperControllerTest, ConfigureParamsSuccess) ASSERT_THAT(cmd_if_conf.names, SizeIs(1lu)); ASSERT_THAT( cmd_if_conf.names, - UnorderedElementsAre(std::string("joint_1/") + hardware_interface::HW_IF_POSITION)); + UnorderedElementsAre(std::string("joint1/") + hardware_interface::HW_IF_POSITION)); EXPECT_EQ(cmd_if_conf.type, controller_interface::interface_configuration_type::INDIVIDUAL); auto state_if_conf = this->controller_->state_interface_configuration(); ASSERT_THAT(state_if_conf.names, SizeIs(2lu)); - ASSERT_THAT(state_if_conf.names, UnorderedElementsAre("joint_1/position", "joint_1/velocity")); + ASSERT_THAT(state_if_conf.names, UnorderedElementsAre("joint1/position", "joint1/velocity")); EXPECT_EQ(state_if_conf.type, controller_interface::interface_configuration_type::INDIVIDUAL); } -TEST_F(GripperControllerTest, ActivateWithWrongJointsNamesFails) -{ - this->SetUpController(); - - this->controller_->get_node()->set_parameter({"joint", "unicorn_joint"}); - - // activate failed, 'joint4' is not a valid joint name for the hardware - ASSERT_EQ( - this->controller_->on_configure(rclcpp_lifecycle::State()), - controller_interface::CallbackReturn::SUCCESS); - ASSERT_EQ( - this->controller_->on_activate(rclcpp_lifecycle::State()), - controller_interface::CallbackReturn::ERROR); -} - TEST_F(GripperControllerTest, ActivateSuccess) { this->SetUpController(); @@ -167,3 +158,12 @@ TEST_F(GripperControllerTest, ActivateDeactivateActivateSuccess) this->controller_->on_activate(rclcpp_lifecycle::State()), controller_interface::CallbackReturn::SUCCESS); } + +int main(int argc, char ** argv) +{ + ::testing::InitGoogleMock(&argc, argv); + rclcpp::init(argc, argv); + int result = RUN_ALL_TESTS(); + rclcpp::shutdown(); + return result; +} diff --git a/parallel_gripper_controller/test/test_parallel_gripper_controller.hpp b/parallel_gripper_controller/test/test_parallel_gripper_controller.hpp index 8bc6ab954c..d206097782 100644 --- a/parallel_gripper_controller/test/test_parallel_gripper_controller.hpp +++ b/parallel_gripper_controller/test/test_parallel_gripper_controller.hpp @@ -42,7 +42,8 @@ class GripperControllerTest : public ::testing::Test void SetUp(); void TearDown(); - void SetUpController(); + void SetUpController( + const std::string & controller_name, controller_interface::return_type expected_result); void SetUpHandles(); protected: