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

0001 /*
0002     SPDX-FileCopyrightText: 2016 Aetf <aetf@unlimitedcodeworks.xyz>
0003 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #ifndef TESTDEBUGSESSION_H
0008 #define TESTDEBUGSESSION_H
0009 
0010 #include "testsexport.h"
0011 
0012 #include <debugger/interfaces/idebugsession.h>
0013 
0014 #include <memory>
0015 
0016 using std::unique_ptr;
0017 
0018 namespace KDevelop {
0019 class IBreakpointController;
0020 class IVariableController;
0021 class IFrameStackModel;
0022 
0023 /**
0024  * \class TestDebugSession testdebugsession.h
0025  *
0026  * This is an IDebugSession implementation that should be used only inside
0027  * unit tests.
0028  *
0029  * The initial debug state is NotStartedState, one can use normal slots to
0030  * change the state, for example calling runToCursor() changes to PausedState.
0031  * See corresponding implementation for what state a specific slot changes to.
0032  *
0033  * This class also allows one to replace IBreakpointController, IVariableController,
0034  * or IFrameStackModel implementation with custom implementation to make it possible
0035  * to write tests more easily.
0036  *
0037  * The usage is as follows:
0038  * \code
0039  * AutoTestShell::init();
0040  * TestCore::initialize(Core::NoUi);
0041  * TestDebugSession* session = new TestDebugSession();
0042  * //replace variable controller or similar
0043  * session->setVariableController(new MyCustomVariableController(session));
0044  * ... //test code
0045  * delete session;
0046  * TestCore::shutdown();
0047  * \endcode
0048  *
0049  * @note It is important to set custom controllers right after the creation of the
0050  * debug session, and not change them inside the test code, as required by
0051  * IDebugSession that controllers do not change during the lifetime of a session
0052  */
0053 class KDEVPLATFORMTESTS_EXPORT TestDebugSession
0054     : public IDebugSession
0055 {
0056     Q_OBJECT
0057 
0058 public:
0059     TestDebugSession();
0060     ~TestDebugSession() override;
0061 
0062     void setBreakpointController(IBreakpointController* breakpointController);
0063     void setVariableController(IVariableController* variableController);
0064     void setFrameStackModel(IFrameStackModel* frameStackModel);
0065 
0066     DebuggerState state() const override;
0067 
0068     bool restartAvaliable() const override;
0069     IBreakpointController* breakpointController() const override;
0070     IVariableController* variableController() const override;
0071     IFrameStackModel* frameStackModel() const override;
0072 
0073 public Q_SLOTS:
0074     void restartDebugger() override;
0075     void stopDebugger() override;
0076     void killDebuggerNow() override;
0077     void interruptDebugger() override;
0078     void run() override;
0079     void runToCursor() override;
0080     void jumpToCursor() override;
0081     void stepOver() override;
0082     void stepIntoInstruction() override;
0083     void stepInto() override;
0084     void stepOverInstruction() override;
0085     void stepOut() override;
0086 
0087 private:
0088     IBreakpointController* m_breakpointController = nullptr;
0089     IVariableController* m_variableController = nullptr;
0090     IFrameStackModel* m_frameStackModel = nullptr;
0091     DebuggerState m_sessionState = NotStartedState;
0092 };
0093 } // end of namespace KDevelop
0094 #endif // TESTDEBUGSESSION_H