blob: 5a7bcfe34ee659aec0d38f1a9267264f68a7a122 [file] [log] [blame]
Patrick Venture25243232018-06-14 10:34:34 -07001#include "dbus/dbusactiveread.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07002#include "test/dbushelper_mock.hpp"
Patrick Venture25243232018-06-14 10:34:34 -07003
Patrick Venture25243232018-06-14 10:34:34 -07004#include <sdbusplus/test/sdbus_mock.hpp>
5#include <string>
6
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07007#include <gmock/gmock.h>
8#include <gtest/gtest.h>
Patrick Venture25243232018-06-14 10:34:34 -07009
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070010using ::testing::_;
Patrick Venture25243232018-06-14 10:34:34 -070011using ::testing::Invoke;
12using ::testing::NotNull;
Patrick Venture25243232018-06-14 10:34:34 -070013
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070014TEST(DbusActiveReadTest, BoringConstructorTest)
15{
Patrick Venture25243232018-06-14 10:34:34 -070016 // 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 Ventureda4a5dd2018-08-31 09:42:48 -070027TEST(DbusActiveReadTest, Read_VerifyCallsToDbusForValue)
28{
Patrick Venture25243232018-06-14 10:34:34 -070029 // 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 Ventureda4a5dd2018-08-31 09:42:48 -070040 .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 Venture25243232018-06-14 10:34:34 -070047
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.