diff options
Diffstat (limited to 'tests/auto/widgets/kernel')
92 files changed, 8407 insertions, 3190 deletions
diff --git a/tests/auto/widgets/kernel/CMakeLists.txt b/tests/auto/widgets/kernel/CMakeLists.txt new file mode 100644 index 0000000000..2d4880ea3c --- /dev/null +++ b/tests/auto/widgets/kernel/CMakeLists.txt @@ -0,0 +1,28 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +add_subdirectory(qapplication) +add_subdirectory(qboxlayout) +add_subdirectory(qformlayout) +add_subdirectory(qgridlayout) +add_subdirectory(qlayout) +add_subdirectory(qstackedlayout) +add_subdirectory(qtooltip) +add_subdirectory(qwidget_window) +add_subdirectory(qwidgetmetatype) +add_subdirectory(qwidgetrepaintmanager) +add_subdirectory(qwidgetsvariant) +add_subdirectory(qwindowcontainer) +add_subdirectory(qsizepolicy) +if(NOT APPLE) + add_subdirectory(qgesturerecognizer) +endif() +add_subdirectory(qwidget) +if(QT_FEATURE_shortcut) + add_subdirectory(qshortcut) +endif() +if(QT_FEATURE_action) + add_subdirectory(qaction) + add_subdirectory(qactiongroup) + add_subdirectory(qwidgetaction) +endif() diff --git a/tests/auto/widgets/kernel/kernel.pro b/tests/auto/widgets/kernel/kernel.pro deleted file mode 100644 index 73fd934502..0000000000 --- a/tests/auto/widgets/kernel/kernel.pro +++ /dev/null @@ -1,26 +0,0 @@ -TEMPLATE=subdirs -SUBDIRS=\ - qaction \ - qactiongroup \ - qapplication \ - qboxlayout \ - qdesktopwidget \ - qformlayout \ - qgesturerecognizer \ - qgridlayout \ - qlayout \ - qstackedlayout \ - qtooltip \ - qwidget \ - qwidget_window \ - qwidgetaction \ - qwidgetmetatype \ - qwidgetsvariant \ - qwindowcontainer \ - qshortcut \ - qsizepolicy - -darwin:SUBDIRS -= \ # Uses native recognizers - qgesturerecognizer \ - -SUBDIRS -= qsound diff --git a/tests/auto/widgets/kernel/qaction/.gitignore b/tests/auto/widgets/kernel/qaction/.gitignore deleted file mode 100644 index bf81f5bf2c..0000000000 --- a/tests/auto/widgets/kernel/qaction/.gitignore +++ /dev/null @@ -1 +0,0 @@ -tst_qaction diff --git a/tests/auto/widgets/kernel/qaction/CMakeLists.txt b/tests/auto/widgets/kernel/qaction/CMakeLists.txt new file mode 100644 index 0000000000..9d1985da0b --- /dev/null +++ b/tests/auto/widgets/kernel/qaction/CMakeLists.txt @@ -0,0 +1,23 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qaction Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qaction LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qaction + SOURCES + tst_qaction.cpp + LIBRARIES + Qt::CorePrivate + Qt::Gui + Qt::GuiPrivate + Qt::Widgets + Qt::WidgetsPrivate +) diff --git a/tests/auto/widgets/kernel/qaction/qaction.pro b/tests/auto/widgets/kernel/qaction/qaction.pro deleted file mode 100644 index c57107b1b0..0000000000 --- a/tests/auto/widgets/kernel/qaction/qaction.pro +++ /dev/null @@ -1,6 +0,0 @@ -CONFIG += testcase -TARGET = tst_qaction -QT += gui-private core-private widgets testlib -SOURCES += tst_qaction.cpp - - diff --git a/tests/auto/widgets/kernel/qaction/tst_qaction.cpp b/tests/auto/widgets/kernel/qaction/tst_qaction.cpp index 3d68e42baf..2820fd710b 100644 --- a/tests/auto/widgets/kernel/qaction/tst_qaction.cpp +++ b/tests/auto/widgets/kernel/qaction/tst_qaction.cpp @@ -1,42 +1,24 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QDialog> #include <QMainWindow> -#include <QtTest/QtTest> +#include <QTest> +#include <QSignalSpy> #include <qapplication.h> #include <qevent.h> #include <qaction.h> +#include <qactiongroup.h> #include <qmenu.h> +#include <qmenubar.h> +#include <qtoolbar.h> #include <qpa/qplatformtheme.h> +#include <qpa/qplatformintegration.h> #include <private/qguiapplication_p.h> +#include <QtWidgets/private/qapplication_p.h> + class tst_QAction : public QObject { Q_OBJECT @@ -49,28 +31,25 @@ public: private slots: void init(); void cleanup(); - void getSetCheck(); - void setText_data(); - void setText(); - void setIconText_data() { setText_data(); } - void setIconText(); void setUnknownFont(); void actionEvent(); - void setStandardKeys(); +#if QT_CONFIG(shortcut) void alternateShortcuts(); void enabledVisibleInteraction(); - void task200823_tooltip(); - void task229128TriggeredSignalWithoutActiongroup(); +#endif void task229128TriggeredSignalWhenInActiongroup(); +#if QT_CONFIG(shortcut) void repeat(); - void setData(); void keysequence(); // QTBUG-53381 void disableShortcutsWithBlockedWidgets_data(); void disableShortcutsWithBlockedWidgets(); void shortcutFromKeyEvent(); // QTBUG-48325 + void disableShortcutInMenuAction_data(); + void disableShortcutInMenuAction(); +#endif private: - int m_lastEventType; + QEvent::Type m_lastEventType; const int m_keyboardScheme; QAction *m_lastAction; }; @@ -82,7 +61,7 @@ tst_QAction::tst_QAction() void tst_QAction::init() { - m_lastEventType = 0; + m_lastEventType = QEvent::None; m_lastAction = nullptr; } @@ -91,33 +70,6 @@ void tst_QAction::cleanup() QVERIFY(QApplication::topLevelWidgets().isEmpty()); } -// Testing get/set functions -void tst_QAction::getSetCheck() -{ - QAction obj1(0); - // QActionGroup * QAction::actionGroup() - // void QAction::setActionGroup(QActionGroup *) - QActionGroup *var1 = new QActionGroup(0); - obj1.setActionGroup(var1); - QCOMPARE(var1, obj1.actionGroup()); - obj1.setActionGroup((QActionGroup *)0); - QCOMPARE((QActionGroup *)0, obj1.actionGroup()); - delete var1; - - // QMenu * QAction::menu() - // void QAction::setMenu(QMenu *) - QMenu *var2 = new QMenu(0); - obj1.setMenu(var2); - QCOMPARE(var2, obj1.menu()); - obj1.setMenu((QMenu *)0); - QCOMPARE((QMenu *)0, obj1.menu()); - delete var2; - - QCOMPARE(obj1.priority(), QAction::NormalPriority); - obj1.setPriority(QAction::LowPriority); - QCOMPARE(obj1.priority(), QAction::LowPriority); -} - class MyWidget : public QWidget { Q_OBJECT @@ -132,46 +84,9 @@ private: tst_QAction *m_test; }; -void tst_QAction::setText_data() -{ - QTest::addColumn<QString>("text"); - QTest::addColumn<QString>("iconText"); - QTest::addColumn<QString>("textFromIconText"); - - //next we fill it with data - QTest::newRow("Normal") << "Action" << "Action" << "Action"; - QTest::newRow("Ampersand") << "Search && Destroy" << "Search & Destroy" << "Search && Destroy"; - QTest::newRow("Mnemonic and ellipsis") << "O&pen File ..." << "Open File" << "Open File"; -} - -void tst_QAction::setText() -{ - QFETCH(QString, text); - - QAction action(0); - action.setText(text); - - QCOMPARE(action.text(), text); - - QFETCH(QString, iconText); - QCOMPARE(action.iconText(), iconText); -} - -void tst_QAction::setIconText() -{ - QFETCH(QString, iconText); - - QAction action(0); - action.setIconText(iconText); - QCOMPARE(action.iconText(), iconText); - - QFETCH(QString, textFromIconText); - QCOMPARE(action.text(), textFromIconText); -} - void tst_QAction::setUnknownFont() // QTBUG-42728 { - QAction action(0); + QAction action(nullptr); QFont font("DoesNotExist", 11); action.setFont(font); @@ -182,72 +97,44 @@ void tst_QAction::setUnknownFont() // QTBUG-42728 void tst_QAction::updateState(QActionEvent *e) { if (!e) { - m_lastEventType = 0; - m_lastAction = 0; + m_lastEventType = QEvent::None; + m_lastAction = nullptr; } else { - m_lastEventType = (int)e->type(); + m_lastEventType = e->type(); m_lastAction = e->action(); } } void tst_QAction::actionEvent() { - QAction a(0); + QAction a(nullptr); a.setText("action text"); // add action MyWidget testWidget(this); testWidget.show(); - QApplication::setActiveWindow(&testWidget); testWidget.addAction(&a); qApp->processEvents(); - QCOMPARE(m_lastEventType, (int)QEvent::ActionAdded); + QCOMPARE(m_lastEventType, QEvent::ActionAdded); QCOMPARE(m_lastAction, &a); // change action a.setText("new action text"); qApp->processEvents(); - QCOMPARE(m_lastEventType, (int)QEvent::ActionChanged); + QCOMPARE(m_lastEventType, QEvent::ActionChanged); QCOMPARE(m_lastAction, &a); // remove action testWidget.removeAction(&a); qApp->processEvents(); - QCOMPARE(m_lastEventType, (int)QEvent::ActionRemoved); + QCOMPARE(m_lastEventType, QEvent::ActionRemoved); QCOMPARE(m_lastAction, &a); } -//basic testing of standard keys -void tst_QAction::setStandardKeys() -{ - QAction act(0); - act.setShortcut(QKeySequence("CTRL+L")); - QList<QKeySequence> list; - act.setShortcuts(list); - act.setShortcuts(QKeySequence::Copy); - QCOMPARE(act.shortcut(), act.shortcuts().first()); - - QList<QKeySequence> expected; - const QKeySequence ctrlC = QKeySequence(QStringLiteral("CTRL+C")); - const QKeySequence ctrlInsert = QKeySequence(QStringLiteral("CTRL+INSERT")); - switch (m_keyboardScheme) { - case QPlatformTheme::MacKeyboardScheme: - expected << ctrlC; - break; - case QPlatformTheme::WindowsKeyboardScheme: - expected << ctrlC << ctrlInsert; - break; - default: // X11 - expected << ctrlC << ctrlInsert << QKeySequence(QStringLiteral("F16")); - break; - } - - QCOMPARE(act.shortcuts(), expected); -} - +#if QT_CONFIG(shortcut) void tst_QAction::alternateShortcuts() { @@ -255,7 +142,7 @@ void tst_QAction::alternateShortcuts() MyWidget testWidget(this); testWidget.show(); - QApplication::setActiveWindow(&testWidget); + QApplicationPrivate::setActiveWindow(&testWidget); { QAction act(&testWidget); @@ -263,15 +150,15 @@ void tst_QAction::alternateShortcuts() QList<QKeySequence> shlist = QList<QKeySequence>() << QKeySequence("CTRL+P") << QKeySequence("CTRL+A"); act.setShortcuts(shlist); - QSignalSpy spy(&act, SIGNAL(triggered())); + QSignalSpy spy(&act, &QAction::triggered); act.setAutoRepeat(true); QTest::keyClick(&testWidget, Qt::Key_A, Qt::ControlModifier); - QCOMPARE(spy.count(), 1); //act should have been triggered + QCOMPARE(spy.size(), 1); //act should have been triggered act.setAutoRepeat(false); QTest::keyClick(&testWidget, Qt::Key_A, Qt::ControlModifier); - QCOMPARE(spy.count(), 2); //act should have been triggered a 2nd time + QCOMPARE(spy.size(), 2); //act should have been triggered a 2nd time //end of the scope of the action, it will be destroyed and removed from wid //This action should also unregister its shortcuts @@ -286,7 +173,7 @@ void tst_QAction::keysequence() { MyWidget testWidget(this); testWidget.show(); - QApplication::setActiveWindow(&testWidget); + QApplicationPrivate::setActiveWindow(&testWidget); { QAction act(&testWidget); @@ -301,12 +188,12 @@ void tst_QAction::keysequence() act.setAutoRepeat(true); QTest::keySequence(&testWidget, ks); QCoreApplication::processEvents(); - QCOMPARE(spy.count(), 1); // act should have been triggered + QCOMPARE(spy.size(), 1); // act should have been triggered act.setAutoRepeat(false); QTest::keySequence(&testWidget, ks); QCoreApplication::processEvents(); - QCOMPARE(spy.count(), 2); //act should have been triggered a 2nd time + QCOMPARE(spy.size(), 2); //act should have been triggered a 2nd time // end of the scope of the action, it will be destroyed and removed from widget // This action should also unregister its shortcuts @@ -320,9 +207,9 @@ void tst_QAction::enabledVisibleInteraction() { MyWidget testWidget(this); testWidget.show(); - QApplication::setActiveWindow(&testWidget); + QApplicationPrivate::setActiveWindow(&testWidget); - QAction act(0); + QAction act(nullptr); // check defaults QVERIFY(act.isEnabled()); QVERIFY(act.isVisible()); @@ -342,53 +229,22 @@ void tst_QAction::enabledVisibleInteraction() act.setEnabled(true); act.setVisible(false); QTest::keyClick(&testWidget, Qt::Key_T, Qt::ControlModifier); - QCOMPARE(spy.count(), 0); //act is not visible, so don't trigger + QCOMPARE(spy.size(), 0); //act is not visible, so don't trigger act.setVisible(false); act.setEnabled(true); QTest::keyClick(&testWidget, Qt::Key_T, Qt::ControlModifier); - QCOMPARE(spy.count(), 0); //act is not visible, so don't trigger + QCOMPARE(spy.size(), 0); //act is not visible, so don't trigger act.setVisible(true); act.setEnabled(true); QTest::keyClick(&testWidget, Qt::Key_T, Qt::ControlModifier); - QCOMPARE(spy.count(), 1); //act is visible and enabled, so trigger -} - -void tst_QAction::task200823_tooltip() -{ - const QScopedPointer<QAction> action(new QAction("foo", nullptr)); - QString shortcut("ctrl+o"); - action->setShortcut(shortcut); - - // we want a non-standard tooltip that shows the shortcut - action->setToolTip(action->text() + QLatin1String(" (") + action->shortcut().toString() + QLatin1Char(')')); - - QString ref = QLatin1String("foo (") + QKeySequence(shortcut).toString() + QLatin1Char(')'); - QCOMPARE(action->toolTip(), ref); + QCOMPARE(spy.size(), 1); //act is visible and enabled, so trigger } -void tst_QAction::task229128TriggeredSignalWithoutActiongroup() -{ - // test without a group - const QScopedPointer<QAction> actionWithoutGroup(new QAction("Test", nullptr)); - QSignalSpy spyWithoutGroup(actionWithoutGroup.data(), SIGNAL(triggered(bool))); - QCOMPARE(spyWithoutGroup.count(), 0); - actionWithoutGroup->trigger(); - // signal should be emitted - QCOMPARE(spyWithoutGroup.count(), 1); - - // it is now a checkable checked action - actionWithoutGroup->setCheckable(true); - actionWithoutGroup->setChecked(true); - spyWithoutGroup.clear(); - QCOMPARE(spyWithoutGroup.count(), 0); - actionWithoutGroup->trigger(); - // signal should be emitted - QCOMPARE(spyWithoutGroup.count(), 1); -} +#endif // QT_CONFIG(shortcut) void tst_QAction::task229128TriggeredSignalWhenInActiongroup() { - QActionGroup ag(0); + QActionGroup ag(nullptr); QAction *action = new QAction("Test", &ag); QAction *checkedAction = new QAction("Test 2", &ag); ag.addAction(action); @@ -397,21 +253,25 @@ void tst_QAction::task229128TriggeredSignalWhenInActiongroup() checkedAction->setCheckable(true); checkedAction->setChecked(true); - QSignalSpy actionSpy(checkedAction, SIGNAL(triggered(bool))); - QSignalSpy actionGroupSpy(&ag, SIGNAL(triggered(QAction*))); - QCOMPARE(actionGroupSpy.count(), 0); - QCOMPARE(actionSpy.count(), 0); + QSignalSpy actionSpy(checkedAction, QOverload<bool>::of(&QAction::triggered)); + QSignalSpy actionGroupSpy(&ag, QOverload<QAction*>::of(&QActionGroup::triggered)); + QCOMPARE(actionGroupSpy.size(), 0); + QCOMPARE(actionSpy.size(), 0); checkedAction->trigger(); // check that both the group and the action have emitted the signal - QCOMPARE(actionGroupSpy.count(), 1); - QCOMPARE(actionSpy.count(), 1); + QCOMPARE(actionGroupSpy.size(), 1); + QCOMPARE(actionSpy.size(), 1); } +#if QT_CONFIG(shortcut) + void tst_QAction::repeat() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + MyWidget testWidget(this); testWidget.show(); - QApplication::setActiveWindow(&testWidget); QVERIFY(QTest::qWaitForWindowActive(&testWidget)); QAction act(&testWidget); @@ -422,7 +282,7 @@ void tst_QAction::repeat() act.setAutoRepeat(true); QTest::keyPress(&testWidget, Qt::Key_F); QTest::keyRelease(&testWidget, Qt::Key_F); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); spy.clear(); QTest::keyPress(&testWidget, Qt::Key_F); @@ -430,7 +290,7 @@ void tst_QAction::repeat() QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::keyRelease(&testWidget, Qt::Key_F); - QCOMPARE(spy.count(), 3); + QCOMPARE(spy.size(), 3); spy.clear(); act.setAutoRepeat(false); @@ -438,29 +298,14 @@ void tst_QAction::repeat() QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::keyRelease(&testWidget, Qt::Key_F); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); spy.clear(); act.setAutoRepeat(true); QTest::keyPress(&testWidget, Qt::Key_F); QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::keyRelease(&testWidget, Qt::Key_F); - QCOMPARE(spy.count(), 2); -} - -void tst_QAction::setData() // QTBUG-62006 -{ - QAction act(nullptr); - QSignalSpy spy(&act, &QAction::changed); - QCOMPARE(act.data(), QVariant()); - QCOMPARE(spy.count(), 0); - act.setData(QVariant()); - QCOMPARE(spy.count(), 0); - - act.setData(-1); - QCOMPARE(spy.count(), 1); - act.setData(-1); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 2); } void tst_QAction::disableShortcutsWithBlockedWidgets_data() @@ -484,6 +329,9 @@ void tst_QAction::disableShortcutsWithBlockedWidgets_data() void tst_QAction::disableShortcutsWithBlockedWidgets() { + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("Window activation is not supported"); + QMainWindow window; QFETCH(Qt::ShortcutContext, shortcutContext); @@ -502,21 +350,20 @@ void tst_QAction::disableShortcutsWithBlockedWidgets() dialog.show(); QVERIFY(QTest::qWaitForWindowExposed(&dialog)); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); QSignalSpy spy(&action, &QAction::triggered); QTest::keyPress(&window, Qt::Key_1); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); } class ShortcutOverrideWidget : public QWidget { public: - ShortcutOverrideWidget(QWidget *parent = 0) : QWidget(parent), shortcutOverrideCount(0) {} - int shortcutOverrideCount; + using QWidget::QWidget; + int shortcutOverrideCount = 0; protected: - bool event(QEvent *e) + bool event(QEvent *e) override { if (e->type() == QEvent::ShortcutOverride) ++shortcutOverrideCount; @@ -528,6 +375,9 @@ protected: // ShortcutOverride event first before passing it on as a normal KeyEvent. void tst_QAction::shortcutFromKeyEvent() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + ShortcutOverrideWidget testWidget; QAction action; action.setShortcut(Qt::Key_1); @@ -542,9 +392,84 @@ void tst_QAction::shortcutFromKeyEvent() // shortcut route for us QKeyEvent e(QEvent::KeyPress, Qt::Key_1, Qt::NoModifier); QApplication::sendEvent(&testWidget, &e); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QCOMPARE(testWidget.shortcutOverrideCount, 1); } +/* + Ignore actions in menus whose menu action has been hidden or disabled. + The menu entry will not be in the menu bar or parent menu, so the action + is not reachable through interactive means. QTBUG-25743 +*/ +void tst_QAction::disableShortcutInMenuAction_data() +{ + QTest::addColumn<QByteArray>("property"); + + QTest::addRow("visible") << QByteArray("visible"); + QTest::addRow("enabled") << QByteArray("enabled"); +} + +void tst_QAction::disableShortcutInMenuAction() +{ + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("QWindow::requestActivate() is not supported."); + + QFETCH(QByteArray, property); + + QMainWindow mw; + QMenu *testMenu = mw.menuBar()->addMenu("Test"); + QAction *testAction = testMenu->addAction("Test Action"); + testAction->setShortcut(Qt::ControlModifier | Qt::Key_A); + QToolBar *toolBar = new QToolBar; + mw.addToolBar(toolBar); + + mw.show(); + QVERIFY(QTest::qWaitForWindowActive(&mw)); + + int expectedTriggerCount = 0; + QSignalSpy spy(testAction, &QAction::triggered); + + QKeyEvent event(QEvent::KeyPress, Qt::Key_A, Qt::ControlModifier); + QApplication::sendEvent(&mw, &event); + QCOMPARE(spy.size(), ++expectedTriggerCount); + + testMenu->menuAction()->setProperty(property, false); + QApplication::sendEvent(&mw, &event); + QCOMPARE(spy.size(), expectedTriggerCount); + + testMenu->menuAction()->setProperty(property, true); + QApplication::sendEvent(&mw, &event); + QCOMPARE(spy.size(), ++expectedTriggerCount); + + // If the action lives somewhere else, then keep firing even + // if the menu has been hidden or disabled. + toolBar->addAction(testAction); + QApplication::sendEvent(&mw, &event); + QCOMPARE(spy.size(), ++expectedTriggerCount); + + testMenu->menuAction()->setProperty(property, false); + QApplication::sendEvent(&mw, &event); + QCOMPARE(spy.size(), ++expectedTriggerCount); + + // unless all other widgets in which the action lives have + // been hidden... + toolBar->hide(); + QApplication::sendEvent(&mw, &event); + QCOMPARE(spy.size(), expectedTriggerCount); + + // ... or disabled + toolBar->show(); + toolBar->setEnabled(false); + QApplication::sendEvent(&mw, &event); + QCOMPARE(spy.size(), expectedTriggerCount); + + // back to normal + toolBar->setEnabled(true); + QApplication::sendEvent(&mw, &event); + QCOMPARE(spy.size(), ++expectedTriggerCount); +} + +#endif // QT_CONFIG(shortcut) + QTEST_MAIN(tst_QAction) #include "tst_qaction.moc" diff --git a/tests/auto/widgets/kernel/qactiongroup/CMakeLists.txt b/tests/auto/widgets/kernel/qactiongroup/CMakeLists.txt new file mode 100644 index 0000000000..e26ee75bc1 --- /dev/null +++ b/tests/auto/widgets/kernel/qactiongroup/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qactiongroup Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qactiongroup LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qactiongroup + SOURCES + tst_qactiongroup.cpp + LIBRARIES + Qt::Gui + Qt::Widgets +) diff --git a/tests/auto/widgets/kernel/qactiongroup/qactiongroup.pro b/tests/auto/widgets/kernel/qactiongroup/qactiongroup.pro deleted file mode 100644 index 87521edfe9..0000000000 --- a/tests/auto/widgets/kernel/qactiongroup/qactiongroup.pro +++ /dev/null @@ -1,6 +0,0 @@ -CONFIG += testcase -TARGET = tst_qactiongroup -QT += widgets testlib -SOURCES += tst_qactiongroup.cpp - - diff --git a/tests/auto/widgets/kernel/qactiongroup/tst_qactiongroup.cpp b/tests/auto/widgets/kernel/qactiongroup/tst_qactiongroup.cpp index 52ca10d31f..0d42340bfb 100644 --- a/tests/auto/widgets/kernel/qactiongroup/tst_qactiongroup.cpp +++ b/tests/auto/widgets/kernel/qactiongroup/tst_qactiongroup.cpp @@ -1,36 +1,12 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#include <QTest> #include <qmainwindow.h> #include <qmenu.h> #include <qaction.h> +#include <qactiongroup.h> class tst_QActionGroup : public QObject { @@ -38,119 +14,9 @@ class tst_QActionGroup : public QObject private slots: void cleanup() { QVERIFY(QApplication::topLevelWidgets().isEmpty()); } - void enabledPropagation(); - void visiblePropagation(); - void exclusive(); void separators(); - void testActionInTwoQActionGroup(); - void unCheckCurrentAction(); }; -void tst_QActionGroup::enabledPropagation() -{ - QActionGroup testActionGroup( 0 ); - - QAction* childAction = new QAction( &testActionGroup ); - QAction* anotherChildAction = new QAction( &testActionGroup ); - QAction* freeAction = new QAction(0); - - QVERIFY( testActionGroup.isEnabled() ); - QVERIFY( childAction->isEnabled() ); - - testActionGroup.setEnabled( false ); - QVERIFY( !testActionGroup.isEnabled() ); - QVERIFY( !childAction->isEnabled() ); - QVERIFY( !anotherChildAction->isEnabled() ); - - childAction->setEnabled(true); - QVERIFY( !childAction->isEnabled()); - - anotherChildAction->setEnabled( false ); - - testActionGroup.setEnabled( true ); - QVERIFY( testActionGroup.isEnabled() ); - QVERIFY( childAction->isEnabled() ); - QVERIFY( !anotherChildAction->isEnabled() ); - - testActionGroup.setEnabled( false ); - QAction *lastChildAction = new QAction(&testActionGroup); - - QVERIFY(!lastChildAction->isEnabled()); - testActionGroup.setEnabled( true ); - QVERIFY(lastChildAction->isEnabled()); - - freeAction->setEnabled(false); - testActionGroup.addAction(freeAction); - QVERIFY(!freeAction->isEnabled()); - delete freeAction; -} - -void tst_QActionGroup::visiblePropagation() -{ - QActionGroup testActionGroup( 0 ); - - QAction* childAction = new QAction( &testActionGroup ); - QAction* anotherChildAction = new QAction( &testActionGroup ); - QAction* freeAction = new QAction(0); - - QVERIFY( testActionGroup.isVisible() ); - QVERIFY( childAction->isVisible() ); - - testActionGroup.setVisible( false ); - QVERIFY( !testActionGroup.isVisible() ); - QVERIFY( !childAction->isVisible() ); - QVERIFY( !anotherChildAction->isVisible() ); - - anotherChildAction->setVisible(false); - - testActionGroup.setVisible( true ); - QVERIFY( testActionGroup.isVisible() ); - QVERIFY( childAction->isVisible() ); - - QVERIFY( !anotherChildAction->isVisible() ); - - testActionGroup.setVisible( false ); - QAction *lastChildAction = new QAction(&testActionGroup); - - QVERIFY(!lastChildAction->isVisible()); - testActionGroup.setVisible( true ); - QVERIFY(lastChildAction->isVisible()); - - freeAction->setVisible(false); - testActionGroup.addAction(freeAction); - QVERIFY(!freeAction->isVisible()); - delete freeAction; -} - -void tst_QActionGroup::exclusive() -{ - QActionGroup group(0); - group.setExclusive(false); - QVERIFY( !group.isExclusive() ); - - QAction* actOne = new QAction( &group ); - actOne->setCheckable( true ); - QAction* actTwo = new QAction( &group ); - actTwo->setCheckable( true ); - QAction* actThree = new QAction( &group ); - actThree->setCheckable( true ); - - group.setExclusive( true ); - QVERIFY( !actOne->isChecked() ); - QVERIFY( !actTwo->isChecked() ); - QVERIFY( !actThree->isChecked() ); - - actOne->setChecked( true ); - QVERIFY( actOne->isChecked() ); - QVERIFY( !actTwo->isChecked() ); - QVERIFY( !actThree->isChecked() ); - - actTwo->setChecked( true ); - QVERIFY( !actOne->isChecked() ); - QVERIFY( actTwo->isChecked() ); - QVERIFY( !actThree->isChecked() ); -} - void tst_QActionGroup::separators() { QMainWindow mw; @@ -166,62 +32,25 @@ void tst_QActionGroup::separators() separator->setSeparator(true); actGroup.addAction(separator); - QListIterator<QAction*> it(actGroup.actions()); - while (it.hasNext()) - menu.addAction(it.next()); + menu.addActions(actGroup.actions()); - QCOMPARE((int)menu.actions().size(), 2); + QCOMPARE(menu.actions().size(), 2); - it = QListIterator<QAction*>(actGroup.actions()); - while (it.hasNext()) - menu.removeAction(it.next()); + const auto removeActions = [&menu](const QList<QAction *> &actions) { + for (QAction *action : actions) + menu.removeAction(action); + }; + removeActions(actGroup.actions()); - QCOMPARE((int)menu.actions().size(), 0); + QCOMPARE(menu.actions().size(), 0); action = new QAction(&actGroup); action->setText("test two"); - it = QListIterator<QAction*>(actGroup.actions()); - while (it.hasNext()) - menu.addAction(it.next()); + menu.addActions(actGroup.actions()); - QCOMPARE((int)menu.actions().size(), 3); + QCOMPARE(menu.actions().size(), 3); } -void tst_QActionGroup::testActionInTwoQActionGroup() -{ - QAction action1("Action 1", this); - - QActionGroup group1(this); - QActionGroup group2(this); - - group1.addAction(&action1); - group2.addAction(&action1); - - QCOMPARE(action1.actionGroup(), &group2); - QCOMPARE(group2.actions().first(), &action1); - QCOMPARE(group1.actions().isEmpty(), true); -} - -void tst_QActionGroup::unCheckCurrentAction() -{ - QActionGroup group(0); - QAction action1(&group) ,action2(&group); - action1.setCheckable(true); - action2.setCheckable(true); - QVERIFY(!action1.isChecked()); - QVERIFY(!action2.isChecked()); - action1.setChecked(true); - QVERIFY(action1.isChecked()); - QVERIFY(!action2.isChecked()); - QAction *current = group.checkedAction(); - QCOMPARE(current, &action1); - current->setChecked(false); - QVERIFY(!action1.isChecked()); - QVERIFY(!action2.isChecked()); - QVERIFY(!group.checkedAction()); -} - - QTEST_MAIN(tst_QActionGroup) #include "tst_qactiongroup.moc" diff --git a/tests/auto/widgets/kernel/qapplication/BLACKLIST b/tests/auto/widgets/kernel/qapplication/BLACKLIST index d7de7bf16e..c68c7d6b14 100644 --- a/tests/auto/widgets/kernel/qapplication/BLACKLIST +++ b/tests/auto/widgets/kernel/qapplication/BLACKLIST @@ -1,4 +1,3 @@ [touchEventPropagation] # QTBUG-66745 -opensuse opensuse-leap diff --git a/tests/auto/widgets/kernel/qapplication/CMakeLists.txt b/tests/auto/widgets/kernel/qapplication/CMakeLists.txt new file mode 100644 index 0000000000..524db06560 --- /dev/null +++ b/tests/auto/widgets/kernel/qapplication/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qapplication LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +add_subdirectory(desktopsettingsaware) +add_subdirectory(modal) +add_subdirectory(test) + +add_dependencies(tst_qapplication + desktopsettingsaware_helper + modal_helper +) diff --git a/tests/auto/widgets/kernel/qapplication/customstyle.json b/tests/auto/widgets/kernel/qapplication/customstyle.json new file mode 100644 index 0000000000..35b80f636c --- /dev/null +++ b/tests/auto/widgets/kernel/qapplication/customstyle.json @@ -0,0 +1,3 @@ +{ + "Keys": [ "customstyle" ] +} diff --git a/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/CMakeLists.txt b/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/CMakeLists.txt new file mode 100644 index 0000000000..f094a451d2 --- /dev/null +++ b/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/CMakeLists.txt @@ -0,0 +1,19 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## desktopsettingsaware Binary: +##################################################################### + +qt_internal_add_executable(desktopsettingsaware_helper + SOURCES + main.cpp + OUTPUT_DIRECTORY + ${CMAKE_CURRENT_BINARY_DIR}/.. + LIBRARIES + Qt::Gui + Qt::Widgets +) + +## Scopes: +##################################################################### diff --git a/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/desktopsettingsaware.pro b/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/desktopsettingsaware.pro deleted file mode 100644 index 442bf33b61..0000000000 --- a/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/desktopsettingsaware.pro +++ /dev/null @@ -1,14 +0,0 @@ -QT += widgets -CONFIG -= app_bundle - -debug_and_release { - CONFIG(debug, debug|release) { - TARGET = ../../debug/desktopsettingsaware_helper - } else { - TARGET = ../../release/desktopsettingsaware_helper - } -} else { - TARGET = ../desktopsettingsaware_helper -} - -SOURCES += main.cpp diff --git a/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/main.cpp b/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/main.cpp index 5e86bcb529..1bf3eef55c 100644 --- a/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/main.cpp +++ b/tests/auto/widgets/kernel/qapplication/desktopsettingsaware/main.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QApplication> diff --git a/tests/auto/widgets/kernel/qapplication/modal/CMakeLists.txt b/tests/auto/widgets/kernel/qapplication/modal/CMakeLists.txt new file mode 100644 index 0000000000..79c5660650 --- /dev/null +++ b/tests/auto/widgets/kernel/qapplication/modal/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## modal Binary: +##################################################################### + +qt_internal_add_executable(modal_helper + SOURCES + base.cpp base.h + main.cpp + OUTPUT_DIRECTORY + ${CMAKE_CURRENT_BINARY_DIR}/.. + LIBRARIES + Qt::Gui + Qt::Widgets +) + +## Scopes: +##################################################################### diff --git a/tests/auto/widgets/kernel/qapplication/modal/base.cpp b/tests/auto/widgets/kernel/qapplication/modal/base.cpp index 249d402f2e..49a90723dd 100644 --- a/tests/auto/widgets/kernel/qapplication/modal/base.cpp +++ b/tests/auto/widgets/kernel/qapplication/modal/base.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include "base.h" diff --git a/tests/auto/widgets/kernel/qapplication/modal/base.h b/tests/auto/widgets/kernel/qapplication/modal/base.h index 153d9ca420..168da92f97 100644 --- a/tests/auto/widgets/kernel/qapplication/modal/base.h +++ b/tests/auto/widgets/kernel/qapplication/modal/base.h @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #ifndef BASE_H #define BASE_H diff --git a/tests/auto/widgets/kernel/qapplication/modal/main.cpp b/tests/auto/widgets/kernel/qapplication/modal/main.cpp index 9dcb6732fa..3f3496834d 100644 --- a/tests/auto/widgets/kernel/qapplication/modal/main.cpp +++ b/tests/auto/widgets/kernel/qapplication/modal/main.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QApplication> #include "base.h" diff --git a/tests/auto/widgets/kernel/qapplication/modal/modal.pro b/tests/auto/widgets/kernel/qapplication/modal/modal.pro deleted file mode 100644 index e832d90821..0000000000 --- a/tests/auto/widgets/kernel/qapplication/modal/modal.pro +++ /dev/null @@ -1,15 +0,0 @@ -QT += widgets -SOURCES += main.cpp \ - base.cpp -debug_and_release { - CONFIG(debug, debug|release) { - TARGET = ../../debug/modal_helper - } else { - TARGET = ../../release/modal_helper - } -} else { - TARGET = ../modal_helper -} -CONFIG -= app_bundle -HEADERS += base.h - diff --git a/tests/auto/widgets/kernel/qapplication/qapplication.pro b/tests/auto/widgets/kernel/qapplication/qapplication.pro deleted file mode 100644 index 21bfb50aee..0000000000 --- a/tests/auto/widgets/kernel/qapplication/qapplication.pro +++ /dev/null @@ -1,6 +0,0 @@ -TEMPLATE = subdirs - -!winrt: SUBDIRS = desktopsettingsaware modal - -test.depends += $$SUBDIRS -SUBDIRS += test diff --git a/tests/auto/widgets/kernel/qapplication/test/BLACKLIST b/tests/auto/widgets/kernel/qapplication/test/BLACKLIST new file mode 100644 index 0000000000..2cc96e857d --- /dev/null +++ b/tests/auto/widgets/kernel/qapplication/test/BLACKLIST @@ -0,0 +1,21 @@ +# This file is mainly used for Android tests since Android +# packages "test" instead of "qapplication" folder +# QTBUG-87025 +[libraryPaths] +android +[setFont] +android +[closeAllWindows] +android +[libraryPaths_qt_plugin_path_2] +android +[desktopSettingsAware] +android +[applicationPalettePolish] +android +[touchEventPropagation] +android +[wheelEventPropagation] +android +[qtbug_12673] +android diff --git a/tests/auto/widgets/kernel/qapplication/test/CMakeLists.txt b/tests/auto/widgets/kernel/qapplication/test/CMakeLists.txt new file mode 100644 index 0000000000..fc80b8af7e --- /dev/null +++ b/tests/auto/widgets/kernel/qapplication/test/CMakeLists.txt @@ -0,0 +1,31 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## test Test: +##################################################################### + +# Collect test data +list(APPEND test_data "../tmp/README") +list(APPEND test_data "../modal") + +qt_internal_add_test(tst_qapplication + SOURCES + ../tst_qapplication.cpp + LIBRARIES + Qt::CorePrivate + Qt::Gui + Qt::GuiPrivate + Qt::Widgets + Qt::WidgetsPrivate + TESTDATA ${test_data} + OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/.." +) + +## Scopes: +##################################################################### + +qt_internal_extend_target(tst_qapplication CONDITION builtin_testdata + DEFINES + BUILTIN_TESTDATA +) diff --git a/tests/auto/widgets/kernel/qapplication/test/test.pro b/tests/auto/widgets/kernel/qapplication/test/test.pro deleted file mode 100644 index 8ade4d8364..0000000000 --- a/tests/auto/widgets/kernel/qapplication/test/test.pro +++ /dev/null @@ -1,28 +0,0 @@ -CONFIG += testcase - -QT += widgets widgets-private testlib -QT += core-private gui-private - -SOURCES += ../tst_qapplication.cpp - -builtin_testdata: DEFINES += BUILTIN_TESTDATA - -TESTDATA = ../test/test.pro ../tmp/README ../modal - -!android:!winrt: SUBPROGRAMS = desktopsettingsaware modal - -debug_and_release { - CONFIG(debug, debug|release) { - TARGET = ../../debug/tst_qapplication - !android:!winrt: TEST_HELPER_INSTALLS = ../debug/helper - for(file, SUBPROGRAMS): TEST_HELPER_INSTALLS += "../debug/$${file}" - } else { - TARGET = ../../release/tst_qapplication - !android:!winrt: TEST_HELPER_INSTALLS = ../release/helper - for(file, SUBPROGRAMS): TEST_HELPER_INSTALLS += "../release/$${file}" - } -} else { - TARGET = ../tst_qapplication - !android:!winrt: TEST_HELPER_INSTALLS = ../helper - for(file, SUBPROGRAMS): TEST_HELPER_INSTALLS += "../$${file}" -} diff --git a/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp b/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp index 5b4a5d30a5..2eb2d84504 100644 --- a/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp +++ b/tests/auto/widgets/kernel/qapplication/tst_qapplication.cpp @@ -1,35 +1,17 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only +#define QT_STATICPLUGIN +#include <QtWidgets/qstyleplugin.h> #include <qdebug.h> -#include <QtTest/QtTest> +#include <QTest> +#include <QTimer> +#include <QLibraryInfo> +#include <QSignalSpy> +#include <QFileSystemWatcher> +#include <QSharedMemory> #include <QtCore/QAbstractEventDispatcher> #include <QtCore/QFileInfo> @@ -41,6 +23,7 @@ #include <QtGui/QFontDatabase> #include <QtGui/QClipboard> +#include <QtGui/QStyleHints> #include <QtWidgets/QApplication> #include <QtWidgets/QMessageBox> @@ -50,11 +33,17 @@ #include <QtWidgets/QLineEdit> #include <QtWidgets/QLabel> #include <QtWidgets/QMainWindow> +#include <QtWidgets/QScrollArea> +#include <QtWidgets/QScrollBar> +#include <QtWidgets/QHeaderView> #include <QtWidgets/private/qapplication_p.h> #include <QtWidgets/QStyle> +#include <QtWidgets/qproxystyle.h> +#include <QtWidgets/QTextEdit> #include <qpa/qwindowsysteminterface.h> #include <qpa/qwindowsysteminterface_p.h> +#include <private/qevent_p.h> #include <private/qhighdpiscaling_p.h> #include <algorithm> @@ -84,6 +73,7 @@ private slots: void setFont_data(); void setFont(); + void setFontForClass(); void args_data(); void args(); @@ -93,7 +83,11 @@ private slots: void quitOnLastWindowClosed(); void closeAllWindows(); void testDeleteLater(); - void testDeleteLaterProcessEvents(); + void testDeleteLaterProcessEvents1(); + void testDeleteLaterProcessEvents2(); + void testDeleteLaterProcessEvents3(); + void testDeleteLaterProcessEvents4(); + void testDeleteLaterProcessEvents5(); #if QT_CONFIG(library) void libraryPaths(); @@ -101,13 +95,17 @@ private slots: void libraryPaths_qt_plugin_path_2(); #endif +#ifdef QT_BUILD_INTERNAL void sendPostedEvents(); +#endif // ifdef QT_BUILD_INTERNAL void thread(); void desktopSettingsAware(); void setActiveWindow(); + void activateDeactivateEvent(); + void focusWidget(); void focusChanged(); void focusOut(); void focusMouseClick(); @@ -121,6 +119,8 @@ private slots: void task109149(); void style(); + void applicationPalettePolish(); + void setColorScheme(); void allWidgets(); void topLevelWidgets(); @@ -128,8 +128,11 @@ private slots: void setAttribute(); void touchEventPropagation(); + void wheelEventPropagation_data(); + void wheelEventPropagation(); void qtbug_12673(); + void qtbug_103611(); void noQuitOnHide(); void globalStaticObjectDestruction(); // run this last @@ -165,6 +168,21 @@ void tst_QApplication::sendEventsOnProcessEvents() QCoreApplication::postEvent(&app, new QEvent(QEvent::Type(QEvent::User + 1))); QCoreApplication::processEvents(); + +#ifdef Q_OS_LINUX + if ((QSysInfo::productType() == "rhel" && QSysInfo::productVersion().startsWith(u'9')) + || (QSysInfo::productType() == "ubuntu" && QSysInfo::productVersion().startsWith(u'2'))) + { + QFile f("/proc/self/maps"); + QVERIFY(f.open(QIODevice::ReadOnly)); + + QByteArray libs = f.readAll(); + if (libs.contains("libqgtk3.") || libs.contains("libqgtk3TestInfix.")) { + QEXPECT_FAIL("", "Fails if qgtk3 (Glib) is loaded, see QTBUG-87137", Abort); + } + } +#endif + QVERIFY(spy.recordedEvents.contains(QEvent::User + 1)); } @@ -202,12 +220,63 @@ void tst_QApplication::staticSetup() QPalette pal; QApplication::setPalette(pal); - - /*QFont font; - QApplication::setFont(font);*/ + QFont font; + QApplication::setFont(font); int argc = 0; QApplication app(argc, nullptr); + + class EventWatcher : public QObject + { + public: + int palette_changed = 0; + int font_changed = 0; + + EventWatcher() + { + qApp->installEventFilter(this); +#if QT_DEPRECATED_SINCE(6, 0) +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + QObject::connect(qApp, &QApplication::paletteChanged, [&]{ ++palette_changed; }); + QObject::connect(qApp, &QApplication::fontChanged, [&]{ ++font_changed; }); +QT_WARNING_POP +#endif + } + + protected: + bool eventFilter(QObject *, QEvent *event) override + { + switch (event->type()) { + case QEvent::ApplicationPaletteChange: + ++palette_changed; + break; + case QEvent::ApplicationFontChange: + ++font_changed; + break; + default: + break; + } + + return false; + } + }; + + EventWatcher watcher; + + QCOMPARE(watcher.palette_changed, 0); + QCOMPARE(watcher.font_changed, 0); + qApp->setPalette(QPalette(Qt::red)); + + font.setBold(!font.bold()); + qApp->setFont(font); + QApplication::processEvents(); +#if QT_DEPRECATED_SINCE(6, 0) + QCOMPARE(watcher.palette_changed, 2); + QCOMPARE(watcher.font_changed, 2); +#else + QCOMPARE(watcher.palette_changed, 1); + QCOMPARE(watcher.font_changed, 1); +#endif } @@ -228,9 +297,6 @@ public: void tst_QApplication::alert() { -#ifdef Q_OS_WINRT - QSKIP("WinRT does not support more than 1 native widget at the same time"); -#endif int argc = 0; QApplication app(argc, nullptr); QApplication::alert(nullptr, 0); @@ -247,10 +313,8 @@ void tst_QApplication::alert() QApplication::alert(&widget, -1); QApplication::alert(&widget, 250); widget2.activateWindow(); - QApplication::setActiveWindow(&widget2); QApplication::alert(&widget, 0); widget.activateWindow(); - QApplication::setActiveWindow(&widget); QApplication::alert(&widget, 200); } @@ -313,13 +377,12 @@ void tst_QApplication::setFont_data() int argc = 0; QApplication app(argc, nullptr); // Needed for QFontDatabase - QFontDatabase fdb; - const QStringList &families = fdb.families(); + const QStringList &families = QFontDatabase::families(); for (int i = 0, count = qMin(3, families.size()); i < count; ++i) { const auto &family = families.at(i); - const QStringList &styles = fdb.styles(family); + const QStringList &styles = QFontDatabase::styles(family); if (!styles.isEmpty()) { - QList<int> sizes = fdb.pointSizes(family, styles.constFirst()); + QList<int> sizes = QFontDatabase::pointSizes(family, styles.constFirst()); if (sizes.isEmpty()) sizes = QFontDatabase::standardSizes(); if (!sizes.isEmpty()) { @@ -368,6 +431,46 @@ void tst_QApplication::setFont() QCOMPARE( app.font(), font ); } +class tstHeaderView : public QHeaderView +{ + Q_OBJECT +public: + explicit tstHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) + : QHeaderView(orientation, parent) + {} +}; +class tstFrame : public QFrame { Q_OBJECT }; +class tstWidget : public QWidget { Q_OBJECT }; + +void tst_QApplication::setFontForClass() +{ + // QTBUG-89910 + // If a default font was not registered for the widget's class, + // it returns the default font of its nearest registered superclass. + int argc = 0; + QApplication app(argc, nullptr); + + QFont font; + int pointSize = 10; + const QByteArrayList classNames{"QHeaderView", "QAbstractItemView", "QAbstractScrollView", "QFrame", "QWidget", "QObject"}; + for (auto className : classNames) { + font.setPointSizeF(pointSize++); + app.setFont(font, className.constData()); + } + + tstHeaderView headView(Qt::Horizontal); + tstFrame frame; + tstWidget widget; + + QFont headViewFont = QApplication::font(&headView); + QFont frameFont = QApplication::font(&frame); + QFont widgetFont = QApplication::font(&widget); + + QCOMPARE(headViewFont.pointSize(), QApplication::font("QHeaderView").pointSize()); + QCOMPARE(frameFont.pointSize(), QApplication::font("QFrame").pointSize()); + QCOMPARE(widgetFont.pointSize(), QApplication::font("QWidget").pointSize()); +} + void tst_QApplication::args_data() { QTest::addColumn<int>("argc_in"); @@ -399,8 +502,8 @@ static char **QString2cstrings(const QString &args) { static QByteArrayList cache; - const auto &list = args.splitRef(' '); - auto argarray = new char*[list.count() + 1]; + const auto &list = QStringView{ args }.split(' '); + auto argarray = new char*[list.size() + 1]; int i = 0; for (; i < list.size(); ++i ) { @@ -486,10 +589,10 @@ void tst_QApplication::lastWindowClosed() QPointer<QDialog> dialog = new QDialog; dialog->setWindowTitle(QLatin1String(QTest::currentTestFunction()) + QLatin1String("Dialog")); QVERIFY(dialog->testAttribute(Qt::WA_QuitOnClose)); - QTimer::singleShot(1000, dialog, &QDialog::accept); + QTimer::singleShot(1000, dialog.data(), &QDialog::accept); dialog->exec(); QVERIFY(dialog); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); QPointer<CloseWidget>widget = new CloseWidget; widget->setWindowTitle(QLatin1String(QTest::currentTestFunction()) + QLatin1String("CloseWidget")); @@ -498,7 +601,7 @@ void tst_QApplication::lastWindowClosed() QObject::connect(&app, &QGuiApplication::lastWindowClosed, widget.data(), &QObject::deleteLater); QCoreApplication::exec(); QVERIFY(!widget); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); spy.clear(); delete dialog; @@ -516,7 +619,7 @@ void tst_QApplication::lastWindowClosed() QTimer::singleShot(1000, &app, &QApplication::closeAllWindows); QCoreApplication::exec(); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); } class QuitOnLastWindowClosedDialog : public QDialog @@ -549,8 +652,8 @@ public slots: other.exec(); // verify that the eventloop ran and let the timer fire - QCOMPARE(spy.count(), 1); - QCOMPARE(appSpy.count(), 1); + QCOMPARE(spy.size(), 1); + QCOMPARE(appSpy.size(), 1); } private: @@ -575,7 +678,7 @@ public slots: timer1.setSingleShot(true); timer1.start(1000); dialog.exec(); - QCOMPARE(spy1.count(), 1); + QCOMPARE(spy1.size(), 1); show(); } @@ -596,7 +699,7 @@ void tst_QApplication::quitOnLastWindowClosed() QCoreApplication::exec(); // lastWindowClosed() signal should only be sent after the last dialog is closed - QCOMPARE(appSpy.count(), 2); + QCOMPARE(appSpy.size(), 2); } { int argc = 0; @@ -611,8 +714,8 @@ void tst_QApplication::quitOnLastWindowClosed() timer1.setSingleShot(true); timer1.start(1000); dialog.exec(); - QCOMPARE(spy1.count(), 1); - QCOMPARE(appSpy.count(), 0); + QCOMPARE(spy1.size(), 1); + QCOMPARE(appSpy.size(), 0); QTimer timer2; connect(&timer2, &QTimer::timeout, &app, &QCoreApplication::quit); @@ -621,8 +724,8 @@ void tst_QApplication::quitOnLastWindowClosed() timer2.start(1000); int returnValue = QCoreApplication::exec(); QCOMPARE(returnValue, 0); - QCOMPARE(spy2.count(), 1); - QCOMPARE(appSpy.count(), 0); + QCOMPARE(spy2.size(), 1); + QCOMPARE(appSpy.size(), 0); } { int argc = 0; @@ -653,14 +756,14 @@ void tst_QApplication::quitOnLastWindowClosed() QCoreApplication::exec(); - QCOMPARE(spy.count(), 1); - QVERIFY(spy2.count() < 15); // Should be around 10 if closing caused the quit + QCOMPARE(spy.size(), 1); + QVERIFY(spy2.size() < 15); // Should be around 10 if closing caused the quit } bool quitApplicationTriggered = false; auto quitSlot = [&quitApplicationTriggered] () { quitApplicationTriggered = true; - QCoreApplication::quit(); + QCoreApplication::exit(); }; { @@ -684,13 +787,13 @@ void tst_QApplication::quitOnLastWindowClosed() QCoreApplication::exec(); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QVERIFY(quitApplicationTriggered); } { int argc = 0; QApplication app(argc, nullptr); - QSignalSpy appSpy(&app, &QApplication::lastWindowClosed); + QSignalSpy appSpy(&app, &QGuiApplication::lastWindowClosed); // exec a dialog for 1 second, then show the window QuitOnLastWindowClosedWindow window; @@ -706,8 +809,8 @@ void tst_QApplication::quitOnLastWindowClosed() QCOMPARE(returnValue, 0); // failure here means the timer above didn't fire, and the // quit was caused the dialog being closed (not the window) - QCOMPARE(timerSpy.count(), 1); - QCOMPARE(appSpy.count(), 2); + QCOMPARE(timerSpy.size(), 1); + QCOMPARE(appSpy.size(), 2); } { int argc = 0; @@ -756,7 +859,7 @@ void tst_QApplication::quitOnLastWindowClosed() QTimer::singleShot(100, &w1, &QWidget::close); QCoreApplication::exec(); - QVERIFY(timerSpy.count() < 10); + QVERIFY(timerSpy.size() < 10); } } @@ -798,11 +901,9 @@ public: void tst_QApplication::closeAllWindows() { -#ifdef Q_OS_WINRT - QSKIP("PromptOnCloseWidget does not work on WinRT - QTBUG-68297"); -#endif int argc = 0; QApplication app(argc, nullptr); + app.setAttribute(Qt::AA_DontUseNativeDialogs, true); // create some windows new QWidget; @@ -811,7 +912,7 @@ void tst_QApplication::closeAllWindows() // show all windows auto topLevels = QApplication::topLevelWidgets(); - for (QWidget *w : qAsConst(topLevels)) { + for (QWidget *w : std::as_const(topLevels)) { w->show(); QVERIFY(QTest::qWaitForWindowExposed(w)); } @@ -828,14 +929,14 @@ void tst_QApplication::closeAllWindows() PromptOnCloseWidget *promptOnCloseWidget = new PromptOnCloseWidget; // show all windows topLevels = QApplication::topLevelWidgets(); - for (QWidget *w : qAsConst(topLevels)) { + for (QWidget *w : std::as_const(topLevels)) { w->show(); QVERIFY(QTest::qWaitForWindowExposed(w)); } // close the last window to open the prompt (eventloop recurses) promptOnCloseWidget->close(); // all windows should not be visible, except the one that opened the prompt - for (QWidget *w : qAsConst(topLevels)) { + for (QWidget *w : std::as_const(topLevels)) { if (w == promptOnCloseWidget) QVERIFY(w->isVisible()); else @@ -847,8 +948,8 @@ void tst_QApplication::closeAllWindows() bool isPathListIncluded(const QStringList &l, const QStringList &r) { - int size = r.count(); - if (size > l.count()) + int size = r.size(); + if (size > l.size()) return false; #if defined (Q_OS_WIN) Qt::CaseSensitivity cs = Qt::CaseInsensitive; @@ -856,22 +957,22 @@ bool isPathListIncluded(const QStringList &l, const QStringList &r) Qt::CaseSensitivity cs = Qt::CaseSensitive; #endif int i = 0, j = 0; - for ( ; i < l.count() && j < r.count(); ++i) { + for ( ; i < l.size() && j < r.size(); ++i) { if (QDir::toNativeSeparators(l[i]).compare(QDir::toNativeSeparators(r[j]), cs) == 0) { ++j; i = -1; } } - return j == r.count(); + return j == r.size(); } #if QT_CONFIG(library) void tst_QApplication::libraryPaths() { #ifndef BUILTIN_TESTDATA - const QString testDir = QFileInfo(QFINDTESTDATA("test/test.pro")).absolutePath(); + const QString testDir = QFileInfo(QFINDTESTDATA("test/CMakeLists.txt")).absolutePath(); #else - const QString testDir = QFileInfo(QFINDTESTDATA("test.pro")).absolutePath(); + const QString testDir = QFileInfo(QFINDTESTDATA("CMakeLists.txt")).absolutePath(); #endif QVERIFY(!testDir.isEmpty()); { @@ -885,7 +986,9 @@ void tst_QApplication::libraryPaths() QStringList actual = QApplication::libraryPaths(); actual.sort(); - QStringList expected = QSet<QString>::fromList((QStringList() << testDir << appDirPath)).toList(); + QStringList expected; + expected << testDir << appDirPath; + expected = QSet<QString>(expected.constBegin(), expected.constEnd()).values(); expected.sort(); QVERIFY2(isPathListIncluded(actual, expected), @@ -897,17 +1000,16 @@ void tst_QApplication::libraryPaths() int argc = 1; QApplication app(argc, &argv0); QString appDirPath = QCoreApplication::applicationDirPath(); - QString installPathPlugins = QLibraryInfo::location(QLibraryInfo::PluginsPath); + QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath); QStringList actual = QApplication::libraryPaths(); actual.sort(); - QStringList expected = QSet<QString>::fromList((QStringList() << installPathPlugins << appDirPath)).toList(); + QStringList expected; + expected << installPathPlugins << appDirPath; + expected = QSet<QString>(expected.constBegin(), expected.constEnd()).values(); expected.sort(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "On WinRT PluginsPath is outside of sandbox. QTBUG-68297", Abort); -#endif QVERIFY2(isPathListIncluded(actual, expected), qPrintable("actual:\n - " + actual.join("\n - ") + "\nexpected:\n - " + expected.join("\n - "))); @@ -921,18 +1023,18 @@ void tst_QApplication::libraryPaths() { qCDebug(lcTests) << "Initial library path:" << QApplication::libraryPaths(); - int count = QApplication::libraryPaths().count(); + int count = QApplication::libraryPaths().size(); #if 0 // this test doesn't work if KDE 4 is installed QCOMPARE(count, 1); // before creating QApplication, only the PluginsPath is in the libraryPaths() #endif - QString installPathPlugins = QLibraryInfo::location(QLibraryInfo::PluginsPath); + QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath); QApplication::addLibraryPath(installPathPlugins); qCDebug(lcTests) << "installPathPlugins" << installPathPlugins; qCDebug(lcTests) << "After adding plugins path:" << QApplication::libraryPaths(); - QCOMPARE(QApplication::libraryPaths().count(), count); + QCOMPARE(QApplication::libraryPaths().size(), count); QApplication::addLibraryPath(testDir); - QCOMPARE(QApplication::libraryPaths().count(), count + 1); + QCOMPARE(QApplication::libraryPaths().size(), count + 1); // creating QApplication adds the applicationDirPath to the libraryPath int argc = 1; @@ -942,19 +1044,19 @@ void tst_QApplication::libraryPaths() // On Windows CE these are identical and might also be the case for other // systems too if (appDirPath != installPathPlugins) - QCOMPARE(QApplication::libraryPaths().count(), count + 2); + QCOMPARE(QApplication::libraryPaths().size(), count + 2); } { int argc = 1; QApplication app(argc, &argv0); qCDebug(lcTests) << "Initial library path:" << QCoreApplication::libraryPaths(); - int count = QCoreApplication::libraryPaths().count(); - QString installPathPlugins = QLibraryInfo::location(QLibraryInfo::PluginsPath); + int count = QCoreApplication::libraryPaths().size(); + QString installPathPlugins = QLibraryInfo::path(QLibraryInfo::PluginsPath); QCoreApplication::addLibraryPath(installPathPlugins); qCDebug(lcTests) << "installPathPlugins" << installPathPlugins; qCDebug(lcTests) << "After adding plugins path:" << QCoreApplication::libraryPaths(); - QCOMPARE(QCoreApplication::libraryPaths().count(), count); + QCOMPARE(QCoreApplication::libraryPaths().size(), count); QString appDirPath = QCoreApplication::applicationDirPath(); @@ -962,14 +1064,14 @@ void tst_QApplication::libraryPaths() QCoreApplication::addLibraryPath(appDirPath + "/.."); qCDebug(lcTests) << "appDirPath" << appDirPath; qCDebug(lcTests) << "After adding appDirPath && appDirPath + /..:" << QCoreApplication::libraryPaths(); - QCOMPARE(QCoreApplication::libraryPaths().count(), count + 1); + QCOMPARE(QCoreApplication::libraryPaths().size(), count + 1); #ifdef Q_OS_MACOS QCoreApplication::addLibraryPath(appDirPath + "/../MacOS"); #else QCoreApplication::addLibraryPath(appDirPath + "/tmp/.."); #endif qCDebug(lcTests) << "After adding appDirPath + /tmp/..:" << QCoreApplication::libraryPaths(); - QCOMPARE(QCoreApplication::libraryPaths().count(), count + 1); + QCOMPARE(QCoreApplication::libraryPaths().size(), count + 1); } } @@ -1011,13 +1113,10 @@ void tst_QApplication::libraryPaths_qt_plugin_path_2() // library path list should contain the default plus the one valid path QStringList expected = QStringList() - << QLibraryInfo::location(QLibraryInfo::PluginsPath) + << QLibraryInfo::path(QLibraryInfo::PluginsPath) << QDir(QCoreApplication::applicationDirPath()).canonicalPath() << QDir(QDir::fromNativeSeparators(QString::fromLatin1(validPath))).canonicalPath(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "On WinRT PluginsPath is outside of sandbox. QTBUG-68297", Abort); -#endif QVERIFY2(isPathListIncluded(QCoreApplication::libraryPaths(), expected), qPrintable("actual:\n - " + QCoreApplication::libraryPaths().join("\n - ") + "\nexpected:\n - " + expected.join("\n - "))); @@ -1035,15 +1134,16 @@ void tst_QApplication::libraryPaths_qt_plugin_path_2() // library path list should contain the default QStringList expected = QStringList() - << QLibraryInfo::location(QLibraryInfo::PluginsPath) + << QLibraryInfo::path(QLibraryInfo::PluginsPath) << QCoreApplication::applicationDirPath(); QVERIFY(isPathListIncluded(QCoreApplication::libraryPaths(), expected)); - qputenv("QT_PLUGIN_PATH", QByteArray()); + qputenv("QT_PLUGIN_PATH", nullptr); } } #endif +#ifdef QT_BUILD_INTERNAL class SendPostedEventsTester : public QObject { Q_OBJECT @@ -1065,14 +1165,14 @@ void SendPostedEventsTester::doTest() QPointer<SendPostedEventsTester> p = this; QApplication::postEvent(this, new QEvent(QEvent::User)); // DeferredDelete should not be delivered until returning from this function - QApplication::postEvent(this, new QDeferredDeleteEvent()); + deleteLater(); QEventLoop eventLoop; QMetaObject::invokeMethod(&eventLoop, "quit", Qt::QueuedConnection); eventLoop.exec(); QVERIFY(p != nullptr); - QCOMPARE(eventSpy.count(), 2); + QCOMPARE(eventSpy.size(), 2); QCOMPARE(eventSpy.at(0), int(QEvent::MetaCall)); QCOMPARE(eventSpy.at(1), int(QEvent::User)); eventSpy.clear(); @@ -1089,6 +1189,7 @@ void tst_QApplication::sendPostedEvents() (void) QCoreApplication::exec(); QVERIFY(p.isNull()); } +#endif void tst_QApplication::thread() { @@ -1216,6 +1317,11 @@ void DeleteLaterWidget::runTest() QCoreApplication::processEvents(); + // At this point, the event queue is empty. As we want a deferred + // deletion to occur before the timer event, we should provoke the + // event dispatcher for the next spin. + QCoreApplication::eventDispatcher()->interrupt(); + QVERIFY(!stillAlive); // verify at the end to make test terminate } @@ -1228,12 +1334,9 @@ void DeleteLaterWidget::checkDeleteLater() void tst_QApplication::testDeleteLater() { -#ifdef Q_OS_MAC - QSKIP("This test fails and then hangs on OS X, see QTBUG-24318"); -#endif int argc = 0; QApplication app(argc, nullptr); - connect(&app, &QApplication::lastWindowClosed, &app, &QCoreApplication::quit); + connect(&app, &QGuiApplication::lastWindowClosed, &app, &QCoreApplication::quit); DeleteLaterWidget *wgt = new DeleteLaterWidget(&app); QTimer::singleShot(500, wgt, &DeleteLaterWidget::runTest); @@ -1245,8 +1348,10 @@ void tst_QApplication::testDeleteLater() QObject *stillAlive = wgt->findChild<QObject*>("deleteLater"); QVERIFY(stillAlive); + wgt->show(); QCoreApplication::exec(); + QVERIFY(wgt->isHidden()); delete wgt; } @@ -1324,10 +1429,8 @@ public slots: } }; -void tst_QApplication::testDeleteLaterProcessEvents() +void tst_QApplication::testDeleteLaterProcessEvents1() { - int argc = 0; - // Calling processEvents() with no event dispatcher does nothing. QObject *object = new QObject; QPointer<QObject> p(object); @@ -1335,75 +1438,85 @@ void tst_QApplication::testDeleteLaterProcessEvents() QApplication::processEvents(); QVERIFY(p); delete object; +} - { - QApplication app(argc, nullptr); - // If you call processEvents() with an event dispatcher present, but - // outside any event loops, deferred deletes are not processed unless - // sendPostedEvents(0, DeferredDelete) is called. - object = new QObject; - p = object; - object->deleteLater(); - QCoreApplication::processEvents(); - QVERIFY(p); - QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); - QVERIFY(!p); - - // If you call deleteLater() on an object when there is no parent - // event loop, and then enter an event loop, the object will get - // deleted. - object = new QObject; - p = object; - object->deleteLater(); - QEventLoop loop; - QTimer::singleShot(1000, &loop, &QEventLoop::quit); - loop.exec(); - QVERIFY(!p); - } - { - // When an object is in an event loop, then calls deleteLater() and enters - // an event loop recursively, it should not die until the parent event - // loop continues. - QApplication app(argc, nullptr); - QEventLoop loop; - EventLoopNester *nester = new EventLoopNester; - p = nester; - QTimer::singleShot(3000, &loop, &QEventLoop::quit); - QTimer::singleShot(0, nester, &EventLoopNester::deleteLaterAndEnterLoop); - - loop.exec(); - QVERIFY(!p); - } - - { - // When the event loop that calls deleteLater() is exited - // immediately, the object should die when returning to the - // parent event loop - QApplication app(argc, nullptr); - QEventLoop loop; - EventLoopNester *nester = new EventLoopNester; - p = nester; - QTimer::singleShot(3000, &loop, &QEventLoop::quit); - QTimer::singleShot(0, nester, &EventLoopNester::deleteLaterAndExitLoop); +void tst_QApplication::testDeleteLaterProcessEvents2() +{ + int argc = 0; + QApplication app(argc, nullptr); + // If you call processEvents() with an event dispatcher present, but + // outside any event loops, deferred deletes are not processed unless + // sendPostedEvents(0, DeferredDelete) is called. + auto object = new QObject; + QPointer<QObject> p(object); + object->deleteLater(); + QCoreApplication::processEvents(); + QVERIFY(p); + QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete); + QVERIFY(!p); + + // If you call deleteLater() on an object when there is no parent + // event loop, and then enter an event loop, the object will get + // deleted. + QEventLoop loop; + object = new QObject; + connect(object, &QObject::destroyed, &loop, &QEventLoop::quit); + p = object; + object->deleteLater(); + QTimer::singleShot(1000, &loop, &QEventLoop::quit); + loop.exec(); + QVERIFY(!p); +} - loop.exec(); - QVERIFY(!p); - } +void tst_QApplication::testDeleteLaterProcessEvents3() +{ + int argc = 0; + // When an object is in an event loop, then calls deleteLater() and enters + // an event loop recursively, it should not die until the parent event + // loop continues. + QApplication app(argc, nullptr); + QEventLoop loop; + EventLoopNester *nester = new EventLoopNester; + QPointer<QObject> p(nester); + QTimer::singleShot(3000, &loop, &QEventLoop::quit); + QTimer::singleShot(0, nester, &EventLoopNester::deleteLaterAndEnterLoop); + + loop.exec(); + QVERIFY(!p); +} - { - // when the event loop that calls deleteLater() also calls - // processEvents() immediately afterwards, the object should - // not die until the parent loop continues - QApplication app(argc, nullptr); - QEventLoop loop; - EventLoopNester *nester = new EventLoopNester(); - p = nester; - QTimer::singleShot(3000, &loop, &QEventLoop::quit); - QTimer::singleShot(0, nester, &EventLoopNester::deleteLaterAndProcessEvents); +void tst_QApplication::testDeleteLaterProcessEvents4() +{ + int argc = 0; + // When the event loop that calls deleteLater() is exited + // immediately, the object should die when returning to the + // parent event loop + QApplication app(argc, nullptr); + QEventLoop loop; + EventLoopNester *nester = new EventLoopNester; + QPointer<QObject> p(nester); + QTimer::singleShot(3000, &loop, &QEventLoop::quit); + QTimer::singleShot(0, nester, &EventLoopNester::deleteLaterAndExitLoop); + + loop.exec(); + QVERIFY(!p); +} - loop.exec(); - QVERIFY(!p); - } +void tst_QApplication::testDeleteLaterProcessEvents5() +{ + // when the event loop that calls deleteLater() also calls + // processEvents() immediately afterwards, the object should + // not die until the parent loop continues + int argc = 0; + QApplication app(argc, nullptr); + QEventLoop loop; + EventLoopNester *nester = new EventLoopNester(); + QPointer<QObject> p(nester); + QTimer::singleShot(3000, &loop, &QEventLoop::quit); + QTimer::singleShot(0, nester, &EventLoopNester::deleteLaterAndProcessEvents); + + loop.exec(); + QVERIFY(!p); } /* @@ -1413,7 +1526,12 @@ void tst_QApplication::desktopSettingsAware() { #if QT_CONFIG(process) QProcess testProcess; - testProcess.start("desktopsettingsaware_helper"); +#ifdef Q_OS_MACOS + QStringList environment = QProcess::systemEnvironment(); + environment += QLatin1String("QT_MAC_DISABLE_FOREGROUND_APPLICATION_TRANSFORM=1"); + testProcess.setEnvironment(environment); +#endif + testProcess.start("./desktopsettingsaware_helper"); QVERIFY2(testProcess.waitForStarted(), qPrintable(QString::fromLatin1("Cannot start 'desktopsettingsaware_helper': %1").arg(testProcess.errorString()))); QVERIFY(testProcess.waitForFinished(10000)); @@ -1441,11 +1559,97 @@ void tst_QApplication::setActiveWindow() delete pb2; w->show(); - QApplication::setActiveWindow(w); // needs this on twm (focus follows mouse) + QApplicationPrivate::setActiveWindow(w); // needs this on twm (focus follows mouse) QVERIFY(pb1->hasFocus()); delete w; } +void tst_QApplication::activateDeactivateEvent() +{ + // Ensure that QWindows (other than QWidgetWindow) + // are activated / deactivated. + class Window : public QWindow + { + public: + using QWindow::QWindow; + + int activateCount = 0; + int deactivateCount = 0; + protected: + bool event(QEvent *e) override + { + switch (e->type()) { + case QEvent::WindowActivate: + ++activateCount; + break; + case QEvent::WindowDeactivate: + ++deactivateCount; + break; + default: + break; + } + return QWindow::event(e); + } + }; + + int argc = 0; + QApplication app(argc, nullptr); + + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("QWindow::requestActivate() is not supported."); + + Window w1; + Window w2; + + w1.show(); + w1.requestActivate(); + QVERIFY(QTest::qWaitForWindowActive(&w1)); + QCOMPARE(w1.activateCount, 1); + QCOMPARE(w1.deactivateCount, 0); + + w2.show(); + w2.requestActivate(); + QVERIFY(QTest::qWaitForWindowActive(&w2)); + QCOMPARE(w1.deactivateCount, 1); + QCOMPARE(w2.activateCount, 1); +} + +void tst_QApplication::focusWidget() +{ + int argc = 0; + QApplication app(argc, nullptr); + + // The focus widget is the active window itself + { + QTextEdit te; + te.show(); + + QVERIFY(QTest::qWaitForWindowActive(&te)); + + const auto focusWidget = QApplication::focusWidget(); + QVERIFY(focusWidget); + QVERIFY(focusWidget->hasFocus()); + QVERIFY(te.hasFocus()); + QCOMPARE(focusWidget, te.focusWidget()); + } + + // The focus widget is a child of the active window + { + QWidget w; + QTextEdit te(&w); + w.show(); + + QVERIFY(QTest::qWaitForWindowActive(&w)); + + const auto focusWidget = QApplication::focusWidget(); + QVERIFY(focusWidget); + QVERIFY(focusWidget->hasFocus()); + QVERIFY(!w.hasFocus()); + QVERIFY(te.hasFocus()); + QCOMPARE(te.focusWidget(), w.focusWidget()); + QCOMPARE(focusWidget, w.focusWidget()); + } +} /* This might fail on some X11 window managers? */ void tst_QApplication::focusChanged() @@ -1467,22 +1671,22 @@ void tst_QApplication::focusChanged() hbox1.addWidget(&le1); hbox1.addWidget(&pb1); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); parent1.show(); - QApplication::setActiveWindow(&parent1); // needs this on twm (focus follows mouse) - QCOMPARE(spy.count(), 1); - QCOMPARE(spy.at(0).count(), 2); + QApplicationPrivate::setActiveWindow(&parent1); // needs this on twm (focus follows mouse) + QCOMPARE(spy.size(), 1); + QCOMPARE(spy.at(0).size(), 2); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QCOMPARE(now, &le1); QCOMPARE(now, QApplication::focusWidget()); QVERIFY(!old); spy.clear(); - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); pb1.setFocus(); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QCOMPARE(now, &pb1); @@ -1491,7 +1695,7 @@ void tst_QApplication::focusChanged() spy.clear(); lb1.setFocus(); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QCOMPARE(now, &lb1); @@ -1500,7 +1704,7 @@ void tst_QApplication::focusChanged() spy.clear(); lb1.clearFocus(); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QVERIFY(!now); @@ -1519,10 +1723,10 @@ void tst_QApplication::focusChanged() hbox2.addWidget(&pb2); parent2.show(); - QApplication::setActiveWindow(&parent2); // needs this on twm (focus follows mouse) - QVERIFY(spy.count() > 0); // one for deactivation, one for activation on Windows - old = qvariant_cast<QWidget*>(spy.at(spy.count()-1).at(0)); - now = qvariant_cast<QWidget*>(spy.at(spy.count()-1).at(1)); + QApplicationPrivate::setActiveWindow(&parent2); // needs this on twm (focus follows mouse) + QVERIFY(spy.size() > 0); // one for deactivation, one for activation on Windows + old = qvariant_cast<QWidget*>(spy.at(spy.size()-1).at(0)); + now = qvariant_cast<QWidget*>(spy.at(spy.size()-1).at(1)); QCOMPARE(now, &le2); QCOMPARE(now, QApplication::focusWidget()); QVERIFY(!old); @@ -1547,10 +1751,10 @@ void tst_QApplication::focusChanged() tab.simulate(now); if (!tabAllControls) { - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); QCOMPARE(now, QApplication::focusWidget()); } else { - QVERIFY(spy.count() > 0); + QVERIFY(spy.size() > 0); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QCOMPARE(now, &pb2); @@ -1560,11 +1764,11 @@ void tst_QApplication::focusChanged() } if (!tabAllControls) { - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); QCOMPARE(now, QApplication::focusWidget()); } else { tab.simulate(now); - QVERIFY(spy.count() > 0); + QVERIFY(spy.size() > 0); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QCOMPARE(now, &le2); @@ -1574,11 +1778,11 @@ void tst_QApplication::focusChanged() } if (!tabAllControls) { - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); QCOMPARE(now, QApplication::focusWidget()); } else { backtab.simulate(now); - QVERIFY(spy.count() > 0); + QVERIFY(spy.size() > 0); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QCOMPARE(now, &pb2); @@ -1589,12 +1793,12 @@ void tst_QApplication::focusChanged() if (!tabAllControls) { - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); QCOMPARE(now, QApplication::focusWidget()); old = &pb2; } else { backtab.simulate(now); - QVERIFY(spy.count() > 0); + QVERIFY(spy.size() > 0); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QCOMPARE(now, &le2); @@ -1605,10 +1809,10 @@ void tst_QApplication::focusChanged() click.simulate(old); if (!(pb2.focusPolicy() & Qt::ClickFocus)) { - QCOMPARE(spy.count(), 0); + QCOMPARE(spy.size(), 0); QCOMPARE(now, QApplication::focusWidget()); } else { - QVERIFY(spy.count() > 0); + QVERIFY(spy.size() > 0); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QCOMPARE(now, &pb2); @@ -1617,7 +1821,7 @@ void tst_QApplication::focusChanged() spy.clear(); click.simulate(old); - QVERIFY(spy.count() > 0); + QVERIFY(spy.size() > 0); old = qvariant_cast<QWidget*>(spy.at(0).at(0)); now = qvariant_cast<QWidget*>(spy.at(0).at(1)); QCOMPARE(now, &le2); @@ -1627,16 +1831,16 @@ void tst_QApplication::focusChanged() } parent1.activateWindow(); - QApplication::setActiveWindow(&parent1); // needs this on twm (focus follows mouse) - QVERIFY(spy.count() == 1 || spy.count() == 2); // one for deactivation, one for activation on Windows + QApplicationPrivate::setActiveWindow(&parent1); // needs this on twm (focus follows mouse) + QVERIFY(spy.size() == 1 || spy.size() == 2); // one for deactivation, one for activation on Windows //on windows, the change of focus is made in 2 steps //(the focusChanged SIGNAL is emitted twice) - if (spy.count()==1) - old = qvariant_cast<QWidget*>(spy.at(spy.count()-1).at(0)); + if (spy.size()==1) + old = qvariant_cast<QWidget*>(spy.at(spy.size()-1).at(0)); else - old = qvariant_cast<QWidget*>(spy.at(spy.count()-2).at(0)); - now = qvariant_cast<QWidget*>(spy.at(spy.count()-1).at(1)); + old = qvariant_cast<QWidget*>(spy.at(spy.size()-2).at(0)); + now = qvariant_cast<QWidget*>(spy.at(spy.size()-1).at(1)); QCOMPARE(now, &le1); QCOMPARE(now, QApplication::focusWidget()); QCOMPARE(old, &le2); @@ -1695,6 +1899,9 @@ void tst_QApplication::focusMouseClick() int argc = 1; QApplication app(argc, &argv0); + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("Window activation is not supported"); + QWidget w; w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); w.setFocusPolicy(Qt::StrongFocus); @@ -1707,7 +1914,7 @@ void tst_QApplication::focusMouseClick() // front most widget has Qt::TabFocus, parent widget accepts clicks as well // now send a mouse button press event and check what happens with the focus // it should be given to the parent widget - QMouseEvent ev(QEvent::MouseButtonPress, QPointF(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); + QMouseEvent ev(QEvent::MouseButtonPress, QPointF(), w.mapToGlobal(QPointF()), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QSpontaneKeyEvent::setSpontaneous(&ev); QVERIFY(ev.spontaneous()); qApp->notify(&w2, &ev); @@ -1718,9 +1925,6 @@ void tst_QApplication::focusMouseClick() QSpontaneKeyEvent::setSpontaneous(&ev); QVERIFY(ev.spontaneous()); qApp->notify(&w2, &ev); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "Fails on WinRT - QTBUG-68297", Abort); -#endif QTRY_COMPARE(QApplication::focusWidget(), &w2); // now back to tab focus and click again (it already had focus) -> focus should stay @@ -1777,6 +1981,194 @@ void tst_QApplication::style() QVERIFY(QApplication::style() != nullptr); } +class CustomStyle : public QProxyStyle +{ +public: + CustomStyle() : QProxyStyle("Windows") { Q_ASSERT(!polished); } + ~CustomStyle() { polished = 0; } + void polish(QPalette &palette) override + { + polished++; + palette.setColor(QPalette::Active, QPalette::Link, Qt::red); + } + static int polished; +}; + +int CustomStyle::polished = 0; + +class CustomStylePlugin : public QStylePlugin +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QStyleFactoryInterface" FILE "customstyle.json") +public: + QStyle *create(const QString &) override { return new CustomStyle; } +}; + +Q_IMPORT_PLUGIN(CustomStylePlugin) + +void tst_QApplication::applicationPalettePolish() +{ + int argc = 1; + +#if defined(QT_BUILD_INTERNAL) + { + qputenv("QT_DESKTOP_STYLE_KEY", "customstyle"); + QApplication app(argc, &argv0); + QVERIFY(CustomStyle::polished); + QVERIFY(!app.palette().resolveMask()); + QCOMPARE(app.palette().color(QPalette::Link), Qt::red); + qunsetenv("QT_DESKTOP_STYLE_KEY"); + } +#endif + + { + QApplication::setStyle(new CustomStyle); + QApplication app(argc, &argv0); + QVERIFY(CustomStyle::polished); + QVERIFY(!app.palette().resolveMask()); + QCOMPARE(app.palette().color(QPalette::Link), Qt::red); + } + + { + QApplication app(argc, &argv0); + app.setStyle(new CustomStyle); + QVERIFY(CustomStyle::polished); + QVERIFY(!app.palette().resolveMask()); + QCOMPARE(app.palette().color(QPalette::Link), Qt::red); + + CustomStyle::polished = 0; + app.setPalette(QPalette()); + QVERIFY(CustomStyle::polished); + QVERIFY(!app.palette().resolveMask()); + QCOMPARE(app.palette().color(QPalette::Link), Qt::red); + + CustomStyle::polished = 0; + QPalette palette; + palette.setColor(QPalette::Active, QPalette::Highlight, Qt::green); + app.setPalette(palette); + QVERIFY(CustomStyle::polished); + QVERIFY(app.palette().resolveMask()); + QCOMPARE(app.palette().color(QPalette::Link), Qt::red); + QCOMPARE(app.palette().color(QPalette::Highlight), Qt::green); + } +} + +void tst_QApplication::setColorScheme() +{ + int argc = 1; + QApplication app(argc, &argv0); + + if (QStringList{"minimal", "offscreen", "wayland", "xcb", "wasm", "webassembly"} + .contains(QGuiApplication::platformName(), Qt::CaseInsensitive)) { + QSKIP("Setting the colorScheme is not implemented on this platform."); + } + qDebug() << "Testing setColorScheme on platform" << QGuiApplication::platformName(); + + if (QByteArrayView(app.style()->metaObject()->className()) == "QWindowsVistaStyle") + QSKIP("Setting the colorScheme is not supported with the Windows Vista style."); + + const Qt::ColorScheme defaultColorScheme = QApplication::styleHints()->colorScheme(); + // if we implement setColorScheme, then we must be able to read it + QVERIFY(defaultColorScheme != Qt::ColorScheme::Unknown); + const Qt::ColorScheme newColorScheme = defaultColorScheme == Qt::ColorScheme::Light + ? Qt::ColorScheme::Dark : Qt::ColorScheme::Light; + + class TopLevelWidget : public QWidget + { + QList<QEvent::Type> events; + public: + TopLevelWidget() + { + setObjectName("colorScheme TopLevelWidget"); + } + + void clearEvents() + { + events.clear(); + } + qsizetype eventCount(QEvent::Type type) const + { + return events.count(type); + } + protected: + bool event(QEvent *event) override + { + switch (event->type()) { + case QEvent::ApplicationPaletteChange: + case QEvent::PaletteChange: + case QEvent::ThemeChange: + events << event->type(); + break; + default: + break; + } + + return QWidget::event(event); + } + } topLevelWidget; + topLevelWidget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevelWidget)); + + QSignalSpy colorSchemeChangedSpy(app.styleHints(), &QStyleHints::colorSchemeChanged); + + // always start with a clean list + topLevelWidget.clearEvents(); + const QPalette defaultPalette = topLevelWidget.palette(); + + bool oldPaletteWhenSchemeChanged = false; + connect(app.styleHints(), &QStyleHints::colorSchemeChanged, this, + [defaultPalette, &topLevelWidget, &oldPaletteWhenSchemeChanged]{ + oldPaletteWhenSchemeChanged = defaultPalette == topLevelWidget.palette(); + }); + + app.styleHints()->setColorScheme(newColorScheme); + QTRY_COMPARE(colorSchemeChangedSpy.count(), 1); + // We have not yet updated the palette when we emit the colorSchemeChanged + // signal, so the toplevel widget should still use the previous palette + QVERIFY(oldPaletteWhenSchemeChanged); + QCOMPARE(topLevelWidget.eventCount(QEvent::ThemeChange), 1); + // We can't guarantee that there is only one ApplicationPaletteChange, + // and they might arrive asynchronously in response to ThemeChange + QTRY_COMPARE_GE(topLevelWidget.eventCount(QEvent::ApplicationPaletteChange), 1); + // But we can guarantee a single PaletteChange event for the widget + QCOMPARE(topLevelWidget.eventCount(QEvent::PaletteChange), 1); + // The palette should have changed + QCOMPARE_NE(topLevelWidget.palette(), defaultPalette); + + topLevelWidget.clearEvents(); + colorSchemeChangedSpy.clear(); + + // verify that a widget shown with a color scheme override in place respect that + QWidget newWidget; + newWidget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&newWidget)); + QCOMPARE(newWidget.palette(), topLevelWidget.palette()); + + // Setting to Unknown should follow the system preference again + app.styleHints()->setColorScheme(Qt::ColorScheme::Unknown); + QTRY_COMPARE(colorSchemeChangedSpy.count(), 1); + QCOMPARE(app.styleHints()->colorScheme(), defaultColorScheme); + QTRY_COMPARE(topLevelWidget.eventCount(QEvent::PaletteChange), 1); + + auto debugPalette = qScopeGuard([defaultPalette, &topLevelWidget]{ + qDebug() << "Inspecting palettes for differences"; + const QPalette palette = topLevelWidget.palette(); + for (int g = 0; g < QPalette::NColorGroups; ++g) { + for (int r = 0; r < QPalette::NColorRoles; ++r) { + const auto group = static_cast<QPalette::ColorGroup>(g); + const auto role = static_cast<QPalette::ColorRole>(r); + qDebug() << "...Checking" << group << role; + const auto actualBrush = palette.brush(group, role); + const auto expectedBrush = defaultPalette.brush(group, role); + if (palette.brush(group, role) != defaultPalette.brush(group, role)) + qWarning() << "...Difference in" << group << role << actualBrush << expectedBrush; + } + } + }); + QCOMPARE(topLevelWidget.palette(), defaultPalette); + debugPalette.dismiss(); +} + void tst_QApplication::allWidgets() { int argc = 1; @@ -1799,11 +2191,11 @@ void tst_QApplication::topLevelWidgets() #endif QCoreApplication::processEvents(); QVERIFY(QApplication::topLevelWidgets().contains(w)); - QCOMPARE(QApplication::topLevelWidgets().count(), 1); + QCOMPARE(QApplication::topLevelWidgets().size(), 1); delete w; w = nullptr; QCoreApplication::processEvents(); - QCOMPARE(QApplication::topLevelWidgets().count(), 0); + QCOMPARE(QApplication::topLevelWidgets().size(), 0); } @@ -1812,25 +2204,24 @@ void tst_QApplication::setAttribute() { int argc = 1; QApplication app(argc, &argv0); - QVERIFY(!QApplication::testAttribute(Qt::AA_ImmediateWidgetCreation)); + QVERIFY(!QApplication::testAttribute(Qt::AA_NativeWindows)); QWidget *w = new QWidget; - QVERIFY(!w->testAttribute(Qt::WA_WState_Created)); + w->show(); // trigger creation; + QVERIFY(!w->testAttribute(Qt::WA_NativeWindow)); delete w; - QApplication::setAttribute(Qt::AA_ImmediateWidgetCreation); - QVERIFY(QApplication::testAttribute(Qt::AA_ImmediateWidgetCreation)); + QApplication::setAttribute(Qt::AA_NativeWindows); + QVERIFY(QApplication::testAttribute(Qt::AA_NativeWindows)); w = new QWidget; - QVERIFY(w->testAttribute(Qt::WA_WState_Created)); - QWidget *w2 = new QWidget(w); - w2->setParent(nullptr); - QVERIFY(w2->testAttribute(Qt::WA_WState_Created)); + w->show(); // trigger creation + QVERIFY(w->testAttribute(Qt::WA_NativeWindow)); delete w; - delete w2; - QApplication::setAttribute(Qt::AA_ImmediateWidgetCreation, false); - QVERIFY(!QApplication::testAttribute(Qt::AA_ImmediateWidgetCreation)); + QApplication::setAttribute(Qt::AA_NativeWindows, false); + QVERIFY(!QApplication::testAttribute(Qt::AA_NativeWindows)); w = new QWidget; - QVERIFY(!w->testAttribute(Qt::WA_WState_Created)); + w->show(); // trigger creation; + QVERIFY(!w->testAttribute(Qt::WA_NativeWindow)); delete w; } @@ -1881,17 +2272,8 @@ void tst_QApplication::touchEventPropagation() int argc = 1; QApplication app(argc, &argv0); - QList<QTouchEvent::TouchPoint> pressedTouchPoints; - QTouchEvent::TouchPoint press(0); - press.setState(Qt::TouchPointPressed); - pressedTouchPoints << press; - - QList<QTouchEvent::TouchPoint> releasedTouchPoints; - QTouchEvent::TouchPoint release(0); - release.setState(Qt::TouchPointReleased); - releasedTouchPoints << release; - QTouchDevice *device = QTest::createTouchDevice(); + QPointingDevice *device = QTest::createTouchDevice(); { // touch event behavior on a window @@ -1908,8 +2290,10 @@ void tst_QApplication::touchEventPropagation() // we must ensure there is a screen position in the TouchPoint that maps to a local 0, 0. const QPoint deviceGlobalPos = QHighDpi::toNativePixels(window.mapToGlobal(QPoint(0, 0)), window.windowHandle()->screen()); - pressedTouchPoints[0].setScreenPos(deviceGlobalPos); - releasedTouchPoints[0].setScreenPos(deviceGlobalPos); + auto pressedTouchPoints = QList<QEventPoint>() << + QEventPoint(0, QEventPoint::State::Pressed, QPointF(), deviceGlobalPos); + auto releasedTouchPoints = QList<QEventPoint>() << + QEventPoint(0, QEventPoint::State::Released, QPointF(), deviceGlobalPos); QWindowSystemInterface::handleTouchEvent(handle, 0, @@ -1966,8 +2350,10 @@ void tst_QApplication::touchEventPropagation() QVERIFY(QTest::qWaitForWindowExposed(&window)); const QPoint deviceGlobalPos = QHighDpi::toNativePixels(window.mapToGlobal(QPoint(50, 150)), window.windowHandle()->screen()); - pressedTouchPoints[0].setScreenPos(deviceGlobalPos); - releasedTouchPoints[0].setScreenPos(deviceGlobalPos); + auto pressedTouchPoints = QList<QEventPoint>() << + QEventPoint(0, QEventPoint::State::Pressed, QPointF(), deviceGlobalPos); + auto releasedTouchPoints = QList<QEventPoint>() << + QEventPoint(0, QEventPoint::State::Released, QPointF(), deviceGlobalPos); QWindowSystemInterface::handleTouchEvent(handle, 0, @@ -2088,12 +2474,168 @@ void tst_QApplication::touchEventPropagation() } } +/*! + Test that wheel events are propagated correctly. + + The event propagation of wheel events is complex: generally, they are propagated + up the parent tree like other input events, until a widget accepts the event. However, + wheel events are ignored by default (unlike mouse events, which are accepted by default, + and ignored in the default implementation of the event handler of QWidget). + + And Qt tries to make sure that wheel events that "belong together" are going to the same + widget. However, for low-precision events as generated by an old-fashioned + mouse wheel, each event is a distinct event, so Qt has no choice than to deliver the event + to the widget under the mouse. + High-precision events, as generated by track pads or other kinetic scrolling devices, come + in a continuous stream, with different phases. Qt tries to make sure that all events in the + same stream go to the widget that accepted the first event. + + Also, QAbstractScrollArea forwards wheel events from the viewport to the relevant scrollbar, + which adds more complexity to the handling. + + This tests two scenarios: + 1) a large widget inside a scrollarea that scrolls, inside a scrollarea that also scrolls + 2) a large widget inside a scrollarea that doesn't scroll, within a scrollarea that does + + For scenario 1 "inner", the expectation is that the inner scrollarea handles all wheel + events. + For scenario 2 "outer", the expectation is that the outer scrollarea handles all wheel + events. +*/ +struct WheelEvent +{ + WheelEvent(Qt::ScrollPhase p = Qt::NoScrollPhase, Qt::Orientation o = Qt::Vertical) + : phase(p), orientation(o) + {} + Qt::ScrollPhase phase = Qt::NoScrollPhase; + Qt::Orientation orientation = Qt::Vertical; +}; +using WheelEventList = QList<WheelEvent>; + +void tst_QApplication::wheelEventPropagation_data() +{ + qRegisterMetaType<WheelEventList>(); + + QTest::addColumn<bool>("innerScrolls"); + QTest::addColumn<WheelEventList>("events"); + + QTest::addRow("inner, classic") + << true + << WheelEventList{{}, {}, {}}; + QTest::addRow("outer, classic") + << false + << WheelEventList{{}, {}, {}}; + QTest::addRow("inner, kinetic") + << true + << WheelEventList{Qt::ScrollBegin, Qt::ScrollUpdate, Qt::ScrollMomentum, Qt::ScrollEnd}; + QTest::addRow("outer, kinetic") + << false + << WheelEventList{Qt::ScrollBegin, Qt::ScrollUpdate, Qt::ScrollMomentum, Qt::ScrollEnd}; + QTest::addRow("inner, partial kinetic") + << true + << WheelEventList{Qt::ScrollUpdate, Qt::ScrollMomentum, Qt::ScrollEnd}; + QTest::addRow("outer, partial kinetic") + << false + << WheelEventList{Qt::ScrollUpdate, Qt::ScrollMomentum, Qt::ScrollEnd}; + QTest::addRow("inner, changing direction") + << true + << WheelEventList{Qt::ScrollUpdate, {Qt::ScrollUpdate, Qt::Horizontal}, Qt::ScrollMomentum, Qt::ScrollEnd}; + QTest::addRow("outer, changing direction") + << false + << WheelEventList{Qt::ScrollUpdate, {Qt::ScrollUpdate, Qt::Horizontal}, Qt::ScrollMomentum, Qt::ScrollEnd}; +} + +void tst_QApplication::wheelEventPropagation() +{ + QFETCH(bool, innerScrolls); + QFETCH(WheelEventList, events); + + const QSize baseSize(500, 500); + const QPointF center(baseSize.width() / 2, baseSize.height() / 2); + int scrollStep = 50; + + int argc = 1; + QApplication app(argc, &argv0); + + QScrollArea outerArea; + outerArea.setObjectName("outerArea"); + outerArea.viewport()->setObjectName("outerArea_viewport"); + QScrollArea innerArea; + innerArea.setObjectName("innerArea"); + innerArea.viewport()->setObjectName("innerArea_viewport"); + QWidget largeWidget; + largeWidget.setObjectName("largeWidget"); + QScrollBar trap(Qt::Vertical, &largeWidget); + trap.setObjectName("It's a trap!"); + + largeWidget.setFixedSize(baseSize * 8); + + // classic wheel events will be grabbed by the widget under the mouse, so don't place a trap + if (events.at(0).phase == Qt::NoScrollPhase) + trap.hide(); + // kinetic wheel events should all go to the first widget; place a trap + else + trap.setGeometry(center.x() - 50, center.y() + scrollStep, 100, baseSize.height()); + + // if the inner area is large enough to host the widget, then it won't scroll + innerArea.setWidget(&largeWidget); + innerArea.setFixedSize(innerScrolls ? baseSize * 4 + : largeWidget.minimumSize() + QSize(100, 100)); + // the outer area always scrolls + outerArea.setFixedSize(baseSize); + outerArea.setWidget(&innerArea); + outerArea.show(); + + if (!QTest::qWaitForWindowExposed(&outerArea)) + QSKIP("Window failed to show, can't run test"); + + auto innerVBar = innerArea.verticalScrollBar(); + innerVBar->setObjectName("innerArea_vbar"); + QCOMPARE(innerVBar->isVisible(), innerScrolls); + auto innerHBar = innerArea.horizontalScrollBar(); + innerHBar->setObjectName("innerArea_hbar"); + QCOMPARE(innerHBar->isVisible(), innerScrolls); + auto outerVBar = outerArea.verticalScrollBar(); + outerVBar->setObjectName("outerArea_vbar"); + QVERIFY(outerVBar->isVisible()); + auto outerHBar = outerArea.horizontalScrollBar(); + outerHBar->setObjectName("outerArea_hbar"); + QVERIFY(outerHBar->isVisible()); + + const QPointF global(outerArea.mapToGlobal(center.toPoint())); + + QSignalSpy innerVSpy(innerVBar, &QAbstractSlider::valueChanged); + QSignalSpy innerHSpy(innerHBar, &QAbstractSlider::valueChanged); + QSignalSpy outerVSpy(outerVBar, &QAbstractSlider::valueChanged); + QSignalSpy outerHSpy(outerHBar, &QAbstractSlider::valueChanged); + + int vcount = 0; + int hcount = 0; + + for (const auto &event : std::as_const(events)) { + const QPoint pixelDelta = event.orientation == Qt::Vertical ? QPoint(0, -scrollStep) : QPoint(-scrollStep, 0); + const QPoint angleDelta = event.orientation == Qt::Vertical ? QPoint(0, -120) : QPoint(-120, 0); + QWindowSystemInterface::handleWheelEvent(outerArea.windowHandle(), center, global, + pixelDelta, angleDelta, Qt::NoModifier, + event.phase); + if (event.orientation == Qt::Vertical) + ++vcount; + else + ++hcount; + QCoreApplication::processEvents(); + QCOMPARE(innerVSpy.size(), innerScrolls ? vcount : 0); + QCOMPARE(innerHSpy.size(), innerScrolls ? hcount : 0); + QCOMPARE(outerVSpy.size(), innerScrolls ? 0 : vcount); + QCOMPARE(outerHSpy.size(), innerScrolls ? 0 : hcount); + } +} + void tst_QApplication::qtbug_12673() { #if QT_CONFIG(process) QProcess testProcess; QStringList arguments; - testProcess.start("modal_helper", arguments); + testProcess.start("./modal_helper", arguments); QVERIFY2(testProcess.waitForStarted(), qPrintable(QString::fromLatin1("Cannot start 'modal_helper': %1").arg(testProcess.errorString()))); QVERIFY(testProcess.waitForFinished(20000)); @@ -2103,6 +2645,20 @@ void tst_QApplication::qtbug_12673() #endif } +void tst_QApplication::qtbug_103611() +{ + { + int argc = 0; + QApplication app(argc, nullptr); + auto ll = QLocale().uiLanguages(); + } + { + int argc = 0; + QApplication app(argc, nullptr); + auto ll = QLocale().uiLanguages(); + } +} + class NoQuitOnHideWidget : public QWidget { Q_OBJECT @@ -2132,8 +2688,26 @@ public: explicit ShowCloseShowWidget(bool showAgain, QWidget *parent = nullptr) : QWidget(parent), showAgain(showAgain) { + int timeout = 500; +#ifdef Q_OS_ANDROID + // On Android, CI Android emulator is not running HW accelerated graphics and can be slow, + // use a longer timeout to avoid flaky failures + timeout = 1000; +#endif + QTimer::singleShot(timeout, this, [] () { QCoreApplication::exit(1); }); + } + + bool shown = false; + +protected: + void showEvent(QShowEvent *) override + { QTimer::singleShot(0, this, &ShowCloseShowWidget::doClose); - QTimer::singleShot(500, this, [] () { QCoreApplication::exit(1); }); + shown = true; + } + void hideEvent(QHideEvent *) override + { + shown = false; } private slots: @@ -2149,16 +2723,21 @@ private: void tst_QApplication::abortQuitOnShow() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This crash, see QTBUG-123172."); + int argc = 0; QApplication app(argc, nullptr); ShowCloseShowWidget window1(false); window1.setWindowTitle(QLatin1String(QTest::currentTestFunction())); window1.show(); + QVERIFY(QTest::qWaitFor([&window1](){ return window1.shown; })); QCOMPARE(QCoreApplication::exec(), 0); ShowCloseShowWidget window2(true); window2.setWindowTitle(QLatin1String(QTest::currentTestFunction())); window2.show(); + QVERIFY(QTest::qWaitFor([&window2](){ return window2.shown; })); QCOMPARE(QCoreApplication::exec(), 1); } @@ -2166,20 +2745,17 @@ void tst_QApplication::abortQuitOnShow() void tst_QApplication::staticFunctions() { QApplication::setStyle(QStringLiteral("blub")); - QApplication::colorSpec(); - QApplication::setColorSpec(42); QApplication::allWidgets(); QApplication::topLevelWidgets(); - QApplication::desktop(); QApplication::activePopupWidget(); + QTest::ignoreMessage(QtWarningMsg, "Must construct a QGuiApplication first."); QApplication::activeModalWidget(); QApplication::focusWidget(); QApplication::activeWindow(); - QApplication::setActiveWindow(nullptr); + QApplicationPrivate::setActiveWindow(nullptr); QApplication::widgetAt(QPoint(0, 0)); QApplication::topLevelAt(QPoint(0, 0)); - QApplication::setGlobalStrut(QSize(0, 0)); - QApplication::globalStrut(); + QTest::ignoreMessage(QtWarningMsg, "Must construct a QApplication first."); QApplication::isEffectEnabled(Qt::UI_General); QApplication::setEffectEnabled(Qt::UI_General, false); } @@ -2232,7 +2808,6 @@ Q_GLOBAL_STATIC(QWidget, tst_qapp_widget); Q_GLOBAL_STATIC(QPixmap, tst_qapp_pixmap); Q_GLOBAL_STATIC(QFont, tst_qapp_font); Q_GLOBAL_STATIC(QRegion, tst_qapp_region); -Q_GLOBAL_STATIC(QFontDatabase, tst_qapp_fontDatabase); #ifndef QT_NO_CURSOR Q_GLOBAL_STATIC(QCursor, tst_qapp_cursor); #endif @@ -2257,7 +2832,6 @@ void tst_QApplication::globalStaticObjectDestruction() QVERIFY(tst_qapp_pixmap()); QVERIFY(tst_qapp_font()); QVERIFY(tst_qapp_region()); - QVERIFY(tst_qapp_fontDatabase()); #ifndef QT_NO_CURSOR QVERIFY(tst_qapp_cursor()); #endif @@ -2268,6 +2842,7 @@ int main(int argc, char *argv[]) { tst_QApplication tc; argv0 = argv[0]; + QTEST_SET_MAIN_SOURCE_PATH return QTest::qExec(&tc, argc, argv); } diff --git a/tests/auto/widgets/kernel/qboxlayout/CMakeLists.txt b/tests/auto/widgets/kernel/qboxlayout/CMakeLists.txt new file mode 100644 index 0000000000..5b60382fba --- /dev/null +++ b/tests/auto/widgets/kernel/qboxlayout/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qboxlayout Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qboxlayout LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qboxlayout + SOURCES + tst_qboxlayout.cpp + LIBRARIES + Qt::Gui + Qt::TestPrivate + Qt::Widgets +) diff --git a/tests/auto/widgets/kernel/qboxlayout/qboxlayout.pro b/tests/auto/widgets/kernel/qboxlayout/qboxlayout.pro deleted file mode 100644 index 1a2c6d6f66..0000000000 --- a/tests/auto/widgets/kernel/qboxlayout/qboxlayout.pro +++ /dev/null @@ -1,6 +0,0 @@ -CONFIG += testcase -TARGET = tst_qboxlayout -QT += widgets testlib testlib-private -SOURCES += tst_qboxlayout.cpp - - diff --git a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp index b2650d1f32..4313d9891c 100644 --- a/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp +++ b/tests/auto/widgets/kernel/qboxlayout/tst_qboxlayout.cpp @@ -1,33 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <QtGui> #include <QtWidgets> @@ -47,6 +22,7 @@ private slots: void sizeConstraints(); void setGeometry(); void setStyleShouldChangeSpacing(); + void widgetSurplus(); void testLayoutEngine_data(); void testLayoutEngine(); @@ -69,15 +45,15 @@ public: vspacing = 10; } - virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, - const QWidget * widget = 0 ) const; + virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = nullptr, + const QWidget * widget = nullptr ) const override; int hspacing; int vspacing; }; -int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/, - const QWidget * widget /*= 0*/ ) const +int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= nullptr*/, + const QWidget * widget /*= nullptr*/ ) const { switch (metric) { case PM_LayoutLeftMargin: @@ -140,7 +116,7 @@ void tst_QBoxLayout::insertLayout() QCOMPARE(dummyParentLayout->count(), 1); // add subLayout to another layout - QTest::ignoreMessage(QtWarningMsg, "QLayout::addChildLayout: layout \"\" already has a parent"); + QTest::ignoreMessage(QtWarningMsg, "QLayout::addChildLayout: layout QHBoxLayout \"\" already has a parent"); vbox->addLayout(subLayout); QCOMPARE((subLayout->parent() == vbox), (vbox->count() == 1)); } @@ -193,7 +169,7 @@ void tst_QBoxLayout::setGeometry() setFrameless(&toplevel); QWidget w(&toplevel); QVBoxLayout *lay = new QVBoxLayout; - lay->setMargin(0); + lay->setContentsMargins(0, 0, 0, 0); lay->setSpacing(0); QHBoxLayout *lay2 = new QHBoxLayout; QDial *dial = new QDial; @@ -216,7 +192,7 @@ void tst_QBoxLayout::setStyleShouldChangeSpacing() window.setWindowTitle(QTest::currentTestFunction()); QHBoxLayout *hbox = new QHBoxLayout(&window); QPushButton *pb1 = new QPushButton(tr("The spacing between this")); - QPushButton *pb2 = new QPushButton(tr("and this button should depend on the style of the parent widget"));; + QPushButton *pb2 = new QPushButton(tr("and this button should depend on the style of the parent widget")); pb1->setAttribute(Qt::WA_LayoutUsesWidgetRect); pb2->setAttribute(Qt::WA_LayoutUsesWidgetRect); hbox->addWidget(pb1); @@ -236,6 +212,69 @@ void tst_QBoxLayout::setStyleShouldChangeSpacing() QTRY_COMPARE(spacing(), 10); } +class MarginEatingStyle : public QProxyStyle +{ +public: + MarginEatingStyle() : QProxyStyle(QStyleFactory::create("windows")) + { + } + + virtual QRect subElementRect(SubElement sr, const QStyleOption *opt, + const QWidget *widget) const override + { + QRect rect = opt->rect; + switch (sr) { + case SE_GroupBoxLayoutItem: + // this is a simplifed version of what the macOS style does + rect.setTop(rect.top() + 20); + rect.setLeft(rect.left() + 20); + rect.setRight(rect.right() - 20); + rect.setBottom(rect.bottom() - 20); + break; + default: + return QProxyStyle::subElementRect(sr, opt, widget); + } + + return rect; + } +}; + +void tst_QBoxLayout::widgetSurplus() +{ + // Test case for QTBUG-67608 - a style requests space in the margin + + QDialog window; + QScopedPointer<MarginEatingStyle> marginEater(new MarginEatingStyle); + QVBoxLayout *vbox = new QVBoxLayout(&window); + vbox->setContentsMargins(0, 0, 0, 0); + vbox->setSpacing(0); + + QLabel *hiddenLabel = new QLabel(tr("Invisible label")); + hiddenLabel->setVisible(false); + + QGroupBox *groupBox = new QGroupBox(tr("Groupbox Title")); + groupBox->setStyle(marginEater.data()); + groupBox->setObjectName("Test group box"); + QPushButton *button1 = new QPushButton(tr("Button 1")); + QPushButton *button2 = new QPushButton(tr("Button 2")); + QVBoxLayout *groupLayout = new QVBoxLayout; + groupLayout->addWidget(button1); + groupLayout->addWidget(button2); + groupBox->setLayout(groupLayout); + + QLabel *label = new QLabel(tr("Visible label")); + + vbox->addWidget(hiddenLabel); + vbox->addWidget(groupBox); + vbox->addWidget(label); + window.setLayout(vbox); + + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QCOMPARE(groupBox->y(), 0); + QCOMPARE(groupBox->x(), 0); +} + void tst_QBoxLayout::taskQTBUG_7103_minMaxWidthNotRespected() { QLabel *label = new QLabel("Qt uses standard C++, but makes extensive use of the C pre-processor to enrich the language. Qt can also be used in several other programming languages via language bindings. It runs on all major platforms, and has extensive internationalization support. Non-GUI features include SQL database access, XML parsing, thread management, network support and a unified cross-platform API for file handling."); @@ -347,14 +386,14 @@ class LayoutItem : public QLayoutItem public: LayoutItem(const Descr &descr) :m_descr(descr) {} - QSize sizeHint() const { return QSize(m_descr.sizeHint, 100); } - QSize minimumSize() const { return QSize(m_descr.minimumSize, 0); } - QSize maximumSize() const { return QSize(m_descr.maximumSize, QLAYOUTSIZE_MAX); } - Qt::Orientations expandingDirections() const - { return m_descr.expanding ? Qt::Horizontal : Qt::Orientations(0); } - void setGeometry(const QRect &r) { m_pos = r.x(); m_size = r.width();} - QRect geometry() const { return QRect(m_pos, 0, m_size, 100); } - bool isEmpty() const { return m_descr.empty; } + QSize sizeHint() const override { return QSize(m_descr.sizeHint, 100); } + QSize minimumSize() const override { return QSize(m_descr.minimumSize, 0); } + QSize maximumSize() const override { return QSize(m_descr.maximumSize, QLAYOUTSIZE_MAX); } + Qt::Orientations expandingDirections() const override + { return m_descr.expanding ? Qt::Horizontal : Qt::Orientations{}; } + void setGeometry(const QRect &r) override { m_pos = r.x(); m_size = r.width();} + QRect geometry() const override { return QRect(m_pos, 0, m_size, 100); } + bool isEmpty() const override { return m_descr.empty; } private: Descr m_descr; @@ -478,14 +517,14 @@ void tst_QBoxLayout::testLayoutEngine() QHBoxLayout box; box.setSpacing(spacing); int i; - for (i = 0; i < itemDescriptions.count(); ++i) { + for (i = 0; i < itemDescriptions.size(); ++i) { Descr descr = itemDescriptions.at(i); LayoutItem *li = new LayoutItem(descr); box.addItem(li); box.setStretch(i, descr.stretch); } box.setGeometry(QRect(0,0,size,100)); - for (i = 0; i < expectedSizes.count(); ++i) { + for (i = 0; i < expectedSizes.size(); ++i) { int xSize = expectedSizes.at(i); int xPos = expectedPositions.at(i); QLayoutItem *item = box.itemAt(i); @@ -507,6 +546,10 @@ void tst_QBoxLayout::replaceWidget() QCOMPARE(boxLayout->indexOf(replaceFrom), 1); QCOMPARE(boxLayout->indexOf(replaceTo), -1); + QCOMPARE(boxLayout->count(), 3); + boxLayout->replaceWidget(replaceFrom, replaceFrom); + QCOMPARE(boxLayout->count(), 3); + delete boxLayout->replaceWidget(replaceFrom, replaceTo); QCOMPARE(boxLayout->indexOf(replaceFrom), -1); diff --git a/tests/auto/widgets/kernel/qdesktopwidget/.gitignore b/tests/auto/widgets/kernel/qdesktopwidget/.gitignore deleted file mode 100644 index d6f7cc7ca9..0000000000 --- a/tests/auto/widgets/kernel/qdesktopwidget/.gitignore +++ /dev/null @@ -1 +0,0 @@ -tst_qdesktopwidget diff --git a/tests/auto/widgets/kernel/qdesktopwidget/qdesktopwidget.pro b/tests/auto/widgets/kernel/qdesktopwidget/qdesktopwidget.pro deleted file mode 100644 index 1ab155f22e..0000000000 --- a/tests/auto/widgets/kernel/qdesktopwidget/qdesktopwidget.pro +++ /dev/null @@ -1,4 +0,0 @@ -CONFIG += testcase -TARGET = tst_qdesktopwidget -QT += widgets testlib -SOURCES += tst_qdesktopwidget.cpp diff --git a/tests/auto/widgets/kernel/qdesktopwidget/tst_qdesktopwidget.cpp b/tests/auto/widgets/kernel/qdesktopwidget/tst_qdesktopwidget.cpp deleted file mode 100644 index 6f2847974f..0000000000 --- a/tests/auto/widgets/kernel/qdesktopwidget/tst_qdesktopwidget.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> -#include <QtWidgets/QDesktopWidget> -#include <QtGui/QWindow> -#include <QDebug> - -class tst_QDesktopWidget : public QObject -{ - Q_OBJECT - -private slots: - void cleanup(); - - void numScreens(); - void primaryScreen(); - void screenNumberForQWidget(); - void screenNumberForQPoint(); - void availableGeometry(); - void screenGeometry(); - void topLevels(); -}; - -void tst_QDesktopWidget::cleanup() -{ - QVERIFY(QApplication::topLevelWidgets().isEmpty()); -} - -void tst_QDesktopWidget::numScreens() -{ - QDesktopWidget desktop; - QVERIFY(desktop.numScreens() > 0); -} - -void tst_QDesktopWidget::primaryScreen() -{ - QDesktopWidget desktop; - QVERIFY(desktop.primaryScreen() >= 0); - QVERIFY(desktop.primaryScreen() < desktop.numScreens()); -} - -void tst_QDesktopWidget::availableGeometry() -{ - QDesktopWidget desktop; - QTest::ignoreMessage(QtWarningMsg, "QDesktopWidget::availableGeometry(): Attempt " - "to get the available geometry of a null widget"); - desktop.availableGeometry((QWidget *)0); - - QRect total; - QRect available; - - for (int i = 0; i < desktop.screenCount(); ++i) { - total = desktop.screenGeometry(i); - available = desktop.availableGeometry(i); - QVERIFY(total.contains(available)); - } - - total = desktop.screenGeometry(); - available = desktop.availableGeometry(); - QVERIFY(total.contains(available)); - QCOMPARE(desktop.availableGeometry(desktop.primaryScreen()), available); - QCOMPARE(desktop.screenGeometry(desktop.primaryScreen()), total); -} - -void tst_QDesktopWidget::screenNumberForQWidget() -{ - QDesktopWidget desktop; - - QCOMPARE(desktop.screenNumber(0), 0); - - QWidget widget; - widget.show(); - QVERIFY(QTest::qWaitForWindowExposed(&widget)); - QVERIFY(widget.isVisible()); - - int widgetScreen = desktop.screenNumber(&widget); - QVERIFY(widgetScreen > -1); - QVERIFY(widgetScreen < desktop.numScreens()); -} - -void tst_QDesktopWidget::screenNumberForQPoint() -{ - // make sure QDesktopWidget::screenNumber(QPoint) returns the correct screen - QDesktopWidget *desktopWidget = QApplication::desktop(); - QRect allScreens; - for (int i = 0; i < desktopWidget->numScreens(); ++i) { - QRect screenGeometry = desktopWidget->screenGeometry(i); - QCOMPARE(desktopWidget->screenNumber(screenGeometry.center()), i); - allScreens |= screenGeometry; - } - - // make sure QDesktopWidget::screenNumber(QPoint) returns a valid screen for points that aren't on any screen - int screen; - screen = desktopWidget->screenNumber(allScreens.topLeft() - QPoint(1, 1)); - - QVERIFY(screen >= 0 && screen < desktopWidget->numScreens()); - screen = desktopWidget->screenNumber(allScreens.topRight() + QPoint(1, 1)); - QVERIFY(screen >= 0 && screen < desktopWidget->numScreens()); - screen = desktopWidget->screenNumber(allScreens.bottomLeft() - QPoint(1, 1)); - QVERIFY(screen >= 0 && screen < desktopWidget->numScreens()); - screen = desktopWidget->screenNumber(allScreens.bottomRight() + QPoint(1, 1)); - QVERIFY(screen >= 0 && screen < desktopWidget->numScreens()); -} - -void tst_QDesktopWidget::screenGeometry() -{ - QDesktopWidget *desktopWidget = QApplication::desktop(); - QTest::ignoreMessage(QtWarningMsg, "QDesktopWidget::screenGeometry(): Attempt " - "to get the screen geometry of a null widget"); - QRect r = desktopWidget->screenGeometry((QWidget *)0); - QVERIFY(r.isNull()); - QWidget widget; - widget.show(); - QVERIFY(QTest::qWaitForWindowExposed(&widget)); - r = desktopWidget->screenGeometry(&widget); - - QRect total; - QRect available; - for (int i = 0; i < desktopWidget->screenCount(); ++i) { - total = desktopWidget->screenGeometry(i); - available = desktopWidget->availableGeometry(i); - } -} - -void tst_QDesktopWidget::topLevels() -{ - // Desktop widgets/windows should not be listed as top-levels. - int topLevelDesktopWidgets = 0; - int topLevelDesktopWindows = 0; - foreach (const QWidget *w, QApplication::topLevelWidgets()) - if (w->windowType() == Qt::Desktop) - topLevelDesktopWidgets++; - foreach (const QWindow *w, QGuiApplication::topLevelWindows()) - if (w->type() == Qt::Desktop) - topLevelDesktopWindows++; - QCOMPARE(topLevelDesktopWidgets, 0); - QCOMPARE(topLevelDesktopWindows, 0); -} - -QTEST_MAIN(tst_QDesktopWidget) -#include "tst_qdesktopwidget.moc" - diff --git a/tests/auto/widgets/kernel/qformlayout/CMakeLists.txt b/tests/auto/widgets/kernel/qformlayout/CMakeLists.txt new file mode 100644 index 0000000000..9e1da4c6a3 --- /dev/null +++ b/tests/auto/widgets/kernel/qformlayout/CMakeLists.txt @@ -0,0 +1,22 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qformlayout Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qformlayout LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qformlayout + SOURCES + tst_qformlayout.cpp + LIBRARIES + Qt::Gui + Qt::TestPrivate + Qt::Widgets + Qt::WidgetsPrivate +) diff --git a/tests/auto/widgets/kernel/qformlayout/qformlayout.pro b/tests/auto/widgets/kernel/qformlayout/qformlayout.pro deleted file mode 100644 index 617183fee6..0000000000 --- a/tests/auto/widgets/kernel/qformlayout/qformlayout.pro +++ /dev/null @@ -1,4 +0,0 @@ -CONFIG += testcase -TARGET = tst_qformlayout -QT += widgets testlib testlib-private -SOURCES += tst_qformlayout.cpp diff --git a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp index cff3dad35e..9638823538 100644 --- a/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp +++ b/tests/auto/widgets/kernel/qformlayout/tst_qformlayout.cpp @@ -1,33 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <qlayout.h> #include <qapplication.h> #include <qwidget.h> @@ -38,6 +13,9 @@ #include <QtWidgets/QLabel> #include <QtWidgets/QLineEdit> #include <QtWidgets/QPushButton> + +#include <private/qdialog_p.h> + #include <QStyleFactory> #include <QSharedPointer> @@ -49,10 +27,10 @@ using namespace QTestPrivate; // ItemRole has enumerators for numerical values 0..2, thus the only // valid numerical values for storing into an ItemRole variable are 0..3: -Q_CONSTEXPR QFormLayout::ItemRole invalidRole = QFormLayout::ItemRole(3); +constexpr QFormLayout::ItemRole invalidRole = QFormLayout::ItemRole(3); struct QFormLayoutTakeRowResultHolder { - QFormLayoutTakeRowResultHolder(QFormLayout::TakeRowResult result) Q_DECL_NOTHROW + QFormLayoutTakeRowResultHolder(QFormLayout::TakeRowResult result) noexcept : labelItem(result.labelItem), fieldItem(result.fieldItem) { @@ -66,20 +44,20 @@ struct QFormLayoutTakeRowResultHolder { if (fieldItem) disposer.setItem(0, QFormLayout::FieldRole, fieldItem); } - QFormLayoutTakeRowResultHolder(QFormLayoutTakeRowResultHolder &&other) Q_DECL_NOTHROW + QFormLayoutTakeRowResultHolder(QFormLayoutTakeRowResultHolder &&other) noexcept : labelItem(other.labelItem), fieldItem(other.fieldItem) { other.labelItem = nullptr; other.fieldItem = nullptr; } - QFormLayoutTakeRowResultHolder &operator=(QFormLayoutTakeRowResultHolder &&other) Q_DECL_NOTHROW + QFormLayoutTakeRowResultHolder &operator=(QFormLayoutTakeRowResultHolder &&other) noexcept { swap(other); return *this; } - void swap(QFormLayoutTakeRowResultHolder &other) Q_DECL_NOTHROW + void swap(QFormLayoutTakeRowResultHolder &other) noexcept { qSwap(labelItem, other.labelItem); qSwap(fieldItem, other.fieldItem); @@ -96,7 +74,9 @@ class tst_QFormLayout : public QObject private slots: void cleanup(); void rowCount(); +#if QT_CONFIG(shortcut) void buddies(); +#endif void getItemPosition(); void wrapping(); void spacing(); @@ -130,6 +110,9 @@ private slots: void takeRow_QLayout(); void setWidget(); void setLayout(); + void hideShowRow(); + void showWithHiddenRow(); + void hiddenRowAndStretch(); /* QLayoutItem *itemAt(int row, ItemRole role) const; @@ -187,6 +170,8 @@ void tst_QFormLayout::rowCount() //TODO: remove items } +#if QT_CONFIG(shortcut) + void tst_QFormLayout::buddies() { QWidget w; @@ -215,6 +200,8 @@ void tst_QFormLayout::buddies() //TODO: empty label? } +#endif // QT_CONFIG(shortcut) + void tst_QFormLayout::getItemPosition() { QWidget w; @@ -265,7 +252,9 @@ void tst_QFormLayout::wrapping() fl->setRowWrapPolicy(QFormLayout::WrapLongRows); QLineEdit *le = new QLineEdit; - QLabel *lbl = new QLabel("A long label"); + QLabel *lbl = new QLabel("A long label which is actually long enough to trigger wrapping," + " even on Android and even if it is executed on a tiling window" + " manager which forces the window into fullscreen mode."); le->setMinimumWidth(200); fl->addRow(lbl, le); @@ -273,9 +262,6 @@ void tst_QFormLayout::wrapping() w.setWindowTitle(QTest::currentTestFunction()); w.show(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "setFixedWidth does not work on WinRT", Abort); -#endif QCOMPARE(le->geometry().y() > lbl->geometry().y(), true); //TODO: additional tests covering different wrapping cases @@ -291,15 +277,15 @@ public: vspacing = 10; } - virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, - const QWidget * widget = 0 ) const; + virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = nullptr, + const QWidget * widget = nullptr) const override; int hspacing; int vspacing; }; -int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/, - const QWidget * widget /*= 0*/ ) const +int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= nullptr*/, + const QWidget * widget /*= nullptr*/ ) const { switch (metric) { case PM_LayoutHorizontalSpacing: @@ -385,7 +371,7 @@ void tst_QFormLayout::contentsRect() class DummyMacStyle : public QCommonStyle { public: - virtual int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const + virtual int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = nullptr, QStyleHintReturn * returnData = 0 ) const override { switch(hint) { case SH_FormLayoutFormAlignment: @@ -405,7 +391,7 @@ public: class DummyQtopiaStyle : public QCommonStyle { public: - virtual int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = 0, QStyleHintReturn * returnData = 0 ) const + virtual int styleHint ( StyleHint hint, const QStyleOption * option = 0, const QWidget * widget = nullptr, QStyleHintReturn * returnData = 0 ) const override { switch(hint) { case SH_FormLayoutFormAlignment: @@ -684,17 +670,21 @@ void tst_QFormLayout::insertRow_QString_QWidget() layout->insertRow(-5, "&Name:", fld1); QLabel *label1 = qobject_cast<QLabel *>(layout->itemAt(0, QFormLayout::LabelRole)->widget()); QVERIFY(label1 != 0); +#if QT_CONFIG(shortcut) QCOMPARE(label1->buddy(), fld1); - +#endif layout->insertRow(0, "&Email:", fld2); QLabel *label2 = qobject_cast<QLabel *>(layout->itemAt(0, QFormLayout::LabelRole)->widget()); QVERIFY(label2 != 0); +#if QT_CONFIG(shortcut) QCOMPARE(label2->buddy(), fld2); - +#endif layout->insertRow(5, "&Age:", fld3); QLabel *label3 = qobject_cast<QLabel *>(layout->itemAt(2, QFormLayout::LabelRole)->widget()); QVERIFY(label3 != 0); +#if QT_CONFIG(shortcut) QCOMPARE(label3->buddy(), fld3); +#endif } void tst_QFormLayout::insertRow_QString_QLayout() @@ -708,21 +698,27 @@ void tst_QFormLayout::insertRow_QString_QLayout() layout->insertRow(-5, "&Name:", fld1); QLabel *label1 = qobject_cast<QLabel *>(layout->itemAt(0, QFormLayout::LabelRole)->widget()); QVERIFY(label1 != 0); +#if QT_CONFIG(shortcut) QVERIFY(!label1->buddy()); +#endif QCOMPARE(layout->rowCount(), 1); layout->insertRow(0, "&Email:", fld2); QLabel *label2 = qobject_cast<QLabel *>(layout->itemAt(0, QFormLayout::LabelRole)->widget()); QVERIFY(label2 != 0); +#if QT_CONFIG(shortcut) QVERIFY(!label2->buddy()); +#endif QCOMPARE(layout->rowCount(), 2); layout->insertRow(5, "&Age:", fld3); QLabel *label3 = qobject_cast<QLabel *>(layout->itemAt(2, QFormLayout::LabelRole)->widget()); QVERIFY(label3 != 0); +#if QT_CONFIG(shortcut) QVERIFY(!label3->buddy()); +#endif QCOMPARE(layout->rowCount(), 3); } @@ -798,6 +794,7 @@ void tst_QFormLayout::removeRow_QWidget() QCOMPARE(layout->rowCount(), 0); QWidget *w3 = new QWidget; + QTest::ignoreMessage(QtWarningMsg, "QFormLayout::takeRow: Invalid widget"); layout->removeRow(w3); delete w3; } @@ -838,6 +835,7 @@ void tst_QFormLayout::removeRow_QLayout() QCOMPARE(layout->rowCount(), 0); QHBoxLayout *l3 = new QHBoxLayout; + QTest::ignoreMessage(QtWarningMsg, "QFormLayout::takeRow: Invalid layout"); layout->removeRow(l3); delete l3; } @@ -877,6 +875,7 @@ void tst_QFormLayout::takeRow() QCOMPARE(layout->rowCount(), 0); QCOMPARE(result.fieldItem->widget(), w1.data()); + QTest::ignoreMessage(QtWarningMsg, "QFormLayout::takeRow: Invalid row 0"); result = layout->takeRow(0); QVERIFY(!result.fieldItem); @@ -917,6 +916,7 @@ void tst_QFormLayout::takeRow_QWidget() QCOMPARE(layout->rowCount(), 0); QWidget *w3 = new QWidget; + QTest::ignoreMessage(QtWarningMsg, "QFormLayout::takeRow: Invalid widget"); result = layout->takeRow(w3); delete w3; @@ -964,6 +964,7 @@ void tst_QFormLayout::takeRow_QLayout() QCOMPARE(layout->rowCount(), 0); QHBoxLayout *l3 = new QHBoxLayout; + QTest::ignoreMessage(QtWarningMsg, "QFormLayout::takeRow: Invalid layout"); result = layout->takeRow(l3); delete l3; @@ -993,7 +994,9 @@ void tst_QFormLayout::setWidget() QCOMPARE(layout.rowCount(), 6); // should be ignored and generate warnings + QTest::ignoreMessage(QtWarningMsg, "QFormLayoutPrivate::setItem: Cell (3, 1) already occupied"); layout.setWidget(3, QFormLayout::FieldRole, &w4); + QTest::ignoreMessage(QtWarningMsg, "QFormLayoutPrivate::setItem: Invalid cell (-1, 1)"); layout.setWidget(-1, QFormLayout::FieldRole, &w4); { @@ -1061,7 +1064,9 @@ void tst_QFormLayout::setLayout() QCOMPARE(layout.rowCount(), 6); // should be ignored and generate warnings + QTest::ignoreMessage(QtWarningMsg, "QFormLayoutPrivate::setItem: Cell (3, 1) already occupied"); layout.setLayout(3, QFormLayout::FieldRole, &l4); + QTest::ignoreMessage(QtWarningMsg, "QLayout::addChildLayout: layout QHBoxLayout \"\" already has a parent"); layout.setLayout(-1, QFormLayout::FieldRole, &l4); QCOMPARE(layout.count(), 3); QCOMPARE(layout.rowCount(), 6); @@ -1107,6 +1112,191 @@ void tst_QFormLayout::setLayout() } } +void tst_QFormLayout::hideShowRow() +{ + QWidget topLevel; + QFormLayout layout; + + const auto makeComplex = []{ + QHBoxLayout *hboxField = new QHBoxLayout; + hboxField->addWidget(new QLineEdit("Left")); + hboxField->addWidget(new QLineEdit("Right")); + return hboxField; + }; + + layout.addRow("Label", new QLineEdit("one")); + layout.addRow("Label", new QLineEdit("two")); + layout.addRow("Label", new QLineEdit("three")); + layout.addRow("Label", makeComplex()); + layout.addRow(new QLineEdit("five")); // spanning widget + layout.addRow(makeComplex()); // spanning layout + + topLevel.setLayout(&layout); + topLevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + + // returns the top-left position of the items in a row + const auto rowPosition = [&layout](int row) { + QRect rect; + if (QLayoutItem *spanningItem = layout.itemAt(row, QFormLayout::SpanningRole)) { + rect = spanningItem->geometry(); + } else { + if (QLayoutItem *labelItem = layout.itemAt(row, QFormLayout::LabelRole)) { + rect = labelItem->geometry(); + } + if (QLayoutItem *fieldItem = layout.itemAt(row, QFormLayout::FieldRole)) { + rect |= fieldItem->geometry(); + } + } + return rect.topLeft(); + }; + + // returns the first widget in a row, even if that row is taken by a layout + const auto rowInputWidget = [&layout](int row) -> QWidget* { + auto fieldItem = layout.itemAt(row, QFormLayout::FieldRole); + if (!fieldItem) + return nullptr; + QWidget *fieldWidget = fieldItem->widget(); + // we happen to know our layout structure + if (!fieldWidget) + fieldWidget = fieldItem->layout()->itemAt(0)->widget(); + return fieldWidget; + }; + + // record the reference positions for all rows + QList<QPoint> rowPositions(layout.rowCount()); + for (int row = 0; row < layout.rowCount(); ++row) + rowPositions[row] = rowPosition(row); + + // hide each row in turn, the next row should take the space of the hidden row + for (int row = 0; row < layout.rowCount(); ++ row) { + layout.setRowVisible(row, false); + QVERIFY(!layout.isRowVisible(row)); + if (row < layout.rowCount() - 1) + QTRY_COMPARE(rowPosition(row + 1), rowPositions[row]); + layout.setRowVisible(row, true); + QVERIFY(layout.isRowVisible(row)); + } + + // Hiding only the label or only the field doesn't hide the row. + for (int row = 0; row < layout.rowCount() - 1; ++row) { + const auto labelItem = layout.itemAt(0, QFormLayout::LabelRole); + if (labelItem) { + labelItem->widget()->hide(); + QVERIFY(layout.isRowVisible(row)); + QCOMPARE(rowPosition(row), rowPositions[row]); + layout.itemAt(0, QFormLayout::LabelRole)->widget()->show(); + } + const auto fieldItem = layout.itemAt(0, QFormLayout::FieldRole); + if (fieldItem) { + fieldItem->widget()->hide(); + QVERIFY(layout.isRowVisible(row)); + QCOMPARE(rowPosition(row), rowPositions[row]); + layout.itemAt(0, QFormLayout::FieldRole)->widget()->show(); + } + } + + // If we hide both label and field, then the row should be considered hidden and the + // following row should move up into the space of the hidden row. We can only test + // this if both label and field are widgets, or if there is a spanning widget. + for (int row = 0; row < layout.rowCount() - 1; ++row) { + QWidget *labelWidget = nullptr; + if (auto labelItem = layout.itemAt(row, QFormLayout::LabelRole)) + labelWidget = labelItem->widget(); + QWidget *fieldWidget = nullptr; + if (auto fieldItem = layout.itemAt(row, QFormLayout::FieldRole)) + fieldWidget = fieldItem->widget(); + + if (!fieldWidget) + continue; + if (labelWidget) + labelWidget->hide(); + fieldWidget->hide(); + QVERIFY(!layout.isRowVisible(row)); + QVERIFY(!layout.isRowVisible(fieldWidget)); + if (labelWidget) + QVERIFY(!layout.isRowVisible(labelWidget)); + QTRY_COMPARE(rowPosition(row + 1), rowPositions[row]); + if (labelWidget) + labelWidget->show(); + fieldWidget->show(); + } + + // hiding a row where a widget has focus must move focus to a widget in the next row + for (int row = 0; row < layout.rowCount(); ++row) { + QWidget *inputWidget = rowInputWidget(row); + QVERIFY(inputWidget); + inputWidget->setFocus(); + layout.setRowVisible(row, false); + QVERIFY(!inputWidget->hasFocus()); + } + + // Now hide all rows, hide the toplevel widget, and show the toplevel widget again. + // None of the widgets inside must be visible. + for (int row = 0; row < layout.rowCount(); ++row) + layout.setRowVisible(row, false); + topLevel.hide(); + topLevel.show(); + for (int row = 0; row < layout.rowCount(); ++row) + QVERIFY(rowInputWidget(row)->isHidden()); +} + +void tst_QFormLayout::showWithHiddenRow() +{ + QWidget topLevel; + QFormLayout layout; + + for (int row = 0; row < 3; ++row) + layout.addRow(QString("Label %1").arg(row), new QLineEdit); + layout.setRowVisible(1, false); + + topLevel.setLayout(&layout); + topLevel.show(); +} + +/* + Test that hiding rows does not leave outdated layout data behind + in hidden items that results in out-of-bounds array access. See + QTBUG-109237. +*/ +void tst_QFormLayout::hiddenRowAndStretch() +{ + QWidget topLevel; + QFormLayout layout; + layout.setRowWrapPolicy(QFormLayout::WrapAllRows); + + // We need our own stretcher item so that QFormLayout doesn't insert + // it's own, as that would grow the size of the layout data array again. + QSpacerItem *stretch = new QSpacerItem(100, 100, QSizePolicy::Expanding, QSizePolicy::Expanding); + layout.setItem(0, QFormLayout::FieldRole, stretch); + + QLabel *lastLabel = nullptr; + QLineEdit *lastField = nullptr; + for (int row = 1; row < 4; ++row) { + QLabel *label = new QLabel(QString("Label %1").arg(row)); + label->setWordWrap(true); + QLineEdit *field = new QLineEdit; + layout.setWidget(row, QFormLayout::LabelRole, label); + layout.setWidget(row, QFormLayout::FieldRole, field); + if (row == 3) { + lastLabel = label; + lastField = field; + } + } + + Q_ASSERT(lastLabel); + Q_ASSERT(lastField); + + topLevel.setLayout(&layout); + topLevel.sizeHint(); + + lastLabel->setVisible(false); + lastField->setVisible(false); + + // should not assert here + topLevel.show(); +} + void tst_QFormLayout::itemAt() { QWidget topLevel; @@ -1196,22 +1386,20 @@ void tst_QFormLayout::layoutAlone() void tst_QFormLayout::taskQTBUG_27420_takeAtShouldUnparentLayout() { QSharedPointer<QFormLayout> outer(new QFormLayout); - QPointer<QFormLayout> inner = new QFormLayout; + QAutoPointer<QFormLayout> holder{new QFormLayout}; + auto inner = holder.get(); outer->addRow(inner); QCOMPARE(outer->count(), 1); QCOMPARE(inner->parent(), outer.data()); QLayoutItem *item = outer->takeAt(0); - QCOMPARE(item->layout(), inner.data()); + QCOMPARE(item->layout(), inner); QVERIFY(!item->layout()->parent()); outer.reset(); - if (inner) - delete item; // success: a taken item/layout should not be deleted when the old parent is deleted - else - QVERIFY(!inner.isNull()); + QVERIFY(holder); // a taken item/layout should not be deleted when the old parent is deleted } void tst_QFormLayout::taskQTBUG_40609_addingWidgetToItsOwnLayout(){ diff --git a/tests/auto/widgets/kernel/qgesturerecognizer/CMakeLists.txt b/tests/auto/widgets/kernel/qgesturerecognizer/CMakeLists.txt new file mode 100644 index 0000000000..ffa54992d3 --- /dev/null +++ b/tests/auto/widgets/kernel/qgesturerecognizer/CMakeLists.txt @@ -0,0 +1,22 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qgesturerecognizer Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qgesturerecognizer LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qgesturerecognizer + SOURCES + tst_qgesturerecognizer.cpp + LIBRARIES + Qt::CorePrivate + Qt::Gui + Qt::GuiPrivate + Qt::Widgets +) diff --git a/tests/auto/widgets/kernel/qgesturerecognizer/qgesturerecognizer.pro b/tests/auto/widgets/kernel/qgesturerecognizer/qgesturerecognizer.pro deleted file mode 100644 index 7c9ddcfb03..0000000000 --- a/tests/auto/widgets/kernel/qgesturerecognizer/qgesturerecognizer.pro +++ /dev/null @@ -1,4 +0,0 @@ -CONFIG += testcase -TARGET = tst_qgesturerecognizer -QT += widgets testlib gui-private core-private -SOURCES += tst_qgesturerecognizer.cpp diff --git a/tests/auto/widgets/kernel/qgesturerecognizer/tst_qgesturerecognizer.cpp b/tests/auto/widgets/kernel/qgesturerecognizer/tst_qgesturerecognizer.cpp index bcf48c21df..cdab480d84 100644 --- a/tests/auto/widgets/kernel/qgesturerecognizer/tst_qgesturerecognizer.cpp +++ b/tests/auto/widgets/kernel/qgesturerecognizer/tst_qgesturerecognizer.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtTest/QTest> @@ -32,8 +7,8 @@ #include <QtWidgets/QWidget> #include <QtWidgets/QGestureEvent> #include <QtGui/QScreen> -#include <QtGui/QTouchDevice> -#include <QtCore/QVector> +#include <QtGui/QPointingDevice> +#include <QtCore/QList> #include <QtCore/QString> #include <QtCore/QHash> #include <QtCore/QDebug> @@ -53,11 +28,12 @@ private Q_SLOTS: void pinchGesture(); void swipeGesture_data(); void swipeGesture(); + void touchReplay(); #endif // !QT_NO_GESTURES private: const int m_fingerDistance; - QTouchDevice *m_touchDevice; + QPointingDevice *m_touchDevice; }; tst_QGestureRecognizer::tst_QGestureRecognizer() @@ -73,7 +49,7 @@ void tst_QGestureRecognizer::initTestCase() #ifndef QT_NO_GESTURES -typedef QVector<Qt::GestureType> GestureTypeVector; +typedef QList<Qt::GestureType> GestureTypeVector; class TestWidget : public QWidget { @@ -95,7 +71,7 @@ TestWidget::TestWidget(const GestureTypeVector &gestureTypes) { setAttribute(Qt::WA_AcceptTouchEvents); - foreach (Qt::GestureType gestureType, gestureTypes) { + for (Qt::GestureType gestureType : gestureTypes) { grabGesture(gestureType); m_receivedGestures.insert(gestureType, false); } @@ -126,8 +102,7 @@ bool TestWidget::event(QEvent * event) return QWidget::event(event); } -static void pressSequence(QTest::QTouchEventSequence &sequence, - QVector<QPoint> &points, +static void pressSequence(QTest::QTouchEventWidgetSequence &sequence, QList<QPoint> &points, QWidget *widget) { const int pointCount = points.size(); @@ -136,10 +111,8 @@ static void pressSequence(QTest::QTouchEventSequence &sequence, sequence.commit(); } -static void linearSequence(int n, const QPoint &delta, - QTest::QTouchEventSequence &sequence, - QVector<QPoint> &points, - QWidget *widget) +static void linearSequence(int n, const QPoint &delta, QTest::QTouchEventWidgetSequence &sequence, + QList<QPoint> &points, QWidget *widget) { const int pointCount = points.size(); for (int s = 0; s < n; ++s) { @@ -151,8 +124,7 @@ static void linearSequence(int n, const QPoint &delta, } } -static void releaseSequence(QTest::QTouchEventSequence &sequence, - QVector<QPoint> &points, +static void releaseSequence(QTest::QTouchEventWidgetSequence &sequence, QList<QPoint> &points, QWidget *widget) { const int pointCount = points.size(); @@ -179,7 +151,7 @@ void tst_QGestureRecognizer::panGesture() QFETCH(int, panSubTest); QFETCH(bool, gestureExpected); - Q_UNUSED(panSubTest) // Single finger pan will be added later. + Q_UNUSED(panSubTest); // Single finger pan will be added later. const int panPoints = 2; const Qt::GestureType gestureType = Qt::PanGesture; @@ -188,11 +160,11 @@ void tst_QGestureRecognizer::panGesture() widget.show(); QVERIFY(QTest::qWaitForWindowExposed(&widget)); - QVector<QPoint> points; + QList<QPoint> points; for (int i = 0; i < panPoints; ++i) points.append(QPoint(10 + i *20, 10 + i *20)); - QTest::QTouchEventSequence panSequence = QTest::touchEvent(&widget, m_touchDevice); + QTest::QTouchEventWidgetSequence panSequence = QTest::touchEvent(&widget, m_touchDevice); pressSequence(panSequence, points, &widget); linearSequence(5, QPoint(20, 20), panSequence, points, &widget); releaseSequence(panSequence, points, &widget); @@ -223,7 +195,7 @@ void tst_QGestureRecognizer::pinchGesture() QFETCH(int, pinchSubTest); QFETCH(bool, gestureExpected); - Q_UNUSED(pinchSubTest) + Q_UNUSED(pinchSubTest); const Qt::GestureType gestureType = Qt::PinchGesture; TestWidget widget(GestureTypeVector(1, gestureType)); @@ -231,11 +203,11 @@ void tst_QGestureRecognizer::pinchGesture() widget.show(); QVERIFY(QTest::qWaitForWindowExposed(&widget)); - QVector<QPoint> points; + QList<QPoint> points; points.append(widget.rect().center()); points.append(points.front() + QPoint(0, 20)); - QTest::QTouchEventSequence pinchSequence = QTest::touchEvent(&widget, m_touchDevice); + QTest::QTouchEventWidgetSequence pinchSequence = QTest::touchEvent(&widget, m_touchDevice); pressSequence(pinchSequence, points, &widget); for (int s = 0; s < 5; ++s) { @@ -288,16 +260,16 @@ void tst_QGestureRecognizer::swipeGesture() // Start a swipe sequence with 2 points (QTBUG-15768) const QPoint fingerDistance(m_fingerDistance, m_fingerDistance); - QVector<QPoint> points; + QList<QPoint> points; for (int i = 0; i < swipePoints - 1; ++i) points.append(fingerDistance + i * fingerDistance); - QTest::QTouchEventSequence swipeSequence = QTest::touchEvent(&widget, m_touchDevice); + QTest::QTouchEventWidgetSequence swipeSequence = QTest::touchEvent(&widget, m_touchDevice); pressSequence(swipeSequence, points, &widget); // Press point #3 points.append(points.last() + fingerDistance); - swipeSequence.press(points.size() - 1, points.last(), &widget); + swipeSequence.stationary(0).stationary(1).press(points.size() - 1, points.last(), &widget); swipeSequence.commit(); Q_ASSERT(points.size() == swipePoints); @@ -330,6 +302,25 @@ void tst_QGestureRecognizer::swipeGesture() } } +void tst_QGestureRecognizer::touchReplay() +{ + const Qt::GestureType gestureType = Qt::TapGesture; + QWidget parent; + TestWidget widget(GestureTypeVector(1, gestureType)); + widget.setParent(&parent); + widget.setGeometry(0, 0, 100, 100); + parent.adjustSize(); + parent.show(); + QVERIFY(QTest::qWaitForWindowActive(&parent)); + + QWindow* windowHandle = parent.window()->windowHandle(); + const QPoint globalPos = QPoint(42, 16); + QTest::touchEvent(windowHandle, m_touchDevice).press(1, globalPos); + QTest::touchEvent(windowHandle, m_touchDevice).release(1, globalPos); + + QVERIFY(widget.gestureReceived(gestureType)); +} + #endif // !QT_NO_GESTURES QTEST_MAIN(tst_QGestureRecognizer) diff --git a/tests/auto/widgets/kernel/qgridlayout/BLACKLIST b/tests/auto/widgets/kernel/qgridlayout/BLACKLIST new file mode 100644 index 0000000000..d3dedae753 --- /dev/null +++ b/tests/auto/widgets/kernel/qgridlayout/BLACKLIST @@ -0,0 +1,3 @@ +# QTBUG-87404 +[minMaxSize] +android diff --git a/tests/auto/widgets/kernel/qgridlayout/CMakeLists.txt b/tests/auto/widgets/kernel/qgridlayout/CMakeLists.txt new file mode 100644 index 0000000000..bf72bc0ae6 --- /dev/null +++ b/tests/auto/widgets/kernel/qgridlayout/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qgridlayout Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qgridlayout LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qgridlayout + SOURCES + sortdialog.ui + tst_qgridlayout.cpp + LIBRARIES + Qt::CorePrivate + Qt::Gui + Qt::GuiPrivate + Qt::TestPrivate + Qt::Widgets + Qt::WidgetsPrivate + ENABLE_AUTOGEN_TOOLS + uic +) diff --git a/tests/auto/widgets/kernel/qgridlayout/qgridlayout.pro b/tests/auto/widgets/kernel/qgridlayout/qgridlayout.pro deleted file mode 100644 index 8947658e8c..0000000000 --- a/tests/auto/widgets/kernel/qgridlayout/qgridlayout.pro +++ /dev/null @@ -1,11 +0,0 @@ -CONFIG += testcase -TARGET = tst_qgridlayout - -QT += widgets widgets-private testlib -QT += core-private gui-private testlib-private - -SOURCES += tst_qgridlayout.cpp -FORMS += sortdialog.ui - - - diff --git a/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp b/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp index 5e0327319b..3c325699a7 100644 --- a/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp +++ b/tests/auto/widgets/kernel/qgridlayout/tst_qgridlayout.cpp @@ -1,33 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <qlayout.h> #include <qapplication.h> #include <qwidget.h> @@ -75,6 +50,7 @@ private slots: void taskQTBUG_40609_addingWidgetToItsOwnLayout(); void taskQTBUG_40609_addingLayoutToItself(); void taskQTBUG_52357_spacingWhenItemIsHidden(); + void taskQTBUG_91261_itemIndexRange(); void replaceWidget(); void dontCrashWhenExtendsToEnd(); }; @@ -82,7 +58,8 @@ private slots: static inline int visibleTopLevelWidgetCount() { int result= 0; - foreach (const QWidget *topLevel, QApplication::topLevelWidgets()) { + const auto topLevels = QApplication::topLevelWidgets(); + for (const QWidget *topLevel : topLevels) { if (topLevel->isVisible()) ++result; } @@ -219,9 +196,9 @@ void tst_QGridLayout::badDistributionBug() QDialog dialog; Ui::SortDialog ui; ui.setupUi(&dialog); - ui.gridLayout->setMargin(0); + ui.gridLayout->setContentsMargins(0, 0, 0, 0); ui.gridLayout->setSpacing(0); - ui.vboxLayout->setMargin(0); + ui.vboxLayout->setContentsMargins(0, 0, 0, 0); ui.vboxLayout->setSpacing(0); ui.okButton->setFixedHeight(20); ui.moreButton->setFixedHeight(20); @@ -234,10 +211,13 @@ void tst_QGridLayout::badDistributionBug() void tst_QGridLayout::setMinAndMaxSize() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("This test crashes on Wayland, see also QTBUG-107184"); + QWidget widget; setFrameless(&widget); QGridLayout layout(&widget); - layout.setMargin(0); + layout.setContentsMargins(0, 0, 0, 0); layout.setSpacing(0); layout.setSizeConstraint(QLayout::SetMinAndMaxSize); widget.show(); @@ -381,12 +361,12 @@ void tst_QGridLayout::setMinAndMaxSize() class SizeHinter : public QWidget { public: - SizeHinter(const QSize &s, QWidget *parent = 0) + SizeHinter(const QSize &s, QWidget *parent = nullptr) : QWidget(parent), sh(s) { } - SizeHinter(int w, int h, QWidget *parent = 0) + SizeHinter(int w, int h, QWidget *parent = nullptr) : QWidget(parent), sh(QSize(w,h)) {} void setSizeHint(QSize s) { sh = s; } - QSize sizeHint() const { return sh; } + QSize sizeHint() const override { return sh; } private: QSize sh; }; @@ -396,7 +376,7 @@ void tst_QGridLayout::spacingAndSpacers() QWidget widget; setFrameless(&widget); QGridLayout layout(&widget); - layout.setMargin(0); + layout.setContentsMargins(0, 0, 0, 0); layout.setSpacing(0); widget.show(); @@ -449,33 +429,33 @@ class Qt42Style : public QProxyStyle public: Qt42Style() : QProxyStyle(QStyleFactory::create("windows")) { - spacing = 6; - margin = 9; - margin_toplevel = 11; } - virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, - const QWidget * widget = 0 ) const; + virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = nullptr, + const QWidget * widget = nullptr ) const override; - int spacing; - int margin; - int margin_toplevel; + int spacing = 6; + int margin = 9; + int margin_toplevel = 11; }; -int Qt42Style::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/, - const QWidget * widget /*= 0*/ ) const +int Qt42Style::pixelMetric(PixelMetric metric, const QStyleOption * option, + const QWidget * widget) const { switch (metric) { - case PM_DefaultLayoutSpacing: - return spacing; - break; - case PM_DefaultTopLevelMargin: - return margin_toplevel; - break; - case PM_DefaultChildMargin: + case PM_LayoutLeftMargin: + case PM_LayoutRightMargin: + case PM_LayoutTopMargin: + case PM_LayoutBottomMargin: + if (option && option->state & State_Window) + return margin_toplevel; + if (widget && widget->isWindow()) + return margin_toplevel; return margin; - break; + case PM_LayoutHorizontalSpacing: + case PM_LayoutVerticalSpacing: + return spacing; default: break; } @@ -489,7 +469,7 @@ typedef QList<QPoint> PointList; class SizeHinterFrame : public QLabel { public: - SizeHinterFrame(QWidget *parent = 0) + SizeHinterFrame(QWidget *parent = nullptr) : QLabel(parent) { init(-1); @@ -508,11 +488,11 @@ public: } void setSizeHint(const QSize &s) { sh = s; } - QSize sizeHint() const { return sh; } + QSize sizeHint() const override { return sh; } void setMinimumSizeHint(const QSize &s) { msh = s; } - QSize minimumSizeHint() const { return msh; } + QSize minimumSizeHint() const override { return msh; } - virtual int heightForWidth(int width) const; + virtual int heightForWidth(int width) const override; void setNumberOfPixels(int numPixels) { m_numPixels = numPixels; @@ -682,7 +662,7 @@ void tst_QGridLayout::spacingsAndMargins() QSKIP("The screen is too small to run this test case"); // We are relying on the order here... - for (int pi = 0; pi < sizehinters.count(); ++pi) { + for (int pi = 0; pi < sizehinters.size(); ++pi) { QPoint pt = sizehinters.at(pi)->mapTo(&toplevel, QPoint(0, 0)); QCOMPARE(pt, expectedpositions.at(pi)); } @@ -852,7 +832,7 @@ void tst_QGridLayout::minMaxSize() QList<QPointer<SizeHinterFrame> > sizehinters; for (int i = 0; i < rows; ++i) { for (int j = 0; j < columns; ++j) { - SizeInfo si = sizeinfos.at(sizehinters.count()); + SizeInfo si = sizeinfos.at(sizehinters.size()); int numpixels = si.hfwNumPixels; if (pass == 1 && numpixels == -1) numpixels = -2; //### yuk, (and don't fake it if it already tests sizehint) @@ -881,7 +861,7 @@ void tst_QGridLayout::minMaxSize() QTRY_COMPARE(toplevel.size(), toplevel.sizeHint()); } // We are relying on the order here... - for (int pi = 0; pi < sizehinters.count(); ++pi) { + for (int pi = 0; pi < sizehinters.size(); ++pi) { QPoint pt = sizehinters.at(pi)->mapTo(&toplevel, QPoint(0, 0)); QCOMPARE(pt, sizeinfos.at(pi).expectedPos); } @@ -900,10 +880,10 @@ public: reimplementSubelementRect = false; } - virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, - const QWidget * widget = 0 ) const; + virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = nullptr, + const QWidget * widget = nullptr ) const override; virtual QRect subElementRect(SubElement sr, const QStyleOption *opt, - const QWidget *widget) const; + const QWidget *widget) const override; int hspacing; int vspacing; @@ -912,8 +892,8 @@ public: int layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation, - const QStyleOption *option = 0, - const QWidget *widget = 0) const; + const QStyleOption *option = nullptr, + const QWidget *widget = nullptr) const override; }; @@ -945,8 +925,8 @@ QRect CustomLayoutStyle::subElementRect(SubElement sr, const QStyleOption *opt, int CustomLayoutStyle::layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation, - const QStyleOption * /*option = 0*/, - const QWidget * /*widget = 0*/) const + const QStyleOption * /*option = nullptr*/, + const QWidget * /*widget = nullptr*/) const { if (orientation == Qt::Horizontal) { switch (CT2(control1, control2)) { @@ -966,8 +946,8 @@ int CustomLayoutStyle::layoutSpacing(QSizePolicy::ControlType control1, } } -int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/, - const QWidget * widget /*= 0*/ ) const +int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= nullptr*/, + const QWidget * widget /*= nullptr*/ ) const { switch (metric) { case PM_LayoutLeftMargin: @@ -1051,7 +1031,7 @@ void tst_QGridLayout::styleDependentSpacingsAndMargins() widget.adjustSize(); QApplication::processEvents(); - for (int pi = 0; pi < expectedpositions.count(); ++pi) { + for (int pi = 0; pi < expectedpositions.size(); ++pi) { QCOMPARE(sizehinters.at(pi)->pos(), expectedpositions.at(pi)); } } @@ -1441,7 +1421,7 @@ void tst_QGridLayout::layoutSpacing() QLayout *layout = widget->layout(); QVERIFY(layout); - for (int pi = 0; pi < expectedpositions.count(); ++pi) { + for (int pi = 0; pi < expectedpositions.size(); ++pi) { QLayoutItem *item = layout->itemAt(pi); //qDebug() << item->widget()->pos(); QCOMPARE(item->widget()->pos(), expectedpositions.at(pi)); @@ -1541,7 +1521,7 @@ void tst_QGridLayout::spacerWithSpacing() QWidget window; QGridLayout layout(&window); layout.setSpacing(1); - layout.setMargin(0); + layout.setContentsMargins(0, 0, 0, 0); populate(&layout, 0, i); populate(&layout, 1, j); populate(&layout, 2, k); @@ -1651,7 +1631,7 @@ void tst_QGridLayout::taskQTBUG_52357_spacingWhenItemIsHidden() QWidget widget; setFrameless(&widget); QGridLayout layout(&widget); - layout.setMargin(0); + layout.setContentsMargins(0, 0, 0, 0); layout.setSpacing(5); QPushButton button1; layout.addWidget(&button1, 0, 0); @@ -1666,6 +1646,56 @@ void tst_QGridLayout::taskQTBUG_52357_spacingWhenItemIsHidden() QTRY_COMPARE_WITH_TIMEOUT(tempWidth, button1.width() + button3.width() + layout.spacing(), 1000); } +void tst_QGridLayout::taskQTBUG_91261_itemIndexRange() +{ + QWidget widget; + QGridLayout lay(&widget); + QPushButton *btn = new QPushButton(&widget); + lay.addWidget(btn, 0, 0); + + { + auto ptr = lay.itemAt(-1); + QCOMPARE(ptr, nullptr); + + ptr = lay.itemAt(0); + QCOMPARE(ptr->widget(), btn); + + ptr = lay.itemAt(1); + QCOMPARE(ptr, nullptr); + } + + { + int row = -1; + int column = -1; + int rowSpan; + int columnSpan; + + lay.getItemPosition(-1, &row, &column, &rowSpan, &columnSpan); + QCOMPARE(row, -1); + QCOMPARE(column, -1); + + lay.getItemPosition(1, &row, &column, &rowSpan, &columnSpan); + QCOMPARE(row, -1); + QCOMPARE(column, -1); + + lay.getItemPosition(0, &row, &column, &rowSpan, &columnSpan); + QCOMPARE(row, 0); + QCOMPARE(column, 0); + } + + { + auto ptr = lay.takeAt(-1); + QCOMPARE(ptr, nullptr); + + ptr = lay.takeAt(1); + QCOMPARE(ptr, nullptr); + + ptr = lay.takeAt(0); + QCOMPARE(ptr->widget(), btn); + delete ptr; + } +} + void tst_QGridLayout::replaceWidget() { QWidget wdg; diff --git a/tests/auto/widgets/kernel/qlayout/CMakeLists.txt b/tests/auto/widgets/kernel/qlayout/CMakeLists.txt new file mode 100644 index 0000000000..6bda750c0f --- /dev/null +++ b/tests/auto/widgets/kernel/qlayout/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qlayout Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qlayout LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +# Collect test data +file(GLOB_RECURSE test_data_glob + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + baseline/*) +list(APPEND test_data ${test_data_glob}) + +qt_internal_add_test(tst_qlayout + SOURCES + tst_qlayout.cpp + LIBRARIES + Qt::Gui + Qt::TestPrivate + Qt::Widgets + Qt::WidgetsPrivate + TESTDATA ${test_data} +) diff --git a/tests/auto/widgets/kernel/qlayout/qlayout.pro b/tests/auto/widgets/kernel/qlayout/qlayout.pro deleted file mode 100644 index e768e19a26..0000000000 --- a/tests/auto/widgets/kernel/qlayout/qlayout.pro +++ /dev/null @@ -1,12 +0,0 @@ -CONFIG += testcase -TARGET = tst_qlayout - -QT += widgets widgets-private testlib testlib-private - -SOURCES += tst_qlayout.cpp -TESTDATA += baseline/* - -android:!android-embedded { - RESOURCES += \ - testdata.qrc -} diff --git a/tests/auto/widgets/kernel/qlayout/testdata.qrc b/tests/auto/widgets/kernel/qlayout/testdata.qrc deleted file mode 100644 index 24e8e56263..0000000000 --- a/tests/auto/widgets/kernel/qlayout/testdata.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<RCC> - <qresource prefix="/"> - <file>baseline/smartmaxsize</file> - </qresource> -</RCC> diff --git a/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp b/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp index 936f581d89..bd170ca8ab 100644 --- a/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp +++ b/tests/auto/widgets/kernel/qlayout/tst_qlayout.cpp @@ -1,35 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <qcoreapplication.h> +#include <qmetaobject.h> #include <qdebug.h> #include <qboxlayout.h> #include <qmenubar.h> @@ -69,6 +45,7 @@ private slots: void controlTypes2(); void adjustSizeShouldMakeSureLayoutIsActivated(); void testRetainSizeWhenHidden(); + void removeWidget(); }; tst_QLayout::tst_QLayout() @@ -104,8 +81,8 @@ public: void setSizeHint(const QSize &s) { sh = s; } - QSize sizeHint() const { return sh; } - QSize minimumSizeHint() const { return msh; } + QSize sizeHint() const override { return sh; } + QSize minimumSizeHint() const override { return msh; } private: QSize sh; @@ -140,7 +117,7 @@ void tst_QLayout::geometry() void tst_QLayout::smartMaxSize() { - QVector<int> expectedWidths; + QList<int> expectedWidths; QFile f(QFINDTESTDATA("baseline/smartmaxsize")); @@ -163,7 +140,7 @@ void tst_QLayout::smartMaxSize() QSizePolicy::MinimumExpanding, QSizePolicy::Ignored }; - Qt::Alignment alignments[] = { 0, + Qt::Alignment alignments[] = { Qt::Alignment{}, Qt::AlignLeft, Qt::AlignRight, Qt::AlignHCenter @@ -221,13 +198,13 @@ class MyLayout : public QLayout { public: MyLayout() : invalidated(false) {} - virtual void invalidate() {invalidated = true;} + virtual void invalidate() override {invalidated = true;} bool invalidated; - QSize sizeHint() const {return QSize();} - void addItem(QLayoutItem*) {} - QLayoutItem* itemAt(int) const {return 0;} - QLayoutItem* takeAt(int) {return 0;} - int count() const {return 0;} + QSize sizeHint() const override {return QSize();} + void addItem(QLayoutItem*) override {} + QLayoutItem* itemAt(int) const override {return 0;} + QLayoutItem* takeAt(int) override {return 0;} + int count() const override {return 0;} }; void tst_QLayout::setContentsMargins() @@ -248,12 +225,28 @@ void tst_QLayout::setContentsMargins() layout.setContentsMargins(52, 53, 54, 55); QVERIFY(!layout.invalidated); + + MyLayout otherLayout; // with default contents margins + QVERIFY(layout.contentsMargins() != otherLayout.contentsMargins()); + layout.unsetContentsMargins(); + QCOMPARE(layout.contentsMargins(), otherLayout.contentsMargins()); + + layout.setContentsMargins(10, 20, 30, 40); + QVERIFY(layout.contentsMargins() != otherLayout.contentsMargins()); + + int contentsMarginsPropertyIndex = QLayout::staticMetaObject.indexOfProperty("contentsMargins"); + QVERIFY(contentsMarginsPropertyIndex >= 0); + QMetaProperty contentsMarginsProperty = QLayout::staticMetaObject.property(contentsMarginsPropertyIndex); + QVERIFY(contentsMarginsProperty.isValid()); + QVERIFY(contentsMarginsProperty.isResettable()); + QVERIFY(contentsMarginsProperty.reset(&layout)); + QCOMPARE(layout.contentsMargins(), otherLayout.contentsMargins()); } class EventReceiver : public QObject { public: - bool eventFilter(QObject *watched, QEvent *event) + bool eventFilter(QObject *watched, QEvent *event) override { if (event->type() == QEvent::Show) { geom = static_cast<QWidget*>(watched)->geometry(); @@ -308,7 +301,7 @@ void tst_QLayout::controlTypes2() { QWidget main; QVBoxLayout *const layout = new QVBoxLayout(&main); - layout->setMargin(0); + layout->setContentsMargins(0, 0, 0, 0); QComboBox *combo = new QComboBox(&main); layout->addWidget(combo); QCOMPARE(layout->controlTypes(), QSizePolicy::ComboBox); @@ -319,7 +312,7 @@ void tst_QLayout::adjustSizeShouldMakeSureLayoutIsActivated() QWidget main; QVBoxLayout *const layout = new QVBoxLayout(&main); - layout->setMargin(0); + layout->setContentsMargins(0, 0, 0, 0); SizeHinterFrame *frame = new SizeHinterFrame(QSize(200, 10), QSize(200, 8)); frame->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); layout->addWidget(frame); @@ -337,7 +330,7 @@ void tst_QLayout::adjustSizeShouldMakeSureLayoutIsActivated() void tst_QLayout::testRetainSizeWhenHidden() { -#if (defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)) || defined(Q_OS_WINRT) +#ifdef Q_OS_ANDROID QSKIP("Test does not work on platforms which default to showMaximized()"); #endif @@ -381,5 +374,34 @@ void tst_QLayout::testRetainSizeWhenHidden() QCOMPARE(widget.sizeHint().height(), normalHeight); } +void tst_QLayout::removeWidget() +{ + QHBoxLayout layout; + QCOMPARE(layout.count(), 0); + std::unique_ptr<QWidget> w(new QWidget); + layout.addWidget(w.get()); + QCOMPARE(layout.count(), 1); + layout.removeWidget(w.get()); + QCOMPARE(layout.count(), 0); + + QPointer<QLayout> childLayout(new QHBoxLayout); + layout.addLayout(childLayout); + QCOMPARE(layout.count(), 1); + + layout.removeWidget(nullptr); + QCOMPARE(layout.count(), 1); + + layout.removeItem(childLayout); + QCOMPARE(layout.count(), 0); + + QVERIFY(!childLayout.isNull()); + + // Test inactive layout consumes ChildRemoved event (QTBUG-124151) + layout.addWidget(w.get()); + layout.setEnabled(false); + w.reset(); + layout.setEnabled(true); +} + QTEST_MAIN(tst_QLayout) #include "tst_qlayout.moc" diff --git a/tests/auto/widgets/kernel/qshortcut/CMakeLists.txt b/tests/auto/widgets/kernel/qshortcut/CMakeLists.txt new file mode 100644 index 0000000000..517286f324 --- /dev/null +++ b/tests/auto/widgets/kernel/qshortcut/CMakeLists.txt @@ -0,0 +1,38 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qshortcut Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qshortcut LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qshortcut + SOURCES + tst_qshortcut.cpp + INCLUDE_DIRECTORIES + .. + LIBRARIES + Qt::Gui + Qt::GuiPrivate + Qt::Widgets + Qt::WidgetsPrivate +) + +qt_internal_add_test(tst_qguishortcut_with_qapplication + SOURCES + ../../../gui/kernel/qshortcut/tst_qshortcut.cpp + DEFINES + tst_QShortcut=tst_QGuiShortcutWithQApplication + INCLUDE_DIRECTORIES + .. + LIBRARIES + Qt::Gui + Qt::GuiPrivate + Qt::Widgets + Qt::WidgetsPrivate +) diff --git a/tests/auto/widgets/kernel/qshortcut/qshortcut.pro b/tests/auto/widgets/kernel/qshortcut/qshortcut.pro deleted file mode 100644 index 043d062d60..0000000000 --- a/tests/auto/widgets/kernel/qshortcut/qshortcut.pro +++ /dev/null @@ -1,10 +0,0 @@ -CONFIG += testcase -TARGET = tst_qshortcut - -# Project Configuration ---------------------------------------------- -INCLUDEPATH += ../ - -# Normal Test Files -------------------------------------------------- -QT += widgets testlib -HEADERS += -SOURCES += tst_qshortcut.cpp diff --git a/tests/auto/widgets/kernel/qshortcut/tst_qshortcut.cpp b/tests/auto/widgets/kernel/qshortcut/tst_qshortcut.cpp index 2c9295d995..d34df43b01 100644 --- a/tests/auto/widgets/kernel/qshortcut/tst_qshortcut.cpp +++ b/tests/auto/widgets/kernel/qshortcut/tst_qshortcut.cpp @@ -1,33 +1,12 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> +#include <QStringList> +#include <QStringListModel> +#include <QSignalSpy> + #include <qapplication.h> #include <qtextedit.h> #include <qlineedit.h> @@ -41,25 +20,84 @@ #include <qshortcut.h> #include <qscreen.h> -class AccelForm; +#include <QtGui/private/qguiapplication_p.h> +#include <QtGui/qpa/qplatformintegration.h> + +#include <QtWidgets/private/qapplication_p.h> + QT_BEGIN_NAMESPACE class QMainWindow; class QTextEdit; QT_END_NAMESPACE -class tst_QShortcut : public QObject +class TestEdit : public QTextEdit { Q_OBJECT public: - tst_QShortcut(); - virtual ~tst_QShortcut(); + 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<QKeyEvent*>(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<QKeyEvent*>(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("<Ctrl+") + c + QLatin1Char('>')); + else if (ke->modifiers() == Qt::AltModifier) + insertPlainText(QLatin1String("<Alt+") + c + QLatin1Char('>')); + else if (ke->modifiers() == Qt::ShiftModifier) + insertPlainText(QLatin1String("<Shift+") + c + QLatin1Char('>')); + 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 +public: enum Action { SetupAccel, TestAccel, - ClearAll - } currentAction; + ClearAll, + TestEnd + }; + Q_ENUM(Action) enum Widget { NoWidget, @@ -70,8 +108,8 @@ public: TriggerSlot5, TriggerSlot6, TriggerSlot7, - SendKeyEvent }; + Q_ENUM(Widget) enum Result { NoResult, @@ -84,7 +122,8 @@ public: Slot7Triggered, SentKeyEvent, Ambiguous - } currentResult; + } currentResult = NoResult; + Q_ENUM(Result) public slots: void slotTrig1() { currentResult = Slot1Triggered; } @@ -101,16 +140,10 @@ public slots: void ambigSlot5() { currentResult = Ambiguous; ambigResult = Slot5Triggered; } void ambigSlot6() { currentResult = Ambiguous; ambigResult = Slot6Triggered; } void ambigSlot7() { currentResult = Ambiguous; ambigResult = Slot7Triggered; } - void statusMessage( const QString& message ) { sbText = message; } - void shortcutDestroyed(QObject* obj); - void sendKeyEvent() { sendKeyEvents(edit, Qt::CTRL + Qt::Key_B, 0); currentResult = SentKeyEvent; } - -public slots: - void initTestCase(); - void cleanupTestCase(); - void cleanup() { QCOMPARE(QApplication::topLevelWidgets().size(), 1); } private slots: + void cleanup(); + void pmf_connect(); void number_data(); void number(); void text_data(); @@ -123,107 +156,81 @@ private slots: void context(); void duplicatedShortcutOverride(); void shortcutToFocusProxy(); + void deleteLater(); + void keys(); protected: static Qt::KeyboardModifiers toButtons( int key ); void defElements(); - void clearAllShortcuts(); - QShortcut *setupShortcut(int testWidget, const QKeySequence &ks); - QShortcut *setupShortcut(int testWidget, const QString &txt, int k1 = 0, int k2 = 0, int k3 = 0, int k4 = 0); + QShortcut *setupShortcut(QWidget *parent, const QString &name, const QKeySequence &ks, + Qt::ShortcutContext context = Qt::WindowShortcut); + QShortcut *setupShortcut(QWidget *parent, const QString &name, Widget testWidget, + const QKeySequence &ks, Qt::ShortcutContext context = Qt::WindowShortcut); - QShortcut *setupShortcut(QWidget *parent, const char *name, int testWidget, const QString &txt, int k1 = 0, int k2 = 0, int k3 = 0, int k4 = 0); - QShortcut *setupShortcut(QWidget *parent, const char *name, int testWidget, const QKeySequence &ks, Qt::ShortcutContext context = Qt::WindowShortcut); - - void sendKeyEvents(QWidget *w, int k1, QChar c1 = 0, int k2 = 0, QChar c2 = 0, int k3 = 0, QChar c3 = 0, int k4 = 0, QChar c4 = 0); - void sendKeyEvents(int k1, QChar c1 = 0, int k2 = 0, QChar c2 = 0, int k3 = 0, QChar c3 = 0, int k4 = 0, QChar c4 = 0); + static void sendKeyEvents(QWidget *w, int k1, QChar c1 = {}, int k2 = 0, QChar c2 = {}, + int k3 = 0, QChar c3 = {}, int k4 = 0, QChar c4 = {}); + void sendKeyEvents(QWidget *w, QKeyCombination k1, QChar c1 = {}) + { sendKeyEvents(w, k1.toCombined(), c1); } void testElement(); - QMainWindow *mainW; - QList<QShortcut*> shortcuts; - QTextEdit *edit; - QString sbText; Result ambigResult; + QScopedPointer<MainWindow> mainWindow; }; -Q_DECLARE_METATYPE(tst_QShortcut::Widget); -Q_DECLARE_METATYPE(tst_QShortcut::Result); -Q_DECLARE_METATYPE(tst_QShortcut::Action); - -class TestEdit : public QTextEdit -{ - Q_OBJECT -public: - TestEdit(QWidget *parent, const char *name) - : QTextEdit(parent) - { - setObjectName(name); - } - -protected: - bool event(QEvent *e) { - // Make testedit allow any Ctrl+Key as shortcut - if (e->type() == QEvent::ShortcutOverride) { - QKeyEvent *ke = static_cast<QKeyEvent*>(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<QKeyEvent*>(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("<Ctrl+") + c + QLatin1Char('>')); - else if (ke->modifiers() == Qt::AltModifier) - insertPlainText(QLatin1String("<Alt+") + c + QLatin1Char('>')); - else if (ke->modifiers() == Qt::ShiftModifier) - insertPlainText(QLatin1String("<Shift+") + c + QLatin1Char('>')); - return true; - } - } - return QTextEdit::event(e); - } -}; - -tst_QShortcut::tst_QShortcut(): mainW( 0 ) +MainWindow::MainWindow() { + setWindowFlags(Qt::X11BypassWindowManagerHint); + m_testEdit = new TestEdit(this, "test_edit"); + setCentralWidget(m_testEdit); + setFixedSize(200, 200); } -tst_QShortcut::~tst_QShortcut() +void tst_QShortcut::cleanup() { - clearAllShortcuts(); + QVERIFY(QApplication::topLevelWidgets().size() <= 1); // The data driven tests keep a widget around } -void tst_QShortcut::initTestCase() +void tst_QShortcut::pmf_connect() { - currentResult = NoResult; - mainW = new QMainWindow(0); - mainW->setWindowFlags(Qt::X11BypassWindowManagerHint); - edit = new TestEdit(mainW, "test_edit"); - mainW->setFixedSize( 200, 200 ); - mainW->setCentralWidget( edit ); - mainW->show(); - mainW->activateWindow(); - QVERIFY(QTest::qWaitForWindowActive(mainW)); - connect( mainW->statusBar(), SIGNAL(messageChanged(QString)), - this, SLOT(statusMessage(QString)) ); -} + class MyObject : public QObject + { + public: + using QObject::QObject; + void onActivated() { ++activated; } + void onAmbiguous() { ++ambiguous; } + void reset() { activated = 0; ambiguous = 0; } + int activated = 0; + int ambiguous = 0; + } myObject; + QWidget parent; + + auto runCheck = [&myObject](QShortcut *sc, int activated, int ambiguous) + { + myObject.reset(); + sc->activated(); + sc->activatedAmbiguously(); + delete sc; + QCOMPARE(myObject.activated, activated); + QCOMPARE(myObject.ambiguous, ambiguous); + }; -void tst_QShortcut::cleanupTestCase() -{ - delete mainW; + runCheck(new QShortcut(QKeySequence(), &parent, + [&myObject]() { ++myObject.activated; }), + 1, 0); + runCheck(new QShortcut(QKeySequence(), &parent, + &myObject, &MyObject::onActivated), + 1, 0); + runCheck(new QShortcut(QKeySequence(), &parent, + &myObject, &MyObject::onActivated, &MyObject::onAmbiguous), + 1, 1); + runCheck(new QShortcut(QKeySequence(), &parent, &myObject, + &MyObject::onActivated, &myObject, &MyObject::onAmbiguous), + 1, 1); } + Qt::KeyboardModifiers tst_QShortcut::toButtons( int key ) { Qt::KeyboardModifiers result = Qt::NoModifier; @@ -268,6 +275,10 @@ void tst_QShortcut::text() { testElement(); } + +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED + // ------------------------------------------------------------------ // Number Elements -------------------------------------------------- // ------------------------------------------------------------------ @@ -276,7 +287,7 @@ void tst_QShortcut::number_data() defElements(); // Clear all - QTest::newRow("N00 - clear") << ClearAll << NoWidget <<QString("")<<0<<0<<0<<0<<0<<0<<0<<0<<NoResult; + QTest::newRow("N00 - clear") << ClearAll << NoWidget <<QString()<<0<<0<<0<<0<<0<<0<<0<<0<<NoResult; //=========================================== // [Shift + key] on non-shift shortcuts testing @@ -288,15 +299,15 @@ void tst_QShortcut::number_data() Shift + Qt::Key_Plus on Qt::Key_Pluss Qt::Key_Plus on Qt::Key_Pluss */ - QTest::newRow("N001 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N001 - slot1") << SetupAccel << TriggerSlot1 << QString() << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; //commented out because the behaviour changed, those tests should be updated - //QTest::newRow("N001:Shift + M - [M]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_M) << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N001:M - [M]") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N001 - slot2") << SetupAccel << TriggerSlot2 << QString("") << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + //QTest::newRow("N001:Shift + M - [M]") << TestAccel << NoWidget << QString() << int(Qt::SHIFT + Qt::Key_M) << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N001:M - [M]") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N001 - slot2") << SetupAccel << TriggerSlot2 << QString() << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; //commented out because the behaviour changed, those tests should be updated - //QTest::newRow("N001:Shift++ [+]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("N001:+ [+]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("N001 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + //QTest::newRow("N001:Shift++ [+]") << TestAccel << NoWidget << QString() << int(Qt::SHIFT + Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("N001:+ [+]") << TestAccel << NoWidget << QString() << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("N001 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Shift + Qt::Key_M on Shift + Qt::Key_M @@ -304,32 +315,32 @@ void tst_QShortcut::number_data() Shift + Qt::Key_Plus on Shift + Qt::Key_Pluss Qt::Key_Plus on Shift + Qt::Key_Pluss */ - QTest::newRow("N002 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::SHIFT + Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N002:Shift+M - [Shift+M]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_M) << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N002:M - [Shift+M]") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N002 - slot2") << SetupAccel << TriggerSlot2 << QString("") << int(Qt::SHIFT + Qt::Key_Plus) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N002:Shift++ [Shift++]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("N002:+ [Shift++]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N002 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("N002 - slot1") << SetupAccel << TriggerSlot1 << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_M).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N002:Shift+M - [Shift+M]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_M).toCombined() << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N002:M - [Shift+M]") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N002 - slot2") << SetupAccel << TriggerSlot2 << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Plus).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N002:Shift++ [Shift++]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Plus).toCombined() << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("N002:+ [Shift++]") << TestAccel << NoWidget << QString() << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N002 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Shift + Qt::Key_F1 on Qt::Key_F1 Qt::Key_F1 on Qt::Key_F1 */ - QTest::newRow("N003 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N003 - slot1") << SetupAccel << TriggerSlot1 << QString() << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; //commented out because the behaviour changed, those tests should be updated - //QTest::newRow("N003:Shift+F1 - [F1]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N003:F1 - [F1]") << TestAccel << NoWidget << QString("") << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N003 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + //QTest::newRow("N003:Shift+F1 - [F1]") << TestAccel << NoWidget << QString() << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N003:F1 - [F1]") << TestAccel << NoWidget << QString() << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N003 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Shift + Qt::Key_F1 on Shift + Qt::Key_F1 Qt::Key_F1 on Shift + Qt::Key_F1 */ - QTest::newRow("N004 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N004:Shift+F1 - [Shift+F1]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N004:F1 - [Shift+F1]") << TestAccel << NoWidget << QString("") << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N004 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("N004 - slot1") << SetupAccel << TriggerSlot1 << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_F1).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N004:Shift+F1 - [Shift+F1]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_F1).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N004:F1 - [Shift+F1]") << TestAccel << NoWidget << QString() << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N004 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Qt::Key_Tab on Qt::Key_Tab @@ -337,14 +348,14 @@ void tst_QShortcut::number_data() Qt::Key_Backtab on Qt::Key_Tab Shift + Qt::Key_Backtab on Qt::Key_Tab */ - QTest::newRow("N005a - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N005a:Tab - [Tab]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N005a - slot1") << SetupAccel << TriggerSlot1 << QString() << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N005a:Tab - [Tab]") << TestAccel << NoWidget << QString() << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; //commented out because the behaviour changed, those tests should be updated - //QTest::newRow("N005a:Shift+Tab - [Tab]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + //QTest::newRow("N005a:Shift+Tab - [Tab]") << TestAccel << NoWidget << QString() << int(Qt::SHIFT + Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; // (Shift+)BackTab != Tab, but Shift+BackTab == Shift+Tab - QTest::newRow("N005a:Backtab - [Tab]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N005a:Shift+Backtab - [Tab]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N005a - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("N005a:Backtab - [Tab]") << TestAccel << NoWidget << QString() << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N005a:Shift+Backtab - [Tab]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Backtab).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N005a - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Qt::Key_Tab on Shift + Qt::Key_Tab @@ -352,12 +363,12 @@ void tst_QShortcut::number_data() Qt::Key_Backtab on Shift + Qt::Key_Tab Shift + Qt::Key_Backtab on Shift + Qt::Key_Tab */ - QTest::newRow("N005b - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::SHIFT + Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N005b:Tab - [Shift+Tab]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N005b:Shift+Tab - [Shift+Tab]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N005b:BackTab - [Shift+Tab]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N005b:Shift+BackTab - [Shift+Tab]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N005b - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("N005b - slot1") << SetupAccel << TriggerSlot1 << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Tab).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N005b:Tab - [Shift+Tab]") << TestAccel << NoWidget << QString() << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N005b:Shift+Tab - [Shift+Tab]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Tab).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N005b:BackTab - [Shift+Tab]") << TestAccel << NoWidget << QString() << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N005b:Shift+BackTab - [Shift+Tab]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Backtab).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N005b - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Qt::Key_Tab on Qt::Key_Backtab @@ -365,15 +376,15 @@ void tst_QShortcut::number_data() Qt::Key_Backtab on Qt::Key_Backtab Shift + Qt::Key_Backtab on Qt::Key_Backtab */ - QTest::newRow("N006a - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N006a:Tab - [BackTab]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N006a - slot1") << SetupAccel << TriggerSlot1 << QString() << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N006a:Tab - [BackTab]") << TestAccel << NoWidget << QString() << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // This should work, since platform dependent code will transform the // Shift+Tab into a Shift+BackTab, which should trigger the shortcut - QTest::newRow("N006a:Shift+Tab - [BackTab]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; //XFAIL - QTest::newRow("N006a:BackTab - [BackTab]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N006a:Shift+Tab - [BackTab]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Tab).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; //XFAIL + QTest::newRow("N006a:BackTab - [BackTab]") << TestAccel << NoWidget << QString() << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; //commented out because the behaviour changed, those tests should be updated - //QTest::newRow("N006a:Shift+BackTab - [BackTab]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N006a - clear") << ClearAll << NoWidget<< QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + //QTest::newRow("N006a:Shift+BackTab - [BackTab]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Backtab).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N006a - clear") << ClearAll << NoWidget<< QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Qt::Key_Tab on Shift + Qt::Key_Backtab @@ -381,12 +392,12 @@ void tst_QShortcut::number_data() Qt::Key_Backtab on Shift + Qt::Key_Backtab Shift + Qt::Key_Backtab on Shift + Qt::Key_Backtab */ - QTest::newRow("N006b - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::SHIFT + Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N006b:Tab - [Shift+BackTab]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N006b:Shift+Tab - [Shift+BackTab]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N006b:BackTab - [Shift+BackTab]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N006b:Shift+BackTab - [Shift+BackTab]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; //XFAIL - QTest::newRow("N006b - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("N006b - slot1") << SetupAccel << TriggerSlot1 << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Backtab).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N006b:Tab - [Shift+BackTab]") << TestAccel << NoWidget << QString() << int(Qt::Key_Tab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N006b:Shift+Tab - [Shift+BackTab]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Tab).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N006b:BackTab - [Shift+BackTab]") << TestAccel << NoWidget << QString() << int(Qt::Key_Backtab) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N006b:Shift+BackTab - [Shift+BackTab]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Backtab).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; //XFAIL + QTest::newRow("N006b - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all //=========================================== // [Shift + key] and [key] on shortcuts with @@ -397,11 +408,11 @@ void tst_QShortcut::number_data() Qt::Key_F1 Shift + Qt::Key_F1 */ - QTest::newRow("N007 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N007 - slot2") << SetupAccel << TriggerSlot2 << QString("") << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N007:F1") << TestAccel << NoWidget << QString("") << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N007:Shift + F1") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("N007 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("N007 - slot1") << SetupAccel << TriggerSlot1 << QString() << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N007 - slot2") << SetupAccel << TriggerSlot2 << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_F1).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N007:F1") << TestAccel << NoWidget << QString() << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N007:Shift + F1") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_F1).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("N007 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Qt::Key_M @@ -409,51 +420,51 @@ void tst_QShortcut::number_data() Ctrl + Qt::Key_M Alt + Qt::Key_M */ - QTest::newRow("N01 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N02 - slot2") << SetupAccel << TriggerSlot2 << QString("") << int(Qt::SHIFT + Qt::Key_M) << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N03 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::CTRL + Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N04 - slot2") << SetupAccel << TriggerSlot2 << QString("") << int(Qt::ALT + Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N:Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N:Shift+Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_M) << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("N:Ctrl+Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::CTRL + Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N:Alt+Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::ALT + Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("N01 - slot1") << SetupAccel << TriggerSlot1 << QString() << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N02 - slot2") << SetupAccel << TriggerSlot2 << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_M).toCombined() << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N03 - slot1") << SetupAccel << TriggerSlot1 << QString() << QKeyCombination(Qt::CTRL, Qt::Key_M).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N04 - slot2") << SetupAccel << TriggerSlot2 << QString() << QKeyCombination(Qt::ALT, Qt::Key_M).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N:Qt::Key_M") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N:Shift+Qt::Key_M") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_M).toCombined() << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("N:Ctrl+Qt::Key_M") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::CTRL, Qt::Key_M).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N:Alt+Qt::Key_M") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::ALT, Qt::Key_M).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; /* Testing Single Sequence Ambiguity Qt::Key_M on shortcut2 */ - QTest::newRow("N05 - slot2") << SetupAccel << TriggerSlot2 << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N:Qt::Key_M on slot") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Ambiguous; - QTest::newRow("N05 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("N05 - slot2") << SetupAccel << TriggerSlot2 << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N:Qt::Key_M on slot") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Ambiguous; + QTest::newRow("N05 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Specialkeys Qt::Key_aring Qt::Key_Aring - Qt::UNICODE_ACCEL + Qt::Key_K + Qt::Key_K */ - QTest::newRow("N06 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::Key_Aring) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N07 - slot2") << SetupAccel << TriggerSlot2 << QString("") << int(Qt::SHIFT+Qt::Key_Aring) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N08 - slot2") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::UNICODE_ACCEL + Qt::Key_K) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N06 - slot1") << SetupAccel << TriggerSlot1 << QString() << int(Qt::Key_Aring) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N07 - slot2") << SetupAccel << TriggerSlot2 << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Aring).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N08 - slot2") << SetupAccel << TriggerSlot1 << QString() << int(Qt::Key_K) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N:Qt::Key_aring") << TestAccel << NoWidget << QString("") << int(Qt::Key_Aring) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N:Qt::Key_Aring") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT+Qt::Key_Aring) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("N:Qt::Key_aring - Text Form") << TestAccel << NoWidget << QString("") << 0 << 0xC5 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N:Qt::Key_Aring - Text Form") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT+0) << 0xC5 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("N:Qt::UNICODE_ACCEL + Qt::Key_K")<< TestAccel << NoWidget << QString("") << int(Qt::UNICODE_ACCEL + Qt::Key_K) << int('k') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N09 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("N:Qt::Key_aring") << TestAccel << NoWidget << QString() << int(Qt::Key_Aring) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N:Qt::Key_Aring") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Aring).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("N:Qt::Key_aring - Text Form") << TestAccel << NoWidget << QString() << 0 << 0xC5 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N:Qt::Key_Aring - Text Form") << TestAccel << NoWidget << QString() << int(Qt::SHIFT) << 0xC5 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("N:Qt::Qt::Key_K") << TestAccel << NoWidget << QString() << int(Qt::Key_K) << int('k') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N09 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Multiple Sequences Qt::Key_M Qt::Key_I, Qt::Key_M Shift+Qt::Key_I, Qt::Key_M */ - QTest::newRow("N10 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N11 - slot2") << SetupAccel << TriggerSlot2 << QString("") << int(Qt::Key_I) << 0 << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("N12 - slot1") << SetupAccel << TriggerSlot1 << QString("") << int(Qt::SHIFT + Qt::Key_I) << 0 << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << NoResult; - - QTest::newRow("N:Qt::Key_M (2)") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N:Qt::Key_I, Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::Key_I) << int('i') << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("N:Shift+Qt::Key_I, Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_I) << int('I') << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("N13 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("N10 - slot1") << SetupAccel << TriggerSlot1 << QString() << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N11 - slot2") << SetupAccel << TriggerSlot2 << QString() << int(Qt::Key_I) << 0 << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("N12 - slot1") << SetupAccel << TriggerSlot1 << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_I).toCombined() << 0 << int(Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << NoResult; + + QTest::newRow("N:Qt::Key_M (2)") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N:Qt::Key_I, Qt::Key_M") << TestAccel << NoWidget << QString() << int(Qt::Key_I) << int('i') << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("N:Shift+Qt::Key_I, Qt::Key_M") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_I).toCombined() << int('I') << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("N:end") << TestEnd << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; } // ------------------------------------------------------------------ @@ -463,7 +474,7 @@ void tst_QShortcut::text_data() { defElements(); // Clear all - QTest::newRow("T00 - clear") << ClearAll << NoWidget <<QString("")<<0<<0<<0<<0<<0<<0<<0<<0<< NoResult; + QTest::newRow("T00 - clear") << ClearAll << NoWidget <<QString()<<0<<0<<0<<0<<0<<0<<0<<0<< NoResult; //=========================================== // [Shift + key] on non-shift shortcuts testing @@ -477,13 +488,13 @@ void tst_QShortcut::text_data() */ QTest::newRow("T001 - slot1") << SetupAccel << TriggerSlot1 << QString("M") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; //commented out because the behaviour changed, those tests should be updated - //QTest::newRow("T001:Shift+M - [M]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_M) << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T001:M - [M]") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + //QTest::newRow("T001:Shift+M - [M]") << TestAccel << NoWidget << QString() << int(Qt::SHIFT + Qt::Key_M) << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T001:M - [M]") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; QTest::newRow("T001 - slot2") << SetupAccel << TriggerSlot2 << QString("+") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; //commented out because the behaviour changed, those tests should be updated - //QTest::newRow("T001:Shift++ [+]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("T001:+ [+]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("T001 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + //QTest::newRow("T001:Shift++ [+]") << TestAccel << NoWidget << QString() << int(Qt::SHIFT + Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("T001:+ [+]") << TestAccel << NoWidget << QString() << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("T001 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Shift + Qt::Key_M on Shift + Qt::Key_M @@ -494,12 +505,12 @@ void tst_QShortcut::text_data() Ctrl + Qt::Key_Plus on Ctrl + Qt::Key_Pluss */ QTest::newRow("T002 - slot1") << SetupAccel << TriggerSlot1 << QString("Shift+M") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T002:Shift+M - [Shift+M]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_M) << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T002:M - [Shift+M]") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("T002:Shift+M - [Shift+M]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_M).toCombined() << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T002:M - [Shift+M]") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; QTest::newRow("T002 - slot2") << SetupAccel << TriggerSlot2 << QString("Shift++") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T002:Shift++ [Shift++]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("T002:+ [Shift++]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T002 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("T002:Shift++ [Shift++]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Plus).toCombined() << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("T002:+ [Shift++]") << TestAccel << NoWidget << QString() << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("T002 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Shift + Ctrl + Qt::Key_Plus on Ctrl + Qt::Key_Plus @@ -508,10 +519,10 @@ void tst_QShortcut::text_data() */ QTest::newRow("T002b - slot1") << SetupAccel << TriggerSlot1 << QString("Ctrl++") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; //commented out because the behaviour changed, those tests should be updated - //QTest::newRow("T002b:Shift+Ctrl++ [Ctrl++]")<< TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::CTRL + Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T002b:Ctrl++ [Ctrl++]") << TestAccel << NoWidget << QString("") << int(Qt::CTRL + Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T002b:+ [Ctrl++]") << TestAccel << NoWidget << QString("") << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T002b - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + //QTest::newRow("T002b:Shift+Ctrl++ [Ctrl++]")<< TestAccel << NoWidget << QString() << int(Qt::SHIFT + Qt::CTRL + Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T002b:Ctrl++ [Ctrl++]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::CTRL, Qt::Key_Plus).toCombined() << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T002b:+ [Ctrl++]") << TestAccel << NoWidget << QString() << int(Qt::Key_Plus) << int('+') << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("T002b - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Shift + Qt::Key_F1 on Qt::Key_F1 @@ -519,18 +530,18 @@ void tst_QShortcut::text_data() */ QTest::newRow("T003 - slot1") << SetupAccel << TriggerSlot1 << QString("F1") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; //commented out because the behaviour changed, those tests should be updated - //QTest::newRow("T003:Shift+F1 - [F1]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T003:F1 - [F1]") << TestAccel << NoWidget << QString("") << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T003 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + //QTest::newRow("T003:Shift+F1 - [F1]") << TestAccel << NoWidget << QString() << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T003:F1 - [F1]") << TestAccel << NoWidget << QString() << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T003 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Shift + Qt::Key_F1 on Shift + Qt::Key_F1 Qt::Key_F1 on Shift + Qt::Key_F1 */ QTest::newRow("T004 - slot1") << SetupAccel << TriggerSlot1 << QString("Shift+F1") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T004:Shift+F1 - [Shift+F1]") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T004:F1 - [Shift+F1]") << TestAccel << NoWidget << QString("") << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T004 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("T004:Shift+F1 - [Shift+F1]") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_F1).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T004:F1 - [Shift+F1]") << TestAccel << NoWidget << QString() << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; + QTest::newRow("T004 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all //=========================================== // [Shift + key] and [key] on shortcuts with @@ -543,9 +554,9 @@ void tst_QShortcut::text_data() */ QTest::newRow("T007 - slot1") << SetupAccel << TriggerSlot1 << QString("F1") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; QTest::newRow("T007 - slot2") << SetupAccel << TriggerSlot2 << QString("Shift+F1") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T007:F1") << TestAccel << NoWidget << QString("") << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T007:Shift + F1") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("T007 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("T007:F1") << TestAccel << NoWidget << QString() << int(Qt::Key_F1) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T007:Shift + F1") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_F1).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("T007 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Sequences Qt::Key_M @@ -558,33 +569,33 @@ void tst_QShortcut::text_data() QTest::newRow("T03 - slot1") << SetupAccel << TriggerSlot1 << QString("Ctrl+M") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; QTest::newRow("T04 - slot2") << SetupAccel << TriggerSlot2 << QString("Alt+M") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T:Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T:Shift + Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_M) << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("T:Ctrl + Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::CTRL + Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T:Alt + Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::ALT + Qt::Key_M) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("T:Qt::Key_M") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T:Shift + Qt::Key_M") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_M).toCombined() << int('M') << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("T:Ctrl + Qt::Key_M") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::CTRL, Qt::Key_M).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T:Alt + Qt::Key_M") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::ALT, Qt::Key_M).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; /* Testing Single Sequence Ambiguity Qt::Key_M on shortcut2 */ QTest::newRow("T05 - slot2") << SetupAccel << TriggerSlot2 << QString("M") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T:Qt::Key_M on TriggerSlot2") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Ambiguous; - QTest::newRow("T06 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("T:Qt::Key_M on TriggerSlot2") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Ambiguous; + QTest::newRow("T06 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Single Specialkeys Qt::Key_aring Qt::Key_Aring - Qt::UNICODE_ACCEL + Qt::Key_K + Qt::Key_K */ /* see comments above on the #ifdef'ery */ QTest::newRow("T06 - slot1") << SetupAccel << TriggerSlot1 << QString::fromLatin1("\x0C5")<< 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; QTest::newRow("T07 - slot2") << SetupAccel << TriggerSlot2 << QString::fromLatin1("Shift+\x0C5")<< 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; QTest::newRow("T08 - slot2") << SetupAccel << TriggerSlot1 << QString("K") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T:Qt::Key_aring") << TestAccel << NoWidget << QString("") << int(Qt::Key_Aring) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T:Qt::Key_Aring") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT+Qt::Key_Aring) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("T:Qt::Key_aring - Text Form") << TestAccel << NoWidget << QString("") << 0 << 0xC5 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T:Qt::Key_Aring - Text Form") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT+0) << 0xC5 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("T:Qt::UNICODE_ACCEL + Qt::Key_K")<< TestAccel << NoWidget << QString("") << int(Qt::UNICODE_ACCEL + Qt::Key_K) << int('k') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T09 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("T:Qt::Key_aring") << TestAccel << NoWidget << QString() << int(Qt::Key_Aring) << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T:Qt::Key_Aring") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_Aring).toCombined() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("T:Qt::Key_aring - Text Form") << TestAccel << NoWidget << QString() << 0 << 0xC5 << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T:Qt::Key_Aring - Text Form") << TestAccel << NoWidget << QString() << int(Qt::SHIFT) << 0xC5 << 0 << 0 << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("T:Qt::Key_K") << TestAccel << NoWidget << QString() << int(Qt::Key_K) << int('k') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T09 - clear") << ClearAll << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all /* Testing Multiple Sequences Qt::Key_M @@ -594,10 +605,40 @@ void tst_QShortcut::text_data() QTest::newRow("T10 - slot1") << SetupAccel << TriggerSlot1 << QString("M") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; QTest::newRow("T11 - slot2") << SetupAccel << TriggerSlot2 << QString("I, M")<< 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; QTest::newRow("T12 - slot1") << SetupAccel << TriggerSlot1 << QString("Shift+I, M")<< 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; - QTest::newRow("T:Qt::Key_M (2)") << TestAccel << NoWidget << QString("") << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T:Qt::Key_I, Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::Key_I) << int('i') << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << Slot2Triggered; - QTest::newRow("T:Shift+Qt::Key_I, Qt::Key_M") << TestAccel << NoWidget << QString("") << int(Qt::SHIFT + Qt::Key_I) << int('I') << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << Slot1Triggered; - QTest::newRow("T13 - clear") << ClearAll << NoWidget << QString("") << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; // Clear all + QTest::newRow("T:Qt::Key_M (2)") << TestAccel << NoWidget << QString() << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T:Qt::Key_I, Qt::Key_M") << TestAccel << NoWidget << QString() << int(Qt::Key_I) << int('i') << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << Slot2Triggered; + QTest::newRow("T:Shift+Qt::Key_I, Qt::Key_M") << TestAccel << NoWidget << QString() << QKeyCombination(Qt::SHIFT, Qt::Key_I).toCombined() << int('I') << int(Qt::Key_M) << int('m') << 0 << 0 << 0 << 0 << Slot1Triggered; + QTest::newRow("T:end") << TestEnd << NoWidget << QString() << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << NoResult; +} + +QT_WARNING_POP + +class ButtonWidget : public QWidget +{ +public: + ButtonWidget(); + + QPushButton *pushButton1() const { return m_pb1; } + QPushButton *pushButton2() const { return m_pb2; } + +private: + QPushButton *m_pb1; + QPushButton *m_pb2; +}; + +ButtonWidget::ButtonWidget() +{ + // Setup two identical shortcuts on different pushbuttons + QString name = QLatin1String("pushbutton-1"); + m_pb1 = new QPushButton(name, this); + m_pb1->setObjectName(name); + name = QLatin1String("pushbutton-2"); + m_pb2 = new QPushButton(name, this); + m_pb2->setObjectName(name); + auto hLayout = new QHBoxLayout(this); + hLayout->addWidget(m_pb1); + hLayout->addWidget(m_pb2); + hLayout->addStretch(); } // ------------------------------------------------------------------ @@ -605,9 +646,14 @@ void tst_QShortcut::text_data() // ------------------------------------------------------------------ void tst_QShortcut::disabledItems() { - clearAllShortcuts(); - mainW->activateWindow(); - QVERIFY(QTest::qWaitForWindowActive(mainW)); + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + + ButtonWidget mainW; + mainW.setWindowTitle(QTest::currentTestFunction()); + mainW.show(); + mainW.activateWindow(); + QVERIFY(QTest::qWaitForWindowActive(&mainW)); /* Testing Disabled Shortcuts Qt::Key_M on slot1 @@ -617,27 +663,27 @@ void tst_QShortcut::disabledItems() */ // Setup two identical shortcuts on different pushbuttons - QPushButton pb1(mainW); - QPushButton pb2(mainW); - pb1.setObjectName("pushbutton-1"); - pb2.setObjectName("pushbutton-2"); - pb1.show(); // Must be show for QShortcutMap::correctSubWindow to trigger - pb2.show(); - - QShortcut *cut1 = setupShortcut(&pb1, "shortcut1-pb1", TriggerSlot1, "M"); - QShortcut *cut2 = setupShortcut(&pb1, "shortcut2-pb1", TriggerSlot1, "Shift+M"); - QShortcut *cut3 = setupShortcut(&pb2, "shortcut3-pb2", TriggerSlot2, "M"); - QShortcut *cut4 = setupShortcut(&pb2, "shortcut4-pb2", TriggerSlot2, "Shift+M"); + auto pb1 = mainW.pushButton1(); + auto pb2 = mainW.pushButton2(); + const QKeyCombination shiftM = Qt::SHIFT | Qt::Key_M; + QShortcut *cut1 = setupShortcut(pb1, "shortcut1-pb1", TriggerSlot1, + QKeySequence(Qt::Key_M)); + QShortcut *cut2 = setupShortcut(pb1, "shortcut2-pb1", TriggerSlot1, + QKeySequence(shiftM)); + QShortcut *cut3 = setupShortcut(pb2, "shortcut3-pb2", TriggerSlot2, + QKeySequence(Qt::Key_M)); + QShortcut *cut4 = setupShortcut(pb2, "shortcut4-pb2", TriggerSlot2, + QKeySequence(shiftM)); cut3->setEnabled(false); cut4->setEnabled(false); currentResult = NoResult; - sendKeyEvents(Qt::Key_M, 'm'); + sendKeyEvents(&mainW, Qt::Key_M, 'm'); QCOMPARE(currentResult, Slot1Triggered); currentResult = NoResult; - sendKeyEvents(Qt::SHIFT+Qt::Key_M, 'M'); + sendKeyEvents(&mainW, shiftM, 'M'); QCOMPARE(currentResult, Slot1Triggered); cut2->setEnabled(false); @@ -650,11 +696,11 @@ void tst_QShortcut::disabledItems() Shift + Qt::Key_M on slot2 */ currentResult = NoResult; - sendKeyEvents( Qt::Key_M, 'm' ); + sendKeyEvents(&mainW, Qt::Key_M, 'm' ); QCOMPARE( currentResult, Slot1Triggered ); currentResult = NoResult; - sendKeyEvents( Qt::SHIFT+Qt::Key_M, 'M' ); + sendKeyEvents(&mainW, shiftM, 'M' ); QCOMPARE( currentResult, Slot2Triggered ); @@ -662,67 +708,37 @@ void tst_QShortcut::disabledItems() Qt::Key_F5 on slot1 Shift + Qt::Key_F5 on slot2 (disabled) */ - clearAllShortcuts(); - cut1 = setupShortcut(&pb1, "shortcut1-pb1", TriggerSlot1, "F5"); - cut4 = setupShortcut(&pb2, "shortcut4-pb2", TriggerSlot2, "Shift+F5"); - - cut1->setKey(QKeySequence("F5")); - cut4->setKey(QKeySequence("Shift+F5")); + qDeleteAll(mainW.findChildren<QShortcut *>()); + const QKeyCombination shiftF5 = Qt::SHIFT | Qt::Key_F5; + cut1 = setupShortcut(pb1, "shortcut1-pb1", TriggerSlot1, QKeySequence(Qt::Key_F5)); + cut4 = setupShortcut(pb2, "shortcut4-pb2", TriggerSlot2, QKeySequence(shiftF5)); cut1->setEnabled(true); cut4->setEnabled(false); currentResult = NoResult; - sendKeyEvents( Qt::Key_F5, 0 ); + sendKeyEvents(&mainW, Qt::Key_F5); QCOMPARE( currentResult, Slot1Triggered ); currentResult = NoResult; - sendKeyEvents( Qt::SHIFT+Qt::Key_F5, 0 ); - QCOMPARE( currentResult, NoResult ); - -#if 0 - qFatal("Not testing statusbar text feedback yet, since not implemented"); - /* Testing Disabled Accel, and the corresponding statusbar feedback - Ctrl + Qt::Key_K, Ctrl + Qt::Key_L on slot1 - Ctrl + Qt::Key_K, Ctrl + Qt::Key_M on slot2 (disabled) - */ - cut1->setKey(QKeySequence("Ctrl+K, Ctrl+L")); - cut4->setKey(QKeySequence("Ctrl+K, Ctrl+M")); - - cut1->setEnabled(true); - cut4->setEnabled(false); - - currentResult = NoResult; - sendKeyEvents( Qt::CTRL+Qt::Key_K, 0 ); - sendKeyEvents( Qt::CTRL+Qt::Key_Q, 0 ); - QCOMPARE( currentResult, NoResult ); - if (over_330) - QCOMPARE( sbText, QString("Ctrl+K, Ctrl+Q not defined") ); - - currentResult = NoResult; - sendKeyEvents( Qt::CTRL+Qt::Key_K, 0 ); - sendKeyEvents( Qt::CTRL+Qt::Key_M, 0 ); + sendKeyEvents(&mainW, shiftF5); QCOMPARE( currentResult, NoResult ); - if (over_330) - QCOMPARE( sbText, QString() ); - - currentResult = NoResult; - sendKeyEvents( Qt::CTRL+Qt::Key_K, 0 ); - sendKeyEvents( Qt::CTRL+Qt::Key_L, 0 ); - QCOMPARE( currentResult, Slot1Triggered ); - if (over_330) - QCOMPARE( sbText, QString() ); -#endif - clearAllShortcuts(); - cut1 = 0; - cut4 = 0; } // ------------------------------------------------------------------ // Ambiguous Elements ----------------------------------------------- // ------------------------------------------------------------------ void tst_QShortcut::ambiguousRotation() { - clearAllShortcuts(); + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + + MainWindow mainW; + const QString name = QLatin1String(QTest::currentTestFunction()); + mainW.setWindowTitle(name); + mainW.show(); + mainW.activateWindow(); + QVERIFY(QTest::qWaitForWindowActive(&mainW)); + /* Testing Shortcut rotation scheme Ctrl + Qt::Key_A on slot1 (disabled) Ctrl + Qt::Key_A on slot2 (disabled) @@ -732,13 +748,15 @@ void tst_QShortcut::ambiguousRotation() Ctrl + Qt::Key_A on slot6 Ctrl + Qt::Key_A on slot7 (disabled) */ - QShortcut *cut1 = setupShortcut(TriggerSlot1, "Ctrl+A"); - QShortcut *cut2 = setupShortcut(TriggerSlot2, "Ctrl+A"); - QShortcut *cut3 = setupShortcut(TriggerSlot3, "Ctrl+A"); - QShortcut *cut4 = setupShortcut(TriggerSlot4, "Ctrl+A"); - QShortcut *cut5 = setupShortcut(TriggerSlot5, "Ctrl+A"); - QShortcut *cut6 = setupShortcut(TriggerSlot6, "Ctrl+A"); - QShortcut *cut7 = setupShortcut(TriggerSlot7, "Ctrl+A"); + const QKeyCombination ctrlA = Qt::CTRL | Qt::Key_A; + QKeySequence ctrlA_Sequence(ctrlA); + QShortcut *cut1 = setupShortcut(&mainW, name, TriggerSlot1, ctrlA_Sequence); + QShortcut *cut2 = setupShortcut(&mainW, name, TriggerSlot2, ctrlA_Sequence); + QShortcut *cut3 = setupShortcut(&mainW, name, TriggerSlot3, ctrlA_Sequence); + QShortcut *cut4 = setupShortcut(&mainW, name, TriggerSlot4, ctrlA_Sequence); + QShortcut *cut5 = setupShortcut(&mainW, name, TriggerSlot5, ctrlA_Sequence); + QShortcut *cut6 = setupShortcut(&mainW, name, TriggerSlot6, ctrlA_Sequence); + QShortcut *cut7 = setupShortcut(&mainW, name, TriggerSlot7, ctrlA_Sequence); cut1->setEnabled(false); cut2->setEnabled(false); @@ -752,37 +770,37 @@ void tst_QShortcut::ambiguousRotation() // Continue... currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot3Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot4Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot6Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot3Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot4Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot6Triggered); @@ -807,94 +825,90 @@ void tst_QShortcut::ambiguousRotation() currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot1Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot2Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot5Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot7Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot1Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot2Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot5Triggered); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(Qt::CTRL+Qt::Key_A); + sendKeyEvents(&mainW, ctrlA); QCOMPARE(currentResult, Ambiguous); QCOMPARE(ambigResult, Slot7Triggered); - - clearAllShortcuts(); - cut1 = 0; cut2 = 0; - cut3 = 0; cut4 = 0; - cut5 = 0; cut6 = 0; - cut7 = 0; } void tst_QShortcut::ambiguousItems() { - clearAllShortcuts(); + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + + ButtonWidget mainW; + mainW.setWindowTitle(QTest::currentTestFunction()); + mainW.show(); + mainW.activateWindow(); + QVERIFY(QTest::qWaitForWindowActive(&mainW)); + /* Testing Ambiguous Shortcuts Qt::Key_M on Pushbutton 1 Qt::Key_M on Pushbutton 2 */ // Setup two identical shortcuts on different pushbuttons - QPushButton pb1(mainW); - QPushButton pb2(mainW); - pb1.setObjectName("pushbutton-1"); - pb2.setObjectName("pushbutton-2"); - pb1.show(); // Must be show for QShortcutMap::correctSubWindow to trigger - pb2.show(); + auto pb1 = mainW.pushButton1(); + auto pb2 = mainW.pushButton2(); - setupShortcut(&pb1, "shortcut1-pb1", TriggerSlot1, "M"); - setupShortcut(&pb1, "shortcut2-pb2", TriggerSlot2, "M"); + setupShortcut(pb1, "shortcut1-pb1", TriggerSlot1, QKeySequence(Qt::Key_M)); + setupShortcut(pb2, "shortcut2-pb2", TriggerSlot2, QKeySequence(Qt::Key_M)); currentResult = NoResult; - sendKeyEvents( Qt::Key_M, 'm' ); + sendKeyEvents(&mainW, Qt::Key_M, 'm' ); QCOMPARE( currentResult, Ambiguous ); QCOMPARE( ambigResult, Slot1Triggered ); currentResult = NoResult; - sendKeyEvents( Qt::Key_M, 'm' ); + sendKeyEvents(&mainW, Qt::Key_M, 'm' ); QCOMPARE( currentResult, Ambiguous ); QCOMPARE( ambigResult, Slot2Triggered ); currentResult = NoResult; - sendKeyEvents( Qt::Key_M, 'm' ); + sendKeyEvents(&mainW, Qt::Key_M, 'm' ); QCOMPARE( currentResult, Ambiguous ); QCOMPARE( ambigResult, Slot1Triggered ); - - clearAllShortcuts(); } @@ -903,32 +917,34 @@ void tst_QShortcut::ambiguousItems() // ------------------------------------------------------------------ void tst_QShortcut::unicodeCompare() { - clearAllShortcuts(); + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + + ButtonWidget mainW; + mainW.setWindowTitle(QTest::currentTestFunction()); + mainW.show(); + mainW.activateWindow(); + QVERIFY(QTest::qWaitForWindowActive(&mainW)); + /* Testing Unicode/non-Unicode Shortcuts Qt::Key_M on Pushbutton 1 Qt::Key_M on Pushbutton 2 */ - QPushButton pb1(mainW); - QPushButton pb2(mainW); - pb1.setObjectName("pushbutton-1"); - pb2.setObjectName("pushbutton-2"); - pb1.show(); // Must be show for QShortcutMap::correctSubWindow to trigger - pb2.show(); + auto pb1 = mainW.pushButton1(); + auto pb2 = mainW.pushButton2(); QKeySequence ks1("Ctrl+M"); // Unicode - QKeySequence ks2(Qt::CTRL+Qt::Key_M); // non-Unicode - setupShortcut(&pb1, "shortcut1-pb1", TriggerSlot1, ks1); - setupShortcut(&pb1, "shortcut2-pb2", TriggerSlot2, ks2); + QKeySequence ks2(Qt::CTRL | Qt::Key_M); // non-Unicode + setupShortcut(pb1, "shortcut1-pb1", TriggerSlot1, ks1); + setupShortcut(pb2, "shortcut2-pb2", TriggerSlot2, ks2); currentResult = NoResult; - sendKeyEvents( Qt::CTRL+Qt::Key_M, 0 ); + sendKeyEvents(&mainW, Qt::CTRL | Qt::Key_M); QCOMPARE( currentResult, Ambiguous ); // They _are_ ambiguous, so the QKeySequence operator== // should indicate the same QVERIFY( ks1 == ks2 ); QVERIFY( !(ks1 != ks2) ); - - clearAllShortcuts(); } // ------------------------------------------------------------------ @@ -936,25 +952,32 @@ void tst_QShortcut::unicodeCompare() // ------------------------------------------------------------------ void tst_QShortcut::keypressConsumption() { - clearAllShortcuts(); - edit->clear(); - QCOMPARE(edit->toPlainText().size(), 0); + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); - QShortcut *cut1 = setupShortcut(edit, "shortcut1-line", TriggerSlot1, "Ctrl+I, A"); - QShortcut *cut2 = setupShortcut(edit, "shortcut1-line", TriggerSlot2, "Ctrl+I, B"); + MainWindow mainW; + mainW.setWindowTitle(QTest::currentTestFunction()); + mainW.show(); + mainW.activateWindow(); + QVERIFY(QTest::qWaitForWindowActive(&mainW)); + auto edit = mainW.testEdit(); + + QKeyCombination ctrlI = Qt::CTRL | Qt::Key_I; + QShortcut *cut1 = setupShortcut(edit, "shortcut1-line", TriggerSlot1, QKeySequence(ctrlI, QKeyCombination(Qt::Key_A))); + QShortcut *cut2 = setupShortcut(edit, "shortcut1-line", TriggerSlot2, QKeySequence(ctrlI, QKeyCombination(Qt::Key_B))); currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(edit, Qt::CTRL + Qt::Key_I, 0); // Send key to edit + sendKeyEvents(edit, ctrlI); // Send key to edit QCOMPARE( currentResult, NoResult ); QCOMPARE( ambigResult, NoResult ); - QCOMPARE(edit->toPlainText(), QString("")); + QCOMPARE(edit->toPlainText(), QString()); // Make sure next keypress is eaten (failing multiple keysequence) sendKeyEvents(edit, Qt::Key_C, 'c'); // Send key to edit QCOMPARE( currentResult, NoResult ); QCOMPARE( ambigResult, NoResult ); - QCOMPARE(edit->toPlainText(), QString("")); + QCOMPARE(edit->toPlainText(), QString()); // Next keypress should be normal sendKeyEvents(edit, Qt::Key_C, 'c'); // Send key to edit @@ -972,7 +995,7 @@ void tst_QShortcut::keypressConsumption() // Make sure keypresses is passed on, since all multiple keysequences // with Ctrl+I are disabled - sendKeyEvents(edit, Qt::CTRL + Qt::Key_I, 0); // Send key to edit + sendKeyEvents(edit, Qt::CTRL | Qt::Key_I); // Send key to edit QCOMPARE( currentResult, NoResult ); QCOMPARE( ambigResult, NoResult ); QVERIFY(edit->toPlainText().endsWith("<Ctrl+I>")); @@ -982,20 +1005,24 @@ void tst_QShortcut::keypressConsumption() QCOMPARE( ambigResult, NoResult ); QVERIFY(edit->toPlainText().endsWith("<Ctrl+I>a")); - clearAllShortcuts(); + qDeleteAll(mainW.findChildren<QShortcut *>()); edit->clear(); QCOMPARE(edit->toPlainText().size(), 0); - setupShortcut(edit, "first", SendKeyEvent, "Ctrl+A"); + auto cut = setupShortcut(edit, QLatin1String("first"), QKeySequence(Qt::CTRL | Qt::Key_A)); + connect(cut, &QShortcut::activated, edit, [this, edit] () { + this->sendKeyEvents(edit, Qt::CTRL | Qt::Key_B); + this->currentResult = tst_QShortcut::SentKeyEvent; + }); // Verify reentrancy when a non-shortcut is triggered as part // of shortcut processing. currentResult = NoResult; ambigResult = NoResult; - sendKeyEvents(edit, Qt::CTRL + Qt::Key_A, 0); + sendKeyEvents(edit, Qt::CTRL | Qt::Key_A); QCOMPARE(currentResult, SentKeyEvent); QCOMPARE(ambigResult, NoResult); - QCOMPARE(edit->toPlainText(), QString(QString("<Ctrl+B>"))); + QCOMPARE(edit->toPlainText(), QLatin1String("<Ctrl+B>")); } // ------------------------------------------------------------------ @@ -1003,12 +1030,17 @@ void tst_QShortcut::keypressConsumption() // ------------------------------------------------------------------ void tst_QShortcut::context() { - clearAllShortcuts(); + const QString name = QLatin1String(QTest::currentTestFunction()); + MainWindow mainW; + mainW.setWindowTitle(name + QLatin1String("_Helper")); + mainW.show(); + auto edit = mainW.testEdit(); QWidget myBox; - TestEdit *other1 = new TestEdit(&myBox, "test_edit_other1"); - TestEdit *other2 = new TestEdit(&myBox, "test_edit_other2"); - QHBoxLayout *layout = new QHBoxLayout(&myBox); + myBox.setWindowTitle(name); + auto other1 = new TestEdit(&myBox, "test_edit_other1"); + auto other2 = new TestEdit(&myBox, "test_edit_other2"); + auto layout = new QHBoxLayout(&myBox); layout->addWidget(other1); layout->addWidget(other2); myBox.show(); @@ -1032,9 +1064,8 @@ void tst_QShortcut::context() // Focus on 'other1' edit, so Active Window context should trigger other1->activateWindow(); // <--- - QApplication::setActiveWindow(other1); - QCOMPARE(qApp->activeWindow(), other1->window()); - QCOMPARE(qApp->focusWidget(), (QWidget *)other1); + QCOMPARE(QApplication::activeWindow(), other1->window()); + QCOMPARE(QApplication::focusWidget(), static_cast<QWidget *>(other1)); currentResult = NoResult; ambigResult = NoResult; @@ -1042,13 +1073,13 @@ void tst_QShortcut::context() other1->clear(); other2->clear(); - QCOMPARE(qApp->focusWidget(), (QWidget *)other1); - sendKeyEvents(other1, Qt::ALT+Qt::Key_1); + QCOMPARE(QApplication::focusWidget(), static_cast<QWidget *>(other1)); + sendKeyEvents(other1, Qt::ALT | Qt::Key_1); QCOMPARE(currentResult, Slot1Triggered); QCOMPARE(ambigResult, NoResult); - QCOMPARE(edit->toPlainText(), QString("")); - QCOMPARE(other1->toPlainText(), QString("")); - QCOMPARE(other2->toPlainText(), QString("")); + QCOMPARE(edit->toPlainText(), QString()); + QCOMPARE(other1->toPlainText(), QString()); + QCOMPARE(other2->toPlainText(), QString()); // ..but not Focus context on 'other2'.. currentResult = NoResult; @@ -1057,12 +1088,12 @@ void tst_QShortcut::context() other1->clear(); other2->clear(); - sendKeyEvents(other1, Qt::ALT+Qt::Key_2); + sendKeyEvents(other1, Qt::ALT | Qt::Key_2); QCOMPARE(currentResult, NoResult); QCOMPARE(ambigResult, NoResult); - QCOMPARE(edit->toPlainText(), QString("")); + QCOMPARE(edit->toPlainText(), QString()); QCOMPARE(other1->toPlainText(), QString("<Alt+2>")); - QCOMPARE(other2->toPlainText(), QString("")); + QCOMPARE(other2->toPlainText(), QString()); // ..however, application global context on 'edit' should.. currentResult = NoResult; @@ -1071,18 +1102,18 @@ void tst_QShortcut::context() other1->clear(); other2->clear(); - sendKeyEvents(other1, Qt::ALT+Qt::Key_3); + sendKeyEvents(other1, Qt::ALT | Qt::Key_3); QCOMPARE(currentResult, Slot3Triggered); QCOMPARE(ambigResult, NoResult); - QCOMPARE(edit->toPlainText(), QString("")); - QCOMPARE(other1->toPlainText(), QString("")); - QCOMPARE(other2->toPlainText(), QString("")); + QCOMPARE(edit->toPlainText(), QString()); + QCOMPARE(other1->toPlainText(), QString()); + QCOMPARE(other2->toPlainText(), QString()); // Changing focus to 'other2' should make the Focus context there work other2->activateWindow(); other2->setFocus(); // ### - QTRY_COMPARE(qApp->activeWindow(), other2->window()); - QCOMPARE(qApp->focusWidget(), (QWidget *)other2); + QTRY_COMPARE(QApplication::activeWindow(), other2->window()); + QCOMPARE(QApplication::focusWidget(), static_cast<QWidget *>(other2)); currentResult = NoResult; ambigResult = NoResult; @@ -1090,23 +1121,21 @@ void tst_QShortcut::context() other1->clear(); other2->clear(); - sendKeyEvents(other2, Qt::ALT+Qt::Key_2); + sendKeyEvents(other2, Qt::ALT | Qt::Key_2); QCOMPARE(currentResult, Slot2Triggered); QCOMPARE(ambigResult, NoResult); - QCOMPARE(edit->toPlainText(), QString("")); - QCOMPARE(other1->toPlainText(), QString("")); - QCOMPARE(other2->toPlainText(), QString("")); - - clearAllShortcuts(); + QCOMPARE(edit->toPlainText(), QString()); + QCOMPARE(other1->toPlainText(), QString()); + QCOMPARE(other2->toPlainText(), QString()); } // QTBUG-38986, do not generate duplicated QEvent::ShortcutOverride in event processing. class OverrideCountingWidget : public QWidget { public: - OverrideCountingWidget(QWidget *parent = 0) : QWidget(parent), overrideCount(0) {} + using QWidget::QWidget; - int overrideCount; + int overrideCount = 0; bool event(QEvent *e) override { @@ -1118,107 +1147,69 @@ public: void tst_QShortcut::duplicatedShortcutOverride() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + OverrideCountingWidget w; w.setWindowTitle(Q_FUNC_INFO); w.resize(200, 200); w.move(QGuiApplication::primaryScreen()->availableGeometry().center() - QPoint(100, 100)); w.show(); - QApplication::setActiveWindow(&w); QVERIFY(QTest::qWaitForWindowActive(&w)); QTest::keyPress(w.windowHandle(), Qt::Key_A); QCoreApplication::processEvents(); QCOMPARE(w.overrideCount, 1); } -// ------------------------------------------------------------------ -// Element Testing helper functions --------------------------------- -// ------------------------------------------------------------------ -void tst_QShortcut::clearAllShortcuts() -{ - QList<QShortcut *> shortcutsCpy = shortcuts; - qDeleteAll(shortcutsCpy); - shortcuts.clear(); -} - -QShortcut *tst_QShortcut::setupShortcut(int testWidget, const QKeySequence &ks) +QShortcut *tst_QShortcut::setupShortcut(QWidget *parent, const QString &name, const QKeySequence &ks, Qt::ShortcutContext context) { - return setupShortcut(mainW, QTest::currentDataTag() ? QTest::currentDataTag() : "", testWidget, ks); -} - -QShortcut *tst_QShortcut::setupShortcut(int testWidget, const QString &txt, int k1, int k2, int k3, int k4) -{ - return setupShortcut(mainW, QTest::currentDataTag() ? QTest::currentDataTag() : "", testWidget, - (txt.isEmpty() ? QKeySequence(k1, k2, k3, k4) : QKeySequence(txt))); -} - -QShortcut *tst_QShortcut::setupShortcut(QWidget *parent, const char *name, int testWidget, const QString &txt, int k1, int k2, int k3, int k4) -{ - return setupShortcut(parent, name, testWidget, - (txt.isEmpty() ? QKeySequence(k1, k2, k3, k4) : QKeySequence(txt))); + // Set up shortcut for next test + auto cut = new QShortcut(ks, parent, nullptr, nullptr, context); + cut->setObjectName(name); + return cut; } -QShortcut *tst_QShortcut::setupShortcut(QWidget *parent, const char *name, int testWidget, +QShortcut *tst_QShortcut::setupShortcut(QWidget *parent, const QString &name, Widget testWidget, const QKeySequence &ks, Qt::ShortcutContext context) { // Set up shortcut for next test - QShortcut *cut = new QShortcut(QKeySequence(), parent, 0, 0, context); - cut->setObjectName(name); - cut->setKey(ks); + auto cut = setupShortcut(parent, name, ks, context); - const char *normal = 0; - const char *ambig = 0; - switch(testWidget) - { + switch (testWidget) { case TriggerSlot1: - normal = SLOT(slotTrig1()); - ambig = SLOT(ambigSlot1()); + connect(cut, &QShortcut::activated, this, &tst_QShortcut::slotTrig1); + connect(cut, &QShortcut::activatedAmbiguously, this, &tst_QShortcut::ambigSlot1); break; case TriggerSlot2: - normal = SLOT(slotTrig2()); - ambig = SLOT(ambigSlot2()); + connect(cut, &QShortcut::activated, this, &tst_QShortcut::slotTrig2); + connect(cut, &QShortcut::activatedAmbiguously, this, &tst_QShortcut::ambigSlot2); break; case TriggerSlot3: - normal = SLOT(slotTrig3()); - ambig = SLOT(ambigSlot3()); + connect(cut, &QShortcut::activated, this, &tst_QShortcut::slotTrig3); + connect(cut, &QShortcut::activatedAmbiguously, this, &tst_QShortcut::ambigSlot3); break; case TriggerSlot4: - normal = SLOT(slotTrig4()); - ambig = SLOT(ambigSlot4()); + connect(cut, &QShortcut::activated, this, &tst_QShortcut::slotTrig4); + connect(cut, &QShortcut::activatedAmbiguously, this, &tst_QShortcut::ambigSlot4); break; case TriggerSlot5: - normal = SLOT(slotTrig5()); - ambig = SLOT(ambigSlot5()); + connect(cut, &QShortcut::activated, this, &tst_QShortcut::slotTrig5); + connect(cut, &QShortcut::activatedAmbiguously, this, &tst_QShortcut::ambigSlot5); break; case TriggerSlot6: - normal = SLOT(slotTrig6()); - ambig = SLOT(ambigSlot6()); + connect(cut, &QShortcut::activated, this, &tst_QShortcut::slotTrig6); + connect(cut, &QShortcut::activatedAmbiguously, this, &tst_QShortcut::ambigSlot6); break; case TriggerSlot7: - normal = SLOT(slotTrig7()); - ambig = SLOT(ambigSlot7()); + connect(cut, &QShortcut::activated, this, &tst_QShortcut::slotTrig7); + connect(cut, &QShortcut::activatedAmbiguously, this, &tst_QShortcut::ambigSlot7); + break; + case NoWidget: break; - case SendKeyEvent: - normal = SLOT(sendKeyEvent()); } - connect(cut, SIGNAL(activated()), this, normal); - if (ambig) - connect(cut, SIGNAL(activatedAmbiguously()), this, ambig); - connect(cut, SIGNAL(destroyed(QObject*)), this, SLOT(shortcutDestroyed(QObject*))); - shortcuts.append(cut); return cut; } -void tst_QShortcut::shortcutDestroyed(QObject* obj) -{ - shortcuts.erase(std::remove(shortcuts.begin(), shortcuts.end(), obj), - shortcuts.end()); -} - -void tst_QShortcut::sendKeyEvents(int k1, QChar c1, int k2, QChar c2, int k3, QChar c3, int k4, QChar c4) -{ - sendKeyEvents(mainW, k1, c1, k2, c2, k3, c3, k4, c4); -} - void tst_QShortcut::sendKeyEvents(QWidget *w, int k1, QChar c1, int k2, QChar c2, int k3, QChar c3, int k4, QChar c4) { Qt::KeyboardModifiers b1 = toButtons( k1 ); @@ -1258,6 +1249,9 @@ void tst_QShortcut::sendKeyEvents(QWidget *w, int k1, QChar c1, int k2, QChar c2 void tst_QShortcut::testElement() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + currentResult = NoResult; QFETCH(tst_QShortcut::Action, action); QFETCH(tst_QShortcut::Widget, testWidget); @@ -1272,25 +1266,51 @@ void tst_QShortcut::testElement() QFETCH(int, c4); QFETCH(tst_QShortcut::Result, result); - if (action == ClearAll) { - clearAllShortcuts(); - } else if (action == SetupAccel) { - setupShortcut(testWidget, txt, k1, k2, k3, k4); - } else { - sendKeyEvents(k1, c1, k2, c2, k3, c3, k4, c4); - QCOMPARE((int)currentResult, (int)result); + auto mainWindowDeleter = qScopeGuard([&]{ + if (action == TestEnd) + mainWindow.reset(); + }); + + 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(mainWindow.data())) + QVERIFY(false); + + switch (action) { + case ClearAll: + qDeleteAll(mainWindow->findChildren<QShortcut *>()); + break; + case SetupAccel: + setupShortcut(mainWindow.data(), txt, testWidget, txt.isEmpty() + ? QKeySequence(k1, k2, k3, k4) : QKeySequence::fromString(txt)); + break; + case TestAccel: + 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: + // taken care of by the mainWindowDeleter + break; } } void tst_QShortcut::shortcutToFocusProxy() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QLineEdit le; QCompleter completer; QStringListModel *slm = new QStringListModel(QStringList() << "a0" << "a1" << "a2", &completer); completer.setModel(slm); completer.setCompletionMode(QCompleter::PopupCompletion); le.setCompleter(&completer); - QShortcut *shortcut = new QShortcut(QKeySequence(Qt::ALT + Qt::Key_S), &le); + QShortcut *shortcut = new QShortcut(QKeySequence(Qt::ALT | Qt::Key_S), &le); QObject::connect(shortcut, &QShortcut::activated, &le, &QLineEdit::clear); le.setFocus(); le.show(); @@ -1305,5 +1325,36 @@ void tst_QShortcut::shortcutToFocusProxy() QCOMPARE(le.text(), QString()); } +void tst_QShortcut::deleteLater() +{ + QWidget w; + QPointer<QShortcut> sc(new QShortcut(QKeySequence(Qt::Key_1), &w)); + sc->deleteLater(); + QTRY_VERIFY(!sc); +} + +void tst_QShortcut::keys() +{ + QLineEdit le; + QShortcut *sc = new QShortcut(QKeySequence::InsertParagraphSeparator, &le); + QVERIFY(sc->keys().contains(QKeySequence(Qt::Key_Enter))); + QVERIFY(sc->keys().contains(QKeySequence(Qt::Key_Return))); + + QSignalSpy spy(sc, &QShortcut::activated); + le.setFocus(); + le.show(); + if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QVERIFY(QTest::qWaitForWindowActive(&le)); + else + QTRY_VERIFY(le.windowHandle()->isActive()); + QCOMPARE(QApplication::focusWidget(), &le); + + QTest::keyEvent(QTest::Press, QApplication::focusWidget(), Qt::Key_Enter); + QTRY_COMPARE(spy.size(), 1); + + QTest::keyEvent(QTest::Press, QApplication::focusWidget(), Qt::Key_Return); + QTRY_COMPARE(spy.size(), 2); +} + QTEST_MAIN(tst_QShortcut) #include "tst_qshortcut.moc" diff --git a/tests/auto/widgets/kernel/qsizepolicy/CMakeLists.txt b/tests/auto/widgets/kernel/qsizepolicy/CMakeLists.txt new file mode 100644 index 0000000000..efdd72db73 --- /dev/null +++ b/tests/auto/widgets/kernel/qsizepolicy/CMakeLists.txt @@ -0,0 +1,24 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qsizepolicy Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qsizepolicy LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qsizepolicy + SOURCES + tst_qsizepolicy.cpp + LIBRARIES + Qt::Gui + Qt::Widgets + Qt::WidgetsPrivate +) + +## Scopes: +##################################################################### diff --git a/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro b/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro deleted file mode 100644 index d325bc4aeb..0000000000 --- a/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro +++ /dev/null @@ -1,7 +0,0 @@ -CONFIG += testcase -contains(QT_CONFIG, c++14): CONFIG += c++14 -TARGET = tst_qsizepolicy - -QT += widgets widgets-private testlib - -SOURCES += tst_qsizepolicy.cpp diff --git a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp index 89bb946be9..19d267f7e5 100644 --- a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp +++ b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp @@ -1,32 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + #include <qsizepolicy.h> Q_DECLARE_METATYPE(Qt::Orientations) @@ -34,6 +8,8 @@ Q_DECLARE_METATYPE(QSizePolicy) Q_DECLARE_METATYPE(QSizePolicy::Policy) Q_DECLARE_METATYPE(QSizePolicy::ControlType) +#include <QTest> + class tst_QSizePolicy : public QObject { Q_OBJECT @@ -109,22 +85,22 @@ void tst_QSizePolicy::constExpr() { /* gcc < 4.8.0 has problems with init'ing variant members in constexpr ctors */ /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54922 */ -#if !defined(Q_CC_GNU) || defined(Q_CC_INTEL) || defined(Q_CC_CLANG) || Q_CC_GNU >= 408 +#if !defined(Q_CC_GNU) || defined(Q_CC_CLANG) || Q_CC_GNU >= 408 // check that certain ctors are constexpr (compile-only): - { Q_CONSTEXPR QSizePolicy sp; Q_UNUSED(sp); } - { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(); Q_UNUSED(sp); } - { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); Q_UNUSED(sp); } - { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding, QSizePolicy::DefaultType); - Q_CONSTEXPR QSizePolicy tp = sp.transposed(); Q_UNUSED(tp); } + { constexpr QSizePolicy sp; Q_UNUSED(sp); } + { constexpr QSizePolicy sp = QSizePolicy(); Q_UNUSED(sp); } + { constexpr QSizePolicy sp = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); Q_UNUSED(sp); } + { constexpr QSizePolicy sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding, QSizePolicy::DefaultType); + constexpr QSizePolicy tp = sp.transposed(); Q_UNUSED(tp); } { // QTBUG-69983: For ControlType != QSizePolicy::DefaultType, qCountTrailingZeroBits() // is used, which MSVC 15.8.1 does not consider constexpr due to built-ins -# if defined(QT_HAS_CONSTEXPR_BUILTINS) && (!defined(Q_CC_MSVC) || _MSC_VER < 1915) - Q_RELAXED_CONSTEXPR auto sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, QSizePolicy::CheckBox); +# if defined(QT_HAS_CONSTEXPR_BITOPS) + constexpr auto sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, QSizePolicy::CheckBox); # else - Q_CONSTEXPR auto sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding, QSizePolicy::DefaultType); + constexpr auto sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding, QSizePolicy::DefaultType); # endif - Q_RELAXED_CONSTEXPR auto tp = sp.transposed(); Q_UNUSED(tp); + constexpr auto tp = sp.transposed(); Q_UNUSED(tp); } #else QSKIP("QSizePolicy cannot be constexpr with this version of the compiler."); diff --git a/tests/auto/widgets/kernel/qstackedlayout/CMakeLists.txt b/tests/auto/widgets/kernel/qstackedlayout/CMakeLists.txt new file mode 100644 index 0000000000..5701f455b5 --- /dev/null +++ b/tests/auto/widgets/kernel/qstackedlayout/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qstackedlayout Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qstackedlayout LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qstackedlayout + SOURCES + tst_qstackedlayout.cpp + LIBRARIES + Qt::Gui + Qt::Widgets + Qt::WidgetsPrivate +) diff --git a/tests/auto/widgets/kernel/qstackedlayout/qstackedlayout.pro b/tests/auto/widgets/kernel/qstackedlayout/qstackedlayout.pro deleted file mode 100644 index 1f94c1b386..0000000000 --- a/tests/auto/widgets/kernel/qstackedlayout/qstackedlayout.pro +++ /dev/null @@ -1,7 +0,0 @@ -CONFIG += testcase -TARGET = tst_qstackedlayout -QT += widgets testlib -SOURCES += tst_qstackedlayout.cpp - - - diff --git a/tests/auto/widgets/kernel/qstackedlayout/tst_qstackedlayout.cpp b/tests/auto/widgets/kernel/qstackedlayout/tst_qstackedlayout.cpp index 10712ea9ad..c73b9725b2 100644 --- a/tests/auto/widgets/kernel/qstackedlayout/tst_qstackedlayout.cpp +++ b/tests/auto/widgets/kernel/qstackedlayout/tst_qstackedlayout.cpp @@ -1,39 +1,17 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <QLineEdit> #include <QLabel> #include <QStackedLayout> #include <qapplication.h> #include <qwidget.h> #include <QPushButton> +#include <QSignalSpy> + +#include <QtWidgets/private/qapplication_p.h> class tst_QStackedLayout : public QObject { @@ -92,6 +70,9 @@ tst_QStackedLayout::tst_QStackedLayout() void tst_QStackedLayout::init() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + if (testWidget) { delete testWidget; testWidget = 0; @@ -128,7 +109,7 @@ void tst_QStackedLayout::testCase() // One widget added to layout QWidget *w1 = new QWidget(testWidget); testLayout->addWidget(w1); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QCOMPARE(spy.at(0).at(0).toInt(), 0); spy.clear(); QCOMPARE(testLayout->currentIndex(), 0); @@ -145,7 +126,7 @@ void tst_QStackedLayout::testCase() // Change the current index testLayout->setCurrentIndex(1); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QCOMPARE(spy.at(0).at(0).toInt(), 1); spy.clear(); QCOMPARE(testLayout->currentIndex(), 1); @@ -159,7 +140,7 @@ void tst_QStackedLayout::testCase() // Second widget removed from layout; back to nothing testLayout->removeWidget(w2); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QCOMPARE(spy.at(0).at(0).toInt(), -1); spy.clear(); QCOMPARE(testLayout->currentIndex(), -1); @@ -281,13 +262,13 @@ protected: return true; } - void focusInEvent(QFocusEvent *event) + void focusInEvent(QFocusEvent *event) override { QLineEdit::focusInEvent(event); hasFakeEditFocus = isSingleFocusWidget(); } - void focusOutEvent(QFocusEvent *event) + void focusOutEvent(QFocusEvent *event) override { hasFakeEditFocus = false; QLineEdit::focusOutEvent(event); @@ -308,7 +289,6 @@ void tst_QStackedLayout::keepFocusAfterSetCurrent() stackLayout->setCurrentIndex(0); testWidget->show(); - QApplication::setActiveWindow(testWidget); QVERIFY(QTest::qWaitForWindowActive(testWidget)); edit1->setFocus(); diff --git a/tests/auto/widgets/kernel/qtooltip/CMakeLists.txt b/tests/auto/widgets/kernel/qtooltip/CMakeLists.txt new file mode 100644 index 0000000000..af2de80e24 --- /dev/null +++ b/tests/auto/widgets/kernel/qtooltip/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qtooltip Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qtooltip LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qtooltip + SOURCES + tst_qtooltip.cpp + LIBRARIES + Qt::Gui + Qt::Widgets + Qt::WidgetsPrivate +) diff --git a/tests/auto/widgets/kernel/qtooltip/qtooltip.pro b/tests/auto/widgets/kernel/qtooltip/qtooltip.pro deleted file mode 100644 index 6e5dd738cb..0000000000 --- a/tests/auto/widgets/kernel/qtooltip/qtooltip.pro +++ /dev/null @@ -1,4 +0,0 @@ -CONFIG += testcase -TARGET = tst_qtooltip -QT += widgets testlib -SOURCES += tst_qtooltip.cpp diff --git a/tests/auto/widgets/kernel/qtooltip/tst_qtooltip.cpp b/tests/auto/widgets/kernel/qtooltip/tst_qtooltip.cpp index 3d609d0b9c..bc0624c9ab 100644 --- a/tests/auto/widgets/kernel/qtooltip/tst_qtooltip.cpp +++ b/tests/auto/widgets/kernel/qtooltip/tst_qtooltip.cpp @@ -1,39 +1,18 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> +#include <QTimer> + #include <qfont.h> #include <qfontmetrics.h> #include <qtooltip.h> #include <qwhatsthis.h> #include <qscreen.h> +#include <QtWidgets/private/qapplication_p.h> + class tst_QToolTip : public QObject { Q_OBJECT @@ -41,12 +20,13 @@ class tst_QToolTip : public QObject private slots: void init(); void cleanup(); - void task183679_data(); - void task183679(); + void keyEvent_data(); + void keyEvent(); void whatsThis(); void setPalette(); void qtbug64550_stylesheet(); void dontCrashOutsideScreenGeometry(); + void marginSetWithStyleSheet(); }; void tst_QToolTip::init() @@ -60,11 +40,11 @@ void tst_QToolTip::cleanup() qApp->setStyleSheet(QString()); } -class Widget_task183679 : public QWidget +class Widget : public QWidget { Q_OBJECT public: - Widget_task183679(QWidget *parent = 0) : QWidget(parent) {} + Widget(QWidget *parent = nullptr) : QWidget(parent) {} void showDelayedToolTip(int msecs) { @@ -76,34 +56,40 @@ public: private slots: void showToolTip() { - QToolTip::showText(mapToGlobal(QPoint(0, 0)), Widget_task183679::toolTipText(), this); + QToolTip::showText(mapToGlobal(QPoint(0, 0)), Widget::toolTipText(), this); } }; Q_DECLARE_METATYPE(Qt::Key) -void tst_QToolTip::task183679_data() +void tst_QToolTip::keyEvent_data() { QTest::addColumn<Qt::Key>("key"); QTest::addColumn<bool>("visible"); - QTest::newRow("non-modifier") << Qt::Key_A << true; + QTest::newRow("non-modifier") << Qt::Key_A << +#if defined(Q_OS_MACOS) + // macOS natively hides tooltips on non-modifier key events, + // so QTipLabel::eventFilter does the same. Match that here. + false; +#else + true; +#endif QTest::newRow("Shift") << Qt::Key_Shift << true; QTest::newRow("Control") << Qt::Key_Control << true; QTest::newRow("Alt") << Qt::Key_Alt << true; QTest::newRow("Meta") << Qt::Key_Meta << true; } -void tst_QToolTip::task183679() +void tst_QToolTip::keyEvent() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QFETCH(Qt::Key, key); QFETCH(bool, visible); -#ifdef Q_OS_MAC - QSKIP("This test fails in the CI system, QTBUG-30040"); -#endif - - Widget_task183679 widget; + Widget widget; widget.move(QGuiApplication::primaryScreen()->availableGeometry().topLeft() + QPoint(50, 50)); // Ensure cursor is not over tooltip, which causes it to hide #ifndef QT_NO_CURSOR @@ -112,7 +98,6 @@ void tst_QToolTip::task183679() widget.setWindowTitle(QLatin1String(QTest::currentTestFunction()) + QLatin1Char(' ') + QLatin1String(QTest::currentDataTag())); widget.show(); - QApplication::setActiveWindow(&widget); QVERIFY(QTest::qWaitForWindowActive(&widget)); widget.showDelayedToolTip(100); @@ -132,7 +117,8 @@ void tst_QToolTip::task183679() static QWidget *findWhatsThat() { - foreach (QWidget *widget, QApplication::topLevelWidgets()) { + const auto widgets = QApplication::topLevelWidgets(); + for (QWidget *widget : widgets) { if (widget->inherits("QWhatsThat")) return widget; } @@ -200,10 +186,12 @@ static QByteArray msgSizeTooSmall(const QSize &actual, const QSize &expected) // Set a large font size and verify that the tool tip is big enough. void tst_QToolTip::qtbug64550_stylesheet() { - Widget_task183679 widget; + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + + Widget widget; widget.setStyleSheet(QStringLiteral("* { font-size: 48pt; }\n")); widget.show(); - QApplication::setActiveWindow(&widget); QVERIFY(QTest::qWaitForWindowActive(&widget)); widget.showDelayedToolTip(100); @@ -212,7 +200,7 @@ void tst_QToolTip::qtbug64550_stylesheet() QVERIFY(toolTip); QTRY_VERIFY(toolTip->isVisible()); - const QRect boundingRect = QFontMetrics(widget.font()).boundingRect(Widget_task183679::toolTipText()); + const QRect boundingRect = QFontMetrics(widget.font()).boundingRect(Widget::toolTipText()); const QSize toolTipSize = toolTip->size(); QVERIFY2(toolTipSize.width() >= boundingRect.width() && toolTipSize.height() >= boundingRect.height(), @@ -225,5 +213,30 @@ void tst_QToolTip::dontCrashOutsideScreenGeometry() { QToolTip::hideText(); } +void tst_QToolTip::marginSetWithStyleSheet() +{ + const char *toolTipText = "Test Tool Tip"; + + qApp->setStyleSheet("QToolTip {font-size: 8px; margin: 5px;}"); + QToolTip::showText(QGuiApplication::primaryScreen()->availableGeometry().topLeft(), toolTipText); + QTRY_VERIFY(QToolTip::isVisible()); + QWidget *toolTip = findToolTip(); + QVERIFY(toolTip); + QTRY_VERIFY(toolTip->isVisible()); + int toolTipHeight = toolTip->size().height(); + qApp->setStyleSheet(QString()); + QToolTip::hideText(); + + qApp->setStyleSheet("QToolTip {font-size: 8px; margin: 10px;}"); + QToolTip::showText(QGuiApplication::primaryScreen()->availableGeometry().topLeft(), toolTipText); + QTRY_VERIFY(QToolTip::isVisible()); + toolTip = findToolTip(); + QVERIFY(toolTip); + QTRY_VERIFY(toolTip->isVisible()); + QCOMPARE_LE(toolTip->size().height(), toolTipHeight + 10); + qApp->setStyleSheet(QString()); + QToolTip::hideText(); +} + QTEST_MAIN(tst_QToolTip) #include "tst_qtooltip.moc" diff --git a/tests/auto/widgets/kernel/qwidget/BLACKLIST b/tests/auto/widgets/kernel/qwidget/BLACKLIST index 1f68308bbe..4c17af245b 100644 --- a/tests/auto/widgets/kernel/qwidget/BLACKLIST +++ b/tests/auto/widgets/kernel/qwidget/BLACKLIST @@ -1,47 +1,49 @@ -# OSX QTBUG-25300 QTBUG-45502 -[normalGeometry] -ubuntu-16.04 -[saveRestoreGeometry] -xcb -b2qt -[restoreVersion1Geometry] -osx -[updateWhileMinimized] -ubuntu-16.04 -ubuntu-18.04 -rhel-7.4 -osx -[focusProxyAndInputMethods] -linux [raise] -# QTBUG-68175 -opensuse opensuse-leap -[setWindowGeometry] -osx -[windowMoveResize] -osx -[childEvents] -osx ci -[renderInvisible] -osx -[optimizedResizeMove] -osx [optimizedResize_topLevel] osx +[render_windowOpacity] +macos arm [render_systemClip] osx -[showMinimizedKeepsFocus] -osx-10.12 ci -osx-10.13 ci -[maskedUpdate] -opensuse +[multipleToplevelFocusCheck] +centos opensuse-leap -[moveInResizeEvent] -ubuntu-16.04 -[moveChild:right] -osx -[activateWindow] -osx-10.12 ci +ubuntu +sles-15 +# QTBUG-87668 +[showMinimizedKeepsFocus] +android +macos-13 ci +macos-14 ci +[normalGeometry] +android +[saveRestoreGeometry] +android +[optimizedResizeMove] +android +[update] +android +[scroll] +android +[moveChild] +android [multipleToplevelFocusCheck] -linux +android +[renderInvisible] +android +[updateWhileMinimized] +android +[doubleRepaint] +android +[setMaskInResizeEvent] +android +[activateWindow] +android +[optimizedResize_topLevel] +android +[hoverPosition] +macos-14 x86 +# QTBUG-124291 +[setParentChangesFocus:make dialog parentless, after] +android diff --git a/tests/auto/widgets/kernel/qwidget/CMakeLists.txt b/tests/auto/widgets/kernel/qwidget/CMakeLists.txt new file mode 100644 index 0000000000..f18bc98bb3 --- /dev/null +++ b/tests/auto/widgets/kernel/qwidget/CMakeLists.txt @@ -0,0 +1,48 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qwidget Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qwidget LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +# Resources: +set(qwidget_resource_files + "geometry-fullscreen.dat" + "geometry-maximized.dat" + "geometry.dat" + "hellotr_la.qm" +) + +qt_internal_add_test(tst_qwidget + SOURCES + tst_qwidget.cpp + LIBRARIES + Qt::CorePrivate + Qt::Gui + Qt::GuiPrivate + Qt::TestPrivate + Qt::Widgets + Qt::WidgetsPrivate + TESTDATA ${qwidget_resource_files} + BUILTIN_TESTDATA +) + +## Scopes: +##################################################################### + +qt_internal_extend_target(tst_qwidget CONDITION AIX + COMPILE_OPTIONS + -fpermissive +) + +qt_internal_extend_target(tst_qwidget CONDITION WIN32 + LIBRARIES + gdi32 + user32 +) diff --git a/tests/auto/widgets/kernel/qwidget/hellotr_la.qm b/tests/auto/widgets/kernel/qwidget/hellotr_la.qm Binary files differnew file mode 100644 index 0000000000..25c0aad583 --- /dev/null +++ b/tests/auto/widgets/kernel/qwidget/hellotr_la.qm diff --git a/tests/auto/widgets/kernel/qwidget/qwidget.pro b/tests/auto/widgets/kernel/qwidget/qwidget.pro deleted file mode 100644 index c1908af2a2..0000000000 --- a/tests/auto/widgets/kernel/qwidget/qwidget.pro +++ /dev/null @@ -1,19 +0,0 @@ -CONFIG += testcase -testcase.timeout = 600 # this test is slow -TARGET = tst_qwidget - -QT += widgets core-private gui-private widgets-private testlib testlib-private - -SOURCES += tst_qwidget.cpp -RESOURCES = qwidget.qrc - -aix-g++*:QMAKE_CXXFLAGS+=-fpermissive - -CONFIG += x11inc - -mac { - LIBS += -framework Security -framework AppKit - OBJECTIVE_SOURCES += tst_qwidget_mac_helpers.mm -} - -win32:!winrt: LIBS += -luser32 -lgdi32 diff --git a/tests/auto/widgets/kernel/qwidget/qwidget.qrc b/tests/auto/widgets/kernel/qwidget/qwidget.qrc deleted file mode 100644 index 1399c4c9db..0000000000 --- a/tests/auto/widgets/kernel/qwidget/qwidget.qrc +++ /dev/null @@ -1,7 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> - <qresource> - <file>geometry.dat</file> - <file>geometry-maximized.dat</file> - <file>geometry-fullscreen.dat</file> - </qresource> -</RCC> diff --git a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data0.qsnap b/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data0.qsnap Binary files differdeleted file mode 100644 index b3473cdefe..0000000000 --- a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data0.qsnap +++ /dev/null diff --git a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data1.qsnap b/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data1.qsnap Binary files differdeleted file mode 100644 index 10007733ca..0000000000 --- a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data1.qsnap +++ /dev/null diff --git a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data2.qsnap b/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data2.qsnap Binary files differdeleted file mode 100644 index cde5964a30..0000000000 --- a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data2.qsnap +++ /dev/null diff --git a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data3.qsnap b/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data3.qsnap Binary files differdeleted file mode 100644 index 23ea1410e4..0000000000 --- a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Motif_data3.qsnap +++ /dev/null diff --git a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data0.qsnap b/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data0.qsnap Binary files differdeleted file mode 100644 index a8918c1d1b..0000000000 --- a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data0.qsnap +++ /dev/null diff --git a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data1.qsnap b/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data1.qsnap Binary files differdeleted file mode 100644 index 0981cf5dd1..0000000000 --- a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data1.qsnap +++ /dev/null diff --git a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data2.qsnap b/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data2.qsnap Binary files differdeleted file mode 100644 index 75d09136cf..0000000000 --- a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data2.qsnap +++ /dev/null diff --git a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data3.qsnap b/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data3.qsnap Binary files differdeleted file mode 100644 index f58f74d030..0000000000 --- a/tests/auto/widgets/kernel/qwidget/testdata/paintEvent/res_Windows_data3.qsnap +++ /dev/null diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp index 58c61d746a..666cb70da9 100644 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp +++ b/tests/auto/widgets/kernel/qwidget/tst_qwidget.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include "../../../shared/highdpi.h" @@ -38,25 +13,32 @@ #include <qlineedit.h> #include <qlistview.h> #include <qmessagebox.h> +#include <qmimedata.h> #include <qpainter.h> #include <qpoint.h> #include <qpushbutton.h> #include <qstyle.h> #include <qwidget.h> #include <qstylefactory.h> -#include <qdesktopwidget.h> #include <private/qwidget_p.h> +#include <private/qwidgetrepaintmanager_p.h> #include <private/qapplication_p.h> #include <private/qhighdpiscaling_p.h> #include <qcalendarwidget.h> #include <qmainwindow.h> #include <qdockwidget.h> #include <qrandom.h> +#include <qsignalspy.h> +#include <qstylehints.h> #include <qtoolbar.h> #include <qtoolbutton.h> +#include <QtCore/qoperatingsystemversion.h> #include <QtGui/qpaintengine.h> +#include <QtGui/qpainterpath.h> #include <QtGui/qbackingstore.h> #include <QtGui/qguiapplication.h> +#include <QtGui/qpa/qplatformwindow.h> +#include <QtGui/qpa/qplatformdrag.h> #include <QtGui/qscreen.h> #include <qmenubar.h> #include <qcompleter.h> @@ -68,17 +50,16 @@ #include <QtWidgets/QGraphicsProxyWidget> #include <QtGui/qwindow.h> #include <qtimer.h> - -#if defined(Q_OS_OSX) -#include "tst_qwidget_mac_helpers.h" // Abstract the ObjC stuff out so not everyone must run an ObjC++ compile. -#endif +#include <QtWidgets/QDoubleSpinBox> +#include <QtWidgets/QComboBox> #include <QtTest/QTest> #include <QtTest/private/qtesthelpers_p.h> using namespace QTestPrivate; +using namespace Qt::StringLiterals; -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined(Q_OS_WIN) # include <QtCore/qt_windows.h> # include <QtGui/private/qguiapplication_p.h> #include <qpa/qplatformnativeinterface.h> @@ -100,11 +81,11 @@ static HWND winHandleOf(const QWidget *w) if (::SwitchDesktop(::GetThreadDesktop(::GetCurrentThreadId())) == 0) \ QSKIP("desktop is not visible, this test would fail"); -#else // Q_OS_WIN && !Q_OS_WINRT +#else // Q_OS_WIN # define Q_CHECK_PAINTEVENTS #endif -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS #include <Security/AuthSession.h> bool macHasAccessToWindowsServer() { @@ -115,7 +96,7 @@ bool macHasAccessToWindowsServer() } #endif -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined(Q_OS_WIN) static inline void setWindowsAnimationsEnabled(bool enabled) { ANIMATIONINFO animation = { sizeof(ANIMATIONINFO), enabled }; @@ -128,10 +109,10 @@ static inline bool windowsAnimationsEnabled() SystemParametersInfo(SPI_GETANIMATION, 0, &animation, 0); return animation.iMinAnimate; } -#else // Q_OS_WIN && !Q_OS_WINRT +#else // Q_OS_WIN inline void setWindowsAnimationsEnabled(bool) {} static inline bool windowsAnimationsEnabled() { return false; } -#endif // !Q_OS_WIN || Q_OS_WINRT +#endif // !Q_OS_WIN template <class T> static QByteArray msgComparisonFailed(T v1, const char *op, T v2) @@ -141,6 +122,36 @@ static QByteArray msgComparisonFailed(T v1, const char *op, T v2) return s.toLocal8Bit(); } +template<class T> class EventSpy : public QObject +{ +public: + EventSpy(T *widget, QEvent::Type event) + : m_widget(widget), eventToSpy(event) + { + if (m_widget) + m_widget->installEventFilter(this); + } + + T *widget() const { return m_widget; } + int count() const { return m_count; } + void clear() { m_count = 0; } + +protected: + bool eventFilter(QObject *object, QEvent *event) override + { + if (event->type() == eventToSpy) + ++m_count; + return QObject::eventFilter(object, event); + } + +private: + T *m_widget; + const QEvent::Type eventToSpy; + int m_count = 0; +}; + +Q_LOGGING_CATEGORY(lcTests, "qt.widgets.tests") + class tst_QWidget : public QObject { Q_OBJECT @@ -152,13 +163,18 @@ public: public slots: void initTestCase(); void cleanup(); + private slots: + void nativeWindowAttribute(); + void addActionOverloads(); void getSetCheck(); void fontPropagation(); void fontPropagation2(); void fontPropagation3(); + void fontPropagationDynamic(); void palettePropagation(); void palettePropagation2(); + void palettePropagationDynamic(); void enabledPropagation(); void ignoreKeyEventsWhenDisabled_QTBUG27417(); void properTabHandlingWhenDisabled_QTBUG27417(); @@ -175,17 +191,32 @@ private slots: void mapFromAndTo(); void focusChainOnHide(); void focusChainOnReparent(); + void focusAbstraction(); void defaultTabOrder(); void reverseTabOrder(); void tabOrderWithProxy(); + void tabOrderWithProxyDisabled(); + void tabOrderWithProxyOutOfOrder(); void tabOrderWithCompoundWidgets(); + void tabOrderWithCompoundWidgetsInflection_data(); + void tabOrderWithCompoundWidgetsInflection(); + void tabOrderWithCompoundWidgetsNoFocusPolicy(); void tabOrderNoChange(); void tabOrderNoChange2(); -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) + void appFocusWidgetWithFocusProxyLater(); + void appFocusWidgetWhenLosingFocusProxy(); + void explicitTabOrderWithComplexWidget(); + void explicitTabOrderWithSpinBox_QTBUG81097(); + void tabOrderList(); + void tabOrderComboBox_data(); + void tabOrderComboBox(); +#if defined(Q_OS_WIN) void activation(); #endif void reparent(); + void setScreen(); void windowState(); + void resizePropagation(); void showMaximized(); void showFullScreen(); void showMinimized(); @@ -194,6 +225,7 @@ private slots: void hideWhenFocusWidgetIsChild(); void normalGeometry(); void setGeometry(); + void setGeometryHidden(); void windowOpacity(); void raise(); void lower(); @@ -202,9 +234,11 @@ private slots: void saveRestoreGeometry(); void restoreVersion1Geometry_data(); void restoreVersion1Geometry(); + void restoreGeometryAfterScreenChange_data(); + void restoreGeometryAfterScreenChange(); void widgetAt(); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS void setMask(); #endif void optimizedResizeMove(); @@ -221,11 +255,15 @@ private slots: void setFixedSize(); void ensureCreated(); + void createAndDestroy(); + void eventsAndAttributesOnDestroy(); void winIdChangeEvent(); void persistentWinId(); void showNativeChild(); + void closeAndShowNativeChild(); + void closeAndShowWithNativeChild(); void transientParent(); - void qobject_castInDestroyedSlot(); + void qobject_castOnDestruction(); void showHideEvent_data(); void showHideEvent(); @@ -237,7 +275,7 @@ private slots: void update(); void isOpaque(); -#ifndef Q_OS_OSX +#ifndef Q_OS_MACOS void scroll(); void scrollNativeChildren(); #endif @@ -256,7 +294,7 @@ private slots: void subtractOpaqueSiblings(); -#if defined (Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined (Q_OS_WIN) void setGeometry_win(); #endif @@ -277,8 +315,11 @@ private slots: void childEvents(); void render(); + void renderChildFillsBackground(); + void renderTargetOffset(); void renderInvisible(); void renderWithPainter(); + void renderRTL(); void render_task188133(); void render_task211796(); void render_task217815(); @@ -300,6 +341,8 @@ private slots: void hideOpaqueChildWhileHidden(); void updateWhileMinimized(); void alienWidgets(); + void nativeWindowPosition_data(); + void nativeWindowPosition(); void adjustSize(); void adjustSize_data(); void updateGeometry(); @@ -309,10 +352,14 @@ private slots: void resizeInPaintEvent(); void opaqueChildren(); + void dumpObjectTree(); + void setMaskInResizeEvent(); void moveInResizeEvent(); - void immediateRepaintAfterInvalidateBuffer(); +#ifdef QT_BUILD_INTERNAL + void immediateRepaintAfterInvalidateBackingStore(); +#endif void effectiveWinId(); void effectiveWinId2(); @@ -322,7 +369,7 @@ private slots: void quitOnCloseAttribute(); void moveRect(); -#if defined (Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined (Q_OS_WIN) void gdiPainting(); void paintOnScreenPossible(); #endif @@ -335,7 +382,10 @@ private slots: void maskedUpdate(); #ifndef QT_NO_CURSOR void syntheticEnterLeave(); + void enterLeaveOnWindowShowHide_data(); + void enterLeaveOnWindowShowHide(); void taskQTBUG_4055_sendSyntheticEnterLeave(); + void hoverPosition(); void underMouse(); void taskQTBUG_27643_enterEvents(); #endif @@ -347,6 +397,8 @@ private slots: void focusWidget_task254563(); void rectOutsideCoordinatesLimit_task144779(); void setGraphicsEffect(); + void render_graphicsEffect_data(); + void render_graphicsEffect(); #ifdef QT_BUILD_INTERNAL void destroyBackingStore(); @@ -356,7 +408,9 @@ private slots: void openModal_taskQTBUG_5804(); - void focusProxyAndInputMethods(); + void focusProxy(); + void imEnabledNotImplemented(); + #ifdef QT_BUILD_INTERNAL void scrollWithoutBackingStore(); #endif @@ -364,7 +418,7 @@ private slots: void taskQTBUG_7532_tabOrderWithFocusProxy(); void movedAndResizedAttributes(); void childAt(); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS void taskQTBUG_11373(); #endif void taskQTBUG_17333_ResizeInfiniteRecursion(); @@ -376,6 +430,7 @@ private slots: void touchEventSynthesizedMouseEvent(); void touchUpdateOnNewTouch(); + void touchCancel(); void touchEventsForGesturePendingWidgets(); void styleSheetPropagation(); @@ -395,24 +450,56 @@ private slots: void tabletTracking(); void closeEvent(); + void closeWithChildWindow(); -private: - bool ensureScreenSize(int width, int height); + void winIdAfterClose(); + void receivesLanguageChangeEvent(); + void receivesApplicationFontChangeEvent(); + void receivesApplicationPaletteChangeEvent(); + void deleteWindowInCloseEvent(); + void quitOnClose(); + + void setParentChangesFocus_data(); + void setParentChangesFocus(); + + void activateWhileModalHidden(); + +#ifdef Q_OS_ANDROID + void showFullscreenAndroid(); +#endif + void setVisibleDuringDestruction(); + + void explicitShowHide(); + + void dragEnterLeaveSymmetry(); + + void reparentWindowHandles_data(); + void reparentWindowHandles(); + +#ifndef QT_NO_CONTEXTMENU + void contextMenuTrigger(); +#endif + +private: const QString m_platform; QSize m_testWidgetSize; QPoint m_availableTopLeft; QPoint m_safeCursorPos; const bool m_windowsAnimationsEnabled; - QTouchDevice *m_touchScreen; + QPointingDevice *m_touchScreen; const int m_fuzz; -}; + QPalette simplePalette(); -bool tst_QWidget::ensureScreenSize(int width, int height) -{ - const QSize available = QGuiApplication::primaryScreen()->availableGeometry().size(); - return (available.width() >= width && available.height() >= height); -} +private: + enum class ScreenPosition { + OffAbove, + OffLeft, + OffBelow, + OffRight, + Contained + }; +}; // Testing get/set functions void tst_QWidget::getSetCheck() @@ -428,18 +515,24 @@ void tst_QWidget::getSetCheck() QVERIFY(var1.data() != obj1.style()); QVERIFY(obj1.style() != nullptr); // style can never be 0 for a widget + const QRegularExpression negativeNotPossible(u"^.*Negative sizes \\(.*\\) are not possible$"_s); + const QRegularExpression largestAllowedSize(u"^.*The largest allowed size is \\(.*\\)$"_s); // int QWidget::minimumWidth() // void QWidget::setMinimumWidth(int) obj1.setMinimumWidth(0); QCOMPARE(obj1.minimumWidth(), 0); + QTest::ignoreMessage(QtWarningMsg, negativeNotPossible); obj1.setMinimumWidth(INT_MIN); QCOMPARE(obj1.minimumWidth(), 0); // A widgets width can never be less than 0 + QTest::ignoreMessage(QtWarningMsg, largestAllowedSize); obj1.setMinimumWidth(INT_MAX); child1.setMinimumWidth(0); QCOMPARE(child1.minimumWidth(), 0); + QTest::ignoreMessage(QtWarningMsg, negativeNotPossible); child1.setMinimumWidth(INT_MIN); QCOMPARE(child1.minimumWidth(), 0); // A widgets width can never be less than 0 + QTest::ignoreMessage(QtWarningMsg, largestAllowedSize); child1.setMinimumWidth(INT_MAX); QCOMPARE(child1.minimumWidth(), QWIDGETSIZE_MAX); // The largest minimum size should only be as big as the maximium @@ -447,14 +540,18 @@ void tst_QWidget::getSetCheck() // void QWidget::setMinimumHeight(int) obj1.setMinimumHeight(0); QCOMPARE(obj1.minimumHeight(), 0); + QTest::ignoreMessage(QtWarningMsg, negativeNotPossible); obj1.setMinimumHeight(INT_MIN); QCOMPARE(obj1.minimumHeight(), 0); // A widgets height can never be less than 0 + QTest::ignoreMessage(QtWarningMsg, largestAllowedSize); obj1.setMinimumHeight(INT_MAX); child1.setMinimumHeight(0); QCOMPARE(child1.minimumHeight(), 0); + QTest::ignoreMessage(QtWarningMsg, negativeNotPossible); child1.setMinimumHeight(INT_MIN); QCOMPARE(child1.minimumHeight(), 0); // A widgets height can never be less than 0 + QTest::ignoreMessage(QtWarningMsg, largestAllowedSize); child1.setMinimumHeight(INT_MAX); QCOMPARE(child1.minimumHeight(), QWIDGETSIZE_MAX); // The largest minimum size should only be as big as the maximium @@ -462,8 +559,10 @@ void tst_QWidget::getSetCheck() // void QWidget::setMaximumWidth(int) obj1.setMaximumWidth(0); QCOMPARE(obj1.maximumWidth(), 0); + QTest::ignoreMessage(QtWarningMsg, negativeNotPossible); obj1.setMaximumWidth(INT_MIN); QCOMPARE(obj1.maximumWidth(), 0); // A widgets width can never be less than 0 + QTest::ignoreMessage(QtWarningMsg, largestAllowedSize); obj1.setMaximumWidth(INT_MAX); QCOMPARE(obj1.maximumWidth(), QWIDGETSIZE_MAX); // QWIDGETSIZE_MAX is the abs max, not INT_MAX @@ -471,8 +570,10 @@ void tst_QWidget::getSetCheck() // void QWidget::setMaximumHeight(int) obj1.setMaximumHeight(0); QCOMPARE(obj1.maximumHeight(), 0); + QTest::ignoreMessage(QtWarningMsg, negativeNotPossible); obj1.setMaximumHeight(INT_MIN); QCOMPARE(obj1.maximumHeight(), 0); // A widgets height can never be less than 0 + QTest::ignoreMessage(QtWarningMsg, largestAllowedSize); obj1.setMaximumHeight(INT_MAX); QCOMPARE(obj1.maximumHeight(), QWIDGETSIZE_MAX); // QWIDGETSIZE_MAX is the abs max, not INT_MAX @@ -532,6 +633,7 @@ void tst_QWidget::getSetCheck() QBoxLayout *var11 = new QBoxLayout(QBoxLayout::LeftToRight); obj1.setLayout(var11); QCOMPARE(static_cast<QLayout *>(var11), obj1.layout()); + QTest::ignoreMessage(QtWarningMsg, "QWidget::setLayout: Cannot set layout to 0"); obj1.setLayout(nullptr); QCOMPARE(static_cast<QLayout *>(var11), obj1.layout()); // You cannot set a 0-pointer layout, that keeps the current delete var11; // This will remove the layout from the widget @@ -552,10 +654,10 @@ void tst_QWidget::getSetCheck() QCOMPARE(true, obj1.autoFillBackground()); var1.reset(); -#if defined (Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined (Q_OS_WIN) obj1.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint); const HWND handle = reinterpret_cast<HWND>(obj1.winId()); // explicitly create window handle - QVERIFY(GetWindowLong(handle, GWL_STYLE) & LONG(WS_POPUP)); + QVERIFY(GetWindowLongPtr(handle, GWL_STYLE) & LONG_PTR(WS_POPUP)); #endif } @@ -577,16 +679,25 @@ tst_QWidget::tst_QWidget() palette.setColor(QPalette::ToolTipBase, QColor(12, 13, 14)); palette.setColor(QPalette::Text, QColor(21, 22, 23)); QApplication::setPalette(palette, "QPropagationTestWidget"); + + if (QApplication::platformName().startsWith(QLatin1String("wayland"))) + qputenv("QT_WAYLAND_DISABLE_WINDOWDECORATION", "1"); } tst_QWidget::~tst_QWidget() { if (m_windowsAnimationsEnabled) setWindowsAnimationsEnabled(m_windowsAnimationsEnabled); + + delete m_touchScreen; } void tst_QWidget::initTestCase() { +#ifdef Q_OS_ANDROID + if (QNativeInterface::QAndroidApplication::sdkVersion() == 33) + QSKIP("Is flaky on Android 13 / RHEL 8.6 and 8.8 (QTQAINFRA-5606)"); +#endif // Size of reference widget, 200 for < 2000, scale up for larger screens // to avoid Windows warnings about minimum size for decorated windows. int width = 200; @@ -609,6 +720,90 @@ void tst_QWidget::cleanup() QTRY_VERIFY(QApplication::topLevelWidgets().isEmpty()); } +template <typename T> +struct ImplicitlyConvertibleTo { + T t; + operator const T() const { return t; } + operator T() { return t; } +}; + +void testFunction0() {} +void testFunction1(bool) {} + +void tst_QWidget::nativeWindowAttribute() +{ + QWidget parent; + QWidget child(&parent); + + QCOMPARE(parent.windowHandle(), nullptr); + QCOMPARE(child.windowHandle(), nullptr); + + // Setting WA_NativeWindow should create window handle + parent.setAttribute(Qt::WA_NativeWindow); + QVERIFY(parent.windowHandle() != nullptr); + // But not its child's window handle + QCOMPARE(child.windowHandle(), nullptr); + // Until the child also gains WA_NativeWindow + child.setAttribute(Qt::WA_NativeWindow); + QVERIFY(child.windowHandle() != nullptr); +} + +void tst_QWidget::addActionOverloads() +{ + // almost exhaustive check of addAction() overloads: + // (text), (icon, text), (icon, text, shortcut), (text, shortcut) + // each with a good sample of ways to QObject::connect() to + // QAction::triggered(bool) + QWidget w; + + // don't just pass QString etc - that'd be too easy (think QStringBuilder) + ImplicitlyConvertibleTo<QString> text = {QStringLiteral("foo")}; + ImplicitlyConvertibleTo<QIcon> icon; + + const auto check = [&](auto &...args) { // don't need to perfectly-forward, only lvalues passed + w.addAction(args...); + + w.addAction(args..., &w, SLOT(deleteLater())); + w.addAction(args..., &w, &QObject::deleteLater); + w.addAction(args..., testFunction0); + w.addAction(args..., &w, testFunction0); + w.addAction(args..., testFunction1); + w.addAction(args..., &w, testFunction1); + w.addAction(args..., [&](bool b) { w.setEnabled(b); }); + w.addAction(args..., &w, [&](bool b) { w.setEnabled(b); }); + + w.addAction(args..., &w, SLOT(deleteLater()), Qt::QueuedConnection); + w.addAction(args..., &w, &QObject::deleteLater, Qt::QueuedConnection); + // doesn't exist: w.addAction(args..., testFunction0, Qt::QueuedConnection); + w.addAction(args..., &w, testFunction0, Qt::QueuedConnection); + // doesn't exist: w.addAction(args..., testFunction1, Qt::QueuedConnection); + w.addAction(args..., &w, testFunction1, Qt::QueuedConnection); + // doesn't exist: w.addAction(args..., [&](bool b) { w.setEnabled(b); }, Qt::QueuedConnection); + w.addAction(args..., &w, [&](bool b) { w.setEnabled(b); }, Qt::QueuedConnection); + }; + const auto check1 = [&](auto &arg, auto &...args) { + check(arg, args...); + check(std::as_const(arg), args...); + }; + const auto check2 = [&](auto &arg1, auto &arg2, auto &...args) { + check1(arg1, arg2, args...); + check1(arg1, std::as_const(arg2), args...); + }; + [[maybe_unused]] + const auto check3 = [&](auto &arg1, auto &arg2, auto &arg3) { + check2(arg1, arg2, arg3); + check2(arg1, arg2, std::as_const(arg3)); + }; + + check1(text); + check2(icon, text); +#ifndef QT_NO_SHORTCUT + ImplicitlyConvertibleTo<QKeySequence> keySequence = {Qt::CTRL | Qt::Key_C}; + check2(text, keySequence); + check3(icon, text, keySequence); +#endif +} + void tst_QWidget::fontPropagation() { QScopedPointer<QWidget> testWidget(new QWidget); @@ -813,6 +1008,66 @@ void tst_QWidget::fontPropagation3() QCOMPARE(p.font().pointSize(), child->font().pointSize()); } +/*! + This tests that children that are added to a widget with an explicitly + defined font inherit that font correctly, merging (and overriding) + with the font that might be defined by the platform theme. +*/ +void tst_QWidget::fontPropagationDynamic() +{ + // override side effects from previous tests + QFont themedFont; + themedFont.setBold(true); + themedFont.setPointSize(42); + QApplication::setFont(themedFont, "QPropagationTestWidget"); + + QWidget parent; + QWidget firstChild(&parent); + + const QFont defaultFont = parent.font(); + QFont appFont = defaultFont; + appFont.setPointSize(72); + + // sanity check + QVERIFY(themedFont != defaultFont); + QVERIFY(themedFont != appFont); + + // palette propagates to existing children + parent.setFont(appFont); + QCOMPARE(firstChild.font().pointSize(), appFont.pointSize()); + + // palatte propagates to children added later + QWidget secondChild(&parent); + QCOMPARE(secondChild.font().pointSize(), appFont.pointSize()); + QWidget thirdChild; + QCOMPARE(thirdChild.font().pointSize(), defaultFont.pointSize()); + thirdChild.setParent(&parent); + QCOMPARE(thirdChild.font().pointSize(), appFont.pointSize()); + + // even if the child has an override in QApplication::font + QPropagationTestWidget themedChild; + themedChild.ensurePolished(); // needed for default font to be set up + QCOMPARE(themedChild.font().pointSize(), themedFont.pointSize()); + QCOMPARE(themedChild.font().bold(), themedFont.bold()); + themedChild.setParent(&parent); + QCOMPARE(themedChild.font().pointSize(), appFont.pointSize()); + QCOMPARE(themedChild.font().bold(), themedFont.bold()); + + // grand children as well + QPropagationTestWidget themedGrandChild; + themedGrandChild.setParent(&themedChild); + QCOMPARE(themedGrandChild.font().pointSize(), appFont.pointSize()); + QCOMPARE(themedGrandChild.font().bold(), themedFont.bold()); + + // child with own font attribute does not inherit from parent + QFont childFont = defaultFont; + childFont.setPointSize(9); + QWidget modifiedChild; + modifiedChild.setFont(childFont); + modifiedChild.setParent(&parent); + QCOMPARE(modifiedChild.font().pointSize(), childFont.pointSize()); +} + void tst_QWidget::palettePropagation() { QScopedPointer<QWidget> testWidget(new QWidget); @@ -852,10 +1107,13 @@ void tst_QWidget::palettePropagation() void tst_QWidget::palettePropagation2() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + // ! Note, the code below is executed in tst_QWidget's constructor. // QPalette palette; - // font.setColor(QPalette::ToolTipBase, QColor(12, 13, 14)); - // font.setColor(QPalette::Text, QColor(21, 22, 23)); + // palette.setColor(QPalette::ToolTipBase, QColor(12, 13, 14)); + // palette.setColor(QPalette::Text, QColor(21, 22, 23)); // qApp->setPalette(palette, "QPropagationTestWidget"); QScopedPointer<QWidget> root(new QWidget); @@ -954,6 +1212,68 @@ void tst_QWidget::palettePropagation2() QCOMPARE(child5->palette().color(QPalette::ToolTipText), sysPalButton); } +/*! + This tests that children that are added to a widget with an explicitly + defined palette inherit that palette correctly, merging (and overriding) + with the palette that might be defined by the platform theme. +*/ +void tst_QWidget::palettePropagationDynamic() +{ + // override side effects from previous tests + QPalette themedPalette; + themedPalette.setColor(QPalette::ToolTipBase, QColor(12, 13, 14)); + themedPalette.setColor(QPalette::Text, QColor(21, 22, 23)); + QApplication::setPalette(themedPalette, "QPropagationTestWidget"); + + QWidget parent; + QWidget firstChild(&parent); + + const QPalette defaultPalette = parent.palette(); + QPalette appPalette = defaultPalette; + const QColor appColor(1, 2, 3); + appPalette.setColor(QPalette::Text, appColor); + + // sanity check + QVERIFY(themedPalette != defaultPalette); + QVERIFY(themedPalette != appPalette); + + // palette propagates to existing children + parent.setPalette(appPalette); + QCOMPARE(firstChild.palette().color(QPalette::Text), appPalette.color(QPalette::Text)); + + // palatte propagates to children added later + QWidget secondChild(&parent); + QCOMPARE(secondChild.palette().color(QPalette::Text), appPalette.color(QPalette::Text)); + QWidget thirdChild; + QCOMPARE(thirdChild.palette().color(QPalette::Text), defaultPalette.color(QPalette::Text)); + thirdChild.setParent(&parent); + QCOMPARE(thirdChild.palette().color(QPalette::Text), appPalette.color(QPalette::Text)); + + // even if the child has an override in QApplication::palette + QPropagationTestWidget themedChild; + themedChild.ensurePolished(); // needed for default palette to be set up + QCOMPARE(themedChild.palette().color(QPalette::Text), themedPalette.color(QPalette::Text)); + QCOMPARE(themedChild.palette().color(QPalette::ToolTipBase), themedPalette.color(QPalette::ToolTipBase)); + themedChild.setParent(&parent); + QCOMPARE(themedChild.palette().color(QPalette::Text), appPalette.color(QPalette::Text)); + QCOMPARE(themedChild.palette().color(QPalette::ToolTipBase), themedPalette.color(QPalette::ToolTipBase)); + + // grand children as well + QPropagationTestWidget themedGrandChild; + themedGrandChild.setParent(&themedChild); + QCOMPARE(themedGrandChild.palette().color(QPalette::Text), appPalette.color(QPalette::Text)); + QCOMPARE(themedGrandChild.palette().color(QPalette::ToolTipBase), themedPalette.color(QPalette::ToolTipBase)); + + // child with own color does not inherit from parent + QPalette childPalette = defaultPalette; + childPalette.setColor(QPalette::Text, Qt::red); + QWidget modifiedChild; + modifiedChild.setPalette(childPalette); + modifiedChild.setParent(&parent); + QCOMPARE(modifiedChild.palette().color(QPalette::Text), childPalette.color(QPalette::Text)); + +} + void tst_QWidget::enabledPropagation() { QScopedPointer<QWidget> testWidget(new QWidget); @@ -1006,12 +1326,17 @@ void tst_QWidget::ignoreKeyEventsWhenDisabled_QTBUG27417() centerOnScreen(&lineEdit); lineEdit.setDisabled(true); lineEdit.show(); + QTest::ignoreMessage(QtWarningMsg, "Keyboard event not accepted by receiving widget"); + QTest::ignoreMessage(QtWarningMsg, "Keyboard event not accepted by receiving widget"); QTest::keyClick(&lineEdit, Qt::Key_A); QTRY_VERIFY(lineEdit.text().isEmpty()); } void tst_QWidget::properTabHandlingWhenDisabled_QTBUG27417() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QWidget widget; widget.setWindowTitle(__FUNCTION__); widget.setMinimumWidth(m_testWidgetSize.width()); @@ -1133,6 +1458,7 @@ void tst_QWidget::visible() QVERIFY( !childWidget->isVisible() ); testWidget->show(); + QVERIFY(testWidget->screen()); QVERIFY( testWidget->isVisible() ); QVERIFY( childWidget->isVisible() ); @@ -1230,7 +1556,7 @@ void tst_QWidget::visible_setWindowOpacity() QVERIFY( !testWidget->isVisible() ); testWidget->setWindowOpacity(0.5); -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined(Q_OS_WIN) QVERIFY(!::IsWindowVisible(winHandleOf(testWidget.data()))); #endif testWidget->setWindowOpacity(1.0); @@ -1420,7 +1746,7 @@ void tst_QWidget::mapFromAndTo() subWindow2->setGeometry(75, 75, 100, 100); subSubWindow->setGeometry(10, 10, 10, 10); -#if !defined(Q_OS_QNX) && !defined(Q_OS_WINRT) +#if !defined(Q_OS_QNX) //update visibility if (windowMinimized) { if (!windowHidden) { @@ -1454,6 +1780,8 @@ void tst_QWidget::mapFromAndTo() QCOMPARE(window.mapToGlobal(QPoint(-10, 0)), QPoint(90, 100)); QCOMPARE(window.mapToGlobal(QPoint(0, -10)), QPoint(100, 90)); QCOMPARE(window.mapToGlobal(QPoint(100, 100)), QPoint(200, 200)); + auto delta = window.mapToGlobal(QPointF(100.5, 100.5)) - QPointF(200.5, 200.5); + QVERIFY(qFuzzyIsNull(delta.manhattanLength())); QCOMPARE(window.mapToGlobal(QPoint(110, 100)), QPoint(210, 200)); QCOMPARE(window.mapToGlobal(QPoint(100, 110)), QPoint(200, 210)); QCOMPARE(window.mapFromGlobal(QPoint(100, 100)), QPoint(0, 0)); @@ -1462,6 +1790,8 @@ void tst_QWidget::mapFromAndTo() QCOMPARE(window.mapFromGlobal(QPoint(90, 100)), QPoint(-10, 0)); QCOMPARE(window.mapFromGlobal(QPoint(100, 90)), QPoint(0, -10)); QCOMPARE(window.mapFromGlobal(QPoint(200, 200)), QPoint(100, 100)); + delta = window.mapFromGlobal(QPointF(200.5, 200.5)) - QPointF(100.5, 100.5); + QVERIFY(qFuzzyIsNull(delta.manhattanLength())); QCOMPARE(window.mapFromGlobal(QPoint(210, 200)), QPoint(110, 100)); QCOMPARE(window.mapFromGlobal(QPoint(200, 210)), QPoint(100, 110)); QCOMPARE(window.mapToParent(QPoint(0, 0)), QPoint(100, 100)); @@ -1572,6 +1902,7 @@ void tst_QWidget::focusChainOnReparent() } QWidget window2; + child22->setParent(child21); child2->setParent(&window2); QWidget *expectedNewChain[5] = {&window2, child2, child21, child22, &window2}; @@ -1612,8 +1943,6 @@ void tst_QWidget::focusChainOnHide() QWidget::setTabOrder(child, parent.data()); parent->show(); - QApplication::setActiveWindow(parent->window()); - child->activateWindow(); child->setFocus(); QTRY_VERIFY(child->hasFocus()); @@ -1654,8 +1983,11 @@ public: setObjectName(name); lineEdit1 = new QLineEdit; + lineEdit1->setObjectName(name + "/lineEdit1"); lineEdit2 = new QLineEdit; + lineEdit2->setObjectName(name + "/lineEdit2"); lineEdit3 = new QLineEdit; + lineEdit3->setObjectName(name + "/lineEdit3"); lineEdit3->setEnabled(false); QHBoxLayout* hbox = new QHBoxLayout(this); @@ -1670,8 +2002,117 @@ public: QLineEdit *lineEdit3; }; +static QList<QWidget *> getFocusChain(QWidget *start, bool bForward) +{ + QList<QWidget *> ret; + QWidget *cur = start; + // detect infinite loop + int count = 100; + auto loopGuard = qScopeGuard([]{ + QFAIL("Inifinite loop detected in focus chain"); + }); + do { + ret += cur; + cur = bForward ? cur->nextInFocusChain() : cur->previousInFocusChain(); + if (!--count) + return ret; + } while (cur != start); + loopGuard.dismiss(); + return ret; +} + +void tst_QWidget::focusAbstraction() +{ + QLoggingCategory::setFilterRules("qt.widgets.focus=true"); + QWidget *widget1 = new QWidget; + widget1->setObjectName("Widget 1"); + QWidget *widget2 = new QWidget; + widget2->setObjectName("Widget 2"); + QWidget *widget3 = new QWidget; + widget3->setObjectName("Widget 3"); + QWidgetPrivate *priv1 = QWidgetPrivate::get(widget1); + QWidgetPrivate *priv2 = QWidgetPrivate::get(widget2); + QWidgetPrivate *priv3 = QWidgetPrivate::get(widget3); + + // Verify initialization + QVERIFY(!priv1->isInFocusChain()); + QVERIFY(!priv2->isInFocusChain()); + QVERIFY(!priv3->isInFocusChain()); + + // Verify, that parenting builds a focus chain. + QWidget parent; + parent.setObjectName("Parent"); + widget1->setParent(&parent); + widget2->setParent(&parent); + widget3->setParent(&parent); + QVERIFY(priv1->isInFocusChain()); + QVERIFY(priv2->isInFocusChain()); + QVERIFY(priv3->isInFocusChain()); + QWidgetList expected{widget1, widget2, widget3, &parent}; + QCOMPARE(getFocusChain(widget1, true), expected); + + // Verify, that reparented focus children end up behind parent. + widget1->setParent(widget2); + priv2->insertIntoFocusChainAfter(widget3); + priv2->reparentFocusChildren(QWidgetPrivate::FocusDirection::Next); + expected = {widget1, &parent, widget3, widget2}; + QCOMPARE(getFocusChain(widget1, true), expected); + QVERIFY(priv1->isInFocusChain()); + QVERIFY(priv2->isInFocusChain()); + QVERIFY(priv3->isInFocusChain()); + + // Check removal + priv3->removeFromFocusChain(QWidgetPrivate::FocusChainRemovalRule::AssertConsistency); + expected.removeOne(widget3); + QCOMPARE(getFocusChain(widget1, true), expected); + QVERIFY(priv1->isInFocusChain()); + QVERIFY(priv2->isInFocusChain()); + QVERIFY(!priv3->isInFocusChain()); + + // Check insert + priv3->insertIntoFocusChain(QWidgetPrivate::FocusDirection::Previous, widget1); + expected = {widget3, widget1, &parent, widget2}; + QCOMPARE(getFocusChain(widget3, true), expected); + + // Verify, that take doesn't break + const QWidgetList taken = QWidgetPrivate::takeFromFocusChain(widget1, widget2); + QVERIFY(priv1->isFocusChainConsistent()); + expected = {widget1, &parent, widget2}; + QCOMPARE(taken, expected); + QVERIFY(priv1->isInFocusChain()); + QVERIFY(priv2->isInFocusChain()); + QVERIFY(!priv3->isInFocusChain()); + + // Verify insertion of multiple widgets + QWidgetPrivate::insertIntoFocusChain(taken, QWidgetPrivate::FocusDirection::Next, widget3); + expected = {widget3, widget1, &parent, widget2}; + QCOMPARE(getFocusChain(widget3, true), expected); + QVERIFY(priv1->isInFocusChain()); + QVERIFY(priv2->isInFocusChain()); + QVERIFY(priv2->isInFocusChain()); + + // Verify broken chain identification + // d'tor asserts chain consistency => repair before going out of scope + auto guard = qScopeGuard([priv2, widget3]{ priv2->focus_next = widget3; }); + + // Nullptr is not allowed + priv2->focus_next = nullptr; + QVERIFY(!priv1->isFocusChainConsistent()); + + // Chain looping back in the middle + priv2->focus_next = widget1; + QVERIFY(!priv1->isFocusChainConsistent()); + + // "last" element pointing to itself + priv2->focus_next = widget2; + QVERIFY(!priv1->isFocusChainConsistent()); +} + void tst_QWidget::defaultTabOrder() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + const int compositeCount = 2; Container container; Composite *composite[compositeCount]; @@ -1690,7 +2131,6 @@ void tst_QWidget::defaultTabOrder() container.setWindowTitle(QLatin1String(QTest::currentTestFunction())); container.show(); container.activateWindow(); - QApplication::setActiveWindow(&container); QVERIFY(QTest::qWaitForWindowActive(&container)); QTRY_VERIFY(firstEdit->hasFocus()); @@ -1726,20 +2166,29 @@ void tst_QWidget::defaultTabOrder() void tst_QWidget::reverseTabOrder() { + if (QGuiApplication::platformName().startsWith(QLatin1StringView("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + const int compositeCount = 2; Container container; - container.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + container.setObjectName(QLatin1StringView("Container")); + container.setWindowTitle(QLatin1StringView(QTest::currentTestFunction())); Composite* composite[compositeCount]; QLineEdit *firstEdit = new QLineEdit(); + firstEdit->setObjectName(QLatin1StringView("FirstEdit")); container.box->addWidget(firstEdit); + static constexpr QLatin1StringView comp("Composite-%1"); for (int i = 0; i < compositeCount; i++) { - composite[i] = new Composite(); + const QString name = QString(comp).arg(i); + composite[i] = new Composite(nullptr, name); + composite[i]->setObjectName(name); container.box->addWidget(composite[i]); } QLineEdit *lastEdit = new QLineEdit(); + lastEdit->setObjectName(QLatin1StringView("LastEdit")); container.box->addWidget(lastEdit); // Reverse tab order inside each composite @@ -1748,7 +2197,6 @@ void tst_QWidget::reverseTabOrder() container.show(); container.activateWindow(); - QApplication::setActiveWindow(&container); QVERIFY(QTest::qWaitForWindowActive(&container)); QTRY_VERIFY(firstEdit->hasFocus()); @@ -1783,8 +2231,144 @@ void tst_QWidget::reverseTabOrder() QVERIFY(firstEdit->hasFocus()); } +void tst_QWidget::tabOrderList() +{ + Composite c; + QCOMPARE(getFocusChain(&c, true), + QList<QWidget *>({&c, c.lineEdit1, c.lineEdit2, c.lineEdit3})); + QWidget::setTabOrder({c.lineEdit3, c.lineEdit2, c.lineEdit1}); + // not starting with 3 like one would maybe expect, but still 3, 2, 1 + QCOMPARE(getFocusChain(&c, true), + QList<QWidget *>({&c, c.lineEdit1, c.lineEdit3, c.lineEdit2})); +} + +void tst_QWidget::tabOrderComboBox_data() +{ + QTest::addColumn<const bool>("editableAtBeginning"); + QTest::addColumn<const QList<int>>("firstTabOrder"); + QTest::addColumn<const QList<int>>("secondTabOrder"); + + QTest::addRow("3 not editable") << false << QList<int>{2, 1, 0} << QList<int>{0, 1, 2}; + QTest::addRow("4 editable") << true << QList<int>{2, 1, 0, 3} << QList<int>{3, 0, 2, 1}; +} + +QWidgetList expectedFocusChain(const QList<QComboBox *> &boxes, const QList<int> &sequence) +{ + Q_ASSERT(boxes.count() == sequence.count()); + QWidgetList widgets; + for (int i : sequence) { + Q_ASSERT(i >= 0); + Q_ASSERT(i < boxes.count()); + QComboBox *box = boxes.at(i); + widgets.append(box); + if (box->lineEdit()) + widgets.append(box->lineEdit()); + } + + return widgets; +} + +QWidgetList realFocusChain(const QList<QComboBox *> &boxes, const QList<int> &sequence) +{ + const QWidgetList all = getFocusChain(boxes.at(sequence.at(0)), true); + QWidgetList chain; + // Filter everything with NoFocus + for (auto *widget : all) { + if (widget->focusPolicy() != Qt::NoFocus) + chain << widget; + } + return chain; +} + +void setTabOrder(const QList<QComboBox *> &boxes, const QList<int> &sequence) +{ + Q_ASSERT(boxes.count() == sequence.count()); + QWidget *previous = nullptr; + for (int i : sequence) { + Q_ASSERT(i >= 0); + Q_ASSERT(i < boxes.count()); + QWidget *box = boxes.at(i); + if (!previous) { + previous = box; + } else { + QWidget::setTabOrder(previous, box); + previous = box; + } + } +} + +void tst_QWidget::tabOrderComboBox() +{ + QFETCH(const bool, editableAtBeginning); + QFETCH(const QList<int>, firstTabOrder); + QFETCH(const QList<int>, secondTabOrder); + const int count = firstTabOrder.count(); + Q_ASSERT(count == secondTabOrder.count()); + Q_ASSERT(count > 1); + + QWidget w; + w.setObjectName("MainWidget"); + QVBoxLayout* layout = new QVBoxLayout(); + w.setLayout(layout); + + QList<QComboBox *> boxes; + for (int i = 0; i < count; ++i) { + auto box = new QComboBox; + box->setObjectName("ComboBox " + QString::number(i)); + if (editableAtBeginning) { + box->setEditable(true); + box->lineEdit()->setObjectName("LineEdit " + QString::number(i)); + } + boxes.append(box); + layout->addWidget(box); + } + layout->addStretch(); + +#define COMPARE(seq)\ + setTabOrder(boxes, seq);\ + QCOMPARE(realFocusChain(boxes, seq), expectedFocusChain(boxes, seq)) + + COMPARE(firstTabOrder); + + if (!editableAtBeginning) { + for (auto *box : boxes) + box->setEditable(box); + } + + COMPARE(secondTabOrder); + + // Remove the focus proxy of the first combobox's line edit. + QComboBox *box = boxes.at(0); + QLineEdit *lineEdit = box->lineEdit(); + const QWidget *prev = lineEdit->previousInFocusChain(); + const QWidget *next = lineEdit->nextInFocusChain(); + const QWidget *proxy = lineEdit->focusProxy(); + QCOMPARE(proxy, box); + lineEdit->setFocusProxy(nullptr); + QCOMPARE(lineEdit->focusProxy(), nullptr); + QCOMPARE(lineEdit->previousInFocusChain(), prev); + QCOMPARE(lineEdit->nextInFocusChain(), next); + + // Remove first item and check chain consistency + boxes.removeFirst(); + delete box; + + // Create new list with 0 removed and other indexes updated + QList<int> thirdTabOrder(secondTabOrder); + thirdTabOrder.removeIf([](int i){ return i == 0; }); + for (int &i : thirdTabOrder) + --i; + + COMPARE(thirdTabOrder); + +#undef COMPARE +} + void tst_QWidget::tabOrderWithProxy() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + const int compositeCount = 2; Container container; container.setWindowTitle(QLatin1String(QTest::currentTestFunction())); @@ -1807,7 +2391,6 @@ void tst_QWidget::tabOrderWithProxy() container.show(); container.activateWindow(); - QApplication::setActiveWindow(&container); QVERIFY(QTest::qWaitForWindowActive(&container)); QTRY_VERIFY(firstEdit->hasFocus()); @@ -1841,8 +2424,86 @@ void tst_QWidget::tabOrderWithProxy() QVERIFY(firstEdit->hasFocus()); } +static QString focusWidgetName() +{ + return QApplication::focusWidget() != nullptr + ? QApplication::focusWidget()->objectName() + : QStringLiteral("No focus widget"); +} + +void tst_QWidget::tabOrderWithProxyDisabled() +{ + Container container; + container.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + + QLineEdit lineEdit1; + lineEdit1.setObjectName("lineEdit1"); + + QWidget containingWidget; + containingWidget.setFocusPolicy(Qt::StrongFocus); + auto *containingLayout = new QVBoxLayout; + QLineEdit lineEdit2; + lineEdit2.setObjectName("lineEdit2"); + QLineEdit lineEdit3; + lineEdit3.setObjectName("lineEdit3"); + containingLayout->addWidget(&lineEdit2); + containingLayout->addWidget(&lineEdit3); + containingWidget.setLayout(containingLayout); + containingWidget.setFocusProxy(&lineEdit2); + lineEdit2.setEnabled(false); + + container.box->addWidget(&lineEdit1); + container.box->addWidget(&containingWidget); + + container.show(); + container.activateWindow(); + + if (!QTest::qWaitForWindowActive(&container)) + QSKIP("Window failed to activate, skipping test"); + + QVERIFY2(lineEdit1.hasFocus(), + qPrintable(focusWidgetName())); + container.tab(); + QVERIFY2(!lineEdit2.hasFocus(), + qPrintable(focusWidgetName())); + QVERIFY2(lineEdit3.hasFocus(), + qPrintable(focusWidgetName())); + container.tab(); + QVERIFY2(lineEdit1.hasFocus(), + qPrintable(focusWidgetName())); + container.backTab(); + QVERIFY2(lineEdit3.hasFocus(), + qPrintable(focusWidgetName())); + container.backTab(); + QVERIFY2(!lineEdit2.hasFocus(), + qPrintable(focusWidgetName())); + QVERIFY2(lineEdit1.hasFocus(), + qPrintable(focusWidgetName())); +} + +//#define DEBUG_FOCUS_CHAIN +static void dumpFocusChain(QWidget *start, bool bForward, const char *desc = nullptr) +{ +#ifdef DEBUG_FOCUS_CHAIN + qDebug() << "Dump focus chain, start:" << start << "isForward:" << bForward << desc; + QWidget *cur = start; + do { + qDebug() << "-" << cur; + auto widgetPrivate = static_cast<QWidgetPrivate *>(qt_widget_private(cur)); + cur = bForward ? widgetPrivate->focus_next : widgetPrivate->focus_prev; + } while (cur != start); +#else + Q_UNUSED(start); + Q_UNUSED(bForward); + Q_UNUSED(desc); +#endif +} + void tst_QWidget::tabOrderWithCompoundWidgets() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + const int compositeCount = 4; Container container; container.setWindowTitle(QLatin1String(QTest::currentTestFunction())); @@ -1878,7 +2539,6 @@ void tst_QWidget::tabOrderWithCompoundWidgets() container.show(); container.activateWindow(); - QApplication::setActiveWindow(&container); QVERIFY(QTest::qWaitForWindowActive(&container)); lastEdit->setFocus(); @@ -1929,34 +2589,224 @@ void tst_QWidget::tabOrderWithCompoundWidgets() QVERIFY(lastEdit->hasFocus()); } -static QVector<QWidget*> getFocusChain(QWidget *start, bool bForward) +void tst_QWidget::tabOrderWithProxyOutOfOrder() { - QVector<QWidget*> ret; - QWidget *cur = start; - do { - ret += cur; - auto widgetPrivate = static_cast<QWidgetPrivate *>(qt_widget_private(cur)); - cur = bForward ? widgetPrivate->focus_next : widgetPrivate->focus_prev; - } while (cur != start); - return ret; + Container container; + container.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + container.setObjectName(QLatin1StringView("Container")); + + // important to create the widgets with parent so that they are + // added to the focus chain already now, and with the buttonBox + // before the outsideButton. + QWidget buttonBox(&container); + buttonBox.setObjectName(QLatin1StringView("buttonBox")); + QPushButton outsideButton(&container); + outsideButton.setObjectName(QLatin1StringView("outsideButton")); + + container.box->addWidget(&outsideButton); + container.box->addWidget(&buttonBox); + QCOMPARE(getFocusChain(&container, true), + QList<QWidget*>({&container, &buttonBox, &outsideButton})); + + // this now adds okButon and cancelButton to the focus chain, + // after the outsideButton - so the outsideButton is in between + // the buttonBox and the children of the buttonBox! + QPushButton okButton(&buttonBox); + okButton.setObjectName("okButton"); + QPushButton cancelButton(&buttonBox); + cancelButton.setObjectName("cancelButton"); + QCOMPARE(getFocusChain(&container, true), + QList<QWidget*>({&container, &buttonBox, &outsideButton, &okButton, &cancelButton})); + + // by setting the okButton as the focusProxy, the outsideButton becomes + // unreachable when navigating the focus chain as the buttonBox is in front + // of, and proxies to the okButton behind the outsideButton. setFocusProxy + // must fix that by moving the buttonBox in front of the first sibling of + // the proxy. + buttonBox.setFocusProxy(&okButton); + QCOMPARE(getFocusChain(&container, true), + QList<QWidget*>({&container, &outsideButton, &buttonBox, &okButton, &cancelButton})); + + container.show(); + container.activateWindow(); + if (!QTest::qWaitForWindowActive(&container)) + QSKIP("Window failed to activate, skipping test"); + + QCOMPARE(QApplication::focusWidget(), &outsideButton); + container.tab(); + QCOMPARE(QApplication::focusWidget(), &okButton); + container.tab(); + QCOMPARE(QApplication::focusWidget(), &cancelButton); + container.tab(); + QCOMPARE(QApplication::focusWidget(), &outsideButton); + + container.backTab(); + QCOMPARE(QApplication::focusWidget(), &cancelButton); + container.backTab(); + QCOMPARE(QApplication::focusWidget(), &okButton); + container.backTab(); + QCOMPARE(QApplication::focusWidget(), &outsideButton); + container.backTab(); + QCOMPARE(QApplication::focusWidget(), &cancelButton); } -//#define DEBUG_FOCUS_CHAIN -static void dumpFocusChain(QWidget *start, bool bForward, const char *desc = nullptr) +static bool isFocusChainConsistent(QWidget *widget) { -#ifdef DEBUG_FOCUS_CHAIN - qDebug() << "Dump focus chain, start:" << start << "isForward:" << bForward << desc; - QWidget *cur = start; - do { - qDebug() << cur; - auto widgetPrivate = static_cast<QWidgetPrivate *>(qt_widget_private(cur)); - cur = bForward ? widgetPrivate->focus_next : widgetPrivate->focus_prev; - } while (cur != start); -#else - Q_UNUSED(start) - Q_UNUSED(bForward) - Q_UNUSED(desc) -#endif + auto forward = getFocusChain(widget, true); + auto backward = getFocusChain(widget, false); + auto logger = qScopeGuard([=]{ + qCritical("Focus chain is not consistent!"); + qWarning() << forward.size() << "forwards: " << forward; + qWarning() << backward.size() << "backwards:" << backward; + }); + // both lists start with the same, the widget + if (forward.takeFirst() != backward.takeFirst()) + return false; + const qsizetype chainLength = forward.size(); + if (backward.size() != chainLength) + return false; + for (qsizetype i = 0; i < chainLength; ++i) { + if (forward.at(i) != backward.at(chainLength - i - 1)) + return false; + } + logger.dismiss(); + return true; +} + +/* + This tests that we end up with consistent and complete chains when we set + the tab order from a widget (the lineEdit) inside a compound (the tabWidget) + to the compound, or visa versa. In that case, QWidget::setTabOrder will walk + the focus chain to the focus child inside the compound to replace the compound + itself when manipulating the tab order. If that last focus child is then + however also the lineEdit, then we must not create an inconsistent or + incomplete loop. + + The tabWidget is seen as a compound because QTabWidget sets the tab bar as + the focus proxy, and it has more widgets inside, like pages, toolbuttons etc. +*/ +void tst_QWidget::tabOrderWithCompoundWidgetsInflection_data() +{ + QTest::addColumn<QByteArrayList>("tabOrder"); + + QTest::addRow("forward") + << QByteArrayList{"dialog", "tabWidget", "lineEdit", "compound", "okButton", "cancelButton"}; + QTest::addRow("backward") + << QByteArrayList{"dialog", "cancelButton", "okButton", "compound", "lineEdit", "tabWidget"}; +} + +void tst_QWidget::tabOrderWithCompoundWidgetsInflection() +{ + QFETCH(const QByteArrayList, tabOrder); + + QDialog dialog; + dialog.setObjectName("dialog"); + QTabWidget *tabWidget = new QTabWidget; + tabWidget->setObjectName("tabWidget"); + tabWidget->setFocusPolicy(Qt::TabFocus); + QWidget *page = new QWidget; + page->setObjectName("page"); + QLineEdit *lineEdit = new QLineEdit; + lineEdit->setObjectName("lineEdit"); + QWidget *compound = new QWidget; + compound->setObjectName("compound"); + compound->setFocusPolicy(Qt::TabFocus); + QPushButton *okButton = new QPushButton("Ok"); + okButton->setObjectName("okButton"); + okButton->setFocusPolicy(Qt::TabFocus); + QPushButton *cancelButton = new QPushButton("Cancel"); + cancelButton->setObjectName("cancelButton"); + cancelButton->setFocusPolicy(Qt::TabFocus); + + QVBoxLayout *pageLayout = new QVBoxLayout; + pageLayout->addWidget(lineEdit); + page->setLayout(pageLayout); + tabWidget->addTab(page, "Tab"); + + QHBoxLayout *compoundLayout = new QHBoxLayout; + compoundLayout->addStretch(); + compoundLayout->addWidget(cancelButton); + compoundLayout->addWidget(okButton); + compound->setFocusProxy(okButton); + compound->setLayout(compoundLayout); + + QVBoxLayout *dialogLayout = new QVBoxLayout; + dialogLayout->addWidget(tabWidget); + dialogLayout->addWidget(compound); + dialog.setLayout(dialogLayout); + + QVERIFY(isFocusChainConsistent(&dialog)); + + QList<QWidget *> expectedFocusChain; + for (qsizetype i = 0; i < tabOrder.size() - 1; ++i) { + QWidget *first = dialog.findChild<QWidget *>(tabOrder.at(i)); + if (!first && tabOrder.at(i) == dialog.objectName()) + first = &dialog; + QVERIFY(first); + if (i == 0) + expectedFocusChain.append(first); + QWidget *second = dialog.findChild<QWidget *>(tabOrder.at(i + 1)); + QVERIFY(second); + expectedFocusChain.append(second); + QWidget::setTabOrder(first, second); + QVERIFY(isFocusChainConsistent(&dialog)); + } + + const auto forwardChain = getFocusChain(&dialog, true); + auto logger = qScopeGuard([=]{ + qCritical("Order of widgets in focus chain not matching:"); + qCritical() << " Actual :" << forwardChain; + qCritical() << " Expected:" << expectedFocusChain; + }); + for (qsizetype i = 0; i < expectedFocusChain.size() - 2; ++i) { + QCOMPARE_LT(forwardChain.indexOf(expectedFocusChain.at(i)), + forwardChain.indexOf(expectedFocusChain.at(i + 1))); + } + logger.dismiss(); +} + +void tst_QWidget::tabOrderWithCompoundWidgetsNoFocusPolicy() +{ + Container container; + container.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + QSpinBox spinbox1; + spinbox1.setObjectName("spinbox1"); + QSpinBox spinbox2; + spinbox2.setObjectName("spinbox2"); + QSpinBox spinbox3; + spinbox3.setObjectName("spinbox3"); + + spinbox1.setFocusPolicy(Qt::StrongFocus); + spinbox2.setFocusPolicy(Qt::NoFocus); + spinbox3.setFocusPolicy(Qt::StrongFocus); + container.box->addWidget(&spinbox1); + container.box->addWidget(&spinbox2); + container.box->addWidget(&spinbox3); + + container.show(); + container.activateWindow(); + + if (!QTest::qWaitForWindowActive(&container)) + QSKIP("Window failed to activate, skipping test"); + + QVERIFY2(spinbox1.hasFocus(), + qPrintable(focusWidgetName())); + container.tab(); + QVERIFY2(!spinbox2.hasFocus(), + qPrintable(focusWidgetName())); + QVERIFY2(spinbox3.hasFocus(), + qPrintable(focusWidgetName())); + container.tab(); + QVERIFY2(spinbox1.hasFocus(), + qPrintable(focusWidgetName())); + container.backTab(); + QVERIFY2(spinbox3.hasFocus(), + qPrintable(focusWidgetName())); + container.backTab(); + QVERIFY2(!spinbox2.hasFocus(), + qPrintable(focusWidgetName())); + QVERIFY2(spinbox1.hasFocus(), + qPrintable(focusWidgetName())); } void tst_QWidget::tabOrderNoChange() @@ -2029,7 +2879,115 @@ void tst_QWidget::tabOrderNoChange2() QCOMPARE(focusChainBackward, getFocusChain(&w, false)); } -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +void tst_QWidget::appFocusWidgetWithFocusProxyLater() +{ + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + + // Given a lineedit without a focus proxy + QWidget window; + window.setWindowTitle(QTest::currentTestFunction()); + QLineEdit *lineEditFocusProxy = new QLineEdit(&window); + QLineEdit *lineEdit = new QLineEdit(&window); + lineEdit->setFocus(); + window.show(); + QVERIFY(QTest::qWaitForWindowActive(&window)); + QCOMPARE(QApplication::focusWidget(), lineEdit); + + // When setting a focus proxy for the focus widget (like QWebEngineView does) + lineEdit->setFocusProxy(lineEditFocusProxy); + + // Then the focus widget should be updated + QCOMPARE(QApplication::focusWidget(), lineEditFocusProxy); + + // So that deleting the lineEdit and later the window, doesn't crash + delete lineEdit; + QCOMPARE(QApplication::focusWidget(), nullptr); +} + +void tst_QWidget::appFocusWidgetWhenLosingFocusProxy() +{ + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + + // Given a lineedit with a focus proxy + QWidget window; + window.setWindowTitle(QTest::currentTestFunction()); + QLineEdit *lineEditFocusProxy = new QLineEdit(&window); + QLineEdit *lineEdit = new QLineEdit(&window); + lineEdit->setFocusProxy(lineEditFocusProxy); + lineEdit->setFocus(); + window.show(); + QVERIFY(QTest::qWaitForWindowActive(&window)); + QCOMPARE(QApplication::focusWidget(), lineEditFocusProxy); + QVERIFY(lineEdit->hasFocus()); + QVERIFY(lineEditFocusProxy->hasFocus()); + + // When unsetting the focus proxy + lineEdit->setFocusProxy(nullptr); + + // then the focus widget should not change + QCOMPARE(QApplication::focusWidget(), lineEditFocusProxy); + QVERIFY(!lineEdit->hasFocus()); + QVERIFY(lineEditFocusProxy->hasFocus()); +} + +void tst_QWidget::explicitTabOrderWithComplexWidget() +{ + // Check that handling tab/backtab with a widget comprimised of other widgets + // handles tabbing correctly + Container window; + auto lineEditOne = new QLineEdit; + window.box->addWidget(lineEditOne); + auto lineEditTwo = new QLineEdit; + window.box->addWidget(lineEditTwo); + QWidget::setTabOrder(lineEditOne, lineEditTwo); + lineEditOne->setFocus(); + window.show(); + QVERIFY(QTest::qWaitForWindowActive(&window)); + QTRY_COMPARE(QApplication::focusWidget(), lineEditOne); + + window.tab(); + QTRY_COMPARE(QApplication::focusWidget(), lineEditTwo); + window.tab(); + QTRY_COMPARE(QApplication::focusWidget(), lineEditOne); + window.backTab(); + QTRY_COMPARE(QApplication::focusWidget(), lineEditTwo); + window.backTab(); + QTRY_COMPARE(QApplication::focusWidget(), lineEditOne); +} + +void tst_QWidget::explicitTabOrderWithSpinBox_QTBUG81097() +{ + // Check the special case of QAbstractSpinBox-like widgets, that have a + // child widget with a focusPolicy() set to its parent. + Container window; + auto spinBoxOne = new QDoubleSpinBox; + auto spinBoxTwo = new QDoubleSpinBox; + auto lineEdit = new QLineEdit; + window.box->addWidget(spinBoxOne); + window.box->addWidget(spinBoxTwo); + window.box->addWidget(lineEdit); + QWidget::setTabOrder(spinBoxOne, spinBoxTwo); + QWidget::setTabOrder(spinBoxTwo, lineEdit); + spinBoxOne->setFocus(); + window.show(); + QVERIFY(QTest::qWaitForWindowActive(&window)); + QTRY_COMPARE(QApplication::focusWidget(), spinBoxOne); + + window.tab(); + QTRY_COMPARE(QApplication::focusWidget(), spinBoxTwo); + window.tab(); + QTRY_COMPARE(QApplication::focusWidget(), lineEdit); + window.backTab(); + QTRY_COMPARE(QApplication::focusWidget(), spinBoxTwo); + window.backTab(); + QTRY_COMPARE(QApplication::focusWidget(), spinBoxOne); + window.backTab(); + QTRY_COMPARE(QApplication::focusWidget(), lineEdit); +} + +#if defined(Q_OS_WIN) void tst_QWidget::activation() { Q_CHECK_PAINTEVENTS @@ -2060,9 +3018,27 @@ void tst_QWidget::activation() } #endif // Q_OS_WIN +struct WindowStateChangeWatcher : public QObject +{ + WindowStateChangeWatcher(QWidget *widget) + { + Q_ASSERT(widget->window()->windowHandle()); + widget->window()->windowHandle()->installEventFilter(this); + lastWindowStates = widget->window()->windowHandle()->windowState(); + } + Qt::WindowStates lastWindowStates; +protected: + bool eventFilter(QObject *receiver, QEvent *event) override + { + if (event->type() == QEvent::WindowStateChange) + lastWindowStates = static_cast<QWindow *>(receiver)->windowState(); + return QObject::eventFilter(receiver, event); + } +}; + void tst_QWidget::windowState() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QSKIP("QTBUG-52974"); #endif @@ -2073,13 +3049,14 @@ void tst_QWidget::windowState() QPoint pos; QSize size = m_testWidgetSize; - if (QGuiApplicationPrivate::platformIntegration()->defaultWindowState(Qt::Widget) - == Qt::WindowFullScreen - || m_platform == QStringLiteral("winrt")) { + const Qt::WindowState defaultWidgetState = + QGuiApplicationPrivate::platformIntegration()->defaultWindowState(Qt::Widget); + if (defaultWidgetState == Qt::WindowFullScreen) size = QGuiApplication::primaryScreen()->size(); - } else { + else if (defaultWidgetState == Qt::WindowMaximized) + size = QGuiApplication::primaryScreen()->availableSize(); + else pos = QPoint(10, 10); - } QWidget widget1; widget1.move(pos); @@ -2194,6 +3171,119 @@ void tst_QWidget::windowState() QTRY_COMPARE(widget1.size(), size); } +// Test propagation of size and state from platform window to QWidget +// Windows and linux/XCB only +void tst_QWidget::resizePropagation() +{ +#if !defined(Q_OS_LINUX) && !defined(Q_OS_WIN) + QSKIP("resizePropagation test is designed for Linux/XCB and Windows only"); +#endif + const bool xcb = (m_platform == QStringLiteral("xcb")); +#ifdef Q_OS_LINUX + if (!xcb) + QSKIP("resizePropagation test is designed for XCB only"); +#endif + + // Windows: + // When a widget is maximized after it has been resized, the widget retains its original size, + // while the window shows maximum size. + // windowStateChanged signal gets fired on a no-op change from/to WindowNoState + + // Initialize widget and signal spy for window handle + QWidget widget; + widget.showMaximized(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + QWindow *window = widget.windowHandle(); + QTRY_VERIFY(window); + QSignalSpy spy(window, &QWindow::windowStateChanged); + int count = 0; + + const QSize screenSize = QGuiApplication::primaryScreen()->size(); + const QSize size1 = QSize(screenSize.width() * 0.5, screenSize.height() * 0.5); + const QSize size2 = QSize(screenSize.width() * 0.625, screenSize.height() * 0.833); + + enum CountIncrementCheck {Equal, Greater}; + enum TargetSizeCheck {Fail, Warn}; + auto verifyResize = [&](const QSize &size, Qt::WindowState windowState, + CountIncrementCheck checkCountIncrement, + TargetSizeCheck checkTargetSize) + { + // Capture count of latest async signals + if (checkCountIncrement == Equal) + count = spy.count(); + + // Resize if required + if (size.isValid()) + widget.resize(size); + + // Wait for the widget anyway + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + + // Check signal count and qDebug output for fail analysis + switch (checkCountIncrement) { + case Greater: { + auto logger = qScopeGuard([&](){ + qDebug() << "spy count:" << spy.count() << "previous count:" << count; + }); + QTRY_VERIFY(spy.count() > count); + logger.dismiss(); + count = spy.count(); + } + break; + case Equal: { + auto logger = qScopeGuard([&](){ + qDebug() << spy << widget.windowState() << window->windowState(); + }); + QCOMPARE(spy.count(), count); + logger.dismiss(); + } + break; + } + + // QTRY necessary because state changes are propagated async + QTRY_COMPARE(widget.windowState(), windowState); + QTRY_COMPARE(window->windowState(), windowState); + + // Check target size with fail or warning + switch (checkTargetSize) { + case Fail: + QCOMPARE(widget.size(), window->size()); + break; + case Warn: + if (widget.size() != window->size()) { + qWarning() << m_platform << "size mismtach tolerated. Widget:" + << widget.size() << "Window:" << window->size(); + } + break; + } + }; + + // test state and size consistency of maximized window + verifyResize(QSize(), Qt::WindowMaximized, Equal, Fail); + if (QTest::currentTestFailed()) + return; + + // test state transition, state and size consistency after resize + verifyResize(size1, Qt::WindowNoState, Greater, xcb ? Warn : Fail ); + if (QTest::currentTestFailed()) + return; + + // test unchanged state, state and size consistency after resize + verifyResize(size2, Qt::WindowNoState, Equal, xcb ? Warn : Fail); + if (QTest::currentTestFailed()) + return; + + // test state transition, state and size consistency after maximize + widget.showMaximized(); + verifyResize(QSize(), Qt::WindowMaximized, Greater, xcb ? Fail : Warn); + if (QTest::currentTestFailed()) + return; + +#ifdef Q_OS_WIN + QCOMPARE(widget.size(), size2); +#endif +} + void tst_QWidget::showMaximized() { QWidget plain; @@ -2275,12 +3365,10 @@ void tst_QWidget::showMaximized() void tst_QWidget::showFullScreen() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QSKIP("QTBUG-52974"); #endif - if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT: This fails. QTBUG-68297"); QWidget plain; QHBoxLayout *layout; QWidget layouted; @@ -2385,7 +3473,7 @@ void tst_QWidget::resizeEvent() wParent.setWindowTitle(QLatin1String(QTest::currentTestFunction())); wParent.resize(m_testWidgetSize); ResizeWidget wChild(&wParent); - wParent.show(); + QTestPrivate::androidCompatibleShow(&wParent); QVERIFY(QTest::qWaitForWindowExposed(&wParent)); QCOMPARE (wChild.m_resizeEventCount, 1); // initial resize event before paint wParent.hide(); @@ -2394,7 +3482,7 @@ void tst_QWidget::resizeEvent() safeSize.setWidth(639); wChild.resize(safeSize); QCOMPARE (wChild.m_resizeEventCount, 1); - wParent.show(); + QTestPrivate::androidCompatibleShow(&wParent); QCOMPARE (wChild.m_resizeEventCount, 2); } @@ -2402,10 +3490,8 @@ void tst_QWidget::resizeEvent() ResizeWidget wTopLevel; wTopLevel.setWindowTitle(QLatin1String(QTest::currentTestFunction())); wTopLevel.resize(m_testWidgetSize); - wTopLevel.show(); + QTestPrivate::androidCompatibleShow(&wTopLevel); QVERIFY(QTest::qWaitForWindowExposed(&wTopLevel)); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT does not support resize", Abort); QCOMPARE (wTopLevel.m_resizeEventCount, 1); // initial resize event before paint for toplevels wTopLevel.hide(); QSize safeSize(640,480); @@ -2413,7 +3499,7 @@ void tst_QWidget::resizeEvent() safeSize.setWidth(639); wTopLevel.resize(safeSize); QCOMPARE (wTopLevel.m_resizeEventCount, 1); - wTopLevel.show(); + QTestPrivate::androidCompatibleShow(&wTopLevel); QVERIFY(QTest::qWaitForWindowExposed(&wTopLevel)); QCOMPARE (wTopLevel.m_resizeEventCount, 2); } @@ -2436,9 +3522,6 @@ void tst_QWidget::showMinimized() plain.showMinimized(); QVERIFY(plain.isMinimized()); QVERIFY(plain.isVisible()); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "Winrt does not support move and resize", Abort); -#endif QVERIFY2(HighDpi::fuzzyCompare(plain.pos(), pos, m_fuzz), qPrintable(HighDpi::msgPointMismatch(plain.pos(), pos))); @@ -2478,8 +3561,8 @@ void tst_QWidget::showMinimizedKeepsFocus() { if (m_platform == QStringLiteral("xcb")) QSKIP("QTBUG-26424"); - if (m_platform == QStringLiteral("wayland")) - QSKIP("Wayland: This fails. Figure out why."); + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("Window activation is not supported."); if (m_platform == QStringLiteral("offscreen")) QSKIP("Platform offscreen does not support showMinimized()"); @@ -2492,7 +3575,7 @@ void tst_QWidget::showMinimizedKeepsFocus() child1.setFocusPolicy(Qt::StrongFocus); child2.setFocusPolicy(Qt::StrongFocus); window.show(); - QApplication::setActiveWindow(&window); + QApplicationPrivate::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); child2.setFocus(); @@ -2516,7 +3599,6 @@ void tst_QWidget::showMinimizedKeepsFocus() QWidget *child = new QWidget(&window); child->setFocusPolicy(Qt::StrongFocus); window.show(); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); child->setFocus(); QTRY_COMPARE(window.focusWidget(), child); @@ -2535,7 +3617,6 @@ void tst_QWidget::showMinimizedKeepsFocus() QWidget *child = new QWidget(&window); child->setFocusPolicy(Qt::StrongFocus); window.show(); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); child->setFocus(); QTRY_COMPARE(window.focusWidget(), child); @@ -2555,7 +3636,6 @@ void tst_QWidget::showMinimizedKeepsFocus() QWidget *child = new QWidget(&window); child->setFocusPolicy(Qt::StrongFocus); window.show(); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); child->setFocus(); QTRY_COMPARE(window.focusWidget(), child); @@ -2576,7 +3656,6 @@ void tst_QWidget::showMinimizedKeepsFocus() QWidget *child = new QWidget(&window); child->setFocusPolicy(Qt::StrongFocus); window.show(); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); child->setFocus(); QTRY_COMPARE(window.focusWidget(), child); @@ -2593,20 +3672,15 @@ void tst_QWidget::showMinimizedKeepsFocus() QTRY_COMPARE(QApplication::focusWidget(), nullptr); window.showNormal(); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS if (!macHasAccessToWindowsServer()) QEXPECT_FAIL("", "When not having WindowServer access, we lose focus.", Continue); -#elif defined(Q_OS_WINRT) - QEXPECT_FAIL("", "Winrt fails here - QTBUG-68297", Continue); #endif QTRY_COMPARE(window.focusWidget(), firstchild); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS if (!macHasAccessToWindowsServer()) QEXPECT_FAIL("", "When not having WindowServer access, we lose focus.", Continue); -#elif defined(Q_OS_WINRT) - QEXPECT_FAIL("", "Winrt fails here - QTBUG-68297", Continue); #endif QTRY_COMPARE(QApplication::focusWidget(), firstchild); } @@ -2635,11 +3709,8 @@ void tst_QWidget::reparent() pal2.setColor(childTLW.backgroundRole(), Qt::yellow); childTLW.setPalette(pal2); - parent.show(); - childTLW.show(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "WinRT does not support more than 1 top level widget", Abort); -#endif + QTestPrivate::androidCompatibleShow(&parent); + QTestPrivate::androidCompatibleShow(&childTLW); QVERIFY(QTest::qWaitForWindowExposed(&parent)); parent.move(parentPosition); @@ -2649,7 +3720,7 @@ void tst_QWidget::reparent() child.setParent(nullptr, child.windowFlags() & ~Qt::WindowType_Mask); child.setGeometry(childPos.x(), childPos.y(), child.width(), child.height()); - child.show(); + QTestPrivate::androidCompatibleShow(&child); #if 0 // QTBUG-26424 if (m_platform == QStringLiteral("xcb")) @@ -2662,10 +3733,33 @@ void tst_QWidget::reparent() QTRY_COMPARE(childTLW.pos(), tlwPos); } +void tst_QWidget::setScreen() +{ + const auto screens = QApplication::screens(); + if (screens.size() < 2) + QSKIP("This test tests nothing on a machine with a single screen."); + + QScreen *screen0 = screens.at(0); + QScreen *screen1 = screens.at(1); + + QWidget window; + window.setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint); + window.setScreen(screen0); + QCOMPARE(window.screen(), screen0); + window.setScreen(screen1); + QCOMPARE(window.screen(), screen1); + + // calling setScreen on a widget that is not a window does nothing + QWidget child(&window); + const QScreen *childScreen = child.screen(); + child.setScreen(childScreen == screen0 ? screen1 : screen0); + QCOMPARE(child.screen(), childScreen); +} + // Qt/Embedded does it differently. void tst_QWidget::icon() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QSKIP("QTBUG-52974"); #endif @@ -2690,8 +3784,8 @@ void tst_QWidget::icon() void tst_QWidget::hideWhenFocusWidgetIsChild() { - if (m_platform == QStringLiteral("wayland")) - QSKIP("Wayland: This fails. Figure out why."); + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("Window activation is not supported."); QScopedPointer<QWidget> testWidget(new QWidget); testWidget->setWindowTitle(__FUNCTION__); @@ -2731,111 +3825,111 @@ void tst_QWidget::hideWhenFocusWidgetIsChild() void tst_QWidget::normalGeometry() { -#ifdef Q_OS_OSX - QSKIP("QTBUG-52974"); -#endif - if (m_platform == QStringLiteral("wayland")) QSKIP("Wayland: This fails. Figure out why."); - else if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT: This fails. Figure out why - QTBUG-68297."); QWidget parent; + QCOMPARE(parent.normalGeometry(), parent.geometry()); parent.setWindowTitle("NormalGeometry parent"); QWidget *child = new QWidget(&parent); QCOMPARE(parent.normalGeometry(), parent.geometry()); QCOMPARE(child->normalGeometry(), QRect()); - const QRect testGeom = QRect(m_availableTopLeft + QPoint(100 ,100), m_testWidgetSize); - parent.setGeometry(testGeom); + parent.setGeometry(QRect(m_availableTopLeft + QPoint(100 ,100), m_testWidgetSize)); parent.showNormal(); QVERIFY(QTest::qWaitForWindowExposed(&parent)); - QApplication::processEvents(); + WindowStateChangeWatcher stateChangeWatcher(&parent); - QRect geom = parent.geometry(); - // ### the window manager places the top-left corner at - // ### 100,100... making geom something like 102,124 (offset by - // ### the frame/frame)... this indicates a rather large different - // ### between how X11 and Windows works - // QCOMPARE(geom, QRect(100, 100, 200, 200)); - QCOMPARE(parent.normalGeometry(), geom); + const QRect normalGeometry = parent.geometry(); + // We can't make any assumptions about the actual geometry compared to the + // requested geometry. In this test, we only care about normalGeometry. + QCOMPARE(parent.normalGeometry(), normalGeometry); parent.setWindowState(parent.windowState() ^ Qt::WindowMaximized); QTRY_VERIFY(parent.windowState() & Qt::WindowMaximized); - QTRY_VERIFY(parent.geometry() != geom); - QTRY_COMPARE(parent.normalGeometry(), geom); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_VERIFY(parent.geometry() != normalGeometry); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); parent.setWindowState(parent.windowState() ^ Qt::WindowMaximized); QTRY_VERIFY(!(parent.windowState() & Qt::WindowMaximized)); - QTRY_COMPARE(parent.geometry(), geom); - QTRY_COMPARE(parent.normalGeometry(), geom); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_COMPARE(parent.geometry(), normalGeometry); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); parent.showMaximized(); - QTRY_VERIFY(parent.windowState() & Qt::WindowMaximized); - QTRY_VERIFY(parent.geometry() != geom); - QCOMPARE(parent.normalGeometry(), geom); + QTRY_VERIFY(parent.windowHandle()->windowState() & Qt::WindowMaximized); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_VERIFY(parent.geometry() != normalGeometry); + QCOMPARE(parent.normalGeometry(), normalGeometry); parent.showNormal(); QTRY_VERIFY(!(parent.windowState() & Qt::WindowMaximized)); - QTRY_COMPARE(parent.geometry(), geom); - QCOMPARE(parent.normalGeometry(), geom); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_COMPARE(parent.geometry(), normalGeometry); + QCOMPARE(parent.normalGeometry(), normalGeometry); + + parent.setWindowState(parent.windowState() ^ Qt::WindowFullScreen); + QTRY_VERIFY(parent.windowState() & Qt::WindowFullScreen); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_VERIFY(parent.geometry() != normalGeometry); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); + + parent.setWindowState(Qt::WindowNoState); + QTRY_VERIFY(!(parent.windowState() & Qt::WindowFullScreen)); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_COMPARE(parent.geometry(), normalGeometry); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); + + parent.showFullScreen(); + QTRY_VERIFY(parent.window()->windowState() & Qt::WindowFullScreen); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_VERIFY(parent.geometry() != normalGeometry); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); + + parent.showNormal(); + QTRY_VERIFY(!(parent.windowHandle()->windowState() & Qt::WindowFullScreen)); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_COMPARE(parent.geometry(), normalGeometry); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); - parent.setWindowState(parent.windowState() ^ Qt::WindowMinimized); - QTest::qWait(10); - parent.setWindowState(parent.windowState() ^ Qt::WindowMaximized); - QTest::qWait(10); if (m_platform == QStringLiteral("xcb")) QSKIP("QTBUG-26424"); - QTRY_VERIFY(parent.windowState() & (Qt::WindowMinimized|Qt::WindowMaximized)); + + parent.setWindowState(parent.windowState() ^ Qt::WindowMaximized); + QTRY_VERIFY(stateChangeWatcher.lastWindowStates & Qt::WindowMaximized); + parent.setWindowState(parent.windowState() ^ Qt::WindowMinimized); + QTRY_VERIFY(stateChangeWatcher.lastWindowStates & Qt::WindowMinimized); + + QTRY_COMPARE(parent.windowState() & (Qt::WindowMinimized|Qt::WindowMaximized), Qt::WindowMinimized|Qt::WindowMaximized); + QTRY_VERIFY(stateChangeWatcher.lastWindowStates & (Qt::WindowMinimized|Qt::WindowMaximized)); // ### when minimized and maximized at the same time, the geometry // ### does *NOT* have to be the normal geometry, it could be the // ### maximized geometry. // QCOMPARE(parent.geometry(), geom); - QTRY_COMPARE(parent.normalGeometry(), geom); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); parent.setWindowState(parent.windowState() ^ Qt::WindowMinimized); - QTest::qWait(10); QTRY_VERIFY(!(parent.windowState() & Qt::WindowMinimized)); QTRY_VERIFY(parent.windowState() & Qt::WindowMaximized); - QTRY_VERIFY(parent.geometry() != geom); - QTRY_COMPARE(parent.normalGeometry(), geom); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_VERIFY(parent.geometry() != normalGeometry); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); parent.setWindowState(parent.windowState() ^ Qt::WindowMaximized); - QTest::qWait(10); QTRY_VERIFY(!(parent.windowState() & Qt::WindowMaximized)); - QTRY_COMPARE(parent.geometry(), geom); - QTRY_COMPARE(parent.normalGeometry(), geom); - - parent.setWindowState(parent.windowState() ^ Qt::WindowFullScreen); - QTest::qWait(10); - QTRY_VERIFY(parent.windowState() & Qt::WindowFullScreen); - QTRY_VERIFY(parent.geometry() != geom); - QTRY_COMPARE(parent.normalGeometry(), geom); - - parent.setWindowState(parent.windowState() ^ Qt::WindowFullScreen); - QTest::qWait(10); - QVERIFY(!(parent.windowState() & Qt::WindowFullScreen)); - QTRY_COMPARE(parent.geometry(), geom); - QTRY_COMPARE(parent.normalGeometry(), geom); - - parent.showFullScreen(); - QTest::qWait(10); - QTRY_VERIFY(parent.windowState() & Qt::WindowFullScreen); - QTRY_VERIFY(parent.geometry() != geom); - QTRY_COMPARE(parent.normalGeometry(), geom); - - parent.showNormal(); - QTest::qWait(10); - QTRY_VERIFY(!(parent.windowState() & Qt::WindowFullScreen)); - QTRY_COMPARE(parent.geometry(), geom); - QTRY_COMPARE(parent.normalGeometry(), geom); + QTRY_COMPARE(stateChangeWatcher.lastWindowStates, parent.windowState()); + QTRY_COMPARE(parent.geometry(), normalGeometry); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); parent.showNormal(); + stateChangeWatcher.lastWindowStates = {}; parent.setWindowState(Qt:: WindowFullScreen | Qt::WindowMaximized); parent.setWindowState(Qt::WindowMinimized | Qt:: WindowFullScreen | Qt::WindowMaximized); parent.setWindowState(Qt:: WindowFullScreen | Qt::WindowMaximized); - QTest::qWait(10); - QTRY_COMPARE(parent.normalGeometry(), geom); + // the actual window will be either fullscreen or maximized + QTRY_VERIFY(stateChangeWatcher.lastWindowStates & (Qt:: WindowFullScreen | Qt::WindowMaximized)); + QTRY_COMPARE(parent.normalGeometry(), normalGeometry); } void tst_QWidget::setGeometry() @@ -2844,31 +3938,59 @@ void tst_QWidget::setGeometry() tlw.setWindowTitle(QLatin1String(QTest::currentTestFunction())); QWidget child(&tlw); - const QPoint topLeft = QGuiApplication::primaryScreen()->availableGeometry().topLeft(); const QSize initialSize = 2 * m_testWidgetSize; - QRect tr(topLeft + QPoint(100,100), initialSize); + QRect tr(m_availableTopLeft + QPoint(100,100), initialSize); QRect cr(50,50,50,50); tlw.setGeometry(tr); child.setGeometry(cr); tlw.showNormal(); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT does not support setGeometry", Abort); QTRY_COMPARE(tlw.geometry().size(), tr.size()); QCOMPARE(child.geometry(), cr); tlw.setParent(nullptr, Qt::Window|Qt::FramelessWindowHint); - tr = QRect(topLeft, initialSize / 2); + tr = QRect(m_availableTopLeft, initialSize / 2); tlw.setGeometry(tr); QCOMPARE(tlw.geometry(), tr); tlw.showNormal(); - QTest::qWait(50); - if (tlw.frameGeometry() != tlw.geometry()) + if (!QTest::qWaitFor([&tlw]{ return tlw.frameGeometry() == tlw.geometry(); })) QSKIP("Your window manager is too broken for this test"); - if (m_platform == QStringLiteral("xcb")) - QSKIP("QTBUG-26424"); + if (m_platform == QStringLiteral("xcb") && tlw.geometry() != tr) + QEXPECT_FAIL("", "QTBUG-26424", Continue); QCOMPARE(tlw.geometry(), tr); } +void tst_QWidget::setGeometryHidden() +{ + if (QGuiApplication::styleHints()->showIsMaximized()) + QSKIP("Platform does not support QWidget::setGeometry() - skipping"); + + QWidget tlw; + tlw.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + QWidget child(&tlw); + + const QRect tr(m_availableTopLeft + QPoint(100, 100), 2 * m_testWidgetSize); + const QRect cr(QPoint(50, 50), m_testWidgetSize); + tlw.setGeometry(tr); + child.setGeometry(cr); + tlw.showNormal(); + + tlw.hide(); + QTRY_VERIFY(tlw.isHidden()); + tlw.setGeometry(cr); + QVERIFY(tlw.testAttribute(Qt::WA_PendingMoveEvent)); + QVERIFY(tlw.testAttribute(Qt::WA_PendingResizeEvent)); + QImage img(tlw.size(), QImage::Format_ARGB32); // just needed to call QWidget::render() + tlw.render(&img); + QVERIFY(!tlw.testAttribute(Qt::WA_PendingMoveEvent)); + QVERIFY(!tlw.testAttribute(Qt::WA_PendingResizeEvent)); + tlw.setGeometry(cr); + QVERIFY(!tlw.testAttribute(Qt::WA_PendingMoveEvent)); + QVERIFY(!tlw.testAttribute(Qt::WA_PendingResizeEvent)); + tlw.resize(cr.size()); + QVERIFY(!tlw.testAttribute(Qt::WA_PendingMoveEvent)); + QVERIFY(!tlw.testAttribute(Qt::WA_PendingResizeEvent)); +} + void tst_QWidget::windowOpacity() { QWidget widget; @@ -2959,7 +4081,7 @@ public: void tst_QWidget::lostUpdatesOnHide() { -#ifndef Q_OS_OSX +#ifndef Q_OS_MACOS UpdateWidget widget; widget.setAttribute(Qt::WA_DontShowOnScreen); widget.setWindowTitle(QLatin1String(QTest::currentTestFunction())); @@ -2975,32 +4097,32 @@ void tst_QWidget::lostUpdatesOnHide() void tst_QWidget::raise() { - QScopedPointer<QWidget> parentPtr(new QWidget); + std::unique_ptr<QWidget> parentPtr(new QWidget); parentPtr->resize(200, 200); parentPtr->setObjectName(QLatin1String("raise")); parentPtr->setWindowTitle(parentPtr->objectName()); QList<UpdateWidget *> allChildren; - UpdateWidget *child1 = new UpdateWidget(parentPtr.data()); + UpdateWidget *child1 = new UpdateWidget(parentPtr.get()); child1->setAutoFillBackground(true); allChildren.append(child1); - UpdateWidget *child2 = new UpdateWidget(parentPtr.data()); + UpdateWidget *child2 = new UpdateWidget(parentPtr.get()); child2->setAutoFillBackground(true); allChildren.append(child2); - UpdateWidget *child3 = new UpdateWidget(parentPtr.data()); + UpdateWidget *child3 = new UpdateWidget(parentPtr.get()); child3->setAutoFillBackground(true); allChildren.append(child3); - UpdateWidget *child4 = new UpdateWidget(parentPtr.data()); + UpdateWidget *child4 = new UpdateWidget(parentPtr.get()); child4->setAutoFillBackground(true); allChildren.append(child4); parentPtr->show(); - QVERIFY(QTest::qWaitForWindowExposed(parentPtr.data())); + QVERIFY(QTest::qWaitForWindowExposed(parentPtr.get())); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS if (child1->internalWinId()) { QSKIP("Cocoa has no Z-Order for views, we hack it, but it results in paint events."); } @@ -3008,9 +4130,9 @@ void tst_QWidget::raise() QObjectList list1{child1, child2, child3, child4}; QCOMPARE(parentPtr->children(), list1); - QCOMPARE(allChildren.count(), list1.count()); + QCOMPARE(allChildren.size(), list1.size()); - for (UpdateWidget *child : qAsConst(allChildren)) { + for (UpdateWidget *child : std::as_const(allChildren)) { int expectedPaintEvents = child == child4 ? 1 : 0; if (expectedPaintEvents == 0) { QCOMPARE(child->numPaintEvents, 0); @@ -3024,9 +4146,10 @@ void tst_QWidget::raise() for (int i = 0; i < 5; ++i) child2->raise(); - QTest::qWait(50); + QVERIFY(QTest::qWaitForWindowExposed(child2)); + QApplication::processEvents(); // process events that could be triggered by raise(); - for (UpdateWidget *child : qAsConst(allChildren)) { + for (UpdateWidget *child : std::as_const(allChildren)) { int expectedPaintEvents = child == child2 ? 1 : 0; int expectedZOrderChangeEvents = child == child2 ? 1 : 0; QTRY_COMPARE(child->numPaintEvents, expectedPaintEvents); @@ -3042,7 +4165,7 @@ void tst_QWidget::raise() // the children underneath doesn't trigger a repaint on the covering widget. QWidget topLevel; topLevel.setWindowTitle(QLatin1String(QTest::currentTestFunction())); - QWidget *parent = parentPtr.take(); + QWidget *parent = parentPtr.release(); parent->setParent(&topLevel); topLevel.show(); @@ -3053,15 +4176,17 @@ void tst_QWidget::raise() onTop->show(); QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); QTRY_VERIFY(onTop->numPaintEvents > 0); + QApplication::processEvents(); // process remaining paint events if there's more than one onTop->reset(); // Reset all the children. - for (UpdateWidget *child : qAsConst(allChildren)) + for (UpdateWidget *child : std::as_const(allChildren)) child->reset(); for (int i = 0; i < 5; ++i) child3->raise(); - QTest::qWait(50); + QVERIFY(QTest::qWaitForWindowExposed(child3)); + QApplication::processEvents(); // process events that could be triggered by raise(); QCOMPARE(onTop->numPaintEvents, 0); QCOMPARE(onTop->numZOrderChangeEvents, 0); @@ -3069,7 +4194,7 @@ void tst_QWidget::raise() QObjectList list3{child1, child4, child2, child3}; QCOMPARE(parent->children(), list3); - for (UpdateWidget *child : qAsConst(allChildren)) { + for (UpdateWidget *child : std::as_const(allChildren)) { int expectedPaintEvents = 0; int expectedZOrderChangeEvents = child == child3 ? 1 : 0; QTRY_COMPARE(child->numPaintEvents, expectedPaintEvents); @@ -3107,9 +4232,9 @@ void tst_QWidget::lower() QObjectList list1{child1, child2, child3, child4}; QCOMPARE(parent->children(), list1); - QCOMPARE(allChildren.count(), list1.count()); + QCOMPARE(allChildren.size(), list1.size()); - for (UpdateWidget *child : qAsConst(allChildren)) { + for (UpdateWidget *child : std::as_const(allChildren)) { int expectedPaintEvents = child == child4 ? 1 : 0; if (expectedPaintEvents == 0) { QCOMPARE(child->numPaintEvents, 0); @@ -3126,7 +4251,7 @@ void tst_QWidget::lower() QTest::qWait(100); - for (UpdateWidget *child : qAsConst(allChildren)) { + for (UpdateWidget *child : std::as_const(allChildren)) { int expectedPaintEvents = child == child3 ? 1 : 0; int expectedZOrderChangeEvents = child == child4 ? 1 : 0; QTRY_COMPARE(child->numZOrderChangeEvents, expectedZOrderChangeEvents); @@ -3141,7 +4266,7 @@ void tst_QWidget::lower() void tst_QWidget::stackUnder() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QSKIP("QTBUG-52974: Cocoa has no Z-Order for views, we hack it, but it results in paint events."); #endif @@ -3172,9 +4297,9 @@ void tst_QWidget::stackUnder() QObjectList list1{child1, child2, child3, child4}; QCOMPARE(parent->children(), list1); - for (UpdateWidget *child : qAsConst(allChildren)) { + for (UpdateWidget *child : std::as_const(allChildren)) { int expectedPaintEvents = child == child4 ? 1 : 0; -#if defined(Q_OS_WIN) || defined(Q_OS_OSX) +#if defined(Q_OS_WIN) || defined(Q_OS_MACOS) if (expectedPaintEvents == 1 && child->numPaintEvents == 2) QEXPECT_FAIL(0, "Mac and Windows issues double repaints for Z-Order change", Continue); #endif @@ -3190,7 +4315,7 @@ void tst_QWidget::stackUnder() QObjectList list2{child1, child4, child2, child3}; QCOMPARE(parent->children(), list2); - for (UpdateWidget *child : qAsConst(allChildren)) { + for (UpdateWidget *child : std::as_const(allChildren)) { int expectedPaintEvents = child == child3 ? 1 : 0; int expectedZOrderChangeEvents = child == child4 ? 1 : 0; QTRY_COMPARE(child->numPaintEvents, expectedPaintEvents); @@ -3205,10 +4330,10 @@ void tst_QWidget::stackUnder() QObjectList list3{child4, child2, child1, child3}; QCOMPARE(parent->children(), list3); - for (UpdateWidget *child : qAsConst(allChildren)) { + for (UpdateWidget *child : std::as_const(allChildren)) { int expectedZOrderChangeEvents = child == child1 ? 1 : 0; if (child == child3) { -#ifndef Q_OS_OSX +#ifndef Q_OS_MACOS QEXPECT_FAIL(0, "See QTBUG-493", Continue); #endif QCOMPARE(child->numPaintEvents, 0); @@ -3269,7 +4394,7 @@ protected: static QPixmap grabFromWidget(QWidget *w, const QRect &rect) { QPixmap pixmap = w->grab(rect); - const qreal devicePixelRatio = pixmap.devicePixelRatioF(); + const qreal devicePixelRatio = pixmap.devicePixelRatio(); if (!qFuzzyCompare(devicePixelRatio, qreal(1))) { pixmap = pixmap.scaled((QSizeF(pixmap.size()) / devicePixelRatio).toSize(), Qt::KeepAspectRatio, Qt::SmoothTransformation); @@ -3308,7 +4433,7 @@ void tst_QWidget::testContentsPropagation() void tst_QWidget::saveRestoreGeometry() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QSKIP("QTBUG-52974"); #endif @@ -3327,8 +4452,13 @@ void tst_QWidget::saveRestoreGeometry() QVERIFY(QTest::qWaitForWindowExposed(&widget)); QApplication::processEvents(); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT does not support move/resize", Abort); + + /* --------------------------------------------------------------------- + * This test function is likely to flake when debugged with Qt Creator. + * (29px offset making the following QTRY_VERIFY2 fail) + * --------------------------------------------------------------------- + */ + QTRY_VERIFY2(HighDpi::fuzzyCompare(widget.pos(), position, m_fuzz), qPrintable(HighDpi::msgPointMismatch(widget.pos(), position))); QCOMPARE(widget.size(), size); @@ -3484,10 +4614,9 @@ void tst_QWidget::restoreVersion1Geometry() const Qt::WindowStates WindowStateMask = Qt::WindowFullScreen | Qt::WindowMaximized | Qt::WindowMinimized; QFile f(fileName); - QVERIFY(f.exists()); - f.open(QIODevice::ReadOnly); + QVERIFY(f.open(QIODevice::ReadOnly)); const QByteArray savedGeometry = f.readAll(); - QCOMPARE(savedGeometry.count(), 46); + QCOMPARE(savedGeometry.size(), 46); f.close(); QWidget widget; @@ -3506,9 +4635,6 @@ void tst_QWidget::restoreVersion1Geometry() QVERIFY(QTest::qWaitForWindowExposed(&widget)); QTest::qWait(100); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT does not support restoreGeometry", Abort); - if (expectedWindowState == Qt::WindowNoState) { QTRY_COMPARE(widget.size(), expectedSize); QCOMPARE(widget.geometry(), expectedNormalGeometry); @@ -3544,9 +4670,71 @@ void tst_QWidget::restoreVersion1Geometry() #endif } +void tst_QWidget::restoreGeometryAfterScreenChange_data() +{ + QTest::addColumn<ScreenPosition>("screenPosition"); + QTest::addColumn<int>("deltaWidth"); + QTest::addColumn<int>("deltaHeight"); + QTest::addColumn<int>("frameMargin"); + QTest::addColumn<bool>("outside"); + + QTest::newRow("offAboveLarge") << ScreenPosition::OffAbove << 200 << 250 << 20 << true; + QTest::newRow("fitting") << ScreenPosition::Contained << 80 << 80 << 20 << false; + QTest::newRow("offRightWide") << ScreenPosition::OffRight << 150 << 80 << 20 << false; + QTest::newRow("offLeftFitting") << ScreenPosition::OffLeft << 70 << 70 << 20 << true; + QTest::newRow("offBelowHigh") << ScreenPosition::OffBelow << 80 << 200 << 20 << false; +} + +void tst_QWidget::restoreGeometryAfterScreenChange() +{ + const QList<QScreen *> &screens = QApplication::screens(); + QVERIFY2(!screens.isEmpty(), "No screens found."); + const QRect screenGeometry = screens.at(0)->geometry(); + + QFETCH(ScreenPosition, screenPosition); + QFETCH(int, deltaWidth); + QFETCH(int, deltaHeight); + QFETCH(int, frameMargin); + QFETCH(bool, outside); + + QRect restoredGeometry = screenGeometry; + restoredGeometry.setHeight(screenGeometry.height() * deltaHeight / 100); + restoredGeometry.setWidth(screenGeometry.width() * deltaWidth / 100); + const float moveMargin = outside ? 1.2 : 0.75; + + switch (screenPosition) { + case ScreenPosition::OffLeft: + restoredGeometry.setLeft(restoredGeometry.width() * (-moveMargin)); + break; + case ScreenPosition::OffAbove: + restoredGeometry.setTop(restoredGeometry.height() * (-moveMargin)); + break; + case ScreenPosition::OffRight: + restoredGeometry.setRight(restoredGeometry.width() * moveMargin); + break; + case ScreenPosition::OffBelow: + restoredGeometry.setBottom(restoredGeometry.height() * moveMargin); + break; + case ScreenPosition::Contained: + break; + } + + // If restored geometry fits into screen and has not been moved, + // it is changed only by frame margin plus one pixel at each edge + const QRect originalGeometry = restoredGeometry.adjusted(1, frameMargin + 1, 1, frameMargin + 1); + + QWidgetPrivate::checkRestoredGeometry(screenGeometry, &restoredGeometry, frameMargin); + + if (deltaHeight < 100 && deltaWidth < 100 && screenPosition == ScreenPosition::Contained) + QCOMPARE(originalGeometry, restoredGeometry); + + // new geometry has to fit on the screen + QVERIFY(screenGeometry.contains(restoredGeometry)); +} + void tst_QWidget::widgetAt() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QSKIP("QTBUG-52974"); #endif @@ -3554,8 +4742,6 @@ void tst_QWidget::widgetAt() QSKIP("Wayland: This fails. Figure out why."); if (m_platform == QStringLiteral("offscreen")) QSKIP("Platform offscreen does not support lower()/raise() or WindowMasks"); - if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT does not support more than 1 top level widget"); Q_CHECK_PAINTEVENTS @@ -3704,7 +4890,8 @@ void tst_QWidget::testDeletionInEventHandlers() w = new Widget; w->show(); w->deleteThis = true; - QMouseEvent me(QEvent::MouseButtonRelease, QPoint(1, 1), Qt::LeftButton, Qt::LeftButton, Qt::KeyboardModifiers()); + QMouseEvent me(QEvent::MouseButtonRelease, QPoint(1, 1), w->mapToGlobal(QPoint(1, 1)), + Qt::LeftButton, Qt::LeftButton, Qt::KeyboardModifiers()); qApp->notify(w, &me); QVERIFY(w.isNull()); delete w; @@ -3743,13 +4930,14 @@ void tst_QWidget::testDeletionInEventHandlers() w->setMouseTracking(true); w->show(); w->deleteThis = true; - me = QMouseEvent(QEvent::MouseMove, QPoint(0, 0), Qt::NoButton, Qt::NoButton, Qt::NoModifier); - QApplication::sendEvent(w, &me); + QMouseEvent me2 = QMouseEvent(QEvent::MouseMove, QPoint(0, 0), w->mapToGlobal(QPoint(0, 0)), + Qt::NoButton, Qt::NoButton, Qt::NoModifier); + QApplication::sendEvent(w, &me2); QVERIFY(w.isNull()); delete w; } -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS class MaskedPainter : public QWidget { public: @@ -3809,22 +4997,20 @@ class StaticWidget : public QWidget Q_OBJECT public: bool partial = false; - bool gotPaintEvent = false; QRegion paintedRegion; - explicit StaticWidget(QWidget *parent = nullptr) : QWidget(parent) + explicit StaticWidget(const QPalette &palette, QWidget *parent = nullptr) : QWidget(parent) { setAttribute(Qt::WA_StaticContents); setAttribute(Qt::WA_OpaquePaintEvent); - setPalette(Qt::red); // Make sure we have an opaque palette. + setPalette(palette); setAutoFillBackground(true); } void paintEvent(QPaintEvent *e) override { paintedRegion += e->region(); - gotPaintEvent = true; -// qDebug() << "paint" << e->region(); + ++paintEvents; // Look for a full update, set partial to false if found. for (QRect r : e->region()) { partial = (r != rect()); @@ -3832,106 +5018,114 @@ public: break; } } + + // Wait timeout ms until at least one paint event has been consumed + // and the counter is no longer increasing. + // => making sure to consume multiple paint events relating to one operation + // before returning true. + bool waitForPaintEvent(int timeout = 100) + { + QDeadlineTimer deadline(timeout); + int count = -1; + while (!deadline.hasExpired() && count != paintEvents) { + count = paintEvents; + QCoreApplication::processEvents(); + if (count == paintEvents && count > 0) { + paintEvents = 0; + return true; + } + } + paintEvents = 0; + return false; + } +private: + int paintEvents = 0; }; /* Test that widget resizes and moves can be done with minimal repaints when WA_StaticContents - and WA_OpaquePaintEvent is set. Test is mac-only for now. + and WA_OpaquePaintEvent is set. */ void tst_QWidget::optimizedResizeMove() { - if (m_platform == QStringLiteral("wayland")) - QSKIP("Wayland: This fails. Figure out why."); + const bool wayland = QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive); + QWidget parent; - parent.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + parent.setPalette(simplePalette()); + parent.setWindowTitle(QTest::currentTestFunction()); parent.resize(400, 400); - StaticWidget staticWidget(&parent); - staticWidget.gotPaintEvent = false; + StaticWidget staticWidget(simplePalette(), &parent); staticWidget.move(150, 150); staticWidget.resize(150, 150); parent.show(); QVERIFY(QTest::qWaitForWindowExposed(&parent)); - QTRY_VERIFY(staticWidget.gotPaintEvent); + QVERIFY(staticWidget.waitForPaintEvent()); - staticWidget.gotPaintEvent = false; staticWidget.move(staticWidget.pos() + QPoint(10, 10)); - QTest::qWait(20); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT does not support move/resize", Abort); - QCOMPARE(staticWidget.gotPaintEvent, false); + if (!wayland) { + QVERIFY(!staticWidget.waitForPaintEvent()); + } else { + if (staticWidget.waitForPaintEvent()) + QSKIP("Wayland is not optimising paint events. Skipping test."); + } - staticWidget.gotPaintEvent = false; staticWidget.move(staticWidget.pos() + QPoint(-10, -10)); - QTest::qWait(20); - QCOMPARE(staticWidget.gotPaintEvent, false); + QVERIFY(!staticWidget.waitForPaintEvent()); - staticWidget.gotPaintEvent = false; staticWidget.move(staticWidget.pos() + QPoint(-10, 10)); - QTest::qWait(20); - QCOMPARE(staticWidget.gotPaintEvent, false); + QVERIFY(!staticWidget.waitForPaintEvent()); - staticWidget.gotPaintEvent = false; staticWidget.resize(staticWidget.size() + QSize(10, 10)); - QTRY_VERIFY(staticWidget.gotPaintEvent); + QVERIFY(staticWidget.waitForPaintEvent()); QCOMPARE(staticWidget.partial, true); - staticWidget.gotPaintEvent = false; staticWidget.resize(staticWidget.size() + QSize(-10, -10)); - QTest::qWait(20); - QCOMPARE(staticWidget.gotPaintEvent, false); + QVERIFY(!staticWidget.waitForPaintEvent()); - staticWidget.gotPaintEvent = false; staticWidget.resize(staticWidget.size() + QSize(10, -10)); - QTRY_VERIFY(staticWidget.gotPaintEvent); + QVERIFY(staticWidget.waitForPaintEvent()); QCOMPARE(staticWidget.partial, true); - staticWidget.gotPaintEvent = false; staticWidget.move(staticWidget.pos() + QPoint(10, 10)); staticWidget.resize(staticWidget.size() + QSize(-10, -10)); - QTest::qWait(20); - QCOMPARE(staticWidget.gotPaintEvent, false); + QVERIFY(!staticWidget.waitForPaintEvent()); - staticWidget.gotPaintEvent = false; staticWidget.move(staticWidget.pos() + QPoint(10, 10)); staticWidget.resize(staticWidget.size() + QSize(10, 10)); - QTRY_VERIFY(staticWidget.gotPaintEvent); + QVERIFY(staticWidget.waitForPaintEvent()); QCOMPARE(staticWidget.partial, true); - staticWidget.gotPaintEvent = false; staticWidget.move(staticWidget.pos() + QPoint(-10, -10)); staticWidget.resize(staticWidget.size() + QSize(-10, -10)); - QTest::qWait(20); - QCOMPARE(staticWidget.gotPaintEvent, false); + QVERIFY(!staticWidget.waitForPaintEvent()); staticWidget.setAttribute(Qt::WA_StaticContents, false); - staticWidget.gotPaintEvent = false; staticWidget.move(staticWidget.pos() + QPoint(-10, -10)); staticWidget.resize(staticWidget.size() + QSize(-10, -10)); - QTRY_VERIFY(staticWidget.gotPaintEvent); + QVERIFY(staticWidget.waitForPaintEvent()); QCOMPARE(staticWidget.partial, false); staticWidget.setAttribute(Qt::WA_StaticContents, true); staticWidget.setAttribute(Qt::WA_StaticContents, false); - staticWidget.gotPaintEvent = false; staticWidget.move(staticWidget.pos() + QPoint(10, 10)); - QTest::qWait(20); - QCOMPARE(staticWidget.gotPaintEvent, false); + QVERIFY(!staticWidget.waitForPaintEvent()); staticWidget.setAttribute(Qt::WA_StaticContents, true); } void tst_QWidget::optimizedResize_topLevel() { + const bool wayland = QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive); + if (QHighDpiScaling::isActive()) QSKIP("Skip due to rounding errors in the regions."); - StaticWidget topLevel; + StaticWidget topLevel(simplePalette()); + topLevel.setPalette(simplePalette()); topLevel.setWindowTitle(QLatin1String(QTest::currentTestFunction())); - topLevel.gotPaintEvent = false; topLevel.show(); QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); - QTRY_VERIFY(topLevel.gotPaintEvent); + QVERIFY(topLevel.waitForPaintEvent()); - topLevel.gotPaintEvent = false; topLevel.partial = false; topLevel.paintedRegion = QRegion(); @@ -3956,12 +5150,15 @@ void tst_QWidget::optimizedResize_topLevel() QRegion expectedUpdateRegion(topLevel.rect()); expectedUpdateRegion -= QRect(QPoint(), topLevel.size() - QSize(10, 10)); - QTRY_VERIFY(topLevel.gotPaintEvent); + QVERIFY(topLevel.waitForPaintEvent()); if (m_platform == QStringLiteral("xcb") || m_platform == QStringLiteral("offscreen")) QSKIP("QTBUG-26424"); - else if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT does not support move/resize", Abort); - QCOMPARE(topLevel.partial, true); + if (!wayland) { + QCOMPARE(topLevel.partial, true); + } else { + if (!topLevel.partial) + QSKIP("Wayland does repaint partially. Skipping test."); + } QCOMPARE(topLevel.paintedRegion, expectedUpdateRegion); } @@ -4037,6 +5234,9 @@ void tst_QWidget::setMaximumSize() void tst_QWidget::setFixedSize() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QWidget w; QSize defaultSize = w.size(); @@ -4064,8 +5264,6 @@ void tst_QWidget::setFixedSize() QVERIFY(QTest::qWaitForWindowActive(&w)); if (m_platform == QStringLiteral("xcb")) QSKIP("QTBUG-26424"); - else if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT does not support move/resize", Abort); QCOMPARE(w.size(), defaultSize + QSize(150,150)); } @@ -4130,9 +5328,139 @@ protected: } public: QList<WId> m_winIdList; - int winIdChangeEventCount() const { return m_winIdList.count(); } + int winIdChangeEventCount() const { return m_winIdList.size(); } }; +class CreateDestroyWidget : public WinIdChangeWidget +{ +public: + void create() { QWidget::create(); } + void destroy() { QWidget::destroy(); } +}; + +void tst_QWidget::createAndDestroy() +{ + CreateDestroyWidget widget; + + // Create and destroy via QWidget + widget.create(); + QVERIFY(widget.testAttribute(Qt::WA_WState_Created)); + QCOMPARE(widget.winIdChangeEventCount(), 1); + QVERIFY(widget.internalWinId()); + + widget.destroy(); + QVERIFY(!widget.testAttribute(Qt::WA_WState_Created)); + QCOMPARE(widget.winIdChangeEventCount(), 2); + QVERIFY(!widget.internalWinId()); + + // Create via QWidget, destroy via QWindow + widget.create(); + QVERIFY(widget.testAttribute(Qt::WA_WState_Created)); + QCOMPARE(widget.winIdChangeEventCount(), 3); + QVERIFY(widget.internalWinId()); + + widget.windowHandle()->destroy(); + QVERIFY(!widget.testAttribute(Qt::WA_WState_Created)); + QCOMPARE(widget.winIdChangeEventCount(), 4); + QVERIFY(!widget.internalWinId()); + + // Create via QWidget again + widget.create(); + QVERIFY(widget.testAttribute(Qt::WA_WState_Created)); + QCOMPARE(widget.winIdChangeEventCount(), 5); + QVERIFY(widget.internalWinId()); + + // Destroy via QWindow, create via QWindow + widget.windowHandle()->destroy(); + QVERIFY(widget.windowHandle()); + QVERIFY(!widget.testAttribute(Qt::WA_WState_Created)); + QCOMPARE(widget.winIdChangeEventCount(), 6); + QVERIFY(!widget.internalWinId()); + + widget.windowHandle()->create(); + QVERIFY(widget.testAttribute(Qt::WA_WState_Created)); + QCOMPARE(widget.winIdChangeEventCount(), 7); + QVERIFY(widget.internalWinId()); +} + +void tst_QWidget::eventsAndAttributesOnDestroy() +{ + // The events and attributes when destroying a widget should + // include those of hiding the widget. + + CreateDestroyWidget widget; + EventSpy<QWidget> showEventSpy(&widget, QEvent::Show); + EventSpy<QWidget> hideEventSpy(&widget, QEvent::Hide); + + QCOMPARE(widget.testAttribute(Qt::WA_WState_Created), false); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(widget.testAttribute(Qt::WA_Mapped), false); + + widget.show(); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Created), true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Visible), true); + QTRY_COMPARE(widget.testAttribute(Qt::WA_Mapped), true); + QCOMPARE(showEventSpy.count(), 1); + QCOMPARE(hideEventSpy.count(), 0); + + widget.hide(); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Created), true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(widget.testAttribute(Qt::WA_Mapped), false); + QCOMPARE(showEventSpy.count(), 1); + QCOMPARE(hideEventSpy.count(), 1); + + widget.show(); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Created), true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Visible), true); + QTRY_COMPARE(widget.testAttribute(Qt::WA_Mapped), true); + QCOMPARE(showEventSpy.count(), 2); + QCOMPARE(hideEventSpy.count(), 1); + + widget.destroy(); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Created), false); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(widget.testAttribute(Qt::WA_Mapped), false); + QCOMPARE(showEventSpy.count(), 2); + QCOMPARE(hideEventSpy.count(), 2); + + const int hideEventsAfterDestroy = hideEventSpy.count(); + + widget.create(); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Created), true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(widget.testAttribute(Qt::WA_Mapped), false); + QCOMPARE(showEventSpy.count(), 2); + QCOMPARE(hideEventSpy.count(), hideEventsAfterDestroy); + + QWidgetPrivate::get(&widget)->setVisible(true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Created), true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Visible), true); + QTRY_COMPARE(widget.testAttribute(Qt::WA_Mapped), true); + QCOMPARE(showEventSpy.count(), 3); + QCOMPARE(hideEventSpy.count(), hideEventsAfterDestroy); + + // Make sure the destroy that happens when a top level + // is moved to being a child does not prevent the child + // being shown again. + + QWidget parent; + QWidget child; + parent.show(); + QVERIFY(QTest::qWaitForWindowExposed(&parent)); + child.show(); + QVERIFY(QTest::qWaitForWindowExposed(&child)); + + child.setParent(&parent); + QCOMPARE(child.testAttribute(Qt::WA_WState_Created), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Visible), false); + + child.show(); + QCOMPARE(child.testAttribute(Qt::WA_WState_Created), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_Visible), true); + QVERIFY(QTest::qWaitForWindowExposed(&child)); +} + void tst_QWidget::winIdChangeEvent() { { @@ -4263,8 +5591,6 @@ void tst_QWidget::transientParent() void tst_QWidget::showNativeChild() { - if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT does not support setGeometry"); QWidget topLevel; topLevel.setGeometry(QRect(m_availableTopLeft + QPoint(100, 100), m_testWidgetSize)); topLevel.setWindowTitle(__FUNCTION__); @@ -4274,6 +5600,75 @@ void tst_QWidget::showNativeChild() QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); } +void tst_QWidget::closeAndShowNativeChild() +{ + QWidget topLevel; + QWidget *nativeChild = new QWidget; + nativeChild->winId(); + nativeChild->setFixedSize(200, 200); + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(nativeChild); + topLevel.setLayout(layout); + + topLevel.show(); + QVERIFY(!nativeChild->isHidden()); + nativeChild->close(); + QVERIFY(nativeChild->isHidden()); + nativeChild->show(); + QVERIFY(!nativeChild->isHidden()); +} + +void tst_QWidget::closeAndShowWithNativeChild() +{ + bool dontCreateNativeWidgetSiblings = QApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings); + auto resetAttribute = qScopeGuard([&]{ + QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, dontCreateNativeWidgetSiblings); + }); + QApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings); + + QWidget topLevel; + topLevel.setObjectName("TopLevel"); + QWidget *nativeChild = new QWidget; + nativeChild->setObjectName("NativeChild"); + nativeChild->setFixedSize(200, 200); + QWidget *normalChild = new QWidget; + normalChild->setObjectName("NormalChild"); + normalChild->setFixedSize(200, 200); + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(nativeChild); + layout->addWidget(normalChild); + topLevel.setLayout(layout); + + nativeChild->setAttribute(Qt::WA_NativeWindow); + + QCOMPARE(normalChild->testAttribute(Qt::WA_WState_Hidden), false); + QCOMPARE(normalChild->testAttribute(Qt::WA_WState_ExplicitShowHide), false); + + QCOMPARE(nativeChild->testAttribute(Qt::WA_WState_Hidden), false); + QCOMPARE(nativeChild->testAttribute(Qt::WA_WState_ExplicitShowHide), false); + + topLevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + const QSize originalSize = topLevel.size(); + topLevel.close(); + + // all children must have the same state + QCOMPARE(nativeChild->isHidden(), normalChild->isHidden()); + QCOMPARE(nativeChild->isVisible(), normalChild->isVisible()); + QCOMPARE(nativeChild->testAttribute(Qt::WA_WState_Visible), + normalChild->testAttribute(Qt::WA_WState_Visible)); + QCOMPARE(nativeChild->testAttribute(Qt::WA_WState_Hidden), + normalChild->testAttribute(Qt::WA_WState_Hidden)); + QCOMPARE(nativeChild->testAttribute(Qt::WA_WState_ExplicitShowHide), + normalChild->testAttribute(Qt::WA_WState_ExplicitShowHide)); + + topLevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + QCOMPARE(topLevel.size(), originalSize); +} + class ShowHideEventWidget : public QWidget { public: @@ -4423,6 +5818,7 @@ void tst_QWidget::update() Q_CHECK_PAINTEVENTS UpdateWidget w; + w.setPalette(simplePalette()); w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); w.resize(100, 100); centerOnScreen(&w); @@ -4436,6 +5832,7 @@ void tst_QWidget::update() w.reset(); UpdateWidget child(&w); + child.setPalette(simplePalette()); child.setGeometry(10, 10, 80, 80); child.show(); @@ -4443,8 +5840,6 @@ void tst_QWidget::update() // widgets are transparent by default, so both should get repaints { - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT does not support setGeometry", Abort); QApplication::processEvents(); QApplication::processEvents(); QCOMPARE(child.numPaintEvents, 1); @@ -4509,6 +5904,7 @@ void tst_QWidget::update() // overlapping sibling UpdateWidget sibling(&w); + sibling.setPalette(simplePalette()); child.setGeometry(10, 10, 20, 20); sibling.setGeometry(15, 15, 20, 20); sibling.show(); @@ -4558,7 +5954,7 @@ void tst_QWidget::update() QCOMPARE(sibling.numPaintEvents, 1); QCOMPARE(sibling.paintedRegion, sibling.visibleRegion()); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS if (child.internalWinId()) // child is native QEXPECT_FAIL(0, "Cocoa compositor paints child and sibling", Continue); #endif @@ -4575,7 +5971,7 @@ void tst_QWidget::update() } } -#ifndef Q_OS_OSX +#ifndef Q_OS_MACOS static inline bool isOpaque(QWidget *widget) { if (!widget) @@ -4586,11 +5982,13 @@ static inline bool isOpaque(QWidget *widget) void tst_QWidget::isOpaque() { -#ifndef Q_OS_OSX +#ifndef Q_OS_MACOS QWidget w; + w.setPalette(simplePalette()); QVERIFY(::isOpaque(&w)); QWidget child(&w); + child.setPalette(simplePalette()); QVERIFY(!::isOpaque(&child)); child.setAutoFillBackground(true); @@ -4653,12 +6051,13 @@ void tst_QWidget::isOpaque() QVERIFY(!::isOpaque(&widget)); QApplication::setPalette(old); + QApplication::sendPostedEvents(&widget, QEvent::ApplicationPaletteChange); QCOMPARE(::isOpaque(&widget), old.color(QPalette::Window).alpha() == 255); } #endif } -#ifndef Q_OS_OSX +#ifndef Q_OS_MACOS /* Test that scrolling of a widget invalidates the correct regions */ @@ -4671,11 +6070,11 @@ void tst_QWidget::scroll() const int h = qMin(500, screen->availableGeometry().height() / 2); UpdateWidget updateWidget; + updateWidget.setPalette(simplePalette()); updateWidget.resize(w, h); updateWidget.reset(); - updateWidget.move(QGuiApplication::primaryScreen()->geometry().center() - QPoint(250, 250)); + updateWidget.move(m_availableTopLeft); updateWidget.showNormal(); - QApplication::setActiveWindow(&updateWidget); QVERIFY(QTest::qWaitForWindowActive(&updateWidget)); QVERIFY(updateWidget.numPaintEvents > 0); @@ -4685,8 +6084,6 @@ void tst_QWidget::scroll() QCoreApplication::processEvents(); QRegion dirty(QRect(0, 0, w, 10)); dirty += QRegion(QRect(0, 10, 10, h - 10)); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT does not support move/resize", Abort); QTRY_COMPARE(updateWidget.paintedRegion, dirty); } @@ -4752,47 +6149,87 @@ void tst_QWidget::scrollNativeChildren() #endif // Mac OS -class DestroyedSlotChecker : public QObject +/* + This class is used as a slot object to test two different steps of + QWidget destruction. + + The first step is connecting the destroyed() signal to an object of + this class (through its operator()). In widgets, destroyed() is + emitted by ~QWidget, and not by ~QObject. This means that in our + operator() we expect the sender of the signal to still be a + QWidget. + + The connection realized at the first step means that now there's + an instance of this class owned by the sender object. That instance + is destroyed when the signal/slot connections are destroyed. + That happens in ~QObject, not in ~QWidget. Therefore, in the + destructor of this class, check that indeed the target is no longer + a QWidget but just a QObject. +*/ +class QObjectCastChecker { - Q_OBJECT - public: - bool wasQWidget = false; + explicit QObjectCastChecker(QWidget *target) + : m_target(target) + { + } -public slots: - void destroyedSlot(QObject *object) + ~QObjectCastChecker() { - wasQWidget = (qobject_cast<QWidget *>(object) != nullptr || object->isWidgetType()); + if (!m_target) + return; + + // When ~QObject is reached, check that indeed the object is no + // longer a QWidget. This relies on slots being disconnected in + // ~QObject (and this "slot object" being destroyed there). + QVERIFY(!qobject_cast<QWidget *>(m_target)); + QVERIFY(!dynamic_cast<QWidget *>(m_target)); + QVERIFY(!m_target->isWidgetType()); } -}; -/* - Test that qobject_cast<QWidget*> returns 0 in a slot - connected to QObject::destroyed. -*/ -void tst_QWidget::qobject_castInDestroyedSlot() -{ - DestroyedSlotChecker checker; + QObjectCastChecker(QObjectCastChecker &&other) noexcept + : m_target(std::exchange(other.m_target, nullptr)) + {} - QWidget *widget = new QWidget(); + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QObjectCastChecker) - QObject::connect(widget, &QObject::destroyed, &checker, &DestroyedSlotChecker::destroyedSlot); - delete widget; + void swap(QObjectCastChecker &other) noexcept + { + qSwap(m_target, other.m_target); + } + + void operator()(QObject *object) const + { + // Test that in a slot connected to destroyed() the emitter is + // still a QWidget. This is because ~QWidget() itself emits the + // signal. + QVERIFY(qobject_cast<QWidget *>(object)); + QVERIFY(dynamic_cast<QWidget *>(object)); + QVERIFY(object->isWidgetType()); + } + +private: + Q_DISABLE_COPY(QObjectCastChecker) + QObject *m_target; +}; - QVERIFY(checker.wasQWidget); +void tst_QWidget::qobject_castOnDestruction() +{ + QWidget widget; + QObject::connect(&widget, &QObject::destroyed, QObjectCastChecker(&widget)); } // Since X11 WindowManager operations are all async, and we have no way to know if the window // manager has finished playing with the window geometry, this test can't be reliable on X11. -using Rects = QVector<QRect>; +using Rects = QList<QRect>; void tst_QWidget::setWindowGeometry_data() { QTest::addColumn<Rects>("rects"); QTest::addColumn<int>("windowFlags"); - QVector<Rects> rects; + QList<Rects> rects; const int width = m_testWidgetSize.width(); const int height = m_testWidgetSize.height(); const QRect availableAdjusted = QGuiApplication::primaryScreen()->availableGeometry().adjusted(100, 100, -100, -100); @@ -4825,11 +6262,9 @@ void tst_QWidget::setWindowGeometry_data() const Qt::WindowFlags windowFlags[] = {Qt::WindowFlags(), Qt::FramelessWindowHint}; const bool skipEmptyRects = (m_platform == QStringLiteral("windows")); - for (Rects l : qAsConst(rects)) { - if (skipEmptyRects) { - l.erase(std::remove_if(l.begin(), l.end(), [] (const QRect &r) { return r.isEmpty(); }), - l.end()); - } + for (Rects l : std::as_const(rects)) { + if (skipEmptyRects) + l.removeIf([] (const QRect &r) { return r.isEmpty(); }); const QRect &rect = l.constFirst(); for (int windowFlag : windowFlags) { QTest::newRow(QString("%1,%2 %3x%4, flags %5") @@ -4846,10 +6281,8 @@ void tst_QWidget::setWindowGeometry_data() void tst_QWidget::setWindowGeometry() { - if (m_platform == QStringLiteral("xcb")) - QSKIP("X11: Skip this test due to Window manager positioning issues."); - else if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT does not support setWindowGeometry"); + if (m_platform == QStringLiteral("xcb") || m_platform.startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("X11/Wayland: Skip this test due to Window manager positioning issues."); QFETCH(Rects, rects); QFETCH(int, windowFlags); @@ -4866,7 +6299,7 @@ void tst_QWidget::setWindowGeometry() QCOMPARE(widget.geometry(), rect); // setGeometry() without showing - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { widget.setGeometry(r); QTest::qWait(100); QCOMPARE(widget.geometry(), r); @@ -4892,7 +6325,7 @@ void tst_QWidget::setWindowGeometry() QTRY_COMPARE(widget.geometry(), rect); // setGeometry() while shown - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { widget.setGeometry(r); QTest::qWait(10); QTRY_COMPARE(widget.geometry(), r); @@ -4907,7 +6340,7 @@ void tst_QWidget::setWindowGeometry() QTRY_COMPARE(widget.geometry(), rect); // setGeometry() after hide() - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { widget.setGeometry(r); QTest::qWait(10); QTRY_COMPARE(widget.geometry(), r); @@ -4917,7 +6350,7 @@ void tst_QWidget::setWindowGeometry() QTRY_COMPARE(widget.geometry(), rect); // show() again, geometry() should still be the same - widget.show(); + QTestPrivate::androidCompatibleShow(&widget); if (rect.isValid()) QVERIFY(QTest::qWaitForWindowExposed(&widget)); QTRY_COMPARE(widget.geometry(), rect); @@ -4943,7 +6376,7 @@ void tst_QWidget::setWindowGeometry() QTRY_COMPARE(widget.geometry(), rect); // setGeometry() while shown - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { widget.setGeometry(r); QTest::qWait(10); QTRY_COMPARE(widget.geometry(), r); @@ -4958,7 +6391,7 @@ void tst_QWidget::setWindowGeometry() QTRY_COMPARE(widget.geometry(), rect); // setGeometry() after hide() - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { widget.setGeometry(r); QTest::qWait(10); QTRY_COMPARE(widget.geometry(), r); @@ -4968,7 +6401,7 @@ void tst_QWidget::setWindowGeometry() QTRY_COMPARE(widget.geometry(), rect); // show() again, geometry() should still be the same - widget.show(); + QTestPrivate::androidCompatibleShow(&widget); if (rect.isValid()) QVERIFY(QTest::qWaitForWindowExposed(&widget)); QTest::qWait(10); @@ -4981,7 +6414,7 @@ void tst_QWidget::setWindowGeometry() } } -#if defined (Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined (Q_OS_WIN) void tst_QWidget::setGeometry_win() { QWidget widget; @@ -5002,7 +6435,7 @@ void tst_QWidget::setGeometry_win() QVERIFY2(rt.top <= m_availableTopLeft.y(), msgComparisonFailed(int(rt.top), "<=", m_availableTopLeft.y())); } -#endif // defined (Q_OS_WIN) && !defined(Q_OS_WINRT) +#endif // defined (Q_OS_WIN) // Since X11 WindowManager operation are all async, and we have no way to know if the window // manager has finished playing with the window geometry, this test can't be reliable on X11. @@ -5018,8 +6451,6 @@ void tst_QWidget::windowMoveResize() QSKIP("X11: Skip this test due to Window manager positioning issues."); if (m_platform == QStringLiteral("wayland")) QSKIP("Wayland: This fails. Figure out why."); - if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT does not support move/resize"); QFETCH(Rects, rects); QFETCH(int, windowFlags); @@ -5039,7 +6470,7 @@ void tst_QWidget::windowMoveResize() QTRY_COMPARE(widget.size(), rect.size()); // move() without showing - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { widget.move(r.topLeft()); widget.resize(r.size()); QApplication::processEvents(); @@ -5069,7 +6500,7 @@ void tst_QWidget::windowMoveResize() QTRY_COMPARE(widget.size(), rect.size()); // move() while shown - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { // XCB: First resize after show of zero-sized gets wrong win_gravity. const bool expectMoveFail = !windowFlags && ((widget.width() == 0 || widget.height() == 0) && r.width() != 0 && r.height() != 0) @@ -5098,11 +6529,11 @@ void tst_QWidget::windowMoveResize() QTRY_COMPARE(widget.size(), rect.size()); // move() after hide() - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { widget.move(r.topLeft()); widget.resize(r.size()); QApplication::processEvents(); -#if defined(Q_OS_OSX) +#if defined(Q_OS_MACOS) if (r.width() == 0 && r.height() > 0) { widget.move(r.topLeft()); widget.resize(r.size()); @@ -5118,7 +6549,7 @@ void tst_QWidget::windowMoveResize() QTRY_COMPARE(widget.size(), rect.size()); // show() again, pos() should be the same - widget.show(); + QTestPrivate::androidCompatibleShow(&widget); if (rect.isValid()) QVERIFY(QTest::qWaitForWindowExposed(&widget)); QApplication::processEvents(); @@ -5149,7 +6580,7 @@ void tst_QWidget::windowMoveResize() QTRY_COMPARE(widget.size(), rect.size()); // move() while shown - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { widget.move(r.topLeft()); widget.resize(r.size()); QApplication::processEvents(); @@ -5169,11 +6600,11 @@ void tst_QWidget::windowMoveResize() QTRY_COMPARE(widget.size(), rect.size()); // move() after hide() - for (const QRect &r : qAsConst(rects)) { + for (const QRect &r : std::as_const(rects)) { widget.move(r.topLeft()); widget.resize(r.size()); QApplication::processEvents(); -#if defined(Q_OS_OSX) +#if defined(Q_OS_MACOS) if (r.width() == 0 && r.height() > 0) { widget.move(r.topLeft()); widget.resize(r.size()); @@ -5189,7 +6620,7 @@ void tst_QWidget::windowMoveResize() QTRY_COMPARE(widget.size(), rect.size()); // show() again, pos() should be the same - widget.show(); + QTestPrivate::androidCompatibleShow(&widget); if (rect.isValid()) QVERIFY(QTest::qWaitForWindowExposed(&widget)); QTest::qWait(10); @@ -5227,7 +6658,7 @@ public: r = QRegion(); } - void enterEvent(QEvent *) override { ++enters; } + void enterEvent(QEnterEvent *) override { ++enters; } void leaveEvent(QEvent *) override { ++leaves; } void resetCounts() @@ -5269,7 +6700,7 @@ bool verifyColor(QWidget &child, const QRegion ®ion, const QColor &color, int const QPixmap pixmap = grabBackingStore ? child.grab(rect) : grabWindow(window, rect.left(), rect.top(), rect.width(), rect.height()); - const QSize actualSize = pixmap.size() / pixmap.devicePixelRatioF(); + const QSize actualSize = pixmap.size() / pixmap.devicePixelRatio(); if (!QTest::qCompare(actualSize, rect.size(), "pixmap.size()", "rect.size()", __FILE__, callerLine)) return false; QPixmap expectedPixmap(pixmap); /* ensure equal formats */ @@ -5331,8 +6762,7 @@ void tst_QWidget::moveChild() parent.setStyle(style.data()); ColorWidget child(&parent, Qt::Widget, Qt::blue); - parent.setGeometry(QRect(QPoint(QApplication::desktop()->availableGeometry(&parent).topLeft()) + QPoint(50, 50), - QSize(200, 200))); + parent.setGeometry(QRect(m_availableTopLeft + QPoint(50, 50), QSize(200, 200))); child.setGeometry(25, 25, 50, 50); #ifndef QT_NO_CURSOR // Try to make sure the cursor is not in a taskbar area to prevent tooltips or window highlighting QCursor::setPos(parent.geometry().topRight() + QPoint(50 , 50)); @@ -5340,11 +6770,19 @@ void tst_QWidget::moveChild() parent.showNormal(); QVERIFY(QTest::qWaitForWindowExposed(&parent)); + if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) { + // On some platforms (macOS), the palette will be different depending on if a + // window is active or not. And because of that, the whole window will be + // repainted when going from Inactive to Active. So wait for the window to be + // active before we continue, so the activation doesn't happen at a random + // time below. And call processEvents to have the paint events delivered right away. + QVERIFY(QTest::qWaitForWindowActive(&parent)); + qApp->processEvents(); + } + QTRY_COMPARE(parent.r, QRegion(parent.rect()) - child.geometry()); QTRY_COMPARE(child.r, QRegion(child.rect())); - if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT does not support setGeometry (and we cannot use QEXPECT_FAIL because of VERIFY_COLOR)"); VERIFY_COLOR(child, child.rect(), child.color); VERIFY_COLOR(parent, QRegion(parent.rect()) - child.geometry(), parent.color); @@ -5360,16 +6798,18 @@ void tst_QWidget::moveChild() QTRY_COMPARE(pos, child.pos()); QTRY_COMPARE(parent.r, QRegion(oldGeometry) - child.geometry()); -#if !defined(Q_OS_OSX) + // should be scrolled in backingstore QCOMPARE(child.r, QRegion()); -#endif VERIFY_COLOR(child, child.rect(), child.color); VERIFY_COLOR(parent, QRegion(parent.rect()) - child.geometry(), parent.color); } void tst_QWidget::showAndMoveChild() { +#ifdef ANDROID + QSKIP("Fails on Android due to removed grabWindow(): QTBUG-118849"); +#endif if (m_platform == QStringLiteral("wayland")) QSKIP("Wayland: This fails. Figure out why."); QWidget parent(nullptr, Qt::Window | Qt::WindowStaysOnTopHint); @@ -5378,8 +6818,7 @@ void tst_QWidget::showAndMoveChild() const QScopedPointer<QStyle> style(QStyleFactory::create(QLatin1String("Windows"))); parent.setStyle(style.data()); - QDesktopWidget desktop; - QRect desktopDimensions = desktop.availableGeometry(&parent); + QRect desktopDimensions = parent.screen()->availableGeometry(); desktopDimensions = desktopDimensions.adjusted(64, 64, -64, -64); #ifndef QT_NO_CURSOR // Try to make sure the cursor is not in a taskbar area to prevent tooltips or window highlighting @@ -5388,7 +6827,6 @@ void tst_QWidget::showAndMoveChild() parent.setGeometry(desktopDimensions); parent.setPalette(Qt::red); parent.show(); - QApplication::setActiveWindow(&parent); QVERIFY(QTest::qWaitForWindowActive(&parent)); QWidget child(&parent); @@ -5402,8 +6840,6 @@ void tst_QWidget::showAndMoveChild() child.move(desktopDimensions.width()/2, desktopDimensions.height()/2); QCoreApplication::processEvents(); - if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT does not support setGeometry (and we cannot use QEXPECT_FAIL because of VERIFY_COLOR)"); VERIFY_COLOR(child, child.rect(), Qt::blue); VERIFY_COLOR(parent, QRegion(parent.rect()) - child.geometry(), Qt::red); } @@ -5411,7 +6847,7 @@ void tst_QWidget::showAndMoveChild() void tst_QWidget::subtractOpaqueSiblings() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QSKIP("QTBUG-52974: Cocoa only has rect granularity."); #endif @@ -5488,14 +6924,12 @@ public slots: void tst_QWidget::multipleToplevelFocusCheck() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QSKIP("QTBUG-52974"); #endif - if (m_platform == QStringLiteral("wayland")) - QSKIP("Wayland: This fails. Figure out why."); - else if (m_platform == QStringLiteral("winrt")) - QSKIP("Winrt: Sometimes crashes in QTextLayout. - QTBUG-68297"); + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("Window activation is not supported"); TopLevelFocusCheck w1; TopLevelFocusCheck w2; @@ -5511,17 +6945,17 @@ void tst_QWidget::multipleToplevelFocusCheck() w2.show(); QVERIFY(QTest::qWaitForWindowExposed(&w2)); - QApplication::setActiveWindow(&w1); w1.activateWindow(); + QApplicationPrivate::setActiveWindow(&w1); QVERIFY(QTest::qWaitForWindowActive(&w1)); - QCOMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&w1)); + QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&w1)); QTest::mouseDClick(&w1, Qt::LeftButton); QTRY_COMPARE(QApplication::focusWidget(), static_cast<QWidget *>(w1.edit)); w2.activateWindow(); - QApplication::setActiveWindow(&w2); + QApplicationPrivate::setActiveWindow(&w2); QVERIFY(QTest::qWaitForWindowActive(&w2)); - QCOMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&w2)); + QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&w2)); QTest::mouseClick(&w2, Qt::LeftButton); QTRY_COMPARE(QApplication::focusWidget(), nullptr); @@ -5529,16 +6963,16 @@ void tst_QWidget::multipleToplevelFocusCheck() QTRY_COMPARE(QApplication::focusWidget(), static_cast<QWidget *>(w2.edit)); w1.activateWindow(); - QApplication::setActiveWindow(&w1); + QApplicationPrivate::setActiveWindow(&w1); QVERIFY(QTest::qWaitForWindowActive(&w1)); - QCOMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&w1)); + QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&w1)); QTest::mouseDClick(&w1, Qt::LeftButton); QTRY_COMPARE(QApplication::focusWidget(), static_cast<QWidget *>(w1.edit)); w2.activateWindow(); - QApplication::setActiveWindow(&w2); + QApplicationPrivate::setActiveWindow(&w2); QVERIFY(QTest::qWaitForWindowActive(&w2)); - QCOMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&w2)); + QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(&w2)); QTest::mouseClick(&w2, Qt::LeftButton); QTRY_COMPARE(QApplication::focusWidget(), nullptr); } @@ -5587,6 +7021,9 @@ public: void tst_QWidget::setFocus() { + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("Window activation is not supported"); + QScopedPointer<QWidget> testWidget(new QWidget); testWidget->resize(m_testWidgetSize); testWidget->setWindowTitle(__FUNCTION__); @@ -5598,7 +7035,7 @@ void tst_QWidget::setFocus() { // move focus to another window testWidget->activateWindow(); - QApplication::setActiveWindow(testWidget.data()); + QApplicationPrivate::setActiveWindow(testWidget.data()); if (testWidget->focusWidget()) testWidget->focusWidget()->clearFocus(); else @@ -5644,15 +7081,13 @@ void tst_QWidget::setFocus() // note: window may be active, but we don't want it to be testWidget->activateWindow(); - QApplication::setActiveWindow(testWidget.data()); + QApplicationPrivate::setActiveWindow(testWidget.data()); if (testWidget->focusWidget()) testWidget->focusWidget()->clearFocus(); else testWidget->clearFocus(); child1.setFocus(); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT fails here - QTBUG-68297", Abort); QVERIFY(!child1.hasFocus()); QCOMPARE(window.focusWidget(), &child1); QCOMPARE(QApplication::focusWidget(), nullptr); @@ -5822,34 +7257,6 @@ void tst_QWidget::setFocus() } } -template<class T> class EventSpy : public QObject -{ -public: - EventSpy(T *widget, QEvent::Type event) - : m_widget(widget), eventToSpy(event) - { - if (m_widget) - m_widget->installEventFilter(this); - } - - T *widget() const { return m_widget; } - int count() const { return m_count; } - void clear() { m_count = 0; } - -protected: - bool eventFilter(QObject *object, QEvent *event) override - { - if (event->type() == eventToSpy) - ++m_count; - return QObject::eventFilter(object, event); - } - -private: - T *m_widget; - const QEvent::Type eventToSpy; - int m_count = 0; -}; - #ifndef QT_NO_CURSOR void tst_QWidget::setCursor() { @@ -5975,6 +7382,9 @@ void tst_QWidget::setCursor() void tst_QWidget::setToolTip() { + if (QApplication::platformName().startsWith(QLatin1String("wayland"))) + QSKIP("Setting mouse cursor position is not possible on Wayland"); + QWidget widget; widget.resize(200, 200); // Showing the widget is not required for the tooltip event count test @@ -5996,6 +7406,9 @@ void tst_QWidget::setToolTip() QCOMPARE(widget.toolTip(), QString()); QCOMPARE(spy.count(), 2); + const int wakeUpDelay = widget.style()->styleHint(QStyle::SH_ToolTip_WakeUpDelay); + const int fallAsleepDelay = widget.style()->styleHint(QStyle::SH_ToolTip_FallAsleepDelay); + for (int pass = 0; pass < 2; ++pass) { QCursor::setPos(m_safeCursorPos); QScopedPointer<QWidget> popup(new QWidget(nullptr, Qt::Popup)); @@ -6015,12 +7428,12 @@ void tst_QWidget::setToolTip() QWindow *popupWindow = popup->windowHandle(); QTest::qWait(10); QTest::mouseMove(popupWindow, QPoint(25, 25)); - QTest::qWait(900); // delay is 700 + QTest::qWait(wakeUpDelay + 200); QCOMPARE(spy1.count(), 1); QCOMPARE(spy2.count(), 0); if (pass == 0) - QTest::qWait(2200); // delay is 2000 + QTest::qWait(fallAsleepDelay + 200); QTest::mouseMove(popupWindow); } @@ -6047,7 +7460,7 @@ void tst_QWidget::testWindowIconChangeEventPropagation() QWidgetList widgets; widgets << &topLevelWidget << &topLevelChild << &dialog << &dialogChild; - QCOMPARE(widgets.count(), 4); + QCOMPARE(widgets.size(), 4); topLevelWidget.show(); dialog.show(); @@ -6061,13 +7474,13 @@ void tst_QWidget::testWindowIconChangeEventPropagation() // Create spy lists. QList <EventSpyPtr> applicationEventSpies; QList <EventSpyPtr> widgetEventSpies; - for (QWidget *widget : qAsConst(widgets)) { + for (QWidget *widget : std::as_const(widgets)) { applicationEventSpies.append(EventSpyPtr::create(widget, QEvent::ApplicationWindowIconChange)); widgetEventSpies.append(EventSpyPtr::create(widget, QEvent::WindowIconChange)); } QList <WindowEventSpyPtr> appWindowEventSpies; QList <WindowEventSpyPtr> windowEventSpies; - for (QWindow *window : qAsConst(windows)) { + for (QWindow *window : std::as_const(windows)) { appWindowEventSpies.append(WindowEventSpyPtr::create(window, QEvent::ApplicationWindowIconChange)); windowEventSpies.append(WindowEventSpyPtr::create(window, QEvent::WindowIconChange)); } @@ -6076,7 +7489,7 @@ void tst_QWidget::testWindowIconChangeEventPropagation() const QIcon windowIcon = qApp->style()->standardIcon(QStyle::SP_TitleBarMenuButton); qApp->setWindowIcon(windowIcon); - for (int i = 0; i < widgets.count(); ++i) { + for (int i = 0; i < widgets.size(); ++i) { // Check QEvent::ApplicationWindowIconChange EventSpyPtr spy = applicationEventSpies.at(i); QWidget *widget = spy->widget(); @@ -6093,7 +7506,7 @@ void tst_QWidget::testWindowIconChangeEventPropagation() QCOMPARE(spy->count(), 1); spy->clear(); } - for (int i = 0; i < windows.count(); ++i) { + for (int i = 0; i < windows.size(); ++i) { // Check QEvent::ApplicationWindowIconChange (sent to QWindow) // QWidgetWindows don't get this event, since the widget takes care of changing the icon WindowEventSpyPtr spy = appWindowEventSpies.at(i); @@ -6111,7 +7524,7 @@ void tst_QWidget::testWindowIconChangeEventPropagation() // Set icon on a top-level widget. topLevelWidget.setWindowIcon(QIcon()); - for (int i = 0; i < widgets.count(); ++i) { + for (int i = 0; i < widgets.size(); ++i) { // Check QEvent::ApplicationWindowIconChange EventSpyPtr spy = applicationEventSpies.at(i); QCOMPARE(spy->count(), 0); @@ -6140,7 +7553,7 @@ void tst_QWidget::minAndMaxSizeWithX11BypassWindowManagerHint() if (m_platform != QStringLiteral("xcb")) QSKIP("This test is for X11 only."); // Same size as in QWidgetPrivate::create. - const QSize desktopSize = QApplication::desktop()->size(); + const QSize desktopSize = QGuiApplication::primaryScreen()->size(); const QSize originalSize(desktopSize.width() / 2, desktopSize.height() * 4 / 10); { // Maximum size. @@ -6207,7 +7620,11 @@ public: return false; } +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + bool nativeEvent(const QByteArray &eventType, void *message, qintptr *) override +#else bool nativeEvent(const QByteArray &eventType, void *message, long *) override +#endif { if (isMapNotify(eventType, message)) gotExpectedMapNotify = true; @@ -6215,7 +7632,11 @@ public: } // QAbstractNativeEventFilter interface +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *) override +#else bool nativeEventFilter(const QByteArray &eventType, void *message, long *) override +#endif { if (isMapNotify(eventType, message)) gotExpectedGlobalEvent = true; @@ -6260,14 +7681,12 @@ void tst_QWidget::clean_qt_x11_enforce_cursor() child->setAttribute(Qt::WA_SetCursor, true); window.show(); - QApplication::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowActive(&window)); QTest::qWait(100); QCursor::setPos(window.geometry().center()); QTest::qWait(100); child->setFocus(); - QApplication::processEvents(); QTest::qWait(100); delete w; @@ -6303,8 +7722,19 @@ public: bool eventFilter(QObject *object, QEvent *event) override { QWidget *widget = qobject_cast<QWidget *>(object); - if (widget && !event->spontaneous()) - events.append(qMakePair(widget, event->type())); + if (widget && !event->spontaneous()) { + switch (event->type()) { + // we might get those events if we couldn't move the cursor + case QEvent::Enter: + case QEvent::Leave: + // we might get this on systems that have an input method installed + case QEvent::InputMethodQuery: + break; + default: + events.append(qMakePair(widget, event->type())); + break; + } + } return false; } @@ -6344,15 +7774,8 @@ QByteArray EventRecorder::msgEventListMismatch(const EventList &expected, const void tst_QWidget::childEvents() { - if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT: This fails. QTBUG-68297."); EventRecorder::EventList expected; - // Move away the cursor; otherwise it might result in an enter event if it's - // inside the widget when the widget is shown. - QCursor::setPos(m_safeCursorPos); - QTest::qWait(100); - { // no children created, not shown QWidget widget; @@ -6392,7 +7815,9 @@ void tst_QWidget::childEvents() << qMakePair(&widget, QEvent::Move) << qMakePair(&widget, QEvent::Resize) << qMakePair(&widget, QEvent::Show) +#ifndef Q_OS_ANDROID << qMakePair(&widget, QEvent::CursorChange) +#endif << qMakePair(&widget, QEvent::ShowToParent); QVERIFY2(spy.eventList() == expected, @@ -6481,7 +7906,9 @@ void tst_QWidget::childEvents() << qMakePair(&widget, QEvent::Move) << qMakePair(&widget, QEvent::Resize) << qMakePair(&widget, QEvent::Show) +#ifndef Q_OS_ANDROID << qMakePair(&widget, QEvent::CursorChange) +#endif << qMakePair(&widget, QEvent::ShowToParent); QVERIFY2(spy.eventList() == expected, @@ -6573,7 +8000,9 @@ void tst_QWidget::childEvents() << qMakePair(&widget, QEvent::Move) << qMakePair(&widget, QEvent::Resize) << qMakePair(&widget, QEvent::Show) +#ifndef Q_OS_ANDROID << qMakePair(&widget, QEvent::CursorChange) +#endif << qMakePair(&widget, QEvent::ShowToParent); QVERIFY2(spy.eventList() == expected, @@ -6607,7 +8036,7 @@ public: } protected: - void paintEvent(QPaintEvent *) + void paintEvent(QPaintEvent *) override { if (ellipse) { QPainter painter(this); @@ -6628,7 +8057,9 @@ private: void tst_QWidget::render() { - return; +#ifdef Q_OS_ANDROID + QSKIP("QTBUG-118984: crashes on Android."); +#endif QCalendarWidget source; source.setWindowTitle(QLatin1String(QTest::currentTestFunction())); // disable anti-aliasing to eliminate potential differences when subpixel antialiasing @@ -6643,16 +8074,11 @@ void tst_QWidget::render() RenderWidget target(&source); target.resize(source.size()); target.show(); - - QCoreApplication::processEvents(); - QCoreApplication::sendPostedEvents(); - QTest::qWait(250); + QVERIFY(QTest::qWaitForWindowExposed(&target)); const QImage sourceImage = source.grab(QRect(QPoint(0, 0), QSize(-1, -1))).toImage(); - QCoreApplication::processEvents(); QImage targetImage = target.grab(QRect(QPoint(0, 0), QSize(-1, -1))).toImage(); - QCoreApplication::processEvents(); - QCOMPARE(sourceImage, targetImage); + QTRY_COMPARE(sourceImage, targetImage); // Fill target.rect() will Qt::red and render // QRegion(0, 0, source->width(), source->height() / 2, QRegion::Ellipse) @@ -6665,66 +8091,73 @@ void tst_QWidget::render() QVERIFY(sourceImage != targetImage); QCOMPARE(targetImage.pixel(target.width() / 2, 29), QColor(Qt::red).rgb()); + if (targetImage.devicePixelRatioF() > 1) + QEXPECT_FAIL("", "This test fails on high-DPI displays", Continue); QCOMPARE(targetImage.pixel(target.width() / 2, 30), sourceImage.pixel(source.width() / 2, 0)); +} - // Test that a child widget properly fills its background - { - QWidget window; - window.setWindowTitle(QLatin1String(QTest::currentTestFunction())); - window.resize(100, 100); - // prevent custom styles - window.setStyle(QStyleFactory::create(QLatin1String("Windows"))); - window.show(); - QVERIFY(QTest::qWaitForWindowExposed(&window)); - QWidget child(&window); - child.resize(window.size()); - child.show(); +// Test that a child widget properly fills its background +void tst_QWidget::renderChildFillsBackground() +{ + QWidget window; + window.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + window.resize(100, 100); + // prevent custom styles + window.setStyle(QStyleFactory::create(QLatin1String("Windows"))); + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QWidget child(&window); + child.resize(window.size()); + child.show(); - QCoreApplication::processEvents(); - const QPixmap childPixmap = child.grab(QRect(QPoint(0, 0), QSize(-1, -1))); - const QPixmap windowPixmap = window.grab(QRect(QPoint(0, 0), QSize(-1, -1))); - QCOMPARE(childPixmap, windowPixmap); - } + QCoreApplication::processEvents(); + const QPixmap childPixmap = child.grab(QRect(QPoint(0, 0), QSize(-1, -1))); + const QPixmap windowPixmap = window.grab(QRect(QPoint(0, 0), QSize(-1, -1))); +#ifndef Q_OS_ANDROID + // On Android all widgets are shown maximized, so the pixmaps + // will be similar + if (!m_platform.startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QEXPECT_FAIL("", "This test fails on all platforms", Continue); +#endif + QCOMPARE(childPixmap, windowPixmap); +} - { // Check that the target offset is correct. - QWidget widget; - widget.setWindowTitle(QLatin1String(QTest::currentTestFunction())); - widget.resize(200, 200); - widget.setAutoFillBackground(true); - widget.setPalette(Qt::red); - // prevent custom styles - widget.setStyle(QStyleFactory::create(QLatin1String("Windows"))); - widget.show(); - QVERIFY(QTest::qWaitForWindowExposed(&widget)); - QImage image(widget.size(), QImage::Format_RGB32); - image.fill(QColor(Qt::blue).rgb()); - - // Target offset (0, 0) - widget.render(&image, QPoint(), QRect(20, 20, 100, 100)); - QCOMPARE(image.pixel(0, 0), QColor(Qt::red).rgb()); - QCOMPARE(image.pixel(99, 99), QColor(Qt::red).rgb()); - QCOMPARE(image.pixel(100, 100), QColor(Qt::blue).rgb()); - - // Target offset (20, 20). - image.fill(QColor(Qt::blue).rgb()); - widget.render(&image, QPoint(20, 20), QRect(20, 20, 100, 100)); - QCOMPARE(image.pixel(0, 0), QColor(Qt::blue).rgb()); - QCOMPARE(image.pixel(19, 19), QColor(Qt::blue).rgb()); - QCOMPARE(image.pixel(20, 20), QColor(Qt::red).rgb()); - QCOMPARE(image.pixel(119, 119), QColor(Qt::red).rgb()); - QCOMPARE(image.pixel(120, 120), QColor(Qt::blue).rgb()); - } -} - -// On Windows the active palette is used instead of the inactive palette even -// though the widget is invisible. This is probably related to task 178507/168682, -// but for the renderInvisible test it doesn't matter, we're mostly interested -// in testing the geometry so just workaround the palette issue for now. +void tst_QWidget::renderTargetOffset() +{ // Check that the target offset is correct. + QWidget widget; + widget.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + widget.resize(200, 200); + widget.setAutoFillBackground(true); + widget.setPalette(Qt::red); + // prevent custom styles + widget.setStyle(QStyleFactory::create(QLatin1String("Windows"))); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + QImage image(widget.size(), QImage::Format_RGB32); + image.fill(QColor(Qt::blue).rgb()); + + // Target offset (0, 0) + widget.render(&image, QPoint(), QRect(20, 20, 100, 100)); + QCOMPARE(image.pixel(0, 0), QColor(Qt::red).rgb()); + QCOMPARE(image.pixel(99, 99), QColor(Qt::red).rgb()); + QCOMPARE(image.pixel(100, 100), QColor(Qt::blue).rgb()); + + // Target offset (20, 20). + image.fill(QColor(Qt::blue).rgb()); + widget.render(&image, QPoint(20, 20), QRect(20, 20, 100, 100)); + QCOMPARE(image.pixel(0, 0), QColor(Qt::blue).rgb()); + QCOMPARE(image.pixel(19, 19), QColor(Qt::blue).rgb()); + QCOMPARE(image.pixel(20, 20), QColor(Qt::red).rgb()); + QCOMPARE(image.pixel(119, 119), QColor(Qt::red).rgb()); + QCOMPARE(image.pixel(120, 120), QColor(Qt::blue).rgb()); +} + +// On some platforms the active palette is used instead of the inactive palette even +// though the widget is invisible, but for the renderInvisible test it doesn't matter, +// as we're mostly interested in testing the geometry, so just workaround the palette +// issue for now. static void workaroundPaletteIssue(QWidget *widget) { -#ifndef Q_OS_WIN - return; -#endif if (!widget) return; @@ -6744,8 +8177,9 @@ void tst_QWidget::renderInvisible() { if (m_platform == QStringLiteral("xcb")) QSKIP("QTBUG-26424"); - if (m_platform == QStringLiteral("winrt")) - QSKIP("WinRT: This fails. QTBUG-68297."); + + if (m_platform.startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: Skip this test, see also QTBUG-107157"); QScopedPointer<QCalendarWidget> calendar(new QCalendarWidget); calendar->move(m_availableTopLeft + QPoint(100, 100)); @@ -6764,12 +8198,10 @@ void tst_QWidget::renderInvisible() dummyFocusWidget.move(calendar->geometry().bottomLeft() + QPoint(0, 100)); dummyFocusWidget.show(); QVERIFY(QTest::qWaitForWindowExposed(&dummyFocusWidget)); - QCoreApplication::processEvents(); - QTest::qWait(120); // Create normal reference image. const QSize calendarSize = calendar->size(); - QImage referenceImage(calendarSize, QImage::Format_ARGB32); + QImage referenceImage(calendarSize, QImage::Format_ARGB32_Premultiplied); calendar->render(&referenceImage); #ifdef RENDER_DEBUG referenceImage.save("referenceImage.png"); @@ -6779,9 +8211,8 @@ void tst_QWidget::renderInvisible() // Create resized reference image. const QSize calendarSizeResized = calendar->size() + QSize(50, 50); calendar->resize(calendarSizeResized); - QCoreApplication::processEvents(); QTest::qWait(30); - QImage referenceImageResized(calendarSizeResized, QImage::Format_ARGB32); + QImage referenceImageResized(calendarSizeResized, QImage::Format_ARGB32_Premultiplied); calendar->render(&referenceImageResized); #ifdef RENDER_DEBUG referenceImageResized.save("referenceImageResized.png"); @@ -6790,12 +8221,11 @@ void tst_QWidget::renderInvisible() // Explicitly hide the calendar. calendar->hide(); - QCoreApplication::processEvents(); QTest::qWait(30); workaroundPaletteIssue(calendar.data()); { // Make sure we get the same image when the calendar is explicitly hidden. - QImage testImage(calendarSizeResized, QImage::Format_ARGB32); + QImage testImage(calendarSizeResized, QImage::Format_ARGB32_Premultiplied); calendar->render(&testImage); #ifdef RENDER_DEBUG testImage.save("explicitlyHiddenCalendarResized.png"); @@ -6811,7 +8241,7 @@ void tst_QWidget::renderInvisible() workaroundPaletteIssue(calendar.data()); { // Never been visible, created or laid out. - QImage testImage(calendarSize, QImage::Format_ARGB32); + QImage testImage(calendarSize, QImage::Format_ARGB32_Premultiplied); calendar->render(&testImage); #ifdef RENDER_DEBUG testImage.save("neverBeenVisibleCreatedOrLaidOut.png"); @@ -6820,11 +8250,10 @@ void tst_QWidget::renderInvisible() } calendar->hide(); - QCoreApplication::processEvents(); QTest::qWait(30); { // Calendar explicitly hidden. - QImage testImage(calendarSize, QImage::Format_ARGB32); + QImage testImage(calendarSize, QImage::Format_ARGB32_Premultiplied); calendar->render(&testImage); #ifdef RENDER_DEBUG testImage.save("explicitlyHiddenCalendar.png"); @@ -6838,7 +8267,7 @@ void tst_QWidget::renderInvisible() navigationBar->hide(); { // Check that the navigation bar isn't drawn when rendering the entire calendar. - QImage testImage(calendarSize, QImage::Format_ARGB32); + QImage testImage(calendarSize, QImage::Format_ARGB32_Premultiplied); calendar->render(&testImage); #ifdef RENDER_DEBUG testImage.save("calendarWithoutNavigationBar.png"); @@ -6847,7 +8276,7 @@ void tst_QWidget::renderInvisible() } { // Make sure the navigation bar renders correctly even though it's hidden. - QImage testImage(navigationBar->size(), QImage::Format_ARGB32); + QImage testImage(navigationBar->size(), QImage::Format_ARGB32_Premultiplied); navigationBar->render(&testImage); #ifdef RENDER_DEBUG testImage.save("explicitlyHiddenNavigationBar.png"); @@ -6861,7 +8290,7 @@ void tst_QWidget::renderInvisible() { // Render next month button. // Fill test image with correct background color. - QImage testImage(nextMonthButton->size(), QImage::Format_ARGB32); + QImage testImage(nextMonthButton->size(), QImage::Format_ARGB32_Premultiplied); navigationBar->render(&testImage, QPoint(), QRegion(), QWidget::RenderFlags()); #ifdef RENDER_DEBUG testImage.save("nextMonthButtonBackground.png"); @@ -6888,7 +8317,6 @@ void tst_QWidget::renderInvisible() // Navigation bar isn't explicitly hidden anymore. navigationBar->show(); - QCoreApplication::processEvents(); QTest::qWait(30); QVERIFY(!calendar->isVisible()); @@ -6900,7 +8328,7 @@ void tst_QWidget::renderInvisible() QCoreApplication::processEvents(); { // Make sure we get an image equal to the resized reference image. - QImage testImage(calendarSizeResized, QImage::Format_ARGB32); + QImage testImage(calendarSizeResized, QImage::Format_ARGB32_Premultiplied); calendar->render(&testImage); #ifdef RENDER_DEBUG testImage.save("calendarResized.png"); @@ -6912,7 +8340,7 @@ void tst_QWidget::renderInvisible() QCalendarWidget calendar; const QSize calendarSize = calendar.sizeHint(); - QImage image(2 * calendarSize, QImage::Format_ARGB32); + QImage image(2 * calendarSize, QImage::Format_ARGB32_Premultiplied); image.fill(QColor(Qt::red).rgb()); calendar.render(&image); @@ -7046,12 +8474,53 @@ void tst_QWidget::renderWithPainter() // Make sure QWidget::render does not modify the render hints set on the painter. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform - | QPainter::NonCosmeticDefaultPen | QPainter::TextAntialiasing); + | QPainter::TextAntialiasing); QPainter::RenderHints oldRenderHints = painter.renderHints(); widget.render(&painter); QCOMPARE(painter.renderHints(), oldRenderHints); } +void tst_QWidget::renderRTL() +{ + QFont f; + f.setStyleStrategy(QFont::NoAntialias); + const QScopedPointer<QStyle> style(QStyleFactory::create(QLatin1String("Windows"))); + + QMenu menu; + menu.setMinimumWidth(200); + menu.setFont(f); + menu.setStyle(style.data()); + menu.addAction("I"); + menu.show(); + menu.setLayoutDirection(Qt::LeftToRight); + QVERIFY(QTest::qWaitForWindowExposed(&menu)); + + QImage imageLTR(menu.size(), QImage::Format_ARGB32); + menu.render(&imageLTR); + //imageLTR.save("/tmp/rendered_1.png"); + + menu.setLayoutDirection(Qt::RightToLeft); + QImage imageRTL(menu.size(), QImage::Format_ARGB32); + menu.render(&imageRTL); + imageRTL = imageRTL.mirrored(true, false); + //imageRTL.save("/tmp/rendered_2.png"); + + QCOMPARE(imageLTR.height(), imageRTL.height()); + QCOMPARE(imageLTR.width(), imageRTL.width()); + static constexpr auto border = 4; + for (int h = border; h < imageRTL.height() - border; ++h) { + // there should be no difference on the right (aka no text) + for (int w = imageRTL.width() / 2; w < imageRTL.width() - border; ++w) { + auto pixLTR = imageLTR.pixel(w, h); + auto pixRTL = imageRTL.pixel(w, h); + if (pixLTR != pixRTL) + qDebug() << "Pixel do not match at" << w << h << ":" + << Qt::hex << pixLTR << "<->" << pixRTL; + QCOMPARE(pixLTR, pixRTL); + } + } +} + void tst_QWidget::render_task188133() { QMainWindow mainWindow; @@ -7059,7 +8528,7 @@ void tst_QWidget::render_task188133() // Make sure QWidget::render does not trigger QWidget::repaint/update // and asserts for Qt::WA_WState_Created. const QPixmap pixmap = mainWindow.grab(QRect(QPoint(0, 0), QSize(-1, -1))); - Q_UNUSED(pixmap) + Q_UNUSED(pixmap); } void tst_QWidget::render_task211796() @@ -7245,7 +8714,7 @@ void tst_QWidget::render_systemClip() // rrrrrrrrrr // ... -#ifndef Q_OS_OSX +#ifndef Q_OS_MACOS for (int i = 0; i < image.height(); ++i) { for (int j = 0; j < image.width(); ++j) { if (i < 50 && j < i) @@ -7599,7 +9068,7 @@ void tst_QWidget::moveWindowInShowEvent_data() QTest::addColumn<QPoint>("initial"); QTest::addColumn<QPoint>("position"); - QPoint p = QGuiApplication::primaryScreen()->availableGeometry().topLeft(); + QPoint p = m_availableTopLeft; QTest::newRow("1") << p << (p + QPoint(10, 10)); QTest::newRow("2") << (p + QPoint(10,10)) << p; @@ -7636,23 +9105,15 @@ void tst_QWidget::moveWindowInShowEvent() // show it widget.showNormal(); QVERIFY(QTest::qWaitForWindowExposed(&widget)); - QTest::qWait(100); // it should have moved QCOMPARE(widget.pos(), position); } void tst_QWidget::repaintWhenChildDeleted() { -#ifdef Q_OS_WIN - if (QSysInfo::WindowsVersion & QSysInfo::WV_VISTA) { - QTest::qWait(1000); - } -#endif ColorWidget w(nullptr, Qt::FramelessWindowHint, Qt::red); w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); - QPoint startPoint = QApplication::desktop()->availableGeometry(&w).topLeft(); - startPoint.rx() += 50; - startPoint.ry() += 50; + const QPoint startPoint = m_availableTopLeft + QPoint(50, 50); w.setGeometry(QRect(startPoint, QSize(100, 100))); w.show(); QVERIFY(QTest::qWaitForWindowExposed(&w)); @@ -7675,9 +9136,7 @@ void tst_QWidget::hideOpaqueChildWhileHidden() { ColorWidget w(nullptr, Qt::FramelessWindowHint, Qt::red); w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); - QPoint startPoint = QApplication::desktop()->availableGeometry(&w).topLeft(); - startPoint.rx() += 50; - startPoint.ry() += 50; + const QPoint startPoint = m_availableTopLeft + QPoint(50, 50); w.setGeometry(QRect(startPoint, QSize(100, 100))); ColorWidget child(&w, Qt::Widget, Qt::blue); @@ -7688,6 +9147,17 @@ void tst_QWidget::hideOpaqueChildWhileHidden() w.show(); QVERIFY(QTest::qWaitForWindowExposed(&w)); + + if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) { + // On some platforms (macOS), the palette will be different depending on if a + // window is active or not. And because of that, the whole window will be + // repainted when going from Inactive to Active. So wait for the window to be + // active before we continue, so the activation doesn't happen at a random + // time below. And call processEvents to have the paint events delivered right away. + QVERIFY(QTest::qWaitForWindowActive(&w)); + qApp->processEvents(); + } + QTRY_COMPARE(child2.r, QRegion(child2.rect())); child.r = QRegion(); child2.r = QRegion(); @@ -7696,8 +9166,6 @@ void tst_QWidget::hideOpaqueChildWhileHidden() child.hide(); child2.hide(); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT: This fails. QTBUG-68297.", Abort); QTRY_COMPARE(w.r, QRegion(child.geometry())); child.show(); @@ -7717,6 +9185,7 @@ void tst_QWidget::updateWhileMinimized() QSKIP("Platform does not support showMinimized()"); #endif UpdateWidget widget; + widget.setPalette(simplePalette()); widget.setWindowTitle(QLatin1String(QTest::currentTestFunction())); // Filter out activation change and focus events to avoid update() calls in QWidget. widget.updateOnActivationChangeAndFocusIn = false; @@ -7736,15 +9205,22 @@ void tst_QWidget::updateWhileMinimized() // Make sure update requests are discarded until the widget is shown again. widget.update(0, 0, 50, 50); QTest::qWait(10); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT: This fails. QTBUG-68297.", Abort); - QCOMPARE(widget.numPaintEvents, 0); + int count = 0; + // mutter/GNOME Shell doesn't unmap when minimizing window. + // More details at https://gitlab.gnome.org/GNOME/mutter/issues/185 + if (m_platform == QStringLiteral("xcb")) { + const QString desktop = qgetenv("XDG_CURRENT_DESKTOP"); + qDebug() << "xcb: XDG_CURRENT_DESKTOP=" << desktop; + if (desktop == QStringLiteral("ubuntu:GNOME") + || desktop == QStringLiteral("GNOME-Classic:GNOME") + || desktop == QStringLiteral("GNOME") + || desktop.isEmpty()) // on local VMs + count = 1; + } + QCOMPARE(widget.numPaintEvents, count); // Restore window. widget.showNormal(); - QTest::qWait(30); - if (m_platform == QStringLiteral("xcb")) - QSKIP("QTBUG-26424"); QTRY_COMPARE(widget.numPaintEvents, 1); QCOMPARE(widget.paintedRegion, QRegion(0, 0, 50, 50)); } @@ -8004,6 +9480,40 @@ void tst_QWidget::alienWidgets() } } +using WidgetAttributes = QList<Qt::WidgetAttribute>; + +void tst_QWidget::nativeWindowPosition_data() +{ + QTest::addColumn<WidgetAttributes>("attributes"); + + QTest::newRow("non-native all the way") + << WidgetAttributes{}; + QTest::newRow("native all the way") + << WidgetAttributes{ Qt::WA_NativeWindow }; + QTest::newRow("native with non-native ancestor") + << WidgetAttributes{ Qt::WA_NativeWindow, Qt::WA_DontCreateNativeAncestors }; +} + +void tst_QWidget::nativeWindowPosition() +{ + QWidget topLevel; + QWidget child(&topLevel); + child.move(5, 5); + + QWidget grandChild(&child); + grandChild.move(10, 10); + + QFETCH(WidgetAttributes, attributes); + for (auto attribute : attributes) + grandChild.setAttribute(attribute); + + topLevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + + QCOMPARE(child.pos(), QPoint(5, 5)); + QCOMPARE(grandChild.pos(), QPoint(10, 10)); +} + class ASWidget : public QWidget { public: @@ -8018,7 +9528,7 @@ public: sp.setHeightForWidth(hfwLayout); QVBoxLayout *vbox = new QVBoxLayout; - vbox->setMargin(0); + vbox->setContentsMargins(0, 0, 0, 0); vbox->addWidget(new ASWidget(sizeHint + QSize(30, 20), sp, false, false)); setLayout(vbox); } @@ -8240,15 +9750,16 @@ void tst_QWidget::sendUpdateRequestImmediately() void tst_QWidget::doubleRepaint() { -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS QSKIP("QTBUG-52974"); #endif -#if defined(Q_OS_OSX) +#if defined(Q_OS_MACOS) if (!macHasAccessToWindowsServer()) QSKIP("Not having window server access causes the wrong number of repaints to be issues"); #endif UpdateWidget widget; + widget.setPalette(simplePalette()); widget.setWindowTitle(QLatin1String(QTest::currentTestFunction())); centerOnScreen(&widget); widget.setFocusPolicy(Qt::StrongFocus); @@ -8265,11 +9776,6 @@ void tst_QWidget::doubleRepaint() // Minmize: Should not trigger a repaint. widget.showMinimized(); QTest::qWait(10); -#if defined(Q_OS_QNX) - QEXPECT_FAIL("", "Platform does not support showMinimized()", Continue); -#endif - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT: This fails. QTBUG-68297.", Abort); QCOMPARE(widget.numPaintEvents, 0); widget.numPaintEvents = 0; @@ -8285,9 +9791,10 @@ void tst_QWidget::resizeInPaintEvent() QWidget window; window.setWindowTitle(QLatin1String(QTest::currentTestFunction())); UpdateWidget widget(&window); + widget.setPalette(simplePalette()); window.resize(200, 200); window.show(); - QApplication::setActiveWindow(&window); + QApplicationPrivate::setActiveWindow(&window); QVERIFY(QTest::qWaitForWindowExposed(&window)); QTRY_VERIFY(widget.numPaintEvents > 0); @@ -8296,13 +9803,10 @@ void tst_QWidget::resizeInPaintEvent() widget.resizeInPaintEvent = true; // This will call resize in the paintEvent, which in turn will call - // invalidateBuffer() and a new update request should be posted. - widget.repaint(); - QCOMPARE(widget.numPaintEvents, 1); - widget.numPaintEvents = 0; - - // Make sure the resize triggers another update. - QTRY_COMPARE(widget.numPaintEvents, 1); + // invalidateBackingStore() and a new update request should be posted. + // the resize triggers another update. + widget.update(); + QTRY_COMPARE(widget.numPaintEvents, 2); } void tst_QWidget::opaqueChildren() @@ -8344,6 +9848,46 @@ void tst_QWidget::opaqueChildren() QCOMPARE(qt_widget_private(&grandChild)->getOpaqueChildren(), QRegion()); } +void tst_QWidget::dumpObjectTree() +{ + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("QWindow::requestActivate() is not supported."); + + QWidget w; + w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + Q_SET_OBJECT_NAME(w); + w.move(100, 100); + w.resize(200, 200); + + QLineEdit le(&w); + Q_SET_OBJECT_NAME(le); + le.resize(200, 200); + + { + const char * const expected[] = { + "QWidget::w I", + " QLineEdit::le I", + " QWidgetLineControl:: ", + }; + for (const char *line : expected) + QTest::ignoreMessage(QtDebugMsg, line); + w.dumpObjectTree(); + } + + QTestPrivate::androidCompatibleShow(&w); + QVERIFY(QTest::qWaitForWindowActive(&w)); + + { + const char * const expected[] = { + "QWidget::w <200x200+100+100>", + " QLineEdit::le F<200x200+0+0>", + " QWidgetLineControl:: ", + }; + for (const char *line : expected) + QTest::ignoreMessage(QtDebugMsg, line); + w.dumpObjectTree(); + } +} class MaskSetWidget : public QWidget { @@ -8358,14 +9902,17 @@ public: paintedRegion += event->region(); for (const QRect &r : event->region()) p.fillRect(r, Qt::red); + + repainted = true; } void resizeEvent(QResizeEvent *) override { - setMask(QRegion(QRect(0, 0, width(), 10).normalized())); + setMask(QRegion(QRect(0, 0, width(), 10))); } QRegion paintedRegion; + bool repainted = false; public slots: void resizeDown() { setGeometry(QRect(0, 50, 50, 50)); } @@ -8375,6 +9922,7 @@ public slots: void tst_QWidget::setMaskInResizeEvent() { UpdateWidget w; + w.setPalette(simplePalette()); w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); w.reset(); w.resize(200, 200); @@ -8392,21 +9940,19 @@ void tst_QWidget::setMaskInResizeEvent() w.reset(); testWidget.paintedRegion = QRegion(); - QTimer::singleShot(0, &testWidget, SLOT(resizeDown())); - QTest::qWait(100); + testWidget.resizeDown(); QRegion expectedParentUpdate(0, 0, 100, 10); // Old testWidget area. expectedParentUpdate += testWidget.geometry(); // New testWidget area. - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT: This fails. QTBUG-68297.", Abort); + QTRY_VERIFY(testWidget.repainted); QTRY_COMPARE(w.paintedRegion, expectedParentUpdate); QTRY_COMPARE(testWidget.paintedRegion, testWidget.mask()); testWidget.paintedRegion = QRegion(); - // Now resize the widget again, but in the oposite direction - QTimer::singleShot(0, &testWidget, SLOT(resizeUp())); - QTest::qWait(100); - + testWidget.repainted = false; + // Now resize the widget again, but in the opposite direction + testWidget.resizeUp(); + QTRY_VERIFY(testWidget.repainted); QTRY_COMPARE(testWidget.paintedRegion, testWidget.mask()); } @@ -8449,12 +9995,14 @@ void tst_QWidget::moveInResizeEvent() QTRY_COMPARE(testWidget.geometry(), expectedGeometry); } -void tst_QWidget::immediateRepaintAfterInvalidateBuffer() +#ifdef QT_BUILD_INTERNAL +void tst_QWidget::immediateRepaintAfterInvalidateBackingStore() { if (m_platform != QStringLiteral("xcb") && m_platform != QStringLiteral("windows")) QSKIP("We don't support immediate repaint right after show on other platforms."); QScopedPointer<UpdateWidget> widget(new UpdateWidget); + widget->setPalette(simplePalette()); widget->setWindowTitle(QLatin1String(QTest::currentTestFunction())); centerOnScreen(widget.data()); widget->show(); @@ -8464,15 +10012,16 @@ void tst_QWidget::immediateRepaintAfterInvalidateBuffer() // Marks the area covered by the widget as dirty in the backing store and // posts an UpdateRequest event. - qt_widget_private(widget.data())->invalidateBuffer(widget->rect()); + qt_widget_private(widget.data())->invalidateBackingStore(widget->rect()); QCOMPARE(widget->numPaintEvents, 0); // The entire widget is already dirty, but this time we want to update immediately // by calling repaint(), and thus we have to repaint the widget and not wait for // the UpdateRequest to be sent when we get back to the event loop. - widget->repaint(); - QCOMPARE(widget->numPaintEvents, 1); + widget->update(); + QTRY_COMPARE(widget->numPaintEvents, 1); } +#endif void tst_QWidget::effectiveWinId() { @@ -8629,7 +10178,7 @@ void tst_QWidget::moveRect() child.move(10, 10); // Don't crash. } -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined(Q_OS_WIN) class GDIWidget : public QDialog { Q_OBJECT @@ -8696,7 +10245,7 @@ void tst_QWidget::paintOnScreenPossible() w2.setAttribute(Qt::WA_PaintOnScreen); QVERIFY(w2.testAttribute(Qt::WA_PaintOnScreen)); } -#endif // Q_OS_WIN && !Q_OS_WINRT +#endif // Q_OS_WIN void tst_QWidget::reparentStaticWidget() { @@ -8829,38 +10378,41 @@ public: void tst_QWidget::translucentWidget() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QPixmap pm(16,16); pm.fill(Qt::red); ColorRedWidget label; label.setWindowTitle(QLatin1String(QTest::currentTestFunction())); label.setFixedSize(16,16); label.setAttribute(Qt::WA_TranslucentBackground); - const QPoint labelPos = QGuiApplication::primaryScreen()->availableGeometry().topLeft(); - label.move(labelPos); + label.move(m_availableTopLeft); label.show(); QVERIFY(QTest::qWaitForWindowExposed(&label)); - QPixmap widgetSnapshot; - -#ifdef Q_OS_WIN - QWidget *desktopWidget = QApplication::desktop()->screen(0); - if (QSysInfo::windowsVersion() >= QSysInfo::WV_VISTA) - widgetSnapshot = grabWindow(desktopWidget->windowHandle(), labelPos.x(), labelPos.y(), label.width(), label.height()); - else -#endif - widgetSnapshot = label.grab(QRect(QPoint(0, 0), label.size())); + QPixmap widgetSnapshot = + label.grab(QRect(QPoint(0, 0), label.size())); const QImage actual = widgetSnapshot.toImage().convertToFormat(QImage::Format_RGB32); - const QImage expected = pm.toImage().scaled(label.devicePixelRatioF() * pm.size()); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT: This fails. QTBUG-68297.", Abort); + QImage expected = pm.toImage().scaled(label.devicePixelRatio() * pm.size()); + expected.setDevicePixelRatio(label.devicePixelRatio()); +#ifdef Q_OS_ANDROID + // Android uses Format_ARGB32_Premultiplied by default + expected = expected.convertToFormat(QImage::Format_RGB32); +#endif QCOMPARE(actual.size(),expected.size()); QCOMPARE(actual,expected); const QWindow *window = label.windowHandle(); - const QSurfaceFormat translucentFormat = window->requestedFormat(); + const QSurfaceFormat translucentFormat = window->format(); label.setAttribute(Qt::WA_TranslucentBackground, false); - const QSurfaceFormat opaqueFormat = window->requestedFormat(); - QVERIFY(translucentFormat != opaqueFormat); + // Changing WA_TranslucentBackground with an already created native window + // has no effect since Qt 5.0 due to the introduction of QWindow et al. + // This means that the change must *not* be reflected in the + // QSurfaceFormat, because there is no change when it comes to the + // underlying native window. Otherwise the state would no longer + // describe reality (the native window) See QTBUG-85714. + QVERIFY(translucentFormat == window->format()); } class MaskResizeTestWidget : public QWidget @@ -8869,7 +10421,7 @@ class MaskResizeTestWidget : public QWidget public: explicit MaskResizeTestWidget(QWidget* p = nullptr) : QWidget(p) { - setMask(QRegion(QRect(0, 0, 100, 100).normalized())); + setMask(QRegion(QRect(0, 0, 100, 100))); } void paintEvent(QPaintEvent* event) override @@ -8885,12 +10437,12 @@ public: public slots: void enlargeMask() { - QRegion newMask(QRect(0, 0, 150, 150).normalized()); + QRegion newMask(QRect(0, 0, 150, 150)); setMask(newMask); } void shrinkMask() { - QRegion newMask(QRect(0, 0, 50, 50).normalized()); + QRegion newMask(QRect(0, 0, 50, 50)); setMask(newMask); } @@ -8898,12 +10450,16 @@ public slots: void tst_QWidget::setClearAndResizeMask() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + UpdateWidget topLevel; + topLevel.setPalette(simplePalette()); topLevel.setWindowTitle(QLatin1String(QTest::currentTestFunction())); topLevel.resize(160, 160); centerOnScreen(&topLevel); topLevel.show(); - QApplication::setActiveWindow(&topLevel); + QApplicationPrivate::setActiveWindow(&topLevel); QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); QTRY_VERIFY(topLevel.numPaintEvents > 0); topLevel.reset(); @@ -8933,6 +10489,7 @@ void tst_QWidget::setClearAndResizeMask() } UpdateWidget child(&topLevel); + child.setPalette(simplePalette()); child.setAutoFillBackground(true); // NB! Opaque child. child.setPalette(Qt::red); child.resize(100, 100); @@ -8947,7 +10504,7 @@ void tst_QWidget::setClearAndResizeMask() child.setMask(childMask); QTRY_COMPARE(child.mask(), childMask); // and ensure that the child widget doesn't get any update. -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS // Mac always issues a full update when calling setMask, and we cannot force it to not do so. if (child.internalWinId()) QCOMPARE(child.numPaintEvents, 1); @@ -8969,7 +10526,7 @@ void tst_QWidget::setClearAndResizeMask() // and ensure that that the child widget gets an update for the area outside the old mask. QTRY_COMPARE(child.numPaintEvents, 1); outsideOldMask = child.rect(); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS // Mac always issues a full update when calling setMask, and we cannot force it to not do so. if (!child.internalWinId()) #endif @@ -8983,7 +10540,7 @@ void tst_QWidget::setClearAndResizeMask() // Mask child widget with a mask that is bigger than the rect child.setMask(QRegion(0, 0, 1000, 1000)); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS // Mac always issues a full update when calling setMask, and we cannot force it to not do so. if (child.internalWinId()) QTRY_COMPARE(child.numPaintEvents, 1); @@ -8996,7 +10553,7 @@ void tst_QWidget::setClearAndResizeMask() // ...and the same applies when clearing the mask. child.clearMask(); QTest::qWait(100); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS // Mac always issues a full update when calling setMask, and we cannot force it to not do so. if (child.internalWinId()) QTRY_VERIFY(child.numPaintEvents > 0); @@ -9026,7 +10583,7 @@ void tst_QWidget::setClearAndResizeMask() QTimer::singleShot(100, &resizeChild, SLOT(shrinkMask())); QTest::qWait(200); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS // Mac always issues a full update when calling setMask, and we cannot force it to not do so. if (child.internalWinId()) QTRY_COMPARE(resizeChild.paintedRegion, resizeChild.mask()); @@ -9038,7 +10595,7 @@ void tst_QWidget::setClearAndResizeMask() const QRegion oldMask = resizeChild.mask(); QTimer::singleShot(0, &resizeChild, SLOT(enlargeMask())); QTest::qWait(100); -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS // Mac always issues a full update when calling setMask, and we cannot force it to not do so. if (child.internalWinId()) QTRY_COMPARE(resizeChild.paintedRegion, resizeChild.mask()); @@ -9205,13 +10762,15 @@ void tst_QWidget::syntheticEnterLeave() { public: using QWidget::QWidget; - void enterEvent(QEvent *) override { ++numEnterEvents; } + void enterEvent(QEnterEvent *) override { ++numEnterEvents; } void leaveEvent(QEvent *) override { ++numLeaveEvents; } int numEnterEvents = 0; int numLeaveEvents = 0; }; QCursor::setPos(m_safeCursorPos); + if (!QTest::qWaitFor([this]{ return QCursor::pos() == m_safeCursorPos; })) + QSKIP("Can't move cursor"); MyWidget window; window.setWindowTitle(QLatin1String(QTest::currentTestFunction())); @@ -9252,7 +10811,8 @@ void tst_QWidget::syntheticEnterLeave() // Position the cursor in the middle of the window. const QPoint globalPos = window.mapToGlobal(QPoint(100, 100)); QCursor::setPos(globalPos); // Enter child2 and grandChild. - QTest::qWait(300); + if (!QTest::qWaitFor([globalPos]{ return QCursor::pos() == globalPos; })) + QSKIP("Can't move cursor"); QCOMPARE(window.numLeaveEvents, 0); QCOMPARE(child2->numLeaveEvents, 0); @@ -9303,6 +10863,129 @@ void tst_QWidget::syntheticEnterLeave() #endif #ifndef QT_NO_CURSOR +void tst_QWidget::enterLeaveOnWindowShowHide_data() +{ + QTest::addColumn<Qt::WindowType>("windowType"); + QTest::addRow("dialog") << Qt::Dialog; + QTest::addRow("popup") << Qt::Popup; +} + + +/*! + Verify that a window that has the mouse gets a leave event + when a dialog or popup opens (even if that dialog or popup is + not under the mouse), and an enter event when the secondary window + closes again (while the mouse is still over the original widget. + + Since mouse grabbing might cause some event interaction, simulate + the opening of the secondary window from a mouse press, like we would with + a button or context menu. See QTBUG-78970. +*/ +void tst_QWidget::enterLeaveOnWindowShowHide() +{ + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("QWindow::requestActivate() is not supported."); + + QFETCH(Qt::WindowType, windowType); + class Widget : public QWidget + { + public: + int numEnterEvents = 0; + int numLeaveEvents = 0; + QPoint enterPosition; + Qt::WindowType secondaryWindowType = {}; + protected: + void enterEvent(QEnterEvent *e) override + { + enterPosition = e->position().toPoint(); + ++numEnterEvents; + } + void leaveEvent(QEvent *) override + { + enterPosition = {}; + ++numLeaveEvents; + } + void mousePressEvent(QMouseEvent *e) override + { + QWidget *secondary = nullptr; + switch (secondaryWindowType) { + case Qt::Dialog: { + QDialog *dialog = new QDialog(this); + dialog->setModal(true); + dialog->setWindowModality(Qt::ApplicationModal); + secondary = dialog; + break; + } + case Qt::Popup: { + QMenu *menu = new QMenu(this); + menu->addAction("Action 1"); + menu->addAction("Action 2"); + secondary = menu; + break; + } + default: + QVERIFY2(false, "Test case not implemented for window type"); + break; + } + + QPoint secondaryPos = e->globalPosition().toPoint(); + if (e->button() == Qt::LeftButton) + secondaryPos += QPoint(10, 10); // cursor outside secondary + else + secondaryPos -= QPoint(10, 10); // cursor inside secondary + secondary->move(secondaryPos); + secondary->show(); + if (!QTest::qWaitForWindowExposed(secondary)) + QEXPECT_FAIL("", "Secondary window failed to show, test will fail", Abort); + if (secondaryWindowType == Qt::Dialog && QGuiApplication::platformName() == "windows") + QTest::qWait(1000); // on Windows, we have to wait for fade-in effects + } + }; + + int expectedEnter = 0; + int expectedLeave = 0; + + Widget widget; + widget.secondaryWindowType = windowType; + const QRect screenGeometry = widget.screen()->availableGeometry(); + const QPoint cursorPos = screenGeometry.topLeft() + QPoint(50, 50); + widget.setGeometry(QRect(cursorPos - QPoint(50, 50), screenGeometry.size() / 4)); + QCursor::setPos(cursorPos); + + if (!QTest::qWaitFor([&]{ return widget.geometry().contains(QCursor::pos()); })) + QSKIP("We can't move the cursor"); + widget.show(); + QVERIFY(QTest::qWaitForWindowActive(&widget)); + + ++expectedEnter; + QTRY_COMPARE_WITH_TIMEOUT(widget.numEnterEvents, expectedEnter, 1000); + QCOMPARE(widget.enterPosition, widget.mapFromGlobal(cursorPos)); + QVERIFY(widget.underMouse()); + + QTest::mouseClick(&widget, Qt::LeftButton, {}, widget.mapFromGlobal(cursorPos)); + ++expectedLeave; + QTRY_COMPARE_WITH_TIMEOUT(widget.numLeaveEvents, expectedLeave, 1000); + QVERIFY(!widget.underMouse()); + QTRY_VERIFY(QApplication::activeModalWidget() || QApplication::activePopupWidget()); + if (QApplication::activeModalWidget()) + QApplication::activeModalWidget()->close(); + else if (QApplication::activePopupWidget()) + QApplication::activePopupWidget()->close(); + ++expectedEnter; + // Use default timeout, the test is flaky on Windows otherwise. + QTRY_VERIFY(widget.numEnterEvents >= expectedEnter); + // When a modal dialog closes we might get more than one enter event on macOS. + // This seems to depend on timing, so we tolerate that flakiness for now. + if (widget.numEnterEvents > expectedEnter && QGuiApplication::platformName() == "cocoa") + QEXPECT_FAIL("dialog", "On macOS, we might get more than one Enter event", Continue); + + QCOMPARE(widget.numEnterEvents, expectedEnter); + QCOMPARE(widget.enterPosition, widget.mapFromGlobal(cursorPos)); + QVERIFY(widget.underMouse()); +} +#endif + +#ifndef QT_NO_CURSOR void tst_QWidget::taskQTBUG_4055_sendSyntheticEnterLeave() { if (m_platform == QStringLiteral("wayland")) @@ -9320,7 +11003,7 @@ void tst_QWidget::taskQTBUG_4055_sendSyntheticEnterLeave() { public: using QWidget::QWidget; - void enterEvent(QEvent *) override { ++numEnterEvents; } + void enterEvent(QEnterEvent *) override { ++numEnterEvents; } void mouseMoveEvent(QMouseEvent *event) override { QCOMPARE(event->button(), Qt::NoButton); @@ -9332,69 +11015,179 @@ void tst_QWidget::taskQTBUG_4055_sendSyntheticEnterLeave() int numEnterEvents = 0, numMouseMoveEvents = 0; }; - QCursor::setPos(m_safeCursorPos); - - SELParent parent; - parent.setWindowTitle(QLatin1String(QTest::currentTestFunction())); - parent.move(200, 200); - parent.resize(200, 200); - SELChild child(&parent); - child.resize(200, 200); - parent.show(); - QVERIFY(QTest::qWaitForWindowActive(&parent)); - - QCursor::setPos(child.mapToGlobal(QPoint(100, 100))); - // Make sure the cursor has entered the child. - QTRY_VERIFY(child.numEnterEvents > 0); - - child.hide(); - child.reset(); - child.show(); - - // Make sure the child gets enter event and no mouse move event. - QTRY_COMPARE(child.numEnterEvents, 1); - QCOMPARE(child.numMouseMoveEvents, 0); - - child.hide(); - child.reset(); - child.setMouseTracking(true); - child.show(); - - // Make sure the child gets enter event. - // Note that we verify event->button() and event->buttons() - // in SELChild::mouseMoveEvent(). - QTRY_COMPARE(child.numEnterEvents, 1); - QCOMPARE(child.numMouseMoveEvents, 0); - - // Sending synthetic enter/leave trough the parent's mousePressEvent handler. - parent.child = &child; - - child.hide(); - child.reset(); - QTest::mouseClick(&parent, Qt::LeftButton); - - // Make sure the child gets enter event. - QTRY_COMPARE(child.numEnterEvents, 1); - QCOMPARE(child.numMouseMoveEvents, 0); - - child.hide(); - child.reset(); - QTest::keyPress(&parent, Qt::Key_Shift); - QTest::mouseClick(&parent, Qt::LeftButton); - - // Make sure the child gets enter event - QTRY_COMPARE(child.numEnterEvents, 1); - QCOMPARE(child.numMouseMoveEvents, 0); - QTest::keyRelease(&child, Qt::Key_Shift); - child.hide(); - child.reset(); - child.setMouseTracking(false); - QTest::mouseClick(&parent, Qt::LeftButton); - - // Make sure the child gets enter event and no mouse move event. - QTRY_COMPARE(child.numEnterEvents, 1); - QCOMPARE(child.numMouseMoveEvents, 0); + QCursor::setPos(m_safeCursorPos); + if (!QTest::qWaitFor([this]{ return QCursor::pos() == m_safeCursorPos; })) + QSKIP("Can't move cursor"); + + SELParent parent; + parent.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + parent.move(200, 200); + parent.resize(200, 200); + SELChild child(&parent); + child.resize(200, 200); + parent.show(); + QVERIFY(QTest::qWaitForWindowActive(&parent)); + + const QPoint childPos = child.mapToGlobal(QPoint(100, 100)); + QCursor::setPos(childPos); + if (!QTest::qWaitFor([childPos]{ return QCursor::pos() == childPos; })) + QSKIP("Can't move cursor"); + + // Make sure the cursor has entered the child. + QTRY_VERIFY(child.numEnterEvents > 0); + + child.hide(); + child.reset(); + child.show(); + + // Make sure the child gets enter event and no mouse move event. + QTRY_COMPARE(child.numEnterEvents, 1); + QCOMPARE(child.numMouseMoveEvents, 0); + + child.hide(); + child.reset(); + child.setMouseTracking(true); + child.show(); + + // Make sure the child gets enter event. + // Note that we verify event->button() and event->buttons() + // in SELChild::mouseMoveEvent(). + QTRY_COMPARE(child.numEnterEvents, 1); + QCOMPARE(child.numMouseMoveEvents, 0); + + // Sending synthetic enter/leave through the parent's mousePressEvent handler. + parent.child = &child; + + child.hide(); + child.reset(); + QTest::mouseClick(&parent, Qt::LeftButton); + + // Make sure the child gets enter event. + QTRY_COMPARE(child.numEnterEvents, 1); + QCOMPARE(child.numMouseMoveEvents, 0); + + child.hide(); + child.reset(); + QTest::keyPress(&parent, Qt::Key_Shift); + QTest::mouseClick(&parent, Qt::LeftButton); + + // Make sure the child gets enter event + QTRY_COMPARE(child.numEnterEvents, 1); + QCOMPARE(child.numMouseMoveEvents, 0); + QTest::keyRelease(&child, Qt::Key_Shift); + child.hide(); + child.reset(); + child.setMouseTracking(false); + QTest::mouseClick(&parent, Qt::LeftButton); + + // Make sure the child gets enter event and no mouse move event. + QTRY_COMPARE(child.numEnterEvents, 1); + QCOMPARE(child.numMouseMoveEvents, 0); } + +void tst_QWidget::hoverPosition() +{ + if (m_platform == QStringLiteral("wayland")) + QSKIP("Wayland: Clients can't set cursor position on wayland."); + + class HoverWidget : public QWidget + { + public: + HoverWidget(QWidget *parent = nullptr) : QWidget(parent) { + setMouseTracking(true); + setAttribute(Qt::WA_Hover); + } + bool event(QEvent *ev) override { + switch (ev->type()) { + case QEvent::HoverMove: + // The docs say that WA_Hover will cause a paint event on enter and leave, but not on move. + update(); + Q_FALLTHROUGH(); + case QEvent::HoverEnter: + case QEvent::HoverLeave: { + qCDebug(lcTests) << ev; + lastHoverType = ev->type(); + ++hoverEventCount; + QHoverEvent *hov = static_cast<QHoverEvent *>(ev); + mousePos = hov->position().toPoint(); + mouseScenePos = hov->scenePosition().toPoint(); + if (ev->type() == QEvent::HoverEnter) + mouseEnterScenePos = hov->scenePosition().toPoint(); + break; + } + default: + break; + } + return QWidget::event(ev); + } + void paintEvent(QPaintEvent *) override { + ++paintEventCount; + QPainter painter(this); + if (mousePos.x() > 0) + painter.setPen(Qt::red); + painter.drawRect(0, 0, width(), height()); + painter.setPen(Qt::darkGreen); + painter.drawLine(mousePos - QPoint(crossHalfWidth, 0), mousePos + QPoint(crossHalfWidth, 0)); + painter.drawLine(mousePos - QPoint(0, crossHalfWidth), mousePos + QPoint(0, crossHalfWidth)); + } + + QEvent::Type lastHoverType = QEvent::None; + int hoverEventCount = 0; + int paintEventCount = 0; + QPoint mousePos; + QPoint mouseScenePos; + QPoint mouseEnterScenePos; + + private: + const int crossHalfWidth = 5; + }; + + QCursor::setPos(m_safeCursorPos); + if (!QTest::qWaitFor([this]{ return QCursor::pos() == m_safeCursorPos; })) + QSKIP("Can't move cursor"); + + QWidget root; + root.resize(300, 300); + HoverWidget h(&root); + h.setGeometry(100, 100, 100, 100); + root.show(); + QVERIFY(QTest::qWaitForWindowExposed(&root)); + + const QPoint middle(50, 50); + // wait until we got the correct global pos + QPoint expectedGlobalPos = root.geometry().topLeft() + QPoint(100, 100) + middle; + if (!QTest::qWaitFor([&]{ return expectedGlobalPos == h.mapToGlobal(middle); })) + QSKIP("Can't move cursor"); + QPoint curpos = h.mapToGlobal(middle); + QCursor::setPos(curpos); + if (!QTest::qWaitFor([curpos]{ return QCursor::pos() == curpos; })) + QSKIP("Can't move cursor"); + QTRY_COMPARE_GE(h.hoverEventCount, 1); // HoverEnter and then probably HoverMove, so usually 2 + QTRY_COMPARE_GE(h.paintEventCount, 2); + const int enterHoverEventCount = h.hoverEventCount; + qCDebug(lcTests) << "hover enter events:" << enterHoverEventCount << "last was" << h.lastHoverType + << "; paint events:" << h.paintEventCount; + QCOMPARE(h.mousePos, middle); + QCOMPARE(h.mouseEnterScenePos, h.mapToParent(middle)); + QCOMPARE(h.mouseScenePos, h.mapToParent(middle)); + QCOMPARE(h.lastHoverType, enterHoverEventCount == 1 ? QEvent::HoverEnter : QEvent::HoverMove); + + curpos += {10, 10}; + QCursor::setPos(curpos); + if (!QTest::qWaitFor([curpos]{ return QCursor::pos() == curpos; })) + QSKIP("Can't move cursor"); + QTRY_COMPARE(h.hoverEventCount, enterHoverEventCount + 1); + QCOMPARE(h.lastHoverType, QEvent::HoverMove); + QTRY_COMPARE_GE(h.paintEventCount, 3); + + curpos += {50, 50}; // in the outer widget, but leaving the inner widget + QCursor::setPos(curpos); + if (!QTest::qWaitFor([curpos]{ return QCursor::pos() == curpos; })) + QSKIP("Can't move cursor"); + QTRY_COMPARE(h.lastHoverType, QEvent::HoverLeave); + QCOMPARE_GE(h.hoverEventCount, enterHoverEventCount + 2); + QTRY_COMPARE_GE(h.paintEventCount, 4); +} #endif void tst_QWidget::windowFlags() @@ -9466,6 +11259,9 @@ void tst_QWidget::updateOnDestroyedSignal() void tst_QWidget::toplevelLineEditFocus() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QLineEdit w; w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); w.setMinimumWidth(m_testWidgetSize.width()); @@ -9497,6 +11293,7 @@ void tst_QWidget::focusWidget_task254563() void tst_QWidget::destroyBackingStore() { UpdateWidget w; + w.setPalette(simplePalette()); w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); centerOnScreen(&w); w.reset(); @@ -9507,7 +11304,7 @@ void tst_QWidget::destroyBackingStore() QTRY_VERIFY(w.numPaintEvents > 0); w.reset(); w.update(); - qt_widget_private(&w)->topData()->backingStoreTracker.create(&w); + qt_widget_private(&w)->topData()->repaintManager.reset(new QWidgetRepaintManager(&w)); w.update(); QApplication::processEvents(); @@ -9522,21 +11319,21 @@ void tst_QWidget::destroyBackingStore() #endif // QT_BUILD_INTERNAL // Helper function -QWidgetBackingStore* backingStore(QWidget &widget) +QWidgetRepaintManager* repaintManager(QWidget &widget) { - QWidgetBackingStore *backingStore = nullptr; + QWidgetRepaintManager *repaintManager = nullptr; #ifdef QT_BUILD_INTERNAL if (QTLWExtra *topExtra = qt_widget_private(&widget)->maybeTopData()) - backingStore = topExtra->backingStoreTracker.data(); + repaintManager = topExtra->repaintManager.get(); #endif - return backingStore; + return repaintManager; } // Tables of 5000 elements do not make sense on Windows Mobile. void tst_QWidget::rectOutsideCoordinatesLimit_task144779() { #ifndef QT_NO_CURSOR - QApplication::setOverrideCursor(Qt::BlankCursor); //keep the cursor out of screen grabs + QGuiApplication::setOverrideCursor(Qt::BlankCursor); //keep the cursor out of screen grabs #endif QWidget main(nullptr, Qt::FramelessWindowHint); //don't get confused by the size of the window frame main.setWindowTitle(QLatin1String(QTest::currentTestFunction())); @@ -9544,8 +11341,7 @@ void tst_QWidget::rectOutsideCoordinatesLimit_task144779() palette.setColor(QPalette::Window, Qt::red); main.setPalette(palette); - QDesktopWidget desktop; - QRect desktopDimensions = desktop.availableGeometry(&main); + QRect desktopDimensions = main.screen()->availableGeometry(); QSize mainSize(400, 400); mainSize = mainSize.boundedTo(desktopDimensions.size()); main.resize(mainSize); @@ -9569,12 +11365,10 @@ void tst_QWidget::rectOutsideCoordinatesLimit_task144779() correct.fill(Qt::green); const QPixmap mainPixmap = grabFromWidget(&main, QRect(QPoint(0, 0), QSize(-1, -1))); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT: This fails. QTBUG-68297.", Abort); QTRY_COMPARE(mainPixmap.toImage().convertToFormat(QImage::Format_RGB32), correct.toImage().convertToFormat(QImage::Format_RGB32)); #ifndef QT_NO_CURSOR - QApplication::restoreOverrideCursor(); + QGuiApplication::restoreOverrideCursor(); #endif } @@ -9624,10 +11418,161 @@ void tst_QWidget::setGraphicsEffect() QVERIFY(!blurEffect); } + +class TestGraphicsEffect : public QGraphicsEffect +{ +public: + TestGraphicsEffect(QObject *parent = nullptr) + : QGraphicsEffect(parent) + { + m_pattern = QPixmap(10, 10); + m_pattern.fill(Qt::lightGray); + QPainter p(&m_pattern); + p.fillRect(QRectF(0, 0, 5, 5), QBrush(Qt::darkGray)); + p.fillRect(QRectF(5, 5, 5, 5), QBrush(Qt::darkGray)); + } + void setExtent(int extent) + { + m_extent = extent; + } + QRectF boundingRectFor(const QRectF &sr) const override + { + return QRectF(sr.x() - m_extent, sr.y() - m_extent, + sr.width() + 2 * m_extent, sr.height() + 2 * m_extent); + } +protected: + void draw(QPainter *painter) override + { + QBrush brush; + brush.setTexture(m_pattern); + brush.setStyle(Qt::TexturePattern); + QPaintDevice *p = painter->device(); + painter->fillRect(QRect(-m_extent, -m_extent, + p->width() + m_extent, p->height() + m_extent), brush); + } + QPixmap m_pattern; + int m_extent = 0; +}; + +static QImage fillExpected1() +{ + QImage expected(QSize(40, 40), QImage::Format_RGB32); + QPainter p(&expected); + p.fillRect(QRect{{0, 0}, expected.size()}, QBrush(Qt::gray)); + p.fillRect(QRect(10, 10, 10, 10), QBrush(Qt::red)); + p.fillRect(QRect(20, 20, 10, 10), QBrush(Qt::blue)); + return expected; +} +static QImage fillExpected2() +{ + QImage expected = fillExpected1(); + QPainter p(&expected); + p.fillRect(QRect(10, 10, 5, 5), QBrush(Qt::darkGray)); + p.fillRect(QRect(15, 15, 5, 5), QBrush(Qt::darkGray)); + p.fillRect(QRect(15, 10, 5, 5), QBrush(Qt::lightGray)); + p.fillRect(QRect(10, 15, 5, 5), QBrush(Qt::lightGray)); + return expected; +} +static QImage fillExpected3() +{ + QImage expected(QSize(40, 40), QImage::Format_RGB32); + QPixmap pattern; + pattern = QPixmap(10, 10); + pattern.fill(Qt::lightGray); + QPainter p(&pattern); + p.fillRect(QRectF(0, 0, 5, 5), QBrush(Qt::darkGray)); + p.fillRect(QRectF(5, 5, 5, 5), QBrush(Qt::darkGray)); + QBrush brush; + brush.setTexture(pattern); + brush.setStyle(Qt::TexturePattern); + QPainter p2(&expected); + p2.fillRect(QRect{{0, 0}, expected.size()}, brush); + return expected; +} +static QImage fillExpected4() +{ + QImage expected = fillExpected1(); + QPixmap pattern; + pattern = QPixmap(10, 10); + pattern.fill(Qt::lightGray); + QPainter p(&pattern); + p.fillRect(QRectF(0, 0, 5, 5), QBrush(Qt::darkGray)); + p.fillRect(QRectF(5, 5, 5, 5), QBrush(Qt::darkGray)); + QBrush brush; + brush.setTexture(pattern); + brush.setStyle(Qt::TexturePattern); + QPainter p2(&expected); + p2.fillRect(QRect{{15, 15}, QSize{20, 20}}, brush); + return expected; +} + +void tst_QWidget::render_graphicsEffect_data() +{ + QTest::addColumn<QImage>("expected"); + QTest::addColumn<bool>("topLevelEffect"); + QTest::addColumn<bool>("child1Effect"); + QTest::addColumn<bool>("child2Effect"); + QTest::addColumn<int>("extent"); + + QTest::addRow("no_effect") << fillExpected1() << false << false << false << 0; + QTest::addRow("first_child_effect") << fillExpected2() << false << true << false << 0; + QTest::addRow("top_level_effect") << fillExpected3() << true << false << false << 0; + QTest::addRow("effect_with_extent") << fillExpected4() << false << false << true << 5; +} + +void tst_QWidget::render_graphicsEffect() +{ + QFETCH(QImage, expected); + QFETCH(bool, topLevelEffect); + QFETCH(bool, child1Effect); + QFETCH(bool, child2Effect); + QFETCH(int, extent); + + QScopedPointer<QWidget> topLevel(new QWidget); + topLevel->setPalette(Qt::gray); + topLevel->resize(40, 40); + topLevel->setWindowTitle(QLatin1String(QTest::currentTestFunction()) + QLatin1String("::") + + QLatin1String(QTest::currentDataTag())); + + // Render widget with 2 child widgets + QImage image(topLevel->size(), QImage::Format_RGB32); + image.fill(QColor(Qt::gray).rgb()); + + QPainter painter(&image); + + QWidget *childWidget1(new QWidget(topLevel.data())); + childWidget1->setAutoFillBackground(true); + childWidget1->setPalette(Qt::red); + childWidget1->resize(10, 10); + childWidget1->move(10, 10); + QWidget *childWidget2(new QWidget(topLevel.data())); + childWidget2->setAutoFillBackground(true); + childWidget2->setPalette(Qt::blue); + childWidget2->resize(10, 10); + childWidget2->move(20, 20); + + TestGraphicsEffect *graphicsEffect(new TestGraphicsEffect(topLevel.data())); + if (topLevelEffect) + topLevel->setGraphicsEffect(graphicsEffect); + if (child1Effect) + childWidget1->setGraphicsEffect(graphicsEffect); + if (child2Effect) + childWidget2->setGraphicsEffect(graphicsEffect); + graphicsEffect->setExtent(extent); + + // Render without effect + topLevel->render(&painter); +#ifdef RENDER_DEBUG + image.save("render_GraphicsEffect" + QTest::currentDataTag() + ".png"); + expected.save("render_GraphicsEffect_expected" + QTest::currentDataTag() + ".png"); +#endif + QCOMPARE(image, expected); +} + void tst_QWidget::activateWindow() { - if (m_platform == QStringLiteral("wayland")) - QSKIP("Wayland: This fails. Figure out why."); + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("Window activation is not supported."); // Test case for QTBUG-26711 @@ -9664,13 +11609,14 @@ void tst_QWidget::activateWindow() QCoreApplication::processEvents(); QTRY_VERIFY(mainwindow->isActiveWindow()); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT: This fails. QTBUG-68297.", Abort); QTRY_VERIFY(!mainwindow2->isActiveWindow()); } void tst_QWidget::openModal_taskQTBUG_5804() { +#ifdef Q_OS_ANDROID + QSKIP("This test hangs on Android"); +#endif class Widget : public QWidget { public: @@ -9693,32 +11639,167 @@ void tst_QWidget::openModal_taskQTBUG_5804() QVERIFY(QTest::qWaitForWindowExposed(win.data())); } -void tst_QWidget::focusProxyAndInputMethods() +/*! + Test that the focus proxy receives focus, and that changing the + focus proxy of a widget that has focus passes focus on correctly. + + The test uses a single window, so we can rely on the window's focus + widget and the QApplication focus widget to be the same. +*/ +void tst_QWidget::focusProxy() { - if (m_platform == QStringLiteral("wayland")) - QSKIP("Wayland: This fails. Figure out why."); - QScopedPointer<QWidget> toplevel(new QWidget(nullptr, Qt::X11BypassWindowManagerHint)); - toplevel->setWindowTitle(QLatin1String(QTest::currentTestFunction())); - toplevel->resize(200, 200); - toplevel->setAttribute(Qt::WA_InputMethodEnabled, true); + QWidget window; + window.setFocusPolicy(Qt::StrongFocus); + class Container : public QWidget + { + public: + Container() + { + edit = new QLineEdit; + edit->installEventFilter(this); + setFocusProxy(edit); + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(edit); + setLayout(layout); + } + + QLineEdit *edit; + int focusInCount = 0; + int focusOutCount = 0; + + protected: + bool eventFilter(QObject *receiver, QEvent *event) override + { + if (receiver == edit) { + switch (event->type()) { + case QEvent::FocusIn: + ++focusInCount; + break; + case QEvent::FocusOut: + ++focusOutCount; + break; + default: + break; + } + } + + return QWidget::eventFilter(receiver, event); + } + }; - QWidget *child = new QWidget(toplevel.data()); - child->setFocusProxy(toplevel.data()); - child->setAttribute(Qt::WA_InputMethodEnabled, true); + auto container1 = new Container; + container1->edit->setObjectName("edit1"); + auto container2 = new Container; + container2->edit->setObjectName("edit2"); - toplevel->setFocusPolicy(Qt::WheelFocus); - child->setFocusPolicy(Qt::WheelFocus); + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(container1); + layout->addWidget(container2); + window.setLayout(layout); - QVERIFY(!child->hasFocus()); - QVERIFY(!toplevel->hasFocus()); + window.setFocus(); + window.show(); + if (!QTest::qWaitForWindowExposed(&window)) + QSKIP("Window exposed failed"); + if (QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) { + window.activateWindow(); + if (!QTest::qWaitForWindowActive(&window)) + QSKIP("Window activation failed"); + } else { + if (!QTest::qWaitFor([&]() { return window.windowHandle()->isActive(); }, 5000)) + QSKIP("Window activation failed"); + } + + // given a widget without focus proxy + QVERIFY(window.hasFocus()); + QCOMPARE(&window, QApplication::focusWidget()); + QVERIFY(!container1->hasFocus()); + QVERIFY(!container2->hasFocus()); + QCOMPARE(container1->focusInCount, 0); + QCOMPARE(container1->focusOutCount, 0); + + // setting a (nested) focus proxy moves focus + window.setFocusProxy(container1); + QCOMPARE(window.focusWidget(), container1->edit); + QCOMPARE(window.focusWidget(), QApplication::focusWidget()); + QVERIFY(window.hasFocus()); // and redirects hasFocus correctly + QVERIFY(container1->edit->hasFocus()); + QCOMPARE(container1->focusInCount, 1); + + // changing the focus proxy should not move focus + window.setFocusProxy(container2); + QCOMPARE(window.focusWidget(), container1->edit); + QCOMPARE(window.focusWidget(), QApplication::focusWidget()); + QVERIFY(!window.hasFocus()); + QCOMPARE(container1->focusOutCount, 0); + + // but setting focus again does + window.setFocus(); + QCOMPARE(window.focusWidget(), container2->edit); + QCOMPARE(window.focusWidget(), QApplication::focusWidget()); + QVERIFY(window.hasFocus()); + QVERIFY(!container1->edit->hasFocus()); + QVERIFY(container2->edit->hasFocus()); + QCOMPARE(container1->focusInCount, 1); + QCOMPARE(container1->focusOutCount, 1); + QCOMPARE(container2->focusInCount, 1); + QCOMPARE(container2->focusOutCount, 0); + + // clearing the focus proxy does not move focus + window.setFocusProxy(nullptr); + QCOMPARE(window.focusWidget(), container2->edit); + QCOMPARE(window.focusWidget(), QApplication::focusWidget()); + QVERIFY(!window.hasFocus()); + QCOMPARE(container1->focusInCount, 1); + QCOMPARE(container1->focusOutCount, 1); + QCOMPARE(container2->focusInCount, 1); + QCOMPARE(container2->focusOutCount, 0); + + // but clearing focus does + window.focusWidget()->clearFocus(); + QCOMPARE(QApplication::focusWidget(), nullptr); + QVERIFY(!window.hasFocus()); + QVERIFY(!container2->hasFocus()); + QVERIFY(!container2->edit->hasFocus()); + QCOMPARE(container2->focusOutCount, 1); +} + +void tst_QWidget::imEnabledNotImplemented() +{ + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("QWindow::requestActivate() is not supported."); + + // Check that a plain widget doesn't report that it supports IM. Only + // widgets that implements either Qt::ImEnabled, or the Qt4 backup + // solution, Qt::ImSurroundingText, should do so. + QWidget topLevel; + QWidget plain(&topLevel); + QLineEdit edit(&topLevel); + topLevel.show(); - toplevel->show(); - QVERIFY(QTest::qWaitForWindowExposed(toplevel.data())); - QApplication::setActiveWindow(toplevel.data()); - QVERIFY(QTest::qWaitForWindowActive(toplevel.data())); - QVERIFY(toplevel->hasFocus()); - QVERIFY(child->hasFocus()); - QCOMPARE(qApp->focusObject(), toplevel.data()); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + QVERIFY(QTest::qWaitForWindowActive(&topLevel)); + + // A plain widget should return false for ImEnabled + plain.setFocus(Qt::OtherFocusReason); + QCOMPARE(QApplication::focusWidget(), &plain); + QVariant imEnabled = QApplication::inputMethod()->queryFocusObject(Qt::ImEnabled, QVariant()); + QVERIFY(imEnabled.isValid()); + QVERIFY(!imEnabled.toBool()); + + // But a lineedit should return true + edit.setFocus(Qt::OtherFocusReason); + QCOMPARE(QApplication::focusWidget(), &edit); + imEnabled = QApplication::inputMethod()->queryFocusObject(Qt::ImEnabled, QVariant()); + QVERIFY(imEnabled.isValid()); + QVERIFY(imEnabled.toBool()); + + // ImEnabled should be false when a lineedit is read-only since + // ImEnabled indicates the widget accepts input method _input_. + edit.setReadOnly(true); + imEnabled = QApplication::inputMethod()->queryFocusObject(Qt::ImEnabled, QVariant()); + QVERIFY(imEnabled.isValid()); + QVERIFY(!imEnabled.toBool()); } #ifdef QT_BUILD_INTERNAL @@ -9727,14 +11808,14 @@ class scrollWidgetWBS : public QWidget public: void deleteBackingStore() { - static_cast<QWidgetPrivate*>(d_ptr.data())->topData()->backingStoreTracker.destroy(); + static_cast<QWidgetPrivate*>(d_ptr.data())->topData()->repaintManager.reset(nullptr); } void enableBackingStore() { - if (!static_cast<QWidgetPrivate*>(d_ptr.data())->maybeBackingStore()) { - static_cast<QWidgetPrivate*>(d_ptr.data())->topData()->backingStoreTracker.create(this); - static_cast<QWidgetPrivate*>(d_ptr.data())->invalidateBuffer(this->rect()); - repaint(); + if (!static_cast<QWidgetPrivate*>(d_ptr.data())->maybeRepaintManager()) { + static_cast<QWidgetPrivate*>(d_ptr.data())->topData()->repaintManager.reset(new QWidgetRepaintManager(this)); + static_cast<QWidgetPrivate*>(d_ptr.data())->invalidateBackingStore(this->rect()); + update(); } } }; @@ -9757,7 +11838,7 @@ void tst_QWidget::scrollWithoutBackingStore() scrollable.scroll(-25,-25); QCOMPARE(child.pos(),QPoint(25,25)); scrollable.enableBackingStore(); - QCOMPARE(child.pos(),QPoint(25,25)); + QTRY_COMPARE(child.pos(),QPoint(25,25)); } #endif @@ -9777,7 +11858,7 @@ void tst_QWidget::taskQTBUG_7532_tabOrderWithFocusProxy() void tst_QWidget::movedAndResizedAttributes() { // Use Qt::Tool as fully decorated windows have a minimum width of 160 on - QWidget w(nullptr, Qt::Tool); + QWidget w; w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); w.show(); @@ -9815,11 +11896,11 @@ void tst_QWidget::movedAndResizedAttributes() QVERIFY(!w.testAttribute(Qt::WA_Resized)); w.showNormal(); - w.move(10,10); + w.move(m_availableTopLeft); QVERIFY(w.testAttribute(Qt::WA_Moved)); QVERIFY(!w.testAttribute(Qt::WA_Resized)); - w.resize(100, 100); + w.resize(m_testWidgetSize); QVERIFY(w.testAttribute(Qt::WA_Moved)); QVERIFY(w.testAttribute(Qt::WA_Resized)); } @@ -9880,7 +11961,7 @@ void tst_QWidget::childAt() QCOMPARE(parent.childAt(120, 120), grandChild); } -#ifdef Q_OS_OSX +#ifdef Q_OS_MACOS void tst_QWidget::taskQTBUG_11373() { @@ -9919,6 +12000,9 @@ void tst_QWidget::taskQTBUG_17333_ResizeInfiniteRecursion() void tst_QWidget::nativeChildFocus() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QWidget w; w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); w.setMinimumWidth(m_testWidgetSize.width()); @@ -10042,7 +12126,7 @@ protected: case QEvent::MouseMove: case QEvent::MouseButtonRelease: { QMouseEvent *me = static_cast<QMouseEvent *>(e); - m_log->push_back(mouseEventLogEntry(objectName(), me->type(), me->pos(), me->buttons())); + m_log->push_back(mouseEventLogEntry(objectName(), me->type(), me->position().toPoint(), me->buttons())); me->accept(); return true; } @@ -10057,13 +12141,16 @@ private: void tst_QWidget::grabMouse() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QStringList log; GrabLoggerWidget w(&log); w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); w.setObjectName(QLatin1String("tst_qwidget_grabMouse")); w.setWindowTitle(w.objectName()); QLayout *layout = new QVBoxLayout(&w); - layout->setMargin(50); + layout->setContentsMargins(50, 50, 50, 50); GrabLoggerWidget *grabber = new GrabLoggerWidget(&log, &w); const QString grabberObjectName = QLatin1String("tst_qwidget_grabMouse_grabber"); grabber->setObjectName(grabberObjectName); @@ -10071,7 +12158,6 @@ void tst_QWidget::grabMouse() layout->addWidget(grabber); centerOnScreen(&w); w.show(); - QApplication::setActiveWindow(&w); QVERIFY(QTest::qWaitForWindowActive(&w)); QStringList expectedLog; @@ -10092,6 +12178,9 @@ void tst_QWidget::grabMouse() void tst_QWidget::grabKeyboard() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QWidget w; w.setWindowTitle(QLatin1String(QTest::currentTestFunction())); w.setObjectName(QLatin1String("tst_qwidget_grabKeyboard")); @@ -10105,7 +12194,6 @@ void tst_QWidget::grabKeyboard() layout->addWidget(nonGrabber); centerOnScreen(&w); w.show(); - QApplication::setActiveWindow(&w); QVERIFY(QTest::qWaitForWindowActive(&w)); nonGrabber->setFocus(); grabber->grabKeyboard(); @@ -10136,12 +12224,20 @@ public: protected: bool event(QEvent *e) override { + qCDebug(lcTests) << e; switch (e->type()) { case QEvent::TouchBegin: + case QEvent::TouchCancel: case QEvent::TouchUpdate: - case QEvent::TouchEnd: + case QEvent::TouchEnd: { + auto te = static_cast<QTouchEvent *>(e); + touchDevice = const_cast<QPointingDevice *>(te->pointingDevice()); + touchPointStates = te->touchPointStates(); + touchPoints = te->points(); if (e->type() == QEvent::TouchBegin) ++m_touchBeginCount; + else if (e->type() == QEvent::TouchCancel) + ++m_touchCancelCount; else if (e->type() == QEvent::TouchUpdate) ++m_touchUpdateCount; else if (e->type() == QEvent::TouchEnd) @@ -10151,6 +12247,7 @@ protected: e->accept(); else e->ignore(); + } return true; case QEvent::Gesture: ++m_gestureEventCount; @@ -10160,7 +12257,7 @@ protected: case QEvent::MouseMove: case QEvent::MouseButtonRelease: ++m_mouseEventCount; - m_lastMouseEventPos = static_cast<QMouseEvent *>(e)->localPos(); + m_lastMouseEventPos = static_cast<QMouseEvent *>(e)->position(); if (m_acceptMouse) e->accept(); else @@ -10174,9 +12271,13 @@ protected: public: int m_touchBeginCount = 0; + int m_touchCancelCount = 0; int m_touchUpdateCount = 0; int m_touchEndCount = 0; int m_touchEventCount = 0; + QPointingDevice *touchDevice = nullptr; + QTouchEvent::TouchPoint::States touchPointStates; + QList<QTouchEvent::TouchPoint> touchPoints; int m_gestureEventCount = 0; bool m_acceptTouch = false; int m_mouseEventCount = 0; @@ -10186,6 +12287,9 @@ public: void tst_QWidget::touchEventSynthesizedMouseEvent() { + if (m_platform.startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("This test failed on Wayland. See also QTBUG-107157."); + { // Simple case, we ignore the touch events, we get mouse events instead TouchMouseWidget widget; @@ -10280,6 +12384,70 @@ void tst_QWidget::touchEventSynthesizedMouseEvent() } } +void tst_QWidget::touchCancel() +{ + TouchMouseWidget widget; + widget.setWindowTitle(QLatin1String(QTest::currentTestFunction())); + widget.setAcceptTouch(true); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(widget.windowHandle())); + + { // cancel right after press + QTest::touchEvent(&widget, m_touchScreen).press(1, QPoint(20, 21), &widget); + QCOMPARE(widget.m_touchBeginCount, 1); + QCOMPARE(widget.touchDevice, m_touchScreen); + QCOMPARE(widget.touchPoints.size(), 1); + QCOMPARE(widget.touchPointStates, Qt::TouchPointPressed); + QCOMPARE(widget.touchPoints.first().position(), QPointF(20, 21)); + + QWindowSystemInterface::handleTouchCancelEvent(widget.windowHandle(), m_touchScreen); + QTRY_COMPARE(widget.m_touchCancelCount, 1); + QCOMPARE(widget.touchDevice, m_touchScreen); + QCOMPARE(widget.touchPoints.size(), 0); + + // should not propagate, since after cancel there should be only new press + QTest::touchEvent(&widget, m_touchScreen).move(1, QPoint(25, 26), &widget); + QCOMPARE(widget.m_touchUpdateCount, 0); + } + + { // cancel after update + QTest::touchEvent(&widget, m_touchScreen).press(1, QPoint(30, 31), &widget); + QCOMPARE(widget.m_touchBeginCount, 2); + QCOMPARE(widget.touchPoints.size(), 1); + QCOMPARE(widget.touchPointStates, Qt::TouchPointPressed); + QCOMPARE(widget.touchPoints.first().position(), QPointF(30, 31)); + + QTest::touchEvent(&widget, m_touchScreen).move(1, QPoint(20, 21)); + QCOMPARE(widget.m_touchUpdateCount, 1); + QCOMPARE(widget.touchPoints.size(), 1); + QCOMPARE(widget.touchPointStates, Qt::TouchPointMoved); + QCOMPARE(widget.touchPoints.first().position(), QPointF(20, 21)); + + QWindowSystemInterface::handleTouchCancelEvent(widget.windowHandle(), m_touchScreen); + QTRY_COMPARE(widget.m_touchCancelCount, 2); + QCOMPARE(widget.touchDevice, m_touchScreen); + QCOMPARE(widget.touchPoints.size(), 0); + + // should not propagate, since after cancel there should be only new press + QTest::touchEvent(&widget, m_touchScreen).move(1, QPoint(25, 26), &widget); + QCOMPARE(widget.m_touchUpdateCount, 1); + } + + { // proper press/release after multiple cancel events should proceed as usual + QTest::touchEvent(&widget, m_touchScreen).press(2, QPoint(15, 16), &widget).press(3, QPoint(25, 26), &widget); + QCOMPARE(widget.m_touchBeginCount, 3); + QCOMPARE(widget.touchDevice, m_touchScreen); + QCOMPARE(widget.touchPoints.size(), 2); + QCOMPARE(widget.touchPointStates, Qt::TouchPointPressed); + + QTest::touchEvent(&widget, m_touchScreen).release(3, QPoint(30, 30), &widget).release(2, QPoint(10, 10), &widget); + QCOMPARE(widget.m_touchEndCount, 1); + QCOMPARE(widget.touchDevice, m_touchScreen); + QCOMPARE(widget.touchPoints.size(), 2); + QCOMPARE(widget.touchPointStates, Qt::TouchPointReleased); + } +} + void tst_QWidget::touchUpdateOnNewTouch() { TouchMouseWidget widget; @@ -10536,9 +12704,10 @@ void tst_QWidget::underMouse() QCOMPARE(QApplication::activePopupWidget(), &popupWidget); // Send an artificial leave event for window, as it won't get generated automatically - // due to cursor not actually being over the window. - QWindowSystemInterface::handleLeaveEvent(window); - QApplication::processEvents(); + // due to cursor not actually being over the window. The Cocoa and offscreen plugins + // do this for us. + if (QGuiApplication::platformName() != "cocoa" && QGuiApplication::platformName() != "offscreen") + QWindowSystemInterface::handleLeaveEvent<QWindowSystemInterface::SynchronousDelivery>(window); // If there is an active popup, undermouse should not be reported (QTBUG-27478), // but opening a popup causes leave for widgets under mouse. @@ -10640,10 +12809,6 @@ void tst_QWidget::underMouse() // Mouse leaves popup and enters topLevelWidget, should cause leave for popup // but no enter to topLevelWidget. -#ifdef Q_OS_DARWIN - // Artificial leave event needed for Cocoa. - QWindowSystemInterface::handleLeaveEvent(popupWindow); -#endif QTest::mouseMove(popupWindow, popupWindow->mapFromGlobal(window->mapToGlobal(inWindowPoint))); QApplication::processEvents(); QVERIFY(!topLevelWidget.underMouse()); @@ -10727,9 +12892,6 @@ public: // when mousing over it. void tst_QWidget::taskQTBUG_27643_enterEvents() { -#ifdef Q_OS_OSX - QSKIP("QTBUG-52974: this test can crash!"); -#endif // Move the mouse cursor to a safe location so it won't interfere QCursor::setPos(m_safeCursorPos); @@ -10836,14 +12998,13 @@ void tst_QWidget::resizeStaticContentsChildWidget_QTBUG35282() widget.resize(200,200); UpdateWidget childWidget(&widget); + childWidget.setPalette(simplePalette()); childWidget.setAttribute(Qt::WA_StaticContents); childWidget.setAttribute(Qt::WA_OpaquePaintEvent); childWidget.setGeometry(250, 250, 500, 500); widget.showNormal(); QVERIFY(QTest::qWaitForWindowExposed(&widget)); - if (m_platform == QStringLiteral("winrt")) - QEXPECT_FAIL("", "WinRT: This fails. QTBUG-68297.", Abort); QCOMPARE(childWidget.numPaintEvents, 0); childWidget.reset(); @@ -10936,60 +13097,54 @@ void tst_QWidget::testForOutsideWSRangeFlag() } } -class TabletWidget : public QWidget +void tst_QWidget::tabletTracking() { -public: - TabletWidget(QWidget *parent) : QWidget(parent) { } - - int tabletEventCount = 0; - int pressEventCount = 0; - int moveEventCount = 0; - int releaseEventCount = 0; - int trackingChangeEventCount = 0; - qint64 uid = -1; + class TabletWidget : public QWidget + { + public: + using QWidget::QWidget; -protected: - void tabletEvent(QTabletEvent *event) override { - ++tabletEventCount; - uid = event->uniqueId(); - switch (event->type()) { - case QEvent::TabletMove: - ++moveEventCount; - break; - case QEvent::TabletPress: - ++pressEventCount; - break; - case QEvent::TabletRelease: - ++releaseEventCount; - break; - default: - break; + int tabletEventCount = 0; + int pressEventCount = 0; + int moveEventCount = 0; + int releaseEventCount = 0; + int trackingChangeEventCount = 0; + qint64 uid = -1; + + protected: + void tabletEvent(QTabletEvent *event) override { + ++tabletEventCount; + uid = event->pointingDevice()->uniqueId().numericId(); + switch (event->type()) { + case QEvent::TabletMove: + ++moveEventCount; + break; + case QEvent::TabletPress: + ++pressEventCount; + break; + case QEvent::TabletRelease: + ++releaseEventCount; + break; + default: + break; + } } - } - - bool event(QEvent *ev) override { - if (ev->type() == QEvent::TabletTrackingChange) - ++trackingChangeEventCount; - return QWidget::event(ev); - } -}; -void tst_QWidget::tabletTracking() -{ - QWidget parent; - parent.setWindowTitle(QLatin1String(QTest::currentTestFunction())); - parent.resize(200,200); - // QWidgetWindow::handleTabletEvent doesn't deliver tablet events to the window's widget, only to a child. - // So it doesn't do any good to show a TabletWidget directly: it needs a parent. - TabletWidget widget(&parent); + bool event(QEvent *ev) override { + if (ev->type() == QEvent::TabletTrackingChange) + ++trackingChangeEventCount; + return QWidget::event(ev); + } + } widget; + widget.setWindowTitle(QLatin1String(QTest::currentTestFunction())); widget.resize(200,200); - parent.showNormal(); - QVERIFY(QTest::qWaitForWindowExposed(&parent)); + widget.showNormal(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); widget.setAttribute(Qt::WA_TabletTracking); QTRY_COMPARE(widget.trackingChangeEventCount, 1); QVERIFY(widget.hasTabletTracking()); - QWindow *window = parent.windowHandle(); + QWindow *window = widget.windowHandle(); QPointF local(10, 10); QPointF global = window->mapToGlobal(local.toPoint()); QPointF deviceLocal = QHighDpi::toNativeLocalPosition(local, window); @@ -10997,7 +13152,7 @@ void tst_QWidget::tabletTracking() qint64 uid = 1234UL; QWindowSystemInterface::handleTabletEvent(window, ulong(QDateTime::currentMSecsSinceEpoch()), deviceLocal, deviceGlobal, - QTabletEvent::Stylus, QTabletEvent::Pen, Qt::NoButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); + int(QInputDevice::DeviceType::Stylus), int(QPointingDevice::PointerType::Pen), Qt::NoButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); QCoreApplication::processEvents(); QTRY_COMPARE(widget.moveEventCount, 1); QCOMPARE(widget.uid, uid); @@ -11006,7 +13161,7 @@ void tst_QWidget::tabletTracking() deviceLocal += QPoint(10, 10); deviceGlobal += QPoint(10, 10); QWindowSystemInterface::handleTabletEvent(window, ulong(QDateTime::currentMSecsSinceEpoch()), deviceLocal, deviceGlobal, - QTabletEvent::Stylus, QTabletEvent::Pen, Qt::NoButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); + int(QInputDevice::DeviceType::Stylus), int(QPointingDevice::PointerType::Pen), Qt::NoButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); QCoreApplication::processEvents(); QTRY_COMPARE(widget.moveEventCount, 2); @@ -11015,7 +13170,7 @@ void tst_QWidget::tabletTracking() QTRY_COMPARE(widget.trackingChangeEventCount, 2); QWindowSystemInterface::handleTabletEvent(window, ulong(QDateTime::currentMSecsSinceEpoch()), deviceLocal, deviceGlobal, - QTabletEvent::Stylus, QTabletEvent::Pen, Qt::LeftButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); + int(QInputDevice::DeviceType::Stylus), int(QPointingDevice::PointerType::Pen), Qt::LeftButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); QCoreApplication::processEvents(); QTRY_COMPARE(widget.pressEventCount, 1); @@ -11023,12 +13178,12 @@ void tst_QWidget::tabletTracking() deviceLocal += QPoint(10, 10); deviceGlobal += QPoint(10, 10); QWindowSystemInterface::handleTabletEvent(window, ulong(QDateTime::currentMSecsSinceEpoch()), deviceLocal, deviceGlobal, - QTabletEvent::Stylus, QTabletEvent::Pen, Qt::LeftButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); + int(QInputDevice::DeviceType::Stylus), int(QPointingDevice::PointerType::Pen), Qt::LeftButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); QCoreApplication::processEvents(); QTRY_COMPARE(widget.moveEventCount, 3); QWindowSystemInterface::handleTabletEvent(window, ulong(QDateTime::currentMSecsSinceEpoch()), deviceLocal, deviceGlobal, - QTabletEvent::Stylus, QTabletEvent::Pen, Qt::NoButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); + int(QInputDevice::DeviceType::Stylus), int(QPointingDevice::PointerType::Pen), Qt::NoButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); QCoreApplication::processEvents(); QTRY_COMPARE(widget.releaseEventCount, 1); @@ -11036,7 +13191,7 @@ void tst_QWidget::tabletTracking() deviceLocal += QPoint(10, 10); deviceGlobal += QPoint(10, 10); QWindowSystemInterface::handleTabletEvent(window, ulong(QDateTime::currentMSecsSinceEpoch()), deviceLocal, deviceGlobal, - QTabletEvent::Stylus, QTabletEvent::Pen, Qt::NoButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); + int(QInputDevice::DeviceType::Stylus), int(QPointingDevice::PointerType::Pen), Qt::NoButton, 0, 0, 0, 0, 0, 0, uid, Qt::NoModifier); QCoreApplication::processEvents(); QTRY_COMPARE(widget.moveEventCount, 3); } @@ -11065,7 +13220,870 @@ void tst_QWidget::closeEvent() widget.windowHandle()->close(); widget.windowHandle()->close(); QCOMPARE(widget.closeCount, 1); + + CloseCountingWidget widget2; + widget2.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget2)); + widget2.close(); + widget2.close(); + QCOMPARE(widget2.closeCount, 1); + widget2.closeCount = 0; + + widget2.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget2)); + widget2.close(); + QCOMPARE(widget2.closeCount, 1); + + CloseCountingWidget widget3; + widget3.close(); + widget3.close(); + QEXPECT_FAIL("", "Closing a widget without a window will unconditionally send close events", Continue); + QCOMPARE(widget3.closeCount, 0); + + QWidget parent; + CloseCountingWidget child; + child.setParent(&parent); + parent.show(); + QVERIFY(QTest::qWaitForWindowExposed(&parent)); + child.close(); + QCOMPARE(child.closeCount, 1); + child.close(); + QEXPECT_FAIL("", "Closing a widget without a window will unconditionally send close events", Continue); + QCOMPARE(child.closeCount, 1); +} + +void tst_QWidget::closeWithChildWindow() +{ + QWidget widget; + auto childWidget = new QWidget(&widget); + childWidget->setAttribute(Qt::WA_NativeWindow); + childWidget->windowHandle()->create(); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + widget.windowHandle()->close(); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + // Check that the child window inside the window is now visible + QVERIFY(childWidget->isVisible()); + + // Now explicitly hide the childWidget + childWidget->hide(); + widget.windowHandle()->close(); + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + QVERIFY(!childWidget->isVisible()); +} + +class WinIdChangeSpy : public QObject +{ + Q_OBJECT +public: + QWidget *widget = nullptr; + WId winId = 0; + explicit WinIdChangeSpy(QWidget *w, QObject *parent = nullptr) + : QObject(parent) + , widget(w) + , winId(widget->winId()) + { + } + +public slots: + bool eventFilter(QObject *obj, QEvent *event) override + { + if (obj == widget) { + if (event->type() == QEvent::WinIdChange) { + winId = widget->winId(); + return true; + } + } + return false; + } +}; + +void tst_QWidget::winIdAfterClose() +{ + auto widget = new QWidget; + auto notifier = new QObject(widget); + auto deleteWidget = new QWidget(new QWidget(widget)); + auto spy = new WinIdChangeSpy(deleteWidget); + deleteWidget->installEventFilter(spy); + connect(notifier, &QObject::destroyed, [&] { delete deleteWidget; }); + + widget->setAttribute(Qt::WA_NativeWindow); + widget->windowHandle()->create(); + widget->show(); + + QVERIFY(QTest::qWaitForWindowExposed(widget)); + QVERIFY(spy->winId); + + widget->windowHandle()->close(); + delete widget; + + QCOMPARE(spy->winId, WId(0)); + delete spy; +} + +class ChangeEventWidget : public QWidget +{ +public: + ChangeEventWidget(QWidget *parent = nullptr) : QWidget(parent) {} + int languageChangeCount = 0; + int applicationFontChangeCount = 0; + int applicationPaletteChangeCount = 0; +protected: + bool event(QEvent *e) override + { + if (e->type() == QEvent::LanguageChange) + languageChangeCount++; + else if (e->type() == QEvent::ApplicationFontChange) + applicationFontChangeCount++; + else if (e->type() == QEvent::ApplicationPaletteChange) + applicationPaletteChangeCount++; + return QWidget::event(e); + } +}; + +class ChangeEventWindow : public QWindow +{ +public: + ChangeEventWindow(QWindow *parent = nullptr) : QWindow(parent) {} + int languageChangeCount = 0; + int applicationFontChangeCount = 0; + int applicationPaletteChangeCount = 0; +protected: + bool event(QEvent *e) override + { + if (e->type() == QEvent::LanguageChange) + languageChangeCount++; + else if (e->type() == QEvent::ApplicationFontChange) + applicationFontChangeCount++; + else if (e->type() == QEvent::ApplicationPaletteChange) + applicationPaletteChangeCount++; + return QWindow::event(e); + } +}; + +void tst_QWidget::receivesLanguageChangeEvent() +{ + // Confirm that any QWindow or QWidget only gets a single + // LanguageChange event when a translator is installed + ChangeEventWidget topLevel; + auto childWidget = new ChangeEventWidget(&topLevel); + topLevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + ChangeEventWindow ww; + ww.show(); + QVERIFY(QTest::qWaitForWindowExposed(&ww)); + ChangeEventWidget topLevelNotShown; + QTranslator t; + QVERIFY(t.load("hellotr_la.qm", ":/")); + QVERIFY(qApp->installTranslator(&t)); + QCoreApplication::sendPostedEvents(0, QEvent::LanguageChange); + QCOMPARE(topLevel.languageChangeCount, 1); + QCOMPARE(topLevelNotShown.languageChangeCount, 1); + QCOMPARE(childWidget->languageChangeCount, 1); + QCOMPARE(ww.languageChangeCount, 1); +} + +void tst_QWidget::receivesApplicationFontChangeEvent() +{ + // Confirm that any QWindow or top level QWidget only gets a single + // ApplicationFontChange event when the font is changed + const QFont origFont = QApplication::font(); + + ChangeEventWidget topLevel; + auto childWidget = new ChangeEventWidget(&topLevel); + topLevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + ChangeEventWindow ww; + ww.show(); + QVERIFY(QTest::qWaitForWindowExposed(&ww)); + ChangeEventWidget topLevelNotShown; + QFont changedFont = origFont; + changedFont.setPointSize(changedFont.pointSize() + 2); + QApplication::setFont(changedFont); + QCoreApplication::sendPostedEvents(0, QEvent::ApplicationFontChange); + QCOMPARE(topLevel.applicationFontChangeCount, 1); + QCOMPARE(topLevelNotShown.applicationFontChangeCount, 1); + // QWidget should not be passing the event on automatically + QCOMPARE(childWidget->applicationFontChangeCount, 0); + QCOMPARE(ww.applicationFontChangeCount, 1); + + QApplication::setFont(origFont); +} + +void tst_QWidget::receivesApplicationPaletteChangeEvent() +{ + // Confirm that any QWindow or top level QWidget only gets a single + // ApplicationPaletteChange event when the font is changed + const QPalette origPalette = QApplication::palette(); + + ChangeEventWidget topLevel; + auto childWidget = new ChangeEventWidget(&topLevel); + topLevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + ChangeEventWindow ww; + ww.show(); + QVERIFY(QTest::qWaitForWindowExposed(&ww)); + ChangeEventWidget topLevelNotShown; + QPalette changedPalette = origPalette; + changedPalette.setColor(QPalette::Base, Qt::red); + QApplication::setPalette(changedPalette); + QCoreApplication::sendPostedEvents(0, QEvent::ApplicationPaletteChange); + QCOMPARE(topLevel.applicationPaletteChangeCount, 1); + QCOMPARE(topLevelNotShown.applicationPaletteChangeCount, 1); + // QWidget should not be passing the event on automatically + QCOMPARE(childWidget->applicationPaletteChangeCount, 0); + QCOMPARE(ww.applicationPaletteChangeCount, 1); + + QApplication::setPalette(origPalette); +} + +class DeleteOnCloseEventWidget : public QWidget +{ +protected: + virtual void closeEvent(QCloseEvent *e) override + { + e->accept(); + delete this; + } +}; + +void tst_QWidget::deleteWindowInCloseEvent() +{ +#ifdef Q_OS_ANDROID + QSKIP("This test crashes on Android"); +#endif + QSignalSpy quitSpy(qApp, &QGuiApplication::lastWindowClosed); + + // Closing this widget should not cause a crash + auto widget = new DeleteOnCloseEventWidget; + widget->show(); + QVERIFY(QTest::qWaitForWindowExposed(widget)); + QTimer::singleShot(0, widget, [&]{ + widget->close(); + }); + QApplication::exec(); + + // It should still result in a single lastWindowClosed emit + QCOMPARE(quitSpy.size(), 1); } +/*! + Verify that both closing and deleting the last (only) window-widget + exits the application event loop. +*/ +void tst_QWidget::quitOnClose() +{ + QSignalSpy quitSpy(qApp, &QGuiApplication::lastWindowClosed); + + std::unique_ptr<QWidget>widget(new QWidget); + widget->show(); + QVERIFY(QTest::qWaitForWindowExposed(widget.get())); + + // QGuiApplication::lastWindowClosed is documented to only be emitted + // when we are in exec() + QTimer::singleShot(0, widget.get(), [&]{ + widget->close(); + }); + QApplication::exec(); + QCOMPARE(quitSpy.size(), 1); + + widget->show(); + QVERIFY(QTest::qWaitForWindowExposed(widget.get())); + QTimer::singleShot(0, widget.get(), [&]{ + widget.reset(); + }); + QApplication::exec(); + QCOMPARE(quitSpy.size(), 2); +} + +void tst_QWidget::setParentChangesFocus_data() +{ + QTest::addColumn<Qt::WindowType>("initialType"); + QTest::addColumn<bool>("initialParent"); + QTest::addColumn<Qt::WindowType>("targetType"); + QTest::addColumn<bool>("targetParent"); + QTest::addColumn<bool>("reparentBeforeShow"); + QTest::addColumn<QString>("focusWidget"); + + for (const bool before : {true, false}) { + const char *tag = before ? "before" : "after"; + QTest::addRow("give dialog parent, %s", tag) + << Qt::Dialog << false << Qt::Dialog << true << before << "lineEdit"; + QTest::addRow("make dialog parentless, %s", tag) + << Qt::Dialog << true << Qt::Dialog << false << before << "lineEdit"; + QTest::addRow("dialog to sheet, %s", tag) + << Qt::Dialog << true << Qt::Sheet << true << before << "lineEdit"; + QTest::addRow("window to widget, %s", tag) + << Qt::Window << true << Qt::Widget << true << before << "windowEdit"; + QTest::addRow("widget to window, %s", tag) + << Qt::Widget << true << Qt::Window << true << before << "lineEdit"; + } +} + +void tst_QWidget::setParentChangesFocus() +{ + QFETCH(Qt::WindowType, initialType); + QFETCH(bool, initialParent); + QFETCH(Qt::WindowType, targetType); + QFETCH(bool, targetParent); + QFETCH(bool, reparentBeforeShow); + QFETCH(QString, focusWidget); + + QWidget window; + window.setObjectName("window"); + QLineEdit *windowEdit = new QLineEdit(&window); + windowEdit->setObjectName("windowEdit"); + windowEdit->setFocus(); + + std::unique_ptr<QWidget> secondary(new QWidget(initialParent ? &window : nullptr, initialType)); + secondary->setObjectName("secondary"); + QLineEdit *lineEdit = new QLineEdit(secondary.get()); + lineEdit->setObjectName("lineEdit"); + QPushButton *pushButton = new QPushButton(secondary.get()); + pushButton->setObjectName("pushButton"); + lineEdit->setFocus(); + + window.show(); + QVERIFY(QTest::qWaitForWindowActive(&window)); + + if (reparentBeforeShow) { + secondary->setParent(targetParent ? &window : nullptr, targetType); + // making a widget into a window doesn't set a focusWidget until shown + if (secondary->focusWidget()) + QCOMPARE(secondary->focusWidget()->objectName(), focusWidget); + } + secondary->show(); + QApplicationPrivate::setActiveWindow(secondary.get()); + QVERIFY(QTest::qWaitForWindowActive(secondary.get())); + + if (!reparentBeforeShow) { + secondary->setParent(targetParent ? &window : nullptr, targetType); + secondary->show(); // reparenting hides, so show again + QApplicationPrivate::setActiveWindow(secondary.get()); + QVERIFY(QTest::qWaitForWindowActive(secondary.get())); + } + QVERIFY(QTest::qWaitFor([]{ return QApplication::focusWidget(); })); + QCOMPARE(QApplication::focusWidget()->objectName(), focusWidget); +} + +void tst_QWidget::activateWhileModalHidden() +{ + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("QWindow::requestActivate() is not supported."); + + QDialog dialog; + dialog.setWindowModality(Qt::ApplicationModal); + dialog.show(); + QVERIFY(QTest::qWaitForWindowActive(&dialog)); + QVERIFY(dialog.isActiveWindow()); + QCOMPARE(QApplication::activeWindow(), &dialog); + + dialog.hide(); + QTRY_VERIFY(!dialog.isVisible()); + + QMainWindow window; + window.show(); + QVERIFY(QTest::qWaitForWindowActive(&window)); + QVERIFY(window.isActiveWindow()); + QCOMPARE(QApplication::activeWindow(), &window); +} + +// Create a simple palette to prevent multiple paint events +QPalette tst_QWidget::simplePalette() +{ + static QPalette simplePalette = []{ + const QColor windowText = Qt::black; + const QColor backGround = QColor(239, 239, 239); + const QColor light = backGround.lighter(150); + const QColor mid = (backGround.darker(130)); + const QColor midLight = mid.lighter(110); + const QColor base = Qt::white; + const QColor dark = backGround.darker(150); + const QColor text = Qt::black; + const QColor highlight = QColor(48, 140, 198); + const QColor hightlightedText = Qt::white; + const QColor button = backGround; + const QColor shadow = dark.darker(135); + + QPalette defaultPalette(windowText, backGround, light, dark, mid, text, base); + defaultPalette.setBrush(QPalette::Midlight, midLight); + defaultPalette.setBrush(QPalette::Button, button); + defaultPalette.setBrush(QPalette::Shadow, shadow); + defaultPalette.setBrush(QPalette::HighlightedText, hightlightedText); + defaultPalette.setBrush(QPalette::Active, QPalette::Highlight, highlight); + return defaultPalette; + }(); + + return simplePalette; +} + +#ifdef Q_OS_ANDROID +void tst_QWidget::showFullscreenAndroid() +{ + QWidget w; + w.setAutoFillBackground(true); + QPalette p = w.palette(); + p.setColor(QPalette::Window, Qt::red); + w.setPalette(p); + + // Need to toggle showFullScreen() twice, see QTBUG-101968 + w.showFullScreen(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + w.showFullScreen(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); + + // Make sure that the lower part of the screen contains the red widget, not + // the buttons. + + const QRect fullGeometry = w.screen()->geometry(); + // Take a rect of (20 x 20) from the bottom area + const QRect grabArea(10, fullGeometry.height() - 30, 20, 20); + const QImage img = grabFromWidget(&w, grabArea).toImage().convertedTo(QImage::Format_RGB32); + + QPixmap expectedPix(20, 20); + expectedPix.fill(Qt::red); + const QImage expectedImg = expectedPix.toImage().convertedTo(QImage::Format_RGB32); + + QCOMPARE(img, expectedImg); +} +#endif // Q_OS_ANDROID + +void tst_QWidget::setVisibleDuringDestruction() +{ + CreateDestroyWidget widget; + widget.create(); + QVERIFY(widget.windowHandle()); + + QSignalSpy signalSpy(widget.windowHandle(), &QWindow::visibleChanged); + EventSpy<QWindow> showEventSpy(widget.windowHandle(), QEvent::Show); + widget.show(); + QTRY_COMPARE(showEventSpy.count(), 1); + QTRY_COMPARE(signalSpy.count(), 1); + + EventSpy<QWindow> hideEventSpy(widget.windowHandle(), QEvent::Hide); + widget.hide(); + QTRY_COMPARE(hideEventSpy.count(), 1); + QTRY_COMPARE(signalSpy.count(), 2); + + widget.show(); + QTRY_COMPARE(showEventSpy.count(), 2); + QTRY_COMPARE(signalSpy.count(), 3); + + widget.destroy(); + QTRY_COMPARE(hideEventSpy.count(), 2); + QTRY_COMPARE(signalSpy.count(), 4); +} + +void tst_QWidget::explicitShowHide() +{ + { + QWidget parent; + parent.setObjectName("Parent"); + QWidget child(&parent); + child.setObjectName("Child"); + + QCOMPARE(parent.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Hidden), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + + parent.show(); + QCOMPARE(parent.testAttribute(Qt::WA_WState_ExplicitShowHide), true); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Hidden), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + + // Fix up earlier expected failure + child.setAttribute(Qt::WA_WState_ExplicitShowHide, false); + + parent.hide(); + QCOMPARE(parent.testAttribute(Qt::WA_WState_ExplicitShowHide), true); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Hidden), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + } + + { + // Test what happens when a child is reparented after showing it + + QWidget parent; + parent.setObjectName("Parent"); + QWidget child; + child.setObjectName("Child"); + + child.show(); + QCOMPARE(child.isVisible(), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + + child.setParent(&parent); + // As documented, a widget becomes invisible as part of changing + // its parent, even if it was previously visible. The user must call + // show() to make the widget visible again. + QCOMPARE(child.isVisible(), false); + + // However, the widget does not end up with Qt::WA_WState_Hidden, + // as QWidget::setParent treats it as a child, which normally will + // not get Qt::WA_WState_Hidden out of the box. + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + + // For some reason we reset WA_WState_ExplicitShowHide, and it's + // not clear whether this is correct or not See QWidget::setParent() + // for a comment with more details. + QEXPECT_FAIL("", "We reset WA_WState_ExplicitShowHide on widget re-parent", Continue); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), true); + + // The fact that the child doesn't have Qt::WA_WState_Hidden means + // it's sufficient to show the parent widget. We don't need to + // explicitly show the child. + parent.show(); + QCOMPARE(child.isVisible(), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + } + + { + QWidget parent; + parent.setObjectName("Parent"); + QWidget child(&parent); + child.setObjectName("Child"); + + parent.show(); + + // If a non-native child ends up being closed, we will hide the + // widget, but do so via QWidget::hide(), which marks the widget + // as explicitly hidden. + + child.setAttribute(Qt::WA_WState_ExplicitShowHide, false); + QCOMPARE(child.close(), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), true); + + child.show(); + child.setAttribute(Qt::WA_NativeWindow); + child.setAttribute(Qt::WA_WState_ExplicitShowHide, false); + QCOMPARE(child.close(), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), true); + + child.show(); + child.setAttribute(Qt::WA_WState_ExplicitShowHide, false); + QCOMPARE(child.windowHandle()->close(), false); // Can't close non-top level QWindows + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + + // If we end up in QWidgetPrivate::handleClose via QWidgetWindow::closeEvent, + // either through QWindow::close(), or via QWSI::handleCloseEvent, we'll still + // do the explicit hide. + + child.show(); + child.setAttribute(Qt::WA_WState_ExplicitShowHide, false); + QCOMPARE(QWindowSystemInterface::handleCloseEvent<QWindowSystemInterface::SynchronousDelivery>( + child.windowHandle()), true); + QEXPECT_FAIL("", "Closing a native child via QWSI is treated as an explicit hide", Continue); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), true); + } + + { + QWidget widget; + widget.show(); + widget.hide(); + + QCOMPARE(widget.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(widget.testAttribute(Qt::WA_WState_ExplicitShowHide), true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Hidden), true); + + // The widget is now explicitly hidden. Showing it again, via QWindow, + // should make the widget visible, and it should not stay hidden, as + // that's an invalid state for a widget. + + widget.windowHandle()->setVisible(true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Visible), true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_ExplicitShowHide), true); + QCOMPARE(widget.testAttribute(Qt::WA_WState_Hidden), false); + } +} + +/*! + Verify that we deliver DragEnter/Leave events symmetrically, even if the + widget entered didn't accept the DragEnter event. +*/ +void tst_QWidget::dragEnterLeaveSymmetry() +{ + QWidget widget; + widget.setAcceptDrops(true); + QLineEdit lineEdit; + QLabel label("Hello world"); + label.setAcceptDrops(true); + + struct EventFilter : QObject + { + bool eventFilter(QObject *receiver, QEvent *event) override + { + switch (event->type()) { + case QEvent::DragEnter: + case QEvent::DragLeave: + receivers[event->type()] << receiver; + break; + + default: + break; + } + + return false; + } + + QMap<QEvent::Type, QList<QObject *>> receivers; + + void clear() { receivers.clear(); } + bool hasEntered(QWidget *widget) const + { + return receivers.value(QEvent::DragEnter).contains(widget); + } + bool hasLeft(QWidget *widget) const + { + return receivers.value(QEvent::DragLeave).contains(widget); + } + } filter; + + widget.installEventFilter(&filter); + lineEdit.installEventFilter(&filter); + label.installEventFilter(&filter); + + QVBoxLayout vbox; + vbox.setContentsMargins(10, 10, 10, 10); + vbox.addWidget(&lineEdit); + vbox.addWidget(&label); + widget.setLayout(&vbox); + + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + + QMimeData data; + data.setColorData(QVariant::fromValue(Qt::red)); + QWindowSystemInterface::handleDrag(widget.windowHandle(), &data, QPoint(1, 1), + Qt::ActionMask, Qt::LeftButton, {}); + QVERIFY(filter.hasEntered(&widget)); + QVERIFY(!filter.hasEntered(&lineEdit)); + QVERIFY(!filter.hasEntered(&label)); + QVERIFY(widget.underMouse()); + QVERIFY(!lineEdit.underMouse()); + filter.clear(); + + QWindowSystemInterface::handleDrag(widget.windowHandle(), &data, lineEdit.geometry().center(), + Qt::ActionMask, Qt::LeftButton, {}); + // DragEnter propagates as the lineEdit doesn't want it, so the widget + // sees both a Leave and an Enter event + QVERIFY(filter.hasLeft(&widget)); + QVERIFY(filter.hasEntered(&widget)); + QVERIFY(filter.hasEntered(&widget)); + // both have the UnderMouse attribute set + QVERIFY(lineEdit.underMouse()); + QVERIFY(widget.underMouse()); + + // The lineEdit didn't accept the DragEnter, but it should still has to + // get the DragLeave so that UnderMouse is cleared; the widget gets both + // Leave and Enter through propagation. + QWindowSystemInterface::handleDrag(widget.windowHandle(), &data, label.geometry().center(), + Qt::ActionMask, Qt::LeftButton, {}); + QVERIFY(filter.hasLeft(&lineEdit)); + QVERIFY(filter.hasLeft(&widget)); + QVERIFY(filter.hasEntered(&label)); + QVERIFY(filter.hasEntered(&widget)); + + QVERIFY(!lineEdit.underMouse()); + QVERIFY(label.underMouse()); + QVERIFY(widget.underMouse()); +} + +void tst_QWidget::reparentWindowHandles_data() +{ + QTest::addColumn<int>("stage"); + QTest::addRow("reparent child") << 1; + QTest::addRow("top level to child") << 2; + QTest::addRow("transient parent") << 3; + QTest::addRow("window container") << 4; +} + +void tst_QWidget::reparentWindowHandles() +{ + const bool nativeSiblingsOriginal = qApp->testAttribute(Qt::AA_DontCreateNativeWidgetSiblings); + qApp->setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); + auto nativeSiblingGuard = qScopeGuard([&]{ + qApp->setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, nativeSiblingsOriginal); + }); + + QFETCH(int, stage); + + switch (stage) { + case 1: { + // Reparent child widget + + QWidget topLevel; + topLevel.setAttribute(Qt::WA_NativeWindow); + QVERIFY(topLevel.windowHandle()); + QPointer<QWidget> child = new QWidget(&topLevel); + child->setAttribute(Qt::WA_DontCreateNativeAncestors); + child->setAttribute(Qt::WA_NativeWindow); + QVERIFY(child->windowHandle()); + + QWidget anotherTopLevel; + anotherTopLevel.setAttribute(Qt::WA_NativeWindow); + QVERIFY(anotherTopLevel.windowHandle()); + QPointer<QWidget> intermediate = new QWidget(&anotherTopLevel); + QPointer<QWidget> leaf = new QWidget(intermediate); + leaf->setAttribute(Qt::WA_DontCreateNativeAncestors); + leaf->setAttribute(Qt::WA_NativeWindow); + QVERIFY(leaf->windowHandle()); + QVERIFY(!intermediate->windowHandle()); + + // Reparenting a native widget should reparent the QWindow + child->setParent(leaf); + QCOMPARE(child->windowHandle()->parent(), leaf->windowHandle()); + QCOMPARE(child->windowHandle()->transientParent(), nullptr); + QVERIFY(!intermediate->windowHandle()); + + // So should reparenting a non-native widget with native children + intermediate->setParent(&topLevel); + QVERIFY(!intermediate->windowHandle()); + QCOMPARE(leaf->windowHandle()->parent(), topLevel.windowHandle()); + QCOMPARE(leaf->windowHandle()->transientParent(), nullptr); + QCOMPARE(child->windowHandle()->parent(), leaf->windowHandle()); + QCOMPARE(child->windowHandle()->transientParent(), nullptr); + } + break; + case 2: { + // Top level to child + + QWidget topLevel; + topLevel.setAttribute(Qt::WA_NativeWindow); + + // A regular top level loses its nativeness + QPointer<QWidget> regularToplevel = new QWidget; + regularToplevel->show(); + QVERIFY(QTest::qWaitForWindowExposed(regularToplevel)); + QVERIFY(regularToplevel->windowHandle()); + regularToplevel->setParent(&topLevel); + QVERIFY(!regularToplevel->windowHandle()); + + // A regular top level loses its nativeness + QPointer<QWidget> regularToplevelWithNativeChildren = new QWidget; + QPointer<QWidget> nativeChild = new QWidget(regularToplevelWithNativeChildren); + nativeChild->setAttribute(Qt::WA_DontCreateNativeAncestors); + nativeChild->setAttribute(Qt::WA_NativeWindow); + QVERIFY(nativeChild->windowHandle()); + regularToplevelWithNativeChildren->show(); + QVERIFY(QTest::qWaitForWindowExposed(regularToplevelWithNativeChildren)); + QVERIFY(regularToplevelWithNativeChildren->windowHandle()); + regularToplevelWithNativeChildren->setParent(&topLevel); + QVERIFY(!regularToplevelWithNativeChildren->windowHandle()); + // But the native child does not + QVERIFY(nativeChild->windowHandle()); + QCOMPARE(nativeChild->windowHandle()->parent(), topLevel.windowHandle()); + + // An explicitly native top level keeps its nativeness, and the window handle moves + QPointer<QWidget> nativeTopLevel = new QWidget; + nativeTopLevel->setAttribute(Qt::WA_NativeWindow); + QVERIFY(nativeTopLevel->windowHandle()); + nativeTopLevel->setParent(&topLevel); + QVERIFY(nativeTopLevel->windowHandle()); + QCOMPARE(nativeTopLevel->windowHandle()->parent(), topLevel.windowHandle()); + } + break; + case 3: { + // Transient parent + + QWidget topLevel; + topLevel.setAttribute(Qt::WA_NativeWindow); + QVERIFY(topLevel.windowHandle()); + QPointer<QWidget> child = new QWidget(&topLevel); + child->setAttribute(Qt::WA_NativeWindow); + QVERIFY(child->windowHandle()); + + QWidget anotherTopLevel; + anotherTopLevel.setAttribute(Qt::WA_NativeWindow); + QVERIFY(anotherTopLevel.windowHandle()); + + // Make transient child of top level + anotherTopLevel.setParent(&topLevel, Qt::Window); + QCOMPARE(anotherTopLevel.windowHandle()->parent(), nullptr); + QCOMPARE(anotherTopLevel.windowHandle()->transientParent(), topLevel.windowHandle()); + + // Make transient child of child + anotherTopLevel.setParent(child, Qt::Window); + QCOMPARE(anotherTopLevel.windowHandle()->parent(), nullptr); + QCOMPARE(anotherTopLevel.windowHandle()->transientParent(), topLevel.windowHandle()); + } + break; + case 4: { + // Window container + + QWidget topLevel; + topLevel.setAttribute(Qt::WA_NativeWindow); + QVERIFY(topLevel.windowHandle()); + + QPointer<QWidget> child = new QWidget(&topLevel); + QVERIFY(!child->windowHandle()); + + QWindow *window = new QWindow; + QWidget *container = QWidget::createWindowContainer(window); + container->setParent(child); + topLevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&topLevel)); + QCOMPARE(window->parent(), topLevel.windowHandle()); + + QWidget anotherTopLevel; + anotherTopLevel.setAttribute(Qt::WA_NativeWindow); + QVERIFY(anotherTopLevel.windowHandle()); + + child->setParent(&anotherTopLevel); + QCOMPARE(window->parent(), anotherTopLevel.windowHandle()); + } + break; + default: + Q_UNREACHABLE(); + } +} + +#ifndef QT_NO_CONTEXTMENU +void tst_QWidget::contextMenuTrigger() +{ + class ContextMenuWidget : public QWidget + { + public: + int events = 0; + + protected: + void contextMenuEvent(QContextMenuEvent *) override { ++events; } + }; + + const Qt::ContextMenuTrigger wasTrigger = QGuiApplication::styleHints()->contextMenuTrigger(); + auto restoreTriggerGuard = qScopeGuard([wasTrigger]{ + QGuiApplication::styleHints()->setContextMenuTrigger(wasTrigger); + }); + + ContextMenuWidget widget; + widget.show(); + QVERIFY(!qApp->topLevelWindows().empty()); + auto *window = qApp->topLevelWindows()[0]; + QVERIFY(window); + QCOMPARE(widget.events, 0); + QGuiApplication::styleHints()->setContextMenuTrigger(Qt::ContextMenuTrigger::Press); + QTest::mousePress(window, Qt::RightButton); + QCOMPARE(widget.events, 1); + QTest::mouseRelease(window, Qt::RightButton); + QCOMPARE(widget.events, 1); + QGuiApplication::styleHints()->setContextMenuTrigger(Qt::ContextMenuTrigger::Release); + QTest::mousePress(window, Qt::RightButton); + QCOMPARE(widget.events, 1); + QTest::mouseRelease(window, Qt::RightButton); + QCOMPARE(widget.events, 2); +} +#endif + QTEST_MAIN(tst_QWidget) #include "tst_qwidget.moc" diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget_mac_helpers.h b/tests/auto/widgets/kernel/qwidget/tst_qwidget_mac_helpers.h deleted file mode 100644 index 87de300da9..0000000000 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget_mac_helpers.h +++ /dev/null @@ -1,35 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#include <QtCore/QString> -#include <QtCore/QPair> -#include <QtWidgets/QWidget> - -#pragma once // Yeah, it's deprecated in general, but it's standard practice for Mac OS X. - -QString nativeWindowTitle(QWidget *widget, Qt::WindowState state); -bool nativeWindowModified(QWidget *widget); diff --git a/tests/auto/widgets/kernel/qwidget/tst_qwidget_mac_helpers.mm b/tests/auto/widgets/kernel/qwidget/tst_qwidget_mac_helpers.mm deleted file mode 100644 index e2d00aa25b..0000000000 --- a/tests/auto/widgets/kernel/qwidget/tst_qwidget_mac_helpers.mm +++ /dev/null @@ -1,57 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the documentation of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -// some versions of CALayer.h use 'slots' as an identifier -#define QT_NO_KEYWORDS - -#include "tst_qwidget_mac_helpers.h" -#include <QApplication> -#include <qpa/qplatformnativeinterface.h> -#include <private/qcore_mac_p.h> - -#include <AppKit/AppKit.h> - -QString nativeWindowTitle(QWidget *window, Qt::WindowState state) -{ - QWindow *qwindow = window->windowHandle(); - NSWindow *nswindow = (NSWindow *) qApp->platformNativeInterface()->nativeResourceForWindow("nswindow", qwindow); - QCFString macTitle; - if (state == Qt::WindowMinimized) { - macTitle = reinterpret_cast<CFStringRef>([[nswindow miniwindowTitle] retain]); - } else { - macTitle = reinterpret_cast<CFStringRef>([[nswindow title] retain]); - } - return macTitle; -} - -bool nativeWindowModified(QWidget *widget) -{ - QWindow *qwindow = widget->windowHandle(); - NSWindow *nswindow = (NSWindow *) qApp->platformNativeInterface()->nativeResourceForWindow("nswindow", qwindow); - return [nswindow isDocumentEdited]; -} diff --git a/tests/auto/widgets/kernel/qwidget_window/BLACKLIST b/tests/auto/widgets/kernel/qwidget_window/BLACKLIST index 934f2e8025..77853a3e8c 100644 --- a/tests/auto/widgets/kernel/qwidget_window/BLACKLIST +++ b/tests/auto/widgets/kernel/qwidget_window/BLACKLIST @@ -1,8 +1,18 @@ +# QTBUG-87412 +[tst_move_show] +android +[tst_show_move_hide_show] +android +[tst_resize_show] +android +[tst_show_resize_hide_show] +android [tst_resize_count] -# QTBUG-66345 -opensuse-42.3 -ubuntu-16.04 -ubuntu-18.04 +android [setWindowState] -ubuntu-18.04 -rhel +android +[mouseMoveWithPopup] +android +# QTBUG-96270 +[tst_paintEventOnSecondShow] +opensuse diff --git a/tests/auto/widgets/kernel/qwidget_window/CMakeLists.txt b/tests/auto/widgets/kernel/qwidget_window/CMakeLists.txt new file mode 100644 index 0000000000..af60c92cbf --- /dev/null +++ b/tests/auto/widgets/kernel/qwidget_window/CMakeLists.txt @@ -0,0 +1,24 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qwidget_window Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qwidget_window LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qwidget_window + SOURCES + tst_qwidget_window.cpp + LIBRARIES + Qt::CorePrivate + Qt::Gui + Qt::GuiPrivate + Qt::TestPrivate + Qt::Widgets + Qt::WidgetsPrivate +) diff --git a/tests/auto/widgets/kernel/qwidget_window/qwidget_window.pro b/tests/auto/widgets/kernel/qwidget_window/qwidget_window.pro deleted file mode 100644 index 00bf763c35..0000000000 --- a/tests/auto/widgets/kernel/qwidget_window/qwidget_window.pro +++ /dev/null @@ -1,4 +0,0 @@ -CONFIG += testcase -TARGET = tst_qwidget_window -QT += widgets testlib core-private gui-private testlib-private -SOURCES += tst_qwidget_window.cpp diff --git a/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp b/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp index c6b5669965..e771737ae0 100644 --- a/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp +++ b/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp @@ -1,33 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <QtGui/QtGui> #include <QtCore/QTextStream> #include <QtCore/QStringList> @@ -43,6 +18,7 @@ #include <qlabel.h> #include <qmainwindow.h> #include <qtoolbar.h> +#include <qsignalspy.h> #include <private/qwindow_p.h> #include <private/qguiapplication_p.h> #include <qpa/qplatformintegration.h> @@ -52,6 +28,8 @@ #include <QtTest/private/qtesthelpers_p.h> +#include <QtWidgets/private/qapplication_p.h> + using namespace QTestPrivate; // Compare a window position that may go through scaling in the platform plugin with fuzz. @@ -75,6 +53,7 @@ public: tst_QWidget_window(); public slots: + void init(); void initTestCase(); void cleanupTestCase(); void cleanup(); @@ -90,6 +69,8 @@ private slots: void tst_show_resize(); void tst_show_resize_hide_show(); + void close(); + void tst_windowFilePathAndwindowTitle_data(); void tst_windowFilePathAndwindowTitle(); void tst_windowFilePath_data(); @@ -104,6 +85,7 @@ private slots: void tst_dnd(); void tst_dnd_events(); void tst_dnd_propagation(); + void tst_dnd_destroyOnDrop(); #endif void tst_qtbug35600(); @@ -113,6 +95,8 @@ private slots: void tst_resize_count(); void tst_move_count(); + void tst_showhide_count(); + void tst_eventfilter_on_toplevel(); void QTBUG_50561_QCocoaBackingStore_paintDevice_crash(); @@ -124,6 +108,16 @@ private slots: void QTBUG_56277_resize_on_showEvent(); + void mouseMoveWithPopup_data(); + void mouseMoveWithPopup(); + + void showHideWindowHandle_data(); + void showHideWindowHandle(); + + void resetFocusObjectOnDestruction(); + + void cleanupOnDestruction(); + private: QSize m_testWidgetSize; const int m_fuzz; @@ -141,6 +135,11 @@ void tst_QWidget_window::initTestCase() { } +void tst_QWidget_window::init() +{ + QTest::failOnWarning(QRegularExpression(".*No such slot.*")); +} + void tst_QWidget_window::cleanupTestCase() { } @@ -190,9 +189,6 @@ void tst_QWidget_window::tst_move_show() const QPoint pos(100, 100); w.move(pos); w.show(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "Winrt does not support move", Abort); -#endif QVERIFY2(qFuzzyCompareWindowPosition(w.pos(), pos, m_fuzz), qPrintable(msgPointMismatch(w.pos(), pos))); } @@ -224,9 +220,6 @@ void tst_QWidget_window::tst_resize_show() QWidget w; w.resize(m_testWidgetSize); w.show(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "Winrt does not support resize", Abort); -#endif QCOMPARE(w.size(), m_testWidgetSize); } @@ -248,6 +241,70 @@ void tst_QWidget_window::tst_show_resize_hide_show() QCOMPARE(w.size(), m_testWidgetSize); } +void tst_QWidget_window::close() +{ + // Verfy that closing a QWidgetWindow deletes its platform window, + // as expected of a QWindow subclass. This must be done also + // if QWidget API is used to close. The QCloseEvent must not be + // spontaneous if the close is triggered by a Qt API that the application + // would call in response to an event, and spontaneous if it is directly + // caused by user interaction, such as clicking the (x) in the titlebar. + // We can simulate this only by generating a WindowSystemEvent. + // Children of the window should get a hide event (never spontaneous when + // caused by closing the window). + + struct Widget : public QWidget + { + using QWidget::QWidget; + int spontClose = -1; + int spontHide = -1; + protected: + void hideEvent(QHideEvent *e) override + { spontHide = e->spontaneous() ? 1 : 0; } + void closeEvent(QCloseEvent *e) override + { spontClose = e->spontaneous() ? 1 : 0; } + }; + + // QWindow::close() + { + Widget w; + Widget child(&w); + w.winId(); + QVERIFY(w.windowHandle()); + QVERIFY(w.windowHandle()->handle()); + w.windowHandle()->close(); + QCOMPARE(w.spontClose, 0); + QCOMPARE(child.spontHide, -1); // was never shown + QVERIFY(w.windowHandle()); + QVERIFY(!w.windowHandle()->handle()); + } + + // QWidget::close() + { + Widget w; + Widget child(&w); + w.show(); + QVERIFY(w.windowHandle()); + QVERIFY(w.windowHandle()->handle()); + w.close(); + QCOMPARE(w.spontClose, 0); + QCOMPARE(child.spontHide, 0); + QVERIFY(w.windowHandle()); + QVERIFY(!w.windowHandle()->handle()); + } + + // User-initiated close + { + Widget w; + Widget child(&w); + w.show(); + QWindowSystemInterface::handleCloseEvent(w.windowHandle()); + QApplication::processEvents(); + QCOMPARE(w.spontClose, 1); + QCOMPARE(child.spontHide, 0); + } +} + class PaintTestWidget : public QWidget { public: @@ -369,14 +426,14 @@ void tst_QWidget_window::tst_windowFilePath() void tst_QWidget_window::tst_showWithoutActivating() { QString platformName = QGuiApplication::platformName().toLower(); - if (platformName == "cocoa") - QSKIP("Cocoa: This fails. Figure out why."); - else if (platformName != QStringLiteral("xcb") - && platformName != QStringLiteral("windows") - && platformName != QStringLiteral("ios") - && platformName != QStringLiteral("tvos") - && platformName != QStringLiteral("watchos")) - QSKIP("Qt::WA_ShowWithoutActivating is currently supported only on xcb, windows, and ios/tvos/watchos platforms."); + if (platformName != QStringLiteral("xcb") + && platformName != QStringLiteral("windows") + && platformName != QStringLiteral("cocoa") + && platformName != QStringLiteral("ios") + && platformName != QStringLiteral("tvos") + && platformName != QStringLiteral("watchos")) + QSKIP("Qt::WA_ShowWithoutActivating is currently supported only on xcb, " \ + "windows, and macos/ios/tvos/watchos platforms."); QWidget w1; w1.setAttribute(Qt::WA_ShowWithoutActivating); @@ -400,6 +457,7 @@ void tst_QWidget_window::tst_paintEventOnSecondShow() { PaintTestWidget w; w.show(); + QVERIFY(QTest::qWaitForWindowExposed(&w)); w.hide(); w.paintEventCount = 0; @@ -411,10 +469,12 @@ void tst_QWidget_window::tst_paintEventOnSecondShow() void tst_QWidget_window::tst_exposeObscuredMapped_QTBUG39220() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + const auto integration = QGuiApplicationPrivate::platformIntegration(); if (!integration->hasCapability(QPlatformIntegration::MultipleWindows) - || !integration->hasCapability(QPlatformIntegration::NonFullScreenWindows) - || QGuiApplication::platformName() == QLatin1String("winrt")) { + || !integration->hasCapability(QPlatformIntegration::NonFullScreenWindows)) { QSKIP("The platform does not have the required capabilities"); } // QTBUG-39220: Fully obscured parent widgets may not receive expose @@ -438,6 +498,9 @@ void tst_QWidget_window::tst_exposeObscuredMapped_QTBUG39220() void tst_QWidget_window::tst_paintEventOnResize_QTBUG50796() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + const QRect availableGeo = QGuiApplication::primaryScreen()->availableGeometry(); QWidget root; @@ -519,10 +582,10 @@ public: : QWidget(w), m_log(log), m_ignoreDragMove(ignoreDragMove) {} protected: - void dragEnterEvent(QDragEnterEvent *); - void dragMoveEvent(QDragMoveEvent *); - void dragLeaveEvent(QDragLeaveEvent *); - void dropEvent(QDropEvent *); + void dragEnterEvent(QDragEnterEvent *) override; + void dragMoveEvent(QDragMoveEvent *) override; + void dragLeaveEvent(QDragLeaveEvent *) override; + void dropEvent(QDropEvent *) override; private: void formatDropEvent(const char *function, const QDropEvent *e, QTextStream &str) const; @@ -532,7 +595,7 @@ private: void DnDEventLoggerWidget::formatDropEvent(const char *function, const QDropEvent *e, QTextStream &str) const { - str << objectName() << "::" << function << " at " << e->pos().x() << ',' << e->pos().y() + str << objectName() << "::" << function << " at " << e->position().toPoint().x() << ',' << e->position().toPoint().y() << " action=" << e->dropAction() << ' ' << quintptr(e->mimeData()) << " '" << e->mimeData()->text() << '\''; } @@ -576,12 +639,15 @@ static QString msgEventAccepted(const QDropEvent &e) { QString message; QTextStream str(&message); - str << "Event at " << e.pos().x() << ',' << e.pos().y() << ' ' << (e.isAccepted() ? "accepted" : "ignored"); + str << "Event at " << e.position().toPoint().x() << ',' << e.position().toPoint().y() << ' ' << (e.isAccepted() ? "accepted" : "ignored"); return message; } void tst_QWidget_window::tst_dnd() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QStringList log; DnDEventLoggerWidget dndTestWidget(&log); @@ -620,15 +686,11 @@ void tst_QWidget_window::tst_dnd() QWidget *dropsAcceptingWidget3 = new DnDEventLoggerWidget(&log, &dndTestWidget, true); dropsAcceptingWidget3->setAcceptDrops(true); dropsAcceptingWidget3->setObjectName(QLatin1String("acceptingDropsWidget3")); - // 260 + 40 = 300 = widget size, must not be more than that. - // otherwise it will break WinRT because there the tlw is maximized every time - // and this window will receive one more event dropsAcceptingWidget3->resize(180, 40); dropsAcceptingWidget3->move(10, 260); dndTestWidget.show(); QVERIFY(QTest::qWaitForWindowExposed(&dndTestWidget)); - qApp->setActiveWindow(&dndTestWidget); QVERIFY(QTest::qWaitForWindowActive(&dndTestWidget)); QMimeData mimeData; @@ -696,7 +758,7 @@ public: DnDEventRecorder() { setAcceptDrops(true); } protected: - void mousePressEvent(QMouseEvent *) + void mousePressEvent(QMouseEvent *) override { QMimeData *mimeData = new QMimeData; mimeData->setData("application/x-dnditemdata", "some data"); @@ -705,30 +767,30 @@ protected: drag->exec(); } - void dragEnterEvent(QDragEnterEvent *e) + void dragEnterEvent(QDragEnterEvent *e) override { e->accept(); _dndEvents.append(QStringLiteral("DragEnter ")); } - void dragMoveEvent(QDragMoveEvent *e) + void dragMoveEvent(QDragMoveEvent *e) override { e->accept(); _dndEvents.append(QStringLiteral("DragMove ")); - emit releaseMouseButton(); + emit dragMoveReceived(); } - void dragLeaveEvent(QDragLeaveEvent *e) + void dragLeaveEvent(QDragLeaveEvent *e) override { e->accept(); _dndEvents.append(QStringLiteral("DragLeave ")); } - void dropEvent(QDropEvent *e) + void dropEvent(QDropEvent *e) override { e->accept(); _dndEvents.append(QStringLiteral("DropEvent ")); } signals: - void releaseMouseButton(); + void dragMoveReceived(); }; void tst_QWidget_window::tst_dnd_events() @@ -763,7 +825,7 @@ void tst_QWidget_window::tst_dnd_events() // Some dnd implementation rely on running internal event loops, so we have to use // the following queued signal hack to simulate mouse clicks in the widget. - QObject::connect(&dndWidget, &DnDEventRecorder::releaseMouseButton, this, [=]() { + QObject::connect(&dndWidget, &DnDEventRecorder::dragMoveReceived, this, [=]() { QTest::mouseRelease(window, Qt::LeftButton); }, Qt::QueuedConnection); @@ -772,6 +834,27 @@ void tst_QWidget_window::tst_dnd_events() QTest::mousePress(window, Qt::LeftButton); QCOMPARE(dndWidget._dndEvents, expectedDndEvents); + + dndWidget._dndEvents.clear(); + dndWidget.disconnect(); + int step = 0; + QObject::connect(&dndWidget, &DnDEventRecorder::dragMoveReceived, this, [window, &step]() { + switch (step++) { + case 0: + QTest::keyPress(window, Qt::Key_Shift, Qt::ShiftModifier); + break; + case 1: + QTest::keyRelease(window, Qt::Key_Shift, Qt::NoModifier); + break; + default: + QTest::mouseRelease(window, Qt::LeftButton); + break; + } + }, Qt::QueuedConnection); + + QTest::mousePress(window, Qt::LeftButton); + const QString expectedDndWithModsEvents = "DragEnter DragMove DragMove DragMove DropEvent "; + QCOMPARE(dndWidget._dndEvents, expectedDndWithModsEvents); } class DropTarget : public QWidget @@ -819,6 +902,9 @@ public: void tst_QWidget_window::tst_dnd_propagation() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QMimeData mimeData; mimeData.setText(QLatin1String("testmimetext")); @@ -833,17 +919,89 @@ void tst_QWidget_window::tst_dnd_propagation() auto posInsideLabel = QHighDpi::toNativePixels(QPoint(60, 60), window->screen()); // Enter DropTarget. - QWindowSystemInterface::handleDrag(window, &mimeData, posInsideDropTarget, supportedActions, 0, 0); + QWindowSystemInterface::handleDrag(window, &mimeData, posInsideDropTarget, supportedActions, {}, {}); // Enter QLabel. This will propagate because default QLabel does // not accept the drop event in dragEnterEvent(). - QWindowSystemInterface::handleDrag(window, &mimeData, posInsideLabel, supportedActions, 0, 0); + QWindowSystemInterface::handleDrag(window, &mimeData, posInsideLabel, supportedActions, {}, {}); // Drop on QLabel. DropTarget will get dropEvent(), because it accepted the event. - QWindowSystemInterface::handleDrop(window, &mimeData, posInsideLabel, supportedActions, 0, 0); + QWindowSystemInterface::handleDrop(window, &mimeData, posInsideLabel, supportedActions, {}, {}); QGuiApplication::processEvents(); QCOMPARE(target.mDndEvents, "enter leave enter drop "); } + +class ReparentSelfOnDropWidget : public QWidget +{ +public: + ReparentSelfOnDropWidget(QWidget *newFutureParent) + : m_newFutureParent(newFutureParent) + { + setAcceptDrops(true); + + const QRect availableGeometry = QGuiApplication::primaryScreen()->availableGeometry(); + auto width = availableGeometry.width() / 6; + auto height = availableGeometry.height() / 4; + + setGeometry(availableGeometry.x() + 200, availableGeometry.y() + 200, width, height); + + QLabel *label = new QLabel(QStringLiteral("Test"), this); + label->setGeometry(40, 40, 60, 60); + label->setAcceptDrops(true); + } + + void dragEnterEvent(QDragEnterEvent *event) override + { + event->accept(); + } + + void dragMoveEvent(QDragMoveEvent *event) override + { + event->acceptProposedAction(); + } + + void dropEvent(QDropEvent *event) override + { + event->accept(); + // Turn 'this' from a top-level widget to a child widget. + // This destroys the QWidgetWindow since the widget is no longer top-level. + setParent(m_newFutureParent); + } + +private: + QWidget *m_newFutureParent; +}; + +void tst_QWidget_window::tst_dnd_destroyOnDrop() +{ + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + + QMimeData mimeData; + mimeData.setText(QLatin1String("testmimetext")); + + QWidget newParent; + newParent.resize(400, 400); + newParent.show(); + QVERIFY(QTest::qWaitForWindowActive(&newParent)); + + ReparentSelfOnDropWidget *target = new ReparentSelfOnDropWidget(&newParent); + target->show(); + QVERIFY(QTest::qWaitForWindowActive(target)); + + Qt::DropActions supportedActions = Qt::DropAction::CopyAction; + QWindow *window = target->windowHandle(); + + auto posInsideDropTarget = QHighDpi::toNativePixels(QPoint(20, 20), window->screen()); + auto posInsideLabel = QHighDpi::toNativePixels(QPoint(60, 60), window->screen()); + + QWindowSystemInterface::handleDrag(window, &mimeData, posInsideDropTarget, supportedActions, {}, {}); + QWindowSystemInterface::handleDrag(window, &mimeData, posInsideLabel, supportedActions, {}, {}); + QWindowSystemInterface::handleDrop(window, &mimeData, posInsideLabel, supportedActions, {}, {}); + + QGuiApplication::processEvents(); +} + #endif void tst_QWidget_window::tst_qtbug35600() @@ -938,7 +1096,7 @@ class ResizeWidget : public QWidget { Q_OBJECT public: - ResizeWidget(QWidget *parent = 0) + ResizeWidget(QWidget *parent = nullptr) : QWidget(parent) , resizeCount(0) { } @@ -956,11 +1114,9 @@ void tst_QWidget_window::tst_resize_count() { { ResizeWidget resize; + resize.setWindowFlags(Qt::X11BypassWindowManagerHint); resize.show(); QVERIFY(QTest::qWaitForWindowExposed(&resize)); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "Winrt does not support resize", Abort); -#endif QCOMPARE(resize.resizeCount, 1); resize.resizeCount = 0; QSize size = resize.size(); @@ -988,6 +1144,7 @@ void tst_QWidget_window::tst_resize_count() } { ResizeWidget parent; + parent.setWindowFlag(Qt::X11BypassWindowManagerHint); ResizeWidget child(&parent); child.resize(m_testWidgetSize); child.winId(); @@ -1007,11 +1164,83 @@ void tst_QWidget_window::tst_resize_count() } +/*! + This test verifies that windows get a balanced number of show + and hide events, no matter how the window was closed. +*/ +void tst_QWidget_window::tst_showhide_count() +{ + class EventSpy : public QObject + { + public: + EventSpy() + { + QApplication::instance()->installEventFilter(this); + } + + int takeCount(QWidget *widget, QEvent::Type type) { + const auto entry = Entry(widget, type); + int count = counter[entry]; + counter[entry] = 0; + return count; + } + protected: + bool eventFilter(QObject *receiver, QEvent *event) override + { + if (QWidget *widget = qobject_cast<QWidget*>(receiver)) { + const auto entry = Entry(widget, event->type()); + ++counter[entry]; + return false; + } + return QObject::eventFilter(receiver, event); + } + private: + using Entry = QPair<QWidget*, QEvent::Type>; + QHash<Entry, int> counter; + }; + + EventSpy spy; + + QWidget w1; + w1.setGeometry(100, 100, 200, 200); + + w1.show(); + QCOMPARE(spy.takeCount(&w1, QEvent::Show), 1); + w1.hide(); + QCOMPARE(spy.takeCount(&w1, QEvent::Hide), 1); + w1.close(); + QCOMPARE(spy.takeCount(&w1, QEvent::Close), 1); + w1.show(); + QCOMPARE(spy.takeCount(&w1, QEvent::Show), 1); + w1.close(); + QCOMPARE(spy.takeCount(&w1, QEvent::Hide), 1); + QCOMPARE(spy.takeCount(&w1, QEvent::Close), 1); + + w1.show(); + QWidget *popup = new QWidget(&w1, Qt::Popup); + popup->setGeometry(120, 120, 30, 30); + popup->show(); + popup->close(); + QCOMPARE(spy.takeCount(popup, QEvent::Show), 1); + QCOMPARE(spy.takeCount(popup, QEvent::Hide), 1); + QCOMPARE(spy.takeCount(popup, QEvent::Close), 1); + + popup->show(); + + // clicking outside the popup should close the popup + QTest::mousePress(popup->window(), Qt::LeftButton, {}, QPoint(-10, -10)); + + QCOMPARE(spy.takeCount(popup, QEvent::Show), 1); + QCOMPARE(spy.takeCount(popup, QEvent::Hide), 1); + QCOMPARE(spy.takeCount(popup, QEvent::Close), 1); +} + + class MoveWidget : public QWidget { Q_OBJECT public: - MoveWidget(QWidget *parent = 0) + MoveWidget(QWidget *parent = nullptr) : QWidget(parent) , moveCount(0) { } @@ -1066,6 +1295,9 @@ protected: void tst_QWidget_window::tst_eventfilter_on_toplevel() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QWidget w; EventFilter filter; w.installEventFilter(&filter); @@ -1124,6 +1356,9 @@ void tst_QWidget_window::QTBUG_50561_QCocoaBackingStore_paintDevice_crash() void tst_QWidget_window::setWindowState_data() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QString platformName = QGuiApplication::platformName().toLower(); QTest::addColumn<Qt::WindowStates>("state"); @@ -1159,11 +1394,6 @@ void tst_QWidget_window::setWindowState() w.setWindowState(state); QCOMPARE(w.windowState(), state); w.show(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("0", "Winrt windows are maximized by default", Abort); - QEXPECT_FAIL("Qt::WindowMinimized", "Winrt windows are maximized by default", Abort); - QEXPECT_FAIL("Qt::WindowFullScreen", "Winrt windows are maximized by default", Abort); -#endif QCOMPARE(w.windowState(), state); QCOMPARE(w.windowHandle()->windowStates(), state); if (!(state & Qt::WindowMinimized)) @@ -1222,5 +1452,402 @@ void tst_QWidget_window::QTBUG_56277_resize_on_showEvent() QVERIFY(geometry.top() > topmostY || geometry.left() > screen->availableGeometry().left()); } +void tst_QWidget_window::mouseMoveWithPopup_data() +{ + QTest::addColumn<Qt::WindowType>("windowType"); + + QTest::addRow("Dialog") << Qt::Dialog; + QTest::addRow("Popup") << Qt::Popup; +} + +void tst_QWidget_window::mouseMoveWithPopup() +{ + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: Skip this test, see also QTBUG-107154"); + + QFETCH(Qt::WindowType, windowType); + + class Window : public QWidget + { + public: + Window(QWidget *parent = nullptr, Qt::WindowFlags flags = {}) + : QWidget(parent, flags|Qt::CustomizeWindowHint|Qt::FramelessWindowHint) + {} + + QSize sizeHint() const override + { + if (parent()) + return QSize(150, 100); + return QSize(250, 250); + } + + Window *popup = nullptr; + Qt::WindowType type = Qt::Popup; + int mousePressCount = 0; + int mouseMoveCount = 0; + int mouseReleaseCount = 0; + void resetCounters() + { + mousePressCount = 0; + mouseMoveCount = 0; + mouseReleaseCount = 0; + } + protected: + void mousePressEvent(QMouseEvent *event) override + { + ++mousePressCount; + + if (event->button() == Qt::RightButton) { + if (!popup) + popup = new Window(this, type); + popup->move(event->globalPosition().toPoint()); + popup->show(); + if (!QTest::qWaitForWindowExposed(popup)) { + delete popup; + popup = nullptr; + QSKIP("Failed to expose popup window!"); + } + } else { + QWidget::mousePressEvent(event); + } + } + void mouseReleaseEvent(QMouseEvent *event) override + { + ++mouseReleaseCount; + QWidget::mouseReleaseEvent(event); + } + void mouseMoveEvent(QMouseEvent *event) override + { + ++mouseMoveCount; + QWidget::mouseMoveEvent(event); + } + }; + Window topLevel; + topLevel.setObjectName("topLevel"); + topLevel.type = windowType; + topLevel.show(); + if (!QTest::qWaitForWindowExposed(&topLevel)) + QSKIP("Failed to expose window!"); + + QCOMPARE(QApplication::activePopupWidget(), nullptr); + QTRY_COMPARE(QApplication::activeWindow(), &topLevel); + + QPoint mousePos = topLevel.geometry().center(); + QWindow *window = nullptr; + Qt::MouseButtons buttons = {}; + auto mouseAction = [&](Qt::MouseButton button, QPoint offset = {}) -> QEvent::Type + { + QEvent::Type type; + if (offset != QPoint()) { + type = QEvent::MouseMove; + } else if (buttons & button) { + type = QEvent::MouseButtonRelease; + buttons &= ~button; + } else { + Q_ASSERT(button != Qt::NoButton); + type = QEvent::MouseButtonPress; + buttons |= button; + window = QApplication::activeWindow()->windowHandle(); + } + + mousePos += offset; + + if (!window) + return QEvent::None; + + bool result = QWindowSystemInterface::handleMouseEvent(window, window->mapFromGlobal(mousePos), + mousePos, buttons, button, type); + QCoreApplication::processEvents(); + if (type == QEvent::MouseButtonRelease && buttons == Qt::NoButton) + window = nullptr; + + if (!result) + return QEvent::None; + return type; + }; + + QCOMPARE(mouseAction(Qt::RightButton), QEvent::MouseButtonPress); + QCOMPARE(topLevel.mousePressCount, 1); + QVERIFY(topLevel.popup); + QCOMPARE(topLevel.popup->mousePressCount, 0); + topLevel.popup->setObjectName(windowType == Qt::Popup ? "Popup" : "Dialog"); + QCOMPARE(QApplication::activePopupWidget(), windowType == Qt::Popup ? topLevel.popup : nullptr); + // if popup, then popup gets the mouse move even though it didn't get any press + QCOMPARE(mouseAction(Qt::NoButton, QPoint(10, 10)), QEvent::MouseMove); + QCOMPARE(topLevel.mouseMoveCount, windowType == Qt::Popup ? 0 : 1); + QCOMPARE(topLevel.popup->mouseMoveCount, windowType == Qt::Popup ? 1 : 0); + // if popup, then popup gets the release even though it didn't get any press + QCOMPARE(mouseAction(Qt::RightButton), QEvent::MouseButtonRelease); + QCOMPARE(topLevel.mouseReleaseCount, windowType == Qt::Popup ? 0 : 1); + QCOMPARE(topLevel.popup->mouseReleaseCount, windowType == Qt::Popup ? 1 : 0); + + Q_ASSERT(buttons == Qt::NoButton); + topLevel.resetCounters(); + topLevel.popup->resetCounters(); + + QTRY_VERIFY(QApplication::activeWindow() == topLevel.popup + || QApplication::activePopupWidget() == topLevel.popup); + + // nested popup, same procedure + QCOMPARE(mouseAction(Qt::RightButton), QEvent::MouseButtonPress); + QVERIFY(topLevel.popup); + QCOMPARE(topLevel.popup->mousePressCount, 1); + QVERIFY(topLevel.popup->popup); + topLevel.popup->popup->setObjectName("NestedPopup"); + QCOMPARE(QApplication::activePopupWidget(), topLevel.popup->popup); + QCOMPARE(topLevel.popup->popup->mousePressCount, 0); + + // nested popup is always a popup and grabs the mouse, so first popup gets nothing + QCOMPARE(mouseAction(Qt::NoButton, QPoint(10, 10)), QEvent::MouseMove); + QCOMPARE(topLevel.popup->mouseMoveCount, 0); + QCOMPARE(topLevel.popup->popup->mouseMoveCount, 1); + + // nested popup gets the release, as before + QCOMPARE(mouseAction(Qt::RightButton), QEvent::MouseButtonRelease); + QCOMPARE(topLevel.popup->mouseReleaseCount, 0); + QCOMPARE(topLevel.popup->popup->mouseReleaseCount, 1); + + Q_ASSERT(buttons == Qt::NoButton); + + // move mouse back into first popup + mouseAction({}, QPoint(-15, -15)); + QVERIFY(!topLevel.popup->popup->geometry().contains(mousePos)); + QVERIFY(topLevel.popup->geometry().contains(mousePos)); + + topLevel.popup->resetCounters(); + topLevel.popup->popup->resetCounters(); + + // closing the nested popup by clicking into the first popup/dialog; the nested popup gets the press + QCOMPARE(mouseAction(Qt::LeftButton), QEvent::MouseButtonPress); + QCOMPARE(topLevel.popup->popup->mousePressCount, 1); + QVERIFY(!topLevel.popup->popup->isVisible()); + QCOMPARE(QApplication::activePopupWidget(), windowType == Qt::Popup ? topLevel.popup : nullptr); + QCOMPARE(QApplication::activeWindow(), windowType == Qt::Popup ? &topLevel : topLevel.popup); + + // the move event following a press that closed the active popup should NOT be delivered to the first popup + QCOMPARE(mouseAction({}, QPoint(-10, -10)), QEvent::MouseMove); + // dialogs might or might not get the event - platform specific behavior in Qt 5 + if (topLevel.popup->mouseMoveCount != 0) + QEXPECT_FAIL("Dialog", "Platform specific behavior", Continue); + QCOMPARE(topLevel.popup->mouseMoveCount, 0); + QCOMPARE(topLevel.popup->popup->mouseMoveCount, 0); + + // but the release event will still be delivered to the first popup - dialogs might not get it + QCOMPARE(mouseAction(Qt::LeftButton), QEvent::MouseButtonRelease); + if (topLevel.popup->mouseReleaseCount != 1 + && !QGuiApplication::platformName().startsWith(QLatin1String("windows"), Qt::CaseInsensitive)) + QEXPECT_FAIL("Dialog", "Platform specific behavior", Continue); + QCOMPARE(topLevel.popup->mouseReleaseCount, 1); +} + +struct ShowHideEntry { + QEvent::Type action; + Qt::WindowType target; + using List = QList<ShowHideEntry>; +}; + +void tst_QWidget_window::showHideWindowHandle_data() +{ + QTest::addColumn<ShowHideEntry::List>("entries"); + + QTest::addRow("show/hide widget") << ShowHideEntry::List{ + { QEvent::Show, Qt::Widget }, { QEvent::Hide, Qt::Widget } + }; + QTest::addRow("show/hide window") << ShowHideEntry::List{ + { QEvent::Show, Qt::Window }, { QEvent::Hide, Qt::Window } + }; + QTest::addRow("show widget, hide window") << ShowHideEntry::List{ + { QEvent::Show, Qt::Widget }, { QEvent::Hide, Qt::Window } + }; + QTest::addRow("show window, hide widget") << ShowHideEntry::List{ + { QEvent::Show, Qt::Window }, { QEvent::Hide, Qt::Widget } + }; + QTest::addRow("show/hide widget, then show window, hide widget") << ShowHideEntry::List{ + { QEvent::Show, Qt::Widget }, { QEvent::Hide, Qt::Widget }, + { QEvent::Show, Qt::Window }, { QEvent::Hide, Qt::Widget } + }; + QTest::addRow("show widget, close widget, show widget") << ShowHideEntry::List{ + { QEvent::Show, Qt::Widget }, { QEvent::Close, Qt::Widget }, { QEvent::Show, Qt::Widget } + }; + QTest::addRow("show widget, close widget, show window") << ShowHideEntry::List{ + { QEvent::Show, Qt::Widget }, { QEvent::Close, Qt::Widget }, { QEvent::Show, Qt::Window } + }; + QTest::addRow("show widget, close window, show widget") << ShowHideEntry::List{ + { QEvent::Show, Qt::Widget }, { QEvent::Close, Qt::Window }, { QEvent::Show, Qt::Widget } + }; + QTest::addRow("show widget, close window, show window") << ShowHideEntry::List{ + { QEvent::Show, Qt::Widget }, { QEvent::Close, Qt::Window }, { QEvent::Show, Qt::Window } + }; +} + +void tst_QWidget_window::showHideWindowHandle() +{ + QWidget parent; + parent.setObjectName("Parent"); + QCOMPARE(parent.isVisible(), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Hidden), true); + + QWidget child; + child.setObjectName("Child"); + QCOMPARE(child.isVisible(), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), true); + + child.setParent(&parent); + QCOMPARE(child.isVisible(), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + + QFETCH(QList<ShowHideEntry>, entries); + for (const auto entry : entries) { + + if (entry.action == QEvent::Show) { + if (entry.target == Qt::Window && !parent.windowHandle()) { + parent.setAttribute(Qt::WA_NativeWindow); + QVERIFY(parent.windowHandle()); + + QCOMPARE(parent.isVisible(), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Hidden), true); + } + + bool wasExplicitShowHide = parent.testAttribute(Qt::WA_WState_ExplicitShowHide); + + if (entry.target == Qt::Widget) + parent.show(); + else + parent.windowHandle()->show(); + + QVERIFY(QTest::qWaitForWindowActive(&parent)); + + QCOMPARE(parent.isVisible(), true); + QVERIFY(parent.windowHandle()); + QCOMPARE(parent.windowHandle()->isVisible(), true); + + QCOMPARE(parent.testAttribute(Qt::WA_WState_Visible), true); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Hidden), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_ExplicitShowHide), + entry.target == Qt::Widget || wasExplicitShowHide); + + QCOMPARE(child.isVisible(), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Visible), true); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + + } else if (entry.action == QEvent::Hide) { + + bool wasExplicitShowHide = parent.testAttribute(Qt::WA_WState_ExplicitShowHide); + + if (entry.target == Qt::Widget) + parent.hide(); + else + parent.windowHandle()->hide(); + + QCOMPARE(parent.isVisible(), false); + QVERIFY(parent.windowHandle()); + QCOMPARE(parent.windowHandle()->isVisible(), false); + + QCOMPARE(parent.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Hidden), true); + QCOMPARE(parent.testAttribute(Qt::WA_WState_ExplicitShowHide), + entry.target == Qt::Widget || wasExplicitShowHide); + + QCOMPARE(child.isVisible(), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + + } else if (entry.action == QEvent::Close) { + + bool wasExplicitShowHide = parent.testAttribute(Qt::WA_WState_ExplicitShowHide); + + if (entry.target == Qt::Widget) + parent.close(); + else + parent.windowHandle()->close(); + + QCOMPARE(parent.isVisible(), false); + QVERIFY(parent.windowHandle()); + QCOMPARE(parent.windowHandle()->isVisible(), false); + + QCOMPARE(parent.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(parent.testAttribute(Qt::WA_WState_Hidden), true); + QCOMPARE(parent.testAttribute(Qt::WA_WState_ExplicitShowHide), + entry.target == Qt::Widget || wasExplicitShowHide); + + QCOMPARE(child.isVisible(), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_ExplicitShowHide), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Visible), false); + QCOMPARE(child.testAttribute(Qt::WA_WState_Hidden), false); + } + } +} + +void tst_QWidget_window::resetFocusObjectOnDestruction() +{ + if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::WindowActivation)) + QSKIP("QWindow::requestActivate() is not supported."); + + QSignalSpy focusObjectChangedSpy(qApp, &QGuiApplication::focusObjectChanged); + + // single top level widget that has focus + std::unique_ptr<QWidget> widget(new QWidget); + widget->setObjectName("Widget 1"); + widget->setFocus(); + widget->show(); + QVERIFY(QTest::qWaitForWindowActive(widget.get())); + + int activeCount = focusObjectChangedSpy.size(); + widget.reset(); + QVERIFY(focusObjectChangedSpy.size() > activeCount); + QCOMPARE(focusObjectChangedSpy.last().last().value<QObject*>(), nullptr); + focusObjectChangedSpy.clear(); + + // top level widget with focused child + widget.reset(new QWidget); + widget->setObjectName("Widget 2"); + QWidget *child = new QWidget(widget.get()); + child->setObjectName("Child widget"); + child->setFocus(); + widget->show(); + QVERIFY(QTest::qWaitForWindowActive(widget.get())); + + activeCount = focusObjectChangedSpy.size(); + widget.reset(); + // we might get more than one signal emission + QVERIFY(focusObjectChangedSpy.size() > activeCount); + QCOMPARE(focusObjectChangedSpy.last().last().value<QObject*>(), nullptr); +} + +class CreateDestroyWidget : public QWidget +{ +public: + using QWidget::create; + using QWidget::destroy; +}; + +void tst_QWidget_window::cleanupOnDestruction() +{ + CreateDestroyWidget widget; + QWidget child(&widget); + + QWidget grandChild(&child); + // Ensure there's not a 1:1 native window hierarhcy that we could + // recurse during QWidget::destroy(), triggering the issue that + // we were failing to clean up when not destroyed via QWidget. + grandChild.setAttribute(Qt::WA_DontCreateNativeAncestors); + grandChild.winId(); + + widget.destroy(); + widget.create(); + + widget.show(); +} + QTEST_MAIN(tst_QWidget_window) #include "tst_qwidget_window.moc" diff --git a/tests/auto/widgets/kernel/qwidgetaction/CMakeLists.txt b/tests/auto/widgets/kernel/qwidgetaction/CMakeLists.txt new file mode 100644 index 0000000000..fb5409464d --- /dev/null +++ b/tests/auto/widgets/kernel/qwidgetaction/CMakeLists.txt @@ -0,0 +1,21 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qwidgetaction Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qwidgetaction LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qwidgetaction + SOURCES + tst_qwidgetaction.cpp + LIBRARIES + Qt::Gui + Qt::TestPrivate + Qt::Widgets +) diff --git a/tests/auto/widgets/kernel/qwidgetaction/qwidgetaction.pro b/tests/auto/widgets/kernel/qwidgetaction/qwidgetaction.pro deleted file mode 100644 index f443758eea..0000000000 --- a/tests/auto/widgets/kernel/qwidgetaction/qwidgetaction.pro +++ /dev/null @@ -1,6 +0,0 @@ -CONFIG += testcase -TARGET = tst_qwidgetaction -QT += widgets testlib testlib-private -SOURCES += tst_qwidgetaction.cpp - - diff --git a/tests/auto/widgets/kernel/qwidgetaction/tst_qwidgetaction.cpp b/tests/auto/widgets/kernel/qwidgetaction/tst_qwidgetaction.cpp index 28731223a9..a06e072b71 100644 --- a/tests/auto/widgets/kernel/qwidgetaction/tst_qwidgetaction.cpp +++ b/tests/auto/widgets/kernel/qwidgetaction/tst_qwidgetaction.cpp @@ -1,33 +1,9 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> +#include <QTimer> #include <qapplication.h> #include <qtoolbar.h> @@ -73,6 +49,18 @@ void tst_QWidgetAction::cleanup() void tst_QWidgetAction::defaultWidget() { + // check that QWidgetAction deals with the widget being deleted before itself: + { + QToolBar tb1; + + QComboBox combo(&tb1); + + auto action = new QWidgetAction(&tb1); + action->setDefaultWidget(&combo); + + tb1.addAction(action); + } + // check that QWidgetAction takes ownership of the widget: { QToolBar tb1; @@ -212,7 +200,7 @@ public: QList<QWidget *> createdWidgets() const { return QWidgetAction::createdWidgets(); } protected: - virtual QWidget *createWidget(QWidget *parent); + virtual QWidget *createWidget(QWidget *parent) override; }; QWidget *ComboAction::createWidget(QWidget *parent) @@ -234,7 +222,7 @@ void tst_QWidgetAction::customWidget() tb1.addAction(action); QList<QWidget *> combos = action->createdWidgets(); - QCOMPARE(combos.count(), 1); + QCOMPARE(combos.size(), 1); QPointer<QComboBox> combo1 = qobject_cast<QComboBox *>(combos.at(0)); QVERIFY(combo1); @@ -242,7 +230,7 @@ void tst_QWidgetAction::customWidget() tb2.addAction(action); combos = action->createdWidgets(); - QCOMPARE(combos.count(), 2); + QCOMPARE(combos.size(), 2); QCOMPARE(combos.at(0), combo1.data()); QPointer<QComboBox> combo2 = qobject_cast<QComboBox *>(combos.at(1)); @@ -396,7 +384,7 @@ class CrashedAction : public QWidgetAction public: inline CrashedAction(QObject *parent) : QWidgetAction(parent) { } - virtual QWidget *createWidget(QWidget *parent) { + virtual QWidget *createWidget(QWidget *parent) override { return new QWidget(parent); } }; diff --git a/tests/auto/widgets/kernel/qwidgetmetatype/CMakeLists.txt b/tests/auto/widgets/kernel/qwidgetmetatype/CMakeLists.txt new file mode 100644 index 0000000000..431a584a60 --- /dev/null +++ b/tests/auto/widgets/kernel/qwidgetmetatype/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qwidgetmetatype Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qwidgetmetatype LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qwidgetmetatype + SOURCES + tst_qwidgetmetatype.cpp + LIBRARIES + Qt::Gui + Qt::Widgets +) diff --git a/tests/auto/widgets/kernel/qwidgetmetatype/qwidgetmetatype.pro b/tests/auto/widgets/kernel/qwidgetmetatype/qwidgetmetatype.pro deleted file mode 100644 index 55228e6a1e..0000000000 --- a/tests/auto/widgets/kernel/qwidgetmetatype/qwidgetmetatype.pro +++ /dev/null @@ -1,4 +0,0 @@ -CONFIG += testcase -TARGET = tst_qwidgetmetatype -QT += widgets testlib -SOURCES += tst_qwidgetmetatype.cpp diff --git a/tests/auto/widgets/kernel/qwidgetmetatype/tst_qwidgetmetatype.cpp b/tests/auto/widgets/kernel/qwidgetmetatype/tst_qwidgetmetatype.cpp index 077e8de328..885c26a128 100644 --- a/tests/auto/widgets/kernel/qwidgetmetatype/tst_qwidgetmetatype.cpp +++ b/tests/auto/widgets/kernel/qwidgetmetatype/tst_qwidgetmetatype.cpp @@ -1,33 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <qwidget.h> #include <qlabel.h> @@ -41,32 +16,79 @@ public: private slots: void metaObject(); + void saveAndLoadBuiltin_data(); + void saveAndLoadBuiltin(); }; class CustomWidget : public QWidget { Q_OBJECT public: - CustomWidget(QWidget *parent = 0) + CustomWidget(QWidget *parent = nullptr) : QWidget(parent) { } }; -Q_STATIC_ASSERT(( QMetaTypeId2<QSizePolicy>::IsBuiltIn)); -Q_STATIC_ASSERT((!QMetaTypeId2<QWidget*>::IsBuiltIn)); -Q_STATIC_ASSERT((!QMetaTypeId2<QList<QSizePolicy> >::IsBuiltIn)); -Q_STATIC_ASSERT((!QMetaTypeId2<QMap<QString,QSizePolicy> >::IsBuiltIn)); +static_assert(( QMetaTypeId2<QSizePolicy>::IsBuiltIn)); +static_assert((!QMetaTypeId2<QWidget*>::IsBuiltIn)); +static_assert((!QMetaTypeId2<QList<QSizePolicy> >::IsBuiltIn)); +static_assert((!QMetaTypeId2<QMap<QString,QSizePolicy> >::IsBuiltIn)); void tst_QWidgetMetaType::metaObject() { - QCOMPARE(QMetaType::metaObjectForType(qMetaTypeId<QWidget*>()), &QWidget::staticMetaObject); - QCOMPARE(QMetaType::metaObjectForType(qMetaTypeId<QLabel*>()), &QLabel::staticMetaObject); - QCOMPARE(QMetaType::metaObjectForType(qMetaTypeId<CustomWidget*>()), &CustomWidget::staticMetaObject); - QCOMPARE(QMetaType::metaObjectForType(qMetaTypeId<QSizePolicy>()), &QSizePolicy::staticMetaObject); + QCOMPARE(QMetaType::fromType<QWidget*>().metaObject(), &QWidget::staticMetaObject); + QCOMPARE(QMetaType::fromType<QLabel*>().metaObject(), &QLabel::staticMetaObject); + QCOMPARE(QMetaType::fromType<CustomWidget*>().metaObject(), &CustomWidget::staticMetaObject); + QCOMPARE(QMetaType::fromType<QSizePolicy>().metaObject(), &QSizePolicy::staticMetaObject); +} + +template <typename T> +struct StreamingTraits +{ + // Streamable by default, as currently all widgets built-in types are streamable + enum { isStreamable = 1 }; +}; + +void tst_QWidgetMetaType::saveAndLoadBuiltin_data() +{ + QTest::addColumn<int>("type"); + QTest::addColumn<bool>("isStreamable"); + +#define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ + QTest::newRow(#RealType) << MetaTypeId << bool(StreamingTraits<RealType>::isStreamable); + QT_FOR_EACH_STATIC_WIDGETS_CLASS(ADD_METATYPE_TEST_ROW) +#undef ADD_METATYPE_TEST_ROW } +void tst_QWidgetMetaType::saveAndLoadBuiltin() +{ + QFETCH(int, type); + QFETCH(bool, isStreamable); + + void *value = QMetaType(type).create(); + + QByteArray ba; + QDataStream stream(&ba, QIODevice::ReadWrite); + QCOMPARE(QMetaType(type).save(stream, value), isStreamable); + QCOMPARE(stream.status(), QDataStream::Ok); + + if (isStreamable) + QVERIFY(QMetaType(type).load(stream, value)); + + stream.device()->seek(0); + stream.resetStatus(); + QCOMPARE(QMetaType(type).load(stream, value), isStreamable); + QCOMPARE(stream.status(), QDataStream::Ok); + + if (isStreamable) + QVERIFY(QMetaType(type).load(stream, value)); + + QMetaType(type).destroy(value); +} + + QTEST_MAIN(tst_QWidgetMetaType) #include "tst_qwidgetmetatype.moc" diff --git a/tests/auto/widgets/kernel/qwidgetrepaintmanager/CMakeLists.txt b/tests/auto/widgets/kernel/qwidgetrepaintmanager/CMakeLists.txt new file mode 100644 index 0000000000..ae91af064c --- /dev/null +++ b/tests/auto/widgets/kernel/qwidgetrepaintmanager/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qwidgetrepaintmanager LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qwidgetrepaintmanager + SOURCES + tst_qwidgetrepaintmanager.cpp + LIBRARIES + Qt::CorePrivate + Qt::Gui + Qt::GuiPrivate + Qt::TestPrivate + Qt::Widgets + Qt::WidgetsPrivate +) diff --git a/tests/auto/widgets/kernel/qwidgetrepaintmanager/tst_qwidgetrepaintmanager.cpp b/tests/auto/widgets/kernel/qwidgetrepaintmanager/tst_qwidgetrepaintmanager.cpp new file mode 100644 index 0000000000..9059a9262e --- /dev/null +++ b/tests/auto/widgets/kernel/qwidgetrepaintmanager/tst_qwidgetrepaintmanager.cpp @@ -0,0 +1,831 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> +#include <QPainter> +#include <QScrollArea> +#include <QScrollBar> +#include <QApplication> + +#include <private/qhighdpiscaling_p.h> +#include <private/qwidget_p.h> +#include <private/qwidgetrepaintmanager_p.h> +#include <qpa/qplatformintegration.h> +#include <qpa/qplatformbackingstore.h> +#include <private/qguiapplication_p.h> + +//#define MANUAL_DEBUG + +class TestWidget : public QWidget +{ +public: + TestWidget(QWidget *parent = nullptr) + : QWidget(parent) + { + } + + QSize sizeHint() const override + { + const int screenWidth = QGuiApplication::primaryScreen()->geometry().width(); + const int width = qMax(200, 100 * ((screenWidth + 500) / 1000)); + return isWindow() ? QSize(width, width) : QSize(width - 40, width - 40); + } + + void initialShow() + { + show(); + if (isWindow()) { + QVERIFY(QTest::qWaitForWindowExposed(this)); + QVERIFY(waitForPainted()); + } + paintedRegions = {}; + } + + bool waitForPainted(int timeout = 5000) + { + int remaining = timeout; + QDeadlineTimer deadline(remaining, Qt::PreciseTimer); + if (!QTest::qWaitFor([this]{ return !paintedRegions.isEmpty(); }, timeout)) + return false; + + // In case of multiple paint events: + // Process events and wait until all have been consumed, + // i.e. paintedRegions no longer changes. + QRegion reg; + while (remaining > 0 && reg != paintedRegions) { + reg = paintedRegions; + QCoreApplication::processEvents(QEventLoop::AllEvents, remaining); + if (reg == paintedRegions) + return true; + + remaining = int(deadline.remainingTime()); + } + return false; + } + + QRegion takePaintedRegions() + { + QRegion result = paintedRegions; + paintedRegions = {}; + return result; + } + QRegion paintedRegions; + + bool event(QEvent *event) override + { + const auto type = event->type(); + if (type == QEvent::WindowActivate || type == QEvent::WindowDeactivate) + return true; + if (type == QEvent::UpdateRequest) + ++updateRequests; + return QWidget::event(event); + } + int updateRequests = 0; + +protected: + void paintEvent(QPaintEvent *event) override + { + paintedRegions += event->region(); + QPainter painter(this); + const QBrush patternBrush = isWindow() ? QBrush(Qt::blue, Qt::VerPattern) + : QBrush(Qt::red, Qt::HorPattern); + painter.fillRect(rect(), patternBrush); + } +}; + +class OpaqueWidget : public QWidget +{ +public: + OpaqueWidget(const QColor &col, QWidget *parent = nullptr) + : QWidget(parent), fillColor(col) + { + setAttribute(Qt::WA_OpaquePaintEvent); + } + + bool event(QEvent *event) override + { + const auto type = event->type(); + if (type == QEvent::WindowActivate || type == QEvent::WindowDeactivate) + return true; + return QWidget::event(event); + } + +protected: + void paintEvent(QPaintEvent *e) override + { + Q_UNUSED(e); + QPainter painter(this); + fillColor.setBlue(paintCount % 255); + painter.fillRect(e->rect(), fillColor); +#ifdef MANUAL_DEBUG + ++paintCount; + painter.drawText(rect(), Qt::AlignCenter, QString::number(paintCount)); +#endif + } + +private: + QColor fillColor; + int paintCount = 0; +}; + +class Draggable : public OpaqueWidget +{ +public: + Draggable(QWidget *parent = nullptr) + : OpaqueWidget(Qt::white, parent) + { + } + + Draggable(const QColor &col, QWidget *parent = nullptr) + : OpaqueWidget(col, parent) + { + left = new OpaqueWidget(Qt::gray, this); + top = new OpaqueWidget(Qt::gray, this); + right = new OpaqueWidget(Qt::gray, this); + bottom = new OpaqueWidget(Qt::gray, this); + } + + QSize sizeHint() const override { + return QSize(100, 100); + } + +protected: + void resizeEvent(QResizeEvent *) override + { + if (!left) + return; + left->setGeometry(0, 0, 10, height()); + top->setGeometry(10, 0, width() - 10, 10); + right->setGeometry(width() - 10, 10, 10, height() - 10); + bottom->setGeometry(10, height() - 10, width() - 10, 10); + } + + void mousePressEvent(QMouseEvent *e) override + { + lastPos = e->position().toPoint(); + } + void mouseMoveEvent(QMouseEvent *e) override + { + QPoint pos = geometry().topLeft(); + pos += e->position().toPoint() - lastPos; + move(pos); + } + void mouseReleaseEvent(QMouseEvent *) override + { + lastPos = {}; + } + +private: + OpaqueWidget *left = nullptr; + OpaqueWidget *top = nullptr; + OpaqueWidget *right = nullptr; + OpaqueWidget *bottom = nullptr; + QPoint lastPos; +}; + +class TestScene : public QWidget +{ +public: + TestScene() + { + setObjectName("scene"); + + // opaque because it has an opaque background color and autoFillBackground is set + area = new QWidget(this); + area->setObjectName("area"); + area->setAutoFillBackground(true); + QPalette palette; + palette.setColor(QPalette::Window, QColor::fromRgb(0, 0, 0)); + area->setPalette(palette); + + // all these children set WA_OpaquePaintEvent + redChild = new Draggable(Qt::red, area); + redChild->setObjectName("redChild"); + + greenChild = new Draggable(Qt::green, area); + greenChild->setObjectName("greenChild"); + + yellowChild = new Draggable(Qt::yellow, this); + yellowChild->setObjectName("yellowChild"); + + nakedChild = new Draggable(this); + nakedChild->move(300, 0); + nakedChild->setObjectName("nakedChild"); + + bar = new OpaqueWidget(Qt::darkGray, this); + bar->setObjectName("bar"); + } + + QWidget *area; + QWidget *redChild; + QWidget *greenChild; + QWidget *yellowChild; + QWidget *nakedChild; + QWidget *bar; + + QSize sizeHint() const override { return QSize(400, 400); } + + bool event(QEvent *event) override + { + const auto type = event->type(); + if (type == QEvent::WindowActivate || type == QEvent::WindowDeactivate) + return true; + return QWidget::event(event); + } + +protected: + void resizeEvent(QResizeEvent *) override + { + area->setGeometry(50, 50, width() - 100, height() - 100); + bar->setGeometry(width() / 2 - 25, height() / 2, 50, height() / 2); + } +}; + +class tst_QWidgetRepaintManager : public QObject +{ + Q_OBJECT + +public: + tst_QWidgetRepaintManager(); + +public slots: + void initTestCase(); + void cleanup(); + +private slots: + void basic(); + void children(); + void opaqueChildren(); + void staticContents(); + void scroll(); + void paintOnScreenUpdates(); + +#if defined(QT_BUILD_INTERNAL) + void scrollWithOverlap(); + void overlappedRegion(); + void fastMove(); + void moveAccross(); + void moveInOutOverlapped(); + +protected: + /* + This helper compares the widget as rendered into the backingstore with the widget + as rendered via QWidget::grab. The latter always produces a fully rendered image, + so differences indicate bugs in QWidgetRepaintManager's or QWidget's painting code. + */ + bool compareWidget(QWidget *w) + { + QBackingStore *backingStore = w->window()->backingStore(); + Q_ASSERT(backingStore && backingStore->handle()); + QPlatformBackingStore *platformBackingStore = backingStore->handle(); + + if (!waitForFlush(w)) { + qWarning() << "Widget" << w << "failed to flush"; + return false; + } + + QImage backingstoreContent = platformBackingStore->toImage(); + if (!w->isWindow()) { + const qreal dpr = w->devicePixelRatioF(); + const QPointF offset = w->mapTo(w->window(), QPointF(0, 0)) * dpr; + backingstoreContent = backingstoreContent.copy(offset.x(), offset.y(), w->width() * dpr, w->height() * dpr); + } + const QImage widgetRender = w->grab().toImage().convertToFormat(backingstoreContent.format()); + + const bool result = backingstoreContent == widgetRender; + +#ifdef MANUAL_DEBUG + if (!result) { + backingstoreContent.save(QString("/tmp/backingstore_%1_%2.png").arg(QTest::currentTestFunction(), QTest::currentDataTag())); + widgetRender.save(QString("/tmp/grab_%1_%2.png").arg(QTest::currentTestFunction(), QTest::currentDataTag())); + } +#endif + return result; + }; + + QRegion dirtyRegion(QWidget *widget) const + { + return QWidgetPrivate::get(widget)->dirty; + } + bool waitForFlush(QWidget *widget) const + { + if (!widget) + return true; + + auto *repaintManager = QWidgetPrivate::get(widget->window())->maybeRepaintManager(); + + if (!repaintManager) + return true; + + return QTest::qWaitFor([repaintManager]{ return !repaintManager->isDirty(); } ); + }; +#endif // QT_BUILD_INTERNAL + + +private: + const int m_fuzz; + bool m_implementsScroll = false; +}; + +tst_QWidgetRepaintManager::tst_QWidgetRepaintManager() : + m_fuzz(int(QHighDpiScaling::factor(QGuiApplication::primaryScreen()))) +{ +} + +void tst_QWidgetRepaintManager::initTestCase() +{ + QWidget widget; + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + + m_implementsScroll = widget.backingStore()->handle()->scroll(QRegion(widget.rect()), 1, 1); + qInfo() << QGuiApplication::platformName() << "QPA backend implements scroll:" << m_implementsScroll; +} + +void tst_QWidgetRepaintManager::cleanup() +{ + QVERIFY(QApplication::topLevelWidgets().isEmpty()); +} + +void tst_QWidgetRepaintManager::basic() +{ + TestWidget widget; + widget.show(); + QVERIFY(QTest::qWaitForWindowExposed(&widget)); + + QCOMPARE(widget.takePaintedRegions(), QRegion(0, 0, widget.width(), widget.height())); + + widget.update(); + QVERIFY(widget.waitForPainted()); + QCOMPARE(widget.takePaintedRegions(), QRegion(0, 0, widget.width(), widget.height())); + + widget.repaint(); + QCOMPARE(widget.takePaintedRegions(), QRegion(0, 0, widget.width(), widget.height())); +} + +/*! + Children cannot assumed to be fully opaque, so the parent will repaint when the + child repaints. +*/ +void tst_QWidgetRepaintManager::children() +{ + if (QStringList{"android"}.contains(QGuiApplication::platformName())) + QSKIP("This test fails on Android"); + + TestWidget widget; + widget.initialShow(); + + TestWidget *child1 = new TestWidget(&widget); + child1->move(20, 20); + child1->show(); + QVERIFY(QTest::qWaitForWindowExposed(child1)); + QVERIFY(child1->waitForPainted()); + QCOMPARE(widget.takePaintedRegions(), QRegion(child1->geometry())); + QCOMPARE(child1->takePaintedRegions(), QRegion(child1->rect())); + + child1->move(20, 30); + QVERIFY(widget.waitForPainted()); + // both the old and the new area covered by child1 need to be repainted + QCOMPARE(widget.takePaintedRegions(), QRegion(20, 20, child1->width(), child1->height() + 10)); + QCOMPARE(child1->takePaintedRegions(), QRegion(child1->rect())); + + TestWidget *child2 = new TestWidget(&widget); + child2->move(30, 30); + child2->raise(); + child2->show(); + + QVERIFY(child2->waitForPainted()); + QCOMPARE(widget.takePaintedRegions(), QRegion(child2->geometry())); + QCOMPARE(child1->takePaintedRegions(), QRegion(10, 0, child2->width() - 10, child2->height())); + QCOMPARE(child2->takePaintedRegions(), QRegion(child2->rect())); + + child1->hide(); + QVERIFY(widget.waitForPainted()); + QCOMPARE(widget.paintedRegions, QRegion(child1->geometry())); +} + +void tst_QWidgetRepaintManager::opaqueChildren() +{ + if (QStringList{"android"}.contains(QGuiApplication::platformName())) + QSKIP("This test fails on Android"); + + TestWidget widget; + widget.initialShow(); + + TestWidget *child1 = new TestWidget(&widget); + child1->move(20, 20); + child1->setAttribute(Qt::WA_OpaquePaintEvent); + child1->show(); + + QVERIFY(child1->waitForPainted()); + QCOMPARE(widget.takePaintedRegions(), QRegion()); + QCOMPARE(child1->takePaintedRegions(), child1->rect()); + + child1->move(20, 30); + QVERIFY(widget.waitForPainted()); + QCOMPARE(widget.takePaintedRegions(), QRegion(20, 20, child1->width(), 10)); + if (!m_implementsScroll) + QEXPECT_FAIL("", "child1 shouldn't get painted, we can just move the area of the backingstore", Continue); + QCOMPARE(child1->takePaintedRegions(), QRegion()); +} + +/*! + When resizing to be larger, a widget with Qt::WA_StaticContents set + should only repaint the newly revealed areas. +*/ +void tst_QWidgetRepaintManager::staticContents() +{ + const auto *integration = QGuiApplicationPrivate::platformIntegration(); + if (!integration->hasCapability(QPlatformIntegration::BackingStoreStaticContents)) + QSKIP("Platform does not support static backingstore content"); + + TestWidget widget; + widget.setAttribute(Qt::WA_StaticContents); + widget.initialShow(); + + // Trigger resize via QWindow (similar to QWSI code path) + QVERIFY(widget.windowHandle()); + QSize oldSize = widget.size(); + widget.windowHandle()->resize(widget.width(), widget.height() + 10); + QVERIFY(widget.waitForPainted()); + QCOMPARE(widget.takePaintedRegions(), QRegion(0, oldSize.width(), widget.width(), 10)); + + // Trigger resize via QWidget + oldSize = widget.size(); + widget.resize(widget.width() + 10, widget.height()); + QVERIFY(widget.waitForPainted()); + QEXPECT_FAIL("", "QWidgetPrivate::setGeometry_sys wrongly triggers full update", Continue); + QCOMPARE(widget.takePaintedRegions(), QRegion(oldSize.width(), 0, 10, widget.height())); +} + +/*! + Scrolling a widget. +*/ +void tst_QWidgetRepaintManager::scroll() +{ + if (QStringList{"android"}.contains(QGuiApplication::platformName())) + QSKIP("This test fails on Android"); + + TestWidget widget; + widget.initialShow(); + + widget.scroll(10, 0); + QVERIFY(widget.waitForPainted()); + if (!m_implementsScroll) + QEXPECT_FAIL("", "This should just repaint the newly exposed region", Continue); + QCOMPARE(widget.takePaintedRegions(), QRegion(0, 0, 10, widget.height())); + + TestWidget *child = new TestWidget(&widget); + child->move(20, 20); + child->initialShow(); + + // a potentially semi-transparent child scrolling needs a full repaint + child->scroll(10, 0); + QVERIFY(child->waitForPainted()); + QCOMPARE(child->takePaintedRegions(), child->rect()); + QCOMPARE(widget.takePaintedRegions(), child->geometry()); + + // a explicitly opaque child scrolling only needs the child to repaint newly exposed regions + child->setAttribute(Qt::WA_OpaquePaintEvent); + child->scroll(10, 0); + QVERIFY(child->waitForPainted()); + if (!m_implementsScroll) + QEXPECT_FAIL("", "This should just repaint the newly exposed region", Continue); + QCOMPARE(child->takePaintedRegions(), QRegion(0, 0, 10, child->height())); + QCOMPARE(widget.takePaintedRegions(), QRegion()); +} + +class PaintOnScreenWidget : public TestWidget +{ +public: + using TestWidget::TestWidget; + + // Explicit override to prevent noPaintOnScreen on Windows + QPaintEngine *paintEngine() const override + { + return nullptr; + } +}; + +void tst_QWidgetRepaintManager::paintOnScreenUpdates() +{ + { + TestWidget topLevel; + topLevel.setObjectName("TopLevel"); + topLevel.resize(500, 500); + TestWidget renderToTextureWidget(&topLevel); + renderToTextureWidget.setObjectName("RenderToTexture"); + renderToTextureWidget.setGeometry(0, 0, 200, 200); + QWidgetPrivate::get(&renderToTextureWidget)->setRenderToTexture(); + + PaintOnScreenWidget paintOnScreenWidget(&topLevel); + paintOnScreenWidget.setObjectName("PaintOnScreen"); + paintOnScreenWidget.setGeometry(200, 200, 300, 300); + + topLevel.initialShow(); + + // Updating before toggling WA_PaintOnScreen should work fine + paintOnScreenWidget.update(); + paintOnScreenWidget.waitForPainted(); + QVERIFY(paintOnScreenWidget.waitForPainted()); + +#ifdef Q_OS_ANDROID + QEXPECT_FAIL("", "This test fails on Android", Abort); +#endif + QCOMPARE(paintOnScreenWidget.takePaintedRegions(), paintOnScreenWidget.rect()); + + renderToTextureWidget.update(); + QVERIFY(renderToTextureWidget.waitForPainted()); + QCOMPARE(renderToTextureWidget.takePaintedRegions(), renderToTextureWidget.rect()); + + // Then toggle WA_PaintOnScreen + paintOnScreenWidget.setAttribute(Qt::WA_PaintOnScreen); + + // The render-to-texture widget updates fine + renderToTextureWidget.update(); + QVERIFY(renderToTextureWidget.waitForPainted()); + QCOMPARE(renderToTextureWidget.takePaintedRegions(), renderToTextureWidget.rect()); + + // Updating the paint-on-screen texture widget will not result + // in a paint event, but should result in an update request. + paintOnScreenWidget.updateRequests = 0; + paintOnScreenWidget.update(); + QVERIFY(QTest::qWaitFor([&]{ return paintOnScreenWidget.updateRequests > 0; })); + + // And should not prevent the render-to-texture widget from receiving updates + renderToTextureWidget.update(); + QVERIFY(renderToTextureWidget.waitForPainted()); + QCOMPARE(renderToTextureWidget.takePaintedRegions(), renderToTextureWidget.rect()); + } + + { + TestWidget paintOnScreenTopLevel; + paintOnScreenTopLevel.setObjectName("PaintOnScreenTopLevel"); + paintOnScreenTopLevel.setAttribute(Qt::WA_PaintOnScreen); + + paintOnScreenTopLevel.initialShow(); + + paintOnScreenTopLevel.updateRequests = 0; + paintOnScreenTopLevel.update(); + QVERIFY(QTest::qWaitFor([&]{ return paintOnScreenTopLevel.updateRequests > 0; })); + + // Turn off paint on screen and make it a render-to-texture widget. + // This will lead us into a QWidgetRepaintManager::markDirty() code + // path that checks updateRequestSent, which is still set from the + // update above since paint-on-screen handling doesn't reset it. + paintOnScreenTopLevel.setAttribute(Qt::WA_PaintOnScreen, false); + QWidgetPrivate::get(&paintOnScreenTopLevel)->setRenderToTexture(); + paintOnScreenTopLevel.update(); + QVERIFY(QTest::qWaitFor([&]{ return paintOnScreenTopLevel.updateRequests > 1; })); + } +} + +#if defined(QT_BUILD_INTERNAL) + +/*! + Verify that overlapping children are repainted correctly when + a widget is moved (via a scroll area) for such a distance that + none of the old area is still visible. QTBUG-26269 +*/ +void tst_QWidgetRepaintManager::scrollWithOverlap() +{ + if (QStringList{"android"}.contains(QGuiApplication::platformName())) + QSKIP("This test fails on Android"); + + class MainWindow : public QWidget + { + public: + MainWindow(QWidget *parent = 0) + : QWidget(parent, Qt::WindowStaysOnTopHint) + { + m_scrollArea = new QScrollArea(this); + m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + QWidget *w = new QWidget; + w->setPalette(QPalette(Qt::gray)); + w->setAutoFillBackground(true); + m_scrollArea->setWidget(w); + m_scrollArea->resize(500, 100); + w->resize(5000, 600); + + m_topWidget = new QWidget(this); + m_topWidget->setPalette(QPalette(Qt::red)); + m_topWidget->setAutoFillBackground(true); + m_topWidget->resize(300, 200); + + resize(600, 300); + } + + void resizeEvent(QResizeEvent *e) override + { + QWidget::resizeEvent(e); + // move scroll area and top widget to the center of the main window + scrollArea()->move((width() - scrollArea()->width()) / 2, (height() - scrollArea()->height()) / 2); + topWidget()->move((width() - topWidget()->width()) / 2, (height() - topWidget()->height()) / 2); + } + + + inline QScrollArea *scrollArea() const { return m_scrollArea; } + inline QWidget *topWidget() const { return m_topWidget; } + + private: + QScrollArea *m_scrollArea; + QWidget *m_topWidget; + }; + + MainWindow w; + w.show(); + + QVERIFY(QTest::qWaitForWindowActive(&w)); + + bool result = compareWidget(w.topWidget()); + // if this fails already, then the system we test on can't compare screenshots from grabbed widgets, + // and we have to skip this test. Possible reasons are differences in surface formats or DPI, or + // unrelated bugs in QPlatformBackingStore::toImage or QWidget::grab. + if (!result) + QSKIP("Cannot compare QWidget::grab with QScreen::grabWindow on this machine"); + + // scroll the horizontal slider to the right side + { + w.scrollArea()->horizontalScrollBar()->setValue(w.scrollArea()->horizontalScrollBar()->maximum()); + QVERIFY(compareWidget(w.topWidget())); + } + + // scroll the vertical slider down + { + w.scrollArea()->verticalScrollBar()->setValue(w.scrollArea()->verticalScrollBar()->maximum()); + QVERIFY(compareWidget(w.topWidget())); + } + + // hide the top widget + { + w.topWidget()->hide(); + QVERIFY(compareWidget(w.scrollArea()->viewport())); + } + + // scroll the horizontal slider to the left side + { + w.scrollArea()->horizontalScrollBar()->setValue(w.scrollArea()->horizontalScrollBar()->minimum()); + QVERIFY(compareWidget(w.scrollArea()->viewport())); + } + + // scroll the vertical slider up + { + w.scrollArea()->verticalScrollBar()->setValue(w.scrollArea()->verticalScrollBar()->minimum()); + QVERIFY(compareWidget(w.scrollArea()->viewport())); + } +} + +/*! + This tests QWidgetPrivate::overlappedRegion, which however is only used in the + QWidgetRepaintManager, so the test is here. +*/ +void tst_QWidgetRepaintManager::overlappedRegion() +{ + TestScene scene; + + if (scene.screen()->availableSize().width() < scene.sizeHint().width() + || scene.screen()->availableSize().height() < scene.sizeHint().height()) { + QSKIP("The screen on this system is too small for this test"); + } + + scene.show(); + QVERIFY(QTest::qWaitForWindowExposed(&scene)); + + auto overlappedRegion = [](QWidget *widget, bool breakAfterFirst = false){ + auto *priv = QWidgetPrivate::get(widget); + // overlappedRegion works on parent coordinates (crect, i.e. QWidget::geometry) + return priv->overlappedRegion(widget->geometry(), breakAfterFirst); + }; + + // the yellow child is not overlapped + QVERIFY(overlappedRegion(scene.yellowChild).isEmpty()); + // the green child is partially overlapped by the yellow child, which + // is at position -50, -50 relative to the green child (and 100x100 large) + QRegion overlap = overlappedRegion(scene.greenChild); + QVERIFY(!overlap.isEmpty()); + QCOMPARE(overlap, QRegion(QRect(-50, -50, 100, 100))); + // the red child is completely obscured by the green child, and partially + // obscured by the yellow child. How exactly this is divided into rects is + // irrelevant for the test. + overlap = overlappedRegion(scene.redChild); + QVERIFY(!overlap.isEmpty()); + QCOMPARE(overlap.boundingRect(), QRect(-50, -50, 150, 150)); + + // moving the red child out of obscurity + scene.redChild->move(100, 0); + overlap = overlappedRegion(scene.redChild); + QTRY_VERIFY(overlap.isEmpty()); + + // moving the red child down so it's partially behind the bar + scene.redChild->move(100, 100); + overlap = overlappedRegion(scene.redChild); + QTRY_VERIFY(!overlap.isEmpty()); + + // moving the yellow child so it is partially overlapped by the bar + scene.yellowChild->move(200, 200); + overlap = overlappedRegion(scene.yellowChild); + QTRY_VERIFY(!overlap.isEmpty()); +} + +void tst_QWidgetRepaintManager::fastMove() +{ + TestScene scene; + scene.show(); + QVERIFY(QTest::qWaitForWindowExposed(&scene)); + + QWidgetRepaintManager *repaintManager = QWidgetPrivate::get(&scene)->maybeRepaintManager(); + QVERIFY(repaintManager->dirtyRegion().isEmpty()); + + // moving yellow; nothing obscured + scene.yellowChild->move(QPoint(25, 0)); + QVERIFY(repaintManager->dirtyRegion().isEmpty()); // fast move + if (m_implementsScroll) { + QCOMPARE(repaintManager->dirtyWidgetList(), QList<QWidget *>() << &scene); + QVERIFY(dirtyRegion(scene.yellowChild).isEmpty()); + } else { + QCOMPARE(repaintManager->dirtyWidgetList(), QList<QWidget *>() << scene.yellowChild << &scene); + QCOMPARE(dirtyRegion(scene.yellowChild), QRect(0, 0, 100, 100)); + } + QCOMPARE(dirtyRegion(&scene), QRect(0, 0, 25, 100)); + QTRY_VERIFY(dirtyRegion(&scene).isEmpty()); + QVERIFY(compareWidget(&scene)); +} + +void tst_QWidgetRepaintManager::moveAccross() +{ + TestScene scene; + scene.show(); + QVERIFY(QTest::qWaitForWindowExposed(&scene)); + + QWidgetRepaintManager *repaintManager = QWidgetPrivate::get(&scene)->maybeRepaintManager(); + QVERIFY(repaintManager->dirtyRegion().isEmpty()); + + for (int i = 0; i < 4; ++i) { + scene.greenChild->move(scene.greenChild->pos() + QPoint(25, 0)); + waitForFlush(&scene); + } + QVERIFY(compareWidget(&scene)); + + for (int i = 0; i < 16; ++i) { + scene.redChild->move(scene.redChild->pos() + QPoint(25, 0)); + waitForFlush(&scene); + } + QVERIFY(compareWidget(&scene)); + + for (int i = 0; i < qMin(scene.area->width(), scene.area->height()); i += 25) { + scene.yellowChild->move(scene.yellowChild->pos() + QPoint(25, 25)); + waitForFlush(&scene); + } + QVERIFY(compareWidget(&scene)); +} + +void tst_QWidgetRepaintManager::moveInOutOverlapped() +{ + TestScene scene; + scene.show(); + QVERIFY(QTest::qWaitForWindowExposed(&scene)); + + QWidgetRepaintManager *repaintManager = QWidgetPrivate::get(&scene)->maybeRepaintManager(); + QVERIFY(repaintManager->dirtyRegion().isEmpty()); + + // yellow out + scene.yellowChild->move(QPoint(-100, 0)); + QVERIFY(!repaintManager->dirtyRegion().isEmpty()); // invalid dest rect + QVERIFY(repaintManager->dirtyWidgetList().isEmpty()); + QVERIFY(waitForFlush(&scene)); + QVERIFY(compareWidget(&scene)); + + // yellow in, obscured by bar + scene.yellowChild->move(QPoint(scene.width() / 2, scene.height() / 2)); + QVERIFY(!repaintManager->dirtyRegion().isEmpty()); // invalid source rect + QVERIFY(repaintManager->dirtyWidgetList().isEmpty()); + QVERIFY(waitForFlush(&scene)); + QVERIFY(compareWidget(&scene)); + + // green out + scene.greenChild->move(QPoint(-100, 0)); + QVERIFY(!repaintManager->dirtyRegion().isEmpty()); // invalid dest rect + QVERIFY(repaintManager->dirtyWidgetList().isEmpty()); + QVERIFY(waitForFlush(&scene)); + QVERIFY(compareWidget(&scene)); + + // green back in, obscured by bar + scene.greenChild->move(QPoint(scene.area->width() / 2 - 50, scene.area->height() / 2 - 50)); + QVERIFY(!repaintManager->dirtyRegion().isEmpty()); // invalid source rect + QVERIFY(repaintManager->dirtyWidgetList().isEmpty()); + QVERIFY(waitForFlush(&scene)); + QVERIFY(compareWidget(&scene)); + + // red back under green + scene.redChild->move(scene.greenChild->pos()); + QVERIFY(!repaintManager->dirtyRegion().isEmpty()); // destination rect obscured + QVERIFY(repaintManager->dirtyWidgetList().isEmpty()); + QVERIFY(waitForFlush(&scene)); + QVERIFY(compareWidget(&scene)); +} +#endif //# defined(QT_BUILD_INTERNAL) + +QTEST_MAIN(tst_QWidgetRepaintManager) +#include "tst_qwidgetrepaintmanager.moc" diff --git a/tests/auto/widgets/kernel/qwidgetsvariant/CMakeLists.txt b/tests/auto/widgets/kernel/qwidgetsvariant/CMakeLists.txt new file mode 100644 index 0000000000..bb3c1e2ad6 --- /dev/null +++ b/tests/auto/widgets/kernel/qwidgetsvariant/CMakeLists.txt @@ -0,0 +1,22 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qwidgetsvariant Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qwidgetsvariant LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qwidgetsvariant + SOURCES + tst_qwidgetsvariant.cpp + INCLUDE_DIRECTORIES + ../../../other/qvariant_common + LIBRARIES + Qt::Gui + Qt::Widgets +) diff --git a/tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.pro b/tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.pro deleted file mode 100644 index 7979e92087..0000000000 --- a/tests/auto/widgets/kernel/qwidgetsvariant/qwidgetsvariant.pro +++ /dev/null @@ -1,6 +0,0 @@ -CONFIG += testcase -TARGET = tst_qwidgetsvariant -INCLUDEPATH += $$PWD/../../../other/qvariant_common -SOURCES += tst_qwidgetsvariant.cpp -QT += testlib widgets - diff --git a/tests/auto/widgets/kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp b/tests/auto/widgets/kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp index fd26e3e4c5..0a03fb9e1d 100644 --- a/tests/auto/widgets/kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp +++ b/tests/auto/widgets/kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp @@ -1,33 +1,8 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <qvariant.h> @@ -77,13 +52,13 @@ void tst_QWidgetsVariant::constructor_invalid() QFETCH(uint, typeId); { QTest::ignoreMessage(QtWarningMsg, QRegularExpression("^Trying to construct an instance of an invalid type, type id:")); - QVariant variant(static_cast<QVariant::Type>(typeId)); + QVariant variant{QMetaType(typeId)}; QVERIFY(!variant.isValid()); QCOMPARE(variant.userType(), int(QMetaType::UnknownType)); } { QTest::ignoreMessage(QtWarningMsg, QRegularExpression("^Trying to construct an instance of an invalid type, type id:")); - QVariant variant(typeId, /* copy */ 0); + QVariant variant(QMetaType(typeId), nullptr); QVERIFY(!variant.isValid()); QCOMPARE(variant.userType(), int(QMetaType::UnknownType)); } @@ -149,7 +124,7 @@ void tst_QWidgetsVariant::writeToReadFromDataStream() class CustomQWidget : public QWidget { Q_OBJECT public: - CustomQWidget(QWidget *parent = 0) : QWidget(parent) {} + CustomQWidget(QWidget *parent = nullptr) : QWidget(parent) {} }; void tst_QWidgetsVariant::qvariant_cast_QObject_data() @@ -176,24 +151,24 @@ void tst_QWidgetsVariant::qvariant_cast_QObject() if (success) { QCOMPARE(o->objectName(), QString::fromLatin1("Hello")); QVERIFY(data.canConvert<QObject*>()); - QVERIFY(data.canConvert(QMetaType::QObjectStar)); - QVERIFY(data.canConvert(::qMetaTypeId<QObject*>())); + QVERIFY(data.canConvert(QMetaType(QMetaType::QObjectStar))); + QVERIFY(data.canConvert(QMetaType::fromType<QObject*>())); QVERIFY(data.value<QObject*>()); - QVERIFY(data.convert(QMetaType::QObjectStar)); - QCOMPARE(data.userType(), int(QMetaType::QObjectStar)); + QVERIFY(data.convert(QMetaType(QMetaType::QObjectStar))); + QCOMPARE(data.metaType().id(), int(QMetaType::QObjectStar)); QVERIFY(data.canConvert<QWidget*>()); - QVERIFY(data.canConvert(::qMetaTypeId<QWidget*>())); + QVERIFY(data.canConvert(QMetaType::fromType<QWidget*>())); QVERIFY(data.value<QWidget*>()); - QVERIFY(data.convert(qMetaTypeId<QWidget*>())); - QCOMPARE(data.userType(), qMetaTypeId<QWidget*>()); + QVERIFY(data.convert(QMetaType::fromType<QWidget*>())); + QCOMPARE(data.metaType(), QMetaType::fromType<QWidget*>()); } else { QVERIFY(!data.canConvert<QObject*>()); - QVERIFY(!data.canConvert(QMetaType::QObjectStar)); - QVERIFY(!data.canConvert(::qMetaTypeId<QObject*>())); + QVERIFY(!data.canConvert(QMetaType(QMetaType::QObjectStar))); + QVERIFY(!data.canConvert(QMetaType::fromType<QObject*>())); QVERIFY(!data.value<QObject*>()); - QVERIFY(!data.convert(QMetaType::QObjectStar)); - QVERIFY(data.userType() != QMetaType::QObjectStar); + QVERIFY(!data.convert(QMetaType(QMetaType::QObjectStar))); + QVERIFY(data.metaType().id() != QMetaType::QObjectStar); } delete o; } @@ -215,10 +190,10 @@ void tst_QWidgetsVariant::debugStream_data() QTest::addColumn<QVariant>("variant"); QTest::addColumn<int>("typeId"); for (int id = QMetaType::LastGuiType + 1; id < QMetaType::User; ++id) { - const char *tagName = QMetaType::typeName(id); + const char *tagName = QMetaType(id).name(); if (!tagName) continue; - QTest::newRow(tagName) << QVariant(static_cast<QVariant::Type>(id)) << id; + QTest::newRow(tagName) << QVariant(QMetaType(id)) << id; } } diff --git a/tests/auto/widgets/kernel/qwindowcontainer/BLACKLIST b/tests/auto/widgets/kernel/qwindowcontainer/BLACKLIST new file mode 100644 index 0000000000..b788b0cea5 --- /dev/null +++ b/tests/auto/widgets/kernel/qwindowcontainer/BLACKLIST @@ -0,0 +1,5 @@ +# QTBUG-87413 +[testActivation] +android +[testAncestorChange] +android diff --git a/tests/auto/widgets/kernel/qwindowcontainer/CMakeLists.txt b/tests/auto/widgets/kernel/qwindowcontainer/CMakeLists.txt new file mode 100644 index 0000000000..787505972f --- /dev/null +++ b/tests/auto/widgets/kernel/qwindowcontainer/CMakeLists.txt @@ -0,0 +1,20 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qwindowcontainer Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qwindowcontainer LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_qwindowcontainer + SOURCES + tst_qwindowcontainer.cpp + LIBRARIES + Qt::Gui + Qt::Widgets +) diff --git a/tests/auto/widgets/kernel/qwindowcontainer/qwindowcontainer.pro b/tests/auto/widgets/kernel/qwindowcontainer/qwindowcontainer.pro deleted file mode 100644 index 50069b7e3e..0000000000 --- a/tests/auto/widgets/kernel/qwindowcontainer/qwindowcontainer.pro +++ /dev/null @@ -1,4 +0,0 @@ -CONFIG += testcase -TARGET = tst_qwindowcontainer -QT += widgets testlib -SOURCES += tst_qwindowcontainer.cpp diff --git a/tests/auto/widgets/kernel/qwindowcontainer/tst_qwindowcontainer.cpp b/tests/auto/widgets/kernel/qwindowcontainer/tst_qwindowcontainer.cpp index 693a792f0a..52aaf094b4 100644 --- a/tests/auto/widgets/kernel/qwindowcontainer/tst_qwindowcontainer.cpp +++ b/tests/auto/widgets/kernel/qwindowcontainer/tst_qwindowcontainer.cpp @@ -1,43 +1,20 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + + +#include <QTest> #include <qapplication.h> #include <qwindow.h> #include <qwidget.h> +#include <qlineedit.h> #include <qdockwidget.h> #include <qmainwindow.h> #include <qscreen.h> #include <qscopedpointer.h> #include <qevent.h> +#include <qboxlayout.h> class Window : public QWindow @@ -49,7 +26,8 @@ public: { } - void exposeEvent(QExposeEvent *) { + void exposeEvent(QExposeEvent *) override + { if (isExposed()) ++numberOfExposes; else @@ -70,16 +48,20 @@ public: private slots: void testShow(); void testPositionAndSize(); + void testSizeHints(); void testExposeObscure(); void testOwnership(); void testBehindTheScenesDeletion(); void testUnparenting(); + void testReparenting(); void testUnparentReparent(); void testActivation(); void testAncestorChange(); void testDockWidget(); void testNativeContainerParent(); void testPlatformSurfaceEvent(); + void embedWidgetWindow(); + void testFocus(); void cleanup(); private: @@ -93,6 +75,9 @@ void tst_QWindowContainer::cleanup() void tst_QWindowContainer::testShow() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QWidget root; root.setWindowTitle(QTest::currentTestFunction()); root.setGeometry(m_availableGeometry.x() + 100, m_availableGeometry.y() + 100, 400, 400); @@ -104,9 +89,6 @@ void tst_QWindowContainer::testShow() root.show(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "Fails on WinRT - QTBUG-68297", Abort); -#endif QVERIFY(QTest::qWaitForWindowExposed(window)); } @@ -131,10 +113,35 @@ void tst_QWindowContainer::testPositionAndSize() QCOMPARE(window->height(), container->height()); } +void tst_QWindowContainer::testSizeHints() +{ + QScopedPointer<QWidget> tlw(new QWidget); + QWindow *window = new QWindow(); + window->setMinimumSize(QSize(200, 200)); + window->setGeometry(m_availableGeometry.x() + 300, m_availableGeometry.y() + 400, 500, 600); + + QScopedPointer<QWidget> container(QWidget::createWindowContainer(window)); + container->setWindowTitle(QTest::currentTestFunction()); + + QVBoxLayout *vbox = new QVBoxLayout(tlw.data()); + vbox->addWidget(container.data()); + vbox->setContentsMargins(0, 0, 0, 0); + // Size hints should work regardless of visibility + QCOMPARE(container->minimumSizeHint(), window->minimumSize()); + QCOMPARE(vbox->minimumSize(), window->minimumSize()); + + // Respect dynamic updates + window->setMinimumSize(QSize(210, 210)); + QCOMPARE(container->minimumSizeHint(), window->minimumSize()); + QCOMPARE(vbox->minimumSize(), window->minimumSize()); +} void tst_QWindowContainer::testExposeObscure() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + Window *window = new Window(); QScopedPointer<QWidget> container(QWidget::createWindowContainer(window)); @@ -143,9 +150,6 @@ void tst_QWindowContainer::testExposeObscure() container->show(); QVERIFY(QTest::qWaitForWindowExposed(container.data())); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "Fails on WinRT - QTBUG-68297", Abort); -#endif QVERIFY(QTest::qWaitForWindowExposed(window)); QVERIFY(window->numberOfExposes > 0); @@ -187,6 +191,9 @@ void tst_QWindowContainer::testBehindTheScenesDeletion() void tst_QWindowContainer::testActivation() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QWidget root; root.setWindowTitle(QTest::currentTestFunction()); @@ -227,12 +234,12 @@ void tst_QWindowContainer::testActivation() void tst_QWindowContainer::testUnparenting() { - QWindow *window = new QWindow(); + QPointer<QWindow> window(new QWindow()); QScopedPointer<QWidget> container(QWidget::createWindowContainer(window)); container->setWindowTitle(QTest::currentTestFunction()); container->setGeometry(m_availableGeometry.x() + 100, m_availableGeometry.y() + 100, 200, 100); - window->setParent(0); + window->setParent(nullptr); container->show(); @@ -240,10 +247,33 @@ void tst_QWindowContainer::testUnparenting() // Window should not be made visible by container.. QVERIFY(!window->isVisible()); + + container.reset(); + QVERIFY(window); + delete window; +} + +void tst_QWindowContainer::testReparenting() +{ + QPointer<QWindow> window1(new QWindow()); + QScopedPointer<QWindow> window2(new QWindow()); + QScopedPointer<QWidget> container(QWidget::createWindowContainer(window1)); + + window1->setParent(window2.data()); + + // Not deleted with container + container.reset(); + QVERIFY(window1); + // but deleted with new parent + window2.reset(); + QVERIFY(!window1); } void tst_QWindowContainer::testUnparentReparent() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QWidget root; QWindow *window = new QWindow(); @@ -261,9 +291,6 @@ void tst_QWindowContainer::testUnparentReparent() QTRY_VERIFY(!window->isVisible()); container->show(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "Fails on WinRT - QTBUG-68297", Abort); -#endif QVERIFY(QTest::qWaitForWindowExposed(window)); QTRY_VERIFY(window->isVisible()); @@ -357,6 +384,9 @@ void tst_QWindowContainer::testDockWidget() void tst_QWindowContainer::testNativeContainerParent() { + if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) + QSKIP("Wayland: This fails. Figure out why."); + QWidget root; root.setWindowTitle(QTest::currentTestFunction()); root.setGeometry(m_availableGeometry.x() + 50, m_availableGeometry.y() + 50, 200, 200); @@ -368,9 +398,6 @@ void tst_QWindowContainer::testNativeContainerParent() root.show(); -#ifdef Q_OS_WINRT - QEXPECT_FAIL("", "Fails on WinRT - QTBUG-68297", Abort); -#endif QVERIFY(QTest::qWaitForWindowExposed(window)); QTRY_COMPARE(window->parent(), container->windowHandle()); } @@ -410,6 +437,99 @@ void tst_QWindowContainer::testPlatformSurfaceEvent() QVERIFY(ok); } +void tst_QWindowContainer::embedWidgetWindow() +{ + { + QWidget parent; + QWidget *widget = new QWidget; + widget->show(); + QVERIFY(QTest::qWaitForWindowExposed(widget)); + QVERIFY(widget->windowHandle()); + QPointer<QWindow> widgetWindow = widget->windowHandle(); + auto *container = QWidget::createWindowContainer(widgetWindow, &parent); + QCOMPARE(container, widget); + QCOMPARE(widget->parent(), &parent); + delete widget; + QTRY_VERIFY(widgetWindow.isNull()); + } + + QPointer<QWidget> widget = new QWidget; + QPointer<QWindow> widgetWindow; + { + QWidget parent; + widget->show(); + QVERIFY(QTest::qWaitForWindowExposed(widget)); + QVERIFY(widget->windowHandle()); + widgetWindow = widget->windowHandle(); + auto *container = QWidget::createWindowContainer(widgetWindow, &parent); + QCOMPARE(container, widget); + QCOMPARE(widget->parent(), &parent); + } + QTRY_VERIFY(widget.isNull()); + QTRY_VERIFY(widgetWindow.isNull()); + +} + +void tst_QWindowContainer::testFocus() +{ + QWidget root; + root.setGeometry(m_availableGeometry); + + QLineEdit *lineEdit = new QLineEdit(&root); + lineEdit->setGeometry(0, 0, m_availableGeometry.width() * 0.1, 17); + lineEdit->setFocusPolicy(Qt::FocusPolicy::StrongFocus); + + QWindow *embedded = new QWindow(); + QWidget *container = QWidget::createWindowContainer(embedded, &root); + container->setGeometry(0, lineEdit->height() + 10, m_availableGeometry.width() * 0.2, m_availableGeometry.height() - (lineEdit->height() + 10)); + container->setFocusPolicy(Qt::StrongFocus); + + root.show(); + QVERIFY(QTest::qWaitForWindowExposed(&root)); + lineEdit->setFocus(); + QTRY_VERIFY(lineEdit->hasFocus()); + QCOMPARE(QGuiApplication::focusWindow(), root.windowHandle()); + QCOMPARE(QApplication::focusWidget(), lineEdit); + + // embedded window gets focused because of mouse click + QPoint embeddedCenter = container->rect().center(); + QTest::mousePress(root.windowHandle(), Qt::LeftButton, {}, embeddedCenter); + QVERIFY(QTest::qWaitForWindowFocused(embedded)); + QVERIFY(container->hasFocus()); + QCOMPARE(QGuiApplication::focusWindow(), embedded); + QCOMPARE(QApplication::focusWidget(), container); + QVERIFY(!lineEdit->hasFocus()); + + QTest::mouseClick(lineEdit, Qt::LeftButton, {}); + QVERIFY(QTest::qWaitForWindowFocused(root.windowHandle())); + QCOMPARE(QGuiApplication::focusWindow(), root.windowHandle()); + QCOMPARE(QApplication::focusWidget(), lineEdit); + QVERIFY(lineEdit->hasFocus()); + + // embedded window gets focused because of Tab key event + QTest::keyClick(root.windowHandle(), Qt::Key_Tab); + QVERIFY(QTest::qWaitForWindowFocused(embedded)); + QVERIFY(container->hasFocus()); + QCOMPARE(QGuiApplication::focusWindow(), embedded); + QCOMPARE(QApplication::focusWidget(), container); + QVERIFY(!lineEdit->hasFocus()); + // A key tab event sent to the root window should cause + // the nextInFocusChain of the window container to get focused + QTest::keyClick(root.windowHandle(), Qt::Key_Tab); + QVERIFY(QTest::qWaitForWindowFocused(root.windowHandle())); + QCOMPARE(QGuiApplication::focusWindow(), root.windowHandle()); + QCOMPARE(QApplication::focusWidget(), lineEdit); + QVERIFY(lineEdit->hasFocus()); + + // embedded window gets focused programmatically + embedded->requestActivate(); + QVERIFY(QTest::qWaitForWindowFocused(embedded)); + QVERIFY(container->hasFocus()); + QCOMPARE(QGuiApplication::focusWindow(), embedded); + QCOMPARE(QApplication::focusWidget(), container); + QVERIFY(!lineEdit->hasFocus()); +} + QTEST_MAIN(tst_QWindowContainer) #include "tst_qwindowcontainer.moc" |