summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp')
-rw-r--r--tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp277
1 files changed, 271 insertions, 6 deletions
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 689a65518e..8e8cec6d4f 100644
--- a/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp
+++ b/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp
@@ -1,5 +1,5 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
@@ -28,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.
@@ -51,6 +53,7 @@ public:
tst_QWidget_window();
public slots:
+ void init();
void initTestCase();
void cleanupTestCase();
void cleanup();
@@ -82,6 +85,7 @@ private slots:
void tst_dnd();
void tst_dnd_events();
void tst_dnd_propagation();
+ void tst_dnd_destroyOnDrop();
#endif
void tst_qtbug35600();
@@ -107,8 +111,13 @@ private slots:
void mouseMoveWithPopup_data();
void mouseMoveWithPopup();
+ void showHideWindowHandle_data();
+ void showHideWindowHandle();
+
void resetFocusObjectOnDestruction();
+ void cleanupOnDestruction();
+
private:
QSize m_testWidgetSize;
const int m_fuzz;
@@ -126,6 +135,11 @@ void tst_QWidget_window::initTestCase()
{
}
+void tst_QWidget_window::init()
+{
+ QTest::failOnWarning(QRegularExpression(".*No such slot.*"));
+}
+
void tst_QWidget_window::cleanupTestCase()
{
}
@@ -677,7 +691,7 @@ void tst_QWidget_window::tst_dnd()
dndTestWidget.show();
QVERIFY(QTest::qWaitForWindowExposed(&dndTestWidget));
- qApp->setActiveWindow(&dndTestWidget);
+ QApplicationPrivate::setActiveWindow(&dndTestWidget);
QVERIFY(QTest::qWaitForWindowActive(&dndTestWidget));
QMimeData mimeData;
@@ -917,6 +931,78 @@ void tst_QWidget_window::tst_dnd_propagation()
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()
@@ -1377,6 +1463,9 @@ void tst_QWidget_window::mouseMoveWithPopup_data()
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
@@ -1552,8 +1641,159 @@ void tst_QWidget_window::mouseMoveWithPopup()
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
@@ -1563,9 +1803,9 @@ void tst_QWidget_window::resetFocusObjectOnDestruction()
widget->show();
QVERIFY(QTest::qWaitForWindowActive(widget.get()));
- int activeCount = focusObjectChangedSpy.count();
+ int activeCount = focusObjectChangedSpy.size();
widget.reset();
- QVERIFY(focusObjectChangedSpy.count() > activeCount);
+ QVERIFY(focusObjectChangedSpy.size() > activeCount);
QCOMPARE(focusObjectChangedSpy.last().last().value<QObject*>(), nullptr);
focusObjectChangedSpy.clear();
@@ -1578,12 +1818,37 @@ void tst_QWidget_window::resetFocusObjectOnDestruction()
widget->show();
QVERIFY(QTest::qWaitForWindowActive(widget.get()));
- activeCount = focusObjectChangedSpy.count();
+ activeCount = focusObjectChangedSpy.size();
widget.reset();
// we might get more than one signal emission
- QVERIFY(focusObjectChangedSpy.count() > activeCount);
+ 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"