|
| 1 | +// Copyright (c) 2025 David Grbac |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | +#include <memory> |
| 17 | +#include <set> |
| 18 | +#include <string> |
| 19 | + |
| 20 | +#include "behaviortree_cpp/bt_factory.h" |
| 21 | + |
| 22 | +#include "utils/test_service.hpp" |
| 23 | +#include "nav2_behavior_tree/plugins/action/toggle_collision_monitor_service.hpp" |
| 24 | + |
| 25 | +class ToggleCollisionMonitorService : public TestService<nav2_msgs::srv::Toggle> |
| 26 | +{ |
| 27 | +public: |
| 28 | + ToggleCollisionMonitorService() |
| 29 | + : TestService("toggle_collision_monitor") |
| 30 | + {} |
| 31 | +}; |
| 32 | + |
| 33 | +class ToggleCollisionMonitorTestFixture : public ::testing::Test |
| 34 | +{ |
| 35 | +public: |
| 36 | + static void SetUpTestCase() |
| 37 | + { |
| 38 | + node_ = std::make_shared<nav2::LifecycleNode>("toggle_collision_monitor_test_fixture"); |
| 39 | + factory_ = std::make_shared<BT::BehaviorTreeFactory>(); |
| 40 | + |
| 41 | + config_ = new BT::NodeConfiguration(); |
| 42 | + |
| 43 | + // Create the blackboard that will be shared by all of the nodes in the tree |
| 44 | + config_->blackboard = BT::Blackboard::create(); |
| 45 | + // Put items on the blackboard |
| 46 | + config_->blackboard->set( |
| 47 | + "node", |
| 48 | + node_); |
| 49 | + config_->blackboard->set<std::chrono::milliseconds>( |
| 50 | + "server_timeout", |
| 51 | + std::chrono::milliseconds(20)); |
| 52 | + config_->blackboard->set<std::chrono::milliseconds>( |
| 53 | + "bt_loop_duration", |
| 54 | + std::chrono::milliseconds(10)); |
| 55 | + config_->blackboard->set<std::chrono::milliseconds>( |
| 56 | + "wait_for_service_timeout", |
| 57 | + std::chrono::milliseconds(1000)); |
| 58 | + config_->blackboard->set("initial_pose_received", false); |
| 59 | + |
| 60 | + factory_->registerNodeType<nav2_behavior_tree::ToggleCollisionMonitorService>( |
| 61 | + "ToggleCollisionMonitor"); |
| 62 | + } |
| 63 | + |
| 64 | + static void TearDownTestCase() |
| 65 | + { |
| 66 | + delete config_; |
| 67 | + config_ = nullptr; |
| 68 | + node_.reset(); |
| 69 | + server_.reset(); |
| 70 | + factory_.reset(); |
| 71 | + } |
| 72 | + |
| 73 | + void TearDown() override |
| 74 | + { |
| 75 | + tree_.reset(); |
| 76 | + } |
| 77 | + |
| 78 | + static std::shared_ptr<ToggleCollisionMonitorService> server_; |
| 79 | + |
| 80 | +protected: |
| 81 | + static nav2::LifecycleNode::SharedPtr node_; |
| 82 | + static BT::NodeConfiguration * config_; |
| 83 | + static std::shared_ptr<BT::BehaviorTreeFactory> factory_; |
| 84 | + static std::shared_ptr<BT::Tree> tree_; |
| 85 | +}; |
| 86 | + |
| 87 | +nav2::LifecycleNode::SharedPtr ToggleCollisionMonitorTestFixture::node_ = nullptr; |
| 88 | +std::shared_ptr<ToggleCollisionMonitorService> |
| 89 | +ToggleCollisionMonitorTestFixture::server_ = nullptr; |
| 90 | +BT::NodeConfiguration * ToggleCollisionMonitorTestFixture::config_ = nullptr; |
| 91 | +std::shared_ptr<BT::BehaviorTreeFactory> |
| 92 | +ToggleCollisionMonitorTestFixture::factory_ = nullptr; |
| 93 | +std::shared_ptr<BT::Tree> ToggleCollisionMonitorTestFixture::tree_ = nullptr; |
| 94 | + |
| 95 | +class ToggleParamTest |
| 96 | + : public ToggleCollisionMonitorTestFixture, |
| 97 | + public ::testing::WithParamInterface<bool> {}; |
| 98 | + |
| 99 | +TEST_P(ToggleParamTest, test_tick) |
| 100 | +{ |
| 101 | + const bool enable = GetParam(); |
| 102 | + |
| 103 | + std::string xml_txt = |
| 104 | + std::string( |
| 105 | + R"( |
| 106 | + <root BTCPP_format="4"> |
| 107 | + <BehaviorTree ID="MainTree"> |
| 108 | + <ToggleCollisionMonitor service_name="toggle_collision_monitor" enable=")") |
| 109 | + + |
| 110 | + std::string(enable ? "true" : "false") + |
| 111 | + R"(" /> |
| 112 | + </BehaviorTree> |
| 113 | + </root>)"; |
| 114 | + |
| 115 | + tree_ = std::make_shared<BT::Tree>(factory_->createTreeFromText(xml_txt, config_->blackboard)); |
| 116 | + EXPECT_EQ(tree_->rootNode()->executeTick(), BT::NodeStatus::SUCCESS); |
| 117 | +} |
| 118 | + |
| 119 | +INSTANTIATE_TEST_SUITE_P(EnableDisable, ToggleParamTest, ::testing::Values(true, false)); |
| 120 | + |
| 121 | +int main(int argc, char ** argv) |
| 122 | +{ |
| 123 | + ::testing::InitGoogleTest(&argc, argv); |
| 124 | + |
| 125 | + // initialize ROS |
| 126 | + rclcpp::init(argc, argv); |
| 127 | + |
| 128 | + // initialize service and spin on new thread |
| 129 | + ToggleCollisionMonitorTestFixture::server_ = |
| 130 | + std::make_shared<ToggleCollisionMonitorService>(); |
| 131 | + std::thread server_thread([]() { |
| 132 | + rclcpp::spin(ToggleCollisionMonitorTestFixture::server_); |
| 133 | + }); |
| 134 | + |
| 135 | + int all_successful = RUN_ALL_TESTS(); |
| 136 | + |
| 137 | + // shutdown ROS |
| 138 | + rclcpp::shutdown(); |
| 139 | + server_thread.join(); |
| 140 | + |
| 141 | + return all_successful; |
| 142 | +} |
0 commit comments