2011-08-01 17:09:08 +00:00
|
|
|
#include <QtGui>
|
|
|
|
#include <QtWebKit>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <QQueue>
|
|
|
|
|
|
|
|
#include "Runner.h"
|
|
|
|
|
2011-09-02 19:52:19 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
Runner::Runner() : QObject()
|
|
|
|
, m_runs(0)
|
|
|
|
, hasErrors(false)
|
|
|
|
, usedConsole(false)
|
|
|
|
, isFinished(false)
|
2011-09-06 15:37:29 +00:00
|
|
|
, didFail(false)
|
|
|
|
{
|
2011-09-02 19:52:19 +00:00
|
|
|
m_page.settings()->enablePersistentStorage();
|
2011-09-06 15:37:29 +00:00
|
|
|
m_ticker.setInterval(TIMER_TICK);
|
|
|
|
|
|
|
|
connect(&m_ticker, SIGNAL(timeout()), this, SLOT(timerEvent()));
|
2011-09-02 19:52:19 +00:00
|
|
|
connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(watch(bool)));
|
|
|
|
connect(&m_page, SIGNAL(consoleLog(QString, int, QString)), this, SLOT(errorLog(QString, int, QString)));
|
|
|
|
connect(&m_page, SIGNAL(internalLog(QString, QString)), this, SLOT(internalLog(QString, QString)));
|
|
|
|
connect(m_page.mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addJHW()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::addFile(const QString &spec) {
|
|
|
|
runnerFiles.enqueue(spec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::go()
|
|
|
|
{
|
|
|
|
m_ticker.stop();
|
|
|
|
m_page.setPreferredContentsSize(QSize(1024, 600));
|
|
|
|
addJHW();
|
2011-10-24 20:40:08 +00:00
|
|
|
|
2011-09-02 19:52:19 +00:00
|
|
|
loadSpec();
|
2011-09-06 15:37:29 +00:00
|
|
|
|
|
|
|
m_ticker.start();
|
2011-09-02 19:52:19 +00:00
|
|
|
}
|
|
|
|
void Runner::addJHW()
|
|
|
|
{
|
|
|
|
m_page.mainFrame()->addToJavaScriptWindowObject("JHW", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::loadSpec()
|
|
|
|
{
|
2011-10-24 20:40:08 +00:00
|
|
|
if (!reportFileName.isEmpty()) {
|
|
|
|
outputFile = new QFile(reportFileName);
|
|
|
|
outputFile->open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
ts = new QTextStream(outputFile);
|
|
|
|
}
|
|
|
|
|
2011-09-02 19:52:19 +00:00
|
|
|
m_page.mainFrame()->load(runnerFiles.dequeue());
|
2011-09-06 15:37:29 +00:00
|
|
|
m_ticker.start();
|
2011-09-02 19:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::watch(bool ok)
|
|
|
|
{
|
|
|
|
if (!ok) {
|
|
|
|
std::cerr << "Can't load " << qPrintable(m_page.mainFrame()->url().toString()) << ", the file may be broken." << std::endl;
|
|
|
|
std::cerr << "Out of curiosity, did your tests try to submit a form and you haven't prevented that?" << std::endl;
|
|
|
|
std::cerr << "Try running your tests in your browser with the Jasmine server and see what happens." << std::endl;
|
|
|
|
QApplication::instance()->exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-24 20:40:08 +00:00
|
|
|
bool Runner::hasElement(const char *select) {
|
2011-09-02 19:52:19 +00:00
|
|
|
return !m_page.mainFrame()->findFirstElement(select).isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::setColors(bool colors) {
|
|
|
|
consoleOutput.showColors = colors;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::reportFile(const QString &file) {
|
|
|
|
reportFileName = file;
|
|
|
|
}
|
|
|
|
|
2011-09-06 15:37:29 +00:00
|
|
|
void Runner::timerPause() {
|
|
|
|
m_ticker.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::timerDone() {
|
|
|
|
m_ticker.start();
|
|
|
|
}
|
|
|
|
|
2011-10-24 20:40:08 +00:00
|
|
|
void Runner::print(const QString &fh, const QString &content) {
|
|
|
|
if (fh == "stdout") {
|
|
|
|
std::cout << qPrintable(content);
|
|
|
|
std::cout.flush();
|
|
|
|
}
|
2011-09-02 19:52:19 +00:00
|
|
|
|
2011-10-24 20:40:08 +00:00
|
|
|
if (fh == "stderr") {
|
|
|
|
std::cerr << qPrintable(content);
|
|
|
|
std::cerr.flush();
|
|
|
|
}
|
2011-09-02 19:52:19 +00:00
|
|
|
|
2011-10-24 20:40:08 +00:00
|
|
|
if (fh == "report") {
|
|
|
|
*ts << qPrintable(content);
|
|
|
|
ts->flush();
|
|
|
|
}
|
2011-09-02 19:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::errorLog(const QString &msg, int lineNumber, const QString &sourceID)
|
|
|
|
{
|
|
|
|
consoleOutput.errorLog(msg, lineNumber, sourceID);
|
|
|
|
reportFileOutput.errorLog(msg, lineNumber, sourceID);
|
|
|
|
|
|
|
|
hasErrors = true;
|
|
|
|
m_runs = 0;
|
2011-09-06 15:37:29 +00:00
|
|
|
m_ticker.start();
|
2011-09-02 19:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::internalLog(const QString ¬e, const QString &msg) {
|
|
|
|
consoleOutput.internalLog(note, msg);
|
|
|
|
reportFileOutput.internalLog(note, msg);
|
|
|
|
}
|
|
|
|
|
2011-10-24 20:40:08 +00:00
|
|
|
void Runner::usedConsole()
|
2011-09-02 19:52:19 +00:00
|
|
|
{
|
|
|
|
usedConsole = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Runner::leavePageAttempt(const QString &msg)
|
|
|
|
{
|
|
|
|
consoleOutput.internalLog("error", msg);
|
|
|
|
m_page.oneFalseConfirm();
|
|
|
|
hasErrors = true;
|
|
|
|
}
|
|
|
|
|
2011-10-24 20:40:08 +00:00
|
|
|
void Runner::finishSuite() {
|
2011-09-02 19:52:19 +00:00
|
|
|
isFinished = true;
|
|
|
|
}
|
2011-08-01 17:09:08 +00:00
|
|
|
|
2011-10-24 20:40:08 +00:00
|
|
|
void Runner::timerEvent() {
|
2011-09-02 19:52:19 +00:00
|
|
|
++m_runs;
|
2011-08-01 17:09:08 +00:00
|
|
|
|
2011-09-02 19:52:19 +00:00
|
|
|
if (hasErrors && m_runs > 2)
|
|
|
|
QApplication::instance()->exit(1);
|
2011-08-01 17:09:08 +00:00
|
|
|
|
2011-09-02 19:52:19 +00:00
|
|
|
if (isFinished) {
|
2011-10-24 20:40:08 +00:00
|
|
|
outputFile->close();
|
|
|
|
|
2011-09-02 19:52:19 +00:00
|
|
|
int exitCode = 0;
|
|
|
|
if (didFail || hasErrors) {
|
|
|
|
exitCode = 1;
|
2011-08-01 17:09:08 +00:00
|
|
|
} else {
|
2011-09-02 19:52:19 +00:00
|
|
|
if (usedConsole) {
|
|
|
|
exitCode = 2;
|
2011-08-01 17:09:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 19:52:19 +00:00
|
|
|
bool runAgain = true;
|
2011-08-01 17:09:08 +00:00
|
|
|
|
2011-09-02 19:52:19 +00:00
|
|
|
if (runnerFiles.count() == 0) {
|
|
|
|
runAgain = false;
|
|
|
|
} else {
|
|
|
|
if (exitCode == 1) {
|
2011-08-01 17:09:08 +00:00
|
|
|
runAgain = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-02 19:52:19 +00:00
|
|
|
if (runAgain) {
|
|
|
|
isFinished = false;
|
|
|
|
loadSpec();
|
|
|
|
} else {
|
|
|
|
QApplication::instance()->exit(exitCode);
|
2011-08-01 17:09:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-06 15:37:29 +00:00
|
|
|
if (m_runs > MAX_LOOPS) {
|
2011-09-02 19:52:19 +00:00
|
|
|
std::cout << "WARNING: too many runs and the test is still not finished!" << std::endl;
|
|
|
|
QApplication::instance()->exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|