1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include <gtest/gtest.h>
#include <ddbusextendedabstractinterface.h>
#include "fakedbus/fakedbusservice.h"
#include <QTest>
#include <QSignalSpy>
DCORE_USE_NAMESPACE
class DBusExtendedInterfaceFoo : public DDBusExtendedAbstractInterface {
Q_OBJECT
public:
static inline const char *staticInterfaceName()
{ return "org.deepin.FakeDBusService"; }
explicit DBusExtendedInterfaceFoo(QObject *parent = nullptr)
: DDBusExtendedAbstractInterface(FakeDBusService::get_service(),
FakeDBusService::get_path(),
staticInterfaceName(),
QDBusConnection::sessionBus(),
parent) {
connect(this, &DBusExtendedInterfaceFoo::propertyChanged, this,
[this](const QString &propName, const QVariant &value){
if (propName == QStringLiteral("strProperty"))
{
const QString &strProperty = qvariant_cast<QString>(value);
if (m_strProp != strProperty)
{
m_strProp = strProperty;
Q_EMIT StrPropertyChanged(m_strProp);
}
return;
}
qWarning() << "property not handle: " << propName;
return;
});
}
~DBusExtendedInterfaceFoo(){
}
Q_PROPERTY(QString strProperty READ strProperty WRITE setStrProperty NOTIFY StrPropertyChanged)
QString strProperty() {
return qvariant_cast<QString>(internalPropGet("strProperty", &m_strProp));
}
void setStrProperty(const QString &value) {
internalPropSet("strProperty", QVariant::fromValue(value), &m_strProp);
}
Q_SIGNALS: // SIGNALS
void StrPropertyChanged(const QString & value) const;
private:
QString m_strProp;
};
class ut_DDBusExtendedAbstractInterface : public testing::Test
{
public:
void SetUp() override
{
m_fakeService = new FakeDBusService;
m_dbusExtend = new DBusExtendedInterfaceFoo;
}
void TearDown() override
{
delete m_fakeService;
delete m_dbusExtend;
}
DBusExtendedInterfaceFoo *m_dbusExtend = nullptr;
FakeDBusService *m_fakeService = nullptr;
};
TEST_F(ut_DDBusExtendedAbstractInterface, sync)
{
EXPECT_TRUE(m_dbusExtend->sync());
m_dbusExtend->setSync(false, false);
EXPECT_FALSE(m_dbusExtend->sync());
}
TEST_F(ut_DDBusExtendedAbstractInterface, useCache)
{
EXPECT_FALSE(m_dbusExtend->useCache());
m_dbusExtend->setUseCache(true);
EXPECT_TRUE(m_dbusExtend->useCache());
}
TEST_F(ut_DDBusExtendedAbstractInterface, startServiceProcess)
{
EXPECT_TRUE(m_dbusExtend->isValid());
m_dbusExtend->startServiceProcess();
}
TEST_F(ut_DDBusExtendedAbstractInterface, getAllProperties)
{
QSignalSpy spy(m_dbusExtend, &DDBusExtendedAbstractInterface::asyncGetAllPropertiesFinished);
m_dbusExtend->setSync(false);
m_dbusExtend->getAllProperties();
EXPECT_TRUE(QTest::qWaitFor([&spy, this]() {
return spy.count() >= 1 && !m_dbusExtend->lastExtendedError().isValid();
}, 2000));
}
TEST_F(ut_DDBusExtendedAbstractInterface, internalPropGet)
{
QSignalSpy asyncPropertyFinishedSpy(m_dbusExtend, &DDBusExtendedAbstractInterface::asyncPropertyFinished);
QSignalSpy propertyChangedSpy(m_dbusExtend, &DDBusExtendedAbstractInterface::propertyChanged);
m_dbusExtend->setSync(false);
// internalPropGet
m_dbusExtend->strProperty();
QObject::connect(m_dbusExtend, &DDBusExtendedAbstractInterface::propertyChanged,
m_dbusExtend, [](const QString &propertyName, const QVariant &value){
if (propertyName == "strProp")
EXPECT_EQ(value.toString(), "testDBusService");
qInfo() << "propertyChanged" << propertyName << value;
});
EXPECT_TRUE(QTest::qWaitFor([&]() {
return asyncPropertyFinishedSpy.count() >= 1 &&
propertyChangedSpy.count() >= 1 &&
!m_dbusExtend->lastExtendedError().isValid();
}, 2000));
}
TEST_F(ut_DDBusExtendedAbstractInterface, internalPropSet)
{
QSignalSpy asyncPropertyFinishedSpy(m_dbusExtend, &DDBusExtendedAbstractInterface::asyncSetPropertyFinished);
QSignalSpy propertyChangedSpy(m_dbusExtend, &DDBusExtendedAbstractInterface::propertyChanged);
m_dbusExtend->setSync(false);
// internalPropSet
m_dbusExtend->setStrProperty("MyProp");
QObject::connect(m_dbusExtend, &DDBusExtendedAbstractInterface::propertyChanged,
m_dbusExtend, [](const QString &propertyName, const QVariant &value){
if (propertyName == "strProp")
EXPECT_EQ(value.toString(), "MyProp");
qInfo() << "propertyChanged" << propertyName << value;
});
EXPECT_TRUE(QTest::qWaitFor([&]() {
return asyncPropertyFinishedSpy.count() >= 1 &&
propertyChangedSpy.count() >= 1 &&
!m_dbusExtend->lastExtendedError().isValid();
}, 2000));
}
#include "ut_ddbusextendedabstractinterface.moc"
|