File: ut_dexportedinterface.cpp

package info (click to toggle)
dtkcore 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: cpp: 22,021; ansic: 183; python: 68; xml: 58; makefile: 27; sh: 15
file content (97 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download
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
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gtest/gtest.h>
#include <ddbussender.h>
#include <dexportedinterface.h>

#include <QtDBus>

DCORE_USE_NAMESPACE

class ut_DExportedInterface : public testing::Test
{
public:
    static void SetUpTestSuite() {
        QDBusConnection::sessionBus().registerService("org.deepin.ExpIntfTest");
        _infc = new DUtil::DExportedInterface;
        _infc->registerAction("quit", "quit the application", [](QString)->QVariant {
            qDebug() << "quit? No Way!";
            return QVariant();
        });

        _infc->registerAction("answer", "answer to the ultimate question of life, the universe, and everything",
                           [](QString)->QVariant {
            return QVariant(42);
        });
    }
    static void TearDownTestSuite() {
        QDBusConnection::sessionBus().unregisterService("org.deepin.ExpIntfTest");
        delete _infc;
    }

    static DUtil::DExportedInterface *_infc;
};

DUtil::DExportedInterface *ut_DExportedInterface::_infc = nullptr;

TEST_F(ut_DExportedInterface, list)
{
    auto caller = DDBusSender().service("org.deepin.ExpIntfTest")
            .path("/")
            .interface("com.deepin.ExportedInterface")
            .method("list");

    auto msg = caller.call();
    msg.waitForFinished();

    QDBusReply<QStringList> reply = msg;

    EXPECT_EQ(reply.value().size(), 2);
}

TEST_F(ut_DExportedInterface, help)
{
    auto caller = DDBusSender().service("org.deepin.ExpIntfTest")
            .path("/")
            .interface("com.deepin.ExportedInterface")
            .method("help")
            .arg(QString("quit"));

    auto msg = caller.call();
    msg.waitForFinished();

    QDBusReply<QString> reply = msg;

    EXPECT_EQ(reply.value(), "quit: quit the application");
}

TEST_F(ut_DExportedInterface, invoke)
{
    auto caller = DDBusSender().service("org.deepin.ExpIntfTest")
            .path("/")
            .interface("com.deepin.ExportedInterface")
            .method("invoke")
            .arg(QString("answer"))
            .arg(QString("balabala"));

    auto msg = caller.call();
    msg.waitForFinished();

    QDBusReply<QVariant> reply = msg;

    EXPECT_EQ(reply.value(), 42);

    caller = DDBusSender().service("org.deepin.ExpIntfTest")
                .path("/")
                .interface("com.deepin.ExportedInterface")
                .method("invoke")
                .arg(QString("unkownAction")) // invoke an unregistered action
                .arg(QString("balabala"));

    msg = caller.call();
    msg.waitForFinished();
    EXPECT_TRUE(msg.isError());
    EXPECT_TRUE(msg.error().type() == QDBusError::ErrorType::InvalidArgs);
}