1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00

Refactored FrameFocus

This commit is contained in:
Joe Ferris 2011-04-20 19:02:20 -04:00
parent c563ed54cb
commit 3615624ec4
2 changed files with 74 additions and 38 deletions

View file

@ -6,44 +6,64 @@ FrameFocus::FrameFocus(WebPage *page, QObject *parent) : Command(page, parent) {
} }
void FrameFocus::start(QStringList &arguments) { void FrameFocus::start(QStringList &arguments) {
int index; findFrames();
bool ok; switch(arguments.length()) {
bool found = false; case 1:
QString response; focusId(arguments[0]);
QList<QWebFrame *> child_frames = page()->currentFrame()->childFrames();
if (arguments.length() > 0) {
if (arguments.length() > 1) {
// Find frame by index.
index = arguments[1].toInt(&ok);
if (ok && 0 <= index && index < child_frames.length()) {
child_frames[index]->setFocus();
found = true;
}
} else {
// Find frame by id.
for (int i = 0; i < child_frames.length(); i++) {
if (child_frames[i]->frameName().compare(arguments[0]) == 0) {
child_frames[i]->setFocus();
found = true;
break; break;
} case 2:
} focusIndex(arguments[1].toInt());
} break;
if (found) { default:
emit finished(true, response); focusParent();
} else {
response = "Unable to locate frame. ";
emit finished(false, response);
}
} else {
// Set focus on parent.
if (page()->currentFrame()->parentFrame() == 0) {
response = "Already at parent frame.";
emit finished(false, response);
} else {
page()->currentFrame()->parentFrame()->setFocus();
emit finished(true, response);
}
} }
} }
void FrameFocus::findFrames() {
frames = page()->currentFrame()->childFrames();
}
void FrameFocus::focusIndex(int index) {
if (isFrameAtIndex(index)) {
frames[index]->setFocus();
success();
} else {
frameNotFound();
}
}
bool FrameFocus::isFrameAtIndex(int index) {
return 0 <= index && index < frames.length();
}
void FrameFocus::focusId(QString name) {
for (int i = 0; i < frames.length(); i++) {
if (frames[i]->frameName().compare(name) == 0) {
frames[i]->setFocus();
success();
return;
}
}
frameNotFound();
}
void FrameFocus::focusParent() {
if (page()->currentFrame()->parentFrame() == 0) {
QString response = "Already at parent frame.";
emit finished(false, response);
} else {
page()->currentFrame()->parentFrame()->setFocus();
success();
}
}
void FrameFocus::frameNotFound() {
QString response = "Unable to locate frame. ";
emit finished(false, response);
}
void FrameFocus::success() {
QString response;
emit finished(true, response);
}

View file

@ -1,6 +1,7 @@
#include "Command.h" #include "Command.h"
class WebPage; class WebPage;
class QWebFrame;
class FrameFocus : public Command { class FrameFocus : public Command {
Q_OBJECT Q_OBJECT
@ -8,5 +9,20 @@ class FrameFocus : public Command {
public: public:
FrameFocus(WebPage *page, QObject *parent = 0); FrameFocus(WebPage *page, QObject *parent = 0);
virtual void start(QStringList &arguments); virtual void start(QStringList &arguments);
private:
void findFrames();
void focusParent();
void focusIndex(int index);
bool isFrameAtIndex(int index);
void focusId(QString id);
void success();
void frameNotFound();
QList<QWebFrame *> frames;
}; };