From 77b48d3d956b6de9dab7cb89bc3285eead63f734 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Mon, 5 Feb 2018 21:10:49 +0100 Subject: QCanBusDevice: Add readAllFrames() and clear() functions [ChangeLog][QCanBusDevice] Added functions to read all frames from the receive buffer and to clear the receive or transmit buffer. Task-number: QTBUG-66143 Change-Id: I0b2f9bd0044c688f6a3261ccce8c22b9e58dfea8 Reviewed-by: Denis Shienkov Reviewed-by: Alex Blasche --- tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'tests') diff --git a/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp b/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp index cf18c4a..f6f3d50 100644 --- a/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp +++ b/tests/auto/qcanbusdevice/tst_qcanbusdevice.cpp @@ -143,6 +143,9 @@ private slots: void conf(); void write(); void read(); + void readAll(); + void clearInputBuffer(); + void clearOutputBuffer(); void error(); void cleanupTestCase(); void tst_filtering(); @@ -271,6 +274,61 @@ void tst_QCanBusDevice::read() QVERIFY(frame2.isValid()); } +void tst_QCanBusDevice::readAll() +{ + enum { FrameNumber = 10 }; + device->disconnectDevice(); + QVERIFY(device->connectDevice()); + QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); + + for (int i = 0; i < FrameNumber; ++i) + device->triggerNewFrame(); + + const QVector frames = device->readAllFrames(); + QCOMPARE(FrameNumber, frames.size()); + QVERIFY(!device->framesAvailable()); +} + +void tst_QCanBusDevice::clearInputBuffer() +{ + device->disconnectDevice(); + QVERIFY(!device->clear(QCanBusDevice::Input)); + + QVERIFY(device->connectDevice()); + QTRY_VERIFY_WITH_TIMEOUT(device->state() == QCanBusDevice::ConnectedState, 5000); + + for (int i = 0; i < 10; ++i) + device->triggerNewFrame(); + + QVERIFY(device->clear(QCanBusDevice::Input)); + + QVERIFY(!device->framesAvailable()); +} + +void tst_QCanBusDevice::clearOutputBuffer() +{ + // this test requires buffered writing + device->setWriteBuffered(true); + device->disconnectDevice(); + QVERIFY(!device->clear(QCanBusDevice::Output)); + + QVERIFY(device->connectDevice()); + + // first test buffered writing, frames will be written after some delay + QSignalSpy spy(device.data(), &QCanBusDevice::framesWritten); + for (int i = 0; i < 10; ++i) + device->writeFrame(QCanBusFrame(0x123, "output")); + QTRY_VERIFY_WITH_TIMEOUT(spy.count() == 10, 5000); + + // now test clearing the buffer before the frames are actually written + spy.clear(); + for (int i = 0; i < 10; ++i) + device->writeFrame(QCanBusFrame(0x123, "output")); + + device->clear(QCanBusDevice::Output); + QTRY_VERIFY_WITH_TIMEOUT(spy.count() == 0, 5000); +} + void tst_QCanBusDevice::error() { QSignalSpy spy(device.data(), &QCanBusDevice::errorOccurred); -- cgit v1.2.3