blob: b40cdae0a4f1fcfefbae8c977e43fe7b544132c2 [file] [log] [blame]
Patrick Venture0e2d68d2018-12-19 07:53:45 -08001#include "env_mock.hpp"
Patrick Venture99b95812018-12-19 08:59:15 -08002#include "gpio_mock.hpp"
Patrick Venture0e2d68d2018-12-19 07:53:45 -08003#include "hwmonio_mock.hpp"
4#include "sensor.hpp"
5
Patrick Venture99b95812018-12-19 08:59:15 -08006#include <gpioplus/test/handle.hpp>
Patrick Williamse8771fd2023-05-10 07:51:06 -05007
Patrick Venture0e2d68d2018-12-19 07:53:45 -08008#include <memory>
9#include <utility>
10
11#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
Kun Yibd9bc002019-08-21 10:05:27 -070014namespace env
15{
16
17// Delegate all calls to getEnv() to the mock
18const char* EnvImpl::get(const char* key) const
19{
20 return mockEnv.get(key);
21}
22
23EnvImpl env_impl;
24
25} // namespace env
26
Patrick Venture0e2d68d2018-12-19 07:53:45 -080027class SensorTest : public ::testing::Test
28{
29 protected:
30 void SetUp() override
31 {
Patrick Venture99b95812018-12-19 08:59:15 -080032 gpioIntf = nullptr;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080033 }
Patrick Venture56c876f2019-03-06 08:50:15 -080034
35 std::string temp = "temp";
36 std::string five = "5";
Patrick Venture0e2d68d2018-12-19 07:53:45 -080037};
38
39using ::testing::Eq;
Patrick Venture99b95812018-12-19 08:59:15 -080040using ::testing::Invoke;
Patrick Venture56c876f2019-03-06 08:50:15 -080041using ::testing::Pair;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080042using ::testing::Return;
Patrick Venture56c876f2019-03-06 08:50:15 -080043using ::testing::StrEq;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080044using ::testing::StrictMock;
45
46TEST_F(SensorTest, BasicConstructorTest)
47{
48 /* Constructor test with nothing in an rcList or GPIO chip. */
Patrick Venture56c876f2019-03-06 08:50:15 -080049 auto sensorKey = std::make_pair(temp, five);
Patrick Venture0e2d68d2018-12-19 07:53:45 -080050 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
51 std::make_unique<hwmonio::HwmonIOMock>();
52 std::string path = "/";
53
54 /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
Kun Yibd9bc002019-08-21 10:05:27 -070055 EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
Patrick Venture0e2d68d2018-12-19 07:53:45 -080056 .WillOnce(Return(""));
Kun Yibd9bc002019-08-21 10:05:27 -070057 EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
Patrick Venture0e2d68d2018-12-19 07:53:45 -080058
59 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
Kun Yibd9bc002019-08-21 10:05:27 -070060 EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
61 EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
62 EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
Patrick Venture0e2d68d2018-12-19 07:53:45 -080063 .WillOnce(Return(""));
64
Patrick Williams02e598a2024-08-16 15:21:23 -040065 auto sensor =
66 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
Patrick Venture0e2d68d2018-12-19 07:53:45 -080067 EXPECT_FALSE(sensor == nullptr);
68}
Patrick Venture99b95812018-12-19 08:59:15 -080069
70TEST_F(SensorTest, SensorRequiresGpio)
71{
72 /* Constructor test with only the GPIO chip set. */
73
Patrick Venture99b95812018-12-19 08:59:15 -080074 StrictMock<GpioHandleMock> gMock;
75 gpioIntf = &gMock;
76
77 /* The following piece of code can probably be copied above once it's
78 * working.
79 */
80 auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
81
Patrick Venture56c876f2019-03-06 08:50:15 -080082 auto sensorKey = std::make_pair(temp, five);
Patrick Venture99b95812018-12-19 08:59:15 -080083 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
84 std::make_unique<hwmonio::HwmonIOMock>();
85 std::string path = "/";
86
Kun Yibd9bc002019-08-21 10:05:27 -070087 EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
Patrick Venture99b95812018-12-19 08:59:15 -080088 .WillOnce(Return("chipA"));
Kun Yibd9bc002019-08-21 10:05:27 -070089 EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("5"));
Patrick Venture99b95812018-12-19 08:59:15 -080090
Patrick Venture56c876f2019-03-06 08:50:15 -080091 EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5")))
Brad Bishop778f5c32019-10-07 15:48:15 -040092 .WillOnce(Invoke([&](const std::string&, const std::string&) {
Patrick Williams02e598a2024-08-16 15:21:23 -040093 return std::move(handleMock);
94 }));
Patrick Venture99b95812018-12-19 08:59:15 -080095
96 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
Kun Yibd9bc002019-08-21 10:05:27 -070097 EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
98 EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
99 EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
Patrick Venture99b95812018-12-19 08:59:15 -0800100 .WillOnce(Return(""));
101
Patrick Williams02e598a2024-08-16 15:21:23 -0400102 auto sensor =
103 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
Patrick Venture99b95812018-12-19 08:59:15 -0800104 EXPECT_FALSE(sensor == nullptr);
105}
Patrick Venturecd40c882019-01-24 14:06:44 -0800106
107TEST_F(SensorTest, SensorHasGainAndOffsetAdjustValue)
108{
109 /* Construct a sensor that has a gain and offset, then verify they are used
110 * when adjusting the value.
111 */
112
Patrick Venture56c876f2019-03-06 08:50:15 -0800113 auto sensorKey = std::make_pair(temp, five);
Patrick Venturecd40c882019-01-24 14:06:44 -0800114 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
115 std::make_unique<hwmonio::HwmonIOMock>();
116 std::string path = "/";
117
Kun Yibd9bc002019-08-21 10:05:27 -0700118 /* Always calls GPIOCHIP_temp5 and GPIO checks, returning empty string. */
119 EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
Patrick Venturecd40c882019-01-24 14:06:44 -0800120 .WillOnce(Return(""));
Kun Yibd9bc002019-08-21 10:05:27 -0700121 EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
Patrick Venturecd40c882019-01-24 14:06:44 -0800122
Kun Yibd9bc002019-08-21 10:05:27 -0700123 EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("10"));
124 EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5")))
Patrick Venturecd40c882019-01-24 14:06:44 -0800125 .WillOnce(Return("15"));
Kun Yibd9bc002019-08-21 10:05:27 -0700126 EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
Patrick Venturecd40c882019-01-24 14:06:44 -0800127 .WillOnce(Return(""));
128
Patrick Williams02e598a2024-08-16 15:21:23 -0400129 auto sensor =
130 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
Patrick Venturecd40c882019-01-24 14:06:44 -0800131 EXPECT_FALSE(sensor == nullptr);
132
133 double startingValue = 1.0;
134 double resultValue = sensor->adjustValue(startingValue);
135 EXPECT_DOUBLE_EQ(resultValue, 25.0);
136}