add more spacing
[personal-kdebase.git] / apps / konsole / src / tests / PartTest.cpp
blob2227611e094218eeb3c2cba1f542de0c10bee337
1 /*
2 Copyright 2008 by Robert Knight <robertknight@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
20 // Own
21 #include "PartTest.h"
23 // Qt
24 #include <QtGui/QWidget>
25 #include <QtGui/QLabel>
26 #include <QtGui/QVBoxLayout>
27 #include <QtGui/QKeyEvent>
28 #include <qtestkeyboard.h>
30 // System
31 #include <termios.h>
32 #include <sys/types.h>
34 // KDE
35 #include <KMenu>
36 #include <KMenuBar>
37 #include <KPluginLoader>
38 #include <KPluginFactory>
39 #include <KParts/Part>
40 #include <KPtyProcess>
41 #include <KPtyDevice>
42 #include <KDialog>
43 #include <KMainWindow>
44 #include <qtest_kde.h>
46 // Konsole
47 #include "../Pty.h"
48 #include "../Session.h"
49 #include "../KeyboardTranslator.h"
51 using namespace Konsole;
53 void PartTest::testFd()
55 // start a pty process
56 KPtyProcess ptyProcess;
57 ptyProcess.setProgram("/bin/ping",QStringList() << "localhost");
58 ptyProcess.setPtyChannels(KPtyProcess::AllChannels);
59 ptyProcess.start();
60 QVERIFY(ptyProcess.waitForStarted());
62 int fd = ptyProcess.pty()->masterFd();
64 // create a Konsole part and attempt to connect to it
65 KParts::Part* terminalPart = createPart();
66 bool result = QMetaObject::invokeMethod(terminalPart,"openTeletype",
67 Qt::DirectConnection,Q_ARG(int,fd));
68 QVERIFY(result);
70 // suspend the KPtyDevice so that the embedded terminal gets a chance to
71 // read from the pty. Otherwise the KPtyDevice will simply read everything
72 // as soon as it becomes available and the terminal will not display any output
73 ptyProcess.pty()->setSuspended(true);
75 KDialog* dialog = new KDialog();
76 dialog->setButtons(0);
77 QVBoxLayout* layout = new QVBoxLayout(dialog->mainWidget());
78 layout->addWidget(new QLabel("Output of 'ping localhost' should appear in a terminal below for 5 seconds"));
79 layout->addWidget(terminalPart->widget());
80 QTimer::singleShot(5000,dialog,SLOT(close()));
81 dialog->exec();
83 delete terminalPart;
84 delete dialog;
85 ptyProcess.kill();
86 ptyProcess.waitForFinished(1000);
88 void PartTest::testShortcutOverride()
90 // FIXME: This test asks the user to press shortcut key sequences manually because
91 // the result is different than when sending the key press via QTest::keyClick()
93 // When the key presses are sent manually, Konsole::TerminalDisplay::event() is called
94 // and the overrideShortcut() signal is emitted by the part.
95 // When the key presses are sent automatically, the shortcut is triggered but
96 // Konsole::TerminalDisplay::event() is not called and the overrideShortcut() signal is
97 // not emitted by the part.
99 // Create a main window with a menu and a test
100 // action with a shortcut set to Ctrl+S, which is also used by the terminal
101 KMainWindow* mainWindow = new KMainWindow();
102 QMenu* fileMenu = mainWindow->menuBar()->addMenu("File");
103 QAction* testAction = fileMenu->addAction("Test");
104 testAction->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_S));
105 connect(testAction,SIGNAL(triggered()),this,SLOT(shortcutTriggered()));
107 // Create terminal part and embed in into the main window
108 KParts::Part* terminalPart = createPart();
109 QVERIFY(terminalPart);
110 mainWindow->setCentralWidget(terminalPart->widget());
111 TerminalInterface* terminal = qobject_cast<TerminalInterface*>(terminalPart);
112 QVERIFY(terminal);
113 terminal->sendInput("Press Ctrl+S twice.\n");
114 mainWindow->show();
116 // Test shortcut with override disabled, so the shortcut will be triggered
117 _shortcutTriggered = false;
118 _override = false;
119 _overrideCalled = false;
120 QVERIFY( connect(terminalPart,SIGNAL(overrideShortcut(QKeyEvent*,bool&)),
121 this,SLOT(overrideShortcut(QKeyEvent*,bool&))) );
123 //QTest::keyClick(terminalPart->widget(),Qt::Key_S,Qt::ControlModifier);
124 _shortcutEventLoop = new QEventLoop();
125 _shortcutEventLoop->exec();
127 QVERIFY(_overrideCalled);
128 QVERIFY(_shortcutTriggered);
129 QVERIFY(!_override);
131 // Test shortcut with override enabled, so the shortcut will not be triggered
132 _override = true;
133 _overrideCalled = false;
134 _shortcutTriggered = false;
136 //QTest::keyClick(terminalPart->widget(),Qt::Key_S,Qt::ControlModifier);
137 _shortcutEventLoop->exec();
139 QVERIFY(_overrideCalled);
140 QVERIFY(!_shortcutTriggered);
141 QVERIFY(_override);
143 delete _shortcutEventLoop;
144 delete terminalPart;
145 delete mainWindow;
147 void PartTest::overrideShortcut(QKeyEvent* event,bool& override)
149 QVERIFY(override == true);
150 if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S)
152 _overrideCalled = true;
153 override = _override;
154 _shortcutEventLoop->exit();
157 void PartTest::shortcutTriggered()
159 _shortcutTriggered = true;
162 KParts::Part* PartTest::createPart()
164 KPluginLoader loader("libkonsolepart");
165 KPluginFactory* factory = loader.factory();
166 Q_ASSERT(factory);
168 KParts::Part* terminalPart = factory->create<KParts::Part>(this);
170 return terminalPart;
173 QTEST_KDEMAIN( PartTest , GUI )
175 #include "PartTest.moc"