test: sensors: pluggable

Tests for pluggable sensors.  Pluggable sensors themselves do nothing
but hold min/max values, and call into Read and Write interfaces.

Change-Id: I8991d22d77172fb9b2f897c02603e5ab5747ac12
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index 738ab91..c6b3375 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,9 +12,12 @@
 	$(PHOSPHOR_DBUS_INTERFACES_LIBS)
 
 # Run all 'check' test programs
-check_PROGRAMS = sensor_manager_unittest
+check_PROGRAMS = sensor_manager_unittest sensor_pluggable_unittest
 TESTS = $(check_PROGRAMS)
 
 # Until libconfig is mocked out or replaced, include it.
 sensor_manager_unittest_SOURCES = sensor_manager_unittest.cpp
 sensor_manager_unittest_LDADD = $(top_builddir)/sensors/manager.o
+
+sensor_pluggable_unittest_SOURCES = sensor_pluggable_unittest.cpp
+sensor_pluggable_unittest_LDADD = $(top_builddir)/sensors/pluggable.o
diff --git a/test/sensor_pluggable_unittest.cpp b/test/sensor_pluggable_unittest.cpp
new file mode 100644
index 0000000..1e32230
--- /dev/null
+++ b/test/sensor_pluggable_unittest.cpp
@@ -0,0 +1,84 @@
+#include "sensors/pluggable.hpp"
+
+#include <chrono>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "test/readinterface_mock.hpp"
+#include "test/writeinterface_mock.hpp"
+
+using ::testing::Invoke;
+
+TEST(PluggableSensorTest, BoringConstructorTest) {
+    // Build a boring Pluggable Sensor.
+
+    int64_t min = 0;
+    int64_t max = 255;
+
+    std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
+    std::unique_ptr<WriteInterface> wi =
+        std::make_unique<WriteInterfaceMock>(min, max);
+
+    std::string name = "name";
+    int64_t timeout = 1;
+
+    PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
+    // Successfully created it.
+}
+
+TEST(PluggableSensorTest, TryReadingTest) {
+    // Verify calling read, calls the ReadInterface.
+
+    int64_t min = 0;
+    int64_t max = 255;
+
+    std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
+    std::unique_ptr<WriteInterface> wi =
+        std::make_unique<WriteInterfaceMock>(min, max);
+
+    std::string name = "name";
+    int64_t timeout = 1;
+
+    ReadInterfaceMock *rip = reinterpret_cast<ReadInterfaceMock *>(ri.get());
+
+    PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
+
+    ReadReturn r;
+    r.value = 0.1;
+    r.updated = std::chrono::high_resolution_clock::now();
+
+    EXPECT_CALL(*rip, read())
+    .WillOnce(
+        Invoke([&](void) {
+            return r;
+        })
+    );
+
+    // TODO(venture): Implement comparison operator for ReadReturn.
+    ReadReturn v = p.read();
+    EXPECT_EQ(r.value, v.value);
+    EXPECT_EQ(r.updated, v.updated);
+}
+
+TEST(PluggableSensorTest, TryWritingTest) {
+    // Verify calling write, calls the WriteInterface.
+
+    int64_t min = 0;
+    int64_t max = 255;
+
+    std::unique_ptr<ReadInterface> ri = std::make_unique<ReadInterfaceMock>();
+    std::unique_ptr<WriteInterface> wi =
+        std::make_unique<WriteInterfaceMock>(min, max);
+
+    std::string name = "name";
+    int64_t timeout = 1;
+
+    WriteInterfaceMock *wip = reinterpret_cast<WriteInterfaceMock *>(wi.get());
+
+    PluggableSensor p(name, timeout, std::move(ri), std::move(wi));
+
+    double value = 0.303;
+
+    EXPECT_CALL(*wip, write(value));
+    p.write(value);
+}