2 * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2008 Trolltech ASA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16 * its contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "DumpRenderTree.h"
32 #include "jsobjects.h"
33 #include "testplugin.h"
39 #include <QScrollArea>
40 #include <QApplication>
42 #include <QFocusEvent>
45 #include <qwebframe.h>
47 #include <qwebsettings.h>
51 extern void qt_drt_run(bool b);
52 extern void qt_dump_set_accepts_editing(bool b);
57 // Choose some default values.
58 const unsigned int maxViewWidth = 800;
59 const unsigned int maxViewHeight = 600;
61 class WebPage : public QWebPage {
64 WebPage(QWidget *parent, DumpRenderTree *drt);
66 QWebPage *createWindow();
68 void javaScriptAlert(QWebFrame *frame, const QString& message);
69 void javaScriptConsoleMessage(const QString& message, unsigned int lineNumber, const QString& sourceID);
70 bool javaScriptConfirm(QWebFrame *frame, const QString& msg);
71 bool javaScriptPrompt(QWebFrame *frame, const QString& msg, const QString& defaultValue, QString* result);
74 void setViewGeometry(const QRect &r)
81 DumpRenderTree *m_drt;
84 WebPage::WebPage(QWidget *parent, DumpRenderTree *drt)
85 : QWebPage(parent), m_drt(drt)
87 settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
88 settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, true);
89 settings()->setAttribute(QWebSettings::LinksIncludedInFocusChain, false);
90 connect(this, SIGNAL(geometryChangeRequest(const QRect &)),
91 this, SLOT(setViewGeometry(const QRect & )));
93 setPluginFactory(new TestPlugin(this));
96 QWebPage *WebPage::createWindow()
98 return m_drt->createWindow();
101 void WebPage::javaScriptAlert(QWebFrame *frame, const QString& message)
103 fprintf(stdout, "ALERT: %s\n", message.toUtf8().constData());
106 void WebPage::javaScriptConsoleMessage(const QString& message, unsigned int lineNumber, const QString&)
108 fprintf (stdout, "CONSOLE MESSAGE: line %d: %s\n", lineNumber, message.toUtf8().constData());
111 bool WebPage::javaScriptConfirm(QWebFrame *frame, const QString& msg)
113 fprintf(stdout, "CONFIRM: %s\n", msg.toUtf8().constData());
117 bool WebPage::javaScriptPrompt(QWebFrame *frame, const QString& msg, const QString& defaultValue, QString* result)
119 fprintf(stdout, "PROMPT: %s, default text: %s\n", msg.toUtf8().constData(), defaultValue.toUtf8().constData());
120 *result = defaultValue;
124 DumpRenderTree::DumpRenderTree()
128 m_controller = new LayoutTestController(this);
129 connect(m_controller, SIGNAL(done()), this, SLOT(dump()), Qt::QueuedConnection);
131 QWebView *view = new QWebView(0);
132 view->resize(QSize(maxViewWidth, maxViewHeight));
133 m_page = new WebPage(view, this);
134 view->setPage(m_page);
135 connect(m_page, SIGNAL(frameCreated(QWebFrame *)), this, SLOT(connectFrame(QWebFrame *)));
136 connectFrame(m_page->mainFrame());
138 m_page->mainFrame()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
139 m_page->mainFrame()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
140 connect(m_page->mainFrame(), SIGNAL(titleChanged(const QString&)),
141 SLOT(titleChanged(const QString&)));
143 m_eventSender = new EventSender(m_page);
144 m_textInputController = new TextInputController(m_page);
146 QObject::connect(this, SIGNAL(quit()), qApp, SLOT(quit()), Qt::QueuedConnection);
148 QFocusEvent event(QEvent::FocusIn, Qt::ActiveWindowFocusReason);
149 QApplication::sendEvent(view, &event);
152 DumpRenderTree::~DumpRenderTree()
160 void DumpRenderTree::open()
164 m_stdin->open(stdin, QFile::ReadOnly);
168 m_notifier = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read);
169 connect(m_notifier, SIGNAL(activated(int)), this, SLOT(readStdin(int)));
173 void DumpRenderTree::open(const QUrl& url)
176 m_page->mainFrame()->load(url);
179 void DumpRenderTree::readStdin(int /* socket */)
181 // Read incoming data from stdin...
182 QByteArray line = m_stdin->readLine();
183 if (line.endsWith('\n'))
184 line.truncate(line.size()-1);
185 //fprintf(stderr, "\n opening %s\n", line.constData());
189 if (line.startsWith("http:") || line.startsWith("https:"))
193 open(QUrl::fromLocalFile(fi.absoluteFilePath()));
199 void DumpRenderTree::resetJSObjects()
201 m_controller->reset();
202 foreach(QWidget *widget, windows)
207 void DumpRenderTree::initJSObjects()
209 QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
211 frame->addToJSWindowObject(QLatin1String("layoutTestController"), m_controller);
212 frame->addToJSWindowObject(QLatin1String("eventSender"), m_eventSender);
213 frame->addToJSWindowObject(QLatin1String("textInputController"), m_textInputController);
217 QString DumpRenderTree::dumpFramesAsText(QWebFrame* frame)
223 QWebFrame *parent = qobject_cast<QWebFrame *>(frame->parent());
225 result.append(QLatin1String("\n--------\nFrame: '"));
226 result.append(frame->name());
227 result.append(QLatin1String("'\n--------\n"));
230 result.append(frame->innerText());
231 result.append(QLatin1String("\n"));
233 if (m_controller->shouldDumpChildrenAsText()) {
234 QList<QWebFrame *> children = frame->childFrames();
235 for (int i = 0; i < children.size(); ++i)
236 result += dumpFramesAsText(children.at(i));
242 void DumpRenderTree::dump()
244 QWebFrame *frame = m_page->mainFrame();
246 //fprintf(stderr, " Dumping\n");
248 // Dump markup in single file mode...
249 QString markup = frame->markup();
250 fprintf(stdout, "Source:\n\n%s\n", markup.toUtf8().constData());
253 // Dump render text...
255 if (m_controller->shouldDumpAsText()) {
256 renderDump = dumpFramesAsText(frame);
258 renderDump = frame->renderTreeDump();
260 if (renderDump.isEmpty()) {
261 printf("ERROR: nil result from %s", m_controller->shouldDumpAsText() ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
263 fprintf(stdout, "%s", renderDump.toUtf8().constData());
266 fprintf(stdout, "#EOF\n");
270 fprintf(stderr, "#EOF\n");
275 // Exit now in single file mode...
280 void DumpRenderTree::titleChanged(const QString &s)
282 if (m_controller->shouldDumpTitleChanges())
283 printf("TITLE CHANGED: %s\n", s.toUtf8().data());
286 void DumpRenderTree::connectFrame(QWebFrame *frame)
288 connect(frame, SIGNAL(cleared()), this, SLOT(initJSObjects()));
289 connect(frame, SIGNAL(provisionalLoad()),
290 layoutTestController(), SLOT(provisionalLoad()));
292 if (frame == m_page->mainFrame()) {
293 connect(frame, SIGNAL(loadDone(bool)),
294 layoutTestController(), SLOT(maybeDump(bool)));
298 QWebPage *DumpRenderTree::createWindow()
300 if (!m_controller->canOpenWindows())
302 QWidget *container = new QWidget(0);
303 container->resize(0, 0);
304 container->move(-1, -1);
306 QWebPage *page = new WebPage(container, this);
307 connect(m_page, SIGNAL(frameCreated(QWebFrame *)), this, SLOT(connectFrame(QWebFrame *)));
308 windows.append(container);
312 int DumpRenderTree::windowCount() const
315 foreach(QWidget *w, windows) {
316 if (w->children().count())
324 #include "DumpRenderTree.moc"