From c8599f1626caa6502ee64a91015a434eedba6909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 2 Oct 2023 12:40:56 +0200 Subject: [PATCH] Add QShortcut test for Qt::ApplicationShortcut A Qt::ApplicationShortcut shortcut is not tied to a specific window. We do however document that the shortcut "is active when one of the applications windows are active", which seems like a strange limitation, but for now we honor it in our test as well by making another window active. Change-Id: I235230ff69df29ee43d356d3efaeedb20071faf3 Reviewed-by: Volker Hilsheimer --- .../gui/kernel/qshortcut/tst_qshortcut.cpp | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/auto/gui/kernel/qshortcut/tst_qshortcut.cpp b/tests/auto/gui/kernel/qshortcut/tst_qshortcut.cpp index 9f1d70e5459..a0ffaa58549 100644 --- a/tests/auto/gui/kernel/qshortcut/tst_qshortcut.cpp +++ b/tests/auto/gui/kernel/qshortcut/tst_qshortcut.cpp @@ -5,16 +5,38 @@ #include #include #include +#include class tst_QShortcut : public QObject { Q_OBJECT private slots: - void trigger(); + void applicationShortcut(); + void windowShortcut(); }; -void tst_QShortcut::trigger() +void tst_QShortcut::applicationShortcut() +{ + auto *shortcut = new QShortcut(Qt::CTRL | Qt::Key_A, this); + shortcut->setContext(Qt::ApplicationShortcut); + QSignalSpy activatedSpy(shortcut, &QShortcut::activated); + + // Need a window to send key event to, even if the shortcut is application + // global. The documentation for Qt::ApplicationShortcut also says that + // the shortcut "is active when one of the applications windows are active", + // but this is only honored for Qt Widgets, not for Qt Gui. For now we + // activate the window just in case. + QWindow window; + window.show(); + QVERIFY(QTest::qWaitForWindowActive(&window)); + QTRY_COMPARE(QGuiApplication::applicationState(), Qt::ApplicationActive); + QTest::sendKeyEvent(QTest::Shortcut, &window, Qt::Key_A, 'a', Qt::ControlModifier); + + QVERIFY(activatedSpy.size() > 0); +} + +void tst_QShortcut::windowShortcut() { QWindow w; new QShortcut(Qt::CTRL | Qt::Key_Q, &w, SLOT(close()));