blob: 1ac21ca3af20835db74cf735e992713eebad7d5d [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
Patrick Venture1fa9aab2018-06-11 10:46:49 -07004#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -07005
Patrick Venture1fa9aab2018-06-11 10:46:49 -07006#include <string>
7#include <vector>
8
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07009#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
12using ::testing::_;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070013using ::testing::Invoke;
14using ::testing::IsNull;
15using ::testing::NotNull;
16using ::testing::Return;
17using ::testing::StrEq;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070018
19/** @brief Setup the expectations for sdbus-based object creation.
20 *
21 * Objects created that inherit a composition from sdbusplus will all
22 * require at least these expectations.
23 *
24 * If you have future sd_bus_emit_properties_changed_strv calls expected,
25 * you'll need to add those calls into your test. This only captures the
26 * property updates you tell it to expect initially.
27 *
28 * TODO: Make it support more cases, as I'm sure there are more.
29 *
30 * @param[in] sdbus_mock - Pointer to your sdbus mock interface used with
31 * the sdbusplus::bus::bus you created.
32 * @param[in] defer - Whether object announcement is deferred.
33 * @param[in] path - the dbus path passed to the object
34 * @param[in] intf - the dbus interface
35 * @param[in] properties - an ordered list of expected property updates.
James Feist0709e2f2020-07-08 10:59:45 -070036 * @param[in] index - a pointer to a valid double in a surviving scope.
Patrick Venture1fa9aab2018-06-11 10:46:49 -070037 */
Patrick Venturee2ec0f62018-09-04 12:30:27 -070038void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, bool defer,
39 const std::string& path, const std::string& intf,
James Feist0709e2f2020-07-08 10:59:45 -070040 const std::vector<std::string>& properties, double* index)
Patrick Venture1fa9aab2018-06-11 10:46:49 -070041{
42 EXPECT_CALL(*sdbus_mock,
43 sd_bus_add_object_vtable(IsNull(), NotNull(), StrEq(path),
44 StrEq(intf), NotNull(), NotNull()))
45 .WillOnce(Return(0));
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
Patrick Venturea58197c2018-06-11 15:29:45 -070054 if (!properties.empty())
Patrick Venture1fa9aab2018-06-11 10:46:49 -070055 {
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())
Patrick Venturee2ec0f62018-09-04 12:30:27 -070061 .WillRepeatedly(Invoke([=](sd_bus* bus, const char* path,
62 const char* interface, char** names) {
Patrick Venture1fa9aab2018-06-11 10:46:49 -070063 EXPECT_STREQ(properties[(*index)++].c_str(), names[0]);
64 return 0;
65 }));
66 }
67
68 return;
69}