2 * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
4 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
5 * Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "DumpRenderTreeQt.h"
35 #include "../../../WebKit/qt/WebCoreSupport/DumpRenderTreeSupportQt.h"
36 #include "EventSenderQt.h"
37 #include "GCControllerQt.h"
38 #include "LayoutTestControllerQt.h"
39 #include "TextInputControllerQt.h"
40 #include "PlainTextControllerQt.h"
41 #include "testplugin.h"
42 #include "WorkQueue.h"
44 #include <QApplication>
46 #include <QCryptographicHash>
50 #include <QFocusEvent>
51 #include <QFontDatabase>
53 #include <QNetworkAccessManager>
54 #include <QNetworkReply>
55 #include <QNetworkRequest>
56 #include <QPaintDevice>
57 #include <QPaintEngine>
64 #include <qwebsettings.h>
65 #include <qwebsecurityorigin.h>
68 #include <QtUiTools/QUiLoader>
72 #include <fontconfig/fontconfig.h>
86 NetworkAccessManager::NetworkAccessManager(QObject* parent)
87 : QNetworkAccessManager(parent)
90 connect(this, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)),
91 this, SLOT(sslErrorsEncountered(QNetworkReply*, const QList<QSslError>&)));
96 void NetworkAccessManager::sslErrorsEncountered(QNetworkReply* reply, const QList<QSslError>& errors)
98 if (reply->url().host() == "127.0.0.1" || reply->url().host() == "localhost") {
101 // Accept any HTTPS certificate.
102 foreach (const QSslError& error, errors) {
103 if (error.error() < QSslError::UnableToGetIssuerCertificate || error.error() > QSslError::HostNameMismatch) {
110 reply->ignoreSslErrors();
116 #ifndef QT_NO_PRINTER
117 class NullPrinter : public QPrinter {
119 class NullPaintEngine : public QPaintEngine {
121 virtual bool begin(QPaintDevice*) { return true; }
122 virtual bool end() { return true; }
123 virtual QPaintEngine::Type type() const { return QPaintEngine::User; }
124 virtual void drawPixmap(const QRectF& r, const QPixmap& pm, const QRectF& sr) { }
125 virtual void updateState(const QPaintEngineState& state) { }
128 virtual QPaintEngine* paintEngine() const { return const_cast<NullPaintEngine*>(&m_engine); }
130 NullPaintEngine m_engine;
134 WebPage::WebPage(QObject* parent, DumpRenderTree* drt)
139 QWebSettings* globalSettings = QWebSettings::globalSettings();
141 globalSettings->setFontSize(QWebSettings::MinimumFontSize, 5);
142 globalSettings->setFontSize(QWebSettings::MinimumLogicalFontSize, 5);
143 globalSettings->setFontSize(QWebSettings::DefaultFontSize, 16);
144 globalSettings->setFontSize(QWebSettings::DefaultFixedFontSize, 13);
146 globalSettings->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
147 globalSettings->setAttribute(QWebSettings::JavascriptCanAccessClipboard, true);
148 globalSettings->setAttribute(QWebSettings::LinksIncludedInFocusChain, false);
149 globalSettings->setAttribute(QWebSettings::PluginsEnabled, true);
150 globalSettings->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
151 globalSettings->setAttribute(QWebSettings::JavascriptEnabled, true);
152 globalSettings->setAttribute(QWebSettings::PrivateBrowsingEnabled, false);
153 globalSettings->setAttribute(QWebSettings::SpatialNavigationEnabled, false);
155 connect(this, SIGNAL(geometryChangeRequested(const QRect &)),
156 this, SLOT(setViewGeometry(const QRect & )));
158 setNetworkAccessManager(m_drt->networkAccessManager());
159 setPluginFactory(new TestPlugin(this));
161 connect(this, SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)), this, SLOT(requestPermission(QWebFrame*, QWebPage::Feature)));
162 connect(this, SIGNAL(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)), this, SLOT(cancelPermission(QWebFrame*, QWebPage::Feature)));
167 delete m_webInspector;
170 QWebInspector* WebPage::webInspector()
172 if (!m_webInspector) {
173 m_webInspector = new QWebInspector;
174 m_webInspector->setPage(this);
176 return m_webInspector;
179 void WebPage::resetSettings()
181 // After each layout test, reset the settings that may have been changed by
182 // layoutTestController.overridePreference() or similar.
183 settings()->resetFontSize(QWebSettings::DefaultFontSize);
184 settings()->resetAttribute(QWebSettings::JavascriptCanOpenWindows);
185 settings()->resetAttribute(QWebSettings::JavascriptEnabled);
186 settings()->resetAttribute(QWebSettings::PrivateBrowsingEnabled);
187 settings()->resetAttribute(QWebSettings::SpatialNavigationEnabled);
188 settings()->resetAttribute(QWebSettings::LinksIncludedInFocusChain);
189 settings()->resetAttribute(QWebSettings::OfflineWebApplicationCacheEnabled);
190 settings()->resetAttribute(QWebSettings::LocalContentCanAccessRemoteUrls);
191 settings()->resetAttribute(QWebSettings::PluginsEnabled);
192 settings()->resetAttribute(QWebSettings::JavascriptCanAccessClipboard);
193 settings()->resetAttribute(QWebSettings::AutoLoadImages);
195 m_drt->layoutTestController()->setCaretBrowsingEnabled(false);
196 m_drt->layoutTestController()->setFrameFlatteningEnabled(false);
197 m_drt->layoutTestController()->setSmartInsertDeleteEnabled(true);
198 m_drt->layoutTestController()->setSelectTrailingWhitespaceEnabled(false);
200 // globalSettings must be reset explicitly.
201 m_drt->layoutTestController()->setXSSAuditorEnabled(false);
203 QWebSettings::setMaximumPagesInCache(0); // reset to default
204 settings()->setUserStyleSheetUrl(QUrl()); // reset to default
206 m_pendingGeolocationRequests.clear();
209 QWebPage *WebPage::createWindow(QWebPage::WebWindowType)
211 return m_drt->createWindow();
214 void WebPage::javaScriptAlert(QWebFrame*, const QString& message)
216 if (!isTextOutputEnabled())
219 fprintf(stdout, "ALERT: %s\n", message.toUtf8().constData());
222 void WebPage::requestPermission(QWebFrame* frame, QWebPage::Feature feature)
226 if (!m_drt->layoutTestController()->ignoreReqestForPermission())
227 setFeaturePermission(frame, feature, PermissionGrantedByUser);
230 if (m_drt->layoutTestController()->isGeolocationPermissionSet())
231 if (m_drt->layoutTestController()->geolocationPermission())
232 setFeaturePermission(frame, feature, PermissionGrantedByUser);
234 setFeaturePermission(frame, feature, PermissionDeniedByUser);
236 m_pendingGeolocationRequests.append(frame);
243 void WebPage::cancelPermission(QWebFrame* frame, QWebPage::Feature feature)
247 m_pendingGeolocationRequests.removeOne(frame);
254 void WebPage::permissionSet(QWebPage::Feature feature)
259 Q_ASSERT(m_drt->layoutTestController()->isGeolocationPermissionSet());
260 foreach (QWebFrame* frame, m_pendingGeolocationRequests)
261 if (m_drt->layoutTestController()->geolocationPermission())
262 setFeaturePermission(frame, feature, PermissionGrantedByUser);
264 setFeaturePermission(frame, feature, PermissionDeniedByUser);
266 m_pendingGeolocationRequests.clear();
274 static QString urlSuitableForTestResult(const QString& url)
276 if (url.isEmpty() || !url.startsWith(QLatin1String("file://")))
279 return QFileInfo(url).fileName();
282 void WebPage::javaScriptConsoleMessage(const QString& message, int lineNumber, const QString&)
284 if (!isTextOutputEnabled())
288 if (!message.isEmpty()) {
289 newMessage = message;
291 size_t fileProtocol = newMessage.indexOf(QLatin1String("file://"));
292 if (fileProtocol != -1) {
293 newMessage = newMessage.left(fileProtocol) + urlSuitableForTestResult(newMessage.mid(fileProtocol));
297 fprintf (stdout, "CONSOLE MESSAGE: line %d: %s\n", lineNumber, newMessage.toUtf8().constData());
300 bool WebPage::javaScriptConfirm(QWebFrame*, const QString& msg)
302 if (!isTextOutputEnabled())
305 fprintf(stdout, "CONFIRM: %s\n", msg.toUtf8().constData());
309 bool WebPage::javaScriptPrompt(QWebFrame*, const QString& msg, const QString& defaultValue, QString* result)
311 if (!isTextOutputEnabled())
314 fprintf(stdout, "PROMPT: %s, default text: %s\n", msg.toUtf8().constData(), defaultValue.toUtf8().constData());
315 *result = defaultValue;
319 bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
321 if (m_drt->layoutTestController()->waitForPolicy()) {
322 QString url = QString::fromUtf8(request.url().toEncoded());
323 QString typeDescription;
326 case NavigationTypeLinkClicked:
327 typeDescription = "link clicked";
329 case NavigationTypeFormSubmitted:
330 typeDescription = "form submitted";
332 case NavigationTypeBackOrForward:
333 typeDescription = "back/forward";
335 case NavigationTypeReload:
336 typeDescription = "reload";
338 case NavigationTypeFormResubmitted:
339 typeDescription = "form resubmitted";
341 case NavigationTypeOther:
342 typeDescription = "other";
345 typeDescription = "illegal value";
348 if (isTextOutputEnabled())
349 fprintf(stdout, "Policy delegate: attempt to load %s with navigation type '%s'\n",
350 url.toUtf8().constData(), typeDescription.toUtf8().constData());
352 m_drt->layoutTestController()->notifyDone();
354 return QWebPage::acceptNavigationRequest(frame, request, type);
357 bool WebPage::supportsExtension(QWebPage::Extension extension) const
359 if (extension == QWebPage::ErrorPageExtension)
360 return m_drt->layoutTestController()->shouldHandleErrorPages();
365 bool WebPage::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output)
367 const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
369 // Lets handle error pages for the main frame for now.
370 if (info->frame != mainFrame())
373 QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
375 errorPage->content = QString("data:text/html,<body/>").toUtf8();
380 QObject* WebPage::createPlugin(const QString& classId, const QUrl& url, const QStringList& paramNames, const QStringList& paramValues)
383 Q_UNUSED(paramNames);
384 Q_UNUSED(paramValues);
385 #ifndef QT_NO_UITOOLS
387 return loader.createWidget(classId, view());
394 void WebPage::setViewGeometry(const QRect& rect)
396 if (WebViewGraphicsBased* v = qobject_cast<WebViewGraphicsBased*>(view()))
397 v->scene()->setSceneRect(QRectF(rect));
398 else if (QWidget *v = view())
399 v->setGeometry(rect);
402 WebViewGraphicsBased::WebViewGraphicsBased(QWidget* parent)
403 : m_item(new QGraphicsWebView)
405 setScene(new QGraphicsScene(this));
406 scene()->addItem(m_item);
409 DumpRenderTree::DumpRenderTree()
410 : m_dumpPixels(false)
412 , m_enableTextOutput(false)
413 , m_standAloneMode(false)
414 , m_graphicsBased(false)
415 , m_persistentStoragePath(QString(getenv("DUMPRENDERTREE_TEMP")))
418 QByteArray viewMode = getenv("QT_DRT_WEBVIEW_MODE");
419 if (viewMode == "graphics")
420 setGraphicsBased(true);
422 DumpRenderTreeSupportQt::overwritePluginDirectories();
424 QWebSettings::enablePersistentStorage(m_persistentStoragePath);
426 m_networkAccessManager = new NetworkAccessManager(this);
427 // create our primary testing page/view.
428 if (isGraphicsBased()) {
429 WebViewGraphicsBased* view = new WebViewGraphicsBased(0);
430 m_page = new WebPage(view, this);
431 view->setPage(m_page);
434 QWebView* view = new QWebView(0);
435 m_page = new WebPage(view, this);
436 view->setPage(m_page);
439 // Use a frame group name for all pages created by DumpRenderTree to allow
440 // testing of cross-page frame lookup.
441 DumpRenderTreeSupportQt::webPageSetGroupName(m_page, "org.webkit.qt.DumpRenderTree");
443 m_mainView->setContextMenuPolicy(Qt::NoContextMenu);
444 m_mainView->resize(QSize(LayoutTestController::maxViewWidth, LayoutTestController::maxViewHeight));
446 // clean up cache by resetting quota.
447 qint64 quota = webPage()->settings()->offlineWebApplicationCacheQuota();
448 webPage()->settings()->setOfflineWebApplicationCacheQuota(quota);
450 // create our controllers. This has to be done before connectFrame,
451 // as it exports there to the JavaScript DOM window.
452 m_controller = new LayoutTestController(this);
453 connect(m_controller, SIGNAL(showPage()), this, SLOT(showPage()));
454 connect(m_controller, SIGNAL(hidePage()), this, SLOT(hidePage()));
456 // async geolocation permission set by controller
457 connect(m_controller, SIGNAL(geolocationPermissionSet()), this, SLOT(geolocationPermissionSet()));
459 connect(m_controller, SIGNAL(done()), this, SLOT(dump()));
460 m_eventSender = new EventSender(m_page);
461 m_textInputController = new TextInputController(m_page);
462 m_plainTextController = new PlainTextController(m_page);
463 m_gcController = new GCController(m_page);
465 // now connect our different signals
466 connect(m_page, SIGNAL(frameCreated(QWebFrame *)),
467 this, SLOT(connectFrame(QWebFrame *)));
468 connectFrame(m_page->mainFrame());
470 connect(m_page, SIGNAL(loadFinished(bool)),
471 m_controller, SLOT(maybeDump(bool)));
472 // We need to connect to loadStarted() because notifyDone should only
473 // dump results itself when the last page loaded in the test has finished loading.
474 connect(m_page, SIGNAL(loadStarted()),
475 m_controller, SLOT(resetLoadFinished()));
476 connect(m_page, SIGNAL(windowCloseRequested()), this, SLOT(windowCloseRequested()));
477 connect(m_page, SIGNAL(printRequested(QWebFrame*)), this, SLOT(dryRunPrint(QWebFrame*)));
479 connect(m_page->mainFrame(), SIGNAL(titleChanged(const QString&)),
480 SLOT(titleChanged(const QString&)));
481 connect(m_page, SIGNAL(databaseQuotaExceeded(QWebFrame*,QString)),
482 this, SLOT(dumpDatabaseQuota(QWebFrame*,QString)));
483 connect(m_page, SIGNAL(statusBarMessage(const QString&)),
484 this, SLOT(statusBarMessage(const QString&)));
486 QObject::connect(this, SIGNAL(quit()), qApp, SLOT(quit()), Qt::QueuedConnection);
488 DumpRenderTreeSupportQt::setDumpRenderTreeModeEnabled(true);
489 QFocusEvent event(QEvent::FocusIn, Qt::ActiveWindowFocusReason);
490 QApplication::sendEvent(m_mainView, &event);
493 DumpRenderTree::~DumpRenderTree()
499 static void clearHistory(QWebPage* page)
501 // QWebHistory::clear() leaves current page, so remove it as well by setting
502 // max item count to 0, and then setting it back to it's original value.
504 QWebHistory* history = page->history();
505 int itemCount = history->maximumItemCount();
508 history->setMaximumItemCount(0);
509 history->setMaximumItemCount(itemCount);
512 void DumpRenderTree::dryRunPrint(QWebFrame* frame)
514 #ifndef QT_NO_PRINTER
516 frame->print(&printer);
520 void DumpRenderTree::resetToConsistentStateBeforeTesting()
522 // reset so that any current loads are stopped
523 // NOTE: that this has to be done before the layoutTestController is
524 // reset or we get timeouts for some tests.
525 m_page->blockSignals(true);
526 m_page->triggerAction(QWebPage::Stop);
527 m_page->blockSignals(false);
529 // reset the layoutTestController at this point, so that we under no
530 // circumstance dump (stop the waitUntilDone timer) during the reset
532 m_controller->reset();
534 // reset mouse clicks counter
535 m_eventSender->resetClickCount();
537 closeRemainingWindows();
539 m_page->resetSettings();
540 m_page->undoStack()->clear();
541 m_page->mainFrame()->setZoomFactor(1.0);
542 clearHistory(m_page);
543 DumpRenderTreeSupportQt::clearFrameName(m_page->mainFrame());
545 m_page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded);
546 m_page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded);
548 WorkQueue::shared()->clear();
549 WorkQueue::shared()->setFrozen(false);
551 DumpRenderTreeSupportQt::resetOriginAccessWhiteLists();
553 // Qt defaults to Windows editing behavior.
554 DumpRenderTreeSupportQt::setEditingBehavior(m_page, "win");
556 QLocale::setDefault(QLocale::c());
559 setlocale(LC_ALL, "");
563 static bool isGlobalHistoryTest(const QUrl& url)
565 if (url.path().contains("globalhistory/"))
570 static bool isWebInspectorTest(const QUrl& url)
572 if (url.path().contains("inspector/"))
577 static bool shouldEnableDeveloperExtras(const QUrl& url)
582 void DumpRenderTree::open(const QUrl& url)
584 DumpRenderTreeSupportQt::dumpResourceLoadCallbacksPath(QFileInfo(url.toString()).path());
585 resetToConsistentStateBeforeTesting();
587 if (shouldEnableDeveloperExtras(m_page->mainFrame()->url())) {
588 layoutTestController()->closeWebInspector();
589 layoutTestController()->setDeveloperExtrasEnabled(false);
592 if (shouldEnableDeveloperExtras(url)) {
593 layoutTestController()->setDeveloperExtrasEnabled(true);
594 if (isWebInspectorTest(url))
595 layoutTestController()->showWebInspector();
598 if (isGlobalHistoryTest(url))
599 layoutTestController()->dumpHistoryCallbacks();
601 // W3C SVG tests expect to be 480x360
602 bool isW3CTest = url.toString().contains("svg/W3C-SVG-1.1");
603 int width = isW3CTest ? 480 : LayoutTestController::maxViewWidth;
604 int height = isW3CTest ? 360 : LayoutTestController::maxViewHeight;
605 m_mainView->resize(QSize(width, height));
606 m_page->setPreferredContentsSize(QSize());
607 m_page->setViewportSize(QSize(width, height));
609 QFocusEvent ev(QEvent::FocusIn);
612 QWebSettings::clearMemoryCaches();
613 #if !(defined(Q_OS_SYMBIAN) && QT_VERSION <= QT_VERSION_CHECK(4, 6, 2))
614 QFontDatabase::removeAllApplicationFonts();
616 #if defined(Q_WS_X11)
620 DumpRenderTreeSupportQt::dumpFrameLoader(url.toString().contains("loading/"));
621 setTextOutputEnabled(true);
622 m_page->mainFrame()->load(url);
625 void DumpRenderTree::readLine()
629 m_stdin->open(stdin, QFile::ReadOnly);
631 if (!m_stdin->isReadable()) {
637 QByteArray line = m_stdin->readLine().trimmed();
639 if (line.isEmpty()) {
644 processLine(QString::fromLocal8Bit(line.constData(), line.length()));
647 void DumpRenderTree::processArgsLine(const QStringList &args)
649 setStandAloneMode(true);
651 for (int i = 1; i < args.size(); ++i)
652 if (!args.at(i).startsWith('-'))
653 m_standAloneModeTestList.append(args[i]);
655 QFileInfo firstEntry(m_standAloneModeTestList.first());
656 if (firstEntry.isDir()) {
657 QDir folderEntry(m_standAloneModeTestList.first());
658 QStringList supportedExt;
659 // Check for all supported extensions (from Scripts/webkitpy/layout_tests/layout_package/test_files.py).
660 supportedExt << "*.html" << "*.shtml" << "*.xml" << "*.xhtml" << "*.xhtmlmp" << "*.pl" << "*.php" << "*.svg";
661 m_standAloneModeTestList = folderEntry.entryList(supportedExt, QDir::Files);
662 for (int i = 0; i < m_standAloneModeTestList.size(); ++i)
663 m_standAloneModeTestList[i] = folderEntry.absoluteFilePath(m_standAloneModeTestList[i]);
666 processLine(m_standAloneModeTestList.first());
667 m_standAloneModeTestList.removeFirst();
669 connect(this, SIGNAL(ready()), this, SLOT(loadNextTestInStandAloneMode()));
672 void DumpRenderTree::loadNextTestInStandAloneMode()
674 if (m_standAloneModeTestList.isEmpty()) {
679 processLine(m_standAloneModeTestList.first());
680 m_standAloneModeTestList.removeFirst();
683 void DumpRenderTree::processLine(const QString &input)
685 QString line = input;
687 m_expectedHash = QString();
689 // single quote marks the pixel dump hash
690 int i = line.indexOf('\'');
692 m_expectedHash = line.mid(i + 1, line.length());
693 line.remove(i, line.length());
697 if (line.startsWith(QLatin1String("http:"))
698 || line.startsWith(QLatin1String("https:"))
699 || line.startsWith(QLatin1String("file:"))) {
705 QDir currentDir = QDir::currentPath();
707 // Try to be smart about where the test is located
708 if (currentDir.dirName() == QLatin1String("LayoutTests"))
709 fi = QFileInfo(currentDir, line.replace(QRegExp(".*?LayoutTests/(.*)"), "\\1"));
710 else if (!line.contains(QLatin1String("LayoutTests")))
711 fi = QFileInfo(currentDir, line.prepend(QLatin1String("LayoutTests/")));
719 open(QUrl::fromLocalFile(fi.absoluteFilePath()));
725 void DumpRenderTree::setDumpPixels(bool dump)
730 void DumpRenderTree::closeRemainingWindows()
732 foreach (QObject* widget, windows)
737 void DumpRenderTree::initJSObjects()
739 QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
741 frame->addToJavaScriptWindowObject(QLatin1String("layoutTestController"), m_controller);
742 frame->addToJavaScriptWindowObject(QLatin1String("eventSender"), m_eventSender);
743 frame->addToJavaScriptWindowObject(QLatin1String("textInputController"), m_textInputController);
744 frame->addToJavaScriptWindowObject(QLatin1String("GCController"), m_gcController);
745 frame->addToJavaScriptWindowObject(QLatin1String("plainText"), m_plainTextController);
748 void DumpRenderTree::showPage()
751 // we need a paint event but cannot process all the events
752 QPixmap pixmap(m_mainView->size());
753 m_mainView->render(&pixmap);
756 void DumpRenderTree::hidePage()
761 QString DumpRenderTree::dumpFrameScrollPosition(QWebFrame* frame)
763 if (!frame || !DumpRenderTreeSupportQt::hasDocumentElement(frame))
767 QPoint pos = frame->scrollPosition();
768 if (pos.x() > 0 || pos.y() > 0) {
769 QWebFrame* parent = qobject_cast<QWebFrame *>(frame->parent());
771 result.append(QString("frame '%1' ").arg(frame->title()));
772 result.append(QString("scrolled to %1,%2\n").arg(pos.x()).arg(pos.y()));
775 if (m_controller->shouldDumpChildFrameScrollPositions()) {
776 QList<QWebFrame*> children = frame->childFrames();
777 for (int i = 0; i < children.size(); ++i)
778 result += dumpFrameScrollPosition(children.at(i));
783 QString DumpRenderTree::dumpFramesAsText(QWebFrame* frame)
785 if (!frame || !DumpRenderTreeSupportQt::hasDocumentElement(frame))
789 QWebFrame* parent = qobject_cast<QWebFrame*>(frame->parent());
791 result.append(QLatin1String("\n--------\nFrame: '"));
792 result.append(frame->frameName());
793 result.append(QLatin1String("'\n--------\n"));
796 QString innerText = frame->toPlainText();
797 result.append(innerText);
798 result.append(QLatin1String("\n"));
800 if (m_controller->shouldDumpChildrenAsText()) {
801 QList<QWebFrame *> children = frame->childFrames();
802 for (int i = 0; i < children.size(); ++i)
803 result += dumpFramesAsText(children.at(i));
809 static QString dumpHistoryItem(const QWebHistoryItem& item, int indent, bool current)
815 result.append(QLatin1String("curr->"));
818 for (int i = start; i < indent; i++)
821 QString url = item.url().toEncoded();
822 if (url.contains("file://")) {
823 static QString layoutTestsString("/LayoutTests/");
824 static QString fileTestString("(file test):");
826 QString res = url.mid(url.indexOf(layoutTestsString) + layoutTestsString.length());
830 result.append(fileTestString);
836 QString target = DumpRenderTreeSupportQt::historyItemTarget(item);
837 if (!target.isEmpty())
838 result.append(QString(QLatin1String(" (in frame \"%1\")")).arg(target));
840 if (DumpRenderTreeSupportQt::isTargetItem(item))
841 result.append(QLatin1String(" **nav target**"));
842 result.append(QLatin1String("\n"));
844 QMap<QString, QWebHistoryItem> children = DumpRenderTreeSupportQt::getChildHistoryItems(item);
845 foreach (QWebHistoryItem item, children)
846 result += dumpHistoryItem(item, 12, false);
851 QString DumpRenderTree::dumpBackForwardList(QWebPage* page)
853 QWebHistory* history = page->history();
856 result.append(QLatin1String("\n============== Back Forward List ==============\n"));
859 // " (file test):fast/loader/resources/click-fragment-link.html **nav target**"
860 // "curr-> (file test):fast/loader/resources/click-fragment-link.html#testfragment **nav target**"
862 int maxItems = history->maximumItemCount();
864 foreach (const QWebHistoryItem item, history->backItems(maxItems)) {
867 result.append(dumpHistoryItem(item, 8, false));
870 QWebHistoryItem item = history->currentItem();
872 result.append(dumpHistoryItem(item, 8, true));
874 foreach (const QWebHistoryItem item, history->forwardItems(maxItems)) {
877 result.append(dumpHistoryItem(item, 8, false));
880 result.append(QLatin1String("===============================================\n"));
884 static const char *methodNameStringForFailedTest(LayoutTestController *controller)
886 const char *errorMessage;
887 if (controller->shouldDumpAsText())
888 errorMessage = "[documentElement innerText]";
889 // FIXME: Add when we have support
890 //else if (controller->dumpDOMAsWebArchive())
891 // errorMessage = "[[mainFrame DOMDocument] webArchive]";
892 //else if (controller->dumpSourceAsWebArchive())
893 // errorMessage = "[[mainFrame dataSource] webArchive]";
895 errorMessage = "[mainFrame renderTreeAsExternalRepresentation]";
900 void DumpRenderTree::dump()
902 // Prevent any further frame load or resource load callbacks from appearing after we dump the result.
903 DumpRenderTreeSupportQt::dumpFrameLoader(false);
904 DumpRenderTreeSupportQt::dumpResourceLoadCallbacks(false);
906 QWebFrame *mainFrame = m_page->mainFrame();
908 if (isStandAloneMode()) {
909 QString markup = mainFrame->toHtml();
910 fprintf(stdout, "Source:\n\n%s\n", markup.toUtf8().constData());
913 // Dump render text...
914 QString resultString;
915 if (m_controller->shouldDumpAsText())
916 resultString = dumpFramesAsText(mainFrame);
918 resultString = mainFrame->renderTreeDump();
919 resultString += dumpFrameScrollPosition(mainFrame);
921 if (!resultString.isEmpty()) {
922 fprintf(stdout, "Content-Type: text/plain\n");
923 fprintf(stdout, "%s", resultString.toUtf8().constData());
925 if (m_controller->shouldDumpBackForwardList()) {
926 fprintf(stdout, "%s", dumpBackForwardList(webPage()).toUtf8().constData());
927 foreach (QObject* widget, windows) {
928 QWebPage* page = qobject_cast<QWebPage*>(widget->findChild<QWebPage*>());
929 fprintf(stdout, "%s", dumpBackForwardList(page).toUtf8().constData());
934 printf("ERROR: nil result from %s", methodNameStringForFailedTest(m_controller));
936 // signal end of text block
937 fputs("#EOF\n", stdout);
938 fputs("#EOF\n", stderr);
940 // FIXME: All other ports don't dump pixels, if generatePixelResults is false.
942 QImage image(m_page->viewportSize(), QImage::Format_ARGB32);
943 image.fill(Qt::white);
944 QPainter painter(&image);
945 mainFrame->render(&painter);
948 QCryptographicHash hash(QCryptographicHash::Md5);
949 for (int row = 0; row < image.height(); ++row)
950 hash.addData(reinterpret_cast<const char*>(image.scanLine(row)), image.width() * 4);
951 QString actualHash = hash.result().toHex();
953 fprintf(stdout, "\nActualHash: %s\n", qPrintable(actualHash));
955 bool dumpImage = true;
957 if (!m_expectedHash.isEmpty()) {
958 Q_ASSERT(m_expectedHash.length() == 32);
959 fprintf(stdout, "\nExpectedHash: %s\n", qPrintable(m_expectedHash));
961 if (m_expectedHash == actualHash)
967 buffer.open(QBuffer::WriteOnly);
968 image.save(&buffer, "PNG");
970 const QByteArray &data = buffer.data();
972 printf("Content-Type: %s\n", "image/png");
973 printf("Content-Length: %lu\n", static_cast<unsigned long>(data.length()));
975 const quint32 bytesToWriteInOneChunk = 1 << 15;
976 quint32 dataRemainingToWrite = data.length();
977 const char *ptr = data.data();
978 while (dataRemainingToWrite) {
979 quint32 bytesToWriteInThisChunk = qMin(dataRemainingToWrite, bytesToWriteInOneChunk);
980 quint32 bytesWritten = fwrite(ptr, 1, bytesToWriteInThisChunk, stdout);
981 if (bytesWritten != bytesToWriteInThisChunk)
983 dataRemainingToWrite -= bytesWritten;
991 puts("#EOF"); // terminate the (possibly empty) pixels block
999 void DumpRenderTree::titleChanged(const QString &s)
1001 if (m_controller->shouldDumpTitleChanges())
1002 printf("TITLE CHANGED: %s\n", s.toUtf8().data());
1005 void DumpRenderTree::connectFrame(QWebFrame *frame)
1007 connect(frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(initJSObjects()));
1008 connect(frame, SIGNAL(provisionalLoad()),
1009 layoutTestController(), SLOT(provisionalLoad()));
1012 void DumpRenderTree::dumpDatabaseQuota(QWebFrame* frame, const QString& dbName)
1014 if (!m_controller->shouldDumpDatabaseCallbacks())
1016 QWebSecurityOrigin origin = frame->securityOrigin();
1017 printf("UI DELEGATE DATABASE CALLBACK: exceededDatabaseQuotaForSecurityOrigin:{%s, %s, %i} database:%s\n",
1018 origin.scheme().toUtf8().data(),
1019 origin.host().toUtf8().data(),
1021 dbName.toUtf8().data());
1022 origin.setDatabaseQuota(5 * 1024 * 1024);
1025 void DumpRenderTree::statusBarMessage(const QString& message)
1027 if (!m_controller->shouldDumpStatusCallbacks())
1030 printf("UI DELEGATE STATUS CALLBACK: setStatusText:%s\n", message.toUtf8().constData());
1033 QWebPage *DumpRenderTree::createWindow()
1035 if (!m_controller->canOpenWindows())
1038 // Create a dummy container object to track the page in DRT.
1039 // QObject is used instead of QWidget to prevent DRT from
1040 // showing the main view when deleting the container.
1042 QObject* container = new QObject(m_mainView);
1043 // create a QWebPage we want to return
1044 QWebPage* page = static_cast<QWebPage*>(new WebPage(container, this));
1045 // gets cleaned up in closeRemainingWindows()
1046 windows.append(container);
1048 // connect the needed signals to the page
1049 connect(page, SIGNAL(frameCreated(QWebFrame*)), this, SLOT(connectFrame(QWebFrame*)));
1050 connectFrame(page->mainFrame());
1051 connect(page, SIGNAL(loadFinished(bool)), m_controller, SLOT(maybeDump(bool)));
1052 connect(page, SIGNAL(windowCloseRequested()), this, SLOT(windowCloseRequested()));
1054 // Use a frame group name for all pages created by DumpRenderTree to allow
1055 // testing of cross-page frame lookup.
1056 DumpRenderTreeSupportQt::webPageSetGroupName(page, "org.webkit.qt.DumpRenderTree");
1061 void DumpRenderTree::windowCloseRequested()
1063 QWebPage* page = qobject_cast<QWebPage*>(sender());
1064 QObject* container = page->parent();
1065 windows.removeAll(container);
1066 // Our use of container->deleteLater() means we need to remove closed pages
1067 // from the org.webkit.qt.DumpRenderTree group explicitly.
1068 DumpRenderTreeSupportQt::webPageSetGroupName(page, "");
1069 container->deleteLater();
1072 int DumpRenderTree::windowCount() const
1074 // include the main view in the count
1075 return windows.count() + 1;
1078 void DumpRenderTree::geolocationPermissionSet()
1080 m_page->permissionSet(QWebPage::Geolocation);
1083 void DumpRenderTree::switchFocus(bool focused)
1085 QFocusEvent event((focused) ? QEvent::FocusIn : QEvent::FocusOut, Qt::ActiveWindowFocusReason);
1086 if (!isGraphicsBased())
1087 QApplication::sendEvent(m_mainView, &event);
1089 if (WebViewGraphicsBased* view = qobject_cast<WebViewGraphicsBased*>(m_mainView))
1090 view->scene()->sendEvent(view->graphicsView(), &event);
1095 #if defined(Q_WS_X11)
1096 void DumpRenderTree::initializeFonts()
1098 static int numFonts = -1;
1100 // Some test cases may add or remove application fonts (via @font-face).
1101 // Make sure to re-initialize the font set if necessary.
1102 FcFontSet* appFontSet = FcConfigGetFonts(0, FcSetApplication);
1103 if (appFontSet && numFonts >= 0 && appFontSet->nfont == numFonts)
1106 QByteArray fontDir = getenv("WEBKIT_TESTFONTS");
1107 if (fontDir.isEmpty() || !QDir(fontDir).exists()) {
1110 "----------------------------------------------------------------------\n"
1111 "WEBKIT_TESTFONTS environment variable is not set correctly.\n"
1112 "This variable has to point to the directory containing the fonts\n"
1113 "you can clone from git://gitorious.org/qtwebkit/testfonts.git\n"
1114 "----------------------------------------------------------------------\n"
1118 char currentPath[PATH_MAX+1];
1119 if (!getcwd(currentPath, PATH_MAX))
1120 qFatal("Couldn't get current working directory");
1121 QByteArray configFile = currentPath;
1122 FcConfig *config = FcConfigCreate();
1123 configFile += "/WebKitTools/DumpRenderTree/qt/fonts.conf";
1124 if (!FcConfigParseAndLoad (config, (FcChar8*) configFile.data(), true))
1125 qFatal("Couldn't load font configuration file");
1126 if (!FcConfigAppFontAddDir (config, (FcChar8*) fontDir.data()))
1127 qFatal("Couldn't add font dir!");
1128 FcConfigSetCurrent(config);
1130 appFontSet = FcConfigGetFonts(config, FcSetApplication);
1131 numFonts = appFontSet->nfont;