diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-06-29 14:23:13 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-06-30 08:58:29 +0000 |
commit | 8553e3c13edd1809523ee35622b65777292a9a88 (patch) | |
tree | 430ede7d3a774314f145a76c7e021db723855c7a /tests/auto | |
parent | 97fe98a269d0f05ba4ced62946af4ab5e7b3c624 (diff) |
QQuickMenu: fix mnemonics for dynamic action/submenu items
Previously mnemonics worked only for "normal" menu items, but not for
dynamically created menu items that present actions and sub-menus. This
patch fixes the issue by specifying the menu as a QObject-parent for
such dynamically created items, because QQuickShortcutContext is based
on QObject-hierarchy.
Change-Id: I3637ef06e9cb2a27673e4618c253279a0123b7c8
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/menu/data/mnemonics.qml | 87 | ||||
-rw-r--r-- | tests/auto/menu/tst_menu.cpp | 49 |
2 files changed, 136 insertions, 0 deletions
diff --git a/tests/auto/menu/data/mnemonics.qml b/tests/auto/menu/data/mnemonics.qml new file mode 100644 index 00000000..de4cd215 --- /dev/null +++ b/tests/auto/menu/data/mnemonics.qml @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.6 +import QtQuick.Controls 2.3 + +ApplicationWindow { + width: 400 + height: 400 + + property alias menu: menu + property alias action: action + property alias menuItem: menuItem + property alias subMenu: subMenu + property alias subMenuItem: subMenuItem + + Menu { + id: menu + + Action { + id: action + text: "&Action" + } + + MenuItem { + id: menuItem + text: "Menu &Item" + } + + Menu { + id: subMenu + title: "Sub &Menu" + + MenuItem { + id: subMenuItem + text: "&Sub Menu Item" + } + } + } +} diff --git a/tests/auto/menu/tst_menu.cpp b/tests/auto/menu/tst_menu.cpp index 981e95cf..c9105f9e 100644 --- a/tests/auto/menu/tst_menu.cpp +++ b/tests/auto/menu/tst_menu.cpp @@ -67,6 +67,7 @@ private slots: void mouse(); void pressAndHold(); void contextMenuKeyboard(); + void mnemonics(); void menuButton(); void addItem(); void menuSeparator(); @@ -351,6 +352,54 @@ void tst_menu::contextMenuKeyboard() QVERIFY(!menu->isVisible()); } +void tst_menu::mnemonics() +{ +#ifdef Q_OS_MACOS + QSKIP("Mnemonics are not used on macOS"); +#endif + + QQuickApplicationHelper helper(this, QLatin1String("mnemonics.qml")); + + QQuickWindow *window = helper.window; + window->show(); + window->requestActivate(); + QVERIFY(QTest::qWaitForWindowActive(window)); + + QQuickMenu *menu = window->property("menu").value<QQuickMenu *>(); + QQuickAction *action = window->property("action").value<QQuickAction *>(); + QQuickMenuItem *menuItem = window->property("menuItem").value<QQuickMenuItem *>(); + QQuickMenu *subMenu = window->property("subMenu").value<QQuickMenu *>(); + QQuickMenuItem *subMenuItem = window->property("subMenuItem").value<QQuickMenuItem *>(); + QVERIFY(menu && action && menuItem && subMenu && subMenuItem); + + menu->open(); + QTRY_VERIFY(menu->isOpened()); + + QSignalSpy actionSpy(action, &QQuickAction::triggered); + QVERIFY(actionSpy.isValid()); + QTest::keyClick(window, Qt::Key_A, Qt::AltModifier); // "&Action" + QCOMPARE(actionSpy.count(), 1); + + menu->open(); + QTRY_VERIFY(menu->isOpened()); + + QSignalSpy menuItemSpy(menuItem, &QQuickMenuItem::triggered); + QVERIFY(menuItemSpy.isValid()); + QTest::keyClick(window, Qt::Key_I, Qt::AltModifier); // "Menu &Item" + QCOMPARE(menuItemSpy.count(), 1); + + menu->open(); + QTRY_VERIFY(menu->isOpened()); + + QTest::keyClick(window, Qt::Key_M, Qt::AltModifier); // "Sub &Menu" + QTRY_VERIFY(subMenu->isOpened()); + + QSignalSpy subMenuItemSpy(subMenuItem, &QQuickMenuItem::triggered); + QVERIFY(subMenuItemSpy.isValid()); + QTest::keyClick(window, Qt::Key_S, Qt::AltModifier); // "&Sub Menu Item" + QCOMPARE(subMenuItemSpy.count(), 1); +} + void tst_menu::menuButton() { if (QGuiApplication::styleHints()->tabFocusBehavior() != Qt::TabFocusAllControls) |