TestingDemo.cxx 5.52 KB
Newer Older
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

#include "TestingDemo.h"
#include "ui_TestingDemo.h"


#include "pqTestUtility.h"
#include "pqEventObserver.h"
#include "pqEventSource.h"

#include <QApplication>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QPushButton>
#include <QTextStream>
#include <QXmlStreamAttributes>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QtDebug>

class XMLEventObserver : public pqEventObserver
{
  QXmlStreamWriter* XMLStream;
  QString XMLString;
24

Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
25 26 27 28 29
public:
  XMLEventObserver(QObject* p) : pqEventObserver(p)
  {
  this->XMLStream = NULL;
  }
30

Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  ~XMLEventObserver()
    {
    delete this->XMLStream;
    }

protected:
  virtual void setStream(QTextStream* stream)
    {
    if (this->XMLStream)
      {
      this->XMLStream->writeEndElement();
      this->XMLStream->writeEndDocument();
      delete this->XMLStream;
      this->XMLStream = NULL;
      }
    if (this->Stream)
      {
      *this->Stream << this->XMLString;
      }
    this->XMLString = QString();
    pqEventObserver::setStream(stream);
    if (this->Stream)
      {
      this->XMLStream = new QXmlStreamWriter(&this->XMLString);
      this->XMLStream->setAutoFormatting(true);
      this->XMLStream->writeStartDocument();
      this->XMLStream->writeStartElement("events");
      }
    }
60

Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  virtual void onRecordEvent(const QString& widget, const QString& command,
    const QString& arguments)
    {
    if(this->XMLStream)
      {
      this->XMLStream->writeStartElement("event");
      this->XMLStream->writeAttribute("widget", widget);
      this->XMLStream->writeAttribute("command", command);
      this->XMLStream->writeAttribute("arguments", arguments);
      this->XMLStream->writeEndElement();
      }
    }
};

class XMLEventSource : public pqEventSource
{
  typedef pqEventSource Superclass;
  QXmlStreamReader *XMLStream;
79

Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
80 81 82 83 84
public:
  XMLEventSource(QObject* p): Superclass(p) { this->XMLStream = NULL;}
  ~XMLEventSource() { delete this->XMLStream; }

protected:
85
  virtual void setContent(const QString& xmlfilename)
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
86 87 88 89 90 91 92 93
    {
    delete this->XMLStream;
    this->XMLStream = NULL;

    QFile xml(xmlfilename);
    if (!xml.open(QIODevice::ReadOnly))
      {
      qDebug() << "Failed to load " << xmlfilename;
94
      return;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
95 96 97
      }
    QByteArray data = xml.readAll();
    this->XMLStream = new QXmlStreamReader(data);
98 99 100 101
    /* This checked for valid event objects, but also caused the first event
     * to get dropped. Commenting this out in the example. If you wish to report
     * empty XML test files a flag indicating whether valid events were found is
     * probably the best way to go.
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
102 103 104 105 106 107 108 109 110 111
    while (!this->XMLStream->atEnd())
      {
      QXmlStreamReader::TokenType token = this->XMLStream->readNext();
      if (token == QXmlStreamReader::StartElement)
        {
        if (this->XMLStream->name() == "event")
          {
          break;
          }
        }
112
      } */
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
113 114 115 116
    if (this->XMLStream->atEnd())
      {
      qDebug() << "Invalid xml" << endl;
      }
117
    return;
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
118
    }
119

120
  int getNextEvent(QString& widget, QString& command, QString&
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    arguments)
    {
    if (this->XMLStream->atEnd())
      {
      return DONE;
      }
    while (!this->XMLStream->atEnd())
      {
      QXmlStreamReader::TokenType token = this->XMLStream->readNext();
      if (token == QXmlStreamReader::StartElement)
        {
        if (this->XMLStream->name() == "event")
          {
          break;
          }
        }
      }
    if (this->XMLStream->atEnd())
      {
      return DONE;
      }
    widget = this->XMLStream->attributes().value("widget").toString();
    command = this->XMLStream->attributes().value("command").toString();
    arguments = this->XMLStream->attributes().value("arguments").toString();
    return SUCCESS;
    }
};

//-----------------------------------------------------------------------------
TestingDemo::TestingDemo()
{
  Ui::TestingDemo ui;
  ui.setupUi(this);

  QObject::connect(ui.actionRecord, SIGNAL(triggered()), this, SLOT(record()));
  QObject::connect(ui.actionPlay, SIGNAL(triggered()), this, SLOT(play()));
  QObject::connect(ui.actionOpen_Dialog, SIGNAL(triggered()), this, SLOT(popup()));

  this->TestUtility = new pqTestUtility(this);
  this->TestUtility->addEventObserver("xml", new XMLEventObserver(this));
  this->TestUtility->addEventSource("xml", new XMLEventSource(this));
}

//-----------------------------------------------------------------------------
TestingDemo::~TestingDemo()
{
  delete this->TestUtility;
}

//-----------------------------------------------------------------------------
void TestingDemo::record()
{
  QString filename = QFileDialog::getSaveFileName (this, "Test File Name",
    QString(), "XML Files (*.xml)");
  if (!filename.isEmpty())
    {
    this->TestUtility->recordTests(filename);
    }
}

//-----------------------------------------------------------------------------
void TestingDemo::play()
{
  QString filename = QFileDialog::getOpenFileName (this, "Test File Name",
    QString(), "XML Files (*.xml)");
  if (!filename.isEmpty())
    {
    this->TestUtility->playTests(filename);
    }
}

//-----------------------------------------------------------------------------
void TestingDemo::popup()
{
  QDialog dialog;
  QHBoxLayout* hbox = new QHBoxLayout(&dialog);
  QPushButton button("Click to Close", &dialog);
  hbox->addWidget(&button);
  QObject::connect(&button, SIGNAL(clicked()), &dialog, SLOT(accept()));
  dialog.exec();
}

//-----------------------------------------------------------------------------
int main(int argc, char* argv[])
{
  QApplication app(argc, argv);
  TestingDemo mainWindow;
  mainWindow.show();
  return app.exec();
}