Created a Response object for the success/message pair

This commit is contained in:
Joe Ferris 2011-05-05 17:45:44 -04:00
parent 7a8cd4cc2f
commit cc74875e4b
17 changed files with 79 additions and 52 deletions

View File

@ -6,17 +6,17 @@ require 'capybara_webkit_builder'
desc "Generate a Makefile using qmake"
file 'Makefile' do
CapybaraWebkitBuilder.makefile
CapybaraWebkitBuilder.makefile or exit(1)
end
desc "Regenerate dependencies using qmake"
task :qmake => 'Makefile' do
CapybaraWebkitBuilder.qmake
CapybaraWebkitBuilder.qmake or exit(1)
end
desc "Build the webkit server"
task :build => :qmake do
CapybaraWebkitBuilder.build
CapybaraWebkitBuilder.build or exit(1)
end
file 'bin/webkit_server' => :build

View File

@ -12,7 +12,7 @@ module CapybaraWebkitBuilder
end
def build
system("make")
system("make") or return false
FileUtils.mkdir("bin") unless File.directory?("bin")
@ -24,8 +24,8 @@ module CapybaraWebkitBuilder
end
def build_all
makefile
qmake
makefile &&
qmake &&
build
end
end

View File

@ -3,6 +3,7 @@
#include <QObject>
#include <QStringList>
#include "Response.h"
class WebPage;
@ -14,7 +15,7 @@ class Command : public QObject {
virtual void start(QStringList &arguments);
signals:
void finished(bool success, QString &response);
void finished(Response *response);
protected:
WebPage *page();

View File

@ -85,13 +85,13 @@ void Connection::startCommand() {
m_command = createCommand(m_commandName.toAscii().constData());
if (m_command) {
connect(m_command,
SIGNAL(finished(bool, QString &)),
SIGNAL(finished(Response *)),
this,
SLOT(finishCommand(bool, QString &)));
SLOT(finishCommand(Response *)));
m_command->start(m_arguments);
} else {
QString failure = QString("Unknown command: ") + m_commandName + "\n";
writeResponse(false, failure);
writeResponse(new Response(false, failure));
}
m_commandName = QString();
}
@ -106,27 +106,29 @@ void Connection::pendingLoadFinished(bool success) {
if (success) {
startCommand();
} else {
QString response = m_page->failureString();
writeResponse(false, response);
QString message = m_page->failureString();
writeResponse(new Response(false, message));
}
}
void Connection::finishCommand(bool success, QString &response) {
void Connection::finishCommand(Response *response) {
m_command->deleteLater();
m_command = NULL;
writeResponse(success, response);
writeResponse(response);
}
void Connection::writeResponse(bool success, QString &response) {
if (success)
void Connection::writeResponse(Response *response) {
if (response->isSuccess())
m_socket->write("ok\n");
else
m_socket->write("failure\n");
QByteArray response_utf8 = response.toUtf8();
QString responseLength = QString::number(response_utf8.size()) + "\n";
m_socket->write(responseLength.toAscii());
m_socket->write(response_utf8);
QByteArray messageUtf8 = response->message().toUtf8();
QString messageLength = QString::number(messageUtf8.size()) + "\n";
m_socket->write(messageLength.toAscii());
m_socket->write(messageUtf8);
delete response;
m_arguments.clear();
m_commandName = QString();
m_argumentsExpected = -1;

View File

@ -4,6 +4,7 @@
class QTcpSocket;
class WebPage;
class Command;
class Response;
class Connection : public QObject {
Q_OBJECT
@ -13,7 +14,7 @@ class Connection : public QObject {
public slots:
void checkNext();
void finishCommand(bool success, QString &response);
void finishCommand(Response *response);
void pendingLoadFinished(bool success);
private:
@ -23,7 +24,7 @@ class Connection : public QObject {
Command *createCommand(const char *name);
void processArgument(const char *line);
void startCommand();
void writeResponse(bool success, QString &response);
void writeResponse(Response *response);
QTcpSocket *m_socket;
QString m_commandName;

View File

@ -9,7 +9,7 @@ Evaluate::Evaluate(WebPage *page, QObject *parent) : Command(page, parent) {
void Evaluate::start(QStringList &arguments) {
QVariant result = page()->currentFrame()->evaluateJavaScript(arguments[0]);
addVariant(result);
emit finished(true, m_buffer);
emit finished(new Response(true, m_buffer));
}
void Evaluate::addVariant(QVariant &object) {

View File

@ -7,12 +7,10 @@ Execute::Execute(WebPage *page, QObject *parent) : Command(page, parent) {
void Execute::start(QStringList &arguments) {
QString script = arguments[0] + QString("; 'success'");
QVariant result = page()->currentFrame()->evaluateJavaScript(script);
QString response;
if (result.isValid()) {
emit finished(true, response);
emit finished(new Response(true));
} else {
response = "Javascript failed to execute";
emit finished(false, response);
emit finished(new Response(false, "Javascript failed to execute"));
}
}

View File

@ -6,15 +6,14 @@ Find::Find(WebPage *page, QObject *parent) : Command(page, parent) {
}
void Find::start(QStringList &arguments) {
QString response;
QString message;
QVariant result = page()->invokeCapybaraFunction("find", arguments);
if (result.isValid()) {
response = result.toString();
emit finished(true, response);
message = result.toString();
emit finished(new Response(true, message));
} else {
response = "Invalid XPath expression";
emit finished(false, response);
emit finished(new Response(false, "Invalid XPath expression"));
}
}

View File

@ -50,8 +50,7 @@ void FrameFocus::focusId(QString name) {
void FrameFocus::focusParent() {
if (page()->currentFrame()->parentFrame() == 0) {
QString response = "Already at parent frame.";
emit finished(false, response);
emit finished(new Response(false, "Already at parent frame."));
} else {
page()->currentFrame()->parentFrame()->setFocus();
success();
@ -59,11 +58,9 @@ void FrameFocus::focusParent() {
}
void FrameFocus::frameNotFound() {
QString response = "Unable to locate frame. ";
emit finished(false, response);
emit finished(new Response(false, "Unable to locate frame. "));
}
void FrameFocus::success() {
QString response;
emit finished(true, response);
emit finished(new Response(true));
}

View File

@ -9,6 +9,6 @@ void Node::start(QStringList &arguments) {
QString functionName = functionArguments.takeFirst();
QVariant result = page()->invokeCapybaraFunction(functionName, functionArguments);
QString attributeValue = result.toString();
emit finished(true, attributeValue);
emit finished(new Response(true, attributeValue));
}

View File

@ -10,7 +10,6 @@ void Reset::start(QStringList &arguments) {
page()->triggerAction(QWebPage::Stop);
page()->currentFrame()->setHtml("<html><body></body></html>");
page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar());
QString response = "";
emit finished(true, response);
emit finished(new Response(true));
}

19
src/Response.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "Response.h"
#include <iostream>
Response::Response(bool success, QString message) {
m_success = success;
m_message = message;
}
Response::Response(bool success) {
Response(success, QString());
}
bool Response::isSuccess() const {
return m_success;
}
QString Response::message() const {
return m_message;
}

13
src/Response.h Normal file
View File

@ -0,0 +1,13 @@
#include <QString>
class Response {
public:
Response(bool success, QString message);
Response(bool success);
bool isSuccess() const;
QString message() const;
private:
bool m_success;
QString m_message;
};

View File

@ -7,8 +7,7 @@ Source::Source(WebPage *page, QObject *parent) : Command(page, parent) {
void Source::start(QStringList &arguments) {
Q_UNUSED(arguments)
QString response = page()->currentFrame()->toHtml();
emit finished(true, response);
QString result = page()->currentFrame()->toHtml();
emit finished(new Response(true, result));
}

View File

@ -9,8 +9,7 @@ void Url::start(QStringList &argments) {
QUrl humanUrl = page()->currentFrame()->url();
QByteArray encodedBytes = humanUrl.toEncoded();
QString response = QString(encodedBytes);
emit finished(true, response);
QString urlString = QString(encodedBytes);
emit finished(new Response(true, urlString));
}

View File

@ -11,10 +11,10 @@ void Visit::start(QStringList &arguments) {
}
void Visit::loadFinished(bool success) {
QString response;
QString message;
if (!success)
response = page()->failureString();
message = page()->failureString();
emit finished(success, response);
emit finished(new Response(success, message));
}

View File

@ -1,8 +1,8 @@
TEMPLATE = app
TARGET = webkit_server
DESTDIR = .
HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h FrameFocus.h
SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp FrameFocus.cpp
HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h FrameFocus.h Response.h
SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp FrameFocus.cpp Response.cpp
RESOURCES = webkit_server.qrc
QT += network webkit
CONFIG += console