summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPaul Olav Tvete <paul.tvete@qt.io>2017-08-23 11:48:11 +0200
committerPaul Olav Tvete <paul.tvete@qt.io>2017-09-26 13:38:06 +0000
commit35781b358887facf954307b0d6c5958ce089eb72 (patch)
tree75c4517ea1b47136e900a66dd15252fbc6b870c1 /tests
parentbc31d2235cbf81c05ef80de723ebc4dc45a89695 (diff)
Return focus to correct widget after showing menu
By the time we call setKeyboardMode(true), the menu may already have taken focus. This change sets keyboardFocusWidget before opening the popup, and makes sure that keyboardFocusWidget is not set to the popup. (We cannot remove the assignment from setKeyboardMode(), since it's called from several places.) [ChangeLog][QtWidgets] Fixed widget losing focus after showing menu second time. Task-number: QTBUG-56860 Change-Id: Ic01726bf694e6f365dd7b601ad555156e0fdf6c5 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp
index 9a0ca0565e..251a351cc1 100644
--- a/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp
+++ b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp
@@ -39,6 +39,7 @@
#include <qstyleoption.h>
#include <QVBoxLayout>
#include <QLabel>
+#include <QPlainTextEdit>
#include <qscreen.h>
#include <qobject.h>
@@ -106,6 +107,7 @@ private slots:
void allowActiveAndDisabled();
#endif
+ void taskQTBUG56860_focus();
void check_endKey();
void check_homeKey();
@@ -710,6 +712,52 @@ void tst_QMenuBar::check_cursorKeys3()
}
#endif
+void tst_QMenuBar::taskQTBUG56860_focus()
+{
+#if defined(Q_OS_DARWIN)
+ QSKIP("Native key events are needed to test menu action activation on macOS.");
+#endif
+ QMainWindow w;
+ QMenuBar *mb = w.menuBar();
+
+ if (mb->platformMenuBar())
+ QSKIP("This test requires the Qt menubar.");
+
+ QMenu *em = mb->addMenu("&Edit");
+ em->setObjectName("EditMenu");
+ em->addAction("&Cut");
+ em->addAction("C&opy");
+ QPlainTextEdit *e = new QPlainTextEdit;
+ e->setObjectName("edit");
+
+ w.setCentralWidget(e);
+ w.show();
+ QApplication::setActiveWindow(&w);
+ QVERIFY(QTest::qWaitForWindowActive(&w));
+
+ QTRY_COMPARE(QApplication::focusWidget(), e);
+
+ // Open menu
+ QTest::keyClick(static_cast<QWidget *>(0), Qt::Key_E, Qt::AltModifier );
+ QTRY_COMPARE(QApplication::activePopupWidget(), em);
+ // key down to trigger focus
+ QTest::keyClick(static_cast<QWidget *>(0), Qt::Key_Down );
+ // and press ENTER to close
+ QTest::keyClick(static_cast<QWidget *>(0), Qt::Key_Enter );
+ QTRY_COMPARE(QApplication::activePopupWidget(), nullptr);
+ // focus should have returned to the editor by now
+ QTRY_COMPARE(QApplication::focusWidget(), e);
+
+ // Now do it all over again...
+ QTest::keyClick(static_cast<QWidget *>(0), Qt::Key_E, Qt::AltModifier );
+ QTRY_COMPARE(QApplication::activePopupWidget(), em);
+ QTest::keyClick(static_cast<QWidget *>(0), Qt::Key_Down );
+ QTest::keyClick(static_cast<QWidget *>(0), Qt::Key_Enter );
+ QTRY_COMPARE(QApplication::activePopupWidget(), nullptr);
+ QTRY_COMPARE(QApplication::focusWidget(), e);
+
+}
+
/*!
If a popupmenu is active you can use home to go quickly to the first item in the menu.
*/