chore: add ut for DDbusSender

- tweek FakeDBusService. add method and read/write propery
- test DDbusSender. call dbus method and get/set dbus property
This commit is contained in:
ck 2023-07-10 08:49:58 +08:00 committed by mike
parent d0b7680edb
commit 3a661dfff0
2 changed files with 85 additions and 7 deletions

View File

@ -6,6 +6,7 @@
#include <QStringList>
#include <QDBusObjectPath>
#include <ddbusinterface.h>
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<QDBusObjectPath> objectPaths READ objectpaths);
inline QList<QDBusObjectPath> objectpaths() { return m_objectpaths; }
Q_PROPERTY(QString strProperty READ strproperty WRITE setStrproperty)
Q_PROPERTY(QList<QDBusObjectPath> 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<QDBusObjectPath> 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<QDBusObjectPath> m_objectpaths;
void registerService();
void unregisterService();
QString m_strproperty;
QList<QDBusObjectPath> m_objectpaths;
};

69
tests/ut_ddbussender.cpp Normal file
View File

@ -0,0 +1,69 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <gtest/gtest.h>
#include "ddbussender.h"
#include <QtDBus>
#include <QDebug>
#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<QString> 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<QVariant> 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"));
}