forked from thoughtbot/capybara-webkit
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move logic to wait for pending page loads into a decorator
- Loading branch information
Showing
5 changed files
with
94 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "PageLoadingCommand.h" | ||
#include "Command.h" | ||
#include "WebPage.h" | ||
|
||
PageLoadingCommand::PageLoadingCommand(Command *command, WebPage *page, QObject *parent) : QObject(parent) { | ||
m_page = page; | ||
m_command = command; | ||
m_pageLoadingFromCommand = false; | ||
m_pageSuccess = true; | ||
m_pendingResponse = NULL; | ||
connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand())); | ||
connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool))); | ||
} | ||
|
||
void PageLoadingCommand::start() { | ||
connect(m_command, SIGNAL(finished(Response *)), this, SLOT(commandFinished(Response *))); | ||
m_command->start(); | ||
}; | ||
|
||
void PageLoadingCommand::pendingLoadFinished(bool success) { | ||
m_pageSuccess = success; | ||
if (m_pageLoadingFromCommand) { | ||
m_pageLoadingFromCommand = false; | ||
if (m_pendingResponse) { | ||
if (m_pageSuccess) { | ||
emit finished(m_pendingResponse); | ||
} else { | ||
QString message = m_page->failureString(); | ||
emit finished(new Response(false, message)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void PageLoadingCommand::pageLoadingFromCommand() { | ||
m_pageLoadingFromCommand = true; | ||
} | ||
|
||
void PageLoadingCommand::commandFinished(Response *response) { | ||
disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand())); | ||
m_command->deleteLater(); | ||
if (m_pageLoadingFromCommand) | ||
m_pendingResponse = response; | ||
else | ||
emit finished(response); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <QObject> | ||
#include <QStringList> | ||
|
||
class Command; | ||
class Response; | ||
class WebPage; | ||
|
||
/* | ||
* Decorates a Command by deferring the finished() signal until any pending | ||
* page loads are complete. | ||
* | ||
* If a Command starts a page load, no signal will be emitted until the page | ||
* load is finished. | ||
* | ||
* If a pending page load fails, the command's response will be discarded and a | ||
* failure response will be emitted instead. | ||
*/ | ||
class PageLoadingCommand : public QObject { | ||
Q_OBJECT | ||
|
||
public: | ||
PageLoadingCommand(Command *command, WebPage *page, QObject *parent = 0); | ||
void start(); | ||
|
||
public slots: | ||
void pageLoadingFromCommand(); | ||
void pendingLoadFinished(bool success); | ||
void commandFinished(Response *response); | ||
|
||
signals: | ||
void finished(Response *response); | ||
|
||
private: | ||
WebPage *m_page; | ||
Command *m_command; | ||
Response *m_pendingResponse; | ||
bool m_pageSuccess; | ||
bool m_pageLoadingFromCommand; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters