1 // THIS EXISTS AS A COPY OF SDBUSPLUS/TEST/HELPERS.HPP until that is merged.
2 #pragma once
3
4 #include <systemd/sd-bus.h>
5
6 #include <sdbusplus/test/sdbus_mock.hpp>
7
8 #include <string>
9 #include <vector>
10
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
13
14 namespace pid_control
15 {
16
17 using ::testing::_;
18 using ::testing::Invoke;
19 using ::testing::IsNull;
20 using ::testing::NotNull;
21 using ::testing::Return;
22 using ::testing::StrEq;
23
24 /** @brief Setup the expectations for sdbus-based object creation.
25 *
26 * Objects created that inherit a composition from sdbusplus will all
27 * require at least these expectations.
28 *
29 * If you have future sd_bus_emit_properties_changed_strv calls expected,
30 * you'll need to add those calls into your test. This only captures the
31 * property updates you tell it to expect initially.
32 *
33 * TODO: Make it support more cases, as I'm sure there are more.
34 *
35 * @param[in] sdbus_mock - Pointer to your sdbus mock interface used with
36 * the sdbusplus::bus_t you created.
37 * @param[in] defer - Whether object announcement is deferred.
38 * @param[in] path - the dbus path passed to the object
39 * @param[in] intf - the dbus interface
40 * @param[in] properties - an ordered list of expected property updates.
41 * @param[in] index - a pointer to a valid double in a surviving scope.
42 */
SetupDbusObject(sdbusplus::SdBusMock * sdbus_mock,bool defer,const std::string & path,const std::string & intf,const std::vector<std::string> & properties,double * index)43 void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, bool defer,
44 const std::string& path, const std::string& intf,
45 const std::vector<std::string>& properties, double* index)
46 {
47 if (!defer)
48 {
49 EXPECT_CALL(*sdbus_mock,
50 sd_bus_emit_object_added(IsNull(), StrEq(path)))
51 .WillOnce(Return(0));
52 }
53
54 if (!properties.empty())
55 {
56 (*index) = 0;
57 EXPECT_CALL(*sdbus_mock,
58 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
59 StrEq(intf), NotNull()))
60 .Times(properties.size())
61 .WillRepeatedly(Invoke([=]([[maybe_unused]] sd_bus* bus,
62 [[maybe_unused]] const char* path,
63 [[maybe_unused]] const char* interface,
64 const char** names) {
65 EXPECT_STREQ(properties[(*index)++].c_str(), names[0]);
66 return 0;
67 }));
68 }
69
70 return;
71 }
72
73 } // namespace pid_control
74