1 // THIS EXISTS AS A COPY OF SDBUSPLUS/TEST/HELPERS.HPP until that is merged. 2 #pragma once 3 4 #include <sdbusplus/test/sdbus_mock.hpp> 5 6 #include <string> 7 #include <vector> 8 9 #include <gmock/gmock.h> 10 #include <gtest/gtest.h> 11 12 using ::testing::_; 13 using ::testing::Invoke; 14 using ::testing::IsNull; 15 using ::testing::NotNull; 16 using ::testing::Return; 17 using ::testing::StrEq; 18 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. 36 * @param[in] index - a pointer to a valid double in a surviving scope. 37 */ 38 void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, bool defer, 39 const std::string& path, const std::string& intf, 40 const std::vector<std::string>& properties, double* index) 41 { 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 54 if (!properties.empty()) 55 { 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()) 61 .WillRepeatedly(Invoke([=](sd_bus* bus, const char* path, 62 const char* interface, char** names) { 63 EXPECT_STREQ(properties[(*index)++].c_str(), names[0]); 64 return 0; 65 })); 66 } 67 68 return; 69 } 70