TesterApplication.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "TesterApplication.h"
00025 #include "Imports.cpp"
00026 
00027 TesterApplication::TesterApplication (int & argc, char ** argv)
00028  :  QCoreApplication (argc, argv), cppUnitException(), timeoutTimer()
00029 {
00030     timeoutTimer.setSingleShot (true);
00031     QObject::connect (&timeoutTimer, SIGNAL (timeout()),
00032                       this, SLOT (timeout()));
00033 }
00034 
00036 int TesterApplication::exec (int maxTime)
00037 {
00038     TesterApplication *app = dynamic_cast <TesterApplication *> (
00039                                             QCoreApplication::instance());
00040     Q_ASSERT (app != 0); // app does not have the correct type.
00041     if (maxTime > 0)
00042         app->timeoutTimer.start (maxTime);
00043 
00044     // Make sure all deferred deletions of QObjects from previous tests are
00045     // performed before running the new one. This is to avoid confusion which
00046     // test is causing trouble in case a dtor crashesh, for example.
00047     QCoreApplication::processEvents (QEventLoop::DeferredDeletion);
00048 
00049     // Now run the loop:
00050     int ret = QCoreApplication::exec();
00051     app->timeoutTimer.stop();
00052 
00053     // Rethrow the catched exception, if any:
00054     if (app->cppUnitException.get()) {
00055         Exception exception (*app->cppUnitException);
00056         app->cppUnitException.reset();
00057         throw exception;
00058     }
00059     return ret;
00060 }
00061 
00063 bool TesterApplication::notify (QObject *receiver, QEvent *event)
00064 {
00065     try {
00066         return QCoreApplication::notify (receiver, event);
00067     } catch (CPPUNIT_NS::Exception e) {
00068         cppUnitException.reset (e.clone());
00069         exit(1);
00070         return false;
00071     }
00072 }
00073 
00074 void TesterApplication::timeout()
00075 {
00076     exit (-1);
00077 }