diff options
author | Michal Klocek <michal.klocek@qt.io> | 2018-04-12 10:48:38 +0200 |
---|---|---|
committer | Michal Klocek <michal.klocek@qt.io> | 2018-07-03 08:03:35 +0000 |
commit | 89bc70bf1389bdbc2133ac58b0f0b60aae167c25 (patch) | |
tree | 979e062b9a96dfa38c90793772a6b782dbeb3b69 | |
parent | 0c2091e2a30d22b57fbef31f852794975d71961f (diff) |
Add tst_loadsignals
Because of the recent problems with unexpected or missing loadStarted
and loadFinished signals (which sometimes interfere with other load's
signals), we add a set of auto-tests for load-signals.
Done-By: Viktor Engelmann <viktor.engelmann@qt.io>
Change-Id: Ia3b65c7b3c4d1ed44e94f409e047db6c6defb821
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
-rw-r--r-- | tests/auto/widgets/loadsignals/BLACKLIST | 14 | ||||
-rw-r--r-- | tests/auto/widgets/loadsignals/loadsignals.pro | 1 | ||||
-rw-r--r-- | tests/auto/widgets/loadsignals/resources/downloadable.tar.gz | bin | 0 -> 131 bytes | |||
-rw-r--r-- | tests/auto/widgets/loadsignals/resources/page1.html | 8 | ||||
-rw-r--r-- | tests/auto/widgets/loadsignals/resources/page2.html | 14 | ||||
-rw-r--r-- | tests/auto/widgets/loadsignals/resources/page3.html | 20 | ||||
-rw-r--r-- | tests/auto/widgets/loadsignals/resources/page4.html | 8 | ||||
-rw-r--r-- | tests/auto/widgets/loadsignals/tst_loadsignals.cpp | 264 | ||||
-rw-r--r-- | tests/auto/widgets/loadsignals/tst_loadsignals.qrc | 9 | ||||
-rw-r--r-- | tests/auto/widgets/widgets.pro | 3 |
10 files changed, 340 insertions, 1 deletions
diff --git a/tests/auto/widgets/loadsignals/BLACKLIST b/tests/auto/widgets/loadsignals/BLACKLIST new file mode 100644 index 000000000..570666a83 --- /dev/null +++ b/tests/auto/widgets/loadsignals/BLACKLIST @@ -0,0 +1,14 @@ +[secondLoadForError_WhenErrorPageEnabled:ErrorPageEnabled] +* + +# QTBUG-65223 +[loadStartedAndFinishedCount:WithAnchorClickedFromJS] +* + +# QTBUG-66869 (https://codereview.qt-project.org/#/c/222112/ is only a workaround) +[loadAfterInPageNavigation_qtbug66869] +* + +# QTBUG-66661 +[fileDownloadDoesNotTriggerLoadSignals_qtbug66661] +* diff --git a/tests/auto/widgets/loadsignals/loadsignals.pro b/tests/auto/widgets/loadsignals/loadsignals.pro new file mode 100644 index 000000000..e99c7f493 --- /dev/null +++ b/tests/auto/widgets/loadsignals/loadsignals.pro @@ -0,0 +1 @@ +include(../tests.pri) diff --git a/tests/auto/widgets/loadsignals/resources/downloadable.tar.gz b/tests/auto/widgets/loadsignals/resources/downloadable.tar.gz Binary files differnew file mode 100644 index 000000000..741cb8ca6 --- /dev/null +++ b/tests/auto/widgets/loadsignals/resources/downloadable.tar.gz diff --git a/tests/auto/widgets/loadsignals/resources/page1.html b/tests/auto/widgets/loadsignals/resources/page1.html new file mode 100644 index 000000000..5cd479ab6 --- /dev/null +++ b/tests/auto/widgets/loadsignals/resources/page1.html @@ -0,0 +1,8 @@ +<html> + <head> + <title>page1</title> + </head> + <body> + <h1>page1</h1> + </body> +</html> diff --git a/tests/auto/widgets/loadsignals/resources/page2.html b/tests/auto/widgets/loadsignals/resources/page2.html new file mode 100644 index 000000000..e3031f56a --- /dev/null +++ b/tests/auto/widgets/loadsignals/resources/page2.html @@ -0,0 +1,14 @@ +<html> + <head> + <title>page2</title> + </head> + <style> + .fardown { + position: absolute; + top: 2500px; + } + </style> + <body> + <div class="fardown" id="anchor">page2 anchor</div> + </body> +</html> diff --git a/tests/auto/widgets/loadsignals/resources/page3.html b/tests/auto/widgets/loadsignals/resources/page3.html new file mode 100644 index 000000000..d38ca31f0 --- /dev/null +++ b/tests/auto/widgets/loadsignals/resources/page3.html @@ -0,0 +1,20 @@ +<html> + <head> + <title>page3</title> + </head> + <script> + setTimeout(function(){ + document.getElementById('anchorLink').click(); + },500); + </script> + <style> + .fardown { + position: absolute; + top: 2500px; + } + </style> + <body> + <div><a id="anchorLink" href="#anchor">page3</a></div> + <div class="fardown" id="anchor">page3 anchor</div> + </body> +</html> diff --git a/tests/auto/widgets/loadsignals/resources/page4.html b/tests/auto/widgets/loadsignals/resources/page4.html new file mode 100644 index 000000000..61976b4fb --- /dev/null +++ b/tests/auto/widgets/loadsignals/resources/page4.html @@ -0,0 +1,8 @@ +<html> + <head> + <title>page4</title> + </head> + <body onload="document.getElementById('downloadLink').focus();"> + <a id="downloadLink" href="downloadable.tar.gz">download</a> + </body> +</html> diff --git a/tests/auto/widgets/loadsignals/tst_loadsignals.cpp b/tests/auto/widgets/loadsignals/tst_loadsignals.cpp new file mode 100644 index 000000000..e614f3751 --- /dev/null +++ b/tests/auto/widgets/loadsignals/tst_loadsignals.cpp @@ -0,0 +1,264 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** 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 https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/QtTest> + +#include "../util.h" +#include "qdebug.h" +#include "qwebenginepage.h" +#include "qwebengineprofile.h" +#include "qwebenginesettings.h" +#include "qwebengineview.h" + +class tst_LoadSignals : public QObject +{ + Q_OBJECT + +public: + tst_LoadSignals(); + virtual ~tst_LoadSignals(); + +public Q_SLOTS: + void initTestCase(); + void init(); + void cleanup(); + +private Q_SLOTS: + void monotonicity(); + void loadStartedAndFinishedCount_data(); + void loadStartedAndFinishedCount(); + void secondLoadForError_WhenErrorPageEnabled_data(); + void secondLoadForError_WhenErrorPageEnabled(); + void loadAfterInPageNavigation_qtbug66869(); + void fileDownloadDoesNotTriggerLoadSignals_qtbug66661(); + +private: + QWebEngineView* view; + QScopedPointer<QSignalSpy> loadStartedSpy; + QScopedPointer<QSignalSpy> loadProgressSpy; + QScopedPointer<QSignalSpy> loadFinishedSpy; +}; + +tst_LoadSignals::tst_LoadSignals() +{ +} + +tst_LoadSignals::~tst_LoadSignals() +{ +} + +void tst_LoadSignals::initTestCase() +{ +} + +void tst_LoadSignals::init() +{ + view = new QWebEngineView(); + view->resize(1024,768); + view->show(); + loadStartedSpy.reset(new QSignalSpy(view->page(), &QWebEnginePage::loadStarted)); + loadProgressSpy.reset(new QSignalSpy(view->page(), &QWebEnginePage::loadProgress)); + loadFinishedSpy.reset(new QSignalSpy(view->page(), &QWebEnginePage::loadFinished)); +} + +void tst_LoadSignals::cleanup() +{ + loadFinishedSpy.reset(); + loadProgressSpy.reset(); + loadStartedSpy.reset(); + delete view; +} + +/** + * Test that we get the expected number of loadStarted and loadFinished signals + */ +void tst_LoadSignals::loadStartedAndFinishedCount_data() +{ + QTest::addColumn<QUrl>("url"); + QTest::addColumn<int>("expectedLoadCount"); + QTest::newRow("Normal") << QUrl("qrc:///resources/page1.html") << 1; + QTest::newRow("WithAnchor") << QUrl("qrc:///resources/page2.html#anchor") << 1; + + // In this case, we get an unexpected additional loadStarted, but no corresponding + // loadFinished, so expectedLoadCount=2 would also not work. See also QTBUG-65223 + QTest::newRow("WithAnchorClickedFromJS") << QUrl("qrc:///resources/page3.html") << 1; +} + +void tst_LoadSignals::loadStartedAndFinishedCount() +{ + QFETCH(QUrl, url); + QFETCH(int, expectedLoadCount); + + view->load(url); + QTRY_COMPARE(loadFinishedSpy->size(), expectedLoadCount); + bool loadSucceeded = (*loadFinishedSpy)[0][0].toBool(); + QVERIFY(loadSucceeded); + + // Wait for 10 seconds (abort waiting if another loadStarted or loadFinished occurs) + QTRY_LOOP_IMPL((loadStartedSpy->size() != expectedLoadCount) + || (loadFinishedSpy->size() != expectedLoadCount), 10000, 100); + + // No further loadStarted should have occurred within this time + QCOMPARE(loadStartedSpy->size(), expectedLoadCount); + QCOMPARE(loadFinishedSpy->size(), expectedLoadCount); +} + +/** + * Test monotonicity of loadProgress signals + */ +void tst_LoadSignals::monotonicity() +{ + view->load(QUrl("qrc:///resources/page1.html")); + QTRY_COMPARE(loadFinishedSpy->size(), 1); + bool loadSucceeded = (*loadFinishedSpy)[0][0].toBool(); + QVERIFY(loadSucceeded); + + // first loadProgress should have 0% progress + QCOMPARE(loadProgressSpy->first()[0].toInt(), 0); + + // every loadProgress should have at least as much progress as the one before + int progress = 0; + for (auto item : *loadProgressSpy) { + QVERIFY(item[0].toInt() >= progress); + progress = item[0].toInt(); + } + + // last loadProgress should have 100% progress + QCOMPARE(loadProgressSpy->last()[0].toInt(), 100); +} + +/** + * Test that we get a second loadStarted and loadFinished signal + * for error-pages (unless error-pages are disabled) + */ +void tst_LoadSignals::secondLoadForError_WhenErrorPageEnabled_data() +{ + QTest::addColumn<bool>("enabled"); + // in this case, we get no second loadStarted and loadFinished, although we had + // agreed on making the navigation to an error page an individual load + QTest::newRow("ErrorPageEnabled") << true; + QTest::newRow("ErrorPageDisabled") << false; +} + +void tst_LoadSignals::secondLoadForError_WhenErrorPageEnabled() +{ + QFETCH(bool, enabled); + view->settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, enabled); + int expectedLoadCount = (enabled ? 2 : 1); + + // RFC 2606 guarantees that this will never become a valid domain + view->load(QUrl("http://nonexistent.invalid")); + QTRY_COMPARE_WITH_TIMEOUT(loadFinishedSpy->size(), expectedLoadCount, 10000); + bool loadSucceeded = (*loadFinishedSpy)[0][0].toBool(); + QVERIFY(!loadSucceeded); + if (enabled) { + bool errorPageLoadSucceeded = (*loadFinishedSpy)[1][0].toBool(); + QVERIFY(errorPageLoadSucceeded); + } + + // Wait for 10 seconds (abort waiting if another loadStarted or loadFinished occurs) + QTRY_LOOP_IMPL((loadStartedSpy->size() != expectedLoadCount) + || (loadFinishedSpy->size() != expectedLoadCount), 10000, 100); + + // No further loadStarted should have occurred within this time + QCOMPARE(loadStartedSpy->size(), expectedLoadCount); + QCOMPARE(loadFinishedSpy->size(), expectedLoadCount); +} + +/** + * Test that a second load after an in-page navigation receives its expected loadStarted and + * loadFinished signal. + */ +void tst_LoadSignals::loadAfterInPageNavigation_qtbug66869() +{ + view->load(QUrl("qrc:///resources/page3.html")); + QTRY_COMPARE(loadFinishedSpy->size(), 1); + bool loadSucceeded = (*loadFinishedSpy)[0][0].toBool(); + QVERIFY(loadSucceeded); + + // page3 does an in-page navigation after 500ms + QTest::qWait(2000); + loadFinishedSpy->clear(); + loadProgressSpy->clear(); + loadStartedSpy->clear(); + + // second load + view->load(QUrl("qrc:///resources/page1.html")); + QTRY_COMPARE(loadFinishedSpy->size(), 1); + loadSucceeded = (*loadFinishedSpy)[0][0].toBool(); + QVERIFY(loadSucceeded); + // loadStarted and loadFinished should have been signalled + QCOMPARE(loadStartedSpy->size(), 1); + + // reminder that we still need to solve the core issue + QFAIL("https://codereview.qt-project.org/#/c/222112/ only hides the symptom, the core issue still needs to be solved"); +} + +/** + * Test that file-downloads don't trigger loadStarted or loadFinished signals. + * See QTBUG-66661 + */ +void tst_LoadSignals::fileDownloadDoesNotTriggerLoadSignals_qtbug66661() +{ + view->load(QUrl("qrc:///resources/page4.html")); + QTRY_COMPARE(loadFinishedSpy->size(), 1); + bool loadSucceeded = (*loadFinishedSpy)[0][0].toBool(); + QVERIFY(loadSucceeded); + + // allow the download + QTemporaryDir tempDir; + QWebEngineDownloadItem::DownloadState downloadState = QWebEngineDownloadItem::DownloadRequested; + connect(view->page()->profile(), &QWebEngineProfile::downloadRequested, + [this, &downloadState, &tempDir](QWebEngineDownloadItem* item){ + connect(item, &QWebEngineDownloadItem::stateChanged, [&downloadState](QWebEngineDownloadItem::DownloadState newState){ + downloadState = newState; + }); + item->setPath(tempDir.filePath(QFileInfo(item->path()).fileName())); + item->accept(); + }); + + // trigger the download link that becomes focused on page4 + QTest::qWait(1000); + QTest::sendKeyEvent(QTest::Press, view->focusProxy(), Qt::Key_Return, QString("\r"), Qt::NoModifier); + QTest::sendKeyEvent(QTest::Release, view->focusProxy(), Qt::Key_Return, QString("\r"), Qt::NoModifier); + + // Wait for 10 seconds (abort waiting if another loadStarted or loadFinished occurs) + QTRY_LOOP_IMPL((loadStartedSpy->size() != 1) + || (loadFinishedSpy->size() != 1), 10000, 100); + + // Download must have occurred + QTRY_COMPARE(downloadState, QWebEngineDownloadItem::DownloadCompleted); + + // No further loadStarted should have occurred within this time + QCOMPARE(loadStartedSpy->size(), 1); + QCOMPARE(loadFinishedSpy->size(), 1); +} + + +QTEST_MAIN(tst_LoadSignals) +#include "tst_loadsignals.moc" diff --git a/tests/auto/widgets/loadsignals/tst_loadsignals.qrc b/tests/auto/widgets/loadsignals/tst_loadsignals.qrc new file mode 100644 index 000000000..316deecb8 --- /dev/null +++ b/tests/auto/widgets/loadsignals/tst_loadsignals.qrc @@ -0,0 +1,9 @@ +<RCC> + <qresource prefix="/"> + <file>resources/page1.html</file> + <file>resources/page2.html</file> + <file>resources/page3.html</file> + <file>resources/page4.html</file> + <file>resources/downloadable.tar.gz</file> + </qresource> +</RCC> diff --git a/tests/auto/widgets/widgets.pro b/tests/auto/widgets/widgets.pro index 778795743..7728a7a51 100644 --- a/tests/auto/widgets/widgets.pro +++ b/tests/auto/widgets/widgets.pro @@ -5,6 +5,7 @@ TEMPLATE = subdirs SUBDIRS += \ origins \ + loadsignals \ qwebenginedefaultsurfaceformat \ qwebenginedownloads \ qwebenginefaviconmanager \ @@ -35,4 +36,4 @@ boot2qt: SUBDIRS -= qwebengineaccessibility qwebenginedefaultsurfaceformat \ qwebenginefaviconmanager qwebenginepage qwebenginehistory \ qwebengineprofile qwebengineschemes qwebenginescript \ qwebengineview qwebenginedownloads qwebenginesettings \ - origins + origins loadsignals |