xref: /openbmc/phosphor-hwmon/test/sensor_unittest.cpp (revision 778f5c39b1e53ee291f10a6d4d13d8fb16bbc112)
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 
13bd9bc00bSKun Yi namespace env
14bd9bc00bSKun Yi {
15bd9bc00bSKun Yi 
16bd9bc00bSKun Yi // Delegate all calls to getEnv() to the mock
17bd9bc00bSKun Yi const char* EnvImpl::get(const char* key) const
18bd9bc00bSKun Yi {
19bd9bc00bSKun Yi     return mockEnv.get(key);
20bd9bc00bSKun Yi }
21bd9bc00bSKun Yi 
22bd9bc00bSKun Yi EnvImpl env_impl;
23bd9bc00bSKun Yi 
24bd9bc00bSKun Yi } // namespace env
25bd9bc00bSKun Yi 
260e2d68d2SPatrick Venture class SensorTest : public ::testing::Test
270e2d68d2SPatrick Venture {
280e2d68d2SPatrick Venture   protected:
290e2d68d2SPatrick Venture     void SetUp() override
300e2d68d2SPatrick Venture     {
3199b95815SPatrick Venture         gpioIntf = nullptr;
320e2d68d2SPatrick Venture     }
3356c876f3SPatrick Venture 
3456c876f3SPatrick Venture     std::string temp = "temp";
3556c876f3SPatrick Venture     std::string five = "5";
360e2d68d2SPatrick Venture };
370e2d68d2SPatrick Venture 
380e2d68d2SPatrick Venture using ::testing::Eq;
3999b95815SPatrick Venture using ::testing::Invoke;
4056c876f3SPatrick Venture using ::testing::Pair;
410e2d68d2SPatrick Venture using ::testing::Return;
4256c876f3SPatrick Venture using ::testing::StrEq;
430e2d68d2SPatrick Venture using ::testing::StrictMock;
440e2d68d2SPatrick Venture 
450e2d68d2SPatrick Venture TEST_F(SensorTest, BasicConstructorTest)
460e2d68d2SPatrick Venture {
470e2d68d2SPatrick Venture     /* Constructor test with nothing in an rcList or GPIO chip. */
4856c876f3SPatrick Venture     auto sensorKey = std::make_pair(temp, five);
490e2d68d2SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
500e2d68d2SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
510e2d68d2SPatrick Venture     std::string path = "/";
520e2d68d2SPatrick Venture 
530e2d68d2SPatrick Venture     /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
54bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
550e2d68d2SPatrick Venture         .WillOnce(Return(""));
56bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
570e2d68d2SPatrick Venture 
580e2d68d2SPatrick Venture     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
59bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
60bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
61bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
620e2d68d2SPatrick Venture         .WillOnce(Return(""));
630e2d68d2SPatrick Venture 
640e2d68d2SPatrick Venture     auto sensor =
650e2d68d2SPatrick Venture         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
660e2d68d2SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
670e2d68d2SPatrick Venture }
6899b95815SPatrick Venture 
6999b95815SPatrick Venture TEST_F(SensorTest, SensorRequiresGpio)
7099b95815SPatrick Venture {
7199b95815SPatrick Venture     /* Constructor test with only the GPIO chip set. */
7299b95815SPatrick Venture 
7399b95815SPatrick Venture     StrictMock<GpioHandleMock> gMock;
7499b95815SPatrick Venture     gpioIntf = &gMock;
7599b95815SPatrick Venture 
7699b95815SPatrick Venture     /* The following piece of code can probably be copied above once it's
7799b95815SPatrick Venture      * working.
7899b95815SPatrick Venture      */
7999b95815SPatrick Venture     auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
8099b95815SPatrick Venture 
8156c876f3SPatrick Venture     auto sensorKey = std::make_pair(temp, five);
8299b95815SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
8399b95815SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
8499b95815SPatrick Venture     std::string path = "/";
8599b95815SPatrick Venture 
86bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
8799b95815SPatrick Venture         .WillOnce(Return("chipA"));
88bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("5"));
8999b95815SPatrick Venture 
9056c876f3SPatrick Venture     EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5")))
91*778f5c39SBrad Bishop         .WillOnce(Invoke([&](const std::string&, const std::string&) {
9299b95815SPatrick Venture             return std::move(handleMock);
9399b95815SPatrick Venture         }));
9499b95815SPatrick Venture 
9599b95815SPatrick Venture     /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
96bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
97bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
98bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
9999b95815SPatrick Venture         .WillOnce(Return(""));
10099b95815SPatrick Venture 
10199b95815SPatrick Venture     auto sensor =
10299b95815SPatrick Venture         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
10399b95815SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
10499b95815SPatrick Venture }
105cd40c881SPatrick Venture 
106cd40c881SPatrick Venture TEST_F(SensorTest, SensorHasGainAndOffsetAdjustValue)
107cd40c881SPatrick Venture {
108cd40c881SPatrick Venture     /* Construct a sensor that has a gain and offset, then verify they are used
109cd40c881SPatrick Venture      * when adjusting the value.
110cd40c881SPatrick Venture      */
111cd40c881SPatrick Venture 
11256c876f3SPatrick Venture     auto sensorKey = std::make_pair(temp, five);
113cd40c881SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
114cd40c881SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
115cd40c881SPatrick Venture     std::string path = "/";
116cd40c881SPatrick Venture 
117bd9bc00bSKun Yi     /* Always calls GPIOCHIP_temp5 and GPIO checks, returning empty string. */
118bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
119cd40c881SPatrick Venture         .WillOnce(Return(""));
120bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
121cd40c881SPatrick Venture 
122bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("10"));
123bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5")))
124cd40c881SPatrick Venture         .WillOnce(Return("15"));
125bd9bc00bSKun Yi     EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
126cd40c881SPatrick Venture         .WillOnce(Return(""));
127cd40c881SPatrick Venture 
128cd40c881SPatrick Venture     auto sensor =
129cd40c881SPatrick Venture         std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
130cd40c881SPatrick Venture     EXPECT_FALSE(sensor == nullptr);
131cd40c881SPatrick Venture 
132cd40c881SPatrick Venture     double startingValue = 1.0;
133cd40c881SPatrick Venture     double resultValue = sensor->adjustValue(startingValue);
134cd40c881SPatrick Venture     EXPECT_DOUBLE_EQ(resultValue, 25.0);
135cd40c881SPatrick Venture }
136