diff options
Diffstat (limited to 'tests/auto/declarative/qdeclarativewebview')
20 files changed, 826 insertions, 0 deletions
diff --git a/tests/auto/declarative/qdeclarativewebview/data/basic.html b/tests/auto/declarative/qdeclarativewebview/data/basic.html new file mode 100644 index 0000000..22e3e24 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/basic.html @@ -0,0 +1,17 @@ +<html> +<head><title>Basic</title> +<link rel="icon" sizes="48x48" href="basic.png"> +<script type="text/javascript"> +<!-- +window.onload = function(){ window.status = "status here"; } +// --> +</script> +</head> +<body leftmargin="0" marginwidth="0"> +<table width="123"> +<tbody> +<tr><td>This is a basic test.</td></tr> +</tbody> +</table> +</body> +</html> diff --git a/tests/auto/declarative/qdeclarativewebview/data/basic.ico b/tests/auto/declarative/qdeclarativewebview/data/basic.ico Binary files differnew file mode 100644 index 0000000..8f3d05e --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/basic.ico diff --git a/tests/auto/declarative/qdeclarativewebview/data/basic.png b/tests/auto/declarative/qdeclarativewebview/data/basic.png Binary files differnew file mode 100644 index 0000000..35717cc --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/basic.png diff --git a/tests/auto/declarative/qdeclarativewebview/data/basic.qml b/tests/auto/declarative/qdeclarativewebview/data/basic.qml new file mode 100644 index 0000000..5394837 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/basic.qml @@ -0,0 +1,5 @@ +import Qt 4.6 + +WebView { + url: "basic.html" +} diff --git a/tests/auto/declarative/qdeclarativewebview/data/elements.html b/tests/auto/declarative/qdeclarativewebview/data/elements.html new file mode 100644 index 0000000..9236867 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/elements.html @@ -0,0 +1,14 @@ +<body leftmargin=0 topmargin=0> +<table width="300px" border=1 cellpadding=0 cellspacing=0> +<tr> +<td align=center width=25%%><p>A</p></td> +<td width=75% height=50px> + <table width=100% border=1 cellpadding=0 cellspacing=0> + <tr> + <td align=center width=50% height=50px><p>B</p></td> + <td align=center width=50% height=50px><p>C</p></td> + </tr> + </table> +</td> +</tr> +</table> diff --git a/tests/auto/declarative/qdeclarativewebview/data/elements.qml b/tests/auto/declarative/qdeclarativewebview/data/elements.qml new file mode 100644 index 0000000..7c030e6 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/elements.qml @@ -0,0 +1,7 @@ +import Qt 4.6 + +WebView { + url: "elements.html" + width: 310 + height: 100 +} diff --git a/tests/auto/declarative/qdeclarativewebview/data/forward.html b/tests/auto/declarative/qdeclarativewebview/data/forward.html new file mode 100644 index 0000000..62ab62d --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/forward.html @@ -0,0 +1,12 @@ +<html> +<head><title>Forward</title> +<link rel="icon" sizes="32x32" href="forward.png"> +</head> +<body leftmargin="0" marginwidth="0"> +<table width="123"> +<tbody> +<tr><td>This is more.</td></tr> +</tbody> +</table> +</body> +</html> diff --git a/tests/auto/declarative/qdeclarativewebview/data/javaScript.html b/tests/auto/declarative/qdeclarativewebview/data/javaScript.html new file mode 100644 index 0000000..35270bc --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/javaScript.html @@ -0,0 +1,11 @@ +<html> +<head><title>JavaScript</title> +<link rel="icon" sizes="48x48" href="basic.png"> +<script type="text/javascript"> +<!-- +window.onload = function(){ window.status = "status here"; } +// --> +</script> +</head> +<body> +This is a JS test. diff --git a/tests/auto/declarative/qdeclarativewebview/data/javaScript.qml b/tests/auto/declarative/qdeclarativewebview/data/javaScript.qml new file mode 100644 index 0000000..07eee88 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/javaScript.qml @@ -0,0 +1,11 @@ +import Qt 4.6 + +WebView { + url: "javaScript.html" + javaScriptWindowObjects: [ + QtObject { + property string qmlprop: "qmlvalue" + WebView.windowObjectName: "myjsname" + } + ] +} diff --git a/tests/auto/declarative/qdeclarativewebview/data/loadError.qml b/tests/auto/declarative/qdeclarativewebview/data/loadError.qml new file mode 100644 index 0000000..1460f30 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/loadError.qml @@ -0,0 +1,5 @@ +import Qt 4.6 + +WebView { + url: "does-not-exist.html" +} diff --git a/tests/auto/declarative/qdeclarativewebview/data/newwindows.html b/tests/auto/declarative/qdeclarativewebview/data/newwindows.html new file mode 100644 index 0000000..dd541f9 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/newwindows.html @@ -0,0 +1,16 @@ +<html> +<head> +<script type="text/javascript"> +<!-- +function clickTheLink() +{ + var ev = document.createEvent('MouseEvents'); + ev.initEvent( "click", true, false ); + document.getElementById('thelink').dispatchEvent(ev); +} +// --> +</script> +</head> +<h1>Multiple windows...</h1> + +<a id=thelink target="_blank" href="newwindows.html">Popup!</a> diff --git a/tests/auto/declarative/qdeclarativewebview/data/newwindows.qml b/tests/auto/declarative/qdeclarativewebview/data/newwindows.qml new file mode 100644 index 0000000..0bc8263 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/newwindows.qml @@ -0,0 +1,33 @@ +// Demonstrates opening new WebViews from HTML + +import Qt 4.6 + +Grid { + columns: 3 + id: pages + height: 300; width: 600 + property int total: 0 + + Component { + id: webViewPage + Rectangle { + width: webView.width + height: webView.height + border.color: "gray" + + WebView { + id: webView + width: 150 // force predictable for test + newWindowComponent: webViewPage + newWindowParent: pages + url: "newwindows.html" + Timer { + interval: 10; running: webView.status==WebView.Ready && total<4; repeat: false; + onTriggered: {total++; webView.evaluateJavaScript("clickTheLink()")} + } + } + } + } + + Loader { sourceComponent: webViewPage } +} diff --git a/tests/auto/declarative/qdeclarativewebview/data/pixelCache.html b/tests/auto/declarative/qdeclarativewebview/data/pixelCache.html new file mode 100644 index 0000000..9412674 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/pixelCache.html @@ -0,0 +1,10 @@ +<html> +<body topmargin=0 leftmargin=0> +<table width=120 cellpadding=0 cellspacing=0> +<tr><td> +<h1>Pixel Cache</h1> +This test is for the pixel cache. Because this is a long document, +as it scrolls, more of the document will need to be rendered. +If the pixelCacheSize is small, the first parts of the document will +no longer be in the cache when it returns. +</table> diff --git a/tests/auto/declarative/qdeclarativewebview/data/pixelCache.qml b/tests/auto/declarative/qdeclarativewebview/data/pixelCache.qml new file mode 100644 index 0000000..08e4d65 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/pixelCache.qml @@ -0,0 +1,6 @@ +import Test 1.0 + +MyWebView { + anchors.fill: parent + url: "pixelCache.html" +} diff --git a/tests/auto/declarative/qdeclarativewebview/data/propertychanges.qml b/tests/auto/declarative/qdeclarativewebview/data/propertychanges.qml new file mode 100644 index 0000000..3dd4e51 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/propertychanges.qml @@ -0,0 +1,33 @@ +import Qt 4.6 + +Item { + width: 240 + height: 160 + Grid { + anchors.fill: parent + objectName: "newWindowParent" + id: newWindowParent + } + + Row { + anchors.fill: parent + id: oldWindowParent + objectName: "oldWindowParent" + } + + Loader { + sourceComponent: webViewComponent + } + Component { + id: webViewComponent + WebView { + id: webView + objectName: "webView" + newWindowComponent: webViewComponent + newWindowParent: oldWindowParent + url: "basic.html" + renderingEnabled: true + pressGrabTime: 200 + } + } +}
\ No newline at end of file diff --git a/tests/auto/declarative/qdeclarativewebview/data/sethtml.qml b/tests/auto/declarative/qdeclarativewebview/data/sethtml.qml new file mode 100644 index 0000000..063b5a8 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/data/sethtml.qml @@ -0,0 +1,5 @@ +import Qt 4.6 + +WebView { + html: "<p>This is a <b>string</b> set on the WebView" +} diff --git a/tests/auto/declarative/qdeclarativewebview/qdeclarativewebview.pro b/tests/auto/declarative/qdeclarativewebview/qdeclarativewebview.pro new file mode 100644 index 0000000..6af0a60 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/qdeclarativewebview.pro @@ -0,0 +1,10 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative +contains(QT_CONFIG,webkit): QT += webkit +macx:CONFIG -= app_bundle + +SOURCES += tst_qdeclarativewebview.cpp testtypes.cpp +HEADERS += testtypes.h + +# Define SRCDIR equal to test's source directory +DEFINES += SRCDIR=\\\"$$PWD\\\" diff --git a/tests/auto/declarative/qdeclarativewebview/testtypes.cpp b/tests/auto/declarative/qdeclarativewebview/testtypes.cpp new file mode 100644 index 0000000..7efc214 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/testtypes.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "testtypes.h" + +void MyWebView::drawContents(QPainter *p, const QRect &r) +{ + pp += r.width()*r.height(); + QDeclarativeWebView::drawContents(p,r); +} + +void registerTypes() +{ + QML_REGISTER_TYPE(Test,1,0,MyWebView,MyWebView); +} diff --git a/tests/auto/declarative/qdeclarativewebview/testtypes.h b/tests/auto/declarative/qdeclarativewebview/testtypes.h new file mode 100644 index 0000000..8eb703f --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/testtypes.h @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef TESTTYPES_H +#define TESTTYPES_H + +#include <private/qdeclarativewebview_p.h> + +class MyWebView : public QDeclarativeWebView +{ + Q_OBJECT + Q_PROPERTY(int pixelsPainted READ pixelsPainted); + +public: + MyWebView() : pp(0) {} + + int pixelsPainted() const { return pp; } + + void drawContents(QPainter *p, const QRect &r); + +private: + int pp; +}; + +QML_DECLARE_TYPE(MyWebView); + +void registerTypes(); + +#endif // TESTTYPES_H diff --git a/tests/auto/declarative/qdeclarativewebview/tst_qdeclarativewebview.cpp b/tests/auto/declarative/qdeclarativewebview/tst_qdeclarativewebview.cpp new file mode 100644 index 0000000..6d16056 --- /dev/null +++ b/tests/auto/declarative/qdeclarativewebview/tst_qdeclarativewebview.cpp @@ -0,0 +1,513 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <qtest.h> +#include <QtTest/QSignalSpy> +#include "../../../shared/util.h" +#include <QtDeclarative/qdeclarativeengine.h> +#include <QtDeclarative/qdeclarativecomponent.h> +#include <private/qdeclarativewebview_p.h> +#include <private/qdeclarativewebview_p_p.h> +#include <private/qdeclarativepositioners_p.h> +#include <QtWebKit/qwebpage.h> +#include <QtWebKit/qwebframe.h> +#include <QtCore/qdir.h> +#include <QtCore/qfile.h> +#include <QtGui/qpainter.h> +#include "testtypes.h" + +class tst_qdeclarativewebview : public QObject +{ + Q_OBJECT +public: + tst_qdeclarativewebview() {} + +private slots: + void initTestCase(); + void basicProperties(); + void settings(); + void historyNav(); + void multipleWindows(); + void elementAreaAt(); + void loadError(); + void setHtml(); + void javaScript(); + void cleanupTestCase(); + void pixelCache(); + void newWindowParent(); + void newWindowComponent(); + void renderingEnabled(); + void pressGrabTime(); + +private: + void checkNoErrors(const QDeclarativeComponent& component); + QDeclarativeEngine engine; + QString tmpDir() const + { + static QString tmpd = QDir::tempPath()+"/tst_qdeclarativewebview-" + + QDateTime::currentDateTime().toString(QLatin1String("yyyyMMddhhmmss")); + return tmpd; + } +}; + +void tst_qdeclarativewebview::initTestCase() +{ + registerTypes(); +} + +static QString strippedHtml(QString html) +{ + html.replace(QRegExp("\\s+"),""); + return html; +} + +static QString fileContents(const QString& filename) +{ + QFile file(filename); + file.open(QIODevice::ReadOnly); + return QString::fromUtf8(file.readAll()); +} + + +static void removeRecursive(const QString& dirname) +{ + QDir dir(dirname); + QFileInfoList entries(dir.entryInfoList(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot)); + for (int i = 0; i < entries.count(); ++i) + if (entries[i].isDir()) + removeRecursive(entries[i].filePath()); + else + dir.remove(entries[i].fileName()); + QDir().rmdir(dirname); +} + +void tst_qdeclarativewebview::cleanupTestCase() +{ + removeRecursive(tmpDir()); +} + +void tst_qdeclarativewebview::checkNoErrors(const QDeclarativeComponent& component) +{ + // Wait until the component is ready + QTRY_VERIFY(component.isReady()); + + + if (component.isError()) { + QList<QDeclarativeError> errors = component.errors(); + for (int ii = 0; ii < errors.count(); ++ii) { + const QDeclarativeError &error = errors.at(ii); + QByteArray errorStr = QByteArray::number(error.line()) + ":" + + QByteArray::number(error.column()) + ":" + + error.description().toUtf8(); + qWarning() << errorStr; + } + } + QVERIFY(!component.isError()); +} + +void tst_qdeclarativewebview::basicProperties() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/basic.qml")); + checkNoErrors(component); + QWebSettings::enablePersistentStorage(tmpDir()); + + QDeclarativeWebView *wv = qobject_cast<QDeclarativeWebView*>(component.create()); + QVERIFY(wv != 0); + QTRY_COMPARE(wv->progress(), 1.0); + QCOMPARE(wv->title(),QString("Basic")); + QTRY_COMPARE(wv->icon().width(), 48); + QCOMPARE(wv->icon(),QPixmap(SRCDIR "/data/basic.png")); + QCOMPARE(wv->statusText(),QString("status here")); + QCOMPARE(strippedHtml(fileContents(SRCDIR "/data/basic.html")), strippedHtml(wv->html())); + QCOMPARE(wv->width(), 123.0); + QCOMPARE(wv->preferredWidth(), 0); + QCOMPARE(wv->preferredHeight(), 0); + QCOMPARE(wv->zoomFactor(), 1.0); + QCOMPARE(wv->url(), QUrl::fromLocalFile(SRCDIR "/data/basic.html")); + QCOMPARE(wv->status(), QDeclarativeWebView::Ready); + QVERIFY(wv->reloadAction()); + QVERIFY(wv->reloadAction()->isEnabled()); + QVERIFY(wv->backAction()); + QVERIFY(!wv->backAction()->isEnabled()); + QVERIFY(wv->forwardAction()); + QVERIFY(!wv->forwardAction()->isEnabled()); + QVERIFY(wv->stopAction()); + QVERIFY(!wv->stopAction()->isEnabled()); + + wv->setPixelCacheSize(0); // mainly testing that it doesn't crash or anything! + QCOMPARE(wv->pixelCacheSize(),0); + wv->reloadAction()->trigger(); + QTRY_COMPARE(wv->progress(), 1.0); +} + +void tst_qdeclarativewebview::settings() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/basic.qml")); + checkNoErrors(component); + QDeclarativeWebView *wv = qobject_cast<QDeclarativeWebView*>(component.create()); + QVERIFY(wv != 0); + QTRY_COMPARE(wv->progress(), 1.0); + + QDeclarativeWebSettings *s = wv->settingsObject(); + + // merely tests that setting gets stored (in QWebSettings) + // behavioural tests are in WebKit. + for (int b=0; b<=1; ++b) { + bool on = !!b; + + s->setAutoLoadImages(on); + s->setDeveloperExtrasEnabled(on); + s->setJavaEnabled(on); + s->setJavascriptCanAccessClipboard(on); + s->setJavascriptCanOpenWindows(on); + s->setJavascriptEnabled(on); + s->setLinksIncludedInFocusChain(on); + s->setLocalContentCanAccessRemoteUrls(on); + s->setLocalStorageDatabaseEnabled(on); + s->setOfflineStorageDatabaseEnabled(on); + s->setOfflineWebApplicationCacheEnabled(on); + s->setPluginsEnabled(on); + s->setPrintElementBackgrounds(on); + s->setPrivateBrowsingEnabled(on); + s->setZoomTextOnly(on); + + QVERIFY(s->autoLoadImages() == on); + QVERIFY(s->developerExtrasEnabled() == on); + QVERIFY(s->javaEnabled() == on); + QVERIFY(s->javascriptCanAccessClipboard() == on); + QVERIFY(s->javascriptCanOpenWindows() == on); + QVERIFY(s->javascriptEnabled() == on); + QVERIFY(s->linksIncludedInFocusChain() == on); + QVERIFY(s->localContentCanAccessRemoteUrls() == on); + QVERIFY(s->localStorageDatabaseEnabled() == on); + QVERIFY(s->offlineStorageDatabaseEnabled() == on); + QVERIFY(s->offlineWebApplicationCacheEnabled() == on); + QVERIFY(s->pluginsEnabled() == on); + QVERIFY(s->printElementBackgrounds() == on); + QVERIFY(s->privateBrowsingEnabled() == on); + QVERIFY(s->zoomTextOnly() == on); + + QVERIFY(s->property("autoLoadImages") == on); + QVERIFY(s->property("developerExtrasEnabled") == on); + QVERIFY(s->property("javaEnabled") == on); + QVERIFY(s->property("javascriptCanAccessClipboard") == on); + QVERIFY(s->property("javascriptCanOpenWindows") == on); + QVERIFY(s->property("javascriptEnabled") == on); + QVERIFY(s->property("linksIncludedInFocusChain") == on); + QVERIFY(s->property("localContentCanAccessRemoteUrls") == on); + QVERIFY(s->property("localStorageDatabaseEnabled") == on); + QVERIFY(s->property("offlineStorageDatabaseEnabled") == on); + QVERIFY(s->property("offlineWebApplicationCacheEnabled") == on); + QVERIFY(s->property("pluginsEnabled") == on); + QVERIFY(s->property("printElementBackgrounds") == on); + QVERIFY(s->property("privateBrowsingEnabled") == on); + QVERIFY(s->property("zoomTextOnly") == on); + } +} + +void tst_qdeclarativewebview::historyNav() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/basic.qml")); + checkNoErrors(component); + QWebSettings::enablePersistentStorage(tmpDir()); + + QDeclarativeWebView *wv = qobject_cast<QDeclarativeWebView*>(component.create()); + QVERIFY(wv != 0); + for (int i=1; i<=2; ++i) { + QTRY_COMPARE(wv->progress(), 1.0); + QCOMPARE(wv->title(),QString("Basic")); + QTRY_COMPARE(wv->icon().width(), 48); + QCOMPARE(wv->icon(),QPixmap(SRCDIR "/data/basic.png")); + QCOMPARE(wv->statusText(),QString("status here")); + QCOMPARE(strippedHtml(fileContents(SRCDIR "/data/basic.html")), strippedHtml(wv->html())); + QCOMPARE(wv->width(), 123.0); + QCOMPARE(wv->preferredWidth(), 0); + QCOMPARE(wv->zoomFactor(), 1.0); + QCOMPARE(wv->url(), QUrl::fromLocalFile(SRCDIR "/data/basic.html")); + QCOMPARE(wv->status(), QDeclarativeWebView::Ready); + QVERIFY(wv->reloadAction()); + QVERIFY(wv->reloadAction()->isEnabled()); + QVERIFY(wv->backAction()); + QVERIFY(!wv->backAction()->isEnabled()); + QVERIFY(wv->forwardAction()); + QVERIFY(!wv->forwardAction()->isEnabled()); + QVERIFY(wv->stopAction()); + QVERIFY(!wv->stopAction()->isEnabled()); + + wv->reloadAction()->trigger(); + } + + wv->setUrl(QUrl::fromLocalFile(SRCDIR "/data/forward.html")); + QTRY_COMPARE(wv->progress(), 1.0); + QCOMPARE(wv->title(),QString("Forward")); + QTRY_COMPARE(wv->icon().width(), 32); + QCOMPARE(wv->icon(),QPixmap(SRCDIR "/data/forward.png")); + QCOMPARE(strippedHtml(fileContents(SRCDIR "/data/forward.html")), strippedHtml(wv->html())); + QCOMPARE(wv->url(), QUrl::fromLocalFile(SRCDIR "/data/forward.html")); + QCOMPARE(wv->status(), QDeclarativeWebView::Ready); + QCOMPARE(wv->statusText(),QString("")); + QVERIFY(wv->reloadAction()); + QVERIFY(wv->reloadAction()->isEnabled()); + QVERIFY(wv->backAction()); + QVERIFY(wv->backAction()->isEnabled()); + QVERIFY(wv->forwardAction()); + QVERIFY(!wv->forwardAction()->isEnabled()); + QVERIFY(wv->stopAction()); + QVERIFY(!wv->stopAction()->isEnabled()); + + wv->backAction()->trigger(); + + QTRY_COMPARE(wv->progress(), 1.0); + QCOMPARE(wv->title(),QString("Basic")); + QCOMPARE(strippedHtml(fileContents(SRCDIR "/data/basic.html")), strippedHtml(wv->html())); + QCOMPARE(wv->url(), QUrl::fromLocalFile(SRCDIR "/data/basic.html")); + QCOMPARE(wv->status(), QDeclarativeWebView::Ready); + QVERIFY(wv->reloadAction()); + QVERIFY(wv->reloadAction()->isEnabled()); + QVERIFY(wv->backAction()); + QVERIFY(!wv->backAction()->isEnabled()); + QVERIFY(wv->forwardAction()); + QVERIFY(wv->forwardAction()->isEnabled()); + QVERIFY(wv->stopAction()); + QVERIFY(!wv->stopAction()->isEnabled()); +} + +void tst_qdeclarativewebview::multipleWindows() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/newwindows.qml")); + checkNoErrors(component); + + QDeclarativeGrid *grid = qobject_cast<QDeclarativeGrid*>(component.create()); + QVERIFY(grid != 0); + QTRY_COMPARE(grid->children().count(), 2+5); // Component, Loader, 5 WebViews + QDeclarativeItem* popup = qobject_cast<QDeclarativeItem*>(grid->children().at(3)); // first popup after Component, Loaded, original. + QVERIFY(popup != 0); + QTRY_COMPARE(popup->x(), 150.0); +} + +void tst_qdeclarativewebview::loadError() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/loadError.qml")); + checkNoErrors(component); + QWebSettings::enablePersistentStorage(tmpDir()); + + QDeclarativeWebView *wv = qobject_cast<QDeclarativeWebView*>(component.create()); + QVERIFY(wv != 0); + for (int i=1; i<=2; ++i) { + QTRY_COMPARE(wv->progress(), 1.0); + QCOMPARE(wv->title(),QString("")); + QCOMPARE(wv->statusText(),QString("")); // HTML 'status bar' text, not error message + QCOMPARE(wv->url(), QUrl::fromLocalFile(SRCDIR "/data/does-not-exist.html")); // Unlike QWebPage, which loses url + QCOMPARE(wv->status(), QDeclarativeWebView::Error); + + wv->reloadAction()->trigger(); + } +} + +void tst_qdeclarativewebview::setHtml() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/sethtml.qml")); + checkNoErrors(component); + QDeclarativeWebView *wv = qobject_cast<QDeclarativeWebView*>(component.create()); + QVERIFY(wv != 0); + QCOMPARE(wv->html(),QString("<html><head></head><body><p>This is a <b>string</b> set on the WebView</p></body></html>")); + + QSignalSpy spy(wv, SIGNAL(htmlChanged())); + wv->setHtml(QString("<html><head><title>Basic</title></head><body><p>text</p></body></html>")); + QCOMPARE(spy.count(),1); +} + +void tst_qdeclarativewebview::elementAreaAt() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/elements.qml")); + checkNoErrors(component); + QDeclarativeWebView *wv = qobject_cast<QDeclarativeWebView*>(component.create()); + QVERIFY(wv != 0); + QTRY_COMPARE(wv->progress(), 1.0); + + QCOMPARE(wv->elementAreaAt(40,30,100,100),QRect(1,1,75,54)); // Area A in data/elements.html + QCOMPARE(wv->elementAreaAt(130,30,200,100),QRect(78,3,110,50)); // Area B + QCOMPARE(wv->elementAreaAt(40,30,400,400),QRect(0,0,310,100)); // Whole view + QCOMPARE(wv->elementAreaAt(130,30,280,280),QRect(76,1,223,54)); // Area BC + QCOMPARE(wv->elementAreaAt(130,30,400,400),QRect(0,0,310,100)); // Whole view +} + +void tst_qdeclarativewebview::javaScript() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/javaScript.qml")); + checkNoErrors(component); + QDeclarativeWebView *wv = qobject_cast<QDeclarativeWebView*>(component.create()); + QVERIFY(wv != 0); + QTRY_COMPARE(wv->progress(), 1.0); + QCOMPARE(wv->evaluateJavaScript("123").toInt(), 123); + QCOMPARE(wv->evaluateJavaScript("window.status").toString(), QString("status here")); + QCOMPARE(wv->evaluateJavaScript("window.myjsname.qmlprop").toString(), QString("qmlvalue")); +} + +void tst_qdeclarativewebview::pixelCache() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/pixelCache.qml")); + checkNoErrors(component); + MyWebView *wv = qobject_cast<MyWebView*>(component.create()); + QVERIFY(wv != 0); + QTRY_COMPARE(wv->progress(), 1.0); + QPixmap pm(150,150); + QPainter p(&pm); + wv->paint(&p,0,0); + const int expected = 120*(150+128); // 120 = width of HTML page, 150=pixmap height, 128=cache extra area + QCOMPARE(wv->pixelsPainted(), expected); + wv->paint(&p,0,0); + QCOMPARE(wv->pixelsPainted(), expected); // nothing new needed to be painted + wv->setPixelCacheSize(0); // clears the cache + wv->paint(&p,0,0); + QCOMPARE(wv->pixelsPainted(), expected*2); // everything needed to be painted + // Note that painted things always go into the cache (even if they don't "fit"), + // just that they will be removed if anything else needs to be painted. + wv->setPixelCacheSize(expected); // won't clear the cache + wv->paint(&p,0,0); + QCOMPARE(wv->pixelsPainted(), expected*2); // still there + wv->setPixelCacheSize(expected-1); // too small - will clear the cache + wv->paint(&p,0,0); + QCOMPARE(wv->pixelsPainted(), expected*3); // repainted +} + +void tst_qdeclarativewebview::newWindowParent() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertychanges.qml")); + checkNoErrors(component); + QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(component.create()); + QDeclarativeWebView *wv = rootItem->findChild<QDeclarativeWebView*>("webView"); + QVERIFY(rootItem != 0); + QVERIFY(wv != 0); + QTRY_COMPARE(wv->progress(), 1.0); + + QDeclarativeItem* oldWindowParent = rootItem->findChild<QDeclarativeItem*>("oldWindowParent"); + QCOMPARE(wv->newWindowParent(), oldWindowParent); + QSignalSpy newWindowParentSpy(wv, SIGNAL(newWindowParentChanged())); + + QDeclarativeItem* newWindowParent = rootItem->findChild<QDeclarativeItem*>("newWindowParent"); + wv->setNewWindowParent(newWindowParent); + QVERIFY(oldWindowParent); + QVERIFY(oldWindowParent->childItems().count() == 0); + QCOMPARE(wv->newWindowParent(), newWindowParent); + QCOMPARE(newWindowParentSpy.count(),1); + + wv->setNewWindowParent(newWindowParent); + QCOMPARE(newWindowParentSpy.count(),1); + + wv->setNewWindowParent(0); + QCOMPARE(newWindowParentSpy.count(),2); +} + +void tst_qdeclarativewebview::newWindowComponent() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertychanges.qml")); + checkNoErrors(component); + QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(component.create()); + QDeclarativeWebView *wv = rootItem->findChild<QDeclarativeWebView*>("webView"); + QVERIFY(rootItem != 0); + QVERIFY(wv != 0); + QTRY_COMPARE(wv->progress(), 1.0); + + QDeclarativeComponent substituteComponent(&engine); + substituteComponent.setData("import Qt 4.6; WebView { objectName: 'newWebView'; url: 'basic.html'; }", QUrl::fromLocalFile("")); + QSignalSpy newWindowComponentSpy(wv, SIGNAL(newWindowComponentChanged())); + + wv->setNewWindowComponent(&substituteComponent); + QCOMPARE(wv->newWindowComponent(), &substituteComponent); + QCOMPARE(newWindowComponentSpy.count(),1); + + wv->setNewWindowComponent(&substituteComponent); + QCOMPARE(newWindowComponentSpy.count(),1); + + wv->setNewWindowComponent(0); + QCOMPARE(newWindowComponentSpy.count(),2); +} + +void tst_qdeclarativewebview::renderingEnabled() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertychanges.qml")); + checkNoErrors(component); + QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(component.create()); + QDeclarativeWebView *wv = rootItem->findChild<QDeclarativeWebView*>("webView"); + QVERIFY(rootItem != 0); + QVERIFY(wv != 0); + QTRY_COMPARE(wv->progress(), 1.0); + + QVERIFY(wv->renderingEnabled()); + QSignalSpy renderingEnabledSpy(wv, SIGNAL(renderingEnabledChanged())); + + wv->setRenderingEnabled(false); + QVERIFY(!wv->renderingEnabled()); + QCOMPARE(renderingEnabledSpy.count(),1); + + wv->setRenderingEnabled(false); + QCOMPARE(renderingEnabledSpy.count(),1); + + wv->setRenderingEnabled(true); + QCOMPARE(renderingEnabledSpy.count(),2); +} + +void tst_qdeclarativewebview::pressGrabTime() +{ + QDeclarativeComponent component(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertychanges.qml")); + checkNoErrors(component); + QDeclarativeItem *rootItem = qobject_cast<QDeclarativeItem*>(component.create()); + QDeclarativeWebView *wv = rootItem->findChild<QDeclarativeWebView*>("webView"); + QVERIFY(rootItem != 0); + QVERIFY(wv != 0); + QTRY_COMPARE(wv->progress(), 1.0); + QCOMPARE(wv->pressGrabTime(), 200); + QSignalSpy pressGrabTimeSpy(wv, SIGNAL(pressGrabTimeChanged())); + + wv->setPressGrabTime(100); + QCOMPARE(wv->pressGrabTime(), 100); + QCOMPARE(pressGrabTimeSpy.count(),1); + + wv->setPressGrabTime(100); + QCOMPARE(pressGrabTimeSpy.count(),1); + + wv->setPressGrabTime(0); + QCOMPARE(pressGrabTimeSpy.count(),2); +} + +QTEST_MAIN(tst_qdeclarativewebview) + +#include "tst_qdeclarativewebview.moc" |