File: test_sessioncontroller.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (270 lines) | stat: -rw-r--r-- 10,342 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/***************************************************************************
 *   Copyright 2008 Andreas Pakulat <apaku@gmx.de>                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/

#include "test_sessioncontroller.h"

#include <tests/autotestshell.h>
#include <tests/testcore.h>

#include <KConfigGroup>
#include <KLocalizedString>

#include "../core.h"
#include "../sessioncontroller.h"
#include "../session.h"
#include <QDebug>
#include <QFileInfo>
#include <QSignalSpy>
#include <QTest>

using namespace KDevelop;

//////////////////// Helper Functions ////////////////////////////////////////

QString sessionDir( ISession* s )
{
    return SessionController::sessionDirectory(s->id().toString());
}

void verifySessionDir( const QString& sessiondir, const QString& name, bool exists )
{
    if( exists )
    {
        qDebug() << "checking existing session" << sessiondir;
        QVERIFY( QFileInfo::exists( sessiondir ) );
        QVERIFY( QFileInfo( sessiondir ).isDir() );
        QVERIFY( QFileInfo::exists( sessiondir+"/sessionrc" ) );
        KSharedConfigPtr cfg = KSharedConfig::openConfig( sessiondir+"/sessionrc" );
        QCOMPARE( name, cfg->group("").readEntry( Session::cfgSessionNameEntry, "" ) );
    } else {
        qDebug() << "checking not-existing dir: " << sessiondir;
        QVERIFY( !QFileInfo::exists( sessiondir ) );
    }
}

void verifySessionDir( ISession* s, bool exists = true )
{
    verifySessionDir(sessionDir(s), s->name(), exists);
}

////////////////////// Fixture ///////////////////////////////////////////////

void TestSessionController::initTestCase()
{
    AutoTestShell::init();
    TestCore::initialize(Core::NoUi);
    m_core = Core::self();
    qRegisterMetaType<KDevelop::ISession*>();
    qRegisterMetaType<KDevelop::Session*>();
}

void TestSessionController::init()
{
    m_sessionCtrl = m_core->sessionController();
}

void TestSessionController::cleanupTestCase()
{
    const auto sessions = m_sessionCtrl->sessions();
    for (const Session* session : sessions) {
        TryLockSessionResult lock = m_sessionCtrl->tryLockSession(session->id().toString());
        if (lock.lock)
            m_sessionCtrl->deleteSession( lock.lock );
    }

    TestCore::shutdown();
}

void TestSessionController::createSession_data()
{
    QTest::addColumn<QString>( "sessionName" );
    QTest::newRow("SimpleName") << "TestSession";
    QTest::newRow("NonLetterChars") << "Test%$Session";
    QTest::newRow("NonAsciiChars") << QStringLiteral("TöstSession");
}

void TestSessionController::createSession()
{
    QFETCH(QString, sessionName);
    int sessionCount = m_sessionCtrl->sessionNames().count();
    Session* s = m_sessionCtrl->createSession( sessionName );
    QVERIFY( m_sessionCtrl->sessionNames().contains( sessionName )  );
    QCOMPARE( sessionCount+1, m_sessionCtrl->sessionNames().count() );
    verifySessionDir( s );
}

void TestSessionController::renameSession()
{
    const QString sessionName = QStringLiteral("TestSession4");
    const QString newSessionName = QStringLiteral("TestOtherSession4");
    KDevelop::Session *s = m_sessionCtrl->createSession( sessionName );
    QCOMPARE( sessionName, s->name() );
    verifySessionDir( s );
    QSignalSpy spy(s, SIGNAL(sessionUpdated(KDevelop::ISession*)));
    s->setName( newSessionName );
    QCOMPARE( newSessionName, s->name() );
    QCOMPARE( spy.size(), 1 );

    verifySessionDir( s );
}

void TestSessionController::canRenameActiveSession()
{
    const QString sessionName = QStringLiteral("TestSession5");
    const QString newSessionName = QStringLiteral("TestOtherSession5");
    KDevelop::Session *s = m_sessionCtrl->createSession( sessionName );
    QCOMPARE( sessionName, s->name() );
    m_sessionCtrl->loadSession( sessionName );
    QSignalSpy spy(s, SIGNAL(sessionUpdated(KDevelop::ISession*)));
    s->setName( newSessionName );
    QCOMPARE( newSessionName, s->name() );
    QCOMPARE( spy.size(), 1 );

    verifySessionDir( s );
}

void TestSessionController::deleteSession()
{
    const QString sessionName = QStringLiteral("TestSession3");
    int sessionCount = m_sessionCtrl->sessionNames().count();
    QPointer<Session> s = m_sessionCtrl->createSession( sessionName );
    QString sessionId = s->id().toString();
    QCOMPARE( sessionCount+1, m_sessionCtrl->sessionNames().count() );
    verifySessionDir( s.data() );
    const QString sessionDir = ::sessionDir(s);

    QSignalSpy spy(m_sessionCtrl, SIGNAL(sessionDeleted(QString)));
    {
        TryLockSessionResult lock = m_sessionCtrl->tryLockSession(sessionId);
        QVERIFY(lock.lock);
        m_sessionCtrl->deleteSession( lock.lock );
    }
    QCOMPARE( sessionCount, m_sessionCtrl->sessionNames().count() );
    QVERIFY( !m_sessionCtrl->sessionNames().contains(sessionId) );

    QCOMPARE(spy.size(), 1);
    QList<QVariant> arguments = spy.takeFirst();

    QString emittedSession = arguments.at(0).toString();
    QCOMPARE( sessionId, emittedSession );

    verifySessionDir( sessionDir, sessionName, false );
}

void TestSessionController::cloneSession()
{
    QString sessionName = QStringLiteral("CloneableSession");
    QString testgrp = QStringLiteral("TestGroup");
    QString testentry = QStringLiteral("TestEntry");
    QString testval = QStringLiteral("TestValue");
    int sessionCount = m_sessionCtrl->sessionNames().count();
    m_sessionCtrl->createSession( sessionName );
    Session* s = m_sessionCtrl->session( sessionName );
    s->config()->group( testgrp ).writeEntry( testentry, testval );
    s->config()->sync();
    QCOMPARE( sessionCount+1, m_sessionCtrl->sessionNames().count() );
    QVERIFY( m_sessionCtrl->session( sessionName ) );

    QString newSession = m_sessionCtrl->cloneSession( sessionName );
    QVERIFY( m_sessionCtrl->session( newSession ) );
    QCOMPARE( sessionCount+2, m_sessionCtrl->sessionNames().count() );
    Session* news = m_sessionCtrl->session( newSession );
    QCOMPARE( testval, news->config()->group( testgrp ).readEntry( testentry, "" ) );
    QCOMPARE( i18n( "Copy of %1", sessionName ), news->name() );

    verifySessionDir( news );


}

void TestSessionController::readFromConfig()
{
    ISession* s = Core::self()->activeSession();
    KConfigGroup grp( s->config(), "TestGroup" );
    grp.writeEntry( "TestEntry", "Test1" );
    KConfigGroup grp2( s->config(), "TestGroup" );
    QCOMPARE(grp.readEntry( "TestEntry", "" ), QStringLiteral( "Test1" ) );
}

void TestSessionController::temporary()
{
    ISession* s = Core::self()->activeSession();
    s->setTemporary(true);
    const QString oldName = s->name();
    const QString dir = sessionDir(s);

    verifySessionDir(s, true);
    Core::self()->sessionController()->cleanup();
    verifySessionDir(dir, oldName, false);
    Core::self()->sessionController()->initialize(oldName);
    QCOMPARE(Core::self()->activeSession()->name(), oldName);
    // dir / UID can be different, hence don't verifySessionDir
}

void TestSessionController::tryLockSession()
{
    const QString id1 = QUuid::createUuid().toString();
    m_sessionCtrl->createSession( id1 )->setTemporary(true);
    const QString id2 = QUuid::createUuid().toString();
    m_sessionCtrl->createSession( id2 )->setTemporary(true);
    {
        // acquired scoped lock
        QVERIFY(!SessionController::isSessionRunning(id1));
        QCOMPARE(SessionController::sessionRunInfo(id1), SessionRunInfo());
        TryLockSessionResult initial = SessionController::tryLockSession(id1);
        QVERIFY(initial.lock);
        QCOMPARE(initial.lock->id(), id1);
        QCOMPARE(initial.runInfo, SessionRunInfo());
        QVERIFY(SessionController::isSessionRunning(id1));

        SessionRunInfo info = SessionController::sessionRunInfo(id1);
        QVERIFY(info != initial.runInfo);
        QVERIFY(info.isRunning);
        QCOMPARE(info.holderApp, QCoreApplication::applicationName());
        QCOMPARE(info.holderPid, static_cast<int>(QCoreApplication::applicationPid()));

        // this should fail
        TryLockSessionResult repeated = SessionController::tryLockSession(id1);
        QVERIFY(!repeated.lock);
        QCOMPARE(repeated.runInfo, info);

        // this should pass (different id)
        QVERIFY(!SessionController::isSessionRunning(id2));
        TryLockSessionResult other = SessionController::tryLockSession(id2);
        QVERIFY(other.lock);
        QCOMPARE(other.lock->id(), id2);
        QCOMPARE(other.runInfo, SessionRunInfo());
        QVERIFY(SessionController::isSessionRunning(id2));
    }

    // scope left, sessions are now unlocked again
    QVERIFY(!SessionController::isSessionRunning(id1));
    QCOMPARE(SessionController::sessionRunInfo(id1), SessionRunInfo());
    QVERIFY(!SessionController::isSessionRunning(id2));
    QCOMPARE(SessionController::sessionRunInfo(id2), SessionRunInfo());

    // can re-lock it here
    TryLockSessionResult final = SessionController::tryLockSession(id1);
    QVERIFY(SessionController::isSessionRunning(id1));
    QVERIFY(final.lock);
    QCOMPARE(final.lock->id(), id1);
    QCOMPARE(final.runInfo, SessionRunInfo());
}

QTEST_GUILESS_MAIN(TestSessionController)