blob: 931a39ef5ef9c3e67ea04ff2e9181185eb06fef5 [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
52 if (properties.empty())
53 {
54 // We always expect one, but in this case we're not concerned with the
55 // output. If there is more than one property update, we should care
56 // and the test writer can add the code to trigger the below case.
57 EXPECT_CALL(*sdbus_mock,
58 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
59 StrEq(intf), NotNull()))
60 .WillOnce(Return(0));
61 }
62 else
63 {
64 (*index) = 0;
65 EXPECT_CALL(*sdbus_mock,
66 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
67 StrEq(intf), NotNull()))
68 .Times(properties.size())
69 .WillRepeatedly(Invoke([=](sd_bus *bus, const char *path,
70 const char *interface, char **names) {
71 EXPECT_STREQ(properties[(*index)++].c_str(), names[0]);
72 return 0;
73 }));
74 }
75
76 return;
77}