iCub-main
testDeviceCanBatterySensor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  * Author: Luca Tricerri
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 #include "gmock/gmock.h"
9 #include "gtest/gtest.h"
10 
11 #include <ethResource.h>
12 
13 #include "EoProtocolAS.h"
14 #include "testUtils.h"
15 #include "embObjBattery.h"
16 
17 using ::testing::_;
18 using ::testing::An;
19 using ::testing::Eq;
20 using ::testing::Gt;
21 using ::testing::InSequence;
22 using ::testing::InvokeArgument;
23 using ::testing::Matcher;
24 using ::testing::Pointee;
25 using ::testing::Return;
26 using ::testing::TypedEq;
27 using testing::internal::operator==;
28 
29 using namespace yarp;
30 using namespace yarp::os;
31 using namespace yarp::dev;
32 
34 {
35  public:
40  using yarp::dev::embObjBattery::embObjBattery::calculateBoardTime;
41  using yarp::dev::embObjBattery::embObjBattery::update;
42 
43  embObjCanBatterysensor_Mock(std::shared_ptr<yarp::dev::embObjDevPrivData> device) : yarp::dev::embObjBattery(device){};
45  MOCK_METHOD(double, calculateBoardTime, (eOabstime_t), (override));
46 };
47 
49 {
50  public:
51  MOCK_METHOD(bool, isOpen, (), (const, override));
52 
53  embObjDevPrivData_Mock(std::string name) : yarp::dev::embObjDevPrivData(name){};
54 };
55 
57 {
58  public:
59  MOCK_METHOD(bool, setcheckRemoteValue, (const eOprotID32_t, void *, const unsigned int, const double, const double), (override));
60  MOCK_METHOD(bool, serviceSetRegulars, (eOmn_serv_category_t, vector<eOprotID32_t> &, double), (override));
61 };
62 
63 TEST(CanBatterysensor, sendConfig2boards_simple_positive_001)
64 {
65  // Setup
68  parser.batteryInfo_ = {100, eobrd_bms, 0, 0, 0, 0, 0, 0, 0};
69 
70  EthResource_Mock deviceRes;
71  uint32_t id32First = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_battery_config);
72 
73  EXPECT_CALL(deviceRes, setcheckRemoteValue(id32First, _, 10, 0.010, 0.050)).WillOnce(Return(true));
74 
75  // Test
76  bool ret = device.sendConfig2boards(parser, &deviceRes);
77 
78  EXPECT_TRUE(ret);
79 }
80 
81 TEST(CanBatterysensor, sendConfig2boards_simple_negative_001)
82 {
83  // Setup
86  parser.batteryInfo_ = {100, eobrd_unknown, 0, 0, 0, 0, 0, 0, 0};
87  EthResource_Mock deviceRes;
88  uint32_t id32First = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_battery_config);
89 
90  EXPECT_CALL(deviceRes, setcheckRemoteValue(id32First, _, 10, 0.010, 0.050)).WillOnce(Return(false));
91 
92  // Test
93  bool ret = device.sendConfig2boards(parser, &deviceRes);
94  EXPECT_FALSE(ret);
95 }
96 
97 TEST(CanBatterysensor, sendStart2boards_simple_positive_001)
98 {
99  // Setup
102  parser.batteryInfo_ = {100, eobrd_bms, 0, 0, 0, 0, 0, 0, 0};
103  EthResource_Mock deviceRes;
104  uint32_t id32First = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_ft_cmmnds_enable);
105  bool enable = 1;
106 
107  // EXPECT_CALL(deviceRes, setcheckRemoteValue(id32First, Pointee(enable), 10, 0.010, 0.050)).WillOnce(Return(true));
108  EXPECT_CALL(deviceRes, setcheckRemoteValue(id32First, _, 10, 0.010, 0.050)).WillOnce(Return(true));
109 
110  // Test
111  bool ret = device.sendStart2boards(parser, &deviceRes);
112  EXPECT_TRUE(ret);
113 }
114 
115 TEST(CanBatterysensor, sendStart2boards_simple_negative_001)
116 {
117  // Setup
120  parser.batteryInfo_ = {100, eobrd_unknown, 0, 0, 0, 0, 0, 0, 0};
121  EthResource_Mock deviceRes;
122  uint32_t id32First = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_ft_cmmnds_enable);
123  bool enable = 1;
124 
125  // EXPECT_CALL(deviceRes, setcheckRemoteValue(id32First, Pointee(enable), 10, 0.010, 0.050)).WillOnce(Return(true));
126  EXPECT_CALL(deviceRes, setcheckRemoteValue(id32First, _, 10, 0.010, 0.050)).WillOnce(Return(false));
127 
128  // Test
129  bool ret = device.sendStart2boards(parser, &deviceRes);
130  EXPECT_FALSE(ret);
131 }
132 
133 TEST(CanBatterysensor, serviceSetRegulars_simple_positive_001)
134 {
135  // Setup
138  parser.batteryInfo_ = {100, eobrd_bms, 0, 0, 0, 0, 0, 0, 0};
139  EthResource_Mock deviceRes;
140  uint32_t id32First = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_battery_status_timedvalue);
141  vector<eOprotID32_t> ids = {id32First};
142 
143  EXPECT_CALL(deviceRes, serviceSetRegulars(eomn_serv_category_battery, ids, _)).WillOnce(Return(true));
144 
145  // Test
146  bool ret = device.initRegulars(parser, &deviceRes);
147  EXPECT_TRUE(ret);
148 }
149 
150 TEST(CanBatterysensor, serviceSetRegulars_simple_negative_001)
151 {
152  // Setup
155  parser.batteryInfo_ = {100, eobrd_bms, 0, 0, 0, 0, 0, 0, 0};
156  EthResource_Mock deviceRes;
157  uint32_t id32First = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_battery_status_timedvalue);
158  vector<eOprotID32_t> ids = {id32First};
159 
160  EXPECT_CALL(deviceRes, serviceSetRegulars(eomn_serv_category_battery, ids, _)).WillOnce(Return(false));
161 
162  // Test
163  bool ret = device.initRegulars(parser, &deviceRes);
164  EXPECT_FALSE(ret);
165 }
166 
167 TEST(CanBatterysensor, update_simple_positive_001)
168 {
169  // Setup
170  yarp::os::Network::init();
171  std::shared_ptr<embObjDevPrivData_Mock> privateData = std::make_shared<embObjDevPrivData_Mock>("test");
172  embObjCanBatterysensor_Mock device(privateData);
173  uint32_t id32First = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_ft_status_timedvalue);
174  eOas_battery_timedvalue_t data = {0 /*age*/, 10 /*temperature in dec C*/, 0, 2, 3, 4, 5, 0};
175  CanBatteryData expected = {1, 3, 4, 5, 2, 2, 7, ""};
176 
177  EXPECT_CALL(*privateData, isOpen()).WillRepeatedly(Return(true));
178  EXPECT_CALL(device, calculateBoardTime(_)).WillRepeatedly(Return(7));
179 
180  // Test
181  bool ret = device.update(id32First, 1, (void *)&data);
182  EXPECT_TRUE(ret);
183  EXPECT_EQ(expected, device.canBatteryData_);
184 }
185 
186 TEST(CanBatterysensor, update_simple_positive_trunk_voltage_001)
187 {
188  // Setup
189  yarp::os::Network::init();
190  std::shared_ptr<embObjDevPrivData_Mock> privateData = std::make_shared<embObjDevPrivData_Mock>("test");
191  embObjCanBatterysensor_Mock device(privateData);
192  uint32_t id32First = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_ft_status_timedvalue);
193  eOas_battery_timedvalue_t data = {0 /*age*/, 10 /*temperature in dec C*/, 0, 2, 3, 4.123, 5, 0};
194  CanBatteryData expected = {1, 3, 4.123, 5, 2, 2, 7, ""};
195 
196  EXPECT_CALL(*privateData, isOpen()).WillRepeatedly(Return(true));
197  EXPECT_CALL(device, calculateBoardTime(_)).WillRepeatedly(Return(7));
198 
199  // Test
200  bool ret = device.update(id32First, 1, (void *)&data);
201  EXPECT_TRUE(ret);
202  EXPECT_EQ(expected, device.canBatteryData_);
203 }
204 
205 TEST(CanBatterysensor, update_simple_negative_001)
206 {
207  // Setup
208  yarp::os::Network::init();
209  std::shared_ptr<embObjDevPrivData_Mock> privateData = std::make_shared<embObjDevPrivData_Mock>("test");
210  embObjCanBatterysensor_Mock device(privateData);
211  uint32_t id32First = eoprot_ID_get(eoprot_endpoint_analogsensors, eoprot_entity_as_battery, 0, eoprot_tag_as_ft_status_timedvalue);
212  eOas_battery_timedvalue_t data = {0 /*age*/, 10 /*temperature in dec C*/, 2, 3, 4, 9};
213  CanBatteryData expected = {1, 2, 3, 4, 5, 0, 7, ""};
214 
215  EXPECT_CALL(*privateData, isOpen()).WillRepeatedly(Return(true));
216  EXPECT_CALL(device, calculateBoardTime(_)).WillRepeatedly(Return(7));
217 
218  // Test
219  bool ret = device.update(id32First, 1, (void *)&data);
220  EXPECT_TRUE(ret);
221  EXPECT_NE(expected, device.canBatteryData_);
222 }
223 
224 TEST(CanBatterysensor, getBatteryVoltage_positive_001)
225 {
226  // Setup
227  std::shared_ptr<embObjDevPrivData_Mock> privateData = std::make_shared<embObjDevPrivData_Mock>("test");
228  embObjCanBatterysensor_Mock device(privateData);
229 
230  device.canBatteryData_ = {1, 2, 3, 4, 5, 0, 6, ""};
231 
232  EXPECT_CALL(*privateData, isOpen()).WillRepeatedly(Return(true));
233 
234  // Test
235  double data;
236  bool ret = device.getBatteryVoltage(data);
237 
238  double expected = 2;
239  EXPECT_EQ(expected, data);
240 }
241 
242 TEST(CanBatterysensor, calculateBoardTime_positive_001)
243 {
244  // Setup
245  yarp::os::Network::init();
246  std::shared_ptr<embObjDevPrivData_Mock> privateData = std::make_shared<embObjDevPrivData_Mock>("test");
247  embObjCanBatterysensor_Mock device(privateData);
248 
249  EXPECT_CALL(*privateData, isOpen()).WillRepeatedly(Return(true));
250 
251  // Test
252  double data;
253  double timestamp;
254  double first = device.embObjBattery::calculateBoardTime(1000000);
255  double second = device.embObjBattery::calculateBoardTime(2000000);
256  double third = device.embObjBattery::calculateBoardTime(3000000);
257  double diff = second - first;
258 
259  EXPECT_TRUE(1.0 == diff);
260 
261  diff = third - first;
262 
263  EXPECT_TRUE(2.0 == diff);
264 }
265 
266 TEST(CanBatterysensor, type)
267 {
268  // Setup
269  std::shared_ptr<embObjDevPrivData_Mock> privateData = std::make_shared<embObjDevPrivData_Mock>("test");
270  embObjCanBatterysensor_Mock device(privateData);
271 
272  EXPECT_CALL(*privateData, isOpen()).WillRepeatedly(Return(true));
273 
274  EXPECT_TRUE(device.type() == eth::iethres_analogbattery);
275 }
@ data
MOCK_METHOD(bool, setcheckRemoteValue,(const eOprotID32_t, void *, const unsigned int, const double, const double),(override))
MOCK_METHOD(bool, serviceSetRegulars,(eOmn_serv_category_t, vector< eOprotID32_t > &, double),(override))
MOCK_METHOD(double, calculateBoardTime,(eOabstime_t),(override))
embObjCanBatterysensor_Mock(std::shared_ptr< yarp::dev::embObjDevPrivData > device)
MOCK_METHOD(bool, isOpen,(),(const, override))
eth::iethresType_t type() override
bool sendConfig2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool getBatteryVoltage(double &voltage) override
bool sendStart2boards(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool initRegulars(ServiceParserCanBattery &parser, eth::AbstractEthResource *deviceRes)
bool update(eOprotID32_t id32, double timestamp, void *rxdata) override
CanBatteryData canBatteryData_
Definition: embObjBattery.h:78
@ iethres_analogbattery
Definition: IethResource.h:76
Copyright (C) 2008 RobotCub Consortium.
TEST(CanBatterysensor, sendConfig2boards_simple_positive_001)