File indexing completed on 2024-04-28 11:20:44

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004 */
0005 
0006 #include "backendtest.h"
0007 
0008 #include "backend.h"
0009 #include "session.h"
0010 #include "../../config-cantor.h"
0011 
0012 #include <KLocalizedString>
0013 #include <QSignalSpy>
0014 #include <QDebug>
0015 
0016 void BackendTest::createSession()
0017 {
0018     // Add our custom plugins path, where we install our plugins, if it isn't default path
0019     const QString& path = QString::fromLocal8Bit(PATH_TO_CANTOR_PLUGINS);
0020     qDebug() << "Adding additional application library path" << path;
0021     if (!QCoreApplication::libraryPaths().contains(path))
0022         QCoreApplication::addLibraryPath(path);
0023 
0024     auto* b = Cantor::Backend::getBackend( backendName() );
0025     if(!b || !b->requirementsFullfilled() )
0026     {
0027         m_session = nullptr;
0028         return;
0029     }
0030 
0031     m_session = b->createSession();
0032 
0033     QSignalSpy spy(m_session, SIGNAL(loginDone()) );
0034     m_session->login();
0035     if(spy.isEmpty())
0036         waitForSignal(m_session, SIGNAL(loginDone()) );
0037 
0038     QVERIFY(!spy.isEmpty());
0039 }
0040 
0041 Cantor::Expression* BackendTest::evalExp(const QString& exp )
0042 {
0043    auto* e = m_session->evaluateExpression(exp);
0044 
0045    if(e->status() == Cantor::Expression::Queued)
0046        waitForSignal( e, SIGNAL(statusChanged(Cantor::Expression::Status)) );
0047 
0048    if (e->status() == Cantor::Expression::Computing)
0049        waitForSignal( e, SIGNAL(statusChanged(Cantor::Expression::Status)) );
0050 
0051    return e;
0052 }
0053 
0054 /**
0055 * simple method that removes whitespaces/other irrelevant stuff,
0056 * so comparing results is easier
0057 */
0058 QString BackendTest::cleanOutput(const QString& out)
0059 {
0060     QString cleaned = out;
0061     cleaned.replace( QLatin1String("&nbsp;"),QLatin1String(" ") );
0062     cleaned.remove( QLatin1String("<br/>") );
0063     cleaned.replace( QChar::ParagraphSeparator, QLatin1Char('\n') );
0064     cleaned.replace( QRegularExpression( QStringLiteral("\\n{2}") ), QStringLiteral("\n") );
0065     cleaned.replace( QRegularExpression( QStringLiteral("\\n\\s*") ), QStringLiteral("\n") );
0066 
0067     return cleaned.trimmed();
0068 }
0069 
0070 void BackendTest::initTestCase()
0071 {
0072     QCoreApplication::setApplicationName(QLatin1String("cantor"));
0073     createSession();
0074     if (!m_session)
0075     {
0076         QString reason = i18n("This test requires a functioning %1 backend", backendName() );
0077         QSKIP( reason.toStdString().c_str(), SkipAll );
0078     }
0079 }
0080 
0081 void BackendTest::cleanupTestCase()
0082 {
0083     if (m_session)
0084         m_session->logout();
0085 }
0086 
0087 Cantor::Session* BackendTest::session()
0088 {
0089     return m_session;
0090 }
0091 
0092 /**
0093 * simple method that blocks and waits for a signal to be emitted
0094 */
0095 void BackendTest::waitForSignal(QObject* sender, const char* signal)
0096 {
0097     QTimer timeout( this );
0098     timeout.setSingleShot( true );
0099 
0100     QEventLoop loop;
0101     connect( sender, signal, &loop, SLOT(quit()) );
0102     connect(&timeout, &QTimer::timeout, &loop, &QEventLoop::quit);
0103     timeout.start( 25000 );
0104     loop.exec();
0105 }