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