diff options
author | Denis Shienkov <denis.shienkov@gmail.com> | 2014-04-19 16:07:21 +0000 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-05-13 13:17:35 +0200 |
commit | f770a90b011a2aaf978dfa75870cc462fae44fa0 (patch) | |
tree | cb88dbab8af815538a8230061da292a1fdb9351a /tests/auto/qserialport/tst_qserialport.cpp | |
parent | c0fc9aecd67323630011eaa9a1c213eed5bc5387 (diff) | |
download | qtserialport-f770a90b011a2aaf978dfa75870cc462fae44fa0.tar.gz |
Add a unit test to verify flush()
On Windows a test will be skipped since flush() does not work there
Change-Id: Ibda38401aff5f1c50644c6d61f9802217146a4fb
Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com>
Diffstat (limited to 'tests/auto/qserialport/tst_qserialport.cpp')
-rw-r--r-- | tests/auto/qserialport/tst_qserialport.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/auto/qserialport/tst_qserialport.cpp b/tests/auto/qserialport/tst_qserialport.cpp index 1fcd794..7d13732 100644 --- a/tests/auto/qserialport/tst_qserialport.cpp +++ b/tests/auto/qserialport/tst_qserialport.cpp @@ -53,6 +53,24 @@ class tst_QSerialPort : public QObject public: explicit tst_QSerialPort(); + static void enterLoop(int secs) + { + ++loopLevel; + QTestEventLoop::instance().enterLoop(secs); + --loopLevel; + } + + static void exitLoop() + { + if (loopLevel > 0) + QTestEventLoop::instance().exitLoop(); + } + + static bool timeout() + { + return QTestEventLoop::instance().timeout(); + } + private slots: void initTestCase(); @@ -65,12 +83,28 @@ private slots: void openNotExisting_data(); void openNotExisting(); + void flush(); + void doubleFlush(); + +protected slots: + void handleBytesWrittenAndExitLoopSlot(qint64 bytesWritten); + void handleBytesWrittenAndExitLoopSlot2(qint64 bytesWritten); + private: QString m_senderPortName; QString m_receiverPortName; QStringList m_availablePortNames; + + static int loopLevel; + static const QByteArray alphabetArray; + static const QByteArray newlineArray; }; +int tst_QSerialPort::loopLevel = 0; + +const QByteArray tst_QSerialPort::alphabetArray("ABCDEFGHIJKLMNOPQRSTUVWXUZ"); +const QByteArray tst_QSerialPort::newlineArray("\n\r"); + tst_QSerialPort::tst_QSerialPort() { } @@ -215,5 +249,67 @@ void tst_QSerialPort::openNotExisting() //QCOMPARE(qvariant_cast<QSerialPort::SerialPortError>(errorSpy.at(0).at(0)), errorCode); } +void tst_QSerialPort::handleBytesWrittenAndExitLoopSlot(qint64 bytesWritten) +{ + QCOMPARE(bytesWritten, (alphabetArray.size() + newlineArray.size())); + exitLoop(); +} + +void tst_QSerialPort::flush() +{ +#ifdef Q_OS_WIN + QSKIP("flush() does not work on Windows"); +#endif + + QSerialPort serialPort(m_senderPortName); + connect(&serialPort, SIGNAL(bytesWritten(qint64)), this, SLOT(handleBytesWrittenAndExitLoopSlot(qint64))); + QSignalSpy bytesWrittenSpy(&serialPort, SIGNAL(bytesWritten(qint64))); + + QVERIFY(serialPort.open(QIODevice::WriteOnly)); + serialPort.write(alphabetArray + newlineArray); + QCOMPARE(serialPort.bytesToWrite(), (alphabetArray.size() + newlineArray.size())); + serialPort.flush(); + QCOMPARE(serialPort.bytesToWrite(), qint64(0)); + enterLoop(1); + QVERIFY2(!timeout(), "Timed out when waiting for the bytesWritten(qint64) signal."); + QCOMPARE(bytesWrittenSpy.count(), 1); +} + +void tst_QSerialPort::handleBytesWrittenAndExitLoopSlot2(qint64 bytesWritten) +{ + static qint64 bytes = 0; + bytes += bytesWritten; + + QVERIFY(bytesWritten == newlineArray.size() || bytesWritten == alphabetArray.size()); + + if (bytes == (alphabetArray.size() + newlineArray.size())) + exitLoop(); +} + +void tst_QSerialPort::doubleFlush() +{ +#ifdef Q_OS_WIN + QSKIP("flush() does not work on Windows"); +#endif + + QSerialPort serialPort(m_senderPortName); + connect(&serialPort, SIGNAL(bytesWritten(qint64)), this, SLOT(handleBytesWrittenAndExitLoopSlot2(qint64))); + QSignalSpy bytesWrittenSpy(&serialPort, SIGNAL(bytesWritten(qint64))); + + QVERIFY(serialPort.open(QIODevice::WriteOnly)); + serialPort.write(alphabetArray); + QCOMPARE(serialPort.bytesToWrite(), alphabetArray.size()); + serialPort.flush(); + QCOMPARE(serialPort.bytesToWrite(), qint64(0)); + serialPort.write(newlineArray); + QCOMPARE(serialPort.bytesToWrite(), newlineArray.size()); + serialPort.flush(); + QCOMPARE(serialPort.bytesToWrite(), qint64(0)); + + enterLoop(1); + QVERIFY2(!timeout(), "Timed out when waiting for the bytesWritten(qint64) signal."); + QCOMPARE(bytesWrittenSpy.count(), 2); +} + QTEST_MAIN(tst_QSerialPort) #include "tst_qserialport.moc" |