diff options
author | Mårten Nordheim <marten.nordheim@qt.io> | 2020-03-24 09:40:10 +0100 |
---|---|---|
committer | Mårten Nordheim <marten.nordheim@qt.io> | 2020-04-16 13:52:11 +0200 |
commit | 487dd80bce9c6006f349ccb09222e1c308200f0a (patch) | |
tree | 6f670113f0b787333d45315ab5b754c396536e0e /tests | |
parent | b34158d7a1e89e5e7b32d3425c3df52aacedbb31 (diff) |
Introduce QSocketNotifier::activate(QSocketDescriptor, QSN::Type)
The pre-existing overload passes an int, but this can mean the
descriptor gets truncated in compilations where the descriptor
is 64-bit.
The old overload with int is visible when querying the metaobject system
so string-based connects still work as before, and connecting to it will
produce a deprecation warning in the output.
At the same time the PMF-based connect will, on recompile, pick the
QSocketDescriptor overload. As an added improvement it also comes with
the notification type, removing the need for separate slots where the
code would be mostly shared anyway.
The QSocketDescriptor type can be implicitly converted to and from
qintptr to ensure existing code still compiles. It can also be
constructed from Qt::HANDLE on Windows.
In this same patch I also update the existing string-based connects in
this module, which then includes updating the parameters for some slots
as well.
[ChangeLog][QtCore][QSocketNotifier] Added
QSocketNotifier::activated(QSocketDescriptor, QSocketNotifier::Type).
This replaces the activated(int) signal which in 64-bit environments
could truncate the socket descriptor. If you use "activated" with the
string-based connect() then you need to update the parameter type of the
signal and slot if it had one. If you use it with the pointer to member
function based connect() then all you need to do is update your slot's
parameter type if it has one. If you need to compile your source code
with multiple versions of Qt then connect() to this function using
pointer to member function and update the slot's parameter type if
needed.
Task-number: QTBUG-70441
Change-Id: Ic43d6bc4c5bcb4040867b2ffad8d36fb01eed8af
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp | 70 |
1 files changed, 65 insertions, 5 deletions
diff --git a/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp b/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp index e3f45df27d..8ba3505d8b 100644 --- a/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp +++ b/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp @@ -53,6 +53,7 @@ # undef min #endif // Q_CC_MSVC + class tst_QSocketNotifier : public QObject { Q_OBJECT @@ -63,6 +64,9 @@ private slots: void posixSockets(); #endif void asyncMultipleDatagram(); + void activationReason_data(); + void activationReason(); + void legacyConnect(); protected slots: void async_readDatagramSlot(); @@ -97,10 +101,10 @@ public: { QSocketNotifier *notifier1 = new QSocketNotifier(readEnd1->socketDescriptor(), QSocketNotifier::Read, this); - connect(notifier1, SIGNAL(activated(int)), SLOT(handleActivated())); + connect(notifier1, SIGNAL(activated(QSocketDescriptor)), SLOT(handleActivated())); QSocketNotifier *notifier2 = new QSocketNotifier(readEnd2->socketDescriptor(), QSocketNotifier::Read, this); - connect(notifier2, SIGNAL(activated(int)), SLOT(handleActivated())); + connect(notifier2, SIGNAL(activated(QSocketDescriptor)), SLOT(handleActivated())); } public slots: @@ -284,12 +288,12 @@ void tst_QSocketNotifier::posixSockets() { QSocketNotifier rn(posixSocket, QSocketNotifier::Read); - connect(&rn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&rn, SIGNAL(activated(QSocketDescriptor)), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy readSpy(&rn, &QSocketNotifier::activated); QVERIFY(readSpy.isValid()); // No write notifier, some systems trigger write notification on socket creation, but not all QSocketNotifier en(posixSocket, QSocketNotifier::Exception); - connect(&en, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&en, SIGNAL(activated(QSocketDescriptor)), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy errorSpy(&en, &QSocketNotifier::activated); QVERIFY(errorSpy.isValid()); @@ -306,7 +310,7 @@ void tst_QSocketNotifier::posixSockets() QCOMPARE(buffer, "hello"); QSocketNotifier wn(posixSocket, QSocketNotifier::Write); - connect(&wn, SIGNAL(activated(int)), &QTestEventLoop::instance(), SLOT(exitLoop())); + connect(&wn, SIGNAL(activated(QSocketDescriptor)), &QTestEventLoop::instance(), SLOT(exitLoop())); QSignalSpy writeSpy(&wn, &QSocketNotifier::activated); QVERIFY(writeSpy.isValid()); qt_safe_write(posixSocket, "goodbye", 8); @@ -385,5 +389,61 @@ void tst_QSocketNotifier::asyncMultipleDatagram() #endif // !Q_OS_WINRT } +void tst_QSocketNotifier::activationReason_data() +{ + QTest::addColumn<QSocketNotifier::Type>("type"); + QTest::addRow("read") << QSocketNotifier::Read; + QTest::addRow("write") << QSocketNotifier::Write; + QTest::addRow("exception") << QSocketNotifier::Exception; +} +void tst_QSocketNotifier::activationReason() +{ + QSocketDescriptor fd = 15; + + QFETCH(QSocketNotifier::Type, type); + + QSocketNotifier notifier(fd, type); + auto activation = new QEvent(QEvent::SockAct); + QCoreApplication::postEvent(¬ifier, activation); + + QSocketNotifier::Type notifierType; + connect(¬ifier, &QSocketNotifier::activated, this, + [¬ifierType, fd](QSocketDescriptor sockfd, QSocketNotifier::Type sntype) { + if (sockfd == fd) + notifierType = sntype; + else + qWarning() << "Got an unexpected socket file descriptor:" << qintptr(sockfd); + }); + + QCoreApplication::processEvents(); + QCOMPARE(notifierType, type); +} + +// This test ensures that we can connect QSocketNotifier::activated to a slot taking an integer +// or qintptr. +void tst_QSocketNotifier::legacyConnect() +{ + qintptr fd = 15; + QSocketNotifier notifier(fd, QSocketNotifier::Read); + auto activation = new QEvent(QEvent::SockAct); + QCoreApplication::postEvent(¬ifier, activation); + + bool receivedQIntPtr = false; + connect(¬ifier, &QSocketNotifier::activated, this, [&receivedQIntPtr, fd](qintptr q){ + if (q == fd) + receivedQIntPtr = true; + }); + bool receivedInt = false; + connect(¬ifier, &QSocketNotifier::activated, this, [&receivedInt, fd](int q){ + if (q == fd) + receivedInt = true; + }); + + QCoreApplication::processEvents(); + QVERIFY(receivedQIntPtr); + QVERIFY(receivedInt); +} + + QTEST_MAIN(tst_QSocketNotifier) #include <tst_qsocketnotifier.moc> |