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.cpp332
1 files changed, 304 insertions, 28 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 5ae4526af2..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,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 <QTest>
@@ -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();
@@ -106,6 +85,7 @@ private slots:
void tst_dnd();
void tst_dnd_events();
void tst_dnd_propagation();
+ void tst_dnd_destroyOnDrop();
#endif
void tst_qtbug35600();
@@ -131,6 +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;
@@ -148,6 +135,11 @@ void tst_QWidget_window::initTestCase()
{
}
+void tst_QWidget_window::init()
+{
+ QTest::failOnWarning(QRegularExpression(".*No such slot.*"));
+}
+
void tst_QWidget_window::cleanupTestCase()
{
}
@@ -699,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;
@@ -939,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()
@@ -1399,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
@@ -1574,5 +1641,214 @@ 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
+ 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"