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