blob: 8271b54bf3d3e56fd6f1f6a29ecd8e1c48f853fb [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 Venture0e2d68d2018-12-19 07:53:45 -08007#include <memory>
8#include <utility>
9
10#include <gmock/gmock.h>
11#include <gtest/gtest.h>
12
13class SensorTest : public ::testing::Test
14{
15 protected:
16 void SetUp() override
17 {
18 envIntf = nullptr;
Patrick Venture99b95812018-12-19 08:59:15 -080019 gpioIntf = nullptr;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080020 }
Patrick Venture56c876f2019-03-06 08:50:15 -080021
22 std::string temp = "temp";
23 std::string five = "5";
Patrick Venture0e2d68d2018-12-19 07:53:45 -080024};
25
26using ::testing::Eq;
Patrick Venture99b95812018-12-19 08:59:15 -080027using ::testing::Invoke;
Patrick Venture56c876f2019-03-06 08:50:15 -080028using ::testing::Pair;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080029using ::testing::Return;
Patrick Venture56c876f2019-03-06 08:50:15 -080030using ::testing::StrEq;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080031using ::testing::StrictMock;
32
33TEST_F(SensorTest, BasicConstructorTest)
34{
35 /* Constructor test with nothing in an rcList or GPIO chip. */
36
37 StrictMock<EnvMock> eMock;
38 envIntf = &eMock;
39
Patrick Venture56c876f2019-03-06 08:50:15 -080040 auto sensorKey = std::make_pair(temp, five);
Patrick Venture0e2d68d2018-12-19 07:53:45 -080041 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
42 std::make_unique<hwmonio::HwmonIOMock>();
43 std::string path = "/";
44
45 /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
Patrick Venture56c876f2019-03-06 08:50:15 -080046 EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five)))
Patrick Venture0e2d68d2018-12-19 07:53:45 -080047 .WillOnce(Return(""));
Patrick Venture56c876f2019-03-06 08:50:15 -080048 EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five)))
49 .WillOnce(Return(""));
Patrick Venture0e2d68d2018-12-19 07:53:45 -080050
51 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
Patrick Venture56c876f2019-03-06 08:50:15 -080052 EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five)))
Patrick Venture0e2d68d2018-12-19 07:53:45 -080053 .WillOnce(Return(""));
Patrick Venture56c876f2019-03-06 08:50:15 -080054 EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five)))
55 .WillOnce(Return(""));
56 EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five)))
Patrick Venture0e2d68d2018-12-19 07:53:45 -080057 .WillOnce(Return(""));
58
59 auto sensor =
60 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
61 EXPECT_FALSE(sensor == nullptr);
62}
Patrick Venture99b95812018-12-19 08:59:15 -080063
64TEST_F(SensorTest, SensorRequiresGpio)
65{
66 /* Constructor test with only the GPIO chip set. */
67
68 StrictMock<EnvMock> eMock;
69 envIntf = &eMock;
70
71 StrictMock<GpioHandleMock> gMock;
72 gpioIntf = &gMock;
73
74 /* The following piece of code can probably be copied above once it's
75 * working.
76 */
77 auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
78
Patrick Venture56c876f2019-03-06 08:50:15 -080079 auto sensorKey = std::make_pair(temp, five);
Patrick Venture99b95812018-12-19 08:59:15 -080080 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
81 std::make_unique<hwmonio::HwmonIOMock>();
82 std::string path = "/";
83
Patrick Venture56c876f2019-03-06 08:50:15 -080084 EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five)))
Patrick Venture99b95812018-12-19 08:59:15 -080085 .WillOnce(Return("chipA"));
Patrick Venture56c876f2019-03-06 08:50:15 -080086 EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five)))
87 .WillOnce(Return("5"));
Patrick Venture99b95812018-12-19 08:59:15 -080088
Patrick Venture56c876f2019-03-06 08:50:15 -080089 EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5")))
Patrick Venture99b95812018-12-19 08:59:15 -080090 .WillOnce(Invoke([&](const std::string& chip, const std::string& line) {
91 return std::move(handleMock);
92 }));
93
94 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
Patrick Venture56c876f2019-03-06 08:50:15 -080095 EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five)))
Patrick Venture99b95812018-12-19 08:59:15 -080096 .WillOnce(Return(""));
Patrick Venture56c876f2019-03-06 08:50:15 -080097 EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five)))
98 .WillOnce(Return(""));
99 EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five)))
Patrick Venture99b95812018-12-19 08:59:15 -0800100 .WillOnce(Return(""));
101
102 auto sensor =
103 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
104 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
113 StrictMock<EnvMock> eMock;
114 envIntf = &eMock;
115
Patrick Venture56c876f2019-03-06 08:50:15 -0800116 auto sensorKey = std::make_pair(temp, five);
Patrick Venturecd40c882019-01-24 14:06:44 -0800117 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
118 std::make_unique<hwmonio::HwmonIOMock>();
119 std::string path = "/";
120
121 /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
Patrick Venture56c876f2019-03-06 08:50:15 -0800122 EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five)))
Patrick Venturecd40c882019-01-24 14:06:44 -0800123 .WillOnce(Return(""));
Patrick Venture56c876f2019-03-06 08:50:15 -0800124 EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five)))
125 .WillOnce(Return(""));
Patrick Venturecd40c882019-01-24 14:06:44 -0800126
Patrick Venture56c876f2019-03-06 08:50:15 -0800127 EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five)))
Patrick Venturecd40c882019-01-24 14:06:44 -0800128 .WillOnce(Return("10"));
Patrick Venture56c876f2019-03-06 08:50:15 -0800129 EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five)))
Patrick Venturecd40c882019-01-24 14:06:44 -0800130 .WillOnce(Return("15"));
Patrick Venture56c876f2019-03-06 08:50:15 -0800131 EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five)))
Patrick Venturecd40c882019-01-24 14:06:44 -0800132 .WillOnce(Return(""));
133
134 auto sensor =
135 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
136 EXPECT_FALSE(sensor == nullptr);
137
138 double startingValue = 1.0;
139 double resultValue = sensor->adjustValue(startingValue);
140 EXPECT_DOUBLE_EQ(resultValue, 25.0);
141}