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

386 lines
9.6 KiB
C++
Raw Permalink Normal View History

2011-04-11 10:24:07 +00:00
/*
Copyright (c) 2010 Sencha Inc.
Copyright (c) 2011 John Bintz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <QtGui>
#include <QtWebKit>
2011-05-29 16:30:19 +00:00
#include <QFile>
#include <QTextStream>
2011-04-11 10:24:07 +00:00
#include <iostream>
#include <QQueue>
2011-04-11 10:24:07 +00:00
#if QT_VERSION < QT_VERSION_CHECK(4, 7, 0)
#error Use Qt 4.7 or later version
#endif
2011-04-12 11:10:03 +00:00
class HeadlessSpecRunnerPage: public QWebPage
{
Q_OBJECT
signals:
void consoleLog(const QString &msg, int lineNumber, const QString &sourceID);
2011-05-03 15:22:18 +00:00
void internalLog(const QString &note, const QString &msg);
2011-04-12 11:10:03 +00:00
protected:
void javaScriptConsoleMessage(const QString & message, int lineNumber, const QString & sourceID);
bool javaScriptConfirm(QWebFrame *frame, const QString &msg);
void javaScriptAlert(QWebFrame *frame, const QString &msg);
2011-04-12 11:10:03 +00:00
};
void HeadlessSpecRunnerPage::javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID)
{
emit consoleLog(message, lineNumber, sourceID);
}
bool HeadlessSpecRunnerPage::javaScriptConfirm(QWebFrame *frame, const QString &msg)
{
2011-05-03 15:22:18 +00:00
emit internalLog("TODO", "jasmine-headless-webkit can't handle confirm() yet! You should mock window.confirm for now. Returning true.");
return true;
}
void HeadlessSpecRunnerPage::javaScriptAlert(QWebFrame *frame, const QString &msg)
{
2011-05-03 15:22:18 +00:00
emit internalLog("alert", msg);
}
2011-04-11 10:24:07 +00:00
class HeadlessSpecRunner: public QObject
{
Q_OBJECT
public:
HeadlessSpecRunner();
2011-05-02 16:00:03 +00:00
void setColors(bool colors);
2011-05-29 16:30:19 +00:00
void reportFile(const QString &file);
void addFile(const QString &spec);
void go();
2011-04-11 10:24:07 +00:00
public slots:
void log(const QString &msg);
2011-05-12 21:02:11 +00:00
void specPassed();
void specFailed();
void printName(const QString &name);
void printResult(const QString &result);
void finishSuite(const QString &duration, const QString &total, const QString& failed);
2011-04-11 10:24:07 +00:00
private slots:
void watch(bool ok);
2011-04-12 11:10:03 +00:00
void errorLog(const QString &msg, int lineNumber, const QString &sourceID);
2011-05-03 15:22:18 +00:00
void internalLog(const QString &note, const QString &msg);
void addJHW();
2011-04-11 10:24:07 +00:00
protected:
bool hasElement(const char *select);
void timerEvent(QTimerEvent *event);
private:
2011-04-12 11:10:03 +00:00
HeadlessSpecRunnerPage m_page;
2011-04-11 10:24:07 +00:00
QBasicTimer m_ticker;
int m_runs;
2011-04-12 11:10:03 +00:00
bool hasErrors;
bool usedConsole;
2011-05-02 16:00:03 +00:00
bool showColors;
2011-05-12 21:02:11 +00:00
bool isFinished;
bool didFail;
2011-05-13 13:15:49 +00:00
bool consoleNotUsedThisRun;
QQueue<QString> runnerFiles;
2011-05-29 16:30:19 +00:00
QString reportFilename;
2011-05-02 16:00:03 +00:00
void red();
void green();
void yellow();
void clear();
void loadSpec();
2011-04-11 10:24:07 +00:00
};
HeadlessSpecRunner::HeadlessSpecRunner()
: QObject()
, m_runs(0)
2011-04-12 11:28:06 +00:00
, hasErrors(false)
, usedConsole(false)
2011-05-02 16:00:03 +00:00
, showColors(false)
2011-05-12 21:02:11 +00:00
, isFinished(false)
, didFail(false)
2011-05-13 13:15:49 +00:00
, consoleNotUsedThisRun(false)
2011-04-11 10:24:07 +00:00
{
m_page.settings()->enablePersistentStorage();
connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(watch(bool)));
2011-04-12 11:10:03 +00:00
connect(&m_page, SIGNAL(consoleLog(QString, int, QString)), this, SLOT(errorLog(QString, int, QString)));
2011-05-03 15:22:18 +00:00
connect(&m_page, SIGNAL(internalLog(QString, QString)), this, SLOT(internalLog(QString, QString)));
connect(m_page.mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addJHW()));
2011-04-11 10:24:07 +00:00
}
void HeadlessSpecRunner::addFile(const QString &spec)
{
runnerFiles.enqueue(spec);
}
void HeadlessSpecRunner::go()
2011-04-11 10:24:07 +00:00
{
m_ticker.stop();
m_page.setPreferredContentsSize(QSize(1024, 600));
addJHW();
loadSpec();
}
void HeadlessSpecRunner::addJHW()
{
m_page.mainFrame()->addToJavaScriptWindowObject("JHW", this);
}
void HeadlessSpecRunner::loadSpec()
{
m_page.mainFrame()->load(runnerFiles.dequeue());
m_ticker.start(200, this);
2011-04-11 10:24:07 +00:00
}
void HeadlessSpecRunner::watch(bool ok)
{
if (!ok) {
2011-05-06 11:20:18 +00:00
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;
2011-04-11 10:24:07 +00:00
QApplication::instance()->exit(1);
return;
}
m_ticker.start(200, this);
}
bool HeadlessSpecRunner::hasElement(const char *select)
{
return !m_page.mainFrame()->findFirstElement(select).isNull();
}
2011-05-02 16:00:03 +00:00
void HeadlessSpecRunner::setColors(bool colors)
{
showColors = colors;
}
2011-05-29 16:30:19 +00:00
void HeadlessSpecRunner::reportFile(const QString &file)
{
reportFilename = file;
}
2011-05-02 16:00:03 +00:00
void HeadlessSpecRunner::red()
{
if (showColors) std::cout << "\033[0;31m";
}
void HeadlessSpecRunner::green()
{
if (showColors) std::cout << "\033[0;32m";
}
void HeadlessSpecRunner::yellow()
{
if (showColors) std::cout << "\033[0;33m";
}
void HeadlessSpecRunner::clear()
{
if (showColors) std::cout << "\033[m";
}
2011-05-12 21:02:11 +00:00
void HeadlessSpecRunner::specPassed()
{
2011-05-13 13:15:49 +00:00
consoleNotUsedThisRun = true;
2011-05-12 21:02:11 +00:00
green();
std::cout << '.';
clear();
fflush(stdout);
}
void HeadlessSpecRunner::specFailed()
{
2011-05-13 13:15:49 +00:00
consoleNotUsedThisRun = true;
2011-05-12 21:02:11 +00:00
didFail = true;
red();
std::cout << 'F';
clear();
fflush(stdout);
}
2011-04-12 11:10:03 +00:00
void HeadlessSpecRunner::errorLog(const QString &msg, int lineNumber, const QString &sourceID)
{
2011-05-02 16:00:03 +00:00
red();
std::cout << "[error] ";
clear();
2011-04-12 11:10:03 +00:00
std::cout << qPrintable(sourceID) << ":" << lineNumber << " : " << qPrintable(msg);
std::cout << std::endl;
2011-04-12 11:28:06 +00:00
2011-04-12 11:10:03 +00:00
hasErrors = true;
2011-04-12 11:29:00 +00:00
m_runs = 0;
2011-04-12 11:28:06 +00:00
m_ticker.start(200, this);
2011-04-12 11:10:03 +00:00
}
2011-05-03 15:22:18 +00:00
void HeadlessSpecRunner::internalLog(const QString &note, const QString &msg) {
red();
std::cout << "[" << qPrintable(note) << "] ";
clear();
std::cout << qPrintable(msg);
std::cout << std::endl;
}
2011-04-11 10:24:07 +00:00
void HeadlessSpecRunner::log(const QString &msg)
{
usedConsole = true;
2011-05-02 16:00:03 +00:00
green();
2011-05-13 13:15:49 +00:00
if (consoleNotUsedThisRun) {
std::cout << std::endl;
consoleNotUsedThisRun = false;
}
2011-05-02 16:00:03 +00:00
std::cout << "[console] ";
clear();
if (msg.contains("\n"))
std::cout << std::endl;
2011-04-11 10:24:07 +00:00
std::cout << qPrintable(msg);
std::cout << std::endl;
}
2011-05-12 21:02:11 +00:00
void HeadlessSpecRunner::printName(const QString &name)
2011-04-11 10:24:07 +00:00
{
2011-05-12 21:02:11 +00:00
std::cout << std::endl << std::endl;
red();
std::cout << qPrintable(name) << std::endl;
clear();
2011-04-11 10:24:07 +00:00
}
2011-05-12 21:02:11 +00:00
void HeadlessSpecRunner::printResult(const QString &result)
{
red();
std::cout << " " << qPrintable(result) << std::endl;
clear();
}
void HeadlessSpecRunner::finishSuite(const QString &duration, const QString &total, const QString& failed)
{
std::cout << std::endl;
if (didFail) {
red();
std::cout << "FAIL: ";
} else {
green();
std::cout << "PASS";
if (hasErrors) {
std::cout << " with JS errors";
}
std::cout << ": ";
2011-05-12 21:02:11 +00:00
}
2011-05-12 21:23:42 +00:00
std::cout << qPrintable(total) << " tests, " << qPrintable(failed) << " failures, " << qPrintable(duration) << " secs.";
2011-05-12 21:02:11 +00:00
clear();
2011-05-12 21:23:42 +00:00
std::cout << std::endl;
2011-05-12 21:02:11 +00:00
2011-05-29 16:30:19 +00:00
if (!reportFilename.isEmpty()) {
QFile reportFH(reportFilename);
if (reportFH.open(QFile::WriteOnly)) {
QTextStream report(&reportFH);
report << qPrintable(total) << "/" << qPrintable(failed) << "/";
report << (usedConsole ? "T" : "F");
report << "/" << qPrintable(duration) << "\n";
reportFH.close();
}
}
2011-05-12 21:02:11 +00:00
isFinished = true;
}
2011-04-11 10:24:07 +00:00
void HeadlessSpecRunner::timerEvent(QTimerEvent *event)
{
2011-04-12 11:28:06 +00:00
++m_runs;
2011-04-11 10:24:07 +00:00
if (event->timerId() != m_ticker.timerId())
return;
2011-04-12 11:29:00 +00:00
if (hasErrors && m_runs > 2)
2011-04-12 11:28:06 +00:00
QApplication::instance()->exit(1);
if (isFinished) {
int exitCode = 0;
if (didFail || hasErrors) {
exitCode = 1;
} else {
if (usedConsole) {
exitCode = 2;
2011-05-12 21:02:11 +00:00
}
}
bool runAgain = true;
if (runnerFiles.count() == 0) {
runAgain = false;
} else {
if (exitCode == 1) {
runAgain = false;
}
}
if (runAgain) {
2011-06-10 19:50:14 +00:00
isFinished = false;
loadSpec();
} else {
QApplication::instance()->exit(exitCode);
}
}
if (m_runs > 30) {
std::cout << "WARNING: too many runs and the test is still not finished!" << std::endl;
QApplication::instance()->exit(1);
2011-04-11 10:24:07 +00:00
}
}
#include "specrunner.moc"
int main(int argc, char** argv)
{
2011-05-29 16:30:19 +00:00
char *reporter = NULL;
char showColors = false;
2011-05-02 16:00:03 +00:00
int c, index;
2011-05-29 16:30:19 +00:00
while ((c = getopt(argc, argv, "cr:")) != -1) {
2011-05-02 16:00:03 +00:00
switch(c) {
case 'c':
showColors = true;
break;
2011-05-29 16:30:19 +00:00
case 'r':
reporter = optarg;
break;
2011-05-02 16:00:03 +00:00
}
}
if (optind == argc) {
2011-04-11 10:24:07 +00:00
std::cerr << "Run Jasmine's SpecRunner headlessly" << std::endl << std::endl;
std::cerr << " specrunner [-c] [-r <report file>] specrunner.html ..." << std::endl;
2011-04-11 10:24:07 +00:00
return 1;
}
QApplication app(argc, argv);
HeadlessSpecRunner runner;
2011-05-29 16:30:19 +00:00
runner.setColors(true);
runner.reportFile(reporter);
for (index = optind; index < argc; index++) {
runner.addFile(QString::fromLocal8Bit(argv[index]));
}
runner.go();
2011-04-11 10:24:07 +00:00
return app.exec();
}
2011-05-29 16:30:19 +00:00