xref: /openbmc/phosphor-hwmon/test/sensor_unittest.cpp (revision 56c876f3a108f4f4fdbe13898ac3ba47b98b9a20)
10e2d68d2SPatrick Venture #include "env_mock.hpp"
299b95815SPatrick Venture #include "gpio_mock.hpp"
30e2d68d2SPatrick Venture #include "hwmonio_mock.hpp"
40e2d68d2SPatrick Venture #include "sensor.hpp"
50e2d68d2SPatrick Venture 
699b95815SPatrick Venture #include <gpioplus/test/handle.hpp>
70e2d68d2SPatrick Venture #include <memory>
80e2d68d2SPatrick Venture #include <utility>
90e2d68d2SPatrick Venture 
100e2d68d2SPatrick Venture #include <gmock/gmock.h>
110e2d68d2SPatrick Venture #include <gtest/gtest.h>
120e2d68d2SPatrick Venture 
130e2d68d2SPatrick Venture class SensorTest : public ::testing::Test
140e2d68d2SPatrick Venture {
150e2d68d2SPatrick Venture   protected:
160e2d68d2SPatrick Venture     void SetUp() override
170e2d68d2SPatrick Venture     {
180e2d68d2SPatrick Venture         envIntf = nullptr;
1999b95815SPatrick Venture         gpioIntf = nullptr;
200e2d68d2SPatrick Venture     }
21*56c876f3SPatrick Venture 
22*56c876f3SPatrick Venture     std::string temp = "temp";
23*56c876f3SPatrick Venture     std::string five = "5";
240e2d68d2SPatrick Venture };
250e2d68d2SPatrick Venture 
260e2d68d2SPatrick Venture using ::testing::Eq;
2799b95815SPatrick Venture using ::testing::Invoke;
28*56c876f3SPatrick Venture using ::testing::Pair;
290e2d68d2SPatrick Venture using ::testing::Return;
30*56c876f3SPatrick Venture using ::testing::StrEq;
310e2d68d2SPatrick Venture using ::testing::StrictMock;
320e2d68d2SPatrick Venture 
330e2d68d2SPatrick Venture TEST_F(SensorTest, BasicConstructorTest)
340e2d68d2SPatrick Venture {
350e2d68d2SPatrick Venture     /* Constructor test with nothing in an rcList or GPIO chip. */
360e2d68d2SPatrick Venture 
370e2d68d2SPatrick Venture     StrictMock<EnvMock> eMock;
380e2d68d2SPatrick Venture     envIntf = &eMock;
390e2d68d2SPatrick Venture 
40*56c876f3SPatrick Venture     auto sensorKey = std::make_pair(temp, five);
410e2d68d2SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
420e2d68d2SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
430e2d68d2SPatrick Venture     std::string path = "/";
440e2d68d2SPatrick Venture 
450e2d68d2SPatrick Venture     /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
46*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five)))
470e2d68d2SPatrick Venture         .WillOnce(Return(""));
48*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five)))
49*56c876f3SPatrick Venture         .WillOnce(Return(""));
500e2d68d2SPatrick Venture 
510e2d68d2SPatrick Venture     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
52*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five)))
530e2d68d2SPatrick Venture         .WillOnce(Return(""));
54*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five)))
55*56c876f3SPatrick Venture         .WillOnce(Return(""));
56*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five)))
570e2d68d2SPatrick Venture         .WillOnce(Return(""));
580e2d68d2SPatrick Venture 
590e2d68d2SPatrick Venture     auto sensor =
600e2d68d2SPatrick Venture         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
610e2d68d2SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
620e2d68d2SPatrick Venture }
6399b95815SPatrick Venture 
6499b95815SPatrick Venture TEST_F(SensorTest, SensorRequiresGpio)
6599b95815SPatrick Venture {
6699b95815SPatrick Venture     /* Constructor test with only the GPIO chip set. */
6799b95815SPatrick Venture 
6899b95815SPatrick Venture     StrictMock<EnvMock> eMock;
6999b95815SPatrick Venture     envIntf = &eMock;
7099b95815SPatrick Venture 
7199b95815SPatrick Venture     StrictMock<GpioHandleMock> gMock;
7299b95815SPatrick Venture     gpioIntf = &gMock;
7399b95815SPatrick Venture 
7499b95815SPatrick Venture     /* The following piece of code can probably be copied above once it's
7599b95815SPatrick Venture      * working.
7699b95815SPatrick Venture      */
7799b95815SPatrick Venture     auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
7899b95815SPatrick Venture 
79*56c876f3SPatrick Venture     auto sensorKey = std::make_pair(temp, five);
8099b95815SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
8199b95815SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
8299b95815SPatrick Venture     std::string path = "/";
8399b95815SPatrick Venture 
84*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five)))
8599b95815SPatrick Venture         .WillOnce(Return("chipA"));
86*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five)))
87*56c876f3SPatrick Venture         .WillOnce(Return("5"));
8899b95815SPatrick Venture 
89*56c876f3SPatrick Venture     EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5")))
9099b95815SPatrick Venture         .WillOnce(Invoke([&](const std::string& chip, const std::string& line) {
9199b95815SPatrick Venture             return std::move(handleMock);
9299b95815SPatrick Venture         }));
9399b95815SPatrick Venture 
9499b95815SPatrick Venture     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
95*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five)))
9699b95815SPatrick Venture         .WillOnce(Return(""));
97*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five)))
98*56c876f3SPatrick Venture         .WillOnce(Return(""));
99*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five)))
10099b95815SPatrick Venture         .WillOnce(Return(""));
10199b95815SPatrick Venture 
10299b95815SPatrick Venture     auto sensor =
10399b95815SPatrick Venture         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
10499b95815SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
10599b95815SPatrick Venture }
106cd40c881SPatrick Venture 
107cd40c881SPatrick Venture TEST_F(SensorTest, SensorHasGainAndOffsetAdjustValue)
108cd40c881SPatrick Venture {
109cd40c881SPatrick Venture     /* Construct a sensor that has a gain and offset, then verify they are used
110cd40c881SPatrick Venture      * when adjusting the value.
111cd40c881SPatrick Venture      */
112cd40c881SPatrick Venture 
113cd40c881SPatrick Venture     StrictMock<EnvMock> eMock;
114cd40c881SPatrick Venture     envIntf = &eMock;
115cd40c881SPatrick Venture 
116*56c876f3SPatrick Venture     auto sensorKey = std::make_pair(temp, five);
117cd40c881SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
118cd40c881SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
119cd40c881SPatrick Venture     std::string path = "/";
120cd40c881SPatrick Venture 
121cd40c881SPatrick Venture     /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
122*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("GPIOCHIP"), Pair(temp, five)))
123cd40c881SPatrick Venture         .WillOnce(Return(""));
124*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("GPIO"), Pair(temp, five)))
125*56c876f3SPatrick Venture         .WillOnce(Return(""));
126cd40c881SPatrick Venture 
127*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("GAIN"), Pair(temp, five)))
128cd40c881SPatrick Venture         .WillOnce(Return("10"));
129*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("OFFSET"), Pair(temp, five)))
130cd40c881SPatrick Venture         .WillOnce(Return("15"));
131*56c876f3SPatrick Venture     EXPECT_CALL(eMock, getEnv(StrEq("REMOVERCS"), Pair(temp, five)))
132cd40c881SPatrick Venture         .WillOnce(Return(""));
133cd40c881SPatrick Venture 
134cd40c881SPatrick Venture     auto sensor =
135cd40c881SPatrick Venture         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
136cd40c881SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
137cd40c881SPatrick Venture 
138cd40c881SPatrick Venture     double startingValue = 1.0;
139cd40c881SPatrick Venture     double resultValue = sensor->adjustValue(startingValue);
140cd40c881SPatrick Venture     EXPECT_DOUBLE_EQ(resultValue, 25.0);
141cd40c881SPatrick Venture }
142