File indexing completed on 2024-05-12 11:57:20

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