Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 1 | #include "env_mock.hpp" |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 2 | #include "gpio_mock.hpp" |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 3 | #include "hwmonio_mock.hpp" |
| 4 | #include "sensor.hpp" |
| 5 | |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 6 | #include <gpioplus/test/handle.hpp> |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 7 | |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 14 | namespace env |
| 15 | { |
| 16 | |
| 17 | // Delegate all calls to getEnv() to the mock |
| 18 | const char* EnvImpl::get(const char* key) const |
| 19 | { |
| 20 | return mockEnv.get(key); |
| 21 | } |
| 22 | |
| 23 | EnvImpl env_impl; |
| 24 | |
| 25 | } // namespace env |
| 26 | |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 27 | class SensorTest : public ::testing::Test |
| 28 | { |
| 29 | protected: |
| 30 | void SetUp() override |
| 31 | { |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 32 | gpioIntf = nullptr; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 33 | } |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 34 | |
| 35 | std::string temp = "temp"; |
| 36 | std::string five = "5"; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | using ::testing::Eq; |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 40 | using ::testing::Invoke; |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 41 | using ::testing::Pair; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 42 | using ::testing::Return; |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 43 | using ::testing::StrEq; |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 44 | using ::testing::StrictMock; |
| 45 | |
| 46 | TEST_F(SensorTest, BasicConstructorTest) |
| 47 | { |
| 48 | /* Constructor test with nothing in an rcList or GPIO chip. */ |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 49 | auto sensorKey = std::make_pair(temp, five); |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 50 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 51 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 52 | std::string path = "/"; |
| 53 | |
| 54 | /* Always calls GPIOCHIP and GPIO checks, returning empty string. */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 55 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5"))) |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 56 | .WillOnce(Return("")); |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 57 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("")); |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 58 | |
| 59 | /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 60 | EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("")); |
| 61 | EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return("")); |
| 62 | EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5"))) |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 63 | .WillOnce(Return("")); |
| 64 | |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame^] | 65 | auto sensor = |
| 66 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
Patrick Venture | 0e2d68d | 2018-12-19 07:53:45 -0800 | [diff] [blame] | 67 | EXPECT_FALSE(sensor == nullptr); |
| 68 | } |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 69 | |
| 70 | TEST_F(SensorTest, SensorRequiresGpio) |
| 71 | { |
| 72 | /* Constructor test with only the GPIO chip set. */ |
| 73 | |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 74 | StrictMock<GpioHandleMock> gMock; |
| 75 | gpioIntf = &gMock; |
| 76 | |
| 77 | /* The following piece of code can probably be copied above once it's |
| 78 | * working. |
| 79 | */ |
| 80 | auto handleMock = std::make_unique<gpioplus::test::HandleMock>(); |
| 81 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 82 | auto sensorKey = std::make_pair(temp, five); |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 83 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 84 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 85 | std::string path = "/"; |
| 86 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 87 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5"))) |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 88 | .WillOnce(Return("chipA")); |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 89 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("5")); |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 90 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 91 | EXPECT_CALL(gMock, build(StrEq("chipA"), StrEq("5"))) |
Brad Bishop | 778f5c3 | 2019-10-07 15:48:15 -0400 | [diff] [blame] | 92 | .WillOnce(Invoke([&](const std::string&, const std::string&) { |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame^] | 93 | return std::move(handleMock); |
| 94 | })); |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 95 | |
| 96 | /* Always calls GAIN and OFFSET, can use ON_CALL instead of EXPECT_CALL */ |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 97 | EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("")); |
| 98 | EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))).WillOnce(Return("")); |
| 99 | EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5"))) |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 100 | .WillOnce(Return("")); |
| 101 | |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame^] | 102 | auto sensor = |
| 103 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
Patrick Venture | 99b9581 | 2018-12-19 08:59:15 -0800 | [diff] [blame] | 104 | EXPECT_FALSE(sensor == nullptr); |
| 105 | } |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 106 | |
| 107 | TEST_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 | |
Patrick Venture | 56c876f | 2019-03-06 08:50:15 -0800 | [diff] [blame] | 113 | auto sensorKey = std::make_pair(temp, five); |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 114 | std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock = |
| 115 | std::make_unique<hwmonio::HwmonIOMock>(); |
| 116 | std::string path = "/"; |
| 117 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 118 | /* Always calls GPIOCHIP_temp5 and GPIO checks, returning empty string. */ |
| 119 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIOCHIP_temp5"))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 120 | .WillOnce(Return("")); |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 121 | EXPECT_CALL(env::mockEnv, get(StrEq("GPIO_temp5"))).WillOnce(Return("")); |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 122 | |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 123 | EXPECT_CALL(env::mockEnv, get(StrEq("GAIN_temp5"))).WillOnce(Return("10")); |
| 124 | EXPECT_CALL(env::mockEnv, get(StrEq("OFFSET_temp5"))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 125 | .WillOnce(Return("15")); |
Kun Yi | bd9bc00 | 2019-08-21 10:05:27 -0700 | [diff] [blame] | 126 | EXPECT_CALL(env::mockEnv, get(StrEq("REMOVERCS_temp5"))) |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 127 | .WillOnce(Return("")); |
| 128 | |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame^] | 129 | auto sensor = |
| 130 | std::make_unique<sensor::Sensor>(sensorKey, hwmonio_mock.get(), path); |
Patrick Venture | cd40c88 | 2019-01-24 14:06:44 -0800 | [diff] [blame] | 131 | EXPECT_FALSE(sensor == nullptr); |
| 132 | |
| 133 | double startingValue = 1.0; |
| 134 | double resultValue = sensor->adjustValue(startingValue); |
| 135 | EXPECT_DOUBLE_EQ(resultValue, 25.0); |
| 136 | } |