File indexing completed on 2024-04-28 03:57:07

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2014 Miquel Sabaté Solà <mikisabate@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef BASE_TEST_H
0009 #define BASE_TEST_H
0010 
0011 #include <QObject>
0012 
0013 #include <KTextEditor/Range>
0014 
0015 namespace KTextEditor
0016 {
0017 class ViewPrivate;
0018 class Document;
0019 class DocumentPrivate;
0020 }
0021 class QMainWindow;
0022 class QVBoxLayout;
0023 class KateViInputMode;
0024 
0025 class QLineEdit;
0026 namespace KateVi
0027 {
0028 class EmulatedCommandBar;
0029 class GlobalState;
0030 class InputModeManager;
0031 }
0032 
0033 /* Syntactic sugar :P */
0034 #define DoTest(...) DoTest_(__LINE__, __FILE__, __VA_ARGS__)
0035 #define FinishTest(...) FinishTest_(__LINE__, __FILE__, __VA_ARGS__)
0036 
0037 /// Helper class that represents a change in a document.
0038 class DocChange
0039 {
0040 public:
0041     enum ChangeType { TextRemoved, TextInserted };
0042 
0043     DocChange(ChangeType changeType, KTextEditor::Range changeRange, QString newText = QString())
0044         : m_changeType(changeType)
0045         , m_changeRange(changeRange)
0046         , m_newText(newText)
0047     {
0048         /* There's nothing to do here. */
0049     }
0050 
0051     ChangeType changeType() const
0052     {
0053         return m_changeType;
0054     }
0055 
0056     KTextEditor::Range changeRange() const
0057     {
0058         return m_changeRange;
0059     }
0060 
0061     QString newText() const
0062     {
0063         return m_newText;
0064     }
0065 
0066 private:
0067     ChangeType m_changeType;
0068     KTextEditor::Range m_changeRange;
0069     QString m_newText;
0070 };
0071 
0072 class BaseTest : public QObject
0073 {
0074     Q_OBJECT
0075 
0076 public:
0077     BaseTest();
0078     ~BaseTest() override;
0079 
0080     static void waitForCompletionWidgetToActivate(KTextEditor::ViewPrivate *kate_view);
0081 
0082 protected:
0083     // Begin/Do/Finish test.
0084 
0085     enum Expectation { ShouldPass, ShouldFail };
0086 
0087     void TestPressKey(const QString &str);
0088     void BeginTest(const QString &original);
0089     void FinishTest_(int line, const char *file, const char *expected, Expectation expectation = ShouldPass, const char *failureReason = nullptr);
0090     void DoTest_(int line,
0091                  const char *file,
0092                  const char *original,
0093                  const char *command,
0094                  const char *expected,
0095                  Expectation expectation = ShouldPass,
0096                  const char *failureReason = nullptr);
0097 
0098     void DoTest2(int line,
0099                  const char *file,
0100                  const QString &original,
0101                  const QString &command,
0102                  const QString &expected,
0103                  Expectation expectation = ShouldPass,
0104                  const char *failureReason = nullptr);
0105 
0106     // Parsing keys.
0107 
0108     Qt::Key parseCodedSpecialKey(const QString &string, int startPos, int *destEndOfCodedKey);
0109     Qt::KeyboardModifier parseCodedModifier(const QString &string, int startPos, int *destEndOfCodedModifier);
0110 
0111     // Emulated command bar.
0112 
0113     KateVi::EmulatedCommandBar *emulatedCommandBar();
0114     QLineEdit *emulatedCommandBarTextEdit();
0115 
0116     // Macros & mappings.
0117 
0118     void clearAllMappings();
0119     void clearAllMacros();
0120 
0121     // Other auxiliar functions.
0122 
0123     void ensureKateViewVisible();
0124 
0125 protected:
0126     KTextEditor::DocumentPrivate *kate_document;
0127     KTextEditor::ViewPrivate *kate_view;
0128     KateViInputMode *vi_input_mode;
0129     KateVi::GlobalState *vi_global;
0130     KateVi::InputModeManager *vi_input_mode_manager;
0131 
0132     bool m_firstBatchOfKeypressesForTest;
0133 
0134     QMainWindow *mainWindow;
0135     QVBoxLayout *mainWindowLayout;
0136 
0137     std::map<QString, Qt::Key> m_codesToSpecialKeys;
0138     std::map<QString, Qt::KeyboardModifier> m_codesToModifiers;
0139 
0140     QList<DocChange> m_docChanges;
0141 
0142 protected Q_SLOTS:
0143     void init();
0144 
0145 private Q_SLOTS:
0146     void textInserted(KTextEditor::Document *document, KTextEditor::Range range);
0147     void textRemoved(KTextEditor::Document *document, KTextEditor::Range range);
0148 };
0149 
0150 #endif /* BASE_TEST_H */