From 7bf002c3b3f8009138fca217c7fa0c234aed21bd Mon Sep 17 00:00:00 2001 From: Eirik Aavitsland Date: Fri, 22 Jul 2016 11:13:41 +0200 Subject: Backwards compatibility fix: No default colormap for Mono QImages This is a partial revert of a4e2f2e687ca7aec88ecf82f72d42ac61e17a5b9. That fix tried to avoid the risk of a crash in pixel() by ensuring Mono QImages created with external data also got a default color table. However, that broke usable behavior in existing code that was painting in Mono QImages using color0/color1. This commit reverts to the old behavior, and instead expands on the checking in pixel() so that lacking color table is handled gracefully for all indexed formats. Task-number: QTBUG-54827 Change-Id: I9164198bed9d20c4b12cdba40a31c141bef3128d Reviewed-by: Eskil Abrahamsen Blomfeldt --- tests/auto/gui/image/qimage/tst_qimage.cpp | 38 ++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'tests/auto') diff --git a/tests/auto/gui/image/qimage/tst_qimage.cpp b/tests/auto/gui/image/qimage/tst_qimage.cpp index 435178a885..6f088bea24 100644 --- a/tests/auto/gui/image/qimage/tst_qimage.cpp +++ b/tests/auto/gui/image/qimage/tst_qimage.cpp @@ -99,6 +99,8 @@ private slots: void setPixel_data(); void setPixel(); + void defaultColorTable_data(); + void defaultColorTable(); void setColorCount(); void setColor(); @@ -1450,6 +1452,38 @@ void tst_QImage::convertToFormatPreserveText() QCOMPARE(imgResult2.textKeys(), listResult); } +void tst_QImage::defaultColorTable_data() +{ + QTest::addColumn("format"); + QTest::addColumn("createdDataCount"); + QTest::addColumn("externalDataCount"); + + // For historical reasons, internally created mono images get a default colormap. + // Externally created and Indexed8 images do not. + QTest::newRow("Mono") << QImage::Format_Mono << 2 << 0; + QTest::newRow("MonoLSB") << QImage::Format_MonoLSB << 2 << 0; + QTest::newRow("Indexed8") << QImage::Format_Indexed8 << 0 << 0; + QTest::newRow("ARGB32_PM") << QImage::Format_A2BGR30_Premultiplied << 0 << 0; +} + +void tst_QImage::defaultColorTable() +{ + QFETCH(QImage::Format, format); + QFETCH(int, createdDataCount); + QFETCH(int, externalDataCount); + + QImage img1(1, 1, format); + QCOMPARE(img1.colorCount(), createdDataCount); + QCOMPARE(img1.colorTable().size(), createdDataCount); + + quint32 buf; + QImage img2(reinterpret_cast(&buf), 1, 1, format); + QCOMPARE(img2.colorCount(), externalDataCount); + + QImage nullImg(0, 0, format); + QCOMPARE(nullImg.colorCount(), 0); +} + void tst_QImage::setColorCount() { QImage img(0, 0, QImage::Format_Indexed8); @@ -3127,8 +3161,8 @@ void tst_QImage::pixel() QImage monolsb(&a, 1, 1, QImage::Format_MonoLSB); QImage indexed(&a, 1, 1, QImage::Format_Indexed8); - QCOMPARE(QColor(mono.pixel(0, 0)), QColor(Qt::black)); - QCOMPARE(QColor(monolsb.pixel(0, 0)), QColor(Qt::black)); + mono.pixel(0, 0); // Don't crash + monolsb.pixel(0, 0); // Don't crash indexed.pixel(0, 0); // Don't crash } } -- cgit v1.2.3 From f5af4428c3ede16c742df7890bc5f5ebf8ed2535 Mon Sep 17 00:00:00 2001 From: David Faure Date: Tue, 26 Jul 2016 10:07:57 +0200 Subject: QVector: fix crash on reserve(0) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It crashed when d was equal to Data::unsharableEmpty(). Task-number: QTBUG-51758 Change-Id: If9f2a7d11892507135f4dc0aeef909f59b7478fc Reviewed-by: Thiago Macieira Reviewed-by: Oswald Buddenhagen Reviewed-by: Jędrzej Nowacki --- tests/auto/corelib/tools/qvector/tst_qvector.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tests/auto') diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index edcf4c72b6..7c90f05ea4 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -276,6 +276,7 @@ private slots: void testOperators() const; void reserve(); + void reserveZero(); void reallocAfterCopy_data(); void reallocAfterCopy(); void initializeListInt(); @@ -2369,13 +2370,34 @@ void tst_QVector::reserve() { QVector a; a.resize(2); + QCOMPARE(fooCtor, 2); QVector b(a); b.reserve(1); QCOMPARE(b.size(), a.size()); + QCOMPARE(fooDtor, 0); } QCOMPARE(fooCtor, fooDtor); } +// This is a regression test for QTBUG-51758 +void tst_QVector::reserveZero() +{ + QVector vec; + vec.detach(); + vec.reserve(0); // should not crash + QCOMPARE(vec.size(), 0); + QCOMPARE(vec.capacity(), 0); + vec.squeeze(); + QCOMPARE(vec.size(), 0); + QCOMPARE(vec.capacity(), 0); + vec.reserve(-1); + QCOMPARE(vec.size(), 0); + QCOMPARE(vec.capacity(), 0); + vec.append(42); + QCOMPARE(vec.size(), 1); + QVERIFY(vec.capacity() >= 1); +} + // This is a regression test for QTBUG-11763, where memory would be reallocated // soon after copying a QVector. void tst_QVector::reallocAfterCopy_data() -- cgit v1.2.3 From 695d85363e537b51b5d0ae6ab08a9a1e7d9e8354 Mon Sep 17 00:00:00 2001 From: Alexander Volkov Date: Mon, 6 Apr 2015 13:47:16 +0300 Subject: Item delegates: show localized detailed tooltips and "What's this?" texts Extract the common part from QItemDelegate and QStyledItemDelegate which uses QLocale to convert a value for Qt::DisplayRole to a string. Use this code to get the text for tooltips and "What's this?". [ChangeLog][QtWidgets][QAbstractItemDelegate] Show localized detailed tooltips and "What's this?" texts. Task-number: QTBUG-16469 Change-Id: I8618763d45b8cfddafc2f263d658ba256be60a15 Reviewed-by: Giuseppe D'Angelo --- .../itemviews/qitemdelegate/qitemdelegate.pro | 2 +- .../itemviews/qitemdelegate/tst_qitemdelegate.cpp | 62 ++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) (limited to 'tests/auto') diff --git a/tests/auto/widgets/itemviews/qitemdelegate/qitemdelegate.pro b/tests/auto/widgets/itemviews/qitemdelegate/qitemdelegate.pro index f7fb41e60c..02a71f8101 100644 --- a/tests/auto/widgets/itemviews/qitemdelegate/qitemdelegate.pro +++ b/tests/auto/widgets/itemviews/qitemdelegate/qitemdelegate.pro @@ -1,6 +1,6 @@ CONFIG += testcase TARGET = tst_qitemdelegate -QT += widgets testlib +QT += widgets widgets-private testlib SOURCES += tst_qitemdelegate.cpp win32:!wince:!winrt: LIBS += -luser32 diff --git a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp index 25f27cb0c7..45bac13c92 100644 --- a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp +++ b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp @@ -56,6 +56,8 @@ #include #include +#include + Q_DECLARE_METATYPE(QAbstractItemDelegate::EndEditHint) #if defined (Q_OS_WIN) && !defined(Q_OS_WINCE) && !defined(Q_OS_WINRT) @@ -229,6 +231,8 @@ private slots: void task257859_finalizeEdit(); void QTBUG4435_keepSelectionOnCheck(); + + void QTBUG16469_textForRole(); }; @@ -1558,6 +1562,64 @@ void tst_QItemDelegate::testLineEditValidation() QCOMPARE(item->data(Qt::DisplayRole).toString(), QStringLiteral("abc,def")); } +void tst_QItemDelegate::QTBUG16469_textForRole() +{ +#ifndef QT_BUILD_INTERNAL + QSKIP("This test requires a developer build"); +#else + struct TestDelegate : public QItemDelegate + { + QString textForRole(Qt::ItemDataRole role, const QVariant &value, const QLocale &locale) + { + QAbstractItemDelegatePrivate *d = reinterpret_cast(qGetPtrHelper(d_ptr)); + return d->textForRole(role, value, locale); + } + } delegate; + QLocale locale; + + const float f = 123.456f; + QCOMPARE(delegate.textForRole(Qt::DisplayRole, f, locale), locale.toString(f)); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, f, locale), locale.toString(f)); + const double d = 123.456; + QCOMPARE(delegate.textForRole(Qt::DisplayRole, d, locale), locale.toString(d, 'g', 6)); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, d, locale), locale.toString(d, 'g', 6)); + const int i = 1234567; + QCOMPARE(delegate.textForRole(Qt::DisplayRole, i, locale), locale.toString(i)); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, i, locale), locale.toString(i)); + const qlonglong ll = 1234567; + QCOMPARE(delegate.textForRole(Qt::DisplayRole, ll, locale), locale.toString(ll)); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, ll, locale), locale.toString(ll)); + const uint ui = 1234567; + QCOMPARE(delegate.textForRole(Qt::DisplayRole, ui, locale), locale.toString(ui)); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, ui, locale), locale.toString(ui)); + const qulonglong ull = 1234567; + QCOMPARE(delegate.textForRole(Qt::DisplayRole, ull, locale), locale.toString(ull)); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, ull, locale), locale.toString(ull)); + + const QDateTime dateTime = QDateTime::currentDateTime(); + const QDate date = dateTime.date(); + const QTime time = dateTime.time(); + const QString shortDate = locale.toString(date, QLocale::ShortFormat); + const QString longDate = locale.toString(date, QLocale::LongFormat); + const QString shortTime = locale.toString(time, QLocale::ShortFormat); + const QString longTime = locale.toString(time, QLocale::LongFormat); + QCOMPARE(delegate.textForRole(Qt::DisplayRole, date, locale), shortDate); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, date, locale), longDate); + QCOMPARE(delegate.textForRole(Qt::DisplayRole, time, locale), shortTime); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, time, locale), longTime); + QCOMPARE(delegate.textForRole(Qt::DisplayRole, dateTime, locale), shortDate + QLatin1Char(' ') + shortTime); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, dateTime, locale), longDate + QLatin1Char(' ') + longTime); + + const QString text("text"); + QCOMPARE(delegate.textForRole(Qt::DisplayRole, text, locale), text); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, text, locale), text); + const QString multipleLines("multiple\nlines"); + QString multipleLines2 = multipleLines; + multipleLines2.replace(QLatin1Char('\n'), QChar::LineSeparator); + QCOMPARE(delegate.textForRole(Qt::DisplayRole, multipleLines, locale), multipleLines2); + QCOMPARE(delegate.textForRole(Qt::ToolTipRole, multipleLines, locale), multipleLines); +#endif +} // ### _not_ covered: -- cgit v1.2.3 From f24cc53cc27d8ed4be4c1d0d2df059dd6a6909a9 Mon Sep 17 00:00:00 2001 From: Denis Kormalev Date: Fri, 22 Jul 2016 19:00:35 +0300 Subject: Fix for race condition in signal activation There was a race condition between QObject::disconnect() and QMetaObject::activate() which can occur if there are multiple BlockingQueued connections to one signal from different threads and they connect/disconnect their connections often. What can happen in this case is: T1 is in activate() method and T2 is in disconnect() method T1 T2 locks sender mutex selects next connection unlocks sender mutex locks sender mutex sets isSlotObject to false creates QMetaCallEvent derefs connection posts event Two things can happen here: 1. Connection can still be valid, but it will have isSlotObject==false and callFunction will be used instead of slotObj 2. Connection can already be invalid To fix it mutex unlock should be moved after QMetaCallEvent creation. Also there is another case, when we don't disconnect but delete the receiver object. In this case it can already be invalid during postEvent, so we need to move mutex unlock after postEvent. Change-Id: I8103798324140ee11de5b4e10906562ba878ff8b Reviewed-by: Olivier Goffart (Woboq GmbH) Reviewed-by: Marc Mutz --- tests/auto/other/qobjectrace/tst_qobjectrace.cpp | 175 +++++++++++++++++++++++ 1 file changed, 175 insertions(+) (limited to 'tests/auto') diff --git a/tests/auto/other/qobjectrace/tst_qobjectrace.cpp b/tests/auto/other/qobjectrace/tst_qobjectrace.cpp index 036f6f6fdd..36481465d5 100644 --- a/tests/auto/other/qobjectrace/tst_qobjectrace.cpp +++ b/tests/auto/other/qobjectrace/tst_qobjectrace.cpp @@ -51,6 +51,7 @@ class tst_QObjectRace: public QObject private slots: void moveToThreadRace(); void destroyRace(); + void disconnectRace(); }; class RaceObject : public QObject @@ -298,6 +299,180 @@ void tst_QObjectRace::destroyRace() delete threads[i]; } +static QAtomicInteger countedStructObjectsCount; +struct CountedFunctor +{ + CountedFunctor() : destroyed(false) { countedStructObjectsCount.fetchAndAddRelaxed(1); } + CountedFunctor(const CountedFunctor &) : destroyed(false) { countedStructObjectsCount.fetchAndAddRelaxed(1); } + CountedFunctor &operator=(const CountedFunctor &) { return *this; } + ~CountedFunctor() { destroyed = true; countedStructObjectsCount.fetchAndAddRelaxed(-1);} + void operator()() const {QCOMPARE(destroyed, false);} + +private: + bool destroyed; +}; + +class DisconnectRaceSenderObject : public QObject +{ + Q_OBJECT +signals: + void theSignal(); +}; + +class DisconnectRaceThread : public QThread +{ + Q_OBJECT + + DisconnectRaceSenderObject *sender; + bool emitSignal; +public: + DisconnectRaceThread(DisconnectRaceSenderObject *s, bool emitIt) + : QThread(), sender(s), emitSignal(emitIt) + { + } + + void run() + { + while (!isInterruptionRequested()) { + QMetaObject::Connection conn = connect(sender, &DisconnectRaceSenderObject::theSignal, + sender, CountedFunctor(), Qt::BlockingQueuedConnection); + if (emitSignal) + emit sender->theSignal(); + disconnect(conn); + yieldCurrentThread(); + } + } +}; + +class DeleteReceiverRaceSenderThread : public QThread +{ + Q_OBJECT + + DisconnectRaceSenderObject *sender; +public: + DeleteReceiverRaceSenderThread(DisconnectRaceSenderObject *s) + : QThread(), sender(s) + { + } + + void run() + { + while (!isInterruptionRequested()) { + emit sender->theSignal(); + yieldCurrentThread(); + } + } +}; + +class DeleteReceiverRaceReceiver : public QObject +{ + Q_OBJECT + + DisconnectRaceSenderObject *sender; + QObject *receiver; + QTimer *timer; +public: + DeleteReceiverRaceReceiver(DisconnectRaceSenderObject *s) + : QObject(), sender(s), receiver(0) + { + timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, &DeleteReceiverRaceReceiver::onTimeout); + timer->start(1); + } + + void onTimeout() + { + if (receiver) + delete receiver; + receiver = new QObject; + connect(sender, &DisconnectRaceSenderObject::theSignal, receiver, CountedFunctor(), Qt::BlockingQueuedConnection); + } +}; + +class DeleteReceiverRaceReceiverThread : public QThread +{ + Q_OBJECT + + DisconnectRaceSenderObject *sender; +public: + DeleteReceiverRaceReceiverThread(DisconnectRaceSenderObject *s) + : QThread(), sender(s) + { + } + + void run() + { + QScopedPointer receiver(new DeleteReceiverRaceReceiver(sender)); + exec(); + } +}; + +void tst_QObjectRace::disconnectRace() +{ + enum { ThreadCount = 20, TimeLimit = 3000 }; + + QCOMPARE(countedStructObjectsCount.load(), 0u); + + { + QScopedPointer sender(new DisconnectRaceSenderObject()); + QScopedPointer senderThread(new QThread()); + senderThread->start(); + sender->moveToThread(senderThread.data()); + + DisconnectRaceThread *threads[ThreadCount]; + for (int i = 0; i < ThreadCount; ++i) { + threads[i] = new DisconnectRaceThread(sender.data(), !(i % 10)); + threads[i]->start(); + } + + QTime timeLimiter; + timeLimiter.start(); + + while (timeLimiter.elapsed() < TimeLimit) + QTest::qWait(10); + + for (int i = 0; i < ThreadCount; ++i) { + threads[i]->requestInterruption(); + QVERIFY(threads[i]->wait(300)); + delete threads[i]; + } + + senderThread->quit(); + QVERIFY(senderThread->wait(300)); + } + + QCOMPARE(countedStructObjectsCount.load(), 0u); + + { + QScopedPointer sender(new DisconnectRaceSenderObject()); + QScopedPointer senderThread(new DeleteReceiverRaceSenderThread(sender.data())); + senderThread->start(); + sender->moveToThread(senderThread.data()); + + DeleteReceiverRaceReceiverThread *threads[ThreadCount]; + for (int i = 0; i < ThreadCount; ++i) { + threads[i] = new DeleteReceiverRaceReceiverThread(sender.data()); + threads[i]->start(); + } + + QTime timeLimiter; + timeLimiter.start(); + + while (timeLimiter.elapsed() < TimeLimit) + QTest::qWait(10); + + senderThread->requestInterruption(); + QVERIFY(senderThread->wait(300)); + + for (int i = 0; i < ThreadCount; ++i) { + threads[i]->quit(); + QVERIFY(threads[i]->wait(300)); + delete threads[i]; + } + } + + QCOMPARE(countedStructObjectsCount.load(), 0u); +} QTEST_MAIN(tst_QObjectRace) #include "tst_qobjectrace.moc" -- cgit v1.2.3