File indexing completed on 2024-05-12 03:57:53
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<KRunner::Action>(); 0026 qDBusRegisterMetaType<KRunner::Actions>(); 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.categoryRelevance = qToUnderlying(KRunner::QueryMatch::CategoryRelevance::Highest); 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.categoryRelevance = qToUnderlying(KRunner::QueryMatch::CategoryRelevance::Highest); 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.categoryRelevance = qToUnderlying(KRunner::QueryMatch::CategoryRelevance::Highest); 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 KRunner::Actions TestRemoteRunner::Actions() 0090 { 0091 std::cout << "Actions" << std::endl; 0092 KRunner::Action action("action1", "document-browser", "Action 1"); 0093 0094 KRunner::Action action2("action2", "document-browser", "Action 2"); 0095 return QList<KRunner::Action>{action, action2}; 0096 } 0097 0098 void TestRemoteRunner::Run(const QString &id, const QString &actionId) 0099 { 0100 std::cout << "Running:" << qPrintable(id) << ":" << qPrintable(actionId) << std::endl; 0101 std::cout.flush(); 0102 } 0103 0104 void TestRemoteRunner::Teardown() 0105 { 0106 if (m_showLifecycleMethodCalls) { 0107 std::cout << "Teardown" << std::endl; 0108 std::cout.flush(); 0109 } 0110 } 0111 0112 QVariantMap TestRemoteRunner::Config() 0113 { 0114 if (m_showLifecycleMethodCalls) { 0115 std::cout << "Config" << std::endl; 0116 std::cout.flush(); 0117 } 0118 0119 return { 0120 {"MatchRegex", "^fo"}, 0121 {"MinLetterCount", 4}, 0122 }; 0123 } 0124 0125 int main(int argc, char **argv) 0126 { 0127 QCoreApplication app(argc, argv); 0128 const auto arguments = app.arguments(); 0129 Q_ASSERT(arguments.count() >= 2); 0130 TestRemoteRunner r(arguments[1], arguments.count() == 3); 0131 app.exec(); 0132 } 0133 0134 #include "moc_testremoterunner.cpp"