jasmine-headless-webkit/ext/jasmine-webkit-specrunner/Runner.cpp

192 lines
4.3 KiB
C++
Raw Normal View History

2011-08-01 17:09:08 +00:00
#include <QtGui>
#include <QtWebKit>
#include <QFile>
#include <QTextStream>
#include <iostream>
#include <sstream>
2011-08-01 17:09:08 +00:00
#include <QQueue>
#include "Runner.h"
#include "Page.h"
2011-08-01 17:09:08 +00:00
using namespace std;
Runner::Runner() : QObject()
2011-10-26 02:22:29 +00:00
, runs(0)
, hasErrors(false)
2011-11-05 18:19:41 +00:00
, _hasSpecFailure(false)
, usedConsole(false)
, isFinished(false)
, useColors(false)
2011-09-06 15:37:29 +00:00
{
2011-10-26 02:22:29 +00:00
page.settings()->enablePersistentStorage();
ticker.setInterval(TIMER_TICK);
2011-09-06 15:37:29 +00:00
2011-10-26 02:22:29 +00:00
connect(&ticker, SIGNAL(timeout()), this, SLOT(timerEvent()));
connect(&page, SIGNAL(loadFinished(bool)), this, SLOT(watch(bool)));
connect(&page, SIGNAL(handleError(const QString &, int, const QString &)), this, SLOT(handleError(const QString &, int, const QString &)));
connect(page.mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addJHW()));
}
void Runner::addFile(const QString &spec) {
runnerFiles.enqueue(spec);
}
void Runner::go() {
2011-10-26 02:22:29 +00:00
ticker.stop();
page.setPreferredContentsSize(QSize(1024, 600));
addJHW();
loadSpec();
}
void Runner::addJHW() {
2011-10-26 02:22:29 +00:00
page.mainFrame()->addToJavaScriptWindowObject("JHW", this);
}
void Runner::handleError(const QString &message, int lineNumber, const QString &sourceID) {
QString messageEscaped = QString(message);
QString sourceIDEscaped = QString(sourceID);
messageEscaped.replace(QString("\""), QString("\\\""));
sourceIDEscaped.replace(QString("\""), QString("\\\""));
std::stringstream ss;
ss << lineNumber;
QString command("JHW._handleError(\"" + messageEscaped + "\", " + QString(ss.str().c_str()) + ", \"" + sourceIDEscaped + "\"); false;");
2011-10-26 02:22:29 +00:00
page.mainFrame()->evaluateJavaScript(command);
hasErrors = true;
}
void Runner::loadSpec()
{
if (!reportFileName.isEmpty()) {
QFile *outputFile = new QFile(reportFileName);
outputFile->open(QIODevice::WriteOnly);
outputFiles.enqueue(outputFile);
}
2011-10-26 02:22:29 +00:00
page.mainFrame()->load(runnerFiles.dequeue());
ticker.start();
}
void Runner::watch(bool ok) {
if (!ok) {
2011-10-26 02:22:29 +00:00
std::cerr << "Can't load " << qPrintable(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-26 02:22:29 +00:00
page.mainFrame()->evaluateJavaScript(QString("JHW._setColors(") + (useColors ? QString("true") : QString("false")) + QString("); false;"));
}
void Runner::setColors(bool colors) {
useColors = colors;
}
void Runner::hasUsedConsole() {
usedConsole = true;
}
2011-10-26 02:22:29 +00:00
void Runner::hasError() {
hasErrors = true;
}
2011-11-05 18:19:41 +00:00
void Runner::hasSpecFailure() {
_hasSpecFailure = true;
}
void Runner::reportFile(const QString &file) {
reportFileName = file;
}
2011-09-06 15:37:29 +00:00
void Runner::timerPause() {
2011-10-26 02:22:29 +00:00
ticker.stop();
2011-09-06 15:37:29 +00:00
}
void Runner::timerDone() {
2011-10-26 02:22:29 +00:00
ticker.start();
2011-09-06 15:37:29 +00:00
}
void Runner::ping() {
runs = 0;
}
void Runner::print(const QString &fh, const QString &content) {
if (fh == "stdout") {
std::cout << qPrintable(content);
std::cout.flush();
}
if (fh == "stderr") {
std::cerr << qPrintable(content);
std::cerr.flush();
}
if (fh == "report") {
QListIterator<QFile *> iterator(outputFiles);
while (iterator.hasNext()) {
QTextStream ts(iterator.next());
ts << qPrintable(content);
ts.flush();
}
}
}
void Runner::finishSuite() {
isFinished = true;
2011-12-12 17:22:32 +00:00
runs = 0;
}
2011-08-01 17:09:08 +00:00
void Runner::timerEvent() {
2011-10-26 02:22:29 +00:00
++runs;
2011-08-01 17:09:08 +00:00
2011-10-26 02:22:29 +00:00
if (hasErrors && runs > 2)
QApplication::instance()->exit(1);
2011-08-01 17:09:08 +00:00
2011-12-12 17:22:32 +00:00
if (isFinished && runs > 2) {
QListIterator<QFile *> iterator(outputFiles);
while (iterator.hasNext()) {
iterator.next()->close();
}
int exitCode = 0;
2011-11-05 18:19:41 +00:00
if (_hasSpecFailure || hasErrors) {
exitCode = 1;
2011-08-01 17:09:08 +00:00
} else {
if (usedConsole) {
exitCode = 2;
2011-08-01 17:09:08 +00:00
}
}
bool runAgain = true;
2011-08-01 17:09:08 +00:00
if (runnerFiles.count() == 0) {
runAgain = false;
} else {
if (exitCode == 1) {
2011-08-01 17:09:08 +00:00
runAgain = false;
}
}
if (runAgain) {
isFinished = false;
loadSpec();
} else {
QApplication::instance()->exit(exitCode);
2011-08-01 17:09:08 +00:00
}
}
2011-10-26 02:22:29 +00:00
if (runs > MAX_LOOPS) {
std::cerr << "WARNING: too many runs and the test is still not finished!" << std::endl;
QApplication::instance()->exit(1);
}
}