diff options
Diffstat (limited to 'WebKit/qt/tests/qwebframe/tst_qwebframe.cpp')
| -rw-r--r-- | WebKit/qt/tests/qwebframe/tst_qwebframe.cpp | 234 |
1 files changed, 165 insertions, 69 deletions
diff --git a/WebKit/qt/tests/qwebframe/tst_qwebframe.cpp b/WebKit/qt/tests/qwebframe/tst_qwebframe.cpp index 17dea99..7c13fd0 100644 --- a/WebKit/qt/tests/qwebframe/tst_qwebframe.cpp +++ b/WebKit/qt/tests/qwebframe/tst_qwebframe.cpp @@ -33,37 +33,14 @@ #include <QRegExp> #include <QNetworkRequest> #include <QNetworkReply> +#ifndef QT_NO_OPENSSL #include <qsslerror.h> +#endif +#include "../util.h" -//TESTED_CLASS= -//TESTED_FILES= - -// Task 160192 -/** - * Starts an event loop that runs until the given signal is received. - Optionally the event loop - * can return earlier on a timeout. - * - * \return \p true if the requested signal was received - * \p false on timeout - */ -static bool waitForSignal(QObject* obj, const char* signal, int timeout = 0) -{ - QEventLoop loop; - QObject::connect(obj, signal, &loop, SLOT(quit())); - QTimer timer; - QSignalSpy timeoutSpy(&timer, SIGNAL(timeout())); - if (timeout > 0) { - QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); - timer.setSingleShot(true); - timer.start(timeout); - } - loop.exec(); - return timeoutSpy.isEmpty(); -} - -/* Mostly a test for the JavaScript related parts of QWebFrame */ - +#if defined(Q_OS_SYMBIAN) +# define SRCDIR "" +#endif struct CustomType { QString string; @@ -578,8 +555,12 @@ private slots: void urlChange(); void domCycles(); void requestedUrl(); + void javaScriptWindowObjectCleared_data(); + void javaScriptWindowObjectCleared(); + void javaScriptWindowObjectClearedOnEvaluate(); void setHtml(); void setHtmlWithResource(); + void setHtmlWithBaseURL(); void ipv6HostEncoding(); void metaData(); void popupFocus(); @@ -592,6 +573,8 @@ private slots: void hasSetFocus(); void render(); void scrollPosition(); + void evaluateWillCauseRepaint(); + void qObjectWrapperWithSameIdentity(); private: QString evalJS(const QString&s) { @@ -692,6 +675,7 @@ void tst_QWebFrame::init() m_page = m_view->page(); m_myObject = new MyQObject(); m_page->mainFrame()->addToJavaScriptWindowObject("myObject", m_myObject); + QDir::setCurrent(SRCDIR); } void tst_QWebFrame::cleanup() @@ -2178,8 +2162,11 @@ public: if (request.url() == QUrl("qrc:/test1.html")) { setHeader(QNetworkRequest::LocationHeader, QString("qrc:/test2.html")); setAttribute(QNetworkRequest::RedirectionTargetAttribute, QUrl("qrc:/test2.html")); - } else if (request.url() == QUrl("qrc:/fake-ssl-error.html")) + } +#ifndef QT_NO_OPENSSL + else if (request.url() == QUrl("qrc:/fake-ssl-error.html")) setError(QNetworkReply::SslHandshakeFailedError, tr("Fake error !")); // force a ssl error +#endif else if (request.url() == QUrl("http://abcdef.abcdef/")) setError(QNetworkReply::HostNotFoundError, tr("Invalid URL")); @@ -2206,8 +2193,10 @@ private slots: emit error(this->error()); else if (request().url() == QUrl("http://abcdef.abcdef/")) emit metaDataChanged(); +#ifndef QT_NO_OPENSSL else if (request().url() == QUrl("qrc:/fake-ssl-error.html")) return; +#endif emit readyRead(); emit finished(); @@ -2224,15 +2213,18 @@ protected: virtual QNetworkReply* createRequest(Operation op, const QNetworkRequest& request, QIODevice* outgoingData) { QString url = request.url().toString(); - if (op == QNetworkAccessManager::GetOperation) + if (op == QNetworkAccessManager::GetOperation) { if (url == "qrc:/test1.html" || url == "http://abcdef.abcdef/") return new FakeReply(request, this); +#ifndef QT_NO_OPENSSL else if (url == "qrc:/fake-ssl-error.html") { FakeReply* reply = new FakeReply(request, this); QList<QSslError> errors; emit sslErrors(reply, errors << QSslError(QSslError::UnspecifiedError)); return reply; } +#endif + } return QNetworkAccessManager::createRequest(op, request, outgoingData); } @@ -2249,32 +2241,68 @@ void tst_QWebFrame::requestedUrl() page.setNetworkAccessManager(networkManager); frame->setUrl(QUrl("qrc:/test1.html")); - QTest::qWait(200); + waitForSignal(frame, SIGNAL(loadFinished(bool)), 200); QCOMPARE(spy.count(), 1); QCOMPARE(frame->requestedUrl(), QUrl("qrc:/test1.html")); QCOMPARE(frame->url(), QUrl("qrc:/test2.html")); frame->setUrl(QUrl("qrc:/non-existent.html")); - QTest::qWait(200); + waitForSignal(frame, SIGNAL(loadFinished(bool)), 200); QCOMPARE(spy.count(), 2); QCOMPARE(frame->requestedUrl(), QUrl("qrc:/non-existent.html")); QCOMPARE(frame->url(), QUrl("qrc:/non-existent.html")); frame->setUrl(QUrl("http://abcdef.abcdef")); - QTest::qWait(200); + waitForSignal(frame, SIGNAL(loadFinished(bool)), 200); QCOMPARE(spy.count(), 3); QCOMPARE(frame->requestedUrl(), QUrl("http://abcdef.abcdef/")); QCOMPARE(frame->url(), QUrl("http://abcdef.abcdef/")); +#ifndef QT_NO_OPENSSL qRegisterMetaType<QList<QSslError> >("QList<QSslError>"); qRegisterMetaType<QNetworkReply* >("QNetworkReply*"); - QSignalSpy spy2(page.networkAccessManager(), SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&))); + QSignalSpy spy2(page.networkAccessManager(), SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>))); frame->setUrl(QUrl("qrc:/fake-ssl-error.html")); - QTest::qWait(200); + waitForSignal(frame, SIGNAL(loadFinished(bool)), 200); QCOMPARE(spy2.count(), 1); QCOMPARE(frame->requestedUrl(), QUrl("qrc:/fake-ssl-error.html")); QCOMPARE(frame->url(), QUrl("qrc:/fake-ssl-error.html")); +#endif +} + +void tst_QWebFrame::javaScriptWindowObjectCleared_data() +{ + QTest::addColumn<QString>("html"); + QTest::addColumn<int>("signalCount"); + QTest::newRow("with <script>") << "<html><body><script></script><p>hello world</p></body></html>" << 1; + QTest::newRow("without <script>") << "<html><body><p>hello world</p></body></html>" << 0; +} + +void tst_QWebFrame::javaScriptWindowObjectCleared() +{ + QWebPage page; + QWebFrame* frame = page.mainFrame(); + QSignalSpy spy(frame, SIGNAL(javaScriptWindowObjectCleared())); + QFETCH(QString, html); + frame->setHtml(html); + + QFETCH(int, signalCount); + QCOMPARE(spy.count(), signalCount); +} + +void tst_QWebFrame::javaScriptWindowObjectClearedOnEvaluate() +{ + QWebPage page; + QWebFrame* frame = page.mainFrame(); + QSignalSpy spy(frame, SIGNAL(javaScriptWindowObjectCleared())); + frame->setHtml("<html></html>"); + QCOMPARE(spy.count(), 0); + frame->evaluateJavaScript("var a = 'a';"); + QCOMPARE(spy.count(), 1); + // no new clear for a new script: + frame->evaluateJavaScript("var a = 1;"); + QCOMPARE(spy.count(), 1); } void tst_QWebFrame::setHtml() @@ -2294,7 +2322,7 @@ void tst_QWebFrame::setHtmlWithResource() // in few seconds, the image should be completey loaded QSignalSpy spy(&page, SIGNAL(loadFinished(bool))); frame->setHtml(html); - QTest::qWait(200); + waitForSignal(frame, SIGNAL(loadFinished(bool)), 200); QCOMPARE(spy.count(), 1); QCOMPARE(frame->evaluateJavaScript("document.images.length").toInt(), 1); @@ -2313,11 +2341,33 @@ void tst_QWebFrame::setHtmlWithResource() // in few seconds, the CSS should be completey loaded frame->setHtml(html2); - QTest::qWait(200); + waitForSignal(frame, SIGNAL(loadFinished(bool)), 200); QCOMPARE(spy.size(), 2); QWebElement p = frame->documentElement().findAll("p").at(0); - QCOMPARE(p.styleProperty("color", QWebElement::RespectCascadingStyles), QLatin1String("red")); + QCOMPARE(p.styleProperty("color", QWebElement::CascadedStyle), QLatin1String("red")); +} + +void tst_QWebFrame::setHtmlWithBaseURL() +{ + QString html("<html><body><p>hello world</p><img src='resources/image2.png'/></body></html>"); + + QWebPage page; + QWebFrame* frame = page.mainFrame(); + + // in few seconds, the image should be completey loaded + QSignalSpy spy(&page, SIGNAL(loadFinished(bool))); + + frame->setHtml(html, QUrl::fromLocalFile(QDir::currentPath())); + waitForSignal(frame, SIGNAL(loadFinished(bool)), 200); + QCOMPARE(spy.count(), 1); + + QCOMPARE(frame->evaluateJavaScript("document.images.length").toInt(), 1); + QCOMPARE(frame->evaluateJavaScript("document.images[0].width").toInt(), 128); + QCOMPARE(frame->evaluateJavaScript("document.images[0].height").toInt(), 128); + + // no history item has to be added. + QCOMPARE(m_view->page()->history()->count(), 0); } class TestNetworkManager : public QNetworkAccessManager @@ -2408,33 +2458,27 @@ void tst_QWebFrame::popupFocus() view.resize(400, 100); view.show(); view.setFocus(); - QTest::qWait(200); - QVERIFY2(view.hasFocus(), - "The WebView should be created"); + QTRY_VERIFY(view.hasFocus()); // open the popup by clicking. check if focus is on the popup QTest::mouseClick(&view, Qt::LeftButton, 0, QPoint(25, 25)); QObject* webpopup = firstChildByClassName(&view, "WebCore::QWebPopup"); QComboBox* combo = qobject_cast<QComboBox*>(webpopup); - QTest::qWait(500); - QVERIFY2(!view.hasFocus() && combo->view()->hasFocus(), - "Focus sould be on the Popup"); + QTRY_VERIFY(!view.hasFocus() && combo->view()->hasFocus()); // Focus should be on the popup // hide the popup and check if focus is on the page combo->hidePopup(); - QTest::qWait(500); - QVERIFY2(view.hasFocus() && !combo->view()->hasFocus(), - "Focus sould be back on the WebView"); + QTRY_VERIFY(view.hasFocus() && !combo->view()->hasFocus()); // Focus should be back on the WebView - // triple the flashing time, should at least blink twice already - int delay = qApp->cursorFlashTime() * 3; + // double the flashing time, should at least blink once already + int delay = qApp->cursorFlashTime() * 2; // focus the lineedit and check if it blinks QTest::mouseClick(&view, Qt::LeftButton, 0, QPoint(200, 25)); m_popupTestView = &view; view.installEventFilter( this ); QTest::qWait(delay); - QVERIFY2(m_popupTestPaintCount >= 4, + QVERIFY2(m_popupTestPaintCount >= 3, "The input field should have a blinking caret"); } @@ -2578,7 +2622,7 @@ void tst_QWebFrame::hasSetFocus() QSignalSpy loadSpy(m_page, SIGNAL(loadFinished(bool))); m_page->mainFrame()->setHtml(html); - QTest::qWait(200); + waitForSignal(m_page->mainFrame(), SIGNAL(loadFinished(bool)), 200); QCOMPARE(loadSpy.size(), 1); QList<QWebFrame*> children = m_page->mainFrame()->childFrames(); @@ -2588,20 +2632,20 @@ void tst_QWebFrame::hasSetFocus() "</body></html>"); frame->setHtml(innerHtml); - QTest::qWait(200); + waitForSignal(frame, SIGNAL(loadFinished(bool)), 200); QCOMPARE(loadSpy.size(), 2); m_page->mainFrame()->setFocus(); - QVERIFY(m_page->mainFrame()->hasFocus()); + QTRY_VERIFY(m_page->mainFrame()->hasFocus()); for (int i = 0; i < children.size(); ++i) { children.at(i)->setFocus(); - QVERIFY(children.at(i)->hasFocus()); + QTRY_VERIFY(children.at(i)->hasFocus()); QVERIFY(!m_page->mainFrame()->hasFocus()); } m_page->mainFrame()->setFocus(); - QVERIFY(m_page->mainFrame()->hasFocus()); + QTRY_VERIFY(m_page->mainFrame()->hasFocus()); } void tst_QWebFrame::render() @@ -2623,26 +2667,24 @@ void tst_QWebFrame::render() QPicture picture; - // render clipping to Viewport - frame->setClipRenderToViewport(true); + QSize size = page.mainFrame()->contentsSize(); + page.setViewportSize(size); + + // render contents layer only (the iframe is smaller than the image, so it will have scrollbars) QPainter painter1(&picture); - frame->render(&painter1); + frame->render(&painter1, QWebFrame::ContentsLayer); painter1.end(); - QSize size = page.mainFrame()->contentsSize(); - page.setViewportSize(size); - QCOMPARE(size.width(), picture.boundingRect().width()); // 100px - QCOMPARE(size.height(), picture.boundingRect().height()); // 100px + QCOMPARE(size.width(), picture.boundingRect().width() + frame->scrollBarGeometry(Qt::Vertical).width()); + QCOMPARE(size.height(), picture.boundingRect().height() + frame->scrollBarGeometry(Qt::Horizontal).height()); - // render without clipping to Viewport - frame->setClipRenderToViewport(false); + // render everything, should be the size of the iframe QPainter painter2(&picture); - frame->render(&painter2); + frame->render(&painter2, QWebFrame::AllLayers); painter2.end(); - QImage resource(":/image.png"); - QCOMPARE(resource.width(), picture.boundingRect().width()); // resource width: 128px - QCOMPARE(resource.height(), picture.boundingRect().height()); // resource height: 128px + QCOMPARE(size.width(), picture.boundingRect().width()); // width: 100px + QCOMPARE(size.height(), picture.boundingRect().height()); // height: 100px } void tst_QWebFrame::scrollPosition() @@ -2669,5 +2711,59 @@ void tst_QWebFrame::scrollPosition() QCOMPARE(y, 29); } +void tst_QWebFrame::evaluateWillCauseRepaint() +{ + QWebView view; + QString html("<html><body>top<div id=\"junk\" style=\"display: block;\">" + "junk</div>bottom</body></html>"); + view.setHtml(html); + view.show(); + + QTest::qWaitForWindowShown(&view); + + view.page()->mainFrame()->evaluateJavaScript( + "document.getElementById('junk').style.display = 'none';"); + + ::waitForSignal(view.page(), SIGNAL(repaintRequested(QRect))); +} + +class TestFactory : public QObject +{ + Q_OBJECT +public: + TestFactory() + : obj(0), counter(0) + {} + + Q_INVOKABLE QObject* getNewObject() + { + delete obj; + obj = new QObject(this); + obj->setObjectName(QLatin1String("test") + QString::number(++counter)); + return obj; + + } + + QObject* obj; + int counter; +}; + +void tst_QWebFrame::qObjectWrapperWithSameIdentity() +{ + m_view->setHtml("<script>function triggerBug() { document.getElementById('span1').innerText = test.getNewObject().objectName; }</script>" + "<body><span id='span1'>test</span></body>"); + + QWebFrame* mainFrame = m_view->page()->mainFrame(); + QCOMPARE(mainFrame->toPlainText(), QString("test")); + + mainFrame->addToJavaScriptWindowObject("test", new TestFactory, QScriptEngine::ScriptOwnership); + + mainFrame->evaluateJavaScript("triggerBug();"); + QCOMPARE(mainFrame->toPlainText(), QString("test1")); + + mainFrame->evaluateJavaScript("triggerBug();"); + QCOMPARE(mainFrame->toPlainText(), QString("test2")); +} + QTEST_MAIN(tst_QWebFrame) #include "tst_qwebframe.moc" |
