File indexing completed on 2024-05-12 04:39:45

0001 /*
0002     SPDX-FileCopyrightText: 2009 Niko Sams <niko.sams@gmail.com>
0003     SPDX-FileCopyrightText: 2016 Aetf <aetf@unlimitedcodeworks.xyz>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #ifndef KDEVDBG_TESTHELPER_H
0009 #define KDEVDBG_TESTHELPER_H
0010 
0011 #include <debugger/interfaces/idebugsession.h>
0012 #include <interfaces/ilaunchconfiguration.h>
0013 
0014 #include <KConfigGroup>
0015 #include <KSharedConfig>
0016 
0017 #include <QPointer>
0018 #include <QString>
0019 #include <QElapsedTimer>
0020 #include <QUrl>
0021 
0022 class IExecutePlugin;
0023 class QModelIndex;
0024 
0025 #define WAIT_FOR_STATE(session, state) \
0026     do { if (!KDevMI::waitForState((session), (state), __FILE__, __LINE__)) return; } while (0)
0027 
0028 #define WAIT_FOR_STATE_AND_IDLE(session, state) \
0029     do { if (!KDevMI::waitForState((session), (state), __FILE__, __LINE__, true)) return; } while (0)
0030 
0031 #define WAIT_FOR(session, condition) \
0032     do { \
0033         KDevMI::TestWaiter w((session), #condition, __FILE__, __LINE__); \
0034         while (w.waitUnless((condition))) /* nothing */ ; \
0035     } while(0)
0036 
0037 #define COMPARE_DATA(index, expected) \
0038     do { if (!KDevMI::compareData((index), (expected), __FILE__, __LINE__)) return; } while (0)
0039 
0040 #define SKIP_IF_ATTACH_FORBIDDEN() \
0041     do { \
0042         if (KDevMI::isAttachForbidden(__FILE__, __LINE__)) \
0043             return; \
0044     } while(0)
0045 
0046 namespace KDevMI {
0047 
0048 class MIDebugSession;
0049 
0050 QUrl findExecutable(const QString& name);
0051 QString findSourceFile(const QString& name);
0052 QString findFile(const char* dir, const QString& name);
0053 bool isAttachForbidden(const char* file, int line);
0054 
0055 bool compareData(const QModelIndex& index, const QString& expected, const char* file, int line, bool useRE = false);
0056 
0057 bool waitForState(MIDebugSession* session, KDevelop::IDebugSession::DebuggerState state, const char* file, int line,
0058                   bool waitForIdle = false);
0059 
0060 bool waitForAWhile(MIDebugSession* session, int ms, const char* file, int line);
0061 
0062 class TestWaiter
0063 {
0064 public:
0065     TestWaiter(MIDebugSession* session_, const char* condition_, const char* file_, int line_);
0066 
0067     bool waitUnless(bool ok);
0068 
0069 private:
0070     QElapsedTimer stopWatch;
0071     QPointer<MIDebugSession> session;
0072     const char* condition;
0073     const char* file;
0074     int line;
0075 };
0076 
0077 class TestLaunchConfiguration : public KDevelop::ILaunchConfiguration
0078 {
0079 public:
0080     explicit TestLaunchConfiguration(const QString& executable = QStringLiteral("debuggee_debugee"),
0081                                      const QUrl& workingDirectory = QUrl{})
0082         : TestLaunchConfiguration(findExecutable(executable), workingDirectory)
0083     {}
0084 
0085     explicit TestLaunchConfiguration(const QUrl& executable, const QUrl& workingDirectory);
0086     const KConfigGroup config() const override { return cfg; }
0087     KConfigGroup config() override { return cfg; };
0088     QString name() const override { return QStringLiteral("Test-Launch"); }
0089     KDevelop::IProject* project() const override { return nullptr; }
0090     KDevelop::LaunchConfigurationType* type() const override { return nullptr; }
0091 
0092     KConfig* rootConfig() { return c.data(); }
0093 private:
0094     KConfigGroup cfg;
0095     KSharedConfigPtr c;
0096 };
0097 
0098 void testEnvironmentSet(MIDebugSession* session, const QString& profileName,
0099                         IExecutePlugin* executePlugin);
0100 
0101 } // end of namespace KDevMI
0102 
0103 #endif // KDEVDBG_TESTHELPER_H