diff --git a/src/autotests/DBusTest.cpp b/src/autotests/DBusTest.cpp index e815d507..9121a6a4 100644 --- a/src/autotests/DBusTest.cpp +++ b/src/autotests/DBusTest.cpp @@ -20,8 +20,14 @@ // Own #include "DBusTest.h" #include "../Session.h" +#include "../Profile.h" +#include "../ProfileWriter.h" + +// Qt #include #include +#include +#include using namespace Konsole; @@ -52,10 +58,27 @@ void DBusTest::initTestCase() } } + // Create test profile + auto profile = Profile::Ptr(new Profile()); + auto profileWriter = new ProfileWriter(); + + do { + _testProfileName = QStringLiteral("konsole-dbus-test-profile-%1") + .arg(QRandomGenerator::global()->generate()); + profile->setProperty(Profile::UntranslatedName, _testProfileName); + profile->setProperty(Profile::Name, _testProfileName); + _testProfilePath = profileWriter->getPath(profile); + } while (QFile::exists(_testProfilePath)); + _testProfileEnv = QStringLiteral("TEST_PROFILE=%1").arg(_testProfileName); + profile->setProperty(Profile::Environment, QStringList(_testProfileEnv)); + // %D = Current Directory (Long) - hacky way to check current directory + profile->setProperty(Profile::LocalTabTitleFormat, QStringLiteral("%D")); + profileWriter->writeProfile(_testProfilePath, profile); + // Create a new Konsole with a separate process id _process = new QProcess; _process->setProcessChannelMode(QProcess::ForwardedChannels); - _process->start(QStringLiteral("konsole"), QStringList(QStringLiteral("--separate"))); + _process->start(QStringLiteral("konsole"), QStringList(QStringLiteral("--separate"))); if (!_process->waitForStarted()) { QFAIL(QStringLiteral("Unable to exec a new Konsole").toLatin1().data()); @@ -92,6 +115,9 @@ void DBusTest::initTestCase() */ void DBusTest::cleanupTestCase() { + // Remove test profile + QFile::remove(_testProfilePath); + // Need to take care of when user has CloseAllTabs=False otherwise // they will get a popup dialog when we try to close this. QSignalSpy quitSpy(_process, SIGNAL(finished(int,QProcess::ExitStatus))); @@ -236,4 +262,112 @@ void DBusTest::testSessions() } } +void DBusTest::testWindows() +{ + // Tested functions: + // [+] int sessionCount(); + // [+] QStringList sessionList(); + // [+] int currentSession(); + // [+] void setCurrentSession(int sessionId); + // [+] int newSession(); + // [+] int newSession(const QString &profile); + // [+] int newSession(const QString &profile, const QString &directory); + // [ ] QString defaultProfile(); + // [ ] QStringList profileList(); + // [ ] void nextSession(); + // [ ] void prevSession(); + // [ ] void moveSessionLeft(); + // [ ] void moveSessionRight(); + // [ ] void setTabWidthToText(bool); + + QDBusReply intReply; + QDBusReply listReply; + QDBusReply voidReply; + QDBusReply stringReply; + + int sessionCount = -1; + int initialSessionId = -1; + + QDBusInterface iface(_interfaceName, + QStringLiteral("/Windows/1"), + QStringLiteral("org.kde.konsole.Window")); + QVERIFY(iface.isValid()); + + intReply = iface.call(QStringLiteral("sessionCount")); + QVERIFY(intReply.isValid()); + sessionCount = intReply.value(); + QVERIFY(sessionCount > 0); + + intReply = iface.call(QStringLiteral("currentSession")); + QVERIFY(intReply.isValid()); + initialSessionId = intReply.value(); + + intReply = iface.call(QStringLiteral("newSession")); + QVERIFY(intReply.isValid()); + ++sessionCount; + int newSessionId = intReply.value(); + QVERIFY(newSessionId != initialSessionId); + + listReply = iface.call(QStringLiteral("sessionList")); + QVERIFY(listReply.isValid()); + + QStringList sessions = listReply.value(); + QVERIFY(sessions.contains(QString::number(initialSessionId))); + QVERIFY(sessions.contains(QString::number(newSessionId))); + QVERIFY(sessions.size() == sessionCount); + + intReply = iface.call(QStringLiteral("newSession"), _testProfileName); + QVERIFY(intReply.isValid()); + ++sessionCount; + newSessionId = intReply.value(); + QVERIFY(newSessionId != initialSessionId); + { + QDBusInterface sessionIface(_interfaceName, + QStringLiteral("/Sessions/%1").arg(newSessionId), + QStringLiteral("org.kde.konsole.Session")); + QVERIFY(iface.isValid()); + + listReply = sessionIface.call(QStringLiteral("environment")); + QVERIFY(listReply.isValid()); + QStringList env = listReply.value(); + QVERIFY(env.contains(_testProfileEnv)); + } + + const auto tempDirectories = QStandardPaths::standardLocations(QStandardPaths::TempLocation); + const QString sessionDirectory = tempDirectories.size() > 0 ? tempDirectories.constFirst() : QStringLiteral("/"); + intReply = iface.call(QStringLiteral("newSession"), _testProfileName, sessionDirectory); + QVERIFY(intReply.isValid()); + ++sessionCount; + newSessionId = intReply.value(); + QVERIFY(newSessionId != initialSessionId); + { + QDBusInterface sessionIface(_interfaceName, + QStringLiteral("/Sessions/%1").arg(newSessionId), + QStringLiteral("org.kde.konsole.Session")); + QVERIFY(iface.isValid()); + + listReply = sessionIface.call(QStringLiteral("environment")); + QVERIFY(listReply.isValid()); + QStringList env = listReply.value(); + QVERIFY(env.contains(_testProfileEnv)); + + // Apparently there's no function for checking CWD. + // The profile uses "%D" as title format, so check the title + stringReply = sessionIface.call(QStringLiteral("title"), Session::DisplayedTitleRole); + QVERIFY(stringReply.isValid()); + QVERIFY(QDir(stringReply.value()) == QDir(sessionDirectory)); + } + + voidReply = iface.call(QStringLiteral("setCurrentSession"), initialSessionId); + QVERIFY(voidReply.isValid()); + + intReply = iface.call(QStringLiteral("currentSession")); + QVERIFY(intReply.isValid()); + QVERIFY(intReply.value() == initialSessionId); + + intReply = iface.call(QStringLiteral("sessionCount")); + QVERIFY(intReply.isValid()); + QVERIFY(intReply.value() == sessionCount); +} + QTEST_MAIN(DBusTest) diff --git a/src/autotests/DBusTest.h b/src/autotests/DBusTest.h index c290b8c9..d65f804f 100644 --- a/src/autotests/DBusTest.h +++ b/src/autotests/DBusTest.h @@ -42,6 +42,7 @@ private Q_SLOTS: void initTestCase(); void cleanupTestCase(); void testSessions(); + void testWindows(); // protected slots are not treated as test cases protected Q_SLOTS: @@ -49,6 +50,10 @@ protected Q_SLOTS: private: QString _interfaceName; QProcess *_process = nullptr; + + QString _testProfileName; + QString _testProfilePath; + QString _testProfileEnv; }; }