Commit 002b3b38 authored by Benjamin Long's avatar Benjamin Long

ENH: Added unit test to pqEventRecorder

Moved dummyEventObserver and dummyEventSource to pqTest.h.
Thus, every unit test can have acces to a simplify observer and source,
without duplicated the code.
parent 8eefdf67
......@@ -4,10 +4,15 @@ set(KIT ${PROJECT_NAME})
set(TEST_SOURCES
pqEventPlayerTest.cpp
pqEventRecorderTest.cpp
pqEventTranslatorTest.cpp
pqTestUtilityTest.cpp
)
set(TEST_MOC_HEADERS
pqTest.h
)
create_test_sourcelist(Tests ${KIT}CppTests.cxx
${TEST_SOURCES}
)
......@@ -21,8 +26,9 @@ include_directories(
)
QT4_GENERATE_MOCS(${TEST_SOURCES})
QT4_WRAP_CPP( TEST_MOC_SRCS ${TEST_MOC_HEADERS} )
add_executable(${KIT}CppTests ${Tests})
add_executable(${KIT}CppTests ${Tests} ${TEST_MOC_SRCS})
target_link_libraries(${KIT}CppTests ${PROJECT_NAME} ${QT_QTTEST_LIBRARY})
macro(SIMPLE_TEST testname)
......@@ -33,5 +39,6 @@ endmacro()
# Add Tests
#
SIMPLE_TEST( pqEventPlayerTest )
SIMPLE_TEST( pqEventRecorderTest )
SIMPLE_TEST( pqEventTranslatorTest )
SIMPLE_TEST( pqTestUtilityTest )
/*=========================================================================
Program: ParaView
Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
All rights reserved.
ParaView is a free software; you can redistribute it and/or modify it
under the terms of the ParaView license version 1.2.
See License_v1.2.txt for the full ParaView license.
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.
=========================================================================*/
// Qt includes
#include <QApplication>
// QtTesting includes
#include "pqEventObserver.h"
#include "pqEventRecorder.h"
#include "pqTestUtility.h"
#include "pqTest.h"
// ----------------------------------------------------------------------------
class pqEventRecorderTester: public QObject
{
Q_OBJECT
private Q_SLOTS:
void testDefaults();
void testSetFile();
void testSetObserver();
void testSetTranslator();
void testSetContiniousFlush();
void testSetContiniousFlush_data();
void testRecordEvents();
void testRecordEvents_data();
};
// ----------------------------------------------------------------------------
void pqEventRecorderTester::testDefaults()
{
pqEventRecorder eventRecorder;
QIODevice* nullDevice = 0;
pqEventObserver* nullObserver = 0;
pqEventTranslator* nullTranslator = 0;
QCOMPARE(eventRecorder.isRecording(), false);
QCOMPARE(eventRecorder.file(), nullDevice);
QCOMPARE(eventRecorder.observer(), nullObserver);
QCOMPARE(eventRecorder.translator(), nullTranslator);
}
// ----------------------------------------------------------------------------
void pqEventRecorderTester::testSetFile()
{
pqEventRecorder recorder;
QFile file;
recorder.setFile(&file);
QCOMPARE(recorder.file(), &file);
}
// ----------------------------------------------------------------------------
void pqEventRecorderTester::testSetObserver()
{
pqEventRecorder recorder;
pqDummyEventObserver observer;
recorder.setObserver(&observer);
QCOMPARE(recorder.observer(), &observer);
}
// ----------------------------------------------------------------------------
void pqEventRecorderTester::testSetTranslator()
{
pqEventRecorder recorder;
pqEventTranslator translator;
recorder.setTranslator(&translator);
QCOMPARE(recorder.translator(), &translator);
}
// ----------------------------------------------------------------------------
void pqEventRecorderTester::testSetContiniousFlush()
{
pqEventRecorder recorder;
QFETCH(bool, activeObserver);
QFETCH(bool, first);
QFETCH(bool, firstResult);
QFETCH(bool, second);
QFETCH(bool, secondResult);
recorder.setObserver(activeObserver ? new pqDummyEventObserver() : 0);
recorder.setContinuousFlush(first);
QCOMPARE(recorder.continuousFlush(), firstResult);
recorder.setContinuousFlush(second);
QCOMPARE(recorder.continuousFlush(), secondResult);
}
// ----------------------------------------------------------------------------
void pqEventRecorderTester::testSetContiniousFlush_data()
{
QTest::addColumn<bool>("activeObserver");
QTest::addColumn<bool>("first");
QTest::addColumn<bool>("firstResult");
QTest::addColumn<bool>("second");
QTest::addColumn<bool>("secondResult");
QTest::newRow("1") << false << false << false << false << false;
QTest::newRow("2") << false << false << false << true << false;
QTest::newRow("3") << false << true << false << false << false;
QTest::newRow("4") << false << true << false << true << false;
QTest::newRow("5") << true << false << false << false << false;
QTest::newRow("6") << true << false << false << true << true;
QTest::newRow("7") << true << true << true << false << false;
QTest::newRow("8") << true << true << true << true << true;
}
// ----------------------------------------------------------------------------
void pqEventRecorderTester::testRecordEvents()
{
pqEventRecorder recorder;
QFETCH(QObject*, ioDevice);
QFETCH(QObject*, activeObserver);
QFETCH(QObject*, activeTranslator);
QFETCH(bool, continuousFlush);
QFETCH(bool, started);
QFETCH(bool, flushing);
recorder.recordEvents(qobject_cast<pqEventTranslator*>(activeTranslator),
qobject_cast<pqEventObserver*>(activeObserver),
qobject_cast<QFile*>(ioDevice),
continuousFlush);
QCOMPARE(recorder.isRecording(), started);
QCOMPARE(recorder.continuousFlush(), flushing);
}
// ----------------------------------------------------------------------------
void pqEventRecorderTester::testRecordEvents_data()
{
QTest::addColumn<QObject*>("ioDevice");
QTest::addColumn<QObject*>("activeObserver");
QTest::addColumn<QObject*>("activeTranslator");
QTest::addColumn<bool>("continuousFlush");
QTest::addColumn<bool>("started");
QTest::addColumn<bool>("flushing");
QFile* nullFile = 0;
QFile* file = new QFile;
pqEventObserver* nullObserver = 0;
pqDummyEventObserver* observer = new pqDummyEventObserver;
pqEventTranslator* nullTranslator = 0;
pqEventTranslator* translator = new pqEventTranslator;
QTest::newRow("1") << qobject_cast<QObject*>(nullFile)
<< qobject_cast<QObject*>(nullObserver )
<< qobject_cast<QObject*>(nullTranslator)
<< false << false << false;
QTest::newRow("2") << qobject_cast<QObject*>(nullFile)
<< qobject_cast<QObject*>(nullObserver )
<< qobject_cast<QObject*>(translator)
<< false << false << false;
QTest::newRow("3") << qobject_cast<QObject*>(nullFile)
<< qobject_cast<QObject*>(observer )
<< qobject_cast<QObject*>(translator)
<< false << false << false;
QTest::newRow("4") << qobject_cast<QObject*>(file)
<< qobject_cast<QObject*>(observer)
<< qobject_cast<QObject*>(translator)
<< false << true << false;
QTest::newRow("4") << qobject_cast<QObject*>(file)
<< qobject_cast<QObject*>(observer)
<< qobject_cast<QObject*>(translator)
<< true << true << true;
}
// ----------------------------------------------------------------------------
CTK_TEST_MAIN(pqEventRecorderTest)
#include "moc_pqEventRecorderTest.cpp"
......@@ -20,7 +20,6 @@
/*=========================================================================
Program: ParaView
Module: pqEventPlayer.h
Copyright (c) 2005-2008 Sandia Corporation, Kitware Inc.
All rights reserved.
......@@ -48,9 +47,71 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
// Qt includes
#include <QtTest/QtTest>
// QtTesting includes
#include "pqEventObserver.h"
#include "pqEventSource.h"
// ----------------------------------------------------------------------------
// Dumy pqEventObserver
class pqDummyEventObserver : public pqEventObserver
{
Q_OBJECT
public:
pqDummyEventObserver(QObject* p = 0) : pqEventObserver(p) {}
~pqDummyEventObserver() {}
virtual void setStream(QTextStream* /*stream*/)
{
}
QString Text;
public slots:
virtual void onRecordEvent(const QString& widget,
const QString& command,
const QString& arguments)
{
this->Text.append(QString("%1, %2, %3#").arg(widget,
command,
arguments));
}
};
// ----------------------------------------------------------------------------
// Dumy eventSource
class pqDummyEventSource : public pqEventSource
{
typedef pqEventSource Superclass;
public:
pqDummyEventSource(QObject* p = 0): Superclass(p) {}
~pqDummyEventSource() {}
protected:
virtual void setContent(const QString& /*xmlfilename*/)
{
return;
}
int getNextEvent(QString& /*widget*/,
QString& /*command*/,
QString& /*arguments*/)
{
return 0;
}
};
// ----------------------------------------------------------------------------
// MACRO
#define CTK_TEST_NOOP_MAIN(TestObject) \
int TestObject(int argc, char *argv[]) \
{ \
......
......@@ -75,52 +75,6 @@ private Q_SLOTS:
void testConvertFromDataDirectory_data();
};
// ----------------------------------------------------------------------------
// Dumy pqEventObserver
class DummyEventObserver : public pqEventObserver
{
Q_OBJECT
public:
DummyEventObserver(QObject* p = 0) : pqEventObserver(p) {}
~DummyEventObserver() {}
protected:
virtual void setStream(QTextStream* /*stream*/)
{}
virtual void onRecordEvent(const QString& /*widget*/,
const QString& /*command*/,
const QString& /*arguments*/)
{}
};
// ----------------------------------------------------------------------------
// Dumy eventSource
class DummyEventSource : public pqEventSource
{
typedef pqEventSource Superclass;
public:
DummyEventSource(QObject* p = 0): Superclass(p) {}
~DummyEventSource() {}
protected:
virtual void setContent(const QString& /*xmlfilename*/)
{
return;
}
int getNextEvent(QString& /*widget*/,
QString& /*command*/,
QString& /*arguments*/)
{
return 0;
}
};
// ----------------------------------------------------------------------------
void pqTestUtilityTester::testDefaults()
{
......@@ -159,12 +113,12 @@ void pqTestUtilityTester::testAddEventSource_data()
QTest::addColumn<QObject*>("source2");
QTest::addColumn<int>("newCount2");
DummyEventSource* nullSource = NULL;
DummyEventSource* dummySource1 = new DummyEventSource();
DummyEventSource* dummySource2 = new DummyEventSource();
DummyEventSource* dummySource3 = new DummyEventSource();
DummyEventSource* dummySource4 = new DummyEventSource();
DummyEventSource* dummySource5 = new DummyEventSource();
pqDummyEventSource* nullSource = NULL;
pqDummyEventSource* dummySource1 = new pqDummyEventSource();
pqDummyEventSource* dummySource2 = new pqDummyEventSource();
pqDummyEventSource* dummySource3 = new pqDummyEventSource();
pqDummyEventSource* dummySource4 = new pqDummyEventSource();
pqDummyEventSource* dummySource5 = new pqDummyEventSource();
QTest::newRow("null") << QString("")
<< qobject_cast<QObject*>(nullSource) << 0
<< QString("null")
......@@ -211,12 +165,12 @@ void pqTestUtilityTester::testAddEventObserver_data()
QTest::addColumn<QObject*>("observer2");
QTest::addColumn<int>("newCount2");
DummyEventObserver* nullObserver = NULL;
DummyEventObserver* dummyObserver1 = new DummyEventObserver();
DummyEventObserver* dummyObserver2 = new DummyEventObserver();
DummyEventObserver* dummyObserver3 = new DummyEventObserver();
DummyEventObserver* dummyObserver4 = new DummyEventObserver();
DummyEventObserver* dummyObserver5 = new DummyEventObserver();
pqDummyEventObserver* nullObserver = NULL;
pqDummyEventObserver* dummyObserver1 = new pqDummyEventObserver();
pqDummyEventObserver* dummyObserver2 = new pqDummyEventObserver();
pqDummyEventObserver* dummyObserver3 = new pqDummyEventObserver();
pqDummyEventObserver* dummyObserver4 = new pqDummyEventObserver();
pqDummyEventObserver* dummyObserver5 = new pqDummyEventObserver();
QTest::newRow("null") << QString("")
<< qobject_cast<QObject*>(nullObserver) << 0
<< QString("null")
......
......@@ -31,7 +31,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
// Qt includes
#include <QDebug>
#include <QIODevice>
#include <QTextStream>
......@@ -46,6 +45,7 @@ pqEventRecorder::pqEventRecorder(QObject *parent)
{
this->ActiveObserver = 0;
this->ActiveTranslator = 0;
this->File = 0;
this->Recording = false;
this->ContinuousFlush = false;
......@@ -61,6 +61,11 @@ pqEventRecorder::~pqEventRecorder()
// ----------------------------------------------------------------------------
void pqEventRecorder::setContinuousFlush(bool value)
{
if (!this->ActiveObserver)
{
return;
}
if (value)
{
QObject::connect(this->ActiveObserver,
......@@ -183,13 +188,14 @@ void pqEventRecorder::stop(int value)
this->ActiveObserver->setStream(NULL);
this->ActiveTranslator->stop();
this->Recording = false;
if (!value)
{
return;
}
this->flush();
this->Recording = false;
emit this->stopped();
}
......
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