File indexing completed on 2024-04-21 03:55:55

0001 /*
0002     SPDX-FileCopyrightText: 2002 David Faure <faure@kde.org>
0003     SPDX-FileCopyrightText: 2003 Waldo Bastian <bastian@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "kruntest.h"
0009 
0010 #include <KIO/ApplicationLauncherJob>
0011 #include <KIO/JobUiDelegate>
0012 #include <KIO/JobUiDelegateFactory>
0013 #include <KIO/OpenUrlJob>
0014 
0015 #include <KService>
0016 
0017 #include <QApplication>
0018 #include <QDebug>
0019 #include <QLabel>
0020 #include <QLayout>
0021 #include <QPushButton>
0022 #include <QTest> // QFINDTESTDATA
0023 
0024 #include <QDir>
0025 #include <qplatformdefs.h>
0026 
0027 static const int s_maxJobs = 100;
0028 
0029 static KIO::OpenUrlJob *jobArray[s_maxJobs];
0030 
0031 static const char testFile[] = "kruntest.cpp";
0032 
0033 static const struct {
0034     const char *text;
0035     const char *expectedResult;
0036     const char *exec;
0037     const char *url;
0038 } s_tests[] = {
0039     {"run(kwrite, no url)", "should work normally", "kwrite", nullptr},
0040     {"run(kwrite, file url)", "should work normally", "kwrite", testFile},
0041     {"run(kwrite, remote url)", "should work normally", "kwrite", "http://www.kde.org"},
0042     {"run(doesnotexit, no url)", "should show error message", "doesnotexist", nullptr},
0043     {"run(doesnotexit, file url)", "should show error message", "doesnotexist", testFile},
0044     {"run(doesnotexit, remote url)", "should use kioexec and show error message", "doesnotexist", "http://www.kde.org"},
0045     {"run(not-executable-desktopfile)", "should ask for confirmation", "nonexec", nullptr},
0046     {"run(missing lib, no url)",
0047      "should show error message (remove libqca-qt5.so.2 for this, e.g. by editing LD_LIBRARY_PATH if qca is in its own prefix)",
0048      "qcatool-qt5",
0049      nullptr},
0050     {"run(missing lib, file url)",
0051      "should show error message (remove libqca-qt5.so.2 for this, e.g. by editing LD_LIBRARY_PATH if qca is in its own prefix)",
0052      "qcatool-qt5",
0053      testFile},
0054     {"run(missing lib, remote url)",
0055      "should show error message (remove libqca-qt5.so.2 for this, e.g. by editing LD_LIBRARY_PATH if qca is in its own prefix)",
0056      "qcatool-qt5",
0057      "http://www.kde.org"},
0058 };
0059 
0060 Receiver::Receiver()
0061 {
0062     QVBoxLayout *lay = new QVBoxLayout(this);
0063     QPushButton *btn = new QPushButton(QStringLiteral("Press here to terminate"), this);
0064     lay->addWidget(btn);
0065     connect(btn, &QPushButton::clicked, qApp, &QApplication::quit);
0066 
0067     start = new QPushButton(QStringLiteral("Launch OpenUrlJobs"), this);
0068     lay->addWidget(start);
0069     connect(start, &QAbstractButton::clicked, this, &Receiver::slotStart);
0070 
0071     stop = new QPushButton(QStringLiteral("Stop those OpenUrlJobs"), this);
0072     stop->setEnabled(false);
0073     lay->addWidget(stop);
0074     connect(stop, &QAbstractButton::clicked, this, &Receiver::slotStop);
0075 
0076     QPushButton *launchOne = new QPushButton(QStringLiteral("Launch one http OpenUrlJob"), this);
0077     lay->addWidget(launchOne);
0078     connect(launchOne, &QAbstractButton::clicked, this, &Receiver::slotLaunchOne);
0079 
0080     for (uint i = 0; i < sizeof(s_tests) / sizeof(*s_tests); ++i) {
0081         QHBoxLayout *hbox = new QHBoxLayout;
0082         lay->addLayout(hbox);
0083         QPushButton *button = new QPushButton(QString::fromUtf8(s_tests[i].text), this);
0084         button->setProperty("testNumber", i);
0085         hbox->addWidget(button);
0086         QLabel *label = new QLabel(QString::fromUtf8(s_tests[i].expectedResult), this);
0087         hbox->addWidget(label);
0088         connect(button, &QAbstractButton::clicked, this, [this, button]() {
0089             slotLaunchTest(button);
0090         });
0091         hbox->addStretch();
0092     }
0093 
0094     adjustSize();
0095     show();
0096 }
0097 
0098 void Receiver::slotLaunchTest(QPushButton *sender)
0099 {
0100     const int testNumber = sender->property("testNumber").toInt();
0101     QList<QUrl> urls;
0102     if (s_tests[testNumber].url) {
0103         QString urlStr = QString::fromUtf8(s_tests[testNumber].url);
0104         if (urlStr == QLatin1String(testFile)) {
0105             urlStr = QFINDTESTDATA(testFile);
0106         }
0107         urls << QUrl::fromUserInput(urlStr);
0108     }
0109     KService::Ptr service;
0110     if (QByteArray(s_tests[testNumber].exec) == "nonexec") {
0111         const QString desktopFile = QFINDTESTDATA("../src/kioworkers/trash/kcmtrash.desktop");
0112         if (desktopFile.isEmpty()) {
0113             qWarning() << "kcmtrash.desktop not found!";
0114         }
0115         const QString dest = QStringLiteral("kcmtrash.desktop");
0116         QFile::remove(dest);
0117         bool ok = QFile::copy(desktopFile, dest);
0118         if (!ok) {
0119             qWarning() << "Failed to copy" << desktopFile << "to" << dest;
0120         }
0121         service = KService::Ptr(new KService(QDir::currentPath() + QLatin1Char('/') + dest));
0122     } else {
0123         service = KService::Ptr(new KService(QStringLiteral("Some Name"), QString::fromLatin1(s_tests[testNumber].exec), QString()));
0124     }
0125     auto *job = new KIO::ApplicationLauncherJob(service, this);
0126     job->setUrls(urls);
0127     job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
0128     job->start();
0129 }
0130 
0131 void Receiver::slotStop()
0132 {
0133     for (int i = 0; i < s_maxJobs; i++) {
0134         qDebug() << "deleting job" << i;
0135         delete jobArray[i];
0136     }
0137     start->setEnabled(true);
0138     stop->setEnabled(false);
0139 }
0140 
0141 void Receiver::slotStart()
0142 {
0143     for (int i = 0; i < s_maxJobs; i++) {
0144         qDebug() << "creating testjob" << i;
0145         jobArray[i] = new KIO::OpenUrlJob(QUrl::fromLocalFile(QDir::tempPath()));
0146         jobArray[i]->setAutoDelete(false);
0147         jobArray[i]->start();
0148     }
0149     start->setEnabled(false);
0150     stop->setEnabled(true);
0151 }
0152 
0153 void Receiver::slotLaunchOne()
0154 {
0155     auto *job = new KIO::OpenUrlJob(QUrl(QStringLiteral("http://www.kde.org")));
0156     job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
0157     job->start();
0158 }
0159 
0160 int main(int argc, char **argv)
0161 {
0162     QApplication::setApplicationName(QStringLiteral("kruntest"));
0163     QApplication app(argc, argv);
0164 
0165     Receiver receiver;
0166     return app.exec();
0167 }
0168 
0169 #include "moc_kruntest.cpp"