|
| 1 | +// Copyright (c) 2025 Polymath Robotics |
| 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 | + |
| 18 | +#include "utils/test_behavior_tree_fixture.hpp" |
| 19 | +#include "utils/test_dummy_tree_node.hpp" |
| 20 | +#include "nav2_behavior_tree/plugins/control/nonblocking_sequence.hpp" |
| 21 | + |
| 22 | +class NonblockingSequenceTestFixture : public nav2_behavior_tree::BehaviorTreeTestFixture |
| 23 | +{ |
| 24 | +public: |
| 25 | + void SetUp() override |
| 26 | + { |
| 27 | + bt_node_ = std::make_shared<nav2_behavior_tree::NonblockingSequence>( |
| 28 | + "nonblocking_sequence", *config_); |
| 29 | + first_child_ = std::make_shared<nav2_behavior_tree::DummyNode>(); |
| 30 | + second_child_ = std::make_shared<nav2_behavior_tree::DummyNode>(); |
| 31 | + third_child_ = std::make_shared<nav2_behavior_tree::DummyNode>(); |
| 32 | + bt_node_->addChild(first_child_.get()); |
| 33 | + bt_node_->addChild(second_child_.get()); |
| 34 | + bt_node_->addChild(third_child_.get()); |
| 35 | + } |
| 36 | + |
| 37 | + void TearDown() override |
| 38 | + { |
| 39 | + first_child_.reset(); |
| 40 | + second_child_.reset(); |
| 41 | + third_child_.reset(); |
| 42 | + bt_node_.reset(); |
| 43 | + } |
| 44 | + |
| 45 | +protected: |
| 46 | + static std::shared_ptr<nav2_behavior_tree::NonblockingSequence> bt_node_; |
| 47 | + static std::shared_ptr<nav2_behavior_tree::DummyNode> first_child_; |
| 48 | + static std::shared_ptr<nav2_behavior_tree::DummyNode> second_child_; |
| 49 | + static std::shared_ptr<nav2_behavior_tree::DummyNode> third_child_; |
| 50 | +}; |
| 51 | + |
| 52 | +std::shared_ptr<nav2_behavior_tree::NonblockingSequence> |
| 53 | +NonblockingSequenceTestFixture::bt_node_ = nullptr; |
| 54 | +std::shared_ptr<nav2_behavior_tree::DummyNode> |
| 55 | +NonblockingSequenceTestFixture::first_child_ = nullptr; |
| 56 | +std::shared_ptr<nav2_behavior_tree::DummyNode> |
| 57 | +NonblockingSequenceTestFixture::second_child_ = nullptr; |
| 58 | +std::shared_ptr<nav2_behavior_tree::DummyNode> |
| 59 | +NonblockingSequenceTestFixture::third_child_ = nullptr; |
| 60 | + |
| 61 | +TEST_F(NonblockingSequenceTestFixture, test_failure_on_idle_child) |
| 62 | +{ |
| 63 | + first_child_->changeStatus(BT::NodeStatus::IDLE); |
| 64 | + EXPECT_THROW(bt_node_->executeTick(), std::runtime_error); |
| 65 | +} |
| 66 | + |
| 67 | +TEST_F(NonblockingSequenceTestFixture, test_failure) |
| 68 | +{ |
| 69 | + first_child_->changeStatus(BT::NodeStatus::FAILURE); |
| 70 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::FAILURE); |
| 71 | + EXPECT_EQ(first_child_->status(), BT::NodeStatus::IDLE); |
| 72 | + EXPECT_EQ(second_child_->status(), BT::NodeStatus::IDLE); |
| 73 | + EXPECT_EQ(third_child_->status(), BT::NodeStatus::IDLE); |
| 74 | + |
| 75 | + first_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 76 | + second_child_->changeStatus(BT::NodeStatus::FAILURE); |
| 77 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::FAILURE); |
| 78 | + EXPECT_EQ(first_child_->status(), BT::NodeStatus::IDLE); |
| 79 | + EXPECT_EQ(second_child_->status(), BT::NodeStatus::IDLE); |
| 80 | + EXPECT_EQ(third_child_->status(), BT::NodeStatus::IDLE); |
| 81 | + |
| 82 | + first_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 83 | + second_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 84 | + third_child_->changeStatus(BT::NodeStatus::FAILURE); |
| 85 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::FAILURE); |
| 86 | + EXPECT_EQ(first_child_->status(), BT::NodeStatus::IDLE); |
| 87 | + EXPECT_EQ(second_child_->status(), BT::NodeStatus::IDLE); |
| 88 | + EXPECT_EQ(third_child_->status(), BT::NodeStatus::IDLE); |
| 89 | + |
| 90 | + first_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 91 | + second_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 92 | + third_child_->changeStatus(BT::NodeStatus::RUNNING); |
| 93 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::RUNNING); |
| 94 | + first_child_->changeStatus(BT::NodeStatus::FAILURE); |
| 95 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::FAILURE); |
| 96 | + EXPECT_EQ(first_child_->status(), BT::NodeStatus::IDLE); |
| 97 | + EXPECT_EQ(second_child_->status(), BT::NodeStatus::IDLE); |
| 98 | + EXPECT_EQ(third_child_->status(), BT::NodeStatus::IDLE); |
| 99 | +} |
| 100 | + |
| 101 | +TEST_F(NonblockingSequenceTestFixture, test_behavior) |
| 102 | +{ |
| 103 | + first_child_->changeStatus(BT::NodeStatus::RUNNING); |
| 104 | + second_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 105 | + third_child_->changeStatus(BT::NodeStatus::RUNNING); |
| 106 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::RUNNING); |
| 107 | + |
| 108 | + first_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 109 | + second_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 110 | + third_child_->changeStatus(BT::NodeStatus::RUNNING); |
| 111 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::RUNNING); |
| 112 | + |
| 113 | + first_child_->changeStatus(BT::NodeStatus::RUNNING); |
| 114 | + second_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 115 | + third_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 116 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::RUNNING); |
| 117 | + |
| 118 | + first_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 119 | + second_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 120 | + third_child_->changeStatus(BT::NodeStatus::SUCCESS); |
| 121 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::SUCCESS); |
| 122 | + |
| 123 | + // Even if first two children are running, we will still tick the third |
| 124 | + // node, which if set to failure, fails everything |
| 125 | + first_child_->changeStatus(BT::NodeStatus::RUNNING); |
| 126 | + second_child_->changeStatus(BT::NodeStatus::RUNNING); |
| 127 | + third_child_->changeStatus(BT::NodeStatus::FAILURE); |
| 128 | + EXPECT_EQ(bt_node_->executeTick(), BT::NodeStatus::FAILURE); |
| 129 | + EXPECT_EQ(first_child_->status(), BT::NodeStatus::IDLE); |
| 130 | + EXPECT_EQ(second_child_->status(), BT::NodeStatus::IDLE); |
| 131 | + EXPECT_EQ(third_child_->status(), BT::NodeStatus::IDLE); |
| 132 | +} |
| 133 | + |
| 134 | +int main(int argc, char ** argv) |
| 135 | +{ |
| 136 | + ::testing::InitGoogleTest(&argc, argv); |
| 137 | + |
| 138 | + // initialize ROS |
| 139 | + rclcpp::init(argc, argv); |
| 140 | + |
| 141 | + int all_successful = RUN_ALL_TESTS(); |
| 142 | + |
| 143 | + // shutdown ROS |
| 144 | + rclcpp::shutdown(); |
| 145 | + |
| 146 | + return all_successful; |
| 147 | +} |
0 commit comments