xref: /openbmc/phosphor-hwmon/test/sensor_unittest.cpp (revision 02e598ab445833e0ce615f88ac0aed7288a100e0)
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>
7e8771fd4SPatrick Williams 
80e2d68d2SPatrick Venture #include <memory>
90e2d68d2SPatrick Venture #include <utility>
100e2d68d2SPatrick Venture 
110e2d68d2SPatrick Venture #include <gmock/gmock.h>
120e2d68d2SPatrick Venture #include <gtest/gtest.h>
130e2d68d2SPatrick Venture 
14bd9bc00bSKun Yi namespace env
15bd9bc00bSKun Yi {
16bd9bc00bSKun Yi 
17bd9bc00bSKun Yi // Delegate all calls to getEnv() to the mock
get(const char * key) const18bd9bc00bSKun Yi const char* EnvImpl::get(const char* key) const
19bd9bc00bSKun Yi {
20bd9bc00bSKun Yi     return mockEnv.get(key);
21bd9bc00bSKun Yi }
22bd9bc00bSKun Yi 
23bd9bc00bSKun Yi EnvImpl env_impl;
24bd9bc00bSKun Yi 
25bd9bc00bSKun Yi } // namespace env
26bd9bc00bSKun Yi 
270e2d68d2SPatrick Venture class SensorTest : public ::testing::Test
280e2d68d2SPatrick Venture {
290e2d68d2SPatrick Venture   protected:
SetUp()300e2d68d2SPatrick Venture     void SetUp() override
310e2d68d2SPatrick Venture     {
3299b95815SPatrick Venture         gpioIntf = nullptr;
330e2d68d2SPatrick Venture     }
3456c876f3SPatrick Venture 
3556c876f3SPatrick Venture     std::string temp = "temp";
3656c876f3SPatrick Venture     std::string five = "5";
370e2d68d2SPatrick Venture };
380e2d68d2SPatrick Venture 
390e2d68d2SPatrick Venture using ::testing::Eq;
4099b95815SPatrick Venture using ::testing::Invoke;
4156c876f3SPatrick Venture using ::testing::Pair;
420e2d68d2SPatrick Venture using ::testing::Return;
4356c876f3SPatrick Venture using ::testing::StrEq;
440e2d68d2SPatrick Venture using ::testing::StrictMock;
450e2d68d2SPatrick Venture 
TEST_F(SensorTest,BasicConstructorTest)460e2d68d2SPatrick Venture TEST_F(SensorTest, BasicConstructorTest)
470e2d68d2SPatrick Venture {
480e2d68d2SPatrick Venture     /* Constructor test with nothing in an rcList or GPIO chip. */
4956c876f3SPatrick Venture     auto sensorKey = std::make_pair(temp, five);
500e2d68d2SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
510e2d68d2SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
520e2d68d2SPatrick Venture     std::string path = "/";
530e2d68d2SPatrick Venture 
540e2d68d2SPatrick Venture     /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
55bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
560e2d68d2SPatrick Venture         .WillOnce(Return(""));
57bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
580e2d68d2SPatrick Venture 
590e2d68d2SPatrick Venture     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
60bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
61bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
62bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
630e2d68d2SPatrick Venture         .WillOnce(Return(""));
640e2d68d2SPatrick Venture 
65*02e598abSPatrick Williams     auto sensor =
66*02e598abSPatrick Williams         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
670e2d68d2SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
680e2d68d2SPatrick Venture }
6999b95815SPatrick Venture 
TEST_F(SensorTest,SensorRequiresGpio)7099b95815SPatrick Venture TEST_F(SensorTest, SensorRequiresGpio)
7199b95815SPatrick Venture {
7299b95815SPatrick Venture     /* Constructor test with only the GPIO chip set. */
7399b95815SPatrick Venture 
7499b95815SPatrick Venture     StrictMock<GpioHandleMock> gMock;
7599b95815SPatrick Venture     gpioIntf = &gMock;
7699b95815SPatrick Venture 
7799b95815SPatrick Venture     /* The following piece of code can probably be copied above once it's
7899b95815SPatrick Venture      * working.
7999b95815SPatrick Venture      */
8099b95815SPatrick Venture     auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
8199b95815SPatrick Venture 
8256c876f3SPatrick Venture     auto sensorKey = std::make_pair(temp, five);
8399b95815SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
8499b95815SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
8599b95815SPatrick Venture     std::string path = "/";
8699b95815SPatrick Venture 
87bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
8899b95815SPatrick Venture         .WillOnce(Return("chipA"));
89bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("5"));
9099b95815SPatrick Venture 
9156c876f3SPatrick Venture     EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5")))
92778f5c39SBrad Bishop         .WillOnce(Invoke([&](const std::string&, const std::string&) {
9399b95815SPatrick Venture             return std::move(handleMock);
9499b95815SPatrick Venture         }));
9599b95815SPatrick Venture 
9699b95815SPatrick Venture     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
97bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
98bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
99bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
10099b95815SPatrick Venture         .WillOnce(Return(""));
10199b95815SPatrick Venture 
102*02e598abSPatrick Williams     auto sensor =
103*02e598abSPatrick Williams         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
10499b95815SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
10599b95815SPatrick Venture }
106cd40c881SPatrick Venture 
TEST_F(SensorTest,SensorHasGainAndOffsetAdjustValue)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 
11356c876f3SPatrick Venture     auto sensorKey = std::make_pair(temp, five);
114cd40c881SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
115cd40c881SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
116cd40c881SPatrick Venture     std::string path = "/";
117cd40c881SPatrick Venture 
118bd9bc00bSKun Yi     /* Always calls GPIOCHIP_temp5 and GPIO checks, returning empty string. */
119bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
120cd40c881SPatrick Venture         .WillOnce(Return(""));
121bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
122cd40c881SPatrick Venture 
123bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("10"));
124bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5")))
125cd40c881SPatrick Venture         .WillOnce(Return("15"));
126bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
127cd40c881SPatrick Venture         .WillOnce(Return(""));
128cd40c881SPatrick Venture 
129*02e598abSPatrick Williams     auto sensor =
130*02e598abSPatrick Williams         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
131cd40c881SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
132cd40c881SPatrick Venture 
133cd40c881SPatrick Venture     double startingValue = 1.0;
134cd40c881SPatrick Venture     double resultValue = sensor->adjustValue(startingValue);
135cd40c881SPatrick Venture     EXPECT_DOUBLE_EQ(resultValue, 25.0);
136cd40c881SPatrick Venture }
137