blob: 1b5486e5035527f7144b2223ae3128eab1bde29c [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
Patrick Venturea0764872020-08-08 07:48:43 -070012namespace pid_control
13{
14
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070015using ::testing::_;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070016using ::testing::Invoke;
17using ::testing::IsNull;
18using ::testing::NotNull;
19using ::testing::Return;
20using ::testing::StrEq;
Patrick Venture1fa9aab2018-06-11 10:46:49 -070021
22/** @brief Setup the expectations for sdbus-based object creation.
23 *
24 * Objects created that inherit a composition from sdbusplus will all
25 * require at least these expectations.
26 *
27 * If you have future sd_bus_emit_properties_changed_strv calls expected,
28 * you'll need to add those calls into your test. This only captures the
29 * property updates you tell it to expect initially.
30 *
31 * TODO: Make it support more cases, as I'm sure there are more.
32 *
33 * @param[in] sdbus_mock - Pointer to your sdbus mock interface used with
34 * the sdbusplus::bus::bus you created.
35 * @param[in] defer - Whether object announcement is deferred.
36 * @param[in] path - the dbus path passed to the object
37 * @param[in] intf - the dbus interface
38 * @param[in] properties - an ordered list of expected property updates.
James Feist0709e2f2020-07-08 10:59:45 -070039 * @param[in] index - a pointer to a valid double in a surviving scope.
Patrick Venture1fa9aab2018-06-11 10:46:49 -070040 */
Patrick Venturee2ec0f62018-09-04 12:30:27 -070041void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, bool defer,
42 const std::string& path, const std::string& intf,
James Feist0709e2f2020-07-08 10:59:45 -070043 const std::vector<std::string>& properties, double* index)
Patrick Venture1fa9aab2018-06-11 10:46:49 -070044{
45 EXPECT_CALL(*sdbus_mock,
46 sd_bus_add_object_vtable(IsNull(), NotNull(), StrEq(path),
47 StrEq(intf), NotNull(), NotNull()))
48 .WillOnce(Return(0));
49
50 if (!defer)
51 {
52 EXPECT_CALL(*sdbus_mock,
53 sd_bus_emit_object_added(IsNull(), StrEq(path)))
54 .WillOnce(Return(0));
55 }
56
Patrick Venturea58197c2018-06-11 15:29:45 -070057 if (!properties.empty())
Patrick Venture1fa9aab2018-06-11 10:46:49 -070058 {
59 (*index) = 0;
60 EXPECT_CALL(*sdbus_mock,
61 sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
62 StrEq(intf), NotNull()))
63 .Times(properties.size())
Hao Jiang841931d2021-02-24 14:45:40 -080064 .WillRepeatedly(
65 Invoke([=](sd_bus* bus, const char* path, const char* interface,
66 const char** names) {
67 EXPECT_STREQ(properties[(*index)++].c_str(), names[0]);
68 return 0;
69 }));
Patrick Venture1fa9aab2018-06-11 10:46:49 -070070 }
71
72 return;
73}
Patrick Venturea0764872020-08-08 07:48:43 -070074
75} // namespace pid_control