File indexing completed on 2024-04-14 03:58:00

0001 /*
0002     SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QJsonArray>
0008 #include <QJsonObject>
0009 #include <QSignalSpy>
0010 #include <QStandardPaths>
0011 #include <qtest.h>
0012 
0013 #include "menutest.h"
0014 #include <purpose/alternativesmodel.h>
0015 #include <purpose/menu.h>
0016 
0017 QTEST_MAIN(MenuTest)
0018 
0019 static QAction *saveAsAction(Purpose::Menu *menu)
0020 {
0021     const auto actions = menu->actions();
0022     for (QAction *action : actions) {
0023         if (action->property("pluginId") == QLatin1String("saveasplugin")) {
0024             return action;
0025         }
0026     }
0027 
0028     Q_ASSERT(!"Couldn't find the saveas plugin");
0029     return nullptr;
0030 }
0031 
0032 void MenuTest::initTestCase()
0033 {
0034     // To let ctest exit, we shouldn't start kio_http_cache_cleaner
0035     qputenv("KIO_DISABLE_CACHE_CLEANER", "yes");
0036 }
0037 
0038 void MenuTest::runJobTest()
0039 {
0040     Purpose::Menu *menu = new Purpose::Menu;
0041     Purpose::AlternativesModel *model = menu->model();
0042     model->setDisabledPlugins({});
0043 
0044     const QString tempfile = m_tempDir.path() + QStringLiteral("/purposetest");
0045     QFile::remove(tempfile);
0046     const QJsonObject input = QJsonObject{{QStringLiteral("urls"), QJsonArray{QStringLiteral("http://kde.org")}},
0047                                           {QStringLiteral("mimeType"), QStringLiteral("dummy/thing")},
0048                                           {QStringLiteral("destinationPath"), QUrl::fromLocalFile(tempfile).url()}};
0049     model->setInputData(input);
0050     model->setPluginType(QStringLiteral("Export"));
0051     menu->reload();
0052 
0053     int error = -1;
0054     QJsonObject output;
0055     connect(menu, &Purpose::Menu::finished, menu, [&error, &output](const QJsonObject &_output, int _error, const QString &errorMessage) {
0056         error = _error;
0057         output = _output;
0058         if (error != 0) {
0059             qDebug() << "job failed with error" << errorMessage;
0060         }
0061     });
0062 
0063     QAction *action = saveAsAction(menu);
0064     QSignalSpy s(menu, &Purpose::Menu::finished);
0065     action->trigger();
0066     QVERIFY(s.count() || s.wait());
0067     QCOMPARE(error, 0);
0068     QCOMPARE(output.count(), 1);
0069     QVERIFY(QFile::remove(tempfile));
0070 
0071     delete menu;
0072 }
0073 
0074 #include "moc_menutest.cpp"