hwmonio:: Add Interface base class and tests

Enable injecting hwmonio::HwmonIO mocks for testing.

Tested: Ran on quanta-q71l and saw all sensors exported to dbus as
expected with the expected values.

Change-Id: I35912bf2a733932d9e1e774ff53b0114ae16560b
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index 7f302ca..1b07171 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,9 +12,11 @@
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS)
 
 # Run all 'check' test programs
-check_PROGRAMS = hwmon_unittest
+check_PROGRAMS = hwmon_unittest fanpwm_unittest
 TESTS = $(check_PROGRAMS)
 
 hwmon_unittest_SOURCES = hwmon_unittest.cpp
 hwmon_unittest_LDADD = $(top_builddir)/hwmon.o
 
+fanpwm_unittest_SOURCES = fanpwm_unittest.cpp
+fanpwm_unittest_LDADD = $(PHOSPHOR_LOGGING_LIBS) $(top_builddir)/fan_pwm.o
diff --git a/test/fanpwm_unittest.cpp b/test/fanpwm_unittest.cpp
new file mode 100644
index 0000000..63520d3
--- /dev/null
+++ b/test/fanpwm_unittest.cpp
@@ -0,0 +1,221 @@
+#include "fan_pwm.hpp"
+
+#include "hwmonio_mock.hpp"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <sdbusplus/test/sdbus_mock.hpp>
+#include <string>
+
+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 = "")
+{
+    EXPECT_CALL(*sdbus_mock,
+                sd_bus_add_object_vtable(
+                    IsNull(),
+                    NotNull(),
+                    StrEq(path),
+                    StrEq(intf),
+                    NotNull(),
+                    NotNull()))
+    .WillOnce(Return(0));
+
+    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 *bus,
+                       const char *path,
+                       const char *interface,
+                       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 *bus,
+                   const char *path,
+                   const char *interface,
+                   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));
+}
diff --git a/test/hwmonio_mock.hpp b/test/hwmonio_mock.hpp
new file mode 100644
index 0000000..4ad944c
--- /dev/null
+++ b/test/hwmonio_mock.hpp
@@ -0,0 +1,32 @@
+#pragma once
+
+#include <gmock/gmock.h>
+
+#include "hwmonio.hpp"
+
+namespace hwmonio {
+
+class HwmonIOMock : public HwmonIOInterface
+{
+    public:
+        virtual ~HwmonIOMock(){};
+
+        MOCK_CONST_METHOD5(read, int64_t(const std::string&,
+                                         const std::string&,
+                                         const std::string&,
+                                         size_t,
+                                         std::chrono::milliseconds));
+
+        MOCK_CONST_METHOD6(write, void(uint32_t,
+                                       const std::string&,
+                                       const std::string&,
+                                       const std::string&,
+                                       size_t,
+                                       std::chrono::milliseconds));
+
+        MOCK_CONST_METHOD0(path, std::string());
+};
+
+} // namespace hwmonio
+
+// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4