summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp')
-rw-r--r--tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp166
1 files changed, 137 insertions, 29 deletions
diff --git a/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp b/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp
index 1af61e9ec6..325e188091 100644
--- a/tests/auto/widgets/widgets/qlabel/tst_qlabel.cpp
+++ b/tests/auto/widgets/widgets/qlabel/tst_qlabel.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,8 @@
#include <qmath.h>
#include <private/qlabel_p.h>
+#include <QtWidgets/private/qapplication_p.h>
+
class Widget : public QWidget
{
Q_OBJECT
@@ -100,12 +77,16 @@ private Q_SLOTS:
#ifndef QT_NO_CONTEXTMENU
void taskQTBUG_7902_contextMenuCrash();
+ void contextMenu_data();
+ void contextMenu();
#endif
void taskQTBUG_48157_dprPixmap();
void taskQTBUG_48157_dprMovie();
void resourceProvider();
+ void mouseEventPropagation_data();
+ void mouseEventPropagation();
private:
QLabel *testWidget;
@@ -190,7 +171,7 @@ void tst_QLabel::setBuddy()
layout->addWidget(test_edit);
layout->addWidget(test_edit2);
test_box->show();
- qApp->setActiveWindow(test_box);
+ QApplicationPrivate::setActiveWindow(test_box);
QVERIFY(test_box->isActiveWindow());
test_label->setBuddy( test_edit );
@@ -575,12 +556,50 @@ void tst_QLabel::taskQTBUG_7902_contextMenuCrash()
w->connect(&ti, SIGNAL(timeout()), w, SLOT(deleteLater()));
ti.start(300);
- QContextMenuEvent *cme = new QContextMenuEvent(QContextMenuEvent::Mouse, w->rect().center());
+ QContextMenuEvent *cme = new QContextMenuEvent(QContextMenuEvent::Mouse, w->rect().center(),
+ w->mapToGlobal(w->rect().center()));
qApp->postEvent(w, cme);
QTest::qWait(350);
// No crash, it's allright.
}
+
+void tst_QLabel::contextMenu_data()
+{
+ QTest::addColumn<QString>("text");
+ QTest::addColumn<Qt::TextInteractionFlag>("interactionFlags");
+ QTest::addColumn<bool>("showsContextMenu");
+
+ QTest::addRow("Read-only") << "Plain Text"
+ << Qt::NoTextInteraction
+ << false;
+ QTest::addRow("Selectable") << "Plain Text"
+ << Qt::TextEditorInteraction
+ << true;
+ QTest::addRow("Link") << "<a href=\"nowhere\">Rich text with link</a>"
+ << Qt::TextBrowserInteraction
+ << true;
+ QTest::addRow("Rich text") << "<b>Rich text without link</b>"
+ << Qt::TextBrowserInteraction
+ << true;
+}
+
+void tst_QLabel::contextMenu()
+{
+ QFETCH(QString, text);
+ QFETCH(Qt::TextInteractionFlag, interactionFlags);
+ QFETCH(bool, showsContextMenu);
+
+ QLabel label(text);
+ label.setTextInteractionFlags(interactionFlags);
+ label.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&label));
+
+ const QPoint menuPosition = label.rect().center();
+ QContextMenuEvent cme(QContextMenuEvent::Mouse, menuPosition, label.mapToGlobal(menuPosition));
+ QApplication::sendEvent(&label, &cme);
+ QCOMPARE(cme.isAccepted(), showsContextMenu);
+}
#endif
void tst_QLabel::taskQTBUG_48157_dprPixmap()
@@ -622,5 +641,94 @@ void tst_QLabel::resourceProvider()
QVERIFY(providerCalled > 0);
}
+// Test if mouse events are correctly propagated to the parent widget,
+// even if a label contains rich text (QTBUG-110055)
+void tst_QLabel::mouseEventPropagation_data()
+{
+ QTest::addColumn<const QString>("text");
+ QTest::addColumn<const Qt::TextInteractionFlag>("interaction");
+ QTest::addColumn<const QList<Qt::MouseButton>>("buttons");
+ QTest::addColumn<const bool>("expectPropagation");
+
+
+ QTest::newRow("RichText")
+ << QString("<b>This is a rich text propagating mouse events</b>")
+ << Qt::LinksAccessibleByMouse
+ << QList<Qt::MouseButton>{Qt::LeftButton, Qt::RightButton, Qt::MiddleButton}
+ << true;
+ QTest::newRow("PlainText")
+ << QString("This is a plain text propagating mouse events")
+ << Qt::LinksAccessibleByMouse
+ << QList<Qt::MouseButton>{Qt::LeftButton, Qt::RightButton, Qt::MiddleButton}
+ << true;
+ QTest::newRow("PlainTextConsume")
+ << QString("This is a plain text consuming mouse events")
+ << Qt::TextSelectableByMouse
+ << QList<Qt::MouseButton>{Qt::LeftButton}
+ << false;
+ QTest::newRow("RichTextConsume")
+ << QString("<b>This is a rich text consuming mouse events</b>")
+ << Qt::TextSelectableByMouse
+ << QList<Qt::MouseButton>{Qt::LeftButton}
+ << false;
+ QTest::newRow("PlainTextNoInteraction")
+ << QString("This is a text not interacting with mouse")
+ << Qt::NoTextInteraction
+ << QList<Qt::MouseButton>{Qt::LeftButton, Qt::RightButton, Qt::MiddleButton}
+ << true;
+ QTest::newRow("RichTextNoInteraction")
+ << QString("<b>This is a rich text not interacting with mouse</b>")
+ << Qt::NoTextInteraction
+ << QList<Qt::MouseButton>{Qt::LeftButton, Qt::RightButton, Qt::MiddleButton}
+ << true;
+}
+
+void tst_QLabel::mouseEventPropagation()
+{
+ class MouseEventWidget : public QWidget
+ {
+ public:
+ uint pressed() const { return m_pressed; }
+ uint released() const { return m_released; }
+
+ private:
+ uint m_pressed = 0;
+ uint m_released = 0;
+ void mousePressEvent(QMouseEvent *event) override
+ {
+ ++m_pressed;
+ return QWidget::mousePressEvent(event);
+ }
+
+ void mouseReleaseEvent(QMouseEvent *event) override
+ {
+ ++m_released;
+ return QWidget::mouseReleaseEvent(event);
+ }
+ };
+
+ QFETCH(const QString, text);
+ QFETCH(const Qt::TextInteractionFlag, interaction);
+ QFETCH(const QList<Qt::MouseButton>, buttons);
+ QFETCH(const bool, expectPropagation);
+
+ MouseEventWidget widget;
+ auto *layout = new QVBoxLayout(&widget);
+ auto *label = new QLabel(text);
+ label->setTextInteractionFlags(interaction);
+
+ layout->addWidget(label);
+ widget.show();
+ QVERIFY(QTest::qWaitForWindowExposed(&widget));
+
+ const QPoint labelCenter = label->rect().center();
+ for (Qt::MouseButton mouseButton : buttons)
+ QTest::mouseClick(label, mouseButton, Qt::KeyboardModifiers(), labelCenter);
+
+ const uint count = expectPropagation ? buttons.count() : 0;
+ QTRY_COMPARE(widget.pressed(), count);
+ QTRY_COMPARE(widget.released(), count);
+}
+
QTEST_MAIN(tst_QLabel)
#include "tst_qlabel.moc"