diff options
author | kuzulis <scapig2@yandex.ru> | 2011-10-22 21:28:28 +0400 |
---|---|---|
committer | kuzulis <kuzulis@kuzulis.localdomain> | 2011-10-22 21:28:28 +0400 |
commit | c7fc8a378e2b239099aab675d4f0f7d922d4db01 (patch) | |
tree | 40ac0a23acfa698417d99197122c84c03a2f334e /tests | |
parent | 0e28b93cbfbc470161584b5f9552dec8103f2de6 (diff) | |
download | qtserialport-c7fc8a378e2b239099aab675d4f0f7d922d4db01.tar.gz |
/tests/guidevtest:
1. modified and simplified structure of the application.
2. now runs the test: "Test Info"
Diffstat (limited to 'tests')
-rw-r--r-- | tests/guidevtest/guidevtest.pro | 10 | ||||
-rw-r--r-- | tests/guidevtest/main.cpp | 1 | ||||
-rw-r--r-- | tests/guidevtest/maindialog.cpp | 336 | ||||
-rw-r--r-- | tests/guidevtest/maindialog.h | 58 | ||||
-rw-r--r-- | tests/guidevtest/maindialog.ui | 112 | ||||
-rw-r--r-- | tests/guidevtest/unittestinfo.cpp | 68 | ||||
-rw-r--r-- | tests/guidevtest/unittests.h | 80 |
7 files changed, 501 insertions, 164 deletions
diff --git a/tests/guidevtest/guidevtest.pro b/tests/guidevtest/guidevtest.pro index d92123f..73888a5 100644 --- a/tests/guidevtest/guidevtest.pro +++ b/tests/guidevtest/guidevtest.pro @@ -22,19 +22,15 @@ include(../../src/src.pri) SOURCES += \ main.cpp \ maindialog.cpp \ - testsdialog.cpp \ - unittestmanager.cpp \ unittestinfo.cpp HEADERS += \ maindialog.h \ - testsdialog.h \ - unittestmanager.h \ - unittestinfo.h + unittests.h FORMS += \ - maindialog.ui \ - testsdialog.ui + maindialog.ui + diff --git a/tests/guidevtest/main.cpp b/tests/guidevtest/main.cpp index f955681..44b9ad1 100644 --- a/tests/guidevtest/main.cpp +++ b/tests/guidevtest/main.cpp @@ -1,4 +1,5 @@ #include <QtGui/QApplication> + #include "maindialog.h" int main(int argc, char *argv[]) diff --git a/tests/guidevtest/maindialog.cpp b/tests/guidevtest/maindialog.cpp index ebab398..3965dc6 100644 --- a/tests/guidevtest/maindialog.cpp +++ b/tests/guidevtest/maindialog.cpp @@ -1,51 +1,194 @@ #include "maindialog.h" #include "ui_maindialog.h" -#include <QtCore/QStateMachine> #include <QtGui/QMessageBox> +#include <QtCore/QSettings> +#include <QtCore/QTimer> +#include <QtCore/QFile> +#include "unittests.h" #include "serialportinfo.h" -#include "unittestmanager.h" -#include "testsdialog.h" +// Logger /* Public methods */ -MainDialog::MainDialog(QWidget *parent) - : QDialog(parent) - , ui(new Ui::MainDialog) +Logger::Logger(QObject *parent) + : QObject(parent), m_file(new QFile(this)) { - ui->setupUi(this); - m_utManager = new UnitTestManager(this); +} + +void Logger::setFileName(const QString &name) +{ + m_file->setFileName(name); +} + +void Logger::addContent(const QString &content, bool clearAll) +{ + QIODevice::OpenMode mode = + QIODevice::WriteOnly | QIODevice::Text; + mode |= (clearAll) ? + QIODevice::Truncate : QIODevice::Append; + + if (m_file->open(mode)) { + m_file->write(content.toLocal8Bit()); + m_file->close(); + } +} + + +// UnitTestBase + +/* Public methods */ + +UnitTestBase::UnitTestBase(UnitID id, Logger *logger, QObject *parent) + : QObject(parent), m_id(id), m_logger(logger) +{ + Q_ASSERT(logger); + m_enableParam = "%1/enable"; +} + +void UnitTestBase::setPair(const QString &src, const QString &dst) +{ + m_srcPort = src; + m_dstPort = dst; +} + +void UnitTestBase::setEnable(bool enable) +{ + QSettings settings; + settings.setValue(m_enableParam.arg(m_id), enable); +} + +bool UnitTestBase::isEnabled() const +{ + QSettings settings; + return settings.value(m_enableParam.arg(m_id)).toBool(); +} + +int UnitTestBase::id() const +{ + return m_id; +} + +QString UnitTestBase::name() const +{ + return m_name; +} + +QString UnitTestBase::description() const +{ + return m_description; +} + + +// UnitTestFactory + +/* Public methods */ + +UnitTestBase *UnitTestFactory::create(UnitTestBase::UnitID id, Logger *logger) +{ + switch (id) { + case UnitTestBase::InfoUnitId: + return new UnitTestInfo(logger); + default:; + } + + return 0; +} + + +// TestsViewModel + +/* Public methods */ + +TestsViewModel::TestsViewModel(const QList<UnitTestBase *> &list, QObject *parent) + : QAbstractListModel(parent) +{ + m_testsList = list; +} + +int TestsViewModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + return m_testsList.count(); +} - m_idleState = new QState(); - m_optionsState = new QState(); - m_runningState = new QState(); +QVariant TestsViewModel::data(const QModelIndex &index, int role) const +{ + if (index.isValid() + && (index.row() < m_testsList.count())) { + UnitTestBase *item = m_testsList.at(index.row()); + if (role == Qt::DisplayRole) + return item->name(); + if (role == Qt::CheckStateRole) + return item->isEnabled() ? Qt::Checked : Qt::Unchecked; + } + return QVariant(); +} - connect(m_idleState,SIGNAL(entered()), this, SLOT(procIdleState())); - connect(m_optionsState,SIGNAL(entered()), this, SLOT(procOptionsState())); - connect(m_runningState,SIGNAL(entered()), this, SLOT(procRunningState())); +QVariant TestsViewModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + Q_UNUSED(section); + Q_UNUSED(orientation); + Q_UNUSED(role); + return QVariant(); +} - // From Idle State to ... - m_idleState->addTransition(ui->optButton, SIGNAL(clicked()), m_optionsState); - m_idleState->addTransition(ui->ctrlButton, SIGNAL(clicked()), m_runningState); +Qt::ItemFlags TestsViewModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags flag = Qt::ItemIsEnabled; + if (index.isValid()) + flag |= Qt::ItemIsUserCheckable | Qt::ItemIsSelectable; + return flag; +} - // From Options State to ... - m_optionsState->addTransition(this, SIGNAL(toIdle()), m_idleState); +bool TestsViewModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + if (index.isValid()) { + UnitTestBase *item = m_testsList.at(index.row()); + if (role == Qt::CheckStateRole) { + bool enable = value.toBool(); + if (item->isEnabled() != enable) { + item->setEnable(enable); + emit dataChanged(index, index); + return true; + } + } + } + return false; +} - // From Running State to ... - m_runningState->addTransition(ui->ctrlButton, SIGNAL(clicked()), m_idleState); - m_runningState->addTransition(this, SIGNAL(toIdle()), m_idleState); - m_runningState->addTransition(m_utManager, SIGNAL(finished()), m_idleState); - m_stateMachine = new QStateMachine(this); - m_stateMachine->addState(m_idleState); - m_stateMachine->addState(m_optionsState); - m_stateMachine->addState(m_runningState); +// MainDialog - m_stateMachine->setInitialState(m_idleState); - m_stateMachine->start(); +/* Public methods */ + +MainDialog::MainDialog(QWidget *parent) + : QDialog(parent), ui(new Ui::MainDialog) + , m_enabledTestsCount(0), m_it(0) +{ + ui->setupUi(this); + + m_logger = new Logger(this); + + fillPairs(); + showSettings(); + createAvailableTests(); + + m_model = new TestsViewModel(m_testsList, this); + ui->listView->setModel(m_model); + + connect(ui->logLineEdit, SIGNAL(textChanged(QString)), + this, SLOT(procLogChanged(QString))); + connect(ui->clearLogCheckBox, SIGNAL(clicked(bool)), + this, SLOT(procClearLogOnStartChanged(bool))); + connect(ui->breakAllCheckBox, SIGNAL(clicked(bool)), + this, SLOT(procBreakAllOnErrorChanged(bool))); + + connect(ui->startButton, SIGNAL(clicked()), + this, SLOT(procStartButtonClick())); } MainDialog::~MainDialog() @@ -55,59 +198,120 @@ MainDialog::~MainDialog() /* Private slots */ -void MainDialog::procIdleState() +void MainDialog::procLogChanged(const QString &log) { - ui->ctrlButton->setText(tr("Start")); - ui->srcBox->setEnabled(true); - ui->dstBox->setEnabled(true); - ui->optButton->setEnabled(true); - procUbdatePortsBox(); + QSettings settings; + settings.setValue(logFileSettingsKey, log); } -void MainDialog::procOptionsState() +void MainDialog::procClearLogOnStartChanged(bool enable) { - TestsDialog dlg(m_utManager); - //dlg.setModal(true); - dlg.exec(); + QSettings settings; + settings.setValue(clearLogOnStartSettingsKey, enable); +} - emit toIdle(); +void MainDialog::procBreakAllOnErrorChanged(bool enable) +{ + QSettings settings; + settings.setValue(breakOnErrorSettingsKey, enable); } -void MainDialog::procRunningState() +void MainDialog::procStartButtonClick() { - if (ui->srcBox->currentText() == ui->dstBox->currentText()) { - QMessageBox msgBox; - msgBox.setIcon(QMessageBox::Warning); - msgBox.setText(tr("You can not choose the source and\n" - "destination the same name.")); - msgBox.exec(); - emit toIdle(); + // Check pair + if (ui->srcComboBox->currentText() == ui->dstComboBox->currentText()) return; + + // Get enabled tests num + m_enabledTestsCount = 0; + foreach (UnitTestBase *test, m_testsList) { + if (test->isEnabled()) + ++m_enabledTestsCount; } - ui->ctrlButton->setText(tr("Stop")); - ui->srcBox->setEnabled(false); - ui->dstBox->setEnabled(false); - ui->optButton->setEnabled(false); + if (!m_enabledTestsCount) + return; - m_utManager->setLogFileName(qApp->applicationDirPath() + "/Test.log"); - m_utManager->setPorts(ui->srcBox->currentText(), - ui->dstBox->currentText()); - m_utManager->start(); + ui->progressBar->setValue(0); + ui->progressBar->setMaximum(m_enabledTestsCount); + + m_logger->setFileName(qApp->applicationDirPath() + + "/" + + ui->logLineEdit->text()); + + // Start run + QString header(tr("\n*** S T A R T E D ***\n")); + m_logger->addContent(header, ui->clearLogCheckBox->isChecked()); + procTestStarted(); + enableUi(false); } -void MainDialog::procUbdatePortsBox() +void MainDialog::procTestStarted() { - ui->srcBox->clear(); - ui->dstBox->clear(); - - foreach(SerialPortInfo inf, SerialPortInfo::availablePorts()) { - if (inf.isValid() && !inf.isBusy()) { - QString s = inf.portName(); - ui->srcBox->addItem(s); - ui->dstBox->addItem(s); - } + UnitTestBase *test = m_testsList.at(m_it++); + if (test->isEnabled()) { + test->setPair(ui->srcComboBox->currentText(), + ui->dstComboBox->currentText()); + QTimer::singleShot(1000, test, SLOT(start())); } + else + procTestStarted(); } +void MainDialog::procTestFinished() +{ + ui->progressBar->setValue(ui->progressBar->maximum() - (--m_enabledTestsCount)); + if (m_enabledTestsCount == 0) { + enableUi(true); + m_it = 0; + + QString header(tr("\n*** S T O P P E D ***\n")); + m_logger->addContent(header); + return; + } + else + procTestStarted(); +} /* Private methods */ + +const QString MainDialog::logFileSettingsKey = "MainDialog/logFileName"; +const QString MainDialog::breakOnErrorSettingsKey = "MainDialog/breakOnError"; +const QString MainDialog::clearLogOnStartSettingsKey = "MainDialog/clearLogOnStart"; + +void MainDialog::showSettings() +{ + QSettings settings; + ui->logLineEdit->setText(settings.value(logFileSettingsKey).toString()); + ui->clearLogCheckBox->setChecked(settings.value(clearLogOnStartSettingsKey).toBool()); + ui->breakAllCheckBox->setChecked(settings.value(clearLogOnStartSettingsKey).toBool()); +} + +// Called only in constructor! +void MainDialog::createAvailableTests() +{ + // + m_testsList.append(UnitTestFactory::create(UnitTestBase::InfoUnitId, m_logger)); + + + foreach(UnitTestBase *test, m_testsList) { + connect(test, SIGNAL(finished()), this, SLOT(procTestFinished())); + } +} + +// Called only in constructor! +void MainDialog::fillPairs() +{ + QStringList list; + foreach (SerialPortInfo inf, SerialPortInfo::availablePorts()) { + if (inf.isValid() && !inf.isBusy()) + list.append(inf.portName()); + } + ui->srcComboBox->addItems(list); + ui->dstComboBox->addItems(list); +} + +void MainDialog::enableUi(bool enable) +{ + ui->scrollArea->setEnabled(enable); + ui->startButton->setEnabled(enable); +} diff --git a/tests/guidevtest/maindialog.h b/tests/guidevtest/maindialog.h index 7de8154..e3aff19 100644 --- a/tests/guidevtest/maindialog.h +++ b/tests/guidevtest/maindialog.h @@ -2,7 +2,7 @@ #define MAINDIALOG_H #include <QtGui/QDialog> -#include <QtCore/QMap> +#include <QtCore/QAbstractListModel> @@ -10,34 +10,60 @@ namespace Ui { class MainDialog; } -class QState; -class QStateMachine; -class UnitTestManager; +class UnitTestBase; -class MainDialog : public QDialog +class TestsViewModel : public QAbstractListModel { Q_OBJECT -signals: - void toIdle(); +public: + explicit TestsViewModel(const QList<UnitTestBase *> &list, QObject *parent = 0); + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + virtual QVariant data(const QModelIndex &index, int role) const; + virtual QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + virtual Qt::ItemFlags flags(const QModelIndex &index) const; + virtual bool setData(const QModelIndex &index, const QVariant &value, + int role = Qt::EditRole); + +private: + QList<UnitTestBase *> m_testsList; +}; + +class Logger; + +class MainDialog : public QDialog +{ + Q_OBJECT public: explicit MainDialog(QWidget *parent = 0); ~MainDialog(); private slots: - void procIdleState(); - void procOptionsState(); - void procRunningState(); - void procUbdatePortsBox(); + void procLogChanged(const QString &log); + void procClearLogOnStartChanged(bool enable); + void procBreakAllOnErrorChanged(bool enable); + + void procStartButtonClick(); + void procTestStarted(); + void procTestFinished(); private: Ui::MainDialog *ui; + TestsViewModel *m_model; + QList<UnitTestBase *> m_testsList; + Logger *m_logger; + int m_enabledTestsCount; + int m_it; + + static const QString logFileSettingsKey; + static const QString breakOnErrorSettingsKey; + static const QString clearLogOnStartSettingsKey; - QState *m_idleState; - QState *m_optionsState; - QState *m_runningState; - QStateMachine *m_stateMachine; - UnitTestManager *m_utManager; + void showSettings(); + void createAvailableTests(); + void fillPairs(); + void enableUi(bool enable); }; #endif // MAINDIALOG_H diff --git a/tests/guidevtest/maindialog.ui b/tests/guidevtest/maindialog.ui index ff57232..94f00f7 100644 --- a/tests/guidevtest/maindialog.ui +++ b/tests/guidevtest/maindialog.ui @@ -6,52 +6,90 @@ <rect> <x>0</x> <y>0</y> - <width>107</width> - <height>220</height> + <width>137</width> + <height>294</height> </rect> </property> <property name="windowTitle"> <string>Quick test</string> </property> - <layout class="QVBoxLayout" name="verticalLayout"> + <layout class="QVBoxLayout" name="verticalLayout_2"> <item> - <widget class="QGroupBox" name="pairBox"> - <property name="title"> - <string>Free for pair:</string> + <widget class="QScrollArea" name="scrollArea"> + <property name="widgetResizable"> + <bool>true</bool> </property> - <layout class="QGridLayout" name="gridLayout"> - <item row="0" column="0"> - <widget class="QLabel" name="srcLabel"> - <property name="text"> - <string>Source:</string> - </property> - </widget> - </item> - <item row="1" column="0"> - <widget class="QComboBox" name="srcBox"/> - </item> - <item row="2" column="0"> - <widget class="QLabel" name="dstLabel"> - <property name="text"> - <string>Destination:</string> - </property> - </widget> - </item> - <item row="3" column="0"> - <widget class="QComboBox" name="dstBox"/> - </item> - </layout> + <widget class="QWidget" name="scrollAreaWidgetContents"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>123</width> + <height>235</height> + </rect> + </property> + <layout class="QVBoxLayout" name="verticalLayout"> + <item> + <widget class="QListView" name="listView"/> + </item> + <item> + <layout class="QGridLayout" name="gridLayout"> + <item row="0" column="0"> + <widget class="QLabel" name="srcLabel"> + <property name="text"> + <string>Src:</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QComboBox" name="srcComboBox"/> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="dstLabel"> + <property name="text"> + <string>Dst:</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QComboBox" name="dstComboBox"/> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="logLabel"> + <property name="text"> + <string>Log:</string> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QLineEdit" name="logLineEdit"> + <property name="text"> + <string/> + </property> + </widget> + </item> + <item row="3" column="0" colspan="2"> + <widget class="QCheckBox" name="clearLogCheckBox"> + <property name="text"> + <string>Clear log on start.</string> + </property> + </widget> + </item> + <item row="4" column="0" colspan="2"> + <widget class="QCheckBox" name="breakAllCheckBox"> + <property name="text"> + <string>Break all on error</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> + </widget> </widget> </item> <item> - <widget class="QPushButton" name="optButton"> - <property name="text"> - <string>Options</string> - </property> - </widget> - </item> - <item> - <widget class="QPushButton" name="ctrlButton"> + <widget class="QPushButton" name="startButton"> <property name="text"> <string>Start</string> </property> @@ -60,7 +98,7 @@ <item> <widget class="QProgressBar" name="progressBar"> <property name="value"> - <number>24</number> + <number>0</number> </property> </widget> </item> diff --git a/tests/guidevtest/unittestinfo.cpp b/tests/guidevtest/unittestinfo.cpp index 61e76f0..03bb462 100644 --- a/tests/guidevtest/unittestinfo.cpp +++ b/tests/guidevtest/unittestinfo.cpp @@ -1,12 +1,12 @@ -#include "unittestinfo.h" +#include "unittests.h" #include "serialportinfo.h" /* Public methods */ -UnitTestInfo::UnitTestInfo(QObject *parent) - : UnitTestBase(UnitTestBase::InfoUnitId, parent) +UnitTestInfo::UnitTestInfo(Logger *logger, QObject *parent) + : UnitTestBase(UnitTestBase::InfoUnitId, logger, parent) { m_name = QString(tr("Info Test")); m_description = QString(tr("Info Test Description")); @@ -16,44 +16,36 @@ UnitTestInfo::UnitTestInfo(QObject *parent) void UnitTestInfo::start() { - bool ret = UnitTestManager::openLog(); - if (!ret) { - emit error(); - return; - } - - QString header(tr("> Test: ID#%1, Name: %2 \n%3\n\n")); + QString header(tr("\n[ Test: ID#%1, Name: %2 ]\n%3\n\n")); header = header .arg(m_id) .arg(m_name) - .arg(UnitTestManager::timestamp()); - - if (UnitTestManager::writeToLog(header)) { - int it = 0; - foreach (SerialPortInfo inf, SerialPortInfo::availablePorts()) { - QString s(tr("Port# %1, name : %2\n" - " location : %3\n" - " description : %4\n" - " valid : %5\n" - " busy : %6\n")); - - s = s - .arg(it++) - .arg(inf.portName()) - .arg(inf.systemLocation()) - .arg(inf.description()) - .arg(inf.isValid()) - .arg(inf.isBusy()); - - ret = UnitTestManager::writeToLog(s); - if (!ret) - break; - } + .arg(QString("timestamp"));/*.arg(UnitTestManager::timestamp());*/ + + m_logger->addContent(header); + + int it = 0; + foreach (SerialPortInfo inf, SerialPortInfo::availablePorts()) { + QString body(tr("Port# %1, name : %2\n" + " location : %3\n" + " description : %4\n" + " valid : %5\n" + " busy : %6\n")); + + body = body + .arg(it++) + .arg(inf.portName()) + .arg(inf.systemLocation()) + .arg(inf.description()) + .arg(inf.isValid()) + .arg(inf.isBusy()); + + m_logger->addContent(body); } - UnitTestManager::closeLog(); - if (ret) - emit finished(); - else - emit error(); + QString trailer(tr("\nFound %1 ports.\n")); + trailer = trailer.arg(it); + m_logger->addContent(trailer); + + emit finished(); } diff --git a/tests/guidevtest/unittests.h b/tests/guidevtest/unittests.h new file mode 100644 index 0000000..89c17a7 --- /dev/null +++ b/tests/guidevtest/unittests.h @@ -0,0 +1,80 @@ +#ifndef UNITTESTS_H +#define UNITTESTS_H + +#include <QtCore/QObject> + + +class QFile; + +class Logger : public QObject +{ + Q_OBJECT +public: + explicit Logger(QObject *parent = 0); + void setFileName(const QString &name); + void addContent(const QString &content, bool clearAll = false); + +private: + QFile *m_file; +}; + +class UnitTestBase : public QObject +{ + Q_OBJECT +signals: + void finished(); + void error(); + +public: + enum UnitID { + InfoUnitId, + + }; + + explicit UnitTestBase(UnitID id, Logger *logger, QObject *parent = 0); + void setPair(const QString &src, const QString &dst); + void setEnable(bool enable); + bool isEnabled() const; + int id() const; + QString name() const; + QString description() const; + +public slots: + virtual void start() = 0; + +protected: + int m_id; + QString m_name; + QString m_description; + QString m_enableParam; + Logger *m_logger; + +private: + QString m_srcPort; + QString m_dstPort; +}; + + +class UnitTestInfo : public UnitTestBase +{ + Q_OBJECT +public: + explicit UnitTestInfo(Logger *logger, QObject *parent = 0); + +public slots: + virtual void start(); +}; + + + + + + +class UnitTestFactory +{ +public: + static UnitTestBase *create(UnitTestBase::UnitID id, Logger *logger); +}; + + +#endif // UNITTESTS_H |