UnitTestImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2006 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <QMessageBox>
00027 # include <QStyleFactory>
00028 #endif
00029 
00030 #include <Base/PyObjectBase.h>
00031 #include <Base/Interpreter.h>
00032 #include <Gui/MainWindow.h>
00033 #include "UnitTestImp.h"
00034 #include "UnitTestPy.h"
00035 
00036 
00037 using namespace TestGui;
00038 
00039 
00040 /* TRANSLATOR TestGui::UnitTestDialog */
00041 
00042 UnitTestDialog* UnitTestDialog::_instance=0;
00043 
00047 UnitTestDialog* UnitTestDialog::instance()
00048 {
00049     // not initialized?
00050     if (!_instance)
00051         _instance = new UnitTestDialog(Gui::getMainWindow());
00052     return _instance;
00053 }
00054 
00058 void UnitTestDialog::destruct ()
00059 {
00060     if (_instance != 0) {
00061         UnitTestDialog *pTmp = _instance;
00062         _instance = 0;
00063         delete pTmp;
00064     }
00065 }
00066 
00070 bool UnitTestDialog::hasInstance()
00071 {
00072     return _instance != 0;
00073 }
00074 
00082 UnitTestDialog::UnitTestDialog(QWidget* parent, Qt::WFlags f)
00083   : QDialog(parent, f)
00084 {
00085     this->setupUi(this);
00086     // As it doesn't seem to be able to change the "Highlight" color for the active colorgroup
00087     // we force e.g. the "Motif" style only for the progressbar to change the color to green or red.
00088     this->progressBar->setStyle(QStyleFactory::create(QString::fromAscii("Motif")));
00089     setProgressColor(QColor(40,210,43)); // a darker green
00090 
00091     // red items
00092     QPalette palette;
00093     palette.setColor(treeViewFailure->foregroundRole(), Qt::red);
00094     treeViewFailure->setPalette(palette);
00095 }
00096 
00100 UnitTestDialog::~UnitTestDialog()
00101 {
00102 }
00103 
00107 void UnitTestDialog::setProgressColor(const QColor& col)
00108 {
00109     QPalette pl = this->progressBar->palette();
00110     pl.setColor(QPalette::Active, QPalette::Highlight, col);
00111     pl.setColor(QPalette::Inactive, QPalette::Highlight, col);
00112     this->progressBar->setPalette(pl);
00113 }
00114 
00118 void UnitTestDialog::on_treeViewFailure_itemDoubleClicked(QTreeWidgetItem * item, int column)
00119 {
00120     QMessageBox::information(this, item->text(0), item->data(0, Qt::UserRole).toString());
00121 }
00122 
00126 void UnitTestDialog::on_helpButton_clicked()
00127 {
00128     QMessageBox::information(this, tr("Help"), tr(
00129         "Enter the name of a callable object which, when called, will return a TestCase."
00130         "Click 'start', and the test thus produced will be run.\n\n"
00131         "Double click on an error in the tree view to see more information about it,"
00132         "including the stack trace."));
00133 }
00134 
00138 void UnitTestDialog::on_aboutButton_clicked()
00139 {
00140     QMessageBox::information(this, tr("About FreeCAD UnitTest"), tr( 
00141         "Copyright (c) Werner Mayer\n\n"
00142         "FreeCAD UnitTest is part of FreeCAD and supports writing Unit Tests for "
00143         "own modules."));
00144 }
00145 
00149 void UnitTestDialog::on_startButton_clicked()
00150 {
00151     reset();
00152     setProgressColor(QColor(40,210,43)); // a darker green
00153     this->startButton->setDisabled(true);
00154     try {
00155         Base::Interpreter().runString(
00156             "import qtunittest, gc\n"
00157             "__qt_test__=qtunittest.QtTestRunner(0,\"\")\n"
00158             "__qt_test__.runClicked()\n"
00159             "del __qt_test__\n"
00160             "gc.collect()\n");
00161     }
00162     catch (const Base::PyException& e) {
00163         std::string msg = e.what();
00164         msg += "\n\n";
00165         msg += e.getStackTrace();
00166         showErrorDialog("Exception", msg.c_str());
00167     }
00168     catch (const Base::Exception& e) {
00169         showErrorDialog("Exception", e.what());
00170     }
00171     this->startButton->setEnabled(true);
00172 }
00173 
00177 void UnitTestDialog::showErrorDialog(const char* title, const char* message)
00178 {
00179     QMessageBox::critical(this, QString::fromLatin1(title), QString::fromLatin1(message));
00180 }
00181 
00185 void UnitTestDialog::reject()
00186 {
00187     reset();
00188     QDialog::reject();
00189 }
00190 
00194 void UnitTestDialog::reset()
00195 {
00196     this->progressBar->reset();
00197     this->treeViewFailure->clear();
00198     this->textLabelRunCt->setText (QString::fromLatin1("<font color=\"#0000ff\">0</font>"));
00199     this->textLabelFailCt->setText(QString::fromLatin1("<font color=\"#0000ff\">0</font>"));
00200     this->textLabelErrCt->setText (QString::fromLatin1("<font color=\"#0000ff\">0</font>"));
00201     this->textLabelRemCt->setText (QString::fromLatin1("<font color=\"#0000ff\">0</font>"));
00202 }
00203 
00207 void UnitTestDialog::addUnitTest(const QString& unit)
00208 {
00209     int ct = this->comboTests->count();
00210     for (int i=0; i<ct; i++) {
00211         if (this->comboTests->itemText(i) == unit)
00212             return;
00213     }
00214 
00215     this->comboTests->addItem(unit);
00216 }
00217 
00221 void UnitTestDialog::setUnitTest(const QString& unit)
00222 {
00223     addUnitTest(unit);
00224     for (int i=0; i<this->comboTests->count(); i++) {
00225         if (this->comboTests->itemText(i) == unit) {
00226             this->comboTests->setCurrentIndex(i);
00227             break;
00228         }
00229     }
00230 }
00231 
00235 QString UnitTestDialog::getUnitTest() const
00236 {
00237     return this->comboTests->currentText();
00238 }
00239 
00243 void UnitTestDialog::setStatusText(const QString& text)
00244 {
00245     this->textLabelStatus->setText(text);
00246 }
00247 
00253 void UnitTestDialog::setProgressFraction(float fraction, const QString& color)
00254 {
00255     if (fraction==0.0f) {
00256         this->progressBar->setRange(0, 100);
00257     }
00258     else {
00259         if (color == QLatin1String("red")) {
00260             setProgressColor(Qt::red);
00261         }
00262 
00263         this->progressBar->setValue((int)(100*fraction));
00264     }
00265 }
00266 
00270 void UnitTestDialog::clearErrorList()
00271 {
00272     this->treeViewFailure->clear();
00273 }
00274 
00279 void UnitTestDialog::insertError(const QString& failure, const QString& details)
00280 {
00281     QTreeWidgetItem* item = new QTreeWidgetItem(treeViewFailure);
00282     item->setText(0,failure);
00283     item->setTextColor(0, Qt::red);
00284     item->setData(0, Qt::UserRole, QVariant(details));
00285 }
00286 
00290 void UnitTestDialog::setRunCount(int ct)
00291 {
00292     this->textLabelRunCt->setText(QString::fromLatin1("<font color=\"#0000ff\">%1</font>").arg(ct));
00293 }
00294 
00298 void UnitTestDialog::setFailCount(int ct)
00299 {
00300     this->textLabelFailCt->setText(QString::fromLatin1("<font color=\"#0000ff\">%1</font>").arg(ct));
00301 }
00302 
00306 void UnitTestDialog::setErrorCount(int ct)
00307 {
00308     this->textLabelErrCt->setText(QString::fromLatin1("<font color=\"#0000ff\">%1</font>").arg(ct));
00309 }
00310 
00314 void UnitTestDialog::setRemainCount(int ct)
00315 {
00316     this->textLabelRemCt->setText(QString::fromLatin1("<font color=\"#0000ff\">%1</font>").arg(ct));
00317 }
00318 
00319 #include "moc_UnitTestImp.cpp"

Generated on Wed Nov 23 19:00:56 2011 for FreeCAD by  doxygen 1.6.1