blob: 15a480762444b5ecb9e098980d23bbdc2df2e01f [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
Kun Yibd9bc002019-08-21 10:05:27 -070013namespace env
14{
15
16// Delegate all calls to getEnv() to the mock
17const char* EnvImpl::get(const char* key) const
18{
19 return mockEnv.get(key);
20}
21
22EnvImpl env_impl;
23
24} // namespace env
25
Patrick Venture0e2d68d2018-12-19 07:53:45 -080026class SensorTest : public ::testing::Test
27{
28 protected:
29 void SetUp() override
30 {
Patrick Venture99b95812018-12-19 08:59:15 -080031 gpioIntf = nullptr;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080032 }
Patrick Venture56c876f2019-03-06 08:50:15 -080033
34 std::string temp = "temp";
35 std::string five = "5";
Patrick Venture0e2d68d2018-12-19 07:53:45 -080036};
37
38using ::testing::Eq;
Patrick Venture99b95812018-12-19 08:59:15 -080039using ::testing::Invoke;
Patrick Venture56c876f2019-03-06 08:50:15 -080040using ::testing::Pair;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080041using ::testing::Return;
Patrick Venture56c876f2019-03-06 08:50:15 -080042using ::testing::StrEq;
Patrick Venture0e2d68d2018-12-19 07:53:45 -080043using ::testing::StrictMock;
44
45TEST_F(SensorTest, BasicConstructorTest)
46{
47 /* Constructor test with nothing in an rcList or GPIO chip. */
Patrick Venture56c876f2019-03-06 08:50:15 -080048 auto sensorKey = std::make_pair(temp, five);
Patrick Venture0e2d68d2018-12-19 07:53:45 -080049 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
50 std::make_unique<hwmonio::HwmonIOMock>();
51 std::string path = "/";
52
53 /* Always calls GPIOCHIP and GPIO checks, returning empty string. */
Kun Yibd9bc002019-08-21 10:05:27 -070054 EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
Patrick Venture0e2d68d2018-12-19 07:53:45 -080055 .WillOnce(Return(""));
Kun Yibd9bc002019-08-21 10:05:27 -070056 EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
Patrick Venture0e2d68d2018-12-19 07:53:45 -080057
58 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
Kun Yibd9bc002019-08-21 10:05:27 -070059 EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
60 EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
61 EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
Patrick Venture0e2d68d2018-12-19 07:53:45 -080062 .WillOnce(Return(""));
63
64 auto sensor =
65 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
66 EXPECT_FALSE(sensor == nullptr);
67}
Patrick Venture99b95812018-12-19 08:59:15 -080068
69TEST_F(SensorTest, SensorRequiresGpio)
70{
71 /* Constructor test with only the GPIO chip set. */
72
Patrick Venture99b95812018-12-19 08:59:15 -080073 StrictMock<GpioHandleMock> gMock;
74 gpioIntf = &gMock;
75
76 /* The following piece of code can probably be copied above once it's
77 * working.
78 */
79 auto handleMock = std::make_unique<gpioplus::test::HandleMock>();
80
Patrick Venture56c876f2019-03-06 08:50:15 -080081 auto sensorKey = std::make_pair(temp, five);
Patrick Venture99b95812018-12-19 08:59:15 -080082 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
83 std::make_unique<hwmonio::HwmonIOMock>();
84 std::string path = "/";
85
Kun Yibd9bc002019-08-21 10:05:27 -070086 EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
Patrick Venture99b95812018-12-19 08:59:15 -080087 .WillOnce(Return("chipA"));
Kun Yibd9bc002019-08-21 10:05:27 -070088 EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("5"));
Patrick Venture99b95812018-12-19 08:59:15 -080089
Patrick Venture56c876f2019-03-06 08:50:15 -080090 EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5")))
Brad Bishop778f5c32019-10-07 15:48:15 -040091 .WillOnce(Invoke([&](const std::string&, const std::string&) {
Patrick Venture99b95812018-12-19 08:59:15 -080092 return std::move(handleMock);
93 }));
94
95 /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */
Kun Yibd9bc002019-08-21 10:05:27 -070096 EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return(""));
97 EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return(""));
98 EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
Patrick Venture99b95812018-12-19 08:59:15 -080099 .WillOnce(Return(""));
100
101 auto sensor =
102 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
103 EXPECT_FALSE(sensor == nullptr);
104}
Patrick Venturecd40c882019-01-24 14:06:44 -0800105
106TEST_F(SensorTest, SensorHasGainAndOffsetAdjustValue)
107{
108 /* Construct a sensor that has a gain and offset, then verify they are used
109 * when adjusting the value.
110 */
111
Patrick Venture56c876f2019-03-06 08:50:15 -0800112 auto sensorKey = std::make_pair(temp, five);
Patrick Venturecd40c882019-01-24 14:06:44 -0800113 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
114 std::make_unique<hwmonio::HwmonIOMock>();
115 std::string path = "/";
116
Kun Yibd9bc002019-08-21 10:05:27 -0700117 /* Always calls GPIOCHIP_temp5 and GPIO checks, returning empty string. */
118 EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5")))
Patrick Venturecd40c882019-01-24 14:06:44 -0800119 .WillOnce(Return(""));
Kun Yibd9bc002019-08-21 10:05:27 -0700120 EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return(""));
Patrick Venturecd40c882019-01-24 14:06:44 -0800121
Kun Yibd9bc002019-08-21 10:05:27 -0700122 EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("10"));
123 EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5")))
Patrick Venturecd40c882019-01-24 14:06:44 -0800124 .WillOnce(Return("15"));
Kun Yibd9bc002019-08-21 10:05:27 -0700125 EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5")))
Patrick Venturecd40c882019-01-24 14:06:44 -0800126 .WillOnce(Return(""));
127
128 auto sensor =
129 std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path);
130 EXPECT_FALSE(sensor == nullptr);
131
132 double startingValue = 1.0;
133 double resultValue = sensor->adjustValue(startingValue);
134 EXPECT_DOUBLE_EQ(resultValue, 25.0);
135}