aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/quick
diff options
context:
space:
mode:
authorMohammadHossein Qanbari <mohammad.qanbari@qt.io>2024-02-14 14:26:15 +0100
committerMohammadHossein Qanbari <mohammad.qanbari@qt.io>2024-04-02 23:22:06 +0100
commita116ecfeae7bcd2424e848cc25728bdec35aeea2 (patch)
tree8fce34e0f91d69703b96fd7b35ad60c153987e10 /tests/auto/quick
parent8e3edefc810799a56fe2e3805a5a5065c8242715 (diff)
Shortcut: Match window shortcut when its window is active
The window shortcut declared in the embedded QQuickView did not get activated unless the QQuickView was the focus window, instead of getting activated because the containing window was active. Now, the shortcut's window is checked to be active, and if it's not, the matcher dose not match the shortcut. The testcase creates a shortcut event in a window which contains an embedded window shortcut. It should pass when the embedded shortcut's window is active. Task-number: QTBUG-121785 Change-Id: I0a27904d73492f1f8ee129441872cc5f2482b785 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
Diffstat (limited to 'tests/auto/quick')
-rw-r--r--tests/auto/quick/qquickshortcut/data/embedded.qml20
-rw-r--r--tests/auto/quick/qquickshortcut/tst_qquickshortcut.cpp87
2 files changed, 107 insertions, 0 deletions
diff --git a/tests/auto/quick/qquickshortcut/data/embedded.qml b/tests/auto/quick/qquickshortcut/data/embedded.qml
new file mode 100644
index 0000000000..f2d5df22ff
--- /dev/null
+++ b/tests/auto/quick/qquickshortcut/data/embedded.qml
@@ -0,0 +1,20 @@
+// Copyright (C) 2024 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+import QtQuick
+import QtQuick.Controls
+
+Rectangle {
+ id: root
+
+ width: 300
+ height: 300
+
+ property bool activated: false
+ property alias shortcut: shortcut
+
+ Shortcut {
+ id: shortcut
+ onActivated: root.activated = true
+ }
+}
diff --git a/tests/auto/quick/qquickshortcut/tst_qquickshortcut.cpp b/tests/auto/quick/qquickshortcut/tst_qquickshortcut.cpp
index 22e1fc21b8..312f77e99a 100644
--- a/tests/auto/quick/qquickshortcut/tst_qquickshortcut.cpp
+++ b/tests/auto/quick/qquickshortcut/tst_qquickshortcut.cpp
@@ -5,7 +5,11 @@
#include <QtQuick/qquickwindow.h>
#include <QtQml/qqmlapplicationengine.h>
#include <QtQuick/qquickitem.h>
+#include <QtQuick/qquickview.h>
#include <QtTest/qsignalspy.h>
+#ifdef QT_WIDGETS_LIB
+#include <QtWidgets/qboxlayout.h>
+#endif
#ifdef QT_QUICKWIDGETS_LIB
#include <QtQuickWidgets/qquickwidget.h>
#endif
@@ -34,6 +38,8 @@ private slots:
void matcher();
void multiple_data();
void multiple();
+ void embedded_data();
+ void embedded();
#ifdef QT_QUICKWIDGETS_LIB
void quickWidgetShortcuts_data();
void quickWidgetShortcuts();
@@ -577,6 +583,87 @@ void tst_QQuickShortcut::contextChange()
QCOMPARE(inactiveWindow->property("activated").toBool(), activated);
}
+void tst_QQuickShortcut::embedded_data()
+{
+ QTest::addColumn<Qt::Key>("testKey");
+ QTest::addColumn<Qt::KeyboardModifiers>("testModifiers");
+ QTest::addColumn<QString>("windowShortcutSequence");
+ QTest::addColumn<bool>("windowShortcutActivated");
+
+ QTest::newRow("windowActivated") << Qt::Key_W << (Qt::ControlModifier|Qt::AltModifier)
+ << "Ctrl+Alt+W" << true;
+}
+
+void tst_QQuickShortcut::embedded()
+{
+#ifndef QT_WIDGETS_LIB
+ QSKIP("Skipping due to QT_WIDGETS_LIB is not defined");
+#else
+ QFETCH(Qt::Key, testKey);
+ QFETCH(Qt::KeyboardModifiers, testModifiers);
+ QFETCH(QString, windowShortcutSequence);
+ QFETCH(bool, windowShortcutActivated);
+
+ QWidget window;
+ QVBoxLayout *layout = new QVBoxLayout {&window};
+ QQuickView *quickView = new QQuickView;
+ quickView->setResizeMode(QQuickView::SizeRootObjectToView);
+ quickView->setSource(testFileUrl("embedded.qml"));
+
+ QWidget *container = QWidget::createWindowContainer(quickView);
+ container->setMinimumSize(quickView->size());
+ container->setFocusPolicy(Qt::TabFocus);
+
+ QWidget *widget = new QWidget;
+ // We will set focus to the widget and its default is NoFocus.
+ widget->setFocusPolicy(Qt::FocusPolicy::StrongFocus);
+
+ layout->addWidget(widget);
+ layout->addWidget(container);
+
+ window.show();
+ QTRY_VERIFY(window.isVisible());
+ // The widget can get focused only when the including window has exposed.
+ QVERIFY(QTest::qWaitForWindowExposed(&window));
+
+ widget->setFocus();
+ QTRY_VERIFY(widget->hasFocus());
+
+ // If the window-context shortcut is expected to be activated,
+ // then the QuickView window needs to be active.
+ // On Linux, the embedded QuickView window is active immediately
+ // after the containing window is active, but on Windows,
+ // the embedded QuickView window is activated after a delay
+ // once the containing window is active.
+ if (windowShortcutActivated)
+ QVERIFY(QTest::qWaitForWindowActive(quickView));
+
+ QQuickItem *item = quickView->rootObject();
+ QVERIFY(item);
+
+ QObject *windowShortcut = item->property("shortcut").value<QObject *>();
+ QVERIFY(windowShortcut);
+
+ windowShortcut->setProperty("context", Qt::WindowShortcut);
+ windowShortcut->setProperty("sequence", windowShortcutSequence);
+
+ QTest::keyPress(&window, testKey, testModifiers);
+ QTest::keyRelease(&window, testKey, testModifiers);
+ QCOMPARE(item->property("activated").toBool(), windowShortcutActivated);
+
+ quickView->requestActivate();
+ QTRY_VERIFY(quickView->isActive());
+ QVERIFY(quickView->isActive());
+
+ item->setProperty("activated", false);
+ QVERIFY(!item->property("activated").toBool());
+
+ QTest::keyPress(&window, testKey, testModifiers);
+ QTest::keyRelease(&window, testKey, testModifiers);
+ QCOMPARE(item->property("activated").toBool(), true);
+#endif
+}
+
#ifdef QT_QUICKWIDGETS_LIB
void tst_QQuickShortcut::quickWidgetShortcuts_data()
{