1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
/****************************************************************************
**
** 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 "httpserver.h"
#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 Q_SLOTS:
void initTestCase();
void init();
private Q_SLOTS:
void monotonicity();
void loadStartedAndFinishedCount_data();
void loadStartedAndFinishedCount();
void secondLoadForError_WhenErrorPageEnabled_data();
void secondLoadForError_WhenErrorPageEnabled();
void loadAfterInPageNavigation_qtbug66869();
void fileDownloadDoesNotTriggerLoadSignals_qtbug66661();
private:
QWebEngineProfile profile;
QWebEnginePage page{&profile};
QWebEngineView view;
QSignalSpy loadStartedSpy{&page, &QWebEnginePage::loadStarted};
QSignalSpy loadProgressSpy{&page, &QWebEnginePage::loadProgress};
QSignalSpy loadFinishedSpy{&page, &QWebEnginePage::loadFinished};
};
void tst_LoadSignals::initTestCase()
{
view.setPage(&page);
view.resize(1024,768);
view.show();
}
void tst_LoadSignals::init()
{
loadStartedSpy.clear();
loadProgressSpy.clear();
loadFinishedSpy.clear();
}
/**
* 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);
QVERIFY(loadFinishedSpy[0][0].toBool());
// 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()
{
HttpServer server;
server.setResourceDirs({ TESTS_SHARED_DATA_DIR });
connect(&server, &HttpServer::newRequest, [] (HttpReqRep *) {
QTest::qWait(250); // just add delay to trigger some progress for every sub resource
});
QVERIFY(server.start());
view.load(server.url("/loadprogress/main.html"));
QTRY_COMPARE(loadFinishedSpy.size(), 1);
QVERIFY(loadFinishedSpy[0][0].toBool());
// first loadProgress should have 0% progress
QCOMPARE(loadProgressSpy.takeFirst()[0].toInt(), 0);
// every loadProgress should have more progress than the one before
int progress = 0;
for (const auto &item : loadProgressSpy) {
QVERIFY(progress < item[0].toInt());
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);
QVERIFY(!loadFinishedSpy[0][0].toBool());
if (enabled)
QVERIFY(loadFinishedSpy[1][0].toBool());
// 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);
QVERIFY(loadFinishedSpy[0][0].toBool());
// 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);
QVERIFY(loadFinishedSpy[0][0].toBool());
// 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);
QVERIFY(loadFinishedSpy[0][0].toBool());
// allow the download
QTemporaryDir tempDir;
QVERIFY(tempDir.isValid());
QWebEngineDownloadItem::DownloadState downloadState = QWebEngineDownloadItem::DownloadRequested;
ScopedConnection sc1 =
connect(&profile, &QWebEngineProfile::downloadRequested,
[&downloadState, &tempDir](QWebEngineDownloadItem *item) {
connect(item, &QWebEngineDownloadItem::stateChanged,
[&downloadState](QWebEngineDownloadItem::DownloadState newState) {
downloadState = newState;
});
item->setDownloadDirectory(tempDir.path());
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"
|