pqTestUtility.cxx 9 KB
Newer Older
Clinton Stimpson's avatar
Clinton Stimpson committed
1 2 3 4 5
/*=========================================================================

   Program: ParaView
   Module:    pqTestUtility.cxx

6
   Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
Clinton Stimpson's avatar
Clinton Stimpson committed
7 8 9
   All rights reserved.

   ParaView is a free software; you can redistribute it and/or modify it
10
   under the terms of the ParaView license version 1.2. 
Clinton Stimpson's avatar
Clinton Stimpson committed
11

12
   See License_v1.2.txt for the full ParaView license.
Clinton Stimpson's avatar
Clinton Stimpson committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   A copy of this license can be obtained by contacting
   Kitware Inc.
   28 Corporate Drive
   Clifton Park, NY 12065
   USA

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/

#include "pqTestUtility.h"

#include <QFileInfo>
36
#include <QDebug>
Clinton Stimpson's avatar
Clinton Stimpson committed
37 38 39 40
#include <QApplication>

#include "pqEventSource.h"
#include "pqEventObserver.h"
41
#include "pqPlayBackEventsDialog.h"
Clinton Stimpson's avatar
Clinton Stimpson committed
42 43 44 45 46 47 48 49
#include "pqRecordEventsDialog.h"
#include "QtTestingConfigure.h"

#ifdef QT_TESTING_WITH_PYTHON
#include "pqPythonEventSource.h"
#include "pqPythonEventObserver.h"
#endif

50
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
Clinton Stimpson committed
51 52 53
pqTestUtility::pqTestUtility(QObject* p) :
  QObject(p)
{
54
  this->PlayingTest = false;
55 56
  this->Translator.addDefaultWidgetEventTranslators(this);
  this->Player.addDefaultWidgetEventPlayers(this);
Clinton Stimpson's avatar
Clinton Stimpson committed
57 58 59 60 61 62 63 64

#ifdef QT_TESTING_WITH_PYTHON
  // add a python event source
  this->addEventSource("py", new pqPythonEventSource(this));
  this->addEventObserver("py", new pqPythonEventObserver(this));
#endif
}

65
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
Clinton Stimpson committed
66 67 68 69
pqTestUtility::~pqTestUtility()
{
}
  
70
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
Clinton Stimpson committed
71 72 73 74 75
pqEventDispatcher* pqTestUtility::dispatcher()
{
  return &this->Dispatcher;
}

76
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
Clinton Stimpson committed
77 78 79 80 81
pqEventPlayer* pqTestUtility::eventPlayer()
{
  return &this->Player;
}

82
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
Clinton Stimpson committed
83 84 85 86 87
pqEventTranslator* pqTestUtility::eventTranslator()
{
  return &this->Translator;
}

88
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
Clinton Stimpson committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102
void pqTestUtility::addEventSource(const QString& fileExtension, pqEventSource* source)
{
  QMap<QString, pqEventSource*>::iterator iter;
  iter = this->EventSources.find(fileExtension);
  if(iter != this->EventSources.end())
    {
    pqEventSource* src = iter.value();
    this->EventSources.erase(iter);
    delete src;
    }
  this->EventSources.insert(fileExtension, source);
  source->setParent(this);
}

103
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
Clinton Stimpson committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
void pqTestUtility::addEventObserver(const QString& fileExtension,
                                     pqEventObserver* observer)
{
  QMap<QString, pqEventObserver*>::iterator iter;
  iter = this->EventObservers.find(fileExtension);
  if(iter != this->EventObservers.end() && iter.value() != observer)
    {
    pqEventObserver* src = iter.value();
    this->EventObservers.erase(iter);
    delete src;
    }
  if(iter != this->EventObservers.end() && iter.value() == observer)
    {
    return;
    }
  this->EventObservers.insert(fileExtension, observer);
  observer->setParent(this);
  

}

125
//-----------------------------------------------------------------------------
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
void pqTestUtility::openPlayerDialog()
{
  pqPlayBackEventsDialog* dialog = new pqPlayBackEventsDialog(this->Player,
                                                              this->Dispatcher,
                                                              this,
                                                              QApplication::activeWindow());
  dialog->exec();
}

//-----------------------------------------------------------------------------
void pqTestUtility::stopTests()
{
  this->PlayingTest = false;
  this->Dispatcher.stop();
}

//-----------------------------------------------------------------------------
143
bool pqTestUtility::playTests(const QString& filename)
Clinton Stimpson's avatar
Clinton Stimpson committed
144
{
145 146
  QStringList files;
  files << filename;
147
  return this->playTests(files);
Clinton Stimpson's avatar
Clinton Stimpson committed
148 149
}

150 151
//-----------------------------------------------------------------------------
bool pqTestUtility::playTests(const QStringList& filenames)
152
{
153
  if (this->PlayingTest)
154
    {
155 156
    qCritical("playTests() cannot be called recursively.");
    return false;
157
    }
158

159 160
  emit this->started();

161 162 163 164 165
  this->PlayingTest = true;

  bool success = true;
  foreach (QString filename, filenames)
    {
166 167 168 169
    if(!this->playingTest())
      {
      break;
      }
170
    QFileInfo info(filename);
171
    emit this->started(filename);
172 173 174 175 176
    QString suffix = info.completeSuffix();
    QMap<QString, pqEventSource*>::iterator iter;
    iter = this->EventSources.find(suffix);
    if(info.isReadable() && iter != this->EventSources.end())
      {
177 178
      iter.value()->setContent(filename);

179 180 181 182
//      QEventLoop loop;
//      QTimer::singleShot(100, &loop, SLOT(quit()));
//      loop.exec();
      QApplication::processEvents();
183 184 185 186 187
      if (!this->Dispatcher.playEvents(*iter.value(), this->Player))
        {
        // dispatcher returned failure, don't continue with rest of the tests
        // and flag error.
        success = false;
188
        emit this->stopped(info.fileName(), success);
189 190
        break;
        }
191
      emit this->stopped(info.fileName(), success);
192 193 194
      }
    }
  this->PlayingTest = false;
195 196

  emit this->stopped();
197
  return success;
198 199
}

200
//-----------------------------------------------------------------------------
Clinton Stimpson's avatar
Clinton Stimpson committed
201 202
void pqTestUtility::recordTests(const QString& filename)
{
Clinton Stimpson's avatar
 
Clinton Stimpson committed
203 204 205 206 207 208 209 210 211 212 213 214
#if defined(Q_WS_MAC)
  // check for native or non-native menu bar.
  // testing framework doesn't work with native menu bar, so let's warn if we
  // get that.
  if(!getenv("QT_MAC_NO_NATIVE_MENUBAR"))
    {
    qWarning("Recording menu events for native Mac menus doesn't work.\n"
             "Set the QT_MAC_NO_NATIVE_MENUBAR environment variable to"
             " correctly record menus");
    }
#endif

Clinton Stimpson's avatar
Clinton Stimpson committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  QMap<QString, pqEventObserver*>::iterator iter;

  QFileInfo info(filename);
  QString suffix = info.completeSuffix();
  pqEventObserver* observer = NULL;
  iter = this->EventObservers.find(suffix);
  if(iter != this->EventObservers.end())
    {
    observer = iter.value();
    }

  if(!observer)
    {
    // cannot find observer for type of file
    return;
    }

  pqRecordEventsDialog* dialog = new pqRecordEventsDialog(this->Translator,
                                          *observer,
                                          filename,
                                          QApplication::activeWindow());
236
  dialog->setAttribute(Qt::WA_QuitOnClose, false);
Clinton Stimpson's avatar
Clinton Stimpson committed
237 238 239
  dialog->show();
}

240 241 242 243 244 245 246 247 248 249 250
// ----------------------------------------------------------------------------
void pqTestUtility::addObjectStateProperty(QObject* object, const QString& property)
{
  this->ObjectStateProperty[object] = property;
}

// ----------------------------------------------------------------------------
QMap<QObject*, QString> pqTestUtility::objectStateProperty() const
{
  return this->ObjectStateProperty;
}
251
//-----------------------------------------------------------------------------
252 253
void pqTestUtility::addDataDirectory(const QString& label, const QDir& path)
{
254
  this->DataDirectories[label] = path;
255 256
}

257
//-----------------------------------------------------------------------------
258 259
void pqTestUtility::removeDataDirectory(const QString& label)
{
260
  this->DataDirectories.remove(label);
261 262
}

263
//-----------------------------------------------------------------------------
264 265
QString pqTestUtility::convertToDataDirectory(const QString& file)
{
266 267 268
  QString normalized_file = file;
  QMap<QString, QDir>::iterator iter;
  for(iter = this->DataDirectories.begin(); iter != this->DataDirectories.end(); ++iter)
269
    {
270 271 272 273 274 275
    QString rel_file = iter.value().relativeFilePath(file);
    if(!rel_file.contains(".."))
      {
      normalized_file = QString("${%1}/%2").arg(iter.key()).arg(rel_file);
      break;
      }
276
    }
277
  return normalized_file;
278 279
}

280
//-----------------------------------------------------------------------------
281 282
QString pqTestUtility::convertFromDataDirectory(const QString& file)
{
283 284 285
  QString filename = file;
  QMap<QString, QDir>::iterator iter;
  for(iter = this->DataDirectories.begin(); iter != this->DataDirectories.end(); ++iter)
286
    {
287 288 289 290 291 292
    QString label = QString("${%1}").arg(iter.key());
    if(filename.contains(label))
      {
      filename.replace(label, iter.value().absolutePath());
      break;
      }
293
    }
294
  return filename;
295
}