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 <prbt_hardware_support/OperationModes.h>
28 
30 {
31 using namespace prbt_hardware_support;
32 
33 using ::testing::_;
34 using ::testing::Le;
35 using ::testing::Return;
36 using ::testing::SetArgReferee;
37 using ::testing::DoAll;
38 
39 
40 
41 class OperationModeSetupExecutorTest : public ::testing::Test
42 {
43 public:
44  void SetUp() override;
45  void TearDown() override;
46 
47  MOCK_METHOD1(setSpeedLimit, bool(const double &));
48 
49 public:
50  double t1_limit_{ 0.1 };
51  double auto_limit_{ 0.2 };
52  std::unique_ptr<OperationModeSetupExecutor> executor_;
53 };
54 
56 {
58 
59  OperationModes op_mode;
60  op_mode.time_stamp = ros::Time::now();
61  op_mode.value = OperationModes::UNKNOWN;
62 
63  ON_CALL(*this, setSpeedLimit(_)).WillByDefault(Return(true));
64 
65  executor_ = std::unique_ptr<OperationModeSetupExecutor>(new OperationModeSetupExecutor(
66  t1_limit_,
67  auto_limit_,
68  std::bind(&OperationModeSetupExecutorTest::setSpeedLimit, 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, setSpeedLimit(t1_limit_)).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, setSpeedLimit(auto_limit_)).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, setSpeedLimit(t1_limit_)).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, setSpeedLimit(t1_limit_)).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, setSpeedLimit(t1_limit_)).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, setSpeedLimit(auto_limit_)).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, setSpeedLimit(Le(0.0))).WillOnce(Return(true));
211 
212  executor_->updateOperationMode(op_mode);
213 }
214 
216 {
217 public:
218  MOCK_METHOD1(call, bool(SetSpeedLimit& srv));
219  MOCK_METHOD0(getService, std::string());
220 };
221 
222 MATCHER_P(IsCorrectSpeedLimitSet, speed_limit, "") { return arg.request.speed_limit == speed_limit; }
223 
228 TEST_F(OperationModeSetupExecutorTest, testSetSpeedLimitSrvSuccess)
229 {
230  const double exp_limit {7.7};
231  SetSpeedLimit exp_srv;
232  exp_srv.request.speed_limit = exp_limit;
233 
235  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
236  EXPECT_CALL(mock, call(IsCorrectSpeedLimitSet(exp_limit))).Times(1).WillOnce(Return(true));
237 
238  EXPECT_TRUE(setSpeedLimitSrv<SetSpeedLimitServiceMock>(mock, exp_limit));
239 }
240 
244 TEST_F(OperationModeSetupExecutorTest, testSetSpeedLimitSrvFailure)
245 {
247  EXPECT_CALL(mock, getService()).WillRepeatedly(Return("TestServiceName"));
248  EXPECT_CALL(mock, call(_)).Times(1).WillOnce(Return(false));
249 
250  const double exp_limit {7.7};
251  EXPECT_FALSE(setSpeedLimitSrv<SetSpeedLimitServiceMock>(mock, exp_limit));
252 }
253 
255  public ::testing::WithParamInterface<std::pair<OperationModes::_value_type, double>>
256 {
257  OperationModes::_value_type getMode()
258  {
259  return GetParam().first;
260  }
261 
263  {
264  return GetParam().second;
265  }
266 };
267 
268 // Usage: If you don't care about uninteresting calls the the MOCK_METHOD but want to suppress the warning
269 typedef ::testing::NiceMock<OperationModeSetupExecutorTestSpeedOverride>
271 
279 {
280  /**********
281  * Step 1 *
282  **********/
283  OperationModes op_mode;
284  op_mode.time_stamp = ros::Time::now();
285  op_mode.value = GetParam().first;
286 
287  executor_->updateOperationMode(op_mode);
288 
289  auto req = GetSpeedOverrideRequest();
290  auto res = GetSpeedOverrideResponse();
291  executor_->getSpeedOverride(req, res);
292  EXPECT_EQ(res.speed_override, GetParam().second);
293 }
294 
296  SpeedOverrideModeTests,
298  ::testing::Values(
299  std::pair<OperationModes::_value_type, double>(OperationModes::UNKNOWN, 0.0),
300  std::pair<OperationModes::_value_type, double>(OperationModes::T1, 0.1),
301  std::pair<OperationModes::_value_type, double>(OperationModes::AUTO, 1.0)
302  )
303 );
304 
305 } // namespace operation_mode_setup_executor_tests
306 
307 int main(int argc, char *argv[])
308 {
309  testing::InitGoogleTest(&argc, argv);
310  return RUN_ALL_TESTS();
311 }
bool call(const std::string &service_name, MReq &req, MRes &res)
static void shutdown()
Sets the allowed speed limit for each frame based on the current operation mode.
static void init()
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)))
int main(int argc, char *argv[])
TEST_P(OperationModeSetupExecutorTestSpeedOverrideNice, testSpeedOverride)
static Time now()
TEST_F(OperationModeSetupExecutorTest, testUpdateOperationMode)
::testing::NiceMock< OperationModeSetupExecutorTestSpeedOverride > OperationModeSetupExecutorTestSpeedOverrideNice


prbt_hardware_support
Author(s):
autogenerated on Tue Feb 2 2021 03:50:17