From e8bf2c6eba6e295ed7ef9807a07b8f2ef8166b19 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 18 Jan 2022 14:49:45 +0100 Subject: Further improve QShortcut test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Get rid of the function-static main window, which would be destroyed after QApplication if the test is run with a subset of test row (that does not include the TestEnd state test). Make the MainWindow a class member of the test class instead, and rename it from "mainW" to "mainWindow" to avoid shadowing by "mainW" widgets in other test functions. Amends 55928821d1b119112c520af3b83dc9ed210cf5f0. Task-number: QTBUG-99630 Pick-to: 6.2 6.3 Change-Id: I83efce5b54afc3a0027a7c0e63efee6a235af585 Reviewed-by: Fabian Kosmale Reviewed-by: Tor Arne Vestbø --- .../widgets/kernel/qshortcut/tst_qshortcut.cpp | 135 ++++++++++----------- 1 file changed, 67 insertions(+), 68 deletions(-) (limited to 'tests/auto/widgets/kernel') diff --git a/tests/auto/widgets/kernel/qshortcut/tst_qshortcut.cpp b/tests/auto/widgets/kernel/qshortcut/tst_qshortcut.cpp index 5530fee72a..a9cd037b38 100644 --- a/tests/auto/widgets/kernel/qshortcut/tst_qshortcut.cpp +++ b/tests/auto/widgets/kernel/qshortcut/tst_qshortcut.cpp @@ -53,6 +53,62 @@ class QMainWindow; class QTextEdit; QT_END_NAMESPACE +class TestEdit : public QTextEdit +{ + Q_OBJECT +public: + TestEdit(QWidget *parent, const char *name) + : QTextEdit(parent) + { + setObjectName(name); + } + +protected: + bool event(QEvent *e) override + { + // Make testedit allow any Ctrl+Key as shortcut + if (e->type() == QEvent::ShortcutOverride) { + QKeyEvent *ke = static_cast(e); + if (ke->modifiers() == Qt::ControlModifier + && ke->key() > Qt::Key_Any + && ke->key() < Qt::Key_ydiaeresis) { + ke->ignore(); + return true; + } + } + + // If keypress not processed as normal, check for + // Ctrl+Key event, and input custom string for + // result comparison. + if (e->type() == QEvent::KeyPress) { + QKeyEvent *ke = static_cast(e); + if (ke->modifiers() && ke->key() > Qt::Key_Any + && ke->key() < Qt::Key_ydiaeresis) { + const QChar c = QLatin1Char(char(ke->key())); + if (ke->modifiers() == Qt::ControlModifier) + insertPlainText(QLatin1String("')); + else if (ke->modifiers() == Qt::AltModifier) + insertPlainText(QLatin1String("')); + else if (ke->modifiers() == Qt::ShiftModifier) + insertPlainText(QLatin1String("')); + return true; + } + } + return QTextEdit::event(e); + } +}; + +class MainWindow : public QMainWindow +{ +public: + MainWindow(); + + TestEdit *testEdit() const { return m_testEdit; } + +private: + TestEdit *m_testEdit; +}; + class tst_QShortcut : public QObject { Q_OBJECT @@ -143,62 +199,7 @@ protected: void testElement(); Result ambigResult; -}; - -class TestEdit : public QTextEdit -{ - Q_OBJECT -public: - TestEdit(QWidget *parent, const char *name) - : QTextEdit(parent) - { - setObjectName(name); - } - -protected: - bool event(QEvent *e) override - { - // Make testedit allow any Ctrl+Key as shortcut - if (e->type() == QEvent::ShortcutOverride) { - QKeyEvent *ke = static_cast(e); - if (ke->modifiers() == Qt::ControlModifier - && ke->key() > Qt::Key_Any - && ke->key() < Qt::Key_ydiaeresis) { - ke->ignore(); - return true; - } - } - - // If keypress not processed as normal, check for - // Ctrl+Key event, and input custom string for - // result comparison. - if (e->type() == QEvent::KeyPress) { - QKeyEvent *ke = static_cast(e); - if (ke->modifiers() && ke->key() > Qt::Key_Any - && ke->key() < Qt::Key_ydiaeresis) { - const QChar c = QLatin1Char(char(ke->key())); - if (ke->modifiers() == Qt::ControlModifier) - insertPlainText(QLatin1String("')); - else if (ke->modifiers() == Qt::AltModifier) - insertPlainText(QLatin1String("')); - else if (ke->modifiers() == Qt::ShiftModifier) - insertPlainText(QLatin1String("')); - return true; - } - } - return QTextEdit::event(e); - } -}; - -class MainWindow : public QMainWindow -{ -public: - MainWindow(); - - TestEdit *testEdit() const { return m_testEdit; } - -private: - TestEdit *m_testEdit; + QScopedPointer mainWindow; }; MainWindow::MainWindow() @@ -1276,8 +1277,6 @@ void tst_QShortcut::testElement() if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) QSKIP("Wayland: This fails. Figure out why."); - static QScopedPointer mainW; - currentResult = NoResult; QFETCH(tst_QShortcut::Action, action); QFETCH(tst_QShortcut::Widget, testWidget); @@ -1294,29 +1293,29 @@ void tst_QShortcut::testElement() auto mainWindowDeleter = qScopeGuard([&]{ if (action == TestEnd) - mainW.reset(); + mainWindow.reset(); }); - if (mainW.isNull()) - mainW.reset(new MainWindow); - mainW->setWindowTitle(QTest::currentTestFunction()); - mainW->show(); - mainW->activateWindow(); + if (mainWindow.isNull()) + mainWindow.reset(new MainWindow); + mainWindow->setWindowTitle(QTest::currentTestFunction()); + mainWindow->show(); + mainWindow->activateWindow(); // Don't use QVERIFY here; the data function uses QEXPECT_FAIL, // which would result in an XPASS failure. - if (!QTest::qWaitForWindowActive(mainW.data())) + if (!QTest::qWaitForWindowActive(mainWindow.data())) QVERIFY(false); switch (action) { case ClearAll: - qDeleteAll(mainW->findChildren()); + qDeleteAll(mainWindow->findChildren()); break; case SetupAccel: - setupShortcut(mainW.data(), txt, testWidget, txt.isEmpty() + setupShortcut(mainWindow.data(), txt, testWidget, txt.isEmpty() ? QKeySequence(k1, k2, k3, k4) : QKeySequence::fromString(txt)); break; case TestAccel: - sendKeyEvents(mainW.data(), k1, char16_t(c1), k2, char16_t(c2), k3, char16_t(c3), k4, char16_t(c4)); + sendKeyEvents(mainWindow.data(), k1, char16_t(c1), k2, char16_t(c2), k3, char16_t(c3), k4, char16_t(c4)); QCOMPARE(currentResult, result); break; case TestEnd: -- cgit v1.2.3