File indexing completed on 2024-04-28 05:50:52

0001 /*
0002     SPDX-FileCopyrightText: 2008 Robert Knight <robertknight@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // Own
0008 #include "PartManualTest.h"
0009 
0010 // Qt
0011 #include <QAction>
0012 #include <QKeyEvent>
0013 #include <QMenu>
0014 #include <QMenuBar>
0015 #include <qtestkeyboard.h>
0016 // System
0017 #include <sys/types.h>
0018 #include <termios.h>
0019 
0020 // KDE
0021 #include <KMainWindow>
0022 #include <KPluginFactory>
0023 #include <QTest>
0024 #include <kservice_version.h>
0025 
0026 // Konsole
0027 #include "../Pty.h"
0028 #include "../session/Session.h"
0029 #include "keyboardtranslator/KeyboardTranslator.h"
0030 
0031 using namespace Konsole;
0032 
0033 void PartManualTest::testShortcutOverride()
0034 {
0035     // FIXME: This test asks the user to press shortcut key sequences manually because
0036     // the result is different than when sending the key press via QTest::keyClick()
0037     //
0038     // When the key presses are sent manually, Konsole::TerminalDisplay::event() is called
0039     // and the overrideShortcut() signal is emitted by the part.
0040     // When the key presses are sent automatically, the shortcut is triggered but
0041     // Konsole::TerminalDisplay::event() is not called and the overrideShortcut() signal is
0042     // not emitted by the part.
0043 
0044     // Create a main window with a menu and a test
0045     // action with a shortcut set to Ctrl+S, which is also used by the terminal
0046     auto mainWindow = new KMainWindow();
0047     QMenu *fileMenu = mainWindow->menuBar()->addMenu(QStringLiteral("File"));
0048     QAction *testAction = fileMenu->addAction(QStringLiteral("Test"));
0049     testAction->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S));
0050     connect(testAction, &QAction::triggered, this, &Konsole::PartManualTest::shortcutTriggered);
0051 
0052     // Create terminal part and embed in into the main window
0053     KParts::Part *terminalPart = createPart();
0054     QVERIFY(terminalPart);
0055     mainWindow->setCentralWidget(terminalPart->widget());
0056     TerminalInterface *terminal = qobject_cast<TerminalInterface *>(terminalPart);
0057     QVERIFY(terminal);
0058     terminal->sendInput(QStringLiteral("Press Ctrl+S twice.\n"));
0059     mainWindow->show();
0060 
0061     // Test shortcut with override disabled, so the shortcut will be triggered
0062     _shortcutTriggered = false;
0063     _override = false;
0064     _overrideCalled = false;
0065     QVERIFY(connect(terminalPart, SIGNAL(overrideShortcut(QKeyEvent *, bool &)), this, SLOT(overrideShortcut(QKeyEvent *, bool &))));
0066 
0067     // QTest::keyClick(terminalPart->widget(),Qt::Key_S,Qt::ControlModifier);
0068     _shortcutEventLoop = new QEventLoop();
0069     _shortcutEventLoop->exec();
0070 
0071     QVERIFY(_overrideCalled);
0072     QVERIFY(_shortcutTriggered);
0073     QVERIFY(!_override);
0074 
0075     // Test shortcut with override enabled, so the shortcut will not be triggered
0076     _override = true;
0077     _overrideCalled = false;
0078     _shortcutTriggered = false;
0079 
0080     // QTest::keyClick(terminalPart->widget(),Qt::Key_S,Qt::ControlModifier);
0081     _shortcutEventLoop->exec();
0082 
0083     QVERIFY(_overrideCalled);
0084     QVERIFY(!_shortcutTriggered);
0085     QVERIFY(_override);
0086 
0087     delete _shortcutEventLoop;
0088     delete terminalPart;
0089     delete mainWindow;
0090 }
0091 void PartManualTest::overrideShortcut(QKeyEvent *event, bool &override)
0092 {
0093     QVERIFY(override);
0094     if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S) {
0095         _overrideCalled = true;
0096         override = _override;
0097         _shortcutEventLoop->exit();
0098     }
0099 }
0100 void PartManualTest::shortcutTriggered()
0101 {
0102     _shortcutTriggered = true;
0103 }
0104 
0105 KParts::Part *PartManualTest::createPart()
0106 {
0107     const KPluginFactory::Result<KParts::Part> result = KPluginFactory::instantiatePlugin<KParts::Part>(KPluginMetaData(QStringLiteral("konsolepart")), this);
0108     Q_ASSERT(result);
0109 
0110     return result.plugin;
0111 }
0112 
0113 QTEST_MAIN(PartManualTest)
0114 
0115 #include "moc_PartManualTest.cpp"