diff options
author | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2014-09-16 15:29:27 +0200 |
---|---|---|
committer | Frederik Gladhorn <frederik.gladhorn@digia.com> | 2014-09-16 15:29:27 +0200 |
commit | ef8ed357881b212e2debe2ccf975996bca2331c2 (patch) | |
tree | 67cb9ee9821bd1fb9b9813b038ffbc06a7847ddd /tests | |
parent | b111a3a6fb4b6c2892fe3b1c9cdf72d8e8f9ea8d (diff) | |
parent | f1761c1236edce428278f7a9e8aa1091097eaa57 (diff) | |
download | qtserialport-ef8ed357881b212e2debe2ccf975996bca2331c2.tar.gz |
Merge remote-tracking branch 'origin/5.3' into 5.4
Conflicts:
.qmake.conf
Change-Id: I5887622f233275703cca8cc2a5db4aaaccc72d97
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qserialport/tst_qserialport.cpp | 164 |
1 files changed, 160 insertions, 4 deletions
diff --git a/tests/auto/qserialport/tst_qserialport.cpp b/tests/auto/qserialport/tst_qserialport.cpp index 9a7cf6d..d296f6d 100644 --- a/tests/auto/qserialport/tst_qserialport.cpp +++ b/tests/auto/qserialport/tst_qserialport.cpp @@ -38,6 +38,7 @@ Q_DECLARE_METATYPE(QSerialPort::SerialPortError); Q_DECLARE_METATYPE(QIODevice::OpenMode); Q_DECLARE_METATYPE(QIODevice::OpenModeFlag); +Q_DECLARE_METATYPE(Qt::ConnectionType); class tst_QSerialPort : public QObject { @@ -100,6 +101,12 @@ private slots: void synchronousReadWrite(); + void asynchronousWriteByBytesWritten_data(); + void asynchronousWriteByBytesWritten(); + + void asynchronousWriteByTimer_data(); + void asynchronousWriteByTimer(); + protected slots: void handleBytesWrittenAndExitLoopSlot(qint64 bytesWritten); void handleBytesWrittenAndExitLoopSlot2(qint64 bytesWritten); @@ -455,12 +462,13 @@ void tst_QSerialPort::twoStageSynchronousLoopback() senderPort.waitForBytesWritten(waitMsecs); QTest::qSleep(waitMsecs); receiverPort.waitForReadyRead(waitMsecs); - QCOMPARE(newlineArray.size(), receiverPort.bytesAvailable()); + QCOMPARE(qint64(newlineArray.size()), receiverPort.bytesAvailable()); + receiverPort.write(receiverPort.readAll()); receiverPort.waitForBytesWritten(waitMsecs); QTest::qSleep(waitMsecs); senderPort.waitForReadyRead(waitMsecs); - QCOMPARE(newlineArray.size(), senderPort.bytesAvailable()); + QCOMPARE(qint64(newlineArray.size()), receiverPort.bytesAvailable()); QCOMPARE(newlineArray, senderPort.readAll()); // second stage @@ -468,12 +476,12 @@ void tst_QSerialPort::twoStageSynchronousLoopback() senderPort.waitForBytesWritten(waitMsecs); QTest::qSleep(waitMsecs); receiverPort.waitForReadyRead(waitMsecs); - QCOMPARE(newlineArray.size(), receiverPort.bytesAvailable()); + QCOMPARE(qint64(newlineArray.size()), receiverPort.bytesAvailable()); receiverPort.write(receiverPort.readAll()); receiverPort.waitForBytesWritten(waitMsecs); QTest::qSleep(waitMsecs); senderPort.waitForReadyRead(waitMsecs); - QCOMPARE(newlineArray.size(), senderPort.bytesAvailable()); + QCOMPARE(qint64(newlineArray.size()), receiverPort.bytesAvailable()); QCOMPARE(newlineArray, senderPort.readAll()); } @@ -503,5 +511,153 @@ void tst_QSerialPort::synchronousReadWrite() QCOMPARE(writeData, readData); } +class AsyncReader : public QObject +{ + Q_OBJECT +public: + explicit AsyncReader(QSerialPort &port, Qt::ConnectionType connectionType, int expectedBytesCount) + : serialPort(port), expectedBytesCount(expectedBytesCount) + { + connect(&serialPort, SIGNAL(readyRead()), this, SLOT(receive()), connectionType); + } + +private slots: + void receive() + { + if (serialPort.bytesAvailable() < expectedBytesCount) + return; + tst_QSerialPort::exitLoop(); + } + +private: + QSerialPort &serialPort; + const int expectedBytesCount; +}; + +class AsyncWriterByBytesWritten : public QObject +{ + Q_OBJECT +public: + explicit AsyncWriterByBytesWritten( + QSerialPort &port, Qt::ConnectionType connectionType, const QByteArray &dataToWrite) + : serialPort(port), writeChunkSize(0) + { + writeBuffer.setData(dataToWrite); + writeBuffer.open(QIODevice::ReadOnly); + connect(&serialPort, SIGNAL(bytesWritten(qint64)), this, SLOT(send()), connectionType); + send(); + } + +private slots: + void send() + { + if (writeBuffer.bytesAvailable() > 0) + serialPort.write(writeBuffer.read(++writeChunkSize)); + } + +private: + QSerialPort &serialPort; + QBuffer writeBuffer; + int writeChunkSize; +}; + +void tst_QSerialPort::asynchronousWriteByBytesWritten_data() +{ + QTest::addColumn<Qt::ConnectionType>("readConnectionType"); + QTest::addColumn<Qt::ConnectionType>("writeConnectionType"); + + QTest::newRow("BothQueued") << Qt::QueuedConnection << Qt::QueuedConnection; + QTest::newRow("BothDirect") << Qt::DirectConnection << Qt::DirectConnection; + QTest::newRow("ReadDirectWriteQueued") << Qt::DirectConnection << Qt::QueuedConnection; + QTest::newRow("ReadQueuedWriteDirect") << Qt::QueuedConnection << Qt::DirectConnection; +} + +void tst_QSerialPort::asynchronousWriteByBytesWritten() +{ +#ifdef Q_OS_WIN + clearReceiver(); +#endif + + QFETCH(Qt::ConnectionType, readConnectionType); + QFETCH(Qt::ConnectionType, writeConnectionType); + + QSerialPort receiverPort(m_receiverPortName); + QVERIFY(receiverPort.open(QSerialPort::ReadOnly)); + AsyncReader reader(receiverPort, readConnectionType, alphabetArray.size()); + + QSerialPort senderPort(m_senderPortName); + QVERIFY(senderPort.open(QSerialPort::WriteOnly)); + AsyncWriterByBytesWritten writer(senderPort, writeConnectionType, alphabetArray); + + enterLoop(1); + QVERIFY2(!timeout(), "Timed out when waiting for the read or write."); + QCOMPARE(receiverPort.bytesAvailable(), qint64(alphabetArray.size())); + QCOMPARE(receiverPort.readAll(), alphabetArray); +} + +class AsyncWriterByTimer : public QObject +{ + Q_OBJECT +public: + explicit AsyncWriterByTimer( + QSerialPort &port, Qt::ConnectionType connectionType, const QByteArray &dataToWrite) + : serialPort(port), writeChunkSize(0) + { + writeBuffer.setData(dataToWrite); + writeBuffer.open(QIODevice::ReadOnly); + connect(&timer, SIGNAL(timeout()), this, SLOT(send()), connectionType); + timer.start(0); + } + +private slots: + void send() + { + if (writeBuffer.bytesAvailable() > 0) + serialPort.write(writeBuffer.read(++writeChunkSize)); + else + timer.stop(); + } + +private: + QSerialPort &serialPort; + QBuffer writeBuffer; + int writeChunkSize; + QTimer timer; +}; + +void tst_QSerialPort::asynchronousWriteByTimer_data() +{ + QTest::addColumn<Qt::ConnectionType>("readConnectionType"); + QTest::addColumn<Qt::ConnectionType>("writeConnectionType"); + + QTest::newRow("BothQueued") << Qt::QueuedConnection << Qt::QueuedConnection; + QTest::newRow("BothDirect") << Qt::DirectConnection << Qt::DirectConnection; + QTest::newRow("ReadDirectWriteQueued") << Qt::DirectConnection << Qt::QueuedConnection; + QTest::newRow("ReadQueuedWriteDirect") << Qt::QueuedConnection << Qt::DirectConnection; +} + +void tst_QSerialPort::asynchronousWriteByTimer() +{ +#ifdef Q_OS_WIN + clearReceiver(); +#endif + + QFETCH(Qt::ConnectionType, readConnectionType); + QFETCH(Qt::ConnectionType, writeConnectionType); + + QSerialPort receiverPort(m_receiverPortName); + QVERIFY(receiverPort.open(QSerialPort::ReadOnly)); + AsyncReader reader(receiverPort, readConnectionType, alphabetArray.size()); + + QSerialPort senderPort(m_senderPortName); + QVERIFY(senderPort.open(QSerialPort::WriteOnly)); + AsyncWriterByTimer writer(senderPort, writeConnectionType, alphabetArray); + + enterLoop(1); + QVERIFY2(!timeout(), "Timed out when waiting for the read or write."); + QCOMPARE(receiverPort.bytesAvailable(), qint64(alphabetArray.size())); + QCOMPARE(receiverPort.readAll(), alphabetArray); +} + QTEST_MAIN(tst_QSerialPort) #include "tst_qserialport.moc" |