| 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 <ddbussender.h> |
| 7 |
#include <dexportedinterface.h> |
| 8 |
|
| 9 |
#include <QtDBus> |
| 10 |
|
| 11 |
DCORE_USE_NAMESPACE |
| 12 |
|
| 13 |
class ut_DExportedInterface : public testing::Test |
| 14 |
{ |
| 15 |
public: |
| 16 |
static void SetUpTestSuite() { |
| 17 |
QDBusConnection::sessionBus().registerService("org.deepin.ExpIntfTest"); |
| 18 |
_infc = new DUtil::DExportedInterface; |
| 19 |
_infc->registerAction("quit", "quit the application", [](QString)->QVariant { |
| 20 |
qDebug() << "quit? No Way!"; |
| 21 |
return QVariant(); |
| 22 |
}); |
| 23 |
|
| 24 |
_infc->registerAction("answer", "answer to the ultimate question of life, the universe, and everything", |
| 25 |
[](QString)->QVariant { |
| 26 |
return QVariant(42); |
| 27 |
}); |
| 28 |
} |
| 29 |
static void TearDownTestSuite() { |
| 30 |
QDBusConnection::sessionBus().unregisterService("org.deepin.ExpIntfTest"); |
| 31 |
delete _infc; |
| 32 |
} |
| 33 |
|
| 34 |
static DUtil::DExportedInterface *_infc; |
| 35 |
}; |
| 36 |
|
| 37 |
DUtil::DExportedInterface *ut_DExportedInterface::_infc = nullptr; |
| 38 |
|
| 39 |
TEST_F(ut_DExportedInterface, list) |
| 40 |
{ |
| 41 |
auto caller = DDBusSender().service("org.deepin.ExpIntfTest") |
| 42 |
.path("/") |
| 43 |
.interface("com.deepin.ExportedInterface") |
| 44 |
.method("list"); |
| 45 |
|
| 46 |
auto msg = caller.call(); |
| 47 |
msg.waitForFinished(); |
| 48 |
|
| 49 |
QDBusReply<QStringList> reply = msg; |
| 50 |
|
| 51 |
EXPECT_EQ(reply.value().size(), 2); |
| 52 |
} |
| 53 |
|
| 54 |
TEST_F(ut_DExportedInterface, help) |
| 55 |
{ |
| 56 |
auto caller = DDBusSender().service("org.deepin.ExpIntfTest") |
| 57 |
.path("/") |
| 58 |
.interface("com.deepin.ExportedInterface") |
| 59 |
.method("help") |
| 60 |
.arg(QString("quit")); |
| 61 |
|
| 62 |
auto msg = caller.call(); |
| 63 |
msg.waitForFinished(); |
| 64 |
|
| 65 |
QDBusReply<QString> reply = msg; |
| 66 |
|
| 67 |
EXPECT_EQ(reply.value(), "quit: quit the application"); |
| 68 |
} |
| 69 |
|
| 70 |
TEST_F(ut_DExportedInterface, invoke) |
| 71 |
{ |
| 72 |
auto caller = DDBusSender().service("org.deepin.ExpIntfTest") |
| 73 |
.path("/") |
| 74 |
.interface("com.deepin.ExportedInterface") |
| 75 |
.method("invoke") |
| 76 |
.arg(QString("answer")) |
| 77 |
.arg(QString("balabala")); |
| 78 |
|
| 79 |
auto msg = caller.call(); |
| 80 |
msg.waitForFinished(); |
| 81 |
|
| 82 |
QDBusReply<QVariant> reply = msg; |
| 83 |
|
| 84 |
EXPECT_EQ(reply.value(), 42); |
| 85 |
|
| 86 |
caller = DDBusSender().service("org.deepin.ExpIntfTest") |
| 87 |
.path("/") |
| 88 |
.interface("com.deepin.ExportedInterface") |
| 89 |
.method("invoke") |
| 90 |
.arg(QString("unkownAction")) // invoke an unregistered action |
| 91 |
.arg(QString("balabala")); |
| 92 |
|
| 93 |
msg = caller.call(); |
| 94 |
msg.waitForFinished(); |
| 95 |
EXPECT_TRUE(msg.isError()); |
| 96 |
EXPECT_TRUE(msg.error().type() == QDBusError::ErrorType::InvalidArgs); |
| 97 |
} |