xref: /openbmc/phosphor-hwmon/test/fanpwm_unittest.cpp (revision 02e598ab445833e0ce615f88ac0aed7288a100e0)
150552377SPatrick Venture #include "fan_pwm.hpp"
250552377SPatrick Venture #include "hwmonio_mock.hpp"
350552377SPatrick Venture 
4043d3230SPatrick Venture #include <sdbusplus/test/sdbus_mock.hpp>
5*e8771fd4SPatrick Williams 
6043d3230SPatrick Venture #include <string>
7043d3230SPatrick Venture 
850552377SPatrick Venture #include <gmock/gmock.h>
950552377SPatrick Venture #include <gtest/gtest.h>
1050552377SPatrick Venture 
1150552377SPatrick Venture using ::testing::_;
1250552377SPatrick Venture using ::testing::Invoke;
1350552377SPatrick Venture using ::testing::IsNull;
1450552377SPatrick Venture using ::testing::NotNull;
1550552377SPatrick Venture using ::testing::Return;
1650552377SPatrick Venture using ::testing::StrEq;
1750552377SPatrick Venture 
1850552377SPatrick Venture static auto FanPwmIntf = "xyz.openbmc_project.Control.FanPwm";
1950552377SPatrick Venture static auto FanPwmProp = "Target";
2050552377SPatrick Venture 
2150552377SPatrick Venture // Handle basic expectations we'll need for all these tests, if it's found that
2250552377SPatrick Venture // this is helpful for more tests, it can be promoted in scope.
SetupDbusObject(sdbusplus::SdBusMock * sdbus_mock,const std::string & path,const std::string & intf,const std::string property="")23043d3230SPatrick Venture void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, const std::string& path,
24043d3230SPatrick Venture                      const std::string& intf, const std::string property = "")
2550552377SPatrick Venture {
2650552377SPatrick Venture     if (property.empty())
2750552377SPatrick Venture     {
2850552377SPatrick Venture         EXPECT_CALL(*sdbus_mock,
29043d3230SPatrick Venture                     sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
30043d3230SPatrick Venture                                                         StrEq(intf), NotNull()))
3150552377SPatrick Venture             .WillOnce(Return(0));
3250552377SPatrick Venture     }
3350552377SPatrick Venture     else
3450552377SPatrick Venture     {
3550552377SPatrick Venture         EXPECT_CALL(*sdbus_mock,
36043d3230SPatrick Venture                     sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
37043d3230SPatrick Venture                                                         StrEq(intf), NotNull()))
3882921ae8SMatt Spinler             .WillOnce(Invoke(
3982921ae8SMatt Spinler                 [=](sd_bus*, const char*, const char*, const char** names) {
4050552377SPatrick Venture                     EXPECT_STREQ(property.c_str(), names[0]);
4150552377SPatrick Venture                     return 0;
42043d3230SPatrick Venture                 }));
4350552377SPatrick Venture     }
4450552377SPatrick Venture 
4550552377SPatrick Venture     return;
4650552377SPatrick Venture }
4750552377SPatrick Venture 
TEST(FanPwmTest,BasicConstructorDeferredTest)48043d3230SPatrick Venture TEST(FanPwmTest, BasicConstructorDeferredTest)
49043d3230SPatrick Venture {
5050552377SPatrick Venture     // Attempt to just instantiate one.
5150552377SPatrick Venture 
5250552377SPatrick Venture     // NOTE: This test's goal is to figure out what's minimally required to
5350552377SPatrick Venture     // mock to instantiate this object.
5450552377SPatrick Venture     sdbusplus::SdBusMock sdbus_mock;
5550552377SPatrick Venture     auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
5650552377SPatrick Venture 
5750552377SPatrick Venture     std::string instancePath = "";
5850552377SPatrick Venture     std::string devPath = "";
5950552377SPatrick Venture     std::string id = "";
6050552377SPatrick Venture     std::string objPath = "asdf";
6150552377SPatrick Venture     bool defer = true;
6250552377SPatrick Venture     uint64_t target = 0x01;
6350552377SPatrick Venture 
6450552377SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
6550552377SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
6650552377SPatrick Venture 
6750552377SPatrick Venture     SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
6850552377SPatrick Venture 
69043d3230SPatrick Venture     hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
70043d3230SPatrick Venture                     objPath.c_str(), defer, target);
7150552377SPatrick Venture }
7250552377SPatrick Venture 
TEST(FanPwmTest,BasicConstructorNotDeferredTest)73043d3230SPatrick Venture TEST(FanPwmTest, BasicConstructorNotDeferredTest)
74043d3230SPatrick Venture {
7550552377SPatrick Venture     // Attempt to just instantiate one.
7650552377SPatrick Venture 
7750552377SPatrick Venture     // NOTE: This test's goal is to figure out what's minimally required to
7850552377SPatrick Venture     // mock to instantiate this object.
7950552377SPatrick Venture     sdbusplus::SdBusMock sdbus_mock;
8050552377SPatrick Venture     auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
8150552377SPatrick Venture 
8250552377SPatrick Venture     std::string instancePath = "";
8350552377SPatrick Venture     std::string devPath = "";
8450552377SPatrick Venture     std::string id = "";
8550552377SPatrick Venture     std::string objPath = "asdf";
8650552377SPatrick Venture     bool defer = false;
8750552377SPatrick Venture     uint64_t target = 0x01;
8850552377SPatrick Venture 
8950552377SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
9050552377SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
9150552377SPatrick Venture 
9250552377SPatrick Venture     SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
9350552377SPatrick Venture 
94043d3230SPatrick Venture     EXPECT_CALL(sdbus_mock, sd_bus_emit_object_added(IsNull(), StrEq("asdf")))
9550552377SPatrick Venture         .WillOnce(Return(0));
9650552377SPatrick Venture 
97043d3230SPatrick Venture     EXPECT_CALL(sdbus_mock, sd_bus_emit_object_removed(IsNull(), StrEq("asdf")))
9850552377SPatrick Venture         .WillOnce(Return(0));
9950552377SPatrick Venture 
100043d3230SPatrick Venture     hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
101043d3230SPatrick Venture                     objPath.c_str(), defer, target);
10250552377SPatrick Venture }
10350552377SPatrick Venture 
TEST(FanPwmTest,WriteTargetValue)104043d3230SPatrick Venture TEST(FanPwmTest, WriteTargetValue)
105043d3230SPatrick Venture {
10650552377SPatrick Venture     // Create a FanPwm and write a value to the object.
10750552377SPatrick Venture 
10850552377SPatrick Venture     sdbusplus::SdBusMock sdbus_mock;
10950552377SPatrick Venture     auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
11050552377SPatrick Venture 
11150552377SPatrick Venture     std::string instancePath = "";
11250552377SPatrick Venture     std::string devPath = "devp";
11350552377SPatrick Venture     std::string id = "the_id";
11450552377SPatrick Venture     std::string objPath = "asdf";
11550552377SPatrick Venture     bool defer = true;
11650552377SPatrick Venture     uint64_t target = 0x01;
11750552377SPatrick Venture 
11850552377SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
11950552377SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
12050552377SPatrick Venture 
12150552377SPatrick Venture     SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
12250552377SPatrick Venture 
12350552377SPatrick Venture     hwmonio::HwmonIOMock* hwmonio =
12450552377SPatrick Venture         reinterpret_cast<hwmonio::HwmonIOMock*>(hwmonio_mock.get());
12550552377SPatrick Venture 
126043d3230SPatrick Venture     hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
127043d3230SPatrick Venture                     objPath.c_str(), defer, target);
12850552377SPatrick Venture 
12950552377SPatrick Venture     target = 0x64;
13050552377SPatrick Venture 
131043d3230SPatrick Venture     EXPECT_CALL(*hwmonio,
132043d3230SPatrick Venture                 write(static_cast<uint32_t>(target), StrEq("pwm"),
133043d3230SPatrick Venture                       StrEq("the_id"), _, hwmonio::retries, hwmonio::delay));
13450552377SPatrick Venture 
13550552377SPatrick Venture     EXPECT_CALL(sdbus_mock,
13650552377SPatrick Venture                 sd_bus_emit_properties_changed_strv(
137043d3230SPatrick Venture                     IsNull(), StrEq("asdf"), StrEq(FanPwmIntf), NotNull()))
13882921ae8SMatt Spinler         .WillOnce(
13982921ae8SMatt Spinler             Invoke([&](sd_bus*, const char*, const char*, const char** names) {
14050552377SPatrick Venture                 EXPECT_EQ(0, strncmp("Target", names[0], 6));
14150552377SPatrick Venture                 return 0;
142043d3230SPatrick Venture             }));
14350552377SPatrick Venture 
14450552377SPatrick Venture     EXPECT_EQ(target, f.target(target));
14550552377SPatrick Venture }
14650552377SPatrick Venture 
TEST(FanPwmTest,WriteTargetValueNoUpdate)147043d3230SPatrick Venture TEST(FanPwmTest, WriteTargetValueNoUpdate)
148043d3230SPatrick Venture {
14950552377SPatrick Venture     // Create a FanPwm and write a value to the object that was the previous
15050552377SPatrick Venture     // value.
15150552377SPatrick Venture 
15250552377SPatrick Venture     sdbusplus::SdBusMock sdbus_mock;
15350552377SPatrick Venture     auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
15450552377SPatrick Venture 
15550552377SPatrick Venture     std::string instancePath = "";
15650552377SPatrick Venture     std::string devPath = "devp";
15750552377SPatrick Venture     std::string id = "the_id";
15850552377SPatrick Venture     std::string objPath = "asdf";
15950552377SPatrick Venture     bool defer = true;
16050552377SPatrick Venture     uint64_t target = 0x01;
16150552377SPatrick Venture 
16250552377SPatrick Venture     std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
16350552377SPatrick Venture         std::make_unique<hwmonio::HwmonIOMock>();
16450552377SPatrick Venture 
16550552377SPatrick Venture     SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
16650552377SPatrick Venture 
167043d3230SPatrick Venture     hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
168043d3230SPatrick Venture                     objPath.c_str(), defer, target);
16950552377SPatrick Venture 
17050552377SPatrick Venture     EXPECT_EQ(target, f.target(target));
17150552377SPatrick Venture }
172