diff --git a/tests/fakedbus/fakedbusservice.h b/tests/fakedbus/fakedbusservice.h index a5daf44..1c0a2f2 100644 --- a/tests/fakedbus/fakedbusservice.h +++ b/tests/fakedbus/fakedbusservice.h @@ -6,6 +6,7 @@ #include #include + #include using Dtk::Core::DDBusInterface; @@ -32,21 +33,29 @@ public: explicit FakeDBusService(QObject *parent = nullptr); ~FakeDBusService(); - Q_PROPERTY(QString strProperty READ strproperty) - inline QString strproperty() { return m_strproperty; } - - Q_PROPERTY(QList objectPaths READ objectpaths); - inline QList objectpaths() { return m_objectpaths; } + Q_PROPERTY(QString strProperty READ strproperty WRITE setStrproperty) + Q_PROPERTY(QList objectPaths READ objectpaths) static QString get_service() { return "org.deepin.FakeDBusService"; } static QString get_path() { return "/org/deepin/FakeDBusService"; } static QString get_interface() { return "org.deepin.FakeDBusService"; } +public Q_SLOTS: + inline QString strproperty() { return m_strproperty; } + void setStrproperty(const QString &var) { + if (var != m_strproperty) + m_strproperty = var; + } + inline QList objectpaths() { return m_objectpaths; } + // ExportScriptableContents + Q_SCRIPTABLE QString foo() { return QString("bar"); } + private: const QString m_service{"org.deepin.FakeDBusService"}; const QString m_path{"/org/deepin/FakeDBusService"}; - QString m_strproperty; - QList m_objectpaths; void registerService(); void unregisterService(); + + QString m_strproperty; + QList m_objectpaths; }; diff --git a/tests/ut_ddbussender.cpp b/tests/ut_ddbussender.cpp new file mode 100644 index 0000000..531c0c8 --- /dev/null +++ b/tests/ut_ddbussender.cpp @@ -0,0 +1,69 @@ +// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: LGPL-3.0-or-later + +#include +#include "ddbussender.h" + +#include +#include + +#include "fakedbus/fakedbusservice.h" + +using Dtk::Core::DDBusInterface; + +class ut_DDBusSender : public testing::Test +{ +public: + void SetUp() override + { + m_testservice = new FakeDBusService(); + m_sender = new DDBusSender; + } + void TearDown() override + { + delete m_testservice; + delete m_sender; + } + + FakeDBusService *m_testservice = nullptr; + DDBusSender *m_sender = nullptr; +}; + + +TEST_F(ut_DDBusSender, DDBusCaller) +{ + QDBusPendingReply reply = m_sender->service(m_testservice->get_service()) + .path(m_testservice->get_path()) + .interface(m_testservice->get_interface()) + .method(QString("foo")) + .call(); + + reply.waitForFinished(); + if (reply.error().isValid()) + qWarning() << reply.error().message(); + + ASSERT_TRUE(reply.value() == QString("bar")); +} + +TEST_F(ut_DDBusSender, DDBusProperty) +{ + auto prop = m_sender->service(m_testservice->get_service()) + .path(m_testservice->get_path()) + .interface(m_testservice->get_interface()) + .property(QString("strProperty")); + + QDBusPendingReply reply = prop.get(); + reply.waitForFinished(); + if (reply.error().isValid()) + qWarning() << reply.error().message(); + + ASSERT_TRUE(reply.value().toString() == QString("testDBusService")); + + auto res = prop.set("myProp"); + res.waitForFinished(); + if (res.error().isValid()) + qWarning() << res.error().message(); + + ASSERT_TRUE(m_testservice->strproperty() == QString("myProp")); +}