testlib: add key sequence function

[ChangeLog][Qt Test] Added keySequence() function.

Task-number: QTBUG-53381
Change-Id: Ib7c3f966fe607f00475ae74aaf070cb988d00141
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
This commit is contained in:
Liang Qi 2017-06-16 10:54:53 +02:00
parent eecf64f61d
commit 7323cd8dc2
3 changed files with 73 additions and 0 deletions

View File

@ -570,6 +570,15 @@
\sa QTest::keyClicks()
*/
/*! \fn void QTest::keySequence(QWidget *widget, const QKeySequence &keySequence)
\overload
\since 5.10
Simulates typing of \a keySequence into a \a widget.
\sa QTest::keyClick(), QTest::keyClicks()
*/
/*! \fn void QTest::keyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
\overload
\since 5.0
@ -606,6 +615,15 @@
\sa QTest::keyClicks()
*/
/*! \fn void QTest::keySequence(QWindow *window, const QKeySequence &keySequence)
\overload
\since 5.10
Simulates typing of \a keySequence into a \a window.
\sa QTest::keyClick(), QTest::keyClicks()
*/
/*! \fn void QTest::keyEvent(KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
Sends a Qt key event to \a widget with the given \a key and an associated \a action.

View File

@ -54,6 +54,7 @@
#include <QtGui/qguiapplication.h>
#include <QtGui/qwindow.h>
#include <QtGui/qevent.h>
#include <QtGui/qkeysequence.h>
#ifdef QT_WIDGETS_LIB
#include <QtWidgets/qwidget.h>
@ -165,6 +166,15 @@ namespace QTest
Q_DECL_UNUSED inline static void keyPress(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
{ keyEvent(Press, window, key, modifier, delay); }
Q_DECL_UNUSED inline static void keySequence(QWindow *window, const QKeySequence &keySequence)
{
for (int i = 0; i < keySequence.count(); ++i) {
const Qt::Key key = Qt::Key(keySequence[i] & ~Qt::KeyboardModifierMask);
const Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(keySequence[i] & Qt::KeyboardModifierMask);
keyClick(window, key, modifiers);
}
}
#ifdef QT_WIDGETS_LIB
static void simulateEvent(QWidget *widget, bool press, int code,
Qt::KeyboardModifiers modifier, QString text, bool repeat, int delay=-1)
@ -294,6 +304,16 @@ namespace QTest
{ keyEvent(Release, widget, key, modifier, delay); }
inline static void keyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1)
{ keyEvent(Click, widget, key, modifier, delay); }
inline static void keySequence(QWidget *widget, const QKeySequence &keySequence)
{
for (int i = 0; i < keySequence.count(); ++i) {
const Qt::Key key = Qt::Key(keySequence[i] & ~Qt::KeyboardModifierMask);
const Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(keySequence[i] & Qt::KeyboardModifierMask);
keyClick(widget, key, modifiers);
}
}
#endif // QT_WIDGETS_LIB
}

View File

@ -62,6 +62,7 @@ private slots:
void task229128TriggeredSignalWithoutActiongroup();
void task229128TriggeredSignalWhenInActiongroup();
void repeat();
void keysequence(); // QTBUG-53381
private:
int m_lastEventType;
@ -276,6 +277,40 @@ void tst_QAction::alternateShortcuts()
QTest::keyClick(&testWidget, Qt::Key_A, Qt::ControlModifier);
}
void tst_QAction::keysequence()
{
MyWidget testWidget(this);
testWidget.show();
QApplication::setActiveWindow(&testWidget);
{
QAction act(&testWidget);
testWidget.addAction(&act);
QKeySequence ks(QKeySequence::SelectAll);
act.setShortcut(ks);
QSignalSpy spy(&act, &QAction::triggered);
act.setAutoRepeat(true);
QTest::keySequence(&testWidget, ks);
QCoreApplication::processEvents();
QCOMPARE(spy.count(), 1); // act should have been triggered
act.setAutoRepeat(false);
QTest::keySequence(&testWidget, ks);
QCoreApplication::processEvents();
QCOMPARE(spy.count(), 2); //act should have been triggered a 2nd time
// end of the scope of the action, it will be destroyed and removed from widget
// This action should also unregister its shortcuts
}
// this tests a crash (if the action did not unregister its alternate shortcuts)
QTest::keyClick(&testWidget, Qt::Key_A, Qt::ControlModifier);
}
void tst_QAction::enabledVisibleInteraction()
{
MyWidget testWidget(this);