blob: 8f12b191219da6df902cc3bc7c22202eccb98923 [file] [log] [blame]
Patrick Venture50552372018-06-07 10:53:56 -07001#include "fan_pwm.hpp"
Patrick Venture50552372018-06-07 10:53:56 -07002#include "hwmonio_mock.hpp"
3
Patrick Venture043d3232018-08-31 10:10:53 -07004#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Williamse8771fd2023-05-10 07:51:06 -05005
Patrick Venture043d3232018-08-31 10:10:53 -07006#include <string>
7
Patrick Venture50552372018-06-07 10:53:56 -07008#include <gmock/gmock.h>
9#include <gtest/gtest.h>
Patrick Venture50552372018-06-07 10:53:56 -070010
11using ::testing::_;
12using ::testing::Invoke;
13using ::testing::IsNull;
14using ::testing::NotNull;
15using ::testing::Return;
16using ::testing::StrEq;
17
18static auto FanPwmIntf = "xyz.openbmc_project.Control.FanPwm";
19static auto FanPwmProp = "Target";
20
21// Handle basic expectations we'll need for all these tests, if it's found that
22// this is helpful for more tests, it can be promoted in scope.
Patrick Venturecc869152018-09-04 12:36:43 -070023void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, const std::string& path,
24 const std::string& intf, const std::string property = "")
Patrick Venture50552372018-06-07 10:53:56 -070025{
Patrick Venture50552372018-06-07 10:53:56 -070026 if (property.empty())
27 {
28 EXPECT_CALL(*sdbus_mock,
Patrick Venture043d3232018-08-31 10:10:53 -070029 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
30 StrEq(intf), NotNull()))
31 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -070032 }
33 else
34 {
35 EXPECT_CALL(*sdbus_mock,
Patrick Venture043d3232018-08-31 10:10:53 -070036 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
37 StrEq(intf), NotNull()))
Matt Spinler82921ae2021-02-02 14:25:06 -060038 .WillOnce(Invoke(
39 [=](sd_bus*, const char*, const char*, const char** names) {
Patrick Williams02e598a2024-08-16 15:21:23 -040040 EXPECT_STREQ(property.c_str(), names[0]);
41 return 0;
42 }));
Patrick Venture50552372018-06-07 10:53:56 -070043 }
44
45 return;
46}
47
Patrick Venture043d3232018-08-31 10:10:53 -070048TEST(FanPwmTest, BasicConstructorDeferredTest)
49{
Patrick Venture50552372018-06-07 10:53:56 -070050 // Attempt to just instantiate one.
51
52 // NOTE: This test's goal is to figure out what's minimally required to
53 // mock to instantiate this object.
54 sdbusplus::SdBusMock sdbus_mock;
55 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
56
57 std::string instancePath = "";
58 std::string devPath = "";
59 std::string id = "";
60 std::string objPath = "asdf";
61 bool defer = true;
62 uint64_t target = 0x01;
63
64 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
65 std::make_unique<hwmonio::HwmonIOMock>();
66
67 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
68
Patrick Venture043d3232018-08-31 10:10:53 -070069 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
70 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -070071}
72
Patrick Venture043d3232018-08-31 10:10:53 -070073TEST(FanPwmTest, BasicConstructorNotDeferredTest)
74{
Patrick Venture50552372018-06-07 10:53:56 -070075 // Attempt to just instantiate one.
76
77 // NOTE: This test's goal is to figure out what's minimally required to
78 // mock to instantiate this object.
79 sdbusplus::SdBusMock sdbus_mock;
80 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
81
82 std::string instancePath = "";
83 std::string devPath = "";
84 std::string id = "";
85 std::string objPath = "asdf";
86 bool defer = false;
87 uint64_t target = 0x01;
88
89 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
90 std::make_unique<hwmonio::HwmonIOMock>();
91
92 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
93
Patrick Venture043d3232018-08-31 10:10:53 -070094 EXPECT_CALL(sdbus_mock, sd_bus_emit_object_added(IsNull(), StrEq("asdf")))
95 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -070096
Patrick Venture043d3232018-08-31 10:10:53 -070097 EXPECT_CALL(sdbus_mock, sd_bus_emit_object_removed(IsNull(), StrEq("asdf")))
98 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -070099
Patrick Venture043d3232018-08-31 10:10:53 -0700100 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
101 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -0700102}
103
Patrick Venture043d3232018-08-31 10:10:53 -0700104TEST(FanPwmTest, WriteTargetValue)
105{
Patrick Venture50552372018-06-07 10:53:56 -0700106 // Create a FanPwm and write a value to the object.
107
108 sdbusplus::SdBusMock sdbus_mock;
109 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
110
111 std::string instancePath = "";
112 std::string devPath = "devp";
113 std::string id = "the_id";
114 std::string objPath = "asdf";
115 bool defer = true;
116 uint64_t target = 0x01;
117
118 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
119 std::make_unique<hwmonio::HwmonIOMock>();
120
121 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
122
Patrick Venturecc869152018-09-04 12:36:43 -0700123 hwmonio::HwmonIOMock* hwmonio =
124 reinterpret_cast<hwmonio::HwmonIOMock*>(hwmonio_mock.get());
Patrick Venture50552372018-06-07 10:53:56 -0700125
Patrick Venture043d3232018-08-31 10:10:53 -0700126 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
127 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -0700128
129 target = 0x64;
130
Patrick Venture043d3232018-08-31 10:10:53 -0700131 EXPECT_CALL(*hwmonio,
132 write(static_cast<uint32_t>(target), StrEq("pwm"),
133 StrEq("the_id"), _, hwmonio::retries, hwmonio::delay));
Patrick Venture50552372018-06-07 10:53:56 -0700134
135 EXPECT_CALL(sdbus_mock,
136 sd_bus_emit_properties_changed_strv(
Patrick Venture043d3232018-08-31 10:10:53 -0700137 IsNull(), StrEq("asdf"), StrEq(FanPwmIntf), NotNull()))
Matt Spinler82921ae2021-02-02 14:25:06 -0600138 .WillOnce(
139 Invoke([&](sd_bus*, const char*, const char*, const char** names) {
Patrick Williams02e598a2024-08-16 15:21:23 -0400140 EXPECT_EQ(0, strncmp("Target", names[0], 6));
141 return 0;
142 }));
Patrick Venture50552372018-06-07 10:53:56 -0700143
144 EXPECT_EQ(target, f.target(target));
145}
146
Patrick Venture043d3232018-08-31 10:10:53 -0700147TEST(FanPwmTest, WriteTargetValueNoUpdate)
148{
Patrick Venture50552372018-06-07 10:53:56 -0700149 // Create a FanPwm and write a value to the object that was the previous
150 // value.
151
152 sdbusplus::SdBusMock sdbus_mock;
153 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
154
155 std::string instancePath = "";
156 std::string devPath = "devp";
157 std::string id = "the_id";
158 std::string objPath = "asdf";
159 bool defer = true;
160 uint64_t target = 0x01;
161
162 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
163 std::make_unique<hwmonio::HwmonIOMock>();
164
165 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
166
Patrick Venture043d3232018-08-31 10:10:53 -0700167 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
168 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -0700169
170 EXPECT_EQ(target, f.target(target));
171}