diff --git a/runners/services/CMakeLists.txt b/runners/services/CMakeLists.txt --- a/runners/services/CMakeLists.txt +++ b/runners/services/CMakeLists.txt @@ -19,6 +19,17 @@ KF5::Activities ) +add_library(krunner_services_test STATIC ${krunner_services_SRCS}) +target_link_libraries(krunner_services_test + KF5::KIOWidgets + KF5::I18n + KF5::Runner + KF5::Service + KF5::Activities +) + install(TARGETS krunner_services DESTINATION ${KDE_INSTALL_PLUGINDIR} ) install(FILES plasma-runner-services.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR}) + +add_subdirectory(autotests) diff --git a/runners/services/autotests/CMakeLists.txt b/runners/services/autotests/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/runners/services/autotests/CMakeLists.txt @@ -0,0 +1,6 @@ +remove_definitions(-DQT_NO_CAST_FROM_ASCII) + +include(ECMAddTests) + +ecm_add_test(servicerunnertest.cpp TEST_NAME servicerunnertest + LINK_LIBRARIES Qt5::Test krunner_services_test) diff --git a/runners/services/autotests/fixtures/chrome-signal.desktop b/runners/services/autotests/fixtures/chrome-signal.desktop new file mode 100644 --- /dev/null +++ b/runners/services/autotests/fixtures/chrome-signal.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Version=1.0 +Terminal=false +Type=Application +Name=Signal ServiceRunnerTest +Exec=/opt/google/chrome/google-chrome --profile-directory=Default --app-id=bikioccmkafdpakkkcpdbppfkghcmihk +Icon=chrome-bikioccmkafdpakkkcpdbppfkghcmihk-Default +StartupWMClass=crx_bikioccmkafdpakkkcpdbppfkghcmihk + diff --git a/runners/services/autotests/fixtures/google-chrome.desktop b/runners/services/autotests/fixtures/google-chrome.desktop new file mode 100644 --- /dev/null +++ b/runners/services/autotests/fixtures/google-chrome.desktop @@ -0,0 +1,22 @@ +[Desktop Entry] +Version=1.0 +Name=Google Chrome ServiceRunnerTest +GenericName=Web Browser +Comment=Access the Internet +Exec=/usr/bin/google-chrome-stable %U +Terminal=false +Icon=google-chrome +Type=Application +Categories=Network;WebBrowser; +MimeType=text/html;text/xml;application/xhtml_xml;image/webp;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp; +X-Ayatana-Desktop-Shortcuts=NewWindow;NewIncognito + +[NewWindow Shortcut Group] +Name=New Window ServiceRunnerTest +Exec=/usr/bin/google-chrome-stable +TargetEnvironment=Unity + +[NewIncognito Shortcut Group] +Name=New Incognito Window ServiceRunnerTest +Exec=/usr/bin/google-chrome-stable --incognito +TargetEnvironment=Unity diff --git a/runners/services/autotests/fixtures/org.kde.konsole.desktop b/runners/services/autotests/fixtures/org.kde.konsole.desktop new file mode 100644 --- /dev/null +++ b/runners/services/autotests/fixtures/org.kde.konsole.desktop @@ -0,0 +1,25 @@ +[Desktop Entry] +Type=Application +Exec=konsole +Icon=utilities-terminal +Terminal=false +Categories=Qt;KDE;System;TerminalEmulator; +Actions=NewWindow;NewTab; +X-DocPath=konsole/index.html +X-DBUS-StartupType=Unique +StartupNotify=true +X-KDE-AuthorizeAction=shell_access + +Name=Konsole ServiceRunnerTest + +GenericName=Terminal ServiceRunnerTest + +Comment=Command line access ServiceRunnerTest + +[Desktop Action NewWindow] +Name=Open a New Window +Exec=konsole + +[Desktop Action NewTab] +Name=Open a New Tab +Exec=konsole --new-tab diff --git a/runners/services/autotests/fixtures/org.kde.yakuake.desktop b/runners/services/autotests/fixtures/org.kde.yakuake.desktop new file mode 100644 --- /dev/null +++ b/runners/services/autotests/fixtures/org.kde.yakuake.desktop @@ -0,0 +1,12 @@ +[Desktop Entry] +Name=Yakuake ServiceRunnerTest +GenericName=Drop-down Terminal ServiceRunnerTest +Exec=yakuake +Icon=yakuake +Type=Application +Terminal=false +Categories=Qt;KDE;System;TerminalEmulator; +Comment=A drop-down terminal emulator based on KDE Konsole technology. +X-DBUS-StartupType=Unique +X-KDE-StartupNotify=false +X-DBUS-ServiceName=org.kde.yakuake diff --git a/runners/services/autotests/servicerunnertest.cpp b/runners/services/autotests/servicerunnertest.cpp new file mode 100644 --- /dev/null +++ b/runners/services/autotests/servicerunnertest.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2016 Harald Sitter + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include + +#include + +#include "../servicerunner.h" + +class ServiceRunnerTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase(); + void cleanupTestCase(); + + void testChromeAppsRelevance(); + void testKonsoleVsYakuakeComment(); +}; + +void ServiceRunnerTest::initTestCase() +{ + QStandardPaths::setTestModeEnabled(true); + + auto appsPath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation); + QDir(appsPath).removeRecursively(); + QVERIFY(QDir().mkpath(appsPath)); + auto fixtureDir = QDir(QFINDTESTDATA("fixtures")); + for(auto fileInfo : fixtureDir.entryInfoList(QDir::Files)) { + auto source = fileInfo.absoluteFilePath(); + auto target = appsPath + QDir::separator() + fileInfo.fileName(); + QVERIFY2(QFile::copy(fileInfo.absoluteFilePath(), target), + qPrintable(QString("can't copy %1 => %2").arg(source, target))); + } + + setlocale(LC_ALL, "C.utf8"); + + KSycoca::self()->ensureCacheValid(); +} + +void ServiceRunnerTest::cleanupTestCase() +{ +} + +void ServiceRunnerTest::testChromeAppsRelevance() +{ + ServiceRunner runner(this, QVariantList()); + Plasma::RunnerContext context; + context.setQuery("chrome"); + + runner.match(context); + + bool chromeFound = false; + bool signalFound = false; + for (auto match : context.matches()) { + qDebug() << "matched" << match.text(); + if (!match.text().contains("ServiceRunnerTest")) { + continue; + } + + if (match.text() == "Google Chrome ServiceRunnerTest") { + QCOMPARE(match.relevance(), 0.8); + chromeFound = true; + } else if (match.text() == "Signal ServiceRunnerTest") { + // Rates lower because it doesn't have it in the name. + QCOMPARE(match.relevance(), 0.7); + signalFound = true; + } + } + QVERIFY(chromeFound); + QVERIFY(signalFound); +} + +void ServiceRunnerTest::testKonsoleVsYakuakeComment() +{ + // Yakuake has konsole mentioned in comment, should be rated lower. + ServiceRunner runner(this, QVariantList()); + Plasma::RunnerContext context; + context.setQuery("kons"); + + runner.match(context); + + bool konsoleFound = false; + bool yakuakeFound = false; + for (auto match : context.matches()) { + qDebug() << "matched" << match.text(); + if (!match.text().contains("ServiceRunnerTest")) { + continue; + } + + if (match.text() == "Konsole ServiceRunnerTest") { + QCOMPARE(match.relevance(), 0.99); + konsoleFound = true; + } else if (match.text() == "Yakuake ServiceRunnerTest") { + // Rates lower because it doesn't have it in the name. + QCOMPARE(match.relevance(), 0.59); + yakuakeFound = true; + } + } + QVERIFY(konsoleFound); + QVERIFY(yakuakeFound); +} + +QTEST_MAIN(ServiceRunnerTest) + +#include "servicerunnertest.moc"