From b2f78b796b5b73d4f0732975ffd66f8aa392c001 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Mon, 5 Dec 2016 14:21:40 -0800 Subject: QCocoaMenu: Avoid exception when inserting item already in this menu This should not happen, but it's clearly not the user's fault. So we should try to carry on as gracefully as possible instead of letting Cocoa abort the application. The patch also factors the repeated calls to QCocoaMenuItem:: nsItem() in QCocoaMenu::insertNative() and improves a warning from QCocoaMenuIten::sync(). Change-Id: Id00135c219aaf40fb565b19a65cab68f6d9863b2 Task-number: QTBUG-57404 Reviewed-by: Richard Moe Gustavsen --- .../auto/widgets/widgets/qmenubar/tst_qmenubar.cpp | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp') diff --git a/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp index 3a4c4545df..2c20d03066 100644 --- a/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp +++ b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp @@ -132,6 +132,7 @@ private slots: void taskQTBUG53205_crashReparentNested(); #ifdef Q_OS_MACOS void taskQTBUG56275_reinsertMenuInParentlessQMenuBar(); + void QTBUG_57404_existingMenuItemException(); #endif void platformMenu(); @@ -1539,6 +1540,32 @@ void tst_QMenuBar::taskQTBUG56275_reinsertMenuInParentlessQMenuBar() QVERIFY(tst_qmenubar_taskQTBUG56275(&menubar)); } + +void tst_QMenuBar::QTBUG_57404_existingMenuItemException() +{ + QMainWindow mw1; + QMainWindow mw2; + mw1.show(); + mw2.show(); + + QMenuBar *mb = new QMenuBar(&mw1); + mw1.setMenuBar(mb); + mb->show(); + QMenu *editMenu = new QMenu(QLatin1String("Edit"), &mw1); + mb->addMenu(editMenu); + QAction *copyAction = editMenu->addAction("&Copy"); + copyAction->setShortcut(QKeySequence("Ctrl+C")); + QTest::ignoreMessage(QtWarningMsg, "Menu item \"&Copy\" has unsupported role QPlatformMenuItem::MenuRole(NoRole)"); + copyAction->setMenuRole(QAction::NoRole); + + QVERIFY(QTest::qWaitForWindowExposed(&mw2)); + QTest::qWait(100); + QTest::ignoreMessage(QtWarningMsg, "Menu item \"&Copy\" already in menu \"Edit\""); + mw2.close(); + mw1.activateWindow(); + QTest::qWait(100); + // No crash, all fine. +} #endif // Q_OS_MACOS QTEST_MAIN(tst_QMenuBar) -- cgit v1.2.3 From 093e1111ef68a8a83e42d5d82fc9d5c93d47071b Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Mon, 5 Dec 2016 14:38:12 -0800 Subject: QCocoaMenu: Don't rely on tags when we can get the actual NSMenuItem -[NSMenu itemWithTag:] clearly states that it'll return the first item with that tag. Furthermore, when and item has been synced more than once, it could be that more than one such item exists in the same menu (e.g. lately changing the role of Edit->Copy). Change-Id: I95a4f0a151659ae273ba03a3cab4a720b781fc3a Task-number: QTBUG-57404 Reviewed-by: Jake Petroules --- tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp') diff --git a/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp index 2c20d03066..d863f70125 100644 --- a/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp +++ b/tests/auto/widgets/widgets/qmenubar/tst_qmenubar.cpp @@ -1560,11 +1560,10 @@ void tst_QMenuBar::QTBUG_57404_existingMenuItemException() QVERIFY(QTest::qWaitForWindowExposed(&mw2)); QTest::qWait(100); - QTest::ignoreMessage(QtWarningMsg, "Menu item \"&Copy\" already in menu \"Edit\""); mw2.close(); mw1.activateWindow(); QTest::qWait(100); - // No crash, all fine. + // No crash, all fine. Ideally, there should be only one warning. } #endif // Q_OS_MACOS -- cgit v1.2.3