File: testremoterunner.cpp

package info (click to toggle)
krunner 5.116.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 828 kB
  • sloc: cpp: 4,954; sh: 46; xml: 25; python: 23; makefile: 7
file content (140 lines) | stat: -rw-r--r-- 4,621 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
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
/*
    SPDX-FileCopyrightText: 2017 David Edmundson <davidedmundson@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "testremoterunner.h"

#include <QCoreApplication>
#include <QDBusConnection>
#include <QImage>

#include <iostream>

#include "krunner1adaptor.h"

// Test DBus runner, if the search term contains "foo" it returns a match, otherwise nothing
// Run prints a line to stdout

TestRemoteRunner::TestRemoteRunner(const QString &serviceName, bool showLifecycleMethodCalls)
{
    new Krunner1Adaptor(this);
    qDBusRegisterMetaType<RemoteMatch>();
    qDBusRegisterMetaType<RemoteMatches>();
    qDBusRegisterMetaType<RemoteAction>();
    qDBusRegisterMetaType<RemoteActions>();
    qDBusRegisterMetaType<RemoteImage>();
    const bool connected = QDBusConnection::sessionBus().registerService(serviceName);
    Q_ASSERT(connected);
    const bool registered = QDBusConnection::sessionBus().registerObject(QStringLiteral("/dave"), this);
    Q_ASSERT(registered);
    m_showLifecycleMethodCalls = showLifecycleMethodCalls;
}

static RemoteImage serializeImage(const QImage &image)
{
    QImage convertedImage = image.convertToFormat(QImage::Format_RGBA8888);
    RemoteImage remoteImage;
    remoteImage.width = convertedImage.width();
    remoteImage.height = convertedImage.height();
    remoteImage.rowStride = convertedImage.bytesPerLine();
    remoteImage.hasAlpha = true, remoteImage.bitsPerSample = 8;
    remoteImage.channels = 4, remoteImage.data = QByteArray(reinterpret_cast<const char *>(convertedImage.constBits()), convertedImage.sizeInBytes());
    return remoteImage;
}

RemoteMatches TestRemoteRunner::Match(const QString &searchTerm)
{
    RemoteMatches ms;
    std::cout << "Matching:" << qPrintable(searchTerm) << std::endl;
    if (searchTerm == QLatin1String("fooCostomIcon")) {
        RemoteMatch m;
        m.id = QStringLiteral("id2");
        m.text = QStringLiteral("Match 1");
        m.type = Plasma::QueryMatch::ExactMatch;
        m.relevance = 0.8;
        QImage icon(10, 10, QImage::Format_RGBA8888);
        icon.fill(Qt::blue);
        m.properties[QStringLiteral("icon-data")] = QVariant::fromValue(serializeImage(icon));

        ms << m;
    } else if (searchTerm.startsWith(QLatin1String("fooDelay"))) {
        // This special query string "fooDelayNNNN" allows us to introduce a desired delay
        // to simulate a slow query
        const int requestedDelayMs = searchTerm.mid(8).toInt();
        RemoteMatch m;
        m.id = QStringLiteral("id3");
        m.text = QStringLiteral("Match 1");
        m.iconName = QStringLiteral("icon1");
        m.type = Plasma::QueryMatch::ExactMatch;
        m.relevance = 0.8;
        m.properties[QStringLiteral("actions")] = QStringList(QStringLiteral("action1"));
        QThread::msleep(requestedDelayMs);
        ms << m;
    } else if (searchTerm.contains(QLatin1String("foo"))) {
        RemoteMatch m;
        m.id = QStringLiteral("id1");
        m.text = QStringLiteral("Match 1");
        m.iconName = QStringLiteral("icon1");
        m.type = Plasma::QueryMatch::ExactMatch;
        m.relevance = 0.8;
        m.properties[QStringLiteral("actions")] = QStringList(QStringLiteral("action1"));
        m.properties[QStringLiteral("multiline")] = true;
        ms << m;
    }
    return ms;
}

RemoteActions TestRemoteRunner::Actions()
{
    RemoteAction action;
    action.id = QStringLiteral("action1");
    action.text = QStringLiteral("Action 1");
    action.iconName = QStringLiteral("document-browser");

    RemoteAction action2;
    action2.id = QStringLiteral("action2");
    action2.text = QStringLiteral("Action 2");
    action2.iconName = QStringLiteral("document-browser");

    return RemoteActions({action, action2});
}

void TestRemoteRunner::Run(const QString &id, const QString &actionId)
{
    std::cout << "Running:" << qPrintable(id) << ":" << qPrintable(actionId) << std::endl;
    std::cout.flush();
}

void TestRemoteRunner::Teardown()
{
    if (m_showLifecycleMethodCalls) {
        std::cout << "Teardown" << std::endl;
        std::cout.flush();
    }
}

QVariantMap TestRemoteRunner::Config()
{
    if (m_showLifecycleMethodCalls) {
        std::cout << "Config" << std::endl;
        std::cout.flush();
    }

    return {
        {"MatchRegex", "^fo"},
        {"MinLetterCount", 4},
    };
}

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    const auto arguments = app.arguments();
    Q_ASSERT(arguments.count() >= 2);
    TestRemoteRunner r(arguments[1], arguments.count() == 3);
    app.exec();
}

#include "moc_testremoterunner.cpp"