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>
52 extern void qt_drt_run(bool b);
53 extern void qt_dump_set_accepts_editing(bool b);
54 extern void qt_dump_frame_loader(bool b);
59 // Choose some default values.
60 const unsigned int maxViewWidth = 800;
61 const unsigned int maxViewHeight = 600;
63 class WebPage : public QWebPage {
66 WebPage(QWidget *parent, DumpRenderTree *drt);
68 QWebPage *createWindow();
70 void javaScriptAlert(QWebFrame *frame, const QString& message);
71 void javaScriptConsoleMessage(const QString& message, unsigned int lineNumber, const QString& sourceID);
72 bool javaScriptConfirm(QWebFrame *frame, const QString& msg);
73 bool javaScriptPrompt(QWebFrame *frame, const QString& msg, const QString& defaultValue, QString* result);
76 void setViewGeometry(const QRect &r)
83 DumpRenderTree *m_drt;
86 WebPage::WebPage(QWidget *parent, DumpRenderTree *drt)
87 : QWebPage(parent), m_drt(drt)
89 settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
90 settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, true);
91 settings()->setAttribute(QWebSettings::LinksIncludedInFocusChain, false);
92 connect(this, SIGNAL(geometryChangeRequest(const QRect &)),
93 this, SLOT(setViewGeometry(const QRect & )));
95 setPluginFactory(new TestPlugin(this));
98 QWebPage *WebPage::createWindow()
100 return m_drt->createWindow();
103 void WebPage::javaScriptAlert(QWebFrame *frame, const QString& message)
105 fprintf(stdout, "ALERT: %s\n", message.toUtf8().constData());
108 void WebPage::javaScriptConsoleMessage(const QString& message, unsigned int lineNumber, const QString&)
110 fprintf (stdout, "CONSOLE MESSAGE: line %d: %s\n", lineNumber, message.toUtf8().constData());
113 bool WebPage::javaScriptConfirm(QWebFrame *frame, const QString& msg)
115 fprintf(stdout, "CONFIRM: %s\n", msg.toUtf8().constData());
119 bool WebPage::javaScriptPrompt(QWebFrame *frame, const QString& msg, const QString& defaultValue, QString* result)
121 fprintf(stdout, "PROMPT: %s, default text: %s\n", msg.toUtf8().constData(), defaultValue.toUtf8().constData());
122 *result = defaultValue;
126 DumpRenderTree::DumpRenderTree()
130 m_controller = new LayoutTestController(this);
131 connect(m_controller, SIGNAL(done()), this, SLOT(dump()), Qt::QueuedConnection);
133 QWebView *view = new QWebView(0);
134 view->resize(QSize(maxViewWidth, maxViewHeight));
135 m_page = new WebPage(view, this);
136 view->setPage(m_page);
137 connect(m_page, SIGNAL(frameCreated(QWebFrame *)), this, SLOT(connectFrame(QWebFrame *)));
138 connectFrame(m_page->mainFrame());
140 m_page->mainFrame()->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
141 m_page->mainFrame()->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
142 connect(m_page->mainFrame(), SIGNAL(titleChanged(const QString&)),
143 SLOT(titleChanged(const QString&)));
145 m_eventSender = new EventSender(m_page);
146 m_textInputController = new TextInputController(m_page);
148 QObject::connect(this, SIGNAL(quit()), qApp, SLOT(quit()), Qt::QueuedConnection);
150 QFocusEvent event(QEvent::FocusIn, Qt::ActiveWindowFocusReason);
151 QApplication::sendEvent(view, &event);
154 DumpRenderTree::~DumpRenderTree()
162 void DumpRenderTree::open()
166 m_stdin->open(stdin, QFile::ReadOnly);
170 m_notifier = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read);
171 connect(m_notifier, SIGNAL(activated(int)), this, SLOT(readStdin(int)));
175 void DumpRenderTree::open(const QUrl& url)
179 qt_dump_frame_loader(url.toString().contains("loading/"));
180 m_page->mainFrame()->load(url);
183 void DumpRenderTree::readStdin(int /* socket */)
185 // Read incoming data from stdin...
186 QByteArray line = m_stdin->readLine();
187 if (line.endsWith('\n'))
188 line.truncate(line.size()-1);
189 //fprintf(stderr, "\n opening %s\n", line.constData());
193 if (line.startsWith("http:") || line.startsWith("https:"))
197 open(QUrl::fromLocalFile(fi.absoluteFilePath()));
203 void DumpRenderTree::resetJSObjects()
205 m_controller->reset();
206 foreach(QWidget *widget, windows)
211 void DumpRenderTree::initJSObjects()
213 QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
215 frame->addToJSWindowObject(QLatin1String("layoutTestController"), m_controller);
216 frame->addToJSWindowObject(QLatin1String("eventSender"), m_eventSender);
217 frame->addToJSWindowObject(QLatin1String("textInputController"), m_textInputController);
221 QString DumpRenderTree::dumpFramesAsText(QWebFrame* frame)
227 QWebFrame *parent = qobject_cast<QWebFrame *>(frame->parent());
229 result.append(QLatin1String("\n--------\nFrame: '"));
230 result.append(frame->name());
231 result.append(QLatin1String("'\n--------\n"));
234 result.append(frame->innerText());
235 result.append(QLatin1String("\n"));
237 if (m_controller->shouldDumpChildrenAsText()) {
238 QList<QWebFrame *> children = frame->childFrames();
239 for (int i = 0; i < children.size(); ++i)
240 result += dumpFramesAsText(children.at(i));
246 void DumpRenderTree::dump()
248 QWebFrame *frame = m_page->mainFrame();
250 //fprintf(stderr, " Dumping\n");
252 // Dump markup in single file mode...
253 QString markup = frame->markup();
254 fprintf(stdout, "Source:\n\n%s\n", markup.toUtf8().constData());
257 // Dump render text...
259 if (m_controller->shouldDumpAsText()) {
260 renderDump = dumpFramesAsText(frame);
262 renderDump = frame->renderTreeDump();
264 if (renderDump.isEmpty()) {
265 printf("ERROR: nil result from %s", m_controller->shouldDumpAsText() ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
267 fprintf(stdout, "%s", renderDump.toUtf8().constData());
270 fprintf(stdout, "#EOF\n");
274 fprintf(stderr, "#EOF\n");
279 // Exit now in single file mode...
284 void DumpRenderTree::titleChanged(const QString &s)
286 if (m_controller->shouldDumpTitleChanges())
287 printf("TITLE CHANGED: %s\n", s.toUtf8().data());
290 void DumpRenderTree::connectFrame(QWebFrame *frame)
292 connect(frame, SIGNAL(cleared()), this, SLOT(initJSObjects()));
293 connect(frame, SIGNAL(provisionalLoad()),
294 layoutTestController(), SLOT(provisionalLoad()));
296 if (frame == m_page->mainFrame()) {
297 connect(frame, SIGNAL(loadDone(bool)),
298 layoutTestController(), SLOT(maybeDump(bool)));
302 QWebPage *DumpRenderTree::createWindow()
304 if (!m_controller->canOpenWindows())
306 QWidget *container = new QWidget(0);
307 container->resize(0, 0);
308 container->move(-1, -1);
310 QWebPage *page = new WebPage(container, this);
311 connect(m_page, SIGNAL(frameCreated(QWebFrame *)), this, SLOT(connectFrame(QWebFrame *)));
312 windows.append(container);
316 int DumpRenderTree::windowCount() const
319 foreach(QWidget *w, windows) {
320 if (w->children().count())
328 #include "DumpRenderTree.moc"