blob: 11dcee966ba2ddb65f29cf1adf00d2405fd5c085 [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>
5#include <string>
6
Patrick Venture50552372018-06-07 10:53:56 -07007#include <gmock/gmock.h>
8#include <gtest/gtest.h>
Patrick Venture50552372018-06-07 10:53:56 -07009
10using ::testing::_;
11using ::testing::Invoke;
12using ::testing::IsNull;
13using ::testing::NotNull;
14using ::testing::Return;
15using ::testing::StrEq;
16
17static auto FanPwmIntf = "xyz.openbmc_project.Control.FanPwm";
18static auto FanPwmProp = "Target";
19
20// Handle basic expectations we'll need for all these tests, if it's found that
21// this is helpful for more tests, it can be promoted in scope.
Patrick Venture043d3232018-08-31 10:10:53 -070022void SetupDbusObject(sdbusplus::SdBusMock *sdbus_mock, const std::string &path,
23 const std::string &intf, const std::string property = "")
Patrick Venture50552372018-06-07 10:53:56 -070024{
25 EXPECT_CALL(*sdbus_mock,
Patrick Venture043d3232018-08-31 10:10:53 -070026 sd_bus_add_object_vtable(IsNull(), NotNull(), StrEq(path),
27 StrEq(intf), NotNull(), NotNull()))
28 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -070029
30 if (property.empty())
31 {
32 EXPECT_CALL(*sdbus_mock,
Patrick Venture043d3232018-08-31 10:10:53 -070033 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
34 StrEq(intf), NotNull()))
35 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -070036 }
37 else
38 {
39 EXPECT_CALL(*sdbus_mock,
Patrick Venture043d3232018-08-31 10:10:53 -070040 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
41 StrEq(intf), NotNull()))
42 .WillOnce(Invoke([=](sd_bus *bus, const char *path,
43 const char *interface, char **names) {
Patrick Venture50552372018-06-07 10:53:56 -070044 EXPECT_STREQ(property.c_str(), names[0]);
45 return 0;
Patrick Venture043d3232018-08-31 10:10:53 -070046 }));
Patrick Venture50552372018-06-07 10:53:56 -070047 }
48
49 return;
50}
51
Patrick Venture043d3232018-08-31 10:10:53 -070052TEST(FanPwmTest, BasicConstructorDeferredTest)
53{
Patrick Venture50552372018-06-07 10:53:56 -070054 // Attempt to just instantiate one.
55
56 // NOTE: This test's goal is to figure out what's minimally required to
57 // mock to instantiate this object.
58 sdbusplus::SdBusMock sdbus_mock;
59 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
60
61 std::string instancePath = "";
62 std::string devPath = "";
63 std::string id = "";
64 std::string objPath = "asdf";
65 bool defer = true;
66 uint64_t target = 0x01;
67
68 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
69 std::make_unique<hwmonio::HwmonIOMock>();
70
71 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
72
Patrick Venture043d3232018-08-31 10:10:53 -070073 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
74 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -070075}
76
Patrick Venture043d3232018-08-31 10:10:53 -070077TEST(FanPwmTest, BasicConstructorNotDeferredTest)
78{
Patrick Venture50552372018-06-07 10:53:56 -070079 // Attempt to just instantiate one.
80
81 // NOTE: This test's goal is to figure out what's minimally required to
82 // mock to instantiate this object.
83 sdbusplus::SdBusMock sdbus_mock;
84 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
85
86 std::string instancePath = "";
87 std::string devPath = "";
88 std::string id = "";
89 std::string objPath = "asdf";
90 bool defer = false;
91 uint64_t target = 0x01;
92
93 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
94 std::make_unique<hwmonio::HwmonIOMock>();
95
96 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
97
Patrick Venture043d3232018-08-31 10:10:53 -070098 EXPECT_CALL(sdbus_mock, sd_bus_emit_object_added(IsNull(), StrEq("asdf")))
99 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -0700100
Patrick Venture043d3232018-08-31 10:10:53 -0700101 EXPECT_CALL(sdbus_mock, sd_bus_emit_object_removed(IsNull(), StrEq("asdf")))
102 .WillOnce(Return(0));
Patrick Venture50552372018-06-07 10:53:56 -0700103
Patrick Venture043d3232018-08-31 10:10:53 -0700104 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
105 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -0700106}
107
Patrick Venture043d3232018-08-31 10:10:53 -0700108TEST(FanPwmTest, WriteTargetValue)
109{
Patrick Venture50552372018-06-07 10:53:56 -0700110 // Create a FanPwm and write a value to the object.
111
112 sdbusplus::SdBusMock sdbus_mock;
113 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
114
115 std::string instancePath = "";
116 std::string devPath = "devp";
117 std::string id = "the_id";
118 std::string objPath = "asdf";
119 bool defer = true;
120 uint64_t target = 0x01;
121
122 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
123 std::make_unique<hwmonio::HwmonIOMock>();
124
125 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
126
127 hwmonio::HwmonIOMock *hwmonio =
128 reinterpret_cast<hwmonio::HwmonIOMock *>(hwmonio_mock.get());
129
Patrick Venture043d3232018-08-31 10:10:53 -0700130 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
131 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -0700132
133 target = 0x64;
134
Patrick Venture043d3232018-08-31 10:10:53 -0700135 EXPECT_CALL(*hwmonio,
136 write(static_cast<uint32_t>(target), StrEq("pwm"),
137 StrEq("the_id"), _, hwmonio::retries, hwmonio::delay));
Patrick Venture50552372018-06-07 10:53:56 -0700138
139 EXPECT_CALL(sdbus_mock,
140 sd_bus_emit_properties_changed_strv(
Patrick Venture043d3232018-08-31 10:10:53 -0700141 IsNull(), StrEq("asdf"), StrEq(FanPwmIntf), NotNull()))
142 .WillOnce(Invoke([&](sd_bus *bus, const char *path,
143 const char *interface, char **names) {
Patrick Venture50552372018-06-07 10:53:56 -0700144 EXPECT_EQ(0, strncmp("Target", names[0], 6));
145 return 0;
Patrick Venture043d3232018-08-31 10:10:53 -0700146 }));
Patrick Venture50552372018-06-07 10:53:56 -0700147
148 EXPECT_EQ(target, f.target(target));
149}
150
Patrick Venture043d3232018-08-31 10:10:53 -0700151TEST(FanPwmTest, WriteTargetValueNoUpdate)
152{
Patrick Venture50552372018-06-07 10:53:56 -0700153 // Create a FanPwm and write a value to the object that was the previous
154 // value.
155
156 sdbusplus::SdBusMock sdbus_mock;
157 auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
158
159 std::string instancePath = "";
160 std::string devPath = "devp";
161 std::string id = "the_id";
162 std::string objPath = "asdf";
163 bool defer = true;
164 uint64_t target = 0x01;
165
166 std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
167 std::make_unique<hwmonio::HwmonIOMock>();
168
169 SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
170
Patrick Venture043d3232018-08-31 10:10:53 -0700171 hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
172 objPath.c_str(), defer, target);
Patrick Venture50552372018-06-07 10:53:56 -0700173
174 EXPECT_EQ(target, f.target(target));
175}