File indexing completed on 2024-04-14 14:27:13

0001 /*
0002     SPDX-FileCopyrightText: 2017 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "testremoterunner.h"
0008 
0009 #include <QCoreApplication>
0010 #include <QDBusConnection>
0011 #include <QImage>
0012 
0013 #include <iostream>
0014 
0015 #include "krunner1adaptor.h"
0016 
0017 // Test DBus runner, if the search term contains "foo" it returns a match, otherwise nothing
0018 // Run prints a line to stdout
0019 
0020 TestRemoteRunner::TestRemoteRunner(const QString &serviceName, bool showLifecycleMethodCalls)
0021 {
0022     new Krunner1Adaptor(this);
0023     qDBusRegisterMetaType<RemoteMatch>();
0024     qDBusRegisterMetaType<RemoteMatches>();
0025     qDBusRegisterMetaType<RemoteAction>();
0026     qDBusRegisterMetaType<RemoteActions>();
0027     qDBusRegisterMetaType<RemoteImage>();
0028     const bool connected = QDBusConnection::sessionBus().registerService(serviceName);
0029     Q_ASSERT(connected);
0030     const bool registered = QDBusConnection::sessionBus().registerObject(QStringLiteral("/dave"), this);
0031     Q_ASSERT(registered);
0032     m_showLifecycleMethodCalls = showLifecycleMethodCalls;
0033 }
0034 
0035 static RemoteImage serializeImage(const QImage &image)
0036 {
0037     QImage convertedImage = image.convertToFormat(QImage::Format_RGBA8888);
0038     RemoteImage remoteImage;
0039     remoteImage.width = convertedImage.width();
0040     remoteImage.height = convertedImage.height();
0041     remoteImage.rowStride = convertedImage.bytesPerLine();
0042     remoteImage.hasAlpha = true, remoteImage.bitsPerSample = 8;
0043     remoteImage.channels = 4, remoteImage.data = QByteArray(reinterpret_cast<const char *>(convertedImage.constBits()), convertedImage.sizeInBytes());
0044     return remoteImage;
0045 }
0046 
0047 RemoteMatches TestRemoteRunner::Match(const QString &searchTerm)
0048 {
0049     RemoteMatches ms;
0050     std::cout << "Matching:" << qPrintable(searchTerm) << std::endl;
0051     if (searchTerm == QLatin1String("fooCostomIcon")) {
0052         RemoteMatch m;
0053         m.id = QStringLiteral("id2");
0054         m.text = QStringLiteral("Match 1");
0055         m.type = Plasma::QueryMatch::ExactMatch;
0056         m.relevance = 0.8;
0057         QImage icon(10, 10, QImage::Format_RGBA8888);
0058         icon.fill(Qt::blue);
0059         m.properties[QStringLiteral("icon-data")] = QVariant::fromValue(serializeImage(icon));
0060 
0061         ms << m;
0062     } else if (searchTerm.startsWith(QLatin1String("fooDelay"))) {
0063         // This special query string "fooDelayNNNN" allows us to introduce a desired delay
0064         // to simulate a slow query
0065         const int requestedDelayMs = searchTerm.mid(8).toInt();
0066         RemoteMatch m;
0067         m.id = QStringLiteral("id3");
0068         m.text = QStringLiteral("Match 1");
0069         m.iconName = QStringLiteral("icon1");
0070         m.type = Plasma::QueryMatch::ExactMatch;
0071         m.relevance = 0.8;
0072         m.properties[QStringLiteral("actions")] = QStringList(QStringLiteral("action1"));
0073         QThread::msleep(requestedDelayMs);
0074         ms << m;
0075     } else if (searchTerm.contains(QLatin1String("foo"))) {
0076         RemoteMatch m;
0077         m.id = QStringLiteral("id1");
0078         m.text = QStringLiteral("Match 1");
0079         m.iconName = QStringLiteral("icon1");
0080         m.type = Plasma::QueryMatch::ExactMatch;
0081         m.relevance = 0.8;
0082         m.properties[QStringLiteral("actions")] = QStringList(QStringLiteral("action1"));
0083         m.properties[QStringLiteral("multiline")] = true;
0084         ms << m;
0085     }
0086     return ms;
0087 }
0088 
0089 RemoteActions TestRemoteRunner::Actions()
0090 {
0091     RemoteAction action;
0092     action.id = QStringLiteral("action1");
0093     action.text = QStringLiteral("Action 1");
0094     action.iconName = QStringLiteral("document-browser");
0095 
0096     RemoteAction action2;
0097     action2.id = QStringLiteral("action2");
0098     action2.text = QStringLiteral("Action 2");
0099     action2.iconName = QStringLiteral("document-browser");
0100 
0101     return RemoteActions({action, action2});
0102 }
0103 
0104 void TestRemoteRunner::Run(const QString &id, const QString &actionId)
0105 {
0106     std::cout << "Running:" << qPrintable(id) << ":" << qPrintable(actionId) << std::endl;
0107     std::cout.flush();
0108 }
0109 
0110 void TestRemoteRunner::Teardown()
0111 {
0112     if (m_showLifecycleMethodCalls) {
0113         std::cout << "Teardown" << std::endl;
0114         std::cout.flush();
0115     }
0116 }
0117 
0118 QVariantMap TestRemoteRunner::Config()
0119 {
0120     if (m_showLifecycleMethodCalls) {
0121         std::cout << "Config" << std::endl;
0122         std::cout.flush();
0123     }
0124 
0125     return {
0126         {"MatchRegex", "^fo"},
0127         {"MinLetterCount", 4},
0128     };
0129 }
0130 
0131 int main(int argc, char **argv)
0132 {
0133     QCoreApplication app(argc, argv);
0134     const auto arguments = app.arguments();
0135     Q_ASSERT(arguments.count() >= 2);
0136     TestRemoteRunner r(arguments[1], arguments.count() == 3);
0137     app.exec();
0138 }
0139 
0140 #include "moc_testremoterunner.cpp"