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