2 * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "DumpRenderTree.h"
31 #include "jsobjects.h"
37 #include <QScrollArea>
38 #include <QApplication>
40 #include <QFocusEvent>
43 #include <qwebframe.h>
44 #include <qwebsettings.h>
48 extern void qt_drt_run(bool b);
49 extern void qt_dump_set_accepts_editing(bool b);
54 // Choose some default values.
55 const unsigned int maxViewWidth = 800;
56 const unsigned int maxViewHeight = 600;
58 class WebPage : public QWebPage {
60 WebPage(QWidget *parent, DumpRenderTree *drt);
62 QWebPage *createWindow();
64 void javaScriptAlert(QWebFrame *frame, const QString& message);
65 void javaScriptConsoleMessage(const QString& message, unsigned int lineNumber, const QString& sourceID);
66 bool javaScriptConfirm(QWebFrame *frame, const QString& msg);
67 bool javaScriptPrompt(QWebFrame *frame, const QString& msg, const QString& defaultValue, QString* result);
70 DumpRenderTree *m_drt;
73 WebPage::WebPage(QWidget *parent, DumpRenderTree *drt)
74 : QWebPage(parent), m_drt(drt)
76 settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
77 settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, true);
80 QWebPage *WebPage::createWindow()
82 return m_drt->createWindow();
85 void WebPage::javaScriptAlert(QWebFrame *frame, const QString& message)
87 fprintf(stdout, "ALERT: %s\n", message.toUtf8().constData());
90 void WebPage::javaScriptConsoleMessage(const QString& message, unsigned int lineNumber, const QString&)
92 fprintf (stdout, "CONSOLE MESSAGE: line %d: %s\n", lineNumber, message.toUtf8().constData());
95 bool WebPage::javaScriptConfirm(QWebFrame *frame, const QString& msg)
97 fprintf(stdout, "CONFIRM: %s\n", msg.toUtf8().constData());
101 bool WebPage::javaScriptPrompt(QWebFrame *frame, const QString& msg, const QString& defaultValue, QString* result)
103 fprintf(stdout, "PROMPT: %s, default text: %s\n", msg.toUtf8().constData(), defaultValue.toUtf8().constData());
104 *result = defaultValue;
108 DumpRenderTree::DumpRenderTree()
112 m_controller = new LayoutTestController(this);
113 connect(m_controller, SIGNAL(done()), this, SLOT(dump()), Qt::QueuedConnection);
115 m_page = new WebPage(0, this);
116 connect(m_page, SIGNAL(frameCreated(QWebFrame *)), this, SLOT(connectFrame(QWebFrame *)));
117 m_page->resize(maxViewWidth, maxViewHeight);
118 m_page->mainFrame()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
119 m_page->mainFrame()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
120 connect(m_page, SIGNAL(titleChanged(const QString&)),
121 SLOT(titleChanged(const QString&)));
123 m_eventSender = new EventSender(m_page);
124 m_textInputController = new TextInputController(m_page);
126 QObject::connect(this, SIGNAL(quit()), qApp, SLOT(quit()), Qt::QueuedConnection);
128 QFocusEvent event(QEvent::FocusIn, Qt::ActiveWindowFocusReason);
129 QApplication::sendEvent(m_page, &event);
132 DumpRenderTree::~DumpRenderTree()
140 void DumpRenderTree::open()
144 m_stdin->open(stdin, QFile::ReadOnly);
148 m_notifier = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read);
149 connect(m_notifier, SIGNAL(activated(int)), this, SLOT(readStdin(int)));
153 void DumpRenderTree::open(const QUrl& url)
159 void DumpRenderTree::readStdin(int /* socket */)
161 // Read incoming data from stdin...
162 QByteArray line = m_stdin->readLine();
163 if (line.endsWith('\n'))
164 line.truncate(line.size()-1);
165 //fprintf(stderr, "\n opening %s\n", line.constData());
169 open(QUrl::fromLocalFile(fi.absoluteFilePath()));
173 void DumpRenderTree::resetJSObjects()
175 m_controller->reset();
176 foreach(QWidget *widget, windows)
181 void DumpRenderTree::initJSObjects()
183 QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
185 frame->addToJSWindowObject("layoutTestController", m_controller);
186 frame->addToJSWindowObject("eventSender", m_eventSender);
187 frame->addToJSWindowObject("textInputController", m_textInputController);
191 QString DumpRenderTree::dumpFramesAsText(QWebFrame* frame)
197 QWebFrame *parent = qobject_cast<QWebFrame *>(frame->parent());
199 result.append(QLatin1String("\n--------\nFrame: '"));
200 result.append(frame->name());
201 result.append(QLatin1String("'\n--------\n"));
204 result.append(frame->innerText());
205 result.append(QLatin1String("\n"));
207 if (m_controller->shouldDumpChildrenAsText()) {
208 QList<QWebFrame *> children = frame->childFrames();
209 for (int i = 0; i < children.size(); ++i)
210 result += dumpFramesAsText(children.at(i));
216 void DumpRenderTree::dump()
218 QWebFrame *frame = m_page->mainFrame();
220 //fprintf(stderr, " Dumping\n");
222 // Dump markup in single file mode...
223 QString markup = frame->markup();
224 fprintf(stdout, "Source:\n\n%s\n", markup.toUtf8().constData());
227 // Dump render text...
229 if (m_controller->shouldDumpAsText()) {
230 renderDump = dumpFramesAsText(frame);
232 renderDump = frame->renderTreeDump();
234 if (renderDump.isEmpty()) {
235 printf("ERROR: nil result from %s", m_controller->shouldDumpAsText() ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
237 fprintf(stdout, "%s", renderDump.toUtf8().constData());
240 fprintf(stdout, "#EOF\n");
245 // Exit now in single file mode...
250 void DumpRenderTree::titleChanged(const QString &s)
252 if (m_controller->shouldDumpTitleChanges())
253 printf("TITLE CHANGED: %s\n", s.toUtf8().data());
256 void DumpRenderTree::connectFrame(QWebFrame *frame)
258 connect(frame, SIGNAL(cleared()), this, SLOT(initJSObjects()));
259 connect(frame, SIGNAL(provisionalLoad()),
260 layoutTestController(), SLOT(provisionalLoad()));
262 if (frame == m_page->mainFrame()) {
263 connect(frame, SIGNAL(loadDone(bool)),
264 layoutTestController(), SLOT(maybeDump(bool)));
268 QWebPage *DumpRenderTree::createWindow()
270 if (!m_controller->canOpenWindows())
272 QWidget *container = new QWidget(0);
273 container->resize(0, 0);
274 container->move(-1, -1);
276 QWebPage *page = new WebPage(container, this);
277 connect(m_page, SIGNAL(frameCreated(QWebFrame *)), this, SLOT(connectFrame(QWebFrame *)));
278 windows.append(container);
282 int DumpRenderTree::windowCount() const
285 foreach(QWidget *w, windows) {
286 if (w->children().count())