Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/UIElements/Qt4/QtUIFactory.cpp

    r163110 r08d042  
    3333#endif
    3434
     35#include <cassert>
    3536#include <iostream>
    36 #include <cassert>
     37#include <fstream>
     38#include <string.h>
     39
     40#include <boost/filesystem/path.hpp>
    3741
    3842#include <Qt/qapplication.h>
     
    4347#include "UIElements/Qt4/QtDialog.hpp"
    4448
     49// boost::python uses placement new which is incompatible with MemDebug.
     50#ifdef HAVE_PYTHON
     51#include "Python/PythonScripting.hpp"
     52#endif
     53
    4554#include "CodePatterns/MemDebug.hpp"
    4655
    47 #include "version.h"
    4856#include "Helpers/defs.hpp"
    4957
    5058using namespace std;
    5159
    52 QtUIFactory::QtUIFactory() :
    53   argc(1)
     60QtUIFactory::QtUIFactory(int _argc, char **_argv) :
     61    argc(1),
     62    argv(new char*[1]),
     63    testlauncher_Interrupted(false),
     64    testlauncher_thread(NULL)
    5465{
    55   // For now we just fake the command line parameters to make QT happy
    56   argv = new char*[1];
    57   argv[0] = new char[256];
    58   strcpy(argv[0],MOLECUILDERVERSION);
    59   app = new QApplication(argc,argv);
     66
     67  // check whether we are in test mode
     68  if ((_argc > 1) && (isTestMode(_argv[1]))) {
     69#ifdef HAVE_BOOST_THREAD_HPP
     70    std::vector<std::string> scripts;
     71    scripts.reserve(_argc-1);
     72    for (int i=2;i<_argc;++i)
     73      scripts.push_back(std::string(_argv[i]));
     74
     75    // check for line-by-line execution
     76    const bool SingleLineStepping = (strncmp(&_argv[1][6], "single", 6) == 0);
     77
     78    // prepare an extra thread
     79    std::cout << "TESTLAUNCHER: Preparing " << std::endl;
     80    testlauncher_thread = new boost::thread(
     81        boost::bind(&QtUIFactory::testrun, this, scripts, SingleLineStepping));
     82#else
     83    std::cerr << "Boost::thread support missing! Cannot launch test scripts.\n";
     84#endif
     85    // use fake commands to not pass test stuff
     86    const int length = strlen(_argv[0]);
     87    argv[0] = new char[length];
     88    strncpy(_argv[0],_argv[0], length);
     89    app = new QApplication(argc,argv);
     90  } else {
     91    app = new QApplication(_argc,_argv);
     92  }
    6093}
    6194
    6295QtUIFactory::~QtUIFactory()
    6396{
    64   //delete app;
    65   delete [] argv[0];
    66   delete [] argv;
     97  if (testlauncher_thread != NULL) {
     98    // notify testlauncher_thread thread that we wish to terminate
     99    testlauncher_thread->interrupt();
     100    // wait till it ends
     101    testlauncher_thread->join();
     102    // and remove
     103    delete testlauncher_thread;
     104  }
     105  // free fake command line argument arrays
     106  delete[] argv[0];
     107  delete[] argv;
    67108}
    68109
     
    75116}
    76117
    77 QtUIFactory::description::description() :
    78     UIFactory::factoryDescription("Qt4")
     118QtUIFactory::description::description(int _argc, char **_argv) :
     119    UIFactory::factoryDescription("Qt4"),
     120    argc(_argc),
     121    argv(_argv)
    79122{}
    80123
     
    83126
    84127UIFactory* QtUIFactory::description::makeFactory(){
    85   return new QtUIFactory();
     128  return new QtUIFactory(argc, argv);
    86129}
     130
     131bool QtUIFactory::isTestMode(const char *_argument)
     132{
     133  return (strncmp(_argument,"--test", 6) == 0);
     134}
     135
     136void QtUIFactory::testrun(const std::vector<std::string> _scripts, const bool _singleLineStepping) const
     137{
     138  std::cout << "TESTLAUNCHER: Waiting for GUI to set up" << std::endl;
     139  testlauncher_sleep(boost::posix_time::seconds(3));
     140
     141  std::vector<std::string>::const_iterator scriptiter = _scripts.begin();
     142  do {
     143    // then launch script
     144    std::cout << "TESTLAUNCHER: Launching script " << *scriptiter
     145        << (_singleLineStepping ? " line by line." : ".") <<std::endl;
     146    if (_singleLineStepping) {
     147      boost::filesystem::path scriptfilename(*scriptiter);
     148      ASSERT( boost::filesystem::exists(scriptfilename),
     149          "QtUIFactory::testrun() - given testmode script file "
     150          +toString(scriptfilename.string())+" does not exist.");
     151      std::ifstream scriptfile(scriptfilename.string().c_str());
     152      std::string scriptfile_line;
     153      const std::string testmode("testmode, line nr.");
     154      unsigned int line_nr = 0;
     155      while(std::getline(scriptfile, scriptfile_line)) {
     156        std::string scriptname = testmode+toString(++line_nr);
     157        executePythonScript(scriptfile_line, scriptname);
     158      }
     159    } else {
     160      executePythonScriptFile(*scriptiter);
     161    }
     162    ++scriptiter;
     163
     164    std::cout << "TESTLAUNCHER: Sleeping after script" << std::endl;
     165    testlauncher_sleep(boost::posix_time::seconds(1.5));
     166
     167  } while ((scriptiter != _scripts.end()) && (!testlauncher_Interrupted));
     168
     169  // send quit signal
     170  std::cout << "TESTLAUNCHER: Quitting" << std::endl;
     171  app->quit();
     172}
     173
     174void QtUIFactory::testlauncher_sleep(const boost::posix_time::time_duration& _period) const
     175{
     176  try {
     177    // first sleep for four seconds
     178#if BOOST_VERSION < 105000
     179    testlauncher_thread->sleep(boost::get_system_time() + _period);
     180#else
     181    boost::this_thread::sleep_for(boost::chrono::seconds(4));
     182#endif
     183  } catch(boost::thread_interrupted &e) {
     184    LOG(2, "INFO: testlauncher thread has received stop signal.");
     185  }
     186}
Note: See TracChangeset for help on using the changeset viewer.