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 #ifndef KDEVPLATFORM_KDEVPLATFORMTESTCORE_H
0008 #define KDEVPLATFORM_KDEVPLATFORMTESTCORE_H
0009 
0010 #include "testsexport.h"
0011 #include <shell/core.h>
0012 
0013 namespace KDevelop {
0014 class SessionController;
0015 class RunController;
0016 class PluginController;
0017 class DocumentController;
0018 class PartController;
0019 class ProjectController;
0020 class UiController;
0021 
0022 /**
0023  * \class TestCore testcore.h
0024  *
0025  * This is an ICore implementation which should be used inside unit tests.
0026  * By default a temporary session named after currently running application
0027  * will be instantiated. Furthermore the mainwindow - if created - will be
0028  * hidden as no user interaction should take place in a unit test.
0029  *
0030  * The simplest code which should suffice for most use cases is:
0031  *
0032  * \code
0033  * MyUnitTest::initTestCase() {
0034  *   AutoTestShell::init()
0035  *   TestCore::initialize();
0036  * }
0037  * MyUnitTest::cleanupTestCase() {
0038  *   TestCore::shutdown();
0039  * }
0040  * \endcode
0041  *
0042  * This class also allows one to replace certain or all parts of the
0043  * shell library with custom implementation to make it possible
0044  * to write plugin tests more easily.
0045  *
0046  * The usage is then as follows:
0047  * \code
0048  * TestCore* core = new TestCore();
0049  * //replace plugin controller or similar
0050  * core->setPluginController( new MyCustomPluginController( core ) );
0051  * //initialize
0052  * core->initialize(Core::Default);
0053  * ... //test code
0054  * // if you want to cleanup manually:
0055  * core->cleanup();
0056  * delete core;
0057  * \endcode
0058  *
0059  * @note It is important to call initialize and cleanup, else the controllers
0060  * won't work properly.
0061  */
0062 class KDEVPLATFORMTESTS_EXPORT TestCore
0063     : public Core
0064 {
0065     Q_OBJECT
0066 
0067 public:
0068     TestCore();
0069     /**
0070      * Initialize the test core with the given setup @p mode.
0071      * Optionally launch with a user given session.
0072      *
0073      * @p mode Use @c Core::NoUi when your unit test does not require any UI
0074      *         NOTE: Even in @c Default mode the mainwindow will be hidden,
0075      *               as unit tests should not depend on user interaction.
0076      * @p session By default a temporary session will be created called "test-%appname".
0077      *            If @p session is not empty, a non-temporary session with the given name
0078      *            will be opened.
0079      * @return the initialized test core
0080      */
0081     static TestCore* initialize(Core::Setup mode = Core::Default, const QString& session = {});
0082 
0083     /**
0084      * Calls @c cleanup() on the current TestCore instance,
0085      * then deletes the core.
0086      *
0087      * @NOTE: It is important to call this in e.g. @c cleanupTestSuite()
0088      *        otherwise the core will be leaked, no proper cleanup will take place
0089      *        and e.g. temporary sessions not properly deleted.
0090      */
0091     static void shutdown();
0092 
0093     /**
0094      * Fakes a shutdown without actually doing the shutdown.
0095      */
0096     void setShuttingDown(bool shuttingDown);
0097 
0098     void setSessionController(SessionController*);
0099     void setPluginController(PluginController*);
0100     void setRunController(RunController*);
0101     void setDocumentController(DocumentController*);
0102     void setPartController(PartController*);
0103     void setProjectController(ProjectController*);
0104     void setLanguageController(LanguageController*);
0105     void setUiController(UiController*);
0106 
0107 private:
0108     void initializeNonStatic(Core::Setup mode, const QString& session);
0109 };
0110 }
0111 
0112 #endif