|
1 | 1 |
|
| 2 | +/* Copyright (C) 2015-2017 Michele Colledanchise - All Rights Reserved |
| 3 | +* |
| 4 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 5 | +* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 6 | +* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 7 | +* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 8 | +* |
| 9 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 10 | +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 11 | +* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 12 | +*/ |
| 13 | + |
| 14 | +#include <gtest/gtest.h> |
| 15 | +#include "action_test_node.h" |
| 16 | +#include "condition_test_node.h" |
| 17 | +#include "behaviortree_cpp/behavior_tree.h" |
| 18 | + |
| 19 | +using BT::NodeStatus; |
| 20 | +using std::chrono::milliseconds; |
| 21 | + |
| 22 | +struct ComplexReactiveSequence2ActionsTest : testing::Test |
| 23 | +{ |
| 24 | + BT::ReactiveSequence root; |
| 25 | + BT::AsyncActionTest action_1; |
| 26 | + BT::AsyncActionTest action_2; |
| 27 | + BT::ReactiveSequence seq_1; |
| 28 | + BT::ReactiveSequence seq_2; |
| 29 | + |
| 30 | + BT::ConditionTestNode condition_1; |
| 31 | + BT::ConditionTestNode condition_2; |
| 32 | + |
| 33 | + ComplexReactiveSequence2ActionsTest() |
| 34 | + : root("root_sequence") |
| 35 | + , action_1("action_1", milliseconds(100)) |
| 36 | + , action_2("action_2", milliseconds(100)) |
| 37 | + , seq_1("sequence_1") |
| 38 | + , seq_2("sequence_2") |
| 39 | + , condition_1("condition_1") |
| 40 | + , condition_2("condition_2") |
| 41 | + { |
| 42 | + root.addChild(&seq_1); |
| 43 | + { |
| 44 | + seq_1.addChild(&condition_1); |
| 45 | + seq_1.addChild(&action_1); |
| 46 | + } |
| 47 | + root.addChild(&seq_2); |
| 48 | + { |
| 49 | + seq_2.addChild(&condition_2); |
| 50 | + seq_2.addChild(&action_2); |
| 51 | + } |
| 52 | + } |
| 53 | + ~ComplexReactiveSequence2ActionsTest() |
| 54 | + { |
| 55 | + haltAllActions(&root); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +struct ComplexReactiveTree : testing::Test |
| 60 | +{ |
| 61 | + BT::ReactiveSequence root; |
| 62 | + BT::AsyncActionTest action_1; |
| 63 | + BT::AsyncActionTest action_2; |
| 64 | + BT::ReactiveFallback fal_1; |
| 65 | + BT::ReactiveFallback fal_2; |
| 66 | + |
| 67 | + BT::ConditionTestNode condition_1; |
| 68 | + BT::ConditionTestNode condition_2; |
| 69 | + |
| 70 | + ComplexReactiveTree() |
| 71 | + : root("root_sequence") |
| 72 | + , action_1("action_1", milliseconds(100)) |
| 73 | + , action_2("action_2", milliseconds(100)) |
| 74 | + , fal_1("fallback_1") |
| 75 | + , fal_2("fallback_2") |
| 76 | + , condition_1("condition_1") |
| 77 | + , condition_2("condition_2") |
| 78 | + { |
| 79 | + root.addChild(&fal_1); |
| 80 | + { |
| 81 | + fal_1.addChild(&condition_1); |
| 82 | + fal_1.addChild(&action_1); |
| 83 | + } |
| 84 | + root.addChild(&fal_2); |
| 85 | + { |
| 86 | + fal_2.addChild(&condition_2); |
| 87 | + fal_2.addChild(&action_2); |
| 88 | + } |
| 89 | + } |
| 90 | + ~ComplexReactiveTree() |
| 91 | + { |
| 92 | + haltAllActions(&root); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +/****************TESTS START HERE***************************/ |
| 97 | + |
| 98 | + |
| 99 | +TEST_F(ComplexReactiveSequence2ActionsTest, ConditionsTrue) |
| 100 | +{ |
| 101 | + BT::NodeStatus state = root.executeTick(); |
| 102 | + |
| 103 | + state = root.executeTick(); |
| 104 | + |
| 105 | + ASSERT_EQ(NodeStatus::RUNNING, state); |
| 106 | + ASSERT_EQ(NodeStatus::RUNNING, seq_1.status()); |
| 107 | + ASSERT_EQ(NodeStatus::SUCCESS, condition_1.status()); |
| 108 | + ASSERT_EQ(NodeStatus::RUNNING, action_1.status()); |
| 109 | + ASSERT_EQ(NodeStatus::IDLE, seq_2.status()); |
| 110 | + ASSERT_EQ(NodeStatus::IDLE, condition_2.status()); |
| 111 | + ASSERT_EQ(NodeStatus::IDLE, action_2.status()); |
| 112 | + |
| 113 | + std::this_thread::sleep_for(milliseconds(300)); |
| 114 | + state = root.executeTick(); |
| 115 | + |
| 116 | + ASSERT_EQ(NodeStatus::RUNNING, state); |
| 117 | + ASSERT_EQ(NodeStatus::SUCCESS, seq_1.status()); |
| 118 | + ASSERT_EQ(NodeStatus::IDLE, condition_1.status()); |
| 119 | + ASSERT_EQ(NodeStatus::IDLE, action_1.status()); |
| 120 | + ASSERT_EQ(NodeStatus::RUNNING, seq_2.status()); |
| 121 | + ASSERT_EQ(NodeStatus::SUCCESS, condition_2.status()); |
| 122 | + ASSERT_EQ(NodeStatus::RUNNING, action_2.status()); |
| 123 | + |
| 124 | + |
| 125 | + std::this_thread::sleep_for(milliseconds(300)); |
| 126 | + state = root.executeTick(); |
| 127 | + |
| 128 | + ASSERT_EQ(NodeStatus::RUNNING, state); |
| 129 | + ASSERT_EQ(NodeStatus::RUNNING, seq_1.status()); |
| 130 | + ASSERT_EQ(NodeStatus::SUCCESS, condition_1.status()); |
| 131 | + ASSERT_EQ(NodeStatus::RUNNING, action_1.status()); |
| 132 | + ASSERT_EQ(NodeStatus::IDLE, seq_2.status()); |
| 133 | + ASSERT_EQ(NodeStatus::IDLE, condition_2.status()); |
| 134 | + ASSERT_EQ(NodeStatus::IDLE, action_2.status());; |
| 135 | +} |
0 commit comments