blob: bcb269ac8a9f917bfab163793cb809e53dfdadb7 [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
Ed Tanousf8b6e552025-06-27 13:27:50 -07004#include <systemd/sd-bus.h>
5
Patrick Venture1fa9aab2018-06-11 10:46:49 -07006#include <sdbusplus/test/sdbus_mock.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -07007
Patrick Venture1fa9aab2018-06-11 10:46:49 -07008#include <string>
9#include <vector>
10
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070011#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
Patrick Venturea0764872020-08-08 07:48:43 -070014namespace pid_control
15{
16
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070017using ::testing::_;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070018using ::testing::Invoke;
19using ::testing::IsNull;
20using ::testing::NotNull;
21using ::testing::Return;
22using ::testing::StrEq;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070023
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
Patrick Williamsb228bc32022-07-22 19:26:56 -050036 * the sdbusplus::bus_t you created.
Patrick Venture1fa9aab2018-06-11 10:46:49 -070037 * @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.
James Feist0709e2f2020-07-08 10:59:45 -070041 * @param[in] index - a pointer to a valid double in a surviving scope.
Patrick Venture1fa9aab2018-06-11 10:46:49 -070042 */
Patrick Venturee2ec0f62018-09-04 12:30:27 -070043void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, bool defer,
44 const std::string& path, const std::string& intf,
James Feist0709e2f2020-07-08 10:59:45 -070045 const std::vector<std::string>& properties, double* index)
Patrick Venture1fa9aab2018-06-11 10:46:49 -070046{
Patrick Venture1fa9aab2018-06-11 10:46:49 -070047 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())
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080061 .WillRepeatedly(Invoke([=]([[maybe_unused]] sd_bus* bus,
62 [[maybe_unused]] const char* path,
63 [[maybe_unused]] const char* interface,
64 const char** names) {
Patrick Williamsbd63bca2024-08-16 15:21:10 -040065 EXPECT_STREQ(properties[(*index)++].c_str(), names[0]);
66 return 0;
67 }));
Patrick Venture1fa9aab2018-06-11 10:46:49 -070068 }
69
70 return;
71}
Patrick Venturea0764872020-08-08 07:48:43 -070072
73} // namespace pid_control