File indexing completed on 2024-05-12 04:38:22

0001 /*
0002     SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "test_sessioncontroller.h"
0008 
0009 #include <tests/autotestshell.h>
0010 #include <tests/testcore.h>
0011 
0012 #include <KConfigGroup>
0013 #include <KLocalizedString>
0014 
0015 #include "../core.h"
0016 #include "../sessioncontroller.h"
0017 #include "../session.h"
0018 #include <QDebug>
0019 #include <QFileInfo>
0020 #include <QSignalSpy>
0021 #include <QTest>
0022 
0023 using namespace KDevelop;
0024 
0025 //////////////////// Helper Functions ////////////////////////////////////////
0026 
0027 QString sessionDir( ISession* s )
0028 {
0029     return SessionController::sessionDirectory(s->id().toString());
0030 }
0031 
0032 void verifySessionDir( const QString& sessiondir, const QString& name, bool exists )
0033 {
0034     if( exists )
0035     {
0036         qDebug() << "checking existing session" << sessiondir;
0037         QVERIFY( QFileInfo::exists( sessiondir ) );
0038         QVERIFY( QFileInfo( sessiondir ).isDir() );
0039         QVERIFY( QFileInfo::exists( sessiondir+"/sessionrc" ) );
0040         KSharedConfigPtr cfg = KSharedConfig::openConfig( sessiondir+"/sessionrc" );
0041         QCOMPARE( name, cfg->group("").readEntry( Session::cfgSessionNameEntry, "" ) );
0042     } else {
0043         qDebug() << "checking not-existing dir: " << sessiondir;
0044         QVERIFY( !QFileInfo::exists( sessiondir ) );
0045     }
0046 }
0047 
0048 void verifySessionDir( ISession* s, bool exists = true )
0049 {
0050     verifySessionDir(sessionDir(s), s->name(), exists);
0051 }
0052 
0053 ////////////////////// Fixture ///////////////////////////////////////////////
0054 
0055 void TestSessionController::initTestCase()
0056 {
0057     AutoTestShell::init();
0058     TestCore::initialize(Core::NoUi);
0059     m_core = Core::self();
0060     qRegisterMetaType<KDevelop::ISession*>();
0061     qRegisterMetaType<KDevelop::Session*>();
0062 }
0063 
0064 void TestSessionController::init()
0065 {
0066     m_sessionCtrl = m_core->sessionController();
0067 }
0068 
0069 void TestSessionController::cleanupTestCase()
0070 {
0071     const auto sessions = m_sessionCtrl->sessions();
0072     for (const Session* session : sessions) {
0073         TryLockSessionResult lock = m_sessionCtrl->tryLockSession(session->id().toString());
0074         if (lock.lock)
0075             m_sessionCtrl->deleteSession( lock.lock );
0076     }
0077 
0078     TestCore::shutdown();
0079 }
0080 
0081 void TestSessionController::createSession_data()
0082 {
0083     QTest::addColumn<QString>( "sessionName" );
0084     QTest::newRow("SimpleName") << "TestSession";
0085     QTest::newRow("NonLetterChars") << "Test%$Session";
0086     QTest::newRow("NonAsciiChars") << QStringLiteral("TöstSession");
0087 }
0088 
0089 void TestSessionController::createSession()
0090 {
0091     QFETCH(QString, sessionName);
0092     int sessionCount = m_sessionCtrl->sessionNames().count();
0093     Session* s = m_sessionCtrl->createSession( sessionName );
0094     QVERIFY( m_sessionCtrl->sessionNames().contains( sessionName )  );
0095     QCOMPARE( sessionCount+1, m_sessionCtrl->sessionNames().count() );
0096     verifySessionDir( s );
0097 }
0098 
0099 void TestSessionController::renameSession()
0100 {
0101     const QString sessionName = QStringLiteral("TestSession4");
0102     const QString newSessionName = QStringLiteral("TestOtherSession4");
0103     KDevelop::Session *s = m_sessionCtrl->createSession( sessionName );
0104     QCOMPARE( sessionName, s->name() );
0105     verifySessionDir( s );
0106     QSignalSpy spy(s, SIGNAL(sessionUpdated(KDevelop::ISession*)));
0107     s->setName( newSessionName );
0108     QCOMPARE( newSessionName, s->name() );
0109     QCOMPARE( spy.size(), 1 );
0110 
0111     verifySessionDir( s );
0112 }
0113 
0114 void TestSessionController::canRenameActiveSession()
0115 {
0116     const QString sessionName = QStringLiteral("TestSession5");
0117     const QString newSessionName = QStringLiteral("TestOtherSession5");
0118     KDevelop::Session *s = m_sessionCtrl->createSession( sessionName );
0119     QCOMPARE( sessionName, s->name() );
0120     m_sessionCtrl->loadSession( sessionName );
0121     QSignalSpy spy(s, SIGNAL(sessionUpdated(KDevelop::ISession*)));
0122     s->setName( newSessionName );
0123     QCOMPARE( newSessionName, s->name() );
0124     QCOMPARE( spy.size(), 1 );
0125 
0126     verifySessionDir( s );
0127 }
0128 
0129 void TestSessionController::deleteSession()
0130 {
0131     const QString sessionName = QStringLiteral("TestSession3");
0132     int sessionCount = m_sessionCtrl->sessionNames().count();
0133     QPointer<Session> s = m_sessionCtrl->createSession( sessionName );
0134     QString sessionId = s->id().toString();
0135     QCOMPARE( sessionCount+1, m_sessionCtrl->sessionNames().count() );
0136     verifySessionDir( s.data() );
0137     const QString sessionDir = ::sessionDir(s);
0138 
0139     QSignalSpy spy(m_sessionCtrl, SIGNAL(sessionDeleted(QString)));
0140     {
0141         TryLockSessionResult lock = m_sessionCtrl->tryLockSession(sessionId);
0142         QVERIFY(lock.lock);
0143         m_sessionCtrl->deleteSession( lock.lock );
0144     }
0145     QCOMPARE( sessionCount, m_sessionCtrl->sessionNames().count() );
0146     QVERIFY( !m_sessionCtrl->sessionNames().contains(sessionId) );
0147 
0148     QCOMPARE(spy.size(), 1);
0149     QList<QVariant> arguments = spy.takeFirst();
0150 
0151     QString emittedSession = arguments.at(0).toString();
0152     QCOMPARE( sessionId, emittedSession );
0153 
0154     verifySessionDir( sessionDir, sessionName, false );
0155 }
0156 
0157 void TestSessionController::cloneSession()
0158 {
0159     QString sessionName = QStringLiteral("CloneableSession");
0160     QString testgrp = QStringLiteral("TestGroup");
0161     QString testentry = QStringLiteral("TestEntry");
0162     QString testval = QStringLiteral("TestValue");
0163     int sessionCount = m_sessionCtrl->sessionNames().count();
0164     m_sessionCtrl->createSession( sessionName );
0165     Session* s = m_sessionCtrl->session( sessionName );
0166     s->config()->group( testgrp ).writeEntry( testentry, testval );
0167     s->config()->sync();
0168     QCOMPARE( sessionCount+1, m_sessionCtrl->sessionNames().count() );
0169     QVERIFY( m_sessionCtrl->session( sessionName ) );
0170 
0171     QString newSession = m_sessionCtrl->cloneSession( sessionName );
0172     QVERIFY( m_sessionCtrl->session( newSession ) );
0173     QCOMPARE( sessionCount+2, m_sessionCtrl->sessionNames().count() );
0174     Session* news = m_sessionCtrl->session( newSession );
0175     QCOMPARE( testval, news->config()->group( testgrp ).readEntry( testentry, "" ) );
0176     QCOMPARE( i18n( "Copy of %1", sessionName ), news->name() );
0177 
0178     verifySessionDir( news );
0179 
0180 
0181 }
0182 
0183 void TestSessionController::readFromConfig()
0184 {
0185     ISession* s = Core::self()->activeSession();
0186     KConfigGroup grp( s->config(), "TestGroup" );
0187     grp.writeEntry( "TestEntry", "Test1" );
0188     KConfigGroup grp2( s->config(), "TestGroup" );
0189     QCOMPARE(grp.readEntry( "TestEntry", "" ), QStringLiteral( "Test1" ) );
0190 }
0191 
0192 void TestSessionController::temporary()
0193 {
0194     ISession* s = Core::self()->activeSession();
0195     s->setTemporary(true);
0196     const QString oldName = s->name();
0197     const QString dir = sessionDir(s);
0198 
0199     verifySessionDir(s, true);
0200     Core::self()->sessionController()->cleanup();
0201     verifySessionDir(dir, oldName, false);
0202     Core::self()->sessionController()->initialize(oldName);
0203     QCOMPARE(Core::self()->activeSession()->name(), oldName);
0204     // dir / UID can be different, hence don't verifySessionDir
0205 }
0206 
0207 void TestSessionController::tryLockSession()
0208 {
0209     const QString id1 = QUuid::createUuid().toString();
0210     m_sessionCtrl->createSession( id1 )->setTemporary(true);
0211     const QString id2 = QUuid::createUuid().toString();
0212     m_sessionCtrl->createSession( id2 )->setTemporary(true);
0213     {
0214         // acquired scoped lock
0215         QVERIFY(!SessionController::isSessionRunning(id1));
0216         QCOMPARE(SessionController::sessionRunInfo(id1), SessionRunInfo());
0217         TryLockSessionResult initial = SessionController::tryLockSession(id1);
0218         QVERIFY(initial.lock);
0219         QCOMPARE(initial.lock->id(), id1);
0220         QCOMPARE(initial.runInfo, SessionRunInfo());
0221         QVERIFY(SessionController::isSessionRunning(id1));
0222 
0223         SessionRunInfo info = SessionController::sessionRunInfo(id1);
0224         QVERIFY(info != initial.runInfo);
0225         QVERIFY(info.isRunning);
0226         QCOMPARE(info.holderApp, QCoreApplication::applicationName());
0227         QCOMPARE(info.holderPid, static_cast<int>(QCoreApplication::applicationPid()));
0228 
0229         // this should fail
0230         TryLockSessionResult repeated = SessionController::tryLockSession(id1);
0231         QVERIFY(!repeated.lock);
0232         QCOMPARE(repeated.runInfo, info);
0233 
0234         // this should pass (different id)
0235         QVERIFY(!SessionController::isSessionRunning(id2));
0236         TryLockSessionResult other = SessionController::tryLockSession(id2);
0237         QVERIFY(other.lock);
0238         QCOMPARE(other.lock->id(), id2);
0239         QCOMPARE(other.runInfo, SessionRunInfo());
0240         QVERIFY(SessionController::isSessionRunning(id2));
0241     }
0242 
0243     // scope left, sessions are now unlocked again
0244     QVERIFY(!SessionController::isSessionRunning(id1));
0245     QCOMPARE(SessionController::sessionRunInfo(id1), SessionRunInfo());
0246     QVERIFY(!SessionController::isSessionRunning(id2));
0247     QCOMPARE(SessionController::sessionRunInfo(id2), SessionRunInfo());
0248 
0249     // can re-lock it here
0250     TryLockSessionResult final = SessionController::tryLockSession(id1);
0251     QVERIFY(SessionController::isSessionRunning(id1));
0252     QVERIFY(final.lock);
0253     QCOMPARE(final.lock->id(), id1);
0254     QCOMPARE(final.runInfo, SessionRunInfo());
0255 }
0256 
0257 QTEST_GUILESS_MAIN(TestSessionController)
0258 
0259 #include "moc_test_sessioncontroller.cpp"