blob: 112f3e57e6e5c1a93e4daf077f2dcbb429b89d4c [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{
26 EXPECT_CALL(*sdbus_mock,
Patrick Venture043d3232018-08-31 10:10:53 -070027 sd_bus_add_object_vtable(IsNull(), NotNull(), StrEq(path),
28 StrEq(intf), NotNull(), NotNull()))
29 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -070030
31 if (property.empty())
32 {
33 EXPECT_CALL(*sdbus_mock,
Patrick Venture043d3232018-08-31 10:10:53 -070034 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
35 StrEq(intf), NotNull()))
36 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -070037 }
38 else
39 {
40 EXPECT_CALL(*sdbus_mock,
Patrick Venture043d3232018-08-31 10:10:53 -070041 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
42 StrEq(intf), NotNull()))
Matt Spinler82921ae2021-02-02 14:25:06 -060043 .WillOnce(Invoke(
44 [=](sd_bus*, const char*, const char*, const char** names) {
Patrick Williamse8771fd2023-05-10 07:51:06 -050045 EXPECT_STREQ(property.c_str(), names[0]);
46 return 0;
Patrick Williamsc8e818d2023-10-20 11:19:30 -050047 }));
Patrick Venture50552372018-06-07 10:53:56 -070048 }
49
50 return;
51}
52
Patrick Venture043d3232018-08-31 10:10:53 -070053TEST(FanPwmTest, BasicConstructorDeferredTest)
54{
Patrick Venture50552372018-06-07 10:53:56 -070055 // Attempt to just instantiate one.
56
57 // NOTE: This test's goal is to figure out what's minimally required to
58 // mock to instantiate this object.
59 sdbusplus::SdBusMock sdbus_mock;
60 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
61
62 std::string instancePath = "";
63 std::string devPath = "";
64 std::string id = "";
65 std::string objPath = "asdf";
66 bool defer = true;
67 uint64_t target = 0x01;
68
69 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
70 std::make_unique<hwmonio::HwmonIOMock>();
71
72 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
73
Patrick Venture043d3232018-08-31 10:10:53 -070074 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
75 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -070076}
77
Patrick Venture043d3232018-08-31 10:10:53 -070078TEST(FanPwmTest, BasicConstructorNotDeferredTest)
79{
Patrick Venture50552372018-06-07 10:53:56 -070080 // Attempt to just instantiate one.
81
82 // NOTE: This test's goal is to figure out what's minimally required to
83 // mock to instantiate this object.
84 sdbusplus::SdBusMock sdbus_mock;
85 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
86
87 std::string instancePath = "";
88 std::string devPath = "";
89 std::string id = "";
90 std::string objPath = "asdf";
91 bool defer = false;
92 uint64_t target = 0x01;
93
94 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
95 std::make_unique<hwmonio::HwmonIOMock>();
96
97 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
98
Patrick Venture043d3232018-08-31 10:10:53 -070099 EXPECT_CALL(sdbus_mock, sd_bus_emit_object_added(IsNull(), StrEq("asdf")))
100 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -0700101
Patrick Venture043d3232018-08-31 10:10:53 -0700102 EXPECT_CALL(sdbus_mock, sd_bus_emit_object_removed(IsNull(), StrEq("asdf")))
103 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -0700104
Patrick Venture043d3232018-08-31 10:10:53 -0700105 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
106 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -0700107}
108
Patrick Venture043d3232018-08-31 10:10:53 -0700109TEST(FanPwmTest, WriteTargetValue)
110{
Patrick Venture50552372018-06-07 10:53:56 -0700111 // Create a FanPwm and write a value to the object.
112
113 sdbusplus::SdBusMock sdbus_mock;
114 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
115
116 std::string instancePath = "";
117 std::string devPath = "devp";
118 std::string id = "the_id";
119 std::string objPath = "asdf";
120 bool defer = true;
121 uint64_t target = 0x01;
122
123 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
124 std::make_unique<hwmonio::HwmonIOMock>();
125
126 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
127
Patrick Venturecc869152018-09-04 12:36:43 -0700128 hwmonio::HwmonIOMock* hwmonio =
129 reinterpret_cast<hwmonio::HwmonIOMock*>(hwmonio_mock.get());
Patrick Venture50552372018-06-07 10:53:56 -0700130
Patrick Venture043d3232018-08-31 10:10:53 -0700131 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
132 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -0700133
134 target = 0x64;
135
Patrick Venture043d3232018-08-31 10:10:53 -0700136 EXPECT_CALL(*hwmonio,
137 write(static_cast<uint32_t>(target), StrEq("pwm"),
138 StrEq("the_id"), _, hwmonio::retries, hwmonio::delay));
Patrick Venture50552372018-06-07 10:53:56 -0700139
140 EXPECT_CALL(sdbus_mock,
141 sd_bus_emit_properties_changed_strv(
Patrick Venture043d3232018-08-31 10:10:53 -0700142 IsNull(), StrEq("asdf"), StrEq(FanPwmIntf), NotNull()))
Matt Spinler82921ae2021-02-02 14:25:06 -0600143 .WillOnce(
144 Invoke([&](sd_bus*, const char*, const char*, const char** names) {
Patrick Williamsc8e818d2023-10-20 11:19:30 -0500145 EXPECT_EQ(0, strncmp("Target", names[0], 6));
146 return 0;
147 }));
Patrick Venture50552372018-06-07 10:53:56 -0700148
149 EXPECT_EQ(target, f.target(target));
150}
151
Patrick Venture043d3232018-08-31 10:10:53 -0700152TEST(FanPwmTest, WriteTargetValueNoUpdate)
153{
Patrick Venture50552372018-06-07 10:53:56 -0700154 // Create a FanPwm and write a value to the object that was the previous
155 // value.
156
157 sdbusplus::SdBusMock sdbus_mock;
158 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
159
160 std::string instancePath = "";
161 std::string devPath = "devp";
162 std::string id = "the_id";
163 std::string objPath = "asdf";
164 bool defer = true;
165 uint64_t target = 0x01;
166
167 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
168 std::make_unique<hwmonio::HwmonIOMock>();
169
170 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
171
Patrick Venture043d3232018-08-31 10:10:53 -0700172 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
173 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -0700174
175 EXPECT_EQ(target, f.target(target));
176}