unittest_operation_mode_setup_executor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Pilz GmbH & Co. KG
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <functional>
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 
23 #include <ros/time.h>
24 
25 #include <std_srvs/SetBool.h>
26 
27 #include <pilz_msgs/GetSpeedOverride.h>
28 #include <pilz_msgs/OperationModes.h>
29 
32 
34 {
35 using namespace prbt_hardware_support;
36 
37 using ::testing::_;
38 using ::testing::DoAll;
39 using ::testing::Le;
40 using ::testing::Return;
41 using ::testing::SetArgReferee;
42 
43 using pilz_msgs::OperationModes;
44 
45 class OperationModeSetupExecutorTest : public ::testing::Test
46 {
47 public:
48  void SetUp() override;
49  void TearDown() override;
50 
51  MOCK_METHOD1(monitorCartesianSpeed, bool(const bool));
52 
53 public:
54  std::unique_ptr<OperationModeSetupExecutor> executor_;
55 };
56 
58 {
60 
61  OperationModes op_mode;
62  op_mode.time_stamp = ros::Time::now();
63  op_mode.value = OperationModes::UNKNOWN;
64 
65  ON_CALL(*this, monitorCartesianSpeed(_)).WillByDefault(Return(true));
66 
67  executor_ = std::unique_ptr<OperationModeSetupExecutor>(new OperationModeSetupExecutor(
68  std::bind(&OperationModeSetupExecutorTest::monitorCartesianSpeed, this, std::placeholders::_1)));
69 }
70 
72 {
74 }
75 
89 TEST_F(OperationModeSetupExecutorTest, testUpdateOperationMode)
90 {
91  /**********
92  * Step 1 *
93  **********/
94  OperationModes op_mode;
95  op_mode.time_stamp = ros::Time::now();
96  op_mode.value = OperationModes::T1;
97 
98  EXPECT_CALL(*this, monitorCartesianSpeed(true)).WillOnce(Return(true));
99 
100  executor_->updateOperationMode(op_mode);
101 
102  /**********
103  * Step 2 *
104  **********/
105  OperationModes op_mode2;
106  op_mode2.time_stamp = ros::Time::now();
107  op_mode2.value = OperationModes::AUTO;
108 
109  EXPECT_CALL(*this, monitorCartesianSpeed(false)).WillOnce(Return(true));
110 
111  executor_->updateOperationMode(op_mode2);
112 }
113 
127 TEST_F(OperationModeSetupExecutorTest, testUpdateOperationModeSameMode)
128 {
129  /**********
130  * Step 1 *
131  **********/
132  OperationModes op_mode;
133  op_mode.time_stamp = ros::Time::now();
134  op_mode.value = OperationModes::T1;
135 
136  EXPECT_CALL(*this, monitorCartesianSpeed(true)).WillOnce(Return(true));
137 
138  executor_->updateOperationMode(op_mode);
139 
140  /**********
141  * Step 2 *
142  **********/
143  OperationModes op_mode2;
144  op_mode2.time_stamp = ros::Time::now();
145  op_mode2.value = op_mode.value;
146 
147  EXPECT_CALL(*this, monitorCartesianSpeed(true)).WillRepeatedly(Return(true));
148 
149  executor_->updateOperationMode(op_mode2);
150 }
151 
165 TEST_F(OperationModeSetupExecutorTest, testUpdateOperationModeSameTime)
166 {
167  /**********
168  * Step 1 *
169  **********/
170  OperationModes op_mode;
171  op_mode.time_stamp = ros::Time::now();
172  op_mode.value = OperationModes::T1;
173 
174  EXPECT_CALL(*this, monitorCartesianSpeed(true)).WillOnce(Return(true));
175 
176  executor_->updateOperationMode(op_mode);
177 
178  /**********
179  * Step 2 *
180  **********/
181  OperationModes op_mode2;
182  op_mode2.time_stamp = op_mode.time_stamp;
183  op_mode2.value = OperationModes::AUTO;
184 
185  EXPECT_CALL(*this, monitorCartesianSpeed(_)).Times(0);
186 
187  executor_->updateOperationMode(op_mode2);
188 }
189 
201 TEST_F(OperationModeSetupExecutorTest, testUpdateOperationModeUnknownMode)
202 {
203  /**********
204  * Step 1 *
205  **********/
206  OperationModes op_mode;
207  op_mode.time_stamp = ros::Time::now();
208  op_mode.value = OperationModes::UNKNOWN;
209 
210  EXPECT_CALL(*this, monitorCartesianSpeed(_)).Times(0);
211 
212  executor_->updateOperationMode(op_mode);
213 }
214 
216 {
217 public:
218  MOCK_METHOD1(call, bool(std_srvs::SetBool& srv));
219  MOCK_METHOD0(getService, std::string());
220 };
221 
222 MATCHER_P(IsSpeedMonitoringSettingCorrect, active_flag, "")
223 {
224  return arg.request.data == active_flag;
225 }
226 
231 TEST_F(OperationModeSetupExecutorTest, testMonitorCartesianSpeedSrvSuccess)
232 {
233  const bool exp_active_flag{ false };
234  std_srvs::SetBool exp_srv;
235  exp_srv.request.data = exp_active_flag;
236 
238  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
239  EXPECT_CALL(mock, call(IsSpeedMonitoringSettingCorrect(exp_active_flag))).Times(1).WillOnce(Return(true));
240 
241  EXPECT_TRUE(monitorCartesianSpeedSrv<MonitorCartesianSpeedServiceMock>(mock, exp_active_flag));
242 }
243 
247 TEST_F(OperationModeSetupExecutorTest, testMonitorCartesianSpeedSrvFailure)
248 {
250  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
251  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(Return(false));
252 
253  const bool exp_active_flag{ false };
254  EXPECT_FALSE(monitorCartesianSpeedSrv<MonitorCartesianSpeedServiceMock>(mock, exp_active_flag));
255 }
256 
259  public ::testing::WithParamInterface<std::pair<pilz_msgs::OperationModes::_value_type, double>>
260 {
261  OperationModes::_value_type getMode()
262  {
263  return GetParam().first;
264  }
265 
267  {
268  return GetParam().second;
269  }
270 };
271 
272 // Usage: If you don't care about uninteresting calls the the MOCK_METHOD but want to suppress the warning
273 typedef ::testing::NiceMock<OperationModeSetupExecutorTestSpeedOverride>
275 
283 {
284  /**********
285  * Step 1 *
286  **********/
287  OperationModes op_mode;
288  op_mode.time_stamp = ros::Time::now();
289  op_mode.value = GetParam().first;
290 
291  executor_->updateOperationMode(op_mode);
292 
293  auto req = pilz_msgs::GetSpeedOverrideRequest();
294  auto res = pilz_msgs::GetSpeedOverrideResponse();
295  executor_->getSpeedOverride(req, res);
296  EXPECT_EQ(res.speed_override, GetParam().second);
297 }
298 
300  ::testing::Values(std::pair<OperationModes::_value_type, double>(OperationModes::UNKNOWN, 0.0),
301  std::pair<OperationModes::_value_type, double>(OperationModes::T1, 0.1),
302  std::pair<OperationModes::_value_type, double>(OperationModes::AUTO, 1.0)), );
303 
304 } // namespace operation_mode_setup_executor_tests
305 
306 int main(int argc, char* argv[])
307 {
308  testing::InitGoogleTest(&argc, argv);
309  return RUN_ALL_TESTS();
310 }
bool call(const std::string &service_name, MReq &req, MRes &res)
INSTANTIATE_TEST_CASE_P(SpeedOverrideModeTests, OperationModeSetupExecutorTestSpeedOverrideNice, ::testing::Values(std::pair< OperationModes::_value_type, double >(OperationModes::UNKNOWN, 0.0), std::pair< OperationModes::_value_type, double >(OperationModes::T1, 0.1), std::pair< OperationModes::_value_type, double >(OperationModes::AUTO, 1.0)),)
static void shutdown()
Activates speed monitoring and sets the speed override based on the current operation mode...
static void init()
int main(int argc, char *argv[])
TEST_P(OperationModeSetupExecutorTestSpeedOverrideNice, testSpeedOverride)
MATCHER_P(IsSpeedMonitoringSettingCorrect, active_flag, "")
static Time now()
TEST_F(OperationModeSetupExecutorTest, testUpdateOperationMode)
::testing::NiceMock< OperationModeSetupExecutorTestSpeedOverride > OperationModeSetupExecutorTestSpeedOverrideNice


prbt_hardware_support
Author(s):
autogenerated on Mon Feb 28 2022 23:14:34