Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 1 | // THIS EXISTS AS A COPY OF SDBUSPLUS/TEST/HELPERS.HPP until that is merged. |
| 2 | #pragma once |
| 3 | |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 4 | #include <sdbusplus/test/sdbus_mock.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 5 | |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 12 | namespace pid_control |
| 13 | { |
| 14 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 15 | using ::testing::_; |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 16 | using ::testing::Invoke; |
| 17 | using ::testing::IsNull; |
| 18 | using ::testing::NotNull; |
| 19 | using ::testing::Return; |
| 20 | using ::testing::StrEq; |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 21 | |
| 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 |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 34 | * the sdbusplus::bus_t you created. |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 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 Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 39 | * @param[in] index - a pointer to a valid double in a surviving scope. |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 40 | */ |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 41 | void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, bool defer, |
| 42 | const std::string& path, const std::string& intf, |
James Feist | 0709e2f | 2020-07-08 10:59:45 -0700 | [diff] [blame] | 43 | const std::vector<std::string>& properties, double* index) |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 44 | { |
| 45 | EXPECT_CALL(*sdbus_mock, |
| 46 | sd_bus_add_object_vtable(IsNull(), NotNull(), StrEq(path), |
| 47 | StrEq(intf), NotNull(), NotNull())) |
ykchiu | 7c6d35d | 2023-05-10 17:01:46 +0800 | [diff] [blame] | 48 | .Times(::testing::AnyNumber()) |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 49 | .WillOnce(Return(0)); |
| 50 | |
| 51 | if (!defer) |
| 52 | { |
| 53 | EXPECT_CALL(*sdbus_mock, |
| 54 | sd_bus_emit_object_added(IsNull(), StrEq(path))) |
| 55 | .WillOnce(Return(0)); |
| 56 | } |
| 57 | |
Patrick Venture | a58197c | 2018-06-11 15:29:45 -0700 | [diff] [blame] | 58 | if (!properties.empty()) |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 59 | { |
| 60 | (*index) = 0; |
| 61 | EXPECT_CALL(*sdbus_mock, |
| 62 | sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path), |
| 63 | StrEq(intf), NotNull())) |
| 64 | .Times(properties.size()) |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 65 | .WillRepeatedly(Invoke([=]([[maybe_unused]] sd_bus* bus, |
| 66 | [[maybe_unused]] const char* path, |
| 67 | [[maybe_unused]] const char* interface, |
| 68 | const char** names) { |
Patrick Williams | e1dbb59 | 2023-10-20 11:19:22 -0500 | [diff] [blame] | 69 | EXPECT_STREQ(properties[(*index)++].c_str(), names[0]); |
| 70 | return 0; |
| 71 | })); |
Patrick Venture | 1fa9aab | 2018-06-11 10:46:49 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return; |
| 75 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 76 | |
| 77 | } // namespace pid_control |