Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 1 | #include "dbus/dbusactiveread.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame^] | 2 | #include "test/dbushelper_mock.hpp" |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 3 | |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 4 | #include <sdbusplus/test/sdbus_mock.hpp> |
| 5 | #include <string> |
| 6 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame^] | 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 9 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame^] | 10 | using ::testing::_; |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 11 | using ::testing::Invoke; |
| 12 | using ::testing::NotNull; |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 13 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame^] | 14 | TEST(DbusActiveReadTest, BoringConstructorTest) |
| 15 | { |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 16 | // Verify we can construct it. |
| 17 | |
| 18 | sdbusplus::SdBusMock sdbus_mock; |
| 19 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 20 | DbusHelperMock helper; |
| 21 | std::string path = "/asdf"; |
| 22 | std::string service = "asdfasdf.asdfasdf"; |
| 23 | |
| 24 | DbusActiveRead ar(bus_mock, path, service, &helper); |
| 25 | } |
| 26 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame^] | 27 | TEST(DbusActiveReadTest, Read_VerifyCallsToDbusForValue) |
| 28 | { |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 29 | // Verify it calls to get the value from dbus when requested. |
| 30 | |
| 31 | sdbusplus::SdBusMock sdbus_mock; |
| 32 | auto bus_mock = sdbusplus::get_mocked_new(&sdbus_mock); |
| 33 | DbusHelperMock helper; |
| 34 | std::string path = "/asdf"; |
| 35 | std::string service = "asdfasdf.asdfasdf"; |
| 36 | |
| 37 | DbusActiveRead ar(bus_mock, path, service, &helper); |
| 38 | |
| 39 | EXPECT_CALL(helper, GetProperties(_, service, path, NotNull())) |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame^] | 40 | .WillOnce( |
| 41 | Invoke([&](sdbusplus::bus::bus& bus, const std::string& service, |
| 42 | const std::string& path, struct SensorProperties* prop) { |
| 43 | prop->scale = -3; |
| 44 | prop->value = 10000; |
| 45 | prop->unit = "x"; |
| 46 | })); |
Patrick Venture | 2524323 | 2018-06-14 10:34:34 -0700 | [diff] [blame] | 47 | |
| 48 | ReadReturn r = ar.read(); |
| 49 | EXPECT_EQ(10, r.value); |
| 50 | } |
| 51 | |
| 52 | // WARN: GetProperties will raise an exception on failure |
| 53 | // Instead of just not updating the value. |