| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "FCGlobal.h" |
| |
|
| | #include <QFontMetrics> |
| | #include <QMessageBox> |
| | #include <QClipboard> |
| | #include <QApplication> |
| |
|
| |
|
| | #include <Base/Interpreter.h> |
| | #include <Gui/MainWindow.h> |
| |
|
| | #include "UnitTestImp.h" |
| | #include "ui_UnitTest.h" |
| |
|
| |
|
| | using namespace TestGui; |
| |
|
| | |
| |
|
| | UnitTestDialog* UnitTestDialog::_instance = nullptr; |
| |
|
| | |
| | |
| | |
| | UnitTestDialog* UnitTestDialog::instance() |
| | { |
| | |
| | if (!_instance) { |
| | _instance = new UnitTestDialog(Gui::getMainWindow()); |
| | } |
| | return _instance; |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::destruct() |
| | { |
| | if (_instance) { |
| | UnitTestDialog* pTmp = _instance; |
| | _instance = nullptr; |
| | delete pTmp; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | bool UnitTestDialog::hasInstance() |
| | { |
| | return _instance != nullptr; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | UnitTestDialog::UnitTestDialog(QWidget* parent, Qt::WindowFlags f) |
| | : QDialog(parent, f) |
| | , ui(new Ui_UnitTest) |
| | { |
| | ui->setupUi(this); |
| | setupConnections(); |
| |
|
| | setProgressColor(QColor(40, 210, 43)); |
| | ui->progressBar->setAlignment(Qt::AlignCenter); |
| |
|
| | |
| | QPalette palette; |
| | palette.setColor(ui->treeViewFailure->foregroundRole(), Qt::red); |
| | ui->treeViewFailure->setPalette(palette); |
| | } |
| |
|
| | |
| | |
| | |
| | UnitTestDialog::~UnitTestDialog() = default; |
| |
|
| | void UnitTestDialog::setupConnections() |
| | { |
| | connect( |
| | ui->treeViewFailure, |
| | &QTreeWidget::itemDoubleClicked, |
| | this, |
| | &UnitTestDialog::onTreeViewFailureItemDoubleClicked |
| | ); |
| | connect(ui->helpButton, &QPushButton::clicked, this, &UnitTestDialog::onHelpButtonClicked); |
| | connect(ui->aboutButton, &QPushButton::clicked, this, &UnitTestDialog::onAboutButtonClicked); |
| | connect(ui->copyButton, &QPushButton::clicked, this, &UnitTestDialog::onCopyButtonClicked); |
| | connect(ui->startButton, &QPushButton::clicked, this, &UnitTestDialog::onStartButtonClicked); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::setProgressColor(const QColor& col) |
| | { |
| | QString qss = QStringLiteral( |
| | "QProgressBar {\n" |
| | " border: 2px solid grey;\n" |
| | " border-radius: 5px;\n" |
| | "}\n" |
| | "\n" |
| | "QProgressBar::chunk {\n" |
| | " background-color: %1;\n" |
| | "}" |
| | ) |
| | .arg(col.name()); |
| | ui->progressBar->setStyleSheet(qss); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::onTreeViewFailureItemDoubleClicked(QTreeWidgetItem* item, int column) |
| | { |
| | Q_UNUSED(column); |
| |
|
| | QString text = item->data(0, Qt::UserRole).toString(); |
| |
|
| | QMessageBox msgBox(this); |
| | msgBox.setIcon(QMessageBox::Information); |
| | msgBox.setWindowTitle(item->text(0)); |
| | msgBox.setDetailedText(text); |
| |
|
| | |
| | if (text.count(QLatin1Char('\n')) > 20) { |
| | QStringList lines = text.split(QLatin1Char('\n')); |
| | lines.erase(lines.begin() + 20, lines.end()); |
| | text = lines.join(QLatin1String("\n")); |
| | } |
| | if (text.size() > 1000) { |
| | text = text.left(1000); |
| | } |
| |
|
| | msgBox.setText(text); |
| | msgBox.exec(); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::onHelpButtonClicked() |
| | { |
| | QMessageBox::information( |
| | this, |
| | tr("Help"), |
| | tr("Enter the name of a callable object which, when called, will return a TestCase.\n" |
| | "Click 'start', and the test thus produced will be run.\n\n" |
| | "Double click on an error in the tree view to see more information about it, " |
| | "including the stack trace.") |
| | ); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::onAboutButtonClicked() |
| | { |
| | QMessageBox::information( |
| | this, |
| | tr("About FreeCAD UnitTest"), |
| | tr("Copyright (c) Werner Mayer\n\n" |
| | "FreeCAD UnitTest is part of FreeCAD and supports writing Unit Tests for " |
| | "ones own modules.") |
| | ); |
| | } |
| |
|
| | void UnitTestDialog::onCopyButtonClicked() |
| | { |
| | QString text; |
| | QTreeWidgetItemIterator it(ui->treeViewFailure); |
| | while (*it) { |
| | text += (*it)->data(0, Qt::UserRole).toString() + QStringLiteral("\n\n"); |
| | ++it; |
| | } |
| | if (text.isEmpty()) { |
| | return; |
| | } |
| | QApplication::clipboard()->setText(text); |
| | setStatusText(tr("Errors copied to clipboard")); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::onStartButtonClicked() |
| | { |
| | reset(); |
| | setProgressColor(QColor(40, 210, 43)); |
| | ui->startButton->setDisabled(true); |
| | try { |
| | Base::Interpreter().runString( |
| | "import qtunittest, gc\n" |
| | "__qt_test__=qtunittest.QtTestRunner(0,\"\")\n" |
| | "__qt_test__.runClicked()\n" |
| | "del __qt_test__\n" |
| | "gc.collect()\n" |
| | ); |
| | } |
| | catch (const Base::PyException& e) { |
| | std::string msg = e.what(); |
| | msg += "\n\n"; |
| | msg += e.getStackTrace(); |
| | showErrorDialog("Exception", msg.c_str()); |
| | } |
| | catch (const Base::Exception& e) { |
| | showErrorDialog("Exception", e.what()); |
| | } |
| | catch (const std::exception& e) { |
| | showErrorDialog("C++ standard exception", e.what()); |
| | } |
| | catch (...) { |
| | showErrorDialog("Unknown exception", "Unknown exception raised"); |
| | } |
| | ui->startButton->setEnabled(true); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::showErrorDialog(const char* title, const char* message) |
| | { |
| | QMessageBox::critical(this, QString::fromLatin1(title), QString::fromLatin1(message)); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::reject() |
| | { |
| | reset(); |
| | QDialog::reject(); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::reset() |
| | { |
| | ui->progressBar->reset(); |
| | ui->treeViewFailure->clear(); |
| | ui->textLabelRunCt->setText(QStringLiteral("<font color=\"#0000ff\">0</font>")); |
| | ui->textLabelFailCt->setText(QStringLiteral("<font color=\"#0000ff\">0</font>")); |
| | ui->textLabelErrCt->setText(QStringLiteral("<font color=\"#0000ff\">0</font>")); |
| | ui->textLabelRemCt->setText(QStringLiteral("<font color=\"#0000ff\">0</font>")); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::addUnitTest(const QString& unit) |
| | { |
| | int ct = ui->comboTests->count(); |
| | for (int i = 0; i < ct; i++) { |
| | if (ui->comboTests->itemText(i) == unit) { |
| | return; |
| | } |
| | } |
| |
|
| | ui->comboTests->addItem(unit); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::setUnitTest(const QString& unit) |
| | { |
| | addUnitTest(unit); |
| | for (int i = 0; i < ui->comboTests->count(); i++) { |
| | if (ui->comboTests->itemText(i) == unit) { |
| | ui->comboTests->setCurrentIndex(i); |
| | break; |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::clearUnitTests() |
| | { |
| | ui->comboTests->clear(); |
| | } |
| |
|
| | |
| | |
| | |
| | QString UnitTestDialog::getUnitTest() const |
| | { |
| | return ui->comboTests->currentText(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | bool UnitTestDialog::runCurrentTest() |
| | { |
| | clearErrorList(); |
| | onStartButtonClicked(); |
| | int count = ui->treeViewFailure->topLevelItemCount(); |
| | reject(); |
| | return (count == 0); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::setStatusText(const QString& text) |
| | { |
| | QFontMetrics fm(font()); |
| | int labelWidth = ui->textLabelStatus->width() - 10; |
| | ui->textLabelStatus->setText(fm.elidedText(text, Qt::ElideMiddle, labelWidth)); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | void UnitTestDialog::setProgressFraction(float fraction, const QString& color) |
| | { |
| | if (fraction == 0.0f) { |
| | ui->progressBar->setRange(0, 100); |
| | } |
| | else { |
| | if (color == QLatin1String("red")) { |
| | setProgressColor(Qt::red); |
| | } |
| |
|
| | ui->progressBar->setValue((int)(100 * fraction)); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::clearErrorList() |
| | { |
| | ui->treeViewFailure->clear(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | void UnitTestDialog::insertError(const QString& failure, const QString& details) |
| | { |
| | QTreeWidgetItem* item = new QTreeWidgetItem(ui->treeViewFailure); |
| | item->setText(0, failure); |
| | item->setForeground(0, Qt::red); |
| | item->setData(0, Qt::UserRole, QVariant(details)); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::setRunCount(int ct) |
| | { |
| | ui->textLabelRunCt->setText(QStringLiteral("<font color=\"#0000ff\">%1</font>").arg(ct)); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::setFailCount(int ct) |
| | { |
| | ui->textLabelFailCt->setText(QStringLiteral("<font color=\"#0000ff\">%1</font>").arg(ct)); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::setErrorCount(int ct) |
| | { |
| | ui->textLabelErrCt->setText(QStringLiteral("<font color=\"#0000ff\">%1</font>").arg(ct)); |
| | } |
| |
|
| | |
| | |
| | |
| | void UnitTestDialog::setRemainCount(int ct) |
| | { |
| | ui->textLabelRemCt->setText(QStringLiteral("<font color=\"#0000ff\">%1</font>").arg(ct)); |
| | } |
| |
|
| | #include "moc_UnitTestImp.cpp" |
| |
|