summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/kernel/qsocketnotifier
diff options
context:
space:
mode:
authorAlex Trotsenko <alex1973tr@gmail.com>2020-12-09 20:54:00 +0200
committerAlex Trotsenko <alex1973tr@gmail.com>2020-12-11 18:08:31 +0200
commit6dac45b24647aba4f5274e3908fc7bb661bcbef5 (patch)
tree3ab123dd42223e9883d2954467e57d4b6d2607b5 /tests/auto/corelib/kernel/qsocketnotifier
parent7715b186c79a968950fd1c8e0993641f2f58b3be (diff)
QSocketNotifier: extend API to provide more flexible startup
Technically, having a single constructor limits the use-cases for this class. We should take into account that: - an opened socket descriptor must be available at the moment of construction; - the constructor unconditionally enables the notifier (the possible solution notifier = new QSocketNotifier(...); notifier->setEnabled(false); is suboptimal due to heavy operations inside the event dispatcher); - for these reasons, QSocketNotifier most often cannot be a member of another class (we have an extra allocation and indirect access). This patch addresses this shortcoming by making it possible to set the socket descriptor at a later point: [ChangeLog][QtCore][QSocketNotifier] Added setSocket() and an additional constructor which requires no socket. Change-Id: I2eb2edf33ddafe99e528aac7d3774ade40795e7a Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Diffstat (limited to 'tests/auto/corelib/kernel/qsocketnotifier')
-rw-r--r--tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp b/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
index 9ab540cdf2..79ee7da5b6 100644
--- a/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
+++ b/tests/auto/corelib/kernel/qsocketnotifier/tst_qsocketnotifier.cpp
@@ -54,6 +54,7 @@ class tst_QSocketNotifier : public QObject
{
Q_OBJECT
private slots:
+ void constructing();
void unexpectedDisconnection();
void mixingWithTimers();
#ifdef Q_OS_UNIX
@@ -85,6 +86,51 @@ static QHostAddress makeNonAny(const QHostAddress &address,
return address;
}
+void tst_QSocketNotifier::constructing()
+{
+ const qintptr fd = 15;
+
+ // Test constructing with no descriptor assigned.
+ {
+ QSocketNotifier notifier(QSocketNotifier::Read);
+
+ QVERIFY(!notifier.isValid());
+ QCOMPARE(notifier.socket(), Q_INT64_C(-1));
+ QCOMPARE(notifier.type(), QSocketNotifier::Read);
+ QVERIFY(!notifier.isEnabled());
+
+ notifier.setEnabled(true);
+ QVERIFY(!notifier.isEnabled());
+
+ notifier.setSocket(fd, true);
+ QVERIFY(notifier.isValid());
+ QCOMPARE(notifier.socket(), fd);
+ QVERIFY(notifier.isEnabled());
+ notifier.setEnabled(false);
+ QVERIFY(!notifier.isEnabled());
+ }
+
+ // Test constructing with the notifications enabled by default.
+ {
+ QSocketNotifier notifier(fd, QSocketNotifier::Write);
+
+ QVERIFY(notifier.isValid());
+ QCOMPARE(notifier.socket(), fd);
+ QCOMPARE(notifier.type(), QSocketNotifier::Write);
+ QVERIFY(notifier.isEnabled());
+
+ notifier.setSocket(fd, false);
+ QVERIFY(!notifier.isEnabled());
+
+ notifier.setEnabled(true);
+ QVERIFY(notifier.isEnabled());
+ notifier.setSocket(-1, true);
+ QVERIFY(!notifier.isValid());
+ QCOMPARE(notifier.socket(), Q_INT64_C(-1));
+ QVERIFY(!notifier.isEnabled());
+ }
+}
+
class UnexpectedDisconnectTester : public QObject
{
Q_OBJECT