File indexing completed on 2024-04-28 04:37:35

0001 /*
0002     SPDX-FileCopyrightText: 2008 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "testcore.h"
0008 
0009 #include <shell/uicontroller.h>
0010 #include <shell/sessioncontroller.h>
0011 #include <shell/languagecontroller.h>
0012 #include <shell/runcontroller.h>
0013 #include <shell/documentcontroller.h>
0014 #include <shell/plugincontroller.h>
0015 #include <shell/partcontroller.h>
0016 #include <shell/projectcontroller.h>
0017 #include <language/backgroundparser/backgroundparser.h>
0018 #include <interfaces/isession.h>
0019 #include "../shell/core_p.h"
0020 
0021 #include <QCoreApplication>
0022 #include <QTest>
0023 
0024 #include <KParts/MainWindow>
0025 
0026 namespace KDevelop {
0027 TestCore::TestCore()
0028     : Core(new CorePrivate(this))
0029 {
0030     Core::m_self = this;
0031 }
0032 
0033 TestCore* TestCore::initialize(Core::Setup mode, const QString& session)
0034 {
0035     qRegisterMetaType<QList<QUrl>>("QList<QUrl>");
0036 
0037     if (!Core::m_self) {
0038         new TestCore;
0039     }
0040 
0041     auto* core = qobject_cast<TestCore*>(Core::m_self);
0042     Q_ASSERT(core);
0043     core->initializeNonStatic(mode, session);
0044 
0045     if (mode == Default) {
0046         // we don't want the window to be visible, hide it
0047         // the unit tests should work anyways
0048         core->uiController()->activeMainWindow()->hide();
0049     }
0050 
0051     // resume the background parser when a unit test replaces the project controller
0052     QObject::connect(core->d->projectController.data(), &ProjectController::destroyed,
0053                      core, [core]() {
0054             core->d->languageController->backgroundParser()->resume();
0055         });
0056 
0057     return core;
0058 }
0059 
0060 void TestCore::initializeNonStatic(Core::Setup mode, const QString& _session)
0061 {
0062     QString session = _session;
0063     if (_session.isEmpty()) {
0064         // use a distinct session name for unit test sessions
0065         // they are temporary (see below) but still - we want to make sure
0066         session = QLatin1String("test-") + QCoreApplication::applicationName();
0067     }
0068 
0069     d->initialize(mode, session);
0070 
0071     if (_session.isEmpty()) {
0072         activeSession()->setTemporary(true);
0073     }
0074 }
0075 
0076 void TestCore::shutdown()
0077 {
0078     if (self()) {
0079         // trigger eventloop to handle Queued connections
0080         // before entering cleanup
0081         // this can fix random crashes under certain conditions
0082         QTest::qWait(1);
0083 
0084         self()->shutdown();
0085 
0086         // wait until Core is deleted via event loop
0087         QTest::qWait(1);
0088     }
0089 }
0090 
0091 void TestCore::setShuttingDown(bool shuttingDown)
0092 {
0093     d->m_shuttingDown = shuttingDown;
0094 }
0095 
0096 void TestCore::setSessionController(SessionController* ctrl)
0097 {
0098     d->sessionController = ctrl;
0099 }
0100 
0101 void TestCore::setPluginController(PluginController* ctrl)
0102 {
0103     d->pluginController = ctrl;
0104 }
0105 
0106 void TestCore::setRunController(RunController* ctrl)
0107 {
0108     d->runController = ctrl;
0109 }
0110 
0111 void TestCore::setDocumentController(DocumentController* ctrl)
0112 {
0113     d->documentController = ctrl;
0114 }
0115 
0116 void TestCore::setPartController(PartController* ctrl)
0117 {
0118     d->partController = ctrl;
0119 }
0120 
0121 void TestCore::setProjectController(ProjectController* ctrl)
0122 {
0123     d->projectController = ctrl;
0124 }
0125 
0126 void TestCore::setLanguageController(LanguageController* ctrl)
0127 {
0128     d->languageController = ctrl;
0129 }
0130 
0131 void TestCore::setUiController(UiController* ctrl)
0132 {
0133     d->uiController = ctrl;
0134 }
0135 }
0136 
0137 #include "moc_testcore.cpp"