~ubuntu-branches/ubuntu/utopic/kdebase/utopic

« back to all changes in this revision

Viewing changes to apps/konsole/src/tests/PartTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Debian Qt/KDE Maintainers, José Manuel Santamaría Lema, Modestas Vainius
  • Date: 2011-05-26 02:53:50 UTC
  • mfrom: (0.7.7 upstream) (0.4.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 296.
  • Revision ID: james.westby@ubuntu.com-20110526025350-7o10g65yegec2rnq
Tags: 4:4.6.3-1
* New upstream release.

[ José Manuel Santamaría Lema ]
* Bump kde-sc-dev-latest build dependency to 4:4.6.3.
* Bump Standards-Version to 3.9.2; no changes needed.

[ Modestas Vainius ]
* Enable DLRestrictions for libraries in this package. Requires
  libdlrestrictions-dev 0.14 and kdelibs5-dev 4:4.6.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright 2008 by Robert Knight <robertknight@gmail.com>
3
 
 
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.
8
 
 
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.
13
 
 
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.
18
 
*/
19
 
 
20
 
// Own
21
 
#include "PartTest.h"
22
 
 
23
 
// Qt
24
 
#include <QtGui/QWidget>
25
 
#include <QtGui/QLabel>
26
 
#include <QtGui/QVBoxLayout>
27
 
#include <QtGui/QKeyEvent>
28
 
#include <qtestkeyboard.h>
29
 
 
30
 
// System
31
 
#include <termios.h>
32
 
#include <sys/types.h>
33
 
 
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>
45
 
 
46
 
// Konsole
47
 
#include "../Pty.h"
48
 
#include "../Session.h"
49
 
#include "../KeyboardTranslator.h"
50
 
 
51
 
using namespace Konsole;
52
 
 
53
 
void PartTest::testFd()
54
 
{
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());
61
 
 
62
 
        int fd = ptyProcess.pty()->masterFd();
63
 
 
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);
69
 
                
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);
74
 
        
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();
82
 
 
83
 
        delete terminalPart;
84
 
        delete dialog;
85
 
        ptyProcess.kill();
86
 
        ptyProcess.waitForFinished(1000);
87
 
}
88
 
void PartTest::testShortcutOverride()
89
 
{
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()
92
 
        //
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.
98
 
        
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()));
106
 
 
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();
115
 
 
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&))) );
122
 
 
123
 
        //QTest::keyClick(terminalPart->widget(),Qt::Key_S,Qt::ControlModifier);
124
 
        _shortcutEventLoop = new QEventLoop();
125
 
        _shortcutEventLoop->exec();
126
 
 
127
 
        QVERIFY(_overrideCalled);
128
 
        QVERIFY(_shortcutTriggered);
129
 
        QVERIFY(!_override);
130
 
 
131
 
        // Test shortcut with override enabled, so the shortcut will not be triggered
132
 
        _override = true;
133
 
        _overrideCalled = false;
134
 
        _shortcutTriggered = false;
135
 
 
136
 
        //QTest::keyClick(terminalPart->widget(),Qt::Key_S,Qt::ControlModifier);
137
 
        _shortcutEventLoop->exec();
138
 
 
139
 
        QVERIFY(_overrideCalled);
140
 
        QVERIFY(!_shortcutTriggered);
141
 
        QVERIFY(_override);
142
 
 
143
 
        delete _shortcutEventLoop;
144
 
        delete terminalPart;
145
 
        delete mainWindow;
146
 
}
147
 
void PartTest::overrideShortcut(QKeyEvent* event,bool& override)
148
 
{
149
 
        QVERIFY(override == true);
150
 
        if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S) 
151
 
        {
152
 
                _overrideCalled = true;
153
 
                override = _override;
154
 
                _shortcutEventLoop->exit();
155
 
        }
156
 
}
157
 
void PartTest::shortcutTriggered()
158
 
{
159
 
        _shortcutTriggered = true;
160
 
}
161
 
 
162
 
KParts::Part* PartTest::createPart()
163
 
{
164
 
        KPluginLoader loader("libkonsolepart");
165
 
        KPluginFactory* factory = loader.factory();
166
 
        Q_ASSERT(factory);
167
 
 
168
 
        KParts::Part* terminalPart = factory->create<KParts::Part>(this);
169
 
        
170
 
        return terminalPart;
171
 
}
172
 
 
173
 
QTEST_KDEMAIN( PartTest , GUI )
174
 
 
175
 
#include "PartTest.moc"
176