diff options
author | Christian Strømme <christian.stromme@theqtcompany.com> | 2015-05-22 14:29:39 +0200 |
---|---|---|
committer | Christian Stromme <christian.stromme@theqtcompany.com> | 2015-06-22 11:24:02 +0000 |
commit | 9efaf07a5e19ef3375be5246c741f4002f95cdfd (patch) | |
tree | a1159fad043e17370ecb531bfcead3e8ec974d63 | |
parent | 9335e65528bd41f2614841304fc5190eded409ad (diff) |
Import qml tests from WebEngine
Tests are taken from QtWebEngine and adjusted to test QtWebView.
Change-Id: I0a8ed5b844beeec44043ff54145d45267321a7cf
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
-rw-r--r-- | tests/auto/qml/html/basic_page.html | 6 | ||||
-rw-r--r-- | tests/auto/qml/html/basic_page2.html | 1 | ||||
-rw-r--r-- | tests/auto/qml/html/direct-image-compositing.html | 66 | ||||
-rw-r--r-- | tests/auto/qml/html/inputmethod.html | 11 | ||||
-rw-r--r-- | tests/auto/qml/html/resources/simple_image.png | bin | 0 -> 10585 bytes | |||
-rw-r--r-- | tests/auto/qml/html/scroll.html | 29 | ||||
-rw-r--r-- | tests/auto/qml/html/testdata.qrc | 12 | ||||
-rw-r--r-- | tests/auto/qml/qml.pro | 4 | ||||
-rw-r--r-- | tests/auto/qml/qquickwebview/dummy.qml | 8 | ||||
-rw-r--r-- | tests/auto/qml/qquickwebview/qquickwebview.pro | 5 | ||||
-rw-r--r-- | tests/auto/qml/qquickwebview/tst_qquickwebview.cpp | 334 | ||||
-rw-r--r-- | tests/auto/qml/shared/qt_webview_quicktest.h | 67 | ||||
-rw-r--r-- | tests/auto/qml/shared/testwindow.h | 59 | ||||
-rw-r--r-- | tests/auto/qml/shared/util.h | 120 | ||||
-rw-r--r-- | tests/auto/qml/tests.pri | 22 |
15 files changed, 744 insertions, 0 deletions
diff --git a/tests/auto/qml/html/basic_page.html b/tests/auto/qml/html/basic_page.html new file mode 100644 index 0000000..53726e4 --- /dev/null +++ b/tests/auto/qml/html/basic_page.html @@ -0,0 +1,6 @@ +<html> +<head> +<title> Basic Page </title> +</head> +<h1>Basic page</h1> +</html> diff --git a/tests/auto/qml/html/basic_page2.html b/tests/auto/qml/html/basic_page2.html new file mode 100644 index 0000000..f8cff29 --- /dev/null +++ b/tests/auto/qml/html/basic_page2.html @@ -0,0 +1 @@ +<h1>Basic page 2</h1> diff --git a/tests/auto/qml/html/direct-image-compositing.html b/tests/auto/qml/html/direct-image-compositing.html new file mode 100644 index 0000000..53a4ca1 --- /dev/null +++ b/tests/auto/qml/html/direct-image-compositing.html @@ -0,0 +1,66 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" + "http://www.w3.org/TR/html4/loose.dtd"> + +<html lang="en"> +<head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <title>Testing direct image layer optimization</title> + <style type="text/css" media="screen"> + img { + float: left; + width: 150px; + height: 150px; + } + img { + -webkit-transform: rotate3d(0, 0, 1, 0); + } + .test { + float: left; + height: 200px; + width: 260px; + } + </style> +</head> +<body> + + <h1>Image optimization in layers</h1> + + <p> + This test exercises direct compositing of images with hardware acceleration. The visual results + using ACCELERATED_COMPOSITING and regular TOT should be identical. Running this test manually with + the correct debug options will show which elements are directly composited. See + <a href="https://bugs.webkit.org/show_bug.cgi?id=23361">https://bugs.webkit.org/show_bug.cgi?id=23361</a> + </p> + + <div class="test"> + <img src="resources/simple_image.png"> + Basic image - no style - can be directly composited + </div> + + <div class="test"> + <img src="resources/simple_image.png" style="border: 5px solid blue;"> + 5px blue border - can NOT be directly composited + </div> + + <div class="test"> + <img src="resources/simple_image.png" style="margin: 5px 5px;"> + margin - can NOT be directly composited + </div> + + <div class="test"> + <img src="resources/simple_image.png" style="background-color: grey;"> + solid background - can be directly composited + </div> + + <div class="test"> + <img src="resources/simple_image.png" style="background: orange url(resources/simple_image.png) -50px -50px;"> + background image - can NOT be directly composited + </div> + + <div class="test"> + <img src="resources/simple_image.png" style="-webkit-transform: rotate3d(0, 0, 1, 10deg);"> + rotated but otherwise no style - can be directly composited + </div> + +</body> +</html> diff --git a/tests/auto/qml/html/inputmethod.html b/tests/auto/qml/html/inputmethod.html new file mode 100644 index 0000000..dc9140f --- /dev/null +++ b/tests/auto/qml/html/inputmethod.html @@ -0,0 +1,11 @@ +<html> +<head> +<title>Basic Page For Input Method Testing</title> +</head> +<body> +<h1>Basic page</h1> +<input id="inputField" /> +<input id="emailInputField" type="email" /> +<div id="editableDiv" contenteditable></div> +</body> +</html> diff --git a/tests/auto/qml/html/resources/simple_image.png b/tests/auto/qml/html/resources/simple_image.png Binary files differnew file mode 100644 index 0000000..4685399 --- /dev/null +++ b/tests/auto/qml/html/resources/simple_image.png diff --git a/tests/auto/qml/html/scroll.html b/tests/auto/qml/html/scroll.html new file mode 100644 index 0000000..ce2193b --- /dev/null +++ b/tests/auto/qml/html/scroll.html @@ -0,0 +1,29 @@ +<!DOCTYPE html> +<html> +<meta name="viewport" content="width=200, height=500, user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1"/> +<script type="text/javascript"> +function pageScroll() { + window.scrollBy(0,50); // horizontal and vertical scroll increments +} +</script> +<style> + body { + background-color: blue; + margin: 50 50 50 50; + } + div { + font-color: white; + background-color: green; + width: 300px; + height: 1000px; + } +</style> + +<head> +<title>Scroll test </title> +</head> +<body onload="pageScroll()"> +<div> +</div> +</body> +</html> diff --git a/tests/auto/qml/html/testdata.qrc b/tests/auto/qml/html/testdata.qrc new file mode 100644 index 0000000..f2cfa48 --- /dev/null +++ b/tests/auto/qml/html/testdata.qrc @@ -0,0 +1,12 @@ +<RCC> + <qresource prefix="/"> + <file>scroll.html</file> + <file>basic_page.html</file> + <file>basic_page2.html</file> + <file>direct-image-compositing.html</file> + <file>inputmethod.html</file> + </qresource> + <qresource prefix="/resources"> + <file>resources/simple_image.png</file> + </qresource> +</RCC> diff --git a/tests/auto/qml/qml.pro b/tests/auto/qml/qml.pro new file mode 100644 index 0000000..97e7558 --- /dev/null +++ b/tests/auto/qml/qml.pro @@ -0,0 +1,4 @@ +TEMPLATE = subdirs + +SUBDIRS += \ + qquickwebview diff --git a/tests/auto/qml/qquickwebview/dummy.qml b/tests/auto/qml/qquickwebview/dummy.qml new file mode 100644 index 0000000..cdead5a --- /dev/null +++ b/tests/auto/qml/qquickwebview/dummy.qml @@ -0,0 +1,8 @@ +import QtQuick 2.0 +import QtWebView 1.1 + + WebView { + width: 100 + height: 62 +} + diff --git a/tests/auto/qml/qquickwebview/qquickwebview.pro b/tests/auto/qml/qquickwebview/qquickwebview.pro new file mode 100644 index 0000000..d93b615 --- /dev/null +++ b/tests/auto/qml/qquickwebview/qquickwebview.pro @@ -0,0 +1,5 @@ +include(../tests.pri) + +exists($${TARGET}.qrc):RESOURCES += $${TARGET}.qrc + +HEADERS += ../shared/util.h diff --git a/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp b/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp new file mode 100644 index 0000000..cd4fb96 --- /dev/null +++ b/tests/auto/qml/qquickwebview/tst_qquickwebview.cpp @@ -0,0 +1,334 @@ +/* + Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "testwindow.h" +#include "util.h" + +#include <QScopedPointer> +#include <QtQml/QQmlEngine> +#include <QtTest/QtTest> +#include <QtWebView/qquickwebview.h> +#include <QtCore/qfile.h> +#include <QtCore/qstandardpaths.h> + +QString getTestFilePath(const QString &testFile) +{ + const QString tempTestFile = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/" + testFile; + const bool exists = QFile::exists(tempTestFile); + if (exists) + return tempTestFile; + + QFile tf(QString(":/") + testFile); + const bool copied = tf.copy(tempTestFile); + + return copied ? tempTestFile : testFile; +} + +class tst_QQuickWebView : public QObject { + Q_OBJECT +public: + tst_QQuickWebView(); + +private Q_SLOTS: + void init(); + void cleanup(); + + void navigationStatusAtStartup(); + void stopEnabledAfterLoadStarted(); + void baseUrl(); + void loadEmptyUrl(); + void loadEmptyPageViewVisible(); + void loadEmptyPageViewHidden(); + void loadNonexistentFileUrl(); + void backAndForward(); + void reload(); + void stop(); + void loadProgress(); + + void show(); + void showWebView(); + void removeFromCanvas(); + void multipleWebViewWindows(); + void multipleWebViews(); + void titleUpdate(); + +private: + inline QQuickWebView *newWebView(); + inline QQuickWebView *webView() const; + void runJavaScript(const QString &script); + QScopedPointer<TestWindow> m_window; + QScopedPointer<QQmlComponent> m_component; +}; + +tst_QQuickWebView::tst_QQuickWebView() +{ + static QQmlEngine *engine = new QQmlEngine(this); + m_component.reset(new QQmlComponent(engine, this)); + m_component->setData(QByteArrayLiteral("import QtQuick 2.0\n" + "import QtWebView 1.1\n" + "WebView {}") + , QUrl()); +} + +QQuickWebView *tst_QQuickWebView::newWebView() +{ + QObject *viewInstance = m_component->create(); + QQuickWebView *webView = qobject_cast<QQuickWebView*>(viewInstance); + return webView; +} + +void tst_QQuickWebView::init() +{ + m_window.reset(new TestWindow(newWebView())); +} + +void tst_QQuickWebView::cleanup() +{ + m_window.reset(); +} + +inline QQuickWebView *tst_QQuickWebView::webView() const +{ + return static_cast<QQuickWebView*>(m_window->webView.data()); +} + +void tst_QQuickWebView::runJavaScript(const QString &script) +{ + webView()->runJavaScript(script); +} + +void tst_QQuickWebView::navigationStatusAtStartup() +{ + QCOMPARE(webView()->canGoBack(), false); + + QCOMPARE(webView()->canGoForward(), false); + + QCOMPARE(webView()->isLoading(), false); +} + +void tst_QQuickWebView::stopEnabledAfterLoadStarted() +{ + QCOMPARE(webView()->isLoading(), false); + + LoadStartedCatcher catcher(webView()); + webView()->setUrl(QUrl(getTestFilePath("basic_page.html"))); + waitForSignal(&catcher, SIGNAL(finished())); + + QCOMPARE(webView()->isLoading(), true); + + QVERIFY(waitForLoadSucceeded(webView())); +} + +void tst_QQuickWebView::baseUrl() +{ + // Test the url is in a well defined state when instanciating the view, but before loading anything. + QVERIFY(webView()->url().isEmpty()); +} + +void tst_QQuickWebView::loadEmptyUrl() +{ + webView()->setUrl(QUrl()); + webView()->setUrl(QUrl(QLatin1String(""))); +} + +void tst_QQuickWebView::loadEmptyPageViewVisible() +{ + m_window->show(); + loadEmptyPageViewHidden(); +} + +void tst_QQuickWebView::loadEmptyPageViewHidden() +{ + QSignalSpy loadSpy(webView(), SIGNAL(loadingChanged(QQuickWebViewLoadRequest*))); + + webView()->setUrl(QUrl(getTestFilePath("basic_page.html"))); + QVERIFY(waitForLoadSucceeded(webView())); + + QCOMPARE(loadSpy.size(), 2); +} + +void tst_QQuickWebView::loadNonexistentFileUrl() +{ + QSignalSpy loadSpy(webView(), SIGNAL(loadingChanged(QQuickWebViewLoadRequest*))); + + webView()->setUrl(QUrl(getTestFilePath("file_that_does_not_exist.html"))); + QVERIFY(waitForLoadFailed(webView())); + + QCOMPARE(loadSpy.size(), 2); +} + +void tst_QQuickWebView::backAndForward() +{ + const QString basicPage = getTestFilePath("basic_page.html"); + const QString basicPage2 = getTestFilePath("basic_page2.html"); + webView()->setUrl(QUrl(basicPage)); + QVERIFY(waitForLoadSucceeded(webView())); + + QCOMPARE(webView()->url().path(), basicPage); + + webView()->setUrl(QUrl(basicPage2)); + QVERIFY(waitForLoadSucceeded(webView())); + + QCOMPARE(webView()->url().path(), basicPage2); + + webView()->goBack(); + QVERIFY(waitForLoadSucceeded(webView())); + + QCOMPARE(webView()->url().path(), basicPage); + + webView()->goForward(); + QVERIFY(waitForLoadSucceeded(webView())); + + QCOMPARE(webView()->url().path(), basicPage2); +} + +void tst_QQuickWebView::reload() +{ + webView()->setUrl(QUrl(getTestFilePath("basic_page.html"))); + QVERIFY(waitForLoadSucceeded(webView())); + + QCOMPARE(webView()->url().path(), getTestFilePath("basic_page.html")); + + webView()->reload(); + QVERIFY(waitForLoadSucceeded(webView())); + + QCOMPARE(webView()->url().path(), getTestFilePath("basic_page.html")); +} + +void tst_QQuickWebView::stop() +{ + webView()->setUrl(QUrl(getTestFilePath("basic_page.html"))); + QVERIFY(waitForLoadSucceeded(webView())); + + QCOMPARE(webView()->url().path(), getTestFilePath("basic_page.html")); + + webView()->stop(); +} + +void tst_QQuickWebView::loadProgress() +{ + QCOMPARE(webView()->loadProgress(), 0); + + webView()->setUrl(QUrl(getTestFilePath("basic_page.html"))); + QSignalSpy loadProgressChangedSpy(webView(), SIGNAL(loadProgressChanged())); + QVERIFY(waitForLoadSucceeded(webView())); + + QVERIFY(loadProgressChangedSpy.count() >= 1); + + QCOMPARE(webView()->loadProgress(), 100); +} + +void tst_QQuickWebView::show() +{ + // This should not crash. + m_window->show(); + QTest::qWait(200); + m_window->hide(); +} + +void tst_QQuickWebView::showWebView() +{ + webView()->setUrl(QUrl(getTestFilePath("direct-image-compositing.html"))); + QVERIFY(waitForLoadSucceeded(webView())); + m_window->show(); + // This should not crash. + webView()->setVisible(true); + QTest::qWait(200); + webView()->setVisible(false); + QTest::qWait(200); +} + +void tst_QQuickWebView::removeFromCanvas() +{ + showWebView(); + + // This should not crash. + QQuickItem *parent = webView()->parentItem(); + QQuickItem noCanvasItem; + webView()->setParentItem(&noCanvasItem); + QTest::qWait(200); + webView()->setParentItem(parent); + webView()->setVisible(true); + QTest::qWait(200); +} + +void tst_QQuickWebView::multipleWebViewWindows() +{ + showWebView(); + + // This should not crash. + QQuickWebView *webView1 = newWebView(); + QScopedPointer<TestWindow> window1(new TestWindow(webView1)); + QQuickWebView *webView2 = newWebView(); + QScopedPointer<TestWindow> window2(new TestWindow(webView2)); + + webView1->setUrl(QUrl(getTestFilePath("scroll.html"))); + QVERIFY(waitForLoadSucceeded(webView1)); + window1->show(); + webView1->setVisible(true); + + webView2->setUrl(QUrl(getTestFilePath("basic_page.html"))); + QVERIFY(waitForLoadSucceeded(webView2)); + window2->show(); + webView2->setVisible(true); + QTest::qWait(200); +} + +void tst_QQuickWebView::multipleWebViews() +{ + showWebView(); + + // This should not crash. + QScopedPointer<QQuickWebView> webView1(newWebView()); + webView1->setParentItem(m_window->contentItem()); + QScopedPointer<QQuickWebView> webView2(newWebView()); + webView2->setParentItem(m_window->contentItem()); + + webView1->setSize(QSizeF(300, 400)); + webView1->setUrl(QUrl(getTestFilePath("scroll.html"))); + QVERIFY(waitForLoadSucceeded(webView1.data())); + webView1->setVisible(true); + + webView2->setSize(QSizeF(300, 400)); + webView2->setUrl(QUrl(getTestFilePath("basic_page.html"))); + QVERIFY(waitForLoadSucceeded(webView2.data())); + webView2->setVisible(true); + QTest::qWait(200); +} + +void tst_QQuickWebView::titleUpdate() +{ + QSignalSpy titleSpy(webView(), SIGNAL(titleChanged())); + + // Load page with no title + webView()->setUrl(QUrl(getTestFilePath("basic_page2.html"))); + QVERIFY(waitForLoadSucceeded(webView())); + QCOMPARE(titleSpy.size(), 1); + + titleSpy.clear(); + + // No titleChanged signal for failed load + webView()->setUrl(QUrl(getTestFilePath("file_that_does_not_exist.html"))); + QVERIFY(waitForLoadFailed(webView())); + QCOMPARE(titleSpy.size(), 0); + +} + +QTEST_MAIN(tst_QQuickWebView) +#include "tst_qquickwebview.moc" diff --git a/tests/auto/qml/shared/qt_webview_quicktest.h b/tests/auto/qml/shared/qt_webview_quicktest.h new file mode 100644 index 0000000..2881b34 --- /dev/null +++ b/tests/auto/qml/shared/qt_webview_quicktest.h @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the QtWebView module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** 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. +** +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QT_WEBVIEW_QUICKTEST_H +#define QT_WEBVIEW_QUICKTEST_H + +#include <QtQuickTest/quicktestglobal.h> + +#ifdef QT_WIDGETS_LIB +#include <QtWidgets/QApplication> +#else +#include <QtGui/QGuiApplication> +#endif + +QT_BEGIN_NAMESPACE + +#ifndef QUICK_TEST_SOURCE_DIR +#define QUICK_TEST_SOURCE_DIR 0 +#endif + +#ifdef QT_WIDGETS_LIB +#define Application QApplication +#else +#define Application QGuiApplication +#endif + +QT_END_NAMESPACE + +#endif // QT_WEBVIEW_QUICKTEST_H diff --git a/tests/auto/qml/shared/testwindow.h b/tests/auto/qml/shared/testwindow.h new file mode 100644 index 0000000..738f9cf --- /dev/null +++ b/tests/auto/qml/shared/testwindow.h @@ -0,0 +1,59 @@ +/* + Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef TESTWINDOW_H +#define TESTWINDOW_H + +#if 0 +#pragma qt_no_master_include +#endif + +#include <QResizeEvent> +#include <QScopedPointer> +#include <QtQuick/qquickitem.h> +#include <QtQuick/qquickview.h> + +// TestWindow: Utility class to ignore QQuickView details. +class TestWindow : public QQuickView { +public: + inline TestWindow(QQuickItem *webView); + QScopedPointer<QQuickItem> webView; + +protected: + inline void resizeEvent(QResizeEvent*); +}; + +inline TestWindow::TestWindow(QQuickItem *webView) + : webView(webView) +{ + Q_ASSERT(webView); + webView->setParentItem(contentItem()); + resize(300, 400); +} + +inline void TestWindow::resizeEvent(QResizeEvent *event) +{ + QQuickView::resizeEvent(event); + webView->setX(0); + webView->setY(0); + webView->setWidth(event->size().width()); + webView->setHeight(event->size().height()); +} + +#endif /* TESTWINDOW_H */ diff --git a/tests/auto/qml/shared/util.h b/tests/auto/qml/shared/util.h new file mode 100644 index 0000000..39dbafa --- /dev/null +++ b/tests/auto/qml/shared/util.h @@ -0,0 +1,120 @@ +/* + Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef UTIL_H +#define UTIL_H + +#include <QEventLoop> +#include <QSignalSpy> +#include <QTimer> +#include <QtTest/QtTest> +#include <QtWebView/qquickwebview.h> +#include <QtWebView/qquickwebviewloadrequest.h> + +#if !defined(TESTS_SOURCE_DIR) +#define TESTS_SOURCE_DIR "" +#endif + +class LoadSpy : public QEventLoop { + Q_OBJECT + +public: + LoadSpy(QQuickWebView *webView) + { + connect(webView, SIGNAL(loadingChanged(QQuickWebViewLoadRequest*)), SLOT(onLoadingChanged(QQuickWebViewLoadRequest*))); + } + + ~LoadSpy() { } + +Q_SIGNALS: + void loadSucceeded(); + void loadFailed(); + +private Q_SLOTS: + void onLoadingChanged(QQuickWebViewLoadRequest *loadRequest) + { + if (loadRequest->status() == QQuickWebView::LoadSucceededStatus) + emit loadSucceeded(); + else if (loadRequest->status() == QQuickWebView::LoadFailedStatus) + emit loadFailed(); + } +}; + +class LoadStartedCatcher : public QObject { + Q_OBJECT + +public: + LoadStartedCatcher(QQuickWebView *webView) + : m_webView(webView) + { + connect(m_webView, SIGNAL(loadingChanged(QQuickWebViewLoadRequest*)), this, SLOT(onLoadingChanged(QQuickWebViewLoadRequest*))); + } + + virtual ~LoadStartedCatcher() { } + +public Q_SLOTS: + void onLoadingChanged(QQuickWebViewLoadRequest *loadRequest) + { + if (loadRequest->status() == QQuickWebView::LoadStartedStatus) + QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); + } + +Q_SIGNALS: + void finished(); + +private: + QQuickWebView *m_webView; +}; + +/** + * 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 + */ +inline bool waitForSignal(QObject *obj, const char *signal, int timeout = 10000) +{ + 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(); +} + +inline bool waitForLoadSucceeded(QQuickWebView *webView, int timeout = 10000) +{ + LoadSpy loadSpy(webView); + return waitForSignal(&loadSpy, SIGNAL(loadSucceeded()), timeout); +} + +inline bool waitForLoadFailed(QQuickWebView *webView, int timeout = 10000) +{ + LoadSpy loadSpy(webView); + return waitForSignal(&loadSpy, SIGNAL(loadFailed()), timeout); +} + +#endif /* UTIL_H */ diff --git a/tests/auto/qml/tests.pri b/tests/auto/qml/tests.pri new file mode 100644 index 0000000..e23f416 --- /dev/null +++ b/tests/auto/qml/tests.pri @@ -0,0 +1,22 @@ +TEMPLATE = app + +# FIXME: Re-enable once we want to run tests on the CI +# CONFIG += testcase + +VPATH += $$_PRO_FILE_PWD_ +TARGET = tst_$$TARGET + +SOURCES += $${TARGET}.cpp +INCLUDEPATH += \ + $$PWD \ + ../shared + +QT += testlib network quick webview-private + +osx: CONFIG -= app_bundle + +DISTFILES += \ + $$PWD/qquickwebview/dummy.qml + +RESOURCES += \ + $$PWD/html/testdata.qrc |