diff --git a/autotests/kuniqueservicetest.cpp b/autotests/kuniqueservicetest.cpp index f8940fe5..58800f69 100644 --- a/autotests/kuniqueservicetest.cpp +++ b/autotests/kuniqueservicetest.cpp @@ -1,177 +1,179 @@ /* This file is part of Kleopatra Copyright (c) 2016 Intevation GmbH It is based on libkdbus kdbusservicetest which is: Copyright (c) 1999 Waldo Bastian Copyright (c) 2011 David Faure Copyright (c) 2011 Kevin Ottens This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* The main modification in this test is that every activateRequested * call needs to set the exit code to signal the application it's done. */ #include #include #include #include #include #include #include #include "utils/kuniqueservice.h" #include class TestObject : public QObject { Q_OBJECT public: TestObject(KUniqueService *service) : m_proc(nullptr), m_callCount(0), m_service(service) {} ~TestObject() { if (m_proc) { m_proc->waitForFinished(); } } int callCount() const { return m_callCount; } private Q_SLOTS: void slotActivateRequested(const QStringList &args, const QString &workingDirectory) { Q_UNUSED(workingDirectory); qDebug() << "Application executed with args" << args; ++m_callCount; if (m_callCount == 1) { Q_ASSERT(args.count() == 1); Q_ASSERT(args.at(0) == QLatin1String("dummy call")); m_service->setExitValue(0); } else if (m_callCount == 2) { Q_ASSERT(args.count() == 2); Q_ASSERT(args.at(1) == QLatin1String("bad call")); m_service->setExitValue(4); } else if (m_callCount == 3) { Q_ASSERT(args.count() == 3); Q_ASSERT(args.at(1) == QLatin1String("real call")); Q_ASSERT(args.at(2) == QLatin1String("second arg")); m_service->setExitValue(0); // OK, all done, quit QCoreApplication::instance()->quit(); } } void slotProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) { Q_UNUSED(exitStatus) qDebug() << "Process exited with code" << exitCode; m_proc = nullptr; if (m_callCount == 2) { Q_ASSERT(exitCode == 4); secondCall(); } } void firstCall() { QStringList args; args << QStringLiteral("bad call"); executeNewChild(args); } void secondCall() { QStringList args; args << QStringLiteral("real call") << QStringLiteral("second arg"); executeNewChild(args); } private: void executeNewChild(const QStringList &args) { // Duplicated from kglobalsettingstest.cpp - make a shared helper method? m_proc = new QProcess(this); connect(m_proc, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(slotProcessFinished(int,QProcess::ExitStatus))); QString appName = QStringLiteral("kuniqueservicetest"); #ifdef Q_OS_WIN appName += QStringLiteral(".exe"); #else if (QFile::exists(appName + QStringLiteral(".shell"))) { appName = QStringLiteral("./") + appName + QStringLiteral(".shell"); + } else if (QFile::exists(QCoreApplication::applicationFilePath())) { + appName = QCoreApplication::applicationFilePath(); } else { Q_ASSERT(QFile::exists(appName)); appName = QStringLiteral("./") + appName; } #endif qDebug() << "about to run" << appName << args; m_proc->start(appName, args); } QProcess *m_proc; int m_callCount; KUniqueService *m_service; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QCoreApplication::setApplicationName(QStringLiteral("kuniqueservicetest")); QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org")); KUniqueService service; TestObject testObject(&service); QObject::connect(&service, SIGNAL(activateRequested(QStringList,QString)), &testObject, SLOT(slotActivateRequested(QStringList,QString))); // Testcase for the problem coming from the old fork-on-startup solution: // the "Activate" D-Bus call would time out if the app took too much time // to be ready. //printf("Sleeping.\n"); //sleep(200); QStringList args; args << QStringLiteral("dummy call"); QMetaObject::invokeMethod(&service, "activateRequested", Qt::QueuedConnection, Q_ARG(QStringList, args), Q_ARG(QString, QDir::currentPath())); QTimer::singleShot(400, &testObject, SLOT(firstCall())); qDebug() << "Running."; a.exec(); qDebug() << "Terminating."; Q_ASSERT(testObject.callCount() == 3); const bool ok = testObject.callCount() == 3; return ok ? 0 : 1; } #include "kuniqueservicetest.moc"