Simplify tst_QShortcut

There's no need for the manual sendKeyEvent, as QTest already
has the ability to send synthetic clicks, and we don't need
a colored window to test shortcut activation.

Change-Id: I8409888664e2316bec4ea64f21dbb8b6915091f4
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit 8606f33641a0a2fb9926ebb604a7fc5894e5fc39)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2023-09-29 13:58:58 +02:00 committed by Qt Cherry-pick Bot
parent a1098f4462
commit 2cd1f9f03d

View File

@ -4,53 +4,24 @@
#include <QTest>
#include <QtGui/qguiapplication.h>
#include <QtGui/qshortcut.h>
#include <QtGui/qpainter.h>
#include <QtGui/qrasterwindow.h>
#include <QtGui/qscreen.h>
#include <QtGui/qwindow.h>
class tst_QShortcut : public QObject
{
Q_OBJECT
public:
private slots:
void trigger();
};
class ColoredWindow : public QRasterWindow {
public:
ColoredWindow(QColor c) : m_color(c) {}
protected:
void paintEvent(QPaintEvent *event) override;
private:
const QColor m_color;
};
void ColoredWindow::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.fillRect(QRect(QPoint(), size()), m_color);
}
static void sendKey(QWindow *target, Qt::Key k, char c, Qt::KeyboardModifiers modifiers)
{
QTest::sendKeyEvent(QTest::Press, target, k, c, modifiers);
QTest::sendKeyEvent(QTest::Release, target, k, c, modifiers);
}
void tst_QShortcut::trigger()
{
ColoredWindow w(Qt::yellow);
w.setTitle(QTest::currentTestFunction());
w.resize(QGuiApplication::primaryScreen()->size() / 4);
QWindow w;
new QShortcut(Qt::CTRL | Qt::Key_Q, &w, SLOT(close()));
w.show();
QVERIFY(QTest::qWaitForWindowExposed(&w));
QTRY_VERIFY(QGuiApplication::applicationState() == Qt::ApplicationActive);
sendKey(&w, Qt::Key_Q, 'q', Qt::ControlModifier);
QTest::sendKeyEvent(QTest::Click, &w, Qt::Key_Q, 'q', Qt::ControlModifier);
QTRY_VERIFY(!w.isVisible());
}