11fa9aabdSPatrick Venture // THIS EXISTS AS A COPY OF SDBUSPLUS/TEST/HELPERS.HPP until that is merged.
21fa9aabdSPatrick Venture #pragma once
31fa9aabdSPatrick Venture
41fa9aabdSPatrick Venture #include <sdbusplus/test/sdbus_mock.hpp>
5a83a3eccSPatrick Venture
61fa9aabdSPatrick Venture #include <string>
71fa9aabdSPatrick Venture #include <vector>
81fa9aabdSPatrick Venture
9da4a5dd1SPatrick Venture #include <gmock/gmock.h>
10da4a5dd1SPatrick Venture #include <gtest/gtest.h>
11da4a5dd1SPatrick Venture
12a076487aSPatrick Venture namespace pid_control
13a076487aSPatrick Venture {
14a076487aSPatrick Venture
15da4a5dd1SPatrick Venture using ::testing::_;
161fa9aabdSPatrick Venture using ::testing::Invoke;
171fa9aabdSPatrick Venture using ::testing::IsNull;
181fa9aabdSPatrick Venture using ::testing::NotNull;
191fa9aabdSPatrick Venture using ::testing::Return;
201fa9aabdSPatrick Venture using ::testing::StrEq;
211fa9aabdSPatrick Venture
221fa9aabdSPatrick Venture /** @brief Setup the expectations for sdbus-based object creation.
231fa9aabdSPatrick Venture *
241fa9aabdSPatrick Venture * Objects created that inherit a composition from sdbusplus will all
251fa9aabdSPatrick Venture * require at least these expectations.
261fa9aabdSPatrick Venture *
271fa9aabdSPatrick Venture * If you have future sd_bus_emit_properties_changed_strv calls expected,
281fa9aabdSPatrick Venture * you'll need to add those calls into your test. This only captures the
291fa9aabdSPatrick Venture * property updates you tell it to expect initially.
301fa9aabdSPatrick Venture *
311fa9aabdSPatrick Venture * TODO: Make it support more cases, as I'm sure there are more.
321fa9aabdSPatrick Venture *
331fa9aabdSPatrick Venture * @param[in] sdbus_mock - Pointer to your sdbus mock interface used with
34b228bc30SPatrick Williams * the sdbusplus::bus_t you created.
351fa9aabdSPatrick Venture * @param[in] defer - Whether object announcement is deferred.
361fa9aabdSPatrick Venture * @param[in] path - the dbus path passed to the object
371fa9aabdSPatrick Venture * @param[in] intf - the dbus interface
381fa9aabdSPatrick Venture * @param[in] properties - an ordered list of expected property updates.
390709e2f1SJames Feist * @param[in] index - a pointer to a valid double in a surviving scope.
401fa9aabdSPatrick Venture */
SetupDbusObject(sdbusplus::SdBusMock * sdbus_mock,bool defer,const std::string & path,const std::string & intf,const std::vector<std::string> & properties,double * index)411fa9aabdSPatrick Venture void SetupDbusObject(sdbusplus::SdBusMock* sdbus_mock, bool defer,
421fa9aabdSPatrick Venture const std::string& path, const std::string& intf,
430709e2f1SJames Feist const std::vector<std::string>& properties, double* index)
441fa9aabdSPatrick Venture {
451fa9aabdSPatrick Venture if (!defer)
461fa9aabdSPatrick Venture {
471fa9aabdSPatrick Venture EXPECT_CALL(*sdbus_mock,
481fa9aabdSPatrick Venture sd_bus_emit_object_added(IsNull(), StrEq(path)))
491fa9aabdSPatrick Venture .WillOnce(Return(0));
501fa9aabdSPatrick Venture }
511fa9aabdSPatrick Venture
52a58197cfSPatrick Venture if (!properties.empty())
531fa9aabdSPatrick Venture {
541fa9aabdSPatrick Venture (*index) = 0;
551fa9aabdSPatrick Venture EXPECT_CALL(*sdbus_mock,
561fa9aabdSPatrick Venture sd_bus_emit_properties_changed_strv(IsNull(), StrEq(path),
571fa9aabdSPatrick Venture StrEq(intf), NotNull()))
581fa9aabdSPatrick Venture .Times(properties.size())
59*a1ae4fa1SHarvey.Wu .WillRepeatedly(Invoke([=]([[maybe_unused]] sd_bus* bus,
60*a1ae4fa1SHarvey.Wu [[maybe_unused]] const char* path,
61*a1ae4fa1SHarvey.Wu [[maybe_unused]] const char* interface,
62841931d2SHao Jiang const char** names) {
631fa9aabdSPatrick Venture EXPECT_STREQ(properties[(*index)++].c_str(), names[0]);
641fa9aabdSPatrick Venture return 0;
651fa9aabdSPatrick Venture }));
661fa9aabdSPatrick Venture }
671fa9aabdSPatrick Venture
681fa9aabdSPatrick Venture return;
691fa9aabdSPatrick Venture }
70a076487aSPatrick Venture
71a076487aSPatrick Venture } // namespace pid_control
72