Clean up ChunkedUpdateDrawingAreaProxy
[WebKit-https.git] / WebKitTools / DumpRenderTree / qt / DumpRenderTreeQt.cpp
1 /*
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/
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
19  *
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.
30  */
31
32 #include "config.h"
33
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"
43
44 #include <QApplication>
45 #include <QBuffer>
46 #include <QCryptographicHash>
47 #include <QDir>
48 #include <QFile>
49 #include <QFileInfo>
50 #include <QFocusEvent>
51 #include <QFontDatabase>
52 #include <QLocale>
53 #include <QNetworkAccessManager>
54 #include <QNetworkReply>
55 #include <QNetworkRequest>
56 #include <QPaintDevice>
57 #include <QPaintEngine>
58 #ifndef QT_NO_PRINTER
59 #include <QPrinter>
60 #endif
61 #include <QUndoStack>
62 #include <QUrl>
63
64 #include <qwebsettings.h>
65 #include <qwebsecurityorigin.h>
66
67 #ifndef QT_NO_UITOOLS
68 #include <QtUiTools/QUiLoader>
69 #endif
70
71 #ifdef Q_WS_X11
72 #include <fontconfig/fontconfig.h>
73 #endif
74
75 #include <limits.h>
76 #include <locale.h>
77
78 #ifndef Q_OS_WIN
79 #include <unistd.h>
80 #endif
81
82 #include <qdebug.h>
83
84 namespace WebCore {
85
86 NetworkAccessManager::NetworkAccessManager(QObject* parent)
87     : QNetworkAccessManager(parent)
88 {
89 #ifndef QT_NO_OPENSSL
90     connect(this, SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)),
91             this, SLOT(sslErrorsEncountered(QNetworkReply*, const QList<QSslError>&)));
92 #endif
93 }
94
95 #ifndef QT_NO_OPENSSL
96 void NetworkAccessManager::sslErrorsEncountered(QNetworkReply* reply, const QList<QSslError>& errors)
97 {
98     if (reply->url().host() == "127.0.0.1" || reply->url().host() == "localhost") {
99         bool ignore = true;
100
101         // Accept any HTTPS certificate.
102         foreach (const QSslError& error, errors) {
103             if (error.error() < QSslError::UnableToGetIssuerCertificate || error.error() > QSslError::HostNameMismatch) {
104                 ignore = false;
105                 break;
106             }
107         }
108
109         if (ignore)
110             reply->ignoreSslErrors();
111     }
112 }
113 #endif
114
115
116 #ifndef QT_NO_PRINTER
117 class NullPrinter : public QPrinter {
118 public:
119     class NullPaintEngine : public QPaintEngine {
120     public:
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) { }
126     };
127
128     virtual QPaintEngine* paintEngine() const { return const_cast<NullPaintEngine*>(&m_engine); }
129
130     NullPaintEngine m_engine;
131 };
132 #endif
133
134 WebPage::WebPage(QObject* parent, DumpRenderTree* drt)
135     : QWebPage(parent)
136     , m_webInspector(0)
137     , m_drt(drt)
138 {
139     QWebSettings* globalSettings = QWebSettings::globalSettings();
140
141     globalSettings->setFontSize(QWebSettings::MinimumFontSize, 0);
142     globalSettings->setFontSize(QWebSettings::MinimumLogicalFontSize, 5);
143     globalSettings->setFontSize(QWebSettings::DefaultFontSize, 16);
144     globalSettings->setFontSize(QWebSettings::DefaultFixedFontSize, 13);
145
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);
154
155     connect(this, SIGNAL(geometryChangeRequested(const QRect &)),
156             this, SLOT(setViewGeometry(const QRect & )));
157
158     setNetworkAccessManager(m_drt->networkAccessManager());
159     setPluginFactory(new TestPlugin(this));
160
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)));
163 }
164
165 WebPage::~WebPage()
166 {
167     delete m_webInspector;
168 }
169
170 QWebInspector* WebPage::webInspector()
171 {
172     if (!m_webInspector) {
173         m_webInspector = new QWebInspector;
174         m_webInspector->setPage(this);
175     }
176     return m_webInspector;
177 }
178
179 void WebPage::resetSettings()
180 {
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);
194
195     m_drt->layoutTestController()->setCaretBrowsingEnabled(false);
196     m_drt->layoutTestController()->setFrameFlatteningEnabled(false);
197     m_drt->layoutTestController()->setSmartInsertDeleteEnabled(true);
198     m_drt->layoutTestController()->setSelectTrailingWhitespaceEnabled(false);
199
200     // globalSettings must be reset explicitly.
201     m_drt->layoutTestController()->setXSSAuditorEnabled(false);
202
203     QWebSettings::setMaximumPagesInCache(0); // reset to default
204     settings()->setUserStyleSheetUrl(QUrl()); // reset to default
205
206     m_pendingGeolocationRequests.clear();
207 }
208
209 QWebPage *WebPage::createWindow(QWebPage::WebWindowType)
210 {
211     return m_drt->createWindow();
212 }
213
214 void WebPage::javaScriptAlert(QWebFrame*, const QString& message)
215 {
216     if (!isTextOutputEnabled())
217         return;
218
219     fprintf(stdout, "ALERT: %s\n", message.toUtf8().constData());
220 }
221
222 void WebPage::requestPermission(QWebFrame* frame, QWebPage::Feature feature)
223 {
224     switch (feature) {
225     case Notifications:
226         if (!m_drt->layoutTestController()->ignoreReqestForPermission())
227             setFeaturePermission(frame, feature, PermissionGrantedByUser);
228         break;
229     case Geolocation:
230         if (m_drt->layoutTestController()->isGeolocationPermissionSet())
231             if (m_drt->layoutTestController()->geolocationPermission())
232                 setFeaturePermission(frame, feature, PermissionGrantedByUser);
233             else
234                 setFeaturePermission(frame, feature, PermissionDeniedByUser);
235         else
236             m_pendingGeolocationRequests.append(frame);
237         break;
238     default:
239         break;
240     }
241 }
242
243 void WebPage::cancelPermission(QWebFrame* frame, QWebPage::Feature feature)
244 {
245     switch (feature) {
246     case Geolocation:
247         m_pendingGeolocationRequests.removeOne(frame);
248         break;
249     default:
250         break;
251     }
252 }
253
254 void WebPage::permissionSet(QWebPage::Feature feature)
255 {
256     switch (feature) {
257     case Geolocation:
258         {
259         Q_ASSERT(m_drt->layoutTestController()->isGeolocationPermissionSet());
260         foreach (QWebFrame* frame, m_pendingGeolocationRequests)
261             if (m_drt->layoutTestController()->geolocationPermission())
262                 setFeaturePermission(frame, feature, PermissionGrantedByUser);
263             else
264                 setFeaturePermission(frame, feature, PermissionDeniedByUser);
265
266         m_pendingGeolocationRequests.clear();
267         break;
268         }
269     default:
270         break;
271     }
272 }
273
274 static QString urlSuitableForTestResult(const QString& url)
275 {
276     if (url.isEmpty() || !url.startsWith(QLatin1String("file://")))
277         return url;
278
279     return QFileInfo(url).fileName();
280 }
281
282 void WebPage::javaScriptConsoleMessage(const QString& message, int lineNumber, const QString&)
283 {
284     if (!isTextOutputEnabled())
285         return;
286
287     QString newMessage;
288     if (!message.isEmpty()) {
289         newMessage = message;
290
291         size_t fileProtocol = newMessage.indexOf(QLatin1String("file://"));
292         if (fileProtocol != -1) {
293             newMessage = newMessage.left(fileProtocol) + urlSuitableForTestResult(newMessage.mid(fileProtocol));
294         }
295     }
296
297     fprintf (stdout, "CONSOLE MESSAGE: line %d: %s\n", lineNumber, newMessage.toUtf8().constData());
298 }
299
300 bool WebPage::javaScriptConfirm(QWebFrame*, const QString& msg)
301 {
302     if (!isTextOutputEnabled())
303         return true;
304
305     fprintf(stdout, "CONFIRM: %s\n", msg.toUtf8().constData());
306     return true;
307 }
308
309 bool WebPage::javaScriptPrompt(QWebFrame*, const QString& msg, const QString& defaultValue, QString* result)
310 {
311     if (!isTextOutputEnabled())
312         return true;
313
314     fprintf(stdout, "PROMPT: %s, default text: %s\n", msg.toUtf8().constData(), defaultValue.toUtf8().constData());
315     *result = defaultValue;
316     return true;
317 }
318
319 bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
320 {
321     if (m_drt->layoutTestController()->waitForPolicy()) {
322         QString url = QString::fromUtf8(request.url().toEncoded());
323         QString typeDescription;
324
325         switch (type) {
326         case NavigationTypeLinkClicked:
327             typeDescription = "link clicked";
328             break;
329         case NavigationTypeFormSubmitted:
330             typeDescription = "form submitted";
331             break;
332         case NavigationTypeBackOrForward:
333             typeDescription = "back/forward";
334             break;
335         case NavigationTypeReload:
336             typeDescription = "reload";
337             break;
338         case NavigationTypeFormResubmitted:
339             typeDescription = "form resubmitted";
340             break;
341         case NavigationTypeOther:
342             typeDescription = "other";
343             break;
344         default:
345             typeDescription = "illegal value";
346         }
347
348         if (isTextOutputEnabled())
349             fprintf(stdout, "Policy delegate: attempt to load %s with navigation type '%s'\n",
350                     url.toUtf8().constData(), typeDescription.toUtf8().constData());
351
352         m_drt->layoutTestController()->notifyDone();
353     }
354     return QWebPage::acceptNavigationRequest(frame, request, type);
355 }
356
357 bool WebPage::supportsExtension(QWebPage::Extension extension) const
358 {
359     if (extension == QWebPage::ErrorPageExtension)
360         return m_drt->layoutTestController()->shouldHandleErrorPages();
361
362     return false;
363 }
364
365 bool WebPage::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output)
366 {
367     const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
368
369     // Lets handle error pages for the main frame for now.
370     if (info->frame != mainFrame())
371         return false;
372
373     QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
374
375     errorPage->content = QString("data:text/html,<body/>").toUtf8();
376
377     return true;
378 }
379
380 QObject* WebPage::createPlugin(const QString& classId, const QUrl& url, const QStringList& paramNames, const QStringList& paramValues)
381 {
382     Q_UNUSED(url);
383     Q_UNUSED(paramNames);
384     Q_UNUSED(paramValues);
385 #ifndef QT_NO_UITOOLS
386     QUiLoader loader;
387     return loader.createWidget(classId, view());
388 #else
389     Q_UNUSED(classId);
390     return 0;
391 #endif
392 }
393
394 void WebPage::setViewGeometry(const QRect& rect)
395 {
396     if (WebViewGraphicsBased* v = qobject_cast<WebViewGraphicsBased*>(view()))
397         v->scene()->setSceneRect(QRectF(rect));
398     else if (QWidget *v = view())
399         v->setGeometry(rect);
400 }
401
402 WebViewGraphicsBased::WebViewGraphicsBased(QWidget* parent)
403     : m_item(new QGraphicsWebView)
404 {
405     setScene(new QGraphicsScene(this));
406     scene()->addItem(m_item);
407 }
408
409 DumpRenderTree::DumpRenderTree()
410     : m_dumpPixels(false)
411     , m_stdin(0)
412     , m_enableTextOutput(false)
413     , m_standAloneMode(false)
414     , m_graphicsBased(false)
415     , m_persistentStoragePath(QString(getenv("DUMPRENDERTREE_TEMP")))
416 {
417
418     QByteArray viewMode = getenv("QT_DRT_WEBVIEW_MODE");
419     if (viewMode == "graphics")
420         setGraphicsBased(true);
421
422     DumpRenderTreeSupportQt::overwritePluginDirectories();
423     DumpRenderTreeSupportQt::activeMockDeviceOrientationClient(true);
424     QWebSettings::enablePersistentStorage(m_persistentStoragePath);
425
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);
432         m_mainView = view;
433     } else {
434         QWebView* view = new QWebView(0);
435         m_page = new WebPage(view, this);
436         view->setPage(m_page);
437         m_mainView = view;
438     }
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");
442
443     m_mainView->setContextMenuPolicy(Qt::NoContextMenu);
444     m_mainView->resize(QSize(LayoutTestController::maxViewWidth, LayoutTestController::maxViewHeight));
445
446     // clean up cache by resetting quota.
447     qint64 quota = webPage()->settings()->offlineWebApplicationCacheQuota();
448     webPage()->settings()->setOfflineWebApplicationCacheQuota(quota);
449
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()));
455
456     // async geolocation permission set by controller
457     connect(m_controller, SIGNAL(geolocationPermissionSet()), this, SLOT(geolocationPermissionSet()));
458
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);
464
465     // now connect our different signals
466     connect(m_page, SIGNAL(frameCreated(QWebFrame *)),
467             this, SLOT(connectFrame(QWebFrame *)));
468     connectFrame(m_page->mainFrame());
469
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*)));
478
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(applicationCacheQuotaExceeded(QWebSecurityOrigin *, quint64)),
484             this, SLOT(dumpApplicationCacheQuota(QWebSecurityOrigin *, quint64)));
485     connect(m_page, SIGNAL(statusBarMessage(const QString&)),
486             this, SLOT(statusBarMessage(const QString&)));
487
488     QObject::connect(this, SIGNAL(quit()), qApp, SLOT(quit()), Qt::QueuedConnection);
489
490     DumpRenderTreeSupportQt::setDumpRenderTreeModeEnabled(true);
491     QFocusEvent event(QEvent::FocusIn, Qt::ActiveWindowFocusReason);
492     QApplication::sendEvent(m_mainView, &event);
493 }
494
495 DumpRenderTree::~DumpRenderTree()
496 {
497     delete m_mainView;
498     delete m_stdin;
499     DumpRenderTreeSupportQt::removeMockDeviceOrientation();
500 }
501
502 static void clearHistory(QWebPage* page)
503 {
504     // QWebHistory::clear() leaves current page, so remove it as well by setting
505     // max item count to 0, and then setting it back to it's original value.
506
507     QWebHistory* history = page->history();
508     int itemCount = history->maximumItemCount();
509
510     history->clear();
511     history->setMaximumItemCount(0);
512     history->setMaximumItemCount(itemCount);
513 }
514
515 void DumpRenderTree::dryRunPrint(QWebFrame* frame)
516 {
517 #ifndef QT_NO_PRINTER
518     NullPrinter printer;
519     frame->print(&printer);
520 #endif
521 }
522
523 void DumpRenderTree::resetToConsistentStateBeforeTesting()
524 {
525     // reset so that any current loads are stopped
526     // NOTE: that this has to be done before the layoutTestController is
527     // reset or we get timeouts for some tests.
528     m_page->blockSignals(true);
529     m_page->triggerAction(QWebPage::Stop);
530     m_page->blockSignals(false);
531
532     // reset the layoutTestController at this point, so that we under no
533     // circumstance dump (stop the waitUntilDone timer) during the reset
534     // of the DRT.
535     m_controller->reset();
536
537     // reset mouse clicks counter
538     m_eventSender->resetClickCount();
539
540     closeRemainingWindows();
541
542     m_page->resetSettings();
543     m_page->undoStack()->clear();
544     m_page->mainFrame()->setZoomFactor(1.0);
545     clearHistory(m_page);
546     DumpRenderTreeSupportQt::clearFrameName(m_page->mainFrame());
547
548     m_page->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded);
549     m_page->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded);
550
551     WorkQueue::shared()->clear();
552     WorkQueue::shared()->setFrozen(false);
553
554     DumpRenderTreeSupportQt::resetOriginAccessWhiteLists();
555
556     // Qt defaults to Windows editing behavior.
557     DumpRenderTreeSupportQt::setEditingBehavior(m_page, "win");
558
559     QLocale::setDefault(QLocale::c());
560
561 #ifndef Q_OS_WINCE
562     setlocale(LC_ALL, "");
563 #endif
564 }
565
566 static bool isGlobalHistoryTest(const QUrl& url)
567 {
568     if (url.path().contains("globalhistory/"))
569         return true;
570     return false;
571 }
572
573 static bool isWebInspectorTest(const QUrl& url)
574 {
575     if (url.path().contains("inspector/"))
576         return true;
577     return false;
578 }
579
580 static bool shouldEnableDeveloperExtras(const QUrl& url)
581 {
582     return true;
583 }
584
585 void DumpRenderTree::open(const QUrl& url)
586 {
587     DumpRenderTreeSupportQt::dumpResourceLoadCallbacksPath(QFileInfo(url.toString()).path());
588     resetToConsistentStateBeforeTesting();
589
590     if (shouldEnableDeveloperExtras(m_page->mainFrame()->url())) {
591         layoutTestController()->closeWebInspector();
592         layoutTestController()->setDeveloperExtrasEnabled(false);
593     }
594
595     if (shouldEnableDeveloperExtras(url)) {
596         layoutTestController()->setDeveloperExtrasEnabled(true);
597         if (isWebInspectorTest(url))
598             layoutTestController()->showWebInspector();
599     }
600
601     if (isGlobalHistoryTest(url))
602         layoutTestController()->dumpHistoryCallbacks();
603
604     // W3C SVG tests expect to be 480x360
605     bool isW3CTest = url.toString().contains("svg/W3C-SVG-1.1");
606     int width = isW3CTest ? 480 : LayoutTestController::maxViewWidth;
607     int height = isW3CTest ? 360 : LayoutTestController::maxViewHeight;
608     m_mainView->resize(QSize(width, height));
609     m_page->setPreferredContentsSize(QSize());
610     m_page->setViewportSize(QSize(width, height));
611
612     QFocusEvent ev(QEvent::FocusIn);
613     m_page->event(&ev);
614
615     QWebSettings::clearMemoryCaches();
616 #if !(defined(Q_OS_SYMBIAN) && QT_VERSION <= QT_VERSION_CHECK(4, 6, 2))
617     QFontDatabase::removeAllApplicationFonts();
618 #endif
619 #if defined(Q_WS_X11)
620     initializeFonts();
621 #endif
622
623     DumpRenderTreeSupportQt::dumpFrameLoader(url.toString().contains("loading/"));
624     setTextOutputEnabled(true);
625     m_page->mainFrame()->load(url);
626 }
627
628 void DumpRenderTree::readLine()
629 {
630     if (!m_stdin) {
631         m_stdin = new QFile;
632         m_stdin->open(stdin, QFile::ReadOnly);
633
634         if (!m_stdin->isReadable()) {
635             emit quit();
636             return;
637         }
638     }
639
640     QByteArray line = m_stdin->readLine().trimmed();
641
642     if (line.isEmpty()) {
643         emit quit();
644         return;
645     }
646
647     processLine(QString::fromLocal8Bit(line.constData(), line.length()));
648 }
649
650 void DumpRenderTree::processArgsLine(const QStringList &args)
651 {
652     setStandAloneMode(true);
653
654     for (int i = 1; i < args.size(); ++i)
655         if (!args.at(i).startsWith('-'))
656             m_standAloneModeTestList.append(args[i]);
657
658     QFileInfo firstEntry(m_standAloneModeTestList.first());
659     if (firstEntry.isDir()) {
660         QDir folderEntry(m_standAloneModeTestList.first());
661         QStringList supportedExt;
662         // Check for all supported extensions (from Scripts/webkitpy/layout_tests/layout_package/test_files.py).
663         supportedExt << "*.html" << "*.shtml" << "*.xml" << "*.xhtml" << "*.xhtmlmp" << "*.pl" << "*.php" << "*.svg";
664         m_standAloneModeTestList = folderEntry.entryList(supportedExt, QDir::Files);
665         for (int i = 0; i < m_standAloneModeTestList.size(); ++i)
666             m_standAloneModeTestList[i] = folderEntry.absoluteFilePath(m_standAloneModeTestList[i]);
667     }
668
669     processLine(m_standAloneModeTestList.first());
670     m_standAloneModeTestList.removeFirst();
671
672     connect(this, SIGNAL(ready()), this, SLOT(loadNextTestInStandAloneMode()));
673 }
674
675 void DumpRenderTree::loadNextTestInStandAloneMode()
676 {
677     if (m_standAloneModeTestList.isEmpty()) {
678         emit quit();
679         return;
680     }
681
682     processLine(m_standAloneModeTestList.first());
683     m_standAloneModeTestList.removeFirst();
684 }
685
686 void DumpRenderTree::processLine(const QString &input)
687 {
688     QString line = input;
689
690     m_expectedHash = QString();
691     if (m_dumpPixels) {
692         // single quote marks the pixel dump hash
693         int i = line.indexOf('\'');
694         if (i > -1) {
695             m_expectedHash = line.mid(i + 1, line.length());
696             line.remove(i, line.length());
697         }
698     }
699
700     if (line.startsWith(QLatin1String("http:"))
701             || line.startsWith(QLatin1String("https:"))
702             || line.startsWith(QLatin1String("file:"))) {
703         open(QUrl(line));
704     } else {
705         QFileInfo fi(line);
706
707         if (!fi.exists()) {
708             QDir currentDir = QDir::currentPath();
709
710             // Try to be smart about where the test is located
711             if (currentDir.dirName() == QLatin1String("LayoutTests"))
712                 fi = QFileInfo(currentDir, line.replace(QRegExp(".*?LayoutTests/(.*)"), "\\1"));
713             else if (!line.contains(QLatin1String("LayoutTests")))
714                 fi = QFileInfo(currentDir, line.prepend(QLatin1String("LayoutTests/")));
715
716             if (!fi.exists()) {
717                 emit ready();
718                 return;
719             }
720         }
721
722         open(QUrl::fromLocalFile(fi.absoluteFilePath()));
723     }
724
725     fflush(stdout);
726 }
727
728 void DumpRenderTree::setDumpPixels(bool dump)
729 {
730     m_dumpPixels = dump;
731 }
732
733 void DumpRenderTree::closeRemainingWindows()
734 {
735     foreach (QObject* widget, windows)
736         delete widget;
737     windows.clear();
738 }
739
740 void DumpRenderTree::initJSObjects()
741 {
742     QWebFrame *frame = qobject_cast<QWebFrame*>(sender());
743     Q_ASSERT(frame);
744     frame->addToJavaScriptWindowObject(QLatin1String("layoutTestController"), m_controller);
745     frame->addToJavaScriptWindowObject(QLatin1String("eventSender"), m_eventSender);
746     frame->addToJavaScriptWindowObject(QLatin1String("textInputController"), m_textInputController);
747     frame->addToJavaScriptWindowObject(QLatin1String("GCController"), m_gcController);
748     frame->addToJavaScriptWindowObject(QLatin1String("plainText"), m_plainTextController);
749 }
750
751 void DumpRenderTree::showPage()
752 {
753     m_mainView->show();
754     // we need a paint event but cannot process all the events
755     QPixmap pixmap(m_mainView->size());
756     m_mainView->render(&pixmap);
757 }
758
759 void DumpRenderTree::hidePage()
760 {
761     m_mainView->hide();
762 }
763
764 QString DumpRenderTree::dumpFrameScrollPosition(QWebFrame* frame)
765 {
766     if (!frame || !DumpRenderTreeSupportQt::hasDocumentElement(frame))
767         return QString();
768
769     QString result;
770     QPoint pos = frame->scrollPosition();
771     if (pos.x() > 0 || pos.y() > 0) {
772         QWebFrame* parent = qobject_cast<QWebFrame *>(frame->parent());
773         if (parent)
774             result.append(QString("frame '%1' ").arg(frame->title()));
775         result.append(QString("scrolled to %1,%2\n").arg(pos.x()).arg(pos.y()));
776     }
777
778     if (m_controller->shouldDumpChildFrameScrollPositions()) {
779         QList<QWebFrame*> children = frame->childFrames();
780         for (int i = 0; i < children.size(); ++i)
781             result += dumpFrameScrollPosition(children.at(i));
782     }
783     return result;
784 }
785
786 QString DumpRenderTree::dumpFramesAsText(QWebFrame* frame)
787 {
788     if (!frame || !DumpRenderTreeSupportQt::hasDocumentElement(frame))
789         return QString();
790
791     QString result;
792     QWebFrame* parent = qobject_cast<QWebFrame*>(frame->parent());
793     if (parent) {
794         result.append(QLatin1String("\n--------\nFrame: '"));
795         result.append(frame->frameName());
796         result.append(QLatin1String("'\n--------\n"));
797     }
798
799     QString innerText = frame->toPlainText();
800     result.append(innerText);
801     result.append(QLatin1String("\n"));
802
803     if (m_controller->shouldDumpChildrenAsText()) {
804         QList<QWebFrame *> children = frame->childFrames();
805         for (int i = 0; i < children.size(); ++i)
806             result += dumpFramesAsText(children.at(i));
807     }
808
809     return result;
810 }
811
812 static QString dumpHistoryItem(const QWebHistoryItem& item, int indent, bool current)
813 {
814     QString result;
815
816     int start = 0;
817     if (current) {
818         result.append(QLatin1String("curr->"));
819         start = 6;
820     }
821     for (int i = start; i < indent; i++)
822         result.append(' ');
823
824     QString url = item.url().toEncoded();
825     if (url.contains("file://")) {
826         static QString layoutTestsString("/LayoutTests/");
827         static QString fileTestString("(file test):");
828
829         QString res = url.mid(url.indexOf(layoutTestsString) + layoutTestsString.length());
830         if (res.isEmpty())
831             return result;
832
833         result.append(fileTestString);
834         result.append(res);
835     } else {
836         result.append(url);
837     }
838
839     QString target = DumpRenderTreeSupportQt::historyItemTarget(item);
840     if (!target.isEmpty())
841         result.append(QString(QLatin1String(" (in frame \"%1\")")).arg(target));
842
843     if (DumpRenderTreeSupportQt::isTargetItem(item))
844         result.append(QLatin1String("  **nav target**"));
845     result.append(QLatin1String("\n"));
846
847     QMap<QString, QWebHistoryItem> children = DumpRenderTreeSupportQt::getChildHistoryItems(item);
848     foreach (QWebHistoryItem item, children)
849         result += dumpHistoryItem(item, 12, false);
850
851     return result;
852 }
853
854 QString DumpRenderTree::dumpBackForwardList(QWebPage* page)
855 {
856     QWebHistory* history = page->history();
857
858     QString result;
859     result.append(QLatin1String("\n============== Back Forward List ==============\n"));
860
861     // FORMAT:
862     // "        (file test):fast/loader/resources/click-fragment-link.html  **nav target**"
863     // "curr->  (file test):fast/loader/resources/click-fragment-link.html#testfragment  **nav target**"
864
865     int maxItems = history->maximumItemCount();
866
867     foreach (const QWebHistoryItem item, history->backItems(maxItems)) {
868         if (!item.isValid())
869             continue;
870         result.append(dumpHistoryItem(item, 8, false));
871     }
872
873     QWebHistoryItem item = history->currentItem();
874     if (item.isValid())
875         result.append(dumpHistoryItem(item, 8, true));
876
877     foreach (const QWebHistoryItem item, history->forwardItems(maxItems)) {
878         if (!item.isValid())
879             continue;
880         result.append(dumpHistoryItem(item, 8, false));
881     }
882
883     result.append(QLatin1String("===============================================\n"));
884     return result;
885 }
886
887 static const char *methodNameStringForFailedTest(LayoutTestController *controller)
888 {
889     const char *errorMessage;
890     if (controller->shouldDumpAsText())
891         errorMessage = "[documentElement innerText]";
892     // FIXME: Add when we have support
893     //else if (controller->dumpDOMAsWebArchive())
894     //    errorMessage = "[[mainFrame DOMDocument] webArchive]";
895     //else if (controller->dumpSourceAsWebArchive())
896     //    errorMessage = "[[mainFrame dataSource] webArchive]";
897     else
898         errorMessage = "[mainFrame renderTreeAsExternalRepresentation]";
899
900     return errorMessage;
901 }
902
903 void DumpRenderTree::dump()
904 {
905     // Prevent any further frame load or resource load callbacks from appearing after we dump the result.
906     DumpRenderTreeSupportQt::dumpFrameLoader(false);
907     DumpRenderTreeSupportQt::dumpResourceLoadCallbacks(false);
908
909     QWebFrame *mainFrame = m_page->mainFrame();
910
911     if (isStandAloneMode()) {
912         QString markup = mainFrame->toHtml();
913         fprintf(stdout, "Source:\n\n%s\n", markup.toUtf8().constData());
914     }
915
916     // Dump render text...
917     QString resultString;
918     if (m_controller->shouldDumpAsText())
919         resultString = dumpFramesAsText(mainFrame);
920     else {
921         resultString = mainFrame->renderTreeDump();
922         resultString += dumpFrameScrollPosition(mainFrame);
923     }
924     if (!resultString.isEmpty()) {
925         fprintf(stdout, "Content-Type: text/plain\n");
926         fprintf(stdout, "%s", resultString.toUtf8().constData());
927
928         if (m_controller->shouldDumpBackForwardList()) {
929             fprintf(stdout, "%s", dumpBackForwardList(webPage()).toUtf8().constData());
930             foreach (QObject* widget, windows) {
931                 QWebPage* page = qobject_cast<QWebPage*>(widget->findChild<QWebPage*>());
932                 fprintf(stdout, "%s", dumpBackForwardList(page).toUtf8().constData());
933             }
934         }
935
936     } else
937         printf("ERROR: nil result from %s", methodNameStringForFailedTest(m_controller));
938
939     // signal end of text block
940     fputs("#EOF\n", stdout);
941     fputs("#EOF\n", stderr);
942
943     // FIXME: All other ports don't dump pixels, if generatePixelResults is false.
944     if (m_dumpPixels) {
945         QImage image(m_page->viewportSize(), QImage::Format_ARGB32);
946         image.fill(Qt::white);
947         QPainter painter(&image);
948         mainFrame->render(&painter);
949         painter.end();
950
951         QCryptographicHash hash(QCryptographicHash::Md5);
952         for (int row = 0; row < image.height(); ++row)
953             hash.addData(reinterpret_cast<const char*>(image.scanLine(row)), image.width() * 4);
954         QString actualHash = hash.result().toHex();
955
956         fprintf(stdout, "\nActualHash: %s\n", qPrintable(actualHash));
957
958         bool dumpImage = true;
959
960         if (!m_expectedHash.isEmpty()) {
961             Q_ASSERT(m_expectedHash.length() == 32);
962             fprintf(stdout, "\nExpectedHash: %s\n", qPrintable(m_expectedHash));
963
964             if (m_expectedHash == actualHash)
965                 dumpImage = false;
966         }
967
968         if (dumpImage) {
969             QBuffer buffer;
970             buffer.open(QBuffer::WriteOnly);
971             image.save(&buffer, "PNG");
972             buffer.close();
973             const QByteArray &data = buffer.data();
974
975             printf("Content-Type: %s\n", "image/png");
976             printf("Content-Length: %lu\n", static_cast<unsigned long>(data.length()));
977
978             const quint32 bytesToWriteInOneChunk = 1 << 15;
979             quint32 dataRemainingToWrite = data.length();
980             const char *ptr = data.data();
981             while (dataRemainingToWrite) {
982                 quint32 bytesToWriteInThisChunk = qMin(dataRemainingToWrite, bytesToWriteInOneChunk);
983                 quint32 bytesWritten = fwrite(ptr, 1, bytesToWriteInThisChunk, stdout);
984                 if (bytesWritten != bytesToWriteInThisChunk)
985                     break;
986                 dataRemainingToWrite -= bytesWritten;
987                 ptr += bytesWritten;
988             }
989         }
990
991         fflush(stdout);
992     }
993
994     puts("#EOF");   // terminate the (possibly empty) pixels block
995
996     fflush(stdout);
997     fflush(stderr);
998
999      emit ready();
1000 }
1001
1002 void DumpRenderTree::titleChanged(const QString &s)
1003 {
1004     if (m_controller->shouldDumpTitleChanges())
1005         printf("TITLE CHANGED: %s\n", s.toUtf8().data());
1006 }
1007
1008 void DumpRenderTree::connectFrame(QWebFrame *frame)
1009 {
1010     connect(frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(initJSObjects()));
1011     connect(frame, SIGNAL(provisionalLoad()),
1012             layoutTestController(), SLOT(provisionalLoad()));
1013 }
1014
1015 void DumpRenderTree::dumpDatabaseQuota(QWebFrame* frame, const QString& dbName)
1016 {
1017     if (!m_controller->shouldDumpDatabaseCallbacks())
1018         return;
1019     QWebSecurityOrigin origin = frame->securityOrigin();
1020     printf("UI DELEGATE DATABASE CALLBACK: exceededDatabaseQuotaForSecurityOrigin:{%s, %s, %i} database:%s\n",
1021            origin.scheme().toUtf8().data(),
1022            origin.host().toUtf8().data(),
1023            origin.port(),
1024            dbName.toUtf8().data());
1025     origin.setDatabaseQuota(5 * 1024 * 1024);
1026 }
1027
1028 void DumpRenderTree::dumpApplicationCacheQuota(QWebSecurityOrigin* origin, quint64 defaultOriginQuota)
1029 {
1030     if (!m_controller->shouldDumpApplicationCacheDelegateCallbacks())
1031         return;
1032
1033     printf("UI DELEGATE APPLICATION CACHE CALLBACK: exceededApplicationCacheOriginQuotaForSecurityOrigin:{%s, %s, %i}\n",
1034            origin->scheme().toUtf8().data(),
1035            origin->host().toUtf8().data(),
1036            origin->port()
1037            );
1038     origin->setApplicationCacheQuota(defaultOriginQuota);
1039 }
1040
1041 void DumpRenderTree::statusBarMessage(const QString& message)
1042 {
1043     if (!m_controller->shouldDumpStatusCallbacks())
1044         return;
1045
1046     printf("UI DELEGATE STATUS CALLBACK: setStatusText:%s\n", message.toUtf8().constData());
1047 }
1048
1049 QWebPage *DumpRenderTree::createWindow()
1050 {
1051     if (!m_controller->canOpenWindows())
1052         return 0;
1053
1054     // Create a dummy container object to track the page in DRT.
1055     // QObject is used instead of QWidget to prevent DRT from
1056     // showing the main view when deleting the container.
1057
1058     QObject* container = new QObject(m_mainView);
1059     // create a QWebPage we want to return
1060     QWebPage* page = static_cast<QWebPage*>(new WebPage(container, this));
1061     // gets cleaned up in closeRemainingWindows()
1062     windows.append(container);
1063
1064     // connect the needed signals to the page
1065     connect(page, SIGNAL(frameCreated(QWebFrame*)), this, SLOT(connectFrame(QWebFrame*)));
1066     connectFrame(page->mainFrame());
1067     connect(page, SIGNAL(loadFinished(bool)), m_controller, SLOT(maybeDump(bool)));
1068     connect(page, SIGNAL(windowCloseRequested()), this, SLOT(windowCloseRequested()));
1069
1070     // Use a frame group name for all pages created by DumpRenderTree to allow
1071     // testing of cross-page frame lookup.
1072     DumpRenderTreeSupportQt::webPageSetGroupName(page, "org.webkit.qt.DumpRenderTree");
1073
1074     return page;
1075 }
1076
1077 void DumpRenderTree::windowCloseRequested()
1078 {
1079     QWebPage* page = qobject_cast<QWebPage*>(sender());
1080     QObject* container = page->parent();
1081     windows.removeAll(container);
1082     // Our use of container->deleteLater() means we need to remove closed pages
1083     // from the org.webkit.qt.DumpRenderTree group explicitly.
1084     DumpRenderTreeSupportQt::webPageSetGroupName(page, "");
1085     container->deleteLater();
1086 }
1087
1088 int DumpRenderTree::windowCount() const
1089 {
1090 // include the main view in the count
1091     return windows.count() + 1;
1092 }
1093
1094 void DumpRenderTree::geolocationPermissionSet() 
1095 {
1096     m_page->permissionSet(QWebPage::Geolocation);
1097 }
1098
1099 void DumpRenderTree::switchFocus(bool focused)
1100 {
1101     QFocusEvent event((focused) ? QEvent::FocusIn : QEvent::FocusOut, Qt::ActiveWindowFocusReason);
1102     if (!isGraphicsBased())
1103         QApplication::sendEvent(m_mainView, &event);
1104     else {
1105         if (WebViewGraphicsBased* view = qobject_cast<WebViewGraphicsBased*>(m_mainView))
1106             view->scene()->sendEvent(view->graphicsView(), &event);
1107     }
1108
1109 }
1110
1111 #if defined(Q_WS_X11)
1112 void DumpRenderTree::initializeFonts()
1113 {
1114     static int numFonts = -1;
1115
1116     // Some test cases may add or remove application fonts (via @font-face).
1117     // Make sure to re-initialize the font set if necessary.
1118     FcFontSet* appFontSet = FcConfigGetFonts(0, FcSetApplication);
1119     if (appFontSet && numFonts >= 0 && appFontSet->nfont == numFonts)
1120         return;
1121
1122     QByteArray fontDir = getenv("WEBKIT_TESTFONTS");
1123     if (fontDir.isEmpty() || !QDir(fontDir).exists()) {
1124         fprintf(stderr,
1125                 "\n\n"
1126                 "----------------------------------------------------------------------\n"
1127                 "WEBKIT_TESTFONTS environment variable is not set correctly.\n"
1128                 "This variable has to point to the directory containing the fonts\n"
1129                 "you can clone from git://gitorious.org/qtwebkit/testfonts.git\n"
1130                 "----------------------------------------------------------------------\n"
1131                );
1132         exit(1);
1133     }
1134     char currentPath[PATH_MAX+1];
1135     if (!getcwd(currentPath, PATH_MAX))
1136         qFatal("Couldn't get current working directory");
1137     QByteArray configFile = currentPath;
1138     FcConfig *config = FcConfigCreate();
1139     configFile += "/WebKitTools/DumpRenderTree/qt/fonts.conf";
1140     if (!FcConfigParseAndLoad (config, (FcChar8*) configFile.data(), true))
1141         qFatal("Couldn't load font configuration file");
1142     if (!FcConfigAppFontAddDir (config, (FcChar8*) fontDir.data()))
1143         qFatal("Couldn't add font dir!");
1144     FcConfigSetCurrent(config);
1145
1146     appFontSet = FcConfigGetFonts(config, FcSetApplication);
1147     numFonts = appFontSet->nfont;
1148 }
1149 #endif
1150
1151 }