blob: 8f12b191219da6df902cc3bc7c22202eccb98923 [file] [log] [blame]
#include "fan_pwm.hpp"
#include "hwmonio_mock.hpp"
#include <sdbusplus/test/sdbus_mock.hpp>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::_;
using ::testing::Invoke;
using ::testing::IsNull;
using ::testing::NotNull;
using ::testing::Return;
using ::testing::StrEq;
static auto FanPwmIntf = "xyz.openbmc_project.Control.FanPwm";
static auto FanPwmProp = "Target";
// Handle basic expectations we'll need for all these tests, if it's found that
// this is helpful for more tests, it can be promoted in scope.
void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, const std::string& path,
const std::string& intf, const std::string property = "")
{
if (property.empty())
{
EXPECT_CALL(*sdbus_mock,
sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
StrEq(intf), NotNull()))
.WillOnce(Return(0));
}
else
{
EXPECT_CALL(*sdbus_mock,
sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
StrEq(intf), NotNull()))
.WillOnce(Invoke(
[=](sd_bus*, const char*, const char*, const char** names) {
EXPECT_STREQ(property.c_str(), names[0]);
return 0;
}));
}
return;
}
TEST(FanPwmTest, BasicConstructorDeferredTest)
{
// Attempt to just instantiate one.
// NOTE: This test's goal is to figure out what's minimally required to
// mock to instantiate this object.
sdbusplus::SdBusMock sdbus_mock;
auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
std::string instancePath = "";
std::string devPath = "";
std::string id = "";
std::string objPath = "asdf";
bool defer = true;
uint64_t target = 0x01;
std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
std::make_unique<hwmonio::HwmonIOMock>();
SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
objPath.c_str(), defer, target);
}
TEST(FanPwmTest, BasicConstructorNotDeferredTest)
{
// Attempt to just instantiate one.
// NOTE: This test's goal is to figure out what's minimally required to
// mock to instantiate this object.
sdbusplus::SdBusMock sdbus_mock;
auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
std::string instancePath = "";
std::string devPath = "";
std::string id = "";
std::string objPath = "asdf";
bool defer = false;
uint64_t target = 0x01;
std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
std::make_unique<hwmonio::HwmonIOMock>();
SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
EXPECT_CALL(sdbus_mock, sd_bus_emit_object_added(IsNull(), StrEq("asdf")))
.WillOnce(Return(0));
EXPECT_CALL(sdbus_mock, sd_bus_emit_object_removed(IsNull(), StrEq("asdf")))
.WillOnce(Return(0));
hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
objPath.c_str(), defer, target);
}
TEST(FanPwmTest, WriteTargetValue)
{
// Create a FanPwm and write a value to the object.
sdbusplus::SdBusMock sdbus_mock;
auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
std::string instancePath = "";
std::string devPath = "devp";
std::string id = "the_id";
std::string objPath = "asdf";
bool defer = true;
uint64_t target = 0x01;
std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
std::make_unique<hwmonio::HwmonIOMock>();
SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
hwmonio::HwmonIOMock* hwmonio =
reinterpret_cast<hwmonio::HwmonIOMock*>(hwmonio_mock.get());
hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
objPath.c_str(), defer, target);
target = 0x64;
EXPECT_CALL(*hwmonio,
write(static_cast<uint32_t>(target), StrEq("pwm"),
StrEq("the_id"), _, hwmonio::retries, hwmonio::delay));
EXPECT_CALL(sdbus_mock,
sd_bus_emit_properties_changed_strv(
IsNull(), StrEq("asdf"), StrEq(FanPwmIntf), NotNull()))
.WillOnce(
Invoke([&](sd_bus*, const char*, const char*, const char** names) {
EXPECT_EQ(0, strncmp("Target", names[0], 6));
return 0;
}));
EXPECT_EQ(target, f.target(target));
}
TEST(FanPwmTest, WriteTargetValueNoUpdate)
{
// Create a FanPwm and write a value to the object that was the previous
// value.
sdbusplus::SdBusMock sdbus_mock;
auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock);
std::string instancePath = "";
std::string devPath = "devp";
std::string id = "the_id";
std::string objPath = "asdf";
bool defer = true;
uint64_t target = 0x01;
std::unique_ptr<hwmonio::HwmonIOInterface> hwmonio_mock =
std::make_unique<hwmonio::HwmonIOMock>();
SetupDbusObject(&sdbus_mock, objPath, FanPwmIntf, FanPwmProp);
hwmon::FanPwm f(std::move(hwmonio_mock), devPath, id, bus_mock,
objPath.c_str(), defer, target);
EXPECT_EQ(target, f.target(target));
}