From c2b04164758d98be42b534cd14faadc07bdb3104 Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Thu, 23 Jan 2014 19:17:52 +0400 Subject: Add the minimal set of the base auto tests Added two types of tests: the constructing tests and the opening tests. The construction tests are checking of the getters to return of default values. Also are checking a correctness work of the constructor of class with different types of a port name. The opening tests are checking the open() method with the valid/invalid open modes and with the valid/invalid port names. The results are compared with the expected behavior (with the error codes and the return values). Change-Id: I503adf629cf613844bf9b96d8704ff017ea5fcdb Reviewed-by: Sergey Belyashov Reviewed-by: Frederik Gladhorn --- tests/auto/auto.pro | 2 +- tests/auto/qserialport/qserialport.pro | 11 ++ tests/auto/qserialport/tst_qserialport.cpp | 219 +++++++++++++++++++++++++++++ 3 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 tests/auto/qserialport/qserialport.pro create mode 100644 tests/auto/qserialport/tst_qserialport.cpp diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index e792a61..14963a2 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -1,2 +1,2 @@ TEMPLATE = subdirs -SUBDIRS += cmake +SUBDIRS += qserialport cmake diff --git a/tests/auto/qserialport/qserialport.pro b/tests/auto/qserialport/qserialport.pro new file mode 100644 index 0000000..c16cd5e --- /dev/null +++ b/tests/auto/qserialport/qserialport.pro @@ -0,0 +1,11 @@ +QT = core testlib +TARGET = tst_qserialport +#CONFIG += testcase + +greaterThan(QT_MAJOR_VERSION, 4) { + QT += serialport +} else { + include($$QTSERIALPORT_PROJECT_ROOT/src/serialport/qt4support/serialport.prf) +} + +SOURCES = tst_qserialport.cpp diff --git a/tests/auto/qserialport/tst_qserialport.cpp b/tests/auto/qserialport/tst_qserialport.cpp new file mode 100644 index 0000000..3e8c700 --- /dev/null +++ b/tests/auto/qserialport/tst_qserialport.cpp @@ -0,0 +1,219 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Denis Shienkov +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSerialPort module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include + +Q_DECLARE_METATYPE(QSerialPort::SerialPortError); +Q_DECLARE_METATYPE(QIODevice::OpenMode); +Q_DECLARE_METATYPE(QIODevice::OpenModeFlag); + +class tst_QSerialPort : public QObject +{ + Q_OBJECT +public: + explicit tst_QSerialPort(); + +private slots: + void initTestCase(); + + void defaultConstruct(); + void constructByName(); + void constructByInfo(); + + void openExisting_data(); + void openExisting(); + void openNotExisting_data(); + void openNotExisting(); + +private: + QString m_senderPortName; + QString m_receiverPortName; + QStringList m_availablePortNames; +}; + +tst_QSerialPort::tst_QSerialPort() +{ +} + +void tst_QSerialPort::initTestCase() +{ + m_senderPortName = QString::fromLocal8Bit(qgetenv("QTEST_SERIALPORT_SENDER")); + m_receiverPortName = QString::fromLocal8Bit(qgetenv("QTEST_SERIALPORT_RECEIVER")); + if (m_senderPortName.isEmpty() || m_receiverPortName.isEmpty()) { +#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) + QSKIP("Test doesn't work because the names of serial ports aren't found in env."); +#else + QSKIP("Test doesn't work because the names of serial ports aren't set found env.", SkipAll); +#endif + } else { + m_availablePortNames << m_senderPortName << m_receiverPortName; + } +} + +void tst_QSerialPort::defaultConstruct() +{ + QSerialPort serialPort; + + QCOMPARE(serialPort.error(), QSerialPort::NoError); + QVERIFY(!serialPort.errorString().isEmpty()); + + // properties + QCOMPARE(serialPort.settingsRestoredOnClose(), true); + QCOMPARE(serialPort.baudRate(), qint32(0)); + QCOMPARE(serialPort.baudRate(QSerialPort::Input), qint32(0)); + QCOMPARE(serialPort.baudRate(QSerialPort::Output), qint32(0)); + QCOMPARE(serialPort.dataBits(), QSerialPort::Data8); + QCOMPARE(serialPort.parity(), QSerialPort::NoParity); + QCOMPARE(serialPort.stopBits(), QSerialPort::OneStop); + QCOMPARE(serialPort.flowControl(), QSerialPort::NoFlowControl); + + QCOMPARE(serialPort.pinoutSignals(), QSerialPort::NoSignal); + QCOMPARE(serialPort.isRequestToSend(), false); + QCOMPARE(serialPort.isDataTerminalReady(), false); + + // QIODevice + QCOMPARE(serialPort.openMode(), QIODevice::NotOpen); + QVERIFY(!serialPort.isOpen()); + QVERIFY(!serialPort.isReadable()); + QVERIFY(!serialPort.isWritable()); + QVERIFY(serialPort.isSequential()); + QCOMPARE(serialPort.canReadLine(), false); + QCOMPARE(serialPort.pos(), qlonglong(0)); + QCOMPARE(serialPort.size(), qlonglong(0)); + QVERIFY(serialPort.atEnd()); + QCOMPARE(serialPort.bytesAvailable(), qlonglong(0)); + QCOMPARE(serialPort.bytesToWrite(), qlonglong(0)); + + char c; + QCOMPARE(serialPort.read(&c, 1), qlonglong(-1)); + QCOMPARE(serialPort.write(&c, 1), qlonglong(-1)); +} + +void tst_QSerialPort::constructByName() +{ + QSerialPort serialPort(m_senderPortName); + QCOMPARE(serialPort.portName(), m_senderPortName); + serialPort.setPortName(m_receiverPortName); + QCOMPARE(serialPort.portName(), m_receiverPortName); +} + +void tst_QSerialPort::constructByInfo() +{ + QSerialPortInfo senderPortInfo(m_senderPortName); + QSerialPort serialPort(senderPortInfo); + QCOMPARE(serialPort.portName(), m_senderPortName); + QSerialPortInfo receiverPortInfo(m_receiverPortName); + serialPort.setPort(receiverPortInfo); + QCOMPARE(serialPort.portName(), m_receiverPortName); +} + +void tst_QSerialPort::openExisting_data() +{ + QTest::addColumn("openMode"); + QTest::addColumn("openResult"); + QTest::addColumn("errorCode"); + + QTest::newRow("NotOpen") << int(QIODevice::NotOpen) << false << QSerialPort::UnsupportedOperationError; + QTest::newRow("ReadOnly") << int(QIODevice::ReadOnly) << true << QSerialPort::NoError; + QTest::newRow("WriteOnly") << int(QIODevice::WriteOnly) << true << QSerialPort::NoError; + QTest::newRow("ReadWrite") << int(QIODevice::ReadWrite) << true << QSerialPort::NoError; + QTest::newRow("Append") << int(QIODevice::Append) << false << QSerialPort::UnsupportedOperationError; + QTest::newRow("Truncate") << int(QIODevice::Truncate) << false << QSerialPort::UnsupportedOperationError; + QTest::newRow("Text") << int(QIODevice::Text) << false << QSerialPort::UnsupportedOperationError; + QTest::newRow("Unbuffered") << int(QIODevice::Unbuffered) << false << QSerialPort::UnsupportedOperationError; +} + +void tst_QSerialPort::openExisting() +{ + QFETCH(int, openMode); + QFETCH(bool, openResult); + QFETCH(QSerialPort::SerialPortError, errorCode); + + foreach (const QString &serialPortName, m_availablePortNames) { + QSerialPort serialPort(serialPortName); + QSignalSpy errorSpy(&serialPort, SIGNAL(error(QSerialPort::SerialPortError))); + QVERIFY(errorSpy.isValid()); + + QCOMPARE(serialPort.portName(), serialPortName); + QCOMPARE(serialPort.open(QIODevice::OpenMode(openMode)), openResult); + QCOMPARE(serialPort.isOpen(), openResult); + QCOMPARE(serialPort.error(), errorCode); + + QCOMPARE(errorSpy.count(), 1); + QCOMPARE(qvariant_cast(errorSpy.at(0).at(0)), errorCode); + } +} + +void tst_QSerialPort::openNotExisting_data() +{ + QTest::addColumn("serialPortName"); + QTest::addColumn("openResult"); + QTest::addColumn("errorCode"); + + QTest::newRow("Empty") << QString("") << false << QSerialPort::DeviceNotFoundError; + QTest::newRow("Null") << QString() << false << QSerialPort::DeviceNotFoundError; + QTest::newRow("NotExists") << QString("ABCDEF") << false << QSerialPort::DeviceNotFoundError; +} + +void tst_QSerialPort::openNotExisting() +{ + QFETCH(QString, serialPortName); + QFETCH(bool, openResult); + //QFETCH(QSerialPort::SerialPortError, errorCode); + + QSerialPort serialPort(serialPortName); + + QSignalSpy errorSpy(&serialPort, SIGNAL(error(QSerialPort::SerialPortError))); + QVERIFY(errorSpy.isValid()); + + QCOMPARE(serialPort.portName(), serialPortName); + QCOMPARE(serialPort.open(QIODevice::ReadOnly), openResult); + QCOMPARE(serialPort.isOpen(), openResult); + //QCOMPARE(serialPort.error(), errorCode); + + //QCOMPARE(errorSpy.count(), 1); + //QCOMPARE(qvariant_cast(errorSpy.at(0).at(0)), errorCode); +} + +QTEST_MAIN(tst_QSerialPort) +#include "tst_qserialport.moc" -- cgit v1.2.1