Commit e1580e63 authored by Utkarsh Ayachit's avatar Utkarsh Ayachit
Browse files

Added example for testing.

parent 6083646b
......@@ -192,4 +192,4 @@ INSTALL(TARGETS QtTesting
LIBRARY DESTINATION ${QtTesting_INSTALL_LIB_DIR} COMPONENT Runtime
ARCHIVE DESTINATION ${QtTesting_INSTALL_LIB_DIR} COMPONENT Development)
add_subdirectory(Examples)
SET (UI_FILES
TestingDemo.ui
)
SET (SOURCE_FILES
TestingDemo.cxx
TestingDemo.h
)
SET (MOC_FILES
TestingDemo.h
)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
QT4_WRAP_UI (UI_BUILT_SOURCES ${UI_FILES})
QT4_WRAP_CPP(MOC_BUILT_SOURCES ${MOC_FILES})
add_executable (TestingDemo
${SOURCE_FILES}
${UI_BUILT_SOURCES}
${MOC_BUILT_SOURCES}
)
target_link_libraries(TestingDemo
QtTesting)
#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;
public:
XMLEventObserver(QObject* p) : pqEventObserver(p)
{
this->XMLStream = NULL;
}
~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");
}
}
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;
public:
XMLEventSource(QObject* p): Superclass(p) { this->XMLStream = NULL;}
~XMLEventSource() { delete this->XMLStream; }
protected:
virtual void setContent(const QString& xmlfilename)
{
delete this->XMLStream;
this->XMLStream = NULL;
QFile xml(xmlfilename);
if (!xml.open(QIODevice::ReadOnly))
{
qDebug() << "Failed to load " << xmlfilename;
return;
}
QByteArray data = xml.readAll();
this->XMLStream = new QXmlStreamReader(data);
while (!this->XMLStream->atEnd())
{
QXmlStreamReader::TokenType token = this->XMLStream->readNext();
if (token == QXmlStreamReader::StartElement)
{
if (this->XMLStream->name() == "event")
{
break;
}
}
}
if (this->XMLStream->atEnd())
{
qDebug() << "Invalid xml" << endl;
}
}
int XMLEventSource::getNextEvent(QString& widget, QString& command, QString&
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();
}
#include <QMainWindow>
class pqTestUtility;
class TestingDemo : public QMainWindow
{
Q_OBJECT
public:
TestingDemo();
~TestingDemo();
protected slots:
void record();
void play();
void popup();
private:
Q_DISABLE_COPY(TestingDemo)
pqTestUtility *TestUtility;
};
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TestingDemo</class>
<widget class="QMainWindow" name="TestingDemo">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>483</width>
<height>392</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QTreeWidget" name="treeWidget">
<column>
<property name="text">
<string>Items</string>
</property>
</column>
<item>
<property name="text">
<string>Item0</string>
</property>
</item>
<item>
<property name="text">
<string>Item1</string>
</property>
</item>
<item>
<property name="text">
<string>Item2</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="spinBox"/>
</item>
<item row="2" column="0" colspan="2">
<widget class="QSlider" name="horizontalSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>483</width>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menu_File">
<property name="title">
<string>&amp;File</string>
</property>
<addaction name="actionOpen_Dialog"/>
<addaction name="actionE_xit"/>
</widget>
<widget class="QMenu" name="menuTesting">
<property name="title">
<string>Testing</string>
</property>
<addaction name="actionRecord"/>
<addaction name="actionPlay"/>
</widget>
<addaction name="menu_File"/>
<addaction name="menuTesting"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionOpen_Dialog">
<property name="text">
<string>Popup Dialog</string>
</property>
</action>
<action name="actionE_xit">
<property name="text">
<string>E&amp;xit</string>
</property>
</action>
<action name="actionRecord">
<property name="text">
<string>Record</string>
</property>
</action>
<action name="actionPlay">
<property name="text">
<string>Play</string>
</property>
</action>
</widget>
<resources/>
<connections>
<connection>
<sender>actionE_xit</sender>
<signal>triggered()</signal>
<receiver>TestingDemo</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>241</x>
<y>195</y>
</hint>
</hints>
</connection>
</connections>
</ui>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment