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