Make the menu bar keyboard test pass on macOS

tst_menubarkeyboard's control case presses Alt+F and expects the File
menu to open. On macOS it never did, for two reasons unrelated to the
F10 shortcut it guards: a QMenuBar is native there, so its menus live
in the system menu bar where QTest key events do not reach them, and Qt
does not turn "&File" into an Alt+F mnemonic on macOS at all. The test
has failed on every Mac build since bd6bed8d6 added it.

On macOS the test now uses an in-window menu bar and switches auto
mnemonics on for its own process, which runs the same QMenuBar code the
other platforms exercise. What it still does not prove on macOS is the
native bar: QETMainWindow::activateMenuBar() calls setActiveAction() on
a bar the system draws, and only F10 in the running application can
say what that does there. Other platforms are unchanged.

Fixes #948.
This commit is contained in:
Jeff Patterson
2026-09-19 09:27:19 -05:00
parent a6b4c3c673
commit aef56fe41d
+26
View File
@@ -21,6 +21,12 @@
#include <QMenuBar>
#include <QShortcut>
#ifdef Q_OS_MACOS
// Exported from QtGui but declared in qkeysequence.h only for the
// documentation build; Qt's own docs say to declare it like this.
Q_GUI_EXPORT void qt_set_sequence_auto_mnemonic(bool b);
#endif
/*
F10 should open the menu bar, as it does in most applications and as
someone working without a mouse will expect. Qt provides this on Windows
@@ -38,6 +44,7 @@ class TstMenuBarKeyboard : public QObject
Q_OBJECT
private slots:
void initTestCase();
void altLetterOpensMenu(); // control -- must pass, or nothing below means anything
void plainF10DoesNothingInQt(); // the gap being filled
void shortcutOpensMenuBar(); // the mechanism QETMainWindow uses
@@ -48,6 +55,13 @@ namespace {
QMainWindow *makeWindow(QMenu **file_menu)
{
auto *w = new QMainWindow;
#ifdef Q_OS_MACOS
// A QMenuBar is native on macOS: its menus live in the system menu
// bar, outside the Qt widget tree, where QTest key events never reach
// them and QMenu::isVisible() stays false. The in-window bar runs the
// same QMenuBar code the other platforms use.
w->menuBar()->setNativeMenuBar(false);
#endif
*file_menu = w->menuBar()->addMenu(QStringLiteral("&File"));
(*file_menu)->addAction(QStringLiteral("Quit"));
w->menuBar()->addMenu(QStringLiteral("&Edit"))->addAction(QStringLiteral("Copy"));
@@ -60,6 +74,18 @@ QMainWindow *makeWindow(QMenu **file_menu)
}
/*
macOS has no Alt+letter menu mnemonics, so Qt does not turn "&File"
into a shortcut there (qt_set_sequence_auto_mnemonic is off). The
control below needs one; switch mnemonics on for this process.
*/
void TstMenuBarKeyboard::initTestCase()
{
#ifdef Q_OS_MACOS
qt_set_sequence_auto_mnemonic(true);
#endif
}
/*
The control. Alt and a menu's letter is known to work, so if this fails
the environment cannot open menus at all and the other two tests are